select * from Course
select * from SC
select * from Student
drop table Course
drop table SC
drop table Student
create table Student
( Sno bigint primary key,
Sname varchar(20) not null,
Ssex char(2),
Sage smallint check(Sage between 15 and 30),
Sdept varchar(20)
)
create table Course
( Cno int primary key,
Cname varchar(20) unique,
Cpno varchar(20),
)
create table SC
( Sno bigint ,
Cno int,
Grade smallint check(Grade between 0 and 100 ),
primary key(Sno,Cno)
)
insert into Student values(20161101,'张一','女',20,'计科')
insert into Student values(20161102,'张二','女',21,'物电')
insert into Student values(20161103,'张三','男',19,'计科')
insert into Student values(20161104,'张四','男',20,'计科')
insert into Student values(20161105,'张五','男',21,'物电')
insert into Course values(1,'高等数学','')
insert into Course values(2,'模拟电路','高等数学')
insert into Course values(3,'数字电路','模拟电路')
insert into Course values(4,'计算机组成原理','数字电路')
insert into SC values(20161101,1,null)
insert into SC values(20161102,1,null)
insert into SC values(20161103,1,null)
insert into SC values(20161104,1,null)
insert into SC values(20161105,1,null)
insert into SC values(20161101,2,null)
insert into SC values(20161102,2,null)
insert into SC values(20161103,2,null)
insert into SC values(20161104,2,null)
insert into SC values(20161101,3,null)
insert into SC values(20161102,3,null)
insert into SC values(20161103,3,null)
insert into SC values(20161101,4,null)
insert into SC values(20161102,4,null)
delete from SC where Sno=20161102 and Cno=4
delete from SC
delete from SC where Sno in(select Sno from Student where Sage=19)
select * from SC where Grade is null
update Student set Sage=23 where Sage=19
update Student set Sage=Sage+1
update SC set Grade=60 where Sno in(select Sno from Student where Sdept='计科')
create table Avr_age
( Sdept varchar(20),
age int,
)
insert into Avr_age select Sdept ,avg(Sage) from Student group by Sdept
select * from Avr_age
alter table Student add Sentrance date
alter table Student alter column Sentrance varchar(10)
alter table Student add unique(Sname)
alter table Student drop column Sentrance
create unique index SCno on SC(Sno asc,Cno desc)
alter index SCno RENAME to SCCC
EXEC sp_rename 'SC.SCno','SCSno','INDEX'
drop index SC.SCSno
select Sno,Sname from Student
select Sage-1 from Student
select Sno,'hhh',Sname from Student
select distinct Sno from SC
select * from Student where Sage<23
select COUNT(*) from Student
select SUM(Sage) from Student
select AVG(Sage) from Student
select MAX(Sage) from Student
select MIN(Sage) from Student
select Sno from Student group by Sno,Sage having AVG(Sage)>0
select SC.*,Student.* from SC,Student
select SC.*,Student.* from SC,Student where SC.Sno=Student.Sno
select FIRSTT.Cname,SECONDD.Cpno from Course FIRSTT,Course SECONDD where FIRSTT.Cpno=SECONDD.Cname
select Student.Sno,Sname,Ssex,Sdept,Cno,Grade from Student left outer join SC on (Student.Sno=SC.Sno)
select a.sno,Sname,avg1 from Student,
(select Sno,AVG(Sage) avg1 from Student Group by Sno) a
where Student.Sno=a.Sno
select Student.Sno,Sname,Cname,Grade from Student,SC,Course where Student.Sno=SC.Sno and SC.Cno=Course.Cno
select Sname from Student where exists
(select * from SC where Sno=Student.Sno and Cno=1)
select Sname,Sage from Student where Sage<all
(select Sage from Student where Sdept='物电')
select Sname,Sage from Student where Sage<any
(select Sage from Student where Sdept='物电')
select Sname,Sage from Student where Sage<some
(select Sage from Student where Sdept='物电')
create view JK_Student
as
select Sno,Sname,Sage from Student where Sdept='计科'
select * from JK_Student
select * from student where sage between 19 and 20
select * from student where sdept in ('语文')
select * from student where sname like '李%'
select * from student where sname like '李_'
select * from student where sdept like '%电'
select * from student where sno like '201611%'
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)
)
grant select
on table Student
to U1
grant insert,update(Sno)
on table SC
to U2
grant all privileges
ob table SC
to U3
with grant option
revoke insert
on table SC
from U2 cascade
revoke insert
on table SC
from public
create role xxx
create table Student
(
Sno numeric(6)
constraint c1 check(Sno Between 90000 and 99999),
Sname char(20)
constraint c2 not null,
Sage numeric
constraint c3 check(Sage<30),
Ssex char(2)
constraint c4 check(Ssex in('男','女')),
constraint StudentKey primary key(Sno)
)
alter table Student
drop constraint c1
alter table Student
add constraint C3 check(Sage<40)
create trigger Student_Count
after insert on Student
referencing
new table as Delta
for each statement
insert into StudentInsertLog(Numbers)
select COUNT(*) from Delta