Building a database script
USE [master] GO CREATE DATABASE [Example] GO USE [Example] GO /****** Object: Table [dbo].[Course] Script Date: 2016/12/6 0:16:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Course]( [C#] [int] IDENTITY(1,1) NOT NULL, [Cname] [nvarchar](50) NOT NULL, [T#] [int] NOT NULL, CONSTRAINT [PK_Course] PRIMARY KEY CLUSTERED ( [C #] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO /****** Object: Table [dbo].[Sc] Script Date: 2016/12/6 0:16:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Sc]( [ID] [int] IDENTITY(1,1) NOT NULL, [S#] [int] NOT NULL, [C#] [int] NOT NULL, [score] [int] NOT NULL, CONSTRAINT [PK_Score] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO /****** Object: Table [dbo].[Student] Script Date: 2016/12/6 0:16:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Student]( [S#] [int] IDENTITY(1,1) NOT NULL, [Sname] [nvarchar](50) NOT NULL, [Sage] [int] NOT NULL, [Ssex] [nvarchar](1) NOT NULL, CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED ( [S#] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO /****** Object: Table [dbo].[Teacher] Script Date: 2016/12/6 0:16:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Teacher]( [T#] [int] IDENTITY(1,1) NOT NULL, [Tname] [nvarchar](50) NOT NULL, CONSTRAINT [PK_Teacher] PRIMARY KEY CLUSTERED ( [T#] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET IDENTITY_INSERT [dbo].[Course] ON INSERT [dbo].[Course] ([C#], [Cname], [T#]) VALUES (1, N'语文', 1) INSERT [dbo].[Course] ([C#], [Cname], [T#]) VALUES (2, N'数学', 1) INSERT [dbo].[Course] ([C#], [Cname], [T#]) VALUES (4, N'政治', 2) INSERT [dbo].[Course] ([C#], [Cname], [T#]) VALUES (5, N'物理', 2) SET IDENTITY_INSERT [dbo].[Course] OFF SET IDENTITY_INSERT [dbo].[Sc] ON INSERT [dbo].[Sc] ([ID], [S#], [C#], [score]) VALUES (1, 1, 1, 90) INSERT [dbo].[Sc] ([ID], [S#], [C#], [score]) VALUES (2, 1, 2, 80) INSERT [dbo].[Sc] ([ID], [S#], [C#], [score]) VALUES (3, 2, 1, 100) INSERT [dbo].[Sc] ([ID], [S#], [C#], [score]) VALUES (6, 2, 2, 50) INSERT [dbo].[Sc] ([ID], [S#], [C#], [score]) VALUES (7, 1, 4, 90) INSERT [dbo].[Sc] ([ID], [S#], [C#], [score]) VALUES (8, 1, 5, 90) SET IDENTITY_INSERT [dbo].[Sc] OFF SET IDENTITY_INSERT [dbo].[Student] ON The INSERT [the dbo]. [Student] ([S #], [Sname], [Sage], [SSEX]) the VALUES ( . 1 , N ' John Doe ' , 20 is , N ' M ' ) The INSERT [the dbo]. [Student] ([S #], [Sname], [Sage], [SSEX]) the VALUES ( 2 , N ' John Doe ' , 21 is , N ' F ' ) The INSERT [the dbo]. [Student] ([S #], [Sname], [Sage], [SSEX]) the VALUES ( . 3 , N ' Wang Wu ' , 22 is , N ' M ' ) SET IDENTITY_INSERT [dbo].[Student] OFF SET IDENTITY_INSERT [dbo].[Teacher] ON INSERT [dbo].[Teacher] ([T#], [Tname]) VALUES (1, N'叶平') INSERT [dbo]. [Teacher] ([# T], [Tname]) VALUES ( 2 , N ' I do not call Ye Ping ' ) SET IDENTITY_INSERT [dbo].[Teacher] OFF ALTER TABLE [dbo].[Course] WITH CHECK ADD CONSTRAINT [FK_Course_Teacher] FOREIGN KEY([T#]) REFERENCES [dbo].[Teacher] ([T#]) GO ALTER TABLE [dbo].[Course] CHECK CONSTRAINT [FK_Course_Teacher] GO ALTER TABLE [dbo].[Sc] WITH CHECK ADD CONSTRAINT [FK_Sc_Course] FOREIGN KEY([C#]) REFERENCES [dbo].[Course] ([C#]) GO ALTER TABLE [dbo].[Sc] CHECK CONSTRAINT [FK_Sc_Course] GO ALTER TABLE [dbo].[Sc] WITH CHECK ADD CONSTRAINT [FK_Sc_Student] FOREIGN KEY([S#]) REFERENCES [dbo].[Student] ([S#]) GO ALTER TABLE [dbo].[Sc] CHECK CONSTRAINT [FK_Sc_Student] GO USE [master] GO ALTER DATABASE [Example] SET READ_WRITE GO topic - 1 , the query " 1 " course than " 2 " course high scores of all students learn numbers; the SELECT dS # from ( the SELECT the bS #, b.score AS b_score, c.score AS c_score from ( the SELECT S #, Score from Sc = the WHERE C # . 1 ) Inner the Join B ( SELECT S #, Score from Sc = the WHERE C # 2 ) C = ON the bS # cS #) D WHERE d.b_score> d.c_score - 2 , the query is greater than the average score of 60 points and the average score of the student number; SELECT S # , AVG (score) from Sc # S Group by the HAVING AVG (score)> 60 - 3 , to query all of the students number, name, number of elective total score; SELECT a.*, CASE The Num the IS the WHEN null THEN 0 the ELSE the Num the END the AS ' Number of courses ' , CASE WHEN Total IS NULL THEN 0 ELSE Total END AS '总成绩' FROM (select S#,Sname from Student) a left join ( The SELECT S # the Num, COUNT (*) AS, SUM (Score) AS Total from Sc Group BY S #) b - looking for all the students and the number of elective total score aS # ON = the bS # - 4 , the query name " Lee " the number of teachers; the SELECT COUNT (*) from Teacher the WHERE Tname the LIKE ' Lee% ' - 5 , the query never learned " Ye Ping " students teacher class the number, name; - (the number of school students learned first find out) SELECT d.* FROM Student d left join ( select distinct a.S# from Sc a inner join (select C# from Course where T#= (select T# from Teacher where Tname='叶平')) b on a.C#=b.C# ) ON cS c # = # dS the WHERE cS # IS null - 6 , query learned " 1 " and I have learned a number " 2 " students of the course number, name; the SELECT b *. From ( the SELECT Distinct S # from Sc WHERE C # = . 1 or C = # 2 Group # S by HAVING COUNT (*) = 2 ) A Inner the Join ( The SELECT S #, Sname from Student) b ON # = aS the bS # - 7 , queries learned " Ye Ping " All courses teachers paid by the students of the school number, name; the SELECT aS # from ( the SELECT * from Sc ) a inner join ( select C# from Course where T#= (select T# from Teacher where Tname='叶平') ) B ON the aC # = aS bC by Group # # HAVING COUNT (*) = ( SELECT COUNT (*) from Course, WHERE T # = ( SELECT T # from Teacher WHERE Tname = ' Epping ' )) - . 9 , the query All course grade less than 60 students school number, name; the SELECT . b * from ( the SELECT DISTINCT S # from Sc the WHERE score < 60 ) a Inner the Join ( The SELECT S #, Sname from Student) b ON # = aS the bS # - 10 , the query did not learn the lesson the students of all school number, name; the SELECT . A * from ( the SELECT S #, Sname from Student) A left the Join (select S# from Sc group by S# having COUNT(*)=(select COUNT(*) from Course)) b aS # ON = the bS # the WHERE the bS # IS null - 11 , there is at least one course with the query " Joe Smith " the same number of students and the students of the school name; the SELECT DISTINCT aS #, c.Sname from ( the SELECT S #, # C from Sc WHERE S #! = ( SELECT S # from Student WHERE Sname = ' John Doe ' )) the Join A Inner (select C# from Sc where S#=(select S# from Student where Sname='张三')) b on a.C#=b.C# inner join ( The SELECT S #, Sname from Student) ON aS c # = cS # - 12 , studied science inquiry number " 1 " students of all other students in class learning numbers and names; the SELECT d *. From ( select S# from (select C# from Sc WHERE S#=1) a inner join (select * from Sc where S#!=1) b on a.C#=b.C# group by b.S# having COUNT(*)=(select COUNT(*) from Sc WHERE S#=1) ) c inner join (select S#,Sname from Student) d on c.S#=d.S#