create database spj
use spj
create table student
( sno char(9) primary key,
sname char(20) not null,
ssex char(2),
sage smallint,
sdept char(20)
);
create table sc
(sno char(9) not null,
cno char(4) not null,
grade smallint,
primary key (sno,cno)
) ;
create table course
(cno char(4) primary key,
cname char(40),
cpno char(4),
ccredit smallint,
foreign key (cpno) references course(cno)
);
create table sc1
(sno char(9) not null,
cno char(4) not null,
grade smallint,
primary key (sno,cno),
foreign key(sno) references student(sno),
foreign key(cno) references course(cno),
);
create table sc2
(sno char(9) not null,
cno char(4) not null,
grade smallint,
primary key (sno,cno),
foreign key(sno) references student(sno)
on delete cascade
on update cascade,
foreign key(cno) references course(cno)
on delete no action
on update cascade
);
create table dept
(deptno numeric(2),
dname char(9) unique,
location char(10),
primary key(deptno)
);
create table student1
(sno char(9) primary key,
sname char(8) not null,
ssex char(2) check (ssex in('男','女')),
sage smallint,
sdept char(20)
);
create table sc3
(sno char(9) not null,
cno char(4) not null,
grade smallint check (grade>=0 and grade<=100),
primary key(sno,cno),
foreign key (sno) references student1(sno),
foreign key (cno) references course(cno)
)
create table student2
(sno char(9),
sname char(8) not null,
ssex char(2),
sage smallint,
sdept char(20),
primary key (sno),
check(ssex='女'or sname not like'ms.%')
);
create table student3
(sno numeric(6),
constraint c1 check (sno between 90000 and 99999),
sname char(20)
constraint c2 not null,
sage numeric(3)
constraint c3 check(sage<30),
ssex char(2)
constraint c4 check (ssex in('男','女')),
constraint studentkey primary key (sno)
);
create table teacher
(eno numeric(4) primary key,
ename char(10),
job char(8),
sal numeric(7,2),
deduct numeric(7,2),
deptno numeric(2),
constraint empfkey foreign key(deptno) references dept(deptno),
constraint c5 check (sal+deduct >=300)
);
alter table student3
drop constraint c4;
alter table student3
drop constraint c1;
alter table student3
add constraint c1 check(sno between 900000 and 999999);
alter table student3
drop constraint c3;
alter table student3
add constraint c3 check(sage <40); create trigger insert_or_update_sal
before insert or update on teacher
for each row
begin
if(new.job='教授')and (new.sal<4000) then
new.sal:=4000;
end if;
end; create table sal_log
(
eno numeric(4) references teacher(eno),
sal numeric(7,2),
username char(10),
date timestamp
);
create trigger insert_sal
after insert on teacher
for each row
as begin
insert into sal_log values(new.eno,new.sal,current_user,current_timestamp);
end; create trigger update_sal
after update on teacher
for ench row
as begin
if(new.sal<>old.sal) then insert into sal_log values(new.eno,new.sal,current_user,current_timestamp);
end if;
end;