MySQL
是一种快速易用的关系型数据库管理系统(RDBMS)
,不少企业都在使用它来构建本身的数据库。python
MySQL由一家瑞典公司MySQL AB
开发、运营并予以支持。它之因此很是流行,缘由在于具有如下这些优势:mysql
RDBMS术语程序员
术语 | 描述 |
---|---|
数据库(Database) |
数据库是带有相关数据的表的集合 |
表(Table) |
表是带有数据的矩阵。数据库中的表就像一种简单的电子表格 |
列(Column) |
每一列(数据元素)都包含着同种类型的数据,好比邮编 |
行(Row) |
行(又被称为元组、项或记录)是一组相关数据,好比有关订阅量的数据 |
冗余(Redundancy) |
存储两次数据,以便使系统更快速 |
主键(Primary Key) |
主键是惟一的。同一张表中不容许出现一样两个键值。一个键值只对应着一行 |
外键(Foreign Key) |
用于链接两张表 |
复合键(Compound Key) |
复合键(又称组合键)是一种由多列组成的键,由于一列并不足以肯定惟一性 |
索引(Index) |
它在数据库中的做用就像书后的索引同样 |
引用完性(Referential Integrity) |
用来确保外键一直指向已存在的一行 |
链接已经启动的Mysqlsql
mac@:~$ mysql -uroot -p9998877
数据库
查看当前的全部数据库bash
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | test | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
默认数据库 | 描述 |
---|---|
information_schema |
提供了访问数据库元数据的方式,元数据如数据库名或表名,列的数据类型或访问权限等 |
test |
用户用来测试的数据库库 |
mysql |
用户权限相关数据 |
performance_schema |
用于收集数据库服务器性能参数 |
sys |
包含了一系列视图、函数和存储过程 |
建立数据库服务器
-- 建立字符串为utf-8的数据库 CREATE DATABASE dbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
-- 建立字符串为gbk的数据库 CREATE DATABASE dbname DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
进入数据库session
use dbname;
删除数据库ide
drop database dbname;
用户相关函数
建立kongling用户,容许全部主机链接,密码设置为as
create user 'kongling'@'%' identified by 'as';
修改kongling
用户的用户名为as
rename user 'kongling'@'%' to 'konglingme'@'192.168.56.1';
修改anshengme
用户的密码为123
set password for 'kongling'@'192.168.56.1' = Password('123');
删除anshengme
用户
drop user 'konglingme'@'192.168.56.1';
用户权限相关数据保存在mysql数据库的user表中,因此也能够直接对其进行操做(不建议)
用户受权
-- 查看权限 show grants for 'root'@'localhost'; -- 受权 grant 权限 on 数据库.表 to '用户'@'IP地址'; -- 取消权限 revoke 权限 on 数据库.表 from '用户'@'IP地址';
建立表
create table 表名( 列名 类型 是否能够为空, 列名 类型 是否能够为空 )ENGINE=InnoDB DEFAULT CHARSET=utf8
是否能够为空
create table tb_name( `username_not` varchar(30) NOT NULL , -- 不可空 `username_null` varchar(30) NULL -- 可空 )ENGINE=InnoDB DEFAULT CHARSET=utf8
默认值,建立列时能够指定默认值,当插入数据时若是未主动设置,则自动添加默认值
create table tb_name( nid int not null defalut 2, num int not null )ENGINE=InnoDB DEFAULT CHARSET=utf8
自增,若是为某列设置自增列,插入数据时无需设置此列,默认将自增(表中只能有一个自增列)
create table tb_name( nid int not null auto_increment primary key, num int null )ENGINE=InnoDB DEFAULT CHARSET=utf8
或
create table tb_name( nid int not null auto_increment, num int null, index(nid) )ENGINE=InnoDB DEFAULT CHARSET=utf8
show session variables like 'auto_inc%'; set session auto_increment_increment=2; set session auto_increment_offset=10; shwo global variables like 'auto_inc%'; set global auto_increment_increment=2; set global auto_increment_offset=10;
主键,一种特殊的惟一索引,不容许有空值,若是主键使用单个列,则它的值必须惟一,若是是多列,则其组合必须惟一。
create table tb1( nid int not null auto_increment primary key, num int null )
或
create table tb1( nid int not null, num int not null, primary key(nid,num) )
外键,一个特殊的索引,只能是指定内容
create table color( nid int not null primary key, name char(16) not null )
create table fruit( nid int not null primary key, smt char(32) null , color_id int not null, constraint fk_cc foreign key (color_id) references color(nid) )
删除表
drop table tb_name;
清空表
-- 若是清空的表又自增列,那么在清空以后会继续上次自增的值继续自增 delete from tb_name; -- 若是清空的表又自增列,那么在清空以后再次添加数据自增的值会重新开始计算 truncate table tb_name;
修改表
-- 添加列 alter table 表名 add 列名 类型; -- 删除列 alter table 表名 drop column 列名; -- 修改列 alter table 表名 modify column 列名 类型; -- 类型 alter table 表名 change 原列名 新列名 类型; -- 列名,类型 -- 添加主键 alter table 表名 add primary key(列名); -- 删除主键 alter table 表名 drop primary key; alter table 表名 modify 列名 int, drop primary key; -- 添加外键 alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段); -- 删除外键 alter table 表名 drop foreign key 外键名称; -- 修改默认值 ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000; -- 删除默认值 ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;