仅供参考
CREATE DATABASE CourseSelection_assignment;
USE CourseSelection_assignment;
CREATE SCHEMA "S-T" AUTHORIZATION db_accessadmin;
CREATE TABLE [S-T].Student
(
Sno CHAR(8) PRIMARY KEY,
Sname VARCHAR(20) UNIQUE,
Ssex CHAR(6),
Sbirthdate DATE,
Smajor VARCHAR(40)
)
CREATE TABLE [S-T].Course
(
Cno CHAR(5) PRIMARY KEY,
Cname VARCHAR(40) NOT NULL,
Ccredit SMALLINT,
Cpno CHAR(5),
FOREIGN KEY (Cpno) REFERENCES [S-T].Course (Cno)
)
CREATE TABLE [S-T].SC
(
Sno CHAR(8),
Cno CHAR(5),
Grade SMALLINT,
Semester CHAR(5),
Teachingclass CHAR(8),
PRIMARY KEY (Sno, Cno),
FOREIGN KEY (Sno) REFERENCES [S-T].Student (Sno),
FOREIGN KEY (Cno) REFERENCES [S-T].Course (Cno)
)
INSERT INTO [S-T].Student (Sno, Sname, Ssex, Sbirthdate, Smajor)
VALUES ('20180001', '李勇', '男', '2000-3-8', '信息安全'),
('20180002', '刘晨', '女', '1999-9-1', '计算机科学与技术'),
('20180003', '王敏', '女', '2001-8-1', '计算机科学与技术'),
('20180004', '张立', '男', '2000-1-8', '计算机科学与技术'),
('20180005', '陈新奇', '男', '2001-11-1', '信息管理与信息系统'),
('20180006', '赵明', '男', '2000-6-12', '数据科学与大数据技术'),
('20180007', '王佳佳', '女', '2001-12-7', '数据科学与大数据技术');
INSERT INTO [S-T].Course (Cno, Cname, Ccredit, Cpno)
VALUES ('81001', '程序设计基础和C语言', 4, NULL),
('81002', '数据结构', 4, '81001'),
('81003', '数据库理论', 4, '81002'),
('81004', '信息系统概论', 4, '81003'),
('81005', '操作系统', 4, '81001'),
('81006', 'Python语言', 3, '81002'),
('81007', '离散数学', 4, NULL),
('81008', '大数据技术概论', 4, '81003');
INSERT INTO [S-T].SC (Sno, Cno, Grade, Semester, Teachingclass)
VALUES ('20180001', '81001', 85, '20192', '81001-01'),
('20180001', '81002', 96, '20201', '81002-01'),
('20180001', '81003', 87, '20202', '81003-01'),
('20180002', '81001', 80, '20192', '81001-02'),
('20180002', '81002', 98, '20201', '81002-01'),
('20180002', '81003', 71, '20202', '81003-02'),
('20180003', '81001', 81, '20192', '81001-01'),
('20180003', '81002', 76, '20201', '81002-02'),
('20180004', '81001', 56, '20192', '81001-02'),
('20180004', '81002', 97, '20201', '81001-02'),
('20180005', '81003', 68, '20202', '81003-01');
CREATE SCHEMA "S-C-SC" AUTHORIZATION 【用户】;
CREATE SCHEMA AUTHORIZATION 【用户】;
CREATE SCHEMA Test AUTHORIZATION 【用户】;
CREATE TABLE Tab1
(
Col1 SMALLINT,
Col2 INT,
Col3 CHAR(20),
Col4 NUMERIC(10, 3),
Col5 DECIMAL(5, 2)
);
DROP TABLE Tab1
DROP SCHEMA Test;
CREATE TABLE [S-T].Student
(
Sno CHAR(8) PRIMARY KEY,
Sname VARCHAR(20) UNIQUE,
Ssex CHAR(6),
Sbirthdate DATE,
Smajor VARCHAR(40)
)
CREATE TABLE [S-T].Course
(
Cno CHAR(5) PRIMARY KEY,
Cname VARCHAR(40) NOT NULL,
Ccredit SMALLINT,
Cpno CHAR(5),
FOREIGN KEY (Cpno) REFERENCES [S-T].Course (Cno)
)
CREATE TABLE [S-T].SC
(
Sno CHAR(8),
Cno CHAR(5),
Grade SMALLINT,
Semester CHAR(5),
Teachingclass CHAR(8),
PRIMARY KEY (Sno, Cno),
FOREIGN KEY (Sno) REFERENCES [S-T].Student (Sno),
FOREIGN KEY (Cno) REFERENCES [S-T].Course (Cno)
)
ALTER TABLE [S-T].Student
ADD Semail VARCHAR(30);
ALTER TABLE [S-T].Student
ALTER COLUMN Sbirthdate VARCHAR(20);
ALTER TABLE [S-T].Course
ADD UNIQUE (Cname);
DROP TABLE [S-T].Student;
CREATE VIEW CS_Student
AS
SELECT Sno, Sname, Ssex, Sbirthdate, Smajor
FROM [S-T].Student
WHERE Smajor = '计算机科学与技术';
DROP TABLE [S-T].Student;
SELECT *
FROM CS_Student;
CREATE UNIQUE INDEX Idx_StudentSname ON [S-T].Student (Sname);
CREATE UNIQUE INDEX Idx_CourseCname ON [S-T].Course (Cname);
CREATE UNIQUE INDEX Idx_SCCno ON [S-T].SC (Sno ASC, Cno DESC);
EXEC sp_rename 'S-T.SC.Idx_SCCno', 'Idx_SCSnoCno', 'INDEX';
DROP INDEX [S-T].Idx_StudentSname;
SELECT Sno, Sname
FROM [S-T].Student;
SELECT Sname, Sno, Smajor
FROM [S-T].Student;
SELECT *
FROM [S-T].Student;
SELECT Sname, DATEDIFF(YEAR, Sbirthdate, GETDATE()) AS Age
FROM [S-T].Student;
SELECT Sname, 'DATA OF BIRTH: ', Sbirthdate, Smajor
FROM [S-T].Student;
SELECT Sno
FROM [S-T].SC;
SELECT DISTINCT Sno
FROM [S-T].SC;
SELECT ALL Sno
FROM [S-T].SC;
SELECT Sname
FROM [S-T].Student
WHERE Smajor = '计算机科学与技术'
SELECT Sname, Ssex
FROM [S-T].Student
WHERE YEAR(Sbirthdate) >= 2000;
SELECT DISTINCT Sno
FROM [S-T].SC
WHERE Grade < 60;
SELECT Sname, Sbirthdate, Smajor
FROM [S-T].Student
WHERE DATEDIFF(YEAR, Sbirthdate, GETDATE()) BETWEEN 20 AND 23;
SELECT Sname, Sbirthdate, Smajor
FROM [S-T].Student
WHERE DATEDIFF(YEAR, Sbirthdate, GETDATE()) NOT BETWEEN 20 AND 23;
SELECT Sname, Ssex
FROM [S-T].Student
WHERE Smajor IN ('计算机科学与技术', '信息安全');
SELECT Sname, Ssex
FROM [S-T].Student
WHERE Smajor NOT IN ('计算机科学与技术', '信息安全');
SELECT *
FROM [S-T].Student
WHERE Sno LIKE '20180003';
SELECT Sname, Sno, Ssex
FROM [S-T].Student
WHERE Sname LIKE '刘%';
SELECT Sno, Sname
FROM [S-T].Student
WHERE Sno LIKE '2018%'
SELECT Cname, Cno
FROM [S-T].Course
WHERE Cno LIKE '81__6';
SELECT Sname, Sno, Ssex
FROM [S-T].Student
WHERE Sname NOT LIKE '刘%';
SELECT Cno, Ccredit
FROM [S-T].Course
WHERE Cname LIKE 'DB\_Design' ESCAPE '\';
SELECT *
FROM [S-T].Course
WHERE Cname LIKE 'DB\_%i__' ESCAPE '\';
SELECT Sno, Cno
FROM [S-T].SC
WHERE Grade IS NULL;
SELECT Sno, Cno
FROM [S-T].SC
WHERE Grade IS NOT NULL;
SELECT Sno, Sname, Ssex
FROM [S-T].Student
WHERE Smajor = '计算机科学与技术'
AND YEAR(Sbirthdate) >= 2000;
SELECT Sno, Grade
FROM [S-T].SC
WHERE Cno = '81003'
ORDER BY Grade DESC;
SELECT *
FROM [S-T].SC
ORDER BY Cno, Grade DESC;
SELECT COUNT(*)
FROM [S-T].Student;
SELECT COUNT(DISTINCT Sno)
FROM [S-T].SC;
SELECT AVG(Grade)
FROM [S-T].SC
WHERE Cno = '81001';
SELECT MAX(Grade)
FROM [S-T].SC
WHERE Cno = '81001';
SELECT SUM(Grade)
FROM [S-T].SC,
[S-T].Course
WHERE Sno = '20180003'
AND SC.Cno = Course.Cno;
SELECT Cno, COUNT(Sno)
FROM [S-T].SC
GROUP BY Cno;
SELECT Sno
FROM [S-T].SC
WHERE Semester = '20192'
GROUP BY Sno
HAVING COUNT(*) >= 1;
SELECT Sno, AVG(Grade)
FROM [S-T].SC
GROUP BY Sno
HAVING AVG(Grade) >= 80;
SELECT TOP 10 Sno
FROM [S-T].SC, [S-T].Course
WHERE Course.Cname = '数据库理论' AND SC.Cno = Course.Cno
ORDER BY Grade DESC;
SELECT Sno, AVG(Grade)
FROM [S-T].SC
GROUP BY Sno
ORDER BY AVG(Grade) DESC
OFFSET 2 ROWS FETCH NEXT 5 ROWS ONLY;
SELECT Student.*, SC.*
FROM [S-T].Student,
[S-T].SC
WHERE Student.Sno = SC.Sno;
SELECT Student.Sno, Sname, Ssex, Sbirthdate, Smajor, Cno, Grade
FROM [S-T].Student,
[S-T].SC
WHERE Student.Sno = SC.Sno;
SELECT Student.Sno, Sname
FROM [S-T].Student,
[S-T].SC
WHERE Student.Sno = SC.Sno
AND SC.Cno = '81002'
AND SC.Grade > 90;
SELECT FIRST.Cno, SECOND.Cpno
FROM [S-T].Course FIRST, [S-T].Course SECOND
WHERE FIRST.Cpno = SECOND.Cno AND SECOND.Cpno IS NOT NULL;
SELECT Student.Sno, Sname, Ssex, Sbirthdate, Cno, Grade
FROM [S-T].Student
LEFT OUTER JOIN [S-T].SC ON Student.Sno = SC.Sno;
SELECT Student.Sno, Sname, Cname, Grade
FROM [S-T].Student,
[S-T].SC,
[S-T].Course
WHERE Student.Sno = SC.Sno
AND SC.Cno = Course.Cno;
SELECT Sno, Sname, Smajor
FROM [S-T].Student
WHERE Smajor IN (SELECT Smajor
FROM [S-T].Student
WHERE Sname = '刘晨');
SELECT Sno, Sname
FROM [S-T].Student
WHERE Sno IN (SELECT Sno
FROM [S-T].SC
WHERE Cno IN (SELECT Cno
FROM [S-T].Course
WHERE Cname = '数据库理论'));
SELECT Sno, Cno
FROM [S-T].SC x
WHERE Grade >= (SELECT AVG(Grade)
FROM [S-T].SC y
WHERE y.Sno = x.Sno);
SELECT Sname, Sbirthdate, Smajor
FROM [S-T].Student
WHERE Sbirthdate > ANY (SELECT Sbirthdate
FROM [S-T].Student
WHERE Smajor = '计算机科学与技术')
AND Smajor <> '计算机科学与技术';
SELECT Sname, Sbirthdate
FROM [S-T].Student
WHERE Sbirthdate > ALL (SELECT Sbirthdate
FROM [S-T].Student
WHERE Smajor = '计算机科学与技术')
AND Smajor <> '计算机科学与技术';
SELECT Sname
FROM [S-T].Student
WHERE EXISTS(SELECT *
FROM [S-T].SC
WHERE Sno = Student.Sno
AND Cno = '81001');
SELECT Sname
FROM [S-T].Student
WHERE NOT EXISTS(SELECT *
FROM [S-T].SC
WHERE SC.Sno = Student.Sno
AND Cno = '81001');
SELECT Sname
FROM [S-T].Student
WHERE NOT EXISTS(SELECT *
FROM [S-T].Course
WHERE NOT EXISTS(SELECT *
FROM [S-T].SC
WHERE Sno = Student.Sno
AND Cno = Course.Cno))
SELECT Sno
FROM [S-T].Student
WHERE NOT EXISTS(SELECT *
FROM [S-T].SC SCX
WHERE SCX.Sno = '20180002'
AND NOT EXISTS(SELECT *
FROM [S-T].SC SCY
WHERE SCY.Sno = Student.Sno
AND SCY.Cno = SCX.Cno));
SELECT *
FROM [S-T].Student
WHERE Smajor = '计算机科学与技术'
UNION
SELECT *
FROM [S-T].Student
WHERE DATEDIFF(YEAR, Sbirthdate, GETDATE()) <= 19;
SELECT Sno
FROM [S-T].SC
WHERE Semester = '20202'
AND Cno = '81001'
UNION
SELECT Sno
FROM [S-T].SC
WHERE Semester = '20202'
AND Cno = '81002';
SELECT *
FROM [S-T].Student
WHERE Smajor = '计算机科学与技术'
INTERSECT
SELECT *
FROM [S-T].Student
WHERE DATEDIFF(YEAR, Sbirthdate, GETDATE()) <= 19;
SELECT Sno
FROM [S-T].SC
WHERE Cno = '81001'
INTERSECT
SELECT Sno
FROM [S-T].SC
WHERE Cno = '81002';
SELECT *
FROM [S-T].Student
WHERE Smajor = '计算机科学与技术'
EXCEPT
SELECT *
FROM [S-T].Student
WHERE DATEDIFF(YEAR, Sbirthdate, GETDATE()) <= 19;
INSERT INTO [S-T].Student(Sno, Sname, Ssex, Smajor, Sbirthdate)
VALUES ('20180009', '陈冬', '男', '信息管理与信息系统', '2000-5-22');
INSERT INTO [S-T].Student
VALUES ('20180008', '张成民', '男', '计算机科学与技术', '2000-4-15');
INSERT INTO [S-T].SC (Sno, Cno, Semester, Teachingclass)
VALUES ('20180005', '81004', '20202', '81004-01');
CREATE TABLE [S-T].Smajor_age
(
Smajor VARCHAR(40),
Avg_age SMALLINT
);
INSERT INTO [S-T].Smajor_age(Smajor, Avg_age)
SELECT Smajor, AVG(DATEDIFF(YEAR, Sbirthdate, GETDATE()))
FROM [S-T].Student
GROUP BY Smajor;
UPDATE [S-T].Student
SET Sbirthdate = '2001-3-18'
WHERE Sno = '20180001'
UPDATE [S-T].SC
SET Grade = Grade - 5
WHERE Semester = '20201'
AND Cno = '81002';
UPDATE [S-T].SC
SET Grade = 0
WHERE Sno IN (SELECT Sno
FROM [S-T].Student
WHERE Smajor = '计算机科学与技术');
DELETE
FROM [S-T].Student
WHERE Sno = '20180007'
DELETE
FROM [S-T].SC
WHERE 1 = 1;
DELETE
FROM [S-T].SC
WHERE Sno IN (SELECT Sno
FROM [S-T].Student
WHERE Smajor = '计算机科学与技术');
INSERT INTO [S-T].SC (Sno, Cno, Grade, Semester, Teachingclass)
VALUES ('20180006', '81004', NULL, '20211', NULL);
UPDATE [S-T].Student
SET Smajor = NULL
WHERE Sno = '20180006';
SELECT *
FROM [S-T].Student
WHERE Sname IS NULL
OR Ssex IS NULL
OR Sbirthdate IS NULL
OR Smajor IS NULL;
SELECT Sno
FROM [S-T].SC
WHERE Grade < 60
AND Cno = '81001';
SELECT Sno
FROM [S-T].SC
WHERE Grade < 60
AND Cno = '81001'
UNION
SELECT sno
FROM [S-T].SC
WHERE Grade IS NULL
AND Cno = '81001';
CREATE VIEW [S-T].IS_Student
AS
SELECT Sno, Sname, Ssex, Sbirthdate, Smajor
FROM [S-T].Student
WHERE Smajor = '信息管理与信息系统';
CREATE VIEW [S-T].IS_Student
AS
SELECT Sno, Sname, Ssex, Sbirthdate, Smajor
FROM [S-T].Student
WHERE Smajor = '信息管理与信息系统'
WITH CHECK OPTION;
CREATE VIEW [S-T].IS_C1(Sno, Sname, Grade)
AS
SELECT Student.Sno, Sname, Grade
FROM [S-T].Student,
[S-T].SC
WHERE Smajor = '信息管理与信息系统'
AND Student.Sno = SC.Sno
AND SC.Cno = '81001';
CREATE VIEW [S-T].IS_C2
AS
SELECT Sno, Sname, Grade
FROM [S-T].IS_C1
WHERE Grade >= 90;
CREATE VIEW [S-T].S_AGE(Sno, Sname, Sage)
AS
SELECT Sno, Sname, DATEDIFF(YEAR, Sbirthdate, GETDATE())
FROM [S-T].Student;
CREATE VIEW [S-T].S_GradeAVG(Son, Gavg)
AS
SELECT Sno, AVG(Grade)
FROM [S-T].SC
GROUP BY Sno;
CREATE VIEW [S-T].F_Student(Fsno, Fname, Fsex, Fbirthdate, Fmajor)
AS
SELECT *
FROM [S-T].Student
WHERE Ssex = '女';
DROP VIEW [S-T].S_AGE;
DROP VIEW [S-T].IS_C1;
SELECT Sno, Sbirthdate
FROM [S-T].IS_Student
WHERE DATEDIFF(YEAR, Sbirthdate, GETDATE()) <= 20;
SELECT IS_Student.Sno, Sname
FROM [S-T].IS_Student,
[S-T].SC
WHERE [S-T].IS_Student.Sno = SC.Sno
AND SC.Cno = '81001';
SELECT *
FROM [S-T].S_GradeAVG
WHERE Gavg >= 90;
UPDATE [S-T].IS_Student
SET Sname = '刘新奇'
WHERE Sno = '20180005';
INSERT INTO [S-T].IS_Student(Sno, Sname, Ssex, Sbirthdate, Smajor)
VALUES ('20180207', '赵新', '男', '2001-7-19', '信息管理与信息系统');
DELETE
FROM [S-T].IS_Student
WHERE Sno = '20180207';