设置在表头上,用来限制字段赋值
包括:
查看表的字段约束条件使用的命令:
mysql > desc db1.t1
建表的时候给表头设置默认值和不允许为空值
create table db1.t31(name char(10) not null,class char(7) default "xxx",likes set("money","game","film") not null default "film,music");
create table db1.t34 (name char(10),age tinyint); # 建表是没有设置默认值和不允许为null
desc db1.t34
alter table db1.t34 modify name char(10) not null default "" ,modify age tinyint unsigned not null default 25; # 修改字段不允许为null 设置默认值
查看表结构时 key 列包含
create table DB1.t43(name char(10),hz_id char(18) unique); # 身份证号唯一
mysql > insert into DB1.t43 values("bob",null)
mysql> create table db1.t35(name char(10),hz_id char(10) primary key ,class cahr(10));
语法格式二:
mysql> create table db1.t36(name char(10),hz_id char(10),class char(10),primary key(hz_id))
删除主键命令格式 向表头下存储数据不受主键的限制
alter table db1.t36 drop primary key;
添加主键标签
alter table db1.t36 add primary key(hz_id)
mysql> create table db1.t37(cip varchar(15),port smallint ,status enum("allow","deny"),primary key(cip,port))
删除复合主键
alter table db1.t37 drop primary key; # 不能单个删除
添加复合主建
alter table db1.t37 add primary key(cip,port);
主键使用总结:无论是一个表头做主键 还是多个表头做主键 约束的方式都是不允许给表赋重复的值和null值
create table db1.t38(行号 int primary key auto_increment ,姓名 char(10),班级 char(7),住址 char(10));
insert into db1.t39(姓名,班级,住址) values("bob","nsd2107","bg");
insert into db1.t39(姓名,班级,住址) values("bob","nsd2107","bg");
也可以自定义自增长 字段的值
自增长列 truncate后从1开始 delete继续编号
给已有表添加行号字段
alter table db1.t3 add id int primary key auto_increment first;
外键的核心思想:保证数据的一致性
插入记录时,字段值在另一个表字段值范围内选择
外键使用规则:
create table 库.表名(表头列表,
foreign key(表头名) # 指定外键
references 库.表(表头名) # 指定参考的表头
on update cascade # 同步更新
on delete cascade # 同步删除
)engine=innodb;
创建工资表
create table db2.gz(gz_id int ,pay float(7,2),
foreign key(gz_id) references yg(yg_id)
on update cascade on delete cascade
)engine=innodb;
查看存储引擎
删除外键 通过外键名称,删除表头的外键设置
alter table db2.gz drop FOREIGN KEY gz_idfk_1(外键名)
在已有表里添加外键
alter table 库.表 add foreign key(表头名) references 库.表(表头名) on update cascade on delete cascade;
给表头加了索引标签之后,会对表头下的数据生成排队信息保存
在表对应的文件里(表名.ibd)比如 给db1 库下t3表的表头加了索引
对应的存储文件是 /var/lib/mysql/db1/t3.ibd
优点:
create database if not exists home;
use home ;
create table tea4 (
id char(6) not null,
name varchar(6) not null,
age int(3) not null,
gender enum('boy','girl') default 'boy',
index(id),index(name)
);
查看新建tea4表的字段结构,可以发现两个非空索引字段的KEY标志为MUL:
mysql> DESC tea4;
+--------+--------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------------+------+-----+---------+-------+
| id | char(6) | NO | MUL | NULL | |
| name | varchar(6) | NO | MUL | NULL | |
| age | int(3) | NO | | NULL | |
| gender | enum('boy','girl') | YES | | boy | |
+--------+--------------------+------+-----+---------+-------+
查看索引的详细信息
//查看详细信息
mysql> SHOW INDEX FROM tea4\G
*************************** 1. row ***************************
Table: tea4
Non_unique: 1
Key_name: id
Seq_in_index: 1
Column_name: id
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE //使用B树算法
Comment:
Index_comment:
*************************** 2. row ***************************
Table: tea4
Non_unique: 1
Key_name: name //索引名称
Seq_in_index: 1
Column_name: name //字段名称
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
drop index id on home.tea4; # 删除name字段的索引
create index nianling on tea4(age); //针对指定字段创建索引
explain select * from db1.t3 where name='sshd';
给db1库下的t3表加索引名
create index xingming on db1.t3(name)
grant 权限列表 on 库名 to 用户名@"客户端地址" identified by "密码" [with grant option](可以用grant添加权限)
库名:表示方式:
权限列表:
用户名:添加用户时 自定义即可 存储在mysql库下user表的user字段下
客户端地址:网络中的哪些主机可以使用添加的用户连接数据库服务 表示的方式有:
with grant option 让添加的用户也可以使用grant命令再添加用户,但用户本身要对mysql库有insert权限
revoke 权限列表 on 库名 from 用户名@"客户端地址";
删除添加的用户
drop user 用户名@"客户端地址";
两台主机 50 数据库服务器 51客户端
mysql > grant all on *.* to dba007@"%" identified by "123qqq...a" with grant option;
# 查看已添加的用户权限
mysql > show grants for dba007@"%";
+---------------------------------------------------------------+
| Grants for dba007@% |
+---------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'dba007'@'%' WITH GRANT OPTION |
+---------------------------------------------------------------+
登陆的用户查看自己的访问权限
mysql> show grants;
//查看可以使用root用来连接的客户端地址
mysql> select host,user from mysql.user where user="root";
+-------------+------+
| host | user |
+-------------+------+
| 192.168.4.% | root | //192.168.4.0/24 网段所有主机
| localhost | root | //本机登录
+-------------+------+
2 rows in set (0.00 sec)
[root@host51 ~]# mysql -h192.168.4.50 -udba007 -p123qqq...a
mysql> grant all on *.* to root@"192.168.4.%" identified by "123qqq...a";
mysql> select user,host from mysql.user;
3. 撤销root从本机访问权限,然后撤销
[root@host50 ~]# mysql -hlocalhost -uroot -pNSD123...a
//查看登录用户信息
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
mysql> revoke all on *.* from root@"localhost";
mysql> show grants;
+--------------------------------------------------------------+
| Grants for root@localhost |
+--------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'root'@'localhost' |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+--------------------------------------------------------------+
2 rows in set (0.00 sec)
[root@dbsvr1 ~]# mysql -h192.168.4.50 -udba007 -p123qqq...A //dba007用户登录
//设置root用户本机登录的权限
mysql> grant all on *.* to root@"localhost" identified by "NSD123...a" with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> SHOW GRANTS FOR root@localhost; //查看权限
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION | //有了
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
//数据库管理员本机登录
[root@host50 ~]# mysql -hlocalhost -uroot -pNSD123...a
mysql> grant select,insert,update,delete on tarena.* to webuser@"%" identified by "123qqq...A";
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show grants for webuser@"%"; //查看webuser用户权限 ,对所有库表没有任何软件仅对tarena库有权限
+---------------------------------------------------------------------+
| Grants for webuser@% |
+---------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'webuser'@'%' |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `tarena`.* TO 'webuser'@'%' |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
//在50主机管理员登录
[root@host50 ~]# mysql -hlocalhost -uroot -pNSD123...a
mysql>
//撤销 webuser用户权限,使其仅有查询记录权限。
mysql> revoke insert,update,delete on tarena.* from webuser@"%";
Query OK, 0 rows affected (0.00 sec)
//查看webuser用户权限
mysql> show grants for webuser@"%";
+---------------------------------------------+
| Grants for webuser@% |
+---------------------------------------------+
| GRANT USAGE ON *.* TO 'webuser'@'%' |
| GRANT SELECT ON `tarena`.* TO 'webuser'@'%' | //只剩select权限了
+---------------------------------------------+
2 rows in set (0.00 sec)
//查看已有的授权用户 是否有dba007用户
mysql> select user , host from mysql.user where user="dba007";
+--------+------+
| user | host |
+--------+------+
| dba007 | % |
+--------+------+
1 row in set (0.00 sec)
//删除dba007用户
mysql> drop user dba007@"%";
Query OK, 0 rows affected (0.00 sec)
grant select on *.* to bob@"%" identified by "Ammm"
# 追加权限
grant insert on *.* to bob@'%'
授权库mysql库的使用:保存grant命令的执行结果 使用到了4张表 分别存储不同的授权信息
mysql> use mysql;
mysql> show tables;
可以通过查看表记录获取已有授权用户及访问权限
也可以修改表记录 修改授权用户的访问权限
查看当前数据库服务已有的用户
select host,user,authentication_string from mysql.user;
# user字段存储用户名
# host字段存储客户端地址
# authentication_string存储链接密码