CREATEDATABASE jxgl
GO
USE jxgl
GoCreateTable Student
(Sno CHAR(5) NOTNULLPRIMARYKEY(Sno),
Sname VARCHAR(20),
Sage SMALLINTCHECK(Sage>=15AND Sage<=45),
Ssex CHAR(2) DEFAULT'男'CHECK(Ssex='男'OR Ssex='女'),
Sdept CHAR(2));CreateTable Course
(Cno CHAR(2) NOTNULLPRIMARYKEY(Cno),
Cname VARCHAR(20),
Cpno CHAR(2),
Ccredit SMALLINT);createtable sc
(sno char(5) notnullconstraint s_f foreignkeyreferences student(sno),
cno char(2) notnull,
grade smallintcheck((grade isnull)or(grade between 0and100)),
primarykey(sno,cno),
foreignkey (cno) references course(cno));insertinto student values('98001','钱横',18,'男','CS');insertinto student values('98002','王林',19,'女','CS');insertinto student values('98003','李民',20,'男','IS');insertinto student values('98004','赵三',16,'女','MA');insertinto Course values('1','数据库系统','5',4);insertinto Course values('2','数学分析',null,2);insertinto Course values('3','信息系统导论','1',3);insertinto Course values('4','操作系统_原理','6',3);insertinto Course values('5','数据结构','7',4);insertinto Course values('6','数据处理基础',null,4);insertinto Course values('7','C语言','6',3);insertinto sc values('98001','1',87);insertinto sc values('98001','2',67);insertinto sc values('98001','3',90);insertinto sc values('98002','2',95);insertinto sc values('98002','3',88);insertinto Student values('98011','张静',27,'女','CS');insertinto Student values('99201','石科',21,'男','CS')
insertinto Student values('99202','宋笑',19,'女','CS')
insertinto Student values('99203','王欢',20,'女','IS')
insertinto Student values('99204','彭来',18,'男','MA')
insertinto Student values('99205','李晓',22,'女','CS')
insertinto Student(Sno,Sname,Sage) values('98012','李四',16);insertinto Student values('99010','赵青江',18,'男','CS')
insertinto Student values('99011','张丽萍',19,'女','CH')
insertinto Student values('99012','陈景欢',20,'男','IS')
insertinto Student values('99013','陈婷婷',16,'女','PH')
insertinto Student values('99014','李军',16,'女','EH')
insertinto SC
select sno,cno,nullfrom Student,Course
where Sdept = 'CS'and cno='5';insertinto Student
selectcast(cast(sno asinteger)+1aschar(5)),sname+'2',sage,ssex,sdept
from Student where Sname='赵三';
go
select *
from Student;select *from Student
update Student set sname='李明',Sage=23where sno='98003'select *from Student
update student set student.Sage = Student.Sage+1from (select top(3) * from student orderby sno) as stu3
where stu3.sno=Student.sno;select *from Student
update top(3) percent Student set student.Sage=Student.Sage+1;select *from Student
select *from sc
update sc
set grade=(selectavg(grade) from sc where cno='3')
where sno = '98001'and cno='3'select *from sc
select *from sc
update sc set grade=0where cno='2'and sno in(select sno from Student where sname = '王林')
select *from sc
select * into TSC from sc
select * from TSC
select *from sc
deletefrom sc
where'CS'=(select sdept from Student
where Student.Sno=sc.sno)
select *from sc
insertinto sc select * from TSC
deletefrom sc
truncatetable sc
insertinto sc values('99010','1',87)
insertinto sc values('99010','3',80)
insertinto sc values('99010','4',87)
insertinto sc values('99010','6',85)
insertinto sc values('99011','1',52)
insertinto sc values('99011','2',47)
insertinto sc values('99011','3',53)
insertinto sc values('99011','5',45)
insertinto sc values('99012','1',84)
insertinto sc values('99012','4',67)
insertinto sc values('99012','5',81)
insertinto sc(sno,cno)values('99010','2')
insertinto sc(sno,cno)values('99012','3')
select * from sc
--2select * into TS from Student
deletefrom TS
select * from TS
--3insertinto SC
select sno,cno,60from Student
where Sdept = 'IS'and cno='7';
// 如果from student course 会进行广义笛卡尔积,会导致重复出现;或者使用distinct 来约束
--4 select * from SC
select * from Student
insertinto TS
select *
from Student
where Ssex='女'and Sage<=16;--5 INSERTINTO TS
SELECT *
FROM STUDENT
WHERE SNO IN
(SELECT SNO
FROM SC
GROUPBY SNO
HAVINGMAX(GRADE)<60
)
--6update Student set sname='刘华',Sage=sage+1where sno='99011'select * from SC
--7update sc
set grade =nullwhere grade<60and cno in(select cno
from Course
where Cname='数据库系统')
select * from SC
--8update student set student.Sage = Student.Sage+1from (select top(4) * from student orderby sno) as stu3
where stu3.sno=Student.sno;--9update sc set grade=nullwhere cno='3'and sno in(select sno from Student where sname = '王林')
select *from sc
--10update sc
set grade=grade*1.05where grade<(selectavg(grade) from sc) and sno in(select sno from Student where ssex = '女')
select *from sc
--11update sc
set grade = grade*0.98where grade<=80and cno='2'update sc
set grade = grade * 0.99where grade >80and cno='2'
--12select * into t11 from Student
select * into t12 from Course
select * into t13 from SC
select * from sc
--13deletefrom sc
where grade ISnullselect * from sc
--14deletefrom sc
where sno in (select sno
from Student
where Sname='钱横')
--15deletefrom sc
where sno ='98005'deletefrom Student
where sno='98005'
--16deletefrom sc
where sno in(select sno
from Student
where Sname like'张%')
select * from Student
deletefrom Student
where Sname like'张%'
--17deletefrom student
deletefrom course
--18insertinto student select * from t1
insertinto sc select * from t2
insertinto course select * from t3