create table student
(sno varchar(20),
sname varchar(20),
sgender varchar(10),
sage int,
sdept varchar(20))
insert into student values('1222','张丽','女',21,'计算机'),('1223','刘立','男',25,'数学'),('1224','王晓华','女',22,'计算机'),('1225','李佳','男',23,'计算机'),('1226','张月','女',22,'建筑')
select *
from student
create table course(cno varchar(20),cname varchar(20),cpno varchar(20),credit int)
insert into course values('0001','数据库','0002',4),
('0002','数据结构','0003',4),
('0003','C语言',null,4),
('0004','数学',null,4),
('0005','生物学导论',null,4),('0006','计算机网络',null,4),('0007','网络安全',null,4)
select *
from course
create table sc(sno varchar(20),cno varchar(20),grade int)
insert into sc values('1222','0001',95),('1222','0002',80),('1222','0003',92),('1222','0004',90),('1223','0004',85),('1223','0002',Null),('1224','0001',93),('1225','0001',87),('1226','0005',Null),('1224','0002',95)
select *
from sc
alter table student
add constraint pk primary key(sno)
alter table course
add constraint pkc primary key(cno)
alter table sc
add constraint psc primary key(sno,cno)
select *
from studentinsert into student values('1227','李娜','女',20,'数学')
insert into student values('1227','李娜','女',20,'数学')
#出错原因是主键重复
insert into sc values('001','005',100)
select *
from sc
delete from sc
where sno='001'
select *
from sc
alter table sc
add constraint fk foreign key(sno) references student(sno)
on delete cascade
on update cascade
#drop table sc
insert into sc values('001','005',100)insert into sc values('1223','005',100)select *
from sc
select *
from studentselect *
from scdelete from student
where sno='1223'
update student
set sno='1224a'
where sno='1224'
select *
from student
select *
from sc
insert into sc values('1225','0004',95)
insert into sc values('1280','0004',95)
update sc
set sno = '1280'
where sno = '1225'
select *
from studentselect *
from scupdate student
set sno ='1224'
where sno = '1224a'
delete from student
where sno = '1224'select *
from studentselect *
from sc
insert into sc values('1226','0001',90)select *
from scinsert into sc values('1226','AAAA',90) [违反了外键约束]
delete from course
where cno='0001'select *
from course
select *
from sc
delete from sc
where cno='AAAA'
select *
from sc
delete from sc
where cno = '0001'select *
from sc
alter table scadd constraint fc foreign key(cno) references course(cno)on delete cascadeon update cascade
insert into sc values('1226','AAAA',90)
#拒绝插入
update course
set cno ='0002a'
where cno ='0002'select *
from course
select *
from sc
delete from course
where cno = '0002a'
select *
from course
select *
from sc
declare mesg varchar(10);
if new.sgender not in ('男','女') thenset mesg='error ssex';signal sqlstate 'HY000' set message_text=mesg;
END if;
insert into student values('1230','网络','女',25,'建筑')
select *
from student
insert into student values('1231','网络','未知',25,'建筑')
BEGIN
declare mesg varchar(100);
if new.grade not between 0 and 100 thenset mesg="error grade";signal sqlstate 'HY000' set message_text=mesg;
end if;
insert into sc values('1227','0001',90)
select *
from sc
insert into sc values('1227','0002',900)
上一篇:linux centos7虚拟机安装mysql5.7详细教程(tar包安装)
下一篇:Leetcode_C++之238. Product of Array Except Self(除本身元素之外数组其他元素的积)