略
在父表上update/delete记录时,同步update/delete掉子表的匹配记录
在父表上update/delete记录时,将子表上匹配记录的列设为null(要注意子表的外键列不能为not null)
如果子表中有匹配的记录,则不允许对父表对应候选键进行update/delete操作
同no action,,都是立即检查外键约束
CREATE TABLE s (
Sno INT ,
Sname CHAR (20) unique,
Sgender CHAR (5)
check (sgender in ('男','女'))
default '男',
Birth CHAR (20) not null,
Sdept CHAR (5),
PRIMARY KEY (Sno)
);
CREATE TABLE c (
Cno INT ,
Cname CHAR (20),
Cpno INT,
Credit INT,
PRIMARY KEY (Cno),
FOREIGN KEY (Cpno) REFERENCES c(Cno)
);
CREATE TABLE sc (
id int not null auto_increment,
Sno INT ,
Cno INT ,
Grade INT
check (grade between 0 and 100),
PRIMARY KEY (id),
FOREIGN KEY (Sno) REFERENCES s(Sno),
FOREIGN KEY (Cno) REFERENCES c(Cno)
);
alter table s add constraint c2 check(sgender in ('男','女'));
alter table s drop constraint c2;
alter table sc drop foreign key sc_ibfk_1;
alter table sc drop foreign key sc_ibfk_2;
alter table s add tname char(20);
alter table s add constraint c1
check (char_length(tname) >= 8 and tname like '^[A-Za-z]+$');
略
要求:创建相关用户和指定的数据库表,针对每个小题先完成授权并进行权限验证,然后再收回权限并进行权限验证。
create user 'wangming'@'localhost';
grant select on table apartment to 'wangming'@'localhost';
grant select on table employee to 'wangming'@'localhost';
revoke select on table apartment from 'wangming'@'localhost';
revoke select on table employee from 'wangming'@'localhost';
create user 'liyong'@'localhost';
grant insert,delete on table apartment to 'liyong'@'localhost';
grant insert,delete on table employee to 'liyong'@'localhost';
revoke insert,delete on table apartment from 'liyong'@'localhost';
revoke insert,delete on table employee from 'liyong'@'localhost';
create view view_empoyee
as select * from employee
where concat(name,'@localhost') = user();
grant select on view_empoyee to 'wangming'@'localhost' , 'liyong'@'localhost' , 'liuxing'@'localhost' , 'zhangxin'@'localhost' , 'zhouping'@'localhost' , 'yanglan'@'localhost';
revoke all on view_empoyee from 'wangming'@'localhost' , 'liyong'@'localhost' , 'liuxing'@'localhost' , 'zhangxin'@'localhost' , 'zhouping'@'localhost' , 'yanglan'@'localhost';
show grants for 'yanglan'@'localhost';
create user 'liuxing'@'localhost';
grant select on table employee to 'liuxing'@'localhost';
grant update(salary) on table employee to 'liuxing'@'localhost';
show grants for 'liuxing'@'localhost';
revoke all on table employee from 'liuxing'@'localhost';
show grants for 'liuxing'@'localhost';
create user 'zhangxin'@'localhost';
grant alter on table apartment to 'zhangxin'@'localhost';
grant alter on table employee to 'zhangxin'@'localhost';
revoke alter on table apartment from 'zhangxin'@'localhost';
revoke alter on table employee from 'zhangxin'@'localhost';
grant all on table apartment to 'zhouping'@'localhost' with grant option;
grant all on table employee to 'zhouping'@'localhost' with grant option;
show grants for 'zhouping'@'localhost';
revoke all on table apartment from 'zhouping'@'localhost';
revoke all on table employee from 'zhouping'@'localhost';
show grants for 'zhouping'@'localhost';
create view message_yanglan
as select max(salary),min(salary),avg(salary) from employee
group by ano;
create user 'yanglan'@'localhost';
grant select on message_yanglan to 'yanglan'@'localhost';
show grants for 'yanglan'@'localhost';
revoke all on message_yanglan from 'yanglan'@'localhost';
show grants for 'yanglan'@'localhost';
提示:
create user 'wangming'@'localhost' identified by '123456';
Show grants for ‘wangming’@’localhost’
对于完整性约束而言,在用户图形界面设置较为方便,使用命令行则相对比较复杂。
对于不同用户权限设置,在设置权限后难切换用户验证较为复杂,使用show grants可以看到某用户所有的权限,作为验证方法较为简便。