diff --git "a/20210326\344\275\234\344\270\232/.keep" "b/20210326\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/20210326\344\275\234\344\270\232/12580/233333.sql" "b/20210326\344\275\234\344\270\232/12580/233333.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5970d58baef454db3cec5548f73e431a9c506ebd --- /dev/null +++ "b/20210326\344\275\234\344\270\232/12580/233333.sql" @@ -0,0 +1,103 @@ +use master +go +create database odXHSD +on +( name='odXHSD', + filename='C:\test\odXHSD.mdf', + size=20, + maxsize=100, + filegrowth=10% + ) + log on + ( name='odXHSD_log', + filename='C:\test\odXHSD_log.ldf', + size=20, + maxsize=100, + filegrowth=10% + ) + go + use odXHSD + go + create table orders + ( orderId int primary key identity(1,1), + orderDate datetime, + ) + insert into orders(orderDate) values('2008-01-12 ') + insert into orders(orderDate) values('2008-02-10 ') + insert into orders(orderDate) values('2008-02-15 ') + insert into orders(orderDate) values('2008-03-10 ') + + + create table orderItem + ( ItemiD int primary key identity, + orderId int , + itemType nvarchar(20), + itemName nvarchar(10), + theNumber int, + theMoney money + ) + + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (1,'文具','笔','72' ,'2' ) + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (1,'文具','尺','10' ,'1' ) + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (1,'体育用品','篮球','1','56' ) + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (2,'文具','笔','36','2') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (2,'文具','固体胶','20','3') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (2,'日常用品','透明胶','36','2') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (2,'体育用品','羽毛球','20','3') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (3,'文具','订书机','20','3') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (3,'文具','订书针','10','3') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (3,'文具','裁纸刀','5','5') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (4,'文具','笔','20','2') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (4,'文具','信纸','50','1') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (4,'日常用品','毛巾','4','5') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (4,'日常用品','透明胶','30','1') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (4,'体育用品','羽毛球','20','3' ) + + select * from orders + select * from orderItem + + select sum(theNumber) from orderItem + select orderId,sum(theNumber)As 订购总数量,avg(theMoney)As 平均单价 from orderItem group by orderId having orderId<3 and avg(theMoney)<10 order by orderId + select sum(theNumber)As 订购总数量,avg (theMoney)As 平均单价 from orderItem group by theNumber,theMoney having sum(theNumber)>50 and avg(theMoney)<10 order by theNumber,theMoney + select itemType,COUNT(*)数量 from orderItem group by itemType order by itemType desc + select sum(theNumber)As 订购总数量,avg(theMoney)As 平均单价 from orderItem group by itemType,theMoney having sum(theNumber)>100 + select itemName As 产品名称,count(theNumber) As 订购次数,sum(theNumber)As 总数量,avg(theMoney)As 平均单价 from orderItem group by itemName + +--使用上次作业的订单数据库,完成下列题目: + + select * from orders + select * from orderItem + +--1.查询 所有的订单 的 订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orders.orderId,orders.orderDate,orderItem.itemType,orderItem.itemName,orderItem.theNumber,orderItem.theMoney from orders +inner join orderItem on orders.orderId=orderItem.orderId group by orders.orderId,orders.orderDate,itemType,itemName,theNumber,theMoney +--2.查询 订购数量大于50的 订单的编号,订单日期,订购产品的类别 和 订购的产品名称 +select orders.orderId,orders.orderDate,orderItem.itemType,orderItem.itemName from orders +inner join orderItem on orders.orderId=orderItem.orderId +where orderItem.theNumber > 50 +group by orders.orderId,orders.orderDate,orderItem.itemType,orderItem.itemName +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId,orders.orderDate,orderItem.itemType,orderItem.itemType,orderItem.theNumber,orderItem.theMoney,sum(orderItem.theNumber*orderItem.theMoney) from orders +inner join orderItem on orders.orderId=orderItem.orderId group by orders.orderId,orders.orderDate,itemType,theNumber,theMoney + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + select orders.orderId,orders.orderDate,orderItem.itemType,orderItem.itemName,orderItem.theNumber ,orderItem.theMoney,sum(orderItem.theNumber*orderItem.theMoney) from orders + inner join orderItem on orders.orderId=orderItem.orderId group by orders.orderId,orders.orderDate,orderItem.itemType,orderItem.itemName,orderItem.theNumber ,orderItem.theMoney + having theMoney >5 and theNumber>=50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 + +select orders.orderId,orderItem.itemName from orders +inner join orderItem on orders.orderId = orderItem.orderId group by orders.orderId,orderItem.itemName + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/12580/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/12580/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..dfbc979a25ca3445809121d9c1230155cd2ce4a3 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/12580/SQLQuery3.sql" @@ -0,0 +1,89 @@ +use master +go +create database student +on +( +name='student', +filename='C:\test\student.mdf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +log on +( +name='STU_log', +filename='C:\test\student_log.ldf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +go +use student +go +create table StuInfo +( +StuNO nvarchar(10) unique not null, +StuName nvarchar(10) not null, +StuAge int not null, +StuAddress nvarchar(200) , +StuSeat nvarchar(8) not null, +StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女') +) + +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2501','张秋利','20','美国硅谷','1','女') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2502','李斯文','18','湖北武汉','2','男') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2503','马文才','22','湖南长沙','3','男') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2504','欧阳俊雄','21','湖北武汉','4','女') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2505','梅超风','20','湖北武汉','5','女') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2506','陈旋风','19','美国硅谷','6','女') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2507','陈风','20','美国硅谷','7','女') + +create table Grade +( +ExamNo int primary key identity(1,1), +StuNo nvarchar(10) references StuInfo(StuNO), +WittenExam int not null, +LabExam int not null +) +select* from Grade +insert into Grade(StuNO,WittenExam,LabExam) +values('s2501','50','70') +insert into Grade(StuNO,WittenExam,LabExam) +values('s2502','60','65') +insert into Grade(StuNO,WittenExam,LabExam) +values('s2503','86','85') +insert into Grade(StuNO,WittenExam,LabExam) +values('s2504','40','80') +insert into Grade(StuNO,WittenExam,LabExam) +values('s2505','70','90') +insert into Grade(StuNO,WittenExam,LabExam) +values('s2506','85','90') + + +select * from StuInfo +select * from Grade + +select StuName,StuAge,WittenExam,LabExam from StuInfo inner join Grade on Grade.StuNo=StuInfo.StuNO + +select StuInfo.StuNO,StuName,Grade.WittenExam,LabExam from StuInfo inner join Grade on Grade.StuNo=StuInfo.StuNO where Grade.WittenExam>=60 and Grade.LabExam>=60 + + select StuInfo.StuNO,StuName,Grade.WittenExam,LabExam from StuInfo + left join Grade on Grade.StuNo=StuInfo.StuNO + + select StuInfo.StuName,StuInfo.StuAge,Grade.WittenExam,Grade.LabExam from StuInfo + inner join Grade on StuInfo.StuNO=Grade.StuNo group by StuInfo.StuName,StuInfo.StuAge,Grade.WittenExam,Grade.LabExam + having StuInfo.StuAge >= 20 order by WittenExam desc + + select StuInfo.StuName,StuInfo.StuSex,avg(Grade.LabExam) as 机试平均分 from StuInfo + inner join Grade on StuInfo.StuNO=Grade.StuNo group by StuInfo.StuSex,Grade.LabExam,StuInfo.StuName + + select StuInfo.StuName,StuInfo.StuSex,sum(Grade.WittenExam) as 笔试总分 from StuInfo + inner join Grade on StuInfo.StuNO=Grade.StuNo group by StuInfo.StuName,StuInfo.StuSex,Grade.WittenExam + diff --git "a/20210326\344\275\234\344\270\232/12580/\344\275\234\344\270\2324.sql" "b/20210326\344\275\234\344\270\232/12580/\344\275\234\344\270\2324.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4949ce373e87d95a879e5223d1d6859ab609a0c7 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/12580/\344\275\234\344\270\2324.sql" @@ -0,0 +1,110 @@ +use master +go +create database bbs +on +( name='bbs', + filename='D:\test\bbs.mdf', + size=10, + maxsize=100, + filegrowth=10% + ) + log on + ( name='bbs_log', + filename='D:\test\bbs_log.ldf', + size=10, + maxsize=100, + filegrowth=10% + ) + go + use bbs + go + create table bbsUsers +( + uID int identity(1,1) not null , + uName nvarchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null, + +) +alter table bbsUsers add constraint PK_bbsUsers_uID primary key(uID) +alter table bbsUsers add constraint UK_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex='男' or uSex='女') +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 or uAge<=60) +alter table bbsUsers add constraint CK_bbsUsers_uPoint check(uPoint>=0) +create table bbsSection +( + sID int identity(1,1) not null , + sName varchar(10) not null, + sUid int , + +) +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid)references bbsUsers(uID) + +create table bbsTopic +( + tID int primary key identity(1,1) not null, + tUID int references bbsUsers(uID) not null , + tSID int references bbsSection(sID) , + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime not null, + tCount int not null, +) +create table bbsReply +( + rID int primary key identity(1,1) not null, + rUID int references bbsUsers(uID) , + rTID int references bbsTopic(tID) , + rMsg text not null, + rTime datetime , + ) + select * from bbsReply +select * from bbsSection +select * from bbsTopic +select * from bbsUsers +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select uName,uPoint into bbsPoint_backup from bbsUsers +insert into bbsSection values('技术名流',1),('读书世界',3),('生活百科',1),('八卦区',3) +insert into bbsTopic values(2,4,'范跑跑!','谁是范跑跑!', '2008-7-8',1),(3,2,'.NET!','与JAVA的区别是什么呀?','2008-9-1',2),(1,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10',0) +select * from bbsUsers + + delete from bbsTopic where tUID=2 + delete from bbsReply where rUID=2 + delete from bbsReply where rID=1 + delete from bbsUsers where uName='逍遥' + + alter table bbsSection drop NK + alter table bbsTopic drop FK__bbsTopic__tUID__1920BF5C + + update bbsUsers set uPoint ='10' where uName='小雨点' + delete bbsSection where sName='生活百科' + delete bbsReply + + select tID,count(tCount) from bbsTopic group by tID + select count(rID ) from bbsReply group by rID + select tUID ,count(tSID) from bbsTopic group by tUID + select tUID ,sum(tCount) from bbsTopic group by tUID + select tUID,avg(tCount) from bbsTopic group by tUID having avg(tCount)>3 + select top 1 uName,uSex,uAge,uPoint from bbsUsers + select*from bbsTopic where tTitle like '%快乐%' and tMsg like '%快乐%' + select*from bbsUsers where uAge>=15 and uAge<=20 and uPoint>10 + select*from bbsUsers where uName like'小_大' + select tUID ,tCount from bbsTopic where tTitle like '%!' + + select bbsUsers.uID ,bbsUsers.uName,sName from bbsSection +right join bbsUsers on bbsUsers.uID=bbsSection.sUid + +select tID ,bbsUsers.uName ,tTitle,tMsg ,tTime from bbsTopic +inner join bbsUsers on bbsTopic.tUID=bbsUsers.uID where tTime>'2008-09-15' + +select uID ,uName,bbsSection.sNamefrom bbsUsers from bbsTopic +left join bbsSection on uID=sUid where uAge<20 + +select top 1 tUID ,bbsUsers.uName ,tTitle ,tMsg , tCount from bbsTopic +inner join bbsUsers on uID=tUID order by tCount DESC + +select bbsSection.sName ,uID ,SUM(tID) from bbsTopic +right join bbsSection on sID=tSID +left join bbsUsers on uID=tUID group by bbsSection.sName,uID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/2\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/2\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..35d3cd05472166b8f07a01bd58c1c691dbe871fc --- /dev/null +++ "b/20210326\344\275\234\344\270\232/2\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" @@ -0,0 +1,261 @@ +drop database Studentinfo + +create database Studentinfo + + + +on + +( + + name='Studentinfo', + + filename='E:\SQL\Studentinfo.mdf', + + size=5mb, + + maxsize=50mb, + + filegrowth=10% + +) + +log on + +( + + name='Studentinfo_log', + + filename='E:\SQL\Studentinfo_log.ldf', + + size=5mb, + + maxsize=50mb, + + filegrowth=10% + +) + + + +use Studentinfo + +go + +create table StuDent + +( + + sTUNO varchar(15), + + stuName nvarchar(20), + + stuAge int, + + stuAddress nvarchar(50), + + stuSeat int, + + stuSex nvarchar(1) default('男') check(stuSex='男' or stuSex='女') + +) + + + +insert into StuDent(sTUNO,stuName,stuAge,stuAddress,stuSeat,stuSex) + +select 's2501','张秋里',20,'美国硅谷',1,'男' union + +select 's2502','李斯文',18,'湖北武汉',2,'女' union + +select 's2503','马文才',22,'湖南长沙',3,'男' union + +select 's2504','欧阳俊雄',21,'湖北武汉',4,'女' union + +select 's2505','梅超风',20,'湖北武汉',5,'男' union + +select 's2506','陈旋风',19,'美国硅谷',6,'男' union + +select 's2507','陈风',20,'美国硅谷',7,'女' + + + + + +create table Score + +( + + examNO int, + + stuNO varchar(15), + + writtenExam int, + + labExam int + +) + +drop table Score + +insert into Score(examNO,stuNO,writtenExam,labExam) + +select 1,'s2501','50','70' union + +select 2,'s2502','60','65' union + +select 3,'s2503','86','85' union + +select 4,'s2504','40','80' union + +select 5,'s2505','70','90' union + +select 6,'s2506','85','90' + + + +--指定别名 + +select stuNO as 学号, stuName as 姓名 ,stuAddress as 地址,stuSeat as 座位号,stuSex as 性别 from StuDent + + + +--查询 stuName,stuAge,stuAddress + +select stuName,stuAge,stuAddress from StuDent + + + +--查询 学号 笔试 机试 指定别名1 + +select stuNO as 学号 ,writtenExam as 笔试,labExam as 机试 from Score + +--指定别名2 + +select stuNO 学号 ,writtenExam 笔试,labExam 机试 from Score + +--指定别名3 + +select 学号=stuNO,笔试=writtenExam,机试=labExam from Score + + + + + +select stuNO+stuName+stuAddress+'@'+stuAddress as 邮箱 from StuDent + + + +--查询并计算总分 + +select stuNO as 学号,writtenExam as 笔试 ,labExam as 机试, writtenExam+labExam as 总分 from Score + + + + + +select stuName ,stuAddress from StuDent + + + +select stuAge as 所有年龄 from StuDent + + + +--查询前三 + +select top 3 * from StuDent + +--查询前四 姓名 座位号 + +select top 4 stuName,stuSeat from StuDent + + + +select top 50 percent * from StuDent + + + +select stuName ,stuAddress='湖北武汉',stuAge=20 from StuDent + + + +--范围查询并降序 + +select labExam from Score where labExam>=60 and labExam<=80 order by labExam DESC + + + +-- in or + +select * from StuDent where stuAddress = '湖北武汉' or stuAddress = '湖南长沙' + +select * from StuDent where stuAddress in('湖北武汉', '湖南长沙') + + + +select writtenExam from Score where writtenExam<70 order by writtenExam + + + +select * from StuDent where stuAge is null or stuAge='' + +select*from StuDent where stuAge is not null and not stuage='' + + + +--模糊查询 + +select * from StuDent where stuName like'张%' + + + +select * from StuDent where stuAddress like '%湖%' + + + +select * from StuDent where stuName like'张_' + + + +select * from StuDent where stuName like'__俊%' + + + +select * from StuDent order by stuAge DESC + + + +select * from StuDent order by stuAge DESC , stuSeat ASC + + + +select top 1 * from Score order by writtenExam DESC + + + +select top 1 * from Score order by labExam ASC + + +--1.查询学生的姓名,年龄,笔试成绩和机试成 +select * from StuDent +select * from Score +select M.StuName 姓名,M.stuAge 年龄,L .writtenExam 鼻屎 ,L .labExam 鸡屎 from StuDent M inner join Score L on M.stuno = L.stuno + + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select Score.stuno 学号,StuDent.StuName 姓名,StuDent.stuAge 年龄,Score .writtenExam 鼻屎 ,Score .labExam 鸡屎 from StuDent inner join Score on StuDent.stuno = Score.stuno WHERE Score .writtenExam>60 AND Score .labExam >60 + + + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select Score.stuno 学号, StuDent.StuName 姓名,StuDent.stuAge 年龄,Score .labExam 鸡屎,Score .writtenExam 鼻屎 from StuDent left join Score on StuDent.stuno = Score.stuno + + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select StuDent.StuName 姓名,StuDent.stuAge 年龄,Score .writtenExam 鼻屎 ,Score .labExam 鸡屎 from StuDent inner join Score on StuDent.stuno = Score.stuno WHERE StuDent.stuAge>=20 order by writtenExam desc + +--5.查询男女生的机试平均分 +select StuDent.stusex 性别,avg(Score .labExam) 鸡屎平均 from StuDent inner join Score on StuDent.stuno = Score.stuno group by StuDent.stusex +--6.查询男女生的笔试总分 +select StuDent.stusex 性别,sum(Score .writtenExam)鼻屎总分 from StuDent inner join Score on StuDent.stuno = Score.stuno group by StuDent.stusex diff --git "a/20210326\344\275\234\344\270\232/2\346\235\250\346\242\246\346\236\227/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/2\346\235\250\346\242\246\346\236\227/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d3162b043b10a11e427532db337c113765ec4326 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/2\346\235\250\346\242\246\346\236\227/SQLQuery2.sql" @@ -0,0 +1,204 @@ +use master + + + +create database bbs + + + +on + + + +( + + + + name='bbs', + + + + filename='E:\bbs.mdf', + + + + size=6MB, + + + + maxsize=100MB, + + + + filegrowth=10MB + + + +) + + + + + + + +log on + + + +( + + + + name='bbs_log', + + + + filename='E:\bss_log.ldf', + + + + size=6MB, + + + + maxsize=100MB, + + + + filegrowth=10MB + + + +) + + + +go + + use bbs + + go + + + + drop database bbs + + --订单表 + + create table orders + + ( + + orderId int primary key, + + orderDate datetime + +) + + + drop table orders + +insert into orders(orderId,orderDate) + +select 1,'2008-01-12' union + +select 2,'2008-02-10' union + +select 3,'2008-02-15' union + +select 4,'2008-03-10' + + + --订单项目表 + + create table orderItem + + ( + + ItemiD int not null primary key identity(1,1) ,--项目编号 + + orderId varchar(50),--订单编号 + + itemType nvarchar(20) ,--产品类别 + + itemName nvarchar(20),--产品名称 + + theNumber int ,--订购数量 + + theMoney int --订购单价 + + + + ) + + drop table orderItem + + select * from orderItem + + insert into orderItem( orderId,itemType,itemName,theNumber,theMoney ) + + select '1', '文具', '笔', 72 , 2 union + + select '1', '文具', '尺', 10 , 1 union + + select '1', '体育用品', '篮球', 1 , 56 union + + select '2', '文具', '笔', 1 , 36 union + + select '2', '文具', '固体胶', 20 , 3 union + + select '2', '日常用品', '透明胶',2 , 1 union + + select '2', '体育用品', '羽毛球', 20 , 3 union + + select '3', '文具', '订书机', 20 , 3 union + + select '3', '文具', '订书针', 10 , 3 union + + select '3', '文具', '载纸刀', 5 , 5 union + + select '4', '文具', '笔', 20 , 2 union + + select '4', '文具', '信纸', 50 , 1 union + + select '4', '日常用品', '毛巾', 4 , 5 union + + select '4', '日常用品', '透明胶', 30 , 1 union + + select '4', '体育用品', '羽毛球', 20 , 3 + + + select * from orderItem + select * from orders + --使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select L .orderId 订单编号 , M.orderDate 日期,L.itemType 类别,L. itemName 名称 ,L.theNumber 数量,L.theMoney 单价 from orders M inner join orderItem L on M.orderId=L.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select L.orderId 订单编号, M.orderDate 日期,L.itemType 类别,L. itemName 名称 ,L.theNumber 数量 from orders M inner join orderItem L on M.orderId=L.orderId where L.theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select L.orderId 订单编号, M.orderDate 日期,L.itemType 类别,L. itemName 名称 ,L.theNumber 数量, L.theMoney 单价 ,L.theMoney * L.theNumber 总价 from orders M inner join orderItem L on M.orderId=L.orderId + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select L.orderId 订单编号, M.orderDate 日期,L.itemType 类别,L. itemName 名称 ,L.theNumber 数量, L.theMoney 单价 ,L.theMoney * L.theNumber 总价 from orders M inner join orderItem L on M.orderId=L.orderId where L.theMoney >=5 and L.theNumber>50 + +--5.查询每个订单分别订购了几个产品,例如: + --编号 订购产品数 +select L.orderId 订单编号, L.theNumber 订购数量 from orders M inner join orderItem L on M.orderId=L.orderId group by L.theNumber,L.orderId + + + + + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量select M.orderDate 日期,L.itemType 类别,L. itemName 名称 ,L.theNumber 数量, L.theMoney 单价 ,L.theMoney * L.theNumber 总价 from orders M inner join orderItem L on M.orderId=L.orderId where L.theMoney >=5 and L.theNumber>50 +select L.orderId 订单编号,L.itemType 类别,count(*) 订购次数, SUM(L.theNumber)总数量 from orders M inner join orderItem L on M.orderId=L.orderId group by L.itemType,L.orderId + + --订单编号 产品类别 订购次数 总数量 + + -- 1 文具 2 82 + -- 1 体育用品 1 1 + -- 2 文具 2 56 + -- 2 体育用品 1 2 + -- 2 日常用品 1 20 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/2\346\235\250\346\242\246\346\236\227/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/2\346\235\250\346\242\246\346\236\227/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..df0c81b4beb6781098669fd87884b26975998ee4 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/2\346\235\250\346\242\246\346\236\227/SQLQuery3.sql" @@ -0,0 +1,110 @@ +create database dds + +on +( + name='dds', + filename='D:\dds.mdf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='dds_log', + filename='D:\dds_log.ldf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +go +use dds +go +--用户信息表 +create table bbsUsers +( + UIDD int identity, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +--板块表 +select * from bbsSection + +create table bbsSection +( + sIDD int identity, + sName varchar(10) not null, + sUid int +) + +alter table bbsUsers add constraint Pk_bbsUsers_UIDD primary key(UIDD) +alter table bbsUsers add constraint Uk_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint Ck_bbsUsers_uSex check(uSex='男'or uSex='女') +alter table bbsUsers add constraint Ck_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint Ck_bbsUsers_uPoint check(uPoint>=0) + +alter table bbsSection add constraint Pk_bbsSection_sIDD primary key(sIDD) +alter table bbsSection add constraint Fk_bbsSection_sUid foreign key(sUid) references bbsUsers(UIDD) + + +--主贴表 +drop table bbsTopic +create table bbsTopic +( + tID int identity primary key, + tUID int foreign key references bbsUsers(UIDD), + tSID int foreign key references bbsSection(sIDD), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) + +--回帖表 +drop table bbsReply +create table bbsReply +( + rID int identity primary key, + rUID int foreign key references bbsUsers(UIDD), + rTID int foreign key references bbsTopic(tID), + rMsg varchar(20) not null, + rTime datetime +) + +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select uName,uPoint into bbsPoint from bbsUsers +insert into bbsSection values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) +insert into bbsTopic values(2,4,'范跑跑 ',' 谁是范跑跑 ','2008-7-8','1'),(3,1,'.NET ',' 与JAVA的区别是什么呀? ','2008-9-1 ','2'), +(1,3,'今年夏天最流行什么 ',' 有谁知道今年夏天最流行 ','2008-9-10','0') +insert into bbsReply values(3,2,'狗屎超粘','2008-7-8'),(1,1,'不知道','2008-7-8'),(1,3,'黑丝','2008-7-8') + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号版主姓名和版块名称 +select * from bbsSection --板块表 + select * from bbsTopic + select M.sIDD 编号,L.uName 姓名,M.sName 版块 from bbsSection M inner join bbsUsers L on M.sIDD=L.UIDD + + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + select * from bbsUsers +select M.tid 编号 ,L.uName 姓名 ,M.tTitle 标题, M.tMsg 内容,M.tTime 时间 from bbsTopic M inner join bbsUsers L on M.tid=L.UIDD where tTime >'2008-9-15' + + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select M.sIDD 编号,L.uage 年龄,L.uName 姓名,M.sName 版块 from bbsSection M inner join bbsUsers L on M.sIDD=L.UIDD where L.uage<20 + + +--4.查询出回复数量最多的主贴的 发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select * from bbsReply + +select UIDD,uName,tTitle,tMsg,tCount from bbsTopic left join bbsUsers on bbsTopic.tID=bbsUsers.UIDD + + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select * from bbsSection +select * from bbsTopic +select * from bbsUsers +select uName 姓名,count(tTitle)总数,sName from bbsUsers inner join bbsTopic on bbsUsers.UIDD=bbsTopic.tUID +inner join bbsSection on bbsTopic.tUID=bbsSection.sUid group by tTitle,uName,sName diff --git "a/20210326\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3391faeca1aea17fb74555a9237f0a322f03e82b --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" @@ -0,0 +1,79 @@ +use master +go + +create database Student05 +go + +use Student05 +go + +create table StuIS +( + StuNO nvarchar(10) unique not null, + StuName nvarchar(10) not null, + StuAge int not null, + StuAddress nvarchar(200) , + StuSeat nvarchar(8) not null, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女') +) + +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2501','张秋利','20','美国硅谷','1','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2502','李斯文','18','湖北武汉','2','男') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2503','马文才','22','湖南长沙','3','男') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2504','欧阳俊雄','21','湖北武汉','3','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2505','梅超风','20','湖北武汉','4','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2506','陈旋风','19','美国硅谷','5','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2507','陈风','20','美国硅谷','7','女') + +create table Grate +( + ExamNo int primary key identity(1,1), + StuNo nvarchar(10) references StuIS(StuNO), + WExam int not null, + LabExam int not null +) +insert into Grate(StuNO,WExam,LabExam) +values('s2501','50','70') +insert into Grate(StuNO,WExam,LabExam) +values('s2502','60','65') +insert into Grate(StuNO,WExam,LabExam) +values('s2503','86','85') +insert into Grate(StuNO,WExam,LabExam) +values('s2504','40','80') +insert into Grate(StuNO,WExam,LabExam) +values('s2505','70','90') +insert into Grate(StuNO,WExam,LabExam) +values('s2506','85','90') + + +--数据如图片1,使用上次作业的数据 +select * from Grate +select * from StuIS +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select StuName,StuAge ,WExam,LabExam from StuIS +inner join Grate on StuIS.StuNO = Grate.StuNO + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuIS.StuNO ,StuName,WExam,LabExam from StuIS +inner join Grate on StuIS.StuNO = Grate.StuNO where Grate.WExam>=60 and Grate.LabExam>=60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select StuIS.StuNO ,StuName,WExam,LabExam from StuIS +left join Grate on StuIS.StuNO = Grate.StuNO + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select StuName,StuAge,WExam,LabExam from StuIS +inner join Grate on StuIS.StuNo = Grate.StuNO where StuIS.StuAge>=20 order by Grate.WExam DESC +--5.查询男女生的机试平均分 +select StuSex , avg(LabExam) from StuIS +inner join Grate on StuIS.StuNO = Grate.StuNO group by StuSex +--6.查询男女生的笔试总分 +select StuSex , sum(WExam) from StuIS +inner join Grate on StuIS.StuNO = Grate.StuNO group by StuSex diff --git "a/20210326\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b7b081e529146762ae37029f8b65e4680808c4ea --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery2.sql" @@ -0,0 +1,88 @@ +create database OrderIno +go + +use OrderIno +go + +create table orders +( + orderID int primary key identity(1,1), + orderDate datetime +) + +create table orderltem +( + ltemID int identity(1,1), + orderID int , + itemType nvarchar(15), + itemName nvarchar(10), + theNumber int , + theMoney money +) + +insert into orders (orderDate) values +('2008-01-12 00:00:00.000'), +('2008-02-10 00:00:00.000'), +('2008-02-15 00:00:00.000'), +('2008-03-10 00:00:00.000') + +insert into orderltem values +(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'生活用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3), +(3,'文具','订书机',20,3), +(3,'文具','订书机',10,3), +(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'生活用品','毛巾',4,5), +(4,'生活用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) + + +--使用上次作业的订单数据库,完成下列题目: + +select * from orders --订单表 +select * from orderltem --订购项目表 + +--订单表(orders)列为:订单编号(orderID 主键) 订购日期(orderDate) --订购项目表(orderItem),列为: --项目编号(ItemID)订单编号(orderID)产品类别(itemType) --产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orders.orderID 订单的编号 ,orderDate 订单日期, itemType 订购产品的类别,itemName 订购的产品名称 from orders +inner join orderltem on orders.orderID =orderltem.orderID + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orders.orderID 订单的编号 ,orderDate 订单日期, itemType 订购产品的类别,itemName 订购的产品名称 from orders +inner join orderltem on orders.orderID =orderltem.orderID where orderltem.theNumber >50 + + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderID 订单的编号 ,orderDate 订单日期, itemType 订购产品的类别,itemName 订购的产品名称, theNumber 订购数量 ,theMoney 订购单价 , theNumber*theMoney 订购总价 from orders +inner join orderltem on orders.orderID =orderltem.orderID + + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderID 订单的编号 ,orderDate 订单日期, itemType 订购产品的类别,itemName 订购的产品名称, theNumber 订购数量 ,theMoney 订购单价 , theNumber*theMoney 订购总价 from orders +inner join orderltem on orders.orderID =orderltem.orderID where orderltem.theMoney>=5 and orderltem.theNumber>=50 + + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderID 订单的编号 , count(*)订购产品数 from orderltem group by orderID + + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +-- 订单编号 产品类别 订购次数 总数量 +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select orderID 订单的编号 , itemType 产品类别 ,count(*)订购次数 , sum(theNumber) 总数量 + from orderltem group by orderID,itemType order by orderID diff --git "a/20210326\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..052fc54b16df2f64fd49bbe71192d5018fbc86b5 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery3.sql" @@ -0,0 +1,116 @@ +use master +go + +create database bbs +go + +use bbs +go + +create table bbsUsers +( + UID int identity(1,1) , + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +-- +alter table bbsUsers add constraint PK_bbsUser_UID primary key(UID) +-- +alter table bbsUsers add constraint UK_bbsUser_uName unique(uName) +-- +alter table bbsUsers add constraint CK_bbsUser_uSex check(uSex in('男','女')) +-- +alter table bbsUsers add constraint CK_bbsUser_uAge check(uAge>=15 or uAge<=60) +-- +alter table bbsUsers add constraint CK_bbsUser_uPoint check(uPoint>=0) +-- + +create table bbsSection +( + sID int identity(1,1), + sName varchar(10) not null, + sUid int +) + +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +-- +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUsers(UID) + + + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int foreign key references bbsUsers(UID), + tSID int foreign key references bbsSection(sID), + tTItle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) + + +create table bbsReply +( + rID INT primary key identity(1,1), + rUID int foreign key references bbsUsers(UID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUsers values +('小雨点','女',20,0), +('逍遥','男',18,4), +('七年级生','男',19,2) + + +select * into bbsPoint from bbsUsers + +insert into bbsSection values +('技术交流',1), +('读书世界',3), +('生活百科',1), +('八卦区',3) + +insert into bbsTopic values +(2,4,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?',2008-9-10,0), +(3,1,'.NET','与JAVA的区别是什么呀?',2008-9-1,2), +(1,3,'范跑跑','谁是范跑跑',2008-7-8,1) + +insert into bbsReply values +(2,1,'不知道',2008-9-10), +(3,2,'不知道',2008-9-1), +(1,3,'不知道',2008-7-8) + +select * from bbsReply --回帖表 +select * from bbsSection --版块表 +select * from bbsTopic --主帖表 +select * from bbsUsers --用户信息表 + +--在论坛数据库中完成以下题目 +--1.查询出 每个版块 的 版主编号, 版主姓名 和版块名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称 from bbsSection +inner join bbsUsers on bbsUsers.UID = bbsSection.sUid + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号,uName 发帖人姓名,tTItle 帖子的标题 ,tMsg 帖子的内容,tTime 发帖时间 from bbsTopic +inner join bbsUsers on bbsUsers.UID = bbsTopic.tUID where tTime>'2008-9-15' + + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称 from bbsSection +inner join bbsUsers on bbsUsers.UID = bbsSection.sUid where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select rUID 发帖人编号 , uName 发帖人姓名 ,tTItle 主贴标题 , tMsg 主贴内容 , tCount 回复数量 from bbsReply +inner join bbsTopic on bbsTopic.tUID = bbsReply.rUID +inner join bbsUsers on bbsUsers.UID = bbsReply.rUID where tCount=(select max(tCount) from bbsTopic) + +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select tSID 版块 , uName 发帖人姓名 , count(*) from bbsTopic +inner join bbsUsers on bbsUsers.UID = bbsTopic.tUID +inner join bbsSection on bbsUsers.UID =bbsSection.sID group by tSID ,uName + diff --git "a/20210326\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/\344\275\234\344\270\232\344\270\200.sql" "b/20210326\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/\344\275\234\344\270\232\344\270\200.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0a1164f59794ea95128bcdbc5619a295cb725632 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/\344\275\234\344\270\232\344\270\200.sql" @@ -0,0 +1,102 @@ +use master +go + +create database Student2 +on +( + name='Student2', + filename='D:\SQL作业\SQL作业4\Student2.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='Student2_log', + filename='D:\SQL作业\SQL作业4\Student2_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use Student2 +go +create table StuInfo +( + StuNo varchar(20) primary key , + StuName nchar(20) not null, + StuAge int not null, + StuAdress varchar(20) not null, + StuSeat int not null, + StuSex char(2) not null +) +go + +insert into StuInfo(StuNo,StuName,StuAge,StuAdress,StuSeat,StuSex) values +('s2501','张秋利',20,'美国硅谷',1,'女'),('s2502','李斯文',18,'湖北武汉',2,'男'), +('s2503','马文才',22,'湖南长沙',3,'女'),('s2504','欧阳俊雄',21,'湖北武汉',4,'男'), +('s2505','梅超风',20,'湖北武汉',5,'女'),('s2506','陈旋风',19,'美国硅谷',6,'女'), +('s2507','陈风',20,'美国硅谷',7,'男') +go + +insert into StuInfo(StuNo,StuName,StuAge,StuAdress,StuSeat,StuSex) values +('s2508','张三',25,'福建南平',3,'男'),('s2509','李四',18,'福建龙岩',2,'男'), +('s2510','王五',22,'湖南长沙',3,'男') +go + +select * from StuInfo + + +use Student2 +go +create table Score +( + ExamNo int primary key identity(1,1), + StuNO varchar(20) not null, + WrittenExam int not null, + LabExam int not null +) +go + +insert into Score(StuNo,WrittenExam,LabExam) values +('s2501',50,70),('s2502',60,65),('s2503',86,85), +('s2504',40,80),('s2505',70,90),('s2506',85,90) +go + +select * from Score +select * from StuInfo + + +--数据如图片1,使用上次作业的数据 + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select StuName 姓名,StuAge 年龄,WrittenExam 笔试成绩,LabExam 机试成绩 from +StuInfo inner join Score on StuInfo.StuNo = Score.StuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuInfo.StuNo 学号,StuName 姓名,WrittenExam 笔试成绩,LabExam 机试成绩 from StuInfo +inner join Score on StuInfo.StuNo = Score.StuNo where WrittenExam>60 and LabExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select StuInfo.StuNo 学号,StuName 姓名,WrittenExam 笔试成绩,LabExam 机试成绩 from StuInfo +left join Score on StuInfo.StuNo = Score.StuNo + + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select StuName 姓名,StuAge 年龄,WrittenExam 笔试成绩,LabExam 机试成绩 from StuInfo inner join Score on +StuInfo.StuNo = Score.StuNo where StuAge>20 order by WrittenExam desc + +--5.查询男女生的机试平均分 +select StuSex 性别,avg(LabExam)机试平均分 from StuInfo inner join Score on StuInfo.StuNo = Score.StuNo group by StuSex + + +--6.查询男女生的笔试总分 +select StuSex 性别,sum(WrittenExam)笔试总分 from StuInfo inner join Score on StuInfo.StuNo = Score.StuNo group by StuSex + + + + +select * from Score +select * from StuInfo \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/\344\275\234\344\270\232\344\270\211.sql" "b/20210326\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/\344\275\234\344\270\232\344\270\211.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c0ed109dc76e31fe842dacc7035332525c1e2189 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/\344\275\234\344\270\232\344\270\211.sql" @@ -0,0 +1,158 @@ +use master +go +create database bbs +on +( + name='bbs', + filename='D:\SQL作业\SQL作业6\bbs.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='bbs_log', + filename='D:\SQL作业\SQL作业6\bbs_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use bbs +create table bbsUsers +( + UID int identity(1,1), + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +go + +alter table bbsUsers add constraint PK_bbsUsers_UID primary key (UID) +alter table bbsUsers add constraint UK_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex='男' or uSex='女') +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint CK_bbsUsers_uPoint check(uPoint>=0) + + +use bbs +create table bbsSection +( + sID int identity(1,1), + sName varchar(10) not null, + sUid int +) +go + +alter table bbsSection add constraint PK_bbsSection_sID primary key (sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUsers(UID) + +use bbs +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int constraint FK_bbsUsers_UID references bbsUsers(UID), + tSID int constraint FK_bbsSection_sID references bbsSection(sID), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +go + +use bbs +create table bbsReply +( + rID int primary key identity(1,1), + rUID int constraint FK_bbsReply_rUID references bbsUsers(UID), + rTID int constraint FK_bbsTopic_tID references bbsTopic(tID), + rMsg text not null, + rTime datetime +) +go + + +--1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +--2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 +select uName,uPoint into bbsPoint from bbsUsers +--3.给论坛开设4个板块 +-- 名称 版主名 +-- 技术交流 小雨点 +-- 读书世界 七年级生 +-- 生活百科 小雨点 +-- 八卦区 七年级生 +insert into bbsSection values ('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + +select * from bbsUsers +--4.向主贴和回帖表中添加几条记录 + +-- 主贴: + +-- 发帖人 板块名 帖子标题 帖子内容 发帖时间 回复数量 +-- 逍遥 八卦区 范跑跑 谁是范跑跑 2008-7-8 1 +-- 七年级生 技术交流 .NET 与JAVA的区别是什么呀? 2008-9-1 2 +-- 小雨点 生活百科 今年夏天最流行什么 有谁知道今年夏天最流行 2008-9-10 0 +-- 什么呀? + +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values +(2,2,'范跑跑','谁是范跑跑','2008-7-8',1),(3,3,'.NET','与JAVA的区别是什么呀?','2008-9-1',2), +(1,4,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10',0) +--5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) +alter table bbsTopic drop constraint FK_bbsUsers_UID +delete from bbsUsers where uName='逍遥' +--6.因为小雨点发帖较多,将其积分增加10分 +update bbsUsers set uPoint=10 where uName='小雨点' +--7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) +alter table bbsTopic drop constraint FK_bbsSection_sID +delete from bbsSection where sName='生活百科' +--8.因回帖积累太多,现需要将所有的回帖删除 +delete from bbsReply + + +select * from bbsTopic +select * from bbsSection +select * from bbsUsers +select * from bbsReply + + + + + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select UID 版主编号,uName 版主姓名,sName 板块名称 from bbsUsers inner join bbsSection +on bbsUsers.UID = bbsSection.sUid + + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select UID 编号,uName 姓名,tTitle 标题,tMsg 内容,tTime 时间 from bbsUsers inner join bbsTopic +on bbsUsers.UID = bbsTopic.tUID where tTime>'2008-9-15' + + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select UID 版主编号,uName 板主名称,sName 板块名称 from bbsUsers inner join bbsSection +on bbsUsers.UID = bbsSection.sUid where uAge<20 + + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 UID 发帖人编号,uName 姓名,tTitle 标题,tMsg 内容,tCount 回复数量 from bbsUsers inner join bbsTopic +on bbsUsers.UID = bbsTopic.tUID order by tCount desc + + +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select UID 发帖人编号,uName 用户名称,sName 板块名称,count(tID) 发帖总数 from bbsTopic inner join bbsUsers +on bbsTopic.tUID = bbsUsers.UID inner join bbsSection +on bbsTopic.tUID = bbsSection.sUid group by UID,uName,sName + + + +select * from bbsTopic +select * from bbsSection +select * from bbsUsers +select * from bbsReply \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/\344\275\234\344\270\232\344\272\214.sql" "b/20210326\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/\344\275\234\344\270\232\344\272\214.sql" new file mode 100644 index 0000000000000000000000000000000000000000..24ef4651be49f25bb940d7eca50c615e4b8a0b2c --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/\344\275\234\344\270\232\344\272\214.sql" @@ -0,0 +1,107 @@ +use master +go + +create database order1 +on +( + name='order1', + filename='D:\SQL作业\SQL作业8\order1.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='order1_log', + filename='D:\SQL作业\SQL作业8\order1_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use order1 +go +create table orders +( + orderId int primary key identity(1,1), + orderDate date +) +go + +insert into orders values('2008-01-12 00:00:00.000'),('2008-02-10 00:00:00.000'), +('2008-02-15 00:00:00.000'),('2008-03-10 00:00:00.000') + +select * from orders--订单表 + + +use order1 +go +create table orderItem +( + ItemiD int, + orderId int constraint FK_orderItem_orderId foreign key references orders(orderId), + itemType nchar(15), + itemName char(20), + theNumber int, + theMoney money +) +go + +insert into orderItem values +(1,1,'文具','笔',72,2),(2,1,'文具','尺',10,1),(3,1,'体育用品','篮球',1,56),(4,2,'文具','笔',36,2), +(5,2,'文具','固体胶',20,3),(6,2,'日常用品','透明胶',2,1),(7,2,'体育用品','羽毛球',20,3),(8,3,'文具','订书机',20,3), +(9,3,'文具','订书针',10,3),(10,3,'文具','裁纸刀',5,5),(11,4,'文具','笔',20,2),(12,4,'文具','信纸',50,1), +(13,4,'日常用品','毛巾',4,5),(14,1,'日常用品','透明胶',30,1),(15,4,'体育用品','羽毛球',20,3) + + + +select * from orderItem +select * from orders + + + +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orderItem.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 数量,theMoney 单价 + from orderItem inner join orders on orderItem.orderId = orders.orderId + + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orderItem.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称 from orderItem inner +join orders on orderItem.orderId = orders.orderId where theNumber>50 + + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量, +theMoney 订购单价,theMoney*theNumber 订购总价 from orderItem inner join orders on +orderItem.orderId = orders.orderId group by orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量, +theMoney 订购单价,theNumber*theMoney 订购总价 from orderItem inner join orders +on orderItem.orderId = orders.orderId where theMoney>=5 and theNumber>=50 + + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 + +select orderId 编号,count(*)订购产品数 from orderItem group by orderId + + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select orderId 订单编号,itemType 产品类别,count(*)订购次数,sum(theNumber)总数量 +from orderItem group by orderId,itemType order by orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ea5bb0ebd885ad35ba8bac3f8bbb5a0a37a47130 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery1.sql" @@ -0,0 +1,59 @@ +create database Student +go +use Student +go + +select * from stuclass +select * from examclass +create table stuclass +( + stuNO varchar(10) primary key , + stuName varchar(10), + stuAge int, + stuAddress nvarchar(50), + stuSeat int, + stuSex int, +) +select * from stuclass +insert into stuclass values ('s2501','张秋利',20,'美国硅谷',1,1),('s2502','李斯文',18,'湖北武汉',2,0),('s2503','马文才',22,'湖南长沙',3,1), +('s2504','欧阳俊雄',21,'湖北武汉',4,0),('s2505','梅超风',20,'湖北武汉',5,1),('s2506','陈旋风',19,'美国硅谷',6,1),('s2507','陈风',20,'美国硅谷',7,0) + + + +create table examclass +( + examNo int primary key identity(1,1), + stuNo varchar(10) references stuclass(stuNO), + writtenExam int , + labExam int , +) +select * FROM DBO.stuclass +select * from examclass inner join stuclass on examclass.stuNo = stuclass.stuNO +insert into examclass values ('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80), +('s2505',70,90),('s2506',85,90) + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +--内连接 +select stuclass.stuName 姓名, stuclass.stuAge 年龄,examclass.writtenExam 笔试成绩,examclass.labExam 机试成绩 from stuclass +inner join examclass on stuclass.stuNO = examclass.stuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select stuclass.stuNO 学号 ,stuclass.stuName 姓名, stuclass.stuAge 年龄, examclass.writtenExam 笔试成绩, examclass.labExam 机试成绩 from examclass +inner join stuclass on stuclass.stuNO = examclass.stuNo where writtenExam>60 and labExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select stuclass.stuNO 学号 ,stuclass.stuName 姓名, stuclass.stuAge 年龄, examclass.writtenExam 笔试成绩, examclass.labExam 机试成绩 from examclass +right join stuclass on stuclass.stuNO = examclass.stuNo + + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuclass.stuName 姓名, stuclass.stuAge 年龄,examclass.writtenExam 笔试成绩,examclass.labExam 机试成绩 from stuclass +inner join examclass on stuclass.stuNO = examclass.stuNo where writtenExam>=20 order by writtenExam DESC + +--5.查询 男女生 的 机试平均分 +select stuSex ,avg(labExam)机试平均分 from stuclass inner join examclass on stuclass.stuNO = examclass.stuNo group by stuSex + + +--6.查询男女生的笔试总分 + +select stuSex,sum(writtenExam) 笔试总分 from stuclass inner join examclass on stuclass.stuNO = examclass.stuNo group by stuSex diff --git "a/20210326\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..12cadecb67b0e3c83754eef8d357ab2bedb3bcd6 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery2.sql" @@ -0,0 +1,67 @@ +create database Demo_01 +go +use Demo_01 +go +create table orders +( + orderId int primary key identity not null, + orderDate datetime not null, +) + +insert into orders values('2008-01-12'),('2008-02-10'),('2008-02-15'),('2008-03-10') + + +create table orderItem +( + ItemiD int primary key identity not null, + orderId int references orders(orderId) not null, + itemType nvarchar(10) not null, + itemName nvarchar(10) not null, + theNumber int not null, + theMoney int not null, +) +insert into orderItem values(1,'文具','笔',72,2),(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56),(2,'文具','笔',36,2),(2,'文具','固体胶',20,3),(2,'日常用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3),(3,'文具','订书机',20,3),(3,'文具','订书针',10,3),(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2),(4,'文具','信纸',50,1),(4,'日常用品','毛巾',4,5),(4,'日常用品','透明胶',30,1),(4,'体育用品','羽毛球',20,3) + +select * from orders +select * from orderItem + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别, itemName 产品名称, +theNumber 订购数量,theMoney 订购单价 from orders inner join orderItem on orders.orderId = orderItem.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orderItem.theNumber 订购数量,orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别, itemName 产品名称 +from orders inner join orderItem on orders.orderId = orderItem.orderId where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId 订单编号,orderDate 订单日期,itemType 产品类别, itemName 产品名称,theNumber 订购数量,theMoney 订购单价, +(theNumber*theMoney) 订购总价 from orders inner join orderItem on orders.orderId = orderItem.orderId + +--4.查询 单价大于等于5 并且 数量大于等于50 的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select theMoney,orderDate 订单日期,itemType 产品类别, itemName 产品名称,orderItem.theNumber 订购数量,theMoney 订购单价, +(theNumber*theMoney) 订购总价 from orderItem inner join orders on orderItem.orderId = orders.orderId where theMoney>=5 and theNumber>=50 + +--5.查询每个订单分别订购了几个产品,例如: +--编号 订购产品数 +--1 3 +--2 4 +select orderId,count(*)订购产品数 from orderItem group by orderId + +--6.查询 每个订单里的 每个类别的产品 分别订购了几次 和 总数量,例如: + + --订单编号 产品类别 订购次数 总数量 + +--1 文具 2 82 +--1 体育用品 1 1 +--2 文具 2 56 +--2 体育用品 1 2 +--2 日常用品 + +select orders.orderId 订单编号,itemType 产品类别, count(*)订购次数,sum(theNumber)总数量 from orderItem inner join +orders on orders.orderId=orderItem.orderId group by itemType,orders.orderId + +select orderId 订单编号,itemType 产品类别, count(*)订购次数,sum(theNumber)总数量 from orderItem +group by orderId,itemType \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ff452f5e250b0e332814e0b9a6d51276a5b70a56 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery3.sql" @@ -0,0 +1,106 @@ +use master +go +create database bbs +on( + name='bbs', + filename='C:\text\bbs.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on( + name='bbs_log', + filename='C:\text\bbs_log.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +go +use bbs +go + +--用户信息表 +create table bbsUsers +( + uID int identity(1,1) not null , + uName nvarchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null, + +) +alter table bbsUsers add constraint PK_bbsUsers_uID primary key(uID) +alter table bbsUsers add constraint UK_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex='男' or uSex='女') +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 or uAge<=60) +alter table bbsUsers add constraint CK_bbsUsers_uPoint check(uPoint>=0) + +--板块表 +create table bbsSection +( + sID int identity(1,1) not null , + sName varchar(10) not null, + sUid int , + +) +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid)references bbsUsers(uID) + +--主贴表 +create table bbsTopic +( + tID int primary key identity(1,1) not null, + tUID int references bbsUsers(uID) not null , + tSID int references bbsSection(sID) , + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime not null, + tCount int not null, +) + +--回贴表 +create table bbsReply +( + rID int primary key identity(1,1) not null, + rUID int references bbsUsers(uID) , + rTID int references bbsTopic(tID) , + rMsg text not null, + rTime datetime , +) +select * from bbsReply +select * from bbsSection +select * from bbsUsers +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select uName,uPoint into bbsPoint_backup from bbsUsers +insert into bbsSection values('技术名流',1),('读书世界',3),('生活百科',1),('八卦区',3) +insert into bbsTopic values(2,4,'范跑跑!','谁是范跑跑!', '2008-7-8',1),(3,2,'.NET!','与JAVA的区别是什么呀?','2008-9-1',2),(1,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10',0) +insert into bbsReply values(3,1,'一名地震自己先跑的教师','2008-7-8'),(1,2,'不知道','2008-9-15'),(2,2,'Java更难','2008-9-20') +update bbsUsers set uPoint=30 where uName='小雨点' + + +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select bbsUsers.uID,bbsUsers.uName 版主姓名,bbsSection.sName 板块名称 from bbsSection right join bbsUsers +on bbsUsers.uID = bbsSection.sUid + + + +--2.查询出主贴的发帖时间在2008-8-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select * from bbsTopic +select bbsTopic.tID 发帖人编号,bbsUsers.uName 发帖人姓名,tTitle 帖子标题, +tMsg 帖子内容,tTime 发帖时间 from bbsTopic inner join bbsUsers on bbsTopic.tUID = bbsUsers.UID +where tTime>='2008-8-15' + + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select * from bbsUsers +select bbsUsers.uID 版主编号,bbsUsers.uName 版主姓名,bbsSection.sName 板块名称 from bbsUsers left join bbsSection +on bbsUsers.uID = bbsSection.sUid where uAge<=20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select tUID 发帖人编号,bbsUsers.uName 发帖人姓名,bbsTopic.tTitle 主贴标题, bbsTopic.tMsg 主贴内容, +bbsTopic.tCount 回复数量 from bbsTopic inner join bbsUsers on uID=tUID order by tCount DESC + +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select * from bbsTopic +select tID,sum(tID)发帖总数 from bbsTopic inner join bbsReply +on bbsTopic.tID = bbsReply.rTID group by tID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\344\275\234\344\270\2321.sql" "b/20210326\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..16968f581b2f792d1eb45f669a2c4899637ae310 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\344\275\234\344\270\2321.sql" @@ -0,0 +1,108 @@ +use master +go + +create database Student05 +go + +use Student05 +go + +create table StuIS +( + StuNO nvarchar(10) unique not null, + StuName nvarchar(10) not null, + StuAge int not null, + StuAddress nvarchar(200) , + StuSeat nvarchar(8) not null, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女') +) + +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2501','张秋利','20','美国硅谷','1','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2502','李斯文','18','湖北武汉','2','男') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2503','马文才','22','湖南长沙','3','男') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2504','欧阳俊雄','21','湖北武汉','3','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2505','梅超风','20','湖北武汉','4','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2506','陈旋风','19','美国硅谷','5','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2507','陈风','20','美国硅谷','7','女') + +select *from StuIS +select 学号=StuNO ,姓名=StuName,年龄=StuAge,地址=StuAddress,座位号=StuSeat,性别=StuSex from StuIS +select Stuname,StuAge,StuAddress from StuIS +select 学号=StuNO ,姓名=StuName,地址=StuAddress,邮箱=StuName+StuAddress from StuIS +select distinct StuAddress from StuIS +select distinct StuAge as 所有年龄 from StuIS +select top 3 *from StuIS +select top 4 StuName,StuSeat from StuIS +select top 50 percent *from StuIS +select * from StuIS where StuAddress in ('湖北武汉','湖南长沙') +select * from StuIS where StuAddress='湖北武汉' or StuAddress ='湖南长沙' +select * from StuIS where StuAge is null +select * from StuIS where StuAge is not null +select * from StuIS where StuName like '张%' +select * from StuIS where StuAddress like '%湖%' +select * from StuIS where StuName like '张_' +select * from StuIS where StuName like '__俊%' +select * from StuIS order by StuAge DESC +select * from StuIS order by StuAge DESC , StuSeat ASC + + +create table Grate +( + ExamNo int primary key identity(1,1), + StuNo nvarchar(10) references StuIS(StuNO), + WExam int not null, + LabExam int not null +) +insert into Grate(StuNO,WExam,LabExam) +values('s2501','50','70') +insert into Grate(StuNO,WExam,LabExam) +values('s2502','60','65') +insert into Grate(StuNO,WExam,LabExam) +values('s2503','86','85') +insert into Grate(StuNO,WExam,LabExam) +values('s2504','40','80') +insert into Grate(StuNO,WExam,LabExam) +values('s2505','70','90') +insert into Grate(StuNO,WExam,LabExam) +values('s2506','85','90') + +select *from Grate +select 学号=StuNO , 笔试=WExam,机试=LabExam from Grate +select StuNo as 学号 , WExam as 笔试, LabExam as 机试 from Grate +select StuNo 学号 ,WExam 笔试,LabExam 机试 from Grate +select StuNO, WExam ,LabExam,总分=WExam+LabExam from Grate +select * from Grate where WExam<70 or WExam>90 order by WExam DESC +select top 1 * from Grate order by WExam DESC +select top 1 * from Grate order by WExam ASC + +select * from StuIS +select * from Grate + + +--数据如图片1,使用上次作业的数据 + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select StuName 姓名,StuAge 年龄,WExam 笔试成绩,LabExam 机试成绩 from StuIS inner join Grate on StuIS.StuNO=Grate.StuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuIS.StuNO,StuName,WExam,LabExam from StuIS left join Grate on StuIS.StuNO=Grate.StuNo where WExam>60 and LabExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select StuName 姓名,WExam 笔试成绩,LabExam 机试成绩 from StuIS left join Grate on StuIS.StuNO=Grate.StuNo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select StuName 姓名,StuAge 年龄, WExam 笔试成绩,LabExam 机试成绩 from StuIS + inner join Grate on StuIS.StuNO=Grate.StuNo where StuAge>=20 order by WExam DESC + +--5.查询男女生的机试平均分 +select StuSex,AVG(LabExam)机试平均分 from StuIS inner join Grate on StuIS.StuNO= Grate.StuNo group by StuSex + +--6.查询男女生的笔试总分 +select StuSex,sum(WExam)笔试总分 from StuIS inner join Grate on StuIS.StuNO=Grate.StuNo group by StuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\344\275\234\344\270\2322.sql" "b/20210326\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2b17333d6f019d9e67a9ba354ab3e948f361954a --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\344\275\234\344\270\2322.sql" @@ -0,0 +1,110 @@ +use master +go + +create database ordersystem +on +( + name='ordersystem', + filename='D:\数据库\ordersystem.mdf', + size=5mb, + maxsize=100mb, + filegrowth=15% +) + +log on +( + name='bank_log', + filename='D:\数据库\ordersystem_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=15% +) + +--订单表(orders)列为:订单编号(orderId 主键) 订购日期(orderDate) +use ordersystem +go + + create table orders + ( + orderId int primary key identity(1,1), + orderDate datetime + ) + +--订购项目表(orderItem),列为: +--项目编号(ItemiD)订单编号(orderId)产品类别(itemType) +--产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) +create table orderItem +( + ItemiD int primary key identity, + orderId nchar(10), + itemType nvarchar(8), + itemName varchar(10), + theNumber int, + theMoney int +) + + +select * from orders +insert into orders values +('2008-01-02 00:00:00.000'), +('2008-02-10 00:00:00.000'), +('2008-02-15 00:00:00.000'), +('2008-03-10 00:00:00.000') + +select * from orderItem +insert into orderItem values +('1','文具','笔','72','2'), +('1','文具','笔','10','1'), +('1','体育用品','篮球','1','56'), +('2','文具','笔','36','2'), +('2','文具','固体胶','20','3'), +('2','日常用品','透明胶','2','1'), +('2','体育用品','羽毛球','20','3'), +('3','文具','订书机','20','3'), +('3','文具','订书针','10','3'), +('3','文具','裁纸刀','5','5'), +('4','文具','笔','20','2'), +('4','文具','信纸','50','1'), +('4','日常用品','毛巾','4','5'), +('4','日常用品','透明胶','30','1'), +('4','体育用品','羽毛球','30','3') + + +--使用上次作业的订单数据库,完成下列题目: + +select * from orders--订单表 +select * from orderItem--订购项目表 + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney from orderItem left join +orders on orderItem.orderId=orders.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orderItem.orderId,orderDate,itemType,itemName from orderItem inner join + orders on orderItem.orderId=orders.orderId where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney,theNumber*theMoney 订购总价 +from orderItem left join orders on orderItem.orderId=orders.orderId + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney,theNumber*theMoney 订购总价 +from orderItem left join orders on orderItem.orderId=orders.orderId where theMoney>5 and theNumber>50 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderId,COUNT(theNumber)产品数 from orderItem group by orderId + +---6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +--订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select orderId,itemType,COUNT(theNumber)订购次数,sum(theNumber)总数量 from orderItem +group by orderId,itemType order by orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\344\275\234\344\270\2323.sql" "b/20210326\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\344\275\234\344\270\2323.sql" new file mode 100644 index 0000000000000000000000000000000000000000..34dbdcdbc87c403af01d93546f35f01cddf6bdb8 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\344\275\234\344\270\2323.sql" @@ -0,0 +1,134 @@ +use master +go + +create database bbs +on +( + name='bbs', + filename='D:\数据库\bbs.mdf', + size=10mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='bbs_log', + filename='D:\数据库\bbs_log.ldf', + size=10mb, + maxsize=100mb, + filegrowth=10mb +) + +use bbs +go + +create table bbsUsers +( + UID int not null, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +alter table bbsUsers add constraint PK primary key (UID) +alter table bbsUsers add constraint FK unique (uName) +alter table bbsUsers add constraint CK check(uSex='男' or uSex='女') +alter table bbsUsers add constraint DK check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint UK check(uPoint>=0) + + +create table bbsSection +( + sID int not null, + sName varchar(10) not null, + sUid int not null +) +alter table bbsSection add constraint BK primary key(sID) +alter table bbsSection add constraint NK foreign key (sUid) references bbsUsers( UID) + + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int references bbsUsers(UID), + tSID int references bbsSection(sID), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +create table bbsReply +( + rID int primary key identity(1,1), + rUID int references bbsUsers(UID), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +select * from bbsUsers +insert into bbsUsers(UID,uName,uSex,uAge,uPoint) +values(1,'小雨点','女','20','0') +insert into bbsUsers(UID,uName,uSex,uAge,uPoint) +values(2,'逍遥','男','18','4') +insert into bbsUsers(UID,uName,uSex,uAge,uPoint) +values(3,'七年级生','男','19','2') + +select * from bbsSection +insert into bbsSection(sID,sName,sUid) +values(5,'技术交流',1) +insert into bbsSection(sID,sName,sUid) +values(6,'读书世界',3) +insert into bbsSection(sID,sName,sUid) +values(7,'生活百科',1) +insert into bbsSection(sID,sName,sUid) +values(8,'八卦区',3) + +select * from bbsTopic +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +values(2,8,'范跑跑','谁是范跑跑','2008-7-8',1) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +values(3,5,'.NET','与JAVA的区别是什么呀?','2008-9-1',2) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +values(1,7,'今年夏天最流行什么 ','有谁知道今年夏天最流行什么呀?','2008-9-10',0) + +select * from bbsReply +insert into bbsReply(rUID,rTID,rMsg,rTime) +values(1,3,'范跑跑','2008-10-2') +insert into bbsReply(rUID,rTID,rMsg,rTime) +values(2,2,'区别不大','2008-10-2') +insert into bbsReply(rUID,rTID,rMsg,rTime) +values(3,1,'蓝色','2008-10-2') + + + +--在论坛数据库中完成以下题目 +--1.查询出 每个版块 的 版主编号,版主姓名和版块名称 +select * from bbsUsers +select * from bbsTopic +select sUid 版主编号,uName 版主姓名,sName 版块名称 from bbsSection inner join bbsTopic on sID=tSID +inner join bbsUsers on tUID=UID + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select * from bbsTopic +select tUID 发帖人编号,uName 发帖人姓名,tTitle 帖子的标题,tMsg 帖子的内容,tTime 发帖时间 +from bbsUsers inner join bbsTopic on UID=tUID where tCount>2008-9-15 + + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select * from bbsUsers +select * from bbsSection +select sUid 版主编号,uName 版主的名称,sName 版主编号 from bbsSection inner join bbsUsers on sUid=UID where uAge<20 + + +--4.查询出 回复数量最多 的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select * from bbsUsers +select tUID 发帖人编号,uName 姓名,tTitle 主贴标题,tMsg 主贴内容,tCount 回复数量 +from bbsTopic inner join bbsUsers on tUID=UID + +--5.在 主贴表中 查询 每个版块中 每个用户 的 发帖总数 +select * from bbsTopic--主贴表 +select * from bbsSection--版块表 +select * from bbsUsers--用户信息表 +select UID 用户编号,uName 用户名,sID 版块编号,count(sID)发帖总数 from bbsTopic inner join bbsUsers on tUID=UID inner join +bbsSection on sUid=UID group by UID,uName,sID diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" new file mode 100644 index 0000000000000000000000000000000000000000..38c0369a0ab4fb1d4f0359eaf730f11e058f32d4 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" @@ -0,0 +1,474 @@ +USE [master] +GO +/****** Object: Database [TestDB] Script Date: 2021/3/15 16:11:24 ******/ +CREATE DATABASE [TestDB] + CONTAINMENT = NONE + ON PRIMARY +( NAME = N'TestDB', FILENAME = N'D:\TestDB.mdf' , SIZE = 4288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) + LOG ON +( NAME = N'TestDB_log', FILENAME = N'D:\TestDB_log.ldf' , SIZE = 1072KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) +GO +ALTER DATABASE [TestDB] SET COMPATIBILITY_LEVEL = 120 +GO +IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) +begin +EXEC [TestDB].[dbo].[sp_fulltext_database] @action = 'enable' +end +GO +ALTER DATABASE [TestDB] SET ANSI_NULL_DEFAULT OFF +GO +ALTER DATABASE [TestDB] SET ANSI_NULLS OFF +GO +ALTER DATABASE [TestDB] SET ANSI_PADDING OFF +GO +ALTER DATABASE [TestDB] SET ANSI_WARNINGS OFF +GO +ALTER DATABASE [TestDB] SET ARITHABORT OFF +GO +ALTER DATABASE [TestDB] SET AUTO_CLOSE OFF +GO +ALTER DATABASE [TestDB] SET AUTO_SHRINK OFF +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS ON +GO +ALTER DATABASE [TestDB] SET CURSOR_CLOSE_ON_COMMIT OFF +GO +ALTER DATABASE [TestDB] SET CURSOR_DEFAULT GLOBAL +GO +ALTER DATABASE [TestDB] SET CONCAT_NULL_YIELDS_NULL OFF +GO +ALTER DATABASE [TestDB] SET NUMERIC_ROUNDABORT OFF +GO +ALTER DATABASE [TestDB] SET QUOTED_IDENTIFIER OFF +GO +ALTER DATABASE [TestDB] SET RECURSIVE_TRIGGERS OFF +GO +ALTER DATABASE [TestDB] SET ENABLE_BROKER +GO +ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF +GO +ALTER DATABASE [TestDB] SET DATE_CORRELATION_OPTIMIZATION OFF +GO +ALTER DATABASE [TestDB] SET TRUSTWORTHY OFF +GO +ALTER DATABASE [TestDB] SET ALLOW_SNAPSHOT_ISOLATION OFF +GO +ALTER DATABASE [TestDB] SET PARAMETERIZATION SIMPLE +GO +ALTER DATABASE [TestDB] SET READ_COMMITTED_SNAPSHOT OFF +GO +ALTER DATABASE [TestDB] SET HONOR_BROKER_PRIORITY OFF +GO +ALTER DATABASE [TestDB] SET RECOVERY FULL +GO +ALTER DATABASE [TestDB] SET MULTI_USER +GO +ALTER DATABASE [TestDB] SET PAGE_VERIFY CHECKSUM +GO +ALTER DATABASE [TestDB] SET DB_CHAINING OFF +GO +ALTER DATABASE [TestDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) +GO +ALTER DATABASE [TestDB] SET TARGET_RECOVERY_TIME = 0 SECONDS +GO +ALTER DATABASE [TestDB] SET DELAYED_DURABILITY = DISABLED +GO +EXEC sys.sp_db_vardecimal_storage_format N'TestDB', N'ON' +GO +USE [TestDB] +GO +/****** Object: Table [dbo].[ClassInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[ClassInfo]( + [ClassId] [int] IDENTITY(1,1) NOT NULL, + [ClassName] [nvarchar](20) NOT NULL, +PRIMARY KEY CLUSTERED +( + [ClassId] 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].[CourseInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[CourseInfo]( + [CourseId] [int] IDENTITY(1,1) NOT NULL, + [CourseName] [nvarchar](50) NOT NULL, + [CourseCredit] [int] NULL DEFAULT ((1)), +PRIMARY KEY CLUSTERED +( + [CourseId] 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].[Scores] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[Scores]( + [ScoreId] [int] IDENTITY(1,1) NOT NULL, + [StuId] [int] NULL, + [CourseId] [int] NULL, + [Score] [int] NULL DEFAULT ((0)), +PRIMARY KEY CLUSTERED +( + [ScoreId] 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].[StuInfo] Script Date: 2021/3/15 16:11:24 ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +CREATE TABLE [dbo].[StuInfo]( + [StuId] [int] IDENTITY(1,1) NOT NULL, + [ClassId] [int] NULL, + [StuName] [nvarchar](10) NOT NULL, + [StuSex] [nvarchar](1) NULL DEFAULT ('男'), + [StuBrithday] [date] NULL, + [StuPhone] [nvarchar](11) NULL, + [StuProvince] [nvarchar](200) NULL, + [CreateDate] [datetime] NULL DEFAULT (getdate()), + [StuAge] [int] NULL, +PRIMARY KEY CLUSTERED +( + [StuId] 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].[ClassInfo] ON + +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (1, N'软件1班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (2, N'软件2班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (3, N'软件3班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (4, N'软件4班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (5, N'软件5班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (6, N'软件6班') +GO +INSERT [dbo].[ClassInfo] ([ClassId], [ClassName]) VALUES (7, N'软件7班') +GO +SET IDENTITY_INSERT [dbo].[ClassInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] ON + +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (1, N'计算机基础', 3) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (2, N'HTML+CSS网页制作', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (3, N'JAVA编程基础', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (4, N'SQL Server数据库基础', 4) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (5, N'C#面向对象编程', 5) +GO +INSERT [dbo].[CourseInfo] ([CourseId], [CourseName], [CourseCredit]) VALUES (6, N'Winform桌面应用程序设计', 5) +GO +SET IDENTITY_INSERT [dbo].[CourseInfo] OFF +GO +SET IDENTITY_INSERT [dbo].[Scores] ON + +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (1, 1, 1, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (2, 1, 2, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (3, 1, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (4, 1, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (5, 2, 1, 60) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (6, 2, 2, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (7, 2, 3, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (8, 2, 4, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (9, 3, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (10, 3, 2, 45) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (11, 3, 3, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (12, 3, 4, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (13, 4, 1, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (14, 4, 2, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (15, 4, 3, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (16, 4, 4, 66) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (17, 5, 1, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (18, 5, 2, 79) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (19, 5, 3, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (20, 5, 4, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (21, 6, 1, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (22, 6, 2, 88) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (23, 6, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (24, 6, 5, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (25, 7, 1, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (26, 7, 2, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (27, 7, 3, 92) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (28, 7, 5, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (29, 8, 1, 58) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (30, 8, 2, 59) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (31, 8, 3, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (32, 8, 5, 75) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (33, 9, 1, 48) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (34, 9, 2, 67) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (35, 9, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (36, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (37, 9, 5, 56) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (38, 1, 1, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (39, 1, 2, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (40, 1, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (41, 1, 4, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (42, 2, 1, 65) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (43, 2, 2, 82) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (44, 2, 3, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (45, 2, 4, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (46, 3, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (47, 3, 2, 50) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (48, 3, 3, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (49, 3, 4, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (50, 4, 1, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (51, 4, 2, 85) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (52, 4, 3, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (53, 4, 4, 71) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (54, 5, 1, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (55, 5, 2, 84) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (56, 5, 3, 77) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (57, 5, 4, 90) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (58, 6, 1, 73) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (59, 6, 2, 93) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (60, 6, 3, 78) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (61, 6, 5, 68) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (62, 7, 1, 89) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (63, 7, 2, 95) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (64, 7, 3, 97) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (65, 7, 5, 83) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (66, 8, 1, 63) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (67, 8, 2, 64) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (68, 8, 3, 70) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (69, 8, 5, 80) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (70, 9, 1, 53) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (71, 9, 2, 72) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (72, 9, 3, 76) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (73, 9, 5, 61) +GO +INSERT [dbo].[Scores] ([ScoreId], [StuId], [CourseId], [Score]) VALUES (74, 9, 5, 61) +GO +SET IDENTITY_INSERT [dbo].[Scores] OFF +GO +SET IDENTITY_INSERT [dbo].[StuInfo] ON + +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (1, 1, N'刘正', N'男', CAST(N'2002-08-02' AS Date), N'13245678121', N'广西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (2, 1, N'黄贵', N'男', CAST(N'2003-07-02' AS Date), N'13345678121', N'江西省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (3, 1, N'陈美', N'女', CAST(N'2002-07-22' AS Date), N'13355678125', N'福建省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (4, 2, N'江文', N'男', CAST(N'2001-07-02' AS Date), N'13347678181', N'湖南省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (5, 2, N'钟琪', N'女', CAST(N'2004-01-13' AS Date), N'13345778129', N'安徽省', CAST(N'2021-03-14 16:46:00.887' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (6, 3, N'曾小林', N'男', CAST(N'2005-05-15' AS Date), N'13345378563', N'安徽省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (7, 3, N'欧阳天天', N'女', CAST(N'2000-08-19' AS Date), N'13347878121', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (8, 3, N'李逍遥', N'男', CAST(N'1999-09-02' AS Date), N'13345678557', N'广东省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 22) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (9, 4, N'刘德华', N'男', CAST(N'1995-06-11' AS Date), N'15345679557', N'福建省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 26) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (10, 4, N'刘翔', N'男', CAST(N'1996-07-09' AS Date), N'18346679589', N'江西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (11, 4, N'曾小贤', N'男', CAST(N'2003-07-02' AS Date), N'18348979589', N'湖南省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (12, 5, N'刘德华', N'男', CAST(N'2002-07-02' AS Date), N'18348979509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (13, 5, N'陈天翔', N'男', CAST(N'2003-07-02' AS Date), N'18348079509', N'湖北省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (14, 5, N'刘能', N'男', CAST(N'2005-08-02' AS Date), N'13245678122', N'广西省', CAST(N'2021-03-14 16:46:00.890' AS DateTime), 16) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (15, 5, N'钟馗', N'男', CAST(N'2004-08-02' AS Date), N'13245678123', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 17) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (16, 5, N'钟吴艳', N'女', CAST(N'2002-08-02' AS Date), N'13245678124', N'广西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (17, 5, N'刘欢', N'男', CAST(N'2001-07-02' AS Date), N'13245678125', N'湖南省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 20) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (18, 5, N'张庭', N'女', CAST(N'2000-07-02' AS Date), N'13245678126', N'江西省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (19, 5, N'曹植', N'男', CAST(N'2000-08-02' AS Date), N'13245678127', N'福建省', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 21) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (20, 5, N'曹操', N'男', CAST(N'2002-08-02' AS Date), N'13245678128', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (21, 5, N'孙尚香', N'女', CAST(N'2003-08-02' AS Date), N'13245678129', N'', CAST(N'2021-03-14 16:46:00.893' AS DateTime), 18) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (22, 3, N'老1', N'女', CAST(N'2002-08-02' AS Date), N'13245678130', N'广东省', CAST(N'2021-03-14 17:02:36.347' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (24, 2, N'老2', N'男', CAST(N'2002-08-03' AS Date), N'13345678945', NULL, CAST(N'2021-03-14 17:03:37.733' AS DateTime), 19) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (25, 4, N'老3', N'男', NULL, N'13645987545', N'广东省', CAST(N'2021-03-14 17:03:43.307' AS DateTime), NULL) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (28, 5, N'老4', N'男', CAST(N'2006-03-05' AS Date), N'13456987456', NULL, CAST(N'2021-03-14 17:04:03.957' AS DateTime), 15) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (29, 5, N'老5', N'女', CAST(N'1998-04-12' AS Date), N'15978456123', NULL, CAST(N'2021-03-14 17:04:47.103' AS DateTime), 23) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (30, 4, N'老6', N'男', CAST(N'1996-08-06' AS Date), N'18945674561', NULL, CAST(N'2021-03-14 17:05:04.990' AS DateTime), 25) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (31, 3, N'老7', N'女', CAST(N'1997-04-06' AS Date), N'18845678912', NULL, CAST(N'2021-03-14 17:05:20.570' AS DateTime), 24) +GO +INSERT [dbo].[StuInfo] ([StuId], [ClassId], [StuName], [StuSex], [StuBrithday], [StuPhone], [StuProvince], [CreateDate], [StuAge]) VALUES (32, 2, N'老10', N'女', CAST(N'1998-08-09' AS Date), N'19945645612', NULL, CAST(N'2021-03-14 17:06:08.107' AS DateTime), 23) +GO +SET IDENTITY_INSERT [dbo].[StuInfo] OFF +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__CourseIn__9526E2773AB7BECE] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[CourseInfo] ADD UNIQUE NONCLUSTERED +( + [CourseName] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +SET ANSI_PADDING ON + +GO +/****** Object: Index [UQ__StuInfo__2D85FC63AF6FC6FA] Script Date: 2021/3/15 16:11:24 ******/ +ALTER TABLE [dbo].[StuInfo] ADD UNIQUE NONCLUSTERED +( + [StuPhone] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([CourseId]) +REFERENCES [dbo].[CourseInfo] ([CourseId]) +GO +ALTER TABLE [dbo].[Scores] WITH CHECK ADD FOREIGN KEY([StuId]) +REFERENCES [dbo].[StuInfo] ([StuId]) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD FOREIGN KEY([ClassId]) +REFERENCES [dbo].[ClassInfo] ([ClassId]) +ON DELETE SET NULL +GO +ALTER TABLE [dbo].[CourseInfo] WITH CHECK ADD CHECK (([CourseCredit]>=(1) AND [CourseCredit]<=(5))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK ((len([StuPhone])=(11))) +GO +ALTER TABLE [dbo].[StuInfo] WITH CHECK ADD CHECK (([StuSex]='女' OR [StuSex]='男')) +GO +USE [master] +GO +ALTER DATABASE [TestDB] SET READ_WRITE +GO + + +--统计每个班的男生数 + select ClassId 班,count(StuSex) 数 from StuInfo where StuSex='男' group by ClassId + +--统计每个班的男、女生数 +select ClassId 班,StuSex 性别,count(StuSex) 数 from StuInfo group by ClassId,StuSex + +--统计每个班的福建人数StuProvince +select ClassId 班,count(StuName) 人数,StuProvince from StuInfo group by ClassId,StuProvince having StuProvince='福建省' + +--统计每个班的各个省的总人数 +select ClassId 班,StuProvince 省,count(StuName) from StuInfo group by ClassId,StuProvince + +--统计每个省的女生数 +select StuProvince,count(StuName) 数,StuSex 省 from StuInfo group by StuProvince,StuSex having StuSex='女' + + +--统计每个省的男、女生数 +select StuProvince,count(StuName) 数,StuSex 省 from StuInfo group by StuProvince,StuSex + +--统计每个学生的考试总分、平均分 +select StuId 学生,sum(Score) 总分,AVG(Score) 平均分 from Scores group by StuId + +--统计出考试总分大于620的学生的考试总分 +select StuId 学生,sum(Score) from Scores group by StuId having sum(Score)>620 + +--统计出每门考试成绩最高分和最低分 +select top 1 Score from Scores order by Score--低 +select top 1 Score from Scores order by Score desc --高 + +--统计出 每个学生的各门成绩的平均分 +select CourseId 课程,AVG(Score) 平均分 from Scores group by CourseId + +select itemName,count(*)订购次数,sum(theNumber) 订购总数量,avg(theMoney)平均单价 from orderItem group by itemName \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy2.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..06236a0bfc22ef11127f5d39027635b426a75a6b --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy2.sql" @@ -0,0 +1,90 @@ +create database Student +on +( + FileName='D:\Student.mdf', + Name='Student', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +log on +( + FileName='D:\Student_log.ldf', + Name='Student_log', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +go + +use Student + +go + +create table Student +( + StuNo varchar(10) primary key not null, + StuName nvarchar(10) not null, + StuAge int, + StuAddress nvarchar(20), + StuSeat int, + StuSex int check(StuSex=1 or StuSex=0) not null +) + +create table Score +( + ExamNo int primary key identity(1,1) not null, + StuNo varchar(10) not null, + WrittenExam int default(0) not null, + LabExam int not null +) + + +insert into Student values +('s2501','张秋利',20,'美国硅谷',1,1), +('s2502','李斯文',18,'湖北武汉',2,0), +('s2503','马文才',22,'湖南长沙',3,1), +('s2504','欧阳俊雄',21,'湖北武汉',4,0), +('s2505','梅超风',20,'湖北武汉',5,1), +('s2506','陈旋风',19,'美国硅谷',6,1), +('s2507','陈风',20,'美国硅谷',7,0), +('s2508','陈风',20,'美国硅谷',7,0) + + +delete from Student + +select * from Student + + +insert into Score values +('s2501',50,70), +('s2502',60,65), +('s2503',86,85), +('s2504',40,80), +('s2505',70,90), +('s2506',85,90), +('s2507',85,90) + +delete from Score + + + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select StuName 姓名,StuAge 年龄,WrittenExam 笔试成绩,LabExam 机试成绩 from Student St inner join Score S on St.StuNo=S.StuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuName 姓名,StuAge 年龄,WrittenExam 笔试成绩,LabExam 机试成绩 from Student St inner join Score S on St.StuNo=S.StuNo where WrittenExam>60 and LabExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select St.StuNO 学号,StuName 姓名,WrittenExam 笔试成绩,LabExam 机试成绩 from Student St left join Score S on St.StuNo=S.StuNo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select StuName 姓名,StuAge 年龄,WrittenExam 笔试成绩,LabExam 机试成绩 from Student St inner join Score S on St.StuNo=S.StuNo where StuAge>=20 order by WrittenExam DESC + +--5.查询男女生的机试平均分 +select AVG(LabExam) from Student St inner join Score S on St.StuNo=S.StuNo + +--6.查询男女生的笔试总分 + +select sum(WrittenExam) from Student St inner join Score S on St.StuNo=S.StuNo diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy3.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e2efae78d68a15fe71c41f5801f6eb1fe6b07f83 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy3.sql" @@ -0,0 +1,83 @@ +create database OrderForm +on +( + FileName='D:\OrderForm.mdf', + Name='OrderForm', + size=5MB, + Maxsize=5MB, + Filegrowth=1MB +) +log on +( + FileName='D:\OrderForm_log.ldf', + Name='OrderForm_log', + size=5MB, + Maxsize=5MB, + Filegrowth=1MB +) +go + +use OrderForm +go + +create table orders +( + orderID int primary key identity(1,1), + orderDate datetime +) + +create table orderltem +( + ltemID int identity(1,1), + orderID int , + itemType nvarchar(15), + itemName nvarchar(10), + theNumber int , + theMoney money +) + +insert into orders (orderDate) values +('2008-01-12 00:00:00.000'), +('2008-02-10 00:00:00.000'), +('2008-02-15 00:00:00.000'), +('2008-03-10 00:00:00.000') + +insert into orderltem values +(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'生活用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3), +(3,'文具','订书机',20,3), +(3,'文具','订书机',10,3), +(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'生活用品','毛巾',4,5), +(4,'生活用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) + + + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select OS.orderID 订单的编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购的产品名称,theNumber 订购数量,theMoney 订购单价 from orderltem OS inner join orders O on OS.orderID=O.orderID + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select OS.orderID 订单的编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购的产品名称 from orderltem OS inner join orders O on OS.orderID=O.orderID where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select OS.orderID 订单的编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购的产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 from orderltem OS inner join orders O on OS.orderID=O.orderID + + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select OS.orderID 订单的编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购的产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 from orderltem OS inner join orders O on OS.orderID=O.orderID where theMoney>=5 and theNumber>=50 + + + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderID 订单编号,theNumber 订购产品数 from orderltem group by orderID,theNumber \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..707af6dbadb895fcc276e03a7ba2011a1c4f49d4 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" @@ -0,0 +1,133 @@ +create database Student2 + +on +( + name='Student2', + filename='D:\Student2.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Student2_log', + filename='D:\Student2_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +use student2 +go + +create table StuInfo +( + sTUNO varchar(15), + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(50), + stuSeat int, + stuSex nvarchar(1) default('男') check(stuSex='男' or stuSex='女') +) + +insert into Stuinfo values ('2501','张秋利','20','美国硅谷','1','女') +insert into Stuinfo values ('2502','李斯文','18','湖北武汉','2','女') +insert into Stuinfo values ('2503','马文才','22','湖南长沙','3','男') +insert into Stuinfo values ('2504','欧阳俊雄','21','湖北武汉','4','男') +insert into Stuinfo values ('2505','梅超风','20','湖北武汉','5','女') +insert into Stuinfo values ('2506','陈旋风','19','美国硅谷','6','男') +insert into Stuinfo values ('2507','陈风','20','美国硅谷','7','男') + +create table Stuexam2 +( + examNO int, + stuNO varchar(15), + writtenExam varchar(200), + labExam varchar(200) +) + +insert into Stuexam2 values ('1','2501','50','70') +insert into Stuexam2 values ('2','2502','60','65') +insert into Stuexam2 values ('3','2503','86','85') +insert into Stuexam2 values ('4','2504','40','80') +insert into Stuexam2 values ('5','2505','70','90') +insert into Stuexam2 values ('6','2506','85','90') + +select sTUNO as 学号 , stuName as 姓名 , stuAge as 年龄 , stuAddress as 地址 , stuSeat as 座号, stuSex as 性别 from Stuinfo +select sTUNO 学号,stuName as 姓名 , stuAge as 年龄 , stuAddress as 地址 , stuSeat as 座号, stuSex as 性别 from Stuinfo +select 学号=sTUNO , 姓名=stuName , 年龄=stuAge , 地址=stuAddress , 座号= stuSeat, 性别= stuSex from Stuinfo + +select stuName , stuAge , stuAddress from Stuinfo + +select examNO as 考号 , stuNO as 学号 , writtenExam as 笔试成绩 , labExam as 机试成绩 from Stuexam2 +select examNO 考号,stuNO as 学号 , writtenExam as 笔试成绩 , labExam as 机试成绩 from Stuexam2 +select 考号=examNO , 学号=stuNO , 笔试成绩=writtenExam , 机试成绩= labExam from Stuexam2 + +select stuName+stuAddress+'@'+stuAddress as 邮箱 from Stuinfo + +select stuNO as 学号,writtenExam as 笔试 ,labExam as 机试, writtenExam+labExam as 总分 from Stuexam2 + +select stuName ,stuAddress from Stuinfo + +select stuAge as 所有年龄 from Stuinfo + +select top 3 * from Stuinfo + +select top 4 * from Stuinfo + +select top 50 percent * from Stuinfo + +select * from Stuinfo where stuAddress = '湖北武汉' and stuAge = 20 + +select * from Stuexam2 where labExam >=60 and labExam <= 80 order by labExam DESC + +select * from Stuinfo where stuAddress = '湖北武汉' or stuAddress='湖南长沙' + +select * from Stuexam2 where writtenExam >=70 and labExam <= 90 order by labExam ASC + +select * from Stuinfo where stuAge is null + +select * from Stuinfo where stuAge is not null + +select * from Stuinfo where stuName like '张%' + +select * from Stuinfo where stuName like '%胡%' + +select * from Stuinfo where stuName like '__俊%' + +select * from Stuinfo order by stuAge DESC + +select * from Stuinfo order by stuAge DESC , stuSeat ASC + +select top 1 * from Stuexam2 order by writtenExam desc + +select top 1 * from Stuexam2 order by writtenExam asc + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 + +select stuName 姓名,stuAge 年龄,writtenExam 笔试, labExam 机试 from StuInfo inner join Stuexam2 on StuInfo.sTUNO = Stuexam2.stuNO + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 + +select StuInfo.sTUNO 学号,stuName 姓名 , writtenExam 笔试, labExam 机试 from StuInfo inner join Stuexam2 on StuInfo.sTUNO = Stuexam2.stuNO where labExam>60 and writtenExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 + +select StuInfo.sTUNO 学号,stuName 姓名,writtenExam 笔试,labExam 机试 from StuInfo left join Stuexam2 on StuInfo.sTUNO =Stuexam2.stuNO + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 + +select stuName 姓名, stuAge 年龄, writtenExam 笔试 , labExam 机试 from StuInfo inner join Stuexam2 on StuInfo.sTUNO = Stuexam2.stuNO where stuAge>=20 + +--5.查询男女生的机试平均分 + +select stuSex 性别 , AVG(labExam) 平均分 from Stuexam2 inner join StuInfo on Stuexam2.stuNO=StuInfo.sTUNO group by stuSex + +--6.查询男女生的笔试总分 + +select stuSex 性别 , sum(writtenExam) 总分 from Stuexam2 inner join StuInfo on Stuexam2.stuNO=StuInfo.sTUNO group by stuSex + +alter table Stuexam2 alter column labExam int + +alter table Stuexam2 alter column writtenExam int \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a9918eab8cf2a16bb66e3dec2f798af533927f9b --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" @@ -0,0 +1,101 @@ +use master +go + +create database order1 +on +( +name='order1', +filename='D:\sql.mdf', +size=5MB, +filegrowth=10MB, +maxsize=50MB +) +log on +( +name='order1_log', +filename='D:\sql.ldf', +size=5MB, +filegrowth=10MB, +maxsize=50MB +) +go + +use order1 +go + +create table orders +( +orderID int primary key identity(1,1) not null, +orderDate datetime not null +) + +create table orderItem +( +itemID int primary key identity(1,1) not null, +orderid int not null, +itemType varchar(20) not null, +itemName varchar(20) not null, +theNumber int not null, +theMoney money not null +) + +insert into orders values ('2008-01-12 00:00.000'), +('2008-02-10 00:00.000'),('2008-02-15 00:00.000'), +('2008-03-10 00:00.000') + +insert into orderItem values(1,'文具','笔','72','2'), +(1,'文具','尺','10','1'),(1,'体育用品','篮球','1','56'), +(2,'文具','笔','36','2'),(2,'文具','固体胶','20','3'), +(2,'日常用品','透明胶','2','1'),(2,'体育用品','羽毛球','20','3'), +(3,'文具','订书机','20','3'),(3,'文具','订书针','10','3'), +(3,'文具','裁纸刀','5','5'),(4,'文具','笔','20','2'), +(4,'文具','信纸','50','1'),(4,'日常用品','毛巾','4','5'), +(4,'日常用品','透明胶','30','1'),(4,'体育用品','羽毛球','20','3') + +select sum(theNumber) from orderItem + +select orderid 订单编号,theNumber 所有物品数量,AVG(theMoney) 平均单价 from orderItem group by orderid,theNumber having orderid<3 and AVG(theMoney)<10 order by orderid asc + +select theNumber 所有物品数量,AVG(theMoney) 平均单价 from orderItem group by theNumber having theNumber>50 and AVG(theMoney)<10 + +select itemType 商品类别 , theNumber 订购次数 from orderItem group by itemType,theNumber + +select itemType 物品类别 , sum(theNumber) 总数量 ,AVG(theMoney) 平均单价 from orderItem group by itemType having sum(theNumber)>100 + +select itemName 产品名称 , count(theNumber) 订购次数, sum(theNumber) 总数量 , AVG(theMoney) 平均单价 from orderItem group by itemName + + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + +select orderItem.orderid 编号,orderDate 日期,itemType 类别 , itemName 名称, theNumber 数量,theMoney 单价 from orderItem inner join orders on orderItem.orderid=orders.orderID + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 + +select orderItem.orderid 编号, orderDate 日期,itemType 类别,itemName 名称 from orderItem inner join orders on orderItem.orderid=orders.orderID where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select orderItem.orderid 编号, orderDate 日期, itemType 类别, itemName 名称, theNumber 数量,theNumber* theMoney 总价 from orderItem inner join orders on orderItem.orderid=orders.orderID group by orderItem.orderid,orderDate,itemType,itemName,theNumber,theMoney + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select orderItem.orderid 编号,orderDate 日期,itemType 类别 , itemName 名称, theNumber 数量,theMoney 单价,theNumber*theMoney 总价 from orderItem inner join orders on orderItem.orderid=orders.orderID where theMoney>=5 and theNumber>=50 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 + +select orderid 编号,theNumber 产品数 from orderItem group by orderid,theNumber + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select orderid 编号,itemType 类别, count(theNumber) 次数 ,theNumber 总数量 from orderItem group by orderid,itemType, theNumber \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..abd6e284b1438bca247e810de56c2da08924479b --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery3.sql" @@ -0,0 +1,123 @@ +create database bbs +on +( +name='bbs', +filename='D:\sql4.mdf', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) +log on +( +name='bbs_log', +filename='D:\sql4.ldf', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) +go + +use bbs +go + +create table bbsUsers +( +UID int identity(1,1), +uName varchar(10) not null, +uSex varchar(2) not null, +uAge int not null, +uPoint int not null +) + +alter table bbsUsers add constraint PK_UID primary key(UID) +alter table bbsUsers add constraint uName unique(uName) +alter table bbsUsers add constraint uSex check(uSex='男' or uSex='女') +alter table bbsUsers add constraint uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint uPoint check(uPoint>=0) + +create table bbsTopic +( +tID int primary key identity(1,1), +tUID int foreign key references bbsUsers(UID), +tSID int foreign key references bbsSection(sID), +tTitle varchar(100) not null, +tMsg text not null, +tTime datetime , +tCount int +) + +create table bbsReply +( +rID int primary key identity(1,1), +rUID int foreign key references bbsUsers(UID), +rTID int foreign key references bbsTopic(tID), +rMsg text not null, +rTime datetime +) + +create table bbsSection +( +sID int identity(1,1), +sName varchar(10) not null, +sUid int +) + +alter table bbsSection add constraint Pk_sID primary key (sID) +alter table bbsSection add constraint CK_sUid foreign key (sUid) references bbsUsers(UID) + +insert into bbsUsers values('小雨点','女','20','0') +insert into bbsUsers values('逍遥','男','18','4') +insert into bbsUsers values('七年级生','男','19','2') + +select uName,uSex from bbsUsers + +select uName,uPoint into bbsPoint from bbsUsers + +select * from bbsSection + +insert into bbsSection (sName,sUid) +values('技术交流',1), + ('读书世界',3), + ('生活百科',1), + ('八卦区',3) + +insert into bbsTopic( tUID,tSID,tTitle,tMsg,tTime,tCount ) +values (2,4,'范跑跑','谁是范跑跑','2008-7-8','1'),(3,1,'.NET','与JAVA的区别是什么','2008-9-1','2'), +(1,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10','0') + +insert into bbsReply(rMsg,rTime,rUID) values +('他是范跑跑','2008-7-9',1), +('不知道','2008-9-2',2), +('不知道','2008-9-11',3) + +alter table bbsReply drop constraint FK__bbsReply__rUID__30F848ED +alter table bbsTopic drop constraint FK__bbsTopic__tUID__2D27B809 +delete bbsUsers where uName='逍遥' + +update bbsUsers set upoint =12 where uName='小雨点' + +alter table bbsTopic drop constraint FK__bbsTopic__tSID__2E1BDC42 +delete bbsSection where sName = '生活百科' + +delete bbsReply + + +--1.查询出每个版块的版主编号,版主姓名和版块名称 + +select UID 版主编号,uName 姓名,sName 名称 from bbsSection inner join bbsUsers on bbsSection.sUid=bbsUsers.UID + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + +select tUID 版主编号,uName 姓名,tTitle 标题,tMsg 内容,tTime 时间 from bbsTopic inner join bbsUsers on bbsTopic.tUID=bbsUsers.UID + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 + +select tID 编号,sName 名称,uName 姓名 from bbsTopic inner join bbsSection on sID=tID inner join bbsUsers on bbsTopic.tUID=bbsUsers.UID where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 + +select top 1 tID 发帖人编号,uName 姓名,tTitle 标题,tMsg 内容,tCount 数量 from bbsTopic inner join bbsUsers on bbsTopic.tUID=bbsUsers.UID order by tCount DESC + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select tSID 版块,tUID 用户,count(tSID) 发帖总数 from bbsTopic inner join bbsSection on sID=tID inner join bbsUsers on bbsTopic.tUID=bbsUsers.UID group by tSID,tUID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\347\273\203\344\271\240\344\270\200.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\347\273\203\344\271\240\344\270\200.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0045b27de743ff7f2321f058d0590d11bbc9378b --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\347\273\203\344\271\240\344\270\200.sql" @@ -0,0 +1,73 @@ +use master +go + +create database Student05 +go + +use Student05 +go + +create table StuIS +( + StuNO nvarchar(10) unique not null, + StuName nvarchar(10) not null, + StuAge int not null, + StuAddress nvarchar(200) , + StuSeat nvarchar(8) not null, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女') +) + +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2501','张秋利','20','美国硅谷','1','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2502','李斯文','18','湖北武汉','2','男') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2503','马文才','22','湖南长沙','3','男') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2504','欧阳俊雄','21','湖北武汉','3','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2505','梅超风','20','湖北武汉','4','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2506','陈旋风','19','美国硅谷','5','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2507','陈风','20','美国硅谷','7','女') + + +create table Grate +( + ExamNo int primary key identity(1,1), + StuNo nvarchar(10) references StuIS(StuNO), + WExam int not null, + LabExam int not null +) +insert into Grate(StuNO,WExam,LabExam) +values('s2501','50','70') +insert into Grate(StuNO,WExam,LabExam) +values('s2502','60','65') +insert into Grate(StuNO,WExam,LabExam) +values('s2503','86','85') +insert into Grate(StuNO,WExam,LabExam) +values('s2504','40','80') +insert into Grate(StuNO,WExam,LabExam) +values('s2505','70','90') +insert into Grate(StuNO,WExam,LabExam) +values('s2506','85','90') + +select *from StuIS +select *from Grate + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select StuName 姓名,StuAge 年龄,WExam 笔试成绩,LabExam 机试成绩 from StuIS inner join Grate on StuIS.StuNO=Grate.StuNo +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuIS.StuNO 学号, StuName 姓名,WExam 笔试成绩, LabExam 机试成绩 from StuIS inner join Grate on StuIS.StuNO=Grate.StuNo +where WExam>60 and LabExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select StuIS.StuNO 学号, StuName 姓名, WExam 笔试成绩,LabExam 机试成绩 from StuIS left join Grate on StuIS.StuNO=Grate.StuNo +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select StuName 姓名,StuAge 年龄,WExam 笔试成绩, LabExam 机试成绩 from StuIS inner join Grate on StuIS.StuNO=Grate.StuNo +where StuAge>=20 order by WExam +--5.查询男女生的机试平均分 +select StuSex 性别,AVG(LabExam) 机试平均分 from StuIS inner join Grate on StuIS.StuNO=Grate.StuNo group by StuSex +--6.查询男女生的笔试总分 +select StuSex 性别,sum(WExam)笔试总分 from StuIS inner join Grate on StuIS.StuNO=Grate.StuNo group by StuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\347\273\203\344\271\240\344\270\211.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\347\273\203\344\271\240\344\270\211.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4c53e41e4e1479f0c063beacaa5abc8ee518d797 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\347\273\203\344\271\240\344\270\211.sql" @@ -0,0 +1,98 @@ +use master +go + +create database bbs +go + +use bbs +go + +create table bbsUsers +( + UID int identity(1,1) , + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) + +alter table bbsUsers add constraint PK_bbsUser_UID primary key(UID) +alter table bbsUsers add constraint UK_bbsUser_uName unique(uName) +alter table bbsUsers add constraint CK_bbsUser_uSex check(uSex in('男','女')) +alter table bbsUsers add constraint CK_bbsUser_uAge check(uAge>=15 or uAge<=60) +alter table bbsUsers add constraint CK_bbsUser_uPoint check(uPoint>=0) + +create table bbsSection +( + sID int identity(1,1), + sName varchar(10) not null, + sUid int +) +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUsers(UID) + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int foreign key references bbsUsers(UID), + tSID int foreign key references bbsSection(sID), + tTItle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) + +create table bbsReply +( + rID INT primary key identity(1,1), + rUID int foreign key references bbsUsers(UID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) +insert into bbsUsers values +('小雨点','女',20,0), +('逍遥','男',18,4), +('七年级生','男',19,2) + + +insert into bbsSection values +('技术交流',1), +('读书世界',3), +('生活百科',1), +('八卦区',3) + +insert into bbsTopic values +(2,4,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?',2008-9-10,0), +(3,1,'.NET','与JAVA的区别是什么呀?',2008-9-1,2), +(1,3,'范跑跑','谁是范跑跑',2008-7-8,1) + +insert into bbsReply values +(2,1,'不知道',2008-9-10), +(3,2,'不知道',2008-9-1), +(1,3,'不知道',2008-7-8) + + + +--1.查询出 每个版块 的 版主编号, 版主姓名 和版块名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称 from bbsSection +inner join bbsUsers on bbsUsers.UID = bbsSection.sUid + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号,uName 发帖人姓名,tTItle 帖子的标题 ,tMsg 帖子的内容,tTime 发帖时间 from bbsTopic +inner join bbsUsers on bbsUsers.UID = bbsTopic.tUID where tTime>'2008-9-15' + + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称 from bbsSection +inner join bbsUsers on bbsUsers.UID = bbsSection.sUid where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select rUID 发帖人编号 , uName 发帖人姓名 ,tTItle 主贴标题 , tMsg 主贴内容 , tCount 回复数量 from bbsReply +inner join bbsTopic on bbsTopic.tUID = bbsReply.rUID +inner join bbsUsers on bbsUsers.UID = bbsReply.rUID where tCount=(select max(tCount) from bbsTopic) + +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select tSID 版块 , uName 发帖人姓名 , count(*) from bbsTopic +inner join bbsUsers on bbsUsers.UID = bbsTopic.tUID +inner join bbsSection on bbsUsers.UID =bbsSection.sID group by tSID ,uName \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\347\273\203\344\271\240\344\272\214.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\347\273\203\344\271\240\344\272\214.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e475baaa5662d91a7073296967ef235036b83a18 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\347\273\203\344\271\240\344\272\214.sql" @@ -0,0 +1,91 @@ +use master +go +create database orde +on +( +name='orde', +filename='D:\orde.mdf', +size=5mb, +filegrowth=10mb, +maxsize=10mb +) +log on +( +name='orde_log', +filename='D:\orde_log.ldf', +size=5mb, +filegrowth=10mb, +maxsize=10mb +) +go +create table orders +( +orderld int primary key identity(1,1) not null, +orderDate datetime not null +) +create table orderltem +( +ltemiD int primary key identity(1,1) not null, +orderld int not null, +itemType nvarchar(20) not null, +itemName nvarchar(20) not null, +theNumber int not null, +theMoney money not null +) + insert into orders values + ('2008-01-12 00:00:00.000'), + ('2008-02-10 00:00:00.000'), + ('2008-02-15 00:00:00.000'), + ('2008-03-10 00:00:00.000') + + insert into orderltem values + (1,'文具','笔','72','2'), + (1,'文具','尺','10','1'), + (1,'体育用品','篮球','1','56'), + (2,'文具','笔','36','2'), + (2,'文具','固体胶','20','3'), + (2,'日常用品','透明胶','2','1'), + (2,'体育用品','羽毛球','20','3'), + (3,'文具','订书机','20','3'), + (3,'文具','订书针','10','3'), + (3,'文具','裁纸刀','5','5'), + (4,'文具','笔','20','2'), + (4,'文具','信纸','50','1'), + (4,'日常用品','毛巾','4','5'), + (4,'日常用品','透明胶','30','1'), + (4,'体育用品','羽毛球','20','3') + + select * from orders + select * from orderltem + + + +-- 1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + +select orders.orderld 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价 from orders inner join orderltem on orders.orderld=orderltem.orderld + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orders.orderld 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称 from orders inner join orderltem on orders.orderld=orderltem.orderld +where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderld 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,theMoney * theNumber 订购总价 from orders inner join orderltem on orders.orderld=orderltem.orderld +group by orders.orderld,orderDate,itemType,itemName,theNumber,theMoney + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderld 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价, theMoney * theNumber 订购总价 from orders inner join orderltem on orders.orderld=orderltem.orderld +where theMoney>5 and theNumber>50 group by orders.orderld,orderDate,itemType,itemName,theNumber,theMoney +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderld 订单的编号 , count(*)订购产品数 from orderltem group by orderld + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +-- 订单编号 产品类别 订购次数 总数量 +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select orderld 订单的编号 , itemType 产品类别 ,count(*)订购次数 , sum(theNumber) 总数量 from orderltem group by orderld,itemType order by orderld \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..dd82438a5112db0f65634bff2f03600b96282e17 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" @@ -0,0 +1,53 @@ +use master +go +create database task +go +use task +go +create table Student +( +stuid nvarchar(10) primary key, +stuname nvarchar(5) not null , +stuage int , +stuaddress nvarchar(200), +stuseat int identity(1,1), +stusex int check(stusex=1 or stusex=0) +) +create table result +( +examn0 int primary key identity(1,1), +stuid nvarchar(10) references Student(stuid), +writtenexam int check(writtenexam>=0 or writtenexam<=100), +labexam int check(labexam>=0 or labexam<=100) +) + +insert into Student +select 's2501','张秋利','20','美国硅谷','1' union +select 's2502','李斯文','18','湖北武汉','0' union +select 's2503','马文才','22','湖南长沙','1' union +select 's2504','欧阳俊雄','21','湖北武汉','0' union +select 's2505','梅超风','20','湖北武汉','1'union +select 's2506','陈旋风','19','美国硅谷','1' union +select 's2507','陈风','20','美国硅谷','0' +select * from Student +insert into result +select 's2501','50','70' union +select 's2502','60','65' union +select 's2503','86','85' union +select 's2504','40','80' union +select 's2505','70','90' union +select 's2506','85','90' +select * from result + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuname 姓名,stuage 年龄,r.writtenexam 笔试成绩,r.labexam 机试成绩 from Student s inner join result r on s.stuid=r.stuid +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select s.stuname 姓名,s.stuid 学号 ,writtenexam 笔试成绩,labexam 机试成绩 from result r inner join Student s on s.stuid=r.stuid where writtenexam>60 and labexam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select s.stuid 学号,s.stuname 姓名, writtenexam 笔试成绩,labexam 机试成绩 from Student s left join result r on s.stuid=r.stuid +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuname 姓名,stuage 年龄,r.writtenexam 笔试成绩,r.labexam 机试成绩 from Student s inner join result r on s.stuid= r.stuid where s.stuage>=20 order by writtenexam desc +--5.查询男女生的机试平均分 +select avg(r.labexam) 机试平均分,stusex 性别 from Student s inner join result r on s.stuid=r.stuid group by stusex +--6.查询男女生的笔试总分 +select stusex 性别,sum(r.writtenexam) 笔试总分 from Student s inner join result r on s.stuid=r.stuid group by stusex diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..86154bf0628eab6fa8fd7d7d097064ea03d13578 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" @@ -0,0 +1,67 @@ +use master +go +create database OrderLibrary +go +use OrderLibrary +go +create table orders +( +orderId int primary key identity(1,1), +orderDate datetime +) +insert into orders +select '2008-01-12 00:00:00.000'union +select '2008-02-10 00:00:00.000'union +select '2008-02-15 00:00:00.000'union +select '2008-03-10 00:00:00.000' +select * from orders +create table orderItem +( +ItemiD int identity(1,1) primary key, +orderId int, +itemType nvarchar(20), +itemName nvarchar(10), +theNumber int, +theMoney int +) +insert into orderItem +select 1,'文具','笔',72,2 union +select 1,'文具','尺',10,1 union +select 1,'体育用品','篮球',1,56 union +select 2,'文具','笔',36,2 union +select 2,'文具','固体胶',20,3 union +select 2,'日常用品','透明胶',2,1 union +select 2,'体育用品','羽毛球',20,3 union +select 3,'文具','订书机',20,3 union +select 3,'文具','订书针',10,3 union +select 3,'文具','裁纸刀',5,5 union +select 4,'文具','笔',20,2 union +select 4,'文具','信纸',50,1 union +select 4,'日常用品','毛巾',4,5 union +select 4,'日常用品','透明胶',30,1 union +select 4,'体育用品','羽毛球',20,3 +select * from orderItem +select * from orders +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select o1.orderId 编号,o1.orderDate 订单日期,o2.itemType 类别 ,o2.itemName 名称,o2.theNumber 数量,o2.theMoney 单价 from orders o1 inner join orderItem o2 on o1.orderId=o2.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select o1.orderId 编号,o1.orderDate 订单日期,o2.itemType 类别,o2.itemName 名称 from orders o1 inner join orderItem o2 on o1.orderId=o2.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select o1.orderId 编号,o1.orderDate 订单日期,o2.itemType 类别,o2.itemName 名称,o2.theNumber 数量,o2.theMoney 单价,o2.theMoney*o2.theNumber 总价 from orders o1 inner join orderItem o2 on o1.orderId=o2.orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select o1.orderId 编号,o1.orderDate 订单日期,o2.itemType 类别,o2.itemName 名称,o2.theNumber 数量,o2.theMoney 单价,o2.theMoney*o2.theNumber 总价 from orders o1 inner join orderItem o2 on o1.orderId=o2.orderId where o2.theMoney>=5 and o2.theNumber>=50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderId 编号,theNumber 数量 from orderItem +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + + --订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select orderId 编号,itemType 产品类别,count(*) 次数,theNumber 总数量 from orderItem group by orderId,itemType,theNumber \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..eb993fa49c11b8dbbb757599b6effa00a2a3f296 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery3.sql" @@ -0,0 +1,16 @@ +3 + +select * from bbsTopic +select * from bbsSection +select * from bbsUsers +select * from bbsReply +--1.查询出每个版块的版主编号 suid,版主姓名 uname和版块名称sname +select bbsSection.sUid 版主编号,bbsUsers.uName 版主姓名,bbsSection.sName 版块名称 from bbsSection inner join bbsUsers on bbsSection.sUid=bbsUsers.uuID +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select bt.tUID 编号,bu.uName 姓名,bt.tTitle 标题,bt.tMsg 内容,bt.tTime 时间 from bbsTopic bt inner join bbsUsers bu on bt.tUID=bu.uuID where bt.tTime<2008-9-15 +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select bs.sUid 编号,bu.uName 名称,bs.sName 版块的名称 from bbsUsers bu inner join bbsSection bs on bs.sUid=bu.uuID where bu.uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select max(bt.tCount) ,bt.tUID,bu.uName,bt.tTitle,bt.tMsg from bbsTopic bt inner join bbsUsers bu on bt.tUID=bu.uuID group by tUID,bu.uName,tTitle,tMsg,max(bt.tCount) +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select bt.tUID 用户,bu.sID 板块,count(*) 发帖总数 from bbsTopic bt inner join bbsSection bu on bt.tSID=bu.sID group by bt.tUID,bu.sID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\2321.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d90e1cf5fdf6d433c3c87e2219aa8a9387d9819a --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\2321.sql" @@ -0,0 +1,98 @@ +use master +go +create database STU +on +( +name='STU', +filename='C:\SQL\STU.mdf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +log on +( +name='STU_log', +filename='C:\SQL\STU_log.ldf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +go +use STU +go +create table StuInfo +( +StuNO nvarchar(10) unique not null, +StuName nvarchar(10) not null, +StuAge int not null, +StuAddress nvarchar(200) , +StuSeat nvarchar(8) not null, +StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女') +) + +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2501','张秋利','20','美国硅谷','1','女') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2502','李斯文','18','湖北武汉','2','男') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2503','马文才','22','湖南长沙','3','男') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2504','欧阳俊雄','21','湖北武汉','4','女') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2505','梅超风','20','湖北武汉','5','女') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2506','陈旋风','19','美国硅谷','6','女') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2507','陈风','20','美国硅谷','7','女') + +create table Chengji +( +ExamNo int primary key identity(1,1), +StuNo nvarchar(10) references StuInfo(StuNO), +WittenExam int not null, +LabExam int not null +) +insert into Chengji(StuNO,WittenExam,LabExam) +values('s2501','50','70') +insert into Chengji(StuNO,WittenExam,LabExam) +values('s2502','60','65') +insert into Chengji(StuNO,WittenExam,LabExam) +values('s2503','86','85') +insert into Chengji(StuNO,WittenExam,LabExam) +values('s2504','40','80') +insert into Chengji(StuNO,WittenExam,LabExam) +values('s2505','70','90') +insert into Chengji(StuNO,WittenExam,LabExam) +values('s2506','85','90') + + +--练习1 +--数据如图片1,使用上次作业的数据 +select * from StuInfo +select * from Chengji +--1.查询学生的姓名(StuName),年龄(StuAge),笔试成绩(WittenExam)和机试成绩(LabExam) +select StuInfo.StuNO,StuInfo.StuName 姓名,StuInfo.StuAge 年龄,Chengji.WittenExam 笔试成绩,Chengji.LabExam 机试成绩 +from StuInfo inner join Chengji on StuInfo.StuNO=Chengji.StuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号(StuNO),姓名(StuName),笔试成绩(WittenExam)和机试成绩(LabExam) +select StuInfo.StuNO 学号,StuInfo.StuName 姓名,Chengji.WittenExam 笔试成绩,Chengji.LabExam 机试成绩 +from StuInfo inner join Chengji on StuInfo.StuNO=Chengji.StuNo +where Chengji.WittenExam>60 and Chengji.LabExam>60 + +--3.查询所有学生的学号(StuNO),姓名(StuName),笔试成绩(WittenExam),机试成绩(LabExam),没有参加考试的学生的成绩以NULL值填充 +select StuInfo.StuNO 学号,StuInfo.StuName 姓名,Chengji.WittenExam 笔试成绩,Chengji.LabExam 机试成绩 +from StuInfo left join Chengji on StuInfo.StuNO=Chengji.StuNo + +--4.查询年龄(StuAge)在20以上(包括20)的学生的姓名(StuName),年龄(StuAge),笔试成绩(WittenExam)和机试成绩(LabExam),并按笔试成绩降序排列 +select StuInfo.StuNO 学号,StuInfo.StuName 姓名,StuInfo.StuAge 年龄,Chengji.WittenExam 笔试成绩,Chengji.LabExam 机试成绩 +from StuInfo inner join Chengji on StuInfo.StuNO=Chengji.StuNo +where StuInfo.StuAge>=20 order by WittenExam DESC + +--5.查询 男女生的 机试(LabExam)平均分 +select StuInfo.StuSex 性别, avg(LabExam) +from StuInfo inner join Chengji on StuInfo.StuNO=Chengji.StuNo +group by StuSex +--6.查询男女生的笔试(WittenExam)总分 +select StuInfo.StuSex 性别, sum(WittenExam) +from StuInfo inner join Chengji on StuInfo.StuNO=Chengji.StuNo +group by StuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\2322.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6156fb3c9e32f344bdd3b2bad6fe17e7f1d5648d --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\2322.sql" @@ -0,0 +1,104 @@ +use master +go +create database ORD +on +( +name='ORD', +filename='C:\SQL\ORD.mdf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +log on +( +name='ORD_log', +filename='C:\SQL\ORD_log.ldf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +go +use ORD +go +create table Orders +( +OrderID int primary key not null, +OrderDate datetime +) +go +use ORD +go +create table Orderltem +( +ItemID int primary key not null, +OrderID int references Orders(OrderID) not null, +ItemType varchar(8) not null, +ItemName varchar(6) not null, +TheNumber int not null, +TheMoney int not null +) +go +insert into Orders (OrderID,OrderDate) values(1,'2008-01-12') +insert into Orders (OrderID,OrderDate) values(2,'2008-02-10') +insert into Orders (OrderID,OrderDate) values(3,'2008-02-15') +insert into Orders (OrderID,OrderDate) values(4,'2008-03-10') + +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(1,1,'文具','笔','72','2') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(2,1,'文具','尺','10','1') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(3,1,'体育用具','篮球','1','56') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(4,2,'文具','笔','36','2') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(5,2,'文具','固体胶','20','3') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(6,2,'日常用品','透明胶','2','1') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(7,2,'体育用品','羽毛球','20','3') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(8,3,'文具','订书机','20','3') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(9,3,'文具','订书机','10','3') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(10,3,'文具','裁纸刀','5','5') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(11,4,'文具','笔','20','2') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(12,4,'文具','信纸','50','1' ) +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(13,4,'日常用品','毛巾','4','5') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(14,4,'日常用品','透明胶','30','1') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(15,4,'体育用品','羽毛球','20','3') + + +--练习2 +--使用上次作业的订单数据库,完成下列题目: +select * from Orders +select * from Orderltem +--1.查询所有的订单的订单的编号(OrderID),订单日期(OrderDate),订购产品的类别(ItemType)和订购的产品名称(ItemName),订购数量(TheNumber)和订购单价(TheMoney) +select Orders.OrderID 编号,Orders.OrderDate 日期,Orderltem.ItemType 类别,Orderltem.ItemName 名称,Orderltem.TheNumber 数量,Orderltem.TheMoney 单价 +from Orders inner join Orderltem on Orders.OrderID=Orderltem.OrderID + +--2.查询订购数量(TheNumber)大于50的订单的编号(OrderID),订单日期(OrderDate),订购产品的类别(ItemType)和订购的产品名称(ItemName) +select Orders.OrderID 编号,Orders.OrderDate 日期,Orderltem.ItemType 类别,Orderltem.ItemName 名称,Orderltem.TheNumber 数量 +from Orders inner join Orderltem on Orders.OrderID=Orderltem.OrderID +where Orderltem.TheNumber>50 + +--3.查询所有的订单的订单的编号(OrderID),订单日期(OrderDate),订购产品的类别(ItemType)和订购的产品名称(ItemName),订购数量(TheNumber)和订购单价(TheMoney)以及订购总价 +select Orders.OrderID 编号,Orders.OrderDate 日期,Orderltem.ItemType 类别,Orderltem.ItemName 名称,Orderltem.TheNumber 数量,Orderltem.TheMoney 单价, sum(Orderltem.TheNumber*Orderltem.TheMoney) 总价 +from Orders inner join Orderltem on Orders.OrderID=Orderltem.OrderID +group by Orders.OrderID,OrderDate,ItemType,ItemName,TheNumber,TheMoney + +--4.查询单价(TheMoney)大于等于5 并且 数量(TheNumber)大于等于50的订单 的订单的编号(OrderID),订单日期(OrderDate),订购产品的类别(ItemType)和订购的产品名称(ItemName),订购数量(TheNumber)和订购单价(TheMoney)以及订购总价 +select Orders.OrderID 编号,Orders.OrderDate 日期,Orderltem.ItemType 类别,Orderltem.ItemName 名称,Orderltem.TheNumber 数量,Orderltem.TheMoney 单价, sum(Orderltem.TheNumber*Orderltem.TheMoney) 总价 +from Orders inner join Orderltem on Orders.OrderID=Orderltem.OrderID +where Orderltem.TheMoney>=5 and Orderltem.TheNumber>=50 +group by Orders.OrderID,OrderDate,ItemType,ItemName,TheNumber,TheMoney + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号(OrderID) 订购产品数 +-- 1 3 +-- 2 4 +select Orders.OrderID 编号,count(*)订购产品数 +from Orderltem inner join Orders on Orders.OrderID=Orderltem.OrderID +group by Orders.OrderID + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +-- 订单编号(OrderID) 产品类别(ItemType) 订购次数 总数量 +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select Orders.OrderID 编号,Orderltem.ItemType 类别,count(*)订购次数,sum(TheNumber) 总数量 +from Orderltem inner join Orders on Orders.OrderID=Orderltem.OrderID +group by Orders.OrderID,ItemType diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\2323.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\2323.sql" new file mode 100644 index 0000000000000000000000000000000000000000..927991549ccf0e5c09603d17c30049ae2ea02546 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\2323.sql" @@ -0,0 +1,121 @@ +use master +go +create database BBS +on +( +name='BBS', +filename='C:\SQL\BBS.mdf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +log on +( +name='BBS_log', +filename='C:\SQL\BBS_log.ldf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +go +use BBS +go +create table BBSUsers--用户信息表 +( +UID int not null, +UName varchar(10) not null, +USex varchar(2) not null, +UAge int not null, +UPoint int not null +) +alter table BBSUsers add constraint PK_BBSUsers_UID primary key(UID) +alter table BBSUsers add constraint UK_BBSUsers_UName unique(UName) +alter table BBSUsers add constraint CK_BBSUsers_USex check(USex='男' or USex='女') +alter table BBSUsers add constraint CK_BBSUsers_UAge check(UAge>=15 and UAge<=60) +alter table BBSUsers add constraint CK_BBSUsers_UPoint check(UPoint>=0) + +go +use BBS +go +create table BBSSection--回帖表 +( +SID int not null, +SName varchar(10) not null, +SUID int not null +) +alter table BBSSection add constraint PK_BBSSection_SID primary key(SID) +alter table BBSSection add constraint FK_BBSSection_SUID foreign key (SUID) references BBSUsers(UID) + +go +use BBS +go +create table BBSTopic--主贴表 +( +TID int primary key identity(1,1) not null, +TUID int references BBSUsers(UID) not null, +TSID int references BBSSection(SID) not null, +TTitle varchar(100) not null, +TMsg text not null, +TTime datetime , +Tcount int , +) +go +use BBS +go +create table BBSReply--版块表 +( +RID int primary key identity(1,1) not null, +RUID int references BBSUsers(UID) not null, +RTID int references BBSTopic(TID) not null, +RMsg text , +RTime datetime , +) +insert into BBSUsers (UID,UName , USex , UAge , UPoint) values(1,'小雨点','女','20','0') +insert into BBSUsers (UID,UName , USex , UAge , UPoint) values(2,'逍遥','男','18','4') +insert into BBSUsers (UID,UName , USex , UAge , UPoint) values(3,'七年级生','男','19','2') + +select UName,UPoint into BBSPoint from BBSUsers + +insert into BBSSection (SID,SName,SUID) values(4,'技术交流',1) +insert into BBSSection (SID,SName,SUID) values(5,'读书世界',3) +insert into BBSSection (SID,SName,SUID) values(6,'生活八卦',1) +insert into BBSSection (SID,SName,SUID) values(7,'八卦区',3) + +insert into BBSTopic (TUID,TSID,TTitle,TMsg,TTime,Tcount) values(2,7,'范跑跑','谁是范跑跑','2008-7-8',1) +insert into BBSTopic (TUID,TSID,TTitle,TMsg,TTime,Tcount) values(3,4,'.NET','与JAVA的区别是什么','2008-9-1',2) +insert into BBSTopic (TUID,TSID,TTitle,TMsg,TTime,Tcount) values(1,6,'今夏最流行什么','有谁知道今夏最流行什么','2008-9-10',0) +insert into BBSTopic (TUID,TSID,TTitle,TMsg,TTime,Tcount) values(2,7,'今夏最流行什么','今夏最流行阿巴阿巴阿巴','2008-7-8',1) + +insert into BBSReply (RUID,RMsg,RTime,RTID) values(1,'范跑跑是什么人。。。','2008-10-1',1) +insert into BBSReply (RUID,RMsg,RTime,RTID) values(2,'它们的区别是什么。。。','2008-11-1',2) +insert into BBSReply (RUID,RMsg,RTime,RTID) values(3,'最流行什么。。。','2008-11-20',3) + + +--练习3 +--在论坛数据库中完成以下题目 +select * from BBSUsers--用户信息表 +select * from BBSSection--版块表 +select * from BBSTopic--主贴表 +select * from BBSReply--回帖表 +--1.查询出每个版块的版主编号(RID),版主姓名(UName)和版块名称(RMsg) +select BBSReply.RID 编号,BBSUsers.UName 姓名,BBSReply.RMsg 版块名称 +from BBSReply inner join BBSUsers on BBSReply.RID=BBSUsers.UID + +--2.查询出主贴的发帖时间(TTime)在2008-9-15以后的主贴的发帖人编号(UID),发帖人姓名(UName),帖子的标题(TTitle),帖子的内容(TMsg)和发帖时间(TTime) +select BBSTopic.TID,BBSUsers.UID 发帖人编号,BBSUsers.UName 姓名,BBSTopic.TTitle 帖子的标题,BBSTopic.TMsg 帖子的内容,BBSTopic.TTime 发帖时间 +from BBSTopic inner join BBSUsers on BBSTopic.TID=BBSUsers.UID where TTime>'2008-9-15' + + +--3.查询出年龄在20以下的版主的编号(RID),版主的名称(UName)和版块的名称(RMsg) +select BBSReply.RID 编号,BBSUsers.UName 姓名,BBSReply.RMsg 版块名称,BBSUsers.UAge 年龄 +from BBSReply inner join BBSUsers on BBSReply.RID=BBSUsers.UID +where BBSUsers.UAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号(UID),发帖人姓名(UName),主贴标题(TTitle),主贴内容(TMsg)和回复数量 +select top 1 TUID 发帖人编号,BBSUsers.UName 姓名,BBSTopic.TTitle 帖子的标题,BBSTopic.TMsg 帖子的内容,Tcount 回复数量 +from BBSTopic inner join BBSUsers on BBSTopic.TID=BBSUsers.UID +order by Tcount desc + +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select * from BBSTopic +select TUID 用户编号, count(TSID) 发帖总数 from BBSTopic group by TUID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\344\275\234\344\270\2321.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1d6c82e404c5c7d5ec16d24c2f2f6dcde472397d --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\344\275\234\344\270\2321.sql" @@ -0,0 +1,76 @@ +create database Studentinfo +on +( + name='Studentinfo', + filename='D:\Studentinfo.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Studentinfo_log', + filename='D:\Studentinfo_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +use Studentinfo +go +create table StuDent +( + sTUNO varchar(15), + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(50), + stuSeat int, + stuSex nvarchar(1) default('男') check(stuSex='男' or stuSex='女') +) + +insert into StuDent(sTUNO,stuName,stuAge,stuAddress,stuSeat,stuSex) +select 's2501','张秋里',20,'美国硅谷',1,'男' union +select 's2502','李斯文',18,'湖北武汉',2,'女' union +select 's2503','马文才',22,'湖南长沙',3,'男' union +select 's2504','欧阳俊雄',21,'湖北武汉',4,'女' union +select 's2505','梅超风',20,'湖北武汉',5,'男' union +select 's2506','陈旋风',19,'美国硅谷',6,'男' union +select 's2507','陈风',20,'美国硅谷',7,'女' + + +create table Score +( + examNO int, + stuNO varchar(15), + writtenExam int, + labExam int +) + + +insert into Score(examNO,stuNO,writtenExam,labExam) +select 1,'s2501','50','70' union +select 2,'s2502','60','65' union +select 3,'s2503','86','85' union +select 4,'s2504','40','80' union +select 5,'s2505','70','90' union +select 6,'s2506','85','90' + + + + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName 姓名,stuAge 年龄,writtenExam 笔试,labExam 机试 from StuDent inner join Score on StuDent.sTUNO=Score.stuNO +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuDent.sTUNO 学号,stuName 姓名,writtenExam 笔试成绩,labExam 机试成绩 from StuDent inner join Score on StuDent.sTUNO=Score.stuNO where writtenExam>60 and labExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select StuDent.sTUNO 学号,stuName 姓名,writtenExam 笔试成绩,labExam 机试成绩 from StuDent left join Score on StuDent.sTUNO=Score.stuNO +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 from StuDent inner join Score on StuDent.sTUNO=Score.stuNO where stuAge>=20 order by writtenExam desc +--5.查询男女生的机试平均分 +select stuSex,avg(labExam)平均分 from Score left join StuDent on Score.stuNO=StuDent.sTUNO group by stuSex +--6.查询男女生的笔试总分 +select stuSex,sum(writtenExam)总分 from Score left join StuDent on Score.stuNO=StuDent.sTUNO group by stuSex + +select * from Score +select * from StuDent diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\344\275\234\344\270\2322.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..223d80b8ee8ebebc9df97fe6186084fd3fc41d66 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\344\275\234\344\270\2322.sql" @@ -0,0 +1,97 @@ +create database cc +on +( + name='cc', + filename='D:\cc.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +log on +( + name='cc_log', + filename='D:\cc.ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +go +use cc +go +--订单表(orders)列为:订单编号(orderId 主键) 订购日期(orderDate) + +create table orders +( + orderId int primary key, + orderDate datetime +) +--插入表1数据 +insert into orders(orderId,orderDate) +select 1,'2008-01-12' union +select 2,'2008-02-10' union +select 3,'2008-02-15' union +select 4,'2008-03-10' +select * from orders +--订购项目表(orderItem),列为: +--项目编号(ItemiD)订单编号(orderId)产品类别(itemType) +--产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) +create table orderItem +( + ItemiD int primary key identity(1,1), + orderId int, + itemType varchar(20), + itemName varchar(20), + theNumber int , + theMoney int +) +--添加表二的外键约束 关联表一的order id +alter table orderItem add constraint FK foreign key (orderId) references orders(orderId) +--插入表2数据 +insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) +select 1,'文具','笔',72,2 union +select 1,'文具','尺',10,1 union +select 1,'体育用品','篮球',1,56 union +select 2,'文具','笔',36,2 union +select 2,'文具','固体胶',20,3 union +select 2,'日常用品','透明胶',2,1 union +select 2,'体育用品','羽毛球',20,3 union +select 3,'文具','订书机',20,3 union +select 3,'文具','订书针',10,3 union +select 3,'文具','裁纸刀',5,5 union +select 4,'文具','笔',20,2 union +select 4,'文具','信纸',50,1 union +select 4,'日常用品','毛巾',4,5 union +select 4,'日常用品','透明胶',30,1 union +select 4,'体育用品','羽毛球',20,3 + +--使用上次作业的订单数据库,完成下列题目: +--订单表(orders) 订购项目表(orderItem) +select * from orders +select * from orderItem +--1.查询所有的订单的 订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orderItem.orderId 订单编号,orders.orderDate 日期,itemType 类别,itemName 产品名称,theNumber 数量,theMoney 单价 from orderItem inner join orders on orderItem.orderId=orders.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orderItem.orderId 订单编号,theNumber 数量,orders.orderDate 日期,itemType 类别,itemName 产品名称 from orderItem inner join orders on orderItem.orderId=orders.orderId where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId 订单编号,orders.orderDate 日期,itemName 产品名称,theNumber 数量,theMoney 单价 ,theMoney*theNumber 订购总价 +from orderItem inner join orders on orderItem.orderId=orders.orderId + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId 订单编号,orderDate 日期,itemType 类别,itemName 产品名称,theNumber 数量,theMoney 单价 ,theMoney*theNumber 订购总价 + from orderItem inner join orders on orderItem.orderId=orders.orderId where theMoney>=5 and theNumber>=50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderId 编号 ,count(itemName)订购产品数 from orderItem group by orderId + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +select orderId,itemType 类别,count(itemType)次数,sum(theNumber)总数量 from orderItem group by orderId,itemType order by orderId asc +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\344\275\234\344\270\2323.sql" "b/20210326\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\344\275\234\344\270\2323.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8277e7b77352c5ea2d1a7ffa435a4e16eee44c33 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\344\275\234\344\270\2323.sql" @@ -0,0 +1,104 @@ +create database bbs + +on +( + name='bbs', + filename='D:\bbs.mdf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +go +use bbs +go +create table bbsUsers +( + UIDD int identity, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +create table bbsSection +( + sIDD int identity, + sName varchar(10) not null, + sUid int +) + +alter table bbsUsers add constraint Pk_bbsUsers_UIDD primary key(UIDD) +alter table bbsUsers add constraint Uk_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint Ck_bbsUsers_uSex check(uSex='男'or uSex='女') +alter table bbsUsers add constraint Ck_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint Ck_bbsUsers_uPoint check(uPoint>=0) + +alter table bbsSection add constraint Pk_bbsSection_sIDD primary key(sIDD) +alter table bbsSection add constraint Fk_bbsSection_sUid foreign key(sUid) references bbsUsers(UIDD) + +create table bbsTopic +( + tID int identity primary key, + tUID int foreign key references bbsUsers(UIDD), + tSID int foreign key references bbsSection(sIDD), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +create table bbsReply +( + rID int identity primary key, + rUID int foreign key references bbsUsers(UIDD), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select uName,uPoint into bbsPoint from bbsUsers +insert into bbsSection values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) +insert into bbsTopic values(2,4,'范跑跑 ',' 谁是范跑跑 ','2008-7-8','1'),(3,1,'.NET ',' 与JAVA的区别是什么呀? ','2008-9-1 ','2'), +(1,3,'今年夏天最流行什么 ',' 有谁知道今年夏天最流行 ','2008-9-10','0') + +insert into bbsReply values(3,2,'八嘎','2008-7-8'),(1,1,'python','2008-7-8'),(1,3,'上海','2008-7-8') + +--据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sIDD,uName,sName from bbsSection left join bbsUsers on sIDD=UIDD + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID,uName,tTitle,tMsg,tTime from bbsTopic left join bbsUsers on tID=UIDD where tTime > '2008-09-15' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sIDD,uName,sName,uAge from bbsSection left join bbsUsers on sIDD=UIDD where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 tUID,uName,tTitle,tMsg,tCount from bbsUsers t left join bbsTopic x on t.UIDD=x.tID order by tCount desc + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select uName 用户名,sName 版块名称,count(*)发帖总数 from bbsTopic + inner join bbsUsers on bbsTopic.tUID = bbsUsers.UIDD + inner join bbsSection on bbsUsers.UIDD= bbsSection.sUid group by uName,sName + + + + +select * from bbsReply --回帖表 +select * from bbsSection --版块表 +select * from bbsTopic --主帖表 +select * from bbsUsers --用户信息表 + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\345\220\264\347\205\214/\344\275\234\344\270\2321.sql" "b/20210326\344\275\234\344\270\232/\345\220\264\347\205\214/\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1d6c82e404c5c7d5ec16d24c2f2f6dcde472397d --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\220\264\347\205\214/\344\275\234\344\270\2321.sql" @@ -0,0 +1,76 @@ +create database Studentinfo +on +( + name='Studentinfo', + filename='D:\Studentinfo.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Studentinfo_log', + filename='D:\Studentinfo_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +use Studentinfo +go +create table StuDent +( + sTUNO varchar(15), + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(50), + stuSeat int, + stuSex nvarchar(1) default('男') check(stuSex='男' or stuSex='女') +) + +insert into StuDent(sTUNO,stuName,stuAge,stuAddress,stuSeat,stuSex) +select 's2501','张秋里',20,'美国硅谷',1,'男' union +select 's2502','李斯文',18,'湖北武汉',2,'女' union +select 's2503','马文才',22,'湖南长沙',3,'男' union +select 's2504','欧阳俊雄',21,'湖北武汉',4,'女' union +select 's2505','梅超风',20,'湖北武汉',5,'男' union +select 's2506','陈旋风',19,'美国硅谷',6,'男' union +select 's2507','陈风',20,'美国硅谷',7,'女' + + +create table Score +( + examNO int, + stuNO varchar(15), + writtenExam int, + labExam int +) + + +insert into Score(examNO,stuNO,writtenExam,labExam) +select 1,'s2501','50','70' union +select 2,'s2502','60','65' union +select 3,'s2503','86','85' union +select 4,'s2504','40','80' union +select 5,'s2505','70','90' union +select 6,'s2506','85','90' + + + + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName 姓名,stuAge 年龄,writtenExam 笔试,labExam 机试 from StuDent inner join Score on StuDent.sTUNO=Score.stuNO +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuDent.sTUNO 学号,stuName 姓名,writtenExam 笔试成绩,labExam 机试成绩 from StuDent inner join Score on StuDent.sTUNO=Score.stuNO where writtenExam>60 and labExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select StuDent.sTUNO 学号,stuName 姓名,writtenExam 笔试成绩,labExam 机试成绩 from StuDent left join Score on StuDent.sTUNO=Score.stuNO +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 from StuDent inner join Score on StuDent.sTUNO=Score.stuNO where stuAge>=20 order by writtenExam desc +--5.查询男女生的机试平均分 +select stuSex,avg(labExam)平均分 from Score left join StuDent on Score.stuNO=StuDent.sTUNO group by stuSex +--6.查询男女生的笔试总分 +select stuSex,sum(writtenExam)总分 from Score left join StuDent on Score.stuNO=StuDent.sTUNO group by stuSex + +select * from Score +select * from StuDent diff --git "a/20210326\344\275\234\344\270\232/\345\220\264\347\205\214/\344\275\234\344\270\2322.sql" "b/20210326\344\275\234\344\270\232/\345\220\264\347\205\214/\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..223d80b8ee8ebebc9df97fe6186084fd3fc41d66 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\220\264\347\205\214/\344\275\234\344\270\2322.sql" @@ -0,0 +1,97 @@ +create database cc +on +( + name='cc', + filename='D:\cc.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +log on +( + name='cc_log', + filename='D:\cc.ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +go +use cc +go +--订单表(orders)列为:订单编号(orderId 主键) 订购日期(orderDate) + +create table orders +( + orderId int primary key, + orderDate datetime +) +--插入表1数据 +insert into orders(orderId,orderDate) +select 1,'2008-01-12' union +select 2,'2008-02-10' union +select 3,'2008-02-15' union +select 4,'2008-03-10' +select * from orders +--订购项目表(orderItem),列为: +--项目编号(ItemiD)订单编号(orderId)产品类别(itemType) +--产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) +create table orderItem +( + ItemiD int primary key identity(1,1), + orderId int, + itemType varchar(20), + itemName varchar(20), + theNumber int , + theMoney int +) +--添加表二的外键约束 关联表一的order id +alter table orderItem add constraint FK foreign key (orderId) references orders(orderId) +--插入表2数据 +insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) +select 1,'文具','笔',72,2 union +select 1,'文具','尺',10,1 union +select 1,'体育用品','篮球',1,56 union +select 2,'文具','笔',36,2 union +select 2,'文具','固体胶',20,3 union +select 2,'日常用品','透明胶',2,1 union +select 2,'体育用品','羽毛球',20,3 union +select 3,'文具','订书机',20,3 union +select 3,'文具','订书针',10,3 union +select 3,'文具','裁纸刀',5,5 union +select 4,'文具','笔',20,2 union +select 4,'文具','信纸',50,1 union +select 4,'日常用品','毛巾',4,5 union +select 4,'日常用品','透明胶',30,1 union +select 4,'体育用品','羽毛球',20,3 + +--使用上次作业的订单数据库,完成下列题目: +--订单表(orders) 订购项目表(orderItem) +select * from orders +select * from orderItem +--1.查询所有的订单的 订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orderItem.orderId 订单编号,orders.orderDate 日期,itemType 类别,itemName 产品名称,theNumber 数量,theMoney 单价 from orderItem inner join orders on orderItem.orderId=orders.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orderItem.orderId 订单编号,theNumber 数量,orders.orderDate 日期,itemType 类别,itemName 产品名称 from orderItem inner join orders on orderItem.orderId=orders.orderId where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId 订单编号,orders.orderDate 日期,itemName 产品名称,theNumber 数量,theMoney 单价 ,theMoney*theNumber 订购总价 +from orderItem inner join orders on orderItem.orderId=orders.orderId + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId 订单编号,orderDate 日期,itemType 类别,itemName 产品名称,theNumber 数量,theMoney 单价 ,theMoney*theNumber 订购总价 + from orderItem inner join orders on orderItem.orderId=orders.orderId where theMoney>=5 and theNumber>=50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderId 编号 ,count(itemName)订购产品数 from orderItem group by orderId + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +select orderId,itemType 类别,count(itemType)次数,sum(theNumber)总数量 from orderItem group by orderId,itemType order by orderId asc +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\220\264\347\205\214/\344\275\234\344\270\2323.sql" "b/20210326\344\275\234\344\270\232/\345\220\264\347\205\214/\344\275\234\344\270\2323.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8277e7b77352c5ea2d1a7ffa435a4e16eee44c33 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\220\264\347\205\214/\344\275\234\344\270\2323.sql" @@ -0,0 +1,104 @@ +create database bbs + +on +( + name='bbs', + filename='D:\bbs.mdf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +go +use bbs +go +create table bbsUsers +( + UIDD int identity, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +create table bbsSection +( + sIDD int identity, + sName varchar(10) not null, + sUid int +) + +alter table bbsUsers add constraint Pk_bbsUsers_UIDD primary key(UIDD) +alter table bbsUsers add constraint Uk_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint Ck_bbsUsers_uSex check(uSex='男'or uSex='女') +alter table bbsUsers add constraint Ck_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint Ck_bbsUsers_uPoint check(uPoint>=0) + +alter table bbsSection add constraint Pk_bbsSection_sIDD primary key(sIDD) +alter table bbsSection add constraint Fk_bbsSection_sUid foreign key(sUid) references bbsUsers(UIDD) + +create table bbsTopic +( + tID int identity primary key, + tUID int foreign key references bbsUsers(UIDD), + tSID int foreign key references bbsSection(sIDD), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +create table bbsReply +( + rID int identity primary key, + rUID int foreign key references bbsUsers(UIDD), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select uName,uPoint into bbsPoint from bbsUsers +insert into bbsSection values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) +insert into bbsTopic values(2,4,'范跑跑 ',' 谁是范跑跑 ','2008-7-8','1'),(3,1,'.NET ',' 与JAVA的区别是什么呀? ','2008-9-1 ','2'), +(1,3,'今年夏天最流行什么 ',' 有谁知道今年夏天最流行 ','2008-9-10','0') + +insert into bbsReply values(3,2,'八嘎','2008-7-8'),(1,1,'python','2008-7-8'),(1,3,'上海','2008-7-8') + +--据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sIDD,uName,sName from bbsSection left join bbsUsers on sIDD=UIDD + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID,uName,tTitle,tMsg,tTime from bbsTopic left join bbsUsers on tID=UIDD where tTime > '2008-09-15' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sIDD,uName,sName,uAge from bbsSection left join bbsUsers on sIDD=UIDD where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 tUID,uName,tTitle,tMsg,tCount from bbsUsers t left join bbsTopic x on t.UIDD=x.tID order by tCount desc + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select uName 用户名,sName 版块名称,count(*)发帖总数 from bbsTopic + inner join bbsUsers on bbsTopic.tUID = bbsUsers.UIDD + inner join bbsSection on bbsUsers.UIDD= bbsSection.sUid group by uName,sName + + + + +select * from bbsReply --回帖表 +select * from bbsSection --版块表 +select * from bbsTopic --主帖表 +select * from bbsUsers --用户信息表 + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..80fa12a5bf2be46a69bddbbf55be94231f9726ad --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" @@ -0,0 +1,91 @@ +create database Student +on +( + FileName='D:\Student.mdf', + Name='Student', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +log on +( + FileName='D:\Student_log.ldf', + Name='Student_log', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +go + +use Student + +go + +create table Student +( + StuNo varchar(10) primary key not null, + StuName nvarchar(10) not null, + StuAge int, + StuAddress nvarchar(20), + StuSeat int, + StuSex int check(StuSex=1 or StuSex=0) not null +) + +create table Score +( + ExamNo int primary key identity(1,1) not null, + StuNo varchar(10) not null, + WrittenExam int default(0) not null, + LabExam int not null +) + + +insert into Student values +('s2501','张秋利',20,'美国硅谷',1,1), +('s2502','李斯文',18,'湖北武汉',2,0), +('s2503','马文才',22,'湖南长沙',3,1), +('s2504','欧阳俊雄',21,'湖北武汉',4,0), +('s2505','梅超风',20,'湖北武汉',5,1), +('s2506','陈旋风',19,'美国硅谷',6,1), +('s2507','陈风',20,'美国硅谷',7,0), +('s2508','陈风',20,'美国硅谷',7,0) + + +delete from Student + +select * from Student + + +insert into Score values +('s2501',50,70), +('s2502',60,65), +('s2503',86,85), +('s2504',40,80), +('s2505',70,90), +('s2506',85,90), +('s2507',85,90) + +delete from Score + + + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select StuName 姓名,StuAge 年龄,WrittenExam 笔试成绩,LabExam 机试成绩 from Student St inner join Score S on St.StuNo=S.StuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuName 姓名,StuAge 年龄,WrittenExam 笔试成绩,LabExam 机试成绩 from Student St inner join Score S on St.StuNo=S.StuNo where WrittenExam>60 and LabExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select St.StuNO 学号,StuName 姓名,WrittenExam 笔试成绩,LabExam 机试成绩 from Student St left join Score S on St.StuNo=S.StuNo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select StuName 姓名,StuAge 年龄,WrittenExam 笔试成绩,LabExam 机试成绩 from Student St inner join Score S on St.StuNo=S.StuNo where StuAge>=20 order by WrittenExam DESC + +--5.查询男女生的机试平均分 +select AVG(LabExam) from Student St inner join Score S on St.StuNo=S.StuNo + +--6.查询男女生的笔试总分 + +select sum(WrittenExam) from Student St inner join Score S on St.StuNo=S.StuNo + diff --git "a/20210326\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b3a7129a49ea2071efc926686da6fc4d5d45eea5 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery2.sql" @@ -0,0 +1,108 @@ +create database OrderForm +on +( + FileName='D:\OrderForm.mdf', + Name='OrderForm', + size=5MB, + Maxsize=5MB, + Filegrowth=1MB +) +log on +( + FileName='D:\OrderForm_log.ldf', + Name='OrderForm_log', + size=5MB, + Maxsize=5MB, + Filegrowth=1MB +) +go + +use OrderForm +go + +create table orders +( + orderID int primary key identity(1,1), + orderDate datetime +) + +create table orderltem +( + ltemID int identity(1,1), + orderID int , + itemType nvarchar(15), + itemName nvarchar(10), + theNumber int , + theMoney money +) + +insert into orders (orderDate) values +('2008-01-12 00:00:00.000'), +('2008-02-10 00:00:00.000'), +('2008-02-15 00:00:00.000'), +('2008-03-10 00:00:00.000') + +insert into orderltem values +(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'生活用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3), +(3,'文具','订书机',20,3), +(3,'文具','订书机',10,3), +(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'生活用品','毛巾',4,5), +(4,'生活用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) + + + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select OS.orderID 订单的编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购的产品名称,theNumber 订购数量,theMoney 订购单价 from orderltem OS inner join orders O on OS.orderID=O.orderID + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select OS.orderID 订单的编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购的产品名称 from orderltem OS inner join orders O on OS.orderID=O.orderID where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select OS.orderID 订单的编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购的产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 from orderltem OS inner join orders O on OS.orderID=O.orderID + + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select OS.orderID 订单的编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购的产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 from orderltem OS inner join orders O on OS.orderID=O.orderID where theMoney>=5 and theNumber>=50 + + + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderID 订单编号,theNumber 订购产品数 from orderltem group by orderID,theNumber + + + + + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日 + +select orderID 订单编号,itemType 订购产品的类别,sum(theNumber) 总数量 from orderltem group by orderID,itemType order by orderID + + +select * from orderltem + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3e44718a8f9008e8ed094f9433c3afa8938fc6c6 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery3.sql" @@ -0,0 +1,117 @@ +create database bbs +on +( + fileName='D:\bbs.mdf', + Name='bbs', + size=5MB, + Maxsize=5MB, + filegrowth=1MB +) +log on +( + fileName='D:\bbs_log.ldf', + Name='bbs_log', + size=5MB, + Maxsize=5MB, + filegrowth=1MB +) +go + +use bbs +go + +create table bbsUsers +( + UID int identity(1,1) , + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +-- +alter table bbsUsers add constraint PK_bbsUser_UID primary key(UID) +-- +alter table bbsUsers add constraint UK_bbsUser_uName unique(uName) +-- +alter table bbsUsers add constraint CK_bbsUser_uSex check(uSex in('男','女')) +-- +alter table bbsUsers add constraint CK_bbsUser_uAge check(uAge>=15 or uAge<=60) +-- +alter table bbsUsers add constraint CK_bbsUser_uPoint check(uPoint>=0) +-- + +create table bbsSection +( + sID int identity(1,1), + sName varchar(10) not null, + sUid int +) + +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +-- +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUsers(UID) + + + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int foreign key references bbsUsers(UID), + tSID int foreign key references bbsSection(sID), + tTItle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) + + +create table bbsReply +( + rID INT primary key identity(1,1), + rUID int foreign key references bbsUsers(UID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUsers values +('小雨点','女',20,0), +('逍遥','男',18,4), +('七年级生','男',19,2) + + +select * into bbsPoint from bbsUsers + +insert into bbsSection values +('技术交流',1), +('读书世界',3), +('生活百科',1), +('八卦区',3) + +insert into bbsTopic values +(2,4,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?',2008-9-10,0), +(3,1,'.NET','与JAVA的区别是什么呀?',2008-9-1,2), +(1,3,'范跑跑','谁是范跑跑',2008-7-8,1) + +insert into bbsReply values +(2,1,'不知道',2008-9-10), +(3,2,'不知道',2008-9-1), +(1,3,'不知道',2008-7-8) + + + + + + +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sID 版主编号,sName 版块名称,uName 版主姓名 from bbsSection 版主编号 inner join bbsUsers on sUid=UID + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID,uName,tTItle,tMsg,tTime from bbsTopic inner join bbsUsers on UID=tUID where tTime>'2008-9-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sID 版主编号,uName 版主姓名,sName 版块名称 from bbsSection inner join bbsUsers on sID=UID where uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 tUID 发帖人编号,uName 发帖人姓名,tTItle 主贴标题,tMsg 主贴内容,tCount 回复数量 from bbsTopic inner join bbsUsers on tUID=UID order by tCount DESC + +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select sName 版块,uName 用户,count(tTItle) 发帖总数 from bbsTopic inner join bbsUsers on tID=UID inner join bbsSection on sID=tSID group by sName,uName diff --git "a/20210326\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\344\277\241\346\201\257\350\241\250.sql" "b/20210326\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\344\277\241\346\201\257\350\241\250.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f7ca264461813aeb569efbacbe2c0762e6a536d2 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\344\277\241\346\201\257\350\241\250.sql" @@ -0,0 +1,139 @@ +use master +go + +create database Stundents + +on +( + name='Stundents', + filename='D:\TEXT\Stundents.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='Stundents_loig', + filename='D:\TEXT\Stundents_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go +use Stundents + +go + +create table StuInfo +( + StuNO char(5) primary key, + StuName nchar(5), + StuAge int, + StuAddress nchar(50), + StuSeat int identity(1,1), + StuSex char(1) check(stuSex = '1' or stuSex = '0') +) +go + +use Stundents +go +create table StuExam +( + ExamNO int identity(1,1) primary key, + StuNO char(5) constraint FK_stuInfo_stuNO references StuInfo(stuNO), + WrittenExam int check(WrittenExam >= 0 and WrittenExam <= 100), + LabExam int check(LabExam >= 0 and LabExam <= 100) +) +go + +use Stundents +go +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSex) values ('s2501','张秋利',20,'美国硅谷','1'), +('s2502','李斯文',18,'湖北武汉','0'),('s2503','马文才',22,'湖南长沙','1'),('s2504','欧阳俊雄',21,'湖北武汉','0'), +('s2505','梅超风',20,'湖北武汉','1'),('s2506','陈旋风',19,'美国硅谷','1'),('s2507','陈风',20,'美国硅谷','0') +go + +use Stundents +go +insert into StuExam(StuNO,WrittenExam,LabExam) values ('s2501','50','70'),('s2502','60','65'),('s2503','86','65'), +('s2504','40','85'),('s2505','70','90'),('s2506','85','90') +go + +select StuNo 学号,StuName 姓名, StuAge 年龄, StuAddress 地址,StuSeat 座位号,StuSex 性别 from StuInfo + +select StuName,StuAge,StuAddress from StuInfo + +select StuNO 学号,笔试 = WrittenExam,LabExam as 机试 from StuExam + +select StuNo,stuName,StuAddress,邮箱 = StuName + '@' + StuAddress from StuInfo + +select StuNo,WrittenExam,LabExam,总分 = WrittenExam + LabExam from StuExam +select distinct StuAddress from StuInfo + +select stuAge, count(*) 所有年龄 from StuInfo group by StuAge + +select top 3 * from StuInfo + +select top 4 StuName,StuSeat from StuInfo + +select top 50 percent * from StuInfo + +select * from StuInfo where StuAddress = '湖北武汉' and StuAge = 20 + +select * from StuExam where LabExam >= 60 and LabExam <= 80 order by LabExam desc + +select * from StuInfo where StuAddress = '湖北武汉' or StuAddress = '湖南长沙' +select * from StuInfo where StuAddress in ('湖北武汉' , '湖南长沙') + +select * from StuExam where WrittenExam < 70 or WrittenExam > 90 order by WrittenExam asc + +select * from StuInfo where StuAge is null + +select * from StuInfo where StuAge is not null + +select * from StuInfo where StuName like '张%' + +select * from StuInfo where StuAddress like'%湖%' + +select * from StuInfo where StuName like '张_' + +select * from StuInfo where StuName like '__俊%' + +select * from StuInfo order by StuAge desc + +select * from StuInfo order by StuAge desc, stuSeat asc + +select top 1 * from StuExam order by WrittenExam desc + +select top 1 * from StuExam order by LabExam asc + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 + +select StuExam.StuNO 学号,stuName 姓名,StuAge 年龄,WrittenExam 笔试,LabExam 机试 +from StuInfo inner join StuExam on StuExam.StuNO = StuInfo.StuNO + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 + +select StuExam.StuNO 学号,StuName 姓名,WrittenExam 笔试,LabExam 机试 +from StuInfo inner join StuExam on StuExam.StuNO = StuInfo.StuNO where WrittenExam > 60 and LabExam > 60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 + +select StuExam.StuNO 学号,StuName 姓名,WrittenExam 笔试,LabExam 机试 +from StuInfo left join StuExam on StuExam.StuNO = StuInfo.StuNO + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 + +select StuExam.StuNO 学号,StuName 姓名,StuAge 年龄,WrittenExam 笔试,LabExam 机试 +from StuInfo inner join StuExam on StuExam.StuNO = StuInfo.StuNO where StuAge >= 20 order by WrittenExam desc + +--5.查询男女生的机试平均分 + +select StuSex 性别,COUNT(*) 人数,AVG(LabExam) 平均分 +from StuInfo inner join StuExam on StuExam.StuNO = StuInfo.StuNO group by StuSex + +--6.查询男女生的笔试总分 + +select StuSex 性别,COUNT(*)人数,sum(WrittenExam) 笔试总分 +from StuInfo inner join StuExam on StuExam.StuNO = StuInfo.StuNO group by StuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\346\226\207\345\205\267\350\241\250.sql" "b/20210326\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\346\226\207\345\205\267\350\241\250.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0ace4b5506b8f7d15d903d0961c35192c81f7093 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\346\226\207\345\205\267\350\241\250.sql" @@ -0,0 +1,127 @@ +use master + +go + +create database TEXT + +go +use TEXT + +go + +create table orders +( + orderID int primary key identity(1,1), + orderDate date +) + +go +use TEXT +go + +create table orderItem +( + ItemiD int primary key identity(1,1), + orderId int, + itemType nvarchar(20), + itemName nvarchar(20), + theNumber int, + theMoney money +) +go +insert into orders (orderDate) values +('2008-01-12 00:00:00.000'), +('2008-02-10 00:00:00.000'), +('2008-02-15 00:00:00.000'), +('2008-03-10 00:00:00.000') +go +insert into orderItem values(1,'文具','笔',72,2),(1,'文具','尺',10,1),(1,'体育用品','篮球',1,56),(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3),(2,'日常用品','透明胶',2,1),(2,'体育用品','羽毛球',20,3),(3,'文具','订书机',20,3),(3,'文具','订书针',10,3), +(3,'文具','裁纸机',5,5),(4,'文具','笔',20,2),(4,'文具','信纸',50,1),(4,'日常用品','毛巾',4,5),(4,'日常用品','透明胶',30,1),(4,'体育用品','羽毛球',20,3) + +select * from orderItem + +--1.查询所有订单订购的所有物品数量总和 + +select sum(theNumber)数量 from orderItem + +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 + +select orderId 订单编号,avg(theMoney)平均单价,theNumber 物品数量 from orderItem where orderId<3 group by orderId,theMoney,theNumber having +avg(theMoney)<10 + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 + +select avg(theMoney) 平均单价,sum(theNumber) 数量总和 from orderItem group by theMoney,theNumber having avg(theMoney)<10 and sum(theNumber)>50 + +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 + +select itemType 类别, COUNT(*) 订购次数 from orderItem group by itemType + +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 + +select itemType 类别,sum(theNumber) 总数量,avg(theMoney) 平均单价 from orderItem group by itemType having sum(theNumber)>100 + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: + +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 + +select itemName 名称, COUNT(*) 订购次数, SUM(theNumber)总数量,avg(theMoney) 平均单价 from orderItem group by itemName + + + + + + + + + + + + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + +select orders.orderID,orderDate,itemType,itemName,theNumber,theMoney from orderItem inner join orders on orders.orderID = orderItem.orderId + + +--2.查询 订购数量 大于50的 订单的编号,订单日期,订购产品的类别和订购的产品名称 + +select orders.orderID 编号,theNumber 数量,orderDate 日期,itemType 类别,itemName 名称 +from orderItem inner join orders on orders.orderID = orderItem.orderId where theNumber > 50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量 和订购单价以及 订购总价 +--select OI.orderID,orderDate,itemType,itemName,theNumber,theMoney,theNumber*theMoney 订购总价 +--from orderItem OI left join orders O on OI.orderId=O.orderID + +select orders.orderID 编号,orderDate 日期,itemType 类别,itemName 名称,theNumber 数量,theMoney 单价,theNumber*theMoney 订购总价 +from orderItem left join orders on orders.orderID = orderItem.orderId + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select orders.orderID 编号,orderDate 日期,itemType 类别,itemName 名称,theNumber 数量,theMoney 单价,theMoney*theNumber 总价 +from orderItem left join orders on orders.orderID = orderItem.orderId where theNumber >= 50 and theMoney >= 5 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 + +select orders.orderID 编号,itemType 类别 from orderItem inner join orders on orders.orderID = orderItem.orderId + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select * from orderItem +select orders.orderID 编号,itemType 产品类别,orderItem.orderId 订购次数,theNumber 数量,orderItem.orderId*theNumber 总数量 +from orderItem inner join orders on orders.orderID = orderItem.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\350\256\272\345\235\233\344\275\234\344\270\232.sql" "b/20210326\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\350\256\272\345\235\233\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a421f525dd125995e20754ab70171ea0d2615967 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\350\256\272\345\235\233\344\275\234\344\270\232.sql" @@ -0,0 +1,156 @@ +use master +go + +create database bbs +go + +use bbs +go + +create table bbsUsers +( + UID int identity(1,1) , + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +-- +alter table bbsUsers add constraint PK_bbsUser_UID primary key(UID) +-- +alter table bbsUsers add constraint UK_bbsUser_uName unique(uName) +-- +alter table bbsUsers add constraint CK_bbsUser_uSex check(uSex in('男','女')) +-- +alter table bbsUsers add constraint CK_bbsUser_uAge check(uAge>=15 or uAge<=60) +-- +alter table bbsUsers add constraint CK_bbsUser_uPoint check(uPoint>=0) +-- + +create table bbsSection +( + sID int identity(1,1), + sName varchar(10) not null, + sUid int +) + +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +-- +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUsers(UID) + + + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int foreign key references bbsUsers(UID), + tSID int foreign key references bbsSection(sID), + tTItle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) + + +create table bbsReply +( + rID INT primary key identity(1,1), + rUID int foreign key references bbsUsers(UID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUsers values +('小雨点','女',20,0), +('逍遥','男',18,4), +('七年级生','男',19,2) + + +select * into bbsPoint from bbsUsers + +insert into bbsSection values +('技术交流',1), +('读书世界',3), +('生活百科',1), +('八卦区',3) + +insert into bbsTopic values +(2,4,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?',2008-9-10,0), +(3,1,'.NET','与JAVA的区别是什么呀?',2008-9-1,2), +(1,3,'范跑跑','谁是范跑跑',2008-7-8,1) + +insert into bbsReply values +(2,1,'不知道',2008-9-10), +(3,2,'不知道',2008-9-1), +(1,3,'不知道',2008-7-8) + +select * from bbsReply --回帖表 +select * from bbsSection --版块表 +select * from bbsTopic --主帖表 +select * from bbsUsers --用户信息表 + +--用户信息表(bbsUsers) +-- 用户编号 UID int 主键 标识列 +-- 用户名 uName varchar(10) 唯一约束 不能为空 +-- 性别 uSex varchar(2) 不能为空 只能是男或女 +-- 年龄 uAge int 不能为空 范围15-60 +-- 积分 uPoint int 不能为空 范围 >= 0 + +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select bbsUsers.UID 版主编号,uName 姓名,sName 版块名称 +from bbsSection inner join bbsUsers on bbsUsers.UID = bbsSection.sUid + +-- 版块表(bbsSection) +-- 版块编号 sID int 标识列 主键 +-- 版块名称 sName varchar(10) 不能为空 +-- 版主编号 sUid int 外键 引用用户信息表的用户编号 + + + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + +select bbsUsers.UID 编号,uName 姓名,tTitle 标题,tMsg 内容,tTime 时间 +from bbsTopic inner join bbsUsers on bbsUsers.UID = bbsTopic.tUID where tTime > '2008-9-15' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select bbsUsers.UID 编号,uName 姓名,sName 版块名称,uAge 年龄 +from bbsSection inner join bbsUsers on bbsUsers.UID = bbsSection.sUid where uAge < 20 + + +--+ 主贴表(bbsTopic) +-- 主贴编号 tID int 主键 标识列, +-- 发帖人编号 tUID int 外键 引用用户信息表的用户编号 +-- 版块编号 tSID int 外键 引用版块表的版块编号 (标明该贴子属于哪个版块) +-- 贴子的标题 tTitle varchar(100) 不能为空 +-- 帖子的内容 tMsg text 不能为空 +-- 发帖时间 tTime datetime +-- 回复数量 tCount int + + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select bbsUsers.UID 编号,uName 姓名,tTitle 标题,tMsg 内容,tCount 回复数量 +from bbsTopic inner join bbsUsers on bbsUsers.UID = bbsTopic.tUID where tCount = (select MAX(tCount) from bbsTopic) + +---5.在主贴表中查询每个版块中每个用户的发帖总数 +select sID 版块,uName 姓名,COUNT(*)总数 from bbsSection inner join bbsUsers on bbsUsers.UID = bbsSection.sUid +inner join bbsTopic on bbsTopic.tUID = bbsUsers.UID group by sID,uName + +--+ 回帖表(bbsReply) +-- 回贴编号 rID int 主键 标识列, +-- 回帖人编号 rUID int 外键 引用用户信息表的用户编号 +-- 对应主贴编号 rTID int 外键 引用主贴表的主贴编号 (标明该贴子属于哪个主贴) +-- 回帖的内容 rMsg text 不能为空 +-- 回帖时间 rTime datetime + + + + + + + + + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + + diff --git "a/20210326\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3b869f11e2fca4afd522525fe7e27d929cfa5d41 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" @@ -0,0 +1,70 @@ +use master +go +create database Student1 +on +( name=' Student1', + filename='D:\Student1.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=15% + +) + log on +( name='Student1_log', + filename='D:\Student1_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=15% +) +go +use Student1 +go + +create table StuInfo +( + stuNo char(5) primary key(stuNo), + stuName nvarchar(20), + stuAge int, + stuAddress text, + stuSeat int identity(1,1), + stuSex char(1) check(stuSex in(1,0)) +) + +create table StuExam +( + examNo int identity(1,1), + stuNo char(5), + writtenExam int check(writtenExam>=0 and writtenExam<=100), + labExam int check(labExam>=0 and labExam<=100) +) + + alter table StuExam add constraint RK_StuExam_stuNo foreign key(stuNo) references StuInfo(stuNo) + + insert into StuInfo values + ('s2501','张秋利',20,'美国硅谷',1), + ('s2502','李斯文',18,'湖北武汉',0), + ('s2503','马文才',22,'湖南长沙',1), + ('s2504','欧阳俊雄',21,'湖北武汉',0), + ('s2505','梅超风',20,'湖北武汉',1), + ('s2506','陈旋风',19,'美国硅谷',1), + ('s2507','陈风',20,'美国硅谷',0) + + + insert into StuExam values + ('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) + +--数据如图片1,使用上次作业的数据 + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 from StuInfo SI join StuExam SE on SI.stuNo=SE.stuNo +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 from StuInfo SI join StuExam SE on SI.stuNo=SE.stuNo where writtenExam >60 and labExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 from StuInfo SI left join StuExam SE on SI.stuNo=SE.stuNo +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 + select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 from StuInfo join StuExam on StuInfo.stuNo=StuExam.stuNo where stuAge>=20 order by writtenExam desc +--5.查询男女生的机试平均分 + --1为男 0为女 +select stuSex,avg(labExam) 机试平均分 from StuInfo SI join StuExam SE on SI.stuNo=SE.stuNo group by stuSex +--6.查询男女生的笔试总分 +select stuSex,sum(writtenExam)from StuInfo SI join StuExam SE on SI.stuNo=SE.stuNo group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..214f60ff69e9384369e390e957b67cf9cc963cb3 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery2.sql" @@ -0,0 +1,70 @@ +use master +go +create database od12 +on +( name='od12', + filename='D:\test\od12.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) + log on +( name='od12_log', + filename='D:\test\od12_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +go +use od12 +go +create table orders +( orderId int primary key, + orderDate nchar(23) +) + +create table orderItem +( ItemiD int, + orderId int references orders(orderId), + itemType nchar(4) not null, + itemName nchar(4) not null, + theNumber int, + theMoney int +) +insert into orders values(1,'2008-01-12 00:00:00.000'),(2,'2008-02-10 00:00:00.000'), + (3,'2008-02-15 00:00:00.000'),(4,'2008-03-10 00:00:00.000') + +insert into orderItem values(1,1,'文具','笔',72,2),(2,1,'文具','尺',10,1),(3,1,'体育用品','篮球',1,56), + (4,2,'文具','笔',36,2),(5,2,'文具','固体胶',20,3),(6,2,'日常用品','透明胶',2,1), + (7,2,'体育用品','羽毛球',20,3),(8,3,'文具','订书机',20,3),(9,3,'文具','订书针',10,3), + (10,3,'文具','裁缝刀',5,5),(11,4,'文具','笔',20,2),(12,4,'文具','信纸',50,1), + (13,4,'日常用品','毛巾',4,5),(14,4,'日常用品','透明胶',30,1),(15,4,'体育用品','羽毛球',20,3) + + +select* from orders + +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select*from orders join orderItem on orders.orderId=orderItem.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select*from orders join orderItem on orders.orderId=orderItem.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem .orderId 订单的编号, orders.orderDate 订单日期 ,itemType 产品的类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价, theNumber*theMoney 订购总价 from orders join orderItem on orders.orderId=orderItem.orderId group by orderItem.orderId,itemType,itemName,theNumber,theMoney +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId 订单的编号,orders.orderDate 订单日期 ,itemType 产品的类别,itemName 产品名称,theNumber 订购数量, theNumber*theMoney 订购总价 from orders join orderItem on orders.orderId=orderItem.orderId where theMoney>=5 and theNumber>=50 group by orderItem.orderId,itemType,itemName,theNumber,theMoney +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderItem.orderId 编号,count(itemType) 订购产品数 from orders join orderItem on orders.orderId=orderItem.orderId group by orderItem.orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 +select orderItem.orderId 订单编号 ,itemType 产品类别 ,count(itemType) 订购次数,sum(theNumber) 总数量 from orders join orderItem on orders.orderId=orderItem.orderId group by orderItem.orderId,itemType \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..60d036f6ddf85bd406906acc60e67600bea3dbcb --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery3.sql" @@ -0,0 +1,91 @@ +use master +go +create database bbs1 +on +( name='bbs1', + filename='D:\test\bbs1.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='bbs1_log', + filename='D:\test\bbs1_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +go +use bbs1 +go +create table bbsUsers +( UID int identity(1,1), + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +select*from bbsUsers +alter table bbsUsers add constraint Pk_bbsUsers_UID primary key(UID) +alter table bbsUsers add constraint Uk_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint Ck_bbsUsers_uSex check(uSex in('男','女')) +alter table bbsUsers add constraint Ck_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint Ck_bbsUsers_uSex check(uPoint>=0) + +create table bbsSection +( sID int identity(1,1), + sName varchar(10) not null, + sUid int +) + +insert into bbsSection values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + +alter table bbsSection add constraint Pk_bbsSection_sID primary key(sID) +alter table bbsSection add constraint Fk_bbsSection_sUid foreign key(sUid) references bbsUsers(UID) +select*from bbsSection + + +create table bbsTopic +( tID int primary key identity(1,1), + tUID int references bbsUsers(UID), + tSID int references bbsSection( sID ), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime , + tCount int +) + +select*from bbsTopic +create table bbsReply +( rID int primary key identity(1,1), + rUID int references bbsUsers(UID), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +insert into bbsTopic values + (2,4,'范跑跑 ',' 谁是范跑跑 ','2008-7-8','1'), + (3,1,'.NET ',' 与JAVA的区别是什么呀? ','2008-9-1 ','2'), + (1,3,'今年夏天最流行什么 ',' 有谁知道今年夏天最流行 ','2008-9-10','0') +insert into bbsReply values(3,1,'谁问的谁就是范跑跑','20210316'), + (1,2,'这个更简单','20210316'), + (1,3,'今年夏天最流行内裤外穿','20210316') + +select*from bbsReply +select*from bbsSection +select*from bbsUsers + + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select UID 版主编号,uName 版主姓名,sName 版块名称 from bbsSection join bbsUsers on bbsSection.sUid=bbsUsers.UID +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号, bbsUsers.UID 发帖人姓名, tTitle 帖子的标题, tMsg 帖子的内容, tTime 发帖时间 from bbsTopic join bbsUsers on bbsUsers.UID=bbsTopic.tUID where tTime >'2008-9-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select bbsSection.sUid 版主的编号, bbsUsers.uName 版主的名称,bbsSection.sName 版块的名称 from bbsSection join bbsUsers on bbsUsers.UID=bbsSection.sUid where bbsUsers.uAge<=20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 bbsTopic.tUID 发帖人编号,bbsUsers.uName 用户 , tTitle 帖子的标题 , tMsg 帖子的内容 ,bbsTopic.tCount 回复数量 from bbsTopic join bbsReply on bbsTopic.tID=bbsReply.rTID join bbsUsers on bbsUsers.UID=bbsTopic.tUID order by bbsTopic.tCount desc +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select bbsSection.sName 板块名称, bbsUsers.uName 用户 , count(tUID) from bbsTopic join bbsUsers on bbsUsers.UID=bbsTopic.tUID join bbsSection on bbsSection.sUid=bbsUsers.UID group by sName,uName diff --git "a/20210326\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4fbb09a47a0f8799240d9bdc4efb0e93a47703ad --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" @@ -0,0 +1,69 @@ +create database Studentinfo + +on +( + name='Studentinfo', + filename='C:\app\Studentinfo.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Studentinfo_log', + filename='C:\app\Studentinfo_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +use Studentinfo +go +create table StuDent +( + sTUNO varchar(15), + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(50), + stuSeat int, + stuSex nvarchar(1) default('男') check(stuSex='男' or stuSex='女') +) + +insert into StuDent(sTUNO,stuName,stuAge,stuAddress,stuSeat,stuSex) +select 's2501','张秋里',20,'美国硅谷',1,'男' union +select 's2502','李斯文',18,'湖北武汉',2,'女' union +select 's2503','马文才',22,'湖南长沙',3,'男' union +select 's2504','欧阳俊雄',21,'湖北武汉',4,'女' union +select 's2505','梅超风',20,'湖北武汉',5,'男' union +select 's2506','陈旋风',19,'美国硅谷',6,'男' union +select 's2507','陈风',20,'美国硅谷',7,'女' + + +create table Score +( + examNO int, + stuNO varchar(15), + writtenExam varchar(200), + labExam varchar(200) +) + +insert into Score(examNO,stuNO,writtenExam,labExam) +select 1,'s2501','50','70' union +select 2,'s2502','60','65' union +select 3,'s2503','86','85' union +select 4,'s2504','40','80' union +select 5,'s2505','70','90' union +select 6,'s2506','85','90' + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName,stuAge,writtenExam,labExam from StuDent inner join Score on StuDent.sTUNO=Score.stuNO +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuDent.sTUNO,stuName,writtenExam,labExam from StuDent inner join Score on StuDent.sTUNO=Score.stuNO where Score.writtenExam>=60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select StuDent.sTUNO,StuDent.StuName,Score.writtenExam,Score.labExam from StuDent left outer join Score on StuDent.sTUNO=Score.stuNO +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName,stuAge,writtenExam,labExam from StuDent inner join Score on StuDent.sTUNO=Score.stuNO where StuDent.stuAge>=20 order by writtenExam desc +--5.查询男女生的机试平均分 +select StuSex,AVG(labExam) from StuDent inner join Score on StuDent.sTUNO=Score.stuNO group by StuSex +--6.查询男女生的笔试总分 +select StuSex,SUM(writtenExam) from StuDent left join Score on StuDent.sTUNO=Score.stuNO group by StuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c446b2f029028517f8019327ad8f798df02af97c --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" @@ -0,0 +1,91 @@ +create database asd +on +( + name='asd', + filename='C:\app\asd.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +log on +( + name='asd_log', + filename='C:\app\asd.ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +go +use asd +go +--订单表(orders)列为:订单编号(orderId 主键) 订购日期(orderDate) + +create table orders +( + orderId int primary key, + orderDate datetime +) +--插入表1数据 +insert into orders(orderId,orderDate) +select 1,'2008-01-12' union +select 2,'2008-02-10' union +select 3,'2008-02-15' union +select 4,'2008-03-10' +select * from orders +--订购项目表(orderItem),列为: +--项目编号(ItemiD)订单编号(orderId)产品类别(itemType) +--产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) +create table orderItem +( + ItemiD int primary key identity(1,1), + orderId int, + itemType varchar(20), + itemName varchar(20), + theNumber int , + theMoney int +) +--添加表二的外键约束 关联表一的order id +alter table orderItem add constraint FK foreign key (orderId) references orders(orderId) +--插入表2数据 +insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) +select 1,'文具','笔',72,2 union +select 1,'文具','尺',10,1 union +select 1,'体育用品','篮球',1,56 union +select 2,'文具','笔',36,2 union +select 2,'文具','固体胶',20,3 union +select 2,'日常用品','透明胶',2,1 union +select 2,'体育用品','羽毛球',20,3 union +select 3,'文具','订书机',20,3 union +select 3,'文具','订书针',10,3 union +select 3,'文具','裁纸刀',5,5 union +select 4,'文具','笔',20,2 union +select 4,'文具','信纸',50,1 union +select 4,'日常用品','毛巾',4,5 union +select 4,'日常用品','透明胶',30,1 union +select 4,'体育用品','羽毛球',20,3 + +--Select*from orders +--Select*from orderItem + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney from orders inner join orderItem on orders.orderId=orderItem.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orders.orderId,theNumber,orderDate,itemType,itemName from orders inner join orderItem on orders.orderId=orderItem.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId,orderDate,itemType,itemName,theMoney,itemName*theMoney from orders inner join orderItem on orders.orderId=orderItem.orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +Select orders.orderId,orderDate,itemType,itemName,theMoney,itemName*theMoney from orders inner join orderItem on orders.orderId=orderItem.orderId where theMoney>5 and itemName>50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderId,COUNT(itemName) from orderItem group by orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +select orderId,itemType,COUNT(itemType),SUM(theMoney) from orderItem group by orderId,itemType order by orderId asc +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c3f876e275ff72bc8ff5b69074ce69d4ce50795b --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery3.sql" @@ -0,0 +1,111 @@ +use master +go +create database BBS +on +( +name='BBS', +filename='C:\app\BBS.mdf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +log on +( +name='BBS_log', +filename='C:\app\BBS_log.ldf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +go +use BBS +go +create table BBSUsers--用户信息表 +( +UID int not null, +UName varchar(10) not null, +USex varchar(2) not null, +UAge int not null, +UPoint int not null +) + +alter table BBSUsers add constraint PK_BBSUsers_UID primary key(UID) +alter table BBSUsers add constraint UK_BBSUsers_UName unique(UName) +alter table BBSUsers add constraint CK_BBSUsers_USex check(USex='男' or USex='女') +alter table BBSUsers add constraint CK_BBSUsers_UAge check(UAge>=15 and UAge<=60) +alter table BBSUsers add constraint CK_BBSUsers_UPoint check(UPoint>=0) + +go +use BBS +go +create table BBSSection--版块表 +( +SID int not null, +SName varchar(10) not null, +SUID int not null +) +alter table BBSSection add constraint PK_BBSSection_SID primary key(SID) +alter table BBSSection add constraint FK_BBSSection_SUID foreign key (SUID) references BBSUsers(UID) + +go +use BBS +go +create table BBSTopic--主贴表 +( +TID int primary key identity(1,1) not null, +TUID int references BBSUsers(UID) not null, +TSID int references BBSSection(SID) not null, +TTitle varchar(100) not null, +TMsg text not null, +TTime datetime , +Tcount int , +) + +go +use BBS +go +create table BBSReply--回帖表 +( +RID int primary key identity(1,1) not null, +RUID int references BBSUsers(UID) not null, +RTID int references BBSTopic(TID) not null, +RMsg text , +RTime datetime , +) +insert into BBSUsers (UID,UName , USex , UAge , UPoint) values(1,'小雨点','女','20','0') +insert into BBSUsers (UID,UName , USex , UAge , UPoint) values(2,'逍遥','男','18','4') +insert into BBSUsers (UID,UName , USex , UAge , UPoint) values(3,'七年级生','男','19','2') + +select UName,UPoint into BBSPoint from BBSUsers + +insert into BBSSection (SID,SName,SUID) values(4,'技术交流',1) +insert into BBSSection (SID,SName,SUID) values(5,'读书世界',3) +insert into BBSSection (SID,SName,SUID) values(6,'生活八卦',1) +insert into BBSSection (SID,SName,SUID) values(7,'八卦区',3) + +insert into BBSTopic (TUID,TSID,TTitle,TMsg,TTime,Tcount) values(2,7,'范跑跑','谁是范跑跑','2008-7-8',1) +insert into BBSTopic (TUID,TSID,TTitle,TMsg,TTime,Tcount) values(3,4,'.NET','与JAVA的区别是什么','2008-9-1',2) +insert into BBSTopic (TUID,TSID,TTitle,TMsg,TTime,Tcount) values(1,6,'今年夏天最流行什么','有谁知道今年夏天最流行什么','2008-9-10',0) + +insert into BBSReply (RUID,RMsg,RTime,RTID) values(1,'范跑跑是什么人','2008-10-1',1) +insert into BBSReply (RUID,RMsg,RTime,RTID) values(2,'它们的区别是什么','2008-11-1',2) +insert into BBSReply (RUID,RMsg,RTime,RTID) values(3,'最流行什么','2008-11-20',3) + + +select * from BBsReply --回帖表 +select * from BBsSection --版块表 +select * from BBsTopic --主帖表 +select * from BBsUsers --用户信息表 + + + +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select SID,UName,SName from BBSUsers inner join BBSSection on BBSUsers.UID=BBSSection.SUID +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select TUID,UName,TTitle,TMsg,TTime from BBSUsers inner join BBSTopic on BBSUsers.UID=BBSTopic.TUID where TTime>2008-9-15 +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select SID,UName,SName,UAge from BBSUsers inner join BBSSection on BBSUsers.UID=BBSSection.SUID where BBSUsers.UAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 TUID,UNAme,TTitle,TMsg,Tcount from BBSTopic inner join BBSUsers on BBSTopic.TUID=BBSUsers.UID ORDER BY Tcount DESC +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select UName,SName,COUNT(*) from BBSTopic inner join BBSSection on BBSTopic.TUID=BBSSection.SUID inner join BBSUsers on BBSTopic.TUID=BBSUsers.UID group by UName,SName diff --git "a/20210326\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery5.sql" "b/20210326\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7fbc34e0d3ced0193ae997362f4a9efa4c9ea3b9 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery5.sql" @@ -0,0 +1,192 @@ +use master +go +create database task +go +use task +go +create table Student +( +stuid nvarchar(10) primary key, +stuname nvarchar(5) not null , +stuage int , +stuaddress nvarchar(200), +stuseat int identity(1,1), +stusex int check(stusex=1 or stusex=0) +) +create table result +( +examn0 int primary key identity(1,1), +stuid nvarchar(10) references Student(stuid), +writtenexam int check(writtenexam>=0 or writtenexam<=100), +labexam int check(labexam>=0 or labexam<=100) +) + +insert into Student +select 's2501','张秋利','20','美国硅谷','1' union +select 's2502','李斯文','18','湖北武汉','0' union +select 's2503','马文才','22','湖南长沙','1' union +select 's2504','欧阳俊雄','21','湖北武汉','0' union +select 's2505','梅超风','20','湖北武汉','1'union +select 's2506','陈旋风','19','美国硅谷','1' union +select 's2507','陈风','20','美国硅谷','0' +select * from Student +insert into result +select 's2501','50','70' union +select 's2502','60','65' union +select 's2503','86','85' union +select 's2504','40','80' union +select 's2505','70','90' union +select 's2506','85','90' +select * from result + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuname 姓名,stuage 年龄,r.writtenexam 笔试成绩,r.labexam 机试成绩 from Student s inner join result r on s.stuid=r.stuid +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select s.stuname 姓名,s.stuid 学号 ,writtenexam 笔试成绩,labexam 机试成绩 from result r inner join Student s on s.stuid=r.stuid where writtenexam>60 and labexam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select s.stuid 学号,s.stuname 姓名, writtenexam 笔试成绩,labexam 机试成绩 from Student s left join result r on s.stuid=r.stuid +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuname 姓名,stuage 年龄,r.writtenexam 笔试成绩,r.labexam 机试成绩 from Student s inner join result r on s.stuid= r.stuid where s.stuage>=20 order by writtenexam desc +--5.查询男女生的机试平均分 +select avg(r.labexam) 机试平均分,stusex 性别 from Student s inner join result r on s.stuid=r.stuid group by stusex +--6.查询男女生的笔试总分 +select stusex 性别,sum(r.writtenexam) 笔试总分 from Student s inner join result r on s.stuid=r.stuid group by stusex + + +use Students +go +create table orders +( + orderId int primary key identity, + orderDate datetime, +) + +create table orderItem +( + ItemiD int primary key identity, + orderId int references orders(orderId), + itemType nvarchar(20), + itemName nvarchar(20), + theNumber int, + theMoney money +) + +insert into orders values('2008-01-12 00:00:00.000'), +('2008-02-10 00:00:00.000'), +('2008-02-15 00:00:00.000'), +('2008-03-10 00:00:00.000') +insert into orderItem values(1,'文具','笔',72,'2'), +(1,'文具','尺',10,'1'), +(1,'体育用品','篮球',1,'56'), +(2,'文具','笔',36,'2'), +(2,'文具','固体胶',20,'3'), +(2,'日常用品','透明胶',2,'1'), +(2,'体育用品','羽毛球',20,'3'), +(3,'文具','订书机',20,'3'), +(3,'文具','订书针',20,'3'), +(3,'文具','裁纸刀',5,'5'), +(4,'文具','笔',20,'2'), +(4,'文具','信封',50,'1'), +(4,'日常用品','毛巾',4,'5'), +(4,'日常用品','透明胶',30,'1'), +(4,'体育用品','羽毛球',20,'3') +select * from orders +select * from orderItem +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select * from orderItem inner join orders on orderItem.orderId=orders.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orders.orderId,orderDate,itemName,itemType from orderItem inner join orders on orderItem.orderId=orders.orderId +where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney,theNumber * theMoney 订购总价 +from orderItem inner join orders on orderItem.orderId=orders.orderId +group by orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney,theNumber * theMoney 订购总价 +from orderItem inner join orders on orderItem.orderId=orders.orderId where theMoney>5 and theNumber>50 +group by orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderID,sum(theNumber) 订购产品数 from orderItem group by orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 + --2 日常用品 1 20 +select * from orderItem +select orderId,itemType,count(ItemName) 订购次数,sum(theNumber) 总数量 from orderItem group by orderId,itemType + +use Students +go +create table bbsUsers +( + uID int identity(1,1) not null , + uName nvarchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null + +) +alter table bbsUsers add constraint PK_bbsUsers_uID primary key(uID) +alter table bbsUsers add constraint UK_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex='男' or uSex='女') +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 or uAge<=60) +alter table bbsUsers add constraint CK_bbsUsers_uPoint check(uPoint>=0) +create table bbsSection +( + sID int identity(1,1) not null , + sName varchar(10) not null, + sUid int , + +) +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid)references bbsUsers(uID) +create table bbsTopic +( + tID int primary key identity(1,1) not null, + tUID int references bbsUsers(uID) not null , + tSID int references bbsSection(sID) , + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime not null, + tCount int not null, +) +create table bbsReply +( + rID int primary key identity(1,1) not null, + rUID int references bbsUsers(uID) , + rTID int references bbsTopic(tID) , + rMsg text not null, + rTime datetime , +) +go +use Students +go +select * from bbsSection +select * from bbsReply +select * from bbsTopic +select * from bbsUsers +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +insert into bbsSection values('技术名流',1),('读书世界',3),('生活百科',1),('八卦区',3) +insert into bbsTopic values(2,4,'范跑跑','谁是范跑跑', 2008-7-8,1),(3,2,'.NET','与JAVA的区别是什么呀?',2008-9-1,2),(1,4,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?',2008-9-10,0) +insert into bbsReply values(2,2,'一名地震自己先跑的教师',2008-7-8),(3,3,'不知道',2008-9-1),(1,1,'流行穿黑裙子',2008-9-10) +update bbsUsers set uPoint=30 where uName='小雨点' + +--1.查询出每个版块的版主编号 suid,版主姓名 uname和版块名称sname +select bbsSection.sUid 版主编号,bbsUsers.uName 版主姓名,bbsSection.sName 版块名称 from bbsSection inner join bbsUsers on bbsSection.sUid=bbsUsers.uuID +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select bt.tUID 编号,bu.uName 姓名,bt.tTitle 标题,bt.tMsg 内容,bt.tTime 时间 from bbsTopic bt inner join bbsUsers bu on bt.tUID=bu.uuID where bt.tTime<2008-9-15 +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select bs.sUid 编号,bu.uName 名称,bs.sName 版块的名称 from bbsUsers bu inner join bbsSection bs on bs.sUid=bu.uuID where bu.uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select max(bt.tCount) ,bt.tUID,bu.uName,bt.tTitle,bt.tMsg from bbsTopic bt inner join bbsUsers bu on bt.tUID=bu.uuID group by tUID,bu.uName,tTitle,tMsg,max(bt.tCount) +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select tSID 版块,tUID 用户,count(tSID) 发帖总数 from bbsTopic inner join bbsSection on sID=tID inner join bbsUsers on tUID=uID group by tSID,tUID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c647641330429c6bf2901e3891c45556442899e7 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" @@ -0,0 +1,28 @@ + +--数据如图片1,使用上次作业的数据 +select * from stuinfo +select * from StuMark +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuinfo.StuNo, StuName 姓名 ,StuAge 年龄,writtenExam 笔试成绩 ,labExam 机试成绩 +from stuinfo inner join StuMark on stuinfo.StuNo = StuMark.StuNo +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select stuinfo.StuNo, StuName 姓名 ,StuAge 年龄,writtenExam 笔试成绩 ,labExam 机试成绩 +from stuinfo inner join StuMark on stuinfo.StuNo = StuMark.StuNo +where writtenExam >60 and labExam >60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select stuinfo.StuNo 学号, StuName 姓名 ,StuAge 年龄,writtenExam 笔试成绩 ,labExam 机试成绩 +from stuinfo left join StuMark on stuinfo.StuNo = StuMark.StuNo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuinfo.StuNo 学号, StuName 姓名 ,StuAge 年龄,writtenExam 笔试成绩 ,labExam 机试成绩 +from stuinfo left join StuMark on stuinfo.StuNo = StuMark.StuNo +where StuAge>=20 +order by writtenExam desc +--5.查询男女生的机试平均分 +select StuSex 性别, avg(labExam) 机试平均成绩 +from stuinfo left join StuMark on stuinfo.StuNo = StuMark.StuNo +group by StuSex +--6.查询男女生的笔试总分 +select StuSex 性别, sum(writtenExam) 笔试总分 +from stuinfo left join StuMark on stuinfo.StuNo = StuMark.StuNo +group by StuSex diff --git "a/20210326\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery5.sql" "b/20210326\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cf04b6b3d2bd41f4b7c1f6144b4e4530a69d9a96 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery5.sql" @@ -0,0 +1,45 @@ + +--使用上次作业的订单数据库,完成下列题目: + select *from orderItem + select *from orders +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + select orderItem.orderId 编号,orderDate 订单日期,itemType 类别,itemName 名称,theNumber 数量,theMoney 单价 + from orderItem left join orders on orderItem.orderId= orders.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 + select orderItem.orderId 编号,orderDate 订单日期,itemType 类别,itemName 名称 ,theNumber 数量,theMoney 单价 + from orderItem left join orders on orderItem.orderId= orders.orderId + where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + select orderItem.orderId 编号,orderDate 订单日期,itemType 类别,itemName 名称,theNumber 数量,theMoney 单价 ,theMoney*theNumber 总价 + from orderItem left join orders on orderItem.orderId= orders.orderId group by orderItem.orderId ,orderDate,itemType,itemName ,theMoney,theNumber +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + + select orderItem.orderId 编号,orderDate 订单日期,itemType 类别,itemName 名称,theNumber 数量,theMoney 单价 ,theMoney*theNumber 总价 + from orderItem left join orders on orderItem.orderId= orders.orderId + where theNumber>= 50 and theMoney<=5 + group by orderItem.orderId ,orderDate,itemType,itemName ,theMoney,theNumber + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 + select *from orderItem + select *from orders + + + select orderId 编号 ,itemName 名称, sum(theNumber) 订购产品数 from orderItem group by orderId, itemName + --6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + select orderId 编号 ,itemType 类别 ,count(itemType)订购次数 ,sum(theNumber) 订购产品数 from orderItem group by orderId, itemType + + + + + diff --git "a/20210326\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery7.sql" "b/20210326\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..dd8aaa7af69c6e9a6cd5ed3f1eef9cc0fa7a0db8 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery7.sql" @@ -0,0 +1,16 @@ +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select UID 版主编号,uName 版主姓名,sName 版块名称 +from bbsSection join bbsUsers on bbsSection.sUid=bbsUsers.UID +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号, bbsUsers.UID 发帖人姓名, tTitle 帖子的标题, tMsg 帖子的内容, tTime 发帖时间 +from bbsTopic join bbsUsers on bbsUsers.UID=bbsTopic.tUID where tTime >'2008-9-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select bbsSection.sUid 版主的编号, bbsUsers.uName 版主的名称,bbsSection.sName 版块的名称 +from bbsSection join bbsUsers on bbsUsers.UID=bbsSection.sUid where bbsUsers.uAge<=20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 bbsTopic.tUID 发帖人编号,bbsUsers.uName 用户 , tTitle 帖子的标题 , tMsg 帖子的内容 ,bbsTopic.tCount 回复数量 +from bbsTopic join bbsReply on bbsTopic.tID=bbsReply.rTID join bbsUsers on bbsUsers.UID=bbsTopic.tUID order by bbsTopic.tCount desc +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select bbsSection.sName 板块名称, bbsUsers.uName 用户 , count(tUID) +from bbsTopic join bbsUsers on bbsUsers.UID=bbsTopic.tUID join bbsSection on bbsSection.sUid=bbsUsers.UID group by sName,uName \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery4.sql" "b/20210326\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9b5242974b689107a0faefe1a7ca34be18258fe6 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery4.sql" @@ -0,0 +1,68 @@ +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select * from StuDent +select StuDent.stuName 姓名, StuDent.stuAge 年龄,Score.writtenExam 笔试成绩,Score.labExam 机试成绩 from StuDent inner join Score on StuDent.stuNO=Score.stuNO + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select * from StuDent +select * from Score +select Score.writtenExam,Score.labExam,StuDent.sTUNO,stuName from Score inner join StuDent on Score.stuNO=StuDent.sTUNO where writtenExam>60 and labExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select StuDent.sTUNO,StuDent.stuName,Score.writtenExam,Score.labExam from StuDent left join Score on StuDent.stuNO=Score.stuNO + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select StuDent.stuAge,StuDent.stuName,Score.writtenExam,Score.labExam from StuDent inner join Score on StuDent.stuNO=Score.stuNO where stuAge>=20 order by writtenExam desc + +--5.查询男女生的机试平均分 +select StuDent.stuSex,avg(Score.labExam) from StuDent inner join Score on StuDent.stuNO=Score.stuNO group by StuDent.stuSex + +--6.查询男女生的笔试总分 +select StuDent.stuSex,sum(Score.writtenExam) from StuDent inner join Score on StuDent.stuNO=Score.stuNO group by StuDent.stuSex + + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + +select orderItem.orderId,orders.orderDate,orderItem.itemType,orderItem.itemName from orderItem inner join orders on orderItem.orderId=orders.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orderItem.orderId,orders.orderDate,orderItem.itemType,orderItem.itemName from orderItem inner join orders on orderItem.orderId=orders.orderId where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId,orders.orderDate,orderItem.itemType,orderItem.itemName,orderItem.theNumber,orderItem.theNumber*theMoney from orderItem inner join orders on orderItem.orderId=orders.orderId + +--5.查询每个订单分别订购了几个产品,例如: + --编号 订购产品数 + -- 1 3 + -- 2 4 +select itemType,orderId,theNumber from orderItem group by itemType,orderId,theNumber + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + + -- 1 文具 2 82 + -- 1 体育用品 1 1 + -- 2 文具 2 56 + -- 2 体育用品 1 2 + -- 2 日常用品 1 20 +select orderId,itemType,count(itemType)订购次数 ,sum(theNumber) 总数量 from orderItem group by orderId,itemType order by orderId + + +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select * from bbsUsers +select * from bbsSection +select bbsSection.sUid,bbsUsers.uName,bbsSection.sName from bbsSection inner join bbsUsers on bbsSection.sUid=bbsUsers.UID + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select bbsTopic.tTime,bbsTopic.tUID,bbsUsers.uName,bbsTopic.tTitle,bbsTopic.tMsg, bbsTopic.tTime from bbsTopic inner join bbsUsers on bbsTopic.tUID=bbsUsers.UID where tTime>2008-9-15 + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select bbsUsers.uAge,bbsSection.sUid,bbsUsers.uName,bbsSection.sName from bbsSection inner join bbsUsers on bbsSection.sUid=bbsUsers.UID group by uAge,sUid,uName,sName having uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select bbsReply.rID, bbsReply.rUID,bbsTopic.tTitle,bbsTopic.tMsg,bbsTopic.tCount from bbsReply inner join bbsTopic on bbsReply.rUID=bbsTopic.tUID where tCOunt=2 + + +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select bbsSection.sName,count(tID) from bbsTopic inner join bbsSection on bbsSection.sUid =bbsTopic.tUID group by sName, tUid + + diff --git "a/20210326\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ed8287af3911188a762d1aff153b2407fd6a5cd8 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" @@ -0,0 +1,107 @@ +use master +go +create database StuScore +on +( + name='StuScore', + filename='D:\StuScore.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='StuScore_log', + filename='D:\StuScore_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use StuScore +go +Create table stuinfo +( + stuNO varchar(20) primary key not null, + stuName nvarchar(20) check(len(StuName)>=2), + stuAge int check(StuAge>=0 AND StuAge<200) default(18) not null, + stuAddress varchar(20) not null, + stuSeat int identity(1,1), + stuSex char(1) check(stuSex='1'or stuSex='0') +) +insert into stuinfo +select 's2501','张秋利','20','美国硅谷','1' union +select 's2502','李斯文','18','湖北武汉','0' union +select 's2503','马文才','22','湖南长沙','1' union +select 's2504','欧阳俊雄','21','湖北武汉','0' union +select 's2505','梅超风','20','湖北武汉','1'union +select 's2506','陈旋风','19','美国硅谷','1' union +select 's2507','陈风','20','美国硅谷','0' +select * from stuinfo + + +Create table stuexam +( + examNO int primary key identity(1,1), + stuNO varchar(20) constraint FK_stuexam_stuNO references stuinfo(stuNO), + writtenExam int check(writtenExam>=0 AND writtenExam<=100), + labExam int check(labExam>=0 AND labExam<=100) +) +insert into stuexam +select 's2501','50','70' union +select 's2502','60','65' union +select 's2503','86','85' union +select 's2504','40','80' union +select 's2505','70','90' union +select 's2506','85','90' +select * from stuexam + + +select * from stuinfo +select 学生学号=stuNO,学生姓名=stuName,学生年龄=stuAge,学生地址=stuAddress,学生座位=stuSeat,学生性别=stuSex from stuinfo +select stuName,stuAge,stuAddress from stuinfo +select stuNO,writtenExam,labExam from stuexam +select 学号=stuNO,笔试=writtenExam,机试=labExam from stuexam +select stuNO 学号,writtenExam 笔试,labExam 机试 from stuexam +select stuNO as 学号,writtenExam as 笔试,labExam as 机试 from stuexam +select stuNO,stuName,stuAddress from stuInfo +select stuName+'@'+stuAddress 邮箱 from stuinfo +select stuNO 学号,writtenExam 笔试,labExam 机试,writtenExam+labExam 总分 from stuexam +select distinct stuAddress from stuInfo +select distinct stuAge 所有年龄 from stuInfo +select top 3 * from stuInfo +select top 4 stuName,stuSeat from stuInfo +select top 30 percent * from stuInfo +select * from stuInfo where stuAddress in ('湖北武汉') and stuAge in (20) +select * from stuExam where labExam>=60 and labExam<=80 order by labExam DESC +select * from stuInfo where stuAddress in ('湖北武汉') or stuAddress in ('湖南长沙') +select * from stuInfo where stuAddress in ('湖北武汉','湖南长沙') +select * from stuExam where not writtenExam>=70 and writtenExam<=90 order by writtenExam ASC +select * from stuInfo where stuAge is null or stuAge='' +select * from stuInfo where stuAge is not null or not stuAge='' +select * from stuInfo where stuName like '张%' +select * from stuInfo where stuAddress like '%湖%' +select * from stuInfo where stuName like '张_' +select * from stuInfo where stuName like '__俊%' +select * from stuInfo order by stuAge DESC +select * from stuInfo order by stuAge DESC,stuSeat ASC +select top 1 * from stuExam order by writtenExam DESC +select top 1 * from stuExam order by labExam ASC + + +--1.查询学生的 姓名 , 年龄 ,笔试成绩 和 机试成绩 +select * from stuinfo --姓名和年龄 +select * from stuexam--笔试和机试 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 from stuinfo inner join stuexam on stuinfo.stuNO=stuexam.stuNO +--2.查询 笔试和机试成绩都在60分以上的 学生 的 学号,姓名,笔试成绩和机试成绩 +select stuinfo.stuNO 学号,stuName 姓名,writtenExam 笔试成绩,labExam 机试成绩 from stuinfo inner join stuexam on stuinfo.stuNO=stuexam.stuNO where stuexam.writtenExam>60 and stuexam.labExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select stuinfo.stuNO 学号,stuName 姓名,writtenExam 笔试成绩,labExam 机试成绩 from stuinfo left join stuexam on stuinfo.stuNO=stuexam.stuNO +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuinfo.stuNO 学号,stuName 姓名,writtenExam 笔试成绩,labExam 机试成绩 from stuinfo left join stuexam on stuinfo.stuNO=stuexam.stuNO +where stuAge>=20 order by writtenExam DESC + +--5.查询男女生的机试平均分 +select stuName 姓名,stuSex 性别,avg(labExam) 机试平均分 from stuinfo left join stuexam on stuinfo.stuNO=stuexam.stuNO group by stuName,stuSex +--6.查询男女生的笔试总分 +select stuSex 性别,sum(writtenExam) 笔试平均分 from stuinfo left join stuexam on stuinfo.stuNO=stuexam.stuNO group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fcd2bff23284d8482c547642f724a754fac850bd --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" @@ -0,0 +1,112 @@ +use master +go +--先创建如图所示表 +create database OrderShop +on +( + name='OrderShop', + filename='D:\OrderShop.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='OrderShop_log', + filename='D:\OrderShop_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use OrderShop +go + +create table orders +( + orderId int primary key identity(1,1), + orderDate datetime +) + +create table orderItem +( + ItemId int primary key identity(1,1), + orderId int foreign key references orders(orderId), + itemType nvarchar(20), + itemName nvarchar(20), + theNumer int, + theMoney int, +) +insert into orders values +('2008-01-12'),('2008-02-10'),('2008-02-15'),('2008-03-10') + +insert into orderItem values +(1,'文具','笔','72','2'), +(1,'文具','尺','10','1'), +(1,'体育用品','篮球','1','56'), +(2,'文具','笔','36','2'), +(2,'文具','固体胶','20','3'), +(2,'日常用品','透明胶','2','1'), +(2,'体育用品','羽毛球','20','3'), +(3,'文具','订书机','20','3'), +(3,'文具','订书针','10','3'), +(3,'文具','裁纸刀','5','5'), +(4,'文具','笔','20','2'), +(4,'文具','信纸','50','1'), +(4,'日常用品','毛巾','4','5'), +(4,'日常用品','透明胶','30','1'), +(4,'体育用品','羽毛球','20','3') + + +--1.查询所有订单订购的所有物品数量总和 +select sum(theNumer) 所有物品数量总和 from orderItem +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select sum(theNumer) 所有物品的数量,avg(theMoney) 平均单价 from orderItem group by orderId having orderId<3 and avg(theMoney)<10 +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select sum(theNumer) 所有物品的数量,avg(theMoney) 平均单价 from orderItem group by orderId having sum(theNumer)>50 and avg(theMoney)<10 +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select itemType,count(*) 订购次数 from orderItem group by itemType +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select sum(theNumer)订购总数量 ,avg(theMoney) 平均单价 from orderItem group by itemType having sum(theNumer)>100 +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: + +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 + + + +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的 订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select * from orderItem +select * from orders + +select orderItem.orderId 订单的编号,orderDate 订单日期,itemType 类别,itemName 产品名称,theNumer 订购数量,theMoney 订购单价 from orderItem inner join orders on orderItem.orderId=orders.orderId +--2.查询订购数量大于50 的 订单的编号, 订单日期, 订购产品的类别 和 订购的产品名称 +select orderItem.orderId 订单的编号,orderDate 订单日期,itemType 类别,itemName 产品名称 from orderItem inner join orders on orderItem.orderId=orders.orderId +group by orderItem.orderId,orderDate,itemType,itemName having sum(theNumer)>50 +--3.查询 所有的订单的订单的编号, 订单日期, 订购产品的类别和 订购的产品名称,订购数量 和订购单价 以及订购总价 +select orderItem.orderId 订单的编号,orderDate 订单日期,itemType 类别,itemName 产品名称,theNumer 订购数量,theMoney 订购单价,sum(theMoney*theNumer) 订购总价 from orderItem inner join orders on orderItem.orderId=orders.orderId +group by orderItem.orderId,orderDate,itemType,itemName,theNumer,theMoney +--4.查询单价大于等于5并且数量大于等于50的订单的 订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId 订单的编号,orderDate 订单日期,itemType 类别,itemName 产品名称,theNumer 订购数量,theMoney 订购单价,sum(theMoney*theNumer) 订购总价 from orderItem inner join orders on orderItem.orderId=orders.orderId +where theMoney>=5 and theNumer>=50 +group by orderItem.orderId,orderDate,itemType,itemName,theNumer,theMoney +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderId,count(*) 订购产品数 from orderItem group by orderId + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +select orderId,itemType,count(*) 订购次数,sum(theNumer) 总数量 from orderItem group by orderId,itemType +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b2e7fb2d09160714ea4909ad37ae0ee5fb22d9b3 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery3.sql" @@ -0,0 +1,121 @@ +use master +go +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use bbs +go + +create table bbsUsers +( + UID int identity(1,1), + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null, +) + +alter table bbsUsers add constraint PK_bbsUsers_UID primary key (UID) +alter table bbsUsers add constraint UQ_bbsUsers_uName unique (uName) +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex='男'or uSex='女') +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint CK_bbsUsers_upoint check(uPoint>=0) + +create table bbsSection +( + sID int identity(1,1), + sName varchar(10) not null, + sUid int, +) +alter table bbsSection add constraint PK_bbsSection_sID primary key (sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key (sUid) references bbsUsers(UID) + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int constraint FK_bbsTopic_tUID references bbsUsers(UID), + tSID int constraint FK_bbsTopic_tSID references bbsSection(sID), + tTitle varchar(100) not null, + tMsg nvarchar(200) not null, + tTime datetime, + tCount int +) +create table bbsReply +( + rID int primary key identity(1,1), + rUID int constraint FK_bbsReply_rUID references bbsUsers(UID), + rTID int constraint FK_bbsReply_rTID references bbsTopic(tID), + rMsg nvarchar(200) not null, + rTime datetime +) + +insert into bbsUsers +select '小雨点','女','20', '0'union +select'逍遥', '男', '18', '4'union +select '七年级生','男','19', '2' +select uName,uPoint into bbsPoint from bbsUsers + +insert into bbsSection +select '技术交流',1 union +select '读书世界',3 union +select '生活百科',1 union +select' 八卦区',3 + +insert into bbsTopic +select 2,4,'范跑跑','谁是范跑跑',' 2008-7-8',1 union +select 3,1,'.NET','与JAVA的区别是什么呀?','2008-9-1',2 union +select 1,3,'今年夏天最流行什么',' 有谁知道今年夏天最流行什么呀?',' 2008-9-10',0 +insert into bbsReply +select 1,1,'随便','2020-12-01' union +select 2,2,'随便','2020-12-01' union +select 3,3,'随便','2020-12-01' + +alter table bbsTopic drop FK_bbsTopic_tUID +alter table bbsReply drop FK_bbsReply_rUID +alter table bbsSection drop FK_bbsSection_sUid +delete bbsUsers where uName='逍遥' + +update bbsUsers set uPoint=10 where uName='小雨点' + +alter table bbsTopic drop FK_bbsTopic_tSID +delete bbsSection where sName='生活百科' + +truncate table bbsReply + +select * from bbsPoint +select * from bbsReply +select * from bbsSection +select * from bbsTopic +select * from bbsUsers + + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sID 版主编号,uName 版主姓名,sName 版块名称 from bbsSection bS inner join bbsUsers bU on bS.sID=bU.UID +--2.查询出主贴的 发帖时间在2008-9-15以后的主贴的 发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号,uName 发帖人姓名,tTitle 标题,tMsg 内容,tTime 发帖时间 from bbsTopic inner join bbsUsers on bbsTopic.tID=bbsUsers.UID where tTime>'2008-9-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sID 版主编号,uName 版主姓名,sName 版块名称 from bbsSection bS inner join bbsUsers bU on bS.sID=bU.UID where uAge<20 +--4.查询出 回复数量最多的主贴的 发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select tUID 发帖人编号,uName 发帖人姓名,tTitle 主贴标题,tMsg 主贴内容,tCount 回复数量 from bbsTopic inner join bbsUsers on bbsTopic.tUID=bbsUsers.UID +where tCount=(select max(tCount) from bbsTopic) +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select sName 版块,uName 用户,count(*)发帖总数 from bbsTopic +left join bbsUsers on bbsTopic.tUID=bbsUsers.UID +left join bbsSection on bbsTopic.tUID=bbsSection.sUid +group by sName,uName \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fb1fb67765e9b1be12d92bcb0a4c7a463d9d8ff2 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" @@ -0,0 +1,71 @@ +create database excise +on +( + name = 'excise', + filename = 'D:\excise.mdf' +) +log on +( + name = 'excise_log', + filename = 'D:\excise_log.ldf' +) +use excise + +create table Student +( + stuNo nvarchar(10) primary key, + stuName nvarchar(10)not null, + stuAge int not null, + stuAddress nvarchar(20) null, + stuSeat int not null, + stuSex nvarchar(1) not null +) +insert into Student(stuNo,stuName,stuAge,stuAddress,stuSeat,stuSex) values +('s2501','张秋利',20,'美国硅谷',1,'男'), +('s2502','李斯文',18,'湖北武汉',2,'女'), +('s2503','马文才',22,'湖南长沙',3,'男'), +('s2504','欧阳俊雄',21,'湖北武汉',4,'女'), +('s2505','梅超风',20,'湖北武汉',5,'男'), +('s2506','陈旋风',19,'美国硅谷',6,'男'), +('s2507','陈风',20,'美国硅谷',7,'女') + +select * from Student + +create table stuExam( + examNo int primary key identity (1,1), + stuNo nvarchar(10) constraint [Fk_Exam_stuNo] foreign key([stuNo]) references [Student]([stuNO]) not null, + writtenExam int not null, + labExam int not null +) +insert into stuExam(stuNo,writtenExam,labExam) values +('s2501',50,70), +('s2502',60,65), +('s2503',86,85), +('s2504',40,80), +('s2505',70,90), +('s2506',85,90) +select * from stuExam + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 from Student s +left join stuExam e on s.stuNo = e.stuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select s.stuNo 学号,stuName 姓名,writtenExam 笔试成绩,labExam 机试成绩 from Student s +left join stuExam e on s.stuNO=e.stuNo where writtenExam>60 and labExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select s.stuNo 学号,stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 from Student s +left join stuExam e on s.stuNo = e.stuNo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 from Student s +left join stuExam e on s.stuNo = e.stuNo where stuAge>=20 order by writtenExam desc + +--5.查询男女生的机试平均分 +select stuSex 性别,avg(labExam) 平均分 from stuExam e +left join Student s on e.stuNo = s.stuNo group by stuSex + +--6.查询男女生的笔试总分 +select stuSex 性别,sum(writtenExam) from stuExam e +left join Student s on e.stuNo = s.stuNo group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a5b94f3ef626563571b4f89556c8369486196406 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" @@ -0,0 +1,88 @@ +create database shop +on +( + name = 'shop', + filename = 'D:\shop.mdf' +) +log on +( + name = 'shop_log', + filename = 'D:\shop_log.ldf' +) + +use shop + +create table orders +( + orderId int primary key identity(1,1), + orderDate datetime not null +) + +create table orderItem +( + ItemiD int identity(1,1) not null, + orderId int references orders(orderId) not null, + itemType nvarchar(10) not null, + itemName nvarchar(10) not null, + theNumber int not null, + theMoney int not null +) + +insert into orders values +('2008-01-12 00:00:00.000'), +('2008-02-10 00:00:00.000'), +('2008-02-15 00:00:00.000'), +('2008-03-10 00:00:00.000') +select * from orders + +insert into orderItem values +(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'日常用品','透明胶',20,3), +(2,'体育用品','羽毛球',20,3), +(3,'文具','订书机',20,3), +(3,'文具','订书针',10,3), +(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'日常用品','毛巾',4,5), +(4,'日常用品','透明胶',30,3), +(4,'体育用品','羽毛球',20,3) +select * from orderItem + + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select o.orderId 订单编号,orderDate 订单日期, itemType 类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价 from orders o +left join orderItem oi on o.orderId = oi.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select o.orderId 订单编号,theNumber 订购数量,orderDate 订单日期,itemType 类别,itemName 产品名称 from orders o +left join orderItem oi on o.orderId = oi.orderId where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select o.orderId 订单编号,orderDate 订单日期,itemType 类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价 from orders o +left join orderItem oi on o.orderId = oi.orderId + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select o.orderId 订单的编号,orderDate 订单日期,itemType 类别,itemName 产品名称,theNumber 订购数量,theNumber*theMoney 订购总价 from orders o +left join orderItem oi on o.orderId = oi.orderId where theMoney>5 and theNumber>= 50 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select o.orderId 编号 ,sum(theNumber) 订购产品数 from orders o +left join orderItem oi on o.orderId = oi.orderId group by o.orderId + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +-- 订单编号 产品类别 订购次数 总数量 +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select o.orderId 订单编号, itemType 产品类别, count(itemType) 订购次数,sum(theNumber)总数量 from orders o +left join orderItem oi on o.orderId=oi.orderId group by o.orderId,itemType \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..44a60f9f7039daf2a6da1cd7f213a23f03af8684 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery3.sql" @@ -0,0 +1,118 @@ +create database bbs +on +( + name = 'bbs', + filename = 'D:\bbs.mdf' +) + +log on +( + name = 'bbs_log', + filename = 'D:\bbs_log.ldf' +) +go + +use bbs +go + +create table bbsUsers +( + UID int identity(1,1) , + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) + +alter table bbsUsers add constraint PK_bbsUser_UID primary key(UID) + +alter table bbsUsers add constraint UK_bbsUser_uName unique(uName) + +alter table bbsUsers add constraint CK_bbsUser_uSex check(uSex in('男','女')) + +alter table bbsUsers add constraint CK_bbsUser_uAge check(uAge>=15 or uAge<=60) + +alter table bbsUsers add constraint CK_bbsUser_uPoint check(uPoint>=0) + + +create table bbsSection +( + sID int identity(1,1), + sName varchar(10) not null, + sUid int +) + +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) + +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUsers(UID) + + + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int foreign key references bbsUsers(UID), + tSID int foreign key references bbsSection(sID), + tTItle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) + + +create table bbsReply +( + rID INT primary key identity(1,1), + rUID int foreign key references bbsUsers(UID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUsers values +('小雨点','女',20,0), +('逍遥','男',18,4), +('七年级生','男',19,2) + + +select * into bbsPoint from bbsUsers + +insert into bbsSection values +('技术交流',1), +('读书世界',3), +('生活百科',1), +('八卦区',3) + +insert into bbsTopic values +(2,4,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?',2008-9-10,0), +(3,1,'.NET','与JAVA的区别是什么呀?',2008-9-1,2), +(1,3,'范跑跑','谁是范跑跑',2008-7-8,1) + +insert into bbsReply values +(2,1,'不知道',2008-9-10), +(3,2,'不知道',2008-9-1), +(1,3,'不知道',2008-7-8) + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称 from bbsSection +left join bbsUsers on bbsSection.sUid = bbsUsers.UID + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号,uName 发帖人姓名,tTItle 帖子的标题,tMsg 帖子的内容,tTime 发帖时间 from bbsTopic +left join bbsUsers on bbsTopic.tUID= bbsUsers.UID where tTime>'2008-9-15 00:00:00' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称, uAge 年龄 from bbsPoint +left join bbsSection on bbsPoint.UID=bbsSection.sUid where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 tUID 发帖人编号,uName 发帖人姓名,tTItle 主贴标题,tMsg 主贴内容,uPoint 回复数量 from bbsTopic +left join bbsUsers on bbsTopic.tUID= bbsUsers.UID order by uPoint desc + +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select sName 版块, uName 用户,count(tTItle) 发帖总数 from bbsTopic +left join bbsSection on bbsTopic.tUID= bbsSection.sUid +left join bbsUsers on bbsTopic.tUID= bbsUsers.UID +group by sName , uName + diff --git "a/20210326\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/\344\275\234\344\270\232\344\270\200.sql" "b/20210326\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/\344\275\234\344\270\232\344\270\200.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5b40263b56ec3afa30574ff030835a76e0d8fa32 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/\344\275\234\344\270\232\344\270\200.sql" @@ -0,0 +1,101 @@ +create database SD + +on +( + name='SD', + filename='D:\Studenta.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='SD_log', + filename='D:\Studenta_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +use SD +go +create table SD2 +( + sTUNO varchar(15), + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(50), + stuSeat int, + stuSex nvarchar(1) default('男') check(stuSex='男' or stuSex='女') +) + +insert into SD2(sTUNO,stuName,stuAge,stuAddress,stuSeat,stuSex) +select 's2501','张秋里',20,'美国硅谷',1,'男' union +select 's2502','李斯文',18,'湖北武汉',2,'女' union +select 's2503','马文才',22,'湖南长沙',3,'男' union +select 's2504','欧阳俊雄',21,'湖北武汉',4,'女' union +select 's2505','梅超风',20,'湖北武汉',5,'男' union +select 's2506','陈旋风',19,'美国硅谷',6,'男' union +select 's2507','陈风',20,'美国硅谷',7,'女' + + +create table Score +( + examNO int, + stuNO varchar(15), + writtenExam varchar(200), + labExam varchar(200) +) + +insert into Score(examNO,stuNO,writtenExam,labExam) +select 1,'s2501','50','70' union +select 2,'s2501','60','65' union +select 3,'s2501','86','85' union +select 4,'s2501','40','80' union +select 5,'s2501','70','90' union +select 6,'s2501','85','90' + +create table stuExam +(examNo int, + stuNo varchar(15), + writtenExam int, + labExam int +) +insert into stuExam(stuNo,writtenExam,labExam) values +('s2501',50,70), +('s2502',60,65), +('s2503',86,85), +('s2504',40,80), +('s2505',70,90), +('s2506',85,90) + + +select * from stuExam +select * from SD2 +select * from Score + +--数据如图片1,使用上次作业的数据 + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName 姓名, stuAge 年龄, writtenExam 笔试成绩, labExam 机试成绩 from SD2 s +left join stuExam e on s.stuNo = e.stuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select stuName 姓名, stuAge 年龄, writtenExam 笔试成绩, labExam 机试成绩 from SD2 s +left join stuExam e on s.stuNo = e.stuNo where writtenExam>60 and labExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select stuName 姓名, s.stuNo 学号,writtenExam 笔试成绩,labExam 机试成绩 from SD2 s +left join stuExam e on s.stuNo = e.stuNo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName 姓名,stuAge 年龄, writtenExam 笔试成绩, labExam 机试成绩 from SD2 s +left join stuExam e on s.stuNo = e.stuNo where stuAge>=20 order by writtenExam desc + +--5.查询男女生的机试平均分 +select stuSex,AVG(labExam)平均分 from stuExam e +left join SD2 s on e.stuNo= s.stuNo group by stuSex + +--6.查询男女生的笔试总分 +select stuSex, SUM(writtenExam) 笔试总分 from stuExam e +left join SD2 s on e.stuNo= s.stuNo group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/\344\275\234\344\270\232\344\270\211.sql" "b/20210326\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/\344\275\234\344\270\232\344\270\211.sql" new file mode 100644 index 0000000000000000000000000000000000000000..71ef22fdab3ed549ee1da34766a41e60b80bd42a --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/\344\275\234\344\270\232\344\270\211.sql" @@ -0,0 +1,90 @@ +create database bbs + +on +( + name='bbs', + filename='D:\Studenta\bbs.mdf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='bbs_log', + filename='D:\Studenta\bbs_log.ldf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +go +use bbs +go +create table bbsUsers +( + UIDD int identity, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +create table bbsSection +( + sIDD int identity, + sName varchar(10) not null, + sUid int +) + +alter table bbsUsers add constraint Pk_bbsUsers_UIDD primary key(UIDD) +alter table bbsUsers add constraint Uk_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint Ck_bbsUsers_uSex check(uSex='男'or uSex='女') +alter table bbsUsers add constraint Ck_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint Ck_bbsUsers_uPoint check(uPoint>=0) + +alter table bbsSection add constraint Pk_bbsSection_sIDD primary key(sIDD) +alter table bbsSection add constraint Fk_bbsSection_sUid foreign key(sUid) references bbsUsers(UIDD) + +create table bbsTopic +( + tID int identity primary key, + tUID int foreign key references bbsUsers(UIDD), + tSID int foreign key references bbsSection(sIDD), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +create table bbsReply +( + rID int identity primary key, + rUID int foreign key references bbsUsers(UIDD), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select uName,uPoint into bbsPoint from bbsUsers +insert into bbsSection values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) +insert into bbsTopic values(2,4,'范跑跑 ',' 谁是范跑跑 ','2008-7-8','1'),(3,1,'.NET ',' 与JAVA的区别是什么呀? ','2008-9-1 ','2'), +(1,3,'今年夏天最流行什么 ',' 有谁知道今年夏天最流行 ','2008-9-10','0') + +insert into bbsReply values(3,2,'八嘎','2008-7-8'),(1,1,'python','2008-7-8'),(1,3,'上海','2008-7-8') + +select * from bbsReply --回帖表 +select * from bbsSection --版块表 +select * from bbsTopic --主帖表 +select * from bbsUsers --用户信息表 + + +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sIDD,uName,sName from bbsSection left join bbsUsers on sIDD=UIDD +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID,tMsg,tTime,uName, tTitle from bbsTopic left join bbsUsers on tID=UIDD where tTime> '2008-9-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sIDD,uName,sName from bbsSection left join bbsUsers on sIDD=UIDD where uAge < 20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select tUID,tMsg,uName, tTitle,tcount from bbsUsers lefrt join bbsTopic c on UIDD = c.tUID order by tcount desc +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select uName 用户名,sName 版块名称,count(*)发帖总数 from bbsTopic + inner join bbsUsers on bbsTopic.tUID = bbsUsers.UIDD + inner join bbsSection on bbsUsers.UIDD= bbsSection.sUid group by uName,sName diff --git "a/20210326\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/\344\275\234\344\270\232\344\272\214.sql" "b/20210326\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/\344\275\234\344\270\232\344\272\214.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d88df8e969393219221f626b89b9764b93fef392 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/\344\275\234\344\270\232\344\272\214.sql" @@ -0,0 +1,92 @@ +create database mall on( + name='mall', + filename='D:\Studenta\mall.mdf' +) +log on( + name='mall_log', + filename='D:\Studenta\mall.ldf' +) + +use shop +go + +--订单表(orders)列为:订单编号(orderId 主键) 订购日期(orderDate) +create table orders( + orderId int primary key identity(1,1), + orderDate datetime default(getdate()) +) + +--订购项目表(orderItem),列为: +create table orderItem( +--项目编号(ItemiD)订单编号(orderId)产品类别(itemType) + ItemiD int identity(1,1), + orderId int references orders(orderId), + itemType nvarchar(20), +--产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) + itemName nvarchar(20), + theNumber int, + theMoney int +) + +insert into orders(orderDate) values +('2008-01-12 00:00:00'), +('2008-02-10 00:00:00'), +('2008-02-15 00:00:00'), +('2008-03-10 00:00:00') +select * from orders + +insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values +(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'日常用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3), +(3,'文具','订书机',20,3), +(3,'文具','订书针',10,3), +(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'日常用品','毛巾',4,5), +(4,'日常用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) +select * from orderItem +select * from orders +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select I.orderId 订单编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购产品的产品名称,theNumber 订购数量 ,theMoney 订购单价 from orderItem I +left join orders d on I.orderId = d.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select I.orderId 订单编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购产品的产品名称 from orderItem I +left join orders d on I.orderId = d.orderId where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select I.orderId 订单编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购产品的产品名称,theNumber 订购数量 ,theMoney 订购单价,theNumber*theMoney 订单总价 from orderItem I +left join orders d on I.orderId = d.orderId + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select I.orderId 订单编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购产品的产品名称,theNumber 订购数量 ,theMoney 订购单价,theNumber*theMoney 订单总价 from orderItem I +left join orders d on I.orderId = d.orderId where theMoney>=5 and theNumber>=50 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select I.orderId 订单编号,SUM(theNumber) 订购数量 from orderItem I group by I.orderId + + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select I.orderId 订单编号,itemType 产品类别,count(theNumber)订购次数,SUM(theNumber) 总数量 from orderItem I +left join orders d on I.orderId = d.orderId group by itemType,I.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery5.sql" "b/20210326\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1d43fae22d176d2ae3642fa7d60d6851f0df19f0 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery5.sql" @@ -0,0 +1,167 @@ +use master +go +create database bbs +on primary +( +name='bbs', +filename='D:\SQL作业.mdf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +log on +( +name='bbs_log', +filename='D:\SQL作业_log.ldf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +go + +use bbs + +--用户信息表(bbsUsers) +--用户编号 useID int 主键 标识列 +--用户名 uName varchar(10) 唯一约束 不能为空 +--性别 uSex varchar(2) 不能为空 只能是男或女 +--年龄 uAge int 不能为空 范围15-60 +--积分 uPoint int 不能为空 范围 >= 0 + + create table bbsUsers --建立用户信息表 + ( + useID int primary key, + uName varchar(10) unique not null, + uSex varchar(2) check(uSex='男'or uSex='女') not null, + uAge int check(uAge between 15 and 60) not null, + uPoint int check(uPoint>=0) not null + ) + go + + + + --版块表(bbsSection) +--版块编号 sID int 标识列 主键 +--版块名称 sName varchar(10) 不能为空 +--版主编号 sUid int 外键 引用用户信息表的用户编号 + +create table bbsSection --建立板块表 +( +sID int primary key, +sName varchar(10) not null, +sUid int foreign key(sUid)references bbsUsers(useID) +) +go + + +--主贴表(bbsTopic) +--主贴编号 tID int 主键 标识列, +--发帖人编号 tUID int 外键 引用用户信息表的用户编号 +--版块编号 tSID int 外键 引用版块表的版块编号 (标明该贴子属于哪个版块) +--贴子的标题 tTitle varchar(100) 不能为空 +--帖子的内容 tMsg text 不能为空 +--发帖时间 tTime datetime +--回复数量 tCount int + +create table bbsTopic --建立主贴表 +( +tID int primary key, +tUID int foreign key(tUID) references bbsUsers(useID), +tSID int foreign key(tSID) references bbsSection(sID), +tTitle varchar(100) not null, +tMsg text not null, +tTime datetime, +tCount int +) +go + + +--回帖表(bbsReply) +--回贴编号 rID int 主键 标识列, +--回帖人编号 rUID int 外键 引用用户信息表的用户编号 +--对应主贴编号 rTID int 外键 引用主贴表的主贴编号 (标明该贴子属于哪个主贴) +--回帖的内容 rMsg text 不能为空 +--回帖时间 rTime datetime + +create table bbsReply --建立回贴表 +( +rID int identity primary key, +rUID int foreign key(rUID) references bbsUsers(useID), +rTID int foreign key(rTID) references bbsTopic(tID), +rMsg text not null, +rTime datetime +) +go + +--.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +--小雨点 女 20 0 +--逍遥 男 18 4 +--七年级生 男 19 2 + +select * from bbsUsers +insert bbsUsers values(1,'小雨点','女',20,0),(2,'逍遥','男',18,4),(3,'七年级生','男',19,2) + +--.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 + --插入多行: + --先创建好空表,然后再插入数据, + --直接插入数据,然后自动生成表。 + --insert into bbsPoint select ,uPoint from bbsUsers + --select uName,uPoint into bbsPoint from bbsUsers + + select uName,uPoint into bbsPoint from bbsUsers + + --给论坛开设4个板块 + --名称 版主名 + --技术交流 小雨点 + --读书世界 七年级生 + --生活百科 小雨点 + --八卦区 七年级生 + +insert bbsSection values(1,'技术交流',1),(2,'读书世界',2),(3,'生活百科',3),(4,'八卦区',3) + +--向主贴和回帖表中添加几条记录 + --主贴: + --发帖人 板块名 帖子标题 帖子内容 发帖时间 回复数量 + --逍遥 八卦区 范跑跑 谁是范跑跑 2008-7-8 1 + --七年级生 技术交流 .NET 与JAVA的区别是什么呀? 2008-9-1 2 + --小雨点 生活百科 今年夏天最流行什么 有谁知道今年夏天最流行什么呀? 2008-9-10 0 + +insert into bbsTopic(tID,tTitle,tMsg,tTime,tCount) +select 1,'范跑跑','谁是范跑跑','2008-7-8',1 union +select 2,'.NET','与JAVA的区别是什么呀?','2008-9-1',2 union +select 3,'今年夏天最流行什么呀?','有谁知道今年夏天最流行什么呀?','2008-9-10',0 + +--回帖: + --分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 + +insert into bbsReply(rMsg,rTime) +select '我是范跑跑','2008-7-10' union +select '没区别','2008-9-2' union +select '最流行冰激凌','2008-9-10' + + + +select * from bbsUsers +select * from bbsTopic +select * from bbsSection +select * from bbsReply + +--1.查询出每个版块的版主编号,版主姓名和版块名称 + +select s1.useID 版主编号,uName 版主姓名,sName 板块名称 from bbsUsers s1 inner join bbsSection s2 on s1.useID=s2.sUid + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + +select useID 发帖人编号,uName 发帖人姓名,tTitle 帖子的标题,tMsg 帖子的内容,tTime 发帖时间 from bbsUsers s1 inner join bbsTopic s2 on s1.useID=s2.tID where tTime>'2008-9-15' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 + +select useID 版主编号,uName 版主姓名,sName 板块名称 from bbsUsers s1 inner join bbsSection s2 on s1.useID=s2.sUid where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 + +select useID,uName,tTitle,tMsg,tCount from bbsUsers s1 inner join bbsTopic s2 on s1.useID=s2.tID group by useID,uName,tTitle,tMsg,tCount having tCount=max(tCount) + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select useID,sum() from bbsTopic s1 inner join bbsUsers s2 on s1.tID=s2.useID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery8.sql" "b/20210326\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery8.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4e294a3b429729e47eeae3b95f467864d8b47515 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery8.sql" @@ -0,0 +1,91 @@ +use master +go + +create database Student +on +( + FileName='D:\SQL作业1.mdf', + Name='Student', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +log on +( + FileName='D:\SQL作业1_log.ldf', + Name='Student_log', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +go + +use Student + +go + +create table Student +( + StuNo varchar(10) primary key not null, + StuName nvarchar(10) not null, + StuAge int, + StuAddress nvarchar(20), + StuSeat int, + StuSex int check(StuSex=1 or StuSex=0) not null +) + +create table Score +( + ExamNo int primary key identity(1,1) not null, + StuNo varchar(10) not null, + WrittenExam int default(0) not null, + LabExam int not null +) + + +insert into Student values +('s2501','张秋利',20,'美国硅谷',1,1), +('s2502','李斯文',18,'湖北武汉',2,0), +('s2503','马文才',22,'湖南长沙',3,1), +('s2504','欧阳俊雄',21,'湖北武汉',4,0), +('s2505','梅超风',20,'湖北武汉',5,1), +('s2506','陈旋风',19,'美国硅谷',6,1), +('s2507','陈风',20,'美国硅谷',7,0) + +select * from Student + + +insert into Score values +('s2501',50,70), +('s2502',60,65), +('s2503',86,85), +('s2504',40,80), +('s2505',70,90), +('s2506',85,90) + +alter table Score add constraint FK_Score_StuNo foreign key(StuNo) references Student(StuNo) + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 + +select StuName 姓名,StuAge 年龄,WrittenExam 笔试成绩,LabExam 机试成绩 from Student s1 inner join Score s2 on s1.StuNo=s2.StuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 + +select s1.StuNo 学号,StuName 姓名,WrittenExam 笔试成绩,LabExam 机试成绩 from Student s1 inner join Score s2 on s1.StuNo=s2.StuNo where WrittenExam>60 and LabExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 + +select s1.StuNo 学号,StuName 姓名,WrittenExam 笔试成绩,LabExam 机试成绩 from Student s1 left join Score s2 on s1.StuNo=s2.StuNo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 + +select StuName,StuAge,WrittenExam,LabExam from Student s1 inner join Score s2 on s1.StuNo=s2.StuNo order by WrittenExam desc + +--5.查询男女生的机试平均分 + +select StuSex 男女,avg(LabExam) 平均分 from Student s1 inner join Score s2 on s1.StuNo=s2.StuNo group by StuSex having StuSex=1 or StuSex=0 + +--6.查询男女生的笔试总分 + +select StuSex 男女,sum(WrittenExam) 总分 from Student s1 inner join Score s2 on s1.StuNo=s2.StuNo group by StuSex having StuSex=1 or StuSex=0 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery9.sql" "b/20210326\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery9.sql" new file mode 100644 index 0000000000000000000000000000000000000000..59f62b80829419fda03d2b3560c672d9d4d225eb --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery9.sql" @@ -0,0 +1,104 @@ +use master + +go + +create database DD --建立数据库 +on primary +( +name='D:\SQL作业3', +filename='D:\SQL作业3.mdf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +log on +( +name='D:\SQL作业3_log', +filename='D:\SQL作业3_log.ldf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +go + +use DD +create table orders --建立订单表 +( +orderId int primary key, +orderDate datetime +) +go + +create table orderltem +( +ltemiD int primary key, +orderId int, +itemType varchar(15), +itemName varchar(20), +theNumber int, +theMoney int +) +go + +insert orders(orderId,orderDate) +select 1,'2008-01-12 00:00:00.000' union +select 2,'2008-02-10 00:00:00.000' union +select 3,'2008-02-15 00:00:00.000' union +select 4,'2008-03-10 00:00:00.000' + +select * from orders + +insert into orderltem(ltemiD,orderId,itemType,itemName,theNumber,theMoney) +select 1,1,'文具','笔',72,2 union +select 2,1,'文具','尺',10,1 union +select 3,1,'体育用品','篮球',1,56 union +select 4,2,'文具','笔',36,2 union +select 5,2,'文具','固体胶',20,3 union +select 6,2,'日常用品','透明胶',2,1 union +select 7,2,'体育用品','羽毛球',20,3 union +select 8,3,'文具','订书机',20,3 union +select 9,3,'文具','丁书针',10,3 union +select 10,3,'文具','裁纸刀',5,5 union +select 11,4,'文具','笔',20,2 union +select 12,4,'文具','信纸',50,1 union +select 13,4,'日常用品','毛巾',4,5 union +select 14,4,'日常用品','透明胶',30,1 union +select 15,5,'体育用品','羽毛球',20,3 + +select * from orderltem + + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + +select s1.orderId 订单编号,orderDate 订单日期,itemType 产品类型,itemName 产品名称,theNumber 订购数量,theMoney 订购单价 from orders s1 inner join orderltem s2 on s1.orderId=s2.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 + +select s1.orderId 订单编号,orderDate 订单日期,itemType 产品类型,itemName 产品名称 from orders s1 inner join orderltem s2 on s1.orderId=s2.orderId where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select s1.orderId 订单编号,orderDate 订单日期,itemType 产品类型,itemName 产品名称,theNumber 订单数量,theMoney 订单单价,sum(theNumber* theMoney) 总价 from orders s1 inner join orderltem s2 on s1.orderId=s2.orderId group by s1.orderId,orderDate,itemType,itemName,theNumber,theMoney + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select s1.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订单数量,theMoney 订单单价,sum(theNumber*theMoney) 总价 from orders s1 inner join orderltem s2 on s1.orderId=s2.orderId group by s1.orderId,orderDate,itemType,itemName,theNumber,theMoney having theMoney>=5 and theNumber>=50 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 + +select s1.orderId,sum(theNumber) 订购总数 from orderltem s1 inner join orders s2 on s1.orderId=s2.orderId group by s1.orderId + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +select s1.orderId 订单编号,itemName 产品类别,count(itemType) 订购次数,sum(theNumber) 订购总数量 from orders s1 inner join orderltem s2 on s1.orderId=s2.orderId group by s1.orderId,itemName order by s1.orderId + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..707af6dbadb895fcc276e03a7ba2011a1c4f49d4 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" @@ -0,0 +1,133 @@ +create database Student2 + +on +( + name='Student2', + filename='D:\Student2.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Student2_log', + filename='D:\Student2_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +use student2 +go + +create table StuInfo +( + sTUNO varchar(15), + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(50), + stuSeat int, + stuSex nvarchar(1) default('男') check(stuSex='男' or stuSex='女') +) + +insert into Stuinfo values ('2501','张秋利','20','美国硅谷','1','女') +insert into Stuinfo values ('2502','李斯文','18','湖北武汉','2','女') +insert into Stuinfo values ('2503','马文才','22','湖南长沙','3','男') +insert into Stuinfo values ('2504','欧阳俊雄','21','湖北武汉','4','男') +insert into Stuinfo values ('2505','梅超风','20','湖北武汉','5','女') +insert into Stuinfo values ('2506','陈旋风','19','美国硅谷','6','男') +insert into Stuinfo values ('2507','陈风','20','美国硅谷','7','男') + +create table Stuexam2 +( + examNO int, + stuNO varchar(15), + writtenExam varchar(200), + labExam varchar(200) +) + +insert into Stuexam2 values ('1','2501','50','70') +insert into Stuexam2 values ('2','2502','60','65') +insert into Stuexam2 values ('3','2503','86','85') +insert into Stuexam2 values ('4','2504','40','80') +insert into Stuexam2 values ('5','2505','70','90') +insert into Stuexam2 values ('6','2506','85','90') + +select sTUNO as 学号 , stuName as 姓名 , stuAge as 年龄 , stuAddress as 地址 , stuSeat as 座号, stuSex as 性别 from Stuinfo +select sTUNO 学号,stuName as 姓名 , stuAge as 年龄 , stuAddress as 地址 , stuSeat as 座号, stuSex as 性别 from Stuinfo +select 学号=sTUNO , 姓名=stuName , 年龄=stuAge , 地址=stuAddress , 座号= stuSeat, 性别= stuSex from Stuinfo + +select stuName , stuAge , stuAddress from Stuinfo + +select examNO as 考号 , stuNO as 学号 , writtenExam as 笔试成绩 , labExam as 机试成绩 from Stuexam2 +select examNO 考号,stuNO as 学号 , writtenExam as 笔试成绩 , labExam as 机试成绩 from Stuexam2 +select 考号=examNO , 学号=stuNO , 笔试成绩=writtenExam , 机试成绩= labExam from Stuexam2 + +select stuName+stuAddress+'@'+stuAddress as 邮箱 from Stuinfo + +select stuNO as 学号,writtenExam as 笔试 ,labExam as 机试, writtenExam+labExam as 总分 from Stuexam2 + +select stuName ,stuAddress from Stuinfo + +select stuAge as 所有年龄 from Stuinfo + +select top 3 * from Stuinfo + +select top 4 * from Stuinfo + +select top 50 percent * from Stuinfo + +select * from Stuinfo where stuAddress = '湖北武汉' and stuAge = 20 + +select * from Stuexam2 where labExam >=60 and labExam <= 80 order by labExam DESC + +select * from Stuinfo where stuAddress = '湖北武汉' or stuAddress='湖南长沙' + +select * from Stuexam2 where writtenExam >=70 and labExam <= 90 order by labExam ASC + +select * from Stuinfo where stuAge is null + +select * from Stuinfo where stuAge is not null + +select * from Stuinfo where stuName like '张%' + +select * from Stuinfo where stuName like '%胡%' + +select * from Stuinfo where stuName like '__俊%' + +select * from Stuinfo order by stuAge DESC + +select * from Stuinfo order by stuAge DESC , stuSeat ASC + +select top 1 * from Stuexam2 order by writtenExam desc + +select top 1 * from Stuexam2 order by writtenExam asc + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 + +select stuName 姓名,stuAge 年龄,writtenExam 笔试, labExam 机试 from StuInfo inner join Stuexam2 on StuInfo.sTUNO = Stuexam2.stuNO + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 + +select StuInfo.sTUNO 学号,stuName 姓名 , writtenExam 笔试, labExam 机试 from StuInfo inner join Stuexam2 on StuInfo.sTUNO = Stuexam2.stuNO where labExam>60 and writtenExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 + +select StuInfo.sTUNO 学号,stuName 姓名,writtenExam 笔试,labExam 机试 from StuInfo left join Stuexam2 on StuInfo.sTUNO =Stuexam2.stuNO + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 + +select stuName 姓名, stuAge 年龄, writtenExam 笔试 , labExam 机试 from StuInfo inner join Stuexam2 on StuInfo.sTUNO = Stuexam2.stuNO where stuAge>=20 + +--5.查询男女生的机试平均分 + +select stuSex 性别 , AVG(labExam) 平均分 from Stuexam2 inner join StuInfo on Stuexam2.stuNO=StuInfo.sTUNO group by stuSex + +--6.查询男女生的笔试总分 + +select stuSex 性别 , sum(writtenExam) 总分 from Stuexam2 inner join StuInfo on Stuexam2.stuNO=StuInfo.sTUNO group by stuSex + +alter table Stuexam2 alter column labExam int + +alter table Stuexam2 alter column writtenExam int \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a9918eab8cf2a16bb66e3dec2f798af533927f9b --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery2.sql" @@ -0,0 +1,101 @@ +use master +go + +create database order1 +on +( +name='order1', +filename='D:\sql.mdf', +size=5MB, +filegrowth=10MB, +maxsize=50MB +) +log on +( +name='order1_log', +filename='D:\sql.ldf', +size=5MB, +filegrowth=10MB, +maxsize=50MB +) +go + +use order1 +go + +create table orders +( +orderID int primary key identity(1,1) not null, +orderDate datetime not null +) + +create table orderItem +( +itemID int primary key identity(1,1) not null, +orderid int not null, +itemType varchar(20) not null, +itemName varchar(20) not null, +theNumber int not null, +theMoney money not null +) + +insert into orders values ('2008-01-12 00:00.000'), +('2008-02-10 00:00.000'),('2008-02-15 00:00.000'), +('2008-03-10 00:00.000') + +insert into orderItem values(1,'文具','笔','72','2'), +(1,'文具','尺','10','1'),(1,'体育用品','篮球','1','56'), +(2,'文具','笔','36','2'),(2,'文具','固体胶','20','3'), +(2,'日常用品','透明胶','2','1'),(2,'体育用品','羽毛球','20','3'), +(3,'文具','订书机','20','3'),(3,'文具','订书针','10','3'), +(3,'文具','裁纸刀','5','5'),(4,'文具','笔','20','2'), +(4,'文具','信纸','50','1'),(4,'日常用品','毛巾','4','5'), +(4,'日常用品','透明胶','30','1'),(4,'体育用品','羽毛球','20','3') + +select sum(theNumber) from orderItem + +select orderid 订单编号,theNumber 所有物品数量,AVG(theMoney) 平均单价 from orderItem group by orderid,theNumber having orderid<3 and AVG(theMoney)<10 order by orderid asc + +select theNumber 所有物品数量,AVG(theMoney) 平均单价 from orderItem group by theNumber having theNumber>50 and AVG(theMoney)<10 + +select itemType 商品类别 , theNumber 订购次数 from orderItem group by itemType,theNumber + +select itemType 物品类别 , sum(theNumber) 总数量 ,AVG(theMoney) 平均单价 from orderItem group by itemType having sum(theNumber)>100 + +select itemName 产品名称 , count(theNumber) 订购次数, sum(theNumber) 总数量 , AVG(theMoney) 平均单价 from orderItem group by itemName + + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + +select orderItem.orderid 编号,orderDate 日期,itemType 类别 , itemName 名称, theNumber 数量,theMoney 单价 from orderItem inner join orders on orderItem.orderid=orders.orderID + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 + +select orderItem.orderid 编号, orderDate 日期,itemType 类别,itemName 名称 from orderItem inner join orders on orderItem.orderid=orders.orderID where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select orderItem.orderid 编号, orderDate 日期, itemType 类别, itemName 名称, theNumber 数量,theNumber* theMoney 总价 from orderItem inner join orders on orderItem.orderid=orders.orderID group by orderItem.orderid,orderDate,itemType,itemName,theNumber,theMoney + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select orderItem.orderid 编号,orderDate 日期,itemType 类别 , itemName 名称, theNumber 数量,theMoney 单价,theNumber*theMoney 总价 from orderItem inner join orders on orderItem.orderid=orders.orderID where theMoney>=5 and theNumber>=50 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 + +select orderid 编号,theNumber 产品数 from orderItem group by orderid,theNumber + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select orderid 编号,itemType 类别, count(theNumber) 次数 ,theNumber 总数量 from orderItem group by orderid,itemType, theNumber \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..abd6e284b1438bca247e810de56c2da08924479b --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery3.sql" @@ -0,0 +1,123 @@ +create database bbs +on +( +name='bbs', +filename='D:\sql4.mdf', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) +log on +( +name='bbs_log', +filename='D:\sql4.ldf', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) +go + +use bbs +go + +create table bbsUsers +( +UID int identity(1,1), +uName varchar(10) not null, +uSex varchar(2) not null, +uAge int not null, +uPoint int not null +) + +alter table bbsUsers add constraint PK_UID primary key(UID) +alter table bbsUsers add constraint uName unique(uName) +alter table bbsUsers add constraint uSex check(uSex='男' or uSex='女') +alter table bbsUsers add constraint uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint uPoint check(uPoint>=0) + +create table bbsTopic +( +tID int primary key identity(1,1), +tUID int foreign key references bbsUsers(UID), +tSID int foreign key references bbsSection(sID), +tTitle varchar(100) not null, +tMsg text not null, +tTime datetime , +tCount int +) + +create table bbsReply +( +rID int primary key identity(1,1), +rUID int foreign key references bbsUsers(UID), +rTID int foreign key references bbsTopic(tID), +rMsg text not null, +rTime datetime +) + +create table bbsSection +( +sID int identity(1,1), +sName varchar(10) not null, +sUid int +) + +alter table bbsSection add constraint Pk_sID primary key (sID) +alter table bbsSection add constraint CK_sUid foreign key (sUid) references bbsUsers(UID) + +insert into bbsUsers values('小雨点','女','20','0') +insert into bbsUsers values('逍遥','男','18','4') +insert into bbsUsers values('七年级生','男','19','2') + +select uName,uSex from bbsUsers + +select uName,uPoint into bbsPoint from bbsUsers + +select * from bbsSection + +insert into bbsSection (sName,sUid) +values('技术交流',1), + ('读书世界',3), + ('生活百科',1), + ('八卦区',3) + +insert into bbsTopic( tUID,tSID,tTitle,tMsg,tTime,tCount ) +values (2,4,'范跑跑','谁是范跑跑','2008-7-8','1'),(3,1,'.NET','与JAVA的区别是什么','2008-9-1','2'), +(1,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10','0') + +insert into bbsReply(rMsg,rTime,rUID) values +('他是范跑跑','2008-7-9',1), +('不知道','2008-9-2',2), +('不知道','2008-9-11',3) + +alter table bbsReply drop constraint FK__bbsReply__rUID__30F848ED +alter table bbsTopic drop constraint FK__bbsTopic__tUID__2D27B809 +delete bbsUsers where uName='逍遥' + +update bbsUsers set upoint =12 where uName='小雨点' + +alter table bbsTopic drop constraint FK__bbsTopic__tSID__2E1BDC42 +delete bbsSection where sName = '生活百科' + +delete bbsReply + + +--1.查询出每个版块的版主编号,版主姓名和版块名称 + +select UID 版主编号,uName 姓名,sName 名称 from bbsSection inner join bbsUsers on bbsSection.sUid=bbsUsers.UID + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + +select tUID 版主编号,uName 姓名,tTitle 标题,tMsg 内容,tTime 时间 from bbsTopic inner join bbsUsers on bbsTopic.tUID=bbsUsers.UID + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 + +select tID 编号,sName 名称,uName 姓名 from bbsTopic inner join bbsSection on sID=tID inner join bbsUsers on bbsTopic.tUID=bbsUsers.UID where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 + +select top 1 tID 发帖人编号,uName 姓名,tTitle 标题,tMsg 内容,tCount 数量 from bbsTopic inner join bbsUsers on bbsTopic.tUID=bbsUsers.UID order by tCount DESC + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select tSID 版块,tUID 用户,count(tSID) 发帖总数 from bbsTopic inner join bbsSection on sID=tID inner join bbsUsers on bbsTopic.tUID=bbsUsers.UID group by tSID,tUID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..de27b0f0e953b87757b9d028c767a5280330141c --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" @@ -0,0 +1,120 @@ +use master +go +create database JXC +on +( name='JXC', + filename='D:\JXC.mdf', + size=20, + maxsize=100, + filegrowth=10% + ) + log on + ( name='JXC_log', + filename='D:\JXC_log.ldf', + size=20, + maxsize=100, + filegrowth=10% + ) + go + use JXC + go + create table orders + ( orderId int primary key identity(1,1), + orderDate datetime, + ) + insert into orders(orderDate) values('2008-01-12 ') + insert into orders(orderDate) values('2008-02-10 ') + insert into orders(orderDate) values('2008-02-15 ') + insert into orders(orderDate) values('2008-03-10 ') + + + create table orderItem + ( ItemiD int primary key identity, + orderId int , + itemType nvarchar(20), + itemName nvarchar(10), + theNumber int, + theMoney money + ) + + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (1,'文具','笔','72' ,'2' ) + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (1,'文具','尺','10' ,'1' ) + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (1,'体育用品','篮球','1','56' ) + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (2,'文具','笔','36','2') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (2,'文具','固体胶','20','3') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (2,'日常用品','透明胶','36','2') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (2,'体育用品','羽毛球','20','3') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (3,'文具','订书机','20','3') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (3,'文具','订书针','10','3') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (3,'文具','裁纸刀','5','5') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (4,'文具','笔','20','2') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (4,'文具','信纸','50','1') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (4,'日常用品','毛巾','4','5') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (4,'日常用品','透明胶','30','1') + insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values (4,'体育用品','羽毛球','20','3' ) + + select *from orders + select *from orderItem + +--1.查询所有的订单的订单编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + +select orderItem.orderId 订单编号 ,orderDate 订单日期, itemType 产品类别 , itemname 产品名称, theNumber 订购数量 ,theMoney 订购单价 from orderItem inner join orders on orderItem.orderId =orders.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 + +select orderItem.orderId 订单编号,orderDate 订单日期, itemType 产品类别 ,itemname 产品名称 , theNumber 订购数量 from orderItem inner join orders on orderItem.orderId =orders.orderId where theNumber>=50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select orderItem.orderId 订单编号,orderDate 订单日期, itemType 产品类别 ,itemname 产品名称 , theNumber 订购数量,theMoney 订购单价,(theNumber)*(theMoney) 订购总价 + +from orderItem inner join orders on orderItem.orderId =orders.orderId group by orderItem.orderId ,orderDate ,itemType ,theNumber ,theMoney,itemname + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select orderItem.orderId 订单编号,orderDate 订单日期, itemType 产品类别 ,itemname 产品名称 , theNumber 订购数量,theMoney 订购单价,(theNumber)*(theMoney) 订购总价 + + from orderItem inner join orders on orderItem.orderId =orders.orderId where theMoney>=5 and theNumber>=50 group by orderItem.orderId ,orderDate ,itemType ,theNumber ,theMoney,itemname + + --5.查询每个订单分别订购了几个产品,例如:编号 订购产品数 + -- 1 3 + -- 2 4 + +select orderId 订单编号,count(itemType) 产品数目 from orderItem group by orderId + + + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +-- 订单编号 产品类别 订购次数 总数量 +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + + +select orderId 订单编号,itemType 产品类别 ,count( itemname) 订购次数 ,sum(theNumber) 订购数量 +from orderItem group by orderId,itemType + + + + + + + + + + + + + + + + + + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery11.sql" "b/20210326\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery11.sql" new file mode 100644 index 0000000000000000000000000000000000000000..39dbe93bb1956116933ac8aefb8b1857118151be --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery11.sql" @@ -0,0 +1,112 @@ + +use master +go +create database L1 +on +( name='L1', + filename='D:\L1.mdf', + size=10, + maxsize=100, + filegrowth=10% + ) + log on + ( name='L1_log', + filename='D:\L1_log.ldf', + size=10, + maxsize=100, + filegrowth=10% + ) + go + use L1 + go + create table bbsUsers + (UID int identity(1,1), + uName varchar(10) not null, + uSex varchar(2) not null , + uAge int not null , + uPoint int not null , + ) + alter table bbsUsers add constraint PK_bbsUsers_UID primary key(UID) + alter table bbsUsers add constraint UK_bbsUsers_uName unique(uName) + alter table bbsUsers add constraint DK_bbsUsers_uSex check(uSex='男'or uSex='女') + alter table bbsUsers add constraint DK_bbsUsers_uAge check(uAge>=15 and uAge<=60) + alter table bbsUsers add constraint DK_bbsUsers_uPoint check(uPoint>=0) + + create table bbsSection + (sID int identity, + sName varchar(10) not null, + sUid int , + ) + alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) + alter table bbsSection add constraint FK_bbsSection_sUid foreign key (sUid) references bbsUsers (UID) + + + create table bbsTopic +( tID int primary key identity, + tUID int foreign key references bbsUsers (UID), + tSID int references bbsSection(sID) , + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime , + tCount int + ) +create table bbsReply +( rID int primary key identity, + rUID int foreign key references bbsUsers (UID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime + ) + select * from bbsSection + insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) + select uName,uPoint into bbsPoint from bbsUsers + + insert into bbsSection values (' 技术交流',1),('读书世界' ,3),(' 生活百科',1),('八卦区' ,3) + insert into bbsTopic values(2,4,'范跑跑!','谁是范跑跑!', '2008-7-8',1),(3,2,'.NET!','与JAVA的区别是什么呀?','2008-9-1',2),(1,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10',0) + + insert into bbsReply values(3,1,'谁问的谁就是范跑跑','20210316'), + (1,2,'这个更简单','20210316'), + (1,3,'今年夏天最流行内裤外穿','20210316') + + +select * from bbsSection +select * from bbsReply +select * from bbsTopic +select * from bbsUsers + + +--1.查询出每个版块的版主编号,版主姓名和版块名称 + +select sid 版主编号, sName 板块名称 , uName 版主姓名 from bbsSection left join bbsUsers on bbsSection.sID=bbsUsers.UID + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + +select bbsUsers.uName 版主姓名,bbsTopic.tTitle 贴标 ,bbsTopic.tMsg 帖内,bbsTopic.ttime 发帖时间 from bbsTopic right join bbsUsers on bbsTopic.tUID=bbsUsers.UID where + +bbsTopic.ttime >'2008-9-15' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 + +select sid 版主编号, sName 板块名称 , uName 版主姓名 from bbsSection left join bbsUsers on bbsSection.sID=bbsUsers.UID where bbsUsers.uAge <20 + + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 + +--select bbsUsers.uName 姓名 , bbsTopic.tTitle 贴标,bbsSection.sID 版主编号,bbsTopic.tMsg 帖内,bbsTopic.tCount from bbsTopic left join bbsUsers left join bbsSection on bbsUsers.UID = bbsSection.sUid + +--注:不会 + + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select tuid ,sum(tcount) from bbsTopic group by tuid + + + + + + + + + + diff --git "a/20210326\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0d9122bb665be79d26f729d81861ba0aca9d2a1e --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery2.sql" @@ -0,0 +1,110 @@ +create database Zmt + +on +( +name='Zmt', +filename='D:\Zmt.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) + +log on + +( +name='Zmt_log', +filename='D:\Zmt_log.ldf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) + +use Zmt + +create table Jxc +( +stuNo char(10) primary key, + +stuName char(10) not null, + +stuAge int not null , + +stuAddress char(10) not null , + +stuSeat int identity , + +steSex int check(steSex=1 or steSex=0), + +) + +create table J + +( + +esamNo int identity primary key , + +stuNO char(10) references Jxc(stuNo), + +writtenExam int not null, + +labExam int not null , + +) + + +insert into Jxc(stuNO,stuName,stuAge,stuAddress,steSex) values + +( 's2501','张秋利',20,'美国硅谷',1 ), + +('s2502','李思文',18,'湖北武汉',0), + +( 's2503','马文才',22,'湖南长沙',1 ), + +( 's2504','欧阳俊雄',21,'湖北武汉',0 ), + +( 's2505','梅超风',20,'湖北武汉',1 ), + +( 's2506','陈旋风',19,'美国硅谷',1 ), + +( 's2507','陈风',20,'美国硅谷',0) + +insert into J(stuNO,writtenExam,labExam) + +select 's2501',50,70 union + +select 's2502',60,65 union + +select 's2503',86,85 union + +select 's2504',40,80 union + +select 's2505',70,90 union + +select 's2506',85,90 + + +--数据如图片1,使用上次作业的数据 + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 + +select stuName 学生名字, stuAge 学生年龄, labExam 机试成绩, writtenExam 笔试成绩 from JXC left join j on Jxc.stuNo =J.stuNO + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 + +select j.stuNo 学号 ,stuName 学生名字 , labExam 机试成绩 ,writtenExam 笔试成绩 from Jxc,J where labExam>60 and writtenExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 + +select Jxc.stuNO 学号,stuName 学生名字 , labExam 机试成绩 ,writtenExam 笔试成绩 from jxc left join j on Jxc.stuNo =J.stuNO + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 + +select stuName 学生名字 ,stuAge 学生年龄, labExam 机试成绩 ,writtenExam 笔试成绩 from jxc left join j on Jxc.stuNo =J.stuNO where stuAge>=20 order by writtenExam desc + +--5.查询男女生的机试平均分 + +select (steSex) 性别 , avg (labExam) 机试平均成绩 from Jxc left join J on Jxc.stuNo =J.stuNO group by steSEX + +--6.查询男女生的笔试总分 + +select (steSex) 性别 , sum (labExam) 机试平总成绩 from Jxc left join j on Jxc.stuNo =J.stuNO group by steSEX diff --git "a/20210326\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3dde0a62839795484070dfaf766de8954a682171 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" @@ -0,0 +1,84 @@ +create database AKA +on +( + name='D:\AKA', + filename='D:\AKA.mdf', + size=5, + maxsize=50, + filegrowth=5% +) +log +on +( + name='D:\AKA_log', + filename='D:\AKA_log.ldf', + size=5, + maxsize=50, + filegrowth=5% +) +use AKA + + +create table stu +( + stuNo varchar(10) not null primary key, + stuName varchar(10) not null, + stuAge int not null, + stuAddress varchar(10) not null, + stuSeat int identity not null, + stuSex varchar(2) not null +) +create table cjb +( + examNO int identity not null, + stuNo varchar(10) foreign key references stu(stuNo) not null, + writtenExam int not null, + labExam int not null +) +insert into stu(stuNo,stuName,stuAge,stuAddress,stuSex)values +('s2501','张秋利',20,'美国硅谷','男'), +('s2502','李斯文',18,'湖北武汉','女'), +('s2503','马文才',22,'湖南长沙','男'), +('s2504','欧阳俊雄',21,'湖北武汉','女'), +('s2505','梅超风',20,'湖北武汉','男'), +('s2506','陈旋风',19,'美国硅谷','男'), +('s2507','陈风',20,'美国硅谷','女') + +insert into cjb(stuNo,writtenExam,labExam)values +('s2501',50,70), +('s2502',60,65), +('s2503',86,85), +('s2504',40,80), +('s2505',70,90), +('s2506',85,90) + + +select * from stu + +select * from cjb + +--数据如图片1,使用上次作业的数据 + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 + +select stuName,stuAge,writtenExam,labExam from stu left join cjb on stu.stuNo=cjb.stuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 + +select stuName,writtenExam,labExam from stu left join cjb on stu.stuNo=cjb.stuNo where writtenExam>60 and labExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 + +select stu.stuNo,stuName,writtenExam,labExam from stu left join cjb on stu.stuNo=cjb.stuNo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 + +select stuName,stuAge,writtenExam,labExam from stu left join cjb on stu.stuNo=cjb.stuNo where stuAge>=20 order by writtenExam desc + +--5.查询男女生的机试平均分 + +select stuSex 性别,avg(labExam)机试平均分 from stu inner join cjb on stu.stuNo=cjb.stuNo group by stuSex + +--6.查询男女生的笔试总分 + +select stuSex 男女,sum(writtenExam)笔试总分 from stu inner join cjb on stu.stuNo=cjb.stuNo group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cea7fa22a1ed9f47ce7aa7b42971bd4d7ba9e744 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery2.sql" @@ -0,0 +1,97 @@ +create database ASA +on( + name='D:\ASA', + filename='D:\ASA.mdf', + size=5, + maxsize=50, + filegrowth=15% +) +log on +( + name='D:\ASA_log', + filename='D:\ASA_log.ldf', + size=5, + maxsize=50, + filegrowth=15% +) +use ASA + +create table orders +( + orderld int not null identity(1,1) primary key , + orderDate datetime not null +) +create table orderltem +( + ltemID int identity not null, + orderld int foreign key references orders(orderld), + itemType varchar(10) not null, + itemName varchar(10) not null, + theNumber int not null, + theMoney int not null, +) +insert into orders values +('2008-01-12 00:00:00.000'), +('2008-02-10 00:00:00.000'), +('2008-02-15 00:00:00.000'), +('2008-03-10 00:00:00.000') +insert into orderltem values +(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'日常用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3), +(3,'文具','订书机',20,3), +(3,'文具','订书针',10,3), +(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'日常用品','毛巾',4,5), +(4,'日常用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) + + + +select * from orders +select * from orderltem + +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + +select orders.orderld,orderDate,itemType,itemName,theNumber,theMoney from orders left join orderltem on orders.orderld=orderltem.orderld + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 + +select orders.orderld,orderDate,itemType,itemName from orders inner join orderltem on orders.orderld=orderltem.orderld where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select orders.orderld,orderDate,itemType,itemName,theNumber,theMoney,theNumber*theMoney 订购总价 from orders left join orderltem on orders.orderld=orderltem.orderld + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select orders.orderld,orderDate,itemType,itemName,theNumber,theMoney,theNumber*theMoney 订购总价 from orders left join orderltem on orders.orderld=orderltem.orderld where theMoney>5 and theNumber>50 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select * from orders +select * from orderltem +select orderld,count(theNumber)产品数 from orderltem group by orderld + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + + +select orderld,itemType,count(*)次数,sum(theNumber)总数量 from orderltem group by orderld,itemType order by orderld diff --git "a/20210326\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..abb475fa241b94173034c87285a63e24a72bd478 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery3.sql" @@ -0,0 +1,101 @@ +create database bbs + +on +( + name='bbs', + filename='D:\bbs.mdf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +go +use bbs +go +create table bbsUsers +( + UIDD int identity, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +create table bbsSection +( + sIDD int identity, + sName varchar(10) not null, + sUid int +) + +alter table bbsUsers add constraint Pk_bbsUsers_UIDD primary key(UIDD) +alter table bbsUsers add constraint Uk_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint Ck_bbsUsers_uSex check(uSex='男'or uSex='女') +alter table bbsUsers add constraint Ck_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint Ck_bbsUsers_uPoint check(uPoint>=0) + +alter table bbsSection add constraint Pk_bbsSection_sIDD primary key(sIDD) +alter table bbsSection add constraint Fk_bbsSection_sUid foreign key(sUid) references bbsUsers(UIDD) + +create table bbsTopic +( + tID int identity primary key, + tUID int foreign key references bbsUsers(UIDD), + tSID int foreign key references bbsSection(sIDD), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +create table bbsReply +( + rID int identity primary key, + rUID int foreign key references bbsUsers(UIDD), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select uName,uPoint into bbsPoint from bbsUsers +insert into bbsSection values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) +insert into bbsTopic values(2,4,'范跑跑 ',' 谁是范跑跑 ','2008-7-8','1'),(3,1,'.NET ',' 与JAVA的区别是什么呀? ','2008-9-1 ','2'), +(1,3,'今年夏天最流行什么 ',' 有谁知道今年夏天最流行 ','2008-9-10','0') + +insert into bbsReply values(3,2,'八嘎','2008-7-8'),(1,1,'python','2008-7-8'),(1,3,'上海','2008-7-8') + + +select * from bbsUsers 用户信息表 +select * from bbsSection 版块表 +select * from bbsTopic 主帖表 +select * from bbsReply 回帖表 + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 + +select sIDD,sName,uName from bbsSection left join bbsUsers on sIDD=uIDD + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 + +select tUID,uName,tTitle,tMsg,tTime from bbsTopic left join bbsUsers on tID=UIdd where tTime>'2008-09-15' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 + +select sIDD,uName,sName from bbsSection left join bbsUsers on sIdd=UIDD where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 + +select top 1 tUID,uName,tTitle,tMsg,tCount from bbsUsers t left join bbsTopic x on t.UIDD=x.tID order by tCount desc + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select uName 用户名,sName 版块名称,count(*)发帖总数 from bbsTopic + inner join bbsUsers on bbsTopic.tUID = bbsUsers.UIDD + inner join bbsSection on bbsUsers.UIDD= bbsSection.sUid group by uName,sName + diff --git "a/20210326\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b823254ddf15c59e9bb79ed1ae73815301935451 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" @@ -0,0 +1,47 @@ +create database Student +on +( + name='Student', + filename='D:\Student.mdf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='Student_log', + filename='D:\Student_log.ldf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +go +use Student +go +create table stuinfo +( + stuNO nvarchar(10) unique not null, + stuName nvarchar(10) not null, + stuAge int not null, + stuAddress nvarchar(200) , + stuSeat nvarchar(10) not null, + stuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女') +) +create table stuexam +( + examNO int primary key identity(1,1), + stuno nvarchar(10) , + writtenexam int not null, + labexam int not null +) +insert into stuinfo(stuNO,stuName,stuAge,stuAddress,stuSeat,stuSex) +values('s2501','张秋利','20','美国硅谷','1','男'),('s2502','李斯文','18','湖北武汉','2','女'),('s2503','马文才','22','湖南长沙','3','男'), +('s2504','欧阳俊雄','21','湖北武汉','4','女'),('s2505','梅超风','20','湖北武汉','5','男'),('s2506','陈旋风','19','美国硅谷','6','男'),('s2507','陈风','20','美国硅谷','7','女') +insert into stuexam(stuNO,writtenexam,labexam) values ('s2501','50','70'),('s2502','60','65'),('s2503','86','85'),('s2504','40','80'),('s2505','70','90'),('s2506','85','90') + +select stuName 姓名,stuAge 年龄,writtenexam 笔试,labexam 机试 from stuinfo t left join stuexam x on t.stuNo=x.stuNo +select stuName 姓名,stuAge 年龄,writtenexam 笔试,labexam 机试 from stuinfo t left join stuexam x on t.stuNo=x.stuNo where writtenexam>60 and labexam>60 +select stuName 姓名,stuAge 年龄,writtenexam 笔试,labexam 机试 from stuinfo t left join stuexam x on t.stuNo=x.stuNo +select stuName 姓名,stuAge 年龄,writtenexam 笔试,labexam 机试 from stuinfo t left join stuexam x on t.stuNo=x.stuNo where stuAge>=20 order by writtenexam desc +select count(stuSex) 性别,avg(labexam) 机试平均分 from stuinfo t left join stuexam x on t.stuNo=x.stuNo group by stuSex +select count(stuSex) 性别,sum(writtenexam) 笔试总分 from stuinfo t left join stuexam x on t.stuNo=x.stuNo group by stuSex diff --git "a/20210326\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fb087b2b71fde8e1c57b415b5fa5b96a3983ce16 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" @@ -0,0 +1,46 @@ +create database tx +on +( + name='tx', + size=10, + filename='D:\tx.mdf', + maxsize=100, + filegrowth=10 +) +log on +( + name='tx_log', + size=10, + filename='D:\tx_log.ldf', + maxsize=100, + filegrowth=10 +) +go +use tx +go +create table orders +( + orderId int primary key identity not null, + orderDate datetime default(getdate()) +) +create table orderItem +( + ItemiD int primary key identity not null, + orderId int, + itemType nvarchar(20), + itemName nvarchar(20), + theNumber int, + theMoney money +) +insert into orderItem values (1,'文具','笔',72,2),(1,'文具','尺',10,1),(1,'体育用品','篮球',1,56),(2,'文具','笔',36,2),(2,'文具','固体胶',20,3), +(2,'日常用品','透明胶',2,1),(2,'体育用品','羽毛球',20,3),(3,'文具','订书机',20,3),(3,'文具','订书钉',10,3),(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2),(4,'文具','信纸',50,1),(4,'日常用品','毛巾',4,5),(4,'日常用品','透明胶',30,1),(4,'体育用品','羽毛球',20,3) +insert into orders (orderDate) values ('2008-01-12 00:00:00.000'),('2008-02-10 00:00:00.000'),('2008-02-15 00:00:00.000'),('2008-03-10 00:00:00.000') +select * from orders +select * from orderItem +select t.orderId 订单的编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购的产品名称,theNumber 订购数量,theMoney 订购单价 from orderItem t left join orders x on t.orderId=x.orderId +select t.orderId 订单的编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购的产品名称 from orderItem t left join orders x on t.orderId=x.orderId where theNumber>50 +select t.orderId 订单的编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购的产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 from orderItem t left join orders x on t.orderId=x.orderId +select t.orderId 订单的编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购的产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 from orderItem t left join orders x on t.orderId=x.orderId where theMoney>=5 and theNumber>=50 +select t.orderId 订单的编号,count(itemType) 订购产品数 from orderItem t left join orders x on t.orderId=x.orderId group by t.orderId +select t.orderId 订单的编号,itemType 订购产品数,count(itemType) 订购次数,sum(theNumber)总数量 from orderItem t left join orders x on t.orderId=x.orderId group by t.orderId,itemType order by t.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3fdf3863fff7b37903b5eee7b6504893e5517e48 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery3.sql" @@ -0,0 +1,75 @@ +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +go +use bbs +go +create table bbsUsers +( + UIDD int identity, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +create table bbsSection +( + sIDD int identity, + sName varchar(10) not null, + sUid int +) + +alter table bbsUsers add constraint Pk_bbsUsers_UIDD primary key(UIDD) +alter table bbsUsers add constraint Uk_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint Ck_bbsUsers_uSex check(uSex='男'or uSex='女') +alter table bbsUsers add constraint Ck_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint Ck_bbsUsers_uPoint check(uPoint>=0) + +alter table bbsSection add constraint Pk_bbsSection_sIDD primary key(sIDD) +alter table bbsSection add constraint Fk_bbsSection_sUid foreign key(sUid) references bbsUsers(UIDD) + +create table bbsTopic +( + tID int identity primary key, + tUID int foreign key references bbsUsers(UIDD), + tSID int foreign key references bbsSection(sIDD), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +create table bbsReply +( + rID int identity primary key, + rUID int foreign key references bbsUsers(UIDD), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select uName,uPoint into bbsPoint from bbsUsers +insert into bbsSection values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) +insert into bbsTopic values(2,4,'范跑跑 ',' 谁是范跑跑 ','2008-7-8','1'),(3,1,'.NET ',' 与JAVA的区别是什么呀? ','2008-9-1 ','2'), +(1,3,'今年夏天最流行什么 ',' 有谁知道今年夏天最流行 ','2008-9-10','0') +insert into bbsReply values(3,2,'不就是你嘛','2008-7-8'),(1,1,'面对对象','2008-7-8'),(1,3,'拖鞋','2008-7-8') + +select sIDD,uName,sName from bbsSection t left join bbsUsers x on t.sIDD=x.UIDD +select tUID,uName,tTitle,tMsg,tTime from bbsTopic t left join bbsUsers x on t.tID=x.UIDD where tTime > '2008-09-15' +select sIDD,uName,sName from bbsSection t left join bbsUsers x on t.sIDD=x.UIDD where uAge<20 +select top 1 tUID,uName,tTitle,tMsg,tCount from bbsUsers t left join bbsTopic x on t.UIDD=x.tID order by tCount desc +select sName,uName,count(tTitle)发帖总数 from bbsTopic t left join bbsSection x on t.tID=x.sIDD left join bbsUsers a on x.sIDD=a.UIDD group by uName,sName \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..635c5d0cc9fe4de5b8352a9bee0909c368bf3aa1 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" @@ -0,0 +1,71 @@ +use master +go +create database student +on +( +name='str', +size=5, +filename='D:\str.mdf', +filegrowth=1, +maxsize=50 +) +log on +( +name='str_log', +size=5, +filename='D:\str_log.ldf', +filegrowth=1, +maxsize=50 +) +use Student +go + +create table StuInfo +( + stuNo char(5) primary key(stuNo), + stuName nvarchar(20), + stuAge int, + stuAddress text, + stuSeat int identity(1,1), + stuSex char(1) check(stuSex in(1,0)) +) +go +create table StuExam +( + examNo int identity(1,1), + stuNo char(5), + writtenExam int check(writtenExam>=0 and writtenExam<=100), + labExam int check(labExam>=0 and labExam<=100) +) + go + alter table StuExam add constraint RK_StuExam_stuNo foreign key(stuNo) references StuInfo(stuNo) + + insert into StuInfo values + ('s2501','张秋利',20,'美国硅谷',1), + ('s2502','李斯文',18,'湖北武汉',0), + ('s2503','马文才',22,'湖南长沙',1), + ('s2504','欧阳俊雄',21,'湖北武汉',0), + ('s2505','梅超风',20,'湖北武汉',1), + ('s2506','陈旋风',19,'美国硅谷',1), + ('s2507','陈风',20,'美国硅谷',0) + + delete from StuExam + + insert into StuExam values + ('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90),('s2507',50,40) + --数据如图片1,使用上次作业的数据 + select * from StuInfo inner join StuExam on StuExam.stuNo=StuInfo.stuNo +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select * from StuInfo +select * from StuExam +select StuExam.labExam,stuName,stuAge,writtenExam from StuInfo inner join StuExam on StuExam.stuNo=StuInfo.stuNo +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuExam.stuNo,labExam,stuName,writtenExam from StuExam inner join StuInfo on StuExam.stuNo=StuInfo.stuNo WHERE StuExam.labExam>60 and writtenExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select StuExam.stuNo,labExam,stuName,writtenExam from StuExam right join StuInfo on StuExam.stuNo=StuInfo.stuNo +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select StuInfo.stuAge,labExam,stuName,writtenExam from StuExam inner join StuInfo on StuExam.stuNo=StuInfo.stuNo where stuAge>=20 order by labExam +--5.查询男女生的机试平均分 +select stuSex,avg(labExam) from StuInfo inner join StuExam on StuExam.stuNo=StuInfo.stuNo group by stuSex +--6.查询男女生的笔试总分 +select stuSex,avg(writtenExam) from StuInfo inner join StuExam on StuExam.stuNo=StuInfo.stuNo group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2e4d07752a15efea8f262ca4d84b1f9f450f07d8 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" @@ -0,0 +1,103 @@ +use master +go +create database Aa +on +( + name='Aa', + filename='E:\Aa.mdf' +) +log on +( + name='Aa_log', + filename='E:\Aa_log.ldf' +) +go + +use Aa +go +--drop table orderItem +create table orders +( + orderId int primary key, + orderDate date +) +go +create table orderItem +( + ItemiD int identity(1,1) primary key, + orderId int references orders(orderId), + itemType varchar(12), + itemName varchar(12), + theNumber int, + theMoney money +) +go +insert into orders values + (1,'2008-01-12'), + (2,'2008-02-10'), + (3,'2008-02-15'), + (4,'2008-03-10') +insert into orderItem values + (1,'文具','笔',72,2), + (1,'文具','尺',10,1), + (1,'体育用品','篮球',1,56), + (2,'文具','笔',36,2), + (2,'文具','固体胶',20,3), + (2,'日常用品','透明胶',2,1), + (2,'体育用品','羽毛球',20,3), + (3,'文具','订书机',20,3), + (3,'文具','订书针',10,3), + (3,'文具','裁纸刀',5,5), + (4,'文具','笔',20,2), + (4,'文具','信纸',50,1), + (4,'日常用品','毛巾',4,5), + (4,'日常用品','透明胶',30,1), + (4,'体育用品','羽毛球',20,3) +--1.查询所有订单订购的所有物品数量总和 +select sum(theNumber) '订购物品总量' from orderItem +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select orderId,sum(theNumber) '订购物品总量',avg(theMoney) '平均单价' from orderItem group by orderId +having orderId<3 and avg(theMoney)<10 +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select orderId,sum(theNumber) '订购物品总量',avg(theMoney) '平均单价' from orderItem group by orderId +having avg(theMoney)>10 and sum(theNumber)>50 +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select itemType '类别',count(*) '分别订购了几次' from orderItem group by itemType +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select itemType '类别',sum(theNumber) '订购物品总量',avg(theMoney) '平均单价' from orderItem +group by itemType having sum(theNumber)>50 +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: +select itemName '产品名称',count(*) '订购次数',sum(theNumber) '订购物品总量',avg(theMoney) '平均单价' from orderItem +group by itemName +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select * from orders +select * from orderItem +select orders.orderId,orderDate,itemType,itemName from orderItem inner join orders on orderItem.orderId=orders.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orderItem.orderId,orderDate,itemType,itemName from orders inner join orderItem on orderItem.orderId=orders.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId,orderDate,itemType,itemName,theMoney,theMoney*theNumber from orders inner join orderItem on orders.orderId=orderItem.orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.theMoney,theNumber from orders inner join orderItem on orders.orderId=orderItem.orderId where theMoney>=5 and theNumber>=50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderId,sum(theNumber) from orderItem group by orderId +--6.查询每个订单里 的每个类别 的产品分别订购了几次 和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select orderItem.orderId,itemType,count(theNumber) from orderItem inner join orders on orderItem.orderId=orders.orderId order by orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0b72f247d94b0d8a4712e1f7efa6ba48060675a6 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery3.sql" @@ -0,0 +1,118 @@ +use master +go +create database bbs1 +on +( name='bbs1', + filename='D:\bbs1.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='bbs1_log', + filename='D:\bbs1_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +go +use bbs1 +go +create table bbsUsers +( UID int identity(1,1), + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +select*from bbsUsers +alter table bbsUsers add constraint Pk_bbsUsers_UID primary key(UID) +alter table bbsUsers add constraint Uk_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint Ck_bbsUsers_uSex check(uSex in('男','女')) +alter table bbsUsers add constraint Ck_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint Ck_bbsUsers_uSex check(uPoint>=0) + +create table bbsSection +( sID int identity(1,1), + sName varchar(10) not null, + sUid int +) + +insert into bbsSection values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) +alter table bbsSection add constraint Pk_bbsSection_sID primary key(sID) +alter table bbsSection add constraint Fk_bbsSection_sUid foreign key(sUid) references bbsUsers(UID) + +select*from bbsSection + + +create table bbsTopic +( tID int primary key identity(1,1), + tUID int references bbsUsers(UID), + tSID int references bbsSection( sID ), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime , + tCount int +) + +select*from bbsTopic +create table bbsReply +( rID int primary key identity(1,1), + rUID int references bbsUsers(UID), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +--1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +-- 小雨点 女 20 0 +-- 逍遥 男 18 4 +-- 七年级生 男 19 2 + +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) + + +-- 2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 + +select uName, upoint into bbsPoint from bbsUsers + + +-- 3.给论坛开设4个板块 + 名称 版主名 + 技术交流 小雨点 + 读书世界 七年级生 + 生活百科 小雨点 + 八卦区 七年级生 +-- 4.向主贴和回帖表中添加几条记录 +insert into bbsTopic values + (2,4,'范跑跑 ',' 谁是范跑跑 ','2008-7-8','1'), + (3,1,'.NET ',' 与JAVA的区别是什么呀? ','2008-9-1 ','2'), + (1,3,'今年夏天最流行什么 ',' 有谁知道今年夏天最流行 ','2008-9-10','0') + +--人数不够,外键有约束 +update bbsTopic set tSID=3 where tSID=1 + +select*from bbsUsers +select*from bbsTopic +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 + +insert into bbsReply values(3,1,'谁问的谁就是范跑跑','20210316'), + (1,2,'这个更简单','20210316'), + (1,3,'今年夏天最流行内裤外穿','20210316') +select*from bbsReply +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select * from bbsUsers +select * from bbsSection +select * from bbsTopic +select * from bbsReply +select UID,uName,sName from bbsUsers inner join bbsSection on bbsUsers.UID=bbsSection.sUid +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select UID,uName,tTitle,tMsg,tTime from bbsUsers inner join bbsTopic on bbsUsers.UID=bbsTopic.tUID where tTime>'2008-9-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +SELECT bbsUsers.uAge,sID,sName,sUid FROM bbsUsers INNER JOIN bbsSection ON bbsUsers.UID=bbsSection.sid WHERE uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select UID,uName,tTitle,tMsg,tCount from bbsUsers inner join bbsTopic on bbsUsers.UID=bbsTopic.tUID where tCount=(select MAX(tCount) from bbsTopic) +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select sName,uName,count(tID) 发帖总数 from bbsUsers left join bbsSection on bbsUsers.UID=bbsSection.sUid left join bbsTopic on bbsUsers.UID=bbsTopic.tUID group by sName,uName diff --git "a/20210326\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\221\250\346\234\253\344\275\234\344\270\232wjw 1.sql" "b/20210326\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\221\250\346\234\253\344\275\234\344\270\232wjw 1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bf8ee0b7cf1e3414386a0a565586586ded3ea80a --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\221\250\346\234\253\344\275\234\344\270\232wjw 1.sql" @@ -0,0 +1,63 @@ +use master +go +create database Student +on( + name='Student', + filename='C:\text\student.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on( + name='Student_log', + filename='C:\text\student_log.ldf', + size=5, + maxsize=100, + filegrowth=10% +) +go +use Student +go +create table stuInfo +( + stuNO varchar(10) primary key, + stuName nvarchar(10), + stuAge int , + stuAddress nvarchar(50), + stuSeat int, + stuSex int , +) +create table Score +( + examNO int primary key identity(1,1), + stuNO varchar(10) references stuInfo(stuNO), + writtenExam int, + labExam int, +) +select * from stuInfo +select * from Score +insert into stuInfo values('s2501','张秋利',20,'美国硅谷',1,1),('s2502','李斯文',18,'湖北武汉',2,0),('s2503','马文才',22,'湖南长沙',3,1), +('s2504','欧阳俊雄',21,'湖北武汉',4,0),('s2505','梅超风',20,'湖北武汉',5,1),('s2506','陈旋风',19,'美国硅谷',6,1), +('s2507','陈风',20,'美国硅谷',7,0) +insert into Score values('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80), +('s2505',70,90),('s2506',85,90) +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuInfo.stuNO 学号,stuName 姓名,stuAge 年龄,Score.writtenExam 笔试成绩,Score.labExam 机试成绩 from stuInfo +inner join Score on stuInfo.stuNO=Score.stuNO +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select stuInfo.stuNO 学号 ,stuInfo.stuName 姓名,writtenExam 笔试成绩,labExam 机试成绩 from Score +inner join stuInfo on stuInfo.stuNO=Score.stuNO where writtenExam>60 and labExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select stuInfo.stuNO 学号,stuName 姓名,stuAge 年龄,Score.writtenExam 笔试成绩,Score.labExam 机试成绩 from stuInfo +left join Score on stuInfo.stuNO=Score.stuNO +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuInfo.stuNO 学号 ,stuInfo.stuName 姓名,stuInfo.stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 from Score +inner join stuInfo on stuInfo.stuNO=Score.stuNO where stuAge>=20 order by writtenExam DESC + +--5.查询男女生的机试平均分 +select stuInfo.stuSex 性别,AVG(labExam) 平均分 from Score +inner join stuInfo on stuInfo.stuNO=Score.stuNO group by stuInfo.stuSex + +--6.查询男女生的笔试总分 +select stuInfo.stuSex 性别,sum(writtenExam) 总分 from Score +inner join stuInfo on stuInfo.stuNO=Score.stuNO group by stuInfo.stuSex diff --git "a/20210326\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\221\250\346\234\253\344\275\234\344\270\232wjw 2.sql" "b/20210326\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\221\250\346\234\253\344\275\234\344\270\232wjw 2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1fa6259cae29633348089d39e98d53458c5f4eb6 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\221\250\346\234\253\344\275\234\344\270\232wjw 2.sql" @@ -0,0 +1,72 @@ +use master +go +create database Demo_01 +on( + name='Demo_01', + filename='C:\text\Demo_01.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on( + name='Demo_01_log', + filename='C:\text\Demo_01_log.ldf', + size=5, + maxsize=100, + filegrowth=10% +) +go +use Demo_01 +go +create table orders +( + orderId int primary key identity not null, + orderDate datetime not null, +) +create table orderItem +( + ItemiD int primary key identity not null, + orderId int references orders(orderId) not null, + itemType nvarchar(10) not null, + itemName nvarchar(10) not null, + theNumber int not null, + theMoney int not null, +) +select * from orders +select * from orderItem +insert into orders values('2008-01-12'),('2008-02-10'),('2008-02-15'),('2008-03-10') +insert into orderItem values(1,'文具','笔',72,2),(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56),(2,'文具','笔',36,2),(2,'文具','固体胶',20,3),(2,'日常用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3),(3,'文具','订书机',20,3),(3,'文具','订书针',10,3),(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2),(4,'文具','信纸',50,1),(4,'日常用品','毛巾',4,5),(4,'日常用品','透明胶',30,1),(4,'体育用品','羽毛球',20,3) + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orders.orderId 订单编号,orders.orderDate 订单日期, orderItem.itemType 订购产品的类别,orderItem.itemName 订购的产品名称,orderItem.theNumber 订购数量,orderItem.theMoney 订购单价 from orders +inner join orderItem on orders.orderId=orderItem.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orders.orderId 订单编号,orders.orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量 from orderItem +inner join orders on orders.orderId= orderItem.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId 订单编号,orders.orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,sum(theNumber*theMoney)订购总价 from orderItem +inner join orders on orders.orderId= orderItem.orderId group by orders.orderId,orders.orderDate,itemType,itemName ,theNumber,theMoney +--4.查询单价大于等于5 并且 数量大于等于50的订单的 订单的编号,订单日期,订购产品的类别 和 订购的产品名称,订购数量 和 订购单价以及 订购总价 +select orders.orderId 订单编号,orders.orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,sum(theNumber*theMoney)订购总价 from orderItem +inner join orders on orders.orderId= orderItem.orderId where theMoney>=5 and theNumber>=50 group by orders.orderId,orders.orderDate,itemType,itemName ,theNumber,theMoney +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orders.orderId 订单编号,count(*)订购产品数 from orders + inner join orderItem on orders.orderId=orderItem.orderId group by orders.orderId + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select orders.orderId 订单编号,itemType 产品类别,count(*)订购次数,sum(theNumber) from orderItem +inner join orders on orders.orderId=orderItem.orderId group by itemType,orders.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\221\250\346\234\253\344\275\234\344\270\232wjw 3.sql" "b/20210326\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\221\250\346\234\253\344\275\234\344\270\232wjw 3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..669898a606e163103ce1b45ca10bb6303724705d --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\221\250\346\234\253\344\275\234\344\270\232wjw 3.sql" @@ -0,0 +1,87 @@ +use master +go +create database bbs +on( + name='bbs', + filename='C:\text\bbs.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on( + name='bbs_log', + filename='C:\text\bbs_log.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +go +use bbs +go +create table bbsUsers +( + uID int identity(1,1) not null , + uName nvarchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null, + +) +alter table bbsUsers add constraint PK_bbsUsers_uID primary key(uID) +alter table bbsUsers add constraint UK_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex='男' or uSex='女') +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 or uAge<=60) +alter table bbsUsers add constraint CK_bbsUsers_uPoint check(uPoint>=0) +create table bbsSection +( + sID int identity(1,1) not null , + sName varchar(10) not null, + sUid int , + +) +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid)references bbsUsers(uID) +create table bbsTopic +( + tID int primary key identity(1,1) not null, + tUID int references bbsUsers(uID) not null , + tSID int references bbsSection(sID) , + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime not null, + tCount int not null, +) +create table bbsReply +( + rID int primary key identity(1,1) not null, + rUID int references bbsUsers(uID) , + rTID int references bbsTopic(tID) , + rMsg text not null, + rTime datetime , +) +select * from bbsReply +select * from bbsSection +select * from bbsTopic +select * from bbsUsers +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select uName,uPoint into bbsPoint_backup from bbsUsers +insert into bbsSection values('技术名流',1),('读书世界',3),('生活百科',1),('八卦区',3) +insert into bbsTopic values(2,4,'范跑跑!','谁是范跑跑!', '2008-7-8',1),(3,2,'.NET!','与JAVA的区别是什么呀?','2008-9-1',2),(1,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10',0) +insert into bbsReply values(3,1,'一名地震自己先跑的教师','2008-7-8'),(1,2,'不知道','2008-9-15'),(2,2,'Java更难','2008-9-20') +update bbsUsers set uPoint=30 where uName='小雨点' +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select bbsUsers.uID 版主编号,bbsUsers.uName 版主姓名,sName 板块名称 from bbsSection +right join bbsUsers on bbsUsers.uID=bbsSection.sUid +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tID 发帖人编号,bbsUsers.uName 发帖人姓名,tTitle 帖子标题,tMsg 帖子内容,tTime 发帖时间 from bbsTopic +inner join bbsUsers on bbsTopic.tUID=bbsUsers.uID where tTime>'2008-09-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select uID 版主编号,uName 版主名称,bbsSection.sName 板块名称 from bbsUsers +left join bbsSection on uID=sUid where uAge<20 +--4.查询出 回复数量最多的主贴 的 发帖人编号, 发帖人姓名, 主贴标题, 主贴内容和 回复数量 +select top 1 tUID 发帖人编号,bbsUsers.uName 发帖人姓名,tTitle 主帖标题,tMsg 主贴内容, tCount 回复数量 from bbsTopic +inner join bbsUsers on uID=tUID order by tCount DESC +--5.在主贴表中查询 每个版块 中 每个用户的 发帖总数 +select bbsSection.sName 板块名称,uID 发帖人编号,SUM(tID)发帖总数 from bbsTopic +right join bbsSection on sID=tSID +left join bbsUsers on uID=tUID group by bbsSection.sName,uID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d3533a7dc10742224abc999d0c55f78317f7c158 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" @@ -0,0 +1,224 @@ +use master +go + +create database Students +on +( + name='Students', + filename='D:\11\Students.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='Students_log', + filename='D:\11\Students_log.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +go + +use Students +go + +create table StuInfo +( + StuNO varchar(5) primary key, + StuName nvarchar(50) not null, + StuAge int not null, + StuAddress nvarchar(50) not null, + StuSeat int not null, + StuSex int default(1) check(StuSex='1' or StuSex='0') +) + +insert into StuInfo values +('s2501','寮犵鍒','20','缇庡浗纭呰胺','1','1'), +('s2502','鏉庢柉鏂','10','婀栧寳姝︽眽','2','0'), +('s2503','椹枃鎵','22','姝︽眽闀挎矙','3','1'), +('s2504','娆ч槼淇婇泟','21','婀栧寳姝︽眽','4','0'), +('s2505','姊呰秴椋','20','婀栧寳姝︽眽','5','1'), +('s2506','闄堟棆椋','19','缇庡浗纭呰胺','6','1'), +('s2507','闄堥','20','缇庡浗纭呰胺','7','0') + +create table ExamInfo +( + examNO int primary key, + StuNO varchar(5) foreign key references StuInfo(StuNO), + WittenExam int, + LabExam int +) + +insert into ExamInfo values +('1','s2501','50','70'), +('2','s2502','60','65'), +('3','s2503','86','85'), +('4','s2504','40','80'), +('5','s2505','70','90'), +('6','s2506','85','90') + +select * from StuInfo +select * from ExamInfo + + +--鏁版嵁濡傚浘鐗1,浣跨敤涓婃浣滀笟鐨勬暟鎹 + +--1.鏌ヨ瀛︾敓鐨勫鍚嶏紝骞撮緞锛岀瑪璇曟垚缁╁拰鏈鸿瘯鎴愮哗 +select StuName,StuAge,WittenExam,LabExam from StuInfo inner join ExamInfo on StuInfo.StuNO = ExamInfo.StuNO +--2.鏌ヨ绗旇瘯鍜屾満璇曟垚缁╅兘鍦60鍒嗕互涓婄殑瀛︾敓鐨勫鍙凤紝濮撳悕锛岀瑪璇曟垚缁╁拰鏈鸿瘯鎴愮哗 +select StuInfo.StuNO,StuName,WittenExam,LabExam from StuInfo inner join ExamInfo on StuInfo.StuNO = ExamInfo.StuNO +where WittenExam>60 and LabExam>60 +--3.鏌ヨ鎵鏈夊鐢熺殑瀛﹀彿锛屽鍚嶏紝绗旇瘯鎴愮哗锛屾満璇曟垚缁╋紝娌℃湁鍙傚姞鑰冭瘯鐨勫鐢熺殑鎴愮哗浠ULL鍊煎~鍏 +select * from StuInfo left join ExamInfo on StuInfo.StuNO = ExamInfo.StuNO +--4.鏌ヨ骞撮緞鍦20浠ヤ笂锛堝寘鎷20锛夌殑瀛︾敓鐨勫鍚嶏紝骞撮緞锛岀瑪璇曟垚缁╁拰鏈鸿瘯鎴愮哗锛屽苟鎸夌瑪璇曟垚缁╅檷搴忔帓鍒 +select StuName,StuAge,WittenExam,LabExam from StuInfo inner join ExamInfo on StuInfo.StuNO = ExamInfo.StuNO +where StuAge>20 order by WittenExam +--5.鏌ヨ鐢峰コ鐢熺殑鏈鸿瘯骞冲潎鍒 +select StuSex,AVG(LabExam)骞冲潎鍒 from ExamInfo inner join StuInfo on StuInfo.StuNO = ExamInfo.StuNO group by StuSex +--6.鏌ヨ鐢峰コ鐢熺殑绗旇瘯鎬诲垎 +select StuSex,AVG(WittenExam)骞冲潎鍒 from ExamInfo inner join StuInfo on StuInfo.StuNO = ExamInfo.StuNO group by StuSex + + +use Students +go +create table orders +( + orderId int primary key identity, + orderDate datetime, +) + +create table orderItem +( + ItemiD int primary key identity, + orderId int references orders(orderId), + itemType nvarchar(20), + itemName nvarchar(20), + theNumber int, + theMoney money +) + +insert into orders values('2008-01-12 00:00:00.000'), +('2008-02-10 00:00:00.000'), +('2008-02-15 00:00:00.000'), +('2008-03-10 00:00:00.000') +insert into orderItem values(1,'鏂囧叿','绗',72,'2'), +(1,'鏂囧叿','灏',10,'1'), +(1,'浣撹偛鐢ㄥ搧','绡悆',1,'56'), +(2,'鏂囧叿','绗',36,'2'), +(2,'鏂囧叿','鍥轰綋鑳',20,'3'), +(2,'鏃ュ父鐢ㄥ搧','閫忔槑鑳',2,'1'), +(2,'浣撹偛鐢ㄥ搧','缇芥瘺鐞',20,'3'), +(3,'鏂囧叿','璁功鏈',20,'3'), +(3,'鏂囧叿','璁功閽',20,'3'), +(3,'鏂囧叿','瑁佺焊鍒',5,'5'), +(4,'鏂囧叿','绗',20,'2'), +(4,'鏂囧叿','淇″皝',50,'1'), +(4,'鏃ュ父鐢ㄥ搧','姣涘肪',4,'5'), +(4,'鏃ュ父鐢ㄥ搧','閫忔槑鑳',30,'1'), +(4,'浣撹偛鐢ㄥ搧','缇芥瘺鐞',20,'3') +select * from orders +select * from orderItem +--浣跨敤涓婃浣滀笟鐨勮鍗曟暟鎹簱锛屽畬鎴愪笅鍒楅鐩細 + +--1.鏌ヨ鎵鏈夌殑璁㈠崟鐨勮鍗曠殑缂栧彿锛岃鍗曟棩鏈燂紝璁㈣喘浜у搧鐨勭被鍒拰璁㈣喘鐨勪骇鍝佸悕绉帮紝璁㈣喘鏁伴噺鍜岃璐崟浠 +select * from orderItem inner join orders on orderItem.orderId=orders.orderId +--2.鏌ヨ璁㈣喘鏁伴噺澶т簬50鐨勮鍗曠殑缂栧彿锛岃鍗曟棩鏈燂紝璁㈣喘浜у搧鐨勭被鍒拰璁㈣喘鐨勪骇鍝佸悕绉 +select orders.orderId,orderDate,itemName,itemType from orderItem inner join orders on orderItem.orderId=orders.orderId +where theNumber>50 +--3.鏌ヨ鎵鏈夌殑璁㈠崟鐨勮鍗曠殑缂栧彿锛岃鍗曟棩鏈燂紝璁㈣喘浜у搧鐨勭被鍒拰璁㈣喘鐨勪骇鍝佸悕绉帮紝璁㈣喘鏁伴噺鍜岃璐崟浠蜂互鍙婅璐讳环 +select orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney,theNumber * theMoney 璁㈣喘鎬讳环 +from orderItem inner join orders on orderItem.orderId=orders.orderId +group by orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney +--4.鏌ヨ鍗曚环澶т簬绛変簬5骞朵笖鏁伴噺澶т簬绛変簬50鐨勮鍗曠殑璁㈠崟鐨勭紪鍙凤紝璁㈠崟鏃ユ湡锛岃璐骇鍝佺殑绫诲埆鍜岃璐殑浜у搧鍚嶇О锛岃璐暟閲忓拰璁㈣喘鍗曚环浠ュ強璁㈣喘鎬讳环 +select orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney,theNumber * theMoney 璁㈣喘鎬讳环 +from orderItem inner join orders on orderItem.orderId=orders.orderId where theMoney>5 and theNumber>50 +group by orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney +--5.鏌ヨ姣忎釜璁㈠崟鍒嗗埆璁㈣喘浜嗗嚑涓骇鍝侊紝渚嬪锛 +-- 缂栧彿 璁㈣喘浜у搧鏁 +-- 1 3 +-- 2 4 +select orderID,sum(theNumber) 璁㈣喘浜у搧鏁 from orderItem group by orderId +--6.鏌ヨ姣忎釜璁㈠崟閲岀殑姣忎釜绫诲埆鐨勪骇鍝佸垎鍒璐簡鍑犳鍜屾绘暟閲忥紝渚嬪锛 + +-- 璁㈠崟缂栧彿 浜у搧绫诲埆 璁㈣喘娆℃暟 鎬绘暟閲 + +-- 1 鏂囧叿 2 82 +-- 1 浣撹偛鐢ㄥ搧 1 1 +-- 2 鏂囧叿 2 56 +-- 2 浣撹偛鐢ㄥ搧 1 2 + --2 鏃ュ父鐢ㄥ搧 1 20 +select * from orderItem +select orderId,itemType,count(ItemName) 璁㈣喘娆℃暟,sum(theNumber) 鎬绘暟閲 from orderItem group by orderId,itemType + + +use Students +go +create table bbsUsers +( + uID int identity(1,1) not null , + uName nvarchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null + +) +alter table bbsUsers add constraint PK_bbsUsers_uID primary key(uID) +alter table bbsUsers add constraint UK_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex='鐢' or uSex='濂') +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 or uAge<=60) +alter table bbsUsers add constraint CK_bbsUsers_uPoint check(uPoint>=0) +create table bbsSection +( + sID int identity(1,1) not null , + sName varchar(10) not null, + sUid int , + +) +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid)references bbsUsers(uID) +create table bbsTopic +( + tID int primary key identity(1,1) not null, + tUID int references bbsUsers(uID) not null , + tSID int references bbsSection(sID) , + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime not null, + tCount int not null, +) +create table bbsReply +( + rID int primary key identity(1,1) not null, + rUID int references bbsUsers(uID) , + rTID int references bbsTopic(tID) , + rMsg text not null, + rTime datetime , +) +go +use Students +go +select * from bbsSection +select * from bbsReply +select * from bbsTopic +select * from bbsUsers +insert into bbsUsers values('灏忛洦鐐','濂',20,0),('閫嶉仴','鐢',18,4),('涓冨勾绾х敓','鐢',19,2) +insert into bbsSection values('鎶鏈悕娴',1),('璇讳功涓栫晫',3),('鐢熸椿鐧剧',1),('鍏崷鍖',3) +insert into bbsTopic values(2,4,'鑼冭窇璺','璋佹槸鑼冭窇璺', 2008-7-8,1),(3,2,'.NET','涓嶫AVA鐨勫尯鍒槸浠涔堝憖锛',2008-9-1,2),(1,4,'浠婂勾澶忓ぉ鏈娴佽浠涔','鏈夎皝鐭ラ亾浠婂勾澶忓ぉ鏈娴佽浠涔堝憖锛',2008-9-10,0) +insert into bbsReply values(2,2,'涓鍚嶅湴闇囪嚜宸卞厛璺戠殑鏁欏笀',2008-7-8),(3,3,'涓嶇煡閬',2008-9-1),(1,1,'娴佽绌块粦瑁欏瓙',2008-9-10) +update bbsUsers set uPoint=30 where uName='灏忛洦鐐' +--鍦ㄨ鍧涙暟鎹簱涓畬鎴愪互涓嬮鐩 +--1.鏌ヨ鍑烘瘡涓増鍧楃殑鐗堜富缂栧彿锛岀増涓诲鍚嶅拰鐗堝潡鍚嶇О +select tID,sName,uName from bbsTopic inner join bbsSection on sID=tID +inner join bbsUsers on tUID=uID +--2.鏌ヨ鍑轰富璐寸殑鍙戝笘鏃堕棿鍦2008-9-15浠ュ悗鐨勪富璐寸殑鍙戝笘浜虹紪鍙凤紝鍙戝笘浜哄鍚嶏紝甯栧瓙鐨勬爣棰橈紝甯栧瓙鐨勫唴瀹瑰拰鍙戝笘鏃堕棿 +select tID,uName,tTitle,tMsg,tTime from bbsTopic inner join bbsUsers on tUID=uID where tTime>'2008-9-15' +--3.鏌ヨ鍑哄勾榫勫湪20浠ヤ笅鐨勭増涓荤殑缂栧彿锛岀増涓荤殑鍚嶇О鍜岀増鍧楃殑鍚嶇О +select tID,sName,uName from bbsTopic inner join bbsSection on sID=tID inner join bbsUsers on tUID=uID +where uAge<20 +--4.鏌ヨ鍑哄洖澶嶆暟閲忔渶澶氱殑涓昏创鐨勫彂甯栦汉缂栧彿锛屽彂甯栦汉濮撳悕锛屼富璐存爣棰橈紝涓昏创鍐呭鍜屽洖澶嶆暟閲 +select top 1 tID,uName,tTitle,tMsg,tCount from bbsTopic inner join bbsUsers on tUID=uID order by tCount DESC +--5.鍦ㄤ富璐磋〃涓煡璇㈡瘡涓増鍧椾腑姣忎釜鐢ㄦ埛鐨勫彂甯栨绘暟 +select tSID 鐗堝潡,tUID 鐢ㄦ埛,count(tSID) 鍙戝笘鎬绘暟 from bbsTopic inner join bbsSection on sID=tID inner join bbsUsers on tUID=uID +group by tSID,tUID + diff --git "a/20210326\344\275\234\344\270\232/\350\203\241\350\266\212/\344\275\234\344\270\232\344\270\200.sql" "b/20210326\344\275\234\344\270\232/\350\203\241\350\266\212/\344\275\234\344\270\232\344\270\200.sql" new file mode 100644 index 0000000000000000000000000000000000000000..edb6350d246f62b347d7ee25721da30ebd02c52f --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\203\241\350\266\212/\344\275\234\344\270\232\344\270\200.sql" @@ -0,0 +1,101 @@ +create database Studentinfo + +on +( + name='Studentinfo', + filename='D:\Studentinfo.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Studentinfo_log', + filename='D:\Studentinfo_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +use Studentinfo +go +create table StuDent +( + sTUNO varchar(15), + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(50), + stuSeat int, + stuSex nvarchar(1) default('男') check(stuSex='男' or stuSex='女') +) + +insert into StuDent(sTUNO,stuName,stuAge,stuAddress,stuSeat,stuSex) +select 's2501','张秋里',20,'美国硅谷',1,'男' union +select 's2502','李斯文',18,'湖北武汉',2,'女' union +select 's2503','马文才',22,'湖南长沙',3,'男' union +select 's2504','欧阳俊雄',21,'湖北武汉',4,'女' union +select 's2505','梅超风',20,'湖北武汉',5,'男' union +select 's2506','陈旋风',19,'美国硅谷',6,'男' union +select 's2507','陈风',20,'美国硅谷',7,'女' + + +create table Score +( + examNO int, + stuNO varchar(15), + writtenExam varchar(200), + labExam varchar(200) +) + +insert into Score(examNO,stuNO,writtenExam,labExam) +select 1,'s2501','50','70' union +select 2,'s2501','60','65' union +select 3,'s2501','86','85' union +select 4,'s2501','40','80' union +select 5,'s2501','70','90' union +select 6,'s2501','85','90' + +create table stuExam +(examNo int, + stuNo varchar(15), + writtenExam int, + labExam int +) +insert into stuExam(stuNo,writtenExam,labExam) values +('s2501',50,70), +('s2502',60,65), +('s2503',86,85), +('s2504',40,80), +('s2505',70,90), +('s2506',85,90) + + +select * from stuExam +select * from StuDent +select * from Score + +--数据如图片1,使用上次作业的数据 + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName 姓名, stuAge 年龄, writtenExam 笔试成绩, labExam 机试成绩 from StuDent s +left join stuExam e on s.stuNo = e.stuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select stuName 姓名, stuAge 年龄, writtenExam 笔试成绩, labExam 机试成绩 from StuDent s +left join stuExam e on s.stuNo = e.stuNo where writtenExam>60 and labExam>60 + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select stuName 姓名, s.stuNo 学号,writtenExam 笔试成绩,labExam 机试成绩 from StuDent s +left join stuExam e on s.stuNo = e.stuNo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName 姓名,stuAge 年龄, writtenExam 笔试成绩, labExam 机试成绩 from StuDent s +left join stuExam e on s.stuNo = e.stuNo where stuAge>=20 order by writtenExam desc + +--5.查询男女生的机试平均分 +select stuSex,AVG(labExam)平均分 from stuExam e +left join StuDent s on e.stuNo= s.stuNo group by stuSex + +--6.查询男女生的笔试总分 +select stuSex, SUM(writtenExam) 笔试总分 from stuExam e +left join StuDent s on e.stuNo= s.stuNo group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\350\203\241\350\266\212/\344\275\234\344\270\232\344\270\211.sql" "b/20210326\344\275\234\344\270\232/\350\203\241\350\266\212/\344\275\234\344\270\232\344\270\211.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8c1e96afdf9d7ff41392fc58d88a55e33493526c --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\203\241\350\266\212/\344\275\234\344\270\232\344\270\211.sql" @@ -0,0 +1,117 @@ +create database bbs +on +( + name = 'bbs', + filename = 'D:\bbs.mdf' +) + +log on +( + name = 'bbs_log', + filename = 'D:\bbs_log.ldf' +) +go + +use bbs +go + +create table bbsUsers +( + UID int identity(1,1) , + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) + +alter table bbsUsers add constraint PK_bbsUser_UID primary key(UID) + +alter table bbsUsers add constraint UK_bbsUser_uName unique(uName) + +alter table bbsUsers add constraint CK_bbsUser_uSex check(uSex in('男','女')) + +alter table bbsUsers add constraint CK_bbsUser_uAge check(uAge>=15 or uAge<=60) + +alter table bbsUsers add constraint CK_bbsUser_uPoint check(uPoint>=0) + + +create table bbsSection +( + sID int identity(1,1), + sName varchar(10) not null, + sUid int +) + +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) + +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUsers(UID) + + + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int foreign key references bbsUsers(UID), + tSID int foreign key references bbsSection(sID), + tTItle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) + + +create table bbsReply +( + rID INT primary key identity(1,1), + rUID int foreign key references bbsUsers(UID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUsers values +('小雨点','女',20,0), +('逍遥','男',18,4), +('七年级生','男',19,2) + + +select * into bbsPoint from bbsUsers + +insert into bbsSection values +('技术交流',1), +('读书世界',3), +('生活百科',1), +('八卦区',3) + +insert into bbsTopic values +(2,4,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?',2008-9-10,0), +(3,1,'.NET','与JAVA的区别是什么呀?',2008-9-1,2), +(1,3,'范跑跑','谁是范跑跑',2008-7-8,1) + +insert into bbsReply values +(2,1,'不知道',2008-9-10), +(3,2,'不知道',2008-9-1), +(1,3,'不知道',2008-7-8) + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称 from bbsSection +left join bbsUsers on bbsSection.sUid = bbsUsers.UID + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号,uName 发帖人姓名,tTItle 帖子的标题,tMsg 帖子的内容,tTime 发帖时间 from bbsTopic +left join bbsUsers on bbsTopic.tUID= bbsUsers.UID where tTime>'2008-9-15 00:00:00' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称, uAge 年龄 from bbsPoint +left join bbsSection on bbsPoint.UID=bbsSection.sUid where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 tUID 发帖人编号,uName 发帖人姓名,tTItle 主贴标题,tMsg 主贴内容,uPoint 回复数量 from bbsTopic +left join bbsUsers on bbsTopic.tUID= bbsUsers.UID order by uPoint desc + +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select sName 版块, uName 用户,count(tTItle) 发帖总数 from bbsTopic +left join bbsSection on bbsTopic.tUID= bbsSection.sUid +left join bbsUsers on bbsTopic.tUID= bbsUsers.UID +group by sName , uName diff --git "a/20210326\344\275\234\344\270\232/\350\203\241\350\266\212/\344\275\234\344\270\232\344\272\214.sql" "b/20210326\344\275\234\344\270\232/\350\203\241\350\266\212/\344\275\234\344\270\232\344\272\214.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d6d1625085e4494e10a945359e3d28589cf56ee0 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\203\241\350\266\212/\344\275\234\344\270\232\344\272\214.sql" @@ -0,0 +1,92 @@ +create database shop on( + name='shop', + filename='D:\shop.mdf' +) +log on( + name='shop_log', + filename='D:\shop.ldf' +) + +use shop +go + +--订单表(orders)列为:订单编号(orderId 主键) 订购日期(orderDate) +create table orders( + orderId int primary key identity(1,1), + orderDate datetime default(getdate()) +) + +--订购项目表(orderItem),列为: +create table orderItem( +--项目编号(ItemiD)订单编号(orderId)产品类别(itemType) + ItemiD int identity(1,1), + orderId int references orders(orderId), + itemType nvarchar(20), +--产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) + itemName nvarchar(20), + theNumber int, + theMoney int +) + +insert into orders(orderDate) values +('2008-01-12 00:00:00'), +('2008-02-10 00:00:00'), +('2008-02-15 00:00:00'), +('2008-03-10 00:00:00') +select * from orders + +insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) values +(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'日常用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3), +(3,'文具','订书机',20,3), +(3,'文具','订书针',10,3), +(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'日常用品','毛巾',4,5), +(4,'日常用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) +select * from orderItem +select * from orders +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select I.orderId 订单编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购产品的产品名称,theNumber 订购数量 ,theMoney 订购单价 from orderItem I +left join orders d on I.orderId = d.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select I.orderId 订单编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购产品的产品名称 from orderItem I +left join orders d on I.orderId = d.orderId where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select I.orderId 订单编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购产品的产品名称,theNumber 订购数量 ,theMoney 订购单价,theNumber*theMoney 订单总价 from orderItem I +left join orders d on I.orderId = d.orderId + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select I.orderId 订单编号,orderDate 订单日期,itemType 订购产品的类别,itemName 订购产品的产品名称,theNumber 订购数量 ,theMoney 订购单价,theNumber*theMoney 订单总价 from orderItem I +left join orders d on I.orderId = d.orderId where theMoney>=5 and theNumber>=50 + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select I.orderId 订单编号,SUM(theNumber) 订购数量 from orderItem I group by I.orderId + + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select I.orderId 订单编号,itemType 产品类别,count(theNumber)订购次数,SUM(theNumber) 总数量 from orderItem I +left join orders d on I.orderId = d.orderId group by itemType,I.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4a4606688df02d7192e475bcb447ff0cb8637293 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" @@ -0,0 +1,101 @@ +use master +go + +create database Student05 +go + +use Student05 +go + +create table StuIS +( + StuNO nvarchar(10) unique not null, + StuName nvarchar(10) not null, + StuAge int not null, + StuAddress nvarchar(200) , + StuSeat nvarchar(8) not null, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女') +) + +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2501','张秋利','20','美国硅谷','1','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2502','李斯文','18','湖北武汉','2','男') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2503','马文才','22','湖南长沙','3','男') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2504','欧阳俊雄','21','湖北武汉','3','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2505','梅超风','20','湖北武汉','4','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2506','陈旋风','19','美国硅谷','5','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2507','陈风','20','美国硅谷','7','女') + +select *from StuIS +select 学号=StuNO ,姓名=StuName,年龄=StuAge,地址=StuAddress,座位号=StuSeat,性别=StuSex from StuIS +select Stuname,StuAge,StuAddress from StuIS +select 学号=StuNO ,姓名=StuName,地址=StuAddress,邮箱=StuName+StuAddress from StuIS +select distinct StuAddress from StuIS +select distinct StuAge as 所有年龄 from StuIS +select top 3 *from StuIS +select top 4 StuName,StuSeat from StuIS +select top 50 percent *from StuIS +select * from StuIS where StuAddress in ('湖北武汉','湖南长沙') +select * from StuIS where StuAddress='湖北武汉' or StuAddress ='湖南长沙' +select * from StuIS where StuAge is null +select * from StuIS where StuAge is not null +select * from StuIS where StuName like '张%' +select * from StuIS where StuAddress like '%湖%' +select * from StuIS where StuName like '张_' +select * from StuIS where StuName like '__俊%' +select * from StuIS order by StuAge DESC +select * from StuIS order by StuAge DESC , StuSeat ASC + + +create table Grate +( + ExamNo int primary key identity(1,1), + StuNo nvarchar(10) references StuIS(StuNO), + WExam int not null, + LabExam int not null +) +insert into Grate(StuNO,WExam,LabExam) +values('s2501','50','70') +insert into Grate(StuNO,WExam,LabExam) +values('s2502','60','65') +insert into Grate(StuNO,WExam,LabExam) +values('s2503','86','85') +insert into Grate(StuNO,WExam,LabExam) +values('s2504','40','80') +insert into Grate(StuNO,WExam,LabExam) +values('s2505','70','90') +insert into Grate(StuNO,WExam,LabExam) +values('s2506','85','90') + + +select 学号=StuNO , 笔试=WExam,机试=LabExam from Grate +select StuNo as 学号 , WExam as 笔试, LabExam as 机试 from Grate +select StuNo 学号 ,WExam 笔试,LabExam 机试 from Grate +select StuNO, WExam ,LabExam,总分=WExam+LabExam from Grate +select * from Grate where WExam<70 or WExam>90 order by WExam DESC +select top 1 * from Grate order by WExam DESC +select top 1 * from Grate order by WExam ASC + +select *from Grate +select *from StuIS +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select StuName 姓名, StuAge 年龄,WExam 笔试成绩 ,LabExam 机试成绩 from StuIS inner join Grate on StuIS.StuNO = Grate.StuNo +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuName 姓名, StuAge 年龄,WExam 笔试成绩 ,LabExam 机试成绩 from StuIS inner join Grate on StuIS.StuNO = Grate.StuNo where WExam >60 and LabExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select StuIS.StuNO 学号,StuName 姓名,WExam 笔试成绩 ,LabExam 机试成绩 from StuIS left join Grate on StuIS.StuNO = Grate.StuNo + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select StuName 姓名, StuAge 年龄,WExam 笔试成绩 ,LabExam 机试成绩 from StuIS inner join Grate on StuIS.StuNO = Grate.StuNo where StuAge>20 order by WExam desc + +--5.查询男女生的机试平均分 +select StuIS.StuSex, avg(LabExam) from StuIS left join Grate on StuIS.StuNO = Grate.StuNo group by StuIS.StuSex +--6.查询男女生的笔试总分 +select StuIS.StuSex, sum(WExam) from StuIS left join Grate on StuIS.StuNO = Grate.StuNo group by StuIS.StuSex + diff --git "a/20210326\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6014f791a1575c6d6c3c82093f23df58e7b4f49c --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery2.sql" @@ -0,0 +1,77 @@ +--先创建如图所示表 +use master +go + +create database Sql +go + +use Sql +go +--订单表(orders)列为:订单编号(orderId 主键) 订购日期(orderDate) +create table orders +( + orderId int primary key identity, + orderDate date +) +--订购项目表(orderItem),列为: +--项目编号(ItemiD)订单编号(orderId)产品类别(itemType) +--产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) +create table orderItem +( + ItemiD int identity, + orderId int , + itemType nvarchar(20), + itemName nvarchar(20), + theNumber int, + theMoney int +) + +insert into orders (orderDate) values +('2008-01-12 00:00:00.000'), +('2008-02-10 00:00:00.000'), +('2008-02-15 00:00:00.000'), +('2008-03-10 00:00:00.000') + +insert into orderItem values +(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'生活用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3), +(3,'文具','订书机',20,3), +(3,'文具','订书机',10,3), +(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'生活用品','毛巾',4,5), +(4,'生活用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) +select * from orders +select * from orderItem +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orders.orderId 订单编号, orderDate 订单日期,itemType 类别, itemName 产品名称 from orders inner join orderItem on orders.orderId=orderItem.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orders.orderId 订单编号, orderDate 订单日期,itemType 类别, itemName 产品名称, theNumber 数量 from orders inner join orderItem on orders.orderId=orderItem.orderId where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId 订单编号, orderDate 订单日期,itemType 类别, itemName 产品名称 ,theNumber 数量, theMoney 订购总价, theNumber*theMoney 订购总价 +from orders inner join orderItem on orders.orderId=orderItem.orderId + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId 订单编号, orderDate 订单日期,itemType 类别, itemName 产品名称 ,theNumber 数量, theMoney 订购单价, theNumber*theMoney 订购总价 +from orders inner join orderItem on orders.orderId=orderItem.orderId where theMoney>=5 and theNumber>50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderItem.orderId , count(itemType) 订购产品数 from orders inner join orderItem on orders.orderId=orderItem.orderId group by orderItem.orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +-- 订单编号 产品类别 订购次数 总数量 +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select orderItem.orderId 订单编号,itemType 产品类别 , count(itemName) 订购次数,sum(theNumber) 总数量 from orders inner join orderItem on orders.orderId=orderItem.orderId group by orderItem.orderId,itemType order by orderItem.orderId asc diff --git "a/20210326\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e2e26f1b014f14a22bf2ee653b6179f5e911f9d4 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery3.sql" @@ -0,0 +1,106 @@ +create database bbs +go + +use bbs +go + +create table bbsUsers +( + UID int identity(1,1) , + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +-- +alter table bbsUsers add constraint PK_bbsUser_UID primary key(UID) +-- +alter table bbsUsers add constraint UK_bbsUser_uName unique(uName) +-- +alter table bbsUsers add constraint CK_bbsUser_uSex check(uSex in('男','女')) +-- +alter table bbsUsers add constraint CK_bbsUser_uAge check(uAge>=15 or uAge<=60) +-- +alter table bbsUsers add constraint CK_bbsUser_uPoint check(uPoint>=0) +-- + +create table bbsSection +( + sID int identity(1,1), + sName varchar(10) not null, + sUid int +) + +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +-- +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUsers(UID) + + + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int foreign key references bbsUsers(UID), + tSID int foreign key references bbsSection(sID), + tTItle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) + + +create table bbsReply +( + rID INT primary key identity(1,1), + rUID int foreign key references bbsUsers(UID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUsers values +('小雨点','女',20,0), +('逍遥','男',18,4), +('七年级生','男',19,2) + + +select * into bbsPoint from bbsUsers + +insert into bbsSection values +('技术交流',1), +('读书世界',3), +('生活百科',1), +('八卦区',3) + +insert into bbsTopic values +(2,4,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?',2008-9-10,0), +(3,1,'.NET','与JAVA的区别是什么呀?',2008-9-1,2), +(1,3,'范跑跑','谁是范跑跑',2008-7-8,1) + +insert into bbsReply values +(2,1,'不知道',2008-9-10), +(3,2,'不知道',2008-9-1), +(1,3,'不知道',2008-7-8) + +select * from bbsUsers +select * from bbsSection +select * from bbsTopic +select * from bbsReply +select * from bbsPoint + + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称 from bbsSection inner join bbsUsers on bbsSection.sUid= bbsUsers.UID +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号,uName 发帖人姓名,tTItle 帖子的标题,tMsg 帖子的内容,tTime 发帖时间 from bbsTopic inner join bbsUsers on bbsTopic.tUID= bbsUsers.UID where tTime>'2008-9-15 00:00:00' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称, uAge 年龄 from bbsPoint inner join bbsSection on bbsPoint.UID=bbsSection.sUid where uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 tUID 发帖人编号,uName 发帖人姓名,tTItle 主贴标题,tMsg 主贴内容,uPoint 回复数量 from bbsTopic inner join bbsUsers on bbsTopic.tUID= bbsUsers.UID order by uPoint desc +--5.在主贴表中查询 每个版块中 每个用户的 发帖总数 +select sName 版块, uName 用户,count(tTItle) 发帖总数 from bbsTopic +inner join bbsSection on bbsTopic.tUID= bbsSection.sUid +inner join bbsUsers on bbsTopic.tUID= bbsUsers.UID +group by sName , uName + diff --git "a/20210326\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e3f238dec7f037e67fe268c8dc2d07d6c786ad94 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" @@ -0,0 +1,67 @@ +use master +go +if exists(select * from sys.databases where name='Student') + drop database Student +create database Student +on +( + name='Student', + filename='D:\SQL\Student.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10MB +) +log on +( + name='Student_log', + filename='D:\SQL\Student_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10MB +) +go +use Student +create table StuInfo +( +stuNO char(5) primary key not null, +stuName nvarchar(20) not null, +stuAge int not null, +stuAddress nvarchar(200) not null, +stuSeat int not null, +stuSex nvarchar(1) default('0') check(StuSex='0' or StuSex='1'), +) +create table stuexam +( +examNO int primary key identity(1,1), +stuNO char(5) constraint FK_StuInfo_stuNO references StuInfo(stuNO), +writtenExam int not null, +labExam int not null +) +insert into StuInfo(stuNO,stuName,stuAge,stuAddress,stuSeat,stuSex) values +('s2501','张秋利',20,'美国硅谷',1,1), +('s2502','李斯文',18,'湖北武汉',2,0), +('s2503','马文才',22,'湖南长沙',3,1), +('s2504','欧阳俊雄',21,'湖北武汉',4,0), +('s2505','梅超风',20,'湖北武汉',5,1), +('s2506','陈旋风',19,'美国硅谷',6,1), +('s2507','陈风',20,'美国硅谷',7,0) +insert into stuexam(stuNO,writtenExam,labExam) values('s2501',50,70), +('s2502',60,65), +('s2503',86,85), +('s2504',40,80), +('s2505',70,90), +('s2506',85,90) +select * from StuInfo +select * from stuexam +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName,stuAge,writtenExam,labExam from StuInfo inner join stuexam on StuInfo.stuNO=stuexam.stuNO +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select stuName,stuAge,writtenExam,labExam from StuInfo inner join stuexam on StuInfo.stuNO=stuexam.stuNO where labExam>60 and writtenExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select StuInfo.stuNO,stuName,writtenExam,labExam from StuInfo left join stuexam on StuInfo.stuNO=stuexam.stuNO +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName,stuAge,writtenExam,labExam from StuInfo inner join stuexam on StuINfo.stuNO=stuexam.stuNO where stuAge>=20 order by writtenExam DESC +--5.查询男女生的机试平均分 +select stuSex,AVG(labExam) from StuInfo left join stuexam on StuInfo.stuNO=stuexam.stuNO group by stuSex +--6.查询男女生的笔试总分 +select stuSex,SUM(writtenExam) from StuInfo left join stuexam on StuInfo.stuNO=stuexam.stuNO group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0d9ae05ac17ce6996ea2899d5d2882caea65a6c8 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery2.sql" @@ -0,0 +1,45 @@ +use master +go +create database heidian +on +( +name='heidian', +filename='D:\SQL\heidian.mdf' +) +log on +( +name='heidian_log', +filename='D:\SQL\heidian_log.ldf' +) +use heidian +go +create table orders +( +orderId int primary key, +orderDate datetime +) +create table orderItem +( +ItemiD int primary key identity, +orderId int, +itemType nvarchar(4), +itemName nvarchar(8), +theNumber int, +theMoney int, +) +insert into orders values(1,'2008-1-12 00:00:00.000'),(2,'2008-2-10 00:00:00.000'),(3,'2008-2-15 00:00:00.000'),(4,'2008-3-10 00:00:00.000') +insert into orderItem values(1,'文具','笔',72,2),(1,'文具','尺',10,1),(1,'体育用品','篮球',1,56),(2,'文具','笔',36,2),(2,'文具','固体胶',20,3),(2,'日常用品','透明胶',2,1),(2,'体育用品','羽毛球',20,3),(3,'文具','订书机',20,3),(3,'文具','订书针',10,3),(3,'文具','裁纸刀',5,5),(4,'文具','笔',20,2),(4,'文具','信纸',50,1),(4,'日常用品','毛巾',4,5),(4,'日常用品','透明胶',30,1),(4,'体育用品','羽毛球',20,3) +select * from orders +select * from orderItem +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orders.orderId,orderDate,itemType,itemName from orders inner join orderItem on orders.orderId=orderItem.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orders.orderId,orderDate,itemType,itemName from orders inner join orderItem on orders.orderId=orderItem.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney,theNumber*theMoney from orders inner join orderItem on orders.orderId=orderItem.orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId,orderDate,itemType,itemName,theNumber,theMoney,theNumber*theMoney from orders inner join orderItem on orders.orderId=orderItem.orderId where theNumber>=50 and theMoney>=5 +--5.查询每个订单分别订购了几个产品 +select orderId,count(itemName) from orderItem group by orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量 +select orderId,itemType,count(itemType),sum(theNumber) from orderItem group by orderId,itemType order by orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cb2b28aff46e29b83dbd1b95c86320ff975bb979 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery3.sql" @@ -0,0 +1,74 @@ +use master +go +create database bbs +on +( +name='bbs', +filename='D:\SQL\bbs.mdf' +) +log on +( +name='bbs_log', +filename='D:\SQL\bbs_log.ldf' +) +go +use bbs +go +create table bbsUsers +( +UID int primary key identity(1,1), +uName varchar(10) not null, +uSex varchar(2) not null check(uSex='鐢' or uSex='濂'), +uAge int not null check(uAge>=15 and uAge<=40), +uPoint int not null check(uPoint>=0) +) +go +create table bbsTopic +( +tID int primary key identity(1,1), +tUID int, +tSID int, +tTitle varchar(100) not null, +tMsg text not null, +tTime datetime, +tCount int +) +go +create table bbsReply +( +rID int primary key identity(1,1), +rUID int constraint FK_bbsUsers_UID references bbsUsers(UID), +rTID int constraint FK_bbsTopic_tID references bbsTopic(tID), +rMsg text not null, +rTime datetime +) +go +create table bbsSection +( +sID int primary key identity(1,1), +sName varchar(10) not null, +sUid int, +) +go +alter table bbsTopic add constraint FK_bbsUsers1_UID foreign key(tUID) references bbsUsers(UID) +alter table bbsTopic add constraint FK_bbsSection_sID foreign key(tSID) references bbsSection(sID) +alter table bbsSection add constraint FK_bbsUsers2_UID foreign key(sUid) references bbsUsers(UID) +insert into bbsUsers(uName,uSex,uAge,uPoint) values('灏忛洦鐐','濂',20,0),('閫嶉仴','鐢',18,4),('涓冨勾绾х敓','鐢',19,2) +select uName,uPoint into bbsPoint from bbsUsers +insert into bbsSection(sName,sUid) values('鎶鏈氦娴',1),('璇讳功涓栫晫',3),('鐢熸椿鐧剧',1),('鍏崷鍖',3) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(2,4,'鑼冭窇璺','璋佹槸鑼冭窇璺','2008-7-8',1),(3,1,'.NET','涓嶫AVA鐨勫尯鍒槸浠涔堝憖锛','2008-9-1',2),(1,3,'浠婂勾澶忓ぉ鏈娴佽浠涔','鏈夎皝鐭ラ亾浠婂勾澶忓ぉ鏈娴佽浠涔堝憖','2008-9-10',0) +insert into bbsReply(rMsg,rTime,rUID,rTID) values('666','2008-1-1',1,2),('666','2008-1-1',2,3),('666','2008-1-1',3,1) +select * from bbsUsers +select * from bbsTopic +select * from bbsReply +select * from bbsSection +--1.鏌ヨ鍑烘瘡涓増鍧楃殑鐗堜富缂栧彿锛岀増涓诲鍚嶅拰鐗堝潡鍚嶇О +select UID,uName,sName from bbsUsers inner join bbsSection on bbsUsers.UID=bbsSection.sUid +--2.鏌ヨ鍑轰富璐寸殑鍙戝笘鏃堕棿鍦2008-9-15浠ュ悗鐨勪富璐寸殑鍙戝笘浜虹紪鍙凤紝鍙戝笘浜哄鍚嶏紝甯栧瓙鐨勬爣棰橈紝甯栧瓙鐨勫唴瀹瑰拰鍙戝笘鏃堕棿 +select UID,uName,tTitle,tMsg,tTime from bbsUsers inner join bbsTopic on bbsUsers.UID=bbsTopic.tUID where tTime>'2008-9-15' +--3.鏌ヨ鍑哄勾榫勫湪20浠ヤ笅鐨勭増涓荤殑缂栧彿锛岀増涓荤殑鍚嶇О鍜岀増鍧楃殑鍚嶇О +select UID,uName,sName from bbsUsers inner join bbsSection on bbsUsers.UID=bbsSection.sUid where uAge<20 +--4.鏌ヨ鍑哄洖澶嶆暟閲忔渶澶氱殑涓昏创鐨勫彂甯栦汉缂栧彿锛屽彂甯栦汉濮撳悕锛屼富璐存爣棰橈紝涓昏创鍐呭鍜屽洖澶嶆暟閲 +select UID,uName,tTitle,tMsg,tCount from bbsUsers inner join bbsTopic on bbsUsers.UID=bbsTopic.tUID where tCount=(select MAX(tCount) from bbsTopic) +--5.鍦ㄤ富璐磋〃涓煡璇㈡瘡涓増鍧椾腑姣忎釜鐢ㄦ埛鐨勫彂甯栨绘暟 +select sName,uName,count(tID) 鍙戝笘鎬绘暟 from bbsTopic left join bbsSection on bbsTopic.tUID=bbsSection.sUid left join bbsUsers on bbsSection.sUid=bbsUsers.UID group by sName,uName diff --git "a/20210326\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye1.sql" "b/20210326\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a47a3f743144ed3656fabcfc05491309883373a1 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye1.sql" @@ -0,0 +1,72 @@ +use master +go + +create database aa +go + +use aa +go + +create table StuIS +( + StuNO nvarchar(10) unique not null, + StuName nvarchar(10) not null, + StuAge int not null, + StuAddress nvarchar(200) , + StuSeat nvarchar(8) not null, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女') +) + +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2501','张秋利','20','美国硅谷','1','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2502','李斯文','18','湖北武汉','2','男') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2503','马文才','22','湖南长沙','3','男') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2504','欧阳俊雄','21','湖北武汉','3','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2505','梅超风','20','湖北武汉','4','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2506','陈旋风','19','美国硅谷','5','女') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2507','陈风','20','美国硅谷','7','女') + +create table Grate +( + ExamNo int primary key identity(1,1), + StuNo nvarchar(10) references StuIS(StuNO), + WExam int not null, + LabExam int not null +) +insert into Grate(StuNO,WExam,LabExam) +values('s2501','50','70') +insert into Grate(StuNO,WExam,LabExam) +values('s2502','60','65') +insert into Grate(StuNO,WExam,LabExam) +values('s2503','86','85') +insert into Grate(StuNO,WExam,LabExam) +values('s2504','40','80') +insert into Grate(StuNO,WExam,LabExam) +values('s2505','70','90') +insert into Grate(StuNO,WExam,LabExam) +values('s2506','85','90') +select * from StuIS +select * from Grate + +--数据如图片1,使用上次作业的数据 + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select StuName,StuAge,WExam,LabExam from StuIS inner join Grate on StuIS.StuNO=Grate.StuNo +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuIS.StuNo,StuName,StuAge,WExam,LabExam from StuIS + inner join Grate on StuIS.StuNO=Grate.StuNo where WExam>60 and LabExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select StuIS.StuNo,StuName,StuAge,WExam,LabExam from StuIS left join Grate on StuIS.StuNO=Grate.StuNo +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select StuName,StuAge,WExam,LabExam from StuIS + inner join Grate on StuIS.StuNO=Grate.StuNo where StuAge>20 order by WExam DESC +--5.查询男女生的机试平均分 +select StuSex,avg(LabExam) 笔试平均分 from StuIS inner join Grate on StuIS.StuNO=Grate.StuNo group by StuSex +--6.查询男女生的笔试总分 +select StuSex,sum(WExam) 笔试总分 from StuIS inner join Grate on StuIS.StuNO=Grate.StuNo group by StuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye2.sql" "b/20210326\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..394123f32a8effb724384540a1818fd87db8e43e --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye2.sql" @@ -0,0 +1,81 @@ +use master +go + +create database Student05 +go + +use Student05 +go +create table orders +( +orderId int primary key identity (1,1), +orderDate date, + +) +go +create table orderItem +( +ItemiD int, +orderId int, +itemType nvarchar(10), +itemName nvarchar(20), +theNumber int, +theMoney money, +) + +insert into orders (orderDate) values +('2008-01-12 00:00:00.000'), +('2008-02-10 00:00:00.000'), +('2008-02-15 00:00:00.000'), +('2008-03-10 00:00:00.000') + +insert into orderItem values +(1,1,'文具','笔',72,2), +(2,1,'文具','尺',10,1), +(3,1,'体育用品','篮球',1,56), +(4,2,'文具','笔',36,2), +(5,2,'文具','固体胶',20,3), +(6,2,'生活用品','透明胶',2,1), +(7,2,'体育用品','羽毛球',20,3), +(8,3,'文具','订书机',20,3), +(9,3,'文具','订书机',10,3), +(10,3,'文具','裁纸刀',5,5), +(11,4,'文具','笔',20,2), +(12,4,'文具','信纸',50,1), +(13,4,'生活用品','毛巾',4,5), +(14,4,'生活用品','透明胶',30,1), +(15,4,'体育用品','羽毛球',20,3) +select* from orders +select* from orderItem +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orders.orderId,orderDate,itemType,itemName,theMoney,theMoney from orders + inner join orderItem on orders.orderId=orderItem.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orders.orderId,orderDate,itemType,itemName from orders +inner join orderItem on orders.orderId=orderItem.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId,orderDate,itemType,itemName,theMoney,theMoney from orders + inner join orderItem on orders.orderId=orderItem.orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderId,orderDate,itemType,itemName,theMoney,theMoney from orders + inner join orderItem on orders.orderId=orderItem.orderId where theMoney>=5 and theNumber>=50 + select* from orderItem +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderItem.orderId,count(itemName)订购产品数 from orderItem + left join orders on orderItem.orderId=orders.orderId group by orderItem.orderId + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +select orderItem.orderId,itemType, count(theNumber)订购次数,sum(theNumber)订购数量 from orderItem + left join orders on orderItem.orderId=orders.orderId group by orderItem.orderId,itemType +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye3.sql" "b/20210326\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cd3fe7590de4b6ea6c47019159a3c8158c235ea2 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye3.sql" @@ -0,0 +1,79 @@ +use master +go +create database bbs +on +( +name='bbs', +filename='D:\SQL\bbs.mdf' +) +log on +( +name='bbs_log', +filename='D:\SQL\bbs_log.ldf' +) +go +use bbs +go +create table bbsUsers +( +UID int primary key identity(1,1), +uName varchar(10) not null, +uSex varchar(2) not null check(uSex='男' or uSex='女'), +uAge int not null check(uAge>=15 and uAge<=40), +uPoint int not null check(uPoint>=0) +) +go +create table bbsTopic +( +tID int primary key identity(1,1), +tUID int, +tSID int, +tTitle varchar(100) not null, +tMsg text not null, +tTime datetime, +tCount int +) +go +create table bbsReply +( +rID int primary key identity(1,1), +rUID int constraint FK_bbsUsers_UID references bbsUsers(UID), +rTID int constraint FK_bbsTopic_tID references bbsTopic(tID), +rMsg text not null, +rTime datetime +) +go +create table bbsSection +( +sID int primary key identity(1,1), +sName varchar(10) not null, +sUid int, +) +go +alter table bbsTopic add constraint FK_bbsUsers1_UID foreign key(tUID) references bbsUsers(UID) +alter table bbsTopic add constraint FK_bbsSection_sID foreign key(tSID) references bbsSection(sID) +alter table bbsSection add constraint FK_bbsUsers2_UID foreign key(sUid) references bbsUsers(UID) +insert into bbsUsers(uName,uSex,uAge,uPoint) values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select uName,uPoint into bbsPoint from bbsUsers +insert into bbsSection(sName,sUid) values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(2,4,'范跑跑','谁是范跑跑','2008-7-8',1),(3,1,'.NET','与JAVA的区别是什么呀?','2008-9-1',2),(1,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀','2008-9-10',0) +insert into bbsReply(rMsg,rTime,rUID,rTID) values('666','2008-1-1',1,2),('666','2008-1-1',2,3),('666','2008-1-1',3,1) +select * from bbsUsers--信 +select * from bbsTopic--主 +select * from bbsReply--回 +select * from bbsSection--版 +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select UID,sName,uName from bbsUsers inner join bbsSection on bbsUsers.UID=bbsSection.sUid +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID,uName,tTitle,tMsg,tTime from bbsTopic + inner join bbsUsers on bbsTopic.tUID=bbsUsers.UID where tTime>2008-09-15 +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select UID,uName,sName from bbsUsers inner join bbsSection on bbsUsers.UID=bbsSection.sUid where uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 + select UID,uName,tTitle,tMsg,tCount from bbsUsers inner join bbsTopic on bbsUsers.UID=bbsTopic.tUID + where tCount=(select MAX(tCount) from bbsTopic) +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select sName,uName,count(tID) 发帖总数 from bbsUsers +left join bbsSection on bbsUsers.UID=bbsSection.sUid +left join bbsTopic on bbsUsers.UID=bbsTopic.tUID group by sName,uName \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/\347\273\203\344\271\2401.sql" "b/20210326\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/\347\273\203\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0f538c0286bd1bed4d2c0b530dc219943d6dd689 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/\347\273\203\344\271\2401.sql" @@ -0,0 +1,66 @@ +create database Student +on +( + name='Student', + filename='D:\SQL\Student.mdf' +) +log on +( + name='Student_log', + filename='D:\SQL\Student_log.ldf' +) +go + +create table StuInfo +( + stuNo char(5) primary key(stuNo), + stuName nvarchar(20), + stuAge int, + stuAddress text, + stuSeat int identity(1,1), + stuSex char(1) check(stuSex in(1,0)) +) + +create table StuExam +( + examNo int identity(1,1), + stuNo char(5), + writtenExam int check(writtenExam>=0 and writtenExam<=100), + labExam int check(labExam>=0 and labExam<=100) +) + + alter table StuExam add constraint RK_StuExam_stuNo foreign key(stuNo) references StuInfo(stuNo) + + insert into StuInfo values + ('s2501','张秋利',20,'美国硅谷',1), + ('s2502','李斯文',18,'湖北武汉',0), + ('s2503','马文才',22,'湖南长沙',1), + ('s2504','欧阳俊雄',21,'湖北武汉',0), + ('s2505','梅超风',20,'湖北武汉',1), + ('s2506','陈旋风',19,'美国硅谷',1), + ('s2507','陈风',20,'美国硅谷',0) + insert into StuExam values + ('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) + +--数据如图片1,使用上次作业的数据 + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 +from StuInfo i inner join StuExam e on i.stuNo=e.stuNo +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select i.stuNo 学号,stuName 姓名,writtenExam 笔试成绩,labExam 机试成绩 from StuInfo i left join StuExam e +on i.stuNo=e.stuNo where writtenExam>60 and labExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 +from StuInfo i left join StuExam e on i.stuNo=e.stuNo +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 +from StuInfo i inner join StuExam e on i.stuNo=e.stuNo +where stuAge>=20 order by writtenExam DESC +--5.查询男女生的机试平均分 +select stuSex 性别,avg(labExam) 机试成绩 +from StuInfo i inner join StuExam e on i.stuNo=e.stuNo +where stuAge>=20 group by stuSex +--6.查询男女生的笔试总分 +select stuSex 性别,sum(writtenExam) 笔试成绩 +from StuInfo i inner join StuExam e on i.stuNo=e.stuNo group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/\347\273\203\344\271\2402.sql" "b/20210326\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/\347\273\203\344\271\2402.sql" new file mode 100644 index 0000000000000000000000000000000000000000..59a3de16b44f20ac63c522959bb995cca254b942 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/\347\273\203\344\271\2402.sql" @@ -0,0 +1,84 @@ +create database Assignment +on +( + name='Assignment', + filename='D:\SQL\Assignment.mdf' +) +log on +( + name='Assignment_log', + filename='D:\SQL\Assignment_log.ldf' +) +go + +use Assignment +go +--drop table orderItem +create table orders +( + orderId int primary key, + orderDate date +) + +create table orderItem +( + ItemiD int identity(1,1) primary key, + orderId int references orders(orderId), + itemType varchar(12), + itemName varchar(12), + theNumber int, + theMoney money +) +insert into orders values + (1,'2008-01-12'), + (2,'2008-02-10'), + (3,'2008-02-15'), + (4,'2008-03-10') +insert into orderItem values + (1,'文具','笔',72,2), + (1,'文具','尺',10,1), + (1,'体育用品','篮球',1,56), + (2,'文具','笔',36,2), + (2,'文具','固体胶',20,3), + (2,'日常用品','透明胶',2,1), + (2,'体育用品','羽毛球',20,3), + (3,'文具','订书机',20,3), + (3,'文具','订书针',10,3), + (3,'文具','裁纸刀',5,5), + (4,'文具','笔',20,2), + (4,'文具','信纸',50,1), + (4,'日常用品','毛巾',4,5), + (4,'日常用品','透明胶',30,1), + (4,'体育用品','羽毛球',20,3) +-- 使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select o.orderId 订单编号,orderDate 订单日期,itemType 订购类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价 +from orders o inner join orderItem i on o.orderId=i.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select o.orderId 订单编号,orderDate 订单日期,itemType 订购类别,itemName 产品名称 +from orders o inner join orderItem i on o.orderId=i.orderId +where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select o.orderId 订单编号,orderDate 订单日期,itemType 订购类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 +from orders o inner join orderItem i on o.orderId=i.orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select o.orderId 订单编号,orderDate 订单日期,itemType 订购类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 +from orders o inner join orderItem i on o.orderId=i.orderId +where theMoney>=5 and theNumber>=50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select o.orderId 订单编号,count(*) from orders o inner join orderItem i on o.orderId=i.orderId group by o.orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select o.orderId 订单编号,itemType 订购类别,count(*) 订购次数,sum(theNumber) 总数量 +from orders o inner join orderItem i on o.orderId=i.orderId group by o.orderId,itemType order by o.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/\347\273\203\344\271\2403.sql" "b/20210326\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/\347\273\203\344\271\2403.sql" new file mode 100644 index 0000000000000000000000000000000000000000..46670cb1fa882f96c162678b21fddf5adeb5dfcf --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/\347\273\203\344\271\2403.sql" @@ -0,0 +1,20 @@ +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称 +from bbsSection bs inner join bbsUsers bu +on sUid=bu.UID +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号,uName 发帖人姓名,tTitle 贴子的标题,tMsg 帖子的内容,tTime 发帖时间 +from bbsTopic bs inner join bbsUsers bu +on tUID=bu.UID where tTime>'2008-9-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称 +from bbsSection bs inner join bbsUsers bu +on sUid=bu.UID where uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select tUID 发帖人编号,uName 发帖人姓名,tTitle 贴子的标题,tMsg 帖子的内容,tCount 回复数量 +from bbsTopic bs inner join bbsUsers bu +on tUID=bu.UID where tCount=(select max(tCount) from bbsTopic) +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select sName,uName 用户名,count(tUID ) 发帖总数 +from bbsSection S left join bbsUsers U on sUid =UID left join bbsTopic T on sID =tUID group by sName,uName diff --git "a/20210326\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..aadb08495749761570ccba76764aff4e96d2a17d --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" @@ -0,0 +1,26 @@ + + select*from bbsPoint + select*from bbsReply + select*from bbsSection + select*from bbsTopic + select*from bbsUsers +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select suid 版主编号, uName 版主姓名,sName 版块名称 from bbsSection join bbsUsers on bbsSection.sUid=bbsUsers.uID + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select rID 发帖人编号, rUID 发帖人姓名, tTitle 帖子的标题 ,tMsg 帖子的内容, rTime 发帖时间 from bbsReply join bbsTopic on bbsReply.rUID=bbsTopic.tUID where rTime>2008-9-15 +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select suid 版主编号, uName 版主姓名,sName 版块名称, uAge 年龄 from bbsSection join bbsUsers on bbsSection.sUid=bbsUsers.uID where uAge<20 +select suid 版主编号, uName 版主姓名,sName 版块名称, uAge 年龄 from bbsSection join bbsUsers on bbsSection.sUid=bbsUsers.uID where uAge<=20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select tCount from bbsTopic +select rID 发帖人编号, rUID 发帖人姓名, tTitle 主贴标题, tMsg 主贴内容 ,tCOunt 回复数量 from bbsReply join bbsTopic on bbsReply.rUID=bbsTopic.tUID where tCOunt=2 +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select sName 板块,tUid 用户,count(tID) 发帖总数 from bbsTopic join bbsSection on bbsSection.sUid =bbsTopic.tUID +group by sName,tUid + + + + + diff --git "a/20210326\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d6096bdb999a5581c35036745c223dfa32e2c9bc --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" @@ -0,0 +1,32 @@ +select *from orders +select*from orderItem +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orderItem.orderid 订单编号,itemType 产品类别, itemName 产品名称 ,orderdate 订单日期,theNumber 数量, theMoney 单价 from orderItem inner join orders on orderItem.orderid=orders.orderid + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orderItem.orderid 订单编号,itemType 产品类别, itemName 产品名称 ,orderdate 订单日期 from orderItem inner join orders on orderItem.orderid=orders.orderid where orderItem.theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderid 订单编号,itemType 产品类别, itemName 产品名称 ,orderdate 订单日期,theNumber 数量, theMoney 单价 , theMoney*theNumber 总价 +from orderItem left join orders on orderItem.orderid =orders.orderid group by orderItem.orderid,itemType,itemName,orderdate,theNumber,theMoney +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderid 订单编号,orderdate 订单日期,itemType 产品类别, itemName 产品名称,theNumber 数量, theMoney 单价 , theMoney*theNumber 总价 +from orderItem left join orders on orderItem.orderid =orders.orderid where theNumber>=50 and theMoney>=5 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- +select orderid 编号,count(theNumber) 订购产品数 from orderItem group by orderid 2 4 + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select orderItem. orderid 编号,itemType 产品类别,count(*)订购次数 ,sum(theNumber)总数量 from orderItem left join orders on orderItem.orderid=orders.orderid group by itemType,orderItem.orderid \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery5.sql" "b/20210326\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2ed2cce4bea93caf124d667a4118ab8140d26fd7 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery5.sql" @@ -0,0 +1,18 @@ + + +--数据如图片1,使用上次作业的数据 +select *from StuMark +select *from stuinfo +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 + +select stuinfo.stuno , StuName 姓名,StuAge 年龄,writtenExam 笔试, labexam 机试 from stuinfo left join StuMark on stuinfo.stuno=StuMark.stuno +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select stuinfo.stuno ,StuSeat 学号 ,StuName 姓名, writtenExam 笔试, labexam 机试 from stuinfo inner join StuMark on stuinfo.stuno=StuMark.stuno where writtenExam>60 and labexam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select StuSeat 学号 ,StuName 姓名, writtenExam 笔试, labexam 机试 from stuinfo inner join StuMark on stuinfo.stuno =StuMark.stuno +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select StuName 姓名, writtenExam 笔试, labexam 机试,StuAge 年龄 from stuinfo inner join StuMark on stuinfo.stuno =StuMark.stuno where StuAge>20 order by writtenExam desc +--5.查询男女生的机试平均分 +select stusex 性别, avg(labexam)平均 from stuinfo inner join StuMark on stuinfo.stuno = StuMark.stuno group by stusex +--6.查询男女生的笔试总分 +select stusex 性别, sum(writtenExam)总分 from stuinfo inner join StuMark on stuinfo.stuno = StuMark.stuno group by stusex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4024cddd2c907278205a6a35d9f771b74b76f6b3 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery1.sql" @@ -0,0 +1,76 @@ +create database Studentinfo +on +( + name='Studentinfo', + filename='D:\Studentinfo.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Studentinfo_log', + filename='D:\Studentinfo_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +use Studentinfo +go +create table StuDent +( + sTUNO varchar(15), + stuName nvarchar(20), + stuAge int, + stuAddress nvarchar(50), + stuSeat int, + stuSex nvarchar(1) default('男') check(stuSex='男' or stuSex='女') +) + +insert into StuDent(sTUNO,stuName,stuAge,stuAddress,stuSeat,stuSex) +select 's2501','张秋里',20,'美国硅谷',1,'男' union +select 's2502','李斯文',18,'湖北武汉',2,'女' union +select 's2503','马文才',22,'湖南长沙',3,'男' union +select 's2504','欧阳俊雄',21,'湖北武汉',4,'女' union +select 's2505','梅超风',20,'湖北武汉',5,'男' union +select 's2506','陈旋风',19,'美国硅谷',6,'男' union +select 's2507','陈风',20,'美国硅谷',7,'女' + + +create table Score +( + examNO int, + stuNO varchar(15), + writtenExam int, + labExam int +) + + +insert into Score(examNO,stuNO,writtenExam,labExam) +select 1,'s2501','50','70' union +select 2,'s2502','60','65' union +select 3,'s2503','86','85' union +select 4,'s2504','40','80' union +select 5,'s2505','70','90' union +select 6,'s2506','85','90' + + + + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName 姓名,stuAge 年龄,writtenExam 笔试,labExam 机试 from StuDent inner join Score on StuDent.sTUNO=Score.stuNO +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuDent.sTUNO 学号,stuName 姓名,writtenExam 笔试成绩,labExam 机试成绩 from StuDent inner join Score on StuDent.sTUNO=Score.stuNO where writtenExam>60 and labExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select StuDent.sTUNO 学号,stuName 姓名,writtenExam 笔试成绩,labExam 机试成绩 from StuDent left join Score on StuDent.sTUNO=Score.stuNO +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 from StuDent inner join Score on StuDent.sTUNO=Score.stuNO where stuAge>=20 order by writtenExam desc +--5.查询男女生的机试平均分 +select stuSex,avg(labExam)平均分 from Score left join StuDent on Score.stuNO=StuDent.sTUNO group by stuSex +--6.查询男女生的笔试总分 +select stuSex,sum(writtenExam)总分 from Score left join StuDent on Score.stuNO=StuDent.sTUNO group by stuSex + +select * from Score +select * from StuDent \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c743c9a8476bb10ac59cfa9117d67280444260f3 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery2.sql" @@ -0,0 +1,90 @@ +create database cc +on +( + name='cc', + filename='D:\cc.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +log on +( + name='cc_log', + filename='D:\cc.ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +go +use cc +go +--订单表(orders)列为:订单编号(orderId 主键) 订购日期(orderDate) + +create table orders +( + orderId int primary key, + orderDate datetime +) +--插入表1数据 +insert into orders(orderId,orderDate) +select 1,'2008-01-12' union +select 2,'2008-02-10' union +select 3,'2008-02-15' union +select 4,'2008-03-10' +select * from orders +--订购项目表(orderItem),列为: +--项目编号(ItemiD)订单编号(orderId)产品类别(itemType) +--产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) +create table orderItem +( + ItemiD int primary key identity(1,1), + orderId int, + itemType varchar(20), + itemName varchar(20), + theNumber int , + theMoney int +) +--添加表二的外键约束 关联表一的order id +alter table orderItem add constraint FK foreign key (orderId) references orders(orderId) +--插入表2数据 +insert into orderItem(orderId,itemType,itemName,theNumber,theMoney) +select 1,'文具','笔',72,2 union +select 1,'文具','尺',10,1 union +select 1,'体育用品','篮球',1,56 union +select 2,'文具','笔',36,2 union +select 2,'文具','固体胶',20,3 union +select 2,'日常用品','透明胶',2,1 union +select 2,'体育用品','羽毛球',20,3 union +select 3,'文具','订书机',20,3 union +select 3,'文具','订书针',10,3 union +select 3,'文具','裁纸刀',5,5 union +select 4,'文具','笔',20,2 union +select 4,'文具','信纸',50,1 union +select 4,'日常用品','毛巾',4,5 union +select 4,'日常用品','透明胶',30,1 union +select 4,'体育用品','羽毛球',20,3 + +--使用上次作业的订单数据库,完成下列题目: +--订单表(orders) 订购项目表(orderItem) +select * from orders +select * from orderItem +--1.查询所有的订单的 订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orderItem.orderId 订单编号,orders.orderDate 日期,itemType 类别,itemName 产品名称,theNumber 数量,theMoney 单价 from orderItem inner join orders on orderItem.orderId=orders.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orderItem.orderId 订单编号,theNumber 数量,orders.orderDate 日期,itemType 类别,itemName 产品名称 from orderItem inner join orders on orderItem.orderId=orders.orderId where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId 订单编号,orders.orderDate 日期,itemName 产品名称,theNumber 数量,theMoney 单价 ,theMoney*theNumber 订购总价 +from orderItem inner join orders on orderItem.orderId=orders.orderId + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId 订单编号,orderDate 日期,itemType 类别,itemName 产品名称,theNumber 数量,theMoney 单价 ,theMoney*theNumber 订购总价 + from orderItem inner join orders on orderItem.orderId=orders.orderId where theMoney>=5 and theNumber>=50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderId 编号 ,count(itemName)订购产品数 from orderItem group by orderId + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +select orderId,itemType 类别,count(itemType)次数,sum(theNumber)总数量 from orderItem group by orderId,itemType order by orderId asc \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c5ec325c5866a3b3479cf5c0114ea9f1a37c494c --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery3.sql" @@ -0,0 +1,98 @@ +create database bbs + +on +( + name='bbs', + filename='D:\bbs.mdf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +go +use bbs +go +create table bbsUsers +( + UIDD int identity, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +create table bbsSection +( + sIDD int identity, + sName varchar(10) not null, + sUid int +) + +alter table bbsUsers add constraint Pk_bbsUsers_UIDD primary key(UIDD) +alter table bbsUsers add constraint Uk_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint Ck_bbsUsers_uSex check(uSex='男'or uSex='女') +alter table bbsUsers add constraint Ck_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint Ck_bbsUsers_uPoint check(uPoint>=0) + +alter table bbsSection add constraint Pk_bbsSection_sIDD primary key(sIDD) +alter table bbsSection add constraint Fk_bbsSection_sUid foreign key(sUid) references bbsUsers(UIDD) + +create table bbsTopic +( + tID int identity primary key, + tUID int foreign key references bbsUsers(UIDD), + tSID int foreign key references bbsSection(sIDD), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +create table bbsReply +( + rID int identity primary key, + rUID int foreign key references bbsUsers(UIDD), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select uName,uPoint into bbsPoint from bbsUsers +insert into bbsSection values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) +insert into bbsTopic values(2,4,'范跑跑 ',' 谁是范跑跑 ','2008-7-8','1'),(3,1,'.NET ',' 与JAVA的区别是什么呀? ','2008-9-1 ','2'), +(1,3,'今年夏天最流行什么 ',' 有谁知道今年夏天最流行 ','2008-9-10','0') + +insert into bbsReply values(3,2,'八嘎','2008-7-8'),(1,1,'python','2008-7-8'),(1,3,'上海','2008-7-8') + +--据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sIDD,uName,sName from bbsSection left join bbsUsers on sIDD=UIDD + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID,uName,tTitle,tMsg,tTime from bbsTopic left join bbsUsers on tID=UIDD where tTime > '2008-09-15' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sIDD,uName,sName,uAge from bbsSection left join bbsUsers on sIDD=UIDD where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 tUID,uName,tTitle,tMsg,tCount from bbsUsers t left join bbsTopic x on t.UIDD=x.tID order by tCount desc + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select uName 用户名,sName 版块名称,count(*)发帖总数 from bbsTopic + inner join bbsUsers on bbsTopic.tUID = bbsUsers.UIDD + inner join bbsSection on bbsUsers.UIDD= bbsSection.sUid group by uName,sName + + + + +select * from bbsReply --回帖表 +select * from bbsSection --版块表 +select * from bbsTopic --主帖表 +select * from bbsUsers --用户信息表 \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery10.sql" "b/20210326\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery10.sql" new file mode 100644 index 0000000000000000000000000000000000000000..85e4b65363303ed32c12561de4bc4e582dc30cdd --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery10.sql" @@ -0,0 +1,88 @@ +create database OrderIno +go + +use OrderIno +go + +create table orders +( + orderID int primary key identity(1,1), + orderDate datetime +) + +create table orderltem +( + ltemID int identity(1,1), + orderID int , + itemType nvarchar(15), + itemName nvarchar(10), + theNumber int , + theMoney money +) + +insert into orders (orderDate) values +('2008-01-12 00:00:00.000'), +('2008-02-10 00:00:00.000'), +('2008-02-15 00:00:00.000'), +('2008-03-10 00:00:00.000') + +insert into orderltem values +(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'生活用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3), +(3,'文具','订书机',20,3), +(3,'文具','订书机',10,3), +(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'生活用品','毛巾',4,5), +(4,'生活用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) + + +--使用上次作业的订单数据库,完成下列题目: + +select * from orders --订单表 +select * from orderltem --订购项目表 + +--订单表(orders)列为:订单编号(orderID 主键) 订购日期(orderDate)--订购项目表(orderItem),列为:--项目编号(ItemID)订单编号(orderID)产品类别(itemType)--产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orders.orderID 订单的编号 ,orderDate 订单日期, itemType 订购产品的类别,itemName 订购的产品名称 from orders +inner join orderltem on orders.orderID =orderltem.orderID + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orders.orderID 订单的编号 ,orderDate 订单日期, itemType 订购产品的类别,itemName 订购的产品名称 from orders +inner join orderltem on orders.orderID =orderltem.orderID where orderltem.theNumber >50 + + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderID 订单的编号 ,orderDate 订单日期, itemType 订购产品的类别,itemName 订购的产品名称, theNumber 订购数量 ,theMoney 订购单价 , theNumber*theMoney 订购总价 from orders +inner join orderltem on orders.orderID =orderltem.orderID + + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderID 订单的编号 ,orderDate 订单日期, itemType 订购产品的类别,itemName 订购的产品名称, theNumber 订购数量 ,theMoney 订购单价 , theNumber*theMoney 订购总价 from orders +inner join orderltem on orders.orderID =orderltem.orderID where orderltem.theMoney>=5 and orderltem.theNumber>=50 + + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderID 订单的编号 , count(*)订购产品数 from orderltem group by orderID + + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +-- 订单编号 产品类别 订购次数 总数量 +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select orderID 订单的编号 , itemType 产品类别 ,count(*)订购次数 , sum(theNumber) 总数量 + from orderltem group by orderID,itemType order by orderID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery12.sql" "b/20210326\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery12.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f001f7ee30741420ebf30d337a4cceaaee8bea78 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery12.sql" @@ -0,0 +1,115 @@ +use master +go + +create database bbs +go + +use bbs +go + +create table bbsUsers +( + UID int identity(1,1) , + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +-- +alter table bbsUsers add constraint PK_bbsUser_UID primary key(UID) +-- +alter table bbsUsers add constraint UK_bbsUser_uName unique(uName) +-- +alter table bbsUsers add constraint CK_bbsUser_uSex check(uSex in('男','女')) +-- +alter table bbsUsers add constraint CK_bbsUser_uAge check(uAge>=15 or uAge<=60) +-- +alter table bbsUsers add constraint CK_bbsUser_uPoint check(uPoint>=0) +-- + +create table bbsSection +( + sID int identity(1,1), + sName varchar(10) not null, + sUid int +) + +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +-- +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUsers(UID) + + + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int foreign key references bbsUsers(UID), + tSID int foreign key references bbsSection(sID), + tTItle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) + + +create table bbsReply +( + rID INT primary key identity(1,1), + rUID int foreign key references bbsUsers(UID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUsers values +('小雨点','女',20,0), +('逍遥','男',18,4), +('七年级生','男',19,2) + + +select * into bbsPoint from bbsUsers + +insert into bbsSection values +('技术交流',1), +('读书世界',3), +('生活百科',1), +('八卦区',3) + +insert into bbsTopic values +(2,4,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?',2008-9-10,0), +(3,1,'.NET','与JAVA的区别是什么呀?',2008-9-1,2), +(1,3,'范跑跑','谁是范跑跑',2008-7-8,1) + +insert into bbsReply values +(2,1,'不知道',2008-9-10), +(3,2,'不知道',2008-9-1), +(1,3,'不知道',2008-7-8) + +select * from bbsReply --回帖表 +select * from bbsSection --版块表 +select * from bbsTopic --主帖表 +select * from bbsUsers --用户信息表 + +--在论坛数据库中完成以下题目 +--1.查询出 每个版块 的 版主编号, 版主姓名 和版块名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称 from bbsSection +inner join bbsUsers on bbsUsers.UID = bbsSection.sUid + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号,uName 发帖人姓名,tTItle 帖子的标题 ,tMsg 帖子的内容,tTime 发帖时间 from bbsTopic +inner join bbsUsers on bbsUsers.UID = bbsTopic.tUID where tTime>'2008-9-15' + + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称 from bbsSection +inner join bbsUsers on bbsUsers.UID = bbsSection.sUid where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select rUID 发帖人编号 , uName 发帖人姓名 ,tTItle 主贴标题 , tMsg 主贴内容 , tCount 回复数量 from bbsReply +inner join bbsTopic on bbsTopic.tUID = bbsReply.rUID +inner join bbsUsers on bbsUsers.UID = bbsReply.rUID where tCount=(select max(tCount) from bbsTopic) + +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select tSID 版块 , uName 发帖人姓名 , count(*) from bbsTopic +inner join bbsUsers on bbsUsers.UID = bbsTopic.tUID +inner join bbsSection on bbsUsers.UID =bbsSection.sID group by tSID ,uName diff --git "a/20210326\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery8.sql" "b/20210326\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery8.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b4640b9a407fc8f4a99f79a3a1387709ddac957c --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery8.sql" @@ -0,0 +1,21 @@ +select*from StuIS +select*from Grate + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select StuName 姓名,StuAge 年龄,WExam 笔试成绩,LabExam 机试成绩 from StuIS inner join Grate on StuIS.StuNO=Grate.StuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select WExam,LabExam ,StuIS.StuNO,StuAge from Grate inner join StuIS on StuIS.StuNO=Grate.StuNo Group by WExam,LabExam ,StuAge having WExam>=60 and LabExam>=60 + + +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select count(ExamNO),StuName,WExam,LabExam from Grate inner join StuIS on StuIS.StuNO=Grate.StuNo group by count(ExamNo) + +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select StuName ,StuAge,Grate.WExam,Grate.LabExam from StuIS inner join Grate on StuIS.StuNO=Grate.StuNo where StuAge>=20 + +--5.查询男女生的机试平均分 +select StuSex,avg(LabExam) from StuIS inner join Grate on StuIS.StuNO=Grate.StuNo group by StuSex + +--6.查询男女生的笔试总分 +select StuSex 性别 ,sum(WExam)笔试总分 from StuIS inner join Grate on StuIS.StuNO=Grate.StuNo group by StuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/.keep" "b/20210326\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/20210326\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery9.1.sql" "b/20210326\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery9.1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..afb50fc2a85ed5ae7c1b8fb31e3c7a737b134f7b --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery9.1.sql" @@ -0,0 +1,64 @@ +create database Student +on +( + name='Student', + filename='D:\Student.mdf', + size=10mb, + maxsize=100mb, + filegrowth=15% +) +log on +( + name='Student_log', + filename='D:\Student_log.ldf', + size=10mb, + maxsize=100mb, + filegrowth=15% +) +go + +use Student +go + +create table StuInfo +( + stuNo char(5) primary key(stuNo), + stuName nvarchar(20), + stuAge int, + stuAddress text, + stuSeat int identity(1,1), + stuSex char(1) check(stuSex in(1,0)) +) + +create table StuExam +( + examNo int identity(1,1), + stuNo char(5), + writtenExam int check(writtenExam>=0 and writtenExam<=100), + labExam int check(labExam>=0 and labExam<=100) +) + + alter table StuExam add constraint RK_StuExam_stuNo foreign key(stuNo) references StuInfo(stuNo) + + insert into StuInfo values + ('s2501','张秋利',20,'美国硅谷',1), + ('s2502','李斯文',18,'湖北武汉',0), + ('s2503','马文才',22,'湖南长沙',1), + ('s2504','欧阳俊雄',21,'湖北武汉',0), + ('s2505','梅超风',20,'湖北武汉',1), + ('s2506','陈旋风',19,'美国硅谷',1), + ('s2507','陈风',20,'美国硅谷',0) + insert into StuExam values + ('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) + select * from StuInfo + select * from StuExam +select stuName,stuAge,writtenexam,labexam from StuInfo inner join StuExam on StuInfo.stuNo = StuExam.stuNo +select StuInfo.stuNo,stuName,writtenexam,labexam from StuInfo inner join StuExam on StuInfo.stuNo = StuExam.stuNo where writtenexam>60 and labexam>60 +--.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select StuInfo.stuNo,stuName,writtenexam,labexam from StuInfo left join StuExam on StuInfo.stuNo = StuExam.stuNo +--查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName, stuAge,writtenexam,labexam from StuInfo inner join StuExam on StuInfo.stuNo = StuExam.stuNo where stuAge>=20 order by writtenexam desc +----查询男女生的机试平均分 +select stuSex,AVG(labexam) from StuInfo inner join StuExam on StuInfo.stuNo = StuExam.stuNo group by stuSex +--6.查询男女生的笔试总分 +select stuSex,sum(writtenexam) from StuInfo inner join StuExam on StuInfo.stuNo = StuExam.stuNo group by stuSex diff --git "a/20210326\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery9.2.sql" "b/20210326\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery9.2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..47c83e186c778e832296216b76d07002aa73662a --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery9.2.sql" @@ -0,0 +1,82 @@ +create database paid +on +( + name='paid', + filename='D:\pan\paid.mdf', + size=10mb, + maxsize=100mb, + filegrowth=15% +) +log on +( + name='paid_log', + filename='D:\pan\paid_log.ldf', + size=10mb, + maxsize=100mb, + filegrowth=15% +) +go + +use paid +go +--drop table orderItem +create table orders +( + orderId int primary key, + orderDate date +) + +create table orderItem +( + ItemiD int identity(1,1) primary key, + orderId int references orders(orderId), + itemType varchar(12), + itemName varchar(12), + theNumber int, + theMoney money +) +insert into orders values + (1,'2008-01-12'), + (2,'2008-02-10'), + (3,'2008-02-15'), + (4,'2008-03-10') +insert into orderItem values + (1,'文具','笔',72,2), + (1,'文具','尺',10,1), + (1,'体育用品','篮球',1,56), + (2,'文具','笔',36,2), + (2,'文具','固体胶',20,3), + (2,'日常用品','透明胶',2,1), + (2,'体育用品','羽毛球',20,3), + (3,'文具','订书机',20,3), + (3,'文具','订书针',10,3), + (3,'文具','裁纸刀',5,5), + (4,'文具','笔',20,2), + (4,'文具','信纸',50,1), + (4,'日常用品','毛巾',4,5), + (4,'日常用品','透明胶',30,1), + (4,'体育用品','羽毛球',20,3) + select * from orders + select * from orderItem + --1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + select orderitem.orderId 订单的编号,orderdate 订单日期,itemtype 订购产品的类别,itemname 订购的产品名称,thenumber 订购数量,themoney 订购单价 from orderItem inner join orders on orderItem.orderId = orders.orderId + --2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 + select orderitem.orderId 订单的编号,orderdate 订单日期,itemtype 订购产品的类别,itemname 订购的产品名称 from orderItem inner join orders on orderItem.orderId = orders.orderId where theMoney>50 + --3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + select orderitem.orderId 订单的编号,orderdate 订单日期,itemtype 订购产品的类别,itemname 订购的产品名称,thenumber 订购数量,themoney 订购单价 ,theNumber*theMoney 订购总价 from orderItem inner join orders on orderItem.orderId = orders.orderId + --4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + select orderitem.orderId 订单的编号,orderdate 订单日期,itemtype 订购产品的类别,itemname 订购的产品名称,thenumber 订购数量,themoney 订购单价 ,theNumber*theMoney 订购总价 from orderItem inner join orders on orderItem.orderId = orders.orderId where theMoney>=5 and theMoney>=50 + --5.查询每个订单分别订购了几个产品,例如: + -- 编号 订购产品数 + -- 1 3 + --2 4 + select orderitem.orderId 订单的编号,count(thenumber)订购产品数 from orderItem inner join orders on orderItem.orderId = orders.orderId group by orderitem.orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + -- 1 文具 2 82 + -- 1 体育用品 1 1 + --2 文具 2 56 + --2 体育用品 1 2 + --2 日常用品 1 20 + select orderitem.orderId 订单的编号,itemtype 订购产品的类别,count(itemtype)订购次数 ,sum(thenumber)总数量 from orderItem inner join orders on orderItem.orderId = orders.orderId group by orderitem.orderId,itemtype order by orderitem.orderId asc \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery9.3.sql" "b/20210326\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery9.3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e5e26b4ac2ca0347f0ca604a1ee7739eeb234d66 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery9.3.sql" @@ -0,0 +1,135 @@ +use master +go + +create database bbs +on +( + name='bbs', + filename='D:\SQL\bbs.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10% + +) +, +( + name='bbs2_DATA', + filename='D:\SQL\bbs2_DATA.Ndf', + size=1MB, + maxsize=100MB, + filegrowth=10% +) +log on +( + name='bbs_ldf', + filename='D:\SQL\bbs_log.ldf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) +go + +use bbs +go + +create table bbsUsers +( + UID int primary key identity(1,1), + uName varchar(10) unique not null, + uSex varchar(2) not null check (uSex='男' or uSex='女'), + uAge int not null check(uAge>=15 and uAge<=60), + uPoint int not null check(uPoint>=0) +) +select *from bbsUsers +create table bbsSection +( + sID int identity(1,1) primary key, + sName varchar(10) not null, + sUid int foreign key references bbsUsers(UID) +) + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int foreign key references bbsUsers(UID), + tSID int foreign key references bbsSection(sID), + tTitle varchar(100) not null, + rMsg text not null, + rTime datetime , + tCount int +) + + +create table bbsReply +( + rID int primary key identity(1,1), + rUID INT foreign key references bbsUsers(UID), + rTID INT foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) +select * from bbsUsers +select * from bbsSection +select *from bbsTopic +select * from bbsReply + +insert into bbsUsers(uName, uSex,uAge,uPoint) +values ('小雨点','女',20,0) +insert into bbsUsers(uName, uSex,uAge,uPoint) +values ('逍遥','男',18,4) +insert into bbsUsers(uName, uSex,uAge,uPoint) +values ('七年级生','男',19,2) + +insert into bbsSection(sName , sUid) +values('技术交流',1) +insert into bbsSection(sName , sUid) +values('读书世界',3) +insert into bbsSection(sName , sUid) +values('生活百科',1) +insert into bbsSection(sName , sUid) +values('八卦区',3) + +insert into bbsTopic(tUID ,tSID,tTitle,rMsg,rTime,tCount) +values (2,5,'范跑跑','谁是范跑跑','2008-7-8',1) +insert into bbsTopic(tUID ,tSID,tTitle,rMsg,rTime,tCount) +values (3,2,'.NET','与JAVA的区别是什么','2008-9-1',2) +insert into bbsTopic(tUID ,tSID,tTitle,rMsg,rTime,tCount) +values (1,4,'今年夏天流行什么','有谁知道今年夏天流行什么呀?','2008-9-10',0) + +insert into bbsReply(rUID,rTID,rMsg,rTime) +values(1,3,'范跑跑','2008-10-2') +insert into bbsReply(rUID,rTID,rMsg,rTime) +values(2,2,'区别不大','2008-10-2') +insert into bbsReply(rUID,rTID,rMsg,rTime) +values(3,1,'蓝色','2008-10-2') + +select uName,uPoint into bbsPoint from bbsUsers --备份uName uPoint 到新表bbsPoint + +alter table bbsTopic drop constraint FK__bbsTopic__tUID__2D27B809 +alter table bbsReply drop constraint FK__bbsReply__rUID__30F848ED +delete from bbsUsers where uName='逍遥' +update bbsUsers set upoint=10 where uName='小雨点' + +alter table bbsTopic drop FK__bbsTopic__tSID__2E1BDC42 +delete bbsSection where sName='生活百科' + +truncate table bbsReply + + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的 UID版主编号,uName版主姓名和 sName版块名称 +select UID 版主编号,uName 版主姓名,sName 版块名称 from bbsUsers +inner join bbsSection on bbsUsers.UID = bbsSection.sUId +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号,uName 发帖人姓名,ttitle 帖子的标题,rMsg 帖子的内容,rTime 发帖时间 from bbsTopic +inner join bbsUsers on bbsUsers.UID = bbsTopic.tUID where rTime>'2008-9-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select UID 版主编号,uName 版主姓名,sName 版块名称 from bbsUsers +inner join bbsSection on bbsUsers.UID = bbsSection.sUId where uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select tUID 发帖人编号,uName 发帖人姓名,ttitle 主贴标题,rMsg 主贴内容,tcount 回复数量 from bbsTopic +inner join bbsUsers on bbsUsers.UID = bbsTopic.tUID where tCount = (select max(tCount) from bbsTopic) +--5.在主贴表中查询 每个版块中 每个用户的发帖总数 +select bbsSection.sName 板块名称,uID 发帖人编号,SUM(tID)发帖总数 from bbsTopic +right join bbsSection on sID=tSID +left join bbsUsers on uID=tUID group by bbsSection.sName,uID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..11d0289d496b5eb21c701098da3192fb5364b664 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" @@ -0,0 +1,56 @@ +/** +* 之前的作业1 +* 建表 +**/ + +use master +create database Student +go + +use Student +create table StuInfo +( + stuNO char(5) primary key, + stuName nchar(5), + stuAge int, + stuAddress nchar(50), + stuSeat int identity(1,1), + stuSex char(1) check(stuSex = '1' or stuSex = '0') +) +go + +use Student +create table StuExam +( + examNO int identity(1,1) primary key, + stuNO char(5) constraint FK_stuInfo_stuNO references StuInfo(stuNO), + writtenExam int check(writtenExam >= 0 and writtenExam <= 100), + labExam int check(labExam >= 0 and labExam <= 100) +) +go + +use Student +insert into StuInfo(stuNO,stuName,stuAge,stuAddress,stuSex) values ('s2501','张秋利',20,'美国硅谷','1'), +('s2502','李斯文',18,'湖北武汉','0'),('s2503','马文才',22,'湖南长沙','1'),('s2504','欧阳俊雄',21,'湖北武汉','0'), +('s2505','梅超风',20,'湖北武汉','1'),('s2506','陈旋风',19,'美国硅谷','1'),('s2507','陈风',20,'美国硅谷','0') +go + +use Student +insert into StuExam(stuNO,writtenExam,labExam) values ('s2501','50','70'),('s2502','60','65'),('s2503','86','65'), +('s2504','40','85'),('s2505','70','90'),('s2506','85','90') +go + +use Student +go +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName,stuAge,writtenExam,labExam from StuInfo SI inner join StuExam SE on SI.stuNO = SE.stuNO +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select stuName,stuAge,writtenExam,labExam from StuInfo SI inner join StuExam SE on SI.stuNO = SE.stuNO where labExam > 60 and writtenExam > 60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select SI.stuNO,stuName,writtenExam,labExam from StuInfo SI left join StuExam SE on SI.stuNO = SE.stuNO +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select SI.stuNO,stuName,writtenExam,labExam from StuInfo SI left join StuExam SE on SI.stuNO = SE.stuNO where stuAge >= 20 order by writtenExam desc +--5.查询男女生的机试平均分 +select stuSex,AVG(labExam) 平均分 from StuExam SE left join StuInfo SI on SE.stuNO = SI.stuNO group by stuSex +--6.查询男女生的笔试总分 +select stuSex,SUM(writtenExam) 总分 from StuExam SE left join StuInfo SI on SE.stuNO = SI.stuNO group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ce98944682968177dc2061b4509e65b5f716034e --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" @@ -0,0 +1,65 @@ +/** +* 作业2 +**/ + +use master +go + +create database store +go + +use store +go + +create table orders +( + orderID int primary key identity(1,1), + orderDate datetime +) +go + +create table orderItem +( + itemID int primary key identity, + orderID int references orders(orderID), + itemType nchar(5), + itemName nchar(20), + theNumber int, + theMoney int +) + +insert into orders values ('2008-01-12'),('2008-02-10'),('2008-02-15'),('2008-3-10') +go +insert into orderItem values (1,'文具','笔',72,2),(1,'文具','尺',10,1),(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2),(2,'文具','固体胶',20,3),(2,'日常用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3),(3,'文具','订书机',20,3),(3,'文具','订书针',10,3), +(3,'文具','裁纸刀',5,5),(4,'文具','笔',20,20),(4,'文具','信纸',50,1),(4,'日常用品','毛巾',4,5), +(4,'日常用品','透明胶',30,1),(4,'体育用品','羽毛球',20,3) +go + +use store +go +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select O.orderID,orderDate,itemType,itemName,theNumber,theMoney from orders O left join orderItem OI on O.orderID = OI.orderID +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select O.orderID,orderDate,itemType,itemName from orders O left join orderItem OI on O.orderID = OI.orderID where theNumber > 50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select O.orderID,orderDate,itemType,itemName,theNumber,theMoney,theMoney * theNumber total from orders O left join orderItem OI on O.orderID = OI.orderID +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select O.orderID,orderDate,itemType,itemName,theNumber,theMoney,theMoney * theNumber total from orders O left join orderItem OI on O.orderID = OI.orderID where theMoney >=5 and theNumber >= 50 +/*5.查询每个订单分别订购了几个产品,例如: + 编号 订购产品数 + 1 3 + 2 4*/ +select O.orderID,SUM(theNumber)订购总数 from orders O left join orderItem OI on O.orderID = OI.orderID group by O.orderID + +/*6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + + 订单编号 产品类别 订购次数 总数量 + + 1 文具 2 82 + 1 体育用品 1 1 + 2 文具 2 56 + 2 体育用品 1 2 + 2 日常用品 1 20*/ +select O.orderID 订单编号,itemType 产品类别,COUNT(itemType)订购次数, SUM(theNumber)订购总数 from orders O inner join orderItem OI on O.orderID = OI.orderID group by O.orderID,itemType order by O.orderID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9398561bb42a7326794f31f3e570134186d71d4d --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery3.sql" @@ -0,0 +1,135 @@ +/** +* 作业3 +* 之前的作业,因为要套用 +**/ +use master +go +create database bbs +on primary +( + name = 'bbs', + filename = 'D:\Document\MSSQLDatabase\bbs\bbs.mdf',--没有E盘,就丢这了 + size = 5MB, + Maxsize = 50MB, + filegrowth = 1MB +) +log on +( + name = 'bbs_log', + filename = 'D:\Document\MSSQLDatabase\bbs\bbs_log.ldf',--没有E盘,就丢这了 + size = 1MB, + Maxsize = 10MB, + filegrowth = 10% +) +go + +use bbs +go +create table bbsUsers +( + UID int identity(1,1), + uName varchar(10), + uSex varchar(2), + uAge int, + uPoint int +) +goto altBbsUsers +bbsTopic: + create table bbsTopic + ( + tID int primary key identity(1,1), + tUID int constraint FK_bbsTopic_UID references bbsUsers(UID), + tSID int constraint FK_bbsSection_sID references bbsSection(sID), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int + ) + goto bbsReply +bbsReply: + create table bbsReply + ( + rID int primary key identity(1,1), + rUID int constraint FK_bbsReply_UID foreign key references bbsUsers(UID), + rTID int constraint FK_bbsSection_tID references bbsTopic(tID), + rMsg text, + rTime datetime + ) + goto endCre +bbsSection: + create table bbsSection + ( + sID int identity(1,1), + sName varchar(10), + sUid int + ) + goto altBbsSection + +altBbsUsers: + alter table bbsUsers add constraint PK_UID primary key(UID) + alter table bbsUsers add constraint UQ_uName unique(uName) + alter table bbsUsers alter column uSex varchar(2) not null + alter table bbsUsers add constraint CK_uSex check(uSex = '男' or uSex = '女') + alter table bbsUsers add constraint DF_uSex default('男') for uSex + alter table bbsUsers add constraint CK_uAge check(uAge between 15 and 60) + alter table bbsUsers add constraint CK_uPoint check(uPoint >= 0) + goto bbsSection +altBbsSection: + alter table bbsSection add constraint PK_sID primary key(sID) + alter table bbsSection alter column sName varchar(10) not null + alter table bbsUsers add constraint FK_bbsUsers_UID foreign key(UID) references bbsUsers(UID) + goto bbsTopic + +endCre: + goto insTable + +insTable: + use bbs + insert into bbsUsers(uName,uSex,uAge,uPoint) values ('小雨点','女',20,0), + ('逍遥','男',18,4),('七年级生','男',19,2)--小雨点 1 逍遥 2 , 七年生 3 + goto buBbsPoint +buBbsPoint: + select uName,uPoint into bbsPoint from bbsUsers + goto startSection +startSection: + insert into bbsSection(sName,sUid) values ('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + goto addReply +addReply: + use bbs + insert into bbsTopic(tUID,tSID,tTitle,tMSG,tTime,tCount) values (2,4,'范跑跑','谁是范跑跑','2008-07-08',1), + (3,1,'.NET','与JAVA的区别是什么啊','2008-09-01',2), + (2,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-09-10',0) + insert into bbsReply(rUID,rTID,rMsg,rTime) values (1,1,'是傻逼',getdate()), + (2,2,'平台和语言的区别',getdate()) + goto delUser +delUser: + alter table bbsReply drop constraint FK_bbsReply_UID + alter table bbsTopic drop constraint FK_bbsTopic_UID + delete from bbsUsers where uID = 2 + goto addPoint +addPoint: + insert into bbsPoint values ('小雨点',10) + update bbsUsers set uPoint += 10 where uName = '小雨点' + goto delSections +delSections: + alter table bbsTopic drop constraint FK_bbsSection_sID + delete from bbsSection where sID = 3 + goto cleReply +cleReply: + delete from bbsReply + go + + +--在论坛数据库中完成以下题目 +use bbs +go +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select UID,uName,sName from bbsSection left join bbsUsers on sUid = UID +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select UID,uName,tTitle,tMsg,tTime from bbsTopic inner join bbsUsers on tUID = UID where tTime > '2008-9-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select UID,uName,sName from bbsSection inner join bbsUsers on sUid = UID where uAge < 20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select UID,uName,tTitle,tMsg,tCount from bbsUsers inner join bbsTopic on UID = tUID where tCount in (select MAX(tCount) from bbsTopic) +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select UID,uName,sName,COUNT(tID)发帖总数 from bbsTopic left join bbsSection on tUID = sUid left join bbsUsers on sUID = UID group by UID,sID,sName,uName diff --git "a/20210326\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQL1.sql" "b/20210326\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQL1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f377f0181dfef50a93c403528114209938ac0ba9 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQL1.sql" @@ -0,0 +1,98 @@ +use master +go +create database STU +on +( +name='STU', +filename='D:\sql\STU.mdf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +log on +( +name='STU_log', +filename='D:\sql\STU_log.ldf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +go +use STU +go +create table StuInfo +( +StuNO nvarchar(10) unique not null, +StuName nvarchar(10) not null, +StuAge int not null, +StuAddress nvarchar(200) , +StuSeat nvarchar(8) not null, +StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女') +) + +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2501','张秋利','20','美国硅谷','1','女') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2502','李斯文','18','湖北武汉','2','男') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2503','马文才','22','湖南长沙','3','男') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2504','欧阳俊雄','21','湖北武汉','4','女') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2505','梅超风','20','湖北武汉','5','女') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2506','陈旋风','19','美国硅谷','6','女') +insert into StuInfo(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2507','陈风','20','美国硅谷','7','女') + +create table Chengji +( +ExamNo int primary key identity(1,1), +StuNo nvarchar(10) references StuInfo(StuNO), +WittenExam int not null, +LabExam int not null +) +insert into Chengji(StuNO,WittenExam,LabExam) +values('s2501','50','70') +insert into Chengji(StuNO,WittenExam,LabExam) +values('s2502','60','65') +insert into Chengji(StuNO,WittenExam,LabExam) +values('s2503','86','85') +insert into Chengji(StuNO,WittenExam,LabExam) +values('s2504','40','80') +insert into Chengji(StuNO,WittenExam,LabExam) +values('s2505','70','90') +insert into Chengji(StuNO,WittenExam,LabExam) +values('s2506','85','90') + + +--练习1 +--数据如图片1,使用上次作业的数据 +select * from StuInfo +select * from Chengji +--1.查询学生的姓名(StuName),年龄(StuAge),笔试成绩(WittenExam)和机试成绩(LabExam) +select StuInfo.StuNO,StuInfo.StuName 姓名,StuInfo.StuAge 年龄,Chengji.WittenExam 笔试成绩,Chengji.LabExam 机试成绩 +from StuInfo inner join Chengji on StuInfo.StuNO=Chengji.StuNo + +--2.查询笔试和机试成绩都在60分以上的学生的学号(StuNO),姓名(StuName),笔试成绩(WittenExam)和机试成绩(LabExam) +select StuInfo.StuNO 学号,StuInfo.StuName 姓名,Chengji.WittenExam 笔试成绩,Chengji.LabExam 机试成绩 +from StuInfo inner join Chengji on StuInfo.StuNO=Chengji.StuNo +where Chengji.WittenExam>60 and Chengji.LabExam>60 + +--3.查询所有学生的学号(StuNO),姓名(StuName),笔试成绩(WittenExam),机试成绩(LabExam),没有参加考试的学生的成绩以NULL值填充 +select StuInfo.StuNO 学号,StuInfo.StuName 姓名,Chengji.WittenExam 笔试成绩,Chengji.LabExam 机试成绩 +from StuInfo left join Chengji on StuInfo.StuNO=Chengji.StuNo + +--4.查询年龄(StuAge)在20以上(包括20)的学生的姓名(StuName),年龄(StuAge),笔试成绩(WittenExam)和机试成绩(LabExam),并按笔试成绩降序排列 +select StuInfo.StuNO 学号,StuInfo.StuName 姓名,StuInfo.StuAge 年龄,Chengji.WittenExam 笔试成绩,Chengji.LabExam 机试成绩 +from StuInfo inner join Chengji on StuInfo.StuNO=Chengji.StuNo +where StuInfo.StuAge>=20 order by WittenExam DESC + +--5.查询 男女生的 机试(LabExam)平均分 +select StuInfo.StuSex 性别, avg(LabExam) 平均分 +from StuInfo inner join Chengji on StuInfo.StuNO=Chengji.StuNo +group by StuSex +--6.查询男女生的笔试(WittenExam)总分 +select StuInfo.StuSex 性别, sum(WittenExam) 总分 +from StuInfo inner join Chengji on StuInfo.StuNO=Chengji.StuNo +group by StuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQL2.sql" "b/20210326\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQL2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..46a6d2be0734cbd1e7e771bc375936b316a8ca3e --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQL2.sql" @@ -0,0 +1,104 @@ +use master +go +create database ORD +on +( +name='ORD', +filename='D:\sql\ORD.mdf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +log on +( +name='ORD_log', +filename='D:\sql\ORD_log.ldf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +go +use ORD +go +create table Orders +( +OrderID int primary key not null, +OrderDate datetime +) +go +use ORD +go +create table Orderltem +( +ItemID int primary key not null, +OrderID int references Orders(OrderID) not null, +ItemType varchar(8) not null, +ItemName varchar(6) not null, +TheNumber int not null, +TheMoney int not null +) +go +insert into Orders (OrderID,OrderDate) values(1,'2008-01-12') +insert into Orders (OrderID,OrderDate) values(2,'2008-02-10') +insert into Orders (OrderID,OrderDate) values(3,'2008-02-15') +insert into Orders (OrderID,OrderDate) values(4,'2008-03-10') + +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(1,1,'文具','笔','72','2') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(2,1,'文具','尺','10','1') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(3,1,'体育用具','篮球','1','56') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(4,2,'文具','笔','36','2') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(5,2,'文具','固体胶','20','3') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(6,2,'日常用品','透明胶','2','1') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(7,2,'体育用品','羽毛球','20','3') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(8,3,'文具','订书机','20','3') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(9,3,'文具','订书机','10','3') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(10,3,'文具','裁纸刀','5','5') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(11,4,'文具','笔','20','2') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(12,4,'文具','信纸','50','1' ) +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(13,4,'日常用品','毛巾','4','5') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(14,4,'日常用品','透明胶','30','1') +insert into Orderltem (ItemID,OrderID,ItemType,ItemName,TheNumber,TheMoney) values(15,4,'体育用品','羽毛球','20','3') + + +--练习2 +--使用上次作业的订单数据库,完成下列题目: +select * from Orders +select * from Orderltem +--1.查询所有的订单的订单的编号(OrderID),订单日期(OrderDate),订购产品的类别(ItemType)和订购的产品名称(ItemName),订购数量(TheNumber)和订购单价(TheMoney) +select Orders.OrderID 编号,Orders.OrderDate 日期,Orderltem.ItemType 类别,Orderltem.ItemName 名称,Orderltem.TheNumber 数量,Orderltem.TheMoney 单价 +from Orders inner join Orderltem on Orders.OrderID=Orderltem.OrderID + +--2.查询订购数量(TheNumber)大于50的订单的编号(OrderID),订单日期(OrderDate),订购产品的类别(ItemType)和订购的产品名称(ItemName) +select Orders.OrderID 编号,Orders.OrderDate 日期,Orderltem.ItemType 类别,Orderltem.ItemName 名称,Orderltem.TheNumber 数量 +from Orders inner join Orderltem on Orders.OrderID=Orderltem.OrderID +where Orderltem.TheNumber>50 + +--3.查询所有的订单的订单的编号(OrderID),订单日期(OrderDate),订购产品的类别(ItemType)和订购的产品名称(ItemName),订购数量(TheNumber)和订购单价(TheMoney)以及订购总价 +select Orders.OrderID 订单的编号,Orders.OrderDate 订单日期,Orderltem.ItemType 产品的类别,Orderltem.ItemName 产品名称,Orderltem.TheNumber 订购数量,Orderltem.TheMoney 订购单价, sum(Orderltem.TheNumber*Orderltem.TheMoney) 订购总价 +from Orders inner join Orderltem on Orders.OrderID=Orderltem.OrderID +group by Orders.OrderID,OrderDate,ItemType,ItemName,TheNumber,TheMoney + +--4.查询单价(TheMoney)大于等于5 并且 数量(TheNumber)大于等于50的订单 的订单的编号(OrderID),订单日期(OrderDate),订购产品的类别(ItemType)和订购的产品名称(ItemName),订购数量(TheNumber)和订购单价(TheMoney)以及订购总价 +select Orders.OrderID 订单编号,Orders.OrderDate 订单日期,Orderltem.ItemType 类别,Orderltem.ItemName 产品名称,Orderltem.TheNumber 订购数量,Orderltem.TheMoney 订购单价, sum(Orderltem.TheNumber*Orderltem.TheMoney) 总价 +from Orders inner join Orderltem on Orders.OrderID=Orderltem.OrderID +where Orderltem.TheMoney>=5 and Orderltem.TheNumber>=50 +group by Orders.OrderID,OrderDate,ItemType,ItemName,TheNumber,TheMoney + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号(OrderID) 订购产品数 +-- 1 3 +-- 2 4 +select Orders.OrderID 编号,count(*)订购产品数 +from Orderltem inner join Orders on Orders.OrderID=Orderltem.OrderID +group by Orders.OrderID + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +-- 订单编号(OrderID) 产品类别(ItemType) 订购次数 总数量 +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select Orders.OrderID 编号,Orderltem.ItemType 类别,count(*)订购次数,sum(TheNumber) 总数量 +from Orderltem inner join Orders on Orders.OrderID=Orderltem.OrderID +group by Orders.OrderID,ItemType diff --git "a/20210326\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQL\344\275\234\344\270\2323.sql" "b/20210326\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQL\344\275\234\344\270\2323.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c73660c5c0d701b88e2520f8c57c1783934622f2 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQL\344\275\234\344\270\2323.sql" @@ -0,0 +1,120 @@ +use master +go +create database BBS +on +( +name='BBS', +filename='D:\sql\BBS.mdf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +log on +( +name='BBS_log', +filename='D:\sql\BBS_log.ldf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +go +use BBS +go +create table BBSUsers--用户信息表 +( +UID int not null, +UName varchar(10) not null, +USex varchar(2) not null, +UAge int not null, +UPoint int not null +) + +alter table BBSUsers add constraint PK_BBSUsers_UID primary key(UID) +alter table BBSUsers add constraint UK_BBSUsers_UName unique(UName) +alter table BBSUsers add constraint CK_BBSUsers_USex check(USex='男' or USex='女') +alter table BBSUsers add constraint CK_BBSUsers_UAge check(UAge>=15 and UAge<=60) +alter table BBSUsers add constraint CK_BBSUsers_UPoint check(UPoint>=0) + +go +use BBS +go +create table BBSSection--版块表 +( +SID int not null, +SName varchar(10) not null, +SUID int not null +) +alter table BBSSection add constraint PK_BBSSection_SID primary key(SID) +alter table BBSSection add constraint FK_BBSSection_SUID foreign key (SUID) references BBSUsers(UID) + +go +use BBS +go +create table BBSTopic--主贴表 +( +TID int primary key identity(1,1) not null, +TUID int references BBSUsers(UID) not null, +TSID int references BBSSection(SID) not null, +TTitle varchar(100) not null, +TMsg text not null, +TTime datetime , +Tcount int , +) + +go +use BBS +go +create table BBSReply--回帖表 +( +RID int primary key identity(1,1) not null, +RUID int references BBSUsers(UID) not null, +RTID int references BBSTopic(TID) not null, +RMsg text , +RTime datetime , +) +insert into BBSUsers (UID,UName , USex , UAge , UPoint) values(1,'小雨点','女','20','0') +insert into BBSUsers (UID,UName , USex , UAge , UPoint) values(2,'逍遥','男','18','4') +insert into BBSUsers (UID,UName , USex , UAge , UPoint) values(3,'七年级生','男','19','2') + +select UName,UPoint into BBSPoint from BBSUsers + +insert into BBSSection (SID,SName,SUID) values(4,'技术交流',1) +insert into BBSSection (SID,SName,SUID) values(5,'读书世界',3) +insert into BBSSection (SID,SName,SUID) values(6,'生活八卦',1) +insert into BBSSection (SID,SName,SUID) values(7,'八卦区',3) + +insert into BBSTopic (TUID,TSID,TTitle,TMsg,TTime,Tcount) values(2,7,'范跑跑','谁是范跑跑','2008-7-8',1) +insert into BBSTopic (TUID,TSID,TTitle,TMsg,TTime,Tcount) values(3,4,'.NET','与JAVA的区别是什么','2008-9-1',2) +insert into BBSTopic (TUID,TSID,TTitle,TMsg,TTime,Tcount) values(1,6,'今年夏天最流行什么','有谁知道今年夏天最流行什么','2008-9-10',0) + +insert into BBSReply (RUID,RMsg,RTime,RTID) values(1,'范跑跑是什么人','2008-10-1',1) +insert into BBSReply (RUID,RMsg,RTime,RTID) values(2,'它们的区别是什么','2008-11-1',2) +insert into BBSReply (RUID,RMsg,RTime,RTID) values(3,'最流行什么','2008-11-20',3) + + +--练习3 +select * from BBsReply --回帖表 +select * from BBsSection --版块表 +select * from BBsTopic --主帖表 +select * from BBsUsers --用户信息表 + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select SID 版主编号,UName 版主姓名,SName 版块名称 FROM BBsSection +left join BBsUsers on SID=UID +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +SELECT TUID 发帖人编号,UName 发帖人姓名,TTitle 帖子的标题,TMsg 帖子的内容,TTime 发帖时间 from BBsTopic +left join BBsUsers ON UID=TID where TTime>'2008-9-15' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select SID 版主的编号,UName 版主的名称,SName 版块的名称,UAge 年龄 from BBsSection +left join BBsUsers on SID=UID where UName<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 TUID 编号,UName 发帖人姓名,TTitle 主贴标题,TMsg 主贴内容, +Tcount 回复数量 from BBsTopic left join BBsUsers on UID = TID ORDER BY Tcount desc + +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select UName 用户名, SName 板块名, count(*) 发帖总数 from BBsTopic +inner join BBsUsers ON TUID=UID +LEFT JOIN BBsSection ON TID = SID GROUP BY UName,SName diff --git "a/20210326\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2401.sql" "b/20210326\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0f538c0286bd1bed4d2c0b530dc219943d6dd689 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2401.sql" @@ -0,0 +1,66 @@ +create database Student +on +( + name='Student', + filename='D:\SQL\Student.mdf' +) +log on +( + name='Student_log', + filename='D:\SQL\Student_log.ldf' +) +go + +create table StuInfo +( + stuNo char(5) primary key(stuNo), + stuName nvarchar(20), + stuAge int, + stuAddress text, + stuSeat int identity(1,1), + stuSex char(1) check(stuSex in(1,0)) +) + +create table StuExam +( + examNo int identity(1,1), + stuNo char(5), + writtenExam int check(writtenExam>=0 and writtenExam<=100), + labExam int check(labExam>=0 and labExam<=100) +) + + alter table StuExam add constraint RK_StuExam_stuNo foreign key(stuNo) references StuInfo(stuNo) + + insert into StuInfo values + ('s2501','张秋利',20,'美国硅谷',1), + ('s2502','李斯文',18,'湖北武汉',0), + ('s2503','马文才',22,'湖南长沙',1), + ('s2504','欧阳俊雄',21,'湖北武汉',0), + ('s2505','梅超风',20,'湖北武汉',1), + ('s2506','陈旋风',19,'美国硅谷',1), + ('s2507','陈风',20,'美国硅谷',0) + insert into StuExam values + ('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) + +--数据如图片1,使用上次作业的数据 + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 +from StuInfo i inner join StuExam e on i.stuNo=e.stuNo +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select i.stuNo 学号,stuName 姓名,writtenExam 笔试成绩,labExam 机试成绩 from StuInfo i left join StuExam e +on i.stuNo=e.stuNo where writtenExam>60 and labExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 +from StuInfo i left join StuExam e on i.stuNo=e.stuNo +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 +from StuInfo i inner join StuExam e on i.stuNo=e.stuNo +where stuAge>=20 order by writtenExam DESC +--5.查询男女生的机试平均分 +select stuSex 性别,avg(labExam) 机试成绩 +from StuInfo i inner join StuExam e on i.stuNo=e.stuNo +where stuAge>=20 group by stuSex +--6.查询男女生的笔试总分 +select stuSex 性别,sum(writtenExam) 笔试成绩 +from StuInfo i inner join StuExam e on i.stuNo=e.stuNo group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2402.sql" "b/20210326\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2402.sql" new file mode 100644 index 0000000000000000000000000000000000000000..59a3de16b44f20ac63c522959bb995cca254b942 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2402.sql" @@ -0,0 +1,84 @@ +create database Assignment +on +( + name='Assignment', + filename='D:\SQL\Assignment.mdf' +) +log on +( + name='Assignment_log', + filename='D:\SQL\Assignment_log.ldf' +) +go + +use Assignment +go +--drop table orderItem +create table orders +( + orderId int primary key, + orderDate date +) + +create table orderItem +( + ItemiD int identity(1,1) primary key, + orderId int references orders(orderId), + itemType varchar(12), + itemName varchar(12), + theNumber int, + theMoney money +) +insert into orders values + (1,'2008-01-12'), + (2,'2008-02-10'), + (3,'2008-02-15'), + (4,'2008-03-10') +insert into orderItem values + (1,'文具','笔',72,2), + (1,'文具','尺',10,1), + (1,'体育用品','篮球',1,56), + (2,'文具','笔',36,2), + (2,'文具','固体胶',20,3), + (2,'日常用品','透明胶',2,1), + (2,'体育用品','羽毛球',20,3), + (3,'文具','订书机',20,3), + (3,'文具','订书针',10,3), + (3,'文具','裁纸刀',5,5), + (4,'文具','笔',20,2), + (4,'文具','信纸',50,1), + (4,'日常用品','毛巾',4,5), + (4,'日常用品','透明胶',30,1), + (4,'体育用品','羽毛球',20,3) +-- 使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select o.orderId 订单编号,orderDate 订单日期,itemType 订购类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价 +from orders o inner join orderItem i on o.orderId=i.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select o.orderId 订单编号,orderDate 订单日期,itemType 订购类别,itemName 产品名称 +from orders o inner join orderItem i on o.orderId=i.orderId +where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select o.orderId 订单编号,orderDate 订单日期,itemType 订购类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 +from orders o inner join orderItem i on o.orderId=i.orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select o.orderId 订单编号,orderDate 订单日期,itemType 订购类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 +from orders o inner join orderItem i on o.orderId=i.orderId +where theMoney>=5 and theNumber>=50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select o.orderId 订单编号,count(*) from orders o inner join orderItem i on o.orderId=i.orderId group by o.orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select o.orderId 订单编号,itemType 订购类别,count(*) 订购次数,sum(theNumber) 总数量 +from orders o inner join orderItem i on o.orderId=i.orderId group by o.orderId,itemType order by o.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2403.sql" "b/20210326\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2403.sql" new file mode 100644 index 0000000000000000000000000000000000000000..46670cb1fa882f96c162678b21fddf5adeb5dfcf --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2403.sql" @@ -0,0 +1,20 @@ +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称 +from bbsSection bs inner join bbsUsers bu +on sUid=bu.UID +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 发帖人编号,uName 发帖人姓名,tTitle 贴子的标题,tMsg 帖子的内容,tTime 发帖时间 +from bbsTopic bs inner join bbsUsers bu +on tUID=bu.UID where tTime>'2008-9-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sUid 版主编号,uName 版主姓名,sName 版块名称 +from bbsSection bs inner join bbsUsers bu +on sUid=bu.UID where uAge<20 +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select tUID 发帖人编号,uName 发帖人姓名,tTitle 贴子的标题,tMsg 帖子的内容,tCount 回复数量 +from bbsTopic bs inner join bbsUsers bu +on tUID=bu.UID where tCount=(select max(tCount) from bbsTopic) +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select sName,uName 用户名,count(tUID ) 发帖总数 +from bbsSection S left join bbsUsers U on sUid =UID left join bbsTopic T on sID =tUID group by sName,uName diff --git "a/20210326\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..faecf51edf3e729d61088f35a43c6d20010ffdd0 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" @@ -0,0 +1,72 @@ +use master +go + +create database Student2 +on +( + name='Student2', + filename='D:\meiguo\Student2.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='Student2_log', + filename='D:\meiguo\Student2_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use Student2 +go +create table StuInfo +( + StuNo varchar(20) primary key , + StuName nchar(20) not null, + StuAge int not null, + StuAdress varchar(20) not null, + StuSeat int not null, + StuSex char(2) not null +) +go + +insert into StuInfo(StuNo,StuName,StuAge,StuAdress,StuSeat,StuSex) values +('s2501','张秋利',20,'美国硅谷',1,1),('s2502','李斯文',18,'湖北武汉',2,0), +('s2503','马文才',22,'湖南长沙',3,1),('s2504','欧阳俊雄',21,'湖北武汉',4,0), +('s2505','梅超风',20,'湖北武汉',5,1),('s2506','陈旋风',19,'美国硅谷',6,1), +('s2507','陈风',20,'美国硅谷',7,0),('s2508','张三',20,'伊拉克',8,0) +go + +use Student2 +go +create table Score +( + ExamNo int primary key identity(1,1), + StuNO varchar(20) not null, + WrittenExam int not null, + LabExam int not null +) +go + +insert into Score(StuNo,WrittenExam,LabExam) values +('s2501',50,70),('s2502',60,65),('s2503',86,85), +('s2504',40,80),('s2505',70,90),('s2506',85,90), +('s2507','',''),('s2508','','') +select * from Score +select * from StuInfo +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select Stuname 姓名,StuAge 年龄,WrittenExam 笔试成绩,LabExam 机试成绩 from StuInfo inner join Score on StuInfo.StuNo=Score.StuNo +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuInfo.StuNo 学号 ,StuName 姓名, WrittenExam 笔试成绩,LabExam 机试成绩 from Score inner join StuInfo on StuInfo.StuNo=Score.StuNo where WrittenExam>60 and LabExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以0值填充 +select StuInfo.StuNo 学号 ,StuName 姓名, WrittenExam 笔试成绩,LabExam 机试成绩 from StuInfo inner join Score on StuInfo.StuNo=Score.StuNo where WrittenExam=0 and LabExam=0 +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select StuName 姓名 ,StuAge 年龄, WrittenExam 笔试成绩,LabExam 机试成绩 from StuInfo inner join Score on StuInfo.StuNo=Score.StuNo where StuAge>=20 order by WrittenExam +--5.查询男女生的机试平均分 +select StuSex 性别, avg(LabExam)机试平均分 from StuInfo inner join Score on StuInfo.StuNo=Score.StuNo group by StuSex +--6.查询男女生的笔试总分 +select StuSex 性别, sum(WrittenExam)笔试总分 from StuInfo inner join Score on StuInfo.StuNo=Score.StuNo group by StuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..25674f50b415d935e0a949068f5e467d79eed669 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery2.sql" @@ -0,0 +1,75 @@ +use master +go +create database market +on( + name=' market', + filename='D:\bank\ market.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on( + name=' market_log', + filename='D:\bank\ market_log.ldf', + size=5, + maxsize=100, + filegrowth=10% +) +USE market +go +create table orders +( + orderId int primary key identity not null, + orderDate datetime not null, +) +create table orderItem +( + ItemiD int primary key identity not null, + orderId int references orders(orderId) not null, + itemType nvarchar(10) not null, + itemName nvarchar(10) not null, + theNumber int not null, + theMoney int not null, +) + +insert into orders values('2008-01-12'),('2008-02-10'),('2008-02-15'),('2008-03-10') +insert into orderItem values +(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(2,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'日常用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3), +(3,'文具','订书机',20,3), +(3,'文具','订书针',10,3), +(3,'文具','裁纸刀',5,5), +(4,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'日常用品','毛巾',4,5), +(4,'日常用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) +select * from orders +select * from orderItem +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney from orders inner join orderItem on orders.orderId=orderItem.orderid +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orderItem.orderId,orderDate,itemType,itemName from orders inner join orderItem on orders.orderId=orderItem.orderid where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及 订购总价 +select orderItem.orderId,orderDate,itemType,itemName,theNumber,theMoney,theNumber*theMoney 订购总价 from orderItem inner join orders on orderItem.orderId=orders.orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select from orders inner join orderItem on orderItem.orderId=orders.orderId where theMoney>=5 and theNumber>=50 +--5.查询每个订单分别订购了几个产品,例如: + -- 编号 订购产品数 + -- 1 3 + -- 2 4 +select orderId 编号 ,count(*) 次数 from orderItem group by orderId +--6.查询每个订单里的每个类别的产品 分别订购了几次 和 总数量,例如: + + --订单编号 产品类别 订购次数 总数量 + -- 1 文具 2 82 + -- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select orderId 编号,itemType,count(*)订购次数,sum(theNumber)总数量 from orderItem group by orderId ,itemType order by orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery3.sql" "b/20210326\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d7d532a9cc1c69d5d7723a4b1658faaa32722859 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery3.sql" @@ -0,0 +1,101 @@ +create database bbs + +on +( + name='bbs', + filename='D:\bbs.mdf', + size=10mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf', + size=10mb, + maxsize=100mb, + filegrowth=10mb +) +go +use bbs +go +create table bbsUsers +( + UIDD int identity, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +create table bbsSection +( + sIDD int identity, + sName varchar(10) not null, + sUid int +) + +alter table bbsUsers add constraint Pk_bbsUsers_UIDD primary key(UIDD) +alter table bbsUsers add constraint Uk_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint Ck_bbsUsers_uSex check(uSex='男'or uSex='女') +alter table bbsUsers add constraint Ck_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint Ck_bbsUsers_uPoint check(uPoint>=0) + +alter table bbsSection add constraint Pk_bbsSection_sIDD primary key(sIDD) +alter table bbsSection add constraint Fk_bbsSection_sUid foreign key(sUid) references bbsUsers(UIDD) + +create table bbsTopic +( + tID int identity primary key, + tUID int foreign key references bbsUsers(UIDD), + tSID int foreign key references bbsSection(sIDD), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +create table bbsReply +( + rID int identity primary key, + rUID int foreign key references bbsUsers(UIDD), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select uName,uPoint into bbsPoint from bbsUsers +insert into bbsSection values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) +insert into bbsTopic values(2,4,'范跑跑 ',' 谁是范跑跑 ','2008-7-8','1'),(3,1,'.NET ',' 与JAVA的区别是什么呀? ','2008-9-1 ','2'), +(1,3,'今年夏天最流行什么 ',' 有谁知道今年夏天最流行 ','2008-9-10','0') + +insert into bbsReply values(3,2,'八嘎','2008-7-8'),(1,1,'python','2008-7-8'),(1,3,'上海','2008-7-8') + + + +select * from bbsReply --回帖表 +select * from bbsSection --版块表 +select * from bbsTopic --主帖表 +select * from bbsUsers --用户信息表 + + +--据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select sIDD,uName,sName from bbsSection left join bbsUsers on sIDD=UIDD + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID,uName,tTitle,tMsg,tTime from bbsTopic left join bbsUsers on tID=UIDD where tTime > '2008-09-15' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select sIDD,uName,sName,uAge from bbsSection left join bbsUsers on sIDD=UIDD where uAge<20 + +--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select top 1 tUID,uName,tTitle,tMsg,tCount from bbsUsers t left join bbsTopic x on t.UIDD=x.tID order by tCount desc + +--5.在主贴表中查询每个版块中每个用户的发帖总数 + +select uName 用户名,sName 版块名称,count(*)发帖总数 from bbsTopic inner join bbsUsers on bbsTopic.tUID = bbsUsers.UIDD + inner join bbsSection on bbsUsers.UIDD= bbsSection.sUid group by uName,sName + + + + diff --git "a/20210326\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/.keep" "b/20210326\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/20210326\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery-\345\255\246\347\224\237\344\277\241\346\201\257\350\277\236\350\241\250\346\237\245\350\257\242.sql" "b/20210326\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery-\345\255\246\347\224\237\344\277\241\346\201\257\350\277\236\350\241\250\346\237\245\350\257\242.sql" new file mode 100644 index 0000000000000000000000000000000000000000..26513e015511e20bc54d4a447c1a101c43d03066 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery-\345\255\246\347\224\237\344\277\241\346\201\257\350\277\236\350\241\250\346\237\245\350\257\242.sql" @@ -0,0 +1,168 @@ +use master +go +create database bbs +on +( + name='bbs', + filename='D:\SQL作业\SQL作业8\bbs.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='bbs_log', + filename='D:\SQL作业\SQL作业8\bbs_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use bbs +create table bbsUsers +( + UID int identity(1,1), + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +go + +alter table bbsUsers add constraint PK_bbsUsers_UID primary key (UID) +alter table bbsUsers add constraint UK_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex='男' or uSex='女') +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint CK_bbsUsers_uPoint check(uPoint>=0) + + +use bbs +create table bbsSection +( + sID int identity(1,1), + sName varchar(10) not null, + sUid int +) +go + +alter table bbsSection add constraint PK_bbsSection_sID primary key (sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUsers(UID) + +use bbs +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int constraint FK_bbsUsers_UID references bbsUsers(UID), + tSID int constraint FK_bbsSection_sID references bbsSection(sID), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +go + +use bbs +create table bbsReply +( + rID int primary key identity(1,1), + rUID int constraint FK_bbsReply_rUID references bbsUsers(UID), + rTID int constraint FK_bbsTopic_tID references bbsTopic(tID), + rMsg text not null, + rTime datetime +) +go + + +--1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +--2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 +select uName,uPoint into bbsPoint from bbsUsers +--3.给论坛开设4个板块 +-- 名称 版主名 +-- 技术交流 小雨点 +-- 读书世界 七年级生 +-- 生活百科 小雨点 +-- 八卦区 七年级生 +insert into bbsSection values ('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + +select * from bbsUsers +--4.向主贴和回帖表中添加几条记录 + +-- 主贴: + +-- 发帖人 板块名 帖子标题 帖子内容 发帖时间 回复数量 +-- 逍遥 八卦区 范跑跑 谁是范跑跑 2008-7-8 1 +-- 七年级生 技术交流 .NET 与JAVA的区别是什么呀? 2008-9-1 2 +-- 小雨点 生活百科 今年夏天最流行什么 有谁知道今年夏天最流行 2008-9-10 0 +-- 什么呀? + +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values +(2,2,'范跑跑','谁是范跑跑','2008-7-8',1),(3,3,'.NET','与JAVA的区别是什么呀?','2008-9-1',2), +(1,4,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10',0) +--5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) +alter table bbsTopic drop constraint FK_bbsUsers_UID +delete from bbsUsers where uName='逍遥' +--6.因为小雨点发帖较多,将其积分增加10分 +update bbsUsers set uPoint=10 where uName='小雨点' +--7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) +alter table bbsTopic drop constraint FK_bbsSection_sID +delete from bbsSection where sName='生活百科' +--8.因回帖积累太多,现需要将所有的回帖删除 +delete from bbsReply + + +select * from bbsTopic--主贴表 +select * from bbsSection--版块表 +select * from bbsUsers--用户信息表 +select * from bbsReply--回帖表 + +--在论坛数据库中完成以下题目 + +--1.在主贴表中统计每个版块的发帖总数 +select tSID 发帖总数,count(*) from bbsTopic group by tSID +select * from bbsTopic--主贴表 + +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID 主贴编号,count(*)回帖数量 from bbsReply group by rTID +select * from bbsReply--回帖表 + +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID 用户编号,count(*)发贴总数 from bbsTopic group by tUID +select * from bbsTopic--主贴表 + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select * from bbsTopic--主贴表 +select tUID 用户编号,sum(tCount)回帖总数 from bbsTopic group by tUID + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select * from bbsTopic--主贴表 +select tSID 板块编号,avg(tCount)平均回复数 from bbsTopic group by tSID having avg(tCount)>3 + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select * from bbsUsers--用户信息表 +select top 1 uName 用户名,uSex 性别,uAge 年龄,uPoint 积分 from bbsUsers group by uName,uSex,uAge,uPoint + +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic--主贴表 +select tTitle 标题,tMsg 内容 from bbsTopic where tTitle like '%快乐%' or tMsg like '%快乐%' + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers--用户信息表 +select * from bbsUsers where uAge between 15 and 20 and uPoint>10 +select * from bbsUsers where uAge>15 and uAge<20 and uPoint>10 + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers--用户信息表 +select * from bbsUsers where uName='小_大' + +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select * from bbsTopic--主贴表 +select tID 主贴编号,tTitle 贴子标题,tMsg 帖子内容 from bbsTopic where tTime>'2008-9-10 12:00:00' and tCount>10 + +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select * from bbsTopic--主贴表 +select tUID,tCount from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery\350\256\242\350\264\255\350\277\236\350\241\250\346\237\245\350\257\242.sql" "b/20210326\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery\350\256\242\350\264\255\350\277\236\350\241\250\346\237\245\350\257\242.sql" new file mode 100644 index 0000000000000000000000000000000000000000..931060b0c1a33d23a5b830ec51efb2994682748b --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery\350\256\242\350\264\255\350\277\236\350\241\250\346\237\245\350\257\242.sql" @@ -0,0 +1,90 @@ +use master +go + +create database order1 +on +( + name='order1', + filename='E:\数据库文件\数据库跟目录文件\连表查询:内连接、外连接、交叉连接、全连接、自连接\order1.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='order1_log', + filename='E:\数据库文件\数据库跟目录文件\连表查询:内连接、外连接、交叉连接、全连接、自连接\order1_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use order1 +go +create table orders +( + orderId int primary key identity(1,1), + orderDate date +) +go + +insert into orders values('2008-01-12'),('2008-02-10'),('2008-02-15'),('2008-03-10') +select * from orders--订单表 + + +use order1 +go +create table orderItem +( + ItemiD int, + orderId int constraint FK_orderItem_orderId foreign key references orders(orderId), + itemType nchar(15), + itemName char(20), + theNumber int, + theMoney money +) +go + +insert into orderItem values +(1,1,'文具','笔',72,2),(2,1,'文具','尺',10,1),(3,1,'体育用品','篮球',1,56),(4,2,'文具','笔',36,2), +(5,2,'文具','固体胶',20,3),(6,2,'日常用品','透明胶',2,1),(7,2,'体育用品','羽毛球',20,3),(8,3,'文具','订书机',20,3), +(9,3,'文具','订书针',10,3),(10,3,'文具','裁纸刀',5,5),(11,4,'文具','笔',20,2),(12,4,'文具','信纸',50,1), +(13,4,'日常用品','毛巾',4,5),(14,1,'日常用品','透明胶',30,1),(15,4,'体育用品','羽毛球',20,3) + +select * from orderItem--订购项目表 + + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + +select orderItem.ItemiD 订单编号,orderDate 订单日期, itemType 订单类别,itemName 产品名称,theNumber 订购数量,theMoney 产品单价 from orders inner join orderItem on orderItem.orderId=orders.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orderItem.ItemiD 订单编号,orderDate 订单日期,itemType 订单类别,itemName 产品名称 from orderItem inner join orders on orderItem.orderId=orders.orderId where theNumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 产品单价,theNumber*theMoney 订购总价 from orderItem inner join orders on orderItem.orderId=orders.orderId + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +select orderItem.orderId 订单编号,orderDate 订单日期,itemType 产品类别,itemName 产品名称,theNumber 订购数量,theMoney 产品单价,theNumber*theMoney 订购总价 from orderItem inner join orders on orderItem.orderId=orders.orderId where theMoney>=5 and theNumber>=50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 + +select orderId 订单类别编, count(theNumber) 订单产品数 from orderItem group by orderId + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 + +select orderItem.orderId 订单编号,itemType 产品类别,count(itemType)订购次数,sum(theNumber)总数 from orderItem inner join orders on orderItem.orderId=orders.orderId group by orderItem.orderId,orderItem.itemType order by orderItem.orderId asc +select orderId,itemType 类别,count(itemType)次数,sum(theNumber)总数量 from orderItem group by orderId,itemType order by orderId asc \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery\350\256\272\345\235\233\350\277\236\350\241\250\346\237\245\350\257\242.sql" "b/20210326\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery\350\256\272\345\235\233\350\277\236\350\241\250\346\237\245\350\257\242.sql" new file mode 100644 index 0000000000000000000000000000000000000000..66ac1a412a9d01c66743fb63ed2417b1fc44b370 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery\350\256\272\345\235\233\350\277\236\350\241\250\346\237\245\350\257\242.sql" @@ -0,0 +1,168 @@ +use master +go +create database bbs +on +( + name='bbs', + filename='E:\数据库文件\数据库跟目录文件\连表查询:内连接、外连接、交叉连接、全连接、自连接\bbs.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='bbs_log', + filename='E:\数据库文件\数据库跟目录文件\连表查询:内连接、外连接、交叉连接、全连接、自连接\bbs_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use bbs +create table bbsUsers +( + UID int identity(1,1), + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +go + +alter table bbsUsers add constraint PK_bbsUsers_UID primary key (UID) +alter table bbsUsers add constraint UK_bbsUsers_uName unique(uName) +alter table bbsUsers add constraint CK_bbsUsers_uSex check(uSex='男' or uSex='女') +alter table bbsUsers add constraint CK_bbsUsers_uAge check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint CK_bbsUsers_uPoint check(uPoint>=0) + + +use bbs +create table bbsSection +( + sID int identity(1,1), + sName varchar(10) not null, + sUid int +) +go + +alter table bbsSection add constraint PK_bbsSection_sID primary key (sID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUsers(UID) + +use bbs +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int constraint FK_bbsUsers_UID references bbsUsers(UID), + tSID int constraint FK_bbsSection_sID references bbsSection(sID), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +go + +use bbs +create table bbsReply +( + rID int primary key identity(1,1), + rUID int constraint FK_bbsReply_rUID references bbsUsers(UID), + rTID int constraint FK_bbsTopic_tID references bbsTopic(tID), + rMsg text not null, + rTime datetime +) +go + + +--1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +--2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 +select uName,uPoint into bbsPoint from bbsUsers +--3.给论坛开设4个板块 +-- 名称 版主名 +-- 技术交流 小雨点 +-- 读书世界 七年级生 +-- 生活百科 小雨点 +-- 八卦区 七年级生 +insert into bbsSection values ('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + +select * from bbsUsers +--4.向主贴和回帖表中添加几条记录 + +-- 主贴: + +-- 发帖人 板块名 帖子标题 帖子内容 发帖时间 回复数量 +-- 逍遥 八卦区 范跑跑 谁是范跑跑 2008-7-8 1 +-- 七年级生 技术交流 .NET 与JAVA的区别是什么呀? 2008-9-1 2 +-- 小雨点 生活百科 今年夏天最流行什么 有谁知道今年夏天最流行 2008-9-10 0 +-- 什么呀? + +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values +(2,2,'范跑跑','谁是范跑跑','2008-7-8',1),(3,3,'.NET','与JAVA的区别是什么呀?','2008-9-1',2), +(1,4,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10',0) +--5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) +alter table bbsTopic drop constraint FK_bbsUsers_UID +delete from bbsUsers where uName='逍遥' +--6.因为小雨点发帖较多,将其积分增加10分 +update bbsUsers set uPoint=10 where uName='小雨点' +--7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) +alter table bbsTopic drop constraint FK_bbsSection_sID +delete from bbsSection where sName='生活百科' +--8.因回帖积累太多,现需要将所有的回帖删除 +delete from bbsReply + + +select * from bbsTopic--主贴表 +select * from bbsSection--版块表 +select * from bbsUsers--用户信息表 +select * from bbsReply--回帖表 + +--在论坛数据库中完成以下题目 + +--1.在主贴表中统计每个版块的发帖总数 +select tSID 发帖总数,count(*) from bbsTopic group by tSID +select * from bbsTopic--主贴表 + +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID 主贴编号,count(*)回帖数量 from bbsReply group by rTID +select * from bbsReply--回帖表 + +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID 用户编号,count(*)发贴总数 from bbsTopic group by tUID +select * from bbsTopic--主贴表 + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select * from bbsTopic--主贴表 +select tUID 用户编号,sum(tCount)回帖总数 from bbsTopic group by tUID + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select * from bbsTopic--主贴表 +select tSID 板块编号,avg(tCount)平均回复数 from bbsTopic group by tSID having avg(tCount)>3 + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select * from bbsUsers--用户信息表 +select top 1 uName 用户名,uSex 性别,uAge 年龄,uPoint 积分 from bbsUsers group by uName,uSex,uAge,uPoint + +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic--主贴表 +select tTitle 标题,tMsg 内容 from bbsTopic where tTitle like '%快乐%' or tMsg like '%快乐%' + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers--用户信息表 +select * from bbsUsers where uAge between 15 and 20 and uPoint>10 +select * from bbsUsers where uAge>15 and uAge<20 and uPoint>10 + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers--用户信息表 +select * from bbsUsers where uName='小_大' + +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select * from bbsTopic--主贴表 +select tID 主贴编号,tTitle 贴子标题,tMsg 帖子内容 from bbsTopic where tTime>'2008-9-10 12:00:00' and tCount>10 + +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select * from bbsTopic--主贴表 +select tUID,tCount from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" "b/20210326\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..59a3de16b44f20ac63c522959bb995cca254b942 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" @@ -0,0 +1,84 @@ +create database Assignment +on +( + name='Assignment', + filename='D:\SQL\Assignment.mdf' +) +log on +( + name='Assignment_log', + filename='D:\SQL\Assignment_log.ldf' +) +go + +use Assignment +go +--drop table orderItem +create table orders +( + orderId int primary key, + orderDate date +) + +create table orderItem +( + ItemiD int identity(1,1) primary key, + orderId int references orders(orderId), + itemType varchar(12), + itemName varchar(12), + theNumber int, + theMoney money +) +insert into orders values + (1,'2008-01-12'), + (2,'2008-02-10'), + (3,'2008-02-15'), + (4,'2008-03-10') +insert into orderItem values + (1,'文具','笔',72,2), + (1,'文具','尺',10,1), + (1,'体育用品','篮球',1,56), + (2,'文具','笔',36,2), + (2,'文具','固体胶',20,3), + (2,'日常用品','透明胶',2,1), + (2,'体育用品','羽毛球',20,3), + (3,'文具','订书机',20,3), + (3,'文具','订书针',10,3), + (3,'文具','裁纸刀',5,5), + (4,'文具','笔',20,2), + (4,'文具','信纸',50,1), + (4,'日常用品','毛巾',4,5), + (4,'日常用品','透明胶',30,1), + (4,'体育用品','羽毛球',20,3) +-- 使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select o.orderId 订单编号,orderDate 订单日期,itemType 订购类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价 +from orders o inner join orderItem i on o.orderId=i.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select o.orderId 订单编号,orderDate 订单日期,itemType 订购类别,itemName 产品名称 +from orders o inner join orderItem i on o.orderId=i.orderId +where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select o.orderId 订单编号,orderDate 订单日期,itemType 订购类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 +from orders o inner join orderItem i on o.orderId=i.orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select o.orderId 订单编号,orderDate 订单日期,itemType 订购类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 +from orders o inner join orderItem i on o.orderId=i.orderId +where theMoney>=5 and theNumber>=50 +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select o.orderId 订单编号,count(*) from orders o inner join orderItem i on o.orderId=i.orderId group by o.orderId +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select o.orderId 订单编号,itemType 订购类别,count(*) 订购次数,sum(theNumber) 总数量 +from orders o inner join orderItem i on o.orderId=i.orderId group by o.orderId,itemType order by o.orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery2.sql" "b/20210326\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0f538c0286bd1bed4d2c0b530dc219943d6dd689 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery2.sql" @@ -0,0 +1,66 @@ +create database Student +on +( + name='Student', + filename='D:\SQL\Student.mdf' +) +log on +( + name='Student_log', + filename='D:\SQL\Student_log.ldf' +) +go + +create table StuInfo +( + stuNo char(5) primary key(stuNo), + stuName nvarchar(20), + stuAge int, + stuAddress text, + stuSeat int identity(1,1), + stuSex char(1) check(stuSex in(1,0)) +) + +create table StuExam +( + examNo int identity(1,1), + stuNo char(5), + writtenExam int check(writtenExam>=0 and writtenExam<=100), + labExam int check(labExam>=0 and labExam<=100) +) + + alter table StuExam add constraint RK_StuExam_stuNo foreign key(stuNo) references StuInfo(stuNo) + + insert into StuInfo values + ('s2501','张秋利',20,'美国硅谷',1), + ('s2502','李斯文',18,'湖北武汉',0), + ('s2503','马文才',22,'湖南长沙',1), + ('s2504','欧阳俊雄',21,'湖北武汉',0), + ('s2505','梅超风',20,'湖北武汉',1), + ('s2506','陈旋风',19,'美国硅谷',1), + ('s2507','陈风',20,'美国硅谷',0) + insert into StuExam values + ('s2501',50,70),('s2502',60,65),('s2503',86,85),('s2504',40,80),('s2505',70,90),('s2506',85,90) + +--数据如图片1,使用上次作业的数据 + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 +from StuInfo i inner join StuExam e on i.stuNo=e.stuNo +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select i.stuNo 学号,stuName 姓名,writtenExam 笔试成绩,labExam 机试成绩 from StuInfo i left join StuExam e +on i.stuNo=e.stuNo where writtenExam>60 and labExam>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 +from StuInfo i left join StuExam e on i.stuNo=e.stuNo +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuName 姓名,stuAge 年龄,writtenExam 笔试成绩,labExam 机试成绩 +from StuInfo i inner join StuExam e on i.stuNo=e.stuNo +where stuAge>=20 order by writtenExam DESC +--5.查询男女生的机试平均分 +select stuSex 性别,avg(labExam) 机试成绩 +from StuInfo i inner join StuExam e on i.stuNo=e.stuNo +where stuAge>=20 group by stuSex +--6.查询男女生的笔试总分 +select stuSex 性别,sum(writtenExam) 笔试成绩 +from StuInfo i inner join StuExam e on i.stuNo=e.stuNo group by stuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQL1.sql" "b/20210326\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQL1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7a68c13582968c1ffa895718884c0d8a5406da1a --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQL1.sql" @@ -0,0 +1,79 @@ +use master +go + +create database Student05 +go + +use Student05 +go + +create table StuIS +( + StuNO nvarchar(10) unique not null, + StuName nvarchar(10) not null, + StuAge int not null, + StuAddress nvarchar(200) , + StuSeat nvarchar(8) not null, + StuSex nvarchar(1) default('鐢') check(StuSex='鐢' or StuSex='濂') +) + +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2501','寮犵鍒','20','缇庡浗纭呰胺','1','濂') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2502','鏉庢柉鏂','18','婀栧寳姝︽眽','2','鐢') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2503','椹枃鎵','22','婀栧崡闀挎矙','3','鐢') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2504','娆ч槼淇婇泟','21','婀栧寳姝︽眽','3','濂') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2505','姊呰秴椋','20','婀栧寳姝︽眽','4','濂') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2506','闄堟棆椋','19','缇庡浗纭呰胺','5','濂') +insert into StuIS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2507','闄堥','20','缇庡浗纭呰胺','7','濂') + +create table Grate +( + ExamNo int primary key identity(1,1), + StuNo nvarchar(10) references StuIS(StuNO), + WExam int not null, + LabExam int not null +) +insert into Grate(StuNO,WExam,LabExam) +values('s2501','50','70') +insert into Grate(StuNO,WExam,LabExam) +values('s2502','60','65') +insert into Grate(StuNO,WExam,LabExam) +values('s2503','86','85') +insert into Grate(StuNO,WExam,LabExam) +values('s2504','40','80') +insert into Grate(StuNO,WExam,LabExam) +values('s2505','70','90') +insert into Grate(StuNO,WExam,LabExam) +values('s2506','85','90') + + +--鏁版嵁濡傚浘鐗1,浣跨敤涓婃浣滀笟鐨勬暟鎹 +select * from Grate +select * from StuIS +--1.鏌ヨ瀛︾敓鐨勫鍚嶏紝骞撮緞锛岀瑪璇曟垚缁╁拰鏈鸿瘯鎴愮哗 +select StuName,StuAge ,WExam,LabExam from StuIS +inner join Grate on StuIS.StuNO = Grate.StuNO + +--2.鏌ヨ绗旇瘯鍜屾満璇曟垚缁╅兘鍦60鍒嗕互涓婄殑瀛︾敓鐨勫鍙凤紝濮撳悕锛岀瑪璇曟垚缁╁拰鏈鸿瘯鎴愮哗 +select StuIS.StuNO ,StuName,WExam,LabExam from StuIS +inner join Grate on StuIS.StuNO = Grate.StuNO where Grate.WExam>=60 and Grate.LabExam>=60 + +--3.鏌ヨ鎵鏈夊鐢熺殑瀛﹀彿锛屽鍚嶏紝绗旇瘯鎴愮哗锛屾満璇曟垚缁╋紝娌℃湁鍙傚姞鑰冭瘯鐨勫鐢熺殑鎴愮哗浠ULL鍊煎~鍏 +select StuIS.StuNO ,StuName,WExam,LabExam from StuIS +left join Grate on StuIS.StuNO = Grate.StuNO + +--4.鏌ヨ骞撮緞鍦20浠ヤ笂锛堝寘鎷20锛夌殑瀛︾敓鐨勫鍚嶏紝骞撮緞锛岀瑪璇曟垚缁╁拰鏈鸿瘯鎴愮哗锛屽苟鎸夌瑪璇曟垚缁╅檷搴忔帓鍒 +select StuName,StuAge,WExam,LabExam from StuIS +inner join Grate on StuIS.StuNo = Grate.StuNO where StuIS.StuAge>=20 order by Grate.WExam DESC +--5.鏌ヨ鐢峰コ鐢熺殑鏈鸿瘯骞冲潎鍒 +select StuSex , avg(LabExam) from StuIS +inner join Grate on StuIS.StuNO = Grate.StuNO group by StuSex +--6.鏌ヨ鐢峰コ鐢熺殑绗旇瘯鎬诲垎 +select StuSex , sum(WExam) from StuIS +inner join Grate on StuIS.StuNO = Grate.StuNO group by StuSex \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQL2.sql" "b/20210326\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQL2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..56dec4b86b92034936e0af73bb6959906e354ccb --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQL2.sql" @@ -0,0 +1,88 @@ +create database OrderIno +go + +use OrderIno +go + +create table orders +( + orderID int primary key identity(1,1), + orderDate datetime +) + +create table orderltem +( + ltemID int identity(1,1), + orderID int , + itemType nvarchar(15), + itemName nvarchar(10), + theNumber int , + theMoney money +) + +insert into orders (orderDate) values +('2008-01-12 00:00:00.000'), +('2008-02-10 00:00:00.000'), +('2008-02-15 00:00:00.000'), +('2008-03-10 00:00:00.000') + +insert into orderltem values +(1,'鏂囧叿','绗',72,2), +(1,'鏂囧叿','灏',10,1), +(1,'浣撹偛鐢ㄥ搧','绡悆',1,56), +(2,'鏂囧叿','绗',36,2), +(2,'鏂囧叿','鍥轰綋鑳',20,3), +(2,'鐢熸椿鐢ㄥ搧','閫忔槑鑳',2,1), +(2,'浣撹偛鐢ㄥ搧','缇芥瘺鐞',20,3), +(3,'鏂囧叿','璁功鏈',20,3), +(3,'鏂囧叿','璁功鏈',10,3), +(3,'鏂囧叿','瑁佺焊鍒',5,5), +(4,'鏂囧叿','绗',20,2), +(4,'鏂囧叿','淇$焊',50,1), +(4,'鐢熸椿鐢ㄥ搧','姣涘肪',4,5), +(4,'鐢熸椿鐢ㄥ搧','閫忔槑鑳',30,1), +(4,'浣撹偛鐢ㄥ搧','缇芥瘺鐞',20,3) + + +--浣跨敤涓婃浣滀笟鐨勮鍗曟暟鎹簱锛屽畬鎴愪笅鍒楅鐩細 + +select * from orders --璁㈠崟琛 +select * from orderltem --璁㈣喘椤圭洰琛 + +--璁㈠崟琛紙orders锛夊垪涓猴細璁㈠崟缂栧彿锛坥rderID 涓婚敭锛 璁㈣喘鏃ユ湡锛坥rderDate锛--璁㈣喘椤圭洰琛紙orderItem锛夛紝鍒椾负锛--椤圭洰缂栧彿锛圛temID锛夎鍗曠紪鍙凤紙orderID锛変骇鍝佺被鍒紙itemType锛--浜у搧鍚嶇О锛坕temName锛 璁㈣喘鏁伴噺锛坱heNumber锛 璁㈣喘鍗曚环锛坱heMoney锛 +--1.鏌ヨ鎵鏈夌殑璁㈠崟鐨勮鍗曠殑缂栧彿锛岃鍗曟棩鏈燂紝璁㈣喘浜у搧鐨勭被鍒拰璁㈣喘鐨勪骇鍝佸悕绉帮紝璁㈣喘鏁伴噺鍜岃璐崟浠 +select orders.orderID 璁㈠崟鐨勭紪鍙 ,orderDate 璁㈠崟鏃ユ湡, itemType 璁㈣喘浜у搧鐨勭被鍒,itemName 璁㈣喘鐨勪骇鍝佸悕绉 from orders +inner join orderltem on orders.orderID =orderltem.orderID + +--2.鏌ヨ璁㈣喘鏁伴噺澶т簬50鐨勮鍗曠殑缂栧彿锛岃鍗曟棩鏈燂紝璁㈣喘浜у搧鐨勭被鍒拰璁㈣喘鐨勪骇鍝佸悕绉 +select orders.orderID 璁㈠崟鐨勭紪鍙 ,orderDate 璁㈠崟鏃ユ湡, itemType 璁㈣喘浜у搧鐨勭被鍒,itemName 璁㈣喘鐨勪骇鍝佸悕绉 from orders +inner join orderltem on orders.orderID =orderltem.orderID where orderltem.theNumber >50 + + +--3.鏌ヨ鎵鏈夌殑璁㈠崟鐨勮鍗曠殑缂栧彿锛岃鍗曟棩鏈燂紝璁㈣喘浜у搧鐨勭被鍒拰璁㈣喘鐨勪骇鍝佸悕绉帮紝璁㈣喘鏁伴噺鍜岃璐崟浠蜂互鍙婅璐讳环 +select orders.orderID 璁㈠崟鐨勭紪鍙 ,orderDate 璁㈠崟鏃ユ湡, itemType 璁㈣喘浜у搧鐨勭被鍒,itemName 璁㈣喘鐨勪骇鍝佸悕绉, theNumber 璁㈣喘鏁伴噺 ,theMoney 璁㈣喘鍗曚环 , theNumber*theMoney 璁㈣喘鎬讳环 from orders +inner join orderltem on orders.orderID =orderltem.orderID + + +--4.鏌ヨ鍗曚环澶т簬绛変簬5骞朵笖鏁伴噺澶т簬绛変簬50鐨勮鍗曠殑璁㈠崟鐨勭紪鍙凤紝璁㈠崟鏃ユ湡锛岃璐骇鍝佺殑绫诲埆鍜岃璐殑浜у搧鍚嶇О锛岃璐暟閲忓拰璁㈣喘鍗曚环浠ュ強璁㈣喘鎬讳环 +select orders.orderID 璁㈠崟鐨勭紪鍙 ,orderDate 璁㈠崟鏃ユ湡, itemType 璁㈣喘浜у搧鐨勭被鍒,itemName 璁㈣喘鐨勪骇鍝佸悕绉, theNumber 璁㈣喘鏁伴噺 ,theMoney 璁㈣喘鍗曚环 , theNumber*theMoney 璁㈣喘鎬讳环 from orders +inner join orderltem on orders.orderID =orderltem.orderID where orderltem.theMoney>=5 and orderltem.theNumber>=50 + + +--5.鏌ヨ姣忎釜璁㈠崟鍒嗗埆璁㈣喘浜嗗嚑涓骇鍝侊紝渚嬪锛 +-- 缂栧彿 璁㈣喘浜у搧鏁 +-- 1 3 +-- 2 4 +select orderID 璁㈠崟鐨勭紪鍙 , count(*)璁㈣喘浜у搧鏁 from orderltem group by orderID + + +--6.鏌ヨ姣忎釜璁㈠崟閲岀殑姣忎釜绫诲埆鐨勪骇鍝佸垎鍒璐簡鍑犳鍜屾绘暟閲忥紝渚嬪锛 +-- 璁㈠崟缂栧彿 浜у搧绫诲埆 璁㈣喘娆℃暟 鎬绘暟閲 +-- 1 鏂囧叿 2 82 +-- 1 浣撹偛鐢ㄥ搧 1 1 +-- 2 鏂囧叿 2 56 +-- 2 浣撹偛鐢ㄥ搧 1 2 +-- 2 鏃ュ父鐢ㄥ搧 1 20 + +select orderID 璁㈠崟鐨勭紪鍙 , itemType 浜у搧绫诲埆 ,count(*)璁㈣喘娆℃暟 , sum(theNumber) 鎬绘暟閲 + from orderltem group by orderID,itemType order by orderID \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQL3.sql" "b/20210326\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQL3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..dd829d2b48d2710d56ed894cd3fcaa23f0977212 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQL3.sql" @@ -0,0 +1,115 @@ +use master +go + +create database bbs +go + +use bbs +go + +create table bbsUsers +( + UID int identity(1,1) , + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +-- +alter table bbsUsers add constraint PK_bbsUser_UID primary key(UID) +-- +alter table bbsUsers add constraint UK_bbsUser_uName unique(uName) +-- +alter table bbsUsers add constraint CK_bbsUser_uSex check(uSex in('鐢','濂')) +-- +alter table bbsUsers add constraint CK_bbsUser_uAge check(uAge>=15 or uAge<=60) +-- +alter table bbsUsers add constraint CK_bbsUser_uPoint check(uPoint>=0) +-- + +create table bbsSection +( + sID int identity(1,1), + sName varchar(10) not null, + sUid int +) + +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) +-- +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUsers(UID) + + + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int foreign key references bbsUsers(UID), + tSID int foreign key references bbsSection(sID), + tTItle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) + + +create table bbsReply +( + rID INT primary key identity(1,1), + rUID int foreign key references bbsUsers(UID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUsers values +('灏忛洦鐐','濂',20,0), +('閫嶉仴','鐢',18,4), +('涓冨勾绾х敓','鐢',19,2) + + +select * into bbsPoint from bbsUsers + +insert into bbsSection values +('鎶鏈氦娴',1), +('璇讳功涓栫晫',3), +('鐢熸椿鐧剧',1), +('鍏崷鍖',3) + +insert into bbsTopic values +(2,4,'浠婂勾澶忓ぉ鏈娴佽浠涔','鏈夎皝鐭ラ亾浠婂勾澶忓ぉ鏈娴佽浠涔堝憖?',2008-9-10,0), +(3,1,'.NET','涓嶫AVA鐨勫尯鍒槸浠涔堝憖?',2008-9-1,2), +(1,3,'鑼冭窇璺','璋佹槸鑼冭窇璺',2008-7-8,1) + +insert into bbsReply values +(2,1,'涓嶇煡閬',2008-9-10), +(3,2,'涓嶇煡閬',2008-9-1), +(1,3,'涓嶇煡閬',2008-7-8) + +select * from bbsReply --鍥炲笘琛 +select * from bbsSection --鐗堝潡琛 +select * from bbsTopic --涓诲笘琛 +select * from bbsUsers --鐢ㄦ埛淇℃伅琛 + +--鍦ㄨ鍧涙暟鎹簱涓畬鎴愪互涓嬮鐩 +--1.鏌ヨ鍑 姣忎釜鐗堝潡 鐨 鐗堜富缂栧彿锛 鐗堜富濮撳悕 鍜岀増鍧楀悕绉 +select sUid 鐗堜富缂栧彿,uName 鐗堜富濮撳悕,sName 鐗堝潡鍚嶇О from bbsSection +inner join bbsUsers on bbsUsers.UID = bbsSection.sUid + +--2.鏌ヨ鍑轰富璐寸殑鍙戝笘鏃堕棿鍦2008-9-15浠ュ悗鐨勪富璐寸殑鍙戝笘浜虹紪鍙凤紝鍙戝笘浜哄鍚嶏紝甯栧瓙鐨勬爣棰橈紝甯栧瓙鐨勫唴瀹瑰拰鍙戝笘鏃堕棿 +select tUID 鍙戝笘浜虹紪鍙,uName 鍙戝笘浜哄鍚,tTItle 甯栧瓙鐨勬爣棰 ,tMsg 甯栧瓙鐨勫唴瀹,tTime 鍙戝笘鏃堕棿 from bbsTopic +inner join bbsUsers on bbsUsers.UID = bbsTopic.tUID where tTime>'2008-9-15' + + +--3.鏌ヨ鍑哄勾榫勫湪20浠ヤ笅鐨勭増涓荤殑缂栧彿锛岀増涓荤殑鍚嶇О鍜岀増鍧楃殑鍚嶇О +select sUid 鐗堜富缂栧彿,uName 鐗堜富濮撳悕,sName 鐗堝潡鍚嶇О from bbsSection +inner join bbsUsers on bbsUsers.UID = bbsSection.sUid where uAge<20 + +--4.鏌ヨ鍑哄洖澶嶆暟閲忔渶澶氱殑涓昏创鐨勫彂甯栦汉缂栧彿锛屽彂甯栦汉濮撳悕锛屼富璐存爣棰橈紝涓昏创鍐呭鍜屽洖澶嶆暟閲 +select rUID 鍙戝笘浜虹紪鍙 , uName 鍙戝笘浜哄鍚 ,tTItle 涓昏创鏍囬 , tMsg 涓昏创鍐呭 , tCount 鍥炲鏁伴噺 from bbsReply +inner join bbsTopic on bbsTopic.tUID = bbsReply.rUID +inner join bbsUsers on bbsUsers.UID = bbsReply.rUID where tCount=(select max(tCount) from bbsTopic) + +--5.鍦ㄤ富璐磋〃涓煡璇㈡瘡涓増鍧椾腑姣忎釜鐢ㄦ埛鐨勫彂甯栨绘暟 +select tSID 鐗堝潡 , uName 鍙戝笘浜哄鍚 , count(*) from bbsTopic +inner join bbsUsers on bbsUsers.UID = bbsTopic.tUID +inner join bbsSection on bbsUsers.UID =bbsSection.sID group by tSID ,uName \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\344\275\234\344\270\2321.sql" "b/20210326\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c921a1b2f6705d2721557181bd78095f3cba9028 --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\344\275\234\344\270\2321.sql" @@ -0,0 +1,73 @@ +use master +go +create database studen +go +use studen +go + +drop table stuifno +drop table soceifno +create table stuifno +( + stuno nvarchar(20) primary key not null, + stuname nvarchar(20) not null , + stuage nvarchar(20) not null, + stuaddress nvarchar(200) not null, + stuseat int not null, + stusex char(2) default('男') check(stusex='男' or stusex='女') +) +create table soceifno +( + examno int primary key identity(1,1), + stuno nvarchar(20) references stuifno(stuno) not null, + writtenexam int not null, + labexam int not null +) +truncate table stuifno +truncate table soceifno + +insert into stuifno +select 's2501','张秋利','20','美国硅谷',1,'男' union +select 's2502','李斯文','18','湖北武汉',2,'女' union +select 's2503','马文才','22','湖南长沙',3,'男' union +select 's2504','欧阳俊雄','21','湖北武汉',4,'女' union +select 's2505','梅超风','20','湖北武汉',5,'男' union +select 's2506','陈旋风','19','美国硅谷',6,'男'union +select 's2507','陈风','20','美国硅谷',7,'女' + +insert into soceifno +select 's2501','50','70' union +select 's2502','60','65' union +select 's2503','86','85' union +select 's2504','40','80' union +select 's2505','70','90' union +select 's2506','85','90' + +select * from stuifno +select * from soceifno + +--数据如图片1,使用上次作业的数据 + +--1.查询 学生的姓名, 年龄, 笔试成绩和 机试成绩 +select stuname 姓名 ,stuage 年龄,writtenexam 笔试成绩,labexam 机试成绩 from soceifno,stuifno where soceifno.stuno=stuifno.stuno + + +--2.查询 笔试 和 机试成绩 都在60分以上 的学生的学号 ,姓名, 笔试成绩 和 机试成绩 +select soceifno.stuno 学号,stuname 姓名, writtenexam 笔试成绩,labexam 机试成绩 from soceifno +inner join stuifno on soceifno.stuno=stuifno.stuno where writtenexam>60 and labexam>60 + +--3.查询 所有学生 的学号,姓名,笔试成绩,机试成绩, 没有参加考试的学生的成绩以NULL值填充 +select stuifno.stuno 学号,stuname 姓名, writtenexam 笔试成绩 , labexam 机试成绩 from stuifno +left join soceifno on soceifno.stuno=stuifno.stuno + +--4.查询 年龄在20以上(包括20)的学生的 姓名, 年龄, 笔试成绩 和机试成绩,并按笔试成绩降序排列 +select stuname 姓名,stuage 年龄,writtenexam 笔试成绩 , labexam 机试成绩 from stuifno +inner join soceifno on soceifno.stuno=stuifno.stuno where stuage>=20 order by writtenexam desc + +--5.查询 男女生的 机试平均分 +select stusex 性别,avg(labexam) 机试平均分 from stuifno +inner join soceifno on soceifno.stuno=stuifno.stuno group by stusex + +--6.查询男女生的笔试总分 +select stusex 性别,sum(writtenexam) 笔试总分 from stuifno +inner join soceifno on soceifno.stuno=stuifno.stuno group by stusex diff --git "a/20210326\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\344\275\234\344\270\2322.sql" "b/20210326\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9ebdd0b4505c05995b7d3dfa584e34de7506eeae --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\344\275\234\344\270\2322.sql" @@ -0,0 +1,83 @@ +use master +go +create database ordersad +go +use ordersad +go + +create table orders +( + orderId int primary key identity(1,1), + orderDate datetime not null +) + +create table orderItem +( + ItemiD int primary key identity(1,1), + orderId int references orders(orderId) not null, + itemType nvarchar(20) not null, + itemName nvarchar(20) not null, + theNumber int not null, + theMoney int not null +) +drop table orderItem +insert into orders +select('2008-01-12 00:00:00.000') union +select('2008-02-10 00:00:00.000') union +select('2008-02-15 00:00:00.000') union +select('2008-03-10 00:00:00.000') + +insert into orderItem +select 1,'文具','笔','72','2' union +select 1,'文具','尺','10','1' union +select 1,'体育用品','篮球','1','56' union +select 2,'文具','笔','36','2' union +select 2,'文具','固体胶','20','3' union +select 2,'日常用品','透明胶','2','1' union +select 2,'体育用品','羽毛球','20','3' union +select 3,'文具','订书机','20','3' union +select 3,'文具','订书针','10','3' union +select 3,'文具','裁纸刀','5','5' union +select 4,'文具','笔','20','2' union +select 4,'文具','信纸','50','1' union +select 4,'日常用品','毛巾','4','5' union +select 4,'日常用品','透明胶','30','1' union +select 4,'体育用品','羽毛球','20','3' + + +select * from orderItem +select * from orders +--使用上次作业的订单数据库,完成下列题目: + +--1.查询 所有的订单的 订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orderItem.orderId 订单编号, orderDate 订单日期,itemType 类别,itemName 产品名称,theNumber 订购数量, theMoney 订购单价 from orderItem + inner join orders on orderItem.orderId=orders.orderId + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orderItem.orderId 订单编号, orderDate 订单日期,itemType 类别,itemName 产品名称 ,theNumber 订购数量 from orderItem +inner join orders on orderItem.orderId=orders.orderId where theNumber>50 +--3.查询所有的订单的 订单的编号, 订单日期,订购产品的 类别和订购的 产品名称, 订购数量 和 订购单价 以及 订购总价 +select orderItem.orderId 订单编号, orderDate 订单日期,itemType 类别,itemName 产品名称 ,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 总价 from orderItem +inner join orders on orderItem.orderId=orders.orderId + + +--4.查询单价大于等于5并且数量大于等于50的 订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orderItem.orderId 订单编号, orderDate 订单日期,itemType 类别,itemName 产品名称 ,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 总价 from orderItem +inner join orders on orderItem.orderId=orders.orderId where theMoney>=5 and theNumber>=50 + +--5.查询 每个订单分别订购了 几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderId 编号,count(theNumber) 订购产品数 from orderItem group by orderID + +--6.查询 每个订单里的 每个类别的 产品分别订购了几次 和 总数量,例如: + +-- 订单编号 产品类别 订购次数 总数量 + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +select orderId 订单编号 ,itemType 产品类别 ,count(itemType)订购次数 ,sum(theNumber) 总数量 from orderItem group by orderId ,itemType order by orderId \ No newline at end of file diff --git "a/20210326\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\344\275\234\344\270\2323.sql" "b/20210326\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\344\275\234\344\270\2323.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9eb02519dc87c4b3cdf8f32b1638a919ac74e91e --- /dev/null +++ "b/20210326\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\344\275\234\344\270\2323.sql" @@ -0,0 +1,155 @@ +use master +go + +create database bbs +on +( + name='bbs', + filename='F:\bbs.mdf', + size=10mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='bbs_log', + filename='F:\bbs_log.ldf', + size=10mb, + maxsize=100mb, + filegrowth=10mb +) +go + +use bbs +go + +create table bbsUsers +( + UID int not null, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) +go +use bbs +go +select * from bbsUsers +alter table bbsUsers add constraint PK primary key (UID) +alter table bbsUsers add constraint FK unique (uName) +alter table bbsUsers add constraint CK check(uSex='男' or uSex='女') +alter table bbsUsers add constraint DK check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint UK check(uPoint>=0) + +create table bbsSection +( + sID int not null, + sName varchar(10) not null, + sUid int not null +) +go +use bbs +go + +alter table bbsSection add constraint BK primary key(sID) +alter table bbsSection add constraint NK foreign key (sUid) references bbsUsers( UID) + +go +use bbs +go +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int references bbsUsers(UID), + tSID int references bbsSection(sID), + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +create table bbsReply +( + rID int primary key identity(1,1), + rUID int references bbsUsers(UID), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +insert into bbsUsers(UID,uName,uSex,uAge,uPoint) +values(1,'小雨点','女','20','0') +insert into bbsUsers(UID,uName,uSex,uAge,uPoint) +values(2,'逍遥','男','18','4') +insert into bbsUsers(UID,uName,uSex,uAge,uPoint) +values(3,'七年级生','男','19','2') + +go +use bbs +go + +select * from bbsReply + +select uName,uPoint into bbsPoint from bbsUsers + +insert into bbsSection(sID,sName,sUid) +values(5,'技术交流',1) +insert into bbsSection(sID,sName,sUid) +values(6,'读书世界',3) +insert into bbsSection(sID,sName,sUid) +values(7,'生活百科',1) +insert into bbsSection(sID,sName,sUid) +values(8,'八卦区',3) + +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +values(2,8,'范跑跑','谁是范跑跑','2008-7-8',1) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +values(3,5,'.NET','与JAVA的区别是什么呀?','2008-9-1',2) +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +values(1,7,'今年夏天最流行什么 ','有谁知道今年夏天最流行什么呀?','2008-9-10',0) + +drop table bbsReply +select * from bbsReply +insert into bbsReply +select 3 , 2 , '范跑跑' , '2008-7-8' union +select 1 , 3 , 'java是代码', '2008-9-4' union +select 2 , 3 , '.NET是什么', '2008-9-4' union +select 2 , 1 , '不穿衣服' , '2008-9-20' + +select * from bbsUsers +delete bbsUsers where uName ='逍遥' +alter table bbsSection drop NK +alter table bbsTopic drop FK__bbsTopic__tUID__1920BF5C +update bbsUsers set uPoint ='10' where uName='小雨点' + +select * from bbsSection +delete bbsSection where sName='生活百科' + +select * from bbsReply +delete bbsReply + +use bbs +go + +--在论坛数据库中完成以下题目 +--1.查询出每个版块的 版主编号, 版主姓名和 版块名称 +select * from bbsSection +select sUid 版主编, uName 版主姓名,sName 板块名称 from bbsSection inner join bbsUsers on bbsSection.sUid=bbsUsers.UID + +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的 发帖人编号 ,发帖人姓名, 帖子的标题 ,帖子的内容 和发帖时间 +select * from bbsTopic +select tUID 发帖人编号, uName 发帖人姓名, tTitle 帖子标题,tMsg 帖子内容, tTime 发帖时间 from bbsTopic +inner join bbsUsers on bbsTopic.tUID=bbsUsers.UID where tTime>'2008-9-15' + +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select UID 版主编号,uAge 年龄, uName 版主名称 ,sName 版块名称 from bbsUsers +inner join bbsSection on bbsSection.sUid=bbsUsers.UID where uAge<20 + +--4.查询出回复数量最多的主贴的 发帖人编号, 发帖人姓名, 主贴标题, 主贴内容和 回复数量 +select TOP 1 tUID 发帖人编号, uName 发帖人姓名, tTitle 帖子标题,tMsg 帖子内容, tCount 回复数量 from bbsTopic +inner join bbsUsers on bbsTopic.tUID=bbsUsers.UID order by tCount DESC + + +--5.在主贴表 中查询 每个版块中 每个用户的 发帖总数 +select * from bbsTopic +select tUID 用户, count(tSID) 发帖总数 from bbsTopic group by tUID + diff --git a/chenxuqing/SQLQuery2.sql b/chenxuqing/SQLQuery2.sql new file mode 100644 index 0000000000000000000000000000000000000000..9108c950d4c466a338bfbd822816f4095c0f8ac0 --- /dev/null +++ b/chenxuqing/SQLQuery2.sql @@ -0,0 +1,48 @@ +use master +go +create database StarManagerDB +go +use StarManagerDB +go +create table StarType +( + T_NO int identity(1,1) primary key, + T_NAME nvarchar(20) +) +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') +select*from StarType +create table StarInfo +( + S_NO int identity(1,1) primary key, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + +select*from StarType +select*from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 A.S_NAME 姓名,A.S_HOBBY 特技,A.S_NATIVE 籍贯 from StarInfo A +ORDER BY A.S_AGE DESC + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select A.T_NO 明星类型编号,count(B.S_NO)明星人数,avg(B.S_AGE) 明星平均年龄 FROM StarType A INNER JOIN StarInfo B ON A.T_NO=B.S_T_NO +GROUP BY A.T_NO HAVING COUNT(B.S_NO)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 B.S_NAME 姓名,B.S_HOBBY 特技,B.S_NATIVE 籍贯 +from StarType A join StarInfo B on A.T_NO=B.S_T_NO +where A.T_NAME='体育明星' order by B.S_AGE DESC \ No newline at end of file diff --git a/chenxuqing/SQLQuery4.sql b/chenxuqing/SQLQuery4.sql new file mode 100644 index 0000000000000000000000000000000000000000..d81e1376485ff555ece8be2ba59aeb69ee71fdb5 --- /dev/null +++ b/chenxuqing/SQLQuery4.sql @@ -0,0 +1,43 @@ +use master +go +create database HOUSE_DB +go +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int identity(1,1) primary key, + type_name varchar(20) unique not null +) +insert into HOUSE_TYPE values +('小户型'), +('经济型'), +('别墅') +select*from HOUSE_TYPE +create table HOUSE +( + house_id int identity(1,1) primary key, + house_name varchar(50) not null, + house_price float not null default('房租'), + type_id int not null references HOUSE_TYPE +) +insert into HOUSE values +('温馨家园',1500,1), +('星海洋房',3000,2), +('复式楼',5000,3) +select*from HOUSE + +--查询所有房屋信息 +select* from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select type_name from HOUSE_TYPE where type_name like '%型' + +--查询出 房屋的名称 和租金,并且按照租金降序排序 +select house_name 房屋名称,house_price 租金 from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name ,type_name 房屋类型 from HOUSE inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price 月租最高 ,house_name 房屋名称 from HOUSE order by house_price desc diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ef9ca9837cc111ee56df6b95689fe7a870a4c651 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,94 @@ +create database Students +go + +use Students +go + +--学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + +create table stuInfo +( + stuID int primary key identity, + stuName nvarchar(20), + stuAge int, + stuSex int check(stuSex=1 or stuSex=0), + time datetime +) + +create table courseInfo +( + courseID int primary key identity, + courseNAME nvarchar(20), + couresMarks int +) + +create table scoreInfo +( + scoerID int primary key identity, + stuiD int, + courseID int, + score int +) + +insert into stuInfo values + ('Tom',19,1,null), + ('Jack',20,0,null), + ('Rose',21,1,null), + ('Lulu',19,1,null), + ('Lili',21,0,null), + ('abc',20,1,'2001-01-07 01:11:36.590') +select * from stuInfo + +insert into courseInfo values + ('JavaBase',4), + ('HTML',2), + ('JavaScript',2), + ('SqlBase',2) +select * from courseInfo + +insert into scoreInfo values + (1,1,80), + (1,2,85), + (1,4,50), + (2,1,75), + (2,3,45), + (2,4,75), + (3,1,45), + (4,1,95), + (4,2,75), + (4,3,90), + (4,4,45) +select * from scoreInfo +select * from stuInfo +select * from courseInfo + + +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + select stuName , count(courseInfo.couresMarks) 选修的课程的数量, avg(score) 所选修的课程的考试的平均分 from stuInfo + inner join courseInfo on stuInfo.stuID=courseInfo.courseID + inner join scoreInfo on courseInfo.courseID=scoreInfo.stuID group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + select courseInfo.courseNAME,count(stuiD)数量,sum(scoreInfo.score)总分 from courseInfo + inner join scoreInfo on courseInfo.courseid=scoreInfo.courseid group by courseInfo.courseNAME +--3.查询出性别一样并且年龄一样的学生的信息 + select distinct A.* from stuInfo A,stuInfo B where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuName!=B.stuName +--4.查询出学分一样的课程信息 + select distinct A. * from courseInfo A,courseInfo B where A.couresMarks=B.couresMarks and A.courseID!=B.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + select stuInfo.stuID,stuInfo.stuName,courseInfo.courseID,scoreInfo.score from scoreInfo + inner join stuInfo on scoreInfo.stuiD=stuInfo.stuID + inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + select stuInfo.stuID,courseInfo.courseID,courseInfo.courseNAME,courseInfo.couresMarks,scoreInfo.score from scoreInfo + inner join stuInfo on scoreInfo.stuiD=stuInfo.stuID + inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 + select stuInfo.stuid,stuInfo.stuname from stuInfo + left join scoreInfo on stuInfo.stuid= scoreInfo .stuid where score is null +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 + select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 + select stuid 学号,avg(score)平均分,count(*)选修课程的数量 from scoreInfo group by stuiD having avg(score)>=70 and count(*)>=2 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4a7e6505e59a6b3b0307437119deadd2207308e4 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" @@ -0,0 +1,81 @@ +use master +go +create database py2 +go +use py2 +go +create table card +( + id varchar(30) primary key, + pasword varchar(30), + balance money, + userName varchar(50) +) +create table computer +( + id varchar(30) primary key, + onUse varchar(2) check(onUse=0 or onUse=1), + note ntext +) +create table record +( + id varchar(30) primary key, + cardId varchar(30) foreign key references card(id), + ComputerId varchar(30) foreign key references computer(id), + beginTime datetime, + endTime datetime, + fee money +) + --上网卡信息表 +insert into card(ID,pasword,balance,userName) +select '0023_ABC','555','98','张军'union +select '0025_bbd','abe','300','朱俊'union +select '0036_CCD','何柳','100','何柳'union +select '0045_YGR','0045_YGR','58','证验'union +select '0078_RJV','55885fg','600','校庆'union +select '0089_EDE','张','134','张峻' +--网吧机器说明表 +insert into computer(id,onUse,note) +select '02','0','25555' union +select '03','1','55555' union +select '04','0','66666' union +select '05','1','88888' union +select '06','0','688878' union +select 'B01','0','558558' +--上网记录表 +insert into record(ID,cardId,ComputerId,beginTime,endTime,fee) +select '23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20' union +select '34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23' union +select '45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6' union +select '47','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6' union +select '55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50' union +select '64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','3' union +select '65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23' union +select '98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23' + +select * from card +select * from computer +select * from record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select b.id 卡号,b.userName 用户名,c.id 机器编号, a.beginTime 开始时间,a.endTime 结束时间,a.fee 消费金额 from record a inner join card b on a.cardId=b.id +inner join computer c on c.id=a.ComputerId where b.userName='张军' order by a.fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select b.id 机器,count(*)上网次数,sum(c.fee)消费的总金额 from computer b left join record c on b.id=c.ComputerId group by b.id,c.fee order by c.fee desc +--3. 查询出所有已经使用过的上网卡的消费总金额 +select b.id, sum(b.fee)消费总金额 from card a inner join record b on a.id=b.cardId group by b.id +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id,userName from card where id in (select id from record group by id ) +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from card where userName = pasword +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 max(ComputerId) 机器号,count(*)次数 from record group by ComputerId order by count(*) desc +----7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select a.id 卡号,a.userName 用户名,b.ComputerId 机器号,b.fee 消费金额 from card a inner join record b on a.id=b.cardId where a.id like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select a.id 卡号,a.userName 用户名,b.ComputerId 机器号,b.beginTime 开始时间,b.endTime 结束时间 ,b.fee 消费金额 from card a inner join record b on a.id=b.cardId where b.beginTime =CONVERT(datetime,b.beginTime) group by b.beginTime,a.id,a.userName,b.ComputerId,b.beginTime,b.endTime,b.fee +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select a.id 卡号,a.userName 用户名,b.ComputerId 机器号,b.beginTime 开始时间,b.endTime 结束时间,b.fee 消费金额 from card a inner join record b on a.id=b.cardId where b.beginTime-b.endTime>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 a.id 卡号,a.userName 用户名,b.ComputerId 机器号,b.beginTime 开始时间,b.endTime 结束时间,b.fee 消费金额 from card a inner join record b on a.id=b.cardId order by b.fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..111bddd4e29f6f2b59537f9a9119cf0643c21aee --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" @@ -0,0 +1 @@ +use master go create database ClassInfo on ( name= 'ClassInfo', filename='D:\SQL代码\ClassInfo.mdf', size=5mb, maxsize=500mb, filegrowth=10% ) log on ( name= 'ClassInfo_ldf', filename='D:\SQL代码\ClassInfo_ldf.ldf', size=5mb, maxsize=500mb, filegrowth=10% ) use ClassInfo go --学生信息表(stuInfo) create table stuInfo ( stuID int primary key identity(1,1), stuName nvarchar(20) not null, stuAge int not null, stuSex int check(stuSex=1 or stuSex=0) not null, time datetime ) insert into stuInfo(stuName,stuAge,stuSex,time) values('Tom',19,1,null), ('Jack',20,0,null), ('Rose',21,1,null), ('Lulu',19,1,null), ('Lili',21,0,null), ('abc',20,1,'2007-01-07 01:11:36.590') select * from stuInfo --课程信息表(courseInfo) create table courseInfo ( courseID int primary key identity(1,1), courseName nvarchar(20) not null, courseMarks int not null ) insert into courseInfo(courseName,courseMarks) values('JavaBase',4), ('HTML',2), ('JavaScript',2), ('SqlBase',2) select * from courseInfo --分数信息表(scoreInfo) create table scoreInfo ( scoreID int primary key identity(1,1), stuID int not null, courseID int not null, score int not null ) insert into scoreInfo(stuID,courseID,score) values(1,1,80), (1,2,85), (1,4,50), (2,1,75), (2,3,45), (2,4,75), (3,1,15), (4,1,95), (4,2,75), (4,3,90), (4,4,45) select * from scoreInfo --题目: --1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 select stuName , count(courseID)课程的数量,avg(score)平均分 from stuInfo inner join scoreInfo on stuInfo.stuID=scoreInfo.stuID group by stuName --2.查询出每门课程的选修的学生的个数和学生考试的总分 select courseName,count(StuID)学生的个数 , sum(score)考试的总分 from scoreInfo inner join courseInfo on scoreInfo.courseID=courseInfo.courseID group by courseName --3.查询出性别一样并且年龄一样的学生的信息 select * from StuInfo where stuAge in(select stuAge from StuInfo group by stuAge,stuSex having COUNT(stuAge)>1 and COUNT(stuSex)>1) --4.查询出学分一样的课程信息 select * from courseInfo where courseMarks in(select courseMarks from courseInfo group by courseMarks having COUNT(courseMarks)>1) --5.查询出参加了考试的学生的学号,姓名,课程号和分数 select stuInfo.stuID,stuName,courseInfo.courseID,score from stuInfo inner join scoreInfo on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID --6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 select stuInfo.stuID,stuName,courseInfo.courseID,courseName,courseMarks,score from stuInfo inner join scoreInfo on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID --7.查询出没有参加考试的学生的学号和姓名 select stuInfo.stuID,stuName,courseName from stuInfo left join courseInfo on stuInfo.stuID=courseInfo.courseID where courseName is null --8.查询出是周六周天来报到的学生 --9.查询出姓名中有字母a的学生的信息 select * from stuInfo where stuName like '%a%' --10.查询出 选修了2门课程以上的 并且考试平均分在70以上 的学生的 学号和 考试平均分 以及 选修课程的数量 select * from stuInfo,scoreInfo select stuInfo.stuID,avg(score),count(courseInfo.courseID) from stuInfo inner join scoreInfo on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID group by stuInfo.stuID having count(courseInfo.courseID)>2 and avg(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..35dd7d606d3d6e7da7da5734b129bc2ba3bb9869 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery2.sql" @@ -0,0 +1,3 @@ +use master go --首先创建网吧计费系统的数据库,表结构和数据如2.bmp所示,表的说明如下: create database charge on ( name='charge', filename='D:\SQL代码\charge.mdf', size=5mb, maxsize=500mb, filegrowth=10% ) log on ( name='charge_ldf', filename='D:\SQL代码\charge_ldf.ldf', size=5mb, maxsize=500mb, filegrowth=10% ) use charge go --表一为上网卡信息表(tbl_card) --id: 卡号,主键 --passWord:密码 --balance:卡上的余额 --userName:该上网卡所属的用户名 create table tbl_card ( ID nvarchar(10) primary key not null, PassWord nvarchar(10) not null, Balance nvarchar(10) not null, UserName nvarchar(10) not null ) insert into tbl_card values ('0023_ABC','555','98','张军'), ('0025_bbd','abe','300','朱俊'), ('0036_CDD','何柳','100','何柳'), ('0045_YGR','0045_YGR','58','证验'), ('0078_RJV','55885fg','600','校庆'), ('0089_EDE','zhang','134','张骏') --表2为网吧机器说明表(tbl_computer) --id:机器编号,主键 --onUse:该机器是否正在使用,0为未使用,1为正在使用 --note:该机器的说明 create table tbl_computer ( ID nvarchar(10) not null primary key, OnUse int check(OnUse=1 or OnUse=0) not null, Note int not null ) insert into tbl_computer values ('02',0,25555), ('03',1,25555), ('04',0,66666), ('05',1,88888), ('06',0,688878), ('B01',0,558558) --表3为上网记录表(tbl_record): --id:记录编号,主键 --cardId:本次上网的卡号,外键 --ComputerId:本次上网记录所使用的机器号,外键 --beginTime:本次上网记录的开始时间 --endTime:本次上网记录的结束时间 --fee:本次上网的费用 create table tbl_record ( ID nvarchar(10) primary key not null, CardID nvarchar(10) not null foreign key references tbl_card(ID), ComputerID nvarchar(10) not null foreign key (ComputerID) references tbl_computer(ID), BeignTime datetime not null, EndTime datetime not null, Fee int not null ) insert into tbl_record values (23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20), (34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23), (45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), (46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00',6), (47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',60), (48,'0023_ABC','03','2007-01-06 15:26:00','2006-01-06 22:55:00',6), (55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), (64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), (65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), (98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) --请完成以下题目: select * from tbl_record --上网记录表(tbl_record) select* from tbl_computer --网吧机器说明表(tbl_computer) select * from tbl_card --上网卡信息表(tbl_card) --1. 查询出用户名为'张军'的上网卡的上网记录, --要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额, --并按消费金额降序排列 select tbl_card.ID 显示卡号,UserName 用户名,tbl_computer.ID 机器编号,BeignTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record +inner join tbl_card on tbl_record.CardID=tbl_card.ID +inner join tbl_computer on tbl_computer.Id=tbl_record.ComputerId where UserName='张军' order by fee DESC --2. 查询出每台机器上的上网次数和消费的总金额 select ComputerID, count(*)上网次数 ,sum(Fee)消费的总金额 from tbl_record group by ComputerID --3. 查询出所有已经使用过的上网卡的消费总金额 select tbl_card.ID,sum(Fee) from tbl_record inner join tbl_card on tbl_record.CardID=tbl_card.ID group by tbl_card.ID --4. 查询出从未消费过的上网卡的卡号和用户名 select tbl_card.ID,UserName from tbl_record right join tbl_card on tbl_record.CardID=tbl_card.ID where Fee is null group by tbl_card.ID,CardID,UserName,Fee --5. 将密码与用户名一样的上网卡信息查询出来 --select * from tbl_card A --INNER join tbl_card B on A.PassWord = B.PassWord where A.PassWord = A .UserName select * from tbl_card where PassWord=UserName --6. 查询出使用次数最多的机器号和使用次数 select top 1 count(*) 使用次数,ComputerID 机器号 from tbl_record group by ComputerID order by count(*) DESC select ComputerID , count(*) from tbl_record group by ComputerID having count(*)=5 --7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 select CardID 卡号,UserName 用户名,ComputerID 上网的机器号,Fee 消费金额 from tbl_record inner join tbl_card on tbl_record.CardID=tbl_card.ID where CardID like '%ABC' --8. 查询出是周六、周天上网的记录, --要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 --9. 查询成一次上网时间超过12小时的的上网记录, --要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 select CardId , userName , ComputerID,BeignTime , EndTime ,Fee from tbl_record inner join tbl_card on tbl_record.CardID=tbl_card.ID where (EndTime-BeignTime)>=12 --10. 查询消费金额排列前三名(最高)的上网记录, --要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 select Fee , CardID,UserName , ComputerID,BeignTime , EndTime from tbl_record inner join tbl_card on tbl_record.CardID=tbl_card.ID where Fee not in (select top 3 fee from tbl_record order by fee DESC) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery--HomeWork.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery--HomeWork.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ab8b45afbbd6cc5144429205a0264c4b6069adf2 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery--HomeWork.sql" @@ -0,0 +1,119 @@ +use master +go + +create database HomeWork +on +( + name='HomeWork', + filename='D:\SQL作业\SQL作业11\HomeWork.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='HomeWork_log', + filename='D:\SQL作业\SQL作业11\HomeWork_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use HomeWork +go +create table stuInfo +( + stuID int primary key identity(1,1), + stuName char(20) not null, + stuAge char(5) not null, + stuSex char(2) not null, + time time +) +go + +insert into stuInfo values('Tom',19,1,null),('Jack',20,0,null),('Rose',21,1,null), +('Lulu',19,1,null),('Lili',21,0,null),('abc',20,1,'2007-01-07 01:11:36.590') + +select * from stuInfo + +use HomeWork +create table courseInfo +( + courseID int primary key identity(1,1), + courseName nvarchar(15) not null, + courseMarks char(5) not null +) +go + +select * from courseInfo + +insert into courseInfo values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) + +select * from courseInfo + + +use HomeWork +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int constraint FK_scoreInfo_stuID references stuInfo(stuID), + courseID int not null, + score int not null +) +go + +insert into scoreInfo values(1,1,80),(1,2,85),(1,4,50),(2,1,75),(2,3,45), +(2,4,75),(3,1,45),(4,1,95),(4,2,75),(4,3,90),(4,4,45) + + +select * from stuInfo +select * from courseInfo +select * from scoreInfo + + +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName 学生,count(*)课程数量,avg(score)平均分 from scoreInfo +inner join stuInfo on stuInfo.stuID = scoreInfo.stuID +inner join courseInfo on scoreInfo.courseID = courseInfo.courseID group by stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 课程,count(*)学生个数,sum(score)考试总分 from scoreInfo +inner join courseInfo on scoreInfo.courseID = courseInfo.courseID group by courseName + +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A,stuInfo B where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuID!=B.stuID + +select s.* from StuInfo s where (select count(*) from StuInfo where stuAge=s.stuAge and stuSex=s.stuSex)>1 + +--4.查询出学分一样的课程信息 +select distinct A.* from courseInfo A,courseInfo B where A.courseMarks=B.courseMarks and A.courseID!=B.courseID + +select c.* from courseInfo c where (select count(*) from courseInfo where courseMarks=c.courseMarks)>1 + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select StuInfo.stuID 学号,stuName 姓名, courseInfo.courseID 课程号,score 分数 from scoreInfo +inner join StuInfo on scoreInfo.stuID = StuInfo.stuID +inner join courseInfo on scoreInfo.courseID = courseInfo.courseID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select StuInfo.stuID 学号,courseInfo.courseID 课程号, courseName 课程名,courseMarks 课程学分,score 分数 from scoreInfo +inner join StuInfo on scoreInfo.stuID = StuInfo.stuID +inner join courseInfo on scoreInfo.courseID = courseInfo.courseID + +--7.查询出没有参加考试的学生的学号和姓名 +select StuInfo.stuID 学号,stuName 姓名 from StuInfo left join scoreInfo on scoreInfo.stuID = StuInfo.stuID +where score is null + +--8.查询出是周六周天来报到的学生 +--I can't do it + +--9.查询出姓名中有字母a的学生的信息 +select * from StuInfo where stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuInfo.stuID 学号,avg(score)平均分,count(*)选修课程数量 from stuInfo +inner join scoreInfo on stuInfo.stuID = scoreInfo.stuID group by stuInfo.stuID +having count(*)>2 and avg(score)>70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery--WB.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery--WB.sql" new file mode 100644 index 0000000000000000000000000000000000000000..dd0399357735a9ce9d6ca8f0e1cabfcd100562d8 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery--WB.sql" @@ -0,0 +1,121 @@ +use master +go +create database WB +on +( + name='WB', + filename='D:\SQL作业\SQL作业11\WB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='WB_log', + filename='D:\SQL作业\SQL作业11\WB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use WB +go +create table tbl_card +( + id varchar(25) primary key, + [passWord] varchar(30) not null, + balance money not null, + userName varchar(20) not null +) +go + + +insert into tbl_card values('0023_ABC','555',98,'张军'),('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'),('0045_YGR','0045_YGR',58,'验证'), +('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张峻') + +select * from tbl_card + +use WB +create table tbl_computer +( + id varchar(20) primary key, + onUse char(2) check(onUse=0 or onUse=1), + note text +) +go + +insert into tbl_computer values('02','0','25555'),('03','1','55555'),('04','0','66666'), +('05','1','88888'),('06','0','688878'),('B01','0','558558') + +select * from tbl_computer + +use WB +create table tbl_record +( + id varchar(30) primary key, + cardId varchar(25) constraint FK_tbl_record_cardId references tbl_card(id), + ComputerId varchar(20) constraint FK_tbl_record_ComputerId references tbl_computer(id), + beginTime time, + endTime time, + fee money +) +go + +insert into tbl_record(id,cardId,ComputerId,beginTime,endTime,fee) values('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), +('34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), +('45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +('46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6'), +('47','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +('48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6'), +('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), +('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','3'), +('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), +('98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') + +select * from tbl_card +select * from tbl_computer +select * from tbl_record + +--请完成以下题目: +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select tbl_card.id 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_card +inner join tbl_record on tbl_card.id = tbl_record.cardId +where userName='张军' order by fee desc + +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId 机器,count(*)次数,sum(fee)总金额 from tbl_record group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId 卡号,sum(fee)总金额 from tbl_record group by cardId + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select cardId 卡号,userName 用户名 from tbl_record +right join tbl_card on tbl_card.id = tbl_record.cardId where tbl_card.id not in(select cardId from tbl_record) + +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card A,tbl_card B where A.passWord = B.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 使用最多的机器号,count(*)次数 from tbl_record group by ComputerId order by count(*) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_card.id 卡号,userName 用户名,ComputerId 机器号,fee 金额 from tbl_card +inner join tbl_record on tbl_card.id = tbl_record.cardId where tbl_card.id like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +--I can't do it + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.id 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 金额 from tbl_card +inner join tbl_record on tbl_card.id = tbl_record.cardId + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 金额 from tbl_record +inner join tbl_card on tbl_card.id = tbl_record.cardId where fee not in(select top 3 fee from tbl_record order by fee desc) + +select top 3 fee from tbl_record order by fee desc + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..08ec52f9f827e770b76973793624c9ad2a5a8cbc --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery1.sql" @@ -0,0 +1,83 @@ +create database bbs +go +use bbs +go + +--学生信息表 +create table stuInfo +( + stuID int primary key identity, + stuName varchar(20), + stuAge int, + stuSex int, + time datetime, +) +--插入数据 +insert into stuInfo(stuName,stuAge,stuSex) values ('Tom',19,1),('Jack',20,0),('Rose',21,1), +('Lulu',19,1),('Lili',21,0) +insert into stuInfo(stuName,stuAge,stuSex,time) values ('abc',20,1,'2007-01-07 01:11:36.590') + +--课程信息表 +create table courseInfo +( + courseID int primary key identity, + courseName varchar(20), + courseMarks int, +) +insert into courseInfo values ('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) + +--分数信息表 +create table scoreInfo +( + scoreID int primary key identity, + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int , +) +insert into scoreInfo values(1,1,80),(1,2,85),(1,4,50),(2,1,75),(2,3,45), +(2,4,75),(3,1,45),(4,1,95),(4,2,75),(4,3,90),(4,4,45) + +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuInfo.stuID, count(*)课程数量,avg(score) 考试平均分 from stuInfo inner join scoreInfo +on stuInfo.stuID = scoreInfo.stuID group by stuInfo.stuID + +--2.查询出 每门课程的选修的学生的个数和学生考试的总分 +select scoreInfo.courseID,count(*)学生个数, sum(score)考试总分 from courseInfo inner join scoreInfo +on courseInfo.courseID = scoreInfo.courseID group by scoreInfo.courseID + +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A,stuInfo B where A.stuSex=B.stuSex and A.stuAge=B.stuAge and A.stuID != B.stuID + +--4.查询出学分一样的课程信息 +select distinct A.* from courseInfo A,courseInfo B where A.courseMarks=B.courseMarks AND A.courseID !=B.courseID + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID 学号,stuInfo.stuName 姓名,courseInfo.courseID 课程号,scoreInfo.score 分数 +from scoreInfo inner join stuInfo on stuInfo.stuID = scoreInfo.stuID +inner join courseInfo on courseInfo.courseID = scoreInfo.courseID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID 学号,stuInfo.stuName 姓名,courseInfo.courseID 课程号,courseInfo.courseName 课程名,courseInfo.courseMarks 课程学分, scoreInfo.score 分数 +from scoreInfo inner join stuInfo on stuInfo.stuID = scoreInfo.stuID +inner join courseInfo on courseInfo.courseID = scoreInfo.courseID + +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID 学号,stuInfo.stuName 姓名 from scoreInfo right join stuInfo on scoreInfo.stuID=stuInfo.stuID +except +select stuInfo.stuID 学号,stuInfo.stuName 姓名 from scoreInfo +right join stuInfo on scoreInfo.stuID=stuInfo.stuID where score is not null + +--8.查询出是周六周天来报到的学生 + + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuInfo.stuID,stuInfo.stuName 姓名,count(*) 所选修的课程,avg(score)考试平均分 from scoreInfo +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +inner join stuInfo on stuInfo.stuID=scoreInfo.stuID group by stuInfo.stuName , stuInfo.stuID having count(*)>2 and avg(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d30e32ec336051b06d746d83305c33fb5a4de4be --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery2.sql" @@ -0,0 +1,84 @@ +create database HZT +go +use HZT +go +create table tbl_card +( + ID varchar(20) primary key , + passWord nvarchar(10), + banlance int, + userName nvarchar(10), +) +create table tbl_computer +( + Id varchar(5) primary key , + onUse int , + note text, +) +create table tbl_record +( + ID int primary key , + cardId varchar(20) references tbl_card(ID), + ComputerId varchar(5) references tbl_computer(Id), + beginTime datetime, + endTime datetime, + fee int, +) + +insert into tbl_card values('0023_ABC','555',98,'张军'),('0025_bbd','abe',300,'朱俊'),('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张峻') +insert into tbl_computer values('02',0,'25555'),('03',1,'55555'),('04',0,'66666'),('05',1,'88888'), +('06',0,'688878'),('B01',0,'558558') +insert into tbl_record values(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20), +(34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23), +(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00',6), +(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) + +select * from tbl_card +select * from tbl_computer +select * from tbl_record + + +--1.查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名, +--机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select tbl_card.ID 卡号,userName 用户名,computerId 机器编号,tbl_record.beginTime 开始时间, tbl_record.endTime 结束时间,tbl_record.fee 消费金额 from tbl_record +inner join tbl_card on tbl_record.cardId =tbl_card.ID where userName='张军' order by fee desc + +--2. 查询出 每台机器 上的上网次数和消费的总金额 +select tbl_computer.Id,count(*)上网的次数,sum(fee)总金额 from tbl_record +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id group by tbl_computer.Id + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select tbl_record.cardId,sum(fee)总金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID group by tbl_record.cardId + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select ID 卡号,userName 用户名 from tbl_card + +-- 5. 将密码与用户名一样的上网卡信息查询出来 +select A.* from tbl_card A,tbl_card B where A.userName=B.passWord + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 tbl_record.ComputerId,count(*) 使用次数 from tbl_record +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id +group by tbl_record.ComputerId order by count(*) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_card.ID 卡号,tbl_card.userName 用户名,tbl_computer.Id 上网的机器号,fee 消费金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id where tbl_card.ID like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 30 percent tbl_card.ID 上网的卡号,tbl_card.userName 用户名,tbl_computer.Id 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id order by fee DESC \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\344\275\234\344\270\2321.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..511404bdaedd27187516882987e09d953f5fc7ca --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\344\275\234\344\270\2321.sql" @@ -0,0 +1,117 @@ +use master +go + +create database TestDB01 +on +( + name='ordersystem', + filename='E:\数据库\TestDB01.mdf', + size=5mb, + maxsize=100mb, + filegrowth=15% +) + +log on +( + name='bank_log', + filename='E:\数据库\TestDB01_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=15% +) + +use TestDB01 +go + +create table stuInfo +( + stuID int primary key identity(1,1), + stuName nvarchar(10) not null, + stuAge nchar(3) not null, + stuSex nvarchar(2) not null, + time datetime +) + +create table courseInfo +( + courseID int primary key identity(1,1), + courseName nchar(20) not null, + courseMarks int +) + +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int foreign key references stuInfo(stuID), + courseID int foreign key references courseInfo(courseID), + score int not null +) +select * from stuInfo +insert into stuInfo(stuName,stuAge,stuSex) values +('Tom','19','1'),('Jack','20','0'), +('Rose','21','1'),('Lulu','19','1'), +('Lili','21','0'),('abc','20','1') +update stuInfo set time='2007-01-07 01:11:36.590' where stuID='6' + +select * from courseInfo +insert into courseInfo values +('JavaBase',4),('HTML',2),('JavaaScript',2),('SqlBase',2) + +select * from scoreInfo +insert into scoreInfo(stuID,courseID,score) values +(1,'1','80'),(1,'2','85'),(1,'4','50'), +(2,'1','75'),(2,'3','45'),(2,'4','75'), +(3,'1','45'),(4,'1','95'),(4,'2','75'), +(4,'3','90'),(4,'4','45') + + +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +select * from stuInfo +select * from courseInfo +select * from scoreInfo +--题目: +--1.查询出 每个学生 所选修的课程的数量 和 所选修的课程的 考试的平均分 +select stuInfo.stuID,stuName,COUNT(scoreInfo.courseID)数量,AVG(score)平均分 from stuInfo inner join scoreInfo on stuInfo.stuID= scoreInfo.stuID +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID group by stuInfo.stuID,stuName + +--2.查询出 每门课程的选修的 学生的个数 和 学生考试的总分 +select cu.courseID,courseName,COUNT(st.stuID)学生的个数,SUM(score)总分 from stuInfo st inner join scoreInfo sc on st.stuID= sc.stuID +inner join courseInfo cu on sc.courseID=cu.courseID group by cu.courseID,courseName + +--3.查询出 性别 一样并且 年龄 一样的学生的信息 +select A.* from StuInfo A ,stuInfo B where +A.stuSex=B.stuSex and A.stuAge=B.stuAge and A.stuID!=B.stuID + +--4.查询出学分一样的课程信息 +select A.courseID,A.courseName,A.courseMarks from courseInfo A,courseInfo B where A.courseMarks=B.courseMarks and A.courseID!=B.courseID +group by A.courseID,A.courseName,A.courseMarks + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select A.stuID,stuName,C.courseID,courseName,score from stuInfo A inner join scoreInfo B +on A.stuID=B.stuID inner join courseInfo C on B.courseID=C.courseID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select A.stuID,C.courseID,courseName,courseMarks,score from stuInfo A inner join scoreInfo B +on A.stuID=B.stuID inner join courseInfo C on B.courseID=C.courseID + + +--7.查询出没有参加考试的学生的学号和姓名 +select A.stuID,stuName from stuInfo A left join scoreInfo B +on A.stuID=B.stuID left join courseInfo C on B.courseID=C.courseID group by A.stuID,stuName +except +select A.stuID,stuName from stuInfo A inner join scoreInfo B +on A.stuID=B.stuID inner join courseInfo C on B.courseID=C.courseID group by A.stuID,stuName + +--8.查询出是周六周天来报到的学生 +select * from stuInfo where time='2007-01-07 01:11:36.590' + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like 'a%' + +--10.查询出选修了 2门课程以上 的并且 考试平均分在 70以上 的学生的 学号 和 考试平均分 以及 选修课程的数量 +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +select stuInfo.stuID,stuName,AVG(score)平均分,COUNT(scoreInfo.courseID)数量 from stuInfo inner join scoreInfo on stuInfo.stuID= scoreInfo.stuID +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID group by stuInfo.stuID,stuName having COUNT(scoreInfo.courseID)>2 and AVG(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\344\275\234\344\270\2322.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9055f400a21d256fdb577120a03e40f18a346f9c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\344\275\234\344\270\2322.sql" @@ -0,0 +1,125 @@ +--首先创建网吧计费系统的数据库,表结构和数据如2.bmp所示,表的说明如下: +use master +go + +create database cybercaf +on +( + name='cybercaf', + filename='E:\数据库\cybercaf.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) + +log on +( + name='cybercaf_log', + filename='E:\数据库\cybercaf_log.ldf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) + +use cybercaf +go + +--表一为上网卡信息表(tbl_card) +--id: 卡号,主键 +--passWord:密码 +--balance:卡上的余额 +--userName:该上网卡所属的用户名 +create table tbl_card +( + id nchar(15) primary key not null, + password nchar(20) not null, + balance nchar(10) not null, + userName nvarchar(10) not null +) + +--表2为网吧机器说明表(tbl_computer) +--id:机器编号,主键 +--onUse:该机器是否正在使用,0为未使用,1为正在使用 +--Note:该机器的说明 +create table tbl_computer +( + id nchar(5) primary key not null, + onUse nchar(1) not null, + Note nchar(20) +) + +--表3为上网记录表(tbl_record): +--id:记录编号,主键 +--cardId:本次上网的卡号,外键 +--ComputerId:本次上网记录所使用的机器号,外键 +--beginTime:本次上网记录的开始时间 +--endTime:本次上网记录的结束时间 +--fee:本次上网的费用 +create table tbl_record +( + id int primary key not null, + cardId nchar(15) foreign key references tbl_card(id), + ComputerId nchar(5) foreign key references tbl_computer(id), + beginTime datetime not null, + endTime datetime not null, + fee int not null +) + +select * from tbl_card +insert into tbl_card values +('0023_ABC','555','98','张军'),('0025_bbd','abe','300','朱俊'), +('0036_CCD','何柳','100','何柳'),('0045_YGR','0045_YGR','58','证验'), +('0078_RJV','55885fg','600','校庆'),('0089_EDE','zhang','134','张峻') + +select * from tbl_computer +insert into tbl_computer values ('02','0','25555'),('03','1','55555'), +('04','0','66666'),('05','1','88888'),('06','0','688878'),('B01','0','558558') + +select * from tbl_record +insert into tbl_record(id,cardId,ComputerId,beginTime,endTime,fee) values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20), +(24,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23), +(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00',6), +(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) +--请完成以下题目: +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select A.id,userName,ComputerId,beginTime,endTime,fee from tbl_card A inner join tbl_record on A.id=cardId where userName='张军' order by fee + +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId,COUNT(ComputerId)上网次数,SUM(fee)消费的总金额 from tbl_record group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select onUse,sum(fee)消费总金额 from tbl_computer A inner join tbl_record B on A.id=ComputerId where onUse='1' group by onUse + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select A.id,userName from tbl_card A inner join tbl_record B on A.id=B.cardId +inner join tbl_computer C on B.ComputerId=C.id where onUse='0' + +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card where password=userName + +--6. 查询出 使用次数最多的机器号和使用次数 +select ComputerId,COUNT(ComputerId)使用次数 from tbl_computer A inner join tbl_record B on A.id = B.ComputerId group by ComputerId +having COUNT(ComputerId)=5 + +--7. 查询出卡号是以'ABC'结尾的 卡号,用户名,上网的机器号和消费金额 +select A.id,userName,ComputerId,fee from tbl_card A inner join tbl_record B on A.id=B.cardId where A.id like '%ABC%' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select B.id,userName,ComputerId,beginTime,endTime,fee from tbl_record A inner join tbl_card B on A.cardId=B.id + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select B.id,userName,ComputerId,beginTime,endTime,fee from tbl_record A inner join tbl_card B on A.cardId=B.id +where endTime-beginTime>'1900-01-01 12:00:00.000' + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select B.id,userName,ComputerId,beginTime,endTime,fee from tbl_record A inner join tbl_card B on A.cardId=B.id +where fee!=50 order by fee DESC + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" new file mode 100644 index 0000000000000000000000000000000000000000..71546008176fbd74096d2053613c6e313f6390dd --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" @@ -0,0 +1,104 @@ + +create database Jxc + +on +( + name='Jxc', + filename='D:\Jxc.mdf' +) +log on +( + name='Jxc_log', + filename='D:\Jxc.ldf_log' +) +use Jxc +create table stuInfo +( + stuID int primary key , + stuName varchar(20), + stuAge int , + stuSex nvarchar(1), + stime datetime +) + +create table courseInfo +( + courseID int primary key , + courseName varchar(25), + courseMarks varchar(5) +) +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int foreign key references stuInfo(stuID) , + courseID int foreign key references courseInfo(courseID), + score int +) +--学生信息表 +insert into stuInfo(stuID,stuName,stuAge,stuSex) +select 1,'Tom',19,1 union +select 2,'Jack',20,0union +select 3,'Rose',21,1union +select 4,'Lulu',19,1 union +select 5,'Lili',21,0union +select 6,'abc',20,1 + +update stuInfo set stime='2007-01-07 01:11:36.590'where stuName='abc' +--课程表 +insert into courseInfo(courseID,courseName,courseMarks) +select 1,'JavaBase',4 union +select 2,'HTML',2 union +select 3,'JavaScript',2 union +select 4,'SqlBase',2 +-- 成绩表 +insert into scoreInfo(stuID,courseID,score) +select 1,1,80 union +select 1,2,85 union +select 1,4,50 union +select 2,1,75 union +select 2,3,45 union +select 2,4,75 union +select 3,1,45 union +select 4,1,95 union +select 4,2,75 union +select 4,3,90 union +select 4,4,45 + +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +select * from stuInfo --学生信息表 +select * from courseInfo --课程表 +select * from scoreInfo -- 成绩表 +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuID,count(courseID)课程的数量,avg(score)平均分 from scoreInfo group by stuID +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseID 课程, count(courseID)选修个数, sum(score)总分 from scoreInfo group by courseID + +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A, stuInfo B +where A.stuSex=B.stuSex AND A.stuAge=B.stuAge AND A.stuID!=B.stuID +--4.查询出学分一样的课程信息 +select distinct A.* from courseInfo A,courseInfo B +where A.courseMarks=B.courseMarks AND A.courseID!=B.courseID +--5.查询出参加了 考试的学生的学号,姓名,课程号和分数 +select scoreInfo.stuID 学号,stuName 姓名,courseID 课程号,score from scoreInfo +left join stuInfo on scoreInfo.stuID=stuInfo.stuID group by scoreInfo.stuID,stuName,courseID,score +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select scoreInfo.stuID 学号, scoreInfo.courseID 课程号,courseInfo.courseName 课程名,courseMarks 学分,scoreInfo.score 分数 from scoreInfo +left join courseInfo on scoreInfo.courseID=courseInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select Stuinfo.stuID 学号,stuInfo.stuName 姓名 from stuInfo +left join scoreInfo on stuInfo.stuID=scoreInfo.stuID +where score is null +--8.查询出是周六周天来报到的学生 + --超纲了!!!!!!!!!!!!!!!! +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出 选修了2门课程 以上的 并且 考试平均分 在70以上 的学生的 学号 和 考试平均分 以及 选修课程的数量 +select stuID,count(courseID)课程的数量,avg(score)平均分 from scoreInfo +group by stuID +having avg(score)>=70 and count(courseID)>=2 + +select * from courseInfo --课程表 +select * from scoreInfo -- 成绩表 +select * from stuInfo --学生信息表 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6e8e82ed43dee2250dee541174cae97b815f427e --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" @@ -0,0 +1,107 @@ +create database wb +on +( + name='wb', + filename='D:\wb.mdf' +) +log on +( + name='wb_log', + filename='D:\wb_log.ldf' +) + +use wb +go + +create table tbl_card +( + id varchar(30) primary key, + pasword varchar(30), + balance money, + userName varchar(50) +) + +create table tbl_computer +( + id varchar(30) primary key, + onUse varchar(2) check(onUse=0 or onUse=1), + note ntext +) + +create table tbl_record +( + id varchar(30) primary key, + cardId varchar(30) foreign key references tbl_card(id), + ComputerId varchar(30) foreign key references tbl_computer(id), + beginTime datetime, + endTime datetime, + fee money +) + +insert into tbl_card(ID,pasword,balance,userName) +select '0023_ABC','555','98','张军'union +select '0025_bbd','abe','300','朱俊'union +select '0036_CCD','何柳','100','何柳'union +select '0045_YGR','0045_YGR','58','证验'union +select '0078_RJV','55885fg','600','校庆'union +select '0089_EDE','张','134','张峻' + +insert into tbl_computer(id,onUse,note) +select '02','0','25555' union +select '03','1','55555' union +select '04','0','66666' union +select '05','1','88888' union +select '06','0','688878' union +select 'B01','0','558558' + +insert into tbl_record(ID,cardId,ComputerId,beginTime,endTime,fee) +select '23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20' union +select '34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23' union +select '45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6' union +select '47','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6' union +select '55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50' union +select '64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','3' union +select '65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23' union +select '98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23' + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + +select tbl_card.id 卡号,userName 用户名, tbl_computer.id 机器编号, beginTime 开始时间, endTime 结束时间, fee 消费金额 from tbl_record +inner join tbl_card on tbl_record.cardId = tbl_card.id +inner join tbl_computer on tbl_card.id = tbl_record.ComputerId where tbl_card.userName = '张军' + +--2. 查询出每台机器上的上网次数和消费的总金额 + +select ComputerId 机器,count(cardId) 次数,sum(fee) 金额 from tbl_record group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 + +select cardId 网卡,sum(fee) 金额 from tbl_record group by cardId + +--4. 查询出从未消费过的上网卡的卡号和用户名 + +select tbl_card.id,userName from tbl_card +left join tbl_record on tbl_card.id=tbl_record.cardId +where fee is null group by userName,tbl_card.id + +--5. 将密码与用户名一样的上网卡信息查询出来 + +select * from tbl_card where userName = pasword + +--6. 查询出使用次数最多的机器号和使用次数 + +select top 1 max(ComputerId) 机器号,count(cardId)次数 from tbl_record group by ComputerId order by count(cardId) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + +select tbl_card.id 卡号,tbl_card.userName 用户名,tbl_record.ComputerId 机器号,tbl_record.fee 消费金额 from tbl_card inner join tbl_record on tbl_card.id=tbl_record .cardId where tbl_card.id like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select top 3 cardId 卡号,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record order by fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery14.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery14.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e873c7ce090e5e99e6a4515ff0f54c72e3bd416c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery14.sql" @@ -0,0 +1,92 @@ +create database Student +on +( +name='Student', +filename='D:\sql.mdl', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) +log on +( +name='Student_log', +filename='D:\sql.ldf', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) + +use Student +go + +create table stuInfo +( +stuID int primary key identity(1,1) not null, +stuName varchar(20) not null, +stuAge int not null, +stuSex char(2) not null, +time datetime +) + +create table courseInfo +( +courseID int primary key identity(1,1) not null, +courseName varchar(20) not null, +courseMarks int not null +) + +create table scoreInfo +( +scoreID int primary key identity(1,1) not null, +stuID int not null, +courseID int not null, +score int not null, +) + +insert into stuInfo(stuName,stuAge,stuSex) values ('Tom','19','1'),('Jack','20','0'), +('Rose','21','1'),('Lulu','19','1'),('Lili','21','0') +insert into stuInfo values('abc','20','1','2007-01-07 01:11:36.590') + +insert into courseInfo values ('JavaBase','4'),('HTML','2'),('JavaScript','2'),('SqlBase','2') + +insert into scoreInfo values ('1','1','80'),('1','2','85'),('1','4','50'),('2','1','75'),('2','3','45'),('2','4','75'), +('3','1','45'),('4','1','95'),('4','2','75'),('4','3','90'),('4','4','45') + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + +select stuID 学生,count(courseID) 数量,AVG(score) 平均分 from scoreInfo group by stuID + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + +select courseID 课程,count(stuID) 学生个数,sum(score) 总分 from scoreInfo group by courseID + +--3.查询出性别一样并且年龄一样的学生的信息 + +select * from stuInfo where stuAge in (select stuAge from stuInfo group by stuAge,stuSex having count(stuAge)>1) + +--4.查询出学分一样的课程信息 + +select * from courseInfo where courseMarks in (select courseMarks from courseInfo group by courseMarks having count(courseMarks)>1) + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + +select scoreInfo.stuID 学号,stuName 姓名,courseID 课程,score 分数 from scoreInfo inner join stuInfo on scoreInfo.stuID=stuInfo.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + +select stuID 学号,courseInfo.courseID 课程号,courseName 课程名,courseMarks 学分,score 分数 from courseInfo inner join scoreInfo on courseInfo.courseID=scoreInfo.courseID + +--7.查询出没有参加考试的学生的学号和姓名 + +select Stuinfo.stuID 学号,stuInfo.stuName 姓名 from stuInfo left join scoreInfo on stuInfo.stuID=scoreInfo.stuID where score is null + +--8.查询出是周六周天来报到的学生 + + +--9.查询出姓名中有字母a的学生的信息 + +select * from stuInfo where stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 + +select stuID 学号,count(courseID) 数量,AVG(score) 平均分 from scoreInfo group by stuID having count(courseID)>=2 and AVG(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\344\275\234\344\270\2321.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a1d5a54a0174b325bc859ec955d8892c31c361c5 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\344\275\234\344\270\2321.sql" @@ -0,0 +1,77 @@ +create table stuInfo +( +stulD int not null, +stuName varchar(20) not null, +stuAge int not null, +stuSex int not null, +time datetime not null +) +create table courseInfo +( +courselD int not null, +courseName varchar(20) not null, +courseMarks int not null +) +create table scoreInfo +( +scoreID int not null, +stuID int not null, +courseID int not null, +score int not null +) + +insert into stuInfo values +('1','Tom','19','1',''),('2','jack','20','0',''), +('3','Rose','21','1',''),('4','Lulu','19','1','') +,('5','Lili','21','0',''),('6','abc','20','1','2007-01-07 01:11:36.590') + + +insert into courseInfo values +('1','javaBase','4'),('2','HTML','2') +,('3','javaScript','2'),('4','SqIBase','2') + + +insert into scoreInfo values +('1','1','1','80'),('2','1','2','85'), +('3','1','4','50'),('4','2','1','75'), +('5','2','3','45'),('6','2','4','75'), +('7','3','1','45'),('8','4','1','95'), +('9','4','2','75'),('10','4','3','90'), +('11','4','4','45') + +select * from stuInfo +select * from courseInfo +select * from scoreInfo + + + +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuID,count(courseID)课程的数量,avg(score)平均分 from scoreInfo group by stuID +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseID 课程, count(courseID)选修个数, sum(score)总分 from scoreInfo group by courseID +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A, stuInfo B +where A.stuSex=B.stuSex AND A.stuAge=B.stuAge AND A.stulD!=B.stulD +--4.查询出学分一样的课程信息 +select distinct A.* from courseInfo A,courseInfo B +where A.courseMarks=B.courseMarks AND A.courselD!=B.courselD +--5.查询出参加了 考试的学生的学号,姓名,课程号和分数 +select scoreInfo.stuID 学号,stuName 姓名,courseID 课程号,score from scoreInfo +left join stuInfo on scoreInfo.stuID=stuInfo.stulD group by scoreInfo.stuID,stuName,courseID,score +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select scoreInfo.stuID 学号, scoreInfo.courseID 课程号,courseInfo.courseName 课程名,courseMarks 学分,scoreInfo.score 分数 from scoreInfo +left join courseInfo on scoreInfo.courseID=courseInfo.courselD +--7.查询出没有参加考试的学生的学号和姓名 +select Stuinfo.stulD 学号,stuInfo.stuName 姓名 from stuInfo +left join scoreInfo on stuInfo.stulD=scoreInfo.stuID +where score is null +--8.查询出是周六周天来报到的学生 + --超纲了!!!!!!!!!!!!!!!! +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出 选修了2门课程 以上的 并且 考试平均分 在70以上 的学生的 学号 和 考试平均分 以及 选修课程的数量 +select stuID,count(courseID)课程的数量,avg(score)平均分 from scoreInfo +group by stuID +having avg(score)>=70 and count(courseID)>=2 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\344\275\234\344\270\2322.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..141af5580e018483b666582c38250bb7b4747e62 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\344\275\234\344\270\2322.sql" @@ -0,0 +1,113 @@ +use master +go +create database wb +on +( + name='wb', + filename='D:\wb.mdf' +) +log on +( + name='wb_log', + filename='D:\wb_log.ldf' +) +go +use wb +go + +create table card +( + id varchar(30) primary key, + pasword varchar(30), + balance money, + userName varchar(50) +) +create table computer +( + id varchar(30) primary key, + onUse varchar(2) check(onUse=0 or onUse=1), + note ntext +) +create table record +( + id varchar(30) primary key, + cardId varchar(30) foreign key references card(id), + ComputerId varchar(30) foreign key references computer(id), + beginTime datetime, + endTime datetime, + fee money +) + + +insert into card(ID,pasword,balance,userName) +select '0023_ABC','555','98','张军'union +select '0025_bbd','abe','300','朱俊'union +select '0036_CCD','何柳','100','何柳'union +select '0045_YGR','0045_YGR','58','证验'union +select '0078_RJV','55885fg','600','校庆'union +select '0089_EDE','张','134','张峻' + + +insert into computer(id,onUse,note) +select '02','0','25555' union +select '03','1','55555' union +select '04','0','66666' union +select '05','1','88888' union +select '06','0','688878' union +select 'B01','0','558558' + + +insert into record(ID,cardId,ComputerId,beginTime,endTime,fee) +select '23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20' union +select '34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23' union +select '45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6' union +select '47','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6' union +select '55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50' union +select '64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','3' union +select '65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23' union +select '98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23' + + + +select * from card +select * from computer +select * from record + + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号, +--用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select card.id 卡号, card.userName 用户名 ,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from card inner join record on card.id=record.cardId where userName='张军' order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId 机器,count(cardId)上网次数,sum(fee)总金额 from record group by cardId,ComputerId +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId,sum(record.fee) 总金额 from record group by cardId + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id,userName from card where id not in(select distinct cardId from record) + +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from card where id=pasword + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号,count(ComputerId)使用次数 from record +group by ComputerId order By count(ComputerId) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器号, fee 消费金额 +from record inner join card on record.cardId=card.id where record.cardId like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号, +--用户名,机器号,开始时间、结束时间和消费金额 +select cardId,userName,P.id,beginTime,endTime,fee from record inner join computer P +on ComputerId=P.id inner join card C on C.id=cardId where datepart(weekday,beginTime)=1 or datepart(weekday,beginTime)=7 + +--9. 查询成一次上网时间超过12小时的的上网记录, +--要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId,userName,P.id,beginTime,endTime,fee from record inner join computer P on ComputerId=P.id inner join card C on C.id=cardId where datediff(hour,beginTime,endTime)>12 + +--10. 查询除消费金额排列前三名(最高)的上网记录, +--要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 cardId 卡号,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from record order by fee desc diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4a7e6505e59a6b3b0307437119deadd2207308e4 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" @@ -0,0 +1,81 @@ +use master +go +create database py2 +go +use py2 +go +create table card +( + id varchar(30) primary key, + pasword varchar(30), + balance money, + userName varchar(50) +) +create table computer +( + id varchar(30) primary key, + onUse varchar(2) check(onUse=0 or onUse=1), + note ntext +) +create table record +( + id varchar(30) primary key, + cardId varchar(30) foreign key references card(id), + ComputerId varchar(30) foreign key references computer(id), + beginTime datetime, + endTime datetime, + fee money +) + --上网卡信息表 +insert into card(ID,pasword,balance,userName) +select '0023_ABC','555','98','张军'union +select '0025_bbd','abe','300','朱俊'union +select '0036_CCD','何柳','100','何柳'union +select '0045_YGR','0045_YGR','58','证验'union +select '0078_RJV','55885fg','600','校庆'union +select '0089_EDE','张','134','张峻' +--网吧机器说明表 +insert into computer(id,onUse,note) +select '02','0','25555' union +select '03','1','55555' union +select '04','0','66666' union +select '05','1','88888' union +select '06','0','688878' union +select 'B01','0','558558' +--上网记录表 +insert into record(ID,cardId,ComputerId,beginTime,endTime,fee) +select '23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20' union +select '34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23' union +select '45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6' union +select '47','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6' union +select '55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50' union +select '64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','3' union +select '65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23' union +select '98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23' + +select * from card +select * from computer +select * from record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select b.id 卡号,b.userName 用户名,c.id 机器编号, a.beginTime 开始时间,a.endTime 结束时间,a.fee 消费金额 from record a inner join card b on a.cardId=b.id +inner join computer c on c.id=a.ComputerId where b.userName='张军' order by a.fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select b.id 机器,count(*)上网次数,sum(c.fee)消费的总金额 from computer b left join record c on b.id=c.ComputerId group by b.id,c.fee order by c.fee desc +--3. 查询出所有已经使用过的上网卡的消费总金额 +select b.id, sum(b.fee)消费总金额 from card a inner join record b on a.id=b.cardId group by b.id +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id,userName from card where id in (select id from record group by id ) +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from card where userName = pasword +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 max(ComputerId) 机器号,count(*)次数 from record group by ComputerId order by count(*) desc +----7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select a.id 卡号,a.userName 用户名,b.ComputerId 机器号,b.fee 消费金额 from card a inner join record b on a.id=b.cardId where a.id like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select a.id 卡号,a.userName 用户名,b.ComputerId 机器号,b.beginTime 开始时间,b.endTime 结束时间 ,b.fee 消费金额 from card a inner join record b on a.id=b.cardId where b.beginTime =CONVERT(datetime,b.beginTime) group by b.beginTime,a.id,a.userName,b.ComputerId,b.beginTime,b.endTime,b.fee +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select a.id 卡号,a.userName 用户名,b.ComputerId 机器号,b.beginTime 开始时间,b.endTime 结束时间,b.fee 消费金额 from card a inner join record b on a.id=b.cardId where b.beginTime-b.endTime>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 a.id 卡号,a.userName 用户名,b.ComputerId 机器号,b.beginTime 开始时间,b.endTime 结束时间,b.fee 消费金额 from card a inner join record b on a.id=b.cardId order by b.fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1e5ce9e41b53d807fac8368e14b84851cbd73f03 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" @@ -0,0 +1,78 @@ +use master +go +create database py +go +use py +go +create table stuInfo +( +stuid int primary key identity(1,1), +stuname nvarchar(20) not null, +stuage int, +stusex int , +time datetime +) +create table courseInfo +( +courseid int primary key identity(1,1), +coursename nvarchar(20) not null, +coursemarkes int +) +create table scoreInfo +( +scoreid int primary key identity(1,1), +stuid int references stuInfo(stuid), +courseid int not null, +score int not null +) + +insert into stuInfo +select 'tom','19','1', NULL union +select 'jack','20','0', NULL union +select 'rose','21','1', NULL union +select 'lulu','19','1', NULL union +select 'lili','21','0', NULL union +select 'abc','20','1','2007-01-07 01:11:36.590' +INSERT INTO courseInfo +select 'javabase','4' union +select 'html','2' union +select 'javascript','2' union +select 'sqlbase','2' +insert into scoreInfo +select '1','1','80'union +select '1','2','85'union +select '1','4','50'union +select '2','1','75'union +select '2','3','45'union +select '2','4','75'union +select '3','1','45'union +select '4','1','95'union +select '4','2','75'union +select '4','3','90'union +select '4','4','45' +select * from stuInfo +select * from courseInfo +select * from scoreInfo +--1.查询出每个学生所选修的课程的数量 和 所选修的课程的考试的平均分 +select stuid,count(*)数量,avg(score) from scoreInfo group by stuid +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select c.courseid,count(*)数量,sum(s.score)总分 from courseInfo c inner join scoreInfo s on c.courseid=s.courseid group by c.courseid +--3.查询出性别一样并且年龄一样的学生的信息 +select a.* from stuInfo a, stuInfo b where a.stuage= b.stuage and a.stusex=b.stusex and a.stuid !=b.stuid +--4.查询出学分一样的课程信息 +select a.* from courseInfo a, courseInfo b where a.coursemarkes=b.coursemarkes and a.coursename!=b.coursename +union +select a.* from courseInfo a, courseInfo b where a.coursemarkes=b.coursemarkes and a.coursename!=b.coursename +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select b.stuid,a.stuname,b.courseid,b.score from stuInfo a inner join scoreInfo b on a.stuid=b.stuid +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select s.stuid,c.courseid,c.coursename,c.coursemarkes,s.score from courseInfo c inner join scoreInfo s on c.courseid=s.courseid +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuid,stuInfo.stuname from stuInfo left join scoreInfo on stuInfo.stuid= scoreInfo .stuid where score is null +select stuid,stuname from stuInfo where stuid not in (select stuid from scoreInfo group by stuid,courseid ) +--8.查询出是周六周天来报到的学生 +select stuname,time from stuInfo where time =CONVERT(datetime,time) group by time,stuname +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuname like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuid 学号 ,count(*) 数量 ,avg(score) 平均分 from scoreInfo group by stuid having count(*)>=2 and avg(score)>=70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..73624e2afa896f521130e2feaf7215c5de8fd35b --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery1.sql" @@ -0,0 +1,122 @@ +use master +go +create database BIAO +on +( +name='BIAO', +filename='C:\SQL\BIAO.mdf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +log on +( +name='BIAO_log', +filename='C:\SQL\BIAO_log.ldf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +go +use BIAO +go +create table StuInfo +( +StuID int primary key not null, +StuName varchar(10) not null, +StuAge int not null, +StuSex varchar(2) check(StuSex='男' or StuSex='女') not null, +Time datetime +) +go +use BIAO +go +create table CourseInfo +( +CourseID int primary key not null, +CourseName varchar(10) not null, +CourseMarks int +) +go +use BIAO +go +create table ScoreInfo +( +ScoreID int primary key not null, +StuID int references StuInfo(StuID), +CourseID int references CourseInfo(CourseID), +Score int +) +go + +insert into StuInfo (StuID,StuName,StuAge,StuSex) values(1,'Tom','19','男') +insert into StuInfo (StuID,StuName,StuAge,StuSex) values(2,'Jack','20','女') +insert into StuInfo (StuID,StuName,StuAge,StuSex) values(3,'Rose','21','男') +insert into StuInfo (StuID,StuName,StuAge,StuSex) values(4,'Lulu','19','男') +insert into StuInfo (StuID,StuName,StuAge,StuSex) values(5,'Lili','21','女') +insert into StuInfo (StuID,StuName,StuAge,StuSex,Time) values(6,'Abc','20','男','2007-01-07 01:11:36.590') +select * from StuInfo + +insert into CourseInfo (CourseID,CourseName,CourseMarks) values(1,'JavaBase','4') +insert into CourseInfo (CourseID,CourseName,CourseMarks) values(2,'HTML','2') +insert into CourseInfo (CourseID,CourseName,CourseMarks) values(3,'JavaScript','2') +insert into CourseInfo (CourseID,CourseName,CourseMarks) values(4,'SqlBase','2') +select * from CourseInfo + +insert into ScoreInfo (ScoreID,StuID,CourseID,Score) values(1,1,1,'80') +insert into ScoreInfo (ScoreID,StuID,CourseID,Score) values(2,1,2,'85') +insert into ScoreInfo (ScoreID,StuID,CourseID,Score) values(3,1,4,'50') +insert into ScoreInfo (ScoreID,StuID,CourseID,Score) values(4,2,1,'75') +insert into ScoreInfo (ScoreID,StuID,CourseID,Score) values(5,2,3,'45') +insert into ScoreInfo (ScoreID,StuID,CourseID,Score) values(6,2,4,'75') +insert into ScoreInfo (ScoreID,StuID,CourseID,Score) values(7,3,1,'45') +insert into ScoreInfo (ScoreID,StuID,CourseID,Score) values(8,4,1,'95') +insert into ScoreInfo (ScoreID,StuID,CourseID,Score) values(9,4,2,'75') +insert into ScoreInfo (ScoreID,StuID,CourseID,Score) values(10,4,3,'90') +insert into ScoreInfo (ScoreID,StuID,CourseID,Score) values(11,4,4,'45') +select * from ScoreInfo + + +--学生信息表(stuInfo) +select * from StuInfo +--课程信息表(courseInfo) +select * from CourseInfo +--分数信息表(scoreInfo) +select * from ScoreInfo +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select StuInfo.StuID 学生编号,StuInfo.StuName 姓名,count(CourseMarks) 课程数量,AVG(Score) 平均分 +from ScoreInfo,StuInfo,CourseInfo +where ScoreInfo.StuId=StuInfo.StuId and ScoreInfo.CourseId=CourseInfo.CourseId +group by StuInfo.StuID,StuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select CourseInfo.CourseID 课程编号,CourseInfo.CourseName 课程名称,count(ScoreInfo.StuID) 学生个数,SUM(Score) 总分 +from ScoreInfo,StuInfo,CourseInfo +where ScoreInfo.StuId=StuInfo.StuId and ScoreInfo.CourseId=CourseInfo.CourseId +group by CourseInfo.CourseID,CourseName + +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo as A,stuInfo as B +where B.StuAge=A.StuAge and B.StuSex=A.StuSex and A.StuID!=B.StuID + +--4.查询出学分一样的课程信息 +select distinct A. * from courseInfo A,courseInfo B where A.CourseMarks=B.CourseMarks AND A.courseID!=B.courseID + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select StuInfo.StuID 学号,StuName 姓名,CourseInfo.CourseID 课程编号,Score 课程分数 +from ScoreInfo inner join CourseInfo on CourseInfo.CourseID=ScoreInfo.CourseID +inner join StuInfo on StuInfo.StuID=ScoreInfo.StuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select StuInfo.StuID 学号,CourseInfo.CourseID 课程编号,CourseName 课程名称,CourseMarks 课程学分,Score 分数 from ScoreInfo +inner join CourseInfo on CourseInfo.CourseID=ScoreInfo.CourseID +inner join StuInfo on StuInfo.StuID=ScoreInfo.StuID + +--7.查询出没有参加考试的学生的学号和姓名 +select StuInfo.StuID 学号,StuName 姓名 from StuInfo +left join ScoreInfo on StuInfo.StuID=ScoreInfo.StuID where Score is null + +--8.查询出姓名中有字母a的学生的信息 +select * from StuInfo where StuName like 'a%' + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e828608278ed648c072f9dca3031c66d80ed4b1b --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery2.sql" @@ -0,0 +1,115 @@ +use master +go +create database TBL +on +( +name='TBL', +filename='C:\SQL\TBL.mdf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +log on +( +name='TBL_log', +filename='C:\SQL\TBL_log.ldf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +go +use TBL +go +create table CardInfo +( +ID varchar(50) primary key not null, --卡号 +PassWord varchar(30) not null, --密码 +Balance int not null, --卡上余额 +UserName varchar(10) not null --该上网卡所属的用户名 +) +go +use TBL +go +create table ComPuter +( +ID varchar(10) primary key not null, --机器编号 +OnUse varchar(2) check(OnUse='是' or OnUse='否') not null, --机器是否使用 +Note text --该机器说明 +) +go +use TBL +go +create table Record +( +ID int primary key not null, --记录编号 +CardID varchar(50) references CardInfo(ID), --本次上网的卡号 +ComPuterID varchar(10) references ComPuter(ID), --本次上网记录所使用的机器号 +BeginTime datetime, --本次上网记录的开始时间 +EndTime datetime, --本次上网记录的结束时间 +Fee int --本次上网的费用 +) + +insert into CardInfo(ID,PassWord,Balance,UserName) values('0023_ABC','555','98','张军') +insert into CardInfo(ID,PassWord,Balance,UserName) values('0025_bbd','abc','300','朱俊') +insert into CardInfo(ID,PassWord,Balance,UserName) values('0036_CCD','何柳','100','何柳') +insert into CardInfo(ID,PassWord,Balance,UserName) values('0045_YGR','0045_YGR','58','证验') +insert into CardInfo(ID,PassWord,Balance,UserName) values('0078_RJV','55885fg','600','校庆') +insert into CardInfo(ID,PassWord,Balance,UserName) values('0089_EDE','zhang','134','张峻') +select * from CardInfo + +insert into ComPuter(ID,OnUse,Note) values('02','否','25555') +insert into ComPuter(ID,OnUse,Note) values('03','是','55555') +insert into ComPuter(ID,OnUse,Note) values('04','否','66666') +insert into ComPuter(ID,OnUse,Note) values('05','是','88888') +insert into ComPuter(ID,OnUse,Note) values('06','否','688878') +insert into ComPuter(ID,OnUse,Note) values('B01','否','558558') +select * from ComPuter + +insert into Record(ID,CardID,ComPuterID,BeginTime,EndTime,Fee) values(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20') +insert into Record(ID,CardID,ComPuterID,BeginTime,EndTime,Fee) values(34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23') +insert into Record(ID,CardID,ComPuterID,BeginTime,EndTime,Fee) values(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50') +insert into Record(ID,CardID,ComPuterID,BeginTime,EndTime,Fee) values(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6') +insert into Record(ID,CardID,ComPuterID,BeginTime,EndTime,Fee) values(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50') +insert into Record(ID,CardID,ComPuterID,BeginTime,EndTime,Fee) values(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6') +insert into Record(ID,CardID,ComPuterID,BeginTime,EndTime,Fee) values(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50') +insert into Record(ID,CardID,ComPuterID,BeginTime,EndTime,Fee) values(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30') +insert into Record(ID,CardID,ComPuterID,BeginTime,EndTime,Fee) values(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23') +insert into Record(ID,CardID,ComPuterID,BeginTime,EndTime,Fee) values(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') + + +--请完成以下题目: +--上网卡信息表 +select * from CardInfo +--网吧机器说明表 +select * from ComPuter +--上网记录表 +select * from Record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +--2. 查询出每台机器上的上网次数和消费的总金额 +select count(ComPuterID)上网次数,sum(fee)消费总金额 from Record group by ComPuterID + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select C.ID 卡号,SUM(R.Fee) 消费总金额 +from CardInfo C inner join Record R on C.ID=R.CardID +group by C.ID + +--4. 查询出从未消费过的上网卡的卡号和用户名 +--5. 将密码与用户名一样的上网卡信息查询出来 +select distinct A.* from CardInfo A , CardInfo B +where A.PassWord=B.UserName + +--6. 查询出使用次数最多的机器号和使用次数 +select * from Record +select top 1 ComPuterID 机器号 ,count(ComPuterID)使用次数 +from Record group by ComPuterID order by count(ComPuterID) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select C.ID 卡号,UserName 用户名, ComPuterID 机器号,Fee 消费金额 from CardInfo C +inner join Record R on C.ID=R.CardID where C.ID like '_____ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select CardID 卡号,UserName 用户名,ComPuterID 机器号,BeginTime 开始时间,EndTime 结束时间, Fee 消费金额 +from Record inner join CardInfo on CardInfo.ID=Record.CardID + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\347\273\203\344\271\2401.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\347\273\203\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e5612332bac5bfa440fa8e45f5bdf08750a62c78 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\347\273\203\344\271\2401.sql" @@ -0,0 +1,104 @@ +drop database Stuinfo +create database Stuinfo + +on +( + name='Stuinfo', + filename='F:\Stuinfo.mdf' +) +log on +( + name='Stuinfo_log', + filename='F:\Stuinfo.ldf_log' +) +use Stuinfo +create table stuInfo +( + stuID int primary key , + stuName varchar(20), + stuAge int , + stuSex nvarchar(1), + stime datetime +) + +create table courseInfo +( + courseID int primary key , + courseName varchar(25), + courseMarks varchar(5) +) +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int foreign key references stuInfo(stuID) , + courseID int foreign key references courseInfo(courseID), + score int +) +--学生信息表 +insert into stuInfo(stuID,stuName,stuAge,stuSex) +select 1,'Tom',19,1 union +select 2,'Jack',20,0union +select 3,'Rose',21,1union +select 4,'Lulu',19,1 union +select 5,'Lili',21,0union +select 6,'abc',20,1 + +update stuInfo set stime='2007-01-07 01:11:36.590'where stuName='abc' +--课程表 +insert into courseInfo(courseID,courseName,courseMarks) +select 1,'JavaBase',4 union +select 2,'HTML',2 union +select 3,'JavaScript',2 union +select 4,'SqlBase',2 +-- 成绩表 +insert into scoreInfo(stuID,courseID,score) +select 1,1,80 union +select 1,2,85 union +select 1,4,50 union +select 2,1,75 union +select 2,3,45 union +select 2,4,75 union +select 3,1,45 union +select 4,1,95 union +select 4,2,75 union +select 4,3,90 union +select 4,4,45 + +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +select * from stuInfo --学生信息表 +select * from courseInfo --课程表 +select * from scoreInfo -- 成绩表 +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuID,count(courseID)课程的数量,avg(score)平均分 from scoreInfo group by stuID +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseID 课程, count(courseID)选修个数, sum(score)总分 from scoreInfo group by courseID + +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A, stuInfo B +where A.stuSex=B.stuSex AND A.stuAge=B.stuAge AND A.stuID!=B.stuID +--4.查询出学分一样的课程信息 +select distinct A.* from courseInfo A,courseInfo B +where A.courseMarks=B.courseMarks AND A.courseID!=B.courseID +--5.查询出参加了 考试的学生的学号,姓名,课程号和分数 +select scoreInfo.stuID 学号,stuName 姓名,courseID 课程号,score from scoreInfo +left join stuInfo on scoreInfo.stuID=stuInfo.stuID group by scoreInfo.stuID,stuName,courseID,score +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select scoreInfo.stuID 学号, scoreInfo.courseID 课程号,courseInfo.courseName 课程名,courseMarks 学分,scoreInfo.score 分数 from scoreInfo +left join courseInfo on scoreInfo.courseID=courseInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select Stuinfo.stuID 学号,stuInfo.stuName 姓名 from stuInfo +left join scoreInfo on stuInfo.stuID=scoreInfo.stuID +where score is null +--8.查询出是周六周天来报到的学生 + --超纲了!!!!!!!!!!!!!!!! +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出 选修了2门课程 以上的 并且 考试平均分 在70以上 的学生的 学号 和 考试平均分 以及 选修课程的数量 +select stuID,count(courseID)课程的数量,avg(score)平均分 from scoreInfo +group by stuID +having avg(score)>=70 and count(courseID)>=2 + +select * from courseInfo --课程表 +select * from scoreInfo -- 成绩表 +select * from stuInfo --学生信息表 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\347\273\203\344\271\2402.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\347\273\203\344\271\2402.sql" new file mode 100644 index 0000000000000000000000000000000000000000..069c26bd4682779b24cc607a56035ec3f6164c6a --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\347\273\203\344\271\2402.sql" @@ -0,0 +1,109 @@ +create database wb +on +( + name='wb', + filename='F:\wb.mdf' +) +log on +( + name='wb_log', + filename='F:\wb_log.ldf' +) +use wb +go + +create table card +( + id varchar(30) primary key, + pasword varchar(30), + balance money, + userName varchar(50) +) +create table computer +( + id varchar(30) primary key, + onUse varchar(2) check(onUse=0 or onUse=1), + note ntext +) +create table record +( + id varchar(30) primary key, + cardId varchar(30) foreign key references card(id), + ComputerId varchar(30) foreign key references computer(id), + beginTime datetime, + endTime datetime, + fee money +) + --上网卡信息表 +insert into card(ID,pasword,balance,userName) +select '0023_ABC','555','98','张军'union +select '0025_bbd','abe','300','朱俊'union +select '0036_CCD','何柳','100','何柳'union +select '0045_YGR','0045_YGR','58','证验'union +select '0078_RJV','55885fg','600','校庆'union +select '0089_EDE','张','134','张峻' +--网吧机器说明表 +insert into computer(id,onUse,note) +select '02','0','25555' union +select '03','1','55555' union +select '04','0','66666' union +select '05','1','88888' union +select '06','0','688878' union +select 'B01','0','558558' +--上网记录表 +insert into record(ID,cardId,ComputerId,beginTime,endTime,fee) +select '23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20' union +select '34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23' union +select '45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6' union +select '47','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6' union +select '55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50' union +select '64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','3' union +select '65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23' union +select '98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23' + +select * from card --上网卡信息表 +select * from computer --网吧机器说明表 +select * from record --上网记录表 +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号, +--用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select card.id 卡号, card.userName 用户名 ,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from card inner join record on card.id=record.cardId where userName='张军' order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId 机器,count(cardId)上网次数,sum(fee)总金额 from record group by cardId,ComputerId +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId,sum(record.fee) 总金额 from record group by cardId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select card.id,userName from card +left join record on card.id=record.cardId +where fee is null group by userName,card.id +--5. 将密码与用户名一样的上网卡信息查询出来 +select A.* from card A,card B where A.pasword=B.userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号,count(ComputerId)使用次数 from record +group by ComputerId order By count(ComputerId) desc + +select * from card --上网卡信息表 +select * from record --上网记录表 +select * from computer --网吧机器说明表 + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器号, fee 消费金额 +from record inner join card on record.cardId=card.id where record.cardId like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号, +--用户名,机器号,开始时间、结束时间和消费金额 +--超纲 +--9. 查询成一次上网时间超过12小时的的上网记录, +--要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +--超纲 +--10. 查询除消费金额排列前三名(最高)的上网记录, +--要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 cardId 卡号,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from record order by fee desc + + +select * from card --上网卡信息表 +select * from computer --网吧机器说明表 +select * from record --上网记录表 +select cardId from record group by cardId--上网记录表 + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery3.29.1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery3.29.1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9fdab4cd11e191b90c409d10b34bbbcab981d4a9 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery3.29.1.sql" @@ -0,0 +1,117 @@ +create database System +on +( + fileName='D:\System.mdf', + name='System', + size=5MB, + Maxsize=5MB, + filegrowth=5MB +) +log on +( + fileName='D:\System_log.ldf', + name='System_log', + size=5MB, + Maxsize=5MB, + filegrowth=5MB +) +go +use System + +go + +create table tbl_card +( + id nvarchar(10) primary key, + passWord nvarchar(15), + balance money , + userName nvarchar(10) +) + +create table tbl_computer +( + id nvarchar(10) primary key, + onUse int check(onUse in (1,0)), + note text +) + +create table tbl_record +( + id nvarchar(10) primary key, + cardId nvarchar(10) foreign key references tbl_card(id), + ComputerId nvarchar(10) foreign key references tbl_computer(id), + beginTime datetime, + endTime datetime, + fee money +) + + +insert into tbl_card values +('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'验证'), +('0078_RJV','55885tg',600,'校庆'), +('0089_EDE','zhang',134,'张峻') + + + +insert into tbl_computer values +('02',0,'25555'), +('03',1,'55555'), +('04',0,'66666'), +('05',1,'88888'), +('06',0,'688878'), +('B01',0,'558558') + +insert into tbl_record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), +(34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), +(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6'), +(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6'), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','300'), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') + + +--tbl_record,tbl_computer ,tbl_card +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select C.id 显示卡号,userName 用户名,P.id 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record R +inner join tbl_card C on C.id=cardId +inner join tbl_computer P on P.id=R.ComputerId where userName='张军' order by fee DESC +--2. 查询出每台机器上的上网次数和消费的总金额 +select C.id 机器,count(ComputerId) 上网次数,sum(fee) 消费的总金额 from tbl_record R +inner join tbl_computer C on C.id=R.ComputerId group by C.id + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId 上网卡,sum(fee) 消费总金额 from tbl_record group by cardId + + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id,userName from tbl_card where id not in(select distinct cardId from tbl_record) + +select * from tbl_card +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card where passWord=userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 C.id,count(ComputerId) from tbl_record R inner join tbl_computer C on C.id=ComputerId group by C.id order by count(ComputerId) DESC +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select userName,P.id,fee from tbl_record R inner join tbl_computer P on ComputerId=P.id inner join tbl_card C on C.id=cardId where cardId like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId,userName,P.id,beginTime,endTime,fee from tbl_record inner join tbl_computer P on ComputerId=P.id inner join tbl_card C on C.id=cardId where datepart(weekday,beginTime)=1 or datepart(weekday,beginTime)=7 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId,userName,P.id,beginTime,endTime,fee from tbl_record inner join tbl_computer P on ComputerId=P.id inner join tbl_card C on C.id=cardId where datediff(hour,beginTime,endTime)>12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId,userName,P.id,beginTime,endTime,fee from tbl_record inner join tbl_computer P on ComputerId=P.id inner join tbl_card C on C.id=cardId where fee not in(select top 3 fee from tbl_record order by fee DESC) + + + + + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery3.29.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery3.29.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1c6f6a12902f878353b59c52dbc1c708722986fa --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery3.29.sql" @@ -0,0 +1,130 @@ +create database School +on +( + fileName='F:\homework\School.mdf', + Name='School', + size=5MB, + MAXsize=5MB, + filegrowth=5MB +) +log on +( + fileName='F:\homework\School_log.ldf', + Name='School_log', + size=5MB, + MAXsize=5MB, + filegrowth=5MB +) + +go + +use School + +go + + +create table stuInfo +( + StuID int primary key identity, + StuName nvarchar(10) not null, + StuAge int not null, + StuSex int , + time date +) + +create table courseInfo +( + courseID int identity, + courseName nvarchar(10), + courseMarks int +) + +create table scoreInfo +( + scoreID int identity, + StuID int , + courseID int , + score int +) + +insert into stuInfo(StuName,StuAge,StuSex,time) values +('Tom',19,1,null), +('Jack',20,0,null), +('Rose',21,1,null), +('Lulu',19,1,null), +('Lili',21,0,null), +('abc',20,1,'2007-01-07 01:11:36.590') + +insert into courseInfo values +('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) + + +insert into scoreInfo values +(1,1,80), +(1,2,85), +(1,4,50), +(2,1,75), +(2,3,45), +(2,4,75), +(3,1,45), +(4,1,95), +(4,2,75), +(4,3,90), +(4,4,45) + + +--stuInfo courseInfo scoreInfo + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select StuName 学生,count(S.StuID) 课程的数量,AVG(score) 考试的平均分 from scoreInfo S inner join stuInfo SI on S.StuID=SI.StuID +inner join courseInfo C on S.courseID=C.courseID group by StuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName,count(StuName) 学生的个数,sum(score) 考试的总分 from scoreInfo S inner join stuInfo SI on S.StuID=SI.StuID +inner join courseInfo C on S.courseID=C.courseID group by courseName + +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo where StuAge + in (select StuAge 年龄 from stuInfo group by StuSex,StuAge having count(StuAge)>1) + +--4.查询出学分一样的课程信息 +select * from courseInfo where courseMarks in (select courseMarks from courseInfo group by courseMarks having count(courseMarks)>1) + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select StuName 姓名,SI.StuID 学号,courseID 课程号,score 分数 from scoreInfo S inner join stuInfo SI on S.StuID=SI.StuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select StuName 姓名,SI.StuID 学号,C.courseID 课程号,courseName 课程名,courseMarks 课程学分,score 分数 from scoreInfo S inner join stuInfo SI on S.StuID=SI.StuID inner join courseInfo C on C.courseID= S.courseID + + +--7.查询出没有参加考试的学生的学号和姓名 +select S.StuID,StuName from stuInfo S left join scoreInfo SI on S.StuID=SI.StuID where score is null + +--8.查询出是周六周天来报到的学生 +select * from stuInfo where datepart(weekday,time)=1 or datepart(weekday,time)=7 + + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where StuName like '%a%' + + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select StuID 学生的学号,AVG(score) 考试平均分,count(courseID) 选修课程的数量 from scoreInfo group by StuID having count(courseID)>2 and AVG(score)>70 + + + + + + + + + + + + + + + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\344\277\241\346\201\257\350\241\250.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\344\277\241\346\201\257\350\241\250.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2b4322ddff4e4e20929d0cd2f9526622206f1a44 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\344\277\241\346\201\257\350\241\250.sql" @@ -0,0 +1,76 @@ +use master +go + +create database TEXT + +go + +use TEXT +go +create table stuInfo +( + stuID int primary key identity(1,1), + stuName nvarchar(10), + stuAge int, + stuSex varchar(2) check(stuSex = '1' or stuSex = '0') default('1'), + time datetime, +) +go + +use TEXT +go +create table courseInfo +( + courseID int primary key identity(1,1), + courseName nvarchar(10), + courseMarks varchar(2) +) +go + +use TEXT +go +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) + +insert into stuInfo values('Tom','19','1',''),('Jack','20','0',''),('Rose','21','1',''),('Lulu','19','1',''),('Lili','21','0',''),('abc','20','1','2007-01-07 01:11:36.590') +insert into courseInfo values('JavaBase','4'),('HTML','2'),('JavaScript','2'),('SqlBase','2') +insert into scoreInfo values(1,1,'80'),(1,2,'85'),(1,4,'50'),(2,1,'75'),(2,3,'45'),(2,4,'75'),(3,1,'45'),(4,1,'95'),(4,2,'75'),(4,3,'90'),(4,4,'45') + + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select courseID 课程,COUNT(*) 数量,AVG(score)平均分 from stuInfo inner join scoreInfo on stuInfo.stuID = scoreInfo.stuID group by courseID + +--2.查询出 每门课程 的 选修的学生 的个数 和 学生考试的总分 +select courseID 课程,count(SC.stuID)选修学生的个数,SUM(score) 总分 from stuInfo SI inner join scoreInfo SC on SI.stuID = SC.stuID group by courseID + +--3.查询出 性别一样 并且 年龄一样 的学生的信息 +select A.* from stuInfo A ,stuInfo B +where A.stuSex=B.stuSex AND A.stuAge=B.stuAge AND A.stuID!=B.stuID + +--4.查询出学分一样的课程信息 +select DISTINCT A.* from courseInfo A inner join courseInfo B on A.courseMarks = B.courseMarks AND a.courseID != B.courseID + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select SC.stuID 学号,stuName 姓名,courseID 课程号,score 分数 from stuInfo ST inner join scoreInfo SC on ST.stuID = SC.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select C.courseID 课程号,stuID 学号,courseName 课程名,courseMarks 课程学分,score 分数 +from courseInfo C inner join scoreInfo S on C.courseID = S.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select ST.stuID 学号,stuName 姓名 from stuInfo ST inner join scoreInfo SC on ST.stuID != SC.stuID +select * from scoreInfo +select * from stuInfo +select * from courseInfo +--8.查询出是周六周天来报到的学生 +select * from stuInfo where DATEPART(dw,[time]) in(0,1) +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName LIKE '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的s 学号 和 考试平均分 以及选修 课程的数量 +select AVG(score) 考试平均分,courseID,COUNT(*) 课程的数量 +from stuInfo ST inner join scoreInfo SC on ST.stuID = SC.stuID group by courseID having AVG(score) > 70 and COUNT(*) > 2 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\347\275\221\345\220\247\350\256\241\350\264\271.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\347\275\221\345\220\247\350\256\241\350\264\271.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f659b224c671ad7c6df1a7e3a2f596f4d5ee74d3 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\347\275\221\345\220\247\350\256\241\350\264\271.sql" @@ -0,0 +1,80 @@ + +use TEXT +go + +create table tbl_card +( + ID varchar(20) primary key, + passWord nvarchar(15), + Balance varchar(50), + UseName nvarchar(5), +) + + +go +use TEXT +go +create table tbl_computer +( + ID varchar(10) primary key, + onUse int check(onUse = 0 or onUse = 1) default(1), + note varchar(10) +) + +go +use TEXT +go +create table tbl_record +( + ID nvarchar(10) primary key, + cardId varchar(20) references tbl_card(ID), + ComputerId varchar(10) references tbl_computer(ID), + beginTime datetime, + endTime datetime, + fee int +) +insert into tbl_card values('0023_ABC','555',98,'张军'),('0025_bbd','abc',300,'朱俊'),('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'),('0078_RJV','55885lg',600,'校庆'),('0089_EDE','zhang',134,'张峻') + +insert into tbl_computer values('02',0,'25555'),('03',1,'55555'),('04',0,'66666'),('05',1,'88888'),('06',0,'688878'),('B01',0,'558558') +insert into tbl_record values(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20), +(34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23),(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00',6),(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6),(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',30),(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) +select * from tbl_card +select * from tbl_record +select * from tbl_computer +--1. 查询出用户名为'张军'的上网卡的上网记录,要求 显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select TC.ID 卡号,UseName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_card TC inner join tbl_record TE on TC.ID = TE.cardId where UseName = '张军' order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select SUM(fee)消费的总金额,COUNT(onUse)上网次数 from tbl_computer TP inner join tbl_record TR on TP.ID = TR.ComputerId where onUse = '1' + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId,SUM(fee)消费总金额 from tbl_record TR inner join tbl_card TC on TR.cardId = TC.ID group by cardId +--4. 查询出从未消费过的上网卡的 卡号 和 用户名 +select TC.ID 卡号,UseName 用户名 from tbl_card TC inner join tbl_record TR on TC.ID = TR.cardId +inner join tbl_computer TP on TP.ID = TR.ComputerId where onUse = 0 +--5. 将密码与用户名一样的上网卡信息查询出来 +select A.* from tbl_card A inner join tbl_card B on A.passWord = B.UseName + +--6. 查询出使用次数最多的机器号和使用次数 +select TC.ID,COUNT(onUse)使用次数 +from tbl_computer TC inner join tbl_record TR on TC.ID = TR.ComputerId GROUP BY TC.ID +--7. 查询出卡号是以'ABC'结尾的 卡号,用户名 ,上网的 机器号 和 消费金额 +select TP.ID 机器号,UseName 用户名,fee 消费金额 from tbl_computer TP inner join tbl_record TR on TP.ID = TR.ComputerId +inner join tbl_card TC on TC.ID = TR.cardId where cardId like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select TC.ID 卡号,UseName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_card TC inner join tbl_record TE on TC.ID = TE.cardId where DATEPART(dw,[beginTime]) in(0,1) +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select TC.ID 卡号,UseName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_card TC inner join tbl_record TE on TC.ID = TE.cardId where DATEDIFF(hh,beginTime,endTime) > 12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select TC.ID 卡号,UseName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_card TC inner join tbl_record TE on TC.ID = TE.cardId where fee < (select max(fee) from tbl_record) order by fee desc + + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..44b8c4f773c93999b3f6f33a79bd4475e741ce62 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" @@ -0,0 +1,97 @@ +use wb +go +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +create table stuInfo +( + stuID int primary key identity, + stuName char(10), + stuAge int, + stuSex int, + time datetime +) +create table courseInfo +( + courseID int primary key identity, + courseName char(20), + courseMarks int +) +create table scoreInfo +( + scoreID int primary key identity, + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) +insert into stuInfo values('Tom',19,1,''),('Jack',20,0,''),('Rose',21,1,''), + ('Lulu',19,1,''),('Lili',21,0,''),('abc',20,1,'2007-01-07 01:11:36.590') + +insert into courseInfo values('JavaBase',4),('HTML',2), + ('JavaScript',2),('SqlBase',2) + +insert into scoreInfo values(1,1,80),(1,2,85),(1,4,50), + (2,1,75),(2,3,45),(2,4,75), + (3,1,45),(4,1,95),(4,2,75), + (4,3,90),(4,4,45) +--题目: +select*from stuInfo +select*from courseInfo +select*from scoreInfo + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName 学生姓名,count(courseID)选课数量, avg(score)所选课考试的平均分 + from scoreInfo + join stuInfo + on scoreInfo.stuID = stuInfo.stuID + group by stuInfo.stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 课程名称, count(stuID)学生个数 ,sum(score)考试总分 + from scoreInfo + join courseInfo + on courseInfo.courseID=scoreInfo.courseID + group by courseName + +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A ,stuInfo B + where A.stuSex=B.stuSex + and A.stuAge=B.stuAge + and A.stuID != B.stuID + +--4.查询出学分一样的课程信息 + +select*from courseInfo + where courseMarks in ( select courseMarks from courseInfo + group by courseMarks + having count(courseMarks)>=2 + ) + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID 学号,stuName 姓名,courseID 课程号,score 分数 + from stuInfo + join scoreInfo + on stuInfo.stuID=scoreInfo.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID 学号,stuName 姓名, scoreInfo .courseID 课程号,courseName 课程名,courseMarks 课程学分,score 分数 + from stuInfo + join scoreInfo + on stuInfo.stuID=scoreInfo.stuID + join courseInfo + on courseInfo.courseID=scoreInfo.courseID + +--7.查询出没有参加考试的学生的学号和姓名 + +select stuID 学号,stuName 姓名 from stuInfo where stuID in +( + select stuID from stuInfo + except + select stuID from scoreInfo +) + +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select*from stuInfo where stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuID 学号,count(courseID)课程数, avg(score)考试平均分 from scoreInfo group by stuID having count(courseID)>=2 and avg(score)>=70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d48b68dc26788ba7b1c94d894bdf512562dd01ce --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery2.sql" @@ -0,0 +1,131 @@ +use master +go +create database wb +on +( name='wb', + filename='D:\wb-mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='wb_log', + filename='D:\wb_log-ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use wb +go +create table tbl_card +( + id varchar(8) primary key, + passWord varchar(15) not null, + balance money not null, + userName nchar(5) +) +create table tbl_computer +( + id char(5) primary key, + onUse int not null, + note text not null +) +create table tbl_record +( + id int primary key, + cardld varchar(8) references tbl_card(id), + ComputerId char(5) references tbl_computer(id), + beginTime datetime , + endTime datetime, + fee money not null +) +insert into tbl_card values('0023_ABC','555',99,'张军'),('0025_bbd','abc',300,'朱俊'),('0036_CCD','何柳',100,'何柳'),('0045_YGR','0045_YGR',58,'验证'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张俊') + +insert into tbl_computer values('02',0,'25555'),('03',1,'55555'),('04',0,'66666'),('05',1,'888888'),('06',0,'688878'),('B01',0,'558558') + + +insert into tbl_record values('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), + ('34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), + ('45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), + ('46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6'), + ('47','0023_ABC','03','2006-12-23 15:26:00','2007-12-23 22:55:00','50'), + ('48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6'), + ('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), + ('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30'), + ('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), + ('98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') + +select*from tbl_card +select*from tbl_computer +select*from tbl_record +--请完成以下题目: +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select cardld 卡号, userName 用户名, beginTime 开始时间, endTime 结束时间, fee 消费金额 + from tbl_record + join tbl_computer + on tbl_computer.id=tbl_record.ComputerId + join tbl_card + on cardld=tbl_card.id + where userName='张军' + order by fee + + +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId, count(ComputerId) 上网次数,sum(fee) 消费总金额 + from tbl_record + group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardld 网卡, sum(fee) 消费总金额 from tbl_record group by cardld + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id 卡号 , userName 用户名 + from tbl_card + where id not in (select cardld from tbl_record ) + +--5. 将密码与用户名一样的上网卡信息查询出来 +select T.id 卡号,T.passWord 密码,T.balance 卡上的余额,T.userName 用户名 + from tbl_card,tbl_card T + where tbl_card.passWord=T.userName + +select T.* + from tbl_card,tbl_card T + where tbl_card.passWord=T.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号, count(ComputerId) 使用次数 + from tbl_record + group by ComputerId + order by count(ComputerId) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select cardld 卡号, userName 用户名, ComputerId 机器号,fee 消费金额 + from tbl_record + join tbl_card + on tbl_card.id=cardld + where cardld like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardld 卡号, userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 + from tbl_record + join tbl_card + on tbl_card.id=cardld + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardld 卡号, userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 + from tbl_record + join tbl_card + on tbl_card.id=cardld + where datediff(HOUR,beginTime,endTime)>=12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 cardld 卡号, userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 + from tbl_record + join tbl_card + on tbl_card.id=cardld + order by fee desc + + + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4589fb299441aaebb0051ea2e9ffa467cd71e9fc --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" @@ -0,0 +1,90 @@ +create database Students +on +( +name='Students_data', +filename='C:\app\Students_data.mdf', +size=1mb, +Maxsize=50mb, +filegrowth=10% +) +log on +( +name='Students_log', +filename='C:\app\Students_log.ldf', +size=1mb, +filegoeth=1mb +) +USE Students +go +create table StuInfo +( +StuID int primary key , +Stuname varchar(10), +StuAge int, +StuSex nvarchar(1), +time datetime +) + +INSERT into StuInfo(StuID,Stuname,StuAge,StuSex,time) +SELECT 1,'Tom',19,1,'' UNION +SELECT 1,'Jack',20,0,'' UNION +SELECT 1,'Rose',21,1,'' UNION +SELECT 1,'Lulu',19,1,'' UNION +SELECT 1,'Lili',21,0,'' UNION +SELECT 1,'abc',20,1,'2007-01-0701:11:36.590' + + +create table courseInfo +( +courseID int primary Key, +courseName varchar(25), +courseMarks varchar(5) +) +INSERT into courseInfo(courseID,courseName,courseMarks) +SELECT 1,'JavaBase',4 UNION +SELECT 2,'HTML',2 UNION +SELECT 3,'JavaScript',2 UNION +SELECT 4,'SplBase',2 + +create table scoreInfo +( +scoreID int primary key identity(1,1), +stuID int, +courseID int foreign key references courseInfo(courseID), +score int +) +INSERT into scoreInfo(scoreID,stuID,courseID,score) +SELECT 1,1,1,80 UNION +SELECT 2,1,2,85 UNION +SELECT 3,1,4,50 UNION +SELECT 4,2,1,75 UNION +SELECT 5,2,3,45 UNION +SELECT 6,2,4,75 UNION +SELECT 7,3,1,45 UNION +SELECT 8,4,1,95 UNION +SELECT 9,4,2,75 UNION +SELECT 10,4,3,90 UNION +SELECT 11,4,4,45 + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +SELECT StuID,count(courseID),Avg(score) FROM scoreInfo group by stuID +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseID,COUNT(courseID),SUM(score) from scoreInfo group by courseID +--3.查询出性别一样并且年龄一样的学生的信息 +select A.StuAge,B.StuSex from StuInfo A inner join StuInfo B on A.StuAge=B.StuSex +--4.查询出学分一样的课程信息 +select A.courseMarks,B.courseMarks from courseInfo A inner join courseInfo B on A.courseMarks=B.courseMarks +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select scoreInfo.stuID 学号,stuName 姓名,courseID 课程号,score from scoreInfo +left join stuInfo on scoreInfo.stuID=stuInfo.stuID group by scoreInfo.stuID,stuName,courseID,score +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select scoreInfo.stuID 学号, scoreInfo.courseID 课程号,courseInfo.courseName 课程名,courseMarks 学分,scoreInfo.score 分数 from scoreInfo +left join courseInfo on scoreInfo.courseID=courseInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select Stuinfo.stuID 学号,stuInfo.stuName 姓名 from stuInfo left join scoreInfo on stuInfo.stuID=scoreInfo.stuID where score is null +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuID,count(courseID)课程的数量,avg(score)平均分 from scoreInfo group by stuID having avg(score)>=70 and count(courseID)>=2 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..603e9290938ff5cc4cf842241702d27b0cf884c9 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" @@ -0,0 +1,91 @@ +create database Student +on +( +name='Student_data', +filename='C:\app\Student_data.mdf', +size=1mb, +Maxsize=50mb, +filegrowth=10% +) +log on +( +name='Student_log', +filename='C:\app\Student_log.ldf', +size=1mb, +filegoeth=1mb +) +USE Student +go +create table tbl_card +( +id int NOT null, +passWord varchar(10) not null, +balance int NOT null, +userName int NOT null +) +INSERT into tbl_card(id,passWord,balance,userName) +SELECT 0023_abc,'555',98,'张军' UNION +SELECT 0025_bbd,'abe',300,'朱俊' UNION +SELECT 0036_CDD,'何柳',100,'何柳' UNION +SELECT 0045_YGR,'0045_YGR',58,'证验' UNION +SELECT 0078_RJV,'55885fg',600,'校庆' UNION +SELECT 0089_EDE,'zhang',134,'张峻' +create table tbl_computer +( +id varchar(30) primary key, +onUse bit NOT null DEFAULT(0), +note int +) +INSERT into tbl_computer(id,onUse,note) +SELECT '02',0,25555 UNION +SELECT '03',1,55555 UNION +SELECT '04',0,66666 UNION +SELECT '05',1,88888 UNION +SELECT '06',0,688878 UNION +SELECT 'B01',0,558558 +create table tbl_record +( +id int NOT null, +cardld char(10) NOT null, +Computerld int, +beginTime datetime, +endTime datetime, +fee int not null +) +INSERT into tbl_record(id,cardld,Computerld,beginTime,endTime,fee) +SELECT '23','0078_RJV','B01,2007-07-15 19:00:00','2007-07-15 21:00:00','20' UNION +SELECT '34','0025_bbd','02,2006-12-25 18:00:00','2006-12-25 21:00:00','23'UNION +SELECT '45','0023_ABC','03,2006-12-23 15:26:00','2006-12-23 22:55:00','50' UNION +SELECT '46','0023_ABC','03,2006-12-22 15:26:00','2006-12-22 22:55:00','6' UNION +SELECT '47','0023_ABC','03,2006-12-23 15:26:00','2006-12-23 22:55:00','50' UNION +SELECT '48','0023_ABC','03,2007-01-06 15:26:00','2007-01-06 22:55:00','6' UNION +SELECT '55','0023_ABC','03,2006-07-21 15:26:00','2006-07-21 22:55:00','50' UNION +SELECT '64','0045_Y...','04,2006-12-24 18:00:00','2006-12-24 22:00:00','3...'UNION +SELECT '65','0025_bbd','02,2006-12-28 18:00:00','2006-12-28 22:00:00','23' UNION +SELECT '98','0025_bbd','02,2006-12-26 18:00:00','2006-12-26 22:00:00','23' +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select card.id , card.userName ,beginTime ,endTime,fee +from card inner join record on card.id=record.cardId where userName='张军' order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId ,count(cardId),sum(fee) from record group by cardId,ComputerId +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId,sum(record.fee) 总金额 from record group by cardId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select card.id,userName from card +left join record on card.id=record.cardId +where fee is null group by userName,card.id +--5. 将密码与用户名一样的上网卡信息查询出来 +select A.* from card A,card B where A.pasword=B.userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号,count(ComputerId)使用次数 from record +group by ComputerId order By count(ComputerId) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器号, fee 消费金额 +from record inner join card on record.cardId=card.id where record.cardId like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId,userName,x.id,beginTime,endTime,fee from tbl_record t inner join tbl_computer x on t.ComputerId=x.id inner join tbl_card a on a.id=t.cardId where datepart(weekday,beginTime)=6 or datepart(weekday,beginTime)=7 +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId,userName,x.id,beginTime,endTime,fee from tbl_record t inner join tbl_computer x on ComputerId=x.id inner join tbl_card a on a.id=t.cardId where datediff(hour,beginTime,endTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 cardId 卡号,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from record order by fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cb07ee7d3da14f44820e8e9c63e1c05ea2a4208f --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" @@ -0,0 +1,86 @@ +use master +go +create database abc +go +use abc +go +create table stuInfo +--学生信息表 +( + stuId int identity(1,1), + stuName nvarchar(10) not null, + stuAge int not null, + stuSex int not null, + time datetime, +) +insert into stuInfo +select 'Tom',19,1,null union +select 'Jack',20,0,null union +select 'Rose',21,1,null union +select 'Lulu',19,1,null union +select 'Lili',21,0,null union +select 'abc',20,1,'2007-01-07 01:11:36.590' + +create table courseInfo +--课程信息表 +( + courseId int identity(1,1), + courceName nvarchar(20) not null, + courseMarks int not null +) +insert into courseInfo +select 'JavaBase',4 union +select 'HTML',2 union +select 'JavaScript',2 union +select 'SqlBase',2 + +create table scoreInfo +--分数信息表 +( + scoreId int identity(1,1), + stuId int not null, + courseId int not null, + score int not null +) +insert into scoreInfo +select 1,1,80 union +select 1,2,85 union +select 1,4,50 union +select 2,1,75 union +select 2,3,45 union +select 2,4,75 union +select 3,1,45 union +select 4,1,95 union +select 4,2,75 union +select 4,3,90 union +select 4,4,45 + +select * from stuInfo --学生信息表 +select * from courseInfo --课程信息表 +select * from scoreInfo --分数信息表 + +--题目: +--1.查询出每个学生所选修的课程的数量 和所选修的课程的考试的平均分 +select stuId 学生,COUNT(*) 课程数量,AVG(score) 考试平均分 from scoreInfo group by stuId +--2.查询出每门课程的选修的学生的个数 和 学生考试的总分 +select C.courseId 课程,count(*)数量,sum(S.score)总分 from courseInfo C +inner join scoreInfo S on C.courseId=S.courseId group by C.courseId +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A, stuInfo B where A.stuage= B.stuage and A.stusex=B.stusex and A.stuId !=B.stuId +--4.查询出学分一样的课程信息 +select * from courseInfo where courseMarks in +(select courseMarks from courseInfo group by courseMarks having count(courseMarks)>1) +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select b.stuId 学号,a.stuname 学生姓名,b.courseId 课程号,b.score 分数 +from stuInfo a inner join scoreInfo b on a.stuId=b.stuId +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuId 学号,b.courseId 课程号,courceName 程名,courseMarks 课程名,score 分数 +from courseInfo a inner join scoreInfo b on a.courseId=b.courseId +--7.查询出没有参加考试的学生的学号和姓名 +select stuId 学号,stuname 学生姓名 from stuInfo where stuId not in (select stuId from scoreInfo group by stuId) +--8.查询出是周六周天来报到的学生 +select stuId from stuInfo where datepart(dw,time)=1 or datepart(dw,time)=7 +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuid 学号 ,count(*) 数量 ,avg(score) 平均分 from scoreInfo group by stuid having count(*)>=2 and avg(score)>=70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..add2b6f1031a5af9b6d1d55c518113129cadc507 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery2.sql" @@ -0,0 +1,112 @@ +use master +go +create database abcd +go +use abcd +go +create table card +( + id varchar(30) primary key, + pasword varchar(30), + balance money, + userName varchar(50) +) +create table computer +( + id varchar(30) primary key, + onUse varchar(2) check(onUse=0 or onUse=1), + note ntext +) +create table record +( + id varchar(30) primary key, + cardId varchar(30) foreign key references card(id), + ComputerId varchar(30) foreign key references computer(id), + beginTime datetime, + endTime datetime, + fee money +) +--上网卡信息表 +insert into card(ID,pasword,balance,userName) +select '0023_ABC','555','98','张军'union +select '0025_bbd','abe','300','朱俊'union +select '0036_CCD','何柳','100','何柳'union +select '0045_YGR','0045_YGR','58','证验'union +select '0078_RJV','55885fg','600','校庆'union +select '0089_EDE','张','134','张峻' +--网吧机器说明表 +insert into computer(id,onUse,note) +select '02','0','25555' union +select '03','1','55555' union +select '04','0','66666' union +select '05','1','88888' union +select '06','0','688878' union +select 'B01','0','558558' +--上网记录表 +insert into record(ID,cardId,ComputerId,beginTime,endTime,fee) +select '23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20' union +select '34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23' union +select '45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6' union +select '47','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6' union +select '55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50' union +select '64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','3' union +select '65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23' union +select '98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23' + +select * from card +select * from computer +select * from record + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名, +--机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select cardId,userName,ComputerId,beginTime,endTime,fee from record a +inner join card b on a.cardId=b.id where b.userName='张军' order by fee desc + +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId,COUNT(ComputerId),SUM(fee) from record group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select SUM(balance) from card + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select a.id,a.userName,b.fee from card a left join record b on a.id=b.cardId +where fee is null group by a.id,a.userName,b.fee + +--5. 将密码与用户名一样的上网卡信息查询出来 +select a.* from card a ,card b where a.pasword=b.userName + +select * from card +select * from computer +select * from record + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号,count(*)次数 from record group by ComputerId order by count(*) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select a.id 卡号,a.userName 用户名,b.ComputerId 机器号,b.fee 消费金额 +from card a inner join record b on a.id=b.cardId where a.id like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select cardId,userName a,P.id,beginTime,endTime,fee +from record inner join computer P +on ComputerId=P.id inner join card C on C.id=cardId +where datepart(weekday,beginTime)=1 or datepart(weekday,beginTime)=7 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select cardId,userName,a.id,beginTime,endTime,fee +from record inner join computer a +on computerId=a.id inner join card C on C.id=cardId +where datediff(hour,beginTime,endTime)>12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select cardId,userName,a.id,beginTime,endTime,fee +from record inner join computer a +on ComputerId=a.id inner join card C on C.id=cardId +where fee not in(select top 3 fee from record order by fee DESC) + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3ff73f40f350d3630f042de9c8e4e6bfc5eadc2b --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" @@ -0,0 +1,72 @@ +use master +go +create database stu +go +use stu +go + +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +create table stuInfo +( + stuID int primary key identity, + stuName varchar(20) , + stuAge int , + dtuSex int check(dtuSex=0 or dtuSex=1), + time datetime +) +create table courseInfo +( + courseID int primary key identity, + courseName varchar(20) , + courseMarks int +) +create table scoreInfo +( + scoreID int primary key identity, + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) +insert into stuInfo(stuName,stuAge,dtuSex,time) +values('Tom',19,1, null),('Jack',20,0,null),('Rose',21,1,null),('Lulu',19,1,null) +,('Lili',21,0,null),('abc',20,1,'2007-01-07 01:11:36.590') +insert into courseInfo (courseName,courseMarks) +values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) +insert into scoreInfo(stuID,courseID,score) +values (1,1,80),(1,2,85),(1,4,50),(2,1,75),(2,3,45),(2,4,75),(3,1,45), +(4,1,95),(4,2,75),(4,3,90),(4,4,45) +--题目: +select*from stuInfo +select*from courseInfo +select*from scoreInfo +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuInfo.stuName 名字,count(courseMarks) 选课数量,avg(score) 考试平均分 from scoreInfo,courseInfo,stuInfo +where courseInfo.courseID=scoreInfo.courseID and stuInfo.stuID=scoreInfo.stuID group by stuName +--2.查询出每门课程的选修的学生的个数 和学生考试的总分 +select courseName 课程名字,count(stuID) 人数,sum(score) 总分 from scoreInfo +inner join courseInfo on courseInfo.courseID=scoreInfo.courseID group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 + +select a.stuName 姓名,b.stuAge 年龄,b.dtuSex 性别 from stuInfo a,stuInfo b +where a.stuAge=b.stuAge and a.dtuSex=b.dtuSex and a.stuID<>b.stuID +--4.查询出学分一样的课程信息 +select distinct a.courseName,a.courseMarks from courseInfo a,courseInfo b +where a.courseMarks=b.courseMarks and a.courseName<>b.courseName +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID,stuName,courseID,score from stuInfo inner join scoreInfo on stuInfo.stuID=scoreInfo.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select scoreInfo.stuID 学号 ,scoreInfo.courseID 课程号 ,courseName 课程名 ,courseMarks 课程学分, scoreInfo.score 分数 +from scoreInfo inner join courseInfo on courseInfo.courseID=scoreInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID 学号,stuName 姓名 ,score +from stuInfo left join scoreInfo on stuInfo.stuID=scoreInfo.stuID +where score is null +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuID 学号, count(stuID)课程数目,avg(score) 平均分 from scoreInfo group by stuID having count(stuID)>=2 and avg(score)>=70 + + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6256c4ffc65f4ffbd35fd41e5f42ad671d55cd43 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" @@ -0,0 +1,121 @@ +use master +go +create database wb +on +( name='wb', + filename='D:\wb-mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='wb_log', + filename='D:\wb_log-ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use wb +go +create table tbl_card +( + id varchar(8) primary key, + passWord varchar(15) not null, + balance money not null, + userName nchar(5) +) +create table tbl_computer +( + id char(5) primary key, + onUse int not null, + note text not null +) +create table tbl_record +( + id int primary key, + cardld varchar(8) references tbl_card(id), + ComputerId char(5) references tbl_computer(id), + beginTime datetime , + endTime datetime, + free money not null +) +insert into tbl_card values('0023_ABC','555',99,'张军'),('0025_bbd','abc',300,'朱俊'),('0036_CCD','何柳',100,'何柳') +,('0045_YGR','0045_YGR',58,'验证'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张俊') + +insert into tbl_computer values('02',0,'25555'),('03',1,'55555'),('04',0,'66666'),('05',1,'888888'),('06',0,'688878'),('B01',0,'558558') + + +insert into tbl_record values('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), + ('34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), + ('45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), + ('46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6'), + ('47','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), + ('48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6'), + ('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), + ('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30'), + ('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), + ('98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') + +select*from tbl_card +select*from tbl_computer +select*from tbl_record + +--请完成以下题目: +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select cardld 卡号, userName 用户名, beginTime 开始时间, endTime 结束时间, free 消费金额 + from tbl_record + join tbl_computer + on tbl_computer.id=tbl_record.ComputerId + join tbl_card + on cardld=tbl_card.id + where userName='张军' + order by free desc + + +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId, count(ComputerId) 上网次数,sum(free) 消费总金额 + from tbl_record + group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardld 网卡, sum(free) 消费总金额 from tbl_record group by cardld + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id 卡号 , userName 用户名 + from tbl_card + where id not in (select cardld from tbl_record ) + +--5. 将密码与用户名一样的上网卡信息查询出来 +select T.id 卡号,T.passWord 密码,T.balance 卡上的余额,T.userName 用户名 + from tbl_card,tbl_card T + where tbl_card.passWord=T.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号, count(ComputerId) 使用次数 + from tbl_record + group by ComputerId + order by count(ComputerId) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select cardld 卡号, userName 用户名, ComputerId 机器号,free 消费金额 + from tbl_record + join tbl_card + on tbl_card.id=cardld + where cardld like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardld 卡号, userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间, free 消费金额 + from tbl_record + join tbl_card + on tbl_card.id=cardld + where datediff(HOUR,beginTime,endTime)>=12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select top 7 cardld 卡号, userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间, free 消费金额 + from tbl_record join tbl_card on tbl_card.id=cardld + order by free asc + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..47523185b74756fc79a7147f5f3b789891182f33 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery1.sql" @@ -0,0 +1,95 @@ +create database BAGA +on +( +name='BAGA', +filename='D:\BAGA.mdf', +size=500mb, +filegrowth=15%, +maxsize=15mb +) +log on +( +name='BAGA_log', +filename='D:\BAGA_log.ldf', +size=500mb, +filegrowth=15%, +maxsize=15mb +) +go +use BAGA +go + +create table tbl_card +( +id varchar(10) primary key not null, +passWord varchar(20) not null, +balance varchar(20) not null, +userName varchar(10) +) +create table tbl_computer +( +id varchar(10) primary key not null, +onUse int not null, +note text not null +) +create table tbl_record +( +id int primary key not null, +cardld varchar(10) foreign key references tbl_card(id), +ComputerId varchar(10) foreign key references tbl_computer(id), +beginTime datetime not null, +endTime datetime not null, +fee money not null +) +insert into tbl_card values('0023_ABC','555',98,'张军'),('0025_abe','abc',300,'朱俊'),('0036_CCD','何柳',100,'何柳'),('0045_YGR','0045_YGR',58,'验证'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张峻') + +insert into tbl_computer values('02',0,'25555'),('03',1,'55555'),('04',0,'66666'),('05',1,'88888'),('06',0,'688878'),('B01',0,'558558') + + +insert into tbl_record values('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), + ('34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), + ('45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), + ('46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6'), + ('47','0023_ABC','03','2006-12-23 15:26:00','2007-12-23 22:55:00','50'), + ('48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6'), + ('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), + ('64','0045_Y...','04','2006-12-24 18:00:00','2006-12-24 22:00:00','3...'), + ('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), + ('98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') +select * from tbl_card 上网卡信息表 +select * from tbl_computer 网吧机器说明表 +select * from tbl_record 上网记录表 +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select cardld 卡号, userName 用户名, beginTime 开始时间, endTime 结束时间, fee 消费金额 from tbl_record inner join tbl_computer on tbl_computer.id=tbl_record.ComputerId + inner join tbl_card on cardld=tbl_card.id where userName='张军' order by fee + +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId,COUNT(ComputerId) 上网次数,SUM(fee) 消费总金额 from tbl_record group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardld 网卡,SUM(fee) 消费总金额 from tbl_record group by cardld + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id 卡号,userName 用户名 from tbl_card where id not in (select cardld from tbl_record ) + +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card where passWord=userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号, count(ComputerId) 使用次数 from tbl_record group by ComputerId order by count(ComputerId) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select cardld 卡号, userName 用户名, ComputerId 机器号,fee 消费金额 from tbl_record inner join tbl_card on tbl_card.id=cardld where cardld like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardld 卡号, userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record inner join tbl_card on tbl_card.id=cardld + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardld 卡号, userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record inner join tbl_card on tbl_card.id=cardld + where datediff(HOUR,beginTime,endTime)>=12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 cardld 卡号, userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record inner join tbl_card on tbl_card.id=cardld + order by fee desc + + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..38d2080b72fba413344c5a0589752a88cb3e85b8 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" @@ -0,0 +1,107 @@ +create database ATM +on +( +name='ATM', +filename='D:\ATM.mdf', +size=500mb, +filegrowth=15%, +maxsize=15mb +) +log on +( +name='ATM_log', +filename='D:\ATM_log.ldf', +size=500mb, +filegrowth=15%, +maxsize=15mb +) +go +use ATM +go + +create table stuInfo +( +stuId int primary key identity(1,1) not null, +stuName varchar(10) not null, +stuAge int not null, +stuSex varchar(2) not null, +time varchar(50) not null +) +insert into stuInfo +select '唐僧',19,'男','NULL' union +select '孙悟空',20,'女','NULL' union +select '猪八戒',21,'男','NULL' union +select '沙悟净',19,'男','NULL' union +select '白龙马',21,'女','NULL' union +select '如来佛',20,'男','2007-01-07 01:11:36.590' + +select * from stuInfo + +create table courseInfo +( +courseID int primary key not null, +courseName varchar(15) not null, +courseMarks int not null, +) +insert into courseInfo +select 1,'JavaB',4 union +select 2,'Html',2 union +select 3,'JavaS',2 union +select 4,'Sql',2 + +select * from courseInfo +create table scoreInfo +( +score int primary key not null, +stuID int not null, +courseID int not null, +score1 int not null +) +insert into scoreInfo +select 1,1,1,80 union +select 2,1,2,85 union +select 3,1,4,50 union +select 4,2,1,75 union +select 5,2,3,45 union +select 6,2,4,75 union +select 7,3,1,45 union +select 8,4,1,95 union +select 9,4,2,75 union +select 10,4,3,90 union +select 11,4,4,45 + +select * from scoreInfo +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName,COUNT(courseID) 数量,AVG(score1) 平均分 from scoreInfo inner join stuInfo on scoreInfo.stuID = stuInfo.stuID +group by stuInfo.stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName,COUNT(stuID) 学生个数,SUM(score1) 总分 from scoreInfo inner join courseInfo on courseInfo.courseID=scoreInfo.courseID +group by courseName + +--3.查询出性别一样并且年龄一样的学生的信息 +select a.* from stuInfo A,stuInfo B where A.stuAge=B.stuAge and A.stuSex=B.stuSex and a.stuName != b.stuName + +--4.查询出学分一样的课程信息 +select a.courseName,a.courseMarks from courseInfo A,courseInfo B where A.courseMarks=B.courseMarks and a.courseName != b.courseName +group by a.courseName,a.courseMarks + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuId,stuName,courseID 课程号,score1 分数 from stuInfo inner join scoreInfo on stuInfo.stuId=scoreInfo.courseID + + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuId,scoreInfo.courseID,courseInfo.courseName,courseInfo.courseMarks,score1 from stuInfo inner join scoreInfo on stuInfo.stuId=scoreInfo.courseID +inner join courseInfo on courseInfo.courseID=scoreInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuId,stuName from stuInfo left join scoreInfo on stuInfo.stuId=scoreInfo.courseID +where score1 is null + +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuID,AVG(score1) 平均分,COUNT(courseID) 数量 from scoreInfo group by stuID having COUNT(courseID)>=2 AND AVG(score1)>70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d5679b98183b59f7ef93a233262099c87a30a67a --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" @@ -0,0 +1,109 @@ +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + +use master +go + +create database Student +on +( + name='Student', + filename='C:\学习\数据库\Student.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Student_log', + filename='C:\学习\数据库\Student_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use Student +go + +create table stuInfo +( + stuID int primary key identity(1,1), + stuName nvarchar(10), + stuAge int, + stuSex int check(stuSex='1'or stuSex='0'), + time datetime +) +create table courseInfo +( + courseID int primary key identity(1,1), + courseName nvarchar(20), + courseMarks int, +) +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) + +insert into stuInfo values +('Tom',19,1,null), +('Jack',20,0,null), +('Rose',21,1,null), +('Lulu',19,1,null), +('Lili',21,0,null), +('abc',20,1,'2007-01-07 01:11:36.590') +insert into courseInfo values +('javaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) + +insert into scoreInfo values +(1,1,80), +(1,2,85), +(1,4,50), +(2,1,75), +(2,3,45), +(2,4,75), +(3,1,45), +(4,1,95), +(4,2,75), +(4,3,90), +(4,4,45) + + +select * from stuInfo +select * from courseInfo +select * from scoreInfo +--1.查询出 每个学生 所选修的课程的数量 和 所选修的课程的考试的平均分 +select stuName 学生,count(*) 课程的数量,AVG(score) 平均分 from stuInfo +inner join scoreInfo on stuInfo.stuID=scoreInfo.stuID +group by stuName +--2.查询出 每门课程 的 选修的学生的个数 和 学生考试的总分 +select courseName 课程,count(*) 个数,sum(score) 总分 from courseInfo +inner join scoreInfo on courseInfo.courseID=scoreInfo.courseID +group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo +select A.stuAge,A.stuSex,a.* from stuInfo A,stuInfo B where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuID!=B.stuID +--4.查询出学分一样的课程信息 +select distinct A.* from courseInfo A,courseInfo B where A.courseMarks=B.courseMarks and A.courseID!=B.courseID +--5.查询出 参加了考试的学生 的学号,姓名,课程号和分数 +select stuInfo.stuID,stuName,courseID,score from stuInfo right join scoreInfo on stuInfo.stuID=scoreInfo.stuID +--6.查询出 参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID,stuName,scoreInfo.courseID,courseInfo.courseName,courseInfo.courseMarks,score from stuInfo +right join scoreInfo on stuInfo.stuID=scoreInfo.stuID +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID,stuInfo.stuName from stuInfo full join scoreInfo on stuInfo.stuID=scoreInfo.stuID +except +select stuInfo.stuID,stuInfo.stuName from stuInfo right join scoreInfo on stuInfo.stuID=scoreInfo.stuID +--8.查询出是周六周天来报到的学生 +select * from stuInfo where DATEPART(dw,time)=7 or DATEPART(dw,time)=1 +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName='%a%' +--10.查询出 选修了2门课程以上的 并且 考试平均分在70以上的 学生 的学号和考试平均分以及选修课程的数量 +select stuID 学号,avg(score) 平均分,count(*) 课程数量 from scoreInfo +group by stuID +having avg(score)>70 and count(*)>2 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1c7948c3afd015d8fe89310b62539b40af40f3ed --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" @@ -0,0 +1,115 @@ + use master + go + + create database Itb + on + ( + name='Internet bar', + filename='D:\Itb.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% + ) + log on + ( + name='Internet bar_log.ldf', + filename='D:\Itb_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% + ) + go + use Itb + go + + create table tbl_card + ( + id nvarchar(10) primary key, + passWord nchar(20), + balance decimal(5,2), + userName nvarchar(20) + ) + + create table tbl_computer + ( + id nvarchar(10) primary key, + onUse char(1) check(onUse=0 or onUse=1), + note text + ) + + create table tbl_record + ( + id nvarchar(10) primary key, + cardId nvarchar(10) references tbl_card(id), + ComputerId nvarchar(10) references tbl_computer(id), + beginTime datetime, + endTime datetime, + fee int + ) + +insert into tbl_card values +('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'), +('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张峻') + +insert into tbl_computer values +('02',0,'25555'), +('03',1,'55555'), +('04',0,'66666'), +('05',1,'88888'), +('06',0,'688878'), +('B01',0,'558558') + +insert into tbl_record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20), +(34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23), +(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00',6), +(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',300), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) + +--请完成以下题目: +select * from tbl_card +select * from tbl_computer +select * from tbl_record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select A.id 卡号,userName 用户名,B.ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_card A +inner join tbl_record B on A.id=B.cardId +where userName='张军' +order by fee DESC +--2. 查询出 每台机器 上的 上网次数 和 消费的总金额 +select ComputerId,count(*) 上网次数,sum(fee) 消费总金额 from tbl_record +group by ComputerId +--3. 查询出所有已经使用过的上网卡的 消费总金额 +select cardId 卡号,sum(fee)消费总金额 from tbl_record group by cardId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select A.id,userName 用户名 from tbl_card A left join tbl_record C on C.cardId=A.id +where A.id NOT IN (select cardId from tbl_record) +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card where userName=passWord +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId,COUNT(*) 使用次数 from tbl_record group by ComputerId order by count(*) DESC +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_record.cardId 卡号,userName 用户名,ComputerId 机器号,fee 消费金额 from tbl_card +full join tbl_record on tbl_card.id=tbl_record.cardId +where tbl_card.id like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record +full join tbl_card on tbl_record.cardId=tbl_card.id +where DATEPART(dw,beginTime)=7 or DATEPART(dw,beginTime)=1 +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId,userName,ComputerId,beginTime,endTime,fee from tbl_record +full join tbl_card on tbl_record.cardId=tbl_card.id +where datediff(HH,beginTime,endTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select * from tbl_record +full join tbl_card on tbl_record.cardId=tbl_card.id +where tbl_record.id not in (select top 3 id from tbl_record order by fee DESC) + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\2271/SQLQuery5.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\2271/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8b1db2b58eb76392d80055bc9dd769236b5fd34e --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\2271/SQLQuery5.sql" @@ -0,0 +1,157 @@ +create database bbs + + + +on + +( + + name='bbs', + + filename='E:\bbs.mdf', + + size=5mb, + + maxsize=50mb, + + filegrowth=10% + +) + +log on + +( + + name='bbs_log', + + filename='E:\bbs_log.ldf', + + size=5mb, + + maxsize=50mb, + + filegrowth=10% + +) + +go + +use bbs + +go + + +--学生信息表 +create table stuInfo +( +stuID int primary key identity not null, +stuName varchar(20) not null, +stuAge int not null, +stuSex varchar(2) not null, +stutime varchar(20) +) +drop table stuInfo +insert into stuInfo +select '梦林',19,'男','NULL'union +select 'a海彪',20,'女','NULL'union +select '吴煌',21,'女','NULL'union +select '巧晶',19,'女','NULL'union +select '心怡',21,'女','NULL'union +select 'a坦克',20,'女','2017_01_07:11:36.590' + +select * from stuInfo + + +--课程表 +create table courseInfo +( +courseID int primary key identity not null, +courseName varchar(20) not null, +courseMarks int not null +) +drop table courseInfo +insert into courseInfo +select '语文',4 union +select '数学',2 union +select '英语',2 union +select 'SQL',2 + +select * from courseInfo + +--分数信息表 +create table scoreInfo +( +scoreID int primary key identity not null, +stuid int not null, +courseID int not null, +score int not null, +) + +insert into scoreInfo +select 1,1,80 union +select 1,2,85 union +select 1,4,50 union +select 2,1,75 union +select 2,3,45 union +select 2,4,75 union +select 3,1,45 union +select 4,1,95 union +select 4,2,75 union +select 4,3,90 union +select 4,4,45 + +select * from scoreInfo +select * from stuInfo +select * from courseInfo + +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select Y.stuName 姓名,M.courseName 科目,AVG(L.score) 平均分,count(M.courseMarks) 课程数量 from stuInfo Y inner join courseInfo M on Y.stuID=M.courseID inner join scoreInfo L on Y.stuID=L.stuid group by M.courseName,Y.stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 科目, count(M.stuID ) 学生人数, sum(score ) 总分 from courseInfo Y inner join stuInfo M ON Y.courseID=M.stuID inner join scoreInfo L on Y.courseID=L.courseID group by Y.courseName + +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo A,stuInfo B where A.stuAge =B.stuAge and A.stuSex=B.stuSex AND A.stuid!=B.stuid + + +--4.查询出学分一样的课程信息 +select * from courseInfo A,courseInfo B where A.courseMarks=B.courseMarks AND A.courseid!=B.courseid + + + +select * from scoreInfo +--课程表 +select * from stuInfo +--学生表 +select * from courseInfo +--分数表 + + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select Y.stuid 学号,Y.stuName 姓名,L.courseID 课程号,M.score 分数 from stuInfo Y inner join scoreInfo M on Y.stuid = M.stuid inner join courseInfo L on Y.stuid =L.courseID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select Y.stuid 学号,M.courseid 课程号,M.courseName 课程名, M.courseMarks 课程学分,L.score 分数 from stuInfo Y inner join courseInfo M on Y.stuid =M.courseid inner join scoreInfo L on Y.stuid = L.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuid,stuname from stuInfo left join scoreInfo on stuInfo.stuId=scoreInfo.stuid +where score is null +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like 'a%' + +--10.查询出选修了2门 课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 + +select stuid 学号,avg(score)平均分,count(courseID)课程的数量 from scoreInfo group by stuid +having avg(score)>70 and count(courseID)>2 + + +select * from scoreInfo +--课程表 +select * from stuInfo +--学生表 +select * from courseInfo +--分数表 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\2271/SQLQuery6.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\2271/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0edd51c00b2992e53bc204734e60e96515c9ccd6 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\2271/SQLQuery6.sql" @@ -0,0 +1,138 @@ +create database dds + + + +on + +( + + name='dds', + + filename='E:\dds.mdf', + + size=5mb, + + maxsize=50mb, + + filegrowth=10% + +) + +log on + +( + + name='dds_log', + + filename='E:\dds_log.ldf', + + size=5mb, + + maxsize=50mb, + + filegrowth=10% + +) + +go + +use dds + +go +create table card --网卡信息表 +( + id varchar(30) primary key,--卡号,主键 + pasword varchar(30),--密码 + balance money,--卡上的余额 + userName varchar(50)--该上网卡所属的用户名 +) +create table computer --网吧机器说明表 +( + id varchar(30) primary key,--机器编号,主键 + onUse varchar(2) check(onUse=0 or onUse=1),--该机器是否正在使用,0为未使用,1为正在使用 + note ntext --该机器的说明 +) +create table record --上网记录表 +( + id varchar(30) primary key,--记录编号,主键 + cardId varchar(30) foreign key references card(id),--本次上网的卡号,外键 + ComputerId varchar(30) foreign key references computer(id),--本次上网记录所使用的机器号,外键 + beginTime datetime,--本次上网记录的开始时间 + endTime datetime,--本次上网记录的结束时间 + fee money --本次上网的费用 +) + +insert into card +select '0023_ABC','555','98','张军'union +select '0025_bbd','abe','300','朱俊'union +select '0036_CCD','何柳','100','何柳'union +select '0045_YGR','0045_YGR','58','证验'union +select '0078_RJV','55885fg','600','校庆'union +select '0089_EDE','张','134','张峻' + +select * from card + +insert into computer +select '02','0','25555' union +select '03','1','55555' union +select '04','0','66666' union +select '05','1','88888' union +select '06','0','688878' union +select 'B01','0','558558' + +select * from computer + +insert into record +select '23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20' union +select '34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23' union +select '45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6' union +select '47','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6' union +select '55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50' union +select '64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','3' union +select '65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23' union +select '98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23' + +select * from record -- --上网记录表 +select * from computer --网吧机器说明表 +select * from card --网卡信息表 + + + +--请完成以下题目: +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + select Y.cardId 卡号,L.userName 用户名,Y.ComputerId 机器编号,Y.beginTime 开始时间,Y.endTime 结束时间,Y.fee 消费金额 from record Y inner join card L on Y.cardId=L.id where L.userName ='张军' order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select count(*) 上网次数, ComputerId 机器编号,sum(fee) 总金额 from record group by ComputerId + + + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardid 网卡, sum(fee) 消费总金额 from record group by cardid + + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select M.id 卡号, M.userName 用户名 from card M left join record Y on Y.cardId=M.id where fee is null GROUP BY M.id,M.userName + + +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from card A,card B where A.pasword=B.userName + + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号,count(*)使用次数 from record group by ComputerId + + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器号, fee 消费金额 from record inner join card on record.cardId=card.id where record.cardId like '%ABC' + + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 cardId 卡号,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from record order by fee desc + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ca04c58f9bcc8d9b614e870cd031925130f9d6e5 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" @@ -0,0 +1,109 @@ +use master +create database excise +on +( + name = 'excise', + filename = 'D:\excise.mdf' +) +log on +( + name = 'excise_log', + filename = 'D:\excise_log.ldf' +) + +use excise + +create table stuInfo +( + stuID int identity primary key, + stuName varchar(10)not null, + stuAge int , + stuSex nvarchar(1) check(stuSex in('男','女')), + time datetime +) +insert into stuInfo values +('Tom',19,'男',null), +('Jack',20,'女',null), +('Rose',21,'男',null), +('Lulu',19,'男',null), +('Lili',21,'女',null), +('abc',20,'男','2007-01-07 01:11:36.590') +select * from stuInfo + +create table courseInfo +( + courseID int identity primary key, + courseName varchar(20) not null, + courseMarks int +) +insert into courseInfo values +('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) +select * from courseInfo + +create table scoreInfo +( + scoreID int identity, + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) + +insert into scoreInfo values +(1,1,80), +(1,2,85), +(1,4,50), +(2,1,75), +(2,3,45), +(2,4,75), +(3,1,45), +(4,1,95), +(4,2,75), +(4,3,90), +(4,4,45) + +--.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuInfo.stuName 姓名,count(courseName)选修课程数量,avg(score)平均分 from stuInfo,courseInfo,scoreInfo where +stuInfo.stuID = courseInfo.courseID and +courseInfo.courseID =scoreInfo.stuID group by stuInfo.stuName,courseName + +--2.查询出 每门课程 的选修的 学生的个数和学生考试的总分 +select courseID 课程,COUNT(distinct stuID)学生个数,SUM(score)总分 from scoreInfo group by courseID + +--3.查询出性别一样并且年龄一样的学生的信息 +select a.* from stuInfo a,stuInfo b where a.stuAge=b.stuAge and a.stuSex = b.stuSex and a.stuID != b.stuID + +--4.查询出学分一样的课程信息 +select a.courseName,a.courseMarks from courseInfo a, courseInfo b +where a.courseMarks = b.courseMarks and a.courseID != B.courseID +group by a.courseName,a.courseMarks + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select st.stuID 学号,st.stuName 姓名,co.courseID 课程号,sc.score 分数 from stuInfo st +left join scoreInfo sc on st.stuID = sc.stuID +left join courseInfo co on sc.courseID = co.courseID +where sc.score is not null + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select st.stuID 学号,st.stuName 姓名,co.courseID 课程号,co.courseName 课程名, sc.score 分数 from stuInfo st +left join scoreInfo sc on st.stuID = sc.stuID +left join courseInfo co on sc.courseID = co.courseID +where sc.score is not null + +--7.查询出没有参加考试的学生的学号和姓名 +select st.stuID 学号,st.stuName from stuInfo st +left join scoreInfo sc on st.stuID = sc.stuID +where sc.score is null + +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的 学号 和 考试平均分 以及 选修课程的数量 +select st.stuID 学号,avg(sc.score)平均分,count(co.courseID) 选修数量 from stuInfo st +left join scoreInfo sc on st.stuID = sc.stuID +left join courseInfo co on sc.courseID = co.courseID +group by st.stuID having avg(sc.score)>70 and count(co.courseID)>=2 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2fa093d9a4944536a3bf32230af9c0d2fa4fb011 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" @@ -0,0 +1,110 @@ +create database wb +on +( + name = 'wb', + filename = 'D:\数据库\wb.mdf' +) +log on +( + name = 'wb_log', + filename = 'D:\数据库\wb_log.ldf' +) + +use wb + +create table tbl_card +( + id varchar(10) primary key, + PassWord nvarchar(10) not null, + balance int not null, + userName nvarchar(2) not null +) +insert into tbl_card values +('0023_ABC','555','98','张军'), +('0025_bbb','abe','300','朱俊'), +('0036_CCD','何柳','100','何柳'), +('0045_YGR','0045_YGR','58','证验'), +('0078_RJV','55885fg','600','校庆'), +('0089_EDE','zhang','134','张峻') +select * from tbl_card + +create table tbl_computer +( + id varchar(3) primary key, + onUser int check(onUser in ('0','1')), + Note int +) +insert into tbl_computer values +('02',0,25555), +('03',1,55555), +('04',0,66666), +('05',1,88888), +('06',0,688878), +('B01',0,558558) +select * from tbl_computer + +create table tbl_record +( + id int primary key, + cardId varchar(10) references tbl_card(id), + ComputerId varchar(3) references tbl_computer(id), + beginTime datetime not null, + endTime datetime not null, + free int not null +) +insert into tbl_record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), +(34,'0025_bbb','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), +(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6'), +(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6'), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30'), +(65,'0025_bbb','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), +(98,'0025_bbb','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') + +--1. 查询出用户名为'张军'的上网卡的上网记录, +--要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select a.id 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间, free 金额 from tbl_card a +left join tbl_record b on b.cardId = a.id +left join tbl_computer c on c.id = b.cardId +where userName = '张军' order by free desc + +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId 机器编号,count(id) 次数,sum(free) 总金额 from tbl_record group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select a.id 卡号,sum(free) 金额 from tbl_card a right join tbl_record b on a.id = b.cardId group by a.id + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select a.id 卡号,a.userName 用户名, b.free from tbl_card a +left join tbl_record b on a.id = b.cardId +where free is null +group by a.id,a.userName,b.free + +--5. 将密码与用户名一样的上网卡信息查询出来 +select a.* from tbl_card a +inner join tbl_card b on a.PassWord = b.PassWord and a.userName != b.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号,count(ComputerId) 次数 from tbl_record group by ComputerId order by count(ComputerId) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select b.userName 用户名,a.ComputerId 机器号,count(cardId) 次数,sum(free) 金额 from tbl_record a +left join tbl_card b on a.cardId = b.id +where a.cardId like '%ABC' +group by b.userName,a.ComputerId + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select * from tbl_card +select * from tbl_computer +select * from tbl_record +select top 7 a.id 卡号, a.userName 用户名, b.ComputerId 机器号,b.beginTime 开始时间,b.endTime 结束时间, max(b.free) 金额 from tbl_card a +left join tbl_record b on a.id = b.cardId +left join tbl_computer c on c.id = b.cardId +group by a.id, b.ComputerId,b.beginTime,b.endTime,a.userName order by max(b.free) diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8dc85f2f212215b2257bad59eafc901078886ff8 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery2.sql" @@ -0,0 +1,88 @@ +create database wbjfk +on +( + name ='wbjfk', + size=10, + filename='D:\wbjfk.mdf', + maxsize=100, + filegrowth=10 +) +log on +( + name ='wbjfk_log', + size=10, + filename='D:\wbjfk_log.ldf', + maxsize=100, + filegrowth=10 +) +go +use wbjfk +go +create table tbl_card +( + id nvarchar(10) primary key, + passWord nvarchar(18), + balance int, + userName nvarchar(20) +) +create table tbl_computer +( + id nvarchar(10) primary key, + onUse int, + note nvarchar(200) +) +create table tbl_record +( + id nvarchar(10) primary key, + cardid nvarchar(10) foreign key references tbl_card(id), + Computerid nvarchar(10) foreign key references tbl_computer(id), + beginTime datetime, + endTime datetime, + fee int +) +insert into tbl_card values('0023_ABC','555',98,'张军'),('0025_bbd','abc',300,'朱俊'),('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'),('0078_RJV','55885lg',600,'校庆'),('0089_EDE','zhang',134,'张峻') +insert into tbl_computer values('02',0,'25555'),('03',1,'55555'),('04',0,'66666'),('05',1,'88888'),('06',0,'688878'),('B01',0,'558558') +insert into tbl_record values(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20), +(34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23), +(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00',6), +(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',30), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) + +select * from tbl_card +select * from tbl_computer +select * from tbl_record + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号 +--,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select cardid,userName,Computerid,beginTime,endTime from tbl_card a left join tbl_record d on a.id = d.cardid + where userName = '张军' order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select b.id,count(Computerid),sum(fee) from tbl_record a left join tbl_computer b on a.Computerid = b.id group by b.id +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardid,sum(fee) from tbl_record group by cardid +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id,userName from tbl_card where id not in (select distinct cardid from tbl_record ) +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card where id=passWord +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId,count(ComputerId) from tbl_record group by ComputerId order by count(ComputerId) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器号, fee 消费金额 +from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where tbl_record.cardId like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardid,userName,Computerid,beginTime,endTime from tbl_card a left join tbl_record d on a.id = d.cardid +where datepart(weekday,beginTime)=6 or datepart(weekday,beginTime)=7 +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId,userName,x.id,beginTime,endTime,fee from tbl_record t +inner join tbl_computer x on ComputerId=x.id inner join tbl_card a on a.id=t.cardId +where datediff(hour,beginTime,endTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId,userName,x.id,beginTime,endTime,fee from tbl_record t +inner join tbl_computer x on ComputerId=x.id inner join tbl_card a on a.id=t.cardId +where fee not in(select top 3 fee from tbl_record order by fee DESC) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a5f22cd1b4866116014a00a43d515c461a1b4f08 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery3.sql" @@ -0,0 +1,76 @@ +create database st +on +( + name ='st', + size=10, + filename='D:\st.mdf', + maxsize=100, + filegrowth=10 +) +log on +( + name ='st_log', + size=10, + filename='D:\st_log.ldf', + maxsize=100, + filegrowth=10 +) +go +use st +go +create table stuInfo +( + stuID int primary key identity, + stuName nvarchar(8), + stuAge int, + stuSex nchar(2), + time datetime +) +create table courseInfo +( + courseID int primary key identity, + courseName nvarchar(12), + courseMarks int +) +create table scoreInfo +( + scoreID int primary key identity, + stuID int, + courseID int, + score int +) +insert into stuInfo(stuName,stuAge,stuSex,time) values ('Tom',19,'男',''),('Jack',20,'女',''),('Rose',21,'男',''), +('Lulu',19,'男',''),('Lili',21,'女',''),('abc',20,'男','2007-01-07 01:11:36.590') +insert into courseInfo values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) +insert into scoreInfo values(1,1,80),(1,2,85),(1,4,50),(2,1,75),(2,3,45),(2,4,75),(3,1,45),(4,1,95),(4,2,75),(4,3,90), +(4,4,45) + +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName,count(c.stuID) 选课数量,avg(score) 所选课考试的平均分 from stuInfo t +inner join courseInfo x on t.stuID=x.courseID left join scoreInfo c on t.stuID=c.stuID group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName,count(StuName) 学生的个数,sum(score) 考试的总分 from scoreInfo s +inner join stuInfo x on s.StuID=x.StuID inner join courseInfo a on s.courseID=a.courseID group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select s.* from stuInfo s,stuInfo x where s.stuSex=x.stuSex and s.stuAge=x.stuAge and s.stuID!=x.stuID +--4.查询出学分一样的课程信息 +select distinct t.* from courseInfo t,courseInfo x where t.courseMarks=x.courseMarks and t.courseID!=x.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select s.stuID,stuName,x.courseID,score from stuInfo s +left join courseInfo x on s.stuID = x.courseID +left join scoreInfo o on o.scoreID = s.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select t.stuID 学号, t.courseID 课程号,x.courseName 课程名,courseMarks 学分,t.score 分数 from scoreInfo t +left join courseInfo x on t.courseID=x.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select t.stuID 学号,t.stuName 姓名 from stuInfo t left join scoreInfo x on t.stuID=x.stuID where score is null +--8.查询出是周六周天来报到的学生 +select t.stuID 学号,t.stuName 姓名 from stuInfo t left join scoreInfo x on t.stuID=x.stuID where score is null +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuID,count(courseID)课程的数量,avg(score)平均分 from scoreInfo group by stuID having avg(score)>=70 and count(courseID)>=2 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..aba6bdddc5afd1fbb17c8273ace26b6a4b08cd0b --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery2.sql" @@ -0,0 +1,118 @@ +use master +go + +create database student +on primary +( +name='student', +filename='D:\.mdf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +log on +( +name='student_log', +filename='D:\_log.ldf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +go + +use student +--创建学生信息表 +create table stuInfo +( +stuId int primary key identity, +stuName varchar(10) not null, +stuAge varchar(6) not null, +stuSex nvarchar(1) not null check(stuSex=1 or stuSex=0), +time datetime +) +go + +--创建课程信息表 +create table courseInfo +( +courseId int primary key identity, +courseName varchar(10) not null, +couorseMarks int +) +go + +--创建分数信息表 +create table scoreInfo +( +scorsId int primary key identity, +stuId int not null, +courseId int not null, +score int +) +go + +select * from stuInfo + +insert into stuInfo(stuName,stuAge,stuSex,time) values ('Tom',19,1,null),('Jack',20,0,null),('Rose',21,1,null),('Lulu',19,1,null),('Lili',21,0,null),('abc',20,1,'2007-01-07 01:11:36.590') + + +select * from courseInfo + +insert into courseInfo(courseName,couorseMarks) values ('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) + +select * from scoreInfo + +insert into scoreInfo(stuId,courseId,score) values +(1,1,80), +(1,2,85), +(1,4,50), +(2,1,75), +(2,3,45), +(2,4,75), +(3,1,45), +(4,1,95), +(4,2,75), +(4,3,90), +(4,4,45) + +--题目: +select * from stuInfo +select * from courseInfo +select * from scoreInfo +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + +select s3.stuId,couorseMarks 课程数量,avg(score) 平均分 from courseInfo s1 inner join scoreInfo s2 on s1.courseId=s2.courseId +inner join stuInfo s3 on s3.stuId=s2.stuId +group by s3.stuId,couorseMarks order by s3.stuId +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + +select s1.courseId,sum(stuId) 人数,sum(score) 总分 from courseInfo s1 inner join scoreInfo s2 on s1.courseId=s2.courseId group by s1.courseId + +--3.查询出性别一样并且年龄一样的学生的信息 + +select s1.* from stuInfo s1,stuInfo s2 where s1.stuAge=s2.stuAge and s1.stuSex=s2.stuSex and s1.stuId!=s2.stuId + +--4.查询出学分一样的课程信息 + +select s1.* from scoreInfo s1,scoreInfo s2 where s1.score=s2.score and s1.stuId!=s2.stuId + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + +select s1.stuId 学号,s1.stuName 姓名,s2.scorsId,s2.score from stuInfo s1 left join scoreInfo s2 on s1.stuId=s2.stuId + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + +select s1.stuId,s2.courseId,s2.courseName,s2.couorseMarks,s1.score from scoreInfo s1,courseInfo s2 +--7.查询出没有参加考试的学生的学号和姓名 + +select * from stuInfo s1 where s1.stuId not in (select s2.stuId from scoreInfo s2) + +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 + +select * from stuInfo s1 where s1.stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 + +select s1.stuId,avg(s1.score),count(s1.courseId) from scoreInfo s1 group by s1.stuId having avg(s1.score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bb8fffef1308f0578c8777f4e1fcd82cf7ec9d76 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery3.sql" @@ -0,0 +1,141 @@ +use master +go + +create database wb +on primary +( +name='wb', +filename='D:\.mdf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +log on +( +name='wb_log', +filename='D:\_log.ldf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +go + +use wb + +--表一为上网卡信息表(tbl_card) +--id: 卡号,主键 +--passWord:密码 +--balance:卡上的余额 +--userName:该上网卡所属的用户名 + +create table tbl_card +( +id varchar(10) primary key, +passWord varchar(18) not null, +balance varchar(20) not null, +userName varchar(16) +) +go + +--表2为网吧机器说明表(tbl_computer) +--id:机器编号,主键 +--onUse:该机器是否正在使用,0为未使用,1为正在使用 +--note:该机器的说明 + +create table tbl_computer +( +id varchar(6) primary key, +onUse varchar(1) check(onUse=0 or onUse=1), +note varchar(100) +) +go + +--表3为上网记录表(tbl_record): +--id:记录编号,主键 +--cardId:本次上网的卡号,外键 +--ComputerId:本次上网记录所使用的机器号,外键 +--beginTime:本次上网记录的开始时间 +--endTime:本次上网记录的结束时间 +--fee:本次上网的费用 + +create table tbl_record +( +id varchar(2) primary key , +cardId varchar(10) foreign key(cardId) references tbl_card(id), +ComputerId varchar(6) foreign key(ComputerId) references tbl_computer(id), +beginTime datetime, +endTime datetime, +fee int +) +go + +insert into tbl_card(id,passWord,balance,userName) values +('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'), +('0036_DDD0','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'), +('0078_RJV','zhang',134,'张俊') + +insert into tbl_computer values +('02',0,25555), +('03',1,55555), +('04',0,66666), +('05',1,88888), +('06',0,688878), +('B01',0,558558) + +insert into tbl_record(id,cardId,ComputerId,beginTime,endTime,fee) values +(23,'0078_RJV','B01','2007-07-15 19:00:00.000','2007-07-15 21:00:00.000',20), +(34,'0025_bbd','02','2006-12-25 18:00:00.000','2006-12-25 22:00:00.000',23), +(45,'0023_ABC','03','2006-12-23 15:26:00.000','2006-12-23 22:55:00.000',50), +(46,'0023_ABC','03','2006-12-22 15:26:00.000','2006-12-22 22:55:00.000',6), +(47,'0023_ABC','03','2006-12-23 15:26:00.000','2006-12-23 22:55:00.000',50), +(48,'0023_ABC','03','2007-01-06 15:26:00.000','2007-01-06 22:55:00.000',6), +(55,'0023_ABC','03','2006-07-21 15:26:00.000','2007-07-21 22:55:00.000',50), +(64,'0045_YGR','04','2006-12-24 18:00:00.000','2006-12-24 22:00:00.000',30), +(65,'0025_bbd','02','2006-12-28 18:00:00.000','2006-12-28 22:00:00.000',23), +(98,'0025_bbd','02','2006-12-26 18:00:00.000','2006-12-26 22:00:00.000',23) + +select * from tbl_card +select * from tbl_computer +select * from tbl_record + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + +select s2.cardId,s1.userName,s2.ComputerId,s2.beginTime,s2.endTime,s2.fee from tbl_card s1,tbl_record s2 where s1.userName='张军' and s2.cardId='0023_ABC' + +--2. 查询出每台机器上的上网次数和消费的总金额 + +select s1.ComputerId,count(s1.ComputerId) 上网次数,sum(s1.fee) 总金额 from tbl_record s1 group by s1.ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 + +select s1.cardId,sum(s1.fee) 总消费 from tbl_record s1 group by s1.cardId + +--4. 查询出从未消费过的上网卡的卡号和用户名 + +select s1.id,s1.userName from tbl_card s1 where s1.id not in (select s2.cardId from tbl_record s2) + +--5. 将密码与用户名一样的上网卡信息查询出来 + +select * from tbl_card s1 where s1.passWord=s1.userName + +--6. 查询出使用次数最多的机器号和使用次数 + +select top 1 s1.ComputerId,count(s1.ComputerId) 使用次数 from tbl_record s1 group by s1.ComputerId order by count(s1.ComputerId) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + +select s1.userName,s2.ComputerId,s2.fee from tbl_card s1,tbl_record s2 where s1.id like'%ABC' and s1.id=s2.cardId + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select s1.id,s1.userName,s2.ComputerId,s2.beginTime,s2.endTime,s2.fee from tbl_card s1,tbl_record s2 where s2.endTime-s2.beginTime>12 and s1.id=s2.cardId + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select top 3 s1.id,s1.userName,s2.ComputerId,s2.beginTime,s2.endTime,s2.fee from tbl_card s1,tbl_record s2 where s1.id=s2.cardId order by s2.fee desc diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..71546008176fbd74096d2053613c6e313f6390dd --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" @@ -0,0 +1,104 @@ + +create database Jxc + +on +( + name='Jxc', + filename='D:\Jxc.mdf' +) +log on +( + name='Jxc_log', + filename='D:\Jxc.ldf_log' +) +use Jxc +create table stuInfo +( + stuID int primary key , + stuName varchar(20), + stuAge int , + stuSex nvarchar(1), + stime datetime +) + +create table courseInfo +( + courseID int primary key , + courseName varchar(25), + courseMarks varchar(5) +) +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int foreign key references stuInfo(stuID) , + courseID int foreign key references courseInfo(courseID), + score int +) +--学生信息表 +insert into stuInfo(stuID,stuName,stuAge,stuSex) +select 1,'Tom',19,1 union +select 2,'Jack',20,0union +select 3,'Rose',21,1union +select 4,'Lulu',19,1 union +select 5,'Lili',21,0union +select 6,'abc',20,1 + +update stuInfo set stime='2007-01-07 01:11:36.590'where stuName='abc' +--课程表 +insert into courseInfo(courseID,courseName,courseMarks) +select 1,'JavaBase',4 union +select 2,'HTML',2 union +select 3,'JavaScript',2 union +select 4,'SqlBase',2 +-- 成绩表 +insert into scoreInfo(stuID,courseID,score) +select 1,1,80 union +select 1,2,85 union +select 1,4,50 union +select 2,1,75 union +select 2,3,45 union +select 2,4,75 union +select 3,1,45 union +select 4,1,95 union +select 4,2,75 union +select 4,3,90 union +select 4,4,45 + +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +select * from stuInfo --学生信息表 +select * from courseInfo --课程表 +select * from scoreInfo -- 成绩表 +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuID,count(courseID)课程的数量,avg(score)平均分 from scoreInfo group by stuID +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseID 课程, count(courseID)选修个数, sum(score)总分 from scoreInfo group by courseID + +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A, stuInfo B +where A.stuSex=B.stuSex AND A.stuAge=B.stuAge AND A.stuID!=B.stuID +--4.查询出学分一样的课程信息 +select distinct A.* from courseInfo A,courseInfo B +where A.courseMarks=B.courseMarks AND A.courseID!=B.courseID +--5.查询出参加了 考试的学生的学号,姓名,课程号和分数 +select scoreInfo.stuID 学号,stuName 姓名,courseID 课程号,score from scoreInfo +left join stuInfo on scoreInfo.stuID=stuInfo.stuID group by scoreInfo.stuID,stuName,courseID,score +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select scoreInfo.stuID 学号, scoreInfo.courseID 课程号,courseInfo.courseName 课程名,courseMarks 学分,scoreInfo.score 分数 from scoreInfo +left join courseInfo on scoreInfo.courseID=courseInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select Stuinfo.stuID 学号,stuInfo.stuName 姓名 from stuInfo +left join scoreInfo on stuInfo.stuID=scoreInfo.stuID +where score is null +--8.查询出是周六周天来报到的学生 + --超纲了!!!!!!!!!!!!!!!! +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出 选修了2门课程 以上的 并且 考试平均分 在70以上 的学生的 学号 和 考试平均分 以及 选修课程的数量 +select stuID,count(courseID)课程的数量,avg(score)平均分 from scoreInfo +group by stuID +having avg(score)>=70 and count(courseID)>=2 + +select * from courseInfo --课程表 +select * from scoreInfo -- 成绩表 +select * from stuInfo --学生信息表 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..61779bdeaba47bc20bd812baa73d4859c2f490c7 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" @@ -0,0 +1,125 @@ +create database AKA +on +( + name='D:\AKA', + filename='D:\AKA.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log +on +( + name='D:\AKA_log', + filename='D:\AKA_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +use AKA +create table stu +( + stuID int identity primary key not null, + stuName varchar(10) not null, + stuAge int not null, + stuSex varchar(2) not null check(stuSex='男' or stuSex='女'), + stutime datetime +) +create table cou +( + courseID int identity primary key not null, + courseName varchar(15) not null, + courseMarks int not null +) +create table sco +( + scoreID int identity not null, + stuID int foreign key references stu(stuID), + courseID int foreign key references cou(courseID), + score int not null, +) +insert into stu values +('Tom',19,'男',null), +('Jack',20,'女',null), +('Rose',21,'男',null), +('Lulu',19,'男',null), +('Lili',21,'女',null), +('abc',20,'男','2007-01-07 01:11:36.590') +select * from stu + +insert into cou values +('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) + +select * from cou + +insert into sco values +(1,1,80), +(1,2,85), +(1,4,50), +(2,1,75), +(2,3,45), +(2,4,75), +(3,1,45), +(4,1,95), +(4,2,75), +(4,3,90), +(4,4,45) + +select * from sco + +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + +--题目: +select * from stu +select * from cou +select * from sco +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + +select stu.stuID 学号,count(*) 课程数量,avg(score)平均分 from stu left join sco on stu.stuID=sco.stuID group by stu.stuID + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + +select courseName 课程,count(*)人数,sum(score)总分 from cou left join sco on cou.courseID=sco.stuID group by courseName + +--3.查询出性别一样并且年龄一样的学生的信息 + +select c.* from stu c,stu s where c.stuAge=s.stuAge and c.stuSex=s.stuSex and c.stuID!=s.stuID + + +--4.查询出学分一样的课程信息 + +select distinct A.* from cou A,cou B where A.courseMarks=B.courseMarks and A.courseID!=B.courseID + +select * from stu +select * from cou +select * from sco + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + +select stu.stuID 学号,stuName 姓名,sco.courseID 课程号,score 分数 from stu right join sco on stu.stuID=sco.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + +select stuID 学号,sco.courseID 课程号,courseName 课程名,courseMarks 课程学分,score 分数 from sco left join cou on sco.courseID=cou.courseID + +--7.查询出没有参加考试的学生的学号和姓名 + +select stu.stuID,stuName from stu left join sco on stu.stuID=sco.stuID where score is null + +--8.查询出是周六周天来报到的学生 +select stuName from stu where DATEPART(WEEKDAY,stutime)=1 or DATEPART(WEEKDAY,stutime)=7 + +--9.查询出姓名中有字母a的学生的信息 + + +select * from stu where stuname like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 + +select avg(score)平均分,stuID 学号,count(*)课程数 from sco group by stuID having avg(score)>70 and count(*)>2 +select * from stu +select * from cou +select * from sco diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..14fb6deb371819ca991903b8cc0daaff9214645c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery2.sql" @@ -0,0 +1,126 @@ +create database wb +on +( name='wb', + filename='D:\wb-mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='wb_log', + filename='D:\wb_log-ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use wb +go +create table tbl_card +( + id varchar(8) primary key, + passWord varchar(15) not null, + balance money not null, + userName nchar(5) +) +create table tbl_computer +( + id char(5) primary key, + onUse int not null, + note text not null +) +create table tbl_record +( + id int primary key, + cardld varchar(8) references tbl_card(id), + ComputerId char(5) references tbl_computer(id), + beginTime datetime , + endTime datetime, + fee money not null +) +insert into tbl_card values('0023_ABC','555',99,'张军'),('0025_bbd','abc',300,'朱俊'),('0036_CCD','何柳',100,'何柳'),('0045_YGR','0045_YGR',58,'验证'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张俊') + +insert into tbl_computer values('02',0,'25555'),('03',1,'55555'),('04',0,'66666'),('05',1,'888888'),('06',0,'688878'),('B01',0,'558558') + + +insert into tbl_record values +('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), +('34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), +('45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +('46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6'), +('47','0023_ABC','03','2006-12-23 15:26:00','2007-12-23 22:55:00','50'), +('48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6'), +('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), +('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30'), +('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), +('98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') + +select*from tbl_card +select*from tbl_computer +select*from tbl_record +--请完成以下题目: +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select cardld 卡号, userName 用户名, beginTime 开始时间, endTime 结束时间, fee 消费金额 + from tbl_record + join tbl_computer + on tbl_computer.id=tbl_record.ComputerId + join tbl_card + on cardld=tbl_card.id + where userName='张军' + order by fee + + +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId, count(ComputerId) 上网次数,sum(fee) 消费总金额 + from tbl_record + group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardld 网卡, sum(fee) 消费总金额 from tbl_record group by cardld + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id 卡号 , userName 用户名 + from tbl_card + where id not in (select cardld from tbl_record ) + +--5. 将密码与用户名一样的上网卡信息查询出来 +select T.id 卡号,T.passWord 密码,T.balance 卡上的余额,T.userName 用户名 + from tbl_card,tbl_card T + where tbl_card.passWord=T.userName + +select T.* + from tbl_card,tbl_card T + where tbl_card.passWord=T.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号, count(ComputerId) 使用次数 + from tbl_record + group by ComputerId + order by count(ComputerId) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select cardld 卡号, userName 用户名, ComputerId 机器号,fee 消费金额 + from tbl_record + join tbl_card + on tbl_card.id=cardld + where cardld like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardld 卡号, userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 + from tbl_record + join tbl_card + on tbl_card.id=cardld + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardld 卡号, userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 + from tbl_record + join tbl_card + on tbl_card.id=cardld + where datediff(HOUR,beginTime,endTime)>=12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 cardld 卡号, userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 + from tbl_record + join tbl_card + on tbl_card.id=cardld + order by fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e94a03fcd053db1b06529ff74f4970d4ceeec508 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" @@ -0,0 +1,65 @@ +create database tx +on +( + name ='tx', + size=10, + filename='D:\tx.mdf', + maxsize=100, + filegrowth=10 +) +log on +( + name ='tx_log', + size=10, + filename='D:\tx_log.ldf', + maxsize=100, + filegrowth=10 +) +go +use tx +go +create table stuInfo +( + stuID int primary key identity, + stuName nvarchar(8), + stuAge int, + stuSex nchar(2), + time datetime +) +create table courseInfo +( + courseID int primary key identity, + courseName nvarchar(12), + courseMarks int +) +create table scoreInfo +( + scoreID int primary key identity, + stuID int, + courseID int, + score int +) +insert into stuInfo(stuName,stuAge,stuSex,time) values ('Tom',19,'男',''),('Jack',20,'女',''),('Rose',21,'男',''), +('Lulu',19,'男',''),('Lili',21,'女',''),('abc',20,'男','2007-01-07 01:11:36.590') +insert into courseInfo values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) +insert into scoreInfo values(1,1,80),(1,2,85),(1,4,50),(2,1,75),(2,3,45),(2,4,75),(3,1,45),(4,1,95),(4,2,75),(4,3,90), +(4,4,45) +select stuName,count(a.courseID) 选课数量,avg(score) 所选课考试的平均分 from stuInfo t inner join courseInfo x on t.stuID=x.courseID left join scoreInfo a on t.stuID=a.stuID group by stuName + +select courseName,count(StuName) 学生的个数,sum(score) 考试的总分 from scoreInfo t inner join stuInfo x on t.StuID=x.StuID inner join courseInfo a on t.courseID=a.courseID group by courseName + +select t.* from stuInfo t,stuInfo x where t.stuSex=x.stuSex and t.stuAge=x.stuAge and t.stuID!=x.stuID + +select distinct t.* from courseInfo t,courseInfo x where t.courseMarks=x.courseMarks and t.courseID!=x.courseID + +select t.stuID 学号,stuName 姓名,courseID 课程号,score from scoreInfo t left join stuInfo x on t.stuID=x.stuID group by t.stuID,stuName,courseID,score + +select t.stuID 学号, t.courseID 课程号,x.courseName 课程名,courseMarks 学分,t.score 分数 from scoreInfo t left join courseInfo x on t.courseID=x.courseID + +select t.stuID 学号,t.stuName 姓名 from stuInfo t left join scoreInfo x on t.stuID=x.stuID where score is null + +select * from stuInfo where datepart(weekday,time)=6 or datepart(weekday,time)=7 + +select * from stuInfo where stuName like '%a%' + +select stuID,count(courseID)课程的数量,avg(score)平均分 from scoreInfo group by stuID having avg(score)>=70 and count(courseID)>=2 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6a9fa2e3f2fe93a6b7fc6d9e6a68758f6bd10e3f --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" @@ -0,0 +1,66 @@ +create database txa +on +( + name ='txa', + size=10, + filename='D:\txa.mdf', + maxsize=100, + filegrowth=10 +) +log on +( + name ='txa_log', + size=10, + filename='D:\txa_log.ldf', + maxsize=100, + filegrowth=10 +) +go +use txa +go +create table tbl_card +( + id nvarchar(10) primary key, + passWord nvarchar(18), + balance int, + userName nvarchar(20) +) +create table tbl_computer +( + id nvarchar(10) primary key, + onUse int, + note nvarchar(200) +) +create table tbl_record +( + id nvarchar(10) primary key, + cardid nvarchar(10) foreign key references tbl_card(id), + Computerid nvarchar(10) foreign key references tbl_computer(id), + beginTime datetime, + endTime datetime, + fee int +) +insert into tbl_card values('0023_ABC','555',98,'张军'),('0025_bbd','abc',300,'朱俊'),('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'),('0078_RJV','55885lg',600,'校庆'),('0089_EDE','zhang',134,'张峻') +insert into tbl_computer values('02',0,'25555'),('03',1,'55555'),('04',0,'66666'),('05',1,'88888'),('06',0,'688878'),('B01',0,'558558') +insert into tbl_record values(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20),(34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23),(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50),(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00',6),(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6),(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50),(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',30),(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23),(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) +select cardid,userName,Computerid,beginTime,endTime,fee from tbl_card t inner join tbl_record x on t.id=cardid where userName='张军' order by fee desc + +select x.id,count(ComputerId) 上网次数,sum(fee) 消费的总金额 from tbl_record t inner join tbl_computer x on t.ComputerId=x.id group by x.id + +select cardId 上网卡,sum(fee) 消费总金额 from tbl_record group by cardId + +select id,userName from tbl_card where id not in(select distinct cardId from tbl_record) + +select * from tbl_card where password=userName + +select top 1 x.id,count(ComputerId) from tbl_record t inner join tbl_computer x on t.Computerid=x.id group by x.id order by count(ComputerId) DESC + +select userName,x.id,fee from tbl_record t inner join tbl_computer x on ComputerId=x.id inner join tbl_card a on a.id=t.cardId where cardId like '%ABC' + +select cardId,userName,x.id,beginTime,endTime,fee from tbl_record t inner join tbl_computer x on t.ComputerId=x.id inner join tbl_card a on a.id=t.cardId where datepart(weekday,beginTime)=6 or datepart(weekday,beginTime)=7 + +select cardId,userName,x.id,beginTime,endTime,fee from tbl_record t inner join tbl_computer x on ComputerId=x.id inner join tbl_card a on a.id=t.cardId where datediff(hour,beginTime,endTime)>12 + +select cardId,userName,x.id,beginTime,endTime,fee from tbl_record t inner join tbl_computer x on ComputerId=x.id inner join tbl_card a on a.id=t.cardId where fee not in(select top 3 fee from tbl_record order by fee DESC) diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f48c38e00aaf6ad702088be4da7375680ceeeaf0 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" @@ -0,0 +1,102 @@ +use master +go +create database ttbs +on +( +name='dada', +size=5, +filename='D:\dada.mdf', +maxsize=50, +filegrowth=1 +) +log on +( +name='dada_log', +size=5, +filename='D:\dada_log.ldf', +maxsize=50, +filegrowth=1 +) +go +use ttbs +go +create table student +( +stuID int primary key identity(1,1), +stuName varchar(20) not null, +stuAge int not null, +stuSex int , +time nvarchar(30) +) +go +create table course +( +courseID int primary key identity(1,1), +courseName varchar(20) not null, +courseMaks int not null +) +go +create table scoreop +( +scoreID int primary key identity(1,1), +sluID int not null , +courseID int not null, +score int not null +) +go +select * from student +select * from course +select * from scoreop +insert into student (stuName,stuAge,stuSex,time) + values('Tom',19,1,NULL), + ('Jack',20,0,NULL), + ('Rose',21,1,NULL), + ('Lulu',19,1,NULL), + ('li',21,0,NULL), + ('abc',20,1,'2007-01-07 01:11:36.590') + +insert into course (courseName,courseMaks) + values('JavaBase',4), + ('HTML',2), + ('javaScipt',2), + ('SqlBase',2) +insert into scoreop (sluID,courseID,score) + values (1,1,80), + (1,2,85), + (1,4,50), + (2,1,75), + (2,3,45), + (2,4,75), + (3,1,45), + (4,1,95), + (4,2,75), + (4,3,90), + (4,4,45) +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + +--题目: +select * from student +select * from course +select * from scoreop +--1.查询出每个学生所选修的课程 的数量 和所选修的课程 的考试的平均分 +select sluID,count(courseID),avg(score) from scoreop group by sluID +--2.查询出 每门课程 的选修的学生的个数 和 学生考试的总分 +select courseID,count(sluID),sum(score) from scoreop group by courseID +--3.查询出性别一样并且年龄一样的学生的信息 +select * from student A,student B where A.StuAge=B.StuAge and A.StuSex=B.StuSex AND A.stuID != B.stuID +--4.查询出学分一样的课程信息 +SELECT A.* FROM course A, course B WHERE A.courseMaks=B.courseMaks AND A.courseName!=B.courseName +UNION +SELECT A.* FROM course A, course B WHERE A.courseMaks=B.courseMaks AND A.courseName!=B.courseName +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +SELECT B.sluID,A.stuName,B.courseID,B.score FROM student A INNER JOIN scoreop B ON A.stuID=B.sluID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +SELECT B.stuName,C.courseID,C.score,A.courseMaks,A.courseName FROM student B INNER JOIN scoreop C ON B.stuID= C.sluID INNER JOIN course A ON C.courseID=A.courseID +--7.查询出没有参加考试的学生的学号和姓名 +SELECT stuID,stuName FROM student WHERE stuID NOT IN (SELECT sluID FROM scoreop GROUP BY sluID,courseID) +--8.查询出是周六周天来报到的学生 +SELECT A.time,stuName FROM student A GROUP BY time,stuName +--9.查询出姓名中有字母a的学生的信息 +select * FROM student WHERE stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select COUNT(*),sluID,AVG(score) from scoreop GROUP BY sluID HAVING COUNT(*)>=2 AND AVG(score)>=70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery11.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery11.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2cf8c28eed6b4d2f50789ca2c930c6384d559610 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery11.sql" @@ -0,0 +1,102 @@ +use master +go +create database ttbs +on +( +name='dada', +size=5, +filename='D:\dada.mdf', +maxsize=50, +filegrowth=1 +) +log on +( +name='dada_log', +size=5, +filename='D:\dada_log.ldf', +maxsize=50, +filegrowth=1 +) +go +use ttbs +go +create table student +( +stuID int primary key identity(1,1), +stuName varchar(20) not null, +stuAge int not null, +stuSex int , +time nvarchar(30) +) +go +create table course +( +courseID int primary key identity(1,1), +courseName varchar(20) not null, +courseMaks int not null +) +go +create table scoreop +( +scoreID int primary key identity(1,1), +sluID int not null , +courseID int not null, +score int not null +) +go +select * from student +select * from course +select * from scoreop +insert into student (stuName,stuAge,stuSex,time) + values('Tom',19,1,NULL), + ('Jack',20,0,NULL), + ('Rose',21,1,NULL), + ('Lulu',19,1,NULL), + ('li',21,0,NULL), + ('abc',20,1,'2007-01-07 01:11:36.590') + +insert into course (courseName,courseMaks) + values('JavaBase',4), + ('HTML',2), + ('javaScipt',2), + ('SqlBase',2) +insert into scoreop (sluID,courseID,score) + values (1,1,80), + (1,2,85), + (1,4,50), + (2,1,75), + (2,3,45), + (2,4,75), + (3,1,45), + (4,1,95), + (4,2,75), + (4,3,90), + (4,4,45) +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + +--题目: +select * from student +select * from course +select * from scoreop +--1.查询出每个学生所选修的课程 的数量 和所选修的课程 的考试的平均分 +select sluID,count(courseID),avg(score) from scoreop group by sluID +--2.查询出 每门课程 的选修的学生的个数 和 学生考试的总分 +select courseID,count(sluID),sum(score) from scoreop group by courseID +--3.查询出性别一样并且年龄一样的学生的信息 +select * from student A,student B where A.StuAge=B.StuAge and A.StuSex=B.StuSex AND A.stuID != B.stuID +--4.查询出学分一样的课程信息 +SELECT A.* FROM course A, course B WHERE A.courseMaks=B.courseMaks AND A.courseName!=B.courseName +UNION +SELECT A.* FROM course A, course B WHERE A.courseMaks=B.courseMaks AND A.courseName!=B.courseName +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +SELECT B.sluID,A.stuName,B.courseID,B.score FROM student A INNER JOIN scoreop B ON A.stuID=B.sluID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +SELECT B.stuName,C.courseID,C.score,A.courseMaks,A.courseName FROM student B INNER JOIN scoreop C ON B.stuID= C.sluID INNER JOIN course A ON C.courseID=A.courseID +--7.查询出没有参加考试的学生的学号和姓名 +SELECT stuID,stuName FROM student WHERE stuID NOT IN (SELECT sluID FROM scoreop GROUP BY sluID,courseID) +--8.查询出是周六周天来报到的学生 +SELECT A.time,stuName FROM student A GROUP BY time,stuName +--9.查询出姓名中有字母a的学生的信息 +select * FROM student WHERE stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select COUNT(*),sluID,AVG(score) from scoreop GROUP BY sluID HAVING COUNT(*)>=2 AND AVG(score)>=70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery12.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery12.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0c1e7ec0259cef6ad63bb46fcebe3f577ede0a15 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery12.sql" @@ -0,0 +1,101 @@ +use master +go +create database TBSE +on +( name='TA', + filename='D:\TA.mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='TA_log', + filename='D:\TA_log.ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use TA +go +create table tbl_card +( + id varchar(8) primary key, + passWord varchar(15) not null, + balance money not null, + userName nchar(5) +) +create table tbl_computer +( + id char(5) primary key, + onUse int not null, + note text not null +) +create table tbl_record +( + id int primary key, + cardld varchar(8) references tbl_card(id), + ComputerId char(5) references tbl_computer(id), + beginTime datetime , + endTime datetime, + fee money not null +) +insert into tbl_card values('0023_ABC','555',99,'张军'), + ('0025_bbd','abc',300,'朱俊'), + ('0036_CCD','何柳',100,'何柳'), + ('0045_YGR','0045_YGR',58,'验证'), + ('0078_RJV','55885fg',600,'校庆'), + ('0089_EDE','zhang',134,'张俊') + +insert into tbl_computer values('02',0,'25555'), + ('03',1,'55555'), + ('04',0,'66666'), + ('05',1,'888888'), + ('06',0,'688878'), + ('B01',0,'558558') + + +insert into tbl_record values('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), + ('34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), + ('45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), + ('46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6'), + ('47','0023_ABC','03','2006-12-23 15:26:00','2007-12-23 22:55:00','50'), + ('48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6'), + ('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), + ('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30'), + ('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), + ('98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') +--请完成以下题目: +select*from tbl_card +select*from tbl_computer +select*from tbl_record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select cardld 卡号, userName 用户名, beginTime 开始时间, endTime 结束时间, fee 消费金额 from tbl_record join tbl_computer on tbl_computer.id=tbl_record.ComputerId + join tbl_card on cardld=tbl_card.id where userName='张军' order by fee +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId, count(ComputerId) 上网次数,sum(fee) 消费总金额 from tbl_record group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardld 网卡, sum(fee) 消费总金额 from tbl_record group by cardld +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id 卡号 , userName 用户名 from tbl_card where id not in (select cardld from tbl_record ) + +--5. 将密码与用户名一样的上网卡信息查询出来 +select T.id 卡号,T.passWord 密码,T.balance 卡上的余额,T.userName 用户名 from tbl_card,tbl_card T where tbl_card.passWord=T.userName + +select T.* from tbl_card,tbl_card T where tbl_card.passWord=T.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号, count(ComputerId) 使用次数 from tbl_record group by ComputerId order by count(ComputerId) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select cardld 卡号, userName 用户名, ComputerId 机器号,fee 消费金额 from tbl_record join tbl_card on tbl_card.id=cardld where cardld like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardld 卡号, userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record join tbl_card on tbl_card.id=cardld + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardld 卡号, userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record join tbl_card on tbl_card.id=cardld where datediff(HOUR,beginTime,endTime)>=12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 cardld 卡号, userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record join tbl_card on tbl_card.id=cardld order by fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a6932547ab87ef3045a66b6a1200f9f6bec99d03 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" @@ -0,0 +1,104 @@ +use master +go +create database TBSE +on +( name='TA', + filename='D:\TA.mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='TA_log', + filename='D:\TA_log.ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use TBSE +go +create table tbl_card +( + id varchar(8) primary key, + passWord varchar(15) not null, + balance money not null, + userName nchar(5) +) +go +create table tbl_computer +( + id char(5) primary key, + onUse int not null, + note text not null +) +go +create table tbl_record +( + id int primary key, + cardld varchar(8) references tbl_card(id), + ComputerId char(5) references tbl_computer(id), + beginTime datetime , + endTime datetime, + fee money not null +) +go +insert into tbl_card values('0023_ABC','555',99,'张军'), + ('0025_bbd','abc',300,'朱俊'), + ('0036_CCD','何柳',100,'何柳'), + ('0045_YGR','0045_YGR',58,'验证'), + ('0078_RJV','55885fg',600,'校庆'), + ('0089_EDE','zhang',134,'张俊') + +insert into tbl_computer values('02',0,'25555'), + ('03',1,'55555'), + ('04',0,'66666'), + ('05',1,'888888'), + ('06',0,'688878'), + ('B01',0,'558558') + + +insert into tbl_record values('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), + ('34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), + ('45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), + ('46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6'), + ('47','0023_ABC','03','2006-12-23 15:26:00','2007-12-23 22:55:00','50'), + ('48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6'), + ('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), + ('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30'), + ('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), + ('98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') +--请完成以下题目: +select*from tbl_card +select*from tbl_computer +select*from tbl_record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select cardld 卡号, userName 用户名, beginTime 开始时间, endTime 结束时间, fee 消费金额 from tbl_record join tbl_computer on tbl_computer.id=tbl_record.ComputerId + join tbl_card on cardld=tbl_card.id where userName='张军' order by fee +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId, count(ComputerId) 上网次数,sum(fee) 消费总金额 from tbl_record group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardld 网卡, sum(fee) 消费总金额 from tbl_record group by cardld +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id 卡号 , userName 用户名 from tbl_card where id not in (select cardld from tbl_record ) + +--5. 将密码与用户名一样的上网卡信息查询出来 +select T.id 卡号,T.passWord 密码,T.balance 卡上的余额,T.userName 用户名 from tbl_card,tbl_card T where tbl_card.passWord=T.userName + +select T.* from tbl_card,tbl_card T where tbl_card.passWord=T.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号, count(ComputerId) 使用次数 from tbl_record group by ComputerId order by count(ComputerId) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select cardld 卡号, userName 用户名, ComputerId 机器号,fee 消费金额 from tbl_record join tbl_card on tbl_card.id=cardld where cardld like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardld 卡号, userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record join tbl_card on tbl_card.id=cardld + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardld 卡号, userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record join tbl_card on tbl_card.id=cardld where datediff(HOUR,beginTime,endTime)>=12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 cardld 卡号, userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record join tbl_card on tbl_card.id=cardld order by fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d8fa2a355e82255b388aa5707112f3a7f610629c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" @@ -0,0 +1,90 @@ +use master +go +create database Demo_03 +on( + name='Demo_03', + filename='C:\TEXT\Demo_03.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on( + name='Demo_03_log', + filename='C:\TEXT\Demo_03_log.ldf', + size=5, + maxsize=100, + filegrowth=10% +) +go +use Demo_03 +go +create table tbl_card +( + ID varchar(20) primary key , + passWord nvarchar(10), + banlance int, + userName nvarchar(10), +) +create table tbl_computer +( + Id varchar(5) primary key , + onUse int , + note text, +) +create table tbl_record +( + ID int primary key , + cardId varchar(20) references tbl_card(ID), + ComputerId varchar(5) references tbl_computer(Id), + beginTime datetime, + endTime datetime, + fee int, +) + +select * from tbl_computer +select * from tbl_card +select * from tbl_record +insert into tbl_card values('0023_ABC','555',98,'张军'),('0025_bbd','abe',300,'朱俊'),('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张峻') +insert into tbl_computer values('02',0,'25555'),('03',1,'55555'),('04',0,'66666'),('05',1,'88888'), +('06',0,'688878'),('B01',0,'558558') +insert into tbl_record values(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20), +(34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23), +(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00',6), +(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select tbl_card.ID 卡号,tbl_card.userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID where tbl_card.userName='张军' order by fee DESC +--2. 查询出每台机器上的上网次数和消费的总金额 +select tbl_computer.Id,count(*)上网的次数,sum(fee)总金额 from tbl_record +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id group by tbl_computer.Id +--3. 查询出所有已经使用过的上网卡的消费总金额 +select tbl_record.cardId,sum(fee)总金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID group by tbl_record.cardId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select ID 卡号,userName 用户名 from tbl_card where ID not in(select distinct cardId from tbl_record) +--5. 将密码与用户名一样的上网卡信息查询出来 +select A.* from tbl_card A ,tbl_card B where A.userName=B.passWord +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 tbl_record.ComputerId,count(*) 使用次数 from tbl_record +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id group by tbl_record.ComputerId order by count(*) DESC +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_card.ID 卡号,tbl_card.userName 用户名,tbl_computer.Id 上网的机器号,fee 消费金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id where tbl_card.ID like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.ID 上网的卡号,tbl_card.userName 用户名,tbl_computer.Id 机器号,beginTime 开始时间,endTime 结束时间,DATEDIFF(hh,beginTime,endTime) 相差的时间,fee 消费金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id where DATEDIFF(hh,beginTime,endTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 30 percent tbl_card.ID 上网的卡号,tbl_card.userName 用户名,tbl_computer.Id 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id order by fee DESC diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\344\275\234\344\270\2321.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e3a339ef9de3d6c9cd49a76f01ab8d8ce9724e25 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\344\275\234\344\270\2321.sql" @@ -0,0 +1,84 @@ +use master +go +create database Demo_02 +on( + name='Demo_02', + filename='C:\text\Demo_02.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on( + name='Demo_02_log', + filename='C:\text\Demo_02_log.;df', + size=5, + maxsize=100, + filegrowth=10% +) +go +use Demo_02 +go +create table stuInfo +( + stuID int primary key identity(1,1) not null, + stuName nvarchar(10) not null, + stuAge int not null , + stuSex int not null , + time datetime, +) +create table courseInfo +( + courseID int primary key identity(1,1) not null, + coueseName nvarchar(20) not null, + courseMarks int not null, +) +create table scoreInfo +( + scoreID int primary key identity(1,1) not null, + stuID int references stuInfo(stuID) not null, + courseID int references courseInfo(courseID) not null, + score int not null, +) +select * from stuInfo +select * from courseInfo +select * from scoreInfo +insert into stuInfo(stuName,stuAge,stuSex) values('Tom',19,1),('Jack',20,0),('Rose',21,1), +('Lulu',19,1),('lii',21,0) +insert into stuInfo values('abc',20,1,'2007-01-07 01:11:36.590') +insert into courseInfo values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) +insert into scoreInfo values(1,1,80),(1,2,85),(1,4,50),(2,1,75),(2,3,45), +(2,4,75),(3,1,45),(4,1,95),(4,2,75),(4,3,90),(4,4,45) + + +--1.查询出 每个学生 所选修的课程的数量 和 所选修的课程的考试的平均分 +select stuInfo.stuID,stuInfo.stuName 姓名,count(*) 所选修的课程,avg(score)考试平均分 from scoreInfo +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +inner join stuInfo on stuInfo.stuID=scoreInfo.stuID group by stuInfo.stuName , stuInfo.stuID +--2.查询出 每门课程 的选修的学生的个数 和 学生考试的总分 +select scoreInfo.courseID,count(*) 课程选修的人数,sum(score)总分 from scoreInfo +left join stuInfo on stuInfo.stuID=scoreInfo.scoreID +inner join courseInfo on courseInfo.courseID=scoreInfo.courseID group by scoreInfo.courseID +--3.查询出 性别一样 并且 年龄一样 的学生的信息 +select A.* from stuInfo A,stuInfo B where A.stuSex=B.stuSex and A.stuAge=B.stuAge and A.stuID != B.stuID +--4.查询出学分一样的课程信息 +select distinct B.* from courseInfo A, courseInfo B where A.courseMarks= B.courseMarks and A.coueseName != B.coueseName +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID 学号,stuInfo.stuName 姓名,courseInfo.courseID 课程号,score 分数 from scoreInfo +inner join stuInfo on stuInfo.stuID=scoreInfo.stuID +inner join courseInfo on scoreInfo.courseID= courseInfo.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID 学号,stuInfo.stuName 姓名,courseInfo.courseID 课程号,courseInfo.coueseName 课程名称,courseInfo.courseMarks 课程学分,score 分数 from scoreInfo +inner join stuInfo on stuInfo.stuID=scoreInfo.stuID +inner join courseInfo on scoreInfo.courseID= courseInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID 学号,stuInfo.stuName 姓名 from scoreInfo +right join stuInfo on scoreInfo.stuID=stuInfo.stuID +except +select stuInfo.stuID 学号,stuInfo.stuName 姓名 from scoreInfo +right join stuInfo on scoreInfo.stuID=stuInfo.stuID where score is not null +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出 选修了2门课程以上 的并且 考试平均分在70以上的学生的学号 和 考试平均分 以及 选修课程的数量 +select stuInfo.stuID,stuInfo.stuName 姓名,count(*) 所选修的课程,avg(score)考试平均分 from scoreInfo +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +inner join stuInfo on stuInfo.stuID=scoreInfo.stuID group by stuInfo.stuName , stuInfo.stuID having count(*)>2 and avg(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery14.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery14.sql" new file mode 100644 index 0000000000000000000000000000000000000000..13012d9ca32788842904cb8e4fdfb6f05ce7270e --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery14.sql" @@ -0,0 +1,221 @@ +create database Student +on +( +name='Student', +filename='D:\sql.mdl', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) +log on +( +name='Student_log', +filename='D:\sql.ldf', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) +go +use Student +go + +create table stuInfo +( +stuID int primary key identity(1,1) not null, +stuName varchar(20) not null, +stuAge int not null, +stuSex char(2) not null, +time datetime +) + +create table courseInfo +( +courseID int primary key identity(1,1) not null, +courseName varchar(20) not null, +courseMarks int not null +) + +create table scoreInfo +( +scoreID int primary key identity(1,1) not null, +stuID int not null, +courseID int not null, +score int not null, +) + +insert into stuInfo(stuName,stuAge,stuSex) values ('Tom','19','1'),('Jack','20','0'), +('Rose','21','1'),('Lulu','19','1'),('Lili','21','0') +insert into stuInfo values('abc','20','1','2007-01-07 01:11:36.590') + +insert into courseInfo values ('JavaBase','4'),('HTML','2'),('JavaScript','2'),('SqlBase','2') + +insert into scoreInfo values ('1','1','80'),('1','2','85'),('1','4','50'),('2','1','75'),('2','3','45'),('2','4','75'), +('3','1','45'),('4','1','95'),('4','2','75'),('4','3','90'),('4','4','45') + + +--1.鏌ヨ鍑烘瘡涓鐢熸墍閫変慨鐨勮绋嬬殑鏁伴噺鍜屾墍閫変慨鐨勮绋嬬殑鑰冭瘯鐨勫钩鍧囧垎 + +select stuID 瀛︾敓,count(courseID) 鏁伴噺,AVG(score) 骞冲潎鍒 from scoreInfo group by stuID + +--2.鏌ヨ鍑烘瘡闂ㄨ绋嬬殑閫変慨鐨勫鐢熺殑涓暟鍜屽鐢熻冭瘯鐨勬诲垎 + +select courseName 璇剧▼,count(stuID) 瀛︾敓涓暟,sum(score) 鎬诲垎 from scoreInfo inner join courseInfo +on scoreInfo.courseID=courseInfo.courseID group by courseInfo.courseID,courseName + +--3.鏌ヨ鍑烘у埆涓鏍峰苟涓斿勾榫勪竴鏍风殑瀛︾敓鐨勪俊鎭 + +select * from stuInfo where stuAge in (select stuAge from stuInfo +group by stuAge,stuSex having count(stuAge)>1)--娉1 + +select A.* from stuInfo A,stuInfo B where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuID!=b.stuID--娉2 + +--4.鏌ヨ鍑哄鍒嗕竴鏍风殑璇剧▼淇℃伅 + +select * from courseInfo where courseMarks in (select courseMarks from courseInfo +group by courseMarks having count(courseMarks)>1) + +--5.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛屽鍚嶏紝璇剧▼鍙峰拰鍒嗘暟 + +select scoreInfo.stuID 瀛﹀彿,stuName 濮撳悕,courseID 璇剧▼,score 鍒嗘暟 +from scoreInfo inner join stuInfo on scoreInfo.stuID=stuInfo.stuID + +--6.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛岃绋嬪彿锛岃绋嬪悕锛岃绋嬪鍒嗗拰鍒嗘暟 + +select stuID 瀛﹀彿,courseInfo.courseID 璇剧▼鍙,courseName 璇剧▼鍚,courseMarks 瀛﹀垎,score 鍒嗘暟 +from courseInfo inner join scoreInfo on courseInfo.courseID=scoreInfo.courseID + +--7.鏌ヨ鍑烘病鏈夊弬鍔犺冭瘯鐨勫鐢熺殑瀛﹀彿鍜屽鍚 +select stuID,stuName from stuInfo +EXCEPT +select stuInfo.stuID,stuName from scoreInfo +inner join stuInfo on scoreInfo.stuID=stuInfo.stuID + +--8.鏌ヨ鍑烘槸鍛ㄥ叚鍛ㄥぉ鏉ユ姤鍒扮殑瀛︾敓 +select stuname,time from stuInfo where DATEPART(dw,time)=1 or DATEPART(dw,time)=7 + +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +--9.鏌ヨ鍑哄鍚嶄腑鏈夊瓧姣峚鐨勫鐢熺殑淇℃伅 + +select * from stuInfo where stuName like '%a%' + +--10.鏌ヨ鍑洪変慨浜2闂ㄨ绋嬩互涓婄殑骞朵笖鑰冭瘯骞冲潎鍒嗗湪70浠ヤ笂鐨勫鐢熺殑瀛﹀彿鍜岃冭瘯骞冲潎鍒嗕互鍙婇変慨璇剧▼鐨勬暟閲 + +select stuID,avg(score) 骞冲潎鍒,count(courseID) 閫変慨璇剧▼鏁伴噺 from scoreInfo group by stuID +having avg(score) >70 and count(courseID)>2 + + + +--棣栧厛鍒涘缓缃戝惂璁¤垂绯荤粺鐨勬暟鎹簱锛岃〃缁撴瀯鍜屾暟鎹2.bmp鎵绀猴紝琛ㄧ殑璇存槑濡備笅锛 +create database wbfee +on +( + name='wbfee', + filename='D:\wbfee.mdf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +log on +( + name='wbfee_log', + filename='D:\wbfee_log.ldf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +go + +use wbfee +go +create table tbl_card +( + id nvarchar(10) primary key, + passWord nvarchar(15), + balance money , + userName nvarchar(10) +) + +create table tbl_computer +( + id nvarchar(10) primary key, + onUse int check(onUse in (1,0)), + note text +) + +create table tbl_record +( + id nvarchar(10) primary key, + cardId nvarchar(10) foreign key references tbl_card(id), + ComputerId nvarchar(10) foreign key references tbl_computer(id), + beginTime datetime, + endTime datetime, + fee money +) + + +insert into tbl_card values +('0023_ABC','555',98,'寮犲啗'), +('0025_bbd','abe',300,'鏈变繆'), +('0036_CCD','',100,'浣曟煶'), +('0045_YGR','0045_YGR',58,'楠岃瘉'), +('0078_RJV','55885tg',600,'鏍″簡'), +('0089_EDE','zhang',134,'寮犲郴') + + + +insert into tbl_computer values +('02',0,'25555'), +('03',1,'55555'), +('04',0,'66666'), +('05',1,'88888'), +('06',0,'688878'), +('B01',0,'558558') + +insert into tbl_record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), +(34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), +(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6'), +(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6'), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','300'), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') + + +--tbl_record,tbl_computer ,tbl_card +--1. 鏌ヨ鍑虹敤鎴峰悕涓'寮犲啗'鐨勪笂缃戝崱鐨勪笂缃戣褰曪紝瑕佹眰鏄剧ず鍗″彿锛岀敤鎴峰悕锛屾満鍣ㄧ紪鍙枫佸紑濮嬫椂闂淬佺粨鏉熸椂闂达紝鍜屾秷璐归噾棰濓紝骞舵寜娑堣垂閲戦闄嶅簭鎺掑垪 +select C.id 鏄剧ず鍗″彿,userName 鐢ㄦ埛鍚,P.id 鏈哄櫒缂栧彿,beginTime 寮濮嬫椂闂,endTime 缁撴潫鏃堕棿,fee 娑堣垂閲戦 from tbl_record R +inner join tbl_card C on C.id=cardId +inner join tbl_computer P on P.id=R.ComputerId where userName='寮犲啗' order by fee DESC +--2. 鏌ヨ鍑烘瘡鍙版満鍣ㄤ笂鐨勪笂缃戞鏁板拰娑堣垂鐨勬婚噾棰 +select C.id 鏈哄櫒,count(ComputerId) 涓婄綉娆℃暟,sum(fee) 娑堣垂鐨勬婚噾棰 from tbl_record R +inner join tbl_computer C on C.id=R.ComputerId group by C.id + +--3. 鏌ヨ鍑烘墍鏈夊凡缁忎娇鐢ㄨ繃鐨勪笂缃戝崱鐨勬秷璐规婚噾棰 +select cardId 涓婄綉鍗,sum(fee) 娑堣垂鎬婚噾棰 from tbl_record group by cardId + + +--4. 鏌ヨ鍑轰粠鏈秷璐硅繃鐨勪笂缃戝崱鐨勫崱鍙峰拰鐢ㄦ埛鍚 +select id,userName from tbl_card where id not in(select distinct cardId from tbl_record) + + +--5. 灏嗗瘑鐮佷笌鐢ㄦ埛鍚嶄竴鏍风殑涓婄綉鍗′俊鎭煡璇㈠嚭鏉 +select * from tbl_card where id=password +--6. 鏌ヨ鍑轰娇鐢ㄦ鏁版渶澶氱殑鏈哄櫒鍙峰拰浣跨敤娆℃暟 +select top 1 C.id,count(ComputerId) from tbl_record R inner join tbl_computer C on C.id=ComputerId group by C.id order by count(ComputerId) DESC +--7. 鏌ヨ鍑哄崱鍙锋槸浠'ABC'缁撳熬鐨勫崱鍙凤紝鐢ㄦ埛鍚嶏紝涓婄綉鐨勬満鍣ㄥ彿鍜屾秷璐归噾棰 +select userName,P.id,fee from tbl_record R inner join tbl_computer P on ComputerId=P.id inner join tbl_card C on C.id=cardId where cardId like '%ABC' + +--8. 鏌ヨ鍑烘槸鍛ㄥ叚銆佸懆澶╀笂缃戠殑璁板綍锛岃姹傛樉绀轰笂缃戠殑鍗″彿锛岀敤鎴峰悕锛屾満鍣ㄥ彿锛屽紑濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 +select cardId,userName,P.id,beginTime,endTime,fee from tbl_record inner join tbl_computer P on ComputerId=P.id inner join tbl_card C on C.id=cardId where datepart(weekday,beginTime)=1 or datepart(weekday,beginTime)=7 + +--9. 鏌ヨ鎴愪竴娆′笂缃戞椂闂磋秴杩12灏忔椂鐨勭殑涓婄綉璁板綍锛岃姹傛樉绀轰笂缃戠殑鍗″彿锛岀敤鎴峰悕锛屾満鍣ㄥ彿锛屽紑濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 +select cardId,userName,P.id,beginTime,endTime,fee from tbl_record inner join tbl_computer P on ComputerId=P.id inner join tbl_card C on C.id=cardId where datediff(hour,beginTime,endTime)>12 + +--10. 鏌ヨ闄ゆ秷璐归噾棰濇帓鍒楀墠涓夊悕(鏈楂)鐨勪笂缃戣褰曪紝瑕佹眰鏄剧ず涓婄綉鐨勫崱鍙凤紝鐢ㄦ埛鍚嶏紝鏈哄櫒鍙凤紝寮濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 +select cardId,userName,P.id,beginTime,endTime,fee from tbl_record inner join tbl_computer P on ComputerId=P.id inner join tbl_card C on C.id=cardId where fee not in(select top 3 fee from tbl_record order by fee DESC) diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a1ebcd3a0243f8ce927ffda11a78290caf54a51a --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/1.sql" @@ -0,0 +1,105 @@ +create database scs +on +( + name='scs', + filename='D:\scs.mdf' +) + +log on +( + name='scs_log', + filename='D:\scs_log.ldf' +) + +use scs +go + +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +create table stuInfo( + stuID int primary key identity(1,1), + stuName varchar(20) not null, + stuAge int not null, + stuSex int check(stuSex=1 or stuSex=0 ) not null, + time datetime +) + +insert into stuInfo values +('Tom',19,1,null), +('Jack',20,0,null), +('Rose',21,1,null), +('Lulu',19,1,null), +('Lili',21,0,null), +('abc',20,1,'2007-01-07 01:11:36.590') + +select * from stuInfo + +create table courseInfo( + courseID int primary key identity(1,1), + courseName varchar(20) not null, + courseMarks int not null +) + +insert into courseInfo values +('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) + +select * from courseInfo + +create table scoreInfo( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID) not null, + courseID int references courseInfo(courseID) not null, + score int not null +) + +insert into scoreInfo values +(1,1,80), +(1,2,85), +(1,4,50), +(2,1,75), +(2,3,45), +(2,4,75), +(3,1,45), +(4,1,95), +(4,2,75), +(4,3,90), +(4,4,45) + +select * from stuInfo +select * from courseInfo +select * from scoreInfo + + +--1.查询出 每个学生 和所选修的课程的考试的平均分所选修的课程的数量 +select stuName,count(distinct courseID),AVG(score) from scoreInfo,stuInfo +where scoreInfo.stuID=stuInfo.stuID group by stuName + +select stuName,count(distinct courseID),AVG(score) from scoreInfo +right join stuInfo on scoreInfo.stuID=stuInfo.stuID +group by stuName + +select stuID 姓名,count(courseID) 课程数量,AVG(score) 平均分 from scoreInfo group by stuID +--2.查询出 每门课程 的 选修的学生的个数和学生考试的总分 +select courseName,count(distinct stuID),sum(score) from scoreInfo +right join courseInfo on scoreInfo.courseID=courseInfo.courseID +group by courseName + +select courseID 课程,count(stuID) 学生个数,sum(score) 总分 from scoreInfo group by courseID +--3.查询出性别一样并且年龄一样的学生的信息 +select A.stuSex,A.stuAge, B.stuName,B.time from stuInfo A inner join stuInfo B on A.stuID=B.stuID WHERE A.stuSex=B.stuSex AND A.stuAge=B.stuAge +--4.查询出学分一样的课程信息 +select DISTINCT A.* from courseInfo A inner join courseInfo B on A.courseMarks=B.courseMarks WHERE A.courseName!=B.courseName +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select S.stuID,T.stuName,S.courseID,S.score from scoreInfo S inner join stuInfo T on S.stuID=T.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select S.stuID,C.courseID,C.courseName,C.courseMarks,S.score from scoreInfo S inner join courseInfo C on S.courseID=C.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select * from stuInfo A left join scoreInfo S on A.stuID=S.stuID where s.courseID is null +--8.查询出是周六周天来报到的学生 +select * from stuInfo where DATEPART(weekday,time)=1 or DATEPART(weekday,time)=7 +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出 选修了2门课程以上 的并且 考试平均分在70以上 的学生的学号 和考试平均分 以及 选修课程的数量 +select A.stuID,AVG(A.score)平均分,COUNT(stuID)选修数量 from scoreInfo A inner join courseInfo B on A.courseID=B.courseID GROUP BY stuID having COUNT(stuID)>2 AND AVG(score)>70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a8f17dd3af199e89674e4f191dbbc083cddc15f1 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/2.sql" @@ -0,0 +1,119 @@ + +create database sos +on +( + name='sos', + filename='D:\sos.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='sos_log', + filename='D:\sos_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use sos +go +create table tbl_card +( + id varchar(25) primary key, + [passWord] varchar(30) not null, + balance money not null, + userName varchar(20) not null +) +go + + +insert into tbl_card values('0023_ABC','555',98,'张军'),('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'),('0045_YGR','0045_YGR',58,'验证'), +('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张峻') + +select * from tbl_card + + +create table tbl_computer +( + id varchar(20) primary key, + onUse char(2) check(onUse=0 or onUse=1), + note text +) +go + +insert into tbl_computer values('02','0','25555'),('03','1','55555'),('04','0','66666'), +('05','1','88888'),('06','0','688878'),('B01','0','558558') + +select * from tbl_computer + + +create table tbl_record +( + id varchar(30) primary key, + cardId varchar(25) constraint FK_tbl_record_cardId references tbl_card(id), + ComputerId varchar(20) constraint FK_tbl_record_ComputerId references tbl_computer(id), + beginTime time, + endTime time, + fee money +) +go + +insert into tbl_record(id,cardId,ComputerId,beginTime,endTime,fee) values('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), +('34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), +('45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +('46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6'), +('47','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +('48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6'), +('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), +('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','3'), +('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), +('98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') + +select * from tbl_card +select * from tbl_computer +select * from tbl_record + +--请完成以下题目: +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select tbl_card.id 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_card +inner join tbl_record on tbl_card.id = tbl_record.cardId +where userName='张军' order by fee desc + +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId 机器,count(*)次数,sum(fee)总金额 from tbl_record group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId 卡号,sum(fee)总金额 from tbl_record group by cardId + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select cardId 卡号,userName 用户名 from tbl_record +right join tbl_card on tbl_card.id = tbl_record.cardId where tbl_card.id not in(select cardId from tbl_record) + +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card A,tbl_card B where A.passWord = B.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 使用最多的机器号,count(*)次数 from tbl_record group by ComputerId order by count(*) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_card.id 卡号,userName 用户名,ComputerId 机器号,fee 金额 from tbl_card +inner join tbl_record on tbl_card.id = tbl_record.cardId where tbl_card.id like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +--I can't do it + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.id 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 金额 from tbl_card +inner join tbl_record on tbl_card.id = tbl_record.cardId + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 金额 from tbl_record +inner join tbl_card on tbl_card.id = tbl_record.cardId where fee not in(select top 3 fee from tbl_record order by fee desc) + +select top 3 fee from tbl_record order by fee desc + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7ef3091825afa181ff7d31fcf76cff0bf287861d --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" @@ -0,0 +1 @@ +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) create database stu go use stu go create table stuInfo ( stuId int primary key identity, stuname nvarchar(20) , stuage int , stusex int check(stusex=1 or stusex=0), time datetime ) create table courseInfo ( courseid int primary key identity, couesename nvarchar(20) , cousrmarks int ) create table scoreInfo ( scoreid int primary key identity, stuid int, courseid int, score int ) insert into stuInfo values ('tom',19,1,null), ('jack',20,0,null), ('rose',21,1,null), ('lulu',19,1,null), ('lili',21,0,null), ('abc',20,1,'2007-01-07 01:11:36.590') insert into courseInfo values ('javabase',4), ('html',2), ('javascript',2), ('sqlbase',2) insert into scoreInfo values (1,1,80), (1,2,85), (1,4,50), (2,1,75), (2,3,45), (2,4,75), (3,1,45), (4,1,95), (4,2,75), (4,3,90), (4,4,45) select * from stuInfo --学生信息表 select * from courseInfo --科目表 select * from scoreInfo --成绩表 select * from stuInfo S inner join scoreInfo O on S.stuId=O.stuid inner join courseInfo C on O.courseid = C.courseid --题目: --1.查询出 每个学生 所选修的 课程的数量 和所 选修的课程的考试的平均分 select stuname 学习, count(C.cousrmarks) 课程, avg(score) 平均分 from stuInfo S inner join scoreInfo O on S.stuId=O.stuid inner join courseInfo C on O.courseid = C.courseid group by stuname --2.查询出每门课程的选修的学生的个数和学生考试的总分 select C.couesename,count(S.stuId) 学生的个数,sum(O.score) 总分 from stuInfo S inner join scoreInfo O on S.stuId=O.stuid inner join courseInfo C on O.courseid = C.courseid group by C.couesename --3.查询出性别一样并且年龄一样的学生的信息 select stuage,stusex ,count(stuage),count(stusex) from stuInfo group by stuage,stusex having count(stuage)>1 and count(stusex) >1 select * from stuInfo where stuage in (select stuage from stuInfo group by stuage,stusex having count(stusex) >1) select * from stuInfo A , stuInfo B where A.stuage = B.stuage and A.stusex = B.stusex and A.stuname!=B.stuname --4.查询出学分一样的课程信息 select * from courseInfo where cousrmarks in (select cousrmarks from courseInfo group by cousrmarks having count(cousrmarks)>1) select * from courseInfo A ,courseInfo B where A.cousrmarks=B.cousrmarks and A.couesename!= B.couesename --5.查询出参加了考试的学生的学号,姓名,课程号和分数 select o.stuid 学号, stuname 姓名,o.courseid 课程号,o.score 分数 from stuInfo S inner join scoreInfo O on S.stuId=O.stuid inner join courseInfo C on O.courseid = C.courseid --6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 select o.stuid 学号, stuname 姓名,o.courseid 课程号,c.couesename 课程名,c.cousrmarks 课程学分,o.score 分数 from stuInfo S inner join scoreInfo O on S.stuId=O.stuid inner join courseInfo C on O.courseid = C.courseid --7.查询出没有参加考试的学生的学号和姓名 select * from stuInfo S left join scoreInfo O on S.stuId=O.stuid left join courseInfo C on O.courseid = C.courseid where c.couesename is null --8.查询出是周六周天来报到的学生 --9.查询出姓名中有字母a的学生的信息 select stuname from stuInfo where stuname like '%a%' --10.查询出选修了 2门课程以上 的并且 考试平均分在70 以上的 学生的学号和考试平均分以及选修课程的数量 select s.stuId, count(c.courseid) 选修课程的数量,avg(score) 考试平均分 from stuInfo S inner join scoreInfo O on S.stuId=O.stuid inner join courseInfo C on O.courseid = C.courseid group by s.stuId having count(c.courseid)>1 and avg(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a24b9b9d72747baba56bc2bf3c59069dec214752 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery2.sql" @@ -0,0 +1,3 @@ +use master go --首先创建网吧计费系统的数据库,表结构和数据如2.bmp所示,表的说明如下: create database internet go use internet go --表一为上网卡信息表(tbl_card) --id: 卡号,主键 --passWord:密码 --balance:卡上的余额 --userName:该上网卡所属的用户名 create table tbl_card ( ID nvarchar(10) primary key not null, PassWord nvarchar(10) not null, Balance nvarchar(10) not null, UserName nvarchar(10) not null ) insert into tbl_card values ('0023_ABC','555','98','张军'), ('0025_bbd','abe','300','朱俊'), ('0036_CDD','何柳','100','何柳'), ('0045_YGR','0045_YGR','58','证验'), ('0078_RJV','55885fg','600','校庆'), ('0089_EDE','zhang','134','张骏') --表2为网吧机器说明表(tbl_computer) --id:机器编号,主键 --onUse:该机器是否正在使用,0为未使用,1为正在使用 --note:该机器的说明 create table tbl_computer ( ID nvarchar(10) not null primary key, OnUse int check(OnUse=1 or OnUse=0) not null, Note int not null ) insert into tbl_computer values ('02',0,25555), ('03',1,25555), ('04',0,66666), ('05',1,88888), ('06',0,688878), ('B01',0,558558) --表3为上网记录表(tbl_record): --id:记录编号,主键 --cardId:本次上网的卡号,外键 --ComputerId:本次上网记录所使用的机器号,外键 --beginTime:本次上网记录的开始时间 --endTime:本次上网记录的结束时间 --fee:本次上网的费用 create table tbl_record ( ID nvarchar(20) primary key not null, CardID nvarchar(10) not null foreign key references tbl_card(ID), ComputerID nvarchar(10) not null foreign key (ComputerID) references tbl_computer(ID), BeignTime datetime not null, EndTime datetime not null, Fee int not null ) insert into tbl_record values (23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20), (34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23), (45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), (46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00',6), (47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',60), (48,'0023_ABC','03','2007-01-06 15:26:00','2006-01-06 22:55:00',6), (55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), (64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), (65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), (98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) --请完成以下题目: select * from tbl_record --上网记录表(tbl_record) select* from tbl_computer --网吧机器说明表(tbl_computer) select * from tbl_card --上网卡信息表(tbl_card) select * from tbl_record R inner join tbl_computer C on r.ComputerID=c.ID inner join tbl_card A on r.CardID=a.ID --1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额, --并按消费金额降序排列 select r.CardID 卡号,a.UserName 用户名,c.ID 机器编号,r.BeignTime 开始时间,r.EndTime 结束时间,r.Fee 消费金额 from tbl_record R inner join tbl_computer C on r.ComputerID=c.ID inner join tbl_card A on r.CardID=a.ID where a.UserName='张军' --2. 查询出每台机器上的上网次数和消费的总金额 select C.ID 机器, count(c.OnUse) 上网次数, sum(r.Fee) 总金额 from tbl_record R inner join tbl_computer C on r.ComputerID=c.ID inner join tbl_card A on r.CardID=a.ID group by C.ID, c.OnUse --3. 查询出所有已经使用过的上网卡的消费总金额 select r.CardID 上网卡,sum(r.Fee) 消费总金额 from tbl_record R inner join tbl_computer C on r.ComputerID=c.ID inner join tbl_card A on r.CardID=a.ID group by r.CardID --4. 查询出从未消费过的上网卡的卡号和用户名 select a.ID 卡号,a.UserName 用户名 from tbl_record R right join tbl_computer C on r.ComputerID=c.ID right join tbl_card A on r.CardID=a.ID group by a.ID,r.Fee,a.UserName having r.Fee is null --5. 将密码与用户名一样的上网卡信息查询出来 select a.* from tbl_card a,tbl_card b where a.PassWord=b.UserName --6. 查询出使用次数最多的机器号和使用次数 select top 1 count(*) 使用次数,ComputerID 机器号 from tbl_record group by ComputerID order by count(*) desc --7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 select r.CardID,a.UserName,c.ID,r.Fee from tbl_record R inner join tbl_computer C on r.ComputerID=c.ID inner join tbl_card A on r.CardID=a.ID where a.ID like '%abc' --8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 --9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 --10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 select Fee , CardID,UserName , ComputerID,BeignTime , EndTime from tbl_record +inner join tbl_card on tbl_record.CardID=tbl_card.ID +where Fee not in (select top 3 fee from tbl_record order by fee DESC) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..613d8279905210e155db2ae9414f01a61d45dab0 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" @@ -0,0 +1,60 @@ +use master +go +create database student +on +( +name='student', +filename='D:\SQL\student.mdf', +size=10mb, +maxsize=100mb, +filegrowth=5mb +) +log on +( +name='student_log', +filename='D:\SQL\student_log.mdf', +size=10mb, +maxsize=100mb, +filegrowth=5mb +) +use student +go +create table stuinfo +( +stuid int primary key identity, +stuname nvarchar(10), +stuage int, +stusex char(1) check(stusex=0 or stusex=1), +time datetime, +) +create table courseinfo +( +courseid int primary key identity, +coursename nvarchar(10), +coursemarks int, +) +create table sores +( +scoreid int primary key identity, +stuid int, +courseid int, +score int +) +insert into stuinfo values('Tom',19,1,''),('Jack',20,0,''),('Rose',21,1,''),('Lulu',19,1,''),('Lili',21,0,''),('abc',20,1,'2007-01-07 01:11:36.590') +insert into courseinfo values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) +insert into sores values(1,1,80),(1,2,85),(1,4,50),(2,1,75),(2,3,45),(2,4,75),(3,1,45),(4,1,95),(4,2,75),(4,3,90),(4,4,45) +select * from stuinfo +select * from courseinfo +select * from sores +select stuname,count(*) 课程数量,AVG(score) 平均分 from stuinfo s1 left join sores s2 on s1.stuid=s2.stuid group by stuname +select coursename,count(stuid) 学生个数,sum(score) 总分 from courseinfo c left join sores s2 on c.courseid=s2.courseid group by coursename +select A.* from stuinfo A inner join stuinfo B on A.stuage=B.stuage where A.stusex=B.stusex and A.stuname != B.stuname and A.stuage=B.stuage +select distinct A.* from courseinfo A inner join courseinfo B on A.coursemarks=B.coursemarks where A.coursemarks=B.coursemarks and A.courseid !=B.courseid +select s1.stuid,stuname,c.courseid,score from stuinfo s1 inner join sores s2 on s1.stuid=s2.stuid inner join courseinfo c on s2.courseid=c.courseid +select s1.stuid,c.courseid,coursename,coursemarks,score from stuinfo s1 inner join sores s2 on s1.stuid=s2.stuid inner join courseinfo c on s2.courseid=c.courseid +select s1.stuid,stuname from stuinfo s1 left join sores s2 on s1.stuid=s2.stuid left join courseinfo c on s2.courseid=c.courseid +except +select s1.stuid,stuname from stuinfo s1 inner join sores s2 on s1.stuid=s2.stuid inner join courseinfo c on s2.courseid=c.courseid +select * from stuinfo where DATEPARt(dw,time) in (0,1) +select * from stuinfo where stuname like '%a%' +select s1.stuid,AVG(s2.score) 平均分,count(coursename) 课程数量 from stuinfo s1 inner join sores s2 on s1.stuid=s2.stuid inner join courseinfo c on s2.courseid=c.courseid group by s1.stuid having count(coursename)>=2 and AVG(score)>=70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e3def6ae59d5dd9db7e8e317776a10833bd2ad73 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery2.sql" @@ -0,0 +1,77 @@ +use master +go + +create database InternetBar +on +( +name='InternetBar', +filename='D:\SQL\InternetBar.mdf', +size=10mb, +maxsize=100mb, +filegrowth=5mb +) +log on +( +name='InternetBar_log', +filename='D:\SQL\InternetBar_log.mdf', +size=10mb, +maxsize=100mb, +filegrowth=5mb +) +use InternetBar +go + +create table tbl_card +( + id char(8) primary key, + password nchar(10), + balance money, + userName nchar(2) +) +go + +create table tbl_computer +( + id char(3) primary key, + onUse int check(onUse in(0,1)), + note text +) +go + +create table tbl_record +( + id int primary key, + cardID char(8) references tbl_card(id), + ComputerID char(3) references tbl_computer(id), + beginTime datetime, + endTime datetime, + fee money +) +go + +insert into tbl_card values('0023_ABC','555',98,'张军'),('0025_bbd','abe',300,'朱俊'),('0036_CCD','何柳',100,'何柳'),('0045_YGR','0045_YGR',58,'证验'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张峻') +insert into tbl_computer values ('02',0,'25555'),('03',1,'55555'),('04',0,'66666'),('05',1,'88888'),('06',0,'688878'),('B01',0,'558558') +insert into tbl_record values (23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20),(34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23),(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50),(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00',6),(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50),(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6),(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50),(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',300),(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23),(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) +select * from tbl_card +select * from tbl_computer +select * from tbl_record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select a.id,userName,b.id,beginTime,endTime,fee from tbl_card a inner join tbl_record c on a.id=c.cardID inner join tbl_computer b on c.ComputerID=b.id where userName='张军' order by fee DESC +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerID,count(beginTime) 上网次数,sum(fee) 消费总金额 from tbl_record group by ComputerID +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardid,sum(fee) 消费总金额 from tbl_record group by cardid +--4. 查询出从未消费过的上网卡的卡号和用户名 +select cardid,userName from tbl_card a left join tbl_record b on a.id=b.cardID where a.id not in(select cardID from tbl_record) +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card where passWord=userName +--6. 查询出使用次数最多的机器号和使用次数 +select b.id,count(ComputerID) 使用次数 from tbl_record a inner join tbl_computer b on a.ComputerID=b.id group by b.id +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select userName,ComputerID,fee from tbl_card a inner join tbl_record b on a.id=b.cardID where a.id like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardID,userName,ComputerID,beginTime,endTime,fee from tbl_record a inner join tbl_card b on a.cardID = b.id where DATEPART(dw,beginTime) in(1,7) +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardID,userName,ComputerID,beginTime,endTime,fee from tbl_record a inner join tbl_card b on a.cardID = b.id where DATEDIFF(hh,beginTime,endTime) > 12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardID,userName,ComputerID,beginTime,endTime,fee from tbl_record a inner join tbl_card b on a.cardID = b.id where fee not in(select top 3 fee from tbl_record order by fee DESC) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c301d1af25bf3c284d4c206d0cc959e60a89a473 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye1.sql" @@ -0,0 +1,88 @@ +use master +go +create database aa +go +use aa +go +create table stuInfo +( +stuID int primary key identity, +stuName nvarchar(10), +stuAge int, +stuSex int check (stuSex in (1,0)), +time datetime null +) +go +create table courseInfo +( +courseID int primary key identity, +courseName nvarchar(20), +coureMarks int +) +go +create table scoreInfo +( +scoreID int primary key identity, +stuID int, +courseID int, +score int +) +select * from stuInfo +select * from courseInfo +select * from scoreInfo +insert into stuInfo (stuName,stuAge,stuSex) values ('Tom',19,1), +('Jack',20,0), +('Rose',21,1), +('Lulu',19,1), +('Lili',21,0) +insert into stuInfo (stuName,stuAge,stuSex,time) values +('abc',20,1,'2007-01-07 01:11:36.590') +insert into courseInfo (courseName,coureMarks) values ('JacaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) +insert into scoreInfo (stuID,courseID,score) values (1,1,80), +(1,1,85), +(1,4,50), +(2,1,75), +(2,3,45), +(2,4,75), +(3,1,45), +(4,1,95), +(4,2,75), +(4,3,90), +(4,4,45) +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +--1.查询出 每个学生 所选修的 课程的数量 和 所选修的课程的 考试的平均分 +select s1.stuID,s1.stuName,count(coureMarks) 课程的数量,avg(score) 平均分 from stuInfo +s1 inner join courseInfo s2 on s1.stuID=s2.courseID inner join scoreInfo s3 on s1.stuID=s3.stuID group by s1.stuID,s1.stuName +--2.查询出 每门课程的选修的学生的个数和学生考试的总分 +select s2.courseName,count(s2.coureMarks) 个数,sum(score) 总分 from stuInfo +s1 inner join courseInfo s2 on s1.stuID=s2.courseID inner join scoreInfo s3 on s1.stuID=s3.stuID group by s2.courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A,stuInfo B where A.stuSex=B.stuSex and A.stuAge=B.stuAge and A.stuID != B.stuID +--4.查询出学分一样的课程信息 +select distinct B.* from courseInfo A, courseInfo B where A.coureMarks= B.coureMarks and A.coureMarks != B.coureMarks +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select s1.stuID,s1.stuName,s3.courseID 课程号,s3.score 分数 from stuInfo +s1 inner join courseInfo s2 on s1.stuID=s2.courseID inner join scoreInfo s3 on s1.stuID=s3.stuID group by s1.stuID,s1.stuName,s3.courseID,s3.score +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select s1.stuID,s1.stuName,s2.courseName,s3.courseID 课程号,s3.score 分数 from stuInfo +s1 inner join courseInfo s2 on s1.stuID=s2.courseID inner join scoreInfo s3 on s1.stuID=s3.stuID group by s1.stuID,s1.stuName,s3.courseID,s3.score,s2.courseName +--7.查询出没有参加考试的学生的学号和姓名 +select s1.stuID,s1.stuName from stuInfo s1 left join scoreInfo s2 on s1.stuID=s2.stuID group by s1.stuID,s1.stuName +except +select s1.stuID,s1.stuName from stuInfo s1 inner join scoreInfo s2 on s1.stuID=s2.stuID group by s1.stuID,s1.stuName +--8.查询出是周六周天来报到的学生 +select * from stuInfo +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select s1.stuID,s1.stuName,count(s3.courseID) 课程的数量,avg(score) 平均分 from stuInfo +s1 inner join courseInfo s2 on s1.stuID=s2.courseID inner join scoreInfo s3 on s1.stuID=s3.stuID group by s1.stuID,s1.stuName +having count(s3.courseID)>2 and avg(score)>70 + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..17c84c4580e3f656bff8739ed5d01643b89121e9 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye2.sql" @@ -0,0 +1,82 @@ +use master +go +create database bb +go +use bb +go +create table tbl_card +( + ID varchar(20) primary key , + passWord nvarchar(10), + banlance int, + userName nvarchar(10), +) +go +create table tbl_computer +( + Id varchar(5) primary key , + onUse int , + note text, +) +go +create table tbl_record +( + ID int primary key , + cardId varchar(20) references tbl_card(ID), + ComputerId varchar(5) references tbl_computer(Id), + beginTime datetime, + endTime datetime, + fee int, +) +go +select * from tbl_computer +select * from tbl_card +select * from tbl_record +insert into tbl_card values('0023_ABC','555',98,'张军'),('0025_bbd','abe',300,'朱俊'),('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张峻') +insert into tbl_computer values('02',0,'25555'),('03',1,'55555'),('04',0,'66666'),('05',1,'88888'), +('06',0,'688878'),('B01',0,'558558') +insert into tbl_record values(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20), +(34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23), +(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00',6), +(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) +select * from tbl_computer +select * from tbl_card +select * from tbl_record +--请完成以下题目: +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select tbl_card.ID 卡号,tbl_card.userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID where tbl_card.userName='张军' order by fee DESC +--2. 查询出每台机器上的上网次数和消费的总金额 +select tbl_computer.Id,count(*)上网的次数,sum(fee)总金额 from tbl_record +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id group by tbl_computer.Id +--3. 查询出所有已经使用过的上网卡的消费总金额 +select tbl_record.cardId,sum(fee)总金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID group by tbl_record.cardId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select ID 卡号,userName 用户名 from tbl_card where ID not in(select distinct cardId from tbl_record) +--5. 将密码与用户名一样的上网卡信息查询出来 +select A.* from tbl_card A ,tbl_card B where A.userName=B.passWord +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 tbl_record.ComputerId,count(*) 使用次数 from tbl_record +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id group by tbl_record.ComputerId order by count(*) DESC +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_card.ID 卡号,tbl_card.userName 用户名,tbl_computer.Id 上网的机器号,fee 消费金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id where tbl_card.ID like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.ID 上网的卡号,tbl_card.userName 用户名,tbl_computer.Id 机器号,beginTime 开始时间,endTime 结束时间,DATEDIFF(hh,beginTime,endTime) 相差的时间,fee 消费金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id where DATEDIFF(hh,beginTime,endTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 30 percent tbl_card.ID 上网的卡号,tbl_card.userName 用户名,tbl_computer.Id 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id order by fee DESC \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7ef3091825afa181ff7d31fcf76cff0bf287861d --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" @@ -0,0 +1 @@ +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) create database stu go use stu go create table stuInfo ( stuId int primary key identity, stuname nvarchar(20) , stuage int , stusex int check(stusex=1 or stusex=0), time datetime ) create table courseInfo ( courseid int primary key identity, couesename nvarchar(20) , cousrmarks int ) create table scoreInfo ( scoreid int primary key identity, stuid int, courseid int, score int ) insert into stuInfo values ('tom',19,1,null), ('jack',20,0,null), ('rose',21,1,null), ('lulu',19,1,null), ('lili',21,0,null), ('abc',20,1,'2007-01-07 01:11:36.590') insert into courseInfo values ('javabase',4), ('html',2), ('javascript',2), ('sqlbase',2) insert into scoreInfo values (1,1,80), (1,2,85), (1,4,50), (2,1,75), (2,3,45), (2,4,75), (3,1,45), (4,1,95), (4,2,75), (4,3,90), (4,4,45) select * from stuInfo --学生信息表 select * from courseInfo --科目表 select * from scoreInfo --成绩表 select * from stuInfo S inner join scoreInfo O on S.stuId=O.stuid inner join courseInfo C on O.courseid = C.courseid --题目: --1.查询出 每个学生 所选修的 课程的数量 和所 选修的课程的考试的平均分 select stuname 学习, count(C.cousrmarks) 课程, avg(score) 平均分 from stuInfo S inner join scoreInfo O on S.stuId=O.stuid inner join courseInfo C on O.courseid = C.courseid group by stuname --2.查询出每门课程的选修的学生的个数和学生考试的总分 select C.couesename,count(S.stuId) 学生的个数,sum(O.score) 总分 from stuInfo S inner join scoreInfo O on S.stuId=O.stuid inner join courseInfo C on O.courseid = C.courseid group by C.couesename --3.查询出性别一样并且年龄一样的学生的信息 select stuage,stusex ,count(stuage),count(stusex) from stuInfo group by stuage,stusex having count(stuage)>1 and count(stusex) >1 select * from stuInfo where stuage in (select stuage from stuInfo group by stuage,stusex having count(stusex) >1) select * from stuInfo A , stuInfo B where A.stuage = B.stuage and A.stusex = B.stusex and A.stuname!=B.stuname --4.查询出学分一样的课程信息 select * from courseInfo where cousrmarks in (select cousrmarks from courseInfo group by cousrmarks having count(cousrmarks)>1) select * from courseInfo A ,courseInfo B where A.cousrmarks=B.cousrmarks and A.couesename!= B.couesename --5.查询出参加了考试的学生的学号,姓名,课程号和分数 select o.stuid 学号, stuname 姓名,o.courseid 课程号,o.score 分数 from stuInfo S inner join scoreInfo O on S.stuId=O.stuid inner join courseInfo C on O.courseid = C.courseid --6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 select o.stuid 学号, stuname 姓名,o.courseid 课程号,c.couesename 课程名,c.cousrmarks 课程学分,o.score 分数 from stuInfo S inner join scoreInfo O on S.stuId=O.stuid inner join courseInfo C on O.courseid = C.courseid --7.查询出没有参加考试的学生的学号和姓名 select * from stuInfo S left join scoreInfo O on S.stuId=O.stuid left join courseInfo C on O.courseid = C.courseid where c.couesename is null --8.查询出是周六周天来报到的学生 --9.查询出姓名中有字母a的学生的信息 select stuname from stuInfo where stuname like '%a%' --10.查询出选修了 2门课程以上 的并且 考试平均分在70 以上的 学生的学号和考试平均分以及选修课程的数量 select s.stuId, count(c.courseid) 选修课程的数量,avg(score) 考试平均分 from stuInfo S inner join scoreInfo O on S.stuId=O.stuid inner join courseInfo C on O.courseid = C.courseid group by s.stuId having count(c.courseid)>1 and avg(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a24b9b9d72747baba56bc2bf3c59069dec214752 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery2.sql" @@ -0,0 +1,3 @@ +use master go --首先创建网吧计费系统的数据库,表结构和数据如2.bmp所示,表的说明如下: create database internet go use internet go --表一为上网卡信息表(tbl_card) --id: 卡号,主键 --passWord:密码 --balance:卡上的余额 --userName:该上网卡所属的用户名 create table tbl_card ( ID nvarchar(10) primary key not null, PassWord nvarchar(10) not null, Balance nvarchar(10) not null, UserName nvarchar(10) not null ) insert into tbl_card values ('0023_ABC','555','98','张军'), ('0025_bbd','abe','300','朱俊'), ('0036_CDD','何柳','100','何柳'), ('0045_YGR','0045_YGR','58','证验'), ('0078_RJV','55885fg','600','校庆'), ('0089_EDE','zhang','134','张骏') --表2为网吧机器说明表(tbl_computer) --id:机器编号,主键 --onUse:该机器是否正在使用,0为未使用,1为正在使用 --note:该机器的说明 create table tbl_computer ( ID nvarchar(10) not null primary key, OnUse int check(OnUse=1 or OnUse=0) not null, Note int not null ) insert into tbl_computer values ('02',0,25555), ('03',1,25555), ('04',0,66666), ('05',1,88888), ('06',0,688878), ('B01',0,558558) --表3为上网记录表(tbl_record): --id:记录编号,主键 --cardId:本次上网的卡号,外键 --ComputerId:本次上网记录所使用的机器号,外键 --beginTime:本次上网记录的开始时间 --endTime:本次上网记录的结束时间 --fee:本次上网的费用 create table tbl_record ( ID nvarchar(20) primary key not null, CardID nvarchar(10) not null foreign key references tbl_card(ID), ComputerID nvarchar(10) not null foreign key (ComputerID) references tbl_computer(ID), BeignTime datetime not null, EndTime datetime not null, Fee int not null ) insert into tbl_record values (23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20), (34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23), (45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), (46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00',6), (47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',60), (48,'0023_ABC','03','2007-01-06 15:26:00','2006-01-06 22:55:00',6), (55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), (64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), (65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), (98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) --请完成以下题目: select * from tbl_record --上网记录表(tbl_record) select* from tbl_computer --网吧机器说明表(tbl_computer) select * from tbl_card --上网卡信息表(tbl_card) select * from tbl_record R inner join tbl_computer C on r.ComputerID=c.ID inner join tbl_card A on r.CardID=a.ID --1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额, --并按消费金额降序排列 select r.CardID 卡号,a.UserName 用户名,c.ID 机器编号,r.BeignTime 开始时间,r.EndTime 结束时间,r.Fee 消费金额 from tbl_record R inner join tbl_computer C on r.ComputerID=c.ID inner join tbl_card A on r.CardID=a.ID where a.UserName='张军' --2. 查询出每台机器上的上网次数和消费的总金额 select C.ID 机器, count(c.OnUse) 上网次数, sum(r.Fee) 总金额 from tbl_record R inner join tbl_computer C on r.ComputerID=c.ID inner join tbl_card A on r.CardID=a.ID group by C.ID, c.OnUse --3. 查询出所有已经使用过的上网卡的消费总金额 select r.CardID 上网卡,sum(r.Fee) 消费总金额 from tbl_record R inner join tbl_computer C on r.ComputerID=c.ID inner join tbl_card A on r.CardID=a.ID group by r.CardID --4. 查询出从未消费过的上网卡的卡号和用户名 select a.ID 卡号,a.UserName 用户名 from tbl_record R right join tbl_computer C on r.ComputerID=c.ID right join tbl_card A on r.CardID=a.ID group by a.ID,r.Fee,a.UserName having r.Fee is null --5. 将密码与用户名一样的上网卡信息查询出来 select a.* from tbl_card a,tbl_card b where a.PassWord=b.UserName --6. 查询出使用次数最多的机器号和使用次数 select top 1 count(*) 使用次数,ComputerID 机器号 from tbl_record group by ComputerID order by count(*) desc --7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 select r.CardID,a.UserName,c.ID,r.Fee from tbl_record R inner join tbl_computer C on r.ComputerID=c.ID inner join tbl_card A on r.CardID=a.ID where a.ID like '%abc' --8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 --9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 --10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 select Fee , CardID,UserName , ComputerID,BeignTime , EndTime from tbl_record +inner join tbl_card on tbl_record.CardID=tbl_card.ID +where Fee not in (select top 3 fee from tbl_record order by fee DESC) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..228d65a23c0bd02542dfe33373056871fd153fa2 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" @@ -0,0 +1,79 @@ +use master +go +create database Student +go +use Student +go +create table stuInfo +( + stuID int primary key identity, + stuName varchar(20)not null, + stuAge int not null, + stuSex int check (stuSex=0 or stuSex=1), + time datetime , +) +insert into stuInfo +select 'Tom',19,1,null union +select 'Jack',20,0,null union +select 'Rose',21,1,null union +select 'Lulu',19,1,null union +select 'Lili',21,0,null union +select 'abc',20,1,'2007-01-07' +select * from stuInfo + +create table courseInfo +( + courseID int primary key identity, + courseName varchar(20) not null, + courseMarks int +) +insert into courseInfo values ('JavaBase',4),('HTML',2),('JavaScipt',2),('SqBase',2) +select * from courseInfo +create table scoreInfo +( + scoreID int primary key identity, + stuID int references stuInfo (stuID), + coureID int references courseInfo (courseID), + score int +) +insert into scoreInfo values(1,1,80),(1,2,85),(1,4,50),(2,1,75),(2,3,45), +(2,4,75),(3,1,45),(4,1,95),(4,2,75),(4,3,90),(4,4,45) +select * from scoreInfo +select * from courseInfo +select * from stuInfo + + + + + + + +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuInfo.stuID 学生 ,count(courseMarks) 课程数量,avg(score) 平均分 from scoreInfo + inner join stuInfo on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on scoreInfo.stuID=courseInfo.courseID group by stuInfo.stuID + + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 课程, count(stuID)个数,sum(score) 总分 from scoreInfo inner join courseInfo on courseInfo.courseID=scoreInfo.coureID group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select a.stuName 学生信息 ,a.stuAge 年龄,a.stuSex 性别 from stuInfo a ,stuInfo b where a.stuAge = b.stuAge and b.stuSex=a.stuSex and a.stuID!=b.stuID + +--4.查询出学分一样的课程信息 +select distinct a.courseMarks 学分, a.courseName 课程 from courseInfo a, courseInfo b where a.courseMarks=b.courseMarks and a.courseName!=b.courseName +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select scoreInfo.stuID 学号,stuName 姓名,coureID 课程号,score 分数 from scoreInfo inner join stuInfo on scoreInfo.stuID=stuInfo.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuID 学号,courseID 课程号,courseName 课程名, courseMarks 课程学分, score 分数 from courseInfo inner join scoreInfo on courseInfo.courseID=scoreInfo.coureID +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID 学号,stuInfo.stuName 姓名 from scoreInfo A right join stuInfo on stuInfo.stuID=A.stuID where A.score is null +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +SELECT *FROM stuInfo where stuName like '%a%' + + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生 的学号 和考试平均分 以及选修课程的数量 +select count(stuID) 课程数量,avg(score) 平均分,stuID 学号 from scoreInfo group by stuID having avg(score)>70 and count(stuID)>2 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..20f5cd8da3163b2e4ed833d47a114799f504d8b1 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" @@ -0,0 +1,82 @@ +use master +go +create database cybercaf +go +use cybercaf +go +create table tbl_card +( + cardid varchar(10) primary key , + passWord varchar(10), + balance money , + userName varchar(10) +) +insert into tbl_card values('0023_ABC','555','98','张军'), +('0025_bbb','abe','300','朱骏'), +('0036_CCD','何柳','100','何柳'), +('0045_YGR','0045_YGR','58','验证'), +('0078_RJV','5585fg','600','校庆'), +('0089_EDE','zhang','134','张俊') +create table tbl_computer +( + computerid varchar(10) primary key , + onUse int check(onUse=0 or onUse=1), + note int +) +insert into tbl_computer values('02',0,25555), +('03',1,55555), +('04',0,66666), +('05',1,88888), +('06',0,688878), +('B01',0,558558) +create table tbl_record +( + recordid int primary key , + cardId varchar(10) references tbl_card(cardid ), + ComputerId varchar(10) references tbl_computer(computerid), + beginTime datetime , + endTime datetime, + fee money +) +insert into tbl_record values(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), +(34,'0025_bbb','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), +(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6'), +(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6'), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30'), +(65,'0025_bbb','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), +(98,'0025_bbb','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') + +--请完成以下题目: + +select * from tbl_card +select * from tbl_computer +select * from tbl_record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select userName 有户名,tbl_card.cardid 卡号,computerid 机器编号, beginTime 开始时间, endtime 结束时间,fee 消费金额 from tbl_card left join tbl_record on tbl_card.cardid=tbl_record.cardid where userName='张军' order by fee DESC +--2. 查询出每台机器上的上网次数和消费的总金额 +select count(*) 机器次数,tbl_computer.computerid 机器, sum(fee)总金额 from tbl_computer left join tbl_record on tbl_computer.computerid=tbl_record.computerid group by tbl_computer.computerid +--3. 查询出所有已经使用过的上网卡的消费总金额 +select tbl_card.cardid 卡, sum(fee) 消费总金额 from tbl_card left join tbl_record on tbl_card.cardid=tbl_record.cardid group by tbl_card.cardid having sum(fee) is not null +--4. 查询出从未消费过的上网卡的卡号和用户名 +select tbl_card.cardid 卡号, userName 用户名, sum(fee) 消费总金额 from tbl_card left join tbl_record on tbl_card.cardid=tbl_record.cardid group by tbl_card.cardid,userName having sum(fee) is null +--5. 将密码与用户名一样的上网卡信息查询出来 +select cardid 卡号,passWord 密码,userName 用户名,balance from tbl_card where passWord=userName +--6. 查询出使用次数最多的机器号和使用次数 +select computerid 机器号,count(computerid) 次数 from tbl_record group by computerid having computerid='03' +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_record.cardId 卡号,userName 用户名, ComputerId 机器号,fee 消费金额 from tbl_record left join tbl_card on +tbl_record.cardId=tbl_card.cardId group by tbl_record.cardId ,userName,ComputerId,fee having tbl_record.cardId like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_record.cardId 卡号,userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from + tbl_record left join tbl_card on tbl_record.cardId=tbl_card.cardid where (endTime-beginTime)>'12:00:00' +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 7 tbl_record.cardId 卡号,userName 用户名, ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record +left join tbl_card on tbl_record.cardId=tbl_card.cardid order by fee asc + + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..00c8997d8457240dcae27d2c2d4af773d7f54f15 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery1.sql" @@ -0,0 +1,55 @@ +create database hh +go +use hh +go +create table stuInfo +( + stuID int primary key identity, + stuName nvarchar(8), + stuAge int, + stuSex nchar(2), + time datetime +) +create table courseInfo +( + courseID int primary key identity, + courseName nvarchar(12), + courseMarks int +) +create table scoreInfo +( + scoreID int primary key identity, + stuID int, + courseID int, + score int +) +insert into stuInfo(stuName,stuAge,stuSex,time) values ('Tom',19,'男',''),('Jack',20,'女',''),('Rose',21,'男',''), +('Lulu',19,'男',''),('Lili',21,'女',''),('abc',20,'男','2007-01-07 01:11:36.590') +insert into courseInfo values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) +insert into scoreInfo values(1,1,80),(1,2,85),(1,4,50),(2,1,75),(2,3,45),(2,4,75),(3,1,45),(4,1,95),(4,2,75),(4,3,90), +(4,4,45) +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +--1.查询出每个学生所选修的课程的数量 和 所选修的课程的考试的平均分 +select stuid,count(*)数量,avg(score) from scoreInfo group by stuid +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select c.courseid,count(*)数量,sum(s.score)总分 from courseInfo c inner join scoreInfo s on c.courseid=s.courseid group by c.courseid +--3.查询出性别一样并且年龄一样的学生的信息 +select a.* from stuInfo a, stuInfo b where a.stuage= b.stuage and a.stusex=b.stusex and a.stuid !=b.stuid +--4.查询出学分一样的课程信息 +select distinct a.* from courseInfo a, courseInfo b where a.courseMarks=b.courseMarks and a.coursename!=b.coursename +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select b.stuid,a.stuname,b.courseid,b.score from stuInfo a inner join scoreInfo b on a.stuid=b.stuid +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select s.stuid,c.courseid,c.coursename,c.courseMarks,s.score from courseInfo c inner join scoreInfo s on c.courseid=s.courseid +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuid,stuInfo.stuname from stuInfo left join scoreInfo on stuInfo.stuid= scoreInfo .stuid where score is null +select stuid,stuname from stuInfo where stuid not in (select stuid from scoreInfo group by stuid,courseid ) +--8.查询出是周六周天来报到的学生 +select stuname,time from stuInfo where time =CONVERT(datetime,time) group by time,stuname +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuname like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuid 学号 ,count(*) 数量 ,avg(score) 平均分 from scoreInfo group by stuid having count(*)>=2 and avg(score)>=70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e3dd2ce04724c54fccac016f7a12687ed88d91a3 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery2.sql" @@ -0,0 +1,86 @@ + +create database yyy +go +use yyy +go +create table tbl_card +( + ID varchar(20) primary key , + passWord nvarchar(10), + banlance int, + userName nvarchar(10), +) +create table tbl_computer +( + Id varchar(5) primary key , + onUse int , + note text, +) +create table tbl_record +( + ID int primary key , + cardId varchar(20) references tbl_card(ID), + ComputerId varchar(5) references tbl_computer(Id), + beginTime datetime, + endTime datetime, + fee int, +) + + +insert into tbl_card values('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'), +('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张峻') +insert into tbl_computer values('02',0,'25555'), +('03',1,'55555'), +('04',0,'66666'), +('05',1,'88888'), +('06',0,'688878'), +('B01',0,'558558') +insert into tbl_record values(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20), +(34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23), +(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00',6), +(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) + + +select * from tbl_computer +select * from tbl_card +select * from tbl_record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select tbl_card.ID 卡号,tbl_card.userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID where tbl_card.userName='张军' order by fee DESC +--2. 查询出每台机器上的上网次数和消费的总金额 +select tbl_computer.Id,count(*)上网的次数,sum(fee)总金额 from tbl_record +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id group by tbl_computer.Id +--3. 查询出所有已经使用过的上网卡的消费总金额 +select tbl_record.cardId,sum(fee)总金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID group by tbl_record.cardId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select ID 卡号,userName 用户名 from tbl_card where ID not in(select distinct cardId from tbl_record) +--5. 将密码与用户名一样的上网卡信息查询出来 +select A.* from tbl_card A ,tbl_card B where A.userName=B.passWord +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 tbl_record.ComputerId,count(*) 使用次数 from tbl_record +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id group by tbl_record.ComputerId order by count(*) DESC +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_card.ID 卡号,tbl_card.userName 用户名,tbl_computer.Id 上网的机器号,fee 消费金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id where tbl_card.ID like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.ID 上网的卡号,tbl_card.userName 用户名,tbl_computer.Id 机器号,beginTime 开始时间,endTime 结束时间,DATEDIFF(hh,beginTime,endTime) 相差的时间,fee 消费金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id where DATEDIFF(hh,beginTime,endTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 30 percent tbl_card.ID 上网的卡号,tbl_card.userName 用户名,tbl_computer.Id 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record +inner join tbl_card on tbl_record.cardId=tbl_card.ID +inner join tbl_computer on tbl_record.ComputerId=tbl_computer.Id order by fee DESC \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery6.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..24ffde0fa3df75013dbb05797ff7f28d39212c08 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery6.sql" @@ -0,0 +1,31 @@ +select*from StuInfo +select*from courseInfo +select*from scoreInfo + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName ,count(*) 课程数量,avg(score) 平均分 from stuInfo inner join scoreInfo on StuInfo.stuID=scoreInfo.stuID +group by stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select count(courseID) 学生个数 ,score 总分 from scoreInfo group by score + +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo A +left join stuInfo B on A.stuID!=B.stuID where A.stuSex=B.stuSex and A.stuAge=B.stuAge +--4.查询出学分一样的课程信息 +select*from scoreInfo A +left join scoreInfo B on A.stuID!=B.stuID where A.score=B.score + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuName,courseID,score from + + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + +--7.查询出没有参加考试的学生的学号和姓名 + +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery11.1sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery11.1sql" new file mode 100644 index 0000000000000000000000000000000000000000..796e81e2d997986a0a28ddddacfd3eb4e3e2560b --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery11.1sql" @@ -0,0 +1,85 @@ +use master +go +create database student +on +( + name='student', + filename='D:\student.mdf', + size=10mb, + maxsize=100mb, + filegrowth=15% +) +log on +( + name='student_log', + filename='D:\studentlog.ldf', + size=10mb, + maxsize=100mb, + filegrowth=15% +) +go +use student +go +create table stuinfo +( + stuid int primary key identity, + stuname nvarchar(10), + stuage nchar(10), + stusex char(1) check (stusex in(1,0)), + time datetime +) +create table courseinfo +( + courseid int primary key identity, + coursename nvarchar(10), + coursemarks int +) +create table scoreinfo +( + scoreid int identity primary key, + stuid int, + courseid int, + score int +) +insert into stuinfo values +('Tom',19,1,null), +('Jack',20,0,null), +('Rose',21,1,null), +('Lulu',19,1,null), +('Lili',21,0,null), +('abc',20,1,'2007-01-07 01:11:36.590') +insert into courseinfo values ('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) +insert into scoreinfo values (1,1,80),(1,2,85),(1,4,50),(2,1,75),(2,3,45),(2,4,75),(3,1,45),(4,1,95),(4,2,75),(4,3,90),(4,4,45) +select * from stuinfo +select * from scoreinfo +select * from courseinfo +--1.查询出 每个学生 所选修的课程的数量 和 所选修的课程的考试的平均分 +select stuinfo.stuid,stuname,count(courseid)课程的数量,avg(score)平均分 from scoreinfo +inner join stuinfo on stuinfo.stuid = scoreinfo.stuid group by stuinfo.stuid,stuinfo.stuname +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseinfo.coursename,count(stuid)学生的个数,sum(score)考试的总分 from scoreinfo +inner join courseinfo on courseinfo.courseid = scoreinfo.courseid group by courseinfo.coursename +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A,stuInfo B where A.stuSex=B.stuSex and A.stuAge=B.stuAge and A.stuID != B.stuID +--4.查询出学分一样的课程信息 +select distinct A.* from courseinfo A,courseinfo B where A.coursemarks = B.coursemarks and A.courseid != B.courseid +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuinfo.stuid,stuinfo.stuname,scoreinfo.courseid,scoreinfo.score from scoreinfo +inner join stuinfo on stuinfo.stuid = scoreinfo.stuid +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select scoreinfo.stuid,courseinfo.courseid,courseinfo.coursename,courseinfo.coursemarks,scoreinfo.score from scoreinfo +inner join courseinfo on courseinfo.courseid = scoreinfo.courseid +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID 学号,stuInfo.stuName 姓名 from stuinfo +left join scoreinfo on scoreInfo.stuID=stuInfo.stuID +except +select stuInfo.stuID 学号,stuInfo.stuName 姓名 from scoreInfo +right join stuInfo on scoreInfo.stuID=stuInfo.stuID where score is not null +--8.查询出是周六周天来报到的学生 +select * from stuInfo where DATEPART(dw,[time]) in(1,7) +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuinfo.stuid 学号,avg(score)考试平均分,count(score.courseid)选修课程的数量 from scoreinfo +inner join courseinfo on scoreInfo.courseID=courseInfo.courseID +inner join stuInfo on stuInfo.stuID=scoreInfo.stuID group by stuInfo.stuName , stuInfo.stuID having count(*)>2 and avg(score)>70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery11.2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery11.2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2f75b5e9878b0cf5fd452cbedf687eb84985e0d8 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery11.2.sql" @@ -0,0 +1,88 @@ +create database tbl +go +use tbl +go + +create table tbl_card +( + id varchar(20) primary key, + passWord varchar(12), + balance money, + userName varchar(10) +) +insert into tbl_card values +('0023_ABC','555',99,'张军'), +('0025_bbd','abc',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'验证'), +('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张俊') + +create table tbl_computer +( + id varchar(3) primary key, + onUse char(1) check(onUse in('1','0')), + note text +) + +insert into tbl_computer values +('02',0,'25555'), +('03',1,'55555'), +('04',0,'66666'), +('05',1,'888888'), +('06',0,'688878'), +('B01',0,'558558') + +create table tbl_record +( + id varchar(20) primary key , + cardId varchar(20) references tbl_card(id), + ComputerId varchar(3) references tbl_computer(id), + beginTime datetime, + endTime datetime, + fee money +) +insert into tbl_record values +('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), +('34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), +('45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +('46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6'), +('47','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +('48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6'), +('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), +('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30'), +('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), +('98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select A.id 卡号,A.userName 用户名,B.beginTime 开始时间,B.endTime 结束时间,B.fee 消费金额 +from tbl_card A join tbl_record B on A.id=B.cardId +where userName like '张军' order by fee DESC +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId 机器编号,count(ComputerId) 上网次数,sum(fee) 消费总金额 from tbl_record group by ComputerId +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId 使用过卡号,sum(fee) 消费总金额 +from tbl_record group by cardId having count(cardId)>0 +--4. 查询出从未消费过的上网卡的卡号和用户名 +select A.id 卡号,A.userName 用户名 +from tbl_card A left join tbl_record B on B.cardId=A.id +where fee is null +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card where userName=passWord +select A.id,A.passWord,A.balance,A.userName from tbl_card A,tbl_card B +where A.userName=B.passWord +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号,count(ComputerId) 使用次数 from tbl_record +group by ComputerId order by count(ComputerId) DESC +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select A.userName 用户名,B.ComputerId 机器号,B.fee 消费金额 +from tbl_card A join tbl_record B on A.id=B.cardId where A.id like '%ABC' +--8. 查询出是周六、周天上网的c记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select B.cardId 卡号,A.userName 用户名,B.ComputerId 机器号,B.beginTime 开始时间,B.endTime 结束时间,B.fee 消费金额 +from tbl_card A join tbl_record B on A.id=B.cardId where datename(dw,B.beginTime)='星期六' or datename(dw,B.beginTime)='星期日' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select B.cardId 卡号,A.userName 用户名,B.ComputerId 机器号,B.beginTime 开始时间,B.endTime 结束时间,B.fee 消费金额 +from tbl_card A join tbl_record B on A.id=B.cardId where datediff(HOUR,B.beginTime,B.endTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 B.cardId 卡号,A.userName 用户名,B.ComputerId 机器号,B.beginTime 开始时间,B.endTime 结束时间,B.fee 消费金额 +from tbl_card A join tbl_record B on A.id=B.cardId order by fee DESC \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..23e10913585653454ee225dc6226c7afa4e29f77 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" @@ -0,0 +1,83 @@ +use master +go + +create database Student +go + +use Student +go + +create table stuInfo +( + stuID int primary key identity(1,1), + stuName varchar(10), + stuAge int, + stuSex int check(stuSex in(0,1)), + [time] datetime +) +go + +create table courseInfo +( + courseID int primary key identity(1,1), + courseName char(20), + courseMarks int +) +go + +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) +go + +insert into stuInfo(stuName,stuAge,stuSex) values ('Tom',19,1), +('Jack',20,0),('Rose',21,1),('Lulu',19,1), +('Lili',21,0) +insert into StuInfo values ('abc',20,1,'2007-01-07 01:11:36.590') + +insert into courseInfo values ('JavaBase',4), +('HTML',2),('JavaScript',2),('SqlBase',2) + +insert into scoreInfo values (1,1,80), +(1,2,85),(1,4,50),(2,1,75),(2,3,45),(2,4,75), +(3,1,45),(4,1,95),(4,2,75),(4,3,90),(4,4,45) +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName,COUNT(CI.courseID) 选课数量,AVG(score)平均分 from stuInfo SI +left join scoreInfo SC on SI.stuID = SC.stuID +left join courseInfo CI on CI.courseID = SC.courseID group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName,COUNT(SC.stuID)选修学生人数,SUM(score)考试总分 from stuInfo SI inner join scoreInfo SC on SI.stuID = SC.stuID +inner join courseInfo CI on CI.courseID = SC.courseID group by CI.courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo A inner join stuInfo B on A.stuAge = B.stuAge +where (A.stuSex = B.stuSex) and (A.stuName != B.stuName) +--4.查询出学分一样的课程信息 +select distinct A.* from courseInfo A inner join courseInfo B on A.courseMarks = B.courseMarks +where (A.courseName != B.courseName) and (A.courseMarks = B.courseMarks) +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select SI.stuID,SI.stuName,CI.courseID,SC.score from stuInfo SI +inner join scoreInfo SC on SI.stuID = SC.stuID +inner join courseInfo CI on SC.courseID = CI.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select SI.stuID,SI.stuName,CI.courseID,CI.courseMarks,SC.score from stuInfo SI +inner join scoreInfo SC on SI.stuID = SC.stuID +inner join courseInfo CI on SC.courseID = CI.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select stuID,stuName from stuInfo +except +select distinct SI.stuID,SI.stuName from stuInfo SI +inner join scoreInfo SC on SI.stuID = SC.stuID +inner join courseInfo CI on SC.courseID = CI.courseID +--8.查询出是周六周天来报到的学生 +select * from stuInfo where DATEPART(dw,[time]) in(1,7) +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select SI.stuID,AVG(score)考试平均分,COUNT(SC.stuID)选修课程数量 from stuInfo SI +inner join scoreInfo SC on SI.stuID = SC.stuID +inner join courseInfo CI on SC.courseID = CI.courseID + group by SI.stuID having COUNT(SC.stuID) > 2 and AVG(score) > 70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c9d10198ebc0f5cd313d892246e322e3bb4e772b --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" @@ -0,0 +1,81 @@ +use master +go + +create database InternetBar +go + +use InternetBar +go + +create table tbl_card +( + id char(8) primary key, + password nchar(10), + balance money, + userName nchar(2) +) +go + +create table tbl_computer +( + id char(3) primary key, + onUse int check(onUse in(0,1)), + note text +) +go + +create table tbl_record +( + id int primary key, + cardID char(8) references tbl_card(id), + ComputerID char(3) references tbl_computer(id), + beginTime datetime, + endTime datetime, + fee money +) +go + +insert into tbl_card values('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'),('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'),('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张峻') +go + +insert into tbl_computer values ('02',0,'25555'), +('03',1,'55555'),('04',0,'66666'),('05',1,'88888'), +('06',0,'688878'),('B01',0,'558558') +go + +insert into tbl_record values (23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20), +(34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23), +(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00',6), +(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',300), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) +go + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select TC.id 卡号,userName 用户名,ComputerID 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record TR inner join tbl_card TC on TR.cardID = TC.id where cardID = (select id from tbl_card where userName = '张军') order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerID,COUNT(ComputerID) 上网次数,SUM(fee)消费总金额 from tbl_record group by ComputerID +--3. 查询出所有已经使用过的上网卡的消费总金额 +select TC.id,SUM(fee)消费总金额 from tbl_card TC inner join tbl_record TR on TC.id = TR.cardID where TC.id = TR.cardID group by TC.id +--4. 查询出从未消费过的上网卡的卡号和用户名 +select TC.id,userName from tbl_card TC left join tbl_record TR on TC.id = TR.cardID where TC.id not in(select cardID from tbl_record) +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card where userName = password +--6. 查询出使用次数最多的机器号和使用次数 +select Top 1 ComputerID,COUNT(ComputerID)使用次数 from tbl_record group by ComputerID order by COUNT(ComputerID) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select TC.id,userName,ComputerID,fee from tbl_card TC inner join tbl_record TR on TC.id = TR.cardID where TC.id like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardID,userName,ComputerID,beginTime,endTime,fee from tbl_record TR inner join tbl_card TC on TR.cardID = TC.id where DATEPART(dw,beginTime) in(1,7) +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardID,userName,ComputerID,beginTime,endTime,fee from tbl_record TR inner join tbl_card TC on TR.cardID = TC.id where DATEDIFF(hh,beginTime,endTime) > 12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardID,userName,ComputerID,beginTime,endTime,fee from tbl_record TR inner join tbl_card TC on TR.cardID = TC.id +where fee not in(select top 3 fee from tbl_record order by fee desc) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery\344\275\234\344\270\2322.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a52ece661aa1a63409e5be635182c8abb774a509 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery\344\275\234\344\270\2322.sql" @@ -0,0 +1,140 @@ +--首先创建网吧计费系统的数据库,表结构和数据如2.bmp所示,表的说明如下: + +use master +go +create database payment +on( + name='payment', + filename='D:\sql\payment.mdf', + size=8mb, + maxsize=80mb, + filegrowth=10% +) +log on( + name='payment_log', + filename='D:\sql\payment_log.ldf', + size=8mb, + maxsize=80mb, + filegrowth=10% +) +--表一为上网卡信息表(tbl_card) +--id: 卡号,主键 +--PassWord:密码 + +--balance:卡上的余额 +--userName:用户名 +create table tbl_card +( + id nvarchar(20) primary key not null, + passWord nvarchar(20) not null, + balance int not null, + userName nvarchar(20) not null +) + + drop table tbl_card +insert into tbl_card +select '0023_ABC','555',98,'张军' union +select '0025_bbd','abe',300,'朱俊' union +select '0036_CCD','何柳',100,'何柳' union +select '0045_YGR','0045_YGR',58,'证验'union +select '0078_RJV','55885fg',600,'校庆' union +select '0089_EDE','zhang',134,'张峻' +--表2为网吧机器说明表(tbl_computer) +--id:机器编号,主键 +--onUse:该机器是否正在使用,0为未使用,1为正在使用 +--note:该机器的说明 +create table tbl_computer( + id nvarchar(20) primary key not null, + onUse nchar(2) default(1) check(onUse=1 or onUse=0), + note int not null +) +drop table tbl_computer +insert into tbl_computer +select '02',0,25555 union +select '03',1,55555 union +select '04',0,66666 union +select '05',1,88888 union +select '06',0,688878 union +select 'B01',0,558558 + +--表3为上网记录表(tbl_record): +--id:记录编号,主键 +--cardId:本次上网的卡号,外键 +--ComputerId:本次上网记录所使用的机器号,外键 +--beginTime:本次上网记录的开始时间 +--endTime:本次上网记录的结束时间 +--fee:本次上网的费用 +create table tbl_record +( + id int primary key not null, + cardId nvarchar(20) not null, + ComputerId nvarchar(20) not null, + beginTime datetime not null, + endTime datetime not null, + fee int +) +DROP TABLE tbl_record +insert into tbl_record +select 23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20 UNION +select 34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23 UNION +select 45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00 ', 50 union +select 46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00', 6 union +select 47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00', 50 union +select 48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 ',6 union +select 55,'0023_ABC','03','2006-07-21 15:26:00', '2006-07-21 22:55:00',50 union +select 64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',300 union +select 65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23 union +select 98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23 + +--请完成以下题目: +SELECT * FROM tbl_card +SELECT * FROM tbl_computer +SELECT * FROM tbl_record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +SELECT tbl_card.id 卡号,tbl_card.userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_card inner join tbl_record + on tbl_card.id=tbl_record.cardId + where tbl_card.userName='张军' order by fee desc +SELECT * FROM tbl_card +SELECT * FROM tbl_computer +SELECT * FROM tbl_record +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId 机器,count(cardId)上网次数,sum(fee)总金额 +from tbl_record group by cardId,ComputerId +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId,sum(fee) 总金额 +from tbl_record group by cardId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select tbl_card.id 卡号,userName 用户名 from tbl_card +left join tbl_record on tbl_card.id =tbl_record.cardId +where fee is null group by tbl_card.id,userName +SELECT * FROM tbl_card +SELECT * FROM tbl_computer +SELECT * FROM tbl_record +--5. 将密码与用户名一样的上网卡信息查询出来 +select a.* FROM tbl_card a,tbl_card b where a.PassWord=b.userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号,count(ComputerId)使用次数 +from tbl_record group by ComputerId order by count(ComputerId) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器号, fee 消费金额 +from tbl_card inner join tbl_record on tbl_card.id=tbl_record.cardId +where tbl_record.cardId like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select * from tbl_card,tbl_record +select tbl_card.id 卡号,userName 用户名,ComputerId 机器号,beginTime 消费金额,endTime 结束时间,fee 消费金额 +from tbl_record inner join tbl_card on tbl_card.id=tbl_record.cardId group by tbl_card.id, userName, + ComputerId,beginTime,endTime,fee + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select * from tbl_card,tbl_record +select tbl_card.id 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额,count(endTime-beginTime)上网记录时间 + from tbl_record inner join tbl_card on tbl_card.id=tbl_record.cardId group by tbl_card.id, userName, + ComputerId,beginTime,endTime,fee +having count(endTime-beginTime)>=12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 cardId 卡号,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record order by fee desc + + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQL\344\275\234\344\270\2321.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQL\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..862ce5735346d478567e3e19bf63a21a123d332d --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQL\344\275\234\344\270\2321.sql" @@ -0,0 +1,126 @@ +use master +go + +drop database StudentInfo + +create database StudentInfo +on( + name = 'StudentInfo', + filename = 'D:\sql\StudentInfo.mdf', + size = 8mb, + maxsize= 80mb, + filegrowth=10% +) +log on ( + + name = 'StudentInfo_log', + filename = 'D:\sql\StudentInfo_log.ldf', + size = 8mb, + maxsize= 80mb, + filegrowth=10% +) +go +use StudentInfo +go + +create table stuInfo +( + stuID int primary key identity(1,1), + stuName nvarchar(10) not null, + stuAge nvarchar(10) not null, + stuSex nchar(2) default(1) check(stuSex='1' or stuSex='0') not null, + time datetime, + +) +insert into stuInfo +select 'Tom',19,'1',null union +select 'Jack', 20,'0',null union +select 'Rose',21,'1',null union +select 'Lulu',19,'1',null union +select 'Lili',21,'0',null union +select 'abc',20,'1','2007-01-07 01:11:36.590' +create table courseInfo +( + courseID INT PRIMARY KEY IDENTITY(1,1), + courseName nvarchar(20) not null, + courseMarks int not null, +) +drop table courseInfo + + +insert into courseInfo +select 'JavaBase',4 union +select 'HTML',2 union +select 'JavaScript',2 union +select 'SqlBase',2 + + + + +create table scoreInfo +( + scoreID INT PRIMARY KEY identity(1,1) not null, + stuID INT NOT NULL, + courseID INT NOT NULL, + score INT +) +DROP TABLE scoreInfo + +insert into scoreInfo +select 1,1,80 union +select 1,2,85 union +select 1,4,50 union +select 2,1,75 union +select 2,3,45 union +select 2,4,75 union +select 3,1,45 union +select 4,1,95 union +select 4,2,75 union +select 4,3,90 union +select 4,4,45 + + + + + + +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + +--题目: +select * from stuInfo +select * from courseInfo +select * from scoreInfo +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select * from courseInfo,scoreInfo +select stuID 序号, + count(courseName) 课程的数量,avg(score) 考试的平均分 FROM courseInfo INNER JOIN scoreInfo + ON courseInfo.courseID=scoreInfo.courseID + GROUP BY stuID +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +SELECT courseName 每门课程,SUM(stuID) 学生的人数,sum(score) 考试的总分 from courseInfo inner join scoreInfo +on courseInfo.courseID=scoreInfo.courseID group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select A. * from stuInfo A ,stuInfo B where A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuID!=B.stuID + +--4.查询出学分一样的课程信息 +select distinct A.* from courseInfo A,courseInfo B +where A.courseMarks=B.courseMarks AND A.courseID!=B.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select * from stuInfo,courseInfo,scoreInfo +select scoreInfo.stuID 学生学号,stuName 名字,courseID 课程,score 分数 from scoreInfo +left join stuInfo on scoreInfo.stuID=stuInfo.stuID +group by scoreInfo.stuID,stuName,courseID,score +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select scoreInfo.stuID 学号,courseInfo.courseID 课程号,courseName 课程名,courseMarks 课程学分,score 分数 from courseInfo left join scoreInfo +on courseInfo.courseID=scoreID +--7.查询出没有参加考试的学生的学号和姓名 +SELECT Stuinfo.stuID 学号,Stuinfo.stuName 姓名 from Stuinfo +left join scoreInfo on stuInfo.stuID=scoreInfo.stuID +where score is null +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select * from scoreInfo +select stuID 学号,avg(score) 平均分,count(courseID) 课程的数量 FROM scoreInfo GROUP BY stuID HAVING count(courseID)>=2 AND avg(score)>=70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\344\275\234\344\270\2321.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b60321540f467d910c2f659fe8889aee601c233e --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\344\275\234\344\270\2321.sql" @@ -0,0 +1,107 @@ +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +create database Student +go +use Student +go + +create table stuInfo +( + stuID int identity(1,1) primary key, + stuName varchar(20) not null, + stuAge int, + stuSex varchar(1) default(1) check(stuSex in('1','0')), + time date +) +insert into stuInfo(stuName,stuAge,stuSex) values +('Tom',19,'1'), +('Jack',20,'0'), +('Rose',21,'1'), +('Lulu',19,'1'), +('Lili',21,'0') +insert into stuInfo values ('abc',20,1,'2001-01-07 01:11:36.590') + + +create table courseInfo +( + courseID int identity(1,1) primary key, + courseName varchar(20) not null, + courseMarks int +) +insert into courseInfo values ('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) + +create table scoreInfo +( + scoreID int identity(1,1) primary key, + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) +insert into scoreInfo values +(1,1,80), +(1,2,85), +(1,4,50), +(2,1,75), +(2,3,45), +(2,4,75), +(3,1,45), +(4,1,95), +(4,2,75), +(4,3,90), +(4,4,45) + +select * from stuInfo +select * from courseInfo +select * from scoreInfo +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + +select StuID 学生编号,count(courseID) 所选修课程数量,avg(score) 平均分 +from scoreInfo group by StuID + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + +select courseID 课程编号,count(StuID) 课程所选修学生个数,sum(score) 总分 +from scoreInfo group by courseID + +--3.查询出性别一样并且年龄一样的学生的信息 + +select A.stuID,A.stuName,A.stuAge,A.stuSex,A.time from StuInfo A,StuInfo B +where A.stuSex=B.stuSex and A.stuAge=B.stuAge and A.stuID != b.stuID + +--4.查询出学分一样的课程信息 + +select difference(A.courseID,A.courseName,A.courseMarks from courseInfo A,courseInfo B +where A.courseMarks=B.courseMarks and A.courseID != b.courseID + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + +select A.stuID 学号,A.stuName 姓名,B.courseID 课程编号,B.score 分数 +from stuInfo A inner join scoreInfo B on A.stuID=B.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + +select A.stuID 学号,B.courseID 课程编号,C.courseName 课程名,C.courseMarks 课程学分,B.score 分数 +from stuInfo A inner join scoreInfo B on A.stuID=B.stuID +inner join courseInfo C on C.courseID=B.courseID + +--7.查询出没有参加考试的学生的学号和姓名 + +select A.stuID 学号,A.stuName 姓名 +from stuInfo A left join scoreInfo B on A.stuID=B.stuID +where score is null + +--8.查询出是周六周天来报到的学生 + + select * from stuInfo where time= + (select time from stuInfo + group by time having datename(dw,time)='星期六' or datename(dw,time)='星期日') + +--9.查询出姓名中有字母a的学生的信息 + +select * from stuInfo where stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 + +select A.StuID 学号,sum(score) 平均分,count(courseID) 选修数量 +from stuInfo A join scoreInfo B on A.stuID=B.stuID +group by A.StuID having count(courseID)>2 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\344\275\234\344\270\2322.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..23c841639bea5771e2c134720cf4c4cf75c11b7b --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\344\275\234\344\270\2322.sql" @@ -0,0 +1,92 @@ +create database tbl +go +use tbl +go + +create table tbl_card +( + id varchar(20) primary key, + passWord varchar(12), + balance money, + userName varchar(10) +) +insert into tbl_card values +('0023_ABC','555',99,'张军'), +('0025_bbd','abc',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'验证'), +('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张俊') + +create table tbl_computer +( + id varchar(3) primary key, + onUse char(1) check(onUse in('1','0')), + note text +) + +insert into tbl_computer values +('02',0,'25555'), +('03',1,'55555'), +('04',0,'66666'), +('05',1,'888888'), +('06',0,'688878'), +('B01',0,'558558') + +create table tbl_record +( + id varchar(20) primary key , + cardId varchar(20) references tbl_card(id), + ComputerId varchar(3) references tbl_computer(id), + beginTime datetime, + endTime datetime, + fee money +) +insert into tbl_record values +('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), +('34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), +('45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +('46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6'), +('47','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +('48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6'), +('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), +('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30'), +('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), +('98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') + +--请完成以下题目: +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select A.id 卡号,A.userName 用户名,B.beginTime 开始时间,B.endTime 结束时间,B.fee 消费金额 +from tbl_card A join tbl_record B on A.id=B.cardId +where userName like '张军' order by fee DESC +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId 机器编号,count(ComputerId) 上网次数,sum(fee) 消费总金额 from tbl_record group by ComputerId +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId 使用过卡号,sum(fee) 消费总金额 +from tbl_record group by cardId having count(cardId)>0 +--4. 查询出从未消费过的上网卡的卡号和用户名 +select A.id 卡号,A.userName 用户名 +from tbl_card A left join tbl_record B on B.cardId=A.id +where fee is null +--5. 将密码与用户名一样的上网卡信息查询出来 + +select * from tbl_card where userName=passWord + +select A.id,A.passWord,A.balance,A.userName from tbl_card A,tbl_card B +where A.userName=B.passWord +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号,count(ComputerId) 使用次数 from tbl_record +group by ComputerId order by count(ComputerId) DESC +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select A.userName 用户名,B.ComputerId 机器号,B.fee 消费金额 +from tbl_card A join tbl_record B on A.id=B.cardId where A.id like '%ABC' +--8. 查询出是周六、周天上网的c记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select B.cardId 卡号,A.userName 用户名,B.ComputerId 机器号,B.beginTime 开始时间,B.endTime 结束时间,B.fee 消费金额 +from tbl_card A join tbl_record B on A.id=B.cardId where datename(dw,B.beginTime)='星期六' or datename(dw,B.beginTime)='星期日' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select B.cardId 卡号,A.userName 用户名,B.ComputerId 机器号,B.beginTime 开始时间,B.endTime 结束时间,B.fee 消费金额 +from tbl_card A join tbl_record B on A.id=B.cardId where datediff(HOUR,B.beginTime,B.endTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 B.cardId 卡号,A.userName 用户名,B.ComputerId 机器号,B.beginTime 开始时间,B.endTime 结束时间,B.fee 消费金额 +from tbl_card A join tbl_record B on A.id=B.cardId order by fee DESCS \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..30f53adb6671b68994eb2962d7badd6722ff878d --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" @@ -0,0 +1,111 @@ +create database student +on +( + name='student', + filename='D:\stu\student.mdf', + size=10mb, + maxsize=100mb, + filegrowth=2mb +) +log on +( + name='student_log', + filename='D:\stu\student_log.ldf', + size=10mb, + maxsize=100mb, + filegrowth=2mb +) +use student +go +create table stuInfo +( + stuID int not null identity primary key, + stuName nchar(10), + stuAge nchar(10), + stuSex varchar(1), + time datetime +) + +create table courseInfo +( + courseID int not null primary key identity, + courseName nvarchar(10), + courseMarks nvarchar(5) +) +create table scoreInfo +( + scoreID int identity, + stuID INT FOREIGN KEY(stuId) references stuInfo(stuID), + courseID int foreign key(courseID) references courseInfo(courseID), + score int +) + + + +insert into stuInfo(stuName,stuAge,stuSex)values +('Tom',19,1), +('Jack',20,0), +('Rose',21,1), +('Lulu',19,1), +('Lili',21,0) +insert into stuInfo(stuName,stuAge,stuSex,time)values +('adc',20,1,'2007-01-07 01:11:36.590') +select * from stuInfo +insert into courseInfo(courseName,courseMarks)values +('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) +select * from courseInfo +drop table courseInfo +insert into scoreInfo(stuID,courseID,score)values +(1,1,80), +(1,2,85), +(1,4,50), +(2,1,75), +(2,3,45), +(2,4,75), +(3,1,45), +(4,1,95), +(4,2,75), +(4,3,90), +(4,4,45) +select * from scoreInfo--分数信息表 +select * from courseInfo--课程信息表 +select * from stuInfo--学生信息表 +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName 姓名,count(courseInfo.courseID) 课程的数量, avg(score) 平均分 from stuInfo inner join scoreInfo on stuInfo.stuID=scoreInfo.stuID +inner join courseInfo on scoreInfo.courseID=courseInfo.courseID group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select * from scoreInfo--分数信息表 +select sc.courseID 课程编号,count(sc.stuID)学生个数,sum(sc.score) 分数 from scoreInfo sc +left join courseInfo co on sc.courseID = co.courseID +group by sc.courseID +--3.查询出性别一样并且年龄一样的学生的信息 +select b.* from stuInfo a,stuInfo b where a.stuSex = b.stuSex and a.stuAge = b.stuAge and a.stuName != b.stuName +--4.查询出学分 一样的课程信息 +select a.courseID,a.courseName,a.courseMarks from courseInfo a,courseInfo b +where a.courseMarks=b.courseMarks and a.courseName != b.courseName +group by a.courseID, a.courseName,a.courseMarks + +select * from courseInfo +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select scoreInfo.scoreId 学号, stuName 学生姓名, scoreInfo.courseID 课程号 ,score 分数 from scoreInfo left join stuInfo on +scoreInfo.stuid=stuInfo.stuid inner join courseInfo on scoreInfo.courseID=courseInfo.courseID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select scoreInfo.scoreId 学号, courseName 课程名, courseInfo.courseID ,coursemarks ,score 分数 from courseInfo left join scoreInfo on +courseInfo.courseID=scoreInfo.courseID +--7.查询出没有参加考试的学生的 学号和姓 + select *from stuInfo where stuID NOT IN( select stuID from scoreInfo ) +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from StuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuInfo.stuID 学号,avg(score)平均分,count(*)选修课程数量 from stuInfo +inner join scoreInfo on stuInfo.stuID = scoreInfo.stuID group by stuInfo.stuID +having count(*)>2 and avg(score)>70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..660f42953777213f2d177f2a1d26c5caf973d694 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery2.sql" @@ -0,0 +1,125 @@ +use master +go +create database wangba +on +( + name='wangba', + filename='D:\wb\wangba.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='wangba_log', + filename='D:\wb\wangba_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + + +use wangba +go +create table tbl_card +( + id varchar(25) primary key, + [passWord] varchar(30) not null, + balance money not null, + userName varchar(20) not null +) + + + +insert into tbl_card values +('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'验证'), +('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张峻') + +select * from tbl_card + +use wangba +go +create table tbl_computer +( + id varchar(20) primary key, + onUse char(2) check(onUse=0 or onUse=1), + note text +) +go + +insert into tbl_computer values('02','0','25555'), +('03','1','55555'), +('04','0','66666'), +('05','1','88888'), +('06','0','688878'), +('B01','0','558558') + +select * from tbl_computer + +use wangba +go +create table tbl_record +( + id varchar(30) primary key, + cardId varchar(25) constraint FK_tbl_record_cardId references tbl_card(id), + ComputerId varchar(20) constraint FK_tbl_record_ComputerId references tbl_computer(id), + beginTime time, + endTime time, + fee money +) +go + +insert into tbl_record(id,cardId,ComputerId,beginTime,endTime,fee) values +('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), +('34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), +('45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +('46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6'), +('47','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +('48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6'), +('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), +('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','3'), +('65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), +('98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') + +select * from tbl_card +select * from tbl_computer +select * from tbl_record + +--请完成以下题目: +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select tbl_card.id 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_card +inner join tbl_record on tbl_card.id = tbl_record.cardId +where userName='张军' order by fee desc + +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId 机器,count(*)次数,sum(fee)总金额 from tbl_record group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId 卡号,sum(fee)总金额 from tbl_record group by cardId + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select cardId 卡号,userName 用户名 from tbl_record +right join tbl_card on tbl_card.id = tbl_record.cardId where tbl_card.id not in(select cardId from tbl_record) + +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card A,tbl_card B where A.passWord = B.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 使用最多的机器号,count(*)次数 from tbl_record group by ComputerId order by count(*) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.id 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 金额 from tbl_card +inner join tbl_record on tbl_card.id = tbl_record.cardId + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\344\275\234\344\270\232\344\270\200.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\344\275\234\344\270\232\344\270\200.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c3c416d80c07f6677a34f547358d747b4ee974b3 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\344\275\234\344\270\232\344\270\200.sql" @@ -0,0 +1,113 @@ +use master +go +create database xuesheng +on +( + name='xuesheng', + filename='C:\test\xuesheng.mdf', + size=10, + maxsize=200, + filegrowth=10% + ) + log on +( + name='xuesheng_log', + filename='C:\test\xuesheng_log.ldf', + size=10, + maxsize=200, + filegrowth=10% + ) + +create table students +( + stuID int primary key identity ( 1,1 ), + stuName nvarchar ( 20 ), + stuAge int, + stuSex nvarchar ( 2 ) check ( stuSex='1' or stuSex='0' ), + time datetime2 + ) + + insert into students(stuName,stuAge,stuSex) values ('Tom','19',1) + insert into students(stuName,stuAge,stuSex) values ('Jack','20',0) + insert into students(stuName,stuAge,stuSex) values ('Rose','21',1) + insert into students(stuName,stuAge,stuSex) values ('Lulu','19',1) + insert into students(stuName,stuAge,stuSex) values ('Lili','21',0) + insert into students(stuName,stuAge,stuSex,time) values ('abc','20',1,'2007-01-07 01:11:36.590') + + select * from students + +create table course +( + courseID int primary key identity ( 1,1 ), + courseName nvarchar(20), + courseMarks nvarchar(10) + ) + + insert into course(courseName,courseMarks) values ('JavaBase',4) + insert into course(courseName,courseMarks) values ('HTML',2) + insert into course(courseName,courseMarks) values ('JavaScript',2) + insert into course(courseName,courseMarks) values ('SqlBase',2) + + select * from course + +create table score +( + scoreID int primary key identity ( 1,1 ), + stuID int references students( stuID ), + courseID nvarchar(20), + score int + +) + + insert into score(stuID,courseID,score) values ('1','1','80') + insert into score(stuID,courseID,score) values ('1','2','85') + insert into score(stuID,courseID,score) values ('1','4','50') + insert into score(stuID,courseID,score) values ('2','1','75') + insert into score(stuID,courseID,score) values ('2','3','45') + insert into score(stuID,courseID,score) values ('2','4','75') + insert into score(stuID,courseID,score) values ('3','1','45') + insert into score(stuID,courseID,score) values ('4','1','95') + insert into score(stuID,courseID,score) values ('4','2','75') + insert into score(stuID,courseID,score) values ('4','3','90') + insert into score(stuID,courseID,score) values ('4','4','45') + + select * from score + +-- 有如图所示的三张表结构,学生信息表(students),课程信息表(course),分数信息表(score) + select * from students + select * from course + select * from score + +--题目: +--1.查询出 每个学生 所选修的课程的 数量 和 所选修的课程的 考试的 平均分 + select students.stuName,count(*) as 课程数量,avg(score.score) as 平均分 from students + inner join score on students.stuID=score.stuID group by students.stuName,score.score +--2.查询出 每门课程 的选修的 学生的个数 和 学生考试的总分 + select course.courseName, count(*), sum(score.score) from course + inner join score on course.courseID=score.courseID group by score.courseID,course.courseName +--3.查询出性别一样并且年龄一样的学生的信息 + select A. * from students A,students B + where A.stuSex=B.stuSex and A.stuAge=B.stuAge and A.stuID <> B.stuID +--4.查询出 学分一样 的课程信息 + select C. * from course C,course D + where C.courseMarks=D.courseMarks and C.courseName <> D.courseName +--5.查询出 参加了考试的 学生的学号,姓名,课程号和分数 + select students.stuID,students.stuName,score.courseID,score.score from students + right join score on students.stuID=score.stuID group by students.stuID,students.stuName,score.courseID,score.score +--6.查询出 参加了考试的 学生的学号,课程号,课程名,课程学分和分数 + select score.stuID,score.courseID,course.courseName,course.courseMarks,score.score from course + right join score on score.courseID=course.courseID group by score.stuID,score.courseID,course.courseName,course.courseMarks,score.score + +--7.查询出 没有参加考试的 学生的学号 和 姓名 + select students.stuID,students.stuName,score.score from students + left join score on students.stuID=score.stuID group by students.stuID,students.stuName,score.score + having score.score is null +--8.查询出是周六周天来报到的学生 + select stuName,time from students +--9.查询出姓名中有字母a的学生的信息 + select * from students + where stuName like '%a%' +--10.查询出 选修了 2门课程以上的 并且 考试平均分在70以上的 学生的 学号 和 考试平均分 以及 选修课程的 数量 + select score.stuID,avg(score.score) as 考试平均分,count(course.courseID) as 选修课程的数量 from course + inner join score on course.courseID=score.courseID group by score.stuID,score.score,course.courseID + having count(course.courseID)>2 and avg(score.score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\344\275\234\344\270\232\344\272\214.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\344\275\234\344\270\232\344\272\214.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e628a71e9fdbbda71da04455bc3b7b77ff41e753 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\344\275\234\344\270\232\344\272\214.sql" @@ -0,0 +1,114 @@ +use master +go + +create database internet +on +( + name='internet', + filename='C:\test\internet.mdf', + size=20, + maxsize=200, + filegrowth=10% + ) +log on +( + name='internet_log', + filename='C:\test\internet_log.ldf', + size=20, + maxsize=200, + filegrowth=10% + ) +go +use internet +go +create table tbl_card +( + id varchar(30) primary key , + passWord nvarchar(20) not null, + balance money, + userName varchar(30) +) +insert into tbl_card(ID,PassWord,Balance,UserName) values('0023_ABC','555','98','张军') +insert into tbl_card(ID,PassWord,Balance,UserName) values('0025_bbd','abc','300','朱俊') +insert into tbl_card(ID,PassWord,Balance,UserName) values('0036_CCD','何柳','100','何柳') +insert into tbl_card(ID,PassWord,Balance,UserName) values('0045_YGR','0045_YGR','58','证验') +insert into tbl_card(ID,PassWord,Balance,UserName) values('0078_RJV','55885fg','600','校庆') +insert into tbl_card(ID,PassWord,Balance,UserName) values('0089_EDE','zhang','134','张峻') + +select * from tbl_card + +create table tbl_computer +( + id varchar(30) primary key, + onUse int check (onUse='1' or onUse='0'), + note ntext +) +insert into tbl_computer values ('02','0','25555') +insert into tbl_computer values ('03','1','55555') +insert into tbl_computer values ('04','0','66666') +insert into tbl_computer values ('05','1','88888') +insert into tbl_computer values ('06','0','6888878') +insert into tbl_computer values ('B01','0','558558') + +create table tbl_record +( + id varchar(30) primary key , + cardId varchar(30) references tbl_card (id), + ComputerId varchar(30) references tbl_computer(id), + beginTime datetime2, + endTime datetime2, + fee money +) + +insert into tbl_record values(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20') +insert into tbl_record values(34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23') +insert into tbl_record values(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50') +insert into tbl_record values(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6') +insert into tbl_record values(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50') +insert into tbl_record values(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6') +insert into tbl_record values(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50') +insert into tbl_record values(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','30') +insert into tbl_record values(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23') +insert into tbl_record values(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') +--请完成以下题目: +select * from tbl_card +select * from tbl_computer +select * from tbl_record +--1. 查询出 用户名为'张军' 的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + select tbl_record.ComputerId,tbl_record.cardId,tbl_card.userName,tbl_computer.id,tbl_record.beginTime,tbl_record.endTime,tbl_record.fee from tbl_record + inner join tbl_card on tbl_record.cardId=tbl_card.id + inner join tbl_computer on tbl_record.ComputerId=tbl_computer.id + where tbl_card.userName ='张军' +--2. 查询出 每台机器上 的 上网次数 和 消费的总金额 + select count(tbl_record.ComputerId) as 上网次数,sum(tbl_record.fee) as 消费的总金额 from tbl_computer + inner join tbl_record on tbl_record.ComputerId=tbl_computer.id + group by ComPuterID + +--3. 查询出 所有已经使用过的上网卡 的 消费总金额 + select tbl_card.ID 卡号,sum(tbl_record.fee) 消费总金额 from tbl_card + inner join tbl_record on tbl_card.ID=tbl_record.CardID + group by tbl_card.ID + +--4. 查询出 从未消费过的上网卡 的 卡号 和 用户名 + select * from tbl_record + right join tbl_card on tbl_record.ComputerId=tbl_card.id +--5. 将密码与用户名一样的上网卡信息查询出来 + select distinct A.* from tbl_card A , tbl_card B + where A.PassWord=B.UserName +--6. 查询出使用次数最多的机器号和使用次数 + select top 1 ComputerId 机器号 ,count(ComputerId)使用次数 from tbl_record + group by ComPuterID order by count(ComPuterID) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + select tbl_card.ID 卡号,UserName 用户名, ComPuterID 机器号,Fee 消费金额 from tbl_card + inner join tbl_record on tbl_card.ID=tbl_record.CardID + where tbl_card.ID like '_____ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select CardID 卡号,userName 用户名,ComPuterID 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record + inner join tbl_card on tbl_card.ID=tbl_record.CardID +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQueryWB.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQueryWB.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a87726d2da88cc233c142a64eb41e14f4d946ec8 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQueryWB.sql" @@ -0,0 +1,133 @@ +use master +go +create database internet +on +( + name='internet', + filename='E:\数据库文件\数据库跟目录文件\交叉连接,两个表的笛卡尔乘积--多表查询 --集合运算 union intersect except(练习)\internet.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10mb +) +log on +( + name='internet_log', + filename='E:\数据库文件\数据库跟目录文件\交叉连接,两个表的笛卡尔乘积--多表查询 --集合运算 union intersect except(练习)\internet_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10mb +) +go +use internet +--首先创建网吧计费系统的数据库,表结构和数据如2.bmp所示,表的说明如下: + +--表一为上网卡信息表(tbl_card) +--id: 卡号,主键 +--passWord:密码 +--balance:卡上的余额 +--userName:该上网卡所属的用户名 +create table tbl_card +( + Card_id char(10) primary key not null, + passWord char(20) not null, + balance float default(0) not null, + userName char(20) not null +) + + + + + + +--表2为网吧机器说明表(tbl_computer) +--id:机器编号,主键 +--onUse:该机器是否正在使用,0为未使用,1为正在使用 +--note:该机器的说明 +create table tbl_computer +( + ComputerId char(10) primary key not null, + onUse char not null, + note char(50) +) + + + + +--表3为上网记录表(tbl_record): +--id:记录编号,主键 +--cardId:本次上网的卡号,外键 +--ComputerId:本次上网记录所使用的机器号,外键 +--beginTime:本次上网记录的开始时间 +--endTime:本次上网记录的结束时间 +--fee:本次上网的费用 + +create table tbl_record +( + recordid int primary key not null, + Card_id char(10) foreign key references tbl_card(Card_id) not null, + ComputerId char(10) foreign key references tbl_computer(ComputerId) not null, + beginTime datetime null, + endTime datetime null, + fee float null +) + +--插入数据 +insert into tbl_computer(ComputerId,onUse,note)values +('02','0','25555'), +('03','1','55555'), +('04','0','66666'), +('05','1','88888'), +('06','0','688878'), +('B01','0','558558') +insert into tbl_record(recordid,Card_id,ComputerId,beginTime,endTime,fee)values +('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), +('34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), +('45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), +('46','0023_ABC','03','2007-12-22 15:26:00','2006-12-22 22:55:00','60'), +('47','0023_ABC','03','2006-12-23 15:26:00','2007-12-23 22:55:00','50'), +('48','0023_ABC','03','2006-01-06 15:26:00','2007-01-06 22:55:00','6'), +('55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), +('64','0045_YGR','04','2007-12-24 18:00:00','2006-12-24 22:00:00','30'), +('65','0025_bbd','02','2007-12-28 18:00:00','2006-12-28 22:00:00','23') +insert into tbl_card(Card_id,passWord,balance,userName)values +('0023_ABC','555','98','张军'), +('0025_bbd','abc','300','朱俊'), +('0036_CCD','河柳','100','何柳'), +('0045_YGR','0045_YGR','58','证验'), +('0078_RJV','55885fg','600','校庆'), +('0089_EDE','zhang','134','张峻') + + + +--查询三个表的数据 +select*from tbl_card +select*from tbl_computer +select*from tbl_record +--请完成以下题目: +--1. 查询出 用户名为'张军' 的上网卡 的上网记录 ,要求显示卡号 ,用户名 ,机器编号 、开始时间 、结束时间 ,和消费金额 ,并按消费金额降序排列 +select tbl_record.Card_id 卡号,userName 用户名,ComputerId 机器编号, beginTime 开始时间, endTime 结束时间,fee 消费金额 from tbl_card inner join tbl_record on tbl_card.Card_id=tbl_record.Card_id where userName='张军' order by fee desc +--2. 查询出 ‘每台机器’上的 ‘上网次数’ 和消费的 ‘总金额’ +select tbl_record.ComputerId 机器编号,count(tbl_record.ComputerId)上网次数,fee*count(tbl_record.ComputerId) 总金额 from tbl_computer inner join tbl_record on tbl_computer.ComputerId=tbl_record.ComputerId group by tbl_record.ComputerId,fee +--3. 查询出所有已经使用过的上网卡的消费总金额 +select Card_id 卡号,sum(fee)总金额 from tbl_record group by Card_id +--4. 查询出从未消费过的上网卡的卡号和用户名 +select tbl_card.Card_id 卡号,userName 用户名 from tbl_record +right join tbl_card on tbl_card.Card_id = tbl_record.Card_id where tbl_card.Card_id not in(select Card_id from tbl_record) +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card A,tbl_card B where A.passWord = B.userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 使用最多的机器号,count(*)次数 from tbl_record group by ComputerId order by count(*) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_card.Card_id 卡号,userName 用户名,ComputerId 机器号,fee 金额 from tbl_card +inner join tbl_record on tbl_card.Card_id = tbl_record.Card_id where tbl_card.Card_id like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.Card_id 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 金额 from tbl_card +inner join tbl_record on tbl_card.Card_id = tbl_record.Card_id +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_record.Card_id 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 金额 from tbl_record +inner join tbl_card on tbl_card.Card_id = tbl_record.Card_id where fee not in(select top 3 fee from tbl_record order by fee desc) + +select top 3 fee from tbl_record order by fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery\345\255\246\347\224\237.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery\345\255\246\347\224\237.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6705f9e58315a1af70529a1859415e5608ec35dd --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery\345\255\246\347\224\237.sql" @@ -0,0 +1,115 @@ +use master +go + +create database HomeWork +on +( + name='HomeWork', + filename='E:\数据库文件\数据库跟目录文件\交叉连接,两个表的笛卡尔乘积--多表查询 + --集合运算 union intersect except(练习)\HomeWork.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='HomeWork_log', + filename='E:\数据库文件\数据库跟目录文件\交叉连接,两个表的笛卡尔乘积--多表查询 + --集合运算 union intersect except(练习)\HomeWork_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use HomeWork +go +create table stuInfo +( + stuID int primary key identity(1,1), + stuName char(20) not null, + stuAge char(5) not null, + stuSex char(2) not null, + time time +) +go + +insert into stuInfo values('Tom',19,1,null),('Jack',20,0,null),('Rose',21,1,null), +('Lulu',19,1,null),('Lili',21,0,null),('abc',20,1,'2007-01-07 01:11:36.590') + +select * from stuInfo + +use HomeWork +create table courseInfo +( + courseID int primary key identity(1,1), + courseName nvarchar(15) not null, + courseMarks char(5) not null +) +go + +select * from courseInfo + +insert into courseInfo values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) + +select * from courseInfo + + +use HomeWork +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int constraint FK_scoreInfo_stuID references stuInfo(stuID), + courseID int not null, + score int not null +) +go + +insert into scoreInfo values(1,1,80),(1,2,85),(1,4,50),(2,1,75),(2,3,45), +(2,4,75),(3,1,45),(4,1,95),(4,2,75),(4,3,90),(4,4,45) + + +select * from stuInfo +select * from courseInfo +select * from scoreInfo + + +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName 学生,count(*)课程数量,avg(score)平均分 from scoreInfo +inner join stuInfo on stuInfo.stuID = scoreInfo.stuID +inner join courseInfo on scoreInfo.courseID = courseInfo.courseID group by stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 课程,count(*)学生个数,sum(score)考试总分 from scoreInfo +inner join courseInfo on scoreInfo.courseID = courseInfo.courseID group by courseName + +--3.查询出性别一样并且年龄一样的学生的信息 +select s.* from StuInfo s where (select count(*) from StuInfo where stuAge=s.stuAge and stuSex=s.stuSex)>1 + +--4.查询出学分一样的课程信息 +select c.* from courseInfo c where (select count(*) from courseInfo where courseMarks=c.courseMarks)>1 + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select StuInfo.stuID 学号,stuName 姓名, courseInfo.courseID 课程号,score 分数 from scoreInfo +inner join StuInfo on scoreInfo.stuID = StuInfo.stuID +inner join courseInfo on scoreInfo.courseID = courseInfo.courseID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select StuInfo.stuID 学号,courseInfo.courseID 课程号, courseName 课程名,courseMarks 课程学分,score 分数 from scoreInfo +inner join StuInfo on scoreInfo.stuID = StuInfo.stuID +inner join courseInfo on scoreInfo.courseID = courseInfo.courseID + +--7.查询出没有参加考试的学生的学号和姓名 +select StuInfo.stuID 学号,stuName 姓名 from scoreInfo inner join StuInfo on scoreInfo.stuID = StuInfo.stuID +where score not in(select score from scoreInfo) +--select * from 分数信息表 where 分数 not in(select 分数 from 分数信息表) + +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from StuInfo where stuName like + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/oooo...one.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/oooo...one.sql" new file mode 100644 index 0000000000000000000000000000000000000000..60f041c8b0db9df56922ed14991422b6cb4ccd32 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/oooo...one.sql" @@ -0,0 +1,96 @@ +create database Students +go + +use Students +go + +--学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + +create table stuInfo +( + stuID int primary key identity, + stuName nvarchar(20), + stuAge int, + stuSex int check(stuSex=1 or stuSex=0), + time datetime +) + +create table courseInfo +( + courseID int primary key identity, + courseNAME nvarchar(20), + couresMarks int +) + +create table scoreInfo +( + scoerID int primary key identity, + stuiD int, + courseID int, + score int +) + +insert into stuInfo values + ('Tom',19,1,null), + ('Jack',20,0,null), + ('Rose',21,1,null), + ('Lulu',19,1,null), + ('Lili',21,0,null), + ('abc',20,1,'2001-01-07 01:11:36.590') + + +insert into courseInfo values + ('JavaBase',4), + ('HTML',2), + ('JavaScript',2), + ('SqlBase',2) + + +insert into scoreInfo values + (1,1,80), + (1,2,85), + (1,4,50), + (2,1,75), + (2,3,45), + (2,4,75), + (3,1,45), + (4,1,95), + (4,2,75), + (4,3,90), + (4,4,45) +select * from scoreInfo +select * from stuInfo +select * from courseInfo + +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + select stuName 学生名字 , count(courseInfo.couresMarks) 选修的课程的数量, avg(score) 所选修的课程的考试的平均分 from stuInfo + inner join courseInfo on stuInfo.stuID=courseInfo.courseID + inner join scoreInfo on courseInfo.courseID=scoreInfo.stuID group by stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + select courseName 课程名字, count(stuiD)学生个数,sum(scoreInfo.score)学生考试的总分 from courseInfo + left join scoreInfo on courseInfo.courseID=scoreInfo.courseID group by courseInfo.courseName + +--3.查询出性别一样并且年龄一样的学生的信息 + select distinct A. * from stuInfo A,stuInfo B where A.stuAge=B.stuAge AND A.stuSex=B.stuSex AND A.stuName!=B.stuName +--4.查询出学分一样的课程信息 + select distinct A.* from courseInfo A,courseInfo B where A.couresMarks=B.couresMarks and A.courseID!=B.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + select stuInfo.stuID,stuInfo.stuName,courseInfo.courseID,scoreInfo.score from scoreInfo + inner join stuInfo on scoreInfo.stuiD= stuInfo.stuID + inner join courseInfo on scoreInfo.courseID=courseInfo.courseID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + select stuInfo.stuID,courseInfo.courseID,courseInfo.courseNAME,courseInfo.couresMarks,scoreInfo.score from scoreInfo + inner join stuInfo on scoreInfo.stuiD= stuInfo.stuID + inner join courseInfo on scoreInfo.courseID=courseInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 + select stuInfo.stuid,stuInfo.stuname from stuInfo + left join scoreInfo on stuInfo.stuid= scoreInfo .stuid where score is null +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 + select * from stuInfo where stuname like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 + select stuid ,count(*) 数量 ,avg(score) 平均分 from scoreInfo group by stuid having count(*)>=2 and avg(score)>=70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/tttt...two.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/tttt...two.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e12f90c57c9cb3fdbe3c33a94cfb0c97b1d3ee7e --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/tttt...two.sql" @@ -0,0 +1,79 @@ +use master +go +create database wangba +go +use wangba +go +create table tbl_cardcard +( + id varchar(50) primary key, + pasword varchar(50), + balance money, + userName varchar(50) +) +create table tbl_computer +( + id varchar(50) primary key, + onUse varchar(2) check(onUse=0 or onUse=1), + note ntext +) +create table tbl_record +( + id varchar(50) primary key, + cardId varchar(50) foreign key references card(id), + ComputerId varchar(50) foreign key references computer(id), + beginTime datetime, + endTime datetime, + fee money +) +insert into tbl_cardcard(ID,pasword,balance,userName) +select '0023_ABC','555','98','张军'union +select '0025_bbd','abe','300','朱俊'union +select '0036_CCD','何柳','100','何柳'union +select '0045_YGR','0045_YGR','58','证验'union +select '0078_RJV','55885fg','600','校庆'union +select '0089_EDE','张','134','张峻' + +insert into tbl_computer(id,onUse,note) +select '02','0','25555' union +select '03','1','55555' union +select '04','0','66666' union +select '05','1','88888' union +select '06','0','688878' union +select 'B01','0','558558' + +insert into tbl_record(ID,cardId,ComputerId,beginTime,endTime,fee) +select '23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20' union +select '34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23' union +select '45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6' union +select '47','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6' union +select '55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50' union +select '64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','3' union +select '65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23' union +select '98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23' + +select * from tbl_cardcard +select * from tbl_computer +select * from tbl_record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select b.id 卡号,b.userName 用户名,c.id 机器编号, a.beginTime 开始时间,a.endTime 结束时间,a.fee 消费金额 from record a inner join card b on a.cardId=b.id +inner join computer c on c.id=a.ComputerId where b.userName='张军' order by a.fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select b.id 机器,count(*)上网次数,sum(c.fee)消费的总金额 from computer b left join record c on b.id=c.ComputerId group by b.id,c.fee order by c.fee desc +--3. 查询出所有已经使用过的上网卡的消费总金额 +select b.id, sum(b.fee)消费总金额 from card a inner join record b on a.id=b.cardId group by b.id +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id,userName from card where id in (select id from record group by id ) +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from card where userName = pasword +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 max(ComputerId) 机器号,count(*)次数 from record group by ComputerId order by count(*) desc +----7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6060b29d10016389571889c900bc0baa60ad8379 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" @@ -0,0 +1,107 @@ +use master +go +--学生信息表(stuInfo), +create database Mar29 +go +use Mar29 +go +create table stuInfo +( +stuID int primary key identity(1,1), +stuName nvarchar(20) not null, +stuAge int not null, +stuSex int check(stuSex=1 or stuSex=0) not null, +time datetime +) +insert into stuInfo(stuName,stuAge,stuSex,time) +values('Tom',19,1,null), +('Jack',20,0,null), +('Rose',21,1,null), +('Lulu',19,1,null), +('Lili',21,0,null), +('abc',20,1,'2007-01-07 01:11:36.590') +select * from stuInfo + +--课程信息表(courseInfo), +create table courseInfo +( +courseID int primary key identity(1,1), +courseName nvarchar(20) not null, +courseMarks int not null +) +insert into courseInfo(courseName,courseMarks) +values('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) +select * from courseInfo +--分数信息表(scoreInfo) +create table scoreInfo +( +scoreID int primary key identity(1,1), +stuID int not null, +courseID int not null, +score int not null +) +insert into scoreInfo(stuID,courseID,score) +values(1,1,80), +(1,2,85), +(1,4,50), +(2,1,75), +(2,3,45), +(2,4,75), +(3,1,15), +(4,1,95), +(4,2,75), +(4,3,90), +(4,4,45) +select * from scoreInfo +--题目: +--select * from stuInfo +--select * from courseInfo +--select * from scoreInfo +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuID 学生,count(courseID)课程的数量,avg(score)平均分 +from scoreInfo group by stuID + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseID 课程, count(courseID)选修个数, sum(score)总分 +from scoreInfo group by courseID + +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A, stuInfo B +where A.stuSex=B.stuSex AND A.stuAge=B.stuAge AND A.stuID!=B.stuID + +--4.查询出学分一样的课程信息 +select distinct A.* from courseInfo A,courseInfo B +where A.courseMarks=B.courseMarks AND A.courseID!=B.courseID + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select SC.stuID 学号,stuName 姓名,courseID 课程号,score +from scoreInfo SC +left join stuInfo ST on SC.stuID=ST.stuID +group by SC.stuID,stuName,courseID,score + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select SC.stuID 学号, SC.courseID 课程号,CO.courseName 课程名,courseMarks 学分,SC.score 分数 +from scoreInfo SC +left join courseInfo CO on SC.courseID=CO.courseID + +--7.查询出没有参加考试的学生的学号和姓名 +select ST.stuID 学号,ST.stuName 姓名 from stuInfo ST +left join scoreInfo SC on ST.stuID=SC.stuID +where score is null + +--8.查询出是周六周天来报到的学生 +--??? + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号 +--和考试平均分以及选修课程的数量 +select stuID,count(courseID)课程的数量,avg(score)平均分 from scoreInfo +group by stuID +having avg(score)>=70 and count(courseID)>=2 + +--drop Database \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0cb6ff081549cc4e366fc46f1e9957519e7fd3ee --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery2.sql" @@ -0,0 +1,130 @@ +USE master +go +create database Mar30 +go +use Mar30 +go + +--表一为上网卡信息表(tbl_card) +create table card +( +--id: 卡号,主键 +id varchar(30) primary key , +----passWord:密码 +passWord varchar(30), +--balance:卡上的余额 +balance money, +--userName:该上网卡所属的用户名 +userName nvarchar(20) +) + +--表2为网吧机器说明表(tbl_computer) +create table computer +( +--id:机器编号,主键 +id varchar(30) primary key, +--onUse:该机器是否正在使用,0为未使用,1为正在使用 +onUse int check(onUse=0 or onUse=1), +--note:该机器的说明 +note ntext +) + +--表3为上网记录表(tbl_record): +create table record +( +--id:记录编号,主键 +id varchar(30) primary key, +--cardId:本次上网的卡号,外键 +cardId varchar(30) foreign key references card(id), +--ComputerId:本次上网记录所使用的机器号,外键 +ComputerId varchar(30) foreign key references computer(id), +--beginTime:本次上网记录的开始时间 +beginTime datetime, +--endTime:本次上网记录的结束时间 +endTime datetime, +--fee:本次上网的费用 +fee money +) + + --上网卡信息表 +insert into card(ID,password,balance,userName) +select '0023_ABC','555','98','张军'union +select '0025_bbd','abe','300','朱俊'union +select '0036_CCD','何柳','100','何柳'union +select '0045_YGR','0045_YGR','58','证验'union +select '0078_RJV','55885fg','600','校庆'union +select '0089_EDE','张','134','张峻' +--网吧机器说明表 +insert into computer(id,onUse,note) +select '02','0','25555' union +select '03','1','55555' union +select '04','0','66666' union +select '05','1','88888' union +select '06','0','688878' union +select 'B01','0','558558' +--上网记录表 +insert into record(ID,cardId,ComputerId,beginTime,endTime,fee) +select '23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20' union +select '34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23' union +select '45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6' union +select '47','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50' union +select '48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6' union +select '55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50' union +select '64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','3' union +select '65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23' union +select '98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23' + +select * from card --上网卡信息表 +select * from computer --网吧机器说明表 +select * from record --上网记录表 + +--请完成以下题目: +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select c.id 卡号, c.userName 用户名 ,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from card c inner join record r on c.id=r.cardId +where userName='张军' +order by fee desc + +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId 机器,count(cardId)上网次数,sum(fee)总金额 +from record +group by cardId,ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId,sum(record.fee) 总金额 +from record +group by cardId + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select c.id,userName +from card c +left join record r on c.id=r.cardId +where fee is null group by userName,c.id + +--5. 将密码与用户名一样的上网卡信息查询出来 +select A.* from card A,card B +where A.password=B.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号,count(ComputerId)使用次数 +from record +group by ComputerId order By count(ComputerId) +desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器号, fee 消费金额 +from record r +inner join card c on r.cardId=c.id +where r.cardId like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +--??? +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +--??? + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 cardId 卡号,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from record +order by fee +desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\344\275\234\344\270\2321.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0d35fc76f6b7177b38addbba7b94a5c0b3d2ebeb --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\344\275\234\344\270\2321.sql" @@ -0,0 +1,118 @@ +use master +go + +create database student +go + +use student +go + +drop table stuInfo +drop table courseInfo +drop table scoreInfo + +create table stuInfo +( + stuID int primary key identity(1,1), + stuName nvarchar(20) not null, + stuAge int not null, + stuSex nchar(2) default(1) check(stuSex='1' or stuSex='0') not null, + time datetime +) + +create table courseInfo +( + courseID int primary key identity(1,1), + courseName nvarchar(20) not null, + courstMarks int +) + +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int not null +) + +insert into stuInfo +values ('Tom',19,'1',NULL ),('Jack',20,'0',NULL), +('Rose',21,'1',NULL),('Lulu',19,'1',NULL), +('Lili',21,'0',NULL),('abc',20,'1','2007-01-01 01:11:36.590') + + +insert into courseInfo +values ('JavaBase',4) , ('HTML',2) , ('JavaScript',2) , ('SqlBase',2) + +select *from stuInfo + +insert into scoreInfo +select 1,1,80 union +select 1,2,85 union +select 1,4,50 union +select 2,1,75 union +select 2,3,45 union +select 2,4,75 union +select 3,1,45 union +select 4,1,95 union +select 4,2,75 union +select 4,3,90 union +select 4,4,45 + + +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + +--题目: +--1.查询出 每个学生 所选修的 课程的数量 和所 选修的课程的考试的平均分 +select * from stuInfo +select * FROM courseInfo +select * from scoreInfo + +select stuID 学生ID ,count(CO.courseID) 课程数量,avg(score)考试平均分 from scoreInfo SC +inner join courseInfo CO on SC.courseID=CO.courseID group by stuID + +--2.查询出 每门课程 的 选修的学生的个数 和 学生考试的总分 +select courseName 每门课程 , count(stuID)学生个数, sum(score)考试总分 from courseInfo +inner join scoreInfo on scoreInfo.courseID=courseInfo.courseID group by courseName + +--3.查询出 性别一样 并且 年龄一样的 学生的信息 + +select b1.* from stuInfo as b1,stuInfo as b2 +where b2.stuAge=b1.stuAge and b2.stuSex=b1.stuSex and b1.stuID!=b2.stuID + +--4.查询出学分一样的课程信息 +select * from courseInfo + +select distinct A.* from courseInfo A,courseInfo B where A.courstMarks=B.courstMarks AND A.courseID!=B.courseID + +--5.查询出参加了 考试的学生的学号 , 姓名, 课程号 和分数 +select * from courseInfo + + select stuInfo.stuID 学号,stuName 姓名,courseInfo.courseID 课程号,score 分数 from scoreInfo + inner join courseInfo on courseInfo.courseID=scoreInfo.courseID + inner join stuInfo on stuInfo.stuID=scoreInfo.stuID + + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + +select stuInfo.stuID 学号,courseInfo.courseID 课程号,courseName 课程名,courstMarks 课程学分,score 分数 from scoreInfo + inner join courseInfo on courseInfo.courseID=scoreInfo.courseID + inner join stuInfo on stuInfo.stuID=scoreInfo.stuID + +--7.查询出 没有参加考试的 学生的学号 和 姓名 +select * from stuInfo +select * from scoreInfo + +select stuInfo.stuID 学号,stuName 姓名 from stuInfo +left join scoreInfo on stuInfo.stuID=scoreInfo.stuID where score is null + +--8.查询出是周六周天来报到的学生 +--不知道啊啊啊啊 +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like 'a%' +--10.查询出选修了 2门课程以上 的并且 考试平均分在70以上的 学生的学号 和 考试平均分以及 选修课程的数量 +select * from scoreInfo + +select stuInfo.stuID 学号,avg(score)考试平均分,count(scoreInfo.courseID)选修课数量 from stuInfo +inner join scoreInfo on scoreInfo.stuID=stuInfo.stuID group by stuInfo.stuID +having avg(score) >70 and count(scoreInfo.courseID)>2 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\344\275\234\344\270\2322.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2e62622a45f0cebd3a81ee35274962f189359d30 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\344\275\234\344\270\2322.sql" @@ -0,0 +1,112 @@ +use master +go + +create database tbl +go + +use tbl +go + +create table tbl_card --表一为上网卡信息表 +( + id nvarchar(20) primary key, --id: 卡号,主键 + passWord nvarchar(20), --passWord:密码 + balance int not null, --balance:卡上的余额 + userName nvarchar(20) not null --userName:该上网卡所属的用户名) +) + +create table tbl_computer --表2为网吧机器说明表(tbl_computer) +( + id nvarchar(20) primary key, --id:机器编号,主键 + onUse nchar(2) check(onUse='0' or onUse='1'),--onUse:该机器是否正在使用,0为未使用,1为正在使用 + note text not null --note:该机器的说明 +) + +create table tbl_record --表3为上网记录表(tbl_record): +( + id int primary key not null, --id:记录编号,主键 + cardId nvarchar(20) references tbl_card(id),--cardId:本次上网的卡号,外键 + computerId nvarchar(20) references tbl_computer(id),--ComputerId:本次上网记录所使用的机器号,外键 + beginTime datetime, --beginTime:本次上网记录的开始时间 + endTime datetime, --endTime:本次上网记录的结束时间 + fee money --fee:本次上网的费用 +) + + +insert into tbl_card --表tbl_card的数据 +values('0023_ABC','555',98,'张军'), + ('0025_bbd','abe',300,'朱俊'), + ('0036_CCD','何柳',100,'何柳'), + ('0045_YGR','0045_YGR',58,'证验'), + ('0078_RJV','55885fg',600,'校庆'), + ('0089_EDE','zhang',134,'张峻') + +insert into tbl_computer --表tbl_computer的数据 +values('02','0','25555'), + ('03','1','55555'), + ('04','0','66666'), + ('05','1','88888'), + ('06','0','688878'), + ('B01','0','558558') + +insert into tbl_record --表tbl_record的数据 +values(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), + (34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'), + (45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), + (46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6'), + (47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'), + (48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6'), + (55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'), + (64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','300'), + (65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'), + (98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23') +select * from tbl_record + + + + +--首先创建网吧计费系统的数据库,表结构和数据如2.bmp所示,表的说明如下: +--请完成以下题目: +--1. 查询出用户名为 '张军' 的上网卡的 上网记录,要求 显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select C.id 卡号,userName 用户名, O.id 机器编号 ,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_card C +inner join tbl_record R on C.id=R.cardId +inner join tbl_computer O on o.id=R.computerId +where userName like '张军' order by fee desc +--2. 查询出 每台机器上 的 上网次数 和 消费的总金额 +select * from tbl_record + +select count(computerId)上网次数,sum(fee)消费总金额 from tbl_record group by computerId + +--3. 查询出 所有已经使用过 的上 网卡的 消费总金额 +select C.id 卡号,SUM(R.fee) 消费总金额 from tbl_card C +inner join tbl_record R on C.id=R.cardId GROUP BY C.id +--4. 查询出从未消费过的上网卡的 卡号 和 用户名 ???? +select id 卡号,userName 用户名 from tbl_card where id not in(select distinct cardId from tbl_record) + +--5. 将 密码 与 用户名 一样的上网卡信息查询出来 +select distinct A.* from tbl_card A , tbl_card B WHERE A.passWord=B.userName + + +--6. 查询出使用次数最多的 机器号 和 使用次数 +select * from tbl_record +select top 1 computerId 机器号 ,count(computerId)使用次数 from tbl_record +group by computerId order by count(computerId) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select C.id 卡号,userName 用户名, computerId 机器号,fee 消费金额 from tbl_card C +inner join tbl_record R on C.id=R.cardId where C.id like '_____ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId 卡号,userName 用户名,computerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record +inner join tbl_card on tbl_card.id=tbl_record.cardId + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的 卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId 卡号,userName 用户名,computerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record +inner join tbl_card on tbl_card.id=tbl_record.cardId + + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的 卡号, 用户名, 机器号 ,开始时间、 结束时间 和 消费金额 +select top 3 cardId 卡号,userName 用户名,computerId 机器号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record +inner join tbl_card on tbl_card.id=tbl_record.cardId order by fee desc + + + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/.keep" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4f297d8df4d22f36c6acf489462cfba84fbef3e5 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,44 @@ +use master +go +create database StarManagerDB +go + +use StarManagerDB +create table StarType +( + T_NO int primary key identity(1,1), + T_Naem varchar(20) +) +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int not null foreign key references StarType(T_NO) +) +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') + + +insert into StarInfo values +('梅西',30 ,'射门', '阿根廷',1), +('科比', 35 ,'过人', '美国',1), +('蔡景现',40 ,'敲代码', '中国',2), +('马斯克',36 ,'造火箭', '外星人',2), +('郭德纲', 50 ,'说相声', '中国',3), +('黄铮', 41 ,'拼多多', '中国',2) + +select*from StarType +select*from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 信息 from StarInfo order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 明星类别编号,count(S_T_NO)明星人数,avg(S_AGE) 明星平均年龄 from StarInfo group by S_T_NO + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarType join StarInfo on StarInfo.S_T_NO=StarType.T_NO where T_Naem=('体育明星') order by S_AGE desc diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4bd6571e2668e169c1269452feb44701a6203724 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" @@ -0,0 +1,67 @@ +use master +go +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + filegrowth=1, + maxsize=50 +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + filegrowth=1, + maxsize=50 +) +go +use HOUSE_DB +create table HOUSE_TYPE +( + type_id int not null primary key identity (1,1), + type_name varchar(50) not null unique, + +) + +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null , + house_price float not null default(0), + type_id int not null foreign key references HOUSE_TYPE(type_id) +) +select*from HOUSE_TYPE +select*from HOUSE + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE_TYPE values +('小户型'), +('经济型'), +('别墅') + +insert into HOUSE values +('建发玺院',20000 , 3 ), +('益阳碧桂园',15500 , 3 ), +('经济房小区',600 ,2 ), +('温馨公寓',1000 ,2 ), +('顺风出租屋', 400 ,1 ), +('负电荷旅馆',600 , 1 ), +('凯旋国际',10000 ,3 ), +('爱情公寓',1300 , 2 ) + +--4、添加查询 +--查询所有房屋信息 +select*from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE join HOUSE_TYPE on HOUSE_TYPE.type_id=HOUSE.type_id where type_name like '%型' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name from HOUSE join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_name ,house_price 最高租金 from HOUSE order by house_price desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery\345\211\21510\351\242\230.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery\345\211\21510\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..94b8e5518e8fd206010f0bde6f3f9f831b91435e --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery\345\211\21510\351\242\230.sql" @@ -0,0 +1,316 @@ + + + +use master +create database ClassicDb +on +( + name='ClassicDb', + filename='E:\数据库文件\数据库跟目录文件\复杂的多表查询\ClassicDb.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='ClassicDb_log', + filename='E:\数据库文件\数据库跟目录文件\复杂的多表查询\ClassicDb_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + + +select*from CourseInfo +select*from StudentCourseScore +select*from StudentInfo +select*from Teachers + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + + + +select C.*,A.CourseId 数学,A.Score 数学成绩,B.CourseId 语文,B.Score 语文成绩 + from StudentCourseScore A,StudentCourseScore B,StudentInfo C + where A.CourseId=2 and B.CourseId=1 AND A.Score>B.Score AND C.Id=A.StudentId + AND A.StudentId = B.StudentId--方法一 + + +select C.*,A.Score,B.Score from StudentInfo C,StudentCourseScore A,StudentCourseScore B +where A.CourseId=2 and B.CourseId=1 and A.StudentId=B.StudentId and A.Score>B.Score and C.Id=A.StudentId--方法二 +--select*from StudentCourseScore where CourseId in (1,2) + + +--select * from +--(select * from StudentCourseScore A WHERE CourseId =1) A +-- join +--(select * from StudentCourseScore B WHERE CourseId =2) B +-- ON A.StudentId=B.StudentId +-- WHERE A.Score>B.Score + +--select A.StudentId 学号,A.CourseId 数学,A.Score 成绩,B.CourseId 语文,B.Score 成绩 from +--(select * from StudentCourseScore A WHERE CourseId =1) A +-- join +--(select * from StudentCourseScore B WHERE CourseId =2) B +-- ON A.StudentId=B.StudentId +-- WHERE A.Score>B.Score + + + + + +select*from StudentCourseScore JOIN StudentInfo ON + +select*from CourseInfo where CourseName = '数学' + + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select C.*,A.CourseId 数学,A.Score 数学成绩,B.CourseId 语文,B.Score 语文成绩 + from StudentCourseScore A,StudentCourseScore B,StudentInfo C + where A.CourseId=2 and B.CourseId=1 AND C.Id=A.StudentId + AND A.StudentId = B.StudentId + + + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from (select * from StudentCourseScore where CourseId=(select id from CourseInfo where CourseName='数学')) A +left join (select * from StudentCourseScore where CourseId=(select id from CourseInfo where CourseName='语文')) B +on A.StudentId=B.StudentId + + + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from (select * from StudentCourseScore where CourseId=(select id from CourseInfo where CourseName='语文'))A +left join (select * from StudentCourseScore where CourseId=(select id from CourseInfo where CourseName='数学'))B +on A.StudentId=B.StudentId where B.CourseId is null + + + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId,StudentName,avg(Score)平均分 from StudentCourseScore A +inner join StudentInfo B on A.StudentId=B.Id group by StudentId,StudentName +having avg(Score)>=60 + + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentInfo where StudentCode in (select StudentId from StudentCourseScore where score is not null) + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select distinct StudentCode,StudentName,count(CourseId)选课总数,sum(score)总成绩 from StudentInfo A +left join StudentCourseScore B on A.StudentCode=B.StudentId group by StudentCode,StudentName + + + +-- 4.1 查有成绩的学生信息 +select * from StudentInfo where StudentCode in (select StudentId from StudentCourseScore where Score is not null) + + +-- 5.查询「李」姓老师的数量 +select count(Teachers.TeacherName)姓李老师数量 from Teachers where TeacherName like('李%') + + +-- 6.查询学过「张三」老师授课的同学的信息 +select * from StudentInfo where StudentCode in (select StudentId from StudentCourseScore where CourseId!=1) + + +-- 7.查询没有学全所有课程的同学的信息 +select A.Id,StudentCode,StudentName,Birthday,Sex,ClassId,count(CourseId)课程数量 from StudentInfo A +inner join StudentCourseScore B on A.StudentCode = B.StudentId group by A.Id,StudentCode,StudentName,Birthday,Sex,ClassId +having count(CourseId)<3 + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery\345\225\206\345\223\201.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery\345\225\206\345\223\201.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4412d52440ed8b355e3c81ebbf4599f2814b1fb7 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery\345\225\206\345\223\201.sql" @@ -0,0 +1,51 @@ +use master +go +create database GoodsDB +go + +use GoodsDB + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney float not null, + Type_ID int not null foreign key references GoodsType(TypeID) +) +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') + +insert into GoodsInfo values +( '提花小西装','红色','菲曼琪',300 ,1 ), +( ' 百搭短裤','绿色','哥弟',100 ,1 ), +( '无袖背心','白色','阿依莲',700 ,1 ), +( '低帮休闲鞋','红色','菲曼琪',900 ,2 ), +( '中跟单鞋','绿色','哥弟',400 ,2 ), +( '平底鞋','白色','阿依莲',200 ,2 ), +( '迷你照相机','红色','尼康',500 ,3 ), +( ' 硬盘','黑色','希捷',600 ,3 ), +( '显卡','黑色','技嘉',800 ,3 ) +select *from GoodsType +select *from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select top 1 GoodsName 商品名称,GoodsColor 商品颜色, GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc + + + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select Type_ID 商品编号,max (GoodsMoney)最高价格,min(GoodsMoney) 最低价格, avg(GoodsMoney) 平均价格 from GoodsInfo group by Type_ID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select*from GoodsInfo where GoodsColor=('红色') and GoodsMoney>300 and GoodsMoney<600 diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/chenxuqing/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/chenxuqing/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9108c950d4c466a338bfbd822816f4095c0f8ac0 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/chenxuqing/SQLQuery2.sql" @@ -0,0 +1,48 @@ +use master +go +create database StarManagerDB +go +use StarManagerDB +go +create table StarType +( + T_NO int identity(1,1) primary key, + T_NAME nvarchar(20) +) +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') +select*from StarType +create table StarInfo +( + S_NO int identity(1,1) primary key, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + +select*from StarType +select*from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 A.S_NAME 姓名,A.S_HOBBY 特技,A.S_NATIVE 籍贯 from StarInfo A +ORDER BY A.S_AGE DESC + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select A.T_NO 明星类型编号,count(B.S_NO)明星人数,avg(B.S_AGE) 明星平均年龄 FROM StarType A INNER JOIN StarInfo B ON A.T_NO=B.S_T_NO +GROUP BY A.T_NO HAVING COUNT(B.S_NO)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 B.S_NAME 姓名,B.S_HOBBY 特技,B.S_NATIVE 籍贯 +from StarType A join StarInfo B on A.T_NO=B.S_T_NO +where A.T_NAME='体育明星' order by B.S_AGE DESC \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/chenxuqing/SQLQuery4.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/chenxuqing/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d81e1376485ff555ece8be2ba59aeb69ee71fdb5 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/chenxuqing/SQLQuery4.sql" @@ -0,0 +1,43 @@ +use master +go +create database HOUSE_DB +go +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int identity(1,1) primary key, + type_name varchar(20) unique not null +) +insert into HOUSE_TYPE values +('小户型'), +('经济型'), +('别墅') +select*from HOUSE_TYPE +create table HOUSE +( + house_id int identity(1,1) primary key, + house_name varchar(50) not null, + house_price float not null default('房租'), + type_id int not null references HOUSE_TYPE +) +insert into HOUSE values +('温馨家园',1500,1), +('星海洋房',3000,2), +('复式楼',5000,3) +select*from HOUSE + +--查询所有房屋信息 +select* from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select type_name from HOUSE_TYPE where type_name like '%型' + +--查询出 房屋的名称 和租金,并且按照租金降序排序 +select house_name 房屋名称,house_price 租金 from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name ,type_name 房屋类型 from HOUSE inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price 月租最高 ,house_name 房屋名称 from HOUSE order by house_price desc diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..54341e3908deefc466e2618fd33c57933d1a4883 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" @@ -0,0 +1,202 @@ + create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore use ClassicDb select * from StudentInfo --学生信息表 select * from Teachers --老师 select * from CourseInfo --课程信息表 select * from StudentCourseScore --学生成绩表 -- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 select C.*,B.Score 数学成绩, A.Score 语文成绩 from StudentCourseScore A , StudentCourseScore B ,StudentInfo C where A.CourseId=1 and B.CourseId=2 and A.StudentId=B.StudentId and A.Score=60 -- 3.查询在 成绩 表存在 成绩的 学生信息 select distinct * from StudentInfo INNER JOIN StudentCourseScore on StudentCourseScore.Id=StudentInfo.Id -- 4.查询 所有同学的 学生编号 、学生姓名、 选课总数、 所有课程的总成绩 (没成绩的显示为 null ) select StudentInfo.StudentCode, StudentName 学生姓名, count(CourseId) 选课总数,sum(score) 所有课程的总成绩 from StudentInfo left join StudentCourseScore on StudentCourseScore.StudentId=StudentInfo.Id group by StudentInfo.StudentCode , StudentName order by StudentCode DESC -- 4.1 查有成绩的学生信息 select * from StudentInfo inner join StudentCourseScore on StudentCourseScore.Id=StudentInfo.Id -- 5.查询「李」姓老师的数量 select count(*)李姓老师的数量 from (select * from Teachers where TeacherName like '李%') A -- 6.查询学过「张三」老师授课的同学的信息 select * from (select * from Teachers where TeacherName='张三') A inner join StudentCourseScore on A.Id=StudentCourseScore.CourseId -- 7.查询没有学全所有课程的同学的信息 select StudentId from StudentCourseScore group by StudentId having count(CourseId)<3 -- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select StudentId from StudentCourseScore A where StudentId!='01' and CourseId + in (select CourseId from StudentCourseScore where StudentId='01') group by StudentId -- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 -- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 -- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 -- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 -- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 -- 14.查询各科成绩最高分、最低分和平均分: -- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 /* 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 */ -- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 -- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 -- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 -- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 -- 18.查询各科成绩前三名的记录 -- 19.查询每门课程被选修的学生数 -- 20.查询出只选修两门课程的学生学号和姓名 -- 21.查询男生、女生人数 -- 22.查询名字中含有「风」字的学生信息 -- 23.查询同名同性学生名单,并统计同名人数 -- 24.查询 1990 年出生的学生名单 -- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 -- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 -- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 -- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) -- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 -- 30.查询不及格的课程 -- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 -- 32.求每门课程的学生人数 -- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 --34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 -- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 -- 36.查询每门功成绩最好的前两名 -- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 -- 38.检索至少选修两门课程的学生学号 -- 39.查询选修了全部课程的学生信息 -- 40.查询各学生的年龄,只按年份来算 -- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 -- 42.查询本周过生日的学生 -- 43.查询下周过生日的学生 -- 44.查询本月过生日的学生 -- 45.查询下月过生日的学生 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\345\244\215\344\271\240\351\242\2302\344\273\243\347\240\201.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\345\244\215\344\271\240\351\242\2302\344\273\243\347\240\201.sql" new file mode 100644 index 0000000000000000000000000000000000000000..955749442f509321bc666b7560305a4b82f2ef52 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\345\244\215\344\271\240\351\242\2302\344\273\243\347\240\201.sql" @@ -0,0 +1,80 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +log on +( + name='HOUSE_DB_ldf', + filename='D:\HOUSE_DB_ldf.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) + +use HOUSE_DB + +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 +create table HOUSE_TYPE +( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null unique +) + + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null , + house_price float not null default(0), + tyoe_id int not null foreign key references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') + +insert into HOUSE(house_name,house_price,tyoe_id) values ('A',5000,1) , ('B',6000,2) ,('C',8000,3) + +SELECT * FROM HOUSE_TYPE + +--4、添加查询 +--查询所有房屋信息 + SELECT * FROM HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +SELECT * FROM HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 名称, house_price 租金 from HOUSE order by house_price DESC + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE_TYPE +inner join HOUSE on HOUSE_TYPE.type_id =HOUSE.tyoe_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select Top 1 house_price 最高的租金, house_name 房屋名称 from HOUSE order by house_price DESC + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\345\244\215\344\271\240\351\242\2303\344\273\243\347\240\201.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\345\244\215\344\271\240\351\242\2303\344\273\243\347\240\201.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a11717ea467602c0a9d113ca65a998336a2b1d00 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\345\244\215\344\271\240\351\242\2303\344\273\243\347\240\201.sql" @@ -0,0 +1,73 @@ +--1、创建明星数据库(StarManagerDB) +use master +go + +create database StarManagerDB +go + +use StarManagerDB +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NatIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) + + + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 +insert into StarType values ('体育明星'),('IT明星'),('相声明星') + +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NatIVE,S_T_NO) +values ('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景观',40,'敲代码','中国',2), +('马斯克',36,'外星人','中国',2),('郭德纲',50,'相声','中国',3),('黄峥',41,'拼多多','中国',2) + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + + +select * from StarType +select * from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select TOP 3 S_AGE 年龄 ,S_NAME 姓名,S_HOBBY 特技, S_NatIVE 籍贯 FROM StarInfo order by S_AGE DESC + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +SELECT S_T_NO 类型编号 ,COUNT(*) 明星人数 ,AVG(S_AGE)明星平均年龄 FROM StarInfo GROUP BY S_T_NO HAVING COUNT(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +SELECT TOP 1 S_AGE 年龄 ,S_NAME 姓名, S_HOBBY 特技 ,S_NatIVE 籍贯 FROM StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO +where T_NAME='体育明星' order by S_AGE DESC \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-\345\211\21510\351\242\230.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-\345\211\21510\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..65b73d4fe386d9bd07e13dab10db84d5c645a33b --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-\345\211\21510\351\242\230.sql" @@ -0,0 +1,270 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + +go + + + +-- 练习题目: +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select C.*,A.CourseId 数学,A.Score 数学成绩,B.CourseId 语文,B.Score 语文成绩 + from StudentCourseScore A,StudentCourseScore B,StudentInfo C + where A.CourseId=2 and B.CourseId=1 AND A.Score>B.Score AND C.Id=A.StudentId + AND A.StudentId = B.StudentId + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select C.*,A.CourseId 数学,A.Score 数学成绩,B.CourseId 语文,B.Score 语文成绩 + from StudentCourseScore A,StudentCourseScore B,StudentInfo C + where A.CourseId=2 and B.CourseId=1 AND C.Id=A.StudentId + AND A.StudentId = B.StudentId + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from (select * from StudentCourseScore where CourseId=(select id from CourseInfo where CourseName='数学')) A +left join (select * from StudentCourseScore where CourseId=(select id from CourseInfo where CourseName='语文')) B +on A.StudentId=B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from (select * from StudentCourseScore where CourseId=(select id from CourseInfo where CourseName='语文'))A +left join (select * from StudentCourseScore where CourseId=(select id from CourseInfo where CourseName='数学'))B +on A.StudentId=B.StudentId where B.CourseId is null + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId,StudentName,avg(Score)平均分 from StudentCourseScore A +inner join StudentInfo B on A.StudentId=B.Id group by StudentId,StudentName +having avg(Score)>=60 + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentInfo where StudentCode in (select StudentId from StudentCourseScore where score is not null) + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select distinct StudentCode,StudentName,count(CourseId)选课总数,sum(score)总成绩 from StudentInfo A +left join StudentCourseScore B on A.StudentCode=B.StudentId group by StudentCode,StudentName + +-- 4.1 查有成绩的学生信息 +select * from StudentInfo where StudentCode in (select StudentId from StudentCourseScore where Score is not null) + + +-- 5.查询「李」姓老师的数量 +select count(TeacherName)李姓老师的数量 from Teachers where TeacherName like '李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +select * from StudentInfo where StudentCode in (select StudentId from StudentCourseScore where CourseId!=1) + +-- 7.查询没有学全所有课程的同学的信息 +select A.Id,StudentCode,StudentName,Birthday,Sex,ClassId,count(CourseId)课程数量 from StudentInfo A +inner join StudentCourseScore B on A.StudentCode = B.StudentId group by A.Id,StudentCode,StudentName,Birthday,Sex,ClassId +having count(CourseId)<3 + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select * from StudentInfo where StudentCode in (select B.StudentId from StudentCourseScore A,StudentCourseScore B +where A.StudentId=1 and B.StudentId!=1 and A.CourseId=B.CourseId) + + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select * from StudentInfo where StudentCode in +(select B.StudentId from StudentCourseScore A cross join StudentCourseScore B +where A.StudentId=1 and B.StudentId!=1 group by B.StudentId having count(A.CourseId)=count(B.CourseId)) + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select StudentName from StudentInfo where StudentCode not in +(select StudentId from StudentCourseScore where CourseId in +(select Id from Teachers where TeacherName = '张三')) + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-\345\244\215\344\271\240\351\242\2302.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-\345\244\215\344\271\240\351\242\2302.sql" new file mode 100644 index 0000000000000000000000000000000000000000..10e10014ba15480996cf49d5c7d0c9e6f6d649b6 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-\345\244\215\344\271\240\351\242\2302.sql" @@ -0,0 +1,71 @@ +use master +go +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\SQL作业\SQL作业13\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) + +log on +( + name='HOUSE_DB_log', + filename='D:\SQL作业\SQL作业13\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +go + +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique(type_name) not null +) +go + + +use HOUSE_DB +go +create table HOUSE +( + HOUSE int primary key identity(1,1) not null, + house_name varchar(50) not null, + house_price float default(0) not null, + type_id int constraint FK_HOUSE_type_id references HOUSE_TYPE(type_id) +) +go + + +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') + +insert into HOUSE values('---','500','1'),('***','800','2'),('青青草原','1000','3') + +select * from HOUSE_TYPE +select * from HOUSE + + +--添加查询 +--查询所有房屋信息 +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋名称,house_price 租金 from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE +inner join HOUSE_TYPE on HOUSE.type_id = HOUSE_TYPE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_name 房屋名称,house_price 租金 from HOUSE where house_price in +(select top 1 house_price from HOUSE order by house_price desc) + +select top 1 house_price from HOUSE order by house_price desc diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-\345\244\215\344\271\240\351\242\2303.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-\345\244\215\344\271\240\351\242\2303.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e917a5a4480f076e44b84371b24df838a6a966f3 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-\345\244\215\344\271\240\351\242\2303.sql" @@ -0,0 +1,65 @@ +use master +go +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\SQL作业\SQL作业13\StarManagerDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='StarManagerDB_log', + filename='D:\SQL作业\SQL作业13\StarManagerDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20) +) +go + +use StarManagerDB +go +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int constraint FK_StarInfo_S_NATIVE references StarType(T_NO) +) +go + +insert into StarType values('体育明星'),('IT明星'),('相声演员') + +insert into StarInfo values('梅西','30','射门','阿根廷','1'),('科比','35','过人','美国','1'), +('蔡景现','40','敲代码','中国','2'),('马斯克','36','造火箭','外星人','2'), +('郭德纲','50','相声','中国','3'),('黄铮','41','拼多多','中国','2') + + +select * from StarInfo +select * from StarType + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_AGE from StarInfo order by S_AGE desc +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_AGE in (select top 3 S_AGE from StarInfo order by S_AGE desc) + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 编号,count(*) 人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select T_NAME 类型,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo +inner join StarType on StarInfo.S_T_NO = StarType.T_NO +where S_AGE in (select top 1 S_AGE from StarInfo inner join StarType on StarInfo.S_T_NO = StarType.T_NO where T_NAME='体育明星' order by S_AGE desc) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\344\272\224\345\215\201\351\242\230.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\344\272\224\345\215\201\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..25339a2b2687723d44c4e59bd52ee93fb89339f4 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\344\272\224\345\215\201\351\242\230.sql" @@ -0,0 +1,238 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select C.*,A.Score 数学成绩,B.Score 语文成绩 from StudentCourseScore A,StudentCourseScore B,StudentInfo C +where A.CourseId=2 and B.CourseId=1 AND A.StudentId=B.StudentId AND A.Score>B.Score AND C.Id=A.StudentId + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from StudentCourseScore A,StudentCourseScore B +where A.CourseId=2 and B.CourseId=1 AND A.StudentId=B.StudentId + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from +(select * from StudentCourseScore A where CourseId=(select ID from CourseInfo where CourseName='数学') ) A left join +(select * from StudentCourseScore A where CourseId=(select ID from CourseInfo where CourseName='语文') ) B on A.StudentId=B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from StudentCourseScore +where CourseId=2 + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId 学生编号,StudentInfo.StudentName 学生姓名,AVG(Score) 平均分 from StudentCourseScore +inner join StudentInfo on StudentInfo.Id=StudentCourseScore.StudentId group by StudentId ,StudentInfo.StudentName + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentCourseScore +inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id + +---- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentInfo.Id 学生编号,StudentName 学生姓名,count( StudentCourseScore.StudentId)选课总数,sum(StudentCourseScore.Score) 总成绩 from StudentInfo +left join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +group by StudentInfo.Id,StudentName,StudentCourseScore.StudentId + +---- 4.1 查有成绩的学生信息 +select distinct StudentInfo.* from StudentInfo +inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId + +-- 5.查询「李」姓老师的数量 +select COUNT(*)数量 from Teachers where TeacherName like '李%' + +---6.查询学过「张三」老师授课的同学的信息 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\270\200.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\270\200.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3ee4578e181c29bfebb36fdd8787f710fc4e21a5 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\270\200.sql" @@ -0,0 +1,34 @@ +create database GoodsDB +go +use GoodsDB +go +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null, +) + +insert into GoodsType(TypeName) values('服装内衣'),('鞋包配饰'),('手机数码') +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID), +) +select * from GoodsType +select * from GoodsInfo +insert into GoodsInfo values('提花小西装','红色','菲曼琪','300',1),('百搭短裤', '绿色', '哥弟', '100', 1),('无袖背心' ,'白色' ,'阿依莲' ,'700' ,1), +('低帮休闲鞋',' 红色 ','菲曼琪',' 900', 2),('中跟单鞋',' 绿色',' 哥弟' ,'400', 2),('平底鞋',' 白色',' 阿依莲',' 200', 2),('迷你照相机', '红色',' 尼康', '500', 3), +('硬盘',' 黑色', '希捷', '600', 3),('显卡' ,'黑色',' 技嘉', '800', 3) +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select TypeID,MAX(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' or GoodsMoney=300 and GoodsMoney=600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\270\211.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\270\211.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7b3b08dac2ccbcdfe4a752a1779413b359e69c67 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\270\211.sql" @@ -0,0 +1,36 @@ +create database StarManagerDB +go +use StarManagerDB +go + +--明星类型表 +create table StarType +( + T_NO int not null identity(1,1) primary key, + T_NAME nvarchar(20), +) + +--明星信息表 +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO), +) +select * from StarType +select * from StarInfo +insert into StarType values ('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values ('梅西',30,'射门','阿根廷',1), ('科比',35,'过人','美国',1), ('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铎',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_AGE,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 类型编号,count(*)明星人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_AGE,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_T_NO=1 order by S_AGE desc diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\272\214.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\272\214.sql" new file mode 100644 index 0000000000000000000000000000000000000000..535ef045813e7896b188013d646bb20bf8456068 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\272\214.sql" @@ -0,0 +1,58 @@ +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='C:\text\HOUSE_DB.maf', + size=5, + maxsize=50, + filegrowth=1% +) +log on +( + name='HOUSE_DB_log', + filename='C:\text\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1% +) +go +use HOUSE_DB +go + +--房屋类型表 +create table HOUSE_TYPE +( + type_id int not null primary key identity, + type_name varchar(50) unique, +) + +--房屋信息表 +create table HOUSE +( + house_id int not null primary key identity, + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') + +insert into HOUSE values ('爱丽小屋',10000000,1),('毛坯房',100,2),('黄子韬',100000,3) + +--查询所有房屋信息 +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 出租屋名称,house_price 出租租金 from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,HOUSE_TYPE.type_name 房屋类型 from HOUSE inner join HOUSE_TYPE on +HOUSE.type_id=HOUSE_TYPE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + --order by + top +select top 1 house_price 房屋租金,house_name 房屋名称 from HOUSE order by house_price desc + +--子查询 + max diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/50\351\242\230.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/50\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7fed0611c9382519c7f45d2758645b89442458af --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/50\351\242\230.sql" @@ -0,0 +1,417 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers + + + +-- 1.查询 数学 课程比 语文 课程成绩高的学生的信息及课程分数 +select C.*,A.Score 数学成绩,b.Score 语文成绩 from StudentCourseScore A,StudentCourseScore B,StudentInfo C +where A.CourseId=2 and B.CourseId=1 and A.StudentId=B.StudentId and A.Score>B.Score and A.StudentId=C.Id + +-- 1.1 查询 同时存在 数学 课程和 语文 课程的情况 +select * from StudentInfo where Id in (select A.StudentId from StudentCourseScore A,StudentCourseScore B +where A.CourseId=(select Id from CourseInfo where CourseName='数学') and +B.CourseId=(select Id from CourseInfo where CourseName='语文') AND A.StudentId=B.StudentId ) + +-- 1.2 查询存在 数学 课程但可能不存在 语文 课程的情况(不存在时显示为 null ) +select * from (select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId + +-- 1.3 查询不存在 数学 课程但存在 语文 课程的情况 +select * from (select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A right join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId + +select * from StudentCourseScore +-- 2.查询平均成绩大于等于 60 分 的同学的 学生编号 和 学生姓名 和 平均成绩 +select A.Id,StudentName,AVG(Score)平均成绩 from StudentInfo A join StudentCourseScore B on A.Id=B.StudentId group by +A.Id,StudentName having AVG(Score)>=60 + + +-- 3.查询在 成绩表 存在成绩的学生信息 +select B.Id,StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A join StudentInfo B on +A.StudentId=B.Id group by B.Id,StudentCode,StudentName,Birthday,Sex,ClassId + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select A.Id,StudentName,COUNT(CourseId)选课总数,SUM(Score)总成绩 from StudentInfo A left join StudentCourseScore B on A.Id=B.StudentId +group by A.Id,StudentName + +-- 4.1 查有成绩的学生信息 +select B.Id,StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A join StudentInfo B on +A.StudentId=B.Id group by B.Id,StudentCode,StudentName,Birthday,Sex,ClassId + +-- 5.查询「李」姓老师的数量 +select count(TeacherName)数量 from Teachers where TeacherName like '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 +select C.* from CourseInfo A,Teachers B,StudentInfo C,StudentCourseScore D where A.TeacherId=B.Id AND C.Id=D.StudentId AND CourseId=A.Id AND TeacherName='张三' + +-- 7.查询 没有 学全所有课程 的同学的信息 +select B.Id,StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id group by +B.Id,StudentCode,StudentName,Birthday,Sex,ClassId +except +select D.* from StudentCourseScore A,StudentCourseScore B,StudentCourseScore C,StudentInfo D where A.CourseId=1 and B.CourseId=2 and C.CourseId=3 +and A.StudentId=B.StudentId AND B.StudentId=C.StudentId AND A.StudentId=D.Id + +-- 8.查询 至少有一门课 与学号为 01 的同学所学相同的同学的信息 +select B.Id,StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id group by +B.Id,StudentCode,StudentName,Birthday,Sex,ClassId +except +select D.* from StudentCourseScore A,StudentCourseScore B,StudentCourseScore C,StudentInfo D where A.CourseId=1 and B.CourseId=2 and C.CourseId=3 +and A.StudentId=B.StudentId AND B.StudentId=C.StudentId AND A.StudentId=D.Id AND D.StudentCode='01' + +-- 9.查询 和 01 号的同学学习的课程 完全相同的其他同学的信息 +select D.* from StudentCourseScore A,StudentCourseScore B,StudentCourseScore C,StudentInfo D where A.CourseId=1 and B.CourseId=2 and C.CourseId=3 +and A.StudentId=B.StudentId AND B.StudentId=C.StudentId AND A.StudentId=D.Id +except +select D.* from StudentCourseScore A,StudentCourseScore B,StudentCourseScore C,StudentInfo D where A.CourseId=1 and B.CourseId=2 and C.CourseId=3 +and A.StudentId=B.StudentId AND B.StudentId=C.StudentId AND A.StudentId=D.Id AND D.StudentCode='01' + + +-- 10.查询 没学过 张三老师讲授的任一门课程的学生姓名 +select C.StudentName from CourseInfo A,Teachers B,StudentInfo C,StudentCourseScore D where A.TeacherId=B.Id AND C.Id=D.StudentId AND CourseId=A.Id +group by C.StudentName +except +select C.StudentName from CourseInfo A,Teachers B,StudentInfo C,StudentCourseScore D where A.TeacherId=B.Id AND C.Id=D.StudentId AND CourseId=A.Id +AND TeacherName='张三' + + +-- 11.查询 两门及其以上 不及格课程的同学的 学号,姓名 及其 平均成绩 +select StudentCode,StudentName,AVG(Score)平均成绩 from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id where Score<60 group by +StudentCode,StudentName having COUNT(CourseId)>=2 + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 +select C.*,Score from StudentCourseScore A join CourseInfo B on A.CourseId=B.Id join StudentInfo C on A.StudentId=C.Id where CourseName='数学' and Score<60 +order by Score DESC + +-- 13.按平均成绩从高到低 显示 所有学生的 所有课程的成绩 以及 平均成绩 +select StudentId,CourseId,Score,AVG(Score)平均成绩 from StudentCourseScore group by StudentId,CourseId,Score order by AVG(Score) DESC + +-- 14.查询各科成绩最高分、最低分和平均分: +select CourseId,MAX(Score)最高分,MIN(Score)最低分,AVG(Score)平均分 from StudentCourseScore group by CourseId + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 +select StudentId,SUM(Score)总成绩 from StudentCourseScore group by StudentId order by SUM(Score) DESC + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 +select StudentId,SUM(Score)总成绩 from StudentCourseScore group by StudentId order by SUM(Score) DESC + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + +-- 18.查询各科成绩前三名的记录 +select top 3 * from StudentCourseScore A,StudentCourseScore B,StudentCourseScore C where A.CourseId='1'AND B.CourseId='2'AND C.CourseId='3' + AND A.StudentId=B.StudentId AND B.StudentId=C.StudentId order by A.Score DESC,B.Score DESC,C.Score DESC + + +-- 19.查询每门课程被选修的学生数 +select CourseId,COUNT(StudentId)学生数 from StudentCourseScore group by CourseId +select * from StudentInfo +select * from CourseInfo + + +-- 20.查询出只选修两门课程的学生学号和姓名 +select B.StudentCode,StudentName from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id group by B.StudentCode,StudentName +having COUNT(CourseId)=2 + + +-- 21.查询男生、女生人数 +select * from StudentInfo A,StudentInfo B where A.Sex='m' AND B.Sex='f'and A.StudentCode=B.StudentCode group by A.Sex,B.Sex + +-- 22.查询名字中含有「风」字的学生信息 +select * from StudentInfo where StudentName like '%风%' + +-- 23.查询同名同性学生名单,并统计同名人数 + + +-- 24.查询 1990 年出生的学生名单 +select * from StudentInfo where Birthday like '1990%' + +-- 25.查询 每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 +select CourseId,AVG(Score)平均成绩 from StudentCourseScore group by CourseId order by AVG(Score) DESC + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 +select StudentCode,StudentName,AVG(Score)平均成绩 from StudentInfo A join StudentCourseScore B on A.Id = B.StudentId +group by StudentCode,StudentName having AVG(Score)>=85 + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 +select B.StudentName,A.Score from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id join CourseInfo C on A.CourseId=C.Id +where C.CourseName='数学' AND A.Score<60 + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) +select B.Id,Score,CourseId from StudentCourseScore A right join StudentInfo B on A.StudentId=B.Id + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 +select B.StudentName,C.CourseName,A.Score from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id join CourseInfo C on +A.CourseId=C.Id where Score>70 + +-- 30.查询不及格的课程 +select CourseId from StudentCourseScore where Score<60 group by CourseId + +-- 31.查询课程编号为 1 且课程成绩在 80 分以上的学生的学号和姓名 +select StudentCode,StudentName from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id where CourseId=1 and Score>=80 + +-- 32.求每门课程的学生人数 +select CourseId,COUNT(StudentId)人数 from StudentCourseScore group by CourseId + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +select top 1 D.Id,StudentCode,StudentName,Birthday,Sex,ClassId,MAX(Score)成绩 from CourseInfo A join Teachers B on A.TeacherId=B.Id join StudentCourseScore C on A.Id=C.CourseId +join StudentInfo D on C.StudentId=D.Id where TeacherName='张三' group by D.Id,StudentCode,StudentName,Birthday,Sex,ClassId + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +select top 1 D.Id,StudentCode,StudentName,Birthday,Sex,ClassId,MAX(Score)成绩 from CourseInfo A join Teachers B on A.TeacherId=B.Id join StudentCourseScore C on A.Id=C.CourseId +join StudentInfo D on C.StudentId=D.Id where TeacherName='张三' group by D.Id,StudentCode,StudentName,Birthday,Sex,ClassId + + +-- 35.查询不同课程 成绩相同的学生的学生编号、课程编号、学生成绩 +select A.Id,A.CourseId,B.CourseId,C.CourseId,A.Score from StudentCourseScore A,StudentCourseScore B,StudentCourseScore C where A.CourseId='1'AND B.CourseId='2'AND C.CourseId='3' + AND A.StudentId=B.StudentId AND B.StudentId=C.StudentId AND A.Score=B.Score AND B.Score=C.Score + +-- 36.查询每门功成绩最好的前两名 +select top 2 * from StudentCourseScore A,StudentCourseScore B,StudentCourseScore C where A.CourseId='1'AND B.CourseId='2'AND C.CourseId='3' + AND A.StudentId=B.StudentId AND B.StudentId=C.StudentId order by A.Score DESC,B.Score DESC,C.Score DESC + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 +select CourseId,COUNT(*)选修人数 from StudentCourseScore group by CourseId having COUNT(*)>5 + +-- 38.检索至少选修两门课程的学生学号 +select B.StudentCode from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id group by B.StudentCode +having COUNT(CourseId)>=2 + +-- 39.查询选修了全部课程的学生信息 +select B.Id,StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id group by B.Id,StudentCode,StudentName,Birthday,Sex,ClassId +having COUNT(CourseId)=3 + +-- 40.查询各学生的年龄,只按年份来算 + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + +-- 42.查询本周过生日的学生 + + +-- 43.查询下周过生日的学生 + + +-- 44.查询本月过生日的学生 + + +-- 45.查询下月过生日的学生 + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\345\244\215\344\271\240\351\242\2302.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\345\244\215\344\271\240\351\242\2302.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7efb01a2a7f7a13241537bd42ec736aca212ab33 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\345\244\215\344\271\240\351\242\2302.sql" @@ -0,0 +1,79 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\数据库\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +log on +( + name='HOUSE_DB_log', + filename='D:\数据库\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) + +use HOUSE_DB +go + +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique not null +) + + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 +create table HOUSE +( + house_id int primary key identity(1,1) not null, + house_name varchar(50) not null, + house_price float default(0) not null, + type_id int foreign key references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +select * from HOUSE_TYPE order by type_id +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') +--HOUSE表中添加至少3条数据,不能全都为同一类型 +select * from HOUSE +insert into HOUSE values ('201','300','1'),('202','600','2'),('203','1000','3') +--4、添加查询 +--查询所有房屋信息 +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' + + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name from HOUSE A inner join HOUSE_TYPE B on A.type_id=B.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price,type_name from HOUSE A inner join HOUSE_TYPE B on A.type_id=B.type_id order by house_price desc + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\345\244\215\344\271\240\351\242\2303.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\345\244\215\344\271\240\351\242\2303.sql" new file mode 100644 index 0000000000000000000000000000000000000000..72518294b06cf62b1f80474550e2041afd756706 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\345\244\215\344\271\240\351\242\2303.sql" @@ -0,0 +1,89 @@ +--1、创建明星数据库(StarManagerDB),然后建立两张表,StarType(明星类型表),StarInfo(明星信息表),表结构分别如下: +use master +go + +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\数据库\StarManagerDB.mdf', + size=5mb, + maxsize=100mb, + filegrowth=5mb +) + +log on +( + name='StarManagerDB_log', + filename='D:\数据库\StarManagerDB_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=5mb +) + +use StarManagerDB +go +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 +select * from StarType +insert into StarType values ('体育明星'),('IT明星'),('相声演员') + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 +select * from StarInfo +insert into StarInfo values +('梅西','30','射门','阿根廷','1'), +('科比','35','过人','美国','1'), +('蔡景现','40','敲代码','中国','2'), +('马斯克','36','造火箭','外星人','2'), +('郭德纲','50','相声','中国','3'), +('黄铮','41','拼多多','中国','2') + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo order by S_AGE desc + +--4、按 明星类型编号 分类查询 明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 类型编号,COUNT(S_NO)人数,AVG(S_AGE)平均年龄 from StarInfo group by S_T_NO +having COUNT(S_NO)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarType inner join StarInfo on T_NO=S_T_NO where T_NAME='体育明星' +order by S_AGE desc diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1da938000b09252895cba644f81a5b664a8565d0 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" @@ -0,0 +1,51 @@ +use master +go +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb + +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb + +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique not null +) +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null, + house_price float not null default(0), + type_id int references HOUSE_TYPE not null +) +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') +insert into HOUSE values ('御龙湾',3000,2),('汤臣一品',5000,3),('近郊园',2000,1) +select * from HOUSE_TYPE +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,HOUSE_TYPE.type_name 房屋类型名称 from HOUSE +inner join HOUSE_TYPE on HOUSE_TYPE.type_id = HOUSE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_name ,house_price from HOUSE group by house_name,house_price order by house_price desc +\ No newline at end of file \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..16f99cd199f000a7a89b32649330d60365868927 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy2.sql" @@ -0,0 +1,55 @@ +use master +go +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb + +) +log on +( + name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb + +) +go +use StarManagerDB +go +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null , + S_AGE int not null , + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +insert into StarType values ('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values ('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2),('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) +--查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select * from StarType +select * from StarInfo +select top 3 s_age 年龄, S_name 姓名,S_hobby 特技,S_native 籍贯信息 from StarInfo order by S_AGE DESC +--按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select t_no 明星类型编号,t_name , count(S_T_NO) 明星人数,avg(S_age) 明星平均年龄 from StarInfo +inner join StarType on StarType.T_NO= StarInfo.S_T_NO group by t_no,t_name having count(S_T_NO)>2 + +--查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_Name 姓名, S_age 年龄,S_hobby 特技,S_native 籍贯信息,T_NAME 类型 from StarInfo +inner join StarType on StarType.T_NO= StarInfo.S_T_NO +where T_NAME='体育明星' +group by S_Name ,S_age,S_hobby ,S_native ,T_NAME +order by S_AGE desc +\ No newline at end of file \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5e0137fc8075ba3cc89379235b23ca742b72ce3d --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy3.sql" @@ -0,0 +1,37 @@ +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +go + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int constraint FK_GoodsInfo_TypeID references GoodsType(TypeID) +) +go +insert into GoodsType values ('服装内衣'),('鞋包数码'),('手机数码') +insert into GoodsInfo values('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟 ',400,2), +('平底鞋','白色','阿依莲',200,2),('迷你照相机','红色 ','尼康',500 ,3), +('硬盘','黑色','希捷',600,3),('显卡 ','黑色','技嘉',800,3) +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney = (select MAX(GoodsMoney) from GoodsInfo) +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型,MAX(GoodsMoney)最高价格,MIN(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo group by TypeID +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where (GoodsColor = '红色') and (GoodsMoney between 300 and 600) +select * from GoodsInfo where (GoodsColor = '红色') and (GoodsMoney >= 300 and GoodsMoney <= 600) +\ No newline at end of file \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy4.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1038d2023d513e9620fd5ff4cb1521bb788c3340 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy4.sql" @@ -0,0 +1,280 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers +-- 1.查询 "数学" 课程比 "语文" 课程成绩高的(同一个学生) 学生的信息 及 课程分数 +select C.*,A.score 数学课程,B.score 语文课程 from StudentCourseScore A,StudentCourseScore B,StudentInfo C +where A.CourseId = 2 and B.CourseId = 1 and A.StudentId=B.StudentId and A.Score>B.Score AND C.Id=A.StudentId +-- 1.1 查询同时存在"数学"课程和"语文"课程 的学生的情况 +select * from StudentCourseScore A +inner join StudentCourseScore B on A.StudentId= B.StudentId +inner join StudentInfo C on A.StudentId= C.Id AND A.CourseId = 1 and B.CourseId = 2 +-- 1.2 查询存在"数学"课程但可能不存在"语文"课程的情况(不存在时显示为 null ) +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId +-- 1.3 查询不存在"数学"课程但存在"语文"课程的情况 +select A.Id,A.StudentId,A.CourseId,A.Score,B.CourseId,B.Score +from (select * from StudentCourseScore a where a.CourseId=1) A +left join (select * from StudentCourseScore a where a.CourseId=2) B on A.StudentId=B.StudentId +where B.CourseId is null +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select a.StudentId 学生编号,b.StudentName 学生姓名,avg(a.Score)平均成绩 +from StudentCourseScore a +inner join StudentInfo b on a.StudentId=b.Id +group by a.StudentId,b.StudentName having avg(a.score)>60 +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentInfo where id in (select studentid from StudentCourseScore ) +-- 4.1 查有成绩的学生信息 +select * from StudentInfo where id in (select studentid from StudentCourseScore where Score is not null ) +-- 5.查询「李」姓老师的数量 +select count(TeacherName) '「李」姓老师的数量' from Teachers where TeacherName like '李%' +select * from Teachers +-- 6.查询学过「张三」老师授课的同学的信息 + +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore + +select a.* from StudentInfo a +inner join StudentCourseScore on StudentCourseScore.StudentId=a.Id +inner join CourseInfo on CourseInfo.Id=StudentCourseScore.CourseId +where CourseInfo.TeacherId =(select Id from Teachers where TeacherName ='张三') +-- 7.查询没有学全所有课程的同学的信息 + +select a.Id,a.StudentCode,a.StudentName,a.Birthday,a.Sex,a.ClassId +from StudentCourseScore b join StudentInfo a on a.Id=b.StudentId +group by a.Id,a.StudentCode,a.StudentName,a.Birthday,a.Sex,a.ClassId +having count(b.CourseId)=(select count(a.CourseName) from CourseInfo a) +union all +select * from StudentInfo +where Id not in (select distinct b.StudentId from StudentCourseScore b where b.Score is not null) + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select distinct a.Id,a.StudentCode,a.StudentName,a.Birthday,a.Sex,a.ClassId +from StudentInfo a join StudentCourseScore b on a.Id=b.StudentId +where b.CourseId in +( + select a.CourseId from StudentCourseScore a + where a.StudentId=(select Id from StudentInfo where StudentCode='01') +) +and a.StudentCode!='01' + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select a.CourseId from StudentCourseScore a where a.StudentId=(select Id from StudentInfo where StudentCode='01') + +select distinct a.Id,a.StudentCode,a.StudentName,a.Birthday,a.Sex,a.ClassId +from StudentInfo a join StudentCourseScore b on a.Id=b.StudentId +left join StudentCourseScore c on c.StudentId=b.StudentId left join StudentCourseScore d on c.StudentId=d.StudentId +where b.CourseId=1 and c.CourseId=2 and d.CourseId=3 and a.StudentCode!='01' + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select * from StudentInfo +where id not in +( + select distinct d.Id from StudentCourseScore a + join CourseInfo b on a.CourseId= b.Id + join Teachers c on b.TeacherId=c.Id + join StudentInfo d on d.Id=a.StudentId + where c.TeacherName='张三' +) +\ No newline at end of file \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5a40cfb4adf2c632640cf3a03603ee86655a67a3 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" @@ -0,0 +1,43 @@ +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\SOL.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_log', +filename='D:\SOL.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) + +use HOUSE_DB +go + +create table HOUSE_TYPE +( +type_id int primary key identity(1,1) not null, +type_name varchar(50) unique not null +) + +create table HOUSE +( +house_id int primary key identity(1,1) not null, +house_name varchar(50) not null, +house_price float default(0) not null, +type_id int foreign key references HOUSE_TYPE(type_id) not null +) + +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +insert into HOUSE values('小户型','400','1'),('经济型','1000','2'),('别墅','2000','3') + +select * from HOUSE + +select * from HOUSE where house_name like '%型%' + +select top 1 house_name,max(house_price) from HOUSE group by house_name,house_price order by max(house_price) desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..58bdf0524e90bbbeb937b3a488a6d148ff16735e --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" @@ -0,0 +1,49 @@ +create database StarManagerDB +on +( +name='StarManagerDB', +filename='D:\sql.mdf', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) +log on +( +name='StarManagerDB_log', +filename='D:\sql.ldf', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) + +use StarManagerDB +go + +create table StarType +( +T_NO int primary key identity(1,1) not null, +T_NAME nvarchar(20) +) + +create table StarInfo +( +S_NO int primary key identity(1,1) not null, +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int foreign key references StarType(T_NO) +) + +insert into StarType values('体育明星'),('IT 明星'),('相声演员') +insert into StarInfo values('梅西','30','射门','阿根廷','1'),('科比','35','过人','美国','1'), +('蔡景现','40','敲代码','中国','2'),('马斯克','36','造火箭','外星人','2'),('郭德纲','50','相声','中国','3'), +('黄铮','41','拼多多','中国','2') + +select top 3 S_NAME 姓名,S_HOBBY 年龄,S_NATIVE 籍贯 from StarInfo group by S_NAME ,S_HOBBY ,S_NATIVE +order by max(S_AGE) DESC + +select S_T_NO 编号,COUNT(*) 人数,AVG(S_AGE) 平均年龄 from StarInfo group by S_T_NO having count(*)>2 + +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo inner join StarType on StarInfo.S_T_NO=StarType.T_NO +where T_NAME = '体育明星' order by S_AGE DESC \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2ec1b36713474a53a48680365a3d1e26e36b1ece --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery3.sql" @@ -0,0 +1,417 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select C.* , A.CourseId 数学 ,A.Score 数学成绩,B.CourseId 语文 ,B.Score 语文成绩 from StudentCourseScore A,StudentCourseScore B,StudentInfo C where A.CourseId = 2 and B.CourseId=1 and A.StudentId=B.StudentId and A.Score>B.Score +and C.Id=A.StudentId + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + +select * from StudentCourseScore A ,StudentCourseScore B where A.CourseId=2 and B.CourseId=1 +and A.StudentId=B.StudentId + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from StudentCourseScore where CourseId!=1 +select * from StudentCourseScore where CourseId=2 +select * from (select * from StudentCourseScore where CourseId!=1) a left join +(select * from StudentCourseScore where CourseId=2) b on a.StudentId=b.StudentId where b.CourseId is null +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 + +select StudentId 编号,StudentName 姓名,AVG(Score) 平均成绩 from StudentCourseScore inner join StudentInfo on StudentCourseScore.Id=StudentInfo.Id +group by StudentId,StudentName having AVG(Score)>=60 + +-- 3.查询在 成绩 表存在成绩的学生信息 +select StudentId from StudentCourseScore group by StudentId +select a.* from StudentInfo a right join (select StudentId from StudentCourseScore group by StudentId) b on a.StudentCode=b.StudentId +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +-- 4.1 查有成绩的学生信息 + +-- 5.查询「李」姓老师的数量 + +select COUNT(*)数量 from Teachers where TeacherName like '%李%' + +-- 6.查询学过「张三」老师授课的同学的信息 + + +-- 7.查询没有学全所有课程的同学的信息 + + + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + + + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/50\351\242\230.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/50\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f008b87fbe96cbfe63eccb462bfcd6f822241020 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/50\351\242\230.sql" @@ -0,0 +1,420 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + + +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select C.*,A.Score 数学成绩, B.Score 语文成绩 from StudentCourseScore A,StudentCourseScore B,StudentInfo C +where A.CourseId=2 and B.CourseId=1 and A.StudentId=B.StudentId and A.Score>B.Score and C.Id=A.StudentId + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from StudentInfo AA +join (select A.StudentId from StudentCourseScore A,StudentCourseScore B where A.CourseId=2 and B.CourseId=1 AND A.StudentId=B.StudentId ) BB +ON AA.Id=BB.StudentId + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + select A.Id,A.StudentId,A.CourseId,A.score,B.CourseId,B.Score from(select * from StudentCourseScore a where a.CourseId=1) A + left join (select * from StudentCourseScore a where a.CourseId=2) B + on A.StudentId=B.StudentId + where B.CourseId is null + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentCode 编号,StudentName 姓名, avg(Score) 平均成绩 from StudentCourseScore inner join StudentInfo on StudentCourseScore.Id= StudentInfo.id group by StudentCode,StudentName +having avg(Score)>=60 + +-- 3.查询在 成绩 表存在成绩的学生信息 + + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentCode 学生编号,StudentName 姓名,count(CourseId) 选课总数,sum(Score) 总成绩 +from StudentInfo left join StudentCourseScore on StudentCourseScore.StudentId=StudentInfo.Id +group by StudentCode,StudentName + +-- 4.1 查有成绩的学生信息 + + +-- 5.查询「李」姓老师的数量 +select TeacherName, count(TeacherName) 数量 from Teachers where TeacherName like '%李%'group by TeacherName + +-- 6.查询学过「张三」老师授课的同学的信息 + + + +-- 7.查询没有学全所有课程的同学的信息 + + + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + + + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\346\210\277\345\261\213.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\346\210\277\345\261\213.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d1352edc6aab5647cdf2d4275a42691d35ffd630 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\346\210\277\345\261\213.sql" @@ -0,0 +1,67 @@ +use master +go +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\HOUSE_DB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_log', +filename='D:\HOUSE_DB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +go + +use HOUSE_DB +go + +create table HOUSE_TYPE +( +type_id int not null primary key identity(1,1), +type_name varchar(50) not null unique +) +create table HOUSE +( +house_id int not null primary key identity(1,1), +house_name varchar(50) not null, +house_price float not null default(0), +type_id int foreign key references HOUSE_TYPE(type_id) not null +) + +insert into HOUSE_TYPE values +('小户型'),('经济型'),('别墅') + +insert into HOUSE values +('小户','5000','1'), +('经济','3000','2'), +('别墅','8000','3') + + + + + +select * from HOUSE_TYPE +select * from HOUSE + + +--查询所有房屋信息 +select * from HOUSE_TYPE inner join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋名称, house_price 租金 from HOUSE order by house_price + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类别 from HOUSE inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_name 房屋名称, max(house_price) 租金 from HOUSE group by house_name diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\346\230\216\346\230\237.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\346\230\216\346\230\237.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a680ddaef5fd048f53195158592071acb7659d39 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\346\230\216\346\230\237.sql" @@ -0,0 +1,67 @@ +use master +go +create database StarManagerDB +on +( +name='StarManagerDB', +filename='D:\StarManagerDB.mdf', +size=5mb, +filegrowth=10mb, +maxsize=50mb +) +log on +( +name='StarManagerDB_log', +filename='D:\StarManagerDB_log.ldf', +size=5mb, +filegrowth=10mb, +maxsize=50mb +) +go + +use StarManagerDB +go + +create table StarType +( +T_NO int not null primary key identity(1,1), +T_NAME nvarchar(20) +) +create table StarInfo +( +S_NO int not null primary key identity(1,1), +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_HATIVE nvarchar(20) default('中国大陆'), +S_T_NO int foreign key references StarType(T_NO) +) + +insert into StarType values +('体育明星'),('IT明星'),('相声演员') + +insert into StarInfo values +('梅西','30','射门','阿根廷','1'), +('科比','35','过人','美国','1'), +('蔡景现','40','敲代码','中国','2'), +('马斯克','36','造火箭','外星人','2'), +('郭德纲','50','相声','中国','3'), +('黄铮','41','拼多多','中国','2') + +select * from StarType + +select * from StarInfo + + + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_HATIVE 籍贯,max(S_AGE) 年龄 from StarInfo group by S_NAME,S_HOBBY,S_HATIVE + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 编号, count(*) 人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(*)>2 + + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_HOBBY 特技,S_HATIVE 籍贯, max(S_AGE)年龄 from StarType inner join StarInfo on StarType.T_NO=StarInfo.S_T_NO +where T_NAME='体育明星'group by S_NAME,S_HOBBY,S_HATIVE + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..86f614ef6f22c06054496e321f340fa7d240090d --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" @@ -0,0 +1,63 @@ +use master +go +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\HOUSE_DB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_log', +filename='D:\HOUSE_DB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +go + +use HOUSE_DB +go + +create table HOUSE_TYPE +( +type_id int not null primary key identity(1,1), +type_name varchar(50) not null unique +) +create table HOUSE +( +house_id int not null primary key identity(1,1), +house_name varchar(50) not null, +house_price float not null default(0), +type_id int foreign key references HOUSE_TYPE(type_id) not null +) + +insert into HOUSE_TYPE values +('小户型'),('经济型'),('别墅') + + +insert into HOUSE values ('小户型',1000.0,1),('经济型',2000.5,2),('别墅',4000.9,3) + + + +select * from HOUSE_TYPE +select * from HOUSE + + +--查询所有房屋信息 +select * from HOUSE_TYPE +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE where house_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select b.house_name,a.type_name from HOUSE_TYPE a inner join HOUSE b on a.type_id=b.house_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price,house_name from HOUSE order by house_price desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ab531ed5cd5676845e4c395646978ee3f79ce7af --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" @@ -0,0 +1,58 @@ +select * from StudentCourseScore +select * from CourseInfo +select * from Teachers +select * from StudentInfo +-- 1.查询"数学 2"课程比" 语文 1 "课程成绩高的学生的信息及课程分数 +select a. *,c.Sex,c.Birthday,c.StudentName,c.StudentCode from StudentCourseScore a, StudentCourseScore b inner join StudentInfo c on b.StudentId=c.Id + where a.CourseId =2 and b.CourseId=1 and a.Score> b.Score and a.StudentId=b.StudentId +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + +select * from StudentCourseScore a, StudentCourseScore b,StudentInfo c where a.CourseId=2 and b.CourseId=1 and a.StudentId=b.StudentId and c.Id=a.StudentId +---- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from +(select * from StudentCourseScore a where CourseId=(select Id from CourseInfo where CourseName='数学'))a left join +(select * from StudentCourseScore a where CourseId=(select Id from CourseInfo where CourseName='语文'))b on a.StudentId=b.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select b.* from +(select * from StudentCourseScore a where CourseId=(select Id from CourseInfo where CourseName='数学'))a right join +(select * from StudentCourseScore a where CourseId=(select Id from CourseInfo where CourseName='语文'))b on a.StudentId=b.StudentId +where a.Id is null + + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select a.StudentId,b.StudentName ,avg(Score)平均成绩 from StudentCourseScore a inner join StudentInfo b on a.StudentId=b.Id group by a.StudentId,b.StudentName having avg(Score)>=60 +---- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentCourseScore a inner join StudentInfo b on a.StudentId=b.Id +--4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select b.StudentId 编号,a.StudentName 姓名,count(*)选课总数,sum(b.Score) 总成绩 +from StudentInfo a left join StudentCourseScore b on a.Id=b.StudentId group by b.StudentId ,a.StudentName +---- 4.1 查有成绩的学生信息 +select b.* from StudentCourseScore a inner join StudentInfo b on a.StudentId=b.Id + +-- 5.查询「李」姓老师的数量 + +select count(*)数量 from Teachers where TeacherName like '李%' +--6.查询学过「张三」老师授课的同学的信息 +select * from StudentInfo a inner join StudentCourseScore b on a.Id=b.StudentId +inner join Teachers c on c.Id=b.CourseId where b.CourseId=1 +-- 7.查询没有学全所有课程的同学的信息 +select * from StudentInfo where StudentCode not in (select StudentId from StudentCourseScore group by StudentId having count(*)=(select count(*) FROM CourseInfo)) + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select a.* from StudentInfo a inner join StudentCourseScore b on a.Id=b.StudentId where b.CourseId between 1 and 3 +----- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select distinct d.StudentName from CourseInfo a inner join Teachers b on a.TeacherId=b.Id inner join StudentCourseScore c on c.CourseId=a.TeacherId inner join StudentInfo d on d.Id=c.StudentId where b.TeacherName!='张三' +---- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 +select a.StudentId,b.StudentName ,avg(a.Score) from StudentCourseScore a inner join StudentInfo b on a.StudentId=b.Id where a.Score<60 group by a.StudentId,b.StudentName having count(*)>=2 + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 +select c.* from StudentCourseScore a inner join CourseInfo b on a.CourseId=b.Id join StudentInfo c on A.StudentId=C.Id where b.CourseName='数学' and a.Score<60 order by a.Score desc +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select a.StudentId,avg(a.Score) 平均成绩 ,a.Score from StudentCourseScore c,StudentCourseScore a join CourseInfo b on a.CourseId=b.Id group by a.StudentId,a.Score order by avg(a.Score) desc +---- 14.查询各科成绩最高分、最低分和平均分: +select b.CourseName,max(a.Score)最高分,min(a.Score)最低分,avg(a.Score)最低分 from StudentCourseScore a inner join CourseInfo b on a.CourseId=b.Id group by b.CourseName \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..adba88d7050d05f6254c489f3920cdebbb223a71 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery3.sql" @@ -0,0 +1,59 @@ +use master +go +create database StarManagerDB +on +( +name='StarManagerDB', +filename='D:\StarManagerDB.mdf', +size=5mb, +filegrowth=10mb, +maxsize=50mb +) +log on +( +name='StarManagerDB_log', +filename='D:\StarManagerDB_log.ldf', +size=5mb, +filegrowth=10mb, +maxsize=50mb +) +go + +use StarManagerDB +go + +create table StarType +( +T_NO int not null primary key identity(1,1), +T_NAME nvarchar(20) +) +create table StarInfo +( +S_NO int not null primary key identity(1,1), +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_HATIVE nvarchar(20) default('中国大陆'), +S_T_NO int foreign key references StarType(T_NO) +) + +insert into StarType values +('体育明星'),('IT明星'),('相声演员') + +insert into StarInfo values +('梅西','30','射门','阿根廷','1'), +('科比','35','过人','美国','1'), +('蔡景现','40','敲代码','中国','2'), +('马斯克','36','造火箭','外星人','2'), +('郭德纲','50','相声','中国','3'), +('黄铮','41','拼多多','中国','2') + + +select * from StarInfo +select * from StarType +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HATIVE 籍贯信息,S_HOBBY 特技,max(S_AGE) from StarInfo group by S_NAME,S_HATIVE,S_HOBBY +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select count(S_NO) 人数,avg(S_AGE) 平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大 的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 max(S_AGE)年龄,S_NAME 姓名,S_HOBBY 特技,S_HATIVE 籍贯信息 from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO WHERE B.T_NAME='体育明星' GROUP BY S_NAME,S_HOBBY,S_HATIVE \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/50\351\242\230.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/50\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..115e8d303e3ac8f38bf3cfab5d3bb0d7a14f877a --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/50\351\242\230.sql" @@ -0,0 +1,186 @@ +use master +go +create database ClassicDB +on +( +name='ClassicDB', +filename='C:\SQL\ClassicDB.mdf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +log on +( +name='ClassicDB_log', +filename='C:\SQL\ClassicDB_log.ldf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +go +use ClassicDB +go +create table StudentInfo +( +Id int PRIMARY key not null IDENTITY, +StudentCode nvarchar(80), +StudentName nvarchar(80), +Birthday date not null, +Sex nvarchar(2), +ClassId int not null +) +go +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + +go +create table Teachers +( +Id int PRIMARY key not null IDENTITY, +TeacherName nvarchar(80) +) +go +insert into Teachers (TeacherName) values('张三') +insert into Teachers (TeacherName) values('李四') +insert into Teachers (TeacherName) values('王五') + +go +create table CourseInfo +( +Id int PRIMARY key not null IDENTITY, +CourseName NVARCHAR(80) not null, +TeacherId int not null +) +go +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + +go +create table StudentCourseScore +( +Id int PRIMARY key not null IDENTITY, +StudentId int not null, +CourseId int not null, +Score int not null +) +go +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + +-- 练习题目: +select * from StudentInfo--学生信息表 +select * from Teachers--老师表 +select * from CourseInfo--课程表 +select * from StudentCourseScore--成绩表 + +--语文2 数学1 英语3 + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 2 4 +select C.*,A.Score 数学成绩,B.Score 语文成绩 +from StudentCourseScore A,StudentCourseScore B,StudentInfo C +where A.CourseId=2 and B.CourseId=1 AND A.StudentId=B.StudentId AND A.Score>B.Score AND C.Id=A.StudentId + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from StudentCourseScore A inner join StudentCourseScore B on A.StudentId = B.StudentId inner join StudentInfo C on +C.Id = A.StudentId AND A.CourseId = 1 and B.CourseId = 2 + +select * from StudentInfo where Id in (select A.StudentId from StudentCourseScore A,StudentCourseScore B +where A.CourseId=2 and B.CourseId=1 AND A.StudentId=B.StudentId ) + +select * from StudentInfo where Id in (select A.StudentId from StudentCourseScore A,StudentCourseScore B +where A.CourseId=(select Id from CourseInfo where CourseName='数学') and B.CourseId=(select Id from CourseInfo where CourseName='语文') +AND A.StudentId=B.StudentId ) + +select * from StudentInfo AA +join (select A.StudentId from StudentCourseScore A,StudentCourseScore B where A.CourseId=2 and B.CourseId=1 AND A.StudentId=B.StudentId ) BB +ON AA.Id=BB.StudentId + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentInfo.StudentCode 学生编号,StudentInfo.StudentName 学生姓名,AVG(Score) 平均成绩 +from StudentInfo inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +group by StudentCode,StudentName +having AVG(Score)>=60 + +-- 3.查询在 成绩 表存在成绩的学生信息 +select StudentInfo.StudentCode 学生编号,StudentInfo.StudentName 学生姓名,StudentCourseScore.Score 分数 +from StudentCourseScore left join StudentInfo on StudentInfo.Id=StudentCourseScore.StudentId + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentId 学生编号,StudentName 学生姓名,count(CourseId) 选课总数,sum(Score) 所有课程的总成绩 +from StudentCourseScore A right join StudentInfo B on A.StudentId=B.Id +group by StudentId,StudentName,StudentCode + +-- 4.1 查有成绩的学生信息 +select distinct StudentInfo.*,StudentCourseScore.* +from StudentInfo right join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId + +-- 5.查询「李」姓老师的数量 +select count(*) +from Teachers where TeacherName like '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 +select StudentInfo.* from StudentInfo +inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +left join CourseInfo on StudentCourseScore.CourseId=CourseInfo.Id +inner join Teachers on Teachers.Id=CourseInfo.TeacherId +where TeacherName='张三' + +-- 7.查询没有学全所有课程的同学的信息 +select StudentId 学生编号,StudentInfo.StudentName 学生姓名,StudentInfo.Birthday 出生日期,StudentInfo.ClassId 班级,StudentInfo.Sex 性别,StudentInfo.StudentCode 编号,count(*)学过的课程 +from StudentCourseScore inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id +group by StudentId,StudentInfo.Birthday,StudentInfo.ClassId,StudentInfo.Sex,StudentInfo.StudentCode,StudentInfo.StudentName having count(*)<3 + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select distinct StudentInfo.* from StudentCourseScore +inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id +where StudentCode=01 + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select StudentInfo.* from StudentInfo +inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +left join CourseInfo on StudentCourseScore.CourseId=CourseInfo.Id +where StudentCourseScore.CourseId=1 + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select distinct StudentInfo.StudentName from StudentInfo +inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +left join CourseInfo on StudentCourseScore.CourseId=CourseInfo.Id +inner join Teachers on Teachers.Id=CourseInfo.TeacherId +where TeacherName!='张三' + + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2301.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2301.sql" new file mode 100644 index 0000000000000000000000000000000000000000..50d9c00cc844d1954b9c707535a57f14062ab5ba --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2301.sql" @@ -0,0 +1,70 @@ +use master +go +create database GoodsDB +on +( +name='GoodsDB', +filename='C:\SQL\GoodsDB.mdf', +size=5MB, +maxsize=50MB, +filegrowth=1MB +) +log on +( +name='GoodsDB_log', +filename='C:\SQL\GoodsDB_log.ldf', +size=5MB, +maxsize=50MB, +filegrowth=1MB +) +go +use GoodsDB +go +create table GoodsType--商品类型表 +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null +) +go +use GoodsDB +go +create table GoodsInfo--商品信息表 +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) +go +insert into GoodsType (TypeName) values ('服装内衣') +insert into GoodsType (TypeName) values ('鞋包配饰') +insert into GoodsType (TypeName) values ('手机数码') + +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('提花小西装','红色','菲曼琪','300','1') +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('百搭短裤','绿色','哥弟','100','1') +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('无袖背心','白色','阿依莲','700','1') +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('低帮休闲鞋','红色','菲曼琪','900','2') +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('中跟单鞋','绿色','哥弟','400','2') +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('平底鞋','白色','阿依莲','200','2') +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('迷你照相机','红色','尼康','500','3') +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('硬盘','黑色','希捷','600','3') +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('显卡','黑色','技嘉','800','3') + +select * from GoodsType--商品类型表 +select * from GoodsInfo--商品信息表 +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 名称,GoodsColor 颜色,GoodsMoney 价格 +from GoodsInfo +order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型,MAX(GoodsMoney) 最高价, MIN(GoodsMoney) 最低价,AVG(GoodsMoney) 平均价 +from GoodsInfo +group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsBrand 商品品牌,GoodsMoney 价格,TypeID 商品类型 +from GoodsInfo +where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2302.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2302.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0524d561711437733ced1911761f8c3bbbcf6756 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2302.sql" @@ -0,0 +1,73 @@ +use master +go +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='C:\SQL\HOUSE_DB.mdf', +size=5MB, +maxsize=50MB, +filegrowth=1MB +) +log on +( +name='HOUSE_DB_log', +filename='C:\SQL\HOUSE_DB_log.ldf', +size=5MB, +maxsize=50MB, +filegrowth=1MB +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( +Type_ID int primary key identity(1,1) not null, +Type_Name varchar(50) unique not null +) +go +use HOUSE_DB +go +create table HOUSE +( +House_ID int primary key identity(1,1) not null, +House_Name varchar(50) not null, +House_Price float default('0') not null, +Type_ID int references HOUSE_TYPE(Type_ID) not null +) +go +insert into HOUSE_TYPE (Type_Name) values('小户型') +insert into HOUSE_TYPE (Type_Name) values('经济型') +insert into HOUSE_TYPE (Type_Name) values('别墅') + +insert into HOUSE (House_Name,House_Price,Type_ID) values('蘑菇屋','3500',1) +insert into HOUSE (House_Name,House_Price,Type_ID) values('苹果屋','4000',1) +insert into HOUSE (House_Name,House_Price,Type_ID) values('大大屋','4500',2) +insert into HOUSE (House_Name,House_Price,Type_ID) values('小小屋','3500',2) +insert into HOUSE (House_Name,House_Price,Type_ID) values('一二三','4000',3) +insert into HOUSE (House_Name,House_Price,Type_ID) values('四五六','3800',3) + +--添加查询 +select * from HOUSE_TYPE--类型 +select * from HOUSE--房屋信息 +--1.查询所有房屋信息 +select * from HOUSE_TYPE +select * from HOUSE + +--2.使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where Type_Name like '%型%' + +--3.查询出房屋的名称和租金,并且按照租金降序排序 +select House_Name 名称,House_Price 租金 +from HOUSE +order by House_Price desc + +--4.使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select HOUSE_TYPE.Type_Name 房屋类型,HOUSE.House_Name 房屋名称 +from HOUSE inner join HOUSE_TYPE on HOUSE_TYPE.Type_ID=HOUSE.Type_ID + +--5.查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 House_Name 房屋名称,max(House_Price) 租金 +from HOUSE +group by House_Name,House_Price +order by House_Price desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2303.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2303.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7689a6371bb93dca763fa679d868a648f37d08fd --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2303.sql" @@ -0,0 +1,69 @@ +use master +go +create database StarManagerDB +on +( +name='StarManagerDB', +filename='C:\SQL\StarManagerDB.mdf', +size=5MB, +maxsize=50MB, +filegrowth=1MB +) +log on +( +name='StarManagerDB_log', +filename='C:\SQL\StarManagerDB_log.ldf', +size=5MB, +maxsize=50MB, +filegrowth=1MB +) +go +use StarManagerDB +go +create table StarType--明星类型表 +( +T_NO int primary key identity(1,1) not null, +T_NAME nvarchar(20) +) +go +use StarManagerDB +go +create table StarInfo--明星信息表 +( +S_NO int primary key identity(1,1) not null, +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int references StarType(T_NO) +) + +insert into StarType (T_NAME) values ('体育明星') +insert into StarType (T_NAME) values ('IT明星') +insert into StarType (T_NAME) values ('相声演员') + +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('梅西','30','射门','阿根廷',1) +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('科比','35','过人','美国',1) +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('蔡景现','40','敲代码','中国',2) +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('马斯克','36','造火箭','外星人',2) +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('郭德纲','50','相声','中国',3) +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('黄铮','41','拼多多','中国',2) + +select * from StarType--明星类型表 +select * from StarInfo--明星信息表 +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯,S_AGE 年龄 +from StarInfo +group by S_NAME,S_HOBBY,S_NATIVE,S_AGE +order by S_AGE desc + +--4、按 明星类型编号 分类查询 明星人数 ,明星平均年龄,显示 明星人数大于2的分组信息 ,要求使用别名显示列名。 +select S_T_NO 类型编号,count(S_NO) 明星人数,avg(S_AGE) 平均年龄 +from StarInfo +group by S_T_NO having count(S_NO)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯,S_AGE 年龄 +from StarInfo +where S_T_NO=1 +order by S_AGE desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/50\351\242\230.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/50\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7fed0611c9382519c7f45d2758645b89442458af --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/50\351\242\230.sql" @@ -0,0 +1,417 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers + + + +-- 1.查询 数学 课程比 语文 课程成绩高的学生的信息及课程分数 +select C.*,A.Score 数学成绩,b.Score 语文成绩 from StudentCourseScore A,StudentCourseScore B,StudentInfo C +where A.CourseId=2 and B.CourseId=1 and A.StudentId=B.StudentId and A.Score>B.Score and A.StudentId=C.Id + +-- 1.1 查询 同时存在 数学 课程和 语文 课程的情况 +select * from StudentInfo where Id in (select A.StudentId from StudentCourseScore A,StudentCourseScore B +where A.CourseId=(select Id from CourseInfo where CourseName='数学') and +B.CourseId=(select Id from CourseInfo where CourseName='语文') AND A.StudentId=B.StudentId ) + +-- 1.2 查询存在 数学 课程但可能不存在 语文 课程的情况(不存在时显示为 null ) +select * from (select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId + +-- 1.3 查询不存在 数学 课程但存在 语文 课程的情况 +select * from (select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A right join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId + +select * from StudentCourseScore +-- 2.查询平均成绩大于等于 60 分 的同学的 学生编号 和 学生姓名 和 平均成绩 +select A.Id,StudentName,AVG(Score)平均成绩 from StudentInfo A join StudentCourseScore B on A.Id=B.StudentId group by +A.Id,StudentName having AVG(Score)>=60 + + +-- 3.查询在 成绩表 存在成绩的学生信息 +select B.Id,StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A join StudentInfo B on +A.StudentId=B.Id group by B.Id,StudentCode,StudentName,Birthday,Sex,ClassId + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select A.Id,StudentName,COUNT(CourseId)选课总数,SUM(Score)总成绩 from StudentInfo A left join StudentCourseScore B on A.Id=B.StudentId +group by A.Id,StudentName + +-- 4.1 查有成绩的学生信息 +select B.Id,StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A join StudentInfo B on +A.StudentId=B.Id group by B.Id,StudentCode,StudentName,Birthday,Sex,ClassId + +-- 5.查询「李」姓老师的数量 +select count(TeacherName)数量 from Teachers where TeacherName like '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 +select C.* from CourseInfo A,Teachers B,StudentInfo C,StudentCourseScore D where A.TeacherId=B.Id AND C.Id=D.StudentId AND CourseId=A.Id AND TeacherName='张三' + +-- 7.查询 没有 学全所有课程 的同学的信息 +select B.Id,StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id group by +B.Id,StudentCode,StudentName,Birthday,Sex,ClassId +except +select D.* from StudentCourseScore A,StudentCourseScore B,StudentCourseScore C,StudentInfo D where A.CourseId=1 and B.CourseId=2 and C.CourseId=3 +and A.StudentId=B.StudentId AND B.StudentId=C.StudentId AND A.StudentId=D.Id + +-- 8.查询 至少有一门课 与学号为 01 的同学所学相同的同学的信息 +select B.Id,StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id group by +B.Id,StudentCode,StudentName,Birthday,Sex,ClassId +except +select D.* from StudentCourseScore A,StudentCourseScore B,StudentCourseScore C,StudentInfo D where A.CourseId=1 and B.CourseId=2 and C.CourseId=3 +and A.StudentId=B.StudentId AND B.StudentId=C.StudentId AND A.StudentId=D.Id AND D.StudentCode='01' + +-- 9.查询 和 01 号的同学学习的课程 完全相同的其他同学的信息 +select D.* from StudentCourseScore A,StudentCourseScore B,StudentCourseScore C,StudentInfo D where A.CourseId=1 and B.CourseId=2 and C.CourseId=3 +and A.StudentId=B.StudentId AND B.StudentId=C.StudentId AND A.StudentId=D.Id +except +select D.* from StudentCourseScore A,StudentCourseScore B,StudentCourseScore C,StudentInfo D where A.CourseId=1 and B.CourseId=2 and C.CourseId=3 +and A.StudentId=B.StudentId AND B.StudentId=C.StudentId AND A.StudentId=D.Id AND D.StudentCode='01' + + +-- 10.查询 没学过 张三老师讲授的任一门课程的学生姓名 +select C.StudentName from CourseInfo A,Teachers B,StudentInfo C,StudentCourseScore D where A.TeacherId=B.Id AND C.Id=D.StudentId AND CourseId=A.Id +group by C.StudentName +except +select C.StudentName from CourseInfo A,Teachers B,StudentInfo C,StudentCourseScore D where A.TeacherId=B.Id AND C.Id=D.StudentId AND CourseId=A.Id +AND TeacherName='张三' + + +-- 11.查询 两门及其以上 不及格课程的同学的 学号,姓名 及其 平均成绩 +select StudentCode,StudentName,AVG(Score)平均成绩 from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id where Score<60 group by +StudentCode,StudentName having COUNT(CourseId)>=2 + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 +select C.*,Score from StudentCourseScore A join CourseInfo B on A.CourseId=B.Id join StudentInfo C on A.StudentId=C.Id where CourseName='数学' and Score<60 +order by Score DESC + +-- 13.按平均成绩从高到低 显示 所有学生的 所有课程的成绩 以及 平均成绩 +select StudentId,CourseId,Score,AVG(Score)平均成绩 from StudentCourseScore group by StudentId,CourseId,Score order by AVG(Score) DESC + +-- 14.查询各科成绩最高分、最低分和平均分: +select CourseId,MAX(Score)最高分,MIN(Score)最低分,AVG(Score)平均分 from StudentCourseScore group by CourseId + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 +select StudentId,SUM(Score)总成绩 from StudentCourseScore group by StudentId order by SUM(Score) DESC + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 +select StudentId,SUM(Score)总成绩 from StudentCourseScore group by StudentId order by SUM(Score) DESC + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + +-- 18.查询各科成绩前三名的记录 +select top 3 * from StudentCourseScore A,StudentCourseScore B,StudentCourseScore C where A.CourseId='1'AND B.CourseId='2'AND C.CourseId='3' + AND A.StudentId=B.StudentId AND B.StudentId=C.StudentId order by A.Score DESC,B.Score DESC,C.Score DESC + + +-- 19.查询每门课程被选修的学生数 +select CourseId,COUNT(StudentId)学生数 from StudentCourseScore group by CourseId +select * from StudentInfo +select * from CourseInfo + + +-- 20.查询出只选修两门课程的学生学号和姓名 +select B.StudentCode,StudentName from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id group by B.StudentCode,StudentName +having COUNT(CourseId)=2 + + +-- 21.查询男生、女生人数 +select * from StudentInfo A,StudentInfo B where A.Sex='m' AND B.Sex='f'and A.StudentCode=B.StudentCode group by A.Sex,B.Sex + +-- 22.查询名字中含有「风」字的学生信息 +select * from StudentInfo where StudentName like '%风%' + +-- 23.查询同名同性学生名单,并统计同名人数 + + +-- 24.查询 1990 年出生的学生名单 +select * from StudentInfo where Birthday like '1990%' + +-- 25.查询 每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 +select CourseId,AVG(Score)平均成绩 from StudentCourseScore group by CourseId order by AVG(Score) DESC + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 +select StudentCode,StudentName,AVG(Score)平均成绩 from StudentInfo A join StudentCourseScore B on A.Id = B.StudentId +group by StudentCode,StudentName having AVG(Score)>=85 + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 +select B.StudentName,A.Score from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id join CourseInfo C on A.CourseId=C.Id +where C.CourseName='数学' AND A.Score<60 + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) +select B.Id,Score,CourseId from StudentCourseScore A right join StudentInfo B on A.StudentId=B.Id + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 +select B.StudentName,C.CourseName,A.Score from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id join CourseInfo C on +A.CourseId=C.Id where Score>70 + +-- 30.查询不及格的课程 +select CourseId from StudentCourseScore where Score<60 group by CourseId + +-- 31.查询课程编号为 1 且课程成绩在 80 分以上的学生的学号和姓名 +select StudentCode,StudentName from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id where CourseId=1 and Score>=80 + +-- 32.求每门课程的学生人数 +select CourseId,COUNT(StudentId)人数 from StudentCourseScore group by CourseId + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +select top 1 D.Id,StudentCode,StudentName,Birthday,Sex,ClassId,MAX(Score)成绩 from CourseInfo A join Teachers B on A.TeacherId=B.Id join StudentCourseScore C on A.Id=C.CourseId +join StudentInfo D on C.StudentId=D.Id where TeacherName='张三' group by D.Id,StudentCode,StudentName,Birthday,Sex,ClassId + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +select top 1 D.Id,StudentCode,StudentName,Birthday,Sex,ClassId,MAX(Score)成绩 from CourseInfo A join Teachers B on A.TeacherId=B.Id join StudentCourseScore C on A.Id=C.CourseId +join StudentInfo D on C.StudentId=D.Id where TeacherName='张三' group by D.Id,StudentCode,StudentName,Birthday,Sex,ClassId + + +-- 35.查询不同课程 成绩相同的学生的学生编号、课程编号、学生成绩 +select A.Id,A.CourseId,B.CourseId,C.CourseId,A.Score from StudentCourseScore A,StudentCourseScore B,StudentCourseScore C where A.CourseId='1'AND B.CourseId='2'AND C.CourseId='3' + AND A.StudentId=B.StudentId AND B.StudentId=C.StudentId AND A.Score=B.Score AND B.Score=C.Score + +-- 36.查询每门功成绩最好的前两名 +select top 2 * from StudentCourseScore A,StudentCourseScore B,StudentCourseScore C where A.CourseId='1'AND B.CourseId='2'AND C.CourseId='3' + AND A.StudentId=B.StudentId AND B.StudentId=C.StudentId order by A.Score DESC,B.Score DESC,C.Score DESC + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 +select CourseId,COUNT(*)选修人数 from StudentCourseScore group by CourseId having COUNT(*)>5 + +-- 38.检索至少选修两门课程的学生学号 +select B.StudentCode from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id group by B.StudentCode +having COUNT(CourseId)>=2 + +-- 39.查询选修了全部课程的学生信息 +select B.Id,StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A join StudentInfo B on A.StudentId=B.Id group by B.Id,StudentCode,StudentName,Birthday,Sex,ClassId +having COUNT(CourseId)=3 + +-- 40.查询各学生的年龄,只按年份来算 + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + +-- 42.查询本周过生日的学生 + + +-- 43.查询下周过生日的学生 + + +-- 44.查询本月过生日的学生 + + +-- 45.查询下月过生日的学生 + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\345\244\215\344\271\240\351\242\2302.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\345\244\215\344\271\240\351\242\2302.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7efb01a2a7f7a13241537bd42ec736aca212ab33 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\345\244\215\344\271\240\351\242\2302.sql" @@ -0,0 +1,79 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\数据库\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +log on +( + name='HOUSE_DB_log', + filename='D:\数据库\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) + +use HOUSE_DB +go + +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique not null +) + + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 +create table HOUSE +( + house_id int primary key identity(1,1) not null, + house_name varchar(50) not null, + house_price float default(0) not null, + type_id int foreign key references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +select * from HOUSE_TYPE order by type_id +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') +--HOUSE表中添加至少3条数据,不能全都为同一类型 +select * from HOUSE +insert into HOUSE values ('201','300','1'),('202','600','2'),('203','1000','3') +--4、添加查询 +--查询所有房屋信息 +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' + + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name from HOUSE A inner join HOUSE_TYPE B on A.type_id=B.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price,type_name from HOUSE A inner join HOUSE_TYPE B on A.type_id=B.type_id order by house_price desc + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\345\244\215\344\271\240\351\242\2303.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\345\244\215\344\271\240\351\242\2303.sql" new file mode 100644 index 0000000000000000000000000000000000000000..72518294b06cf62b1f80474550e2041afd756706 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\345\244\215\344\271\240\351\242\2303.sql" @@ -0,0 +1,89 @@ +--1、创建明星数据库(StarManagerDB),然后建立两张表,StarType(明星类型表),StarInfo(明星信息表),表结构分别如下: +use master +go + +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\数据库\StarManagerDB.mdf', + size=5mb, + maxsize=100mb, + filegrowth=5mb +) + +log on +( + name='StarManagerDB_log', + filename='D:\数据库\StarManagerDB_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=5mb +) + +use StarManagerDB +go +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 +select * from StarType +insert into StarType values ('体育明星'),('IT明星'),('相声演员') + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 +select * from StarInfo +insert into StarInfo values +('梅西','30','射门','阿根廷','1'), +('科比','35','过人','美国','1'), +('蔡景现','40','敲代码','中国','2'), +('马斯克','36','造火箭','外星人','2'), +('郭德纲','50','相声','中国','3'), +('黄铮','41','拼多多','中国','2') + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo order by S_AGE desc + +--4、按 明星类型编号 分类查询 明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 类型编号,COUNT(S_NO)人数,AVG(S_AGE)平均年龄 from StarInfo group by S_T_NO +having COUNT(S_NO)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarType inner join StarInfo on T_NO=S_T_NO where T_NAME='体育明星' +order by S_AGE desc diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8d72570c3cfe793349e6009f6d0f80cbe37a32e4 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" @@ -0,0 +1,53 @@ +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size = 5mb, + maxsize=50mb, + filegrowth=1mb +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int not null primary key identity, + type_name varchar(50) not null unique +) +create table HOUSE +( + ouse_id int not null primary key identity, + house_name varchar(50) not null , + house_price float not null default(0), + type_id int not null foreign key references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE(type_name) +select '小户型' union +select '经济型' union +select '别墅' +select * from HOUSE_TYPE +insert into HOUSE(house_name,house_price,type_id) +select '平房','500',3 union +select '高楼','1000',2 union +select '总统','2000',1 +select * from HOUSE +--4、添加查询 +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select HOUSE.house_name ,HOUSE_TYPE.type_name from HOUSE_TYPE inner join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price,house_name from HOUSE order by house_price desc +select house_price,house_name from HOUSE where house_price in(select max(house_price) from HOUSE) diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4be642bab02b55c730658c29aa0ebe6c583d31d9 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery2.sql" @@ -0,0 +1,51 @@ +create database StarManagerDB +on +( +name='StarManagerDB', +filename='D:\StarManagerDB.mdf' + ) + log on + ( + name='StarManagerDB_log', +filename='D:\StarManagerDB_log.ldf' + ) + use StarManagerDB + go + create table StarType + ( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) + ) + create table StarInfo + ( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20)default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) + ) + insert into StarType(T_NAME) + select '体育明星' union + select 'IT明星' union + select '相声演员' + select * from StarType + insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) + select '梅西',30,'射门','阿根廷',1 union + select '科比',35,'过人','美国',1 union + select '蔡景现',40,'敲代码','中国',2 union + select '马斯克',36,'造火箭','外星人',2 union + select '郭德纲',50,'相声','中国',3 union + select '黄铮',41,'拼多多','中国',2 + select * from StarInfo + select * from StarType +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄, +--显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 类型编号,count(S_T_NO)明星人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarType + inner join StarInfo on StarType.T_NO=StarInfo.S_T_NO where T_NAME='体育明星' + order by S_AGE desc + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\345\244\215\344\271\24050.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\345\244\215\344\271\24050.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f567faa43389755c76db782f28750581a5f671cb --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\345\244\215\344\271\24050.sql" @@ -0,0 +1,277 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +select * from StudentCourseScore -- 学生课程成绩 +select * from CourseInfo --课程表 +select * from Teachers --老师 +select * from StudentInfo --学生 +-- 1.查询"数学"课程比"语文"课程成绩高的学生的信息及课程分数 +select C.* ,A.Score 数学成绩,b.Score 语文成绩 +from StudentCourseScore A,StudentCourseScore B,StudentInfo C +WHERE A.CourseId=1 AND B.CourseId=2 AND A.StudentId=B.StudentId AND A.Score>B.Score and C.Id=A.StudentId +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select b.StudentId +from StudentCourseScore A,StudentCourseScore B +WHERE A.CourseId=1 AND B.CourseId=2 AND A.StudentId=B.StudentId + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + + +select * from +(select * from StudentCourseScore A where CourseId=(select id from CourseInfo where CourseName='数学')) A left JOIN +(select * from StudentCourseScore A where CourseId=(select id from CourseInfo where CourseName='语文')) B ON A.StudentId=B.StudentId + + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from +(select * from StudentCourseScore A where CourseId=(select id from CourseInfo where CourseName='数学')) A left JOIN +(select * from StudentCourseScore A where CourseId=(select id from CourseInfo where CourseName='语文')) B ON A.StudentId=B.StudentId +where b.CourseId is null +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select a.StudentId,StudentName,avg(score)平均成绩 from StudentCourseScore A INNER JOIN StudentInfo B ON +A.StudentId=B.Id group by a.StudentId,StudentName having avg(score)>=60 +-- 3.查询在 成绩 表存在成绩的学生信息 +select distinct b.* from StudentCourseScore A inner join StudentInfo B +ON A.StudentId=b.Id + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select A.StudentCode 学生编号,a.StudentName 姓名,COUNT(B.CourseId)选课总数,sum(b.Score)总成绩 from StudentInfo A left join StudentCourseScore B +ON A.Id=B.StudentId group by A.StudentCode ,a.StudentName + + +-- 4.1 查有成绩的学生信息 +select a.StudentName,b.StudentId,a.ClassId,a.Birthday,a.Sex,a.StudentCode from StudentInfo a left join StudentCourseScore b +on a.Id=b.StudentId where Score is not null group by StudentName,StudentId,ClassId,Birthday,Sex,StudentCode +-- 5.查询「李」姓老师的数量 +select TeacherName,count(TeacherName)数量 from Teachers where TeacherName like '李%' group by TeacherName +-- 6.查询学过「张三」老师授课的同学的信息 +select b.* from StudentCourseScore a inner join StudentInfo b on a.StudentId=b.Id +where CourseId='2' +-- 7.查询没有学全所有课程的同学的信息 + +select StudentId,b.StudentName,b.ClassId,b.Birthday,b.Sex,b.StudentCode,count(CourseId)课程数量 from StudentCourseScore A +left join StudentInfo B ON A.StudentId=B.Id +where CourseId in (select id from CourseInfo) group by StudentId,b.StudentName,b.ClassId,b.Birthday,b.Sex,b.StudentCode +having count(CourseId)<3 +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + + +select * from StudentInfo where StudentCode in(select distinct B.StudentId from StudentCourseScore A,StudentCourseScore B +where A.StudentId = 1 and B.StudentId != 1 and B.CourseId = A.CourseId) +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select * from StudentInfo +where StudentCode in(select A.StudentId from StudentCourseScore A cross join StudentCourseScore B +where B.StudentId = 1 and A.StudentId != 1 +group by A.StudentId +having count(distinct A.CourseId) = count(distinct B.CourseId)) +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +--由此可推 张三教授的科目是数学编号为2,张三编号为1 +--先查学过数学的学号 +select * from StudentInfo +where id not in +( +select StudentId from StudentCourseScore +where CourseId in( +select id from CourseInfo where teacherid += ( select id from Teachers where TeacherName ='张三'))) + + + +select * from StudentInfo --学生 +select * from StudentCourseScore -- 学生课程成绩 +select * from Teachers --老师 +select * from CourseInfo --课程表 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9f128b9d0e1be59f00350b9264401f94556ea20c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" @@ -0,0 +1,564 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + +--StudentInfo Teachers CourseInfo StudentCourseScore + +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers +select * from CourseInfo + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +--select * from StudentCourseScore where CourseId='1' +--select * from StudentCourseScore where CourseId='2' + +select * from (select StudentId,Score from StudentCourseScore where CourseId='1') A , +(select StudentId,Score from StudentCourseScore where CourseId='2') B +inner join StudentInfo S on S.Id=StudentId +where A.StudentId=B.StudentId and A.Score1 + +select * from StudentCourseScore where StudentId in +(select StudentId from StudentCourseScore +where StudentId not in (select StudentId from StudentCourseScore +where CourseId='2'group by StudentId,CourseId) or CourseId='1' group by StudentId having count(CourseId)>1 +) +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId 学生编号,StudentName 学生姓名,AVG(Score) 平均成绩 from StudentCourseScore SCS inner join StudentInfo S on S.Id=SCS.StudentId +group by StudentId,StudentName having AVG(Score)>60 + +-- 3.查询在 成绩 表存在成绩的学生信息 +--select StudentName from StudentInfo S left join StudentCourseScore SCS on S.Id=SCS.StudentId where Score!='' group by StudentName +select * from StudentInfo where StudentName in(select StudentName from StudentInfo S left join StudentCourseScore SCS on S.Id=SCS.StudentId +where Score!='' group by StudentName) + + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select S.Id 学生编号,StudentName 学生姓名,count(CourseId) ,sum(Score) from StudentInfo S +left join StudentCourseScore SCS on S.Id=SCS.StudentId +group by S.Id,StudentName + +-- 4.1 查有成绩的学生信息 +select * from StudentInfo where StudentName in(select StudentName from StudentInfo S left join StudentCourseScore SCS on S.Id=SCS.StudentId +where Score!='' group by StudentName) + + +-- 5.查询「李」姓老师的数量 +select count(TeacherName) 数量 from Teachers where TeacherName like '%李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +--select Id from Teachers where TeacherName='张三' +--select CourseName from CourseInfo where TeacherId in (select Id from Teachers where TeacherName='张三') +--select StudentId from StudentCourseScore where CourseId=(select Id from Teachers where TeacherName='张三') + + +select * from StudentInfo where Id in +(select StudentId from StudentCourseScore where CourseId=(select Id from Teachers where TeacherName='张三')) +-- 7.查询没有学全所有课程的同学的信息 +--select StudentId from StudentCourseScore group by StudentId having count(CourseId)>2 + + +select * from StudentInfo where Id in (select StudentId from StudentCourseScore group by StudentId having count(CourseId)>2) +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +--select CourseId from StudentCourseScore where StudentId='1' +--select StudentId from StudentCourseScore where StudentId!='1' and CourseId in (select CourseId from StudentCourseScore where StudentId='1' +--) group by StudentId + + + +select * from StudentInfo where Id in (select StudentId from StudentCourseScore where StudentId!='1' and CourseId in (select CourseId from StudentCourseScore where StudentId='1' +) group by StudentId) +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +--select CourseId from StudentCourseScore where StudentId='1' +--select StudentId,count(CourseId) from StudentCourseScore where StudentId!='1' group by StudentId having count(CourseId)>2 +--select StudentId,CourseId from StudentCourseScore +--where StudentId in (select StudentId from StudentCourseScore where StudentId!='1' group by StudentId having count(CourseId)>2) +--select StudentId from (select StudentId,CourseId from StudentCourseScore +--where StudentId in (select StudentId from StudentCourseScore +--where StudentId!='1' group by StudentId having count(CourseId)>2)) A ,(select CourseId from StudentCourseScore where StudentId='1') B +--where A.CourseId=B.CourseId group by StudentId + + +select * from StudentInfo +where id in (select StudentId from (select StudentId,CourseId from StudentCourseScore +where StudentId in (select StudentId from StudentCourseScore +where StudentId!='1' group by StudentId having count(CourseId)>2)) A , +(select CourseId from StudentCourseScore where StudentId='1') B +where A.CourseId=B.CourseId group by StudentId) + + + +select * from StudentInfo where id in +(select StudentId from +(select StudentId,CourseId from StudentCourseScore where StudentId in ( + select StudentId from StudentCourseScore where StudentId != + '1' + group by StudentId having count(CourseId) > 2)) A, + (select CourseId from StudentCourseScore where StudentId = + '1') B + where A.CourseId = B.CourseId group by StudentId) + + + + + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + + + +select * from StudentInfo where Id not in (select StudentId from StudentCourseScore +where CourseId=(select Id from Teachers where TeacherName='张三')) + + + + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + +--select StudentId,count(score) from StudentCourseScore where Score<60 group by StudentId,score +--select StudentId,count(score) from StudentCourseScore group by StudentId,Score having Score<60 + +--select StudentId,count(CourseId) 平均成绩 from StudentCourseScore where Score<60 group by StudentId having count(CourseId)>=2 +--两门及其以上不及格课程的同学的学号 + +select SCS.StudentId,StudentName,AVG(score) 平均成绩 from StudentCourseScore SCS join StudentInfo SI on SI.Id=StudentId group by SCS.StudentId,StudentName +having StudentId in (select StudentId from StudentCourseScore where Score<60 group by StudentId having count(CourseId)>=2 +) + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + +--select Id from CourseInfo where CourseName='数学' + +--select StudentId from StudentCourseScore +--where CourseId in (select Id from CourseInfo where CourseName='数学') and Score<60 + +select * from StudentInfo +where Id=(select StudentId from StudentCourseScore +where CourseId in (select Id from CourseInfo where CourseName='数学') and Score<60 +) + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + +select CourseId,score 课程的成绩,AVG(score) 平均成绩 from StudentCourseScore group by score,CourseId order by AVG(score) DESC + + + +-- 14.查询各科成绩最高分、最低分和平均分: +select CourseId,MAX(score) 最高分,MIN(score) 最低分,AVG(score) 平均分 from StudentCourseScore group by CourseId + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + + +--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + --((cast((select count(*) from StudentCourseScore where score>=60) as int)/ +--cast((select count(*) from StudentCourseScore) as int)) as float) +--select (sum(case when score>=60 then 1 else 0 end)*100) from StudentCourseScore + +select CourseId 课程ID,CourseName 课程,MAX(score) 最高分,MIN(score) 最低分,AVG(score), +(select distinct cast((select distinct (cast((select count(*) from StudentCourseScore where score>=60) as int)*100/cast((select count(*) from StudentCourseScore) as int))*1.0 +from StudentCourseScore) as nvarchar)+'%' from StudentCourseScore) 及格率 , +(select distinct cast((select distinct (cast((select count(*) from StudentCourseScore where score between '70' and '80') as int)*100/cast((select count(*) from StudentCourseScore) as int))*1.0 +from StudentCourseScore) as nvarchar)+'%' from StudentCourseScore) 中等率, +(select distinct cast((select distinct (cast((select count(*) from StudentCourseScore where score between '80' and '90') as int)*100/cast((select count(*) from StudentCourseScore) as int))*1.0 +from StudentCourseScore) as nvarchar)+'%' from StudentCourseScore) 优良率, +(select distinct cast((select distinct (cast((select count(*) from StudentCourseScore where score>90) as int)*100/cast((select count(*) from StudentCourseScore) as int))*1.0 +from StudentCourseScore) as nvarchar)+'%' from StudentCourseScore) 优良率 +from StudentCourseScore SCS inner join CourseInfo C on CourseId=C.Id group by CourseId,CourseName + +--要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + + +select CourseId 课程号,count(StudentId) 选修人数 from StudentCourseScore group by CourseId order by count(StudentId) DESC,CourseId ASC + + +--按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers +select * from CourseInfo + + +select CourseId,Score from StudentCourseScore order by Score DESC + +select A.CourseId,A.Score,row_number()over(order by Score DESC) from StudentCourseScore A,StudentCourseScore B +where A.Score=B.Score and row_number()over(order by Score DESC)='' + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + +select + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 + + + + + + + + + + + + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..06a887d3c9fe8222b088ae541205f5eff982b87a --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery2.sql" @@ -0,0 +1,235 @@ +-- 练习题目: + +-- 1.查询 "数学" 课程比 "语文" 课程成绩高的(同一个学生) 学生的信息 及 课程分数 + +select C.*,A.Score 数学成绩,B.Score 语文成绩 from StudentCourseScore A,StudentCourseScore B,StudentInfo C +where A.CourseId=2 and B.CourseId=1 AND A.StudentId=B.StudentId AND A.Score>B.Score AND C.Id=A.StudentId + + +-- 1.1 查询同时存在"数学"课程和"语文"课程 的学生的情况 + + +select * from StudentCourseScore A inner join StudentCourseScore B on A.StudentId = B.StudentId inner join StudentInfo C on +C.Id = A.StudentId AND A.CourseId = 1 and B.CourseId = 2 +------------------------------ + + + +-- 1.2 查询存在"数学"课程但可能不存在"语文"课程的情况(不存在时显示为 null ) +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId + + +-- 1.3 查询不存在"数学"课程但存在"语文"课程的情况 + +select * from StudentCourseScore A left join StudentCourseScore B on A.StudentId=B.StudentId +where A.CourseId=(select Id from CourseInfo where CourseName='语文') and B.CourseId=(select Id from CourseInfo where CourseName='英语') +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 + +select StudentCode 编号,StudentName 名字,AVG(Score)平均成绩 from StudentInfo S inner join +StudentCourseScore T on S.Id=T.StudentId group by StudentCode,StudentName having AVG(Score) >= 60 +-- 3.查询在 成绩表 存在 成绩的 学生信息 + +select * from StudentCourseScore S inner join StudentInfo T on S.Id=T.Id + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select StudentCode 学生编号,StudentName 学生姓名,COUNT(CourseId)选课总数,sum(Score)总成绩 from StudentCourseScore S left join StudentInfo T on S.StudentId=T.Id +group by StudentCode,StudentName +-- 4.1 查有成绩的学生信息 + +select * from StudentInfo A inner join StudentCourseScore S on A.Id=S.Id +-- 5.查询「李」姓老师的数量 + +select * from Teachers where TeacherName like '李%' +-- 6.查询学过「张三」老师授课的同学的信息 +select * from StudentCourseScore S inner join StudentInfo ST on S.StudentId=ST.Id where CourseId=(select Id from Teachers where TeacherName='张三') + +-- 7.查询 没有学 全 所有课程 的 同学的信息 +select ST.*,COUNT(CourseId)课程 from StudentInfo ST left join StudentCourseScore S on ST.Id = S.Id group by +ST.Id,StudentCode,StudentName,Birthday,Sex,ClassId having COUNT(CourseId) in (select COUNT(CourseId) from CourseInfo) + + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select * from dbo.StudentInfo +select * from dbo.Teachers +select * from CourseInfo +select * from StudentCourseScore + +SELECT S.* FROM StudentInfo S inner join StudentCourseScore SC on S.Id = SC.Id WHERE StudentId!=1 AND CourseId IN +(select courseID from StudentCourseScore where StudentId=1) + +-- 9.查询和"01"号的同学学习的课程 完全相同的其他同学的信息 +SELECT StudentId,COUNT(DISTINCT CourseId) FROM StudentCourseScore WHERE StudentId!=1 AND CourseId IN +(select distinct courseID from StudentCourseScore where StudentId=1) +GROUP BY StudentId HAVING COUNT(DISTINCT CourseId)= (select COUNT(distinct courseID) from StudentCourseScore where StudentId=1) + +select * from StudentCourseScore + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select StudentName from StudentInfo where Id not in +(select StudentId from StudentCourseScore S left join CourseInfo C on s.Id=C.TeacherId join Teachers T on C.TeacherId = T.Id where TeacherName='张三' ) + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2302.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2302.sql" new file mode 100644 index 0000000000000000000000000000000000000000..658d8e5492ccb0be85738cb6ab337d8bfcfcb775 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2302.sql" @@ -0,0 +1,59 @@ +use master + +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename ='D:\HOUSE_DB.mdf', + size=5MB, + maxsize=50MB, + filegrowth=1Mb +) +log on +( + name='HOUSE_DB_log', + filename ='D:\HOUSE_DB_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=1Mb +) +go +use HOUSE_DB + +go + +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique not null +) + +go +use HOUSE_DB +go +create table HOUSE +( + house_id int primary key identity(1,1) not null, + house_name varchar(50) not null, + house_price float default('0') not null, + type_id int references HOUSE_TYPE(type_id) not null +) +insert into HOUSE_TYPE values('别墅'),('经济型'),('小户型') + +insert into HOUSE values('水木年华',1500,3),('风林水榭',800,1),('锦绣花园',1200,2) + +select * from HOUSE +select * from HOUSE_TYPE + +select T.type_id 房屋编号,house_id 房屋编号,house_name 房屋名称,house_price 房租,type_name 房屋类型 +from HOUSE H left join HOUSE_TYPE T on H.type_id = T.type_id + +select * from HOUSE_TYPE where type_name like '%型%' + +select house_name 名称,house_price 租金 from HOUSE order by house_price desc + +select h.type_id 房屋类型,house_name 房屋名称 from HOUSE_TYPE T inner join HOUSE H on h.type_id = T.type_id + +select house_price 租金,house_name 房屋名称 from HOUSE where house_price in(select MAX(house_price) from HOUSE) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2303.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2303.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b23306d9cc2f7ba37d46a3c333459202238c4420 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2303.sql" @@ -0,0 +1,42 @@ +use master +go + +create database StarManagerDB + +go + +use StarManagerDB + +go + +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20) +) +go + +use StarManagerDB + +go + +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +insert into StarType values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 编号,COUNT(S_NAME) 人数,AVG(S_AGE)平均年龄 from StarInfo group by S_T_NO having COUNT(S_NAME) > 2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select S_AGE 年龄,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo where S_AGE in(select MAX(S_AGE) from StarInfo) diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..578b790b37567544c24ec189ee3cf49a19a8eb83 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery2.sql" @@ -0,0 +1,65 @@ +use master +go +create database HOUSE_DB +on +( name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity, + type_name varchar(50) unique not null +) + +create table HOUSE +( + house_id int primary key identity, + house_name varchar(50) not null, + house_price float default(0) not null, + type_id int references HOUSE_TYPE(type_id) not null +) + +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +select*from HOUSE_TYPE +insert into HOUSE values('马云家',100,'1'),('马化腾家',200,'2'),('我家',100000000,'3') + +select*from HOUSE_TYPE +select*from HOUSE + +--4、添加查询 +--查询所有房屋信息 +select * from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id + +--使用模糊查询包含”型“字的房屋类型信息 +select*from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋的名称,house_price 租金 + from HOUSE + order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price 最高月租, house_name 房屋名称 from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id order by house_price desc diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e97627e2bf9f757200e7fc800fe467f8d93ee939 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery3.sql" @@ -0,0 +1,63 @@ +use master +go +create database StarManagerDB +on +( name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity, + T_NAME nvarchar(20) +) + +create table StarInfo +( + S_NO int primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +insert into StarType values('体育明星'),('IT明星'),('相声明星') + +insert into StarInfo values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1), + ('察景现',40,'敲代码',default,2),('马斯克',36,'造火箭','外星人',2), + ('郭德纲',50,'相声',default,3),('黄铮',41,'拼多多',default,2) + + +select*from StarType +select*from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 + from StarInfo + order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO ,count(*) 明星人数,avg(S_AGE) 明星平均年龄 + from StarInfo + group by S_T_NO + having count(*) >2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_AGE 最大的姓名,S_HOBBY 特技,S_NATIVE 籍贯 + from StarType + join StarInfo + on StarInfo.S_T_NO=StarType.T_NO + where T_NAME='体育明星' + order by S_AGE desc diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery4.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..efa93c46747cd9126e1112ca5d9bd8f37c724c06 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery4.sql" @@ -0,0 +1,511 @@ + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +select*from StudentInfo +select*from Teachers +select*from CourseInfo +select*from StudentCourseScore + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +SELECT * FROM ( select S1.StudentId SID1,S1.CourseId CID2,S1.Score SC2,S2.CourseId CID1,S2.Score SC1 from StudentCourseScore S1 + join StudentCourseScore S2 on S1.StudentId=S2.StudentId + where S1.CourseId = ( select Id from CourseInfo where CourseName='数学' ) + and S2.CourseId = ( select Id from CourseInfo where CourseName='语文' ) + and S1.score=60 + +-- 3.查询在 成绩 表存在成绩的学生信息 +select distinct(StudentId) 学生编号,StudentName 学生姓名 + from StudentCourseScore + join StudentInfo + on StudentCourseScore.StudentId=StudentInfo.Id + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentInfo.Id 学生编号,StudentName 学生姓名,count(CourseId)选课总数,sum(Score)总成绩 + from StudentInfo + left join StudentCourseScore + on StudentCourseScore.StudentId=StudentInfo.Id + group by StudentInfo.Id,StudentName + + +-- 4.1 查有成绩的学生信息 +select*from StudentInfo + where Id in(select StudentId from StudentCourseScore group by StudentId) + +-- 5.查询「李」姓老师的数量 +select count(distinct TeacherName) 李姓老师的数量 + from Teachers + where TeacherName like '李%' group by Id + + +-- 6.查询学过「张三」老师授课的同学的信息 +select StudentId 学号, StudentName 姓名,Birthday 生日,Sex 性别,ClassId 班级 + from StudentCourseScore + join StudentInfo + on StudentInfo.Id=StudentCourseScore.StudentId + where CourseId in (select Id from CourseInfo where TeacherId = (select Id from Teachers where TeacherName ='张三' )) + +-- 7.查询没有学全所有课程的同学的信息 +--学过但没学全的: +select Id from StudentInfo + where Id in ( select StudentId from StudentCourseScore group by StudentId having count(CourseId)=0 or count(CourseId)<(select count(CourseName) from CourseInfo)) +--一门课都没学过的: +select Id from StudentInfo + except + select StudentId from StudentCourseScore + +--把上面两个结合: +select id from StudentInfo + where Id in ( select StudentId from StudentCourseScore group by StudentId having count(CourseId)<(select count(CourseName) from CourseInfo) ) +union all + (select Id from StudentInfo + except + select StudentId from StudentCourseScore) + +--最后用列名显示: +select Id 学号, StudentName 姓名,Birthday 生日,Sex 性别,ClassId 班级 from StudentInfo where id in(select id from StudentInfo + where Id in ( select StudentId from StudentCourseScore group by StudentId having count(CourseId)<(select count(CourseName) from CourseInfo) ) +union all + (select Id from StudentInfo + except + select StudentId from StudentCourseScore)) + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +--查出01学生学过的所有课程: +select Id from StudentInfo where StudentCode =01 +select CourseId from StudentCourseScore where StudentId in (select Id from StudentInfo where StudentCode =01 ) + +--查出至少有一门课与01同学所学课程相同的学生学号 及 选课总数: +select StudentId ,count(CourseId) 所学相同课数量 + from StudentCourseScore + where CourseId in + (select CourseId from StudentCourseScore where StudentId in (select Id from StudentInfo where StudentCode =01 ) ) + group by StudentId having count(CourseId)>=1 + + +--利用学号来总查询: +select * from StudentInfo + where Id in (select StudentId from StudentCourseScore where CourseId in (select CourseId from StudentCourseScore where StudentId in (select Id from StudentInfo where StudentCode =01 ) ) + group by StudentId having count(CourseId)>=1) + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select * from StudentInfo + where Id in (select StudentId from StudentCourseScore where CourseId in (select CourseId from StudentCourseScore where StudentId in (select Id from StudentInfo where StudentCode =01 ) ) + group by StudentId having count(CourseId)=3) + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +--求出不是张三老师的课程编号: +select Id from Teachers where TeacherName = '张三' +select Id from CourseInfo where TeacherId !=(select Id from Teachers where TeacherName = '张三') + +--求出没学过张三老师课程编号的学生学号: +select StudentId from StudentCourseScore where CourseId not in (select Id from CourseInfo where TeacherId =(select Id from Teachers where TeacherName = '张三')) +--最后总查询: +select Id 学号, StudentName 姓名,Birthday 生日,Sex 性别,ClassId 班级 + from StudentInfo + where Id in (select StudentId from StudentCourseScore + where CourseId not in (select Id from CourseInfo where TeacherId !=(select Id from Teachers where TeacherName = '张三'))) +---以上是错误的 ,因为一个学生可以选多个课程,2且数据是一条一条的。 应该一开始就排除学过张三老师的学生学号 + + +select Id 学号, StudentName 姓名,Birthday 生日,Sex 性别,ClassId 班级 + from StudentInfo where Id not in (select StudentId from StudentCourseScore + where CourseId in (select Id from CourseInfo where TeacherId =(select Id from Teachers where TeacherName = '张三'))) + + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a3b4b279b8e9099b68fc6b3e55ee195c78a1740c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" @@ -0,0 +1,68 @@ +Create Database StarManagerDB +on +( +name='StarManagerDB_data', +filename='C:\app\StarManagerDB_data.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) +log on +( +name='StarManagerDB_log', +filename='C:\app\StarManagerDB_log.ldf', +size=5mb, +filegrowth=100mb +) +USE StarManagerDB +go +CREATE TABLE StarType +( +T_NO int primary key identity(1,1)not null, +T_NAME nvarchar(20) +) +set IDENTITY_INSERT StarType off +INSERT StarType(T_NO,T_NAME) +SELECT 1,'体育明星' UNION +SELECT 2,'IT明星' UNION +SELECT 3,'相声演员' + + +go +CREATE TABLE StarInfo +( +S_NO int primary key identity(1,1)not null, +S_NAME nvarchar(20), +S_AGE int, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) DEFAULT '中国大陆', +S_T_NO int foreign key references StarType(T_NO) +) +set IDENTITY_INSERT StarInfo on +INSERT StarInfo(S_NO,S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +SELECT 1,'梅西',30,'射门','阿根廷',1 UNIONa +SELECT 2,'科比',35,'过人','美国',1 UNION +SELECT 3,'蔡景现',40,'敲代码','中国',2 UNION +SELECT 4,'马斯克',36,'造火箭','外星人',2 UNION +SELECT 5,'郭德纲',50,'相声','中国',3 UNION +SELECT 6,'黄铮',41,'拼多多','中国',2 + +Select * from StarType +Select * from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3(S_AGE),S_NAME,S_NATIVE from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO,AVG(S_AGE),COUNT(S_T_NO) from StarInfo group by S_T_NO having Count(S_T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +Select top 1 S_NAME ,S_AGE,S_HOBBY,S_NATIVE +from StarInfo inner join StarType on StarInfo.S_T_NO=StarType.T_NO where T_NAME='体育明星' order by S_AGE desc + + + + + + + + + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3b2cbb741c70301b13728095c8760bd82e29dcc8 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" @@ -0,0 +1,53 @@ +create database HOUSE_DB +on +( +name='HOUSE_DB_data', +filename='C:\app\HOUSE_DB_data.mdf', +size=5mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_log', +filename='C:\app\HOUSE_DB_log.ldf', +size=5mb, +filegrowth=1mb +) +USE HOUSE_DB +go +CREATE TABLE HOUSE_TYPE +( +type_id int not null primary key, +type_name varchar(50) unique (type_name) +) +INSERT HOUSE_TYPE(type_id,type_name) +SELECT 1,'小户型' UNION +SELECT 2,'经济型' UNION +SELECT 3,'别墅' + +go +CREATE TABLE HOUSE +( +house_id int not null primary key, +house_name varchar(50), +house_price int, +type_id int foreign key references HOUSE_TYPE +) +INSERT HOUSE(house_id,house_name,house_price,type_id) +SELECT 1,'易居',1380,1 UNION +SELECT 2,'桐心居',2650,3 UNION +SELECT 3,'屈居',1650,2 + +Select * from HOUSE_TYPE +Select * from HOUSE + +--查询所有房屋信息 +Select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name LIKE '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +Select type_name,house_name from HOUSE_TYPE inner join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1(house_price),house_name from HOUSE order by house_price desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2580f817b63a083e47c35093dc3bebd2f0e8b90d --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery3.sql" @@ -0,0 +1,428 @@ +-- 练习题目: + + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select C.*,A.Score 数学成绩,B.Score 语文成绩 from StudentCourseScore A,StudentCourseScore B,StudentInfo C +where A.CourseId=2 and B.CourseId=1 AND A.StudentId=B.StudentId AND A.Score>B.Score AND C.Id=A.StudentId + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from StudentCourseScore A inner join StudentCourseScore B on A.StudentId = B.StudentId inner join StudentInfo C on +C.Id = A.StudentId AND A.CourseId = 1 and B.CourseId = 2 + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from(select * from + StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文'))A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学'))) B on A.StudentId=B.StudentId + + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId,StudentName,avg(Score) 平均成绩 from StudentCourseScore +inner join StudentInfo on StudentCourseScore.Id=StudentInfo.Id +group by StudentId,StudentName +having avg(Score)>=60 +order by StudentId + + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentCourseScore left join StudentInfo on StudentCourseScore.Id=StudentInfo.Id + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentId,StudentName,sum(CourseId) 选课总数,sum(Score) 总成绩 from StudentInfo +inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.Id +group by StudentId,StudentName,CourseId + + +-- 4.1 查有成绩的学生信息 +select distinct A.* from StudentInfo A right join StudentCourseScore B on A.Id=B.StudentId + + +-- 5.查询「李」姓老师的数量 +select * from Teachers where TeacherName like '李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +select D.* from Teachers A +inner join CourseInfo B on A.Id=B.TeacherId +inner join StudentCourseScore C on B.Id=C.CourseId +inner join StudentInfo D on C.StudentId=D.Id where TeacherName='张三' + + +-- 7.查询没有学全所有课程的同学的信息 + + + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + + + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b7889e97d9a6bf761a3ad0a7ff7865a2175ef962 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" @@ -0,0 +1,66 @@ +use master +go +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +go +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique +) +create table HOUSE +( + house_id int primary key identity(1,1) not null, + house_name varchar(50) not null, + house_price float not null default(0), + type_id int references HOUSE_TYPE(type_id) +) + +insert into HOUSE_TYPE values +('小户型'), +('经济型'), +('别墅') + +insert into HOUSE values +('闽大厕所',0,1), +('闽大操场',100,2), +('闽大公寓',300,3) + + +select * from HOUSE +select * from HOUSE_TYPE + +--查询所有房屋信息 +select * from HOUSE_TYPE a inner join HOUSE b on a.type_id=b.type_id + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select HOUSE.type_id,house_name,type_name from HOUSE +inner join HOUSE_TYPE on HOUSE.type_id= HOUSE_TYPE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_id,house_name,house_price from HOUSE +where house_price in (select max(house_price) from HOUSE ) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..821eaed1b29fabae1533224ae6e4bacbf2dfaa47 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery2.sql" @@ -0,0 +1,270 @@ +use master +go + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + + +select * from StudentInfo --学生信息表 +select * from Teachers --教师 +select * from CourseInfo --课程信息表 +select * from StudentCourseScore--学生课程成绩 + +-- 1.查询 "数学 "课程 比" 语文 "课程 成绩高的 学生的信息 及 课程分数 + +select b.*,a1.Score 语文,a2.Score 数学 from StudentCourseScore a1 , StudentCourseScore a2 ,StudentInfo b where +a1.CourseId='1' and a2.CourseId='2' and a1.Score=60 + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentCourseScore a inner join StudentInfo b on a.StudentId=b.Id + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select b.StudentId,a.StudentName,count(*),sum(b.Score) +from StudentInfo a left join StudentCourseScore b on a.Id=b.StudentId +group by b.StudentId,a.StudentName + +-- 4.1 查有成绩的学生信息 +select b.* from StudentCourseScore a inner join StudentInfo b on a.StudentId=b.Id + + +-- 5.查询「李」姓老师的数量 +select * from Teachers where TeacherName like '李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +select a.*,c.TeacherName from StudentInfo a inner join StudentCourseScore b +on a.Id=b.StudentId inner join Teachers c +on c.Id=b.CourseId where b.CourseId=1 + + +select * from StudentInfo --学生信息表 +select * from Teachers --教师 +select * from CourseInfo --课程信息表 +select * from StudentCourseScore--学生课程成绩 +-- 7.查询没有学全所有课程的同学的信息 +select * from StudentInfo where Id in +(select id from StudentCourseScore group by CourseId,Id having COUNT(CourseId)<3) + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select * from + + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5ec9d3fcd7ae3daf5e74a0844c7e5e03158632f8 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery3.sql" @@ -0,0 +1,48 @@ +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20) +) + +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) + +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') + +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + +select * from StarType +select * from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技, S_NATIVE 籍贯 from StarInfo order by S_AGE DESC + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select count(S_NO) 明星人数,AVG(S_AGE) 明星平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 max(S_AGE)年龄,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 +from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO WHERE B.T_NAME='体育明星' GROUP BY S_NAME,S_HOBBY,S_NATIVE \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4a68075f3c11ecc51c13c2e21a61107c955027f4 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" @@ -0,0 +1,65 @@ +use master +go +create database HOUSE_DB +on +( name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity, + type_name varchar(50) unique not null +) + +create table HOUSE +( + house_id int primary key identity, + house_name varchar(50) not null, + house_price float default(0) not null, + type_id int references HOUSE_TYPE(type_id) not null +) + +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +select*from HOUSE_TYPE +insert into HOUSE values('平家',100,'1'),('小家',200,'2'),('小楼',100000000,'3') + +select*from HOUSE_TYPE +select*from HOUSE + +--4、添加查询 +--查询所有房屋信息 +select * from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id + +--使用模糊查询包含”型“字的房屋类型信息 +select*from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋的名称,house_price 租金 + from HOUSE + order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price 最高月租, house_name 房屋名称 from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id order by house_price desc diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e97627e2bf9f757200e7fc800fe467f8d93ee939 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery3.sql" @@ -0,0 +1,63 @@ +use master +go +create database StarManagerDB +on +( name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity, + T_NAME nvarchar(20) +) + +create table StarInfo +( + S_NO int primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +insert into StarType values('体育明星'),('IT明星'),('相声明星') + +insert into StarInfo values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1), + ('察景现',40,'敲代码',default,2),('马斯克',36,'造火箭','外星人',2), + ('郭德纲',50,'相声',default,3),('黄铮',41,'拼多多',default,2) + + +select*from StarType +select*from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 + from StarInfo + order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO ,count(*) 明星人数,avg(S_AGE) 明星平均年龄 + from StarInfo + group by S_T_NO + having count(*) >2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_AGE 最大的姓名,S_HOBBY 特技,S_NATIVE 籍贯 + from StarType + join StarInfo + on StarInfo.S_T_NO=StarType.T_NO + where T_NAME='体育明星' + order by S_AGE desc diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery4.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ef06029e981df3223f91eebefd245a9cd1b47ca6 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery4.sql" @@ -0,0 +1,84 @@ +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore +-- 练习题目: + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select C.* ,A.Score 数学成绩,b.Score 语文成绩 +from StudentCourseScore A,StudentCourseScore B,StudentInfo C +WHERE A.CourseId=1 AND B.CourseId=2 AND A.StudentId=B.StudentId AND A.Score>B.Score and C.Id=A.StudentId + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select b.StudentId +from StudentCourseScore A,StudentCourseScore B +WHERE A.CourseId=1 AND B.CourseId=2 AND A.StudentId=B.StudentId + + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from +(select * from StudentCourseScore A where CourseId=(select id from CourseInfo where CourseName='数学')) A left JOIN +(select * from StudentCourseScore A where CourseId=(select id from CourseInfo where CourseName='语文')) B ON A.StudentId=B.StudentId + + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from +(select * from StudentCourseScore A where CourseId=(select id from CourseInfo where CourseName='数学')) A left JOIN +(select * from StudentCourseScore A where CourseId=(select id from CourseInfo where CourseName='语文')) B ON A.StudentId=B.StudentId +where b.CourseId is null + + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentCode 学生编号,StudentName 学生姓名 ,avg(Score) 平均成绩 from StudentInfo +right join StudentCourseScore on StudentId=StudentCode group by StudentCode,StudentName having avg(Score)>=60 + + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentInfo where StudentCode in(select StudentId from StudentCourseScore ) + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select A.StudentCode 学生编号,A.StudentName 姓名,COUNT(B.CourseId)选课总数,sum(b.Score)总成绩 from StudentInfo A left join StudentCourseScore B +ON A.Id=B.StudentId group by A.StudentCode ,A.StudentName + + +-- 4.1 查有成绩的学生信息 +select a.StudentName,b.StudentId,a.ClassId,a.Birthday,a.Sex,a.StudentCode from StudentInfo a left join StudentCourseScore b +on a.Id=b.StudentId where Score is not null group by StudentName,StudentId,ClassId,Birthday,Sex,StudentCode + + +-- 5.查询「李」姓老师的数量 +select count(TeacherName) 李姓老师的数量 from Teachers where TeacherName like '李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +select b.* from StudentCourseScore a inner join StudentInfo b on a.StudentId=b.Id +where CourseId='2' + + +-- 7.查询没有学全所有课程的同学的信息 +select StudentId,b.StudentName,b.ClassId,b.Birthday,b.Sex,b.StudentCode,count(CourseId)课程数量 from StudentCourseScore A +left join StudentInfo B ON A.StudentId=B.Id +where CourseId in (select id from CourseInfo) group by StudentId,b.StudentName,b.ClassId,b.Birthday,b.Sex,b.StudentCode +having count(CourseId)<3 +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select * from StudentInfo where StudentCode in( +select distinct StudentId from StudentCourseScore where CourseId in( +select CourseId from StudentCourseScore where StudentId=1) +and StudentId != 1 +) +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select * from StudentInfo +where StudentCode in(select A.StudentId from StudentCourseScore A cross join StudentCourseScore B +where B.StudentId = 1 and A.StudentId != 1 +group by A.StudentId +having count(distinct A.CourseId) = count(distinct B.CourseId)) + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + select StudentName from StudentInfo where Id not in(select StudentId from StudentCourseScore where CourseId + in(select Id from CourseInfo where TeacherId in(select Id from Teachers where TeacherName='张三')) group by StudentId +) + + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d3c6906e0d4d06fdf0e55ff908b7c8935a860985 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery1.sql" @@ -0,0 +1,64 @@ +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\HOUSE_DB.mdf', +size=500mb, +filegrowth=15%, +maxsize=15mb +) +log on +( +name='HOUSE_DB_log', +filename='D:\HOUSE_DB_log.ldf', +size=500mb, +filegrowth=15%, +maxsize=15mb +) +go +use HOUSE_DB +go +drop table HOUSE_TYPE +create table HOUSE_TYPE +( +type_id int primary key not null identity(1,1), +type_name varchar(50) unique not null +) +drop table HOUSE +create table HOUSE +( +house_id int not null primary key identity(1,1) , +house_name varchar(50) not null, +house_price float default(0) not null, +type_id int foreign key references HOUSE_TYPE(type_id), +) +select * from HOUSE_TYPE +insert into HOUSE_TYPE +select '小户型' union +select '经济型' union +select '别墅' +select * from HOUSE +insert into HOUSE(house_name,house_price,type_id) +select '豪华',1000,1union +select '梦幻型',900,2union +select '古典型',500,3 + +--查询所有房屋信息 +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name, house_price from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 类型名称 from HOUSE inner join HOUSE_TYPE on +HOUSE.type_id=HOUSE_TYPE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 MAX(house_price),house_name from HOUSE GROUP BY house_name,house_price +order by house_price desc + + + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bdd3d5cca3dd8fabc4774ae259dc55a9e02c8751 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" @@ -0,0 +1,65 @@ +create database StarManagerDB +on +( +name='StarManagerDB', +filename='D:\StarManagerDB.mdf', +size=500mb, +filegrowth=15%, +maxsize=15mb +) +log on +( +name='StarManagerDB_log', +filename='D:\StarManagerDB_log.ldf', +size=500mb, +filegrowth=15%, +maxsize=158mb +) +go +use StarManagerDB +go +drop table StarType +create table StarType +( +T_NO int not null primary key , +T_NAME nvarchar(20) +) +drop table StarInfo +create table StarInfo +( +S_NO int not null primary key, +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int foreign key references StarType(T_NO) +) +select * from StarType +insert into StarType +select 1,'体育明星' union +select 2,'IT 明星' union +select 3,'相声演员' + +select * from StarInfo +insert into StarInfo(S_NO,S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +select 1,'梅西',30,'射门','阿根廷',1 union +select 2,'科比',35,'过人','美国',1 union +select 3,'蔡景现',40,'敲代码','中国',2 union +select 4,'马斯克',36,'造火箭','外星人',2 union +select 5,'郭德纲',50,'相声','中国',3 union +select 6,'黄铮',41,'拼多多','中国',2 + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 MAX(S_AGE) 最大年龄,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo + group by S_AGE,S_HOBBY,S_NATIVE order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select COUNT(S_NO) 明星人数,AVG(S_AGE) 平均年龄 from StarInfo having COUNT(S_NO)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 MAX(S_AGE), T_NAME,S_NAME,S_HOBBY,S_NATIVE from StarType inner join StarInfo +on StarType.T_NO=StarInfo.S_T_NO where T_NAME='体育明星' +group by S_AGE,T_NAME,S_NAME,S_HOBBY,S_NATIVE +order by S_AGE desc + + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..909b04f6a1befaba7d80648324be61cac3b74e2c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery3.sql" @@ -0,0 +1,250 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go +select * from StudentInfo +select * from Teachers +select * from CourseInfo --课程信息 +select * from StudentCourseScore --分数表 + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select C.*,A.Score,B.Score from StudentCourseScore A,StudentCourseScore B,StudentInfo C +where A.CourseId=2 and B.CourseId=1 and A.StudentId=B.StudentId AND A.Score>B.Score AND C.ID=A.StudentId + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from CourseInfo where CourseName='数学' +union +select * from CourseInfo where CourseName='语文' +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) B on A.StudentId=B.StudentId + + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId,StudentName,avg(Score) 平均成绩 from StudentCourseScore +inner join StudentInfo on StudentCourseScore.Id=StudentInfo.Id +group by StudentId,StudentName +having avg(Score)>=60 +order by StudentId + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentCourseScore left join StudentInfo on StudentCourseScore.Id=StudentInfo.Id + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentId,StudentName,sum(CourseId) 选课总数,sum(Score) 总成绩 from StudentInfo +inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.Id +group by StudentId,StudentName,CourseId +-- 4.1 查有成绩的学生信息 +select distinct A.* from StudentInfo A right join StudentCourseScore B on A.Id=B.StudentId +-- 5.查询「李」姓老师的数量 +select * from Teachers where TeacherName like '李%' +-- 6.查询学过「张三」老师授课的同学的信息 +select D.* from Teachers A +inner join CourseInfo B on A.Id=B.TeacherId +inner join StudentCourseScore C on B.Id=C.CourseId +inner join StudentInfo D on C.StudentId=D.Id where TeacherName='张三' +-- 7.查询没有学全所有课程的同学的信息 +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b6aee383657137ea0ae0dbef1b58fbae9f5a1ffe --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" @@ -0,0 +1,59 @@ +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=20% +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=20% +) +go +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity(1,1), + type_name varchar(50) unique +) + +create table HOUSE +( + house_id int primary key identity(1,1), + house_name varchar(50), + house_price float default(0), + type_id int references HOUSE_TYPE(type_id) +) + +insert into HOUSE_TYPE values +('小户型'),('经济型'),('别墅') + +insert into HOUSE values +('天元山','30000',1), +('奥比岛','20000',2), +('摩尔庄园','10000',3) + +--查询所有房屋信息 +select * from HOUSE full join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price DESC +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name from HOUSE full join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_price,house_name from HOUSE where house_price in (select max(house_price) from HOUSE) + + \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..347c4a3bc915f461ba0d5e50a457d362b9930f3e --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" @@ -0,0 +1,63 @@ +use master +go + +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=20% +) +log on +( + name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=20% +) +go +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') + +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo where S_AGE in (select top 3 S_AGE from StarInfo order by S_AGE DESC) +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 类型编号,count(S_T_NO)人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarType inner join StarInfo on StarInfo.S_T_NO=StarType.T_NO +where T_NAME='体育明星' and S_AGE in (select max(S_AGE) from StarInfo where S_T_NO=1) + +select * from StarInfo +select * from StarType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery4.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4749e899faa8cc8a9c099394b66397c3d74f1112 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery4.sql" @@ -0,0 +1,379 @@ +use master +go + +create database ClassicDb +GO +use ClassicDb +GO +create table StudentInfo +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null +) +GO +-- select * from StudentInfo +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) +GO +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) +go +-- select * from Teachers +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') +GO +create table CourseInfo +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null +) +go + +-- select * from CourseInfo +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) +GO + + + +create table StudentCourseScore +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null +) +go +-- select * from StudentCourseScore +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) +go +-- 练习题目: + + +select * from StudentInfo +select * from StudentCourseScore +select * from CourseInfo +select * from Teachers + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select C.*,A.Score 数学,B.Score 语文 from StudentCourseScore A,StudentCourseScore B,StudentInfo C +where A.CourseId=1 AND B.CourseId=2 AND A.StudentId=B.StudentId AND A.Score>B.Score AND C.Id=A.StudentId +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select c.*,a.CourseId,b.CourseId from StudentCourseScore A join StudentCourseScore B on A.StudentId=B.StudentId +join StudentInfo C on C.Id=A.StudentId +where A.CourseId=1 and B.CourseId=2 +-- 1.2 查询存在" 数学2 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select StudentName 姓名,A.StudentId,A.CourseId,b.StudentId,b.CourseId from +(select * from StudentCourseScore where CourseId in (select Id from CourseInfo where CourseName='数学')) A +left join +(select * from StudentCourseScore where CourseId in (select Id from CourseInfo where CourseName='语文')) B +on A.StudentId=B.StudentId +inner join StudentInfo C on c.Id=a.StudentId +-- 1.3 查询 不存在" 数学2 " 课程但存在" 语文1 "课程的 学生 情况 +select * from +(select * from StudentCourseScore where CourseId in(select Id from CourseInfo where CourseName='语文')) A +left join +(select * from StudentCourseScore where CourseId in(select Id from CourseInfo where CourseName='数学')) B +on a.StudentId=b.StudentId +where b.CourseId is null + +select * from StudentCourseScore +select * from StudentInfo +-- 2.查询 平均成绩大于等于 60 分 的 同学 的 学生编号 和 学生姓名 和 平均成绩 +select StudentId 学生编号,StudentName 学生姓名,avg(Score) 平均成绩 from StudentCourseScore +inner join StudentInfo on StudentInfo.Id=StudentCourseScore.StudentId +group by StudentId,StudentName having avg(Score)>=60 + + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentInfo where Id in(select StudentId from StudentCourseScore) +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select A.Id 学生编号,a.StudentName 学生姓名,count(*) 选课总数,sum(Score) 总成绩 from StudentInfo A +left join StudentCourseScore B on A.Id=B.StudentId +group by A.Id,StudentName + + +-- 4.1 查有成绩的学生信息 +select * from StudentCourseScore a +left join StudentInfo b on a.StudentId=b.Id +-- 5.查询「李」姓老师的数量 +select count(*)数量 from Teachers where TeacherName like '李%' +select * from Teachers + +-- 6.查询 学过「张三」老师授课 的 同学的信息 + +select * from StudentInfo A +inner join StudentCourseScore B on A.Id=B.StudentId +inner join (select Id from CourseInfo where TeacherId in (select Id from Teachers where TeacherName='张三'))C on B.CourseId=C.Id + +-- 7.查询没有学全所有课程的同学的信息 + +select * from StudentInfo A join +(select StudentId,count(*) 学习课程数量 from StudentCourseScore group by StudentId having count(*) not in (select count(*) from CourseInfo))B +on A.Id=b.StudentId + +-- 8.查询 至少有一门课与学号为" 01 "的同学所学相同的 同学的信息 +select CourseId from StudentCourseScore where StudentId=1 + + +select distinct B.* from StudentCourseScore A +inner join StudentInfo B on A.StudentId=B.Id +where CourseId in (select CourseId from StudentCourseScore where StudentId=1) +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select c.* from (select courseId from StudentCourseScore where StudentId=1) A +left join StudentCourseScore B on A.CourseId=b.CourseId +inner join StudentInfo C on B.StudentId=C.Id +group by C.Id,StudentCode,StudentName,Birthday,Sex,ClassId +having count(*)=(select count(*) from StudentCourseScore where StudentId=1) +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select StudentName 学生姓名 from StudentInfo where Id not in +(select StudentId from StudentCourseScore where CourseId in (select Id from courseInfo where TeacherId in (select Id from Teachers where TeacherName='张三'))) + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 +select StudentId,count(*)不及格次数,StudentName,avg(Score) 平均成绩 from StudentCourseScore +left join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id +where Score<60 group by StudentId,StudentName having count(*)>=2 + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 +select b.* from StudentCourseScore a +join StudentInfo b on a.StudentId=b.Id +where courseId in(select Id from CourseInfo where CourseName='数学') and Score<60 order by Score DESC +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select StudentName,avg(Score) 平均成绩,sum(Score) 所有课程的成绩 from StudentInfo A +left join StudentCourseScore B on A.Id=b.StudentId +group by StudentName order by avg(Score) DESC + +-- 14.查询各科成绩最高分、最低分和平均分: +select CourseId,max(Score) 最高分,MIN(Score) 最低分,avg(Score) 平均分 from StudentCourseScore group by CourseId +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出 课程号 和 选修人数 ,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..670b31ad8eead5bd4d9d9211f479e83968324e48 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery2.sql" @@ -0,0 +1,87 @@ +create database HOUSE_DB + + + +on + +( + + name='HOUSE_DB', + + filename='E:\HOUSE_DB.mdf', + + size=5mb, + + maxsize=50mb, + + filegrowth=10% + +) + +log on + +( + + name='HOUSE_DB_log', + + filename='E:\HOUSE_DB_log.ldf', + + size=5mb, + + maxsize=50mb, + + filegrowth=10% + +) + +go + +use HOUSE_DB +go + +drop table HOUSE_TYPE + +create table HOUSE_TYPE --房屋类型表 +( +typeid int not null primary key identity(1,1),--类型编号 +typename varchar(50) not null unique --类型名称 +) + +drop table HOUSE + +create table HOUSE --房屋信息表 +( +houseId int not null primary key identity(1,1), --房屋编号 +housename varchar(50) not null, --房屋名称 +houseprice float not null default(0), --房租 +typeId int not null foreign key references HOUSE_TYPE (typeid) --房屋类型 +) +insert into HOUSE_TYPE +select '小户型'union +select '经济型'union +select '别墅' + +select* from HOUSE_TYPE + +insert into HOUSE(housename ,houseprice,typeId) +select '厕所',20,1 union +select '奥里给',10 ,2union +select '八嘎压路' ,100,3 + +select *from HOUSE --查询所有房屋信息 + +select* from HOUSE_TYPE where typename like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select housename 房屋名称,houseprice 租金 from house order by houseprice desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select M.housename 房屋名称,L.typename 类型名称 from HOUSE M inner join HOUSE_TYPE L on M.typeId=L.typeid + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 max(houseprice) 月租最高,housename 房屋名称 from HOUSE group by housename,houseprice order by houseprice desc + + + + + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ae1021b4bcc7f7dda2b4bbf8fc4d0aaf526d9638 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery3.sql" @@ -0,0 +1,91 @@ +create database StarManagerDB + + + +on + +( + + name='StarManagerDB', + + filename='E:\StarManagerDB.mdf', + + size=5mb, + + maxsize=50mb, + + filegrowth=10% + +) + +log on + +( + + name='StarManagerDB', + + filename='E:\StarManagerDB_log.ldf', + + size=5mb, + + maxsize=50mb, + + filegrowth=10% + +) + +go + +use HOUSE_DB +go + +drop table starType +create table starType --明星类型表 +( +T_NO int not null primary key ,--明星类型编号 +T_NAME nvarchar(20) --明星类型 +) + +drop table StarInfo + +create table StarInfo --(明星信息表) +( +S_NO int not null primary key , --明星编号 +S_NAME nvarchar(20) not null , +S_AGE int not null, --年龄 +S_HOBB nvarchar(20) , --特技 +S_NATIVE nvarchar(20) default('中国大陆'),-- 籍贯 +S_T_NO int foreign key references starType(T_NO) --明星类型编号 +) + +insert into starType +select 1,'体育明星' union +select 2,'IT明星' union +select 3,'相声演员' + +select * from starType + +insert into StarInfo +select 1,'梅西',30,'射门','阿根廷',1 union +select 2,'科比',35,'过人','美国',1 union +select 3,'蔡景现',40,'敲代码','中国',2 union +select 4,'马斯克',36,'造火箭','外星人',2 union +select 5,'郭德纲',50,'相声','中国',3 union +select 6,'黄铮',50,'拼多多','中国',2 union +select 7,'杨梦林',21,'奥特曼变身第一人','光之国',2 + +select * from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 max(S_AGE) 年龄最大,S_NAME 明星的姓名,S_HOBB 特技,S_NATIVE 籍贯 from StarInfo group by S_NAME,S_HOBB , S_NATIVE,S_NAME + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select count(*)明星人数,avg(S_AGE) 明星平均年龄 from StarInfo having count(*)>2 + + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 max(M.S_AGE) 年龄最大,M.S_NAME 明星的姓名,M.S_HOBB 特技,M.S_NATIVE 籍贯,L.T_NAME 明星类型 from StarInfo M inner join starType L on M.S_T_NO=L.T_NO + where L.T_NAME = '体育明星' group by M.S_NAME ,M.S_HOBB,M.S_NATIVE,L.T_NAME,M.S_AGE + + + StarInfo M \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery4.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b22f3a36a874e9e07d129fa23811eccc60b2557e --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery4.sql" @@ -0,0 +1,457 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +select * from StudentInfo --学生信息表 +select * from Teachers --老师信息表 +select * from CourseInfo --课程表 +select * from StudentCourseScore --分数表 + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select C.*,A.Score 数学成绩,B.Score 语文成绩 from StudentCourseScore A,StudentCourseScore B,StudentInfo C +where A.CourseId=2 AND B.CourseId=1 AND a.StudentId=B.StudentId AND A.Score>B.Score AND C.Id=A.StudentId + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + + select * from StudentInfo where Id in (select A.StudentId from StudentCourseScore A,StudentCourseScore B +where A.CourseId=2 and B.CourseId=1 AND A.StudentId=B.StudentId ) + + + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + + +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) B on A.StudentId=B.StudentId + + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select * from StudentInfo --学生信息表 +select * from Teachers --老师信息表 +select * from CourseInfo --课程表 +select * from StudentCourseScore --分数表 + + +select StudentCourseScore .StudentId 学生编号,StudentName 学生姓名,avg(Score) 平均成绩 from StudentInfo inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +group by StudentCourseScore .StudentId,StudentName having avg(Score)>=60 +-- 3.查询在 成绩 表存在成绩的学生信息 + +select distinct M.* from StudentCourseScore Y left join StudentInfo M on Y.StudentId=M.Id + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentId 学生编号,StudentName 学生姓名,sum(CourseId) 选课总数, sum(Score) 总成绩 from StudentInfo inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +group by StudentCourseScore .StudentId,StudentName + + + +-- 4.1 查有成绩的学生信息 +select distinct M.* from StudentCourseScore Y left join StudentInfo M on Y.StudentId=M.Id + +-- 5.查询「李」姓老师的数量 + +select count(*) 李姓老师的数量 from Teachers where TeacherName like '%李%' + + + +-- 6.查询学过「张三」老师授课的同学的信息 +select distinct M.* from StudentCourseScore Y left join StudentInfo M on Y.StudentId=M.Id where CourseId ='2' + + + +-- 7.查询没有学全所有课程的同学的信息 + +select * from StudentInfo --学生信息表 +select * from Teachers --老师信息表 +select * from CourseInfo --课程表 +select * from StudentCourseScore --分数表 + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + + + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select * from StudentInfo --学生信息表 +select * from Teachers --老师信息表 +select * from CourseInfo --课程表 +select * from StudentCourseScore --分数表 + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 +select StudentId 学号,Studentname 姓名,avg(Score) 平均成绩 from StudentCourseScore Y inner join StudentInfo M on Y.StudentId =M.Id where Score<60 and CourseId>=2 group by StudentId,Studentname + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + +select M.Id 学号 ,Y.Score 数学分数 from StudentCourseScore Y inner join CourseInfo M on Y.CourseId =M.Id where Score<60 and CourseName = '数学' order by Score desc + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + +select L.Id 学生编号,M.Id 课程编号, avg(Score) 平均成绩 from StudentCourseScore Y inner join CourseInfo M on Y.CourseId =M.Id inner join StudentInfo L on Y.Id =L.Id group by M.Id,L.Id order by avg(Score) desc + +-- 14.查询各科成绩最高分、最低分和平均分: +select Y.CourseId 课程编号,max(Score) 最高分, min(Score) 最低分, avg(Score)平均分 from StudentCourseScore Y inner join CourseInfo M on Y.CourseId =M.Id group by Y.CourseId + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + + + + --及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + --要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + --按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +--*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + +select * from StudentInfo --学生信息表 +select * from Teachers --老师信息表 +select * from CourseInfo --课程表 +select * from StudentCourseScore --分数表 + +-- 18.查询各科成绩前三名的记录 + +select top 3 Score,M.Id from StudentCourseScore Y inner join CourseInfo M on Y.CourseId =M.Id group by M.Id,Score order by Score desc + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + +select M.Id 学生学号, M.StudentName 学生姓名 from StudentCourseScore Y inner join StudentInfo M on Y.Id =M.Id where CourseId ='2' +-- 21.查询男生、女生人数 + +select count(sex)男女生人数 from StudentInfo group by sex + + + +-- 22.查询名字中含有「风」字的学生信息 + +select id 学号,StudentName 姓名 from StudentInfo where StudentName like '%风%' group by id, StudentName + + +select * from StudentInfo --学生信息表 +select * from Teachers --老师信息表 +select * from CourseInfo --课程表 +select * from StudentCourseScore --分数表 + + +-- 23.查询同名同性学生名单,并统计同名人数 + + +select count(*)人数 from StudentInfo A ,StudentInfo B where A.StudentName = B.studentName and A.id!=B.id + +-- 24.查询 1990 年出生的学生名单 +select * from StudentInfo where Birthday ='1990' + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + +select L.Id 学生编号,M.Id 课程编号, avg(Score) 平均成绩 from StudentCourseScore Y inner join CourseInfo M on Y.CourseId =M.Id inner join StudentInfo L on Y.Id =L.Id group by M.Id,L.Id order by avg(Score) desc ,M.Id asc + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + +select Y.Id 学号,M.StudentName 姓名 ,avg(Score) 平均成绩 from StudentCourseScore Y inner join StudentInfo M on Y.Id=M.Id group by Y.id,M.StudentName having avg(Score)>=85 + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + +select Y.StudentId 学号, StudentName 学生姓名 ,Score 分数 from StudentCourseScore Y inner join CourseInfo M on Y.CourseId =M.Id inner join StudentInfo L on Y.StudentId=L.Id where CourseName = '数学'and Score <60 group by StudentName,Score,Y.StudentId + +select * from StudentInfo --学生信息表 +select * from Teachers --老师信息表 +select * from CourseInfo --课程表 +select * from StudentCourseScore --分数表 + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + +select + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/50\351\242\230.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/50\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2d9a1c0733e26c3526dfaca456b9b96abaabb9ba --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/50\351\242\230.sql" @@ -0,0 +1,270 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + +go + +select * from CourseInfo +select * from StudentCourseScore +select * from Teachers +select * from StudentInfo + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select c.*,a.Score 语文成绩,b.Score 数学成绩 from StudentCourseScore a +left join StudentCourseScore b on a.CourseId = 1 and b.CourseId = 2 +left join StudentInfo c on c.Id = b.StudentId +where a.Score < b.Score and a.StudentId = b.StudentId + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +--法一(自己) +select * from StudentCourseScore a +left join StudentCourseScore b on a.StudentId = b.StudentId and (a.CourseId = 1 and b.CourseId = 2) +left join StudentInfo c on c.Id = a.StudentId +where b.Id is not null +--法二(老师) +select * from StudentCourseScore A inner join StudentCourseScore B on A.StudentId = B.StudentId inner join StudentInfo C on +C.Id = A.StudentId AND A.CourseId = 1 and B.CourseId = 2 + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId +where b.CourseId is null + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId 学号, StudentName 姓名,AVG(Score)平均分 from StudentInfo a +inner join StudentCourseScore b on a.Id = b.StudentId + group by StudentId,StudentName having AVG(Score)>60 + + +-- 3.查询在 成绩 表存在成绩的学生信息 +select StudentId,StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore a +left join StudentInfo b on a.StudentId = b.Id +where Score is not null group by StudentId,StudentCode,StudentName,Birthday,Sex,ClassId + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentId 学号,StudentName 姓名, count(b.Id)选课总数,sum(Score)课程总分 from StudentInfo a +left join StudentCourseScore b on a.Id = b.StudentId +group by StudentId,StudentName + +-- 4.1 查有成绩的学生信息 +select StudentId 学号,StudentName 姓名, count(b.Id)选课总数,sum(Score)课程总分 from StudentInfo a +left join StudentCourseScore b on a.Id = b.StudentId +where StudentId is not null +group by StudentId,StudentName + +-- 5.查询「李」姓老师的数量 +select count(TeacherName) from Teachers where TeacherName like '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 张三教语文 +select c.*,a.* from StudentCourseScore a +left join Teachers b on a.Id = b.Id +left join StudentInfo c on a.StudentId=c.Id +where a.CourseId = 1 + +-- 7.查询没有学全所有课程的同学的信息 +select a.*,count (CourseId) 所学的总课程 from StudentInfo a +left join StudentCourseScore b on a.Id = b.StudentId +group by a.Id,StudentCode,StudentName,Birthday,Sex,ClassId having count (CourseId) !=3 + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select * from StudentCourseScore where StudentId in(select * from StudentInfo where StudentCode = 01 ) + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..27ab58224fa2dc2268b64e55d4c24ebb5a63645c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" @@ -0,0 +1,52 @@ +create database GoodsDB +on +( + name = 'GoodsDB', + filename = 'D:\GoodsDB.mdf' +) +log on +( + name = 'GoodsDB_log', + filename = 'D:\GoodsDB_log.ldf' +) +use GoodsDB + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null , + TypeID int references GoodsType(TypeID) +) +insert into GoodsType values +('服装内衣'),('鞋包配饰'),('手机数码') +insert into GoodsInfo values +('提米小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋 ','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','希捷',800,3) +select * from GoodsType +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 名称,GoodsColor 颜色 ,GoodsMoney 价格 from GoodsInfo +where GoodsMoney in (select max(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TYPEID 编号,MIN(GoodsMoney)最低价, Max(GoodsMoney)最高价,avg(GoodsMoney)平均价 from GoodsInfo +group by TYPEID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor = '红色' and GoodsMoney betw \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..be03d38ec0e84f3337c50b4821615ab2636a0c2c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" @@ -0,0 +1,57 @@ +create database HOUSE_DB +on +( + name = 'HOUSE_DB', + filename = 'D:\HOUSE_DB.mdf', + size = 5, + maxsize = 50, + filegrowth = 10% +) +log on +( + name = 'HOUSE_DB_log', + filename = 'D:\HOUSE_DB_log.ldf' +) + +use HOUSE_DB + +create table HOUSE_TYPE +( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null unique +) +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE values +('小户型'), +('经济型'), +('别墅') +insert into HOUSE values +('学区房',25000,1), +('别墅型',40000,3), +('小区房',15000,2), +('复古型',35000,3), +('西式复古型',40000,3) +select * from HOUSE_TYPE +select * from HOUSE + +--查询所有房屋信息 +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE where house_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select * from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select b.house_name 名称,a.type_name 房屋类型名称 from HOUSE_TYPE a +inner join HOUSE b on a.type_id = b.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select * from HOUSE where house_price in(select max(house_price) from HOUSE) diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..290d9d6de9095267f7297da5c068df9c15de7d99 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery3.sql" @@ -0,0 +1,41 @@ +create database StarManagerDB + +use StarManagerDB + +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null , + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) +select * from StarType +select * from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 * from StarInfo order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select count(S_T_NO)明星人数,AVG(S_AGE)平均年龄 from StarInfo +group by S_T_NO having count(S_T_NO)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_T_NO = 1 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fb1260eb03355e20f1154b4c7ece1b1eae2adef9 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" @@ -0,0 +1,39 @@ +create database HOUSE_DB +on +( + name='HOUSE_DB', + size=5, + filename='D:\HOUSE_DB.mdf', + filegrowth=1, + maxsize=50 +) +log on +( + name='HOUSE_DB_log', + size=5, + filename='D:\HOUSE_DB_log.ldf', + filegrowth=1, + maxsize=50 +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int not null primary key identity, + type_name varchar(50) not null unique, +) +create table HOUSE +( + house_id int not null primary key identity, + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null foreign key references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +insert into HOUSE values('五五开',500,1),('PDD',800,3),('大司马',600,2) +select * from HOUSE +select * from HOUSE_TYPE where type_name like '%型%' +select house_name,house_price from HOUSE order by house_price desc +select house_name,type_name from HOUSE t inner join HOUSE_TYPE x on t.type_id=x.type_id +select top 1 house_name,house_price from HOUSE order by house_price desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d21af48bf7136a534e45da0a7b5b4fb025949881 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery2.sql" @@ -0,0 +1,41 @@ +create database StarManagerDB +on +( + name='StarManagerDB', + size=5, + filename='D:\StarManagerDB.mdf', + filegrowth=1, + maxsize=50 +) +log on +( + name='StarManagerDB_log', + size=5, + filename='D:\StarManagerDB_log.ldf', + filegrowth=1, + maxsize=50 +) +go +use StarManagerDB +go +create table StarType +( + T_NO int not null primary key identity, + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int not null primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) +insert into StarType values ('体育明星'),('唱歌明星'),('相声演员') +insert into StarInfo values('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo order by S_AGE desc +select count(S_NAME) 明星人数,avg(S_AGE) 明星平均年龄 from StarInfo group by S_T_NO having count(S_NAME)>2 +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo where S_T_NO=1 order by S_AGE desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8f5bd9ca28fb67199072b2bee84a314ae57e8d2e --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery3.sql" @@ -0,0 +1,235 @@ +create database ClassicDb +GO + + +use ClassicDb +GO + + +create table StudentInfo +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null +) + + +GO + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + + + + + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + + +go + + +select * from StudentInfo +select * from CourseInfo +select * from Teachers +select * from StudentCourseScore + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高 的 学生的信息 及 课程分数 + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文') +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId 学生编号,StudentInfo.StudentName 学生姓名,AVG(Score) 平均分 from StudentCourseScore +inner join StudentInfo on StudentInfo.Id=StudentCourseScore.StudentId group by StudentId ,StudentInfo.StudentName +having AVG(Score)>=60 +-- 3.查询在 成绩 表存在成绩的学生信息 +select distinct StudentInfo.* from StudentCourseScore +inner join StudentInfo on StudentInfo.Id=StudentCourseScore.StudentId +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentInfo.Id 学生编号,StudentName 学生姓名,count( StudentCourseScore.StudentId)选课总数, +sum(StudentCourseScore.Score) 总成绩 from StudentInfo +left join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +group by StudentInfo.Id,StudentName,StudentCourseScore.StudentId +-- 4.1 查有成绩的学生信息 +select distinct StudentInfo.* from StudentInfo +inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +-- 5.查询「李」姓老师的数量 +select COUNT(*)数量 from Teachers where TeacherName like '李%' +-- 6.查询学过「张三」老师授课的同学的信息 + +-- 7.查询没有学全所有课程的同学的信息 + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select distinct CourseId from StudentCourseScore where StudentId=(select StudentId from StudentInfo where StudentCode=01) +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/.keep" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..300a78b3314191f920259853631e24a64ca69942 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery1.sql" @@ -0,0 +1,271 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +select * from CourseInfo +select * from StudentInfo +select * from StudentCourseScore +select * from Teachers + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select * from StudentCourseScore s1,StudentCourseScore s2,StudentInfo s3 where s1.CourseId=1 and s2.CourseId=2 and s1.StudentId=s2.StudentId and s1.Score=2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + +select top 1 s2.S_NAME,s2.S_HOBBY,s2.S_NATIVE from StarType s1 inner join StarInfo s2 on s1.T_NO=s2.S_T_NO where s1.T_NO=1 order by s2.S_AGE desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..347349d20c3e5cdc338a0ffd57b0994950f93458 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" @@ -0,0 +1,40 @@ +use master +go +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\SQL\HOUSE_DB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_log', +filename='D:\SQL\HOUSE_DB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +use HOUSE_DB +go +create table HOUSE_TYPE +( +type_id int primary key identity(1,1) not null, +type_name varchar(50) unique not null, +) +create table HOUSE +( +house_id int not null primary key identity, +house_name varchar(20) not null, +house_price float not null default(0), +type_id int not null references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +insert into HOUSE values('茅房',600,1),('山洞',1000,2),('水沟',1500,3) +select * from HOUSE +select * from HOUSE_TYPE where type_name like '%型' +select house_name,house_price from HOUSE order by house_price DESC +select house_name,type_name from HOUSE_TYPE A inner join HOUSE B on A.type_id=B.type_id +select house_price,house_name from HOUSE where house_price=(select max(house_price) from HOUSE ) diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e212d1832ded361978fb55ad6c48fa70202f41a7 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery2.sql" @@ -0,0 +1,40 @@ +use master +go +create database StarManagerDB +on +( +name='StarManagerDB', +filename='D:\SQL\StarManagerDB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='StarManagerDB_log', +filename='D:\SQL\StarManagerDB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +use StarManagerDB +go +create table StarType +( +T_NO int not null primary key identity(1,1), +T_NAME nvarchar(20) +) +create table StarInfo +( +S_NO int not null primary key identity(1,1), +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int references StarType(T_NO) +) +insert into StarType values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2),('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) +select top 3 S_NAME 姓名,S_HOBBY 年龄,S_NATIVE 籍贯 from StarInfo order by S_AGE DESC +select count(S_NAME) 人数,AVG(S_AGE) 平均年龄 from StarInfo group by S_T_NO having count(S_NAME)>2 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_T_NO=1 and S_AGE=(select max(S_AGE) from StarInfo) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..df6bb6f372892d3ff6ff0cb34fb855665fc1bb64 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery3.sql" @@ -0,0 +1,279 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + + + + select * from StudentInfo + select * from Teachers + select * from CourseInfo + select * from StudentCourseScore + + + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + select b.*,a.Score 数学成绩,c.Score 语文成绩 + from StudentCourseScore a,StudentInfo b,StudentCourseScore c + where a.StudentId=c.StudentId and a.CourseId =1 and c.CourseId =2 and c.Score60 + + +-- 3.查询在 成绩 表存在成绩的学生信息 + select distinct * from StudentCourseScore join StudentInfo on StudentCourseScore.Id=StudentInfo.Id + + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + select studentcode,studentname,count(CourseId),sum(score) from StudentInfo left join StudentCourseScore + on StudentInfo.StudentCode=StudentCourseScore.StudentId group by studentcode,studentname +-- 4.1 查有成绩的学生信息 + select distinct * from StudentCourseScore right join StudentInfo + on StudentInfo.Id=StudentCourseScore.Id + +-- 5.查询「李」姓老师的数量 + select count(teachername) from Teachers where teachername like '李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 + select a.* from StudentInfo a + join StudentCourseScore b on a.Id=b.StudentId + join CourseInfo c on c.Id=b.CourseId + where c.TeacherId =(select Id from Teachers where TeacherName ='张三') + + +-- 7.查询没有学全所有课程的同学的信息 + select * from StudentInfo where StudentCode not in(select StudentId from StudentCourseScore + group by StudentId having count(*) = (select count(*) from CourseInfo)) + + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + select * from StudentInfo where StudentCode in(select distinct B.StudentId from StudentCourseScore A,StudentCourseScore B + where A.StudentId = 1 and B.StudentId != 1 and B.CourseId = A.CourseId) + + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + select A.Id,studentCode,StudentName,Birthday,Sex,ClassId from StudentInfo A inner join StudentCourseScore B on A.Id=B.StudentId + group by A.Id,studentCode,StudentName,Birthday,Sex,ClassId + having count(*) in (select count(CourseId) from StudentCourseScore group by StudentId having StudentId=1) and StudentCode!=01 + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + select * from StudentInfo + where StudentCode not in (select StudentId from StudentCourseScore + where CourseId = (select CourseId from Teachers where TeacherName = '张三')) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/\346\270\205\346\230\216\344\275\234\344\270\232\344\271\213\346\264\222\346\264\222\346\260\264\345\225\246.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/\346\270\205\346\230\216\344\275\234\344\270\232\344\271\213\346\264\222\346\264\222\346\260\264\345\225\246.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3ee23c0bd856fb61bd09536c9628516993f1942c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/\346\270\205\346\230\216\344\275\234\344\270\232\344\271\213\346\264\222\346\264\222\346\260\264\345\225\246.sql" @@ -0,0 +1,73 @@ +create database StarManagerDB + +on + +( +name='StarManagerDB', +filename ='D:\StarManagerDB.mdf' , +size=5, +maxsize=100, +filegrowth=10% +) +log on + +( +name='StarManagerDB_log', +filename ='D:\StarManagerDB_log.ldf' , +size=5, +maxsize=100, +filegrowth=10% +) + +use StarManagerDB + +create table StarType + +( +T_NO int not null primary key identity(1,1), +T_NAME nvarchar (20) +) + +create table StarInfo +( +S_NO int not null primary key identity(1,1), +S_NAME nvarchar (20) not null, +S_AGE int not null, +S_HOBBY nvarchar (20) , +S_BATIVE nvarchar (20) default('中国大陆'), +S_T_NO int foreign key references StarType(T_NO) +) + +insert into StarType values + +('体育明星'), +( 'IT明星' ), +('相声演员') + +insert into StarInfo values + +( '梅西',30,'射门','阿根廷',1), +( '科比',35,'过人','美国',1), +( '蔡景现',40,'敲代码','中国',2), +( '马斯克',36,'造火箭','外星人',2 ), +( '郭德纲',50,'相声','中国',3 ), +( '黄铮',41,'拼多多','中国',2 ) + + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + + select top 3 (S_AGE) 年龄,S_NAME 姓名,S_HOBBY 特技,S_BATIVE 籍贯 from StarInfo + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + +select S_T_NO 明星类型编号 ,count (S_NO) 人数, avg(S_AGE) 平均年龄 from StarInfo group by S_T_NO + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + +select * + + from StarInfo where + + S_AGE=(select max(S_AGE) from StarInfo where S_T_NO=1 ) + + \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/\346\270\205\346\230\216\344\275\234\344\270\232\344\271\213\347\203\255\350\272\253\347\203\255\350\272\253.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/\346\270\205\346\230\216\344\275\234\344\270\232\344\271\213\347\203\255\350\272\253\347\203\255\350\272\253.sql" new file mode 100644 index 0000000000000000000000000000000000000000..daff87d989ae5883bb5f20fb016f53055edcf2b6 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/\346\270\205\346\230\216\344\275\234\344\270\232\344\271\213\347\203\255\350\272\253\347\203\255\350\272\253.sql" @@ -0,0 +1,112 @@ +create database Jxc + +on + +( +name='Jxc', +filename='D:\Jxc.mdf', +size=5, +maxsize=100, +filegrowth=10% +) + +log on + +( +name='Jxc_log', +filename='D:\Jxc_log.ldf', +size=5, +maxsize=100, +filegrowth=10% +) + +use Jxc + +create table HOUSE_TYPE +( +type_id int not null primary key identity , +type_name varchar (50) not null unique + +) + +create table HOUSE + +( +house_id int not null primary key identity , +house_name varchar(50) not null , +house_price float not null default(0), +type_id int not null foreign key references HOUSE_TYPE(type_id) + + +) + +insert into HOUSE_TYPE + +select '别墅' union + + +select '经济型' union + + +select '小户型' + + +insert into HOUSE + +select '黑心“大”宾馆',50000,1 union +select '天天通宵网吧',15,2 union +select '公园长椅',0,3 + + +--查询所有房屋信息 + +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 + +select * from HOUSE_TYPE where type_name like '__型' or type_name like '_型_' or type_name like '型__' + +--查询出房屋的名称和租金,并且按照租金降序排序 + +select house_name 房屋名称, house_price 房租 from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + +select house_name 房屋名称,HOUSE_TYPE.type_id 房屋类型 from HOUSE inner join HOUSE_TYPE on + + HOUSE.type_id=HOUSE_TYPE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + +select house_name 房屋名称, house_price 房租 from HOUSE + +where house_price=(select MAX(house_price)from HOUSE) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/\346\270\205\346\230\216\344\275\234\344\270\232\344\271\213\350\246\201\344\275\240\345\221\275\344\272\224\345\215\201.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/\346\270\205\346\230\216\344\275\234\344\270\232\344\271\213\350\246\201\344\275\240\345\221\275\344\272\224\345\215\201.sql" new file mode 100644 index 0000000000000000000000000000000000000000..29f0e7b91b59944397c4435e49789664b3760305 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/\346\270\205\346\230\216\344\275\234\344\270\232\344\271\213\350\246\201\344\275\240\345\221\275\344\272\224\345\215\201.sql" @@ -0,0 +1,520 @@ + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + + + + +select * from CourseInfo +select * from Teachers +select * from StudentCourseScore +select * from StudentInfo + + +-- 练习题目: + + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select StudentInfo.* , a.Score 语文成绩,s.Score 数学成绩 from StudentCourseScore a,StudentCourseScore s,StudentInfo + +where StudentInfo.Id=s.StudentId and a.CourseId=1 and s.CourseId=2 and a.Score=60 + +-- 3.查询在 成绩 表存在成绩的学生信息 + +select distinct StudentInfo.* from StudentInfo right join StudentCourseScore on StudentId=StudentCode + +select * from StudentInfo where studentcode in ( +select StudentId from StudentCourseScore) +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select studentcode 学生编号,studentName 学生姓名,count(courseid) 选课总数,sum(score) 总分 + + from StudentInfo left join StudentCourseScore on StudentId=StudentCode group by studentcode,studentName + +-- 4.1 查有成绩的学生信息 + + +select distinct StudentInfo.* + + from StudentInfo right join StudentCourseScore on StudentId=StudentCode + + +-- 5.查询「李」姓老师的数量 + +select count (teacherName) 李姓老师数量 from Teachers where teacherName like '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 + +select b.* from StudentInfo b where studentcode in +(select studentid from StudentCourseScore where courseid in + ( select id from CourseInfo where TeacherId in( +select id from Teachers where TeacherName='张三' ))) + + + + +-- 7.查询没有学全所有课程的同学的信息 +select b.* from StudentInfo b where StudentCode in( +select studentid from StudentCourseScore +except +select a.studentid from StudentCourseScore a,StudentCourseScore b, StudentCourseScore c +where a.CourseId=1 and b.CourseId=2 and c.CourseId=3 and c.StudentId=b.StudentId and c.StudentId=a.StudentId +) +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select b.* from StudentInfo b where studentcode in ( + +select distinct b.StudentId from StudentCourseScore a,StudentCourseScore b +where a.StudentId=1 and b.CourseId=a.CourseId + + +) + + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + + +select b.* from StudentInfo b where studentcode in ( +select distinct a.StudentId from StudentCourseScore a,StudentCourseScore b,StudentCourseScore c where +a.CourseId=1 and b.CourseId=2 and c.CourseId=3 and a.StudentId!=1 and a.studentid=b.StudentId and c.StudentId=b.StudentId +) + + + +select b.* from StudentInfo b where studentcode in ( +select StudentId + from StudentCourseScore where CourseId in( select CourseId from StudentCourseScore where StudentId=1) and StudentId!=1 + group by StudentId + having count(StudentId)=3 +) + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + + + +select b.* from StudentInfo b where studentcode in( + +select distinct studentid from StudentCourseScore + +except + +( +select studentid from StudentCourseScore where courseid in +( + select id from CourseInfo where TeacherId in +( +select id from Teachers where TeacherName='张三' +) +) +) +) + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + select studentid 学号, studentName 学生姓名,avg(Score) 平均成绩 from StudentCourseScore b left join StudentInfo a + + on a.StudentCode=b.StudentId where score<60 group by studentid ,studentName having count(*)>=2 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + +select * from CourseInfo +select * from Teachers +select * from StudentCourseScore +select * from StudentInfo + + +select distinct StudentInfo.*,StudentCourseScore.Score from StudentCourseScore left join StudentInfo +on StudentInfo.StudentCode=StudentCourseScore.StudentId where StudentId in (select StudentId from StudentCourseScore where CourseId =1 and Score<60) +order by StudentCourseScore.Score desc + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fc623754159f1dd951954e34f28779f9f9dd802c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" @@ -0,0 +1,66 @@ +create database HOUSE_DB +on +( + name='D:\HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log +on +( + name='D:\HOUSE_DB-log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +use HOUSE_DB + +create table HOUSE_TYPE +( + type_id int not null primary key identity, + type_name varchar(50) not null unique +) +create table HOUSE +( + house_id int not null primary key identity, + house_name varchar(50) not null , + house_price float not null default(0), + type_id int not null foreign key references HOUSE_TYPE(type_id) +) + +insert into HOUSE_TYPE values +('小户型'), +('经济型'), +('别墅') +insert into HOUSE values +('坟',6000,1), +('公园长椅',2000,2), +('四合院',30000,3) + +select * from HOUSE_TYPE +select * from HOUSE + +--查询所有房屋信息 + +select * from HOUSE_TYPE left join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id + +--使用模糊查询包含”型“字的房屋类型信息 + +select * from HOUSE_TYPE where type_name like '__型' + +--查询出房屋的名称和租金,并且按照租金降序排序 + +select house_name 名称,house_price 租金 from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + +select type_name 房屋类型,house_name 房屋名称 from HOUSE_TYPE right join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + + +select top 1 house_price 租金,house_name 名称 from HOUSE order by house_price desc + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7a3d3778b353c2dbfb057ddbb604a444c8428e60 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery2.sql" @@ -0,0 +1,69 @@ +create database StarManagerDB +on +( + name='D:\StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log +on +( + name='D:\StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +use StarManagerDB + +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) + +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄峥',41,'拼多多','中国',2) + +select * from StarType +select * from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 +from StarInfo order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + +select T_NO,count(*)人数,avg(S_AGE)平均年龄 from StarType join StarInfo on T_NO=S_T_NO group by T_NO having count(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + + +select * from StarInfo +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo join StarType +on T_NO=S_T_NO +where T_NAME='体育明星' +order by S_AGE desc + + \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ee4a760fba8e72d95c603e9919bb7aecda63dc4d --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery3.sql" @@ -0,0 +1,445 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + + + + +-- 练习题目: + +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + + +select C.*,A.Score 语文,B.Score 数学 from StudentCourseScore A +join StudentCourseScore B on A.StudentId=B.StudentId +join StudentInfo C on C.Id=A.StudentId +where A.CourseId=1 and B.CourseId=2 and A.Score=60 + +-- 3.查询在 成绩 表存在成绩的学生信息 + +select * from StudentCourseScore A join StudentInfo B on A.StudentId=B.StudentCode + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select StudentInfo.Id 学生编号,StudentName 学生姓名,count( StudentCourseScore.StudentId)选课总数,sum(StudentCourseScore.Score) 总成绩 from StudentInfo +left join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +group by StudentInfo.Id,StudentName,StudentCourseScore.StudentId + +-- 4.1 查有成绩的学生信息 + +select distinct StudentInfo.* from StudentInfo +inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId + + +-- 5.查询「李」姓老师的数量 + + +select TeacherName,count(*)数量 from Teachers where TeacherName like '李%' group by TeacherName + +-- 6.查询学过「张三」老师授课的同学的信息 + +select b.* from StudentCourseScore a inner join StudentInfo b on a.StudentId=b.Id +where CourseId='2' + + +-- 7.查询没有学全所有课程的同学的信息 + + +select StudentId,b.StudentName,b.ClassId,b.Birthday,b.Sex,b.StudentCode,count(CourseId)课程数量 from StudentCourseScore A +left join StudentInfo B ON A.StudentId=B.Id +where CourseId in (select id from CourseInfo) group by StudentId,b.StudentName,b.ClassId,b.Birthday,b.Sex,b.StudentCode +having count(CourseId)<3 + + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select distinct StudentInfo.* from StudentCourseScore +inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id +where StudentId=(select StudentId from StudentInfo where StudentCode=01) + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4b2797b74da95fb0b5bf3790f77db2fc247f4444 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" @@ -0,0 +1,40 @@ +create database HOUSE_DB +on +( + name='HOUSE_DB', + size=5, + filename='D:\HOUSE_DB.mdf', + filegrowth=1, + maxsize=50 +) +log on +( + name='HOUSE_DB_log', + size=5, + filename='D:\HOUSE_DB_log.ldf', + filegrowth=1, + maxsize=50 +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int not null primary key identity, + type_name varchar(50) not null unique, +) +create table HOUSE +( + house_id int not null primary key identity, + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null foreign key references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +insert into HOUSE values('五五开',500,1),('PDD',800,3),('大司马',600,2) +select * from HOUSE +select * from HOUSE_TYPE where type_name like '%型%' +select house_name,house_price from HOUSE order by house_price desc +select house_name,type_name from HOUSE t inner join HOUSE_TYPE x on t.type_id=x.type_id +select top 1 house_name,house_price from HOUSE order by house_price desc + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fa4a796f4336c44f17df4fa18e85cc89c54ae119 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" @@ -0,0 +1,42 @@ +create database StarManagerDB +on +( + name='StarManagerDB', + size=5, + filename='D:\StarManagerDB.mdf', + filegrowth=1, + maxsize=50 +) +log on +( + name='StarManagerDB_log', + size=5, + filename='D:\StarManagerDB_log.ldf', + filegrowth=1, + maxsize=50 +) +go +use StarManagerDB +go +create table StarType +( + T_NO int not null primary key identity, + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int not null primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) +insert into StarType values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo order by S_AGE desc +select count(S_NAME) 明星人数,avg(S_AGE) 明星平均年龄 from StarInfo group by S_T_NO having count(S_NAME)>2 +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo where S_T_NO=1 order by S_AGE desc + + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..51465ae21c111b22f0b2f2940febfff709955ffd --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery3.sql" @@ -0,0 +1,480 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select c.*,b.courseid,b.score,a.courseid,a.score from (select * from StudentCourseScore where CourseId=1) A, +(select * from StudentCourseScore where CourseId=2) B,(select * from StudentInfo) C +where A.studentid=b.studentid and C.id=A.studentid and b.score>a.score +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from (select * from StudentCourseScore where CourseId=1) A,(select * from StudentCourseScore where CourseId=2) B +where A.studentid=b.studentid +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from (select * from StudentCourseScore where CourseId=2) A left join +(select * from StudentCourseScore where CourseId=1) B on A.studentid=b.studentid +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from StudentCourseScore where StudentId in +(select StudentId from StudentCourseScore +where StudentId not in (select StudentId from StudentCourseScore +where CourseId='2'group by StudentId,CourseId) or CourseId='1' group by StudentId having count(CourseId)>1) +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentCode,studentname,avg(score) 平均成绩 from StudentCourseScore t inner join StudentInfo x on t.StudentId=x.Id +group by StudentCode,studentname having avg(score)>=60 +-- 3.查询在 成绩 表存在成绩的学生信息 +select distinct x.* from StudentCourseScore t join StudentInfo x on t.StudentId=x.Id +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentCode,StudentName,count(CourseId) 选课总数,sum(Score)课程的总成绩 from StudentInfo t left join StudentCourseScore x +on t.Id=x.StudentId group by StudentCode,StudentName +-- 4.1 查有成绩的学生信息 +select distinct x.* from StudentCourseScore t join StudentInfo x on t.StudentId=x.Id +-- 5.查询「李」姓老师的数量 +select count(id) 老师的数量 from Teachers where TeacherName like '李%' +-- 6.查询学过「张三」老师授课的同学的信息 +select x.* from StudentCourseScore t inner join StudentInfo x on t.StudentId=x.Id + where courseid=(select id from Teachers where TeacherName='张三') +-- 7.查询没有学全所有课程的同学的信息 +select t.* from StudentInfo t left join StudentCourseScore x +on t.Id=x.StudentId where score is null +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select * from StudentInfo where Id in (select StudentId from StudentCourseScore where StudentId!='1' and CourseId in (select CourseId from StudentCourseScore where StudentId='1' +) group by StudentId) +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select * from StudentInfo where id in +(select StudentId from +(select StudentId,CourseId from StudentCourseScore where StudentId in ( + select StudentId from StudentCourseScore where StudentId != + '1' + group by StudentId having count(CourseId) > 2)) A, + (select CourseId from StudentCourseScore where StudentId = + '1') B + where A.CourseId = B.CourseId group by StudentId) +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select * from StudentInfo where Id not in (select StudentId from StudentCourseScore +where CourseId=(select Id from Teachers where TeacherName='张三')) +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 +select SCS.StudentId,StudentName,AVG(score) 平均成绩 from StudentCourseScore SCS join StudentInfo SI on SI.Id=StudentId group by SCS.StudentId,StudentName +having StudentId in (select StudentId from StudentCourseScore where Score<60 group by StudentId having count(CourseId)>=2) +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 +select * from StudentInfo +where Id=(select StudentId from StudentCourseScore +where CourseId in (select Id from CourseInfo where CourseName='数学') and Score<60) +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select CourseId,score 课程的成绩,AVG(score) 平均成绩 from StudentCourseScore group by score,CourseId order by AVG(score) DESC +-- 14.查询各科成绩最高分、最低分和平均分: +select CourseId,MAX(score) 最高分,MIN(score) 最低分,AVG(score) 平均分 from StudentCourseScore group by CourseId +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ +select +CourseId,CourseName as 课程, +max(Score) as 最高分, +min(Score) as 最低分, +avg(Score) as 平均分, +cast(cast(sum(case when Score>=60 then 1 else 0 end)*100/(count(StudentId)) as float) as nvarchar)+'%' as 及格率, +cast(cast(sum(case when Score >= 70 and Score >= 80 then 1 else 0 end)*100/(count(StudentId)) as float) as nvarchar)+'%' as 中等率, +cast(cast(sum(case when Score >= 80 and Score >= 90 then 1 else 0 end)*100/(count(StudentId)) as float) as nvarchar)+'%' as 优良率, +cast(cast(sum(case when Score >= 90 then 1 else 0 end)*100/(count(StudentId)) as float) as nvarchar)+'%' as 优秀率 +from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +group by CourseId,CourseName + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + +select * from StudentCourseScore +order by CourseId,Score desc + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + +select StudentId,sum(Score) from StudentCourseScore +group by StudentId +order by sum(Score) desc + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + +select StudentId,sum(Score) from StudentCourseScore +group by StudentId +order by sum(Score) desc + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + +select +CourseName, +cast(cast(sum(case when Score<=100 and Score>85 then 1 else 0 end)*100/(count(StudentId)) as float) as nvarchar)+'%' as [100-85] , +cast(count(case when Score<=100 and Score>85 then 1 else 0 end) as int) as 人数, +cast(cast(sum(case when Score<=85 and Score>70 then 1 else 0 end)*100/(count(StudentId)) as float) as nvarchar)+'%' as [85-70] , +cast(count(case when Score<=85 and Score>70 then 1 else 0 end) as int) as 人数, +cast(cast(sum(case when Score<=70 and Score>60 then 1 else 0 end)*100/(count(StudentId)) as float) as nvarchar)+'%' as [70-60] , +cast(count(case when Score<=70 and Score>60 then 1 else 0 end) as int) as 人数, +cast(cast(sum(case when Score<=60 and Score>=0 then 1 else 0 end)*100/(count(StudentId)) as float) as nvarchar)+'%' as [60-0] , +cast(count(case when Score<=60 and Score>=0 then 1 else 0 end) as int) as 人数 +from StudentCourseScore A +inner join CourseInfo B on B.Id = A.CourseId +group by CourseName + +-- 18.查询各科成绩前三名的记录 + +select CourseId,Score from StudentCourseScore A where +(select count(CourseId) from StudentCourseScore where CourseId=A.CourseId AND A.score85 + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + +select StudentName,CourseName,Score from StudentCourseScore A +inner join CourseInfo B on A.CourseId = B.Id +inner join StudentInfo C on A.StudentId = C.StudentCode +where CourseName = '数学' and Score<60 + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + +select StudentName,CourseName,Score from StudentInfo A +full join StudentCourseScore B on B.StudentId = A.StudentCode +full join CourseInfo C on B.CourseId = C.Id + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + +select StudentName as 姓名,CourseName as 课程名称,Score as 分数 from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where Score>70 + +-- 30.查询不及格的课程 + +select CourseName from StudentCourseScore A +inner join CourseInfo B on A.CourseId = B.Id +where Score<60 + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + +select StudentId as 学号,StudentName as 姓名 from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +where C.Id = 01 and Score>=80 + +-- 32.求每门课程的学生人数 + +select CourseName,count(*) as 学生人数 from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +inner join CourseInfo C on A.CourseId = C.Id +group by CourseId,CourseName + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + +select top 1 B.*, Score as 成绩 from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 2 +order by Score desc + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + +select distinct top 1 B.*, Score as 成绩 from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +where CourseId = 2 +order by Score desc + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + +select A.StudentId as 学生编号,A.CourseId as 课程编号,A.Score as 学生成绩 from StudentCourseScore A,StudentCourseScore B +where A.CourseId != B.CourseId and A.Score = B.Score +group by A.StudentId,A.CourseId,A.Score + +-- 36.查询每门功成绩最好的前两名 + +select CourseId,Score from StudentCourseScore A where +(select count(CourseId) from StudentCourseScore where CourseId=A.CourseId AND A.score=2 +group by StudentId + +-- 39.查询选修了全部课程的学生信息 + +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore A +inner join StudentInfo B on A.StudentId = B.StudentCode +group by StudentCode,StudentName,Birthday,Sex,ClassId +having count(CourseId)=3 + +-- 40.查询各学生的年龄,只按年份来算 + +select StudentName,datediff(year,Birthday,getdate()) as 年龄 from StudentInfo + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 + + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..98b272b9232991570295905aa14769a57f139d2e --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" @@ -0,0 +1,432 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +-- 练习题目: + + + +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select * from StudentCourseScore +SELECT * FROM CourseInfo +select a.*,c.StudentName,Sex,ClassId from StudentCourseScore A, StudentCourseScore B inner join StudentInfo c on b.StudentId=c.StudentCode +where A.CourseId='2' AND B.CourseId='1'and A.Score>B.Score and a.StudentId=b.StudentId +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from StudentCourseScore +select * from StudentInfo +select b.* from StudentCourseScore A, StudentCourseScore B , StudentInfo c +where a.CourseId=1 and b.CourseId=2 and a.StudentId=b.StudentId and b.StudentId=c.Id + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId + + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select b.* from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A right join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId +where a.Id is null + + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select a.StudentId,b.StudentName , avg(Score) from StudentCourseScore a +inner join StudentInfo b on a.StudentId=b.Id +group by a.StudentId ,b.StudentName +-- 3.查询在 成绩表 存在 成绩 的 学生信息 + +select * from StudentCourseScore a inner join StudentInfo b on a.StudentId=b.Id + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select a.StudentName,StudentId,count(*),sum(b.Score) +from StudentInfo a left join StudentCourseScore b on a.Id=b.StudentId group by StudentId,StudentName + + +-- 4.1 查有成绩的学生信息 +select Score,b.* from StudentCourseScore a inner join StudentInfo b on a.StudentId=b.Id + + +-- 5.查询「李」姓老师的数量 +select * from Teachers where TeacherName like '李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +select * from StudentInfo +select * from StudentCourseScore +select * from StudentCourseScore a inner join StudentInfo b on a.StudentId=b.Id where CourseId=2 +-- 7.查询没有学全所有课程的同学的信息 +select * from StudentInfo where not StudentCode in +(select StudentId from StudentCourseScore group by StudentId having COUNT(*)=(select count(*) from CourseInfo)) + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + + +select * from StudentInfo where StudentCode in +(select StudentId from StudentCourseScore group by StudentId having COUNT(*)<=3) + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select * from StudentInfo where StudentCode in +(select StudentId from StudentCourseScore group by StudentId having COUNT(*)=(select count(*) from CourseInfo)) + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3875f9adba971099305cde1ffdb1f1c49cc198da --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" @@ -0,0 +1,65 @@ +use master +go +create database HOUSE_DB +on +( name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity, + type_name varchar(50) unique not null +) + +create table HOUSE +( + house_id int primary key identity, + house_name varchar(50) not null, + house_price float default(0) not null, + type_id int references HOUSE_TYPE(type_id) not null +) + +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +select*from HOUSE_TYPE +insert into HOUSE values('马云家',100,'1'),('马化腾家',200,'2'),('我家',100000000,'3') + +select*from HOUSE_TYPE +select*from HOUSE + +--4、添加查询 +--查询所有房屋信息 +select * from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id + +--使用模糊查询包含”型“字的房屋类型信息 +select*from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋的名称,house_price 租金 + from HOUSE + order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price 最高月租, house_name 房屋名称 from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id order by house_price desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..118ce8b19e38b9449ee3fde999d88feb62538952 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery3.sql" @@ -0,0 +1,64 @@ +use master +go +create database StarManagerDB +on +( +name='nana', +size=5, +filename='D:\nana.mdf' +) +log on +( +name='nana_log', +size=5, +filename='D:\nana_log.ldf' +) +go +use StarManagerDB +go +create table StarType +( +T_NO int NOT NULL, +T_NAME NVARCHAR(20) +) +alter table StarType add constraint ok_StarType_T_NO primary key(T_NO) +GO +CREATE TABLE StarInfo +( +S_NO INT NOT NULL, +S_NAME NVARCHAR(20) NOT NULL, +S_AGE INT NOT NULL, +S_HOBBY NVARCHAR(20), +S_NATIVE NVARCHAR(20) default('中国大陆'), +S_T_NO INT +) +alter table StarInfo add constraint ak_StarInfo_S_NO primary key(S_NO) +alter table StarInfo add constraint nk_StarInfo_S_T_NO foreign key(S_T_NO) references StarType(T_NO) +go +select * from StarType +insert into StarType(T_NO,T_NAME) + values(1,'体育明星'), + (2,'IT明星'), + (3,'相声演员') +insert into StarInfo(S_NO,S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) + values(1,'梅西',30,'射门','阿根廷',1), +(2,'科比',35,'过人','美国',1), +(3,'蔡景现',40,'敲代码','中国',2), +(4,'马斯克',36,'造火箭','外星人',2), +(5,'郭德纲',50,'相声','中国',3), +(6,'黄铮',41,'拼多多','中国',2) + +select * from StarInfo +select * from StarType + + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc + + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + +select count(S_NO) 明星人数,avg(S_AGE) 明星平均年龄 , S_T_NO from StarInfo group by S_T_NO having S_T_NO>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME,S_HOBBY,S_NATIVE from StarType a inner join StarInfo b on a.T_NO=b.S_T_NO where T_NAME='体育明星' order by S_AGE \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..498b24bede4cd91bd5a1d3e2df375fa450eb565a --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" @@ -0,0 +1,195 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8835711e8091d1775c4236e7fe0e21d1f0f761a5 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery2.sql" @@ -0,0 +1,48 @@ +use master +go +create database HOUSE_DB +on( + name='HOUSE_DB', + filename='C:\text\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=1% +) +log on( + name='HOUSE_DB_log', + filename='C:\text\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1% +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique not null, +) +create table HOUSE +( + house_id int primary key identity(1,1) not null, + house_name varchar(50) not null, + house_price float default(0) not null , + type_id int references HOUSE_TYPE(type_id) not null, +) +select * from HOUSE_TYPE +select * from HOUSE +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +insert into HOUSE values('宝山花园','12000',3),('山水花苑','2500',1),('香山居','3000',1),('骊山花园','5000',2), +('湘水居','2000',1) +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 名称,house_price 租金 from HOUSE order by house_price DESC +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,HOUSE_TYPE.type_name 房屋类型铭名称 from HOUSE +inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +--查询所有房屋 中月租最高 的房屋,显示最高的租金和房屋名称 +select top 1 house_name 房屋名称,house_price 房屋租金 from HOUSE order by house_price DESC diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery5.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..77e7091d0cc755e58642e7db8d59b37c75450c51 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery5.sql" @@ -0,0 +1,51 @@ +use master +go +create database StarManagerDB +on( + name='StarManagerDB', + filename='C:\TEXT\StarManagerDB.mdf', + size=5, + maxsize=50, + filegrowth=1% +) +log on( + name='StarManagerDB_log', + filename='C:\TEXT\StarManagerDB_log.ldf', + size=5, + maxsize=50, + filegrowth=1% +) +go +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20), +) +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO), +) +select * from StarType +select * from StarInfo +insert into StarType values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41, '拼多多','中国',2) +--查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE DESC +--按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 明星类型编号,COUNT(*)明星人数,AVG(S_AGE) 平均年龄 from StarInfo group by S_T_NO having COUNT(*)>2 +--查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名, S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO +where StarType.T_NAME='体育明星' order by S_AGE DESC \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery6.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0c2feee8815f54bb5fbdf3651844872d11d403c4 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery6.sql" @@ -0,0 +1,60 @@ +use master +go +use ClassicDb +go +select * from Teachers +select * from CourseInfo +select * from StudentInfo +select * from StudentCourseScore +-- 1.查询"数学 "课程比" 语文 "课程成绩高 的 学生的信息 及 课程分数 +select C.*,A.Score 语文, B.Score 数学 from StudentCourseScore A,StudentCourseScore B,StudentInfo C +where A.CourseId=1 and B.CourseId=2 and A.StudentId=B.StudentId and A.Score=60 +-- 3.查询在 成绩 表存在成绩的学生信息 +select distinct StudentInfo.* from StudentCourseScore +inner join StudentInfo on StudentInfo.Id=StudentCourseScore.StudentId +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentInfo.Id 学生编号,StudentName 学生姓名,count( StudentCourseScore.StudentId)选课总数,sum(StudentCourseScore.Score) 总成绩 from StudentInfo +left join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +group by StudentInfo.Id,StudentName,StudentCourseScore.StudentId +-- 4.1 查有成绩的学生信息 +select distinct StudentInfo.* from StudentInfo +inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +-- 5.查询「李」姓老师的数量 +select COUNT(*)数量 from Teachers where TeacherName like '李%' +-- 6.查询学过「张三」老师授课的同学的信息 +select StudentInfo.* from StudentInfo +inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +left join CourseInfo on StudentCourseScore.CourseId=CourseInfo.Id +inner join Teachers on Teachers.Id=CourseInfo.TeacherId +where Teachers.TeacherName='张三' +-- 7.查询没有学全所有课程的同学的信息 +select StudentId,StudentInfo.StudentName,StudentInfo.Birthday,StudentInfo.ClassId,StudentInfo.Sex,StudentInfo.StudentCode,count(*) from StudentCourseScore +inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id +group by StudentId,StudentInfo.Birthday,StudentInfo.ClassId,StudentInfo.Sex,StudentInfo.StudentCode,StudentInfo.StudentName having count(*)<3 +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select distinct StudentInfo.* from StudentCourseScore +inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id +where StudentId=(select StudentId from StudentInfo where StudentCode=01) +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select distinct CourseId from StudentCourseScore where StudentId=(select StudentId from StudentInfo where StudentCode=01) +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select * from StudentInfo where id not in(select StudentInfo.Id from StudentCourseScore +inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id +left join CourseInfo on StudentCourseScore.CourseId=CourseInfo.Id +inner join Teachers on StudentCourseScore.CourseId=Teachers.Id where TeacherName='张三' +) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..daf68ac62ba3264d5fd3e58d25947422f5f362de --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" @@ -0,0 +1,568 @@ +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5Mb, + maxsize=50Mb, + filegrowth=1Mb +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5Mb, + maxsize=50Mb, + filegrowth=1Mb +) +go + +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity not null, + type_name varchar(50) unique not null +) + +create table HOUSE +( + house_id int primary key identity not null, + house_name varchar(50), + house_price float default('0') not null, + type_id int references HOUSE_TYPE(type_id) +) + +insert into HOUSE_TYPE values +('小户型'), +('经济型'), +('别墅') + +insert into HOUSE values +('123',1000,1), +('132',5000,2), +('321',99999,3) + +--查询所有房屋信息 +select * from HOUSE inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select type_name,house_price from HOUSE inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +order by house_price +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select type_name,house_name from HOUSE inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price,house_name from HOUSE order by house_price DESC + + +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20) +) + +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) + +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') + +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的姓名,特技和特技信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技, S_NATIVE 特技 from StarInfo order by S_AGE DESC +--4、按明星类型编号分类 查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 明星人数大于2的分组,count(S_NO) 明星人数,AVG(S_AGE) 明星平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order BY S_AGE DESC + +select * from StarType +select * from StarInfo + +-- 练习题目: +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers +select * from CourseInfo +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select C.*,A.Score 语文,B.Score 数学 from StudentCourseScore A join StudentCourseScore B on A.StudentId=B.StudentId join StudentInfo C on C.Id=A.StudentId +where A.CourseId=1 and B.CourseId=2 and A.Score=60 + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentInfo + +select * from StudentCourseScore A join StudentInfo B on A.StudentId=B.StudentCode + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select StudentCode 学生编号,StudentName 学生姓名,count(CourseId) 选课总数,sum(Score) 总成绩 from StudentInfo join StudentCourseScore on StudentCourseScore.StudentId=StudentInfo.StudentCode +group by StudentCode,StudentName +-- 4.1 查有成绩的学生信息 + +select * from StudentCourseScore A join StudentInfo B on A.StudentId=B.StudentCode + +-- 5.查询「李」姓老师的数量 + +select count(TeacherName) 李姓老师的数量 from Teachers where TeacherName like '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 + + +select * from +(select * from StudentCourseScore where CourseId=(select TeacherId from CourseInfo +join Teachers on CourseInfo.TeacherId=Teachers.Id where TeacherName='张三')) A +left join StudentInfo B on B.StudentCode=A.StudentId + + +-- 7.查询没有学全所有课程的同学的信息 + + +select * from StudentInfo where StudentCode not in (select StudentCode from StudentInfo where +StudentCode in (select StudentId from StudentCourseScore where CourseId='1') +and StudentCode in (select StudentId from StudentCourseScore where CourseId='2') +and StudentCode in (select StudentId from StudentCourseScore where CourseId='3')) + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers +select * from CourseInfo + +select * from StudentInfo A left join StudentCourseScore B on A.StudentCode=B.StudentId where CourseId in +(select CourseId from StudentCourseScore where StudentId='1') and StudentCode!='01' + + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select * from StudentInfo where StudentCode in +(select CourseId from StudentCourseScore A join StudentInfo B on A.StudentId=B.StudentCode where StudentCode='01') and StudentCode!='01' + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select StudentName from StudentInfo where StudentCode not in(select StudentId from StudentCourseScore where CourseId=(select TeacherId from CourseInfo +join Teachers on CourseInfo.TeacherId=Teachers.Id where TeacherName='张三')) + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 +use master +go + +create database ClassicDb +on + ( + name = 'ClassicDb', + filename = 'D:\ClassicDb.mdf', + size = 10mb, + maxsize = 50mb, + filegrowth = 10mb + ) + log on + ( + name = 'ClassicDb_log', + filename = 'D:\ClassicDb_log.ldf', + size = 10mb, + maxsize = 50mb, + filegrowth = 10mb + ) +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c97d3d1dadb0202d0b758b82b5d5bbfa9ebb2f1d --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/2.sql" @@ -0,0 +1,75 @@ +--1、创建明星数据库(StarManagerDB),然后建立两张表,StarType(明星类型表),StarInfo(明星信息表),表结构分别如下: + +create database StarManagerDB +go +use StarManagerDB +go + +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 + +create table StarType( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO + +create table StarInfo( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) + +--使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 + +insert StarType values +('体育明星'), +('IT明星'), +('相声演员') + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + +insert StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO,count(*)人数,AVG(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select S.S_NAME 姓名,S.S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo S +left join StarType T on S.S_T_NO=T.T_NO where T_NAME='体育明星' \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c4bfb46bd9f2b04d467fc7249f66950c45d87902 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/3.sql" @@ -0,0 +1,73 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +create database HOUSEDB +on( + name='HOUSEDB', + filename='D:\HOUSEDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) + +log on( +name='HOUSE_DB', +filename='D:\HOUSEDB.ldf' +) + +use HOUSEDB +go + +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 + +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 +create table HOUSE_TYPE( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null unique +) + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 + +create table HOUSE( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null foreign key references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 + +insert HOUSE_TYPE(type_name) values +('小户型'),('经济型'),('别墅') + +insert HOUSE values +('上海陆家嘴',1000,1), +('北京老胡同',5000,2), +('西双版纳别野',2000,3) + +--4、添加查询 +--查询所有房屋信息 +select * from HOUSE H left join HOUSE_TYPE T on H.type_id=T.type_id + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE where house_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,T.type_id,T.type_name from HOUSE H left join HOUSE_TYPE T on H.type_id=T.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price,house_name from HOUSE order by house_price desc diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/50.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/50.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e87d290e7dd4365eec8947d99251a368dbedc6c8 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/50.sql" @@ -0,0 +1,265 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +select * from CourseInfo --课程信息表 +select * from StudentCourseScore --成绩表 +select * from StudentInfo --学生信息表 +select * from Teachers --老师信息表 + +---- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select C.*,A.Score 数学成绩,B.Score 语文成绩 from StudentCourseScore A,StudentCourseScore B, StudentInfo C +where A.CourseId=2 and B.CourseId=1 and A.StudentId=B.StudentId and A.Score>B.Score and C.Id=A.StudentId + + -- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的 学生的情况 + select A.StudentId from StudentCourseScore A,StudentCourseScore B + where (A.CourseId=1 and B.CourseId=2) and (A.StudentId=B.StudentId) + +---- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId + + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) B on A.StudentId=B.StudentId + + + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select S.Id,S.StudentName,Avg(C.Score) from StudentInfo S + join StudentCourseScore C on S.Id=C.Id +group by S.Id,S.StudentName having Avg(C.Score)>=60 + + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from CourseInfo --课程信息表 +select * from StudentCourseScore --成绩表 +select * from StudentInfo --学生信息表 +select * from Teachers --老师信息表 + +select * from StudentCourseScore A right join StudentInfo B ON A.Id=B.Id + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select S.StudentCode,S.StudentName,count(C.StudentId),SUM(C.Score) from StudentInfo S +left join StudentCourseScore C on S.Id=C.Id group by S.StudentCode,S.StudentName + +-- 4.1 查有成绩的学生信息 +select B.* from StudentCourseScore A left join StudentInfo B on A.Id=B.Id + + +-- 5.查询「李」姓老师的数量 +select count(*) from Teachers where TeacherName like '%李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +select * from Teachers --老师信息表 +select * from StudentInfo --学生信息表 +select * from StudentCourseScore --成绩表 +select * from CourseInfo --课程信息表 + +select * from StudentCourseScore A +left join CourseInfo B on A.CourseId=B.Id +left join Teachers C on B.TeacherId=C.Id + where TeacherName like '%张三%' + +-- 7.查询没有学全所有课程的同学的信息 +select * from StudentCourseScore A FULL join StudentInfo B ON A.Id=B.Id + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 select distinct* from StudentInfo a, (select StudentId from StudentCourseScore where courseid in (select CourseId from StudentCourseScore a, StudentInfo b where a.StudentId=b.Id and b.Id=01))b where a.Id=b.StudentId and a.Id!=01 -- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 select * from StudentInfo where id in (select StudentId from StudentCourseScore group by StudentId having count(*)= (select count(*) from StudentCourseScore a join StudentInfo b on a.StudentId=b.Id where StudentId=1)) and StudentCode!=01 -- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 select StudentName from StudentInfo where id not in (select StudentId from StudentCourseScore a, (select c.Id from Teachers t inner join CourseInfo c on t.Id=c.TeacherId where t.TeacherName='张三') b where a.CourseId=b.Id) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2c61bf8c6a081e7cbc84703317274f703e6dc2c3 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" @@ -0,0 +1,68 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +create database HOUSE_DB +on +( + name = HOUSE_DB, + filename='D:\新建文件夹\HOUSE_DB.mdf ', + size = 5mb, + filegrowth =1mb, + maxsize = 50mb +) +log on +( + name = HOUSE_DB_log, + filename='D:\新建文件夹\HOUSE_DB.ldf ', + size = 5mb, + filegrowth =1mb, + maxsize = 50mb +) +go + +use HOUSE_DB +go +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 +create table HOUSE_TYPE +( + type_id int not null primary key identity, + type_name varchar(50) not null unique +) +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 +create table HOUSE +( + house_id int not null primary key identity, + house_name varchar(50) not null , + house_price float not null default(0), + type_id int not null foreign key references HOUSE_TYPE(type_id) +) +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') +insert into HOUSE values +('贫穷水平',1000,1), +('中等水平',3000,2), +('富裕水平',5000,3) +--4、添加查询 +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE ORDER BY house_price DESC +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select H.house_name,T.type_name from HOUSE H INNER JOIN HOUSE_TYPE T ON H.type_id=T.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +SELECT TOP 1 house_price,house_name FROM HOUSE ORDER BY house_price DESC \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..532b4ae6c552bcd1e56f5067a36f884bfc6efe2d --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery2.sql" @@ -0,0 +1,66 @@ +--1、创建明星数据库(StarManagerDB),然后建立两张表,StarType(明星类型表),StarInfo(明星信息表),表结构分别如下: +create database StarManagerDB +go + +use StarManagerDB +go +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) DEFAULT('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select count(*) 明星人数,avg(i.S_AGE) 明星平均年龄 from StarInfo i inner join StarType t on i.S_T_NO=t.T_NO group by t.T_NAME having count(*)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 i.S_NAME 姓名,i.S_HOBBY 特技,i.S_NATIVE 籍贯信息 from StarInfo i inner join StarType t on i.S_T_NO=t.T_NO where t.T_NAME='体育明星' order by i.S_AGE desc diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..816914d828098c756c91281e403df4ba249bdaf6 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery3.sql" @@ -0,0 +1 @@ +create database ClassicDb GO use ClassicDb GO create table StudentInfo ( Id int PRIMARY key not null IDENTITY, StudentCode nvarchar(80), StudentName nvarchar(80), Birthday date not null, Sex nvarchar(2), ClassId int not null ) GO -- select * from StudentInfo insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) GO CREATE TABLE Teachers ( Id int PRIMARY key not null IDENTITY, TeacherName nvarchar(80) ) go -- select * from Teachers insert into Teachers (TeacherName) values('张三') insert into Teachers (TeacherName) values('李四') insert into Teachers (TeacherName) values('王五') GO create table CourseInfo ( Id int PRIMARY key not null IDENTITY, CourseName NVARCHAR(80) not null, TeacherId int not null ) go -- select * from CourseInfo insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) GO create table StudentCourseScore ( Id int PRIMARY key not null IDENTITY, StudentId int not null, CourseId int not null, Score int not null ) go insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) go select * from StudentInfo select * from CourseInfo select * from Teachers select * from StudentCourseScore -- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 select * from StudentCourseScore a,StudentCourseScore b,StudentInfo c where a.CourseId=1and b.CourseId=2 and a.Score=60 -- 3.查询在 成绩 表存在成绩的学生信息 select distinct b.* from StudentCourseScore a join StudentInfo b on a.StudentId=b.Id -- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) select a.StudentCode,a.StudentName,count(b.CourseId),sum(Score) from StudentInfo a left join StudentCourseScore b on a.Id=b.StudentId group by a.StudentCode,a.StudentName -- 4.1 查有成绩的学生信息 select distinct b.* from StudentCourseScore a right join StudentInfo b on a.StudentId=b.Id -- 5.查询「李」姓老师的数量 select count(TeacherName) from Teachers where TeacherName like '李%' -- 6.查询学过「张三」老师授课的同学的信息 select * from StudentInfo where id in (select StudentId from StudentCourseScore where CourseId = (select a.Id from CourseInfo a join Teachers b on a.TeacherId=b.Id where b.TeacherName='张三')) -- 7.查询没有学全所有课程的同学的信息 select * from StudentInfo where id in (select StudentId from StudentCourseScore group by StudentId having count(CourseId)<3) -- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 select distinct* from StudentInfo a, (select StudentId from StudentCourseScore where courseid in (select CourseId from StudentCourseScore a, StudentInfo b where a.StudentId=b.Id and b.Id=01))b where a.Id=b.StudentId and a.Id!=01 -- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 select * from StudentInfo where id in (select StudentId from StudentCourseScore group by StudentId having count(*)= (select count(*) from StudentCourseScore a join StudentInfo b on a.StudentId=b.Id where StudentId=1)) and StudentCode!=01 -- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 select StudentName from StudentInfo where id not in (select StudentId from StudentCourseScore a, (select c.Id from Teachers t inner join CourseInfo c on t.Id=c.TeacherId where t.TeacherName='张三') b where a.CourseId=b.Id) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..347349d20c3e5cdc338a0ffd57b0994950f93458 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" @@ -0,0 +1,40 @@ +use master +go +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\SQL\HOUSE_DB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_log', +filename='D:\SQL\HOUSE_DB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +use HOUSE_DB +go +create table HOUSE_TYPE +( +type_id int primary key identity(1,1) not null, +type_name varchar(50) unique not null, +) +create table HOUSE +( +house_id int not null primary key identity, +house_name varchar(20) not null, +house_price float not null default(0), +type_id int not null references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +insert into HOUSE values('茅房',600,1),('山洞',1000,2),('水沟',1500,3) +select * from HOUSE +select * from HOUSE_TYPE where type_name like '%型' +select house_name,house_price from HOUSE order by house_price DESC +select house_name,type_name from HOUSE_TYPE A inner join HOUSE B on A.type_id=B.type_id +select house_price,house_name from HOUSE where house_price=(select max(house_price) from HOUSE ) diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e212d1832ded361978fb55ad6c48fa70202f41a7 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery2.sql" @@ -0,0 +1,40 @@ +use master +go +create database StarManagerDB +on +( +name='StarManagerDB', +filename='D:\SQL\StarManagerDB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='StarManagerDB_log', +filename='D:\SQL\StarManagerDB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +use StarManagerDB +go +create table StarType +( +T_NO int not null primary key identity(1,1), +T_NAME nvarchar(20) +) +create table StarInfo +( +S_NO int not null primary key identity(1,1), +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int references StarType(T_NO) +) +insert into StarType values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2),('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) +select top 3 S_NAME 姓名,S_HOBBY 年龄,S_NATIVE 籍贯 from StarInfo order by S_AGE DESC +select count(S_NAME) 人数,AVG(S_AGE) 平均年龄 from StarInfo group by S_T_NO having count(S_NAME)>2 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_T_NO=1 and S_AGE=(select max(S_AGE) from StarInfo) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f9a0aee453c032314e8150e6eb52d4a1568f884f --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery3.sql" @@ -0,0 +1,210 @@ +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select A.*,C.* from StudentCourseScore A inner join StudentCourseScore B on A.StudentId=B.StudentId inner join StudentInfo C on A.StudentId=C.Id +where A.CourseId=2 and B.CourseId=1 and a.Score > b.Score and A.StudentId=B.StudentId + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from StudentCourseScore A inner join StudentCourseScore B on A.StudentId=B.StudentId inner join StudentInfo C on A.StudentId=C.Id +where A.CourseId=2 and B.CourseId=1 and A.StudentId=B.StudentId + + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore B where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select B.* from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A right join +(select * from StudentCourseScore B where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId where A.Id is NUll +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentCode,StudentName,AVG(Score) from StudentInfo A inner join StudentCourseScore B on A.Id=B.StudentId +group by StudentCode,StudentName having AVG(Score)>=60 +-- 3.查询在 成绩 表存在成绩的学生信息 +select distinct A.* from StudentInfo A right join StudentCourseScore B on A.Id=B.StudentId +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentCode,StudentName,count(CourseId) 选课总数,sum(Score) 总成绩 from StudentInfo A +left join StudentCourseScore B on A.Id=B.StudentId group by StudentCode,StudentName +-- 4.1 查有成绩的学生信息 +select distinct A.* from StudentInfo A +right join StudentCourseScore B on A.Id=B.StudentId +-- 5.查询「李」姓老师的数量 +select count(teacherName) 姓李老师的数量 from Teachers where TeacherName like '李%' +-- 6.查询学过「张三」老师授课的同学的信息 +select D.* from Teachers A +inner join CourseInfo B on A.Id=B.TeacherId +inner join StudentCourseScore C on B.Id=C.CourseId +inner join StudentInfo D on C.StudentId=D.Id where TeacherName='张三' +-- 7.查询没有学全所有课程的同学的信息 +select * from StudentInfo where StudentCode not in(select StudentId from StudentCourseScore +group by StudentId having count(*) = (select count(*) from CourseInfo)) +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select * from StudentInfo where StudentCode in(select distinct B.StudentId from StudentCourseScore A,StudentCourseScore B +where A.StudentId = 1 and B.StudentId != 1 and B.CourseId = A.CourseId) +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select A.Id,studentCode,StudentName,Birthday,Sex,ClassId from StudentInfo A inner join StudentCourseScore B on A.Id=B.StudentId group by A.Id,studentCode,StudentName,Birthday,Sex,ClassId having count(*) in (select count(CourseId) from StudentCourseScore group by StudentId having StudentId=1) and StudentCode!=01 +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select * from StudentInfo where StudentCode not in (select StudentId from StudentCourseScore where CourseId = (select CourseId from Teachers where TeacherName = '张三')) + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8d0c72a2d0911c746bf6b793598405245b01b105 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye1.sql" @@ -0,0 +1,55 @@ +use master +go +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\test\HOUSE_DB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1% +) +log on +( +name='HOUSE_DB_log', +filename='D:\test\HOUSE_DB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1% +) +use HOUSE_DB +go +create table HOUSE_TYPE +( +type_id int not null primary key identity(1,1), +type_name varchar(50) not null unique +) +create table HOUSE +( +house_id int not null primary key identity (1,1), +house_name varchar(50) not null default (0), +house_price float not null, +type_id int not null references HOUSE_TYPE (type_id), + +) +insert into HOUSE_TYPE (type_name) values ('小户型'),('经济型'),('别墅') +insert into HOUSE (house_name,house_price,type_id) +values('小户',1000,1), +('经济房',2000,2), +('豪华房',3000,3) +select * from HOUSE_TYPE +select * from HOUSE + +use HOUSE_DB +go + +--查询所有房屋信息 +select * from HOUSE_TYPE s1 inner join HOUSE s2 on s1.type_id=s2.type_id +--使用模糊查询包含”型“字的房屋类型信息 +select type_name 房屋类型 from HOUSE_TYPE where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋的名称,house_price 租金 from HOUSE group by house_name,house_price order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select type_name 房屋类型,house_name 房屋名称 from HOUSE_TYPE s1 inner join HOUSE s2 on s1.type_id=s2.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_name 房屋名称,max(house_price)最高的租金 from HOUSE group by house_name diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..af56987de3fd508efb1c28e10d7cff7149ca6d78 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye2.sql" @@ -0,0 +1,39 @@ +use master +go +create database StarManagerDB +use StarManagerDB +go +create table StarType +( +T_NO int not null primary key identity (1,1), +T_NAME nvarchar(20) +) +create table StarInfo +( +S_NO int not null primary key identity(1,1), +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default ('中国大陆'), +S_T_NO int references StarType(T_NO) +) +insert into StarType (T_NAME) values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) +select * from StarType +select * from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select TOP 3 S_NAME 姓名, S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo group by S_NAME,S_AGE,S_HOBBY,S_NATIVE +order by S_AGE DESC +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select T_NAME,count(S_T_NO)类型编号 ,avg(S_AGE)平均年龄 from StarType s1 inner join StarInfo s2 on s1.T_NO=s2.S_T_NO +group by T_NAME +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名, S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯,T_NAME 类型 from StarInfo s1 inner join StarType s2 on s1.S_T_NO=s2.T_NO where S_T_NO=1 group by S_NAME,S_AGE,S_HOBBY,S_NATIVE ,T_NAME +order by S_AGE desc + + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..437352af77f6d55e0f372684726d0396644c2be5 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye3.sql" @@ -0,0 +1,269 @@ + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select * from StudentCourseScore s1,StudentCourseScore s2,StudentInfo s3 +where s1.CourseId=2 and s2.CourseId=1 and s1.StudentId=s2.StudentId and s1.Score>s2.Score and s3.Id=s1.StudentId + +select * from StudentCourseScore A join StudentCourseScore B on A.StudentId=B.StudentId +join StudentInfo C on C.Id=A.StudentId and A.CourseId=2 AND B.CourseId=1 AND A.Score>B.Score +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from StudentCourseScore s1 join StudentCourseScore s2 on s1.StudentId=s2.StudentId +join StudentInfo s3 on s3.Id=s1.StudentId +and s1.CourseId=1 and s2.CourseId=2 + +select * from StudentInfo where Id in (select A.StudentId from StudentCourseScore A,StudentCourseScore B +where A.CourseId=2 and B.CourseId=1 AND A.StudentId=B.StudentId ) + +select * from StudentInfo where Id in (select A.StudentId from StudentCourseScore A,StudentCourseScore B +where A.CourseId=(select Id from CourseInfo where CourseName='数学') and B.CourseId=(select Id from CourseInfo where CourseName='语文') +AND A.StudentId=B.StudentId ) + +select * from StudentInfo AA +join (select A.StudentId from StudentCourseScore A,StudentCourseScore B where A.CourseId=2 and B.CourseId=1 AND A.StudentId=B.StudentId ) BB +ON AA.Id=BB.StudentId +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from (select *from StudentCourseScore A where CourseId = ( select Id from CourseInfo where CourseName = '数学')) s1 +left join (select *from StudentCourseScore A where CourseId = ( select Id from CourseInfo where CourseName = '语文')) s2 on s1.StudentId=s2.StudentId +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from StudentCourseScore +except +(select *from StudentCourseScore A where CourseId = ( select Id from CourseInfo where CourseName = '数学')) +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select s1.Id,s1.StudentName, avg(Score)平均分 from StudentInfo s1 inner join StudentCourseScore s2 on s1.Id=s2.StudentId group by s1.Id,s1.StudentName +having avg(Score)>60 + +-- 3.查询在 成绩 表存在成绩的学生信息 +select s2.Id,s2.StudentName,s1.Score from StudentCourseScore s1 + right join StudentInfo s2 on s1.StudentId=s2.Id group by s2.Id,s2.StudentName,s1.Score + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentInfo.Id 学生编号,StudentName 学生姓名,count(CourseId)选课总数,sum(Score)总成绩 from StudentInfo +left join StudentCourseScore on StudentCourseScore.StudentId=StudentInfo.Id +group by StudentInfo.Id,StudentName + +-- 4.1 查有成绩的学生信息 +select*from StudentInfo +where Id in(select StudentId from StudentCourseScore group by StudentId) + +-- 5.查询「李」姓老师的数量 +select count(*)姓李老师的数量 from Teachers where TeacherName like '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 +select StudentId 学号, StudentName 姓名,Birthday 生日,Sex 性别,ClassId 班级 from StudentCourseScore +join StudentInfo on StudentInfo.Id=StudentCourseScore.StudentId +where CourseId in (select Id from CourseInfo where TeacherId = (select Id from Teachers where TeacherName ='张三' )) + +-- 7.查询没有学全所有课程的同学的信息 +select StudentId,StudentInfo.StudentName,StudentInfo.Birthday,StudentInfo.ClassId,StudentInfo.Sex,StudentInfo.StudentCode,count(*) from StudentCourseScore +inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id +group by StudentId,StudentInfo.Birthday,StudentInfo.ClassId,StudentInfo.Sex,StudentInfo.StudentCode,StudentInfo.StudentName having count(*)<3 + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select distinct StudentInfo.* from StudentCourseScore +inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id +where StudentId=(select StudentId from StudentInfo where StudentCode=01) + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select distinct CourseId from StudentCourseScore where StudentId=(select StudentId from StudentInfo where StudentCode=01) + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select * from StudentInfo where id not in(select StudentInfo.Id from StudentCourseScore +inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id +left join CourseInfo on StudentCourseScore.CourseId=CourseInfo.Id +inner join Teachers on StudentCourseScore.CourseId=Teachers.Id where TeacherName='张三' +) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..54341e3908deefc466e2618fd33c57933d1a4883 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" @@ -0,0 +1,202 @@ + create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore use ClassicDb select * from StudentInfo --学生信息表 select * from Teachers --老师 select * from CourseInfo --课程信息表 select * from StudentCourseScore --学生成绩表 -- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 select C.*,B.Score 数学成绩, A.Score 语文成绩 from StudentCourseScore A , StudentCourseScore B ,StudentInfo C where A.CourseId=1 and B.CourseId=2 and A.StudentId=B.StudentId and A.Score=60 -- 3.查询在 成绩 表存在 成绩的 学生信息 select distinct * from StudentInfo INNER JOIN StudentCourseScore on StudentCourseScore.Id=StudentInfo.Id -- 4.查询 所有同学的 学生编号 、学生姓名、 选课总数、 所有课程的总成绩 (没成绩的显示为 null ) select StudentInfo.StudentCode, StudentName 学生姓名, count(CourseId) 选课总数,sum(score) 所有课程的总成绩 from StudentInfo left join StudentCourseScore on StudentCourseScore.StudentId=StudentInfo.Id group by StudentInfo.StudentCode , StudentName order by StudentCode DESC -- 4.1 查有成绩的学生信息 select * from StudentInfo inner join StudentCourseScore on StudentCourseScore.Id=StudentInfo.Id -- 5.查询「李」姓老师的数量 select count(*)李姓老师的数量 from (select * from Teachers where TeacherName like '李%') A -- 6.查询学过「张三」老师授课的同学的信息 select * from (select * from Teachers where TeacherName='张三') A inner join StudentCourseScore on A.Id=StudentCourseScore.CourseId -- 7.查询没有学全所有课程的同学的信息 select StudentId from StudentCourseScore group by StudentId having count(CourseId)<3 -- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select StudentId from StudentCourseScore A where StudentId!='01' and CourseId + in (select CourseId from StudentCourseScore where StudentId='01') group by StudentId -- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 -- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 -- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 -- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 -- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 -- 14.查询各科成绩最高分、最低分和平均分: -- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 /* 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 */ -- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 -- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 -- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 -- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 -- 18.查询各科成绩前三名的记录 -- 19.查询每门课程被选修的学生数 -- 20.查询出只选修两门课程的学生学号和姓名 -- 21.查询男生、女生人数 -- 22.查询名字中含有「风」字的学生信息 -- 23.查询同名同性学生名单,并统计同名人数 -- 24.查询 1990 年出生的学生名单 -- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 -- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 -- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 -- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) -- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 -- 30.查询不及格的课程 -- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 -- 32.求每门课程的学生人数 -- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 --34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 -- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 -- 36.查询每门功成绩最好的前两名 -- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 -- 38.检索至少选修两门课程的学生学号 -- 39.查询选修了全部课程的学生信息 -- 40.查询各学生的年龄,只按年份来算 -- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 -- 42.查询本周过生日的学生 -- 43.查询下周过生日的学生 -- 44.查询本月过生日的学生 -- 45.查询下月过生日的学生 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/review2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/review2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..495ce59122b25fb23661d0a1f0df3e8c8270514d --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/review2.sql" @@ -0,0 +1,80 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +log on +( + name='HOUSE_DB_ldf', + filename='D:\HOUSE_DB_ldf.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) + +use HOUSE_DB + +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 +create table HOUSE_TYPE +( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null unique +) + + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null , + house_price float not null default(0), + tyoe_id int not null foreign key references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') + +insert into HOUSE(house_name,house_price,tyoe_id) values ('A',5000,1) , ('B',6000,2) ,('C',8000,3) + +SELECT * FROM HOUSE_TYPE + +--4、添加查询 +--查询所有房屋信息 + SELECT * FROM HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +SELECT * FROM HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 名称, house_price 租金 from HOUSE order by house_price DESC + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE_TYPE +inner join HOUSE on HOUSE_TYPE.type_id =HOUSE.tyoe_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select Top 1 house_price 最高的租金, house_name 房屋名称 from HOUSE order by house_price DESC + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/review3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/review3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a165dd973b83e99a48228e317524270a8791a31f --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/review3.sql" @@ -0,0 +1,73 @@ +--1、创建明星数据库(StarManagerDB) +use master +go + +create database StarManagerDB +go + +use StarManagerDB +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NatIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) + + + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 +insert into StarType values ('体育明星'),('IT明星'),('相声明星') + +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NatIVE,S_T_NO) +values ('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景观',40,'敲代码','中国',2), +('马斯克',36,'外星人','中国',2),('郭德纲',50,'相声','中国',3),('黄峥',41,'拼多多','中国',2) + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + + +select * from StarType +select * from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select TOP 3 S_AGE 年龄 ,S_NAME 姓名,S_HOBBY 特技, S_NatIVE 籍贯 FROM StarInfo order by S_AGE DESC + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +SELECT S_T_NO 类型编号 ,COUNT(*) 明星人数 ,AVG(S_AGE)明星平均年龄 FROM StarInfo GROUP BY S_T_NO HAVING COUNT(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +SELECT TOP 1 S_AGE 年龄 ,S_NAME 姓名, S_HOBBY 特技 ,S_NatIVE 籍贯 FROM StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO +where T_NAME='体育明星' order by S_AGE DESC \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..011590131b6aff8a5e14b94ee53760e18ef0439f --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" @@ -0,0 +1,54 @@ +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5MB, + maxsize=50MB, + filegrowth=1MB +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=1MB +) +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) not null unique, + +) +create table HOUSE +( + house_id int primary key identity(1,1) not null, + house_name varchar(50) not null , + house_price float not null default('0'), + type_id int references HOUSE_TYPE (type_id) not null +) +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') +insert into HOUSE values ('小家','1800',1),('瓦房','1500',2),('小楼','2500',3) +select * from HOUSE_TYPE +select * from HOUSE + +--4、添加查询 +--查询所有房屋信息 +--使用模糊查询包含”型“字的房屋类型信息 +--查询出房屋的名称和租金,并且按照租金降序排序 +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select * from HOUSE +select * from HOUSE_TYPE where type_name like '%型%' +select house_name,house_price from HOUSE order by house_price desc +select HOUSE.type_id,house_name,type_name from HOUSE inner join HOUSE_TYPE on HOUSE.type_id= HOUSE_TYPE.type_id +select house_id,house_name,house_price from HOUSE where house_price in (select max(house_price) from HOUSE ) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..45844f42aa5a6b41ac6190fed5c2686acad96df7 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" @@ -0,0 +1,71 @@ +use master +go +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5MB, + maxsize=50MB, + filegrowth=1MB +) +log on +( + name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=1MB +) +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity (1,1) not null, + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default '中国大陆', + S_T_NO int references StarType(T_NO) +) + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 +insert into StarType values ('体育明星'),('IT明星'),('相声演员') +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 +insert into StarInfo values('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select *from StarType +select *from StarInfo +select top 3 S_AGE, S_NAME 姓名,S_HOBBY 特技, S_NATIVE 籍贯信息 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 明星类型编号,count(S_NO) 明星人数,avg(S_AGE) 平均年龄 from StarInfo group by S_T_NO having +count(S_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select T_NAME 明星类型,S_AGE 年龄,S_NAME 姓名, S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo left join StarType on StarType.T_NO=StarInfo.S_T_NO + where T_NAME ='体育明星' and S_AGE in ( select max(S_AGE) from StarInfo join StarType on StarType.T_NO=StarInfo.S_T_NO where T_NAME ='体育明星' ) + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5e06963dea985fc35555b10b776c84a1838130f6 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery3.sql" @@ -0,0 +1,457 @@ +-- 练习题目: + + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息 及课程分数 +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore +-------------------------------------- +select C.*,A.Score 数学分数,A.CourseId 数学课程,B.CourseId 语文课程,B.Score 语文分数 from + StudentCourseScore A , StudentCourseScore B,StudentInfo C where + A.courseId=2 and B.courseId=1 and A.Score>B.Score and A.StudentId=B.StudentId and C.Id=A.StudentId + + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from StudentInfo where Id in (select A.StudentId from StudentCourseScore A, StudentCourseScore B +where A.CourseId=(select Id from CourseInfo where CourseName='数学' ) and B.CourseId=(select Id from CourseInfo where CourseName='语文') +and A. StudentId =B.StudentId) + + + +select * from StudentCourseScore A join StudentCourseScore B on A.StudentId=B.StudentId join + StudentInfo C on C.Id=A.StudentId and A.CourseId=2 and B.CourseId=1 + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from +(select * from StudentCourseScore A where CourseId in(select Id from CourseInfo where CourseName='数学')) A left join + (select * from StudentCourseScore B where CourseId in(select id from CourseInfo where CourseName='语文')) + B on A.StudentId=B.StudentId + +select * from StudentCourseScore A where CourseId in(select Id from CourseInfo where CourseName='数学' ) left +select * from StudentCourseScore B where CourseId in(select id from CourseInfo where CourseName='语文') + + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from StudentCourseScore B where CourseId in(select id from CourseInfo where CourseName='语文') +select * from StudentCourseScore A where CourseId in(select Id from CourseInfo where CourseName='数学' ) + + + + +select * from StudentCourseScore where StudentId not in (select StudentId from StudentCourseScore + where CourseId in(select Id from CourseInfo where CourseName='数学')) + + + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId 学生编号,StudentName 学生姓名,avg(Score) 平均成绩 from StudentCourseScore +left join StudentInfo on StudentInfo.Id=StudentCourseScore.StudentId group by StudentId,StudentName having avg(Score)>=60 + +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentInfo where ID in(select StudentId from StudentCourseScore) + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentInfo.Id , StudentName, count(CourseId),sum(Score) from StudentCourseScore right join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id + group by StudentInfo.Id,StudentName + +-- 4.1 查有成绩的学生信息 +select * from StudentInfo where Id in (select StudentId from StudentCourseScore group by StudentId) + + + +-- 5.查询「李」姓老师的数量 +select count(TeacherName) from Teachers where TeacherName like'李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +select * from StudentInfo where Id in(select StudentId from StudentCourseScore where CourseId + in(select Id from CourseInfo where TeacherId in(select Id from Teachers where TeacherName='张三')) group by StudentId +) + + + + +-- 7.查询没有学全所有课程的同学的信息 +select * from StudentInfo where Id in (select StudentId from StudentCourseScore group by StudentId having count(StudentId)<3) + + +-- 8.查询至少有一门课与学号为" 01 "的同学 所学相同 的 同学的信息 +select * from StudentInfo where Id in +(select StudentId from StudentCourseScore where CourseId in (select Id from StudentInfo where StudentCode='01')) +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select * from StudentInfo where id in (select StudentId from StudentCourseScore where CourseId in +(select CourseId from StudentCourseScore where StudentId in(select Id from StudentInfo where StudentCode='01')) + group by StudentId having count(CourseId)=3) + + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select StudentName from StudentInfo + + select StudentName from StudentInfo where Id not in(select StudentId from StudentCourseScore where CourseId + in(select Id from CourseInfo where TeacherId in(select Id from Teachers where TeacherName='张三')) group by StudentId +) + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 + + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3663b6c97301245602fd108f4a374134188171c1 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery1.sql" @@ -0,0 +1,59 @@ +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\HOUSE_DB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_log', +filename='D:\HOUSE_DB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +go + +use HOUSE_DB +go +create table HOUSE_TYPE +( +type_id int primary key not null identity, +type_name varchar(50) unique not null +) + + +create table HOUSE +( +house_id int not null primary key identity, +house_name varchar(50) not null, --房屋名称 +house_price float not null default(0), --房租 +type_id int not null references HOUSE_TYPE(type_id) +) + +insert into HOUSE_TYPE values ('小户型'), +('经济型'), +('别墅') + +insert into HOUSE values ('a',1000,1), +('b',3000,2), +('c',9000,3) + +select * from HOUSE_TYPE +select * from HOUSE + +--4、添加查询 +--查询所有房屋信息 + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name, house_price from HOUSE ORDER BY house_price DESC +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name from HOUSE_TYPE inner join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select max(house_price) from HOUSE +select house_price,house_name from HOUSE where house_price=(select max(house_price) from HOUSE) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery6.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..39debb797bdbd96c09f9b90c0b859835a63f8b1b --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery6.sql" @@ -0,0 +1,45 @@ +create database StarManagerDB + +go +use StarManagerDB +go + +create table StarType +( +T_NO int identity primary key not null,--明星类型编号 +T_NAME nvarchar(20) --明星类型 +) +create table StarInfo +( +S_NO int not null primary key identity(1,1) ,--明星编号 +S_NAME nvarchar(20) not null,--明星姓名 +S_AGE int not null ,--明星年龄 +S_HOBBY nvarchar(20),--特技 +S_NATIVE nvarchar(20) default('中国大陆'),--明星籍贯 +S_T_NO int references StarType(T_NO)--明星类型编号 + +) + +insert into StarType values('体育明星'), +('IT明星'), +('相声演员') + +insert into StarInfo values ('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + +select * from StarType +select * from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 * from StarInfo ORDER BY S_AGE DESC +select S_NAME 姓名 ,S_HOBBY 特技 ,S_NATIVE 籍贯信息 from StarInfo where exists (select top 3 * from StarInfo ORDER BY S_AGE DESC) +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 编号 ,count(S_T_NO) 明星人数 ,avg(S_AGE) 明星平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select max(S_AGE) from StarInfo where S_T_NO=1 +select S_NAME,S_HOBBY,S_NATIVE from StarInfo where S_AGE=(select max(S_AGE) from StarInfo where S_T_NO=1) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery7.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7a73334017f5d7bb2a8dd6700dea78fdd2bab9b1 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery7.sql" @@ -0,0 +1,243 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + + + select * from StudentInfo + select * from Teachers + select * from CourseInfo + select * from StudentCourseScore + + -- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + select c.*, a.CourseId 数学,a.Score 成绩, b.CourseId 语文,b.Score 成绩 from StudentCourseScore a ,StudentCourseScore b,StudentInfo c where a.CourseId=1 and b.CourseId=2 and a.StudentId=b.StudentId and + a.Score>b.Score and a.StudentId=c.Id + -- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 + select * from StudentCourseScore a,StudentCourseScore b where a.StudentId=b.CourseId and a.CourseId=1 and b.CourseId=2 + -- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) B on A.StudentId=B.StudentId +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentId, COUNT(CourseId),AVG(Score) from StudentCourseScore group by StudentId having AVG(Score)>=60 +select StudentCode,StudentName,平均分 from StudentInfo a right join (select StudentId,AVG(Score) 平均分 from StudentCourseScore group by StudentId having AVG(Score)>=60) b on a.StudentCode=b.StudentId +-- 3.查询在 成绩 表存在成绩的学生信息 +select StudentId from StudentCourseScore group by StudentId +select a.* from StudentInfo a right join (select StudentId from StudentCourseScore group by StudentId) b on a.StudentCode=b.StudentId +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentCode 学生编号,StudentName 学生姓名,count(CourseId) 选课总数,sum(Score) 总成绩 from StudentInfo join StudentCourseScore on StudentCourseScore.StudentId=StudentInfo.StudentCode +group by StudentCode,StudentName +-- 4.1 查有成绩的学生信息 +select * from StudentCourseScore A join StudentInfo B on A.StudentId=B.StudentCode +-- 5.查询「李」姓老师的数量 +select COUNT(TeacherName) 李姓老师的数量 from Teachers where TeacherName like '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 +select StudentId from StudentCourseScore where CourseId=1 +select a.* from StudentInfo a inner join (select StudentId from StudentCourseScore where CourseId=1) b on b.StudentId=a.StudentCode +-- 7.查询没有学全所有课程的同学的信息 +select * from StudentInfo where StudentCode not in (select StudentId from StudentCourseScore group by StudentId having count(*)=(select count(*) FROM CourseInfo)) +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select * from StudentInfo A left join StudentCourseScore B on A.StudentCode=B.StudentId where CourseId in +(select CourseId from StudentCourseScore where StudentId='1') and StudentCode!='01' +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select * from StudentInfo where StudentCode in +(select CourseId from StudentCourseScore A join StudentInfo B on A.StudentId=B.StudentCode where StudentCode='01') and StudentCode!='01' +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select StudentName from StudentInfo where StudentCode not in(select StudentId from StudentCourseScore where CourseId=(select TeacherId from CourseInfo +join Teachers on CourseInfo.TeacherId=Teachers.Id where TeacherName='张三')) + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/.keep" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery13.1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery13.1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..16d6eda8aa8ac183f61fd5b54d19fe4dfa5d2611 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery13.1.sql" @@ -0,0 +1,50 @@ +use master +go +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb + +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb + +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique not null +) +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null, + house_price float not null default(0), + type_id int references HOUSE_TYPE not null +) +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') +insert into HOUSE values ('御龙湾',3000,2),('汤臣一品',5000,3),('近郊园',2000,1) +select * from HOUSE_TYPE +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,HOUSE_TYPE.type_name 房屋类型名称 from HOUSE +inner join HOUSE_TYPE on HOUSE_TYPE.type_id = HOUSE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_name ,house_price from HOUSE group by house_name,house_price order by house_price desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery13.2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery13.2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f8e6968b69dcf9285b11ac411ce8f5eec78b59be --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery13.2.sql" @@ -0,0 +1,54 @@ +use master +go +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb + +) +log on +( + name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb + +) +go +use StarManagerDB +go +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null , + S_AGE int not null , + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +insert into StarType values ('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values ('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2),('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) +--查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select * from StarType +select * from StarInfo +select top 3 s_age 年龄, S_name 姓名,S_hobby 特技,S_native 籍贯信息 from StarInfo order by S_AGE DESC +--按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select t_no 明星类型编号,t_name , count(S_T_NO) 明星人数,avg(S_age) 明星平均年龄 from StarInfo +inner join StarType on StarType.T_NO= StarInfo.S_T_NO group by t_no,t_name having count(S_T_NO)>2 + +--查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_Name 姓名, S_age 年龄,S_hobby 特技,S_native 籍贯信息,T_NAME 类型 from StarInfo +inner join StarType on StarType.T_NO= StarInfo.S_T_NO +where T_NAME='体育明星' +group by S_Name ,S_age,S_hobby ,S_native ,T_NAME +order by S_AGE desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery13.3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery13.3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6c31939b84b41f141367f223c509de2930cf34f2 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery13.3.sql" @@ -0,0 +1,36 @@ +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +go + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int constraint FK_GoodsInfo_TypeID references GoodsType(TypeID) +) +go +insert into GoodsType values ('服装内衣'),('鞋包数码'),('手机数码') +insert into GoodsInfo values('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟 ',400,2), +('平底鞋','白色','阿依莲',200,2),('迷你照相机','红色 ','尼康',500 ,3), +('硬盘','黑色','希捷',600,3),('显卡 ','黑色','技嘉',800,3) +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney = (select MAX(GoodsMoney) from GoodsInfo) +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型,MAX(GoodsMoney)最高价格,MIN(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo group by TypeID +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where (GoodsColor = '红色') and (GoodsMoney between 300 and 600) +select * from GoodsInfo where (GoodsColor = '红色') and (GoodsMoney >= 300 and GoodsMoney <= 600) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery13.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery13.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c5f26d5f80dfbd6e304fe0c579c8fc4087d25725 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery13.sql" @@ -0,0 +1,279 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers +-- 1.查询 "数学" 课程比 "语文" 课程成绩高的(同一个学生) 学生的信息 及 课程分数 +select C.*,A.score 数学课程,B.score 语文课程 from StudentCourseScore A,StudentCourseScore B,StudentInfo C +where A.CourseId = 2 and B.CourseId = 1 and A.StudentId=B.StudentId and A.Score>B.Score AND C.Id=A.StudentId +-- 1.1 查询同时存在"数学"课程和"语文"课程 的学生的情况 +select * from StudentCourseScore A +inner join StudentCourseScore B on A.StudentId= B.StudentId +inner join StudentInfo C on A.StudentId= C.Id AND A.CourseId = 1 and B.CourseId = 2 +-- 1.2 查询存在"数学"课程但可能不存在"语文"课程的情况(不存在时显示为 null ) +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId +-- 1.3 查询不存在"数学"课程但存在"语文"课程的情况 +select A.Id,A.StudentId,A.CourseId,A.Score,B.CourseId,B.Score +from (select * from StudentCourseScore a where a.CourseId=1) A +left join (select * from StudentCourseScore a where a.CourseId=2) B on A.StudentId=B.StudentId +where B.CourseId is null +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select a.StudentId 学生编号,b.StudentName 学生姓名,avg(a.Score)平均成绩 +from StudentCourseScore a +inner join StudentInfo b on a.StudentId=b.Id +group by a.StudentId,b.StudentName having avg(a.score)>60 +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentInfo where id in (select studentid from StudentCourseScore ) +-- 4.1 查有成绩的学生信息 +select * from StudentInfo where id in (select studentid from StudentCourseScore where Score is not null ) +-- 5.查询「李」姓老师的数量 +select count(TeacherName) '「李」姓老师的数量' from Teachers where TeacherName like '李%' +select * from Teachers +-- 6.查询学过「张三」老师授课的同学的信息 + +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore + +select a.* from StudentInfo a +inner join StudentCourseScore on StudentCourseScore.StudentId=a.Id +inner join CourseInfo on CourseInfo.Id=StudentCourseScore.CourseId +where CourseInfo.TeacherId =(select Id from Teachers where TeacherName ='张三') +-- 7.查询没有学全所有课程的同学的信息 + +select a.Id,a.StudentCode,a.StudentName,a.Birthday,a.Sex,a.ClassId +from StudentCourseScore b join StudentInfo a on a.Id=b.StudentId +group by a.Id,a.StudentCode,a.StudentName,a.Birthday,a.Sex,a.ClassId +having count(b.CourseId)=(select count(a.CourseName) from CourseInfo a) +union all +select * from StudentInfo +where Id not in (select distinct b.StudentId from StudentCourseScore b where b.Score is not null) + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +select distinct a.Id,a.StudentCode,a.StudentName,a.Birthday,a.Sex,a.ClassId +from StudentInfo a join StudentCourseScore b on a.Id=b.StudentId +where b.CourseId in +( + select a.CourseId from StudentCourseScore a + where a.StudentId=(select Id from StudentInfo where StudentCode='01') +) +and a.StudentCode!='01' + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select a.CourseId from StudentCourseScore a where a.StudentId=(select Id from StudentInfo where StudentCode='01') + +select distinct a.Id,a.StudentCode,a.StudentName,a.Birthday,a.Sex,a.ClassId +from StudentInfo a join StudentCourseScore b on a.Id=b.StudentId +left join StudentCourseScore c on c.StudentId=b.StudentId left join StudentCourseScore d on c.StudentId=d.StudentId +where b.CourseId=1 and c.CourseId=2 and d.CourseId=3 and a.StudentCode!='01' + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select * from StudentInfo +where id not in +( + select distinct d.Id from StudentCourseScore a + join CourseInfo b on a.CourseId= b.Id + join Teachers c on b.TeacherId=c.Id + join StudentInfo d on d.Id=a.StudentId + where c.TeacherName='张三' +) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0156452c8e11bc695bc94086180c51fc46144fa5 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" @@ -0,0 +1,294 @@ +/** +* 五十题 +*/ + +use ClassicDb +go + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select SI.*,A.Score 数学成绩,B.Score 语文成绩 from StudentCourseScore A cross join StudentCourseScore B +inner join StudentInfo SI on SI.StudentCode = A.StudentId +where A.CourseId = (select Id from CourseInfo where CourseName = '数学') +and B.CourseId = (select Id from CourseInfo where CourseName = '语文') +and A.Score > B.Score and A.StudentId = B.StudentId + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的学生情况 +select * from StudentInfo +where StudentCode in(select A.StudentId from StudentCourseScore A,StudentCourseScore B +where A.StudentId = B.StudentId +and A.CourseId = (select Id from CourseInfo where CourseName = '语文') +and B.CourseId = (select Id from CourseInfo where CourseName = '数学')) + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from (select * from StudentCourseScore where CourseId = (select Id from CourseInfo where CourseName = '数学')) A +left join (select * from StudentCourseScore where CourseId = (select Id from CourseInfo where CourseName = '语文')) B +on A.StudentId = B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况. +select * from (select * from StudentCourseScore where CourseId in (select Id from CourseInfo where CourseName = '语文')) A +left join (select * from StudentCourseScore where CourseId = (select Id from CourseInfo where CourseName = '数学')) B +on A.StudentId = B.StudentId +where b.StudentId is null + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentCode,StudentName,AVG(Score)平均成绩 from StudentInfo SI +inner join StudentCourseScore SCS on SI.StudentCode = SCS.StudentId +group by StudentCode,StudentName +having StudentCode in (select StudentId from StudentCourseScore group by StudentId having AVG(Score) >= 60) + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentInfo +where StudentCode in (select StudentId from StudentCourseScore where Score is not null) + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select distinct StudentCode,StudentName,COUNT(CourseId)选课总数,SUM(Score)总分 from StudentInfo SI +left join StudentCourseScore SCS on SI.StudentCode = SCS.StudentId +group by StudentCode,StudentName + +-- 4.1 查有成绩的学生信息 +select distinct SI.* from StudentInfo SI +inner join StudentCourseScore SCS on SI.StudentCode = SCS.StudentId +where Score is not null + +-- 5.查询「李」姓老师的数量 +select count(*) 老师的数量 from Teachers +where TeacherName like '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 +select * from StudentInfo +where StudentCode in (select StudentId from StudentCourseScore where CourseId = (select CourseId from Teachers where TeacherName = '张三')) + +-- 7.查询没有学全所有课程的同学的信息 +select * from StudentInfo where StudentCode not in(select StudentId from StudentCourseScore +group by StudentId having count(*) = (select count(*) from CourseInfo)) + +-- 8.查询至少有一门课与学号为"01"的同学所学相同的同学的信息 +select * from StudentInfo where StudentCode in(select distinct B.StudentId from StudentCourseScore A,StudentCourseScore B +where A.StudentId = 1 and B.StudentId != 1 and B.CourseId = A.CourseId) + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select * from StudentInfo + where StudentCode in( + select StudentId from StudentInfo SI + join StudentCourseScore SCS on SCS.StudentId = SI.StudentCode + where StudentCode in( + select StudentId from StudentCourseScore + where StudentId != 01 group by StudentId having COUNT(*) = (select COUNT(*) from StudentCourseScore + where StudentId = 01 group by StudentId)) + group by StudentId + having count(*) = (select count(*) from StudentCourseScore where StudentId = 01 group by StudentId) +) + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select * from StudentInfo +where StudentCode not in (select StudentId from StudentCourseScore where CourseId = (select CourseId from Teachers where TeacherName = '张三')) + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 +select StudentCode,StudentName,AVG(Score)平均成绩 from StudentInfo SI +inner join StudentCourseScore SCS on SCS.StudentId = SI.StudentCode +where StudentCode in(select A.StudentId from StudentCourseScore A,StudentCourseScore B where A.StudentId = B.StudentId and A.Score < 60 and B.Score < 60 group by A.StudentId) +group by StudentCode,StudentName + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 +select * from StudentInfo SI +inner join StudentCourseScore SCS on SI.StudentCode = SCS.StudentId +where StudentCode in(select distinct StudentId from StudentCourseScore where CourseId = (select CourseId from CourseInfo where CourseName = '数学') and Score < 60) +order by scs.Score desc + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select StudentName,A.Score 语文成绩,B.Score 数学成绩,C.Score 英语成绩,D.平均分 from StudentInfo SI +left join (select * from StudentCourseScore where CourseId = (select id from CourseInfo where CourseName = '语文')) A on SI.StudentCode = A.StudentId +left join (select * from StudentCourseScore where CourseId = (select id from CourseInfo where CourseName = '数学')) B on SI.StudentCode = B.StudentId +left join (select * from StudentCourseScore where CourseId = (select id from CourseInfo where CourseName = '英语')) C on SI.StudentCode = C.StudentId +left join (select StudentId,AVG(Score)平均分 from StudentCourseScore group by StudentId) D on SI.StudentCode = D.StudentId +order by D.平均分 desc + +-- 14.查询各科成绩最高分、最低分和平均分: +select CourseName,MAX(Score) 最高分,MIN(Score) 最低分,AVG(Score) 平均分 from StudentCourseScore SCS +inner join CourseInfo CI on SCS.CourseId = CI.Id +group by CourseName + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + +select CI.Id,CourseName,MAX(SCS.Score)最高分,MIN(SCS.Score)最低分,AVG(SCS.Score)平均分,COUNT(StudentId)选修人数, +CAST(CONVERT(decimal(10,2),(A.及格人数*1.0*100 / COUNT(StudentId))) as varchar(50)) + '%' 及格率, +CAST(CONVERT(decimal(10,2),(B.中等人数*1.0*100 / COUNT(StudentId))) as varchar(50)) + '%' 中等率, +CAST(CONVERT(decimal(10,2),(C.优良人数*1.0*100 / COUNT(StudentId))) as varchar(50)) + '%' 优良率, +CAST(CONVERT(decimal(10,2),(D.优秀人数*1.0*100 / COUNT(StudentId))) as varchar(50)) + '%' 优秀率 +from CourseInfo CI +left join StudentCourseScore SCS on CI.Id = SCS.CourseId +left join (select CourseId,COUNT(*) 及格人数 from StudentCourseScore where Score >=60 group by CourseId)A on A.CourseId = CI.Id +left join (select CourseId,COUNT(*) 中等人数 from StudentCourseScore where Score between 70 and 79 group by CourseId)B on B.CourseId = CI.Id +left join (select CourseId,COUNT(*) 优良人数 from StudentCourseScore where Score between 80 and 89 group by CourseId)C on C.CourseId = CI.Id +left join (select CourseId,COUNT(*) 优秀人数 from StudentCourseScore where Score >= 90 group by CourseId)D on D.CourseId = CI.Id +group by CI.Id,CourseName,A.及格人数,B.中等人数,C.优良人数,D.优秀人数 +order by COUNT(*) desc,Id asc + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 +select *,RANK()over(partition by CourseID order by Score desc)排名 from StudentCourseScore + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 +select *,dense_RANK()over(partition by CourseID order by Score desc)排名 from StudentCourseScore + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 +--不会 + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 +select CI.Id,CourseName,CAST(CONVERT(decimal(10,2),(A.[0-60]*1.0*100 / COUNT(StudentId))) as varchar(50)) + '%' [0-60所占百分比],A.[0-60] 人数, +CAST(CONVERT(decimal(10,2),(B.[60-70]*1.0*100 / COUNT(StudentId))) as varchar(50)) + '%'[60-70所占百分比],B.[60-70] 人数, +CAST(CONVERT(decimal(10,2),(C.[70-85]*1.0*100 / COUNT(StudentId))) as varchar(50)) + '%'[70-85所占百分比],C.[70-85] 人数, +CAST(CONVERT(decimal(10,2),(D.[85-100]*1.0*100 / COUNT(StudentId))) as varchar(50)) + '%' [85-100所占百分比],D.[85-100] 人数 +from CourseInfo CI +left join StudentCourseScore SCS on CI.Id = SCS.CourseId +left join (select CourseId,COUNT(*) [0-60] from StudentCourseScore where Score between 0 and 60 group by CourseId)A on A.CourseId = CI.Id +left join (select CourseId,COUNT(*) [60-70] from StudentCourseScore where Score between 60 and 70 group by CourseId)B on B.CourseId = CI.Id +left join (select CourseId,COUNT(*) [70-85] from StudentCourseScore where Score between 70 and 85 group by CourseId)C on C.CourseId = CI.Id +left join (select CourseId,COUNT(*) [85-100] from StudentCourseScore where Score between 85 and 100 group by CourseId)D on D.CourseId = CI.Id +group by CI.Id,CourseName,A.[0-60],B.[60-70],C.[70-85],D.[85-100] +order by COUNT(*) desc,Id asc + +-- 18.查询各科成绩前三名的记录 +select SI.*,CHSScore.排名 语文排名,MathScore.排名 数学排名,ENScore.排名 英语排名 from StudentInfo SI +left join (select TOP 3 ROW_NUMBER()OVER(order by Score desc)排名,StudentId,Score from StudentCourseScore + where CourseId = (select Id from CourseInfo where CourseName = '语文') order by Score desc) CHSScore on CHSScore.StudentId = SI.StudentCode +left join (select TOP 3 ROW_NUMBER()OVER(order by Score desc)排名,StudentId,Score from StudentCourseScore + where CourseId = (select Id from CourseInfo where CourseName = '数学') order by Score desc) MathScore on MathScore.StudentId = SI.StudentCode +left join (select TOP 3 ROW_NUMBER()OVER(order by Score desc)排名,StudentId,Score from StudentCourseScore + where CourseId = (select Id from CourseInfo where CourseName = '英语') order by Score desc) ENScore on ENScore.StudentId = SI.StudentCode +where MathScore.排名 is not null or CHSScore.排名 is not null or ENScore.排名 is not null + +-- 19.查询每门课程被选修的学生数 +select CI.Id,CourseName,COUNT(StudentId)选修人数 from CourseInfo CI +left join StudentCourseScore SCS on CI.Id = SCS.CourseId +group by CI.Id,CI.CourseName +order by COUNT(*) desc,Id asc + +-- 20.查询出只选修两门课程的学生学号和姓名 +select StudentCode,StudentName from StudentInfo SI +left join StudentCourseScore SCS on SI.StudentCode = SCS.StudentId +group by StudentCode,StudentName +having count(StudentId) = 2 + +-- 21.查询男生、女生人数 +select sex,COUNT(*)人数0 from StudentInfo group by Sex + +-- 22.查询名字中含有「风」字的学生信息 +select * from StudentInfo where StudentName like '%风%' + +-- 23.查询同名同性学生名单,并统计同名人数 +select StudentName,count(*) 同名人数 from StudentInfo group by StudentName having count(*) >= 2 + +-- 24.查询 1990 年出生的学生名单 +select * from StudentInfo where year(Birthday) = '1990' + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 +select CI.Id,CourseName,AVG(Score)平均成绩 from CourseInfo CI +left join StudentCourseScore SCS on CI.Id = SCS.CourseId +group by CI.Id,CourseName order by AVG(Score) desc,CI.Id asc + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 +select StudentCode,StudentName,AVG(Score)平均成绩 from StudentInfo SI +left join StudentCourseScore SCS on SI.StudentCode = SCS.StudentId +group by StudentCode,StudentName +having AVG(Score) >= 85 + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 +select StudentName,Score from StudentInfo SI left join StudentCourseScore SCS on SI.StudentCode = SCS.StudentId +where CourseId = (select Id from CourseInfo where CourseName = '数学') and Score < 60 + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) +select SI.*,CourseName,SCS.Score from StudentInfo SI left join StudentCourseScore SCS on SI.StudentCode = SCS.StudentId +left join CourseInfo CI on SCS.CourseId = CI.Id + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 +select StudentName,CourseName,SCS.Score from StudentInfo SI left join StudentCourseScore SCS on SI.StudentCode = SCS.StudentId +left join CourseInfo CI on SCS.CourseId = CI.Id where Score > 70 + +-- 30.查询不及格的课程 +select SI.*,CourseName,SCS.Score from StudentInfo SI left join StudentCourseScore SCS on SI.StudentCode = SCS.StudentId +left join CourseInfo CI on SCS.CourseId = CI.Id where Score < 60 + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 +select StudentCode,StudentName from StudentInfo SI left join StudentCourseScore SCS on SI.StudentCode = SCS.StudentId +where CourseId = 01 and Score > 80 + +-- 32.求每门课程的学生人数 +select CI.CourseName,COUNT(*)学生人数 from CourseInfo CI left join StudentCourseScore SCS on CI.Id = SCS.CourseId +group by CourseName + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +select SI.*,Score from StudentInfo SI left join StudentCourseScore SCS on SI.StudentCode = SCS.StudentId +where Score = (select top 1 Score from StudentCourseScore + where CourseId in (select id from CourseInfo where TeacherId in (select Id from Teachers where TeacherName = '张三'))order by Score desc) + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +select SI.*,Score from StudentInfo SI left join StudentCourseScore SCS on SI.StudentCode = SCS.StudentId +where Score = (select MAX(Score) from StudentCourseScore + where CourseId in (select id from CourseInfo where TeacherId in (select Id from Teachers where TeacherName = '张三'))) + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 +select StudentCode,SCS.CourseId,SCS.Score,A.CourseId,A.Score,B.CourseId,B.Score from StudentInfo SI left join StudentCourseScore SCS on SI.StudentCode = SCS.StudentId +cross join StudentCourseScore A cross join StudentCourseScore B +where SCS.StudentId = A.StudentId and A.StudentId = B.StudentId and SCS.CourseId = 1 and A.CourseId = 2 and B.CourseId = 3 and SCS.Score = A.Score and A.Score =B.Score + +-- 36.查询每门功成绩最好的前两名 +select top 2 CHST2.StudentId 语文最好的前两名,CHST2.Score,MathT2.StudentId 数学最好的前两名,MathT2.Score,ENT2.StudentId 英语最好的前两名,ENT2.Score from +(select top 2 StudentId,Score from StudentCourseScore where CourseId = (select id from CourseInfo where CourseName = '语文')order by Score desc) CHST2 +cross join (select top 2 StudentId,Score from StudentCourseScore where CourseId = (select id from CourseInfo where CourseName = '数学')order by Score desc) MathT2 +cross join (select top 2 StudentId,Score from StudentCourseScore where CourseId = (select id from CourseInfo where CourseName = '英语')order by Score desc) ENT2 +where CHST2.StudentId != MathT2.StudentId and MathT2.StudentId = ENT2.StudentId + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 +select CourseName,COUNT(*)选修人数 from CourseInfo CI inner join StudentCourseScore SCS on CI.Id = SCS.CourseId group by CourseName having COUNT(*) > 5 + +-- 38.检索至少选修两门课程的学生学号 +select StudentCode from StudentInfo SI left join StudentCourseScore SCS on SI.StudentCode = SCS.StudentId group by StudentCode having COUNT(CourseId) >= 2 + +-- 39.查询选修了全部课程的学生信息 +select StudentCode from StudentInfo SI left join StudentCourseScore SCS on SI.StudentCode = SCS.StudentId group by StudentCode having COUNT(CourseId) = (select count(*)课程数量 from CourseInfo) + +-- 40.查询各学生的年龄,只按年份来算 +select StudentCode,StudentName,DATEDIFF(YY,Birthday,GETDATE()) 年龄 from StudentInfo + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 +select *, +case when MONTH(Birthday) < MONTH(GETDATE()) + then YEAR(GETDATE()) - YEAR(Birthday) - 1 +when (MONTH(Birthday) = MONTH(GETDATE())) and day(Birthday) < day(GETDATE()) + then YEAR(GETDATE()) - YEAR(Birthday) - 1 +else DATEDIFF(YY,Birthday,GETDATE()) +end 年龄 +from StudentInfo + +-- 42.查询本周过生日的学生 +select * from StudentInfo where DATEPART(wk,Birthday) = DATEPART(wk,getdate()) + +-- 43.查询下周过生日的学生 +select * from StudentInfo where DATEPART(wk,Birthday) - DATEPART(wk,GETDATE()) = 1 + +-- 44.查询本月过生日的学生 +select * from StudentInfo where DATEPART(mm,Birthday) - DATEPART(mm,GETDATE()) = 0 + +-- 45.查询下月过生日的学生 +select * from StudentInfo where datepart(mm,Birthday) = (DATEPART(mm,GETDATE()) + 1) + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d08f662c742c9e6ed9cdd0529c35e838197f9395 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" @@ -0,0 +1,56 @@ +/** +* 复习题2 +*/ + +use master +go + +create database HOUSE_DB +on primary +( + name = 'House', + filename = 'D:\Document\MSSQLDATABASE\House\house.mdf', + size = 5MB, + maxsize = 50MB, + filegrowth = 1MB +) +log on +( + name = 'House_log', + filename = 'D:\Document\MSSQLDATABASE\House\house_log.ldf', + size = 5MB, + maxsize = 10MB, + filegrowth = 1MB +) +go + +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, --类型编号 + type_name varchar(50) unique(type_name) not null, --类型名称 +) +go + +create table HOUSE +( + house_id int primary key identity(1,1) not null, --房屋编号 + house_name varchar(50) not null, --房屋名称 + house_price float default(0) not null, --房租 + type_id int references HOUSE_TYPE(type_id) not null +) +go + +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') +go + +insert into HOUSE values ('小户型',1000.0,1),('经济型',2000.5,2),('别墅',4000.9,3) +go + +select * from HOUSE +select * from HOUSE where house_name like '%型%' +select house_name,house_id from HOUSE order by house_price desc +select house_name,type_name from HOUSE H inner join HOUSE_TYPE HT on H.type_id = HT.type_id +select house_name,house_price from HOUSE where house_price in(select MAX(house_price) from HOUSE) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9ea7e93a527ff6a6b5ec90e28a6e8bc45489f44e --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery3.sql" @@ -0,0 +1,47 @@ +/** +* 复习题3 +*/ +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity(1,1) not null,--明星类型编号 + T_NAME nvarchar(20) +) +go + +create table StartInfo +( + S_NO int primary key identity(1,1) not null,--明星编号 + S_NAME nvarchar(20) not null,--明星姓名 + S_AGE int not null,--明星年龄 + S_HOBBY nvarchar(20),--特技 + S_NATIVE nvarchar(20) default('中国大陆'), --籍贯 + S_T_NO int references StarType(T_NO) --明星类型编号 +) +go + +insert into StarType values ('体育明星'),('IT明星'),('相声演员') +go + +insert into StartInfo values ('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2),('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) +go + +--查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select TOP 3 S_NAME 明星姓名,S_HOBBY 特价,S_NATIVE 籍贯 from StartInfo order by S_AGE desc + +--按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 明星类型,count(*) 人数,AVG(S_AGE) 平均年龄 from StartInfo group by S_T_NO having COUNT(*) > 2 + +--查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select S_NAME 明星姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StartInfo +where S_AGE in(select MAX(S_AGE) from StartInfo where S_T_NO = (select T_NO from StarType where T_NAME = '体育明星')) diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cda24cd71134f7e1ed181ba583d0f2c43e3e9d8b --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery2.sql" @@ -0,0 +1,64 @@ +use master +go +create database HOUSE_DB +on( + name='HOUSE_DB', + filename='D:\SQL\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1% +) +log on +( + name='HOUSE_DB_log', + filename='D:\SQL\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1% +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique not null, + +) + +create table HOUSE +( + house_id int primary key identity(1,1) not null, + house_name varchar(50) not null, + house_price float default(0) not null, + type_id int not null foreign key references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +select * from HOUSE_TYPE +insert into HOUSE_TYPE +select '小户型' union +select '经济型' union +select '别墅 ' +--HOUSE表中添加至少3条数据,不能全都为同一类型 +select * from HOUSE + +insert into HOUSE +select '家居房', 3000,1 union +select '学区房',6000,2 union +select '郊外别墅',10000,3 + +--4、添加查询 +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE +select * from HOUSE_TYPE where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋的名称,house_price 租金 from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,HOUSE_TYPE.type_id from HOUSE inner join HOUSE_TYPE on HOUSE_TYPE.type_id=HOUSE.house_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price 租金,house_name 房屋的名称 from HOUSE order by house_price desc + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7a0acd7ad1391c5a91c9ced5e6ea538ec1d66879 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery3.sql" @@ -0,0 +1,62 @@ +use master +go +create database StarManagerDB + +on( + name='StarManagerDB', + filename='D:\SQL\StarManagerDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1% +) +log on +( + name='StarManagerDB_log', + filename='D:\SQL\StarManagerDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1% +) + +create table StarType +( + T_NO int primary key identity(1,1), + T_NAME nvarchar(20) not null +) +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) +drop table StarType +drop table StarInfo +select * from StarType +insert into StarType +values('体育明星'),('IT明星'),('相声演员') + +select * from StarInfo +insert into StarInfo +values('梅西',30,'射门','阿根廷',1) +,('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马克斯',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select * from StarType--(明星类型表) +select * from StarInfo--(明星信息表) +select top 3 S_AGE 年龄,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc +--4、按 明星类型编号 分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +SELECT * FROM StarInfo + +select S_T_NO 明星类型编号,count(S_NO) 明星人数,avg(S_AGE) 明星平均年龄 from StarInfo +group by S_T_NO having count(S_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + +select top 1 T_NAME 类型,S_AGE 年龄最大,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 FROM StarInfo +INNER JOIN StarType ON T_NO=S_T_NO where T_NAME='体育明星' ORDER BY S_AGE DESC \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery50.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery50.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e4d8bedf8a31b643e07e8e0ca60b3078ecc10a2c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery50.sql" @@ -0,0 +1,439 @@ +-- 练习题目: +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore + +-- 1.查询"数学 "课程 比" 语文 "课程 成绩高的学生的信息及课程分数 + +select C.*,A.Score 数学,B.Score 语文 FROM StudentCourseScore A,StudentCourseScore B,StudentInfo C +where A.CourseId=2 and B.CourseId=1 and A.StudentId=B.StudentId and A.Score>B.Score AND C.Id=A.StudentId + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * FROM StudentCourseScore A inner join StudentCourseScore B on A.StudentId=B.StudentId inner join StudentInfo C +on C.Id=A.StudentId +and A.CourseId=2 and B.CourseId=1 + +select * from StudentInfo AA +join (select A.StudentId from StudentCourseScore A,StudentCourseScore B where A.CourseId=2 and B.CourseId=1 AND A.StudentId=B.StudentId ) BB +ON AA.Id=BB.StudentId + + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B on A.StudentId=B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) A---有误出错啦 + + + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore + +SELECT a.StudentId 学生编号, StudentName 学生姓名,avg(score) 平均成绩 + from StudentCourseScore a inner join StudentInfo b on a.StudentId=b.Id group by a.StudentId,StudentName + having avg(score)>=60 + +-- 3.查询在 成绩 表存在成绩的学生信息 + +select distinct b.* from StudentCourseScore a inner join StudentInfo b +on a.StudentId=b.Id + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select a.StudentCode 学生编号,a.StudentName 学生姓名,count(ClassId) 选课总数,sum(b.Score)总成绩 from StudentInfo a left join StudentCourseScore b on +a.ID=b.StudentId group by a.StudentCode,a.StudentName + +-- 4.1 查有成绩的学生信息 +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore + +select a.StudentName 学生名字,b.StudentId ,a.Birthday 出生日期,a.Sex 性别,a.StudentCode from StudentInfo a left join StudentCourseScore b +on a.Id=b.StudentId group by a.StudentName,b.StudentId,a.Birthday,a.Sex,a.StudentCode + +-- 5.查询「李」姓老师的数量 +select TeacherName, count(TeacherName )数量 from Teachers where TeacherName like '李%' group by TeacherName + +-- 6.查询学过「张三」老师授课的同学的信息 + +select b.* from StudentCourseScore a +inner join StudentInfo b on a.StudentId=b.Id where CourseId='2' +-- 7.查询没有学全所有课程的同学的信息 +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore + +select StudentId,b.studentName,b.ClassId,b.Birthday,b.Sex,b.StudentCode, +count(courseId)课程数量 from StudentCourseScore a left join StudentInfo b on a.StudentId=B.Id +where CourseId in (select id from CourseInfo) group by +StudentId,b.StudentName,b.ClassId,b.Birthday,b.Sex,b.StudentCode +having count(courseId)<3 +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select * from StudentInfo where StudentCode in(select distinct b.StudentId from StudentCourseScore a,StudentCourseScore b +where a.StudentId = 1 and a.StudentId!=1 and a.CourseId=b.CourseId) +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +SELECT StudentId 编号,COUNT(DISTINCT CourseId)课程 FROM StudentCourseScore WHERE StudentId!=1 AND CourseId 课程编号 IN + (select distinct courseID from StudentCourseScore where StudentId=1) +GROUP BY StudentId HAVING COUNT(DISTINCT CourseId)= (select COUNT(distinct courseID) from StudentCourseScore where StudentId=1) + + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select * from StudentInfo where id not in(select StudentId from StudentCourseScore +where CourseId in(select id from CourseInfo where teacherid=(select id from Teachers where TeacherName = '张三'))) + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 +select + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + +-- 14.查询各科成绩最高分、最低分和平均分: + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + +-- 18.查询各科成绩前三名的记录 + + +-- 19.查询每门课程被选修的学生数 + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..dab40382cf7134993e878d010851d22949ce9a64 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/SQLQuery3.sql" @@ -0,0 +1,285 @@ +-- 练习题目: + +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 + +select b.*,a.Score 数学成绩,c.Score 语文成绩 +from StudentCourseScore a,StudentInfo b,StudentCourseScore c +where a.StudentId=c.StudentId and a.CourseId =1 and c.CourseId =2 and c.Score=2 + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + +select a.* from StudentInfo a join StudentCourseScore b on a.Id=b.StudentId +join CourseInfo c on b.CourseId=c.Id where c.CourseName='数学' and b.Score<60 +order by b.Score DESC + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + +select C.StudentName 姓名,A.CourseId 课程编号,A.Score 成绩,B.平均成绩 from +(select a.StudentId,a.CourseId,a.Score from StudentCourseScore a group by StudentId,a.CourseId,a.Score) A +left join (select StudentId,AVG(Score) 平均成绩 from StudentCourseScore group by StudentId) B on A.StudentId=B.StudentId +join StudentInfo C on A.StudentId=C.Id + +-- 14.查询各科成绩最高分、最低分和平均分: + +select a.CourseName 科目,max(b.Score) 最高分,min(b.Score) 最低分,avg(b.Score) 平均分 +from CourseInfo a join StudentCourseScore b on a.Id=b.CourseId +group by a.CourseName + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + --及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + --要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + --按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 +select a.课程ID,a.课程名,a.最高分,a.最低分,a.平均分,(b.及格人数/a.选修人数) from +(select a.Id 课程ID,a.CourseName 课程名,max(b.Score) 最高分,min(b.Score) 最低分,avg(b.Score) 平均分,count(b.Score) 选修人数 +from CourseInfo a join StudentCourseScore b on a.Id=b.CourseId +group by a.Id,a.CourseName) a left join +(select a.CourseId,count(a.Score) 及格人数 from (select * from StudentCourseScore where Score>60 ) a group by a.CourseId) b on a.课程ID=b.CourseId +left join (select a.CourseId,count(a.Score) 中等人数 from (select * from StudentCourseScore where Score between 70 and 80 ) a group by a.CourseId) c on c.CourseId=b.CourseId +left join (select a.CourseId,count(a.Score) 优良人数 from (select * from StudentCourseScore where Score between 80 and 90 ) a group by a.CourseId) d on c.CourseId=d.CourseId +left join (select a.CourseId,count(a.Score) 优秀人数 from (select * from StudentCourseScore where Score>=90 ) a group by a.CourseId) e on d.CourseId=e.CourseId + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + +select * from CourseInfo +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6a6f04a9875c0a05da926f2127eb23a08cc011ab --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery2.sql" @@ -0,0 +1,55 @@ +create database HOUSE +on +( + name='HOUSE', + filename='D:\fz\HOUSE.mdf', + size=10mb, + filegrowth=2mb + +) +log on +( + name='HOUSE_log', + filename='D:\fz\HOUSE_log.ldf', + size=10mb, + filegrowth=2mb + + +) +use HOUSE +go +create table HOUSE_TYPE +( + typeid int not null primary key identity(1,1) , + typename varchar(50) not null unique , + +) +create table HOUSE +( + houseid int not null primary key identity , + housename varchar(50) not null, + houseprice float not null default('0'), + typeid int not null foreign key(typeid)references HOUSE_TYPE(typeid) + +) +insert into HOUSE_TYPE(typename)values('小型户'),('经济型'),('别墅') +select * from HOUSE_TYPE +insert into HOUSE(housename,houseprice,typeid)values('碧桂园',6000,1), +('好利园',5000,1), +('家园',4000,2), +('华人园',3000,3), +('老家园',2000,3) +select * from HOUSE +--查询所有房屋信息 +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE + +--查询出房屋的名称和租金,并且按照租金降序排序 +select housename,houseprice from HOUSE + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select housename,typename from HOUSE inner join HOUSE_TYPE on HOUSE.typeid=HOUSE_TYPE.typeid + + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 max(houseprice) 房租,housename from HOUSE group by housename,houseprice order by houseprice desc diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b6b37f06023fef14a9d093c8b4e03eb59073562b --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery3.sql" @@ -0,0 +1,51 @@ +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\mx\StarManagerDB.mdf' + +) +log on +( + name='StarManagerDB_log', + filename='D:\mx\StarManagerDB_log.ldf' + + +) +use StarManagerDB +go +create table StarType +( + t_no int not null primary key identity(1,1), + t_name nvarchar(20) + +) +create table StarInfo + ( + s_no int not null primary key identity(1,1), + s_name nvarchar(20) not null , + s_age int not null, + s_hobby nvarchar(20), + s_native nvarchar(20) default('中国大陆'), + t_no int foreign key(t_no)references StarType(t_no) + + ) + insert into StarType(t_name)values('体育明星'),('IT明星'),('相声明星') + select * from StarType + insert into StarInfo(s_name,s_age,s_hobby,s_native,t_no)values + ('梅西',30,'射门','阿根廷',1), + ('科比',35,'过人','美国',1), + ('菜景现',40,'敲代码','中国',2), + ('马斯克',36,'造火箭','外星人',2), + ('郭德纲',30,'相声','中国',3), + ('黄铮',41,'拼多多','中国',2) + +select * from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 s_name 姓名,s_hobby 特技,s_native 籍贯 from StarInfo order by s_age desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select t_no 明星类型编号, count(*)人数, avg(s_age)平均年龄 from StarInfo group by t_no having count(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + select top 1 s_name 姓名,s_hobby 特技,s_native 籍贯 from StarInfo where t_no='1'order by s_age desc diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery4.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1e3147797277d747df83068f41ac4b0d6b14d4f7 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery4.sql" @@ -0,0 +1,440 @@ + + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go +select * from StudentInfo--学生表 +select * from Teachers--教师表 +select * from CourseInfo--课程表 +select * from StudentCourseScore + +-- 练习题目: + + + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select C.*,A.Score 数学成绩,B.Score 语文成绩 from StudentCourseScore A,StudentCourseScore B,StudentInfo C +where A.CourseId=2 and B.CourseId=1 and A.StudentId=B.StudentId and A.Score>B.Score and C.Id=A.StudentId + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from StudentInfo AA join (select A.StudentId from StudentCourseScore A,StudentCourseScore B +WHERE A.CourseId=2 and B.CourseId=1 and A.StudentId=B.StudentId) +BB on AA.Id=BB.StudentId + + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select * from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo WHERE CourseName='数学'))A JOIN +(select * from StudentCourseScore A where CourseId=( select Id from CourseInfo where CourseName='语文')) +B on A.StudentId=B.StudentId + + + +select * from StudentInfo--学生表 +select * from Teachers--教师表 +select * from CourseInfo--课程表 +select * from StudentCourseScore + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select StudentId,CourseId from StudentCourseScore where CourseID!='1' group by StudentId,CourseId--未完成、 + + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentCode 学生编号,StudentName 学生姓名,avg(Score)平均成绩 from StudentCourseScore inner join StudentInfo on +StudentCourseScore.Id= StudentInfo.Id group by StudentName ,StudentCode having avg(Score) >=60 + + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentCourseScore + + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + + + + + +select * from StudentInfo--学生表 +select * from Teachers--教师表 +select * from CourseInfo--课程表 +select * from StudentCourseScore +-- 4.1 查有成绩的学生信息 +select A.*,B.* from StudentCourseScore A inner join StudentInfo B on A.Id=B.Id where Score!='0' + + +-- 5.查询「李」姓老师的数量 +select * from Teachers where TeacherName like '李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +select B.* from StudentInfo B inner join CourseInfo A on B.Id=A.Id where ClassId='1' + + +-- 7.查询没有学全所有课程的同学的信息 + + + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + + + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/50 (2).sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/50 (2).sql" new file mode 100644 index 0000000000000000000000000000000000000000..d601ed91613bd3e62060e3fab7efbd15f56e8022 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/50 (2).sql" @@ -0,0 +1,58 @@ +use master +go +use ClassicDb +go +select * from Teachers +select * from CourseInfo +select * from StudentInfo +select * from StudentCourseScore +-- 1.查询"数学 "课程比" 语文 "课程成绩高 的 学生的信息 及 课程分数 +select C.*,A.Score 语文, B.Score 数学 from StudentCourseScore A,StudentCourseScore B,StudentInfo C +where A.CourseId=1 and B.CourseId=2 and A.StudentId=B.StudentId and A.Score=60 +-- 3.查询在 成绩 表存在成绩的学生信息 +select distinct StudentInfo.* from StudentCourseScore +inner join StudentInfo on StudentInfo.Id=StudentCourseScore.StudentId +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentInfo.Id 学生编号,StudentName 学生姓名,count( StudentCourseScore.StudentId)选课总数,sum(StudentCourseScore.Score) 总成绩 from StudentInfo +left join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +group by StudentInfo.Id,StudentName,StudentCourseScore.StudentId +-- 4.1 查有成绩的学生信息 +select distinct StudentInfo.* from StudentInfo +inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +-- 5.查询「李」姓老师的数量 +select COUNT(*)数量 from Teachers where TeacherName like '李%' +-- 6.查询学过「张三」老师授课的同学的信息 +select StudentInfo.* from StudentInfo +inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +left join CourseInfo on StudentCourseScore.CourseId=CourseInfo.Id +inner join Teachers on Teachers.Id=CourseInfo.TeacherId +where Teachers.TeacherName='张三' +-- 7.查询没有学全所有课程的同学的信息 +select StudentId,StudentInfo.StudentName,StudentInfo.Birthday,StudentInfo.ClassId,StudentInfo.Sex,StudentInfo.StudentCode,count(*) from StudentCourseScore +inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id +group by StudentId,StudentInfo.Birthday,StudentInfo.ClassId,StudentInfo.Sex,StudentInfo.StudentCode,StudentInfo.StudentName having count(*)<3 +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select * from +(select * from StudentCourseScore where StudentId=(select Id from StudentInfo where StudentName='赵雷')) +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select StudentInfo.* from StudentCourseScore A +inner join StudentCourseScore B on A.StudentId=B.StudentId +inner join StudentInfo on A.StudentId=StudentInfo.Id where A.CourseId=B.CourseId +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select StudentInfo.StudentName from StudentInfo +inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +where CourseId !=(select Id from Teachers where TeacherName='张三') \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/SQLQuery1 (3).sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/SQLQuery1 (3).sql" new file mode 100644 index 0000000000000000000000000000000000000000..72203a35346ad74ecab51d2883b02b3ea5e3c8c2 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/SQLQuery1 (3).sql" @@ -0,0 +1,63 @@ +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\test\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=1 + ) + log on + ( + name='HOUSE_DB_log', + filename='D:\test\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1 + ) + + go + use HOUSE_DB + go + + create table HOUSE_TYPE + ( + type_id int primary key identity (1,1) not null, + type_name varchar(50) unique not null, + ) + + insert into HOUSE_TYPE(type_name) values ('小户型'),('经济型'),('别墅') + + select * from HOUSE_TYPE + + create table HOUSE + ( + house_id int primary key identity (1,1), + house_name varchar(50) not null, + house_price float(2) default ('0') not null, + type_id int references HOUSE_TYPE(type_id) + ) + + insert into HOUSE(house_name,house_price,type_id) values ('水林天禧','1234','1'),('龙门天下','7894','2'),('蓝天花园','9674','3') + + select *from HOUSE + +-- 4、添加查询 +--查询所有房屋信息 + select * from HOUSE_TYPE + select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 + select * from HOUSE_TYPE + inner join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id + where type_name like '__型' +--查询出房屋的名称和租金,并且按照租金降序排序 + select house_name as 房屋名称,house_price as 租金 from HOUSE + group by house_name,house_price order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + select HOUSE_TYPE.type_name,HOUSE.house_name as 房屋名称 from HOUSE_TYPE + inner join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + select top 1 house_price as 租金 , HOUSE.house_name as 房屋名称 from HOUSE \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e61adc914ce73a1bcc352737a9841f7cb590f155 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/SQLQuery3.sql" @@ -0,0 +1,60 @@ +use master +go +create database StarManagerDB +on( + name='StarManagerDB', + filename='C:\test\StarManagerDB.mdf', + size=5, + maxsize=50, + filegrowth=1% +) +log on( + name='StarManagerDB_log', + filename='C:\test\StarManagerDB_log.ldf', + size=5, + maxsize=50, + filegrowth=1% +) +go +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20), +) +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO), +) +select * from StarType +select * from StarInfo +insert into StarType values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41, '拼多多','中国',2) + + + +select * from StarType +select * from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + select top 3 S_AGE as 年龄 ,S_NAME as 名字,S_HOBBY as 特技,S_NATIVE as 籍贯 from StarInfo order by S_AGE desc +--4、按明星类型编号 分类查询 明星人数 ,明星平均年龄 ,显示明星人数大于2的分组信息,要求使用别名显示列名。 + select S_T_NO, count(S_T_NO) as 明星人数,avg(S_AGE)as平均年龄 from StarInfo + group by S_T_NO + having count(S_T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + select top 1 S_AGE as 年龄,S_NAME as 名字,S_HOBBY as 特技,S_NATIVE as 籍贯 from StarType + inner join StarInfo on StarInfo.S_T_NO=StarType.T_NO + where T_NO ='1' + group by S_AGE ,S_HOBBY,S_NATIVE,S_NAME \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ce8688887bdafb8d63865d12959be3babd68b08c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" @@ -0,0 +1,426 @@ +-- 练习题目: + +select * from dbo.StudentInfo +select * from dbo.Teachers +select * from CourseInfo +select * from StudentCourseScore + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select C.*,A.Score 数学成绩,B.Score 语文成绩 from StudentCourseScore A,StudentCourseScore B,StudentInfo C +where A.CourseId=2 and B.CourseId=1 AND A.StudentId=B.StudentId AND A.Score>B.Score AND C.Id=A.StudentId + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select * from StudentCourseScore A inner join StudentCourseScore B on A.StudentId = B.StudentId inner join StudentInfo C on +C.Id = A.StudentId AND A.CourseId = 1 and B.CourseId = 2 + + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) + +select * from (select * from StudentCourseScore where CourseId=2) A left join +(select * from StudentCourseScore where CourseId=1) B on A.studentid=b.studenti + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + + + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select StudentCode,studentname,avg(score) 平均成绩 from StudentCourseScore a inner join StudentInfo b on a.StudentId=b.Id +group by StudentCode,studentname having avg(score)>=60 + + +-- 3.查询在 成绩 表存在成绩的学生信息 +select distinct a.* from StudentCourseScore b join StudentInfo x on a.StudentId=b.Id + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select StudentCode,StudentName,count(CourseId) 选课总数,sum(Score)课程的总成绩 from StudentInfo a left join StudentCourseScore b +on a.Id=b.StudentId group by StudentCode,StudentName + + +-- 4.1 查有成绩的学生信息 + +select distinct a.* from StudentCourseScore b join StudentInfo a on t.StudentId=b.Id + +-- 5.查询「李」姓老师的数量 +select count(id) 老师的数量 from Teachers where TeacherName like '李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 + +select a.* from StudentCourseScore b inner join StudentInfo x on a.StudentId=b.Id + where courseid=(select id from Teachers where TeacherName='张三') + +-- 7.查询没有学全所有课程的同学的信息 +select a.* from StudentInfo a left join StudentCourseScore b +on a.Id=b.StudentId where score is null + + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select * from StudentInfo where StudentCode in(select distinct B.StudentId from StudentCourseScore A,StudentCourseScore B +where A.StudentId = 1 and B.StudentId != 1 and B.CourseId = A.CourseId) + + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select * from StudentInfo +where StudentCode not in (select StudentId from StudentCourseScore where CourseId = (select CourseId from Teachers where TeacherName = '张三')) + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bb06e582488b9d825333414ca7b3656952bdbee4 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery2.sql" @@ -0,0 +1,58 @@ +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\HOUSE_DB.mdf', +size=5MB, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_log', +filename='D:\HOUSE_DB_log.ldf', +size=5MB, +maxsize=50mb, +filegrowth=1mb +) +use HOUSE_DB +go +create table HOUSE_TYPE +( +type_id int primary key identity not null, +type_name varchar(50) unique not null +) +create table HOUSE +( +house_id int primary key identity not null, +house_name varchar(50) not null, +house_price float default(0) not null, +type_id int foreign key references HOUSE_TYPE not null +) + +--添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 + insert into HOUSE_TYPE values + ('小户型'), + ('经济型'), + ('别墅') + select * from HOUSE_TYPE + insert into HOUSE values + ('天上人间',200,3), + ('地狱边境',300,2), + ('时间尽头',400,1) + select * from HOUSE + +-- 添加查询 +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%%型' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price DESC +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name from HOUSE +inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 (house_price),house_name from HOUSE order by house_price desc diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8ee4e3da6dc47af89fbe27e174150a3e2034d563 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery3.sql" @@ -0,0 +1,43 @@ +create database StarManagerDB +use StarManagerDB +go +create table StarType +( +T_NO int primary key identity(1,1) not null , +T_NAME nvarchar(20) +) + +create table StarInfo +( +S_NO int primary key identity(1,1) not null, +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NARIVE nvarchar(20) default('中国大陆'), +S_T_NO int foreign key references StarType(T_NO) +) + +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') +select * from StarType + +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + +select * from StarInfo + +--查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_AGE 年龄, S_HOBBY 特技,S_NARIVE 籍贯 from StarInfo order by S_AGE DESC +--按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select COUNT(*)明星人数,AVG(S_AGE)平均年龄 from StarInfo +inner join StarType ON StarType.T_NO = StarInfo.S_T_NO group by T_NAME having count(*)>2 +--查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名, S_HOBBY 特技, S_NARIVE 籍贯 from StarInfo +inner join StarType on StarType.T_NO=StarInfo.S_NO where StarInfo.S_T_NO=1 order by S_AGE DESC diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..459411d0b50b7f38b0ce8d195b28e9a3b8d78503 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" @@ -0,0 +1,446 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +select * from StudentInfo +select * from StudentCourseScore +select * from Teachers +select * from CourseInfo + +-- 练习题目: + +-- 1.查询 "数学" 课程比 "语文" 课程成绩高的(同一个学生) 学生的信息 及 课程分数 +select A.Score 数学成绩,B.Score 语文成绩,C.* from StudentCourseScore A,StudentCourseScore B ,StudentInfo C +where A.CourseId=2 and B.CourseId=1 and A.StudentId=B.StudentId and A.Score>B.Score and C.StudentCode=A.StudentId +-- 1.1 查询同时存在"数学"课程和"语文"课程 的学生的情况 +select A.Score 数学成绩,B.Score 语文成绩,C.* from StudentCourseScore A,StudentCourseScore B ,StudentInfo C +where A.CourseId=2 and B.CourseId=1 and A.StudentId=B.StudentId and C.StudentCode=A.StudentId +-- 1.2 查询存在"数学"课程但可能不存在"语文"课程的情况(不存在时显示为 null ) +select * from +( +select * from StudentCourseScore A where CourseId= +(select Id from CourseInfo where CourseName='数学') +) A +left join +( +select * from StudentCourseScore A where CourseId= +(select Id from CourseInfo where CourseName='语文') +) B +on A.StudentId=B.StudentId + +-- 1.3 查询不存在"数学"课程但存在"语文"课程的情况 +select A.Id,A.StudentId,A.CourseId,A.Score,B.CourseId,B.Score +from (select * from StudentCourseScore a where a.CourseId=1) A +left join (select * from StudentCourseScore a where a.CourseId=2) B on A.StudentId=B.StudentId +where B.CourseId is null + + + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select SI.StudentCode 学生编号,SI.StudentName 学生姓名, avg(Score)平均成绩 +from StudentCourseScore SC +inner join StudentInfo SI on SC.StudentId=SI.StudentCode +group by SI.StudentCode,SI.StudentName +having avg(Score)>=60 + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentInfo where Id in (select distinct b.StudentId from StudentCourseScore b) + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +--select * from StudentInfo +--select * from StudentCourseScore +select ST.Id 学生编号,ST.StudentName 学生姓名,count(SC.CourseId)选课总数,sum(SC.Score)总成绩 +from StudentInfo ST left join StudentCourseScore SC on ST.ID=SC.StudentId +group by ST.Id,ST.StudentName + + +-- 4.1 查有成绩的学生信息 +--select * from StudentInfo +--select * from StudentCourseScore +select * from StudentInfo +where Id in (select StudentId from StudentCourseScore where Score is not null ) + + +-- 5.查询「李」姓老师的数量 +--select * from Teachers +select count(TeacherName)李姓老师的数量 from Teachers +where TeacherName like '李%' + + +-- 6.查询学过「张三」老师授课的同学的信息 +--select * from StudentInfo +--select * from StudentCourseScore +--select * from Teachers +--select * from CourseInfo +select * from StudentInfo +where ID= (select StudentId from StudentCourseScore +where (CourseId=(select ID from CourseInfo +where TeacherId =(select Id from Teachers +where TeacherName='张三')))) + +-- 7.查询没有学全所有课程的同学的信息 + + + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + + + +-- 9.查询和"01"号的同学学习的课程 完全相同的其他同学的信息 + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3c79b4ca07a8ea61589a76edc414e891b9c6c9ba --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery2.sql" @@ -0,0 +1,79 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +log on +( + name='HOUSE_DB_ldf', + filename='D:\HOUSE_DB_ldf.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) + +use HOUSE_DB + +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 +create table HOUSE_TYPE +( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null unique +) + + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null , + house_price float not null default(0), + tyoe_id int not null foreign key references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') + +insert into HOUSE(house_name,house_price,tyoe_id) values ('A',5000,1) , ('B',6000,2) ,('C',8000,3) + +SELECT * FROM HOUSE_TYPE + +--4、添加查询 +--查询所有房屋信息 + SELECT * FROM HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +SELECT * FROM HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 名称, house_price 租金 from HOUSE order by house_price DESC + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE_TYPE +inner join HOUSE on HOUSE_TYPE.type_id =HOUSE.tyoe_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select Top 1 house_price 最高的租金, house_name 房屋名称 from HOUSE order by house_price DESC \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a165dd973b83e99a48228e317524270a8791a31f --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery3.sql" @@ -0,0 +1,73 @@ +--1、创建明星数据库(StarManagerDB) +use master +go + +create database StarManagerDB +go + +use StarManagerDB +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NatIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) + + + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 +insert into StarType values ('体育明星'),('IT明星'),('相声明星') + +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NatIVE,S_T_NO) +values ('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景观',40,'敲代码','中国',2), +('马斯克',36,'外星人','中国',2),('郭德纲',50,'相声','中国',3),('黄峥',41,'拼多多','中国',2) + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + + +select * from StarType +select * from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select TOP 3 S_AGE 年龄 ,S_NAME 姓名,S_HOBBY 特技, S_NatIVE 籍贯 FROM StarInfo order by S_AGE DESC + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +SELECT S_T_NO 类型编号 ,COUNT(*) 明星人数 ,AVG(S_AGE)明星平均年龄 FROM StarInfo GROUP BY S_T_NO HAVING COUNT(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +SELECT TOP 1 S_AGE 年龄 ,S_NAME 姓名, S_HOBBY 特技 ,S_NatIVE 籍贯 FROM StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO +where T_NAME='体育明星' order by S_AGE DESC \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f2b5b10d3bd0aff7872e4b80089bad6514d63486 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery2.sql" @@ -0,0 +1,77 @@ +use master +go + + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1% +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1% + ) + +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null unique +) + + +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null , + house_price float not null default('0'), + type_id int not null +) +alter table HOUSE add constraint FK_HOUSE_type_id foreign key (type_id) references HOUSE_TYPE(type_id) + + +delete from HOUSE_TYPE where type_id=3 + +insert into HOUSE_TYPE(type_id,type_name) +values(1,'小户型'), + (2,'经济型'), + (3,'别墅') +select * from HOUSE_TYPE order by type_id + +delete from HOUSE + +set IDENTITY_INSERT HOUSE ON +insert into HOUSE(house_id,house_name,house_price,type_id ) +values(1,'海边公寓','1000',2), + (2,'花园小房','500',1), + (3,'海边别墅','2000',3) +set IDENTITY_INSERT HOUSE OFF + +select * from HOUSE + + + +--4、添加查询 +--查询所有房屋信息 +--使用模糊查询 包含”型“字 的 房屋类型信息 +select * FROM HOUSE_TYPE WHERE type_name like '%型%' + +--查询出 房屋的名称 和 租金,并且按照 租金 降序排序 +select house_name 房屋名称,house_price 租金 FROM HOUSE order by house_price DESC + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型 from HOUSE +inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id + +--查询所有 房屋中月租最高的房屋,显示 最高的租金 和 房屋名称 +select top 1 house_price 租金,house_name 房屋名称 from HOUSE order by house_price DESC diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..98e8fb6e3acad7e4f1c77c23137647403fbaef70 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery3.sql" @@ -0,0 +1,54 @@ +use master +go +create database StarManagerDB +go +use StarManagerDB +go + +create table StarType --明星类型表 +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) not null +) + +create table StarInfo --明星信息表 +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +insert into StarType +values('体育明星'), + ('IT明星'), + ('相声明星') +select* from StarType + + +insert into StarInfo +values('梅西',30,'射门','阿根廷',1), + ('科比',35,'过人','美国',1), + ('蔡景现',40,'敲代码','中国',2), + ('马斯克',36,'造火箭','外星人',3), + ('郭德纲',50,'相声','中国',3), + ('黄铮',41,'拼多多','中国',2) +select* from StarInfo + +--3、查询年龄最大的3个明星的 姓名, 特技 和 籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc + +--4、按明星 类型编号分类查询 明星人数, 明星平均年龄 , 显示明星人数 大于2的分组信息,要求使用别名显示列名。 +select * from StarInfo + +select S_T_NO 类型编号, count(S_T_NO) 明星人数 ,avg(S_AGE) 明星平均年龄 from StarInfo +group by S_T_NO having count(S_T_NO)>2 + +--5、查询明星类型为 “体育明星” 中 年龄最大 的 姓名 、 特技 、 籍贯信息 ,要求显示列别名。 +select top 1 T_NAME 明星类型 , S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo +left join StarType on StarInfo.S_T_NO=StarType.T_NO where T_NAME='体育明星' +order by S_AGE desc + + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery4.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a830441084934539c9ec38a67bbfba16392bcc27 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery4.sql" @@ -0,0 +1,277 @@ +use ClassicDb +go + +select * FROM CourseInfo +select * from StudentCourseScore +select* from StudentInfo +select * from Teachers + +-- 练习题目: + + + +-- 1.查询 "数学 "课程 比 " 语文 "课程 成绩高的 学生的信息 及 课程分数 +select C.*,A.Score 数学成绩,B.Score 语文成绩 from StudentCourseScore A,StudentCourseScore B ,StudentInfo C +where A.CourseId=2 and B.CourseId=1 and A.StudentId=B.StudentId and A.Score>B.Score and A.StudentId=C.Id + +-- 1.1 查询同时存在 " 数学 "课程 和 " 语文 "课程的学生情况 +select C.* from StudentCourseScore A,StudentCourseScore B ,StudentInfo C +where A.CourseId=2 and B.CourseId=1 and A.StudentId=B.StudentId and A.StudentId=C.Id + + +-- 1.2 查询存在 " 数学 "课程但可能 不存在" 语文 "课程 的学生情况(不存在时显示为 null ) +select* from +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='数学')) A left join +(select * from StudentCourseScore A where CourseId=(select Id from CourseInfo where CourseName='语文')) B +on A.StudentId=B.StudentId + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 + +select * from StudentCourseScore where CourseId=1 and StudentId +not in(select StudentId from StudentCourseScore where CourseId=2 ) + +-- 2.查询平均成绩大于等于 60 分的同学的 学生编号 和 学生姓名 和 平均成绩 +select StudentId 学生编号,StudentName 学生姓名,avg(Score) 平均成绩 from StudentCourseScore +inner join StudentInfo on StudentInfo.Id=StudentCourseScore.StudentId +group by StudentId,StudentName having avg(Score)>=60 + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentCourseScore +select * from StudentInfo + +select distinct A.* FROM StudentInfo A +left join StudentCourseScore B on A.Id=B.StudentId + + +-- 4.查询所有同学的 学生编号 、学生姓名、 选课总数、 所有课程的总成绩 (没成绩的显示为 null ) +select*from StudentCourseScore + +select StudentId 学生编号,StudentName 学生姓名,count(CourseId) 选课总数,sum(Score) 课程总成绩 from StudentCourseScore A +right join StudentInfo B ON A.StudentId=B.Id group by StudentId,StudentName + +-- 4.1 查有成绩的学生信息 +select distinct B.* from StudentCourseScore A left join StudentInfo B ON A.StudentId=B.Id + + +-- 5.查询「李」姓老师的数量 +select * from Teachers where TeacherName like '%李%' + +-- 6.查询学过「张三」老师授课的同学的信息 +select * from Teachers where TeacherName='张三' + +select * from CourseInfo where TeacherId=(select id from Teachers where TeacherName='张三') + +select * from StudentCourseScore A inner join StudentInfo B on A.StudentId=B.Id where CourseId +in (select Id from CourseInfo where TeacherId=(select id from Teachers where TeacherName='张三') +) + +-- 7.查询没有学全所有课程的同学的信息 ? +select * from CourseInfo +select * from StudentCourseScore where CourseId in (select id from CourseInfo) + + +select * from StudentCourseScore + +select * from StudentInfo where id not in(select StudentId from StudentCourseScore where CourseId in +(select id from CourseInfo)) + +select A. StudentId 学生编号,count(CourseId) 所学课程数 from StudentCourseScore A +inner join StudentInfo B on A.StudentId=B.Id + group by StudentId having count(CourseId)>3 + +select StudentId 学生编号,count(CourseId) 所学课程数 from StudentCourseScore A +group by StudentId having count(CourseId)>3 + +select * from +(select * from StudentInfo where id not in(select StudentId from StudentCourseScore where CourseId in +(select id from CourseInfo)))A LEFT join +(select StudentId ,count(CourseId) 所学课程数 from StudentCourseScore +group by StudentId having count(CourseId)>3 ) B on A.Id=B.StudentId +-- 8.查询至少有 一门课与学号为" 01 " 的同学所学相同的 同学的信息 ? +select* from StudentInfo where id=1 + +select distinct A. * from StudentInfo A +right join StudentCourseScore B on A.Id=B.StudentId +where CourseId in(select id from CourseInfo) + + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 ? +select * from StudentCourseScore where CourseId in() + +select DISTINCT A. * from StudentInfo A, StudentCourseScore B,CourseInfo C +where C.Id in(select id from CourseInfo) + + + + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 ??? +select *from CourseInfo +select * from Teachers +select * from StudentCourseScore + +select * from StudentInfo where not in +(select StudentId from StudentCourseScore A inner join StudentInfo B on A.StudentId=B.Id where CourseId +in (select Id from CourseInfo where TeacherId=(select id from Teachers where TeacherName='张三') +)) + + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ + + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9d8dfb77df1735ffb6aead85b9bf39d97646e088 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" @@ -0,0 +1,43 @@ +use master +go + +create database GoodsDB +go + +use GoodsDB +go +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) + +insert into GoodsType +values ('服装内衣'),('鞋包配饰'),('手机数码') +select* from GoodsType + +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +values ('提花小西装',' 红色',' 菲曼琪', 300, 1),('百搭短裤',' 绿色',' 哥弟', 100 ,1),('无袖背心',' 白色 ','阿依莲' ,700, 1), +('低帮休闲鞋',' 红色',' 菲曼琪',900, 2),('中跟单鞋','绿色',' 哥弟', 400 ,2),('平底鞋',' 白色',' 阿依莲', 200, 2), +('迷你照相机',' 红色',' 尼康', 500 ,3),('硬盘',' 黑色', '希捷' ,600 ,3),('显卡',' 黑色',' 技嘉', 800, 3) +select * from GoodsInfo + +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select TOP 1 GoodsMoney 商品价格,GoodsName 商品名称,GoodsColor 商品颜色 from GoodsInfo + group by GoodsName,GoodsMoney,GoodsColor order by GoodsMoney DESC + +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodsMoney) 商品最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID + +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery--\345\244\215\344\271\240\351\242\230.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery--\345\244\215\344\271\240\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..75145f4cc420a4a47da2efede2e24c6509d28792 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery--\345\244\215\344\271\240\351\242\230.sql" @@ -0,0 +1,59 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\SQL作业\SQL作业12--复习题\GoodsDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='GoodsDB_log', + filename='D:\SQL作业\SQL作业12--复习题\GoodsDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +go + +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int constraint FK_GoodsInfo_TypeID references GoodsType(TypeID) +) +go + +insert into GoodsInfo values('提花小西装','红色','菲曼琪','300','1'),('百搭短裤','绿色','哥弟','100','1'), +('无袖背心','白色','阿依莲','700','1'),('低帮休闲鞋','红色','菲曼琪','900','2'),('中跟单鞋','绿色','哥弟','400','2'), +('平底鞋','白色','阿依莲','200','2'),('迷你照相机','红色','尼康','500','3'),('硬盘','黑色','希捷','600','3'),('显卡','黑色','技嘉','800','3') + +select * from GoodsType +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1346b846a6bcba0c070660e7832ee3745ade09de --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery1.sql" @@ -0,0 +1,34 @@ +create database GoodsDB +go +use GoodsDB +go + +--商品类型表 +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null, +) +insert into GoodsType(TypeName) values('服装内衣'),('鞋包配饰'),('手机数码') + + +--商品信息表 +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID), +) +insert into GoodsInfo values('提花小西装','红色','菲曼琪','300',1),('百搭短裤', '绿色', '哥弟', '100', 1),('无袖背心' ,'白色' ,'阿依莲' ,'700' ,1), +('低帮休闲鞋',' 红色 ','菲曼琪',' 900', 2),('中跟单鞋',' 绿色',' 哥弟' ,'400', 2),('平底鞋',' 白色',' 阿依莲',' 200', 2),('迷你照相机', '红色',' 尼康', '500', 3), +('硬盘',' 黑色', '希捷', '600', 3),('显卡' ,'黑色',' 技嘉', '800', 3) +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,goodscolor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' or GoodsMoney='300' and GoodsMoney='600' \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..09f65f4ce38763ddaaca16c750b85e905886d4c5 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery1.sql" @@ -0,0 +1,94 @@ +--1、创建商品数据库(GoodsDB) +use master +go + +create database GoodsDB +on +( + name='GoodsDB', + filename='E:\数据库\GoodsDB.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) + +log on +( + name='GoodsDB_log', + filename='E:\数据库\GoodsDB_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) + +use GoodsDB +go +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) + +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor商品颜色 nvarchar 20 否 +--GoodsBrand商品品牌 nvarchar 20 +--GoodsMoney商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 +select * from GoodsType +insert into GoodsType values ('服装内衣'),('鞋包配饰'),('手机数码') + +--商品信息表(GoodsInfo) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 +select * from GoodsInfo +insert into GoodsInfo values +('提花小西装','红色','菲曼琪','300','1'), +('百搭短裤','绿色','哥弟','100','1'), +('无袖背心','白色','阿依莲','700','1'), +('低帮休闲鞋','红色','菲曼琪','900','2'), +('中跟单鞋','绿色','哥弟','400','2'), +('平底鞋','白色','阿依莲','200','2'), +('迷你照相机','红色','尼康','500','3'), +('硬盘','黑色','希捷','600','3'), +('显卡','黑色','技嘉','800','3') + + +--3、查询 价格最贵的 商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc + +--4、按 商品类型编号 分组查询商品 最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型编号 ,MAX(GoodsMoney)最高价格,MIN(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" new file mode 100644 index 0000000000000000000000000000000000000000..69d734058812acc14051d665bb8e74879e8374ab --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" @@ -0,0 +1,100 @@ +use master +go + +create database GoodsDB +on primary +( +name='GoodsDB', +filename='D:\.mdf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) + +log on +( +name='GoodsDB_log', +filename='D:\_log.ldf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +go + +use GoodsDB + +----字段名 说明 类型 长度 可否为空 约束 +----TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +----TypeName 商品类型名称 nvarchar 20 否 + +create table GoodsType --建立商品类型表 +( +TypeID int primary key identity not null, +TypeName nvarchar(20) not null +) +go + +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +create table GoodsInfo --建立商品信息表 +( +GoodsID int primary key identity not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int foreign key (TypeID) references GoodsType(TypeID) +) +go + +select * from GoodsType + +insert into GoodsType(TypeName) values +('服饰内衣'), +('鞋包配饰'), +('手机数码') + +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 + +select * from GoodsInfo + +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) + +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select top 1 s1.GoodsName 商品名称,s1.GoodsColor 商品颜色,s1.GoodsMoney 商品价格 from GoodsInfo s1 order by s1.GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select s2.TypeID,count(s2.TypeID)数量,max(s2.GoodsMoney) 最高价格,min(s2.GoodsMoney) 最低价格,avg(s2.GoodsMoney) 平均价格 from GoodsInfo s2 group by s2.TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from(select top 6 s1.GoodsName,s1.GoodsColor,s1.GoodsMoney from GoodsInfo s1 order by s1.GoodsMoney) s2 where s2.GoodsColor='红色' diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4466dd164c30a196a1267395a47a1e6ba485ca65 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" @@ -0,0 +1,65 @@ +USE master +GO +CREATE DATABASE GoodsDB +on +( +name='wq', +filename='D:\wq.mdf', +size=1 +) +log on +( +name='wq_log', +filename='D:\wq_log.ldf', +size=1 +) +GO +use GoodsDB +GO +CREATE TABLE GoodsType +( +TypeID INT NOT NULL IDENTITY(1,1), +TypeName NVARCHAR(20) NOT NULL +) +GO +ALTER TABLE GoodsType add constraint DK_GoodsType_TYPEID PRIMARY KEY(TYPEID) +SELECT * FROM GoodsType +INSERT INTO GoodsType(TypeName) +values('服装内衣'), +('鞋包配饰'), +('手机数码') +CREATE TABLE GoodsInfo +( +GoodsID INT NOT NULL IDENTITY(1,1), +GoodsName NVARCHAR(20) NOT NULL, +GoodsColor NVARCHAR(20) NOT NULL , +GoodsBrand NVARCHAR(20) , +GoodsMoney MONEY NOT NULL, +TypeID INT +) +GO +ALTER TABLE GoodsInfo ADD CONSTRAINT GK_GoodsInfo_GoodsID primary key(GoodsID) +ALTER TABLE GoodsInfo ADD CONSTRAINT JK_GoodsInfo_TypeID FOREIGN KEY(TYPEID) references GoodsType(TypeID) +SELECT * FROM GoodsInfo +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +values ('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100 ,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +SELECT top 1 GoodsName 名称,GoodsColor 颜色,max(GoodsMoney) 价格 FROM GoodsInfo group by GoodsName,GoodsColor order by max(GoodsMoney) desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select TypeID 编号,MAX(GoodsMoney)最高,min(GoodsMoney)最低,avg(GoodsMoney)平均 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4466dd164c30a196a1267395a47a1e6ba485ca65 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery2.sql" @@ -0,0 +1,65 @@ +USE master +GO +CREATE DATABASE GoodsDB +on +( +name='wq', +filename='D:\wq.mdf', +size=1 +) +log on +( +name='wq_log', +filename='D:\wq_log.ldf', +size=1 +) +GO +use GoodsDB +GO +CREATE TABLE GoodsType +( +TypeID INT NOT NULL IDENTITY(1,1), +TypeName NVARCHAR(20) NOT NULL +) +GO +ALTER TABLE GoodsType add constraint DK_GoodsType_TYPEID PRIMARY KEY(TYPEID) +SELECT * FROM GoodsType +INSERT INTO GoodsType(TypeName) +values('服装内衣'), +('鞋包配饰'), +('手机数码') +CREATE TABLE GoodsInfo +( +GoodsID INT NOT NULL IDENTITY(1,1), +GoodsName NVARCHAR(20) NOT NULL, +GoodsColor NVARCHAR(20) NOT NULL , +GoodsBrand NVARCHAR(20) , +GoodsMoney MONEY NOT NULL, +TypeID INT +) +GO +ALTER TABLE GoodsInfo ADD CONSTRAINT GK_GoodsInfo_GoodsID primary key(GoodsID) +ALTER TABLE GoodsInfo ADD CONSTRAINT JK_GoodsInfo_TypeID FOREIGN KEY(TYPEID) references GoodsType(TypeID) +SELECT * FROM GoodsInfo +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +values ('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100 ,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +SELECT top 1 GoodsName 名称,GoodsColor 颜色,max(GoodsMoney) 价格 FROM GoodsInfo group by GoodsName,GoodsColor order by max(GoodsMoney) desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select TypeID 编号,MAX(GoodsMoney)最高,min(GoodsMoney)最低,avg(GoodsMoney)平均 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9004746cd47668cb25e91bf5ce7d1a00eb2b3a39 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" @@ -0,0 +1,43 @@ +use master +go +create database GoodsDB +go +use GoodsDB +go +create table GoodsType +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null +) +create table GoodsInfo +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20) not null, +GoodsMoney money not null, +TypeID int foreign key references GoodsType(TypeID) +) +insert into GoodsType +select '服装内衣' union +select '鞋包配饰' union +select '手机数码' +select * from GoodsType +insert into GoodsInfo +select '提花小西装','红色','菲',300,1 union +select '百搭短裤','绿色','哥弟',100,1 union +select '无袖背心','白色','白色',700,1 union +select '低帮休闲鞋','红色','菲曼琪',900,2 union +select '中跟单鞋','绿色','哥弟',400,2 union +select '平底鞋','白色','阿依莲',200,2 union +select '迷你照相机','红色','尼康',500,3 union +select '硬盘','黑色','希捷',600,3 union +select '显卡','黑色','技嘉',800,3 +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 名称, max(GoodsMoney) 价格,GoodsColor 颜色 from GoodsInfo group by GoodsName ,GoodsColor, GoodsMoney order by GoodsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格, avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor = '红色' and GoodsMoney between 300 and 600 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..06ef56725261b216537a4c78b31c264205e0ee9a --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery1.sql" @@ -0,0 +1,44 @@ +use master +go +create database GoodsDB +go +use GoodsDB +go + +--商品类型表 +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null, +) +go +use GoodsDB +go +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID), +) +go +insert into GoodsType(TypeName) values('服装内衣'),('鞋包配饰'),('手机数码') + +insert into GoodsInfo values('提花小西装','红色','菲曼琪','300',1),('百搭短裤', '绿色', '哥弟', '100', 1),('无袖背心' ,'白色' ,'阿依莲' ,'700' ,1), +('低帮休闲鞋',' 红色 ','菲曼琪',' 900', 2),('中跟单鞋',' 绿色',' 哥弟' ,'400', 2),('平底鞋',' 白色',' 阿依莲',' 200', 2),('迷你照相机', '红色',' 尼康', '500', 3), +('硬盘',' 黑色', '希捷', '600', 3),('显卡' ,'黑色',' 技嘉', '800', 3) + + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,goodscolor 商品颜色,GoodsMoney 商品价格 +from GoodsInfo order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价格 +from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo +where GoodsColor='红色' or GoodsMoney='300' and GoodsMoney='600' \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/sql.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/sql.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ffbec086d8279930972eb215223cdb61e1f9c219 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/sql.sql" @@ -0,0 +1,62 @@ +use master +go +create database GoodsDB +on +( name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +go +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity, + TypeName nvarchar(20) not null + +) + +create table GoodsInfo +( + GoodsID int primary key identity, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null, + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +insert into GoodsInfo values('提花小西装', '红色', '菲曼琪', '300', 1),('百搭短裤', '绿色', '哥弟', '100', 1), + ('无袖背心', '白色', '阿依莲', '700', 1),('低帮休闲鞋', '红色', '菲曼琪', '900', 2), + ('中跟单鞋', '绿色', '哥弟', '400', 2),('平底鞋', '白色', '阿依莲','200', 2), + ('迷你照相机', '红色', '尼康', '500', 3),('硬盘', '黑色', '希捷', '600', 3), + ('显卡', '黑色', '技嘉','800', 3) +select*from GoodsType +select*from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + select GoodsName 商品名称, GoodsColor 商品颜色, GoodsMoney 商品价格 + from GoodsInfo + where GoodsMoney=(select max(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格, avg(GoodsMoney) 平均价格 + from GoodsInfo + join GoodsType + on GoodsType.TypeID=GoodsInfo.TypeID + group by TypeName + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and (GoodsMoney between 300 and 600) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c2dd0f666979029f12b6bfadb44d781132163437 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" @@ -0,0 +1,56 @@ +drop database GoodsDB +create database GoodsDB +on +( + name='GoodsDB', + filename='F:\GoodsDB.mdf' +) +log on +( + name='GoodsDB_log', + filename='F:\GoodsDB_log.mdf' +) +use GoodsDB +go +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20)not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) +insert into GoodsType +select '服装内衣' union +select '鞋包配饰' union +select '手机数码' +select * from GoodsType +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +select '提花小西装', '红色', '菲曼琪', '300', 1 union +select '百搭短裤', '绿色', '哥弟', '100 ', 1 union +select '无袖背心', '白色', '阿依莲', '700 ', 1 union +select '低帮休闲鞋', '红色', '菲曼琪', '900', 2 union +select '中跟单鞋', '绿色', '哥弟', '400', 2 union +select '平底鞋', '白色', '阿依莲', '200 ', 2 union +select '迷你照相机', '红色', '尼康', '500', 3 union +select '硬盘', '黑色', '希捷 ', '600 ', 3 union +select '显卡 ', '黑色', '技嘉', '800 ', 3 +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select GoodsName 商品名称,GoodsColor 商品颜色 ,GoodsMoney 商品价格 from GoodsInfo +where GoodsMoney in (select max(GoodsMoney) from GoodsInfo) +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 +from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + +select * from GoodsType +select * from GoodsInfo \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c87ad3357819d9e6331389562b551466a94d4292 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" @@ -0,0 +1,67 @@ +create database GoodsDB + +use GoodsDB +go + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null , + GoodsColor nvarchar(20) not null , + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) + +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') + + +insert into GoodsInfo values +('提花小西装','红色','菲曼琪','300',1), +('百搭短裤','绿色','哥弟','100',1), +('无袖背心','白色','阿依莲','700',1), +('低帮休闲鞋','红色','菲曼琪','900',2), +('中跟单鞋','绿色','哥弟','400',2), +('平底鞋','白色','阿依莲','200',2), +('迷你照相机','红色','尼康','500',3), +('硬盘','黑色','希捷','600',3), +('显卡','黑色','技嘉','800',3) + + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney in (select max(GoodsMoney) from GoodsInfo) +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型编号,Max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between '300' and '600' + + + + + + + + + + + + + + + + + + + + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2301.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2301.sql" new file mode 100644 index 0000000000000000000000000000000000000000..054115dac0baea13e7d1fc144f584e1f0fee4500 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2301.sql" @@ -0,0 +1,45 @@ +use master +go +create database GoodsDB +go + +use GoodsDB + +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +go + +use GoodsDB + +go + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +insert into GoodsInfo values('提花小西装','红色','菲曼琪','300',1),('百搭短裤','绿色','哥弟','100',1),('无袖背心','白色','阿依莲','700',1), +('低帮休闲鞋','红色','菲曼琪','900',2),('中跟单鞋','绿色','哥弟','400',2),('平底鞋','白色','阿依莲','200',2),('迷你照相机','红色','尼康','500',3), +('硬盘','黑色','希捷','600',3),('显卡','黑色','技嘉','800',3) + + +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney = (select MAX(GoodsMoney) from GoodsInfo) +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID,MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor = '红色'and GoodsMoney >= 300 and GoodsMoney <=600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fa5e44d0b04aaa5873cfa04bbd23fa251871e130 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" @@ -0,0 +1,62 @@ +use master +go +create database GoodsDB +on +( name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +go +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity, + TypeName nvarchar(20) not null + +) + +create table GoodsInfo +( + GoodsID int primary key identity, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null, + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +insert into GoodsInfo values('提花小西装', '红色', '菲曼琪', '300', 1),('百搭短裤', '绿色', '哥弟', '100', 1), + ('无袖背心', '白色', '阿依莲', '700', 1),('低帮休闲鞋', '红色', '菲曼琪', '900', 2), + ('中跟单鞋', '绿色', '哥弟', '400', 2),('平底鞋', '白色', '阿依莲','200', 2), + ('迷你照相机', '红色', '尼康', '500', 3),('硬盘', '黑色', '希捷', '600', 3), + ('显卡', '黑色', '技嘉','800', 3) +select*from GoodsType +select*from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + select GoodsName 商品名称, GoodsColor 商品颜色, GoodsMoney 商品价格 + from GoodsInfo + where GoodsMoney=(select max(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格, avg(GoodsMoney) 平均价格 + from GoodsInfo + join GoodsType + on GoodsType.TypeID=GoodsInfo.TypeID + group by TypeName + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and (GoodsMoney between 300 and 600) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2bd05e89cf42c07264fdf55cce3e34b071f7ee5b --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" @@ -0,0 +1,58 @@ +create database GoodsDB +on +( +name='GoodsDB_data', +filename='C:\app\GoodsDB_data.mdf', +size=10mb, +maxsize=100mb, +filegrowth=10% +) +log on +( +name='GoodsDB_log', +filename='C:\app\GoodsDB_log.ldf', +size=10mb, +filegrowth=1mb +) +USE GoodsDB +go +create table GoodsType +( +TypeID int primary key IDENTITY(1,1) not null, +TypeName nvarchar(20) +) +insert GoodsType(TypeID,TypeName) +select 1,'服装内衣' union +select 2,'鞋包配饰' union +select 3,'手机数码' + +go +create table GoodsInfo +( +GoodsID int primary key IDENTITY(1,1) not null, +GoodsName nvarchar(20), +GoodsColor nvarchar(20), +GoodsBrand nvarchar(20), +GoodsMoney money, +TypeID int foreign key references GoodsType(TypeID) +) +insert GoodsInfo(GoodsID,GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +select 1,'提花小西装','红色','菲曼琪',300,1 union +select 2,'百搭短裤','绿色','哥弟',100,1 union +select 3,'无袖背心','白色','阿依莲',700,1 union +select 4,'低帮休闲鞋','红色','菲曼琪',900,2 union +select 5,'中跟单鞋','绿色','哥弟',400,2 union +select 6,'平底鞋','白色','阿依莲',200,2 union +select 7,'迷你照相机','红色','尼康',500,3 union +select 8,'硬盘','黑色','希捷',600,3 union +select 9,'显卡','黑色','技嘉',800,3 + +Select * from GoodsType +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName,GoodsColor,GoodsMoney from GoodsInfo where GoodsMoney=(select max(GoodsMoney) from GoodsInfo ) +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select max(GoodsMoney),min(GoodsMoney),avg(GoodsMoney) from GoodsInfo inner join GoodsType on GoodsInfo.TypeID=GoodsType.TypeID group by TypeName +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +Select GoodsID,GoodsColor='红色',GoodsMoney from GoodsInfo where GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e9c7b0ab4f9cd9e7c6829bc9f377743a66c544b4 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery3.sql" @@ -0,0 +1,70 @@ +----1、创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表), +use master +go +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=10, + maxsize=50, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=10, + maxsize=50, + filegrowth=10% +) +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') + +insert into GoodsInfo values +('提花小西装','红色','菲曼琪','300',1), +('百搭短裤','绿色','哥弟','100',1), +('无袖背心','白色','阿依莲','700',1), +('低帮休闲鞋','红色','菲曼琪','900',2), +('中跟单鞋', '绿色', '哥弟', '400', 2), +('平底鞋', '白色', '阿依莲', '200', 2), +('迷你照相机','红色','尼康','500',3), +('硬盘','黑色','希捷','600',3), +('显卡','黑色','技嘉','800',3) + + +select * from GoodsType +select * from GoodsInfo + +--3、查询 价格最贵的商品名称 ,商品颜色 和 商品价格,要求 使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类别,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 +from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f4f9a30bdf4ca9500d5c5557cdbce1e979aaa607 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" @@ -0,0 +1,49 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null, + +) +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null, + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +insert into GoodsInfo values('提花小西装','红色','菲曼琪' ,'300', 1),('百搭短裤','绿色','哥弟' ,'100', 1), +('无袖背心', '白色', '阿依莲', '700', 1),('低帮休闲鞋', '红色',' 菲曼琪',' 900', 2),('中跟单鞋','绿色','哥弟','400', 2), +('平底鞋','白色','阿依莲','200', 2),('迷你照相机','红色','尼康','500',3),('硬盘','黑色','希捷', '600', 3), +('显卡','黑色','技嘉','800',3) +select * from GoodsInfo +select * from GoodsType +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney in(select max(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型编号,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select *from GoodsInfo where GoodsColor='红色'and (GoodsMoney between 300 and 600) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..22c624720f4b3c537c1c7fe62255daf3f01e924a --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery1.sql" @@ -0,0 +1,61 @@ +create database GoodsDB +on +( +name='GoodsDB', +filename='D:\GoodsDB.mdf', +size=500mb, +filegrowth=15%, +maxsize=15mb +) +log on +( +name='GoodsDB_log', +filename='D:\GoodsDB_log.ldf', +size=500mb, +filegrowth=15%, +maxsize=15mb +) +go +use GoodsDB +go + +create table GoodsType +( +TypeID int primary key not null, +TypeName nvarchar(20) not null +) + +create table GoodsInfo +( +GoodsID int not null primary key , +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int foreign key references GoodsType(TypeID) +) + +select * from GoodsType +select * from GoodsInfo +insert into GoodsType values (1,'服装内衣'),(2,'鞋包配饰'),(3,'手机数码') +drop table GoodsInfo +insert into GoodsInfo values +(1,'提花小西装','红色','菲曼琪 ','300 ',1), +(2,'百搭短裤','绿色','哥弟','100',1), +(3,'无袖背心','白色','阿依莲','700',1), +(4,'低帮休闲鞋','红色','菲曼琪','900',2), +(5,'中跟单鞋','绿色','哥弟','400',2), +(6,'平底鞋','白色','阿依莲','200',2), +(7,'迷你照相机','红色','尼康','500',3), +(8,'硬盘','黑色','希捷','500',3), +(9,'显卡','黑色','技嘉','800',3) + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 max(GoodsMoney)价格最贵,GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo +group by GoodsMoney,GoodsName,GoodsColor order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo +group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select GoodsColor,GoodsMoney from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..90775fd27622cb8d3ac238519d29153d5d82377c --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" @@ -0,0 +1,63 @@ +use master +go + +create database GoodsDB +on +( + name='GoodsDB', + filename='C:\学习\数据库\GoodsDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='C:\学习\数据库\GoodsDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') + +insert into GoodsInfo values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +select * from GoodsInfo +select * from GoodsType +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney in (select max(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号 分组查询 商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型编号,max(GoodsMoney) 商品最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney)平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c229e084b4ab61e95f3d36a7340a65c2feb16bad --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery2.sql" @@ -0,0 +1,85 @@ +create database GOOdSDB + + + +on + +( + + name='GOOdSDB', + + filename='E:\GOOdSDB.mdf', + + size=5mb, + + maxsize=50mb, + + filegrowth=10% + +) + +log on + +( + + name='GOOdSDB_log', + + filename='E:\GOOdSDB_log.ldf', + + size=5mb, + + maxsize=50mb, + + filegrowth=10% + +) + +go + +use GOOdSDB + +create table GoodsType --商品类型表 +( +Typeid int primary key not null, +TypeName nvarchar(20) not null, +) +drop table GoodsInfo +create table GoodsInfo --商品信息表 +( +Goodsid int primary key not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +Typeid int foreign key references GoodsType(Typeid) +) +insert into GoodsType +select 1,'服装内衣' union +select 2,'鞋包配饰' union +select 3,'手机数码' + +select * from GoodsType + +insert into GoodsInfo +select 1, '提花小西装', '红色', '菲曼琪', 300 ,1union +select 2, '百搭短裤', '绿色', '哥弟', 100, 1 union +select 3,'无袖背心', '白色', '阿依莲',700, 1 union +select 4,'低帮休闲鞋', '红色', '菲曼琪', 900, 2 union +select 5,'中跟单鞋 ', '绿色', '哥弟', 400, 2 union +select 6, '平底鞋 ', '白色', '阿依莲', 200, 2 union +select 7, '迷你照相机', '红色', '尼康', 500, 3 union +select 8,'硬盘', '黑色', '希捷', 600, 3 union +select 9,'显卡', '黑色', '技嘉' ,800, 3 + +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 颜色, max(GoodsMoney) 价格 from GoodsInfo group by GoodsName,GoodsColor,GoodsMoney order by GoodsMoney desc + + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select Typeid 商品类型编号, max(GoodsMoney)最高价格,min(GoodsMoney)最低价格, avg (GoodsMoney)平均价格 from GoodsInfo group by Typeid + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select*from GoodsInfo where GoodsMoney between 300 and 600 and GoodsColor='红色' + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..381a66dd03ed41895178359c34e749cef9e4c279 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" @@ -0,0 +1,53 @@ +create database GoodsDB +on +( + name = 'GoodsDB', + filename = 'D:\数据库\GoodsDB.mdf' +) +log on +( + name = 'GoodsDB_log', + filename = 'D:\数据库\GoodsDB_log.ldf' +) +use GoodsDB + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null , + TypeID int references GoodsType(TypeID) +) +insert into GoodsType values +('服装内衣'),('鞋包配饰'),('手机数码') +insert into GoodsInfo values +('提米小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋 ','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','希捷',800,3) +select * from GoodsType +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 名称,GoodsColor 颜色 ,GoodsMoney 价格 from GoodsInfo +where GoodsMoney in (select max(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TYPEID 编号,MIN(GoodsMoney)最低价, Max(GoodsMoney)最高价,avg(GoodsMoney)平均价 from GoodsInfo +group by TYPEID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor = '红色' and GoodsMoney between 300 and 600 + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1a772e6897a1dc406cb00c89a25eca0a85e3030f --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" @@ -0,0 +1,47 @@ +use master +go + +create database GoodsDB +on +( name='GoodsDB', + filename='D:\SQL\GoodsDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=5mb +) +log on +( name='GoodsDB_log', + filename='D:\SQL\GoodsDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=5mb +) +use GoodsDB +go +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int constraint FK_GoodsInfo_TypeID references GoodsType(TypeID) +) +insert into GoodsType values ('服装内衣'),('鞋包数码'),('手机数码') +insert into GoodsInfo values('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟 ',400,2), +('平底鞋','白色','阿依莲',200,2),('迷你照相机','红色 ','尼康',500 ,3), +('硬盘','黑色','希捷',600,3),('显卡 ','黑色','技嘉',800,3) +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo +where GoodsMoney = (select MAX(GoodsMoney) from GoodsInfo) +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型,MAX(GoodsMoney)最高价格,MIN(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo group by TypeID +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor = '红色' and GoodsMoney >= 300 and GoodsMoney <= 600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery5.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9598dfcca176868247438e43d245d2afa76750ec --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery5.sql" @@ -0,0 +1,101 @@ +use master +go + +create database GoodsDB +on primary +( +name='GoodsDB', +filename='D:\.mdf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) + +log on +( +name='GoodsDB_log', +filename='D:\_log.ldf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +go + +use GoodsDB + +----字段名 说明 类型 长度 可否为空 约束 +----TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +----TypeName 商品类型名称 nvarchar 20 否 + +create table GoodsType --建立商品类型表 +( +TypeID int primary key identity not null, +TypeName nvarchar(20) not null +) +go + +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +create table GoodsInfo --建立商品信息表 +( +GoodsID int primary key identity not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int foreign key (TypeID) references GoodsType(TypeID) +) +go + +select * from GoodsType + +insert into GoodsType(TypeName) values +('服饰内衣'), +('鞋包配饰'), +('手机数码') + +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 + +select * from GoodsInfo + +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) + +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select top 1 s1.GoodsName 商品名称,s1.GoodsColor 商品颜色,s1.GoodsMoney 商品价格 from GoodsInfo s1 order by s1.GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select s2.TypeID,count(s2.TypeID)数量,max(s2.GoodsMoney) 最高价格,min(s2.GoodsMoney) 最低价格,avg(s2.GoodsMoney) 平均价格 from GoodsInfo s2 group by s2.TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from(select top 6 s1.GoodsName,s1.GoodsColor,s1.GoodsMoney from GoodsInfo s1 order by s1.GoodsMoney) s2 where s2.GoodsColor='红色' + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..72cf4dce9925889b51eaf7a0ca092c3e5293c011 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" @@ -0,0 +1,48 @@ +create database GoodsDB + +use GoodsDB +go + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null , + GoodsColor nvarchar(20) not null , + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) + +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') + + +insert into GoodsInfo values +('提花小西装','红色','菲曼琪','300',1), +('百搭短裤','绿色','哥弟','100',1), +('无袖背心','白色','阿依莲','700',1), +('低帮休闲鞋','红色','菲曼琪','900',2), +('中跟单鞋','绿色','哥弟','400',2), +('平底鞋','白色','阿依莲','200',2), +('迷你照相机','红色','尼康','500',3), +('硬盘','黑色','希捷','600',3), +('显卡','黑色','技嘉','800',3) + +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsMoney 商品价格,GoodsName 商品名称,GoodsColor 商品颜色 from GoodsInfo group by GoodsName,GoodsColor,GoodsMoney order by GoodsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodsMoney) 最高价格,min(GoodsMoney ) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor ='红色'and GoodsMoney>300 and GoodsMoney<600 + + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e1ff12d72ee6ff08717d0a9a2008bcce40d98cbe --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" @@ -0,0 +1,73 @@ +create database Jxc + +on + +( +name='Jxc', +filename='D:\Jxc.mdf', +size=5, +maxsize=100, +filegrowth=10% +) +log on + +( +name='Jxc_log', +filename='D:\Jxc_log.ldf', +size=5, +maxsize=100, +filegrowth=10% +) + +use Jxc + +create table GoodsType +( +TypeID int not null primary key identity(1,1), +TypeName nvarchar(20) not null +) + +create table GoodInfo + + ( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null , + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null , + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) + ) + + insert into GoodsType + + select '服装内衣' union + select '鞋包配饰' union + select '手机数码' + + insert into GoodInfo + + select '提花小西装','红色','菲曼琪',300 ,1 union + select '百搭短裤','绿色','哥弟',100,1 union + select '无袖背心','白色',' 阿依莲',700,1 union + select '低帮休闲鞋',' 红色','菲曼琪',900,2 union + select ' 中跟单鞋','绿色','哥弟',400,2 union + select '平底鞋','白色',' 阿依莲',200,2 union + select '迷你照相机 ','红色','尼康 ',500,3 union + select ' 硬盘','黑色','希捷',600,3 union + select ' 显卡','黑色','技嘉',800,3 + + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + select GoodsName 商品名称 ,GoodsColor 商品颜色 ,GoodsMoney 商品价格 from GoodInfo + where GoodsMoney in (select max(GoodsMoney) from GoodInfo) + +--4、按商品类型编号分组 查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select TypeID 类型编号, max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodInfo +group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fa916cc8122969084d6c97253084474c63cc1ddc --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" @@ -0,0 +1,59 @@ +create database GoodsDB +on +( + name='E:\GoodsDB', + filename='E:\GoodsDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log +on +( + name='E:\GoodsDB_log', + filename='E:\GoodsDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +use GoodsDB + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) + +insert into GoodsType values +('服饰内衣'), +('鞋包配饰'), +('手机数码') +insert into GoodsInfo values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +select * from GoodsType +select * from GoodsInfo + +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 + from GoodsInfo order by GoodsMoney desc + +select TypeID 编号,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 + from GoodsInfo group by TypeID + + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney<=600 and GoodsMoney>=300 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a41c8a58aa58cbfe44a775bc3e28b1779b2596d8 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" @@ -0,0 +1,48 @@ +create database GoodsDB +on +( name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=5mb, + maxsize=100mb, + filegrowth=5mb +) +log on +( name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=5mb +) +go +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity, + TypeName nvarchar(20) not null + +) + +create table GoodsInfo +( + GoodsID int primary key identity, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null, + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +insert into GoodsInfo values('提花小西装', '红色', '菲曼琪', '300', 1),('百搭短裤', '绿色', '哥弟', '100', 1), + ('无袖背心', '白色', '阿依莲', '700', 1),('低帮休闲鞋', '红色', '菲曼琪', '900', 2), + ('中跟单鞋', '绿色', '哥弟', '400', 2),('平底鞋', '白色', '阿依莲','200', 2), + ('迷你照相机', '红色', '尼康', '500', 3),('硬盘', '黑色', '希捷', '600', 3), + ('显卡', '黑色', '技嘉','800', 3) +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc +select max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4466dd164c30a196a1267395a47a1e6ba485ca65 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" @@ -0,0 +1,65 @@ +USE master +GO +CREATE DATABASE GoodsDB +on +( +name='wq', +filename='D:\wq.mdf', +size=1 +) +log on +( +name='wq_log', +filename='D:\wq_log.ldf', +size=1 +) +GO +use GoodsDB +GO +CREATE TABLE GoodsType +( +TypeID INT NOT NULL IDENTITY(1,1), +TypeName NVARCHAR(20) NOT NULL +) +GO +ALTER TABLE GoodsType add constraint DK_GoodsType_TYPEID PRIMARY KEY(TYPEID) +SELECT * FROM GoodsType +INSERT INTO GoodsType(TypeName) +values('服装内衣'), +('鞋包配饰'), +('手机数码') +CREATE TABLE GoodsInfo +( +GoodsID INT NOT NULL IDENTITY(1,1), +GoodsName NVARCHAR(20) NOT NULL, +GoodsColor NVARCHAR(20) NOT NULL , +GoodsBrand NVARCHAR(20) , +GoodsMoney MONEY NOT NULL, +TypeID INT +) +GO +ALTER TABLE GoodsInfo ADD CONSTRAINT GK_GoodsInfo_GoodsID primary key(GoodsID) +ALTER TABLE GoodsInfo ADD CONSTRAINT JK_GoodsInfo_TypeID FOREIGN KEY(TYPEID) references GoodsType(TypeID) +SELECT * FROM GoodsInfo +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +values ('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100 ,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +SELECT top 1 GoodsName 名称,GoodsColor 颜色,max(GoodsMoney) 价格 FROM GoodsInfo group by GoodsName,GoodsColor order by max(GoodsMoney) desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select TypeID 编号,MAX(GoodsMoney)最高,min(GoodsMoney)最低,avg(GoodsMoney)平均 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0ec655b6c46c5e60afed9a3a2ec6c135777d007b --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" @@ -0,0 +1,64 @@ +use master +go +create database GoodsDB +on( + name='GoodsDB', + filename='C:\TEXT\GoodsDB.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on ( + name='GoodsDB_log', + filename='C:\TEXT\GoodsDB_log.ldf', + size=5, + maxsize=100, + filegrowth=10% +) +go +use GoodsDB +go +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null, +) +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int references GoodsType(TypeID), +) + select * from GoodsType + select * from GoodsInfo + insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + insert into GoodsInfo values('提花小西装','红色','菲曼琪',300.00,1),('百搭短裤','绿色','哥弟',100.00,1), + ('无袖背心','白色','阿依莲',700.00,1),('低帮休闲鞋','红色','菲曼琪',900.00,2), + ('中跟单鞋', '绿色', '哥弟', 400.00, 2),('平底鞋', '白色', '阿依莲', 200, 2), + ('迷你照相机','红色','尼康',500.00,3),('硬盘','黑色','希捷',600.00,3), + ('显卡','黑色','技嘉',800.00,3) + +--3、查询 价格最贵 的商品名称,商品颜色和商品价格,要求使用别名显示列名 + select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品金额 from GoodsInfo order by GoodsMoney DESC + select top 1 GoodsName as 商品名称,GoodsColor as 商品颜色,GoodsMoney as 商品金额 from GoodsInfo order by GoodsMoney DESC + select top 1 商品名称=GoodsName , 商品颜色=GoodsColor , 商品金额=GoodsMoney from GoodsInfo order by GoodsMoney DESC +--4、按商品 类型编号 分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + select TypeID 类型编号,MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + + + + + + + + + + + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fab95b187dfe0aeced8ddc56273c484a3af8b47c --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery2.sql" @@ -0,0 +1,52 @@ +use master +go +create database GoodsDB +on( + name='GoodsDB', + filename='C:\TEXT\GoodsDB.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on ( + name='GoodsDB_log', + filename='C:\TEXT\GoodsDB_log.ldf', + size=5, + maxsize=100, + filegrowth=10% +) +go +use GoodsDB +go +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null, +) +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int references GoodsType(TypeID), +) + select * from GoodsType + select * from GoodsInfo + insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + insert into GoodsInfo values('提花小西装','红色','菲曼琪',300.00,1),('百搭短裤','绿色','哥弟',100.00,1), + ('无袖背心','白色','阿依莲',700.00,1),('低帮休闲鞋','红色','菲曼琪',900.00,2), + ('中跟单鞋', '绿色', '哥弟', 400.00, 2),('平底鞋', '白色', '阿依莲', 200, 2), + ('迷你照相机','红色','尼康',500.00,3),('硬盘','黑色','希捷',600.00,3), + ('显卡','黑色','技嘉',800.00,3) + +--3、查询 价格最贵 的商品名称,商品颜色和商品价格,要求使用别名显示列名 + select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品金额 from GoodsInfo order by GoodsMoney DESC + select top 1 GoodsName as 商品名称,GoodsColor as 商品颜色,GoodsMoney as 商品金额 from GoodsInfo order by GoodsMoney DESC + select top 1 商品名称=GoodsName , 商品颜色=GoodsColor , 商品金额=GoodsMoney from GoodsInfo order by GoodsMoney DESC +--4、按商品 类型编号 分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + select TypeID 类型编号,MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/\345\244\215\344\271\240\344\275\234\344\270\232.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/\345\244\215\344\271\240\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..696d82bf899de0275973bde8580da08ff767d964 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/\345\244\215\344\271\240\344\275\234\344\270\232.sql" @@ -0,0 +1,67 @@ +--创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +create table GoodsInfo( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int foreign key(TypeID) references GoodsType(TypeID) +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 +insert GoodsType(TypeName) values +('服装内衣'), +('鞋包配饰'), +('手机数码') + +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 + +--set identity_insert GoodsType ON--打开 +--set identity_insert GoodsInfo ON--打开 + +insert into GoodsInfo values +('提花小西装','红色','菲曼琪','300',1), +('百搭短裤','绿色','哥弟','100',1), +('无袖背心','白色','阿依莲','700',1), +('低帮休闲鞋','红色','菲曼琪','900',2), +('中跟单鞋','绿色','哥弟','400',2), +('平底鞋','白色','阿依莲','200',2), +('迷你照相机','红色','尼康','500',3), +('硬盘','黑色','希捷','600',3), +('显卡','黑色','技嘉','800',3) + +select * from GoodsInfo +----3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsMoney 商品价格,GoodsName 商品名称,GoodsColor 商品颜色 from GoodsInfo order by GoodsMoney desc + +----4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,MAX(GoodsMoney)最高价格,AVG(GoodsMoney)平均价格,MIN(GoodsMoney)最低价格 from GoodsInfo group by typeID + +----5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and (GoodsMoney>300 and GoodsMoney<600) diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..72cf4dce9925889b51eaf7a0ca092c3e5293c011 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" @@ -0,0 +1,48 @@ +create database GoodsDB + +use GoodsDB +go + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null , + GoodsColor nvarchar(20) not null , + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) + +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') + + +insert into GoodsInfo values +('提花小西装','红色','菲曼琪','300',1), +('百搭短裤','绿色','哥弟','100',1), +('无袖背心','白色','阿依莲','700',1), +('低帮休闲鞋','红色','菲曼琪','900',2), +('中跟单鞋','绿色','哥弟','400',2), +('平底鞋','白色','阿依莲','200',2), +('迷你照相机','红色','尼康','500',3), +('硬盘','黑色','希捷','600',3), +('显卡','黑色','技嘉','800',3) + +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsMoney 商品价格,GoodsName 商品名称,GoodsColor 商品颜色 from GoodsInfo group by GoodsName,GoodsColor,GoodsMoney order by GoodsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodsMoney) 最高价格,min(GoodsMoney ) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor ='红色'and GoodsMoney>300 and GoodsMoney<600 + + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0c60954b11a91443341f4a5e82c3fbe77aa1acee --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" @@ -0,0 +1,46 @@ +use master +go + +create database GoodsDB +on +( name='GoodsDB', + filename='D:\SQL\GoodsDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=5mb +) +log on +( name='GoodsDB_log', + filename='D:\SQL\GoodsDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=5mb +) +use GoodsDB +go +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int constraint FK_GoodsInfo_TypeID references GoodsType(TypeID) +) +insert into GoodsType values ('鏈嶈鍐呰。'),('闉嬪寘鏁扮爜'),('鎵嬫満鏁扮爜') +insert into GoodsInfo values('鎻愯姳灏忚タ瑁','绾㈣壊','鑿叉浖鐞',300,1), +('鐧炬惌鐭¥','缁胯壊','鍝ュ紵',100,1),('鏃犺鑳屽績','鐧借壊','闃夸緷鑾',700,1), +('浣庡府浼戦棽闉','绾㈣壊','鑿叉浖鐞',900,2),('涓窡鍗曢瀷','缁胯壊','鍝ュ紵 ',400,2), +('骞冲簳闉','鐧借壊','闃夸緷鑾',200,2),('杩蜂綘鐓х浉鏈','绾㈣壊 ','灏煎悍',500 ,3), +('纭洏','榛戣壊','甯屾嵎',600,3),('鏄惧崱 ','榛戣壊','鎶鍢',800,3) +--鏌ヨ浠锋牸鏈璐电殑鍟嗗搧鍚嶇О锛屽晢鍝侀鑹插拰鍟嗗搧浠锋牸锛岃姹備娇鐢ㄥ埆鍚嶆樉绀哄垪鍚 +select GoodsName 鍟嗗搧鍚嶇О,GoodsColor 鍟嗗搧棰滆壊,GoodsMoney 鍟嗗搧浠锋牸 from GoodsInfo where GoodsMoney = (select MAX(GoodsMoney) from GoodsInfo) +--鎸夊晢鍝佺被鍨嬬紪鍙峰垎缁勬煡璇㈠晢鍝佹渶楂樹环鏍硷紝鏈浣庝环鏍煎拰骞冲潎浠锋牸锛岃姹備娇鐢ㄥ埆鍚嶆樉绀哄垪鍚 +select TypeID 鍟嗗搧绫诲瀷,MAX(GoodsMoney)鏈楂樹环鏍,MIN(GoodsMoney)鏈浣庝环鏍,AVG(GoodsMoney)骞冲潎浠锋牸 from GoodsInfo group by TypeID +--鏌ヨ鍟嗗搧淇℃伅鎵鏈夊垪锛岃姹傚晢鍝侀鑹蹭负绾㈣壊锛屼环鏍煎湪300~600涔嬮棿 +select * from GoodsInfo where GoodsColor = '绾㈣壊' and GoodsMoney >= 300 and GoodsMoney <= 600 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/goodsDB.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/goodsDB.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c5584747b31cb30c99657379dffaaa8e7b93d586 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/goodsDB.sql" @@ -0,0 +1,38 @@ +use master +go + +create database GoodsDB +go + +use GoodsDB +go +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) +insert into GoodsType +values ('服装内衣'),('鞋包配饰'),('手机数码') +select* from GoodsType +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +values ('提花小西装',' 红色',' 菲曼琪', 300, 1),('百搭短裤',' 绿色',' 哥弟', 100 ,1),('无袖背心',' 白色 ','阿依莲' ,700, 1), +('低帮休闲鞋',' 红色',' 菲曼琪',900, 2),('中跟单鞋','绿色',' 哥弟', 400 ,2),('平底鞋',' 白色',' 阿依莲', 200, 2), +('迷你照相机',' 红色',' 尼康', 500 ,3),('硬盘',' 黑色', '希捷' ,600 ,3),('显卡',' 黑色',' 技嘉', 800, 3) +select * from GoodsInfo +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select TOP 1 GoodsMoney 商品价格,GoodsName 商品名称,GoodsColor 商品颜色 from GoodsInfo + group by GoodsName,GoodsMoney,GoodsColor order by GoodsMoney DESC +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodsMoney) 商品最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0386f3e403034df6b6d00dc06cd3af3590890cc8 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" @@ -0,0 +1,59 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\SQL作业\SQL作业12--复习题\GoodsDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='GoodsDB_log', + filename='D:\SQL作业\SQL作业12--复习题\GoodsDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +go + +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int constraint FK_GoodsInfo_TypeID references GoodsType(TypeID) +) +go + +insert into GoodsInfo values('提花小西装','红色','菲曼琪','300','1'),('百搭短裤','绿色','哥弟','100','1'), +('无袖背心','白色','阿依莲','700','1'),('低帮休闲鞋','红色','菲曼琪','900','2'),('中跟单鞋','绿色','哥弟','400','2'), +('平底鞋','白色','阿依莲','200','2'),('迷你照相机','红色','尼康','500','3'),('硬盘','黑色','希捷','600','3'),('显卡','黑色','技嘉','800','3') + +select * from GoodsType +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..577d15d4f1170eac820ddec336efe3fa0166a64c --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" @@ -0,0 +1,53 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null, + +) +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null, + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +insert into GoodsInfo values('提花小西装','红色','菲曼琪' ,'300', 1),('百搭短裤','绿色','哥弟' ,'100', 1), +('无袖背心', '白色', '阿依莲', '700', 1),('低帮休闲鞋', '红色',' 菲曼琪',' 900', 2),('中跟单鞋','绿色','哥弟','400', 2), +('平底鞋','白色','阿依莲','200', 2),('迷你照相机','红色','尼康','500',3),('硬盘','黑色','希捷', '600', 3), +('显卡','黑色','技嘉','800',3) +select * from GoodsInfo +select * from GoodsType +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称, GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc + + +select GoodsName 商品名称, GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney in (select max(GoodsMoney) from GoodsInfo) +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型编号,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and (GoodsMoney between 300 and 600) + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a34856760f28eae22dddccadc1467d30035d66fa --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery2.sql" @@ -0,0 +1,43 @@ +create database xxx +go +use xxx +go + +create table GoodsType +( + TypeID int primary key identity, + TypeName nvarchar(20) not null + +) + +create table GoodsInfo +( + GoodsID int primary key identity, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null, + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +insert into GoodsInfo values('提花小西装', '红色', '菲曼琪', '300', 1), +('百搭短裤', '绿色', '哥弟', '100', 1), +('无袖背心', '白色', '阿依莲', '700', 1), +('低帮休闲鞋', '红色', '菲曼琪', '900', 2), +('中跟单鞋', '绿色', '哥弟', '400', 2), +('平底鞋', '白色', '阿依莲','200', 2), +('迷你照相机', '红色', '尼康', '500', 3), +('硬盘', '黑色', '希捷', '600', 3), +('显卡', '黑色', '技嘉','800', 3) + + +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo ORDER BY GoodsMoney DESC +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney<=600 and GoodsMoney>=300 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6c31939b84b41f141367f223c509de2930cf34f2 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery2.sql" @@ -0,0 +1,36 @@ +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +go + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int constraint FK_GoodsInfo_TypeID references GoodsType(TypeID) +) +go +insert into GoodsType values ('服装内衣'),('鞋包数码'),('手机数码') +insert into GoodsInfo values('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟 ',400,2), +('平底鞋','白色','阿依莲',200,2),('迷你照相机','红色 ','尼康',500 ,3), +('硬盘','黑色','希捷',600,3),('显卡 ','黑色','技嘉',800,3) +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney = (select MAX(GoodsMoney) from GoodsInfo) +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型,MAX(GoodsMoney)最高价格,MIN(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo group by TypeID +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where (GoodsColor = '红色') and (GoodsMoney between 300 and 600) +select * from GoodsInfo where (GoodsColor = '红色') and (GoodsMoney >= 300 and GoodsMoney <= 600) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery12.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery12.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cac608362168946ccc7168bb8769e10b76c08d0e --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery12.sql" @@ -0,0 +1,36 @@ +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +go + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int constraint FK_GoodsInfo_TypeID references GoodsType(TypeID) +) +go +insert into GoodsType values ('服装内衣'),('鞋包数码'),('手机数码') +insert into GoodsInfo values('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟 ',400,2), +('平底鞋','白色','阿依莲',200,2),('迷你照相机','红色 ','尼康',500 ,3), +('硬盘','黑色','希捷',600,3),('显卡 ','黑色','技嘉',800,3) +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney = (select MAX(GoodsMoney) from GoodsInfo) +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型,MAX(GoodsMoney)最高价格,MIN(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo group by TypeID +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where (GoodsColor = '红色') and (GoodsMoney between 300 and 600) +select * from GoodsInfo where (GoodsColor = '红色') and (GoodsMoney >= 300 and GoodsMoney <= 600) diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..820a27a4ad08ecd6323e089d25e91ceea084dab7 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" @@ -0,0 +1,45 @@ +use master +go + +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +go + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int constraint FK_GoodsInfo_TypeID references GoodsType(TypeID) +) +go + +insert into GoodsType values ('服装内衣'),('鞋包数码'),('手机数码') + +insert into GoodsInfo values('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟 ',400,2), +('平底鞋','白色','阿依莲',200,2),('迷你照相机','红色 ','尼康',500 ,3), +('硬盘','黑色','希捷',600,3),('显卡 ','黑色','技嘉',800,3) + +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney = (select MAX(GoodsMoney) from GoodsInfo) +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型,MAX(GoodsMoney)最高价格,MIN(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo group by TypeID +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 ,包含300和600么?咱不知道,咱也懒的问 +select * from GoodsInfo where (GoodsColor = '红色') and (GoodsMoney between 300 and 600) +select * from GoodsInfo where (GoodsColor = '红色') and (GoodsMoney >= 300 and GoodsMoney <= 600) +-- 如果不包含300和600 +select * from GoodsInfo where (GoodsColor = '红色') and ((GoodsMoney between 300 and 600) and (GoodsMoney not in(300,600))) +select * from GoodsInfo where (GoodsColor = '红色') and (GoodsMoney > 300 and GoodsMoney < 600) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..73b08c1481ca8c708caf9cc9c9d3b700f4b00ca1 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery1.sql" @@ -0,0 +1,58 @@ +use master +go +create database GoodsDB +on( + name='GoodsType', + filename='D:\sql\GoodsType.mdf', + size=8mb, + maxsize=80mb, + filegrowth=10% + +) +log on( + + name='GoodsType_log', + filename='D:\sql\GoodsType_log.ldf', + size=8mb, + maxsize=80mb, + filegrowth=10% + +) +go +use GoodsDB +go +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null, +) +select * from GoodsType +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID), +) +select * from GoodsInfo +insert into GoodsInfo +values('提花小西装','红色','菲曼琪',300.00,1), + ('百搭短裤','绿色','哥弟',100.00,1), + ('无袖背心','白色','阿依莲',700.00,1), + ('低帮休闲鞋','红色','菲曼琪',900.00,2), + ('中跟单鞋', '绿色', '哥弟', 400.00, 2), + ('平底鞋', '白色', '阿依莲', 200, 2), + ('迷你照相机','红色','尼康',500.00,3), + ('硬盘','黑色','希捷',600.00,3), + ('显卡','黑色','技嘉',800.00,3) +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select * from GoodsType +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney DESC +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select * from GoodsInfo +select TypeID 类型编号,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色'and GoodsMoney>=300 and GoodsMoney<=600 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/2021-4-2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/2021-4-2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..573b3b9bf966900cac6b7e0dd5b58b251b631050 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/2021-4-2.sql" @@ -0,0 +1,108 @@ +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +create database Student +go +use Student +go + +create table stuInfo +( + stuID int identity(1,1) primary key, + stuName varchar(20) not null, + stuAge int, + stuSex varchar(1) default(1) check(stuSex in('1','0')), + time date +) +insert into stuInfo(stuName,stuAge,stuSex) values +('Tom',19,'1'), +('Jack',20,'0'), +('Rose',21,'1'), +('Lulu',19,'1'), +('Lili',21,'0') +insert into stuInfo values ('abc',20,1,'2001-01-07 01:11:36.590') + + +create table courseInfo +( + courseID int identity(1,1) primary key, + courseName varchar(20) not null, + courseMarks int +) +insert into courseInfo values ('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) + +create table scoreInfo +( + scoreID int identity(1,1) primary key, + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) +insert into scoreInfo values +(1,1,80), +(1,2,85), +(1,4,50), +(2,1,75), +(2,3,45), +(2,4,75), +(3,1,45), +(4,1,95), +(4,2,75), +(4,3,90), +(4,4,45) + +select * from stuInfo +select * from courseInfo +select * from scoreInfo +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + +select StuID 学生编号,count(courseID) 所选修课程数量,avg(score) 平均分 +from scoreInfo group by StuID + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + +select courseID 课程编号,count(StuID) 课程所选修学生个数,sum(score) 总分 +from scoreInfo group by courseID + +--3.查询出性别一样并且年龄一样的学生的信息 + +select A.stuID,A.stuName,A.stuAge,A.stuSex,A.time from StuInfo A,StuInfo B +where A.stuSex=B.stuSex and A.stuAge=B.stuAge and A.stuID != b.stuID + +--4.查询出学分一样的课程信息 + +select difference(A.courseID,A.courseName,A.courseMarks from courseInfo A,courseInfo B +where A.courseMarks=B.courseMarks and A.courseID != b.courseID + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + +select A.stuID 学号,A.stuName 姓名,B.courseID 课程编号,B.score 分数 +from stuInfo A inner join scoreInfo B on A.stuID=B.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + +select A.stuID 学号,B.courseID 课程编号,C.courseName 课程名,C.courseMarks 课程学分,B.score 分数 +from stuInfo A inner join scoreInfo B on A.stuID=B.stuID +inner join courseInfo C on C.courseID=B.courseID + +--7.查询出没有参加考试的学生的学号和姓名 + +select A.stuID 学号,A.stuName 姓名 +from stuInfo A left join scoreInfo B on A.stuID=B.stuID +where score is null + +--8.查询出是周六周天来报到的学生 + + --学生愚昧,请老师解惑! + select * from stuInfo where time= + (select time ,datename(dw,time) from stuInfo + group by time having datename(dw,time)='星期六' or datename(dw,time)='星期日') + +--9.查询出姓名中有字母a的学生的信息 + +select * from stuInfo where stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 + +select A.StuID 学号,sum(score) 平均分,count(courseID) 选修数量 +from stuInfo A join scoreInfo B on A.stuID=B.stuID +group by A.StuID having count(courseID)>2 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c06b445d6c6719b8a0fd57040a06bebc8d725bef --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" @@ -0,0 +1,55 @@ +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\sp\GoodsDB.mdf', + size=10mb, + filegrowth=2mb +) +log on +( + name='GoodsDB_log', + filename='D:\sp\GoodsDB_log.ldf', + size=10mb, + filegrowth=2mb +) +use GoodsDB +go +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null , + GoodsColor nvarchar(20) not null , + GoosdBrand nvarchar(20), + GoodsMoney money not null, + TypeID INT foreign key (TypeID) references GoodsType(TypeID) +) +insert into GoodsType(TypeName)values +('服装内衣'), +('鞋包配饰'), +('手机数码') +select * from GoodsType +insert into GoodsInfo(GoodsName,GoodsColor,GoosdBrand,GoodsMoney,TypeID)values +('提花小西装',' 红色',' 菲曼琪', 300, 1), +('百搭短裤 ','绿色',' 哥弟', 100, 1), +('无袖背心', '白色',' 阿依莲', 700, 1), +('低帮休闲鞋',' 红色',' 菲曼琪', 900, 2), +('中跟单鞋',' 绿色',' 哥弟', 400, 2), +('平底鞋',' 白色',' 阿依莲', 200, 2), +('迷你照相机',' 红色', '尼康', 500, 3), +('硬盘', '黑色',' 希捷', 600, 3), +(' 显卡',' 黑色', '技嘉' ,800, 3) +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney in (select max(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格 ,avg(GoodsMoney)平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where (GoodsMoney>=300 and GoodsMoney<=600) and GoodsColor=' 红色' \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\345\244\215\344\271\240\351\242\230.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\345\244\215\344\271\240\351\242\230.sql" new file mode 100644 index 0000000000000000000000000000000000000000..417577f8b5023d629d3acc5369f316cfb6a4b8e6 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\345\244\215\344\271\240\351\242\230.sql" @@ -0,0 +1,73 @@ +use master +go + +create database GoodsDB +on +( + name='GoodsDB', + filename='C:\test\GoodsDB.mdf', + size=20, + maxsize=200, + filegrowth=10% + ) + log on +( + name='GoodsDB_log', + filename='C:\test\GoodsDB_log.ldf', + size=20, + maxsize=200, + filegrowth=10% + ) + + go + use GoodsDB + go + +create table GoodsType + +( + TypeID int primary key identity (1,1) not null, + TypeName nvarchar (20) not null, + ) + + insert into GoodsType(TypeName) values ('服装内衣'),('鞋包配饰'),('手机数码') + + select * from GoodsType + + create table GoodsInfo +( + GoodsID int primary key identity (1,1), + GoodsName nvarchar (20) not null, + GoodsColor nvarchar (20) not null, + GoodsBrand nvarchar (20) not null, + GoodsMoney money not null, + TyoeID int references GoodsType(TypeID) + ) + + insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TyoeID) + values ('提花小西装','红色','菲曼琪','300','1') , + ('百搭短裤','绿色','哥弟','300','1'), + ('无袖背心','白色','阿依莲','700','1'), + ('低帮休闲鞋','红色','菲曼琪','900','2'), + ('中跟单鞋','绿色','哥弟','400','2'), + ('平底','白色','阿依莲','700','1'), + ('迷你照相机','红色','尼康','500','3') + + select * from GoodsInfo + + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + + select top 1 GoodsName as 商品名称,GoodsColor as 商品颜色,GoodsMoney as 商品价格 from GoodsInfo + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + + select GoodsName as 商品名称 ,max(GoodsMoney) as 最高价格,min(GoodsMoney)as 最低价格,avg(GoodsMoney)as 平均价格 from GoodsInfo + group by GoodsMoney,GoodsName + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + select GoodsName as 商品名称,GoodsColor as 商品颜色,GoodsMoney as 商品价格 from GoodsInfo + where GoodsColor='红色' and GoodsMoney >=300 and GoodsMoney <=600 + + select GoodsName as 商品名称,GoodsColor as 商品颜色,GoodsMoney as 商品价格 from GoodsInfo + where GoodsColor='红色' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..df2269d9442434a36aaeb6e1f8f69b5bdccc2fa0 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery1.sql" @@ -0,0 +1,95 @@ +use master +go +--1、创建商品数据库(GoodsDB) +create database GoodsDB +on +( + name='GoodsDB', + filename='E:\数据库文件\数据库跟目录文件\商品库(复习多表查询)\GoodsDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='E:\数据库文件\数据库跟目录文件\商品库(复习多表查询)\GoodsDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +go +use GoodsDB + +--然后建立表GoodsType(商品类型表)表结构分别如下: +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 +create table GoodsType +( + TypeID int not null primary key identity(1,1) , + TypeName nvarchar(20) not null +) +--GoodsInfo(商品信息表),表结构分别如下: +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) null, + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 + --1 服装内衣 + --2 鞋包配饰 + --3 手机数码] +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') + +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 +insert into GoodsInfo values +('提花小西装' ,'红色', '菲曼琪' ,300 ,1), +('百搭短裤' ,'绿色', '哥弟' ,100 ,1), +('无袖背心' ,'白色' ,'阿依莲' ,700 ,1), +('低帮休闲鞋' ,'红色', '菲曼琪' ,900 ,2), +('中跟单鞋' ,'绿色' ,'哥弟' ,400 ,2), +('平底鞋' ,'白色', '阿依莲' ,200 ,2), +('迷你照相机' ,'红色', '尼康' ,500 ,3), +('硬盘' ,'黑色', '希捷' ,600 ,3), +('显卡' ,'黑色' ,'技嘉' ,800 ,3) +select*from GoodsType +select*from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney=(select max(GoodsMoney)from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select GoodsType.TypeID 商品类型编号,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsType inner join GoodsInfo on +GoodsType.TypeID=GoodsInfo.TypeID group by GoodsType.TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select*from GoodsInfo where GoodsColor='红色' and GoodsMoney>300and GoodsMoney<600 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/1111.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/1111.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3429252e2289a5345257756832926dcbfc66a194 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/1111.sql" @@ -0,0 +1,40 @@ +create database GoodsDB +on +( name='GoodsDB', + filename='D:\SQL\GoodsDB.mdf' +) +log on +( name='GoodsDB_log', + filename='D:\SQL\GoodsDB_log.ldf' +) +use GoodsDB +go +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int constraint FK_GoodsInfo_TypeID references GoodsType(TypeID) +) +insert into GoodsType values +('服装内衣'),('鞋包数码'),('手机数码') +insert into GoodsInfo values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟 ',400,2), +('平底鞋','白色','阿依莲',200,2),('迷你照相机','红色 ','尼康',500 ,3), +('硬盘','黑色','希捷',600,3),('显卡 ','黑色','技嘉',800,3) + +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney = (select MAX(GoodsMoney) from GoodsInfo) +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型,MAX(GoodsMoney)最高价格,MIN(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo group by TypeID +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor = '红色' and GoodsMoney >= 300 and GoodsMoney <= 600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQL1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQL1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..794ea0af3c3c25636dedcb3c1868d3e63b068e6c --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQL1.sql" @@ -0,0 +1,53 @@ +create database GoodsDB + +use GoodsDB +go + +create table GoodsType +( +TypeID int not null primary key identity(1,1), +TypeName nvarchar(20) not null +) + +create table GoodsInfo +( +GoodsID int not null primary key identity(1,1), +GoodsName nvarchar(20) not null , +GoodsColor nvarchar(20) not null , +GoodsBrand nvarchar(20) , +GoodsMoney money not null, +TypeID int foreign key references GoodsType(TypeID) +) + +insert into GoodsType values +('鏈嶈鍐呰。'), +('闉嬪寘閰嶉グ'), +('鎵嬫満鏁扮爜') + + +insert into GoodsInfo values +('鎻愯姳灏忚タ瑁','绾㈣壊','鑿叉浖鐞','300',1), +('鐧炬惌鐭¥','缁胯壊','鍝ュ紵','100',1), +('鏃犺鑳屽績','鐧借壊','闃夸緷鑾','700',1), +('浣庡府浼戦棽闉','绾㈣壊','鑿叉浖鐞','900',2), +('涓窡鍗曢瀷','缁胯壊','鍝ュ紵','400',2), +('骞冲簳闉','鐧借壊','闃夸緷鑾','200',2), +('杩蜂綘鐓х浉鏈','绾㈣壊','灏煎悍','500',3), +('纭洏','榛戣壊','甯屾嵎','600',3), +('鏄惧崱','榛戣壊','鎶鍢','800',3) + +select * from GoodsInfo +--3銆佹煡璇环鏍兼渶璐电殑鍟嗗搧鍚嶇О锛屽晢鍝侀鑹插拰鍟嗗搧浠锋牸锛岃姹備娇鐢ㄥ埆鍚嶆樉绀哄垪鍚 +select top 1 GoodsMoney 鍟嗗搧浠锋牸,GoodsName 鍟嗗搧鍚嶇О,GoodsColor 鍟嗗搧棰滆壊 +from GoodsInfo +group by GoodsName,GoodsColor,GoodsMoney +order by GoodsMoney desc + +--4銆佹寜鍟嗗搧绫诲瀷缂栧彿鍒嗙粍鏌ヨ鍟嗗搧鏈楂樹环鏍硷紝鏈浣庝环鏍煎拰骞冲潎浠锋牸锛岃姹備娇鐢ㄥ埆鍚嶆樉绀哄垪鍚 +select TypeID 绫诲瀷缂栧彿,max(GoodsMoney) 鏈楂樹环鏍,min(GoodsMoney ) 鏈浣庝环鏍,avg(GoodsMoney) 骞冲潎浠锋牸 +from GoodsInfo +group by TypeID +--5銆佹煡璇㈠晢鍝佷俊鎭墍鏈夊垪锛岃姹傚晢鍝侀鑹蹭负绾㈣壊锛屼环鏍煎湪300~600涔嬮棿 +select * +from GoodsInfo +where GoodsColor ='绾㈣壊'and GoodsMoney>300 and GoodsMoney<600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cb3c1484abbd2bf4c8fe438bc84c44dfd39b273c --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery1.sql" @@ -0,0 +1,57 @@ +use master +go + +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType --商品类型表 +( + TypeID int not null primary key identity(1,1), --商品类型编号 + TypeName nvarchar(20) not null --商品类型名称 +) + + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), --商品编号 + GoodsName nvarchar(20) not null, --商品名称 + GoodsColor nvarchar(20) not null, --商品颜色 + GoodsBrand nvarchar(20) , --商品品牌 + GoodsMoney money not null, --商品价格 + TypeID int references GoodsType (TypeID) --商品类型编号 +) + +insert into GoodsType +values (1,'服装内衣'), + (2,'鞋包配饰'), + (3,'手机数码') + + + +insert into GoodsInfo(GoodsID,GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +values (1,'提花小西装','红色','菲曼琪','300',1), + (2,'百搭短裤','绿色','哥弟','100',1), + (3,'无袖背心','白色','阿依莲','700',1), + (4,'低帮休闲鞋','红色','菲曼琪','900',2), + (5,'中跟单鞋','绿色','哥弟','400',2), + (6,'平底鞋','白色','阿依莲','200',2), + (7,'迷你照相机','红色','尼康','500',3), + (8,'硬盘','黑色','希捷','600',3), + (9,'显卡','黑色','技嘉','800',3) + + +--3、查询价格最贵的 商品名称, 商品颜色 和 商品价格,要求使用 别名 显示列名 +select TOP 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo +order by GoodsMoney DESC + +--4、按 商品类型编号 分组查询 商品最高价格, 最低价格 和 平均价格,要求使用别名显示列名 +select TypeID 商品类型编号,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 from GoodsInfo +group by TypeID + + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 + diff --git "a/\347\254\254\345\215\201\344\272\224\346\254\241\344\275\234\344\270\232/.keep" "b/\347\254\254\345\215\201\344\272\224\346\254\241\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\224\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\224\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b3d9a2fc50f401ea6df7e47b616aae3f0745a613 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\224\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" @@ -0,0 +1,71 @@ +create database GoodsDB +on +( + name = 'GoodsDB', + filename = 'D:\GoodsDB.mdf' +) +log on +( + name = 'GoodsDB_log', + filename = 'D:\GoodsDB_log.ldf' +) +use GoodsDB + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null , + TypeID int references GoodsType(TypeID) +) +insert into GoodsType values +('服装内衣'),('鞋包配饰'),('手机数码') +insert into GoodsInfo values +('提米小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋 ','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','希捷',800,3) +select * from GoodsType +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 名称,GoodsColor 颜色 ,GoodsMoney 价格 from GoodsInfo +where GoodsMoney in (select max(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TYPEID 编号,MIN(GoodsMoney)最低价, Max(GoodsMoney)最高价,avg(GoodsMoney)平均价 from GoodsInfo +group by TYPEID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor = '红色' and GoodsMoney between 300 and 600 + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select a.TypeID 商品编号,a.TypeName 商品名称,round(avg(b.GoodsMoney),2)平均价格 from GoodsType a +left join GoodsInfo b on a.TypeID = b.TypeID +group by a.TypeID,a.TypeName + +--7、查询每种颜色的商品数 +select GoodsColor 颜色,count(*) from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add remark text +update GoodsType set remark = '我是商品类型信息备注' + +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop constraint FK__GoodsInfo__TypeI__1273C1CD + +--10、删除所有商品类型记录 +delete from GoodsType +select * from GoodsType diff --git "a/\347\254\254\345\215\201\344\272\224\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\272\224\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..dfb34cbd4cb6babc446fb19175a303965b4f22b6 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\224\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" @@ -0,0 +1,75 @@ +create database HOUSE_DB +on +( + name = 'HOUSE_DB', + filename = 'D:\HOUSE_DB.mdf', + size = 5, + maxsize = 50, + filegrowth = 10% +) +log on +( + name = 'HOUSE_DB_log', + filename = 'D:\HOUSE_DB_log.ldf' +) + +use HOUSE_DB + +create table HOUSE_TYPE +( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null unique +) +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE values +('小户型'), +('经济型'), +('别墅') +insert into HOUSE values +('学区房',25000,1), +('别墅型',40000,3), +('小区房',15000,2), +('复古型',35000,3), +('西式复古型',40000,3) +select * from HOUSE_TYPE +select * from HOUSE + +--查询所有房屋信息 +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE where house_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select * from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select b.house_name 名称,a.type_name 房屋类型名称 from HOUSE_TYPE a +inner join HOUSE b on a.type_id = b.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select * from HOUSE where house_price in(select max(house_price) from HOUSE) + +-- 查询每种房屋类型的房屋数量 +select house_name 房屋类型, count(*)数量 from HOUSE group by house_name + +-- 修改房屋表的房屋名称字段,把数据类型改成nvarchar(30) +alter table HOUSE alter column house_name nvarchar(30) + +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add home text + +-- 把房屋价格的默认值0改为100 +alter table house drop constraint DF__HOUSE__house_pri__1367E606 +alter table house add constraint df_s default(100) for house_price + +--查询出名字最长的房屋信息。 +select top 1 b.*,len(a.house_name)长度 from HOUSE a +left join HOUSE b on a.house_id = b.house_id +order by len(a.house_name) desc diff --git "a/\347\254\254\345\215\201\344\272\224\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\272\224\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ccb62b29a268e2efc7a4a0fab4fc40e9d16d456b --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\224\346\254\241\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery3.sql" @@ -0,0 +1,63 @@ +create database StarManagerDB + +use StarManagerDB + +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null , + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) +select * from StarType +select * from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 * from StarInfo order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select count(S_T_NO)明星人数,AVG(S_AGE)平均年龄 from StarInfo +group by S_T_NO having count(S_T_NO)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_T_NO = 1 + +--6、请统计每种明星类型的人数 +select b.T_NAME 明星类型,count(a.S_T_NO)数量 from StarInfo a +left join StarType b on a.S_T_NO=b.T_NO +group by b.T_NAME + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add income int default(10000000) + +--8、查询每个国家的明星人数。 +select S_NATIVE 国家, count(*)人数 from StarInfo group by S_NATIVE + +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select *,rank()over(partition by S_NATIVE order by s_age desc) from StarInfo + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarInfo where S_NAME like '%西' and len(S_NAME)=2 + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table starInfo add time datetime default(getdate()) +alter table starType add time datetime default(getdate()) + diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/.keep" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/4.09/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/4.09/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..22b75b286489a772612a0a815152c83d69e8ef2c --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/4.09/SQLQuery2.sql" @@ -0,0 +1,104 @@ +use master +go + +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\text\GoodsDB.mdf', + size=5mb, + maxsize=600mb, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='D:\text\GoodsDB_log.ldf', + size=5mb, + maxsize=600mb, + filegrowth=10% +) +use GoodsDB +go +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID +create table GoodsInfo +( + GoodsID int not null identity(1,1) primary key, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int foreign key references GoodsType (TypeID) +) +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 +insert into GoodsType (TypeName) +values ('服装内衣'),('鞋包配饰'),('手机数码') +select * from GoodsType +select * from GoodsInfo +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +values ('提花小西装','红色','费曼琪',300,1), +--2 百搭短裤 绿色 哥弟 100 1 + ('百搭短裤','绿色','哥弟',100,1), +--3 无袖背心 白色 阿依莲 700 1 +('无袖背心','白色','阿依莲',700,1), +--4 低帮休闲鞋 红色 菲曼琪 900 2 +('低帮休闲鞋','红色','费曼琪',900,2), +--5 中跟单鞋 绿色 哥弟 400 2 +('中跟单鞋','绿色','哥弟',400,2), +--6 平底鞋 白色 阿依莲 200 2 +('平底鞋','白色','阿依莲',200,2), +--7 迷你照相机 红色 尼康 500 3 +('迷你照相机','红色','尼康',500,3), +--8 硬盘 黑色 希捷 600 3 +('硬盘','黑色','希捷',600,3), +--9 显卡 黑色 技嘉 800 3 +('显卡','黑色','技嘉',800,3) +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select TOP 1 GoodsMoney 商品价格,GoodsName 商品名称,GoodsColor 商品颜色 from GoodsInfo + group by GoodsName,GoodsMoney,GoodsColor order by GoodsMoney DESC +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodsMoney) 商品最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 + from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select TypeName 类别名称,avg(GoodsMoney) 平均价格 from GoodsInfo +inner join GoodsType on GoodsType.TypeID=GoodsInfo.TypeID group by TypeName + +--7、查询每种颜色的商品数 +select GoodsColor 颜色,count(*) 商品数 from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add Notes text +update GoodsType set Notes='我是商品类型信息备注' +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop constraint FK__GoodsInfo__TypeI__1273C1CD + +--10、删除所有商品类型记录 +truncate table GoodsType diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/4.09/SQLQuery4.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/4.09/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..67453bb5691cad223f103860cef7354487d0e7a3 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/4.09/SQLQuery4.sql" @@ -0,0 +1,91 @@ +use master +go + +create database StarManagerDB +go + +use StarManagerDB +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NatIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) + + + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 +insert into StarType values ('体育明星'),('IT明星'),('相声明星') + +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NatIVE,S_T_NO) +values ('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景观',40,'敲代码','中国',2), +('马斯克',36,'外星人','中国',2),('郭德纲',50,'相声','中国',3),('黄峥',41,'拼多多','中国',2) + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + + +select * from StarType +select * from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select TOP 3 S_AGE 年龄 ,S_NAME 姓名,S_HOBBY 特技, S_NatIVE 籍贯 FROM StarInfo order by S_AGE DESC + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +SELECT S_T_NO 类型编号 ,COUNT(*) 明星人数 ,AVG(S_AGE)明星平均年龄 FROM StarInfo GROUP BY S_T_NO HAVING COUNT(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +SELECT TOP 1 S_AGE 年龄 ,S_NAME 姓名, S_HOBBY 特技 ,S_NatIVE 籍贯 FROM StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO +where T_NAME='体育明星' order by S_AGE DESC +--6、请统计每种明星类型的人数 +select T_NAME 明星类型,count(*)人数 from StarInfo inner join StarType on StarInfo.S_T_NO=StarType.T_NO + group by T_NAME + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add Income money default(10000000) not null + +--8、查询每个国家的明星人数。 +select S_NATIVE 籍贯,count(*) 人数 from StarInfo group by S_NATIVE + +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select StarInfo.* , T_NAME from StarInfo inner join StarType on StarInfo.S_T_NO=StarType.T_NO order by S_NATIVE,S_AGE DESC + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select StarInfo.* , T_NAME from StarInfo inner join StarType on StarInfo.S_T_NO=StarType.T_NO where S_NAME like '_西' + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add S_TIME datetime default(getdate()) not null +alter table StarType add S_T_TIME datetime default(getdate()) not null \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/4.09/SQLQuery5.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/4.09/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2b0306ac1dc03fb407c0f5171b90a03caf8901d6 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/4.09/SQLQuery5.sql" @@ -0,0 +1,87 @@ +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) + +use HOUSE_DB +go + +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 +create table HOUSE_TYPE +( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null unique +) +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null , + house_price float not null default(0), + tyoe_id int not null foreign key references HOUSE_TYPE(type_id) +) +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') + +insert into HOUSE(house_name,house_price,tyoe_id) values ('A',5000,1) , ('B',6000,2) ,('C',8000,3) + +--4、添加查询 +--查询所有房屋信息 +SELECT * FROM HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 名称, house_price 租金 from HOUSE order by house_price DESC + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE_TYPE +inner join HOUSE on HOUSE_TYPE.type_id =HOUSE.tyoe_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select Top 1 house_price 最高的租金, house_name 房屋名称 from HOUSE order by house_price DESC + +-- 查询每种房屋类型的房屋数量 +select house_name,count(*) 房屋数量 from HOUSE group by house_name + +-- 修改房屋表的房屋名称字段,把数据类型改成nvarchar(30) +alter table HOUSE alter column house_name nvarchar(30) + +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add address text + +-- 把房屋价格的默认值0改为100 +alter table house drop DF__HOUSE__house_pri__276EDEB3 +alter table house add CONSTRAINT DF default(100) for house_price + +--查询出名字最长的房屋信息。 +select top 1 ouse_id,house_name,house_price,type_id,dizhi, len(house_name)名字长度 from HOUSE +group by ouse_id ,house_name,house_price,type_id,dizhi order by house_name desc diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\345\244\215\344\271\240\351\242\230\344\270\200.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\345\244\215\344\271\240\351\242\230\344\270\200.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5e864fc93e74fe5cc5e46cc63e591002c283c92a --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\345\244\215\344\271\240\351\242\230\344\270\200.sql" @@ -0,0 +1,105 @@ +--1、创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表) +use master +go + +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\text\GoodsDB.mdf', + size=5mb, + maxsize=600mb, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='D:\text\GoodsDB_log.ldf', + size=5mb, + maxsize=600mb, + filegrowth=10% +) +use GoodsDB +go +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID +create table GoodsInfo +( + GoodsID int not null identity(1,1) primary key, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int foreign key references GoodsType (TypeID) +) +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 +insert into GoodsType (TypeName) +values ('服装内衣'),('鞋包配饰'),('手机数码') +select * from GoodsType +select * from GoodsInfo +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +values ('提花小西装','红色','费曼琪',300,1), +--2 百搭短裤 绿色 哥弟 100 1 + ('百搭短裤','绿色','哥弟',100,1), +--3 无袖背心 白色 阿依莲 700 1 +('无袖背心','白色','阿依莲',700,1), +--4 低帮休闲鞋 红色 菲曼琪 900 2 +('低帮休闲鞋','红色','费曼琪',900,2), +--5 中跟单鞋 绿色 哥弟 400 2 +('中跟单鞋','绿色','哥弟',400,2), +--6 平底鞋 白色 阿依莲 200 2 +('平底鞋','白色','阿依莲',200,2), +--7 迷你照相机 红色 尼康 500 3 +('迷你照相机','红色','尼康',500,3), +--8 硬盘 黑色 希捷 600 3 +('硬盘','黑色','希捷',600,3), +--9 显卡 黑色 技嘉 800 3 +('显卡','黑色','技嘉',800,3) +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select TOP 1 GoodsMoney 商品价格,GoodsName 商品名称,GoodsColor 商品颜色 from GoodsInfo + group by GoodsName,GoodsMoney,GoodsColor order by GoodsMoney DESC +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodsMoney) 商品最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 + from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select TypeName 类别名称,avg(GoodsMoney) 平均价格 from GoodsInfo +inner join GoodsType on GoodsType.TypeID=GoodsInfo.TypeID group by TypeName + +--7、查询每种颜色的商品数 +select GoodsColor 颜色,count(*) 商品数 from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add Notes text +update GoodsType set Notes='我是商品类型信息备注' +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop constraint FK__GoodsInfo__TypeI__1273C1CD + +--10、删除所有商品类型记录 +truncate table GoodsType diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\345\244\215\344\271\240\351\242\230\344\270\211.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\345\244\215\344\271\240\351\242\230\344\270\211.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3673422e5d2ac05b47fb6ccbf2240002084158f9 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\345\244\215\344\271\240\351\242\230\344\270\211.sql" @@ -0,0 +1,92 @@ +--创建明星数据库(StarManagerDB) +use master +go + +create database StarManagerDB +go + +use StarManagerDB +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NatIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) + + + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 +insert into StarType values ('体育明星'),('IT明星'),('相声明星') + +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NatIVE,S_T_NO) +values ('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景观',40,'敲代码','中国',2), +('马斯克',36,'外星人','中国',2),('郭德纲',50,'相声','中国',3),('黄峥',41,'拼多多','中国',2) + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + + +select * from StarType +select * from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select TOP 3 S_AGE 年龄 ,S_NAME 姓名,S_HOBBY 特技, S_NatIVE 籍贯 FROM StarInfo order by S_AGE DESC + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +SELECT S_T_NO 类型编号 ,COUNT(*) 明星人数 ,AVG(S_AGE)明星平均年龄 FROM StarInfo GROUP BY S_T_NO HAVING COUNT(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +SELECT TOP 1 S_AGE 年龄 ,S_NAME 姓名, S_HOBBY 特技 ,S_NatIVE 籍贯 FROM StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO +where T_NAME='体育明星' order by S_AGE DESC +--6、请统计每种明星类型的人数 +select T_NAME 明星类型,count(*)人数 from StarInfo inner join StarType on StarInfo.S_T_NO=StarType.T_NO + group by T_NAME + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add Income money default(10000000) not null + +--8、查询每个国家的明星人数。 +select S_NATIVE 籍贯,count(*) 人数 from StarInfo group by S_NATIVE + +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select StarInfo.* , T_NAME from StarInfo inner join StarType on StarInfo.S_T_NO=StarType.T_NO order by S_NATIVE,S_AGE DESC + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select StarInfo.* , T_NAME from StarInfo inner join StarType on StarInfo.S_T_NO=StarType.T_NO where S_NAME like '_西' + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add S_TIME datetime default(getdate()) not null +alter table StarType add S_T_TIME datetime default(getdate()) not null \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\345\244\215\344\271\240\351\242\230\344\272\214.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\345\244\215\344\271\240\351\242\230\344\272\214.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e2caa33d345b66444bbde7706b229446588c53f3 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\345\244\215\344\271\240\351\242\230\344\272\214.sql" @@ -0,0 +1,92 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) + +use HOUSE_DB +go + +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 +create table HOUSE_TYPE +( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null unique +) +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null , + house_price float not null default(0), + tyoe_id int not null foreign key references HOUSE_TYPE(type_id) +) +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') + +insert into HOUSE(house_name,house_price,tyoe_id) values ('A',5000,1) , ('B',6000,2) ,('C',8000,3) + +--4、添加查询 +--查询所有房屋信息 +SELECT * FROM HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 名称, house_price 租金 from HOUSE order by house_price DESC + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE_TYPE +inner join HOUSE on HOUSE_TYPE.type_id =HOUSE.tyoe_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select Top 1 house_price 最高的租金, house_name 房屋名称 from HOUSE order by house_price DESC + +-- 查询每种房屋类型的房屋数量 +select house_name,count(*) 房屋数量 from HOUSE group by house_name + +-- 修改房屋表的房屋名称字段,把数据类型改成nvarchar(30) +alter table HOUSE alter column house_name nvarchar(30) + +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add address text + +-- 把房屋价格的默认值0改为100 +alter table house drop DF__HOUSE__house_pri__276EDEB3 +alter table house add CONSTRAINT DF default(100) for house_price + +--查询出名字最长的房屋信息。 +select top 1 ouse_id,house_name,house_price,type_id,dizhi, len(house_name)名字长度 from HOUSE +group by ouse_id ,house_name,house_price,type_id,dizhi order by house_name desc diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-GoodsDB2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-GoodsDB2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..136d9b7f016229af07ec4fb8beab923fbd13aaf5 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-GoodsDB2.sql" @@ -0,0 +1,106 @@ +use master +go +create database GoodsDB2 +on +( + name='GoodsDB2', + filename='D:\SQL作业\SQL作业14\GoodsDB2.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='GoodsDB2_log', + filename='D:\SQL作业\SQL作业14\GoodsDB2_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use GoodsDB2 +go +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +go + +use GoodsDB2 +go +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int constraint FK_GoodsInfo_TypeID references GoodsType(TypeID) +) +go + + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 + +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 + +insert into GoodsInfo values ('提花小西装','红色','菲曼琪','300','1') +insert into GoodsInfo values ('百搭短裤','绿色','哥弟','100','1') +insert into GoodsInfo values ('无袖背心','白色','阿依莲','700','1') +insert into GoodsInfo values ('低帮休闲鞋','红色','菲曼琪','900','2') +insert into GoodsInfo values ('中跟单鞋','绿色','哥弟','400','2') +insert into GoodsInfo values ('平底鞋','白色','阿依莲','200','2') +insert into GoodsInfo values ('迷你照相机','红色','尼康','500','3') +insert into GoodsInfo values ('硬盘','黑色','希捷','600','3') +insert into GoodsInfo values ('显卡','黑色','技嘉','800','3') + + +select * from GoodsInfo +select * from GoodsType +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 名称,GoodsColor 颜色,GoodsMoney 价格 from GoodsInfo +where GoodsMoney in (select max(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select TypeName,avg(GoodsMoney)平均价格 from GoodsType A inner join GoodsInfo B +on A.TypeID = B.TypeID group by TypeName + +--7、查询每种颜色的商品数 +select GoodsColor,count(GoodsColor)数量 from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add GoodsRemark text +update GoodsType set GoodsRemark='我是商品类型信息备注' +select * from GoodsType + +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop FK_GoodsInfo_TypeID + +--10、删除所有商品类型记录 +delete GoodsType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-HOUSE_DB2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-HOUSE_DB2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..835d755de20e3db81b0f7326c52786c11f5c0459 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-HOUSE_DB2.sql" @@ -0,0 +1,89 @@ +use master +go +create database HOUSE_DB2 +on +( + name='HOUSE_DB2', + filename='D:\SQL作业\SQL作业14\HOUSE_DB2.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) + +log on +( + name='HOUSE_DB2_log', + filename='D:\SQL作业\SQL作业14\HOUSE_DB2_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +go + +use HOUSE_DB2 +go +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique(type_name) not null +) +go + + +use HOUSE_DB2 +go +create table HOUSE +( + house_id int primary key identity(1,1) not null, + house_name varchar(50) not null, + house_price float default(0) not null, + type_id int constraint FK_HOUSE_type_id references HOUSE_TYPE(type_id) +) +go + + +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') + +insert into HOUSE values('---','500','1'),('***','800','2'),('青青草原','1000','3') + +select * from HOUSE_TYPE +select * from HOUSE + + +--添加查询 +--查询所有房屋信息 +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋名称,house_price 租金 from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE +inner join HOUSE_TYPE on HOUSE.type_id = HOUSE_TYPE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_name 房屋名称,house_price 租金 from HOUSE where house_price in +(select top 1 house_price from HOUSE order by house_price desc) + +select top 1 house_price from HOUSE order by house_price desc + +--查询每种房屋类型的房屋数量 +select type_id 类型,count(type_id)数量 from HOUSE group by type_id + +--修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +select * from HOUSE +alter table HOUSE alter column house_name nvarchar(30) + +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +select * from HOUSE +alter table HOUSE add address text + +--把房屋价格的默认值0改为100 +alter table HOUSE drop DF_HOUSE_house_price +alter table HOUSE add constraint DF default(100) for house_price + +--查询出名字最长的房屋信息. +select * from HOUSE where len(house_name) = (select max(len(house_name)) from HOUSE) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-StarManagerDB.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-StarManagerDB.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0104eefa89425ea8711768b6f52aa9f0a39aaa8b --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-StarManagerDB.sql" @@ -0,0 +1,94 @@ +use master +go +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\SQL作业\SQL作业14\StarManagerDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='StarManagerDB_log', + filename='D:\SQL作业\SQL作业14\StarManagerDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20) +) +go + +use StarManagerDB +go +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int constraint FK_StarInfo_S_NATIVE references StarType(T_NO) +) +go + +insert into StarType values('体育明星'),('IT明星'),('相声演员') + +insert into StarInfo values('梅西','30','射门','阿根廷','1'),('科比','35','过人','美国','1'), +('蔡景现','40','敲代码','中国','2'),('马斯克','36','造火箭','外星人','2'), +('郭德纲','50','相声','中国','3'),('黄铮','41','拼多多','中国','2') + + +select * from StarInfo +select * from StarType + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_AGE from StarInfo order by S_AGE desc +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_AGE in (select top 3 S_AGE from StarInfo order by S_AGE desc) + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 编号,count(*) 人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select T_NAME 类型,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo +inner join StarType on StarInfo.S_T_NO = StarType.T_NO +where S_AGE in (select top 1 S_AGE from StarInfo inner join StarType on StarInfo.S_T_NO = StarType.T_NO where T_NAME='体育明星' order by S_AGE desc) + +--6、请统计每种明星类型的人数 +select T_NAME,count(S_T_No)人数 from StarType A inner join StarInfo B +on A.T_NO = B.S_T_NO group by T_NAME + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add income int +alter table StarInfo add constraint DF_StarInfo_income default(10000000) for income + +--8、查询每个国家的明星人数。 +select S_NATIVE 国家,count(S_NATIVE)人数 from StarInfo group by S_NATIVE + +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select distinct A.*,B.T_NAME from StarInfo A inner join StarType B +on A.S_T_NO = B.T_NO order by S_NATIVE,S_AGE desc + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select A.*,T_NAME from StarInfo A inner join StarType B +on A.S_T_NO = B.T_NO where S_NAME like '_西' + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add CreateTime datetime +alter table StarInfo add constraint DK_StarInfo_CreateTime default(getdate()) for CreateTime + +alter table StarType add CreTime datetime +alter table StarType add constraint DK_StarType_CreTime default(getdate()) for CreTime + +select * from StarInfo +select * from StarType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\270\200.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\270\200.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e1f305c1bc8b6140c9449ce7a2ceb9cfa499cefe --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\270\200.sql" @@ -0,0 +1,53 @@ +create database GoodsDB +go +use GoodsDB +go +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null, +) + +insert into GoodsType(TypeName) values('服装内衣'),('鞋包配饰'),('手机数码') +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID), +) +alter table GoodsInfo add constraint FK_TypeID foreign key(TypeID) references GoodsType(TypeID) +select * from GoodsType +select * from GoodsInfo +insert into GoodsInfo values('提花小西装','红色','菲曼琪','300',1),('百搭短裤', '绿色', '哥弟', '100', 1),('无袖背心' ,'白色' ,'阿依莲' ,'700' ,1), +('低帮休闲鞋',' 红色 ','菲曼琪',' 900', 2),('中跟单鞋',' 绿色',' 哥弟' ,'400', 2),('平底鞋',' 白色',' 阿依莲',' 200', 2),('迷你照相机', '红色',' 尼康', '500', 3), +('硬盘',' 黑色', '希捷', '600', 3),('显卡' ,'黑色',' 技嘉', '800', 3) +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select TypeID,MAX(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' or GoodsMoney=300 and GoodsMoney=600 + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select * from GoodsInfo +select GoodsInfo.TypeID,TypeName 类别名称,avg(GoodsMoney)平均价格 from GoodsInfo +inner join GoodsType on GoodsType.TypeID=GoodsInfo.TypeID group by GoodsInfo.TypeID,TypeName + +--7、查询每种颜色的商品数 +select GoodsColor 颜色,COUNT(*)颜色数量 from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add name varchar(20) default('我是商品类型信息备注') not null + +--在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop FK_TypeID + +--10、删除所有商品类型记录 + +delete from GoodsType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\270\211.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\270\211.sql" new file mode 100644 index 0000000000000000000000000000000000000000..40a2ca212070e7221fdc0458879943b1b74684d1 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\270\211.sql" @@ -0,0 +1,62 @@ +create database StarManagerDB +go +use StarManagerDB +go + +--明星类型表 +create table StarType +( + T_NO int not null identity(1,1) primary key, + T_NAME nvarchar(20), +) + +--明星信息表 +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO), +) +select * from StarType +select * from StarInfo +insert into StarType values ('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values ('梅西',30,'射门','阿根廷',1), ('科比',35,'过人','美国',1), ('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铎',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_AGE,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 类型编号,count(*)明星人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_AGE,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_T_NO=1 order by S_AGE desc + +--6、请统计每种明星类型的人数 +select * from StarInfo +select S_T_NO,T_NAME 明星类型, count(*)明星人数 from StarInfo +inner join StarType ON StarInfo.S_T_NO=StarType.T_NO group by S_T_NO,T_NAME + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add NSR int not null default(10000000) + +--8、查询每个国家的明星人数。 +select * from StarType +select * from StarInfo +select S_NATIVE 国家名称, count(*)国家总数 from StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO group by S_NATIVE + +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select * from StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO order by S_AGE desc + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO where S_NAME like '_西' + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarType add time datetime default('2021-4-9 9:30') +alter table StarInfo add time datetime default('2021_4_9 9:30') \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\272\214.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\272\214.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2473b66759313f95eb624ef441aeefc52ea4faa8 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\244\215\344\271\240\344\272\214.sql" @@ -0,0 +1,73 @@ +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='C:\text\HOUSE_DB.maf', + size=5, + maxsize=50, + filegrowth=1% +) +log on +( + name='HOUSE_DB_log', + filename='C:\text\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1% +) +go +use HOUSE_DB +go + +--房屋类型表 +create table HOUSE_TYPE +( + type_id int not null primary key identity, + type_name varchar(50) unique, +) + +--房屋信息表 +create table HOUSE +( + house_id int not null primary key identity, + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') + +insert into HOUSE values ('爱丽小屋',10000000,1),('毛坯房',100,2),('黄子韬',100000,3) + +--查询所有房屋信息 +select * from HOUSE_TYPE +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 出租屋名称,house_price 出租租金 from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,HOUSE_TYPE.type_name 房屋类型 from HOUSE inner join HOUSE_TYPE on +HOUSE.type_id=HOUSE_TYPE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + --order by + top +select top 1 house_price 房屋租金,house_name 房屋名称 from HOUSE order by house_price desc + +--查询每种房屋类型的房屋数量 +select house_name,count(*) 房屋数量 from HOUSE group by house_name + +--修改房屋表的房屋名称字段,把数据类型改成nvarchar(30) +sp_rename 'HOUSE.house_name','name' +alter table HOUSE alter column name nvarchar(30)not null +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add house_dizhi varchar(30) + +-- 把房屋价格的默认值0改为100 +alter table HOUSE modify house_price default 100 + +--查询出名字最长的房屋信息。 +select HOUSE.* from HOUSE where +len(name)=(select max(len(name)) from HOUSE) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\345\244\215\344\271\240\351\242\230123.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\345\244\215\344\271\240\351\242\230123.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a28f12c623fa5675ba00f8087834c5f8c273022e --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/\345\244\215\344\271\240\351\242\230123.sql" @@ -0,0 +1,53 @@ +--复习题1 + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select GT.TypeName,AVG(GoodsMoney)平均价格 from GoodsInfo GI join GoodsType GT on GI.TypeID=GT.TypeID group by GT.TypeName + +--7、查询每种颜色的商品数 +select GoodsColor,COUNT(*)商品数 from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add postscript text default('我是商品类型信息备注') + +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop FK__GoodsInfo__TypeI__173876EA + +--10、删除所有商品类型记录 +delete from GoodsType where TypeName=TypeName + +--复习题2 +--查询每种房屋类型的房屋数量 +select type_id,COUNT(*)数量 from HOUSE group by type_id + +--修改房屋类型表的房屋名称字段,把数据类型改成nvarchar(30) +alter table HOUSE_TYPE alter column type_name nvarchar(30) + +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add site nvarchar(30) + +--把房屋价格的默认值0改为100 +alter table HOUSE add house_price cdefault(100) + +--查询出名字最长的房屋信息 +select house_name from HOUSE where len(house_name)=(select max(len(house_name)) from HOUSE) + + +--复习题3 +--6、请统计每种明星类型的人数 +select S_T_NO,COUNT(*)人数 from StarInfo group by S_T_NO + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add AnnualIncome nchar(20) default(10000000) + +--8、查询每个国家的明星人数。 +select S_NATIVE,COUNT(*)人数 from StarInfo group by S_NATIVE + +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select * from StarInfo join StarType on S_T_NO=T_NO order by S_NAME ,S_AGE + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarInfo where S_NAME like '__' and S_NAME like '%西%' + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add CreationTime datetime default(getdate()) +alter table StrType add CreationTime datetime default(getdate()) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" new file mode 100644 index 0000000000000000000000000000000000000000..71546008176fbd74096d2053613c6e313f6390dd --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" @@ -0,0 +1,104 @@ + +create database Jxc + +on +( + name='Jxc', + filename='D:\Jxc.mdf' +) +log on +( + name='Jxc_log', + filename='D:\Jxc.ldf_log' +) +use Jxc +create table stuInfo +( + stuID int primary key , + stuName varchar(20), + stuAge int , + stuSex nvarchar(1), + stime datetime +) + +create table courseInfo +( + courseID int primary key , + courseName varchar(25), + courseMarks varchar(5) +) +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int foreign key references stuInfo(stuID) , + courseID int foreign key references courseInfo(courseID), + score int +) +--学生信息表 +insert into stuInfo(stuID,stuName,stuAge,stuSex) +select 1,'Tom',19,1 union +select 2,'Jack',20,0union +select 3,'Rose',21,1union +select 4,'Lulu',19,1 union +select 5,'Lili',21,0union +select 6,'abc',20,1 + +update stuInfo set stime='2007-01-07 01:11:36.590'where stuName='abc' +--课程表 +insert into courseInfo(courseID,courseName,courseMarks) +select 1,'JavaBase',4 union +select 2,'HTML',2 union +select 3,'JavaScript',2 union +select 4,'SqlBase',2 +-- 成绩表 +insert into scoreInfo(stuID,courseID,score) +select 1,1,80 union +select 1,2,85 union +select 1,4,50 union +select 2,1,75 union +select 2,3,45 union +select 2,4,75 union +select 3,1,45 union +select 4,1,95 union +select 4,2,75 union +select 4,3,90 union +select 4,4,45 + +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +select * from stuInfo --学生信息表 +select * from courseInfo --课程表 +select * from scoreInfo -- 成绩表 +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuID,count(courseID)课程的数量,avg(score)平均分 from scoreInfo group by stuID +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseID 课程, count(courseID)选修个数, sum(score)总分 from scoreInfo group by courseID + +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from stuInfo A, stuInfo B +where A.stuSex=B.stuSex AND A.stuAge=B.stuAge AND A.stuID!=B.stuID +--4.查询出学分一样的课程信息 +select distinct A.* from courseInfo A,courseInfo B +where A.courseMarks=B.courseMarks AND A.courseID!=B.courseID +--5.查询出参加了 考试的学生的学号,姓名,课程号和分数 +select scoreInfo.stuID 学号,stuName 姓名,courseID 课程号,score from scoreInfo +left join stuInfo on scoreInfo.stuID=stuInfo.stuID group by scoreInfo.stuID,stuName,courseID,score +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select scoreInfo.stuID 学号, scoreInfo.courseID 课程号,courseInfo.courseName 课程名,courseMarks 学分,scoreInfo.score 分数 from scoreInfo +left join courseInfo on scoreInfo.courseID=courseInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select Stuinfo.stuID 学号,stuInfo.stuName 姓名 from stuInfo +left join scoreInfo on stuInfo.stuID=scoreInfo.stuID +where score is null +--8.查询出是周六周天来报到的学生 + --超纲了!!!!!!!!!!!!!!!! +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出 选修了2门课程 以上的 并且 考试平均分 在70以上 的学生的 学号 和 考试平均分 以及 选修课程的数量 +select stuID,count(courseID)课程的数量,avg(score)平均分 from scoreInfo +group by stuID +having avg(score)>=70 and count(courseID)>=2 + +select * from courseInfo --课程表 +select * from scoreInfo -- 成绩表 +select * from stuInfo --学生信息表 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..42e706e159834088a9e0de43bf735a023c738e77 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy2.sql" @@ -0,0 +1,77 @@ +use master +go +create database market +on( + name='market', + filename='D:\cs\market.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on( + name='market_log', + filename='D:\cs\market_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use market +go +create table GoodsType( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) +create table GoodsInfo( + GoodsID INT primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) +insert into GoodsType (TypeName) +values +('服装内衣'), + ('鞋包配饰'), +( '手机数码') +select * from GoodsType + + insert into GoodsInfo + values ('提花小西装','红色','菲曼琪','300',1), + ('百搭短裤','绿色','哥弟','100',1), + ('无心背心','白色','阿依莲','700',1), + ('低帮休闲鞋','红色','菲曼琪','900',2), + ('中跟单鞋','绿色','哥弟','400',2), + ('平底鞋','白色','阿依莲','200',2), + ('迷你照相机','红色','尼康','500',3), + ('硬盘','黑色','希捷','600',3), + ('显卡','黑色','技嘉','800',3) + + select*from GoodsInfo + select * from GoodsType +-- 3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsMoney 价格,GoodsName 商品名称,GoodsColor 商品颜色,max(GoodsMoney)价格 + from GoodsInfo group by GoodsName ,GoodsColor,GoodsMoney order by GoodsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 编号,max( GoodsMoney)最高价格,min( GoodsMoney)最低价格,avg( GoodsMoney)平均价格 from GoodsInfo +group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo where GoodsColor like '红色%' and GoodsMoney>=300 and GoodsMoney<=600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select b.TypeName 商品的类别,round(avg(a.GoodsMoney),2)平均价格 from GoodsInfo a inner join GoodsType b on a.TypeID=b.TypeID +group by TypeName +--7、查询每种颜色的商品数 +select GoodsColor,count(GoodsColor)商品数 from GoodsInfo +group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add remark text +update GoodsType set remark='我是商品类型信息备注' + +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop FK__GoodsInfo__TypeI__1273C1CD +--10、删除所有商品类型记录 +delete GoodsType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d8436da183ecd9413ed3aed6359eb4c388334e69 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy3.sql" @@ -0,0 +1,55 @@ +create database HOUSE +on +( + name='HOUSE', + filename='D:\fz\HOUSE.mdf', + size=10mb, + filegrowth=2mb + +) +log on +( + name='HOUSE_log', + filename='D:\fz\HOUSE_log.ldf', + size=10mb, + filegrowth=2mb + + +) +use HOUSE +go +create table HOUSE_TYPE +( + typeid int not null primary key identity(1,1) , + typename varchar(50) not null unique , + +) +create table HOUSE +( + houseid int not null primary key identity , + housename varchar(50) not null, + houseprice float not null default('0'), + typeid int not null foreign key(typeid)references HOUSE_TYPE(typeid) + +) +insert into HOUSE_TYPE(typename)values('小型户'),('经济型'),('别墅') +select * from HOUSE_TYPE +insert into HOUSE(housename,houseprice,typeid)values('碧桂园',6000,1), +('好利园',5000,1), +('家园',4000,2), +('华人园',3000,3), +('老家园',2000,3) +select * from HOUSE +--查询所有房屋信息 +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE + +--查询出房屋的名称和租金,并且按照租金降序排序 +select housename,houseprice from HOUSE + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select housename,typename from HOUSE inner join HOUSE_TYPE on HOUSE.typeid=HOUSE_TYPE.typeid + + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 max(houseprice) 房租,housename from HOUSE group by housename,houseprice order by houseprice desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a32ef2940571e50d4ae777e08f646ba7b77183a1 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" @@ -0,0 +1,75 @@ +use master +go +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\HOUSE_DB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_log', +filename='D:\HOUSE_DB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +go + +use HOUSE_DB +go + +create table HOUSE_TYPE +( +type_id int not null primary key identity(1,1), +type_name varchar(50) not null unique +) +create table HOUSE +( +house_id int not null primary key identity(1,1), +house_name varchar(50) not null, +house_price float not null default(0), +type_id int foreign key references HOUSE_TYPE(type_id) not null +) + +insert into HOUSE_TYPE values +('小户型'),('经济型'),('别墅') + + +insert into HOUSE values ('小户型',1000.0,1),('经济型',2000.5,2),('别墅',4000.9,3) + + + +select * from HOUSE_TYPE +select * from HOUSE + + +--查询所有房屋信息 +select * from HOUSE_TYPE +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE where house_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select b.house_name,a.type_name from HOUSE_TYPE a inner join HOUSE b on a.type_id=b.house_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price,house_name from HOUSE order by house_price desc +-- 查询每种房屋类型的房屋数量 +select count(*) 数量,type_name 房屋类型 from HOUSE_TYPE group by type_name +-- 修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +alter table HOUSE alter column house_name nvarchar(30) +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add 地址 text +--把房屋价格的默认值0改为100 +alter table HOUSE drop constraint DF_ADSASD +alter table HOUSE add constraint DF_ADSASD default(100)for house_price +--查询出名字最长的房屋信息。 +select house_name,LEN(house_name) from HOUSE WHERE LEN(house_name) =(select max(len(house_name)) from HOUSE) + diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8c6c8b7e139466f9db203235af0048c29403c2ac --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" @@ -0,0 +1,77 @@ +use master +go +create database StarManagerDB +on +( +name='StarManagerDB', +filename='D:\StarManagerDB.mdf', +size=5mb, +filegrowth=10mb, +maxsize=50mb +) +log on +( +name='StarManagerDB_log', +filename='D:\StarManagerDB_log.ldf', +size=5mb, +filegrowth=10mb, +maxsize=50mb +) +go + +use StarManagerDB +go + +create table StarType +( +T_NO int not null primary key identity(1,1), +T_NAME nvarchar(20) +) +create table StarInfo +( +S_NO int not null primary key identity(1,1), +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_HATIVE nvarchar(20) default('中国大陆'), +S_T_NO int foreign key references StarType(T_NO) +) + +insert into StarType values +('体育明星'),('IT明星'),('相声演员') + +insert into StarInfo values +('梅西','30','射门','阿根廷','1'), +('科比','35','过人','美国','1'), +('蔡景现','40','敲代码','中国','2'), +('马斯克','36','造火箭','外星人','2'), +('郭德纲','50','相声','中国','3'), +('黄铮','41','拼多多','中国','2') + + +select * from StarInfo +select * from StarType +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HATIVE 籍贯信息,S_HOBBY 特技,max(S_AGE) from StarInfo group by S_NAME,S_HATIVE,S_HOBBY +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select count(S_NO) 人数,avg(S_AGE) 平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大 的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 max(S_AGE)年龄,S_NAME 姓名,S_HOBBY 特技,S_HATIVE 籍贯信息 from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO WHERE B.T_NAME='体育明星' GROUP BY S_NAME,S_HOBBY,S_HATIVE +--6、请统计每种明星类型的人数 +select count(*),b.T_NAME from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO group by b.T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +select * from StarInfo +alter table StarInfo add 年收入 int +alter table StarInfo add constraint dawda default(10000000) for 年收入 +--8、查询每个国家的明星人数。 +select S_HATIVE 人数,count(*) from StarInfo group by S_HATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 + +select * from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO order by S_HATIVE,S_AGE +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO WHERE S_NAME LIKE '_西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add 创建时间 datetime +alter table StarType add 创建时间 datetime +alter table StarType add constraint dadwadaw default(getdate()) for 创建时间 +alter table StarInfo add constraint dwada default(getdate()) for 创建时间 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c7069decf65b6d8058023763b73e670ecd8a04eb --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery3.sql" @@ -0,0 +1,57 @@ +use master +go +create database GoodsDB +go +use GoodsDB +go +create table GoodsType +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null +) +create table GoodsInfo +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20) not null, +GoodsMoney money not null, +TypeID int foreign key references GoodsType(TypeID) +) +insert into GoodsType +select '服装内衣' union +select '鞋包配饰' union +select '手机数码' +select * from GoodsType +insert into GoodsInfo +select '提花小西装','红色','菲',300,1 union +select '百搭短裤','绿色','哥弟',100,1 union +select '无袖背心','白色','白色',700,1 union +select '低帮休闲鞋','红色','菲曼琪',900,2 union +select '中跟单鞋','绿色','哥弟',400,2 union +select '平底鞋','白色','阿依莲',200,2 union +select '迷你照相机','红色','尼康',500,3 union +select '硬盘','黑色','希捷',600,3 union +select '显卡','黑色','技嘉',800,3 +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 名称, max(GoodsMoney) 价格,GoodsColor 颜色 from GoodsInfo group by GoodsName ,GoodsColor, GoodsMoney order by GoodsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格, avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor = '红色' and GoodsMoney between 300 and 600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select b.TypeName 类别名称,avg(GoodsMoney)平均价格 from GoodsInfo a join GoodsType b on a.TypeID=b.TypeID group by b.TypeName +--7、查询每种颜色的商品数 +select GoodsColor,count(*)数 from GoodsInfo group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add 备注 nvarchar(200) +insert into GoodsType (备注) +select '我是商品类型信息备注' union +select '我是商品类型信息备注' union +select '我是商品类型信息备注' +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop constraint FK__GoodsInfo__TypeI__1273C1CD +--10、删除所有商品类型记录 +delete from GoodsType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\346\210\277\345\261\213.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\346\210\277\345\261\213.sql" new file mode 100644 index 0000000000000000000000000000000000000000..732b4f23bc610f1b2cbde862ec92bc6d23e893cd --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\346\210\277\345\261\213.sql" @@ -0,0 +1,87 @@ +use master +go +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\HOUSE_DB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_log', +filename='D:\HOUSE_DB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +go + +use HOUSE_DB +go + +create table HOUSE_TYPE +( +type_id int not null primary key identity(1,1), +type_name varchar(50) not null unique +) +create table HOUSE +( +house_id int not null primary key identity(1,1), +house_name varchar(50) not null, +house_price float not null default(0), +type_id int foreign key references HOUSE_TYPE(type_id) not null +) + +insert into HOUSE_TYPE values +('小户型'),('经济型'),('别墅') + +insert into HOUSE values +('小户','5000','1'), +('经济','3000','2'), +('别墅','8000','3') + + + + + +select * from HOUSE_TYPE +select * from HOUSE + + +--查询所有房屋信息 +select * from HOUSE_TYPE inner join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋名称, house_price 租金 from HOUSE order by house_price + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类别 from HOUSE inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_name 房屋名称, max(house_price) 租金 from HOUSE group by house_name + + + + + +select * from HOUSE_TYPE +select * from HOUSE + +--查询每种房屋类型的房屋数量 +select type_name 房屋类别,count(*) from HOUSE_TYPE inner join HOUSE on HOUSE_TYPE.type_id= HOUSE.type_id group by type_name +--修改房屋表的房屋名称字段,把数据类型改成nvarchar(30) +alter table HOUSE alter column house_name nvarchar(30) +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add 地段 varchar(100) +--把房屋价格的默认值0改为100 +alter table house drop DF__HOUSE__house_pri__276EDEB3 +alter table house add CONSTRAINT DF default(100) for house_price +--查询出名字最长的房屋信息。 +select top 1 ouse_id,house_name,house_price,type_id,dizhi, len(house_name)名字长度 from HOUSE +group by ouse_id ,house_name,house_price,type_id,dizhi order by house_name desc diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\346\230\216\346\230\237.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\346\230\216\346\230\237.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2dad800877380e7cdd0b1dfff50e980563d669ce --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\346\230\216\346\230\237.sql" @@ -0,0 +1,87 @@ +use master +go +create database StarManagerDB +on +( +name='StarManagerDB', +filename='D:\StarManagerDB.mdf', +size=5mb, +filegrowth=10mb, +maxsize=50mb +) +log on +( +name='StarManagerDB_log', +filename='D:\StarManagerDB_log.ldf', +size=5mb, +filegrowth=10mb, +maxsize=50mb +) +go + +use StarManagerDB +go + +create table StarType +( +T_NO int not null primary key identity(1,1), +T_NAME nvarchar(20) +) +create table StarInfo +( +S_NO int not null primary key identity(1,1), +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_HATIVE nvarchar(20) default('中国大陆'), +S_T_NO int foreign key references StarType(T_NO) +) + +insert into StarType values +('体育明星'),('IT明星'),('相声演员') + +insert into StarInfo values +('梅西','30','射门','阿根廷','1'), +('科比','35','过人','美国','1'), +('蔡景现','40','敲代码','中国','2'), +('马斯克','36','造火箭','外星人','2'), +('郭德纲','50','相声','中国','3'), +('黄铮','41','拼多多','中国','2') + +select * from StarType + +select * from StarInfo + + + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_HATIVE 籍贯,max(S_AGE) 年龄 from StarInfo group by S_NAME,S_HOBBY,S_HATIVE + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 编号, count(*) 人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(*)>2 + + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_HOBBY 特技,S_HATIVE 籍贯, max(S_AGE)年龄 from StarType inner join StarInfo on StarType.T_NO=StarInfo.S_T_NO +where T_NAME='体育明星'group by S_NAME,S_HOBBY,S_HATIVE + + + + +select * from StarType +select * from StarInfo + +--6、请统计每种明星类型的人数 +select T_NAME 明星类型,count(*)人数 from StarType inner join StarInfo on StarType.T_NO=StarInfo.S_T_NO group by T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add 年收入 varchar(100) +--8、查询每个国家的明星人数。 +select S_HATIVE 国家,count(*) from StarType inner join StarInfo on StarType.T_NO=StarInfo.S_T_NO group by S_HATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select * from StarType inner join StarInfo on StarType.T_NO=StarInfo.S_T_NO +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select *,T_NAME from StarType inner join StarInfo on StarType.T_NO=StarInfo.S_T_NO where S_NAME like '_西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarType add 创建时间 datetime default(getdate()) +alter table StarInfo add 创建时间 datetime default(getdate()) + diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\350\241\243\347\211\251.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\350\241\243\347\211\251.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e25962705cadb8ab448fa5cdf514e77546a6dfd0 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\350\241\243\347\211\251.sql" @@ -0,0 +1,92 @@ +USE master +GO +CREATE DATABASE GoodsDB +on +( +name='wq', +filename='D:\wq.mdf', +size=5mb +) +log on +( +name='wq_log', +filename='D:\wq_log.ldf', +size=5mb +) +GO +use GoodsDB +GO +CREATE TABLE GoodsType +( +TypeID INT NOT NULL IDENTITY(1,1), +TypeName NVARCHAR(20) NOT NULL +) +GO +ALTER TABLE GoodsType add constraint DK_GoodsType_TYPEID PRIMARY KEY(TYPEID) +SELECT * FROM GoodsType +INSERT INTO GoodsType(TypeName) +values('服装内衣'), +('鞋包配饰'), +('手机数码') + +CREATE TABLE GoodsInfo +( +GoodsID INT NOT NULL IDENTITY(1,1), +GoodsName NVARCHAR(20) NOT NULL, +GoodsColor NVARCHAR(20) NOT NULL , +GoodsBrand NVARCHAR(20) , +GoodsMoney MONEY NOT NULL, +TypeID INT +) +GO +ALTER TABLE GoodsInfo ADD CONSTRAINT GK_GoodsInfo_GoodsID primary key(GoodsID) +ALTER TABLE GoodsInfo ADD CONSTRAINT JK_GoodsInfo_TypeID FOREIGN KEY(TYPEID) references GoodsType(TypeID) +SELECT * FROM GoodsInfo +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +values ('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100 ,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) + + +select * from GoodsType +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +SELECT top 1 GoodsName 名称,GoodsColor 颜色,max(GoodsMoney) 价格 FROM GoodsInfo group by GoodsName,GoodsColor order by max(GoodsMoney) desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select TypeID 编号,MAX(GoodsMoney)最高,min(GoodsMoney)最低,avg(GoodsMoney)平均 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + + +select * from GoodsType +select * from GoodsInfo + + + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select TypeName 商品类别,GoodsName 商品名称,AVG(GoodsMoney)平均价格 from GoodsType inner join GoodsInfo +on GoodsType.TypeID=GoodsInfo.TypeID group by TypeName,GoodsName + +--7、查询每种颜色的商品数 +select GoodsColor 商品颜色, count(TypeID) 商品数 from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add 备注 varchar(100) text +update GoodsType set remark='我是商品类型信息备注' +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop constraint JK_GoodsInfo_TypeID + +--10、删除所有商品类型记录 +delete from GoodsType diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a32ef2940571e50d4ae777e08f646ba7b77183a1 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" @@ -0,0 +1,75 @@ +use master +go +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\HOUSE_DB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_log', +filename='D:\HOUSE_DB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +go + +use HOUSE_DB +go + +create table HOUSE_TYPE +( +type_id int not null primary key identity(1,1), +type_name varchar(50) not null unique +) +create table HOUSE +( +house_id int not null primary key identity(1,1), +house_name varchar(50) not null, +house_price float not null default(0), +type_id int foreign key references HOUSE_TYPE(type_id) not null +) + +insert into HOUSE_TYPE values +('小户型'),('经济型'),('别墅') + + +insert into HOUSE values ('小户型',1000.0,1),('经济型',2000.5,2),('别墅',4000.9,3) + + + +select * from HOUSE_TYPE +select * from HOUSE + + +--查询所有房屋信息 +select * from HOUSE_TYPE +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE where house_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select b.house_name,a.type_name from HOUSE_TYPE a inner join HOUSE b on a.type_id=b.house_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price,house_name from HOUSE order by house_price desc +-- 查询每种房屋类型的房屋数量 +select count(*) 数量,type_name 房屋类型 from HOUSE_TYPE group by type_name +-- 修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +alter table HOUSE alter column house_name nvarchar(30) +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add 地址 text +--把房屋价格的默认值0改为100 +alter table HOUSE drop constraint DF_ADSASD +alter table HOUSE add constraint DF_ADSASD default(100)for house_price +--查询出名字最长的房屋信息。 +select house_name,LEN(house_name) from HOUSE WHERE LEN(house_name) =(select max(len(house_name)) from HOUSE) + diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8c6c8b7e139466f9db203235af0048c29403c2ac --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" @@ -0,0 +1,77 @@ +use master +go +create database StarManagerDB +on +( +name='StarManagerDB', +filename='D:\StarManagerDB.mdf', +size=5mb, +filegrowth=10mb, +maxsize=50mb +) +log on +( +name='StarManagerDB_log', +filename='D:\StarManagerDB_log.ldf', +size=5mb, +filegrowth=10mb, +maxsize=50mb +) +go + +use StarManagerDB +go + +create table StarType +( +T_NO int not null primary key identity(1,1), +T_NAME nvarchar(20) +) +create table StarInfo +( +S_NO int not null primary key identity(1,1), +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_HATIVE nvarchar(20) default('中国大陆'), +S_T_NO int foreign key references StarType(T_NO) +) + +insert into StarType values +('体育明星'),('IT明星'),('相声演员') + +insert into StarInfo values +('梅西','30','射门','阿根廷','1'), +('科比','35','过人','美国','1'), +('蔡景现','40','敲代码','中国','2'), +('马斯克','36','造火箭','外星人','2'), +('郭德纲','50','相声','中国','3'), +('黄铮','41','拼多多','中国','2') + + +select * from StarInfo +select * from StarType +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HATIVE 籍贯信息,S_HOBBY 特技,max(S_AGE) from StarInfo group by S_NAME,S_HATIVE,S_HOBBY +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select count(S_NO) 人数,avg(S_AGE) 平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大 的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 max(S_AGE)年龄,S_NAME 姓名,S_HOBBY 特技,S_HATIVE 籍贯信息 from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO WHERE B.T_NAME='体育明星' GROUP BY S_NAME,S_HOBBY,S_HATIVE +--6、请统计每种明星类型的人数 +select count(*),b.T_NAME from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO group by b.T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +select * from StarInfo +alter table StarInfo add 年收入 int +alter table StarInfo add constraint dawda default(10000000) for 年收入 +--8、查询每个国家的明星人数。 +select S_HATIVE 人数,count(*) from StarInfo group by S_HATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 + +select * from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO order by S_HATIVE,S_AGE +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO WHERE S_NAME LIKE '_西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add 创建时间 datetime +alter table StarType add 创建时间 datetime +alter table StarType add constraint dadwadaw default(getdate()) for 创建时间 +alter table StarInfo add constraint dwada default(getdate()) for 创建时间 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c7069decf65b6d8058023763b73e670ecd8a04eb --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery3.sql" @@ -0,0 +1,57 @@ +use master +go +create database GoodsDB +go +use GoodsDB +go +create table GoodsType +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null +) +create table GoodsInfo +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20) not null, +GoodsMoney money not null, +TypeID int foreign key references GoodsType(TypeID) +) +insert into GoodsType +select '服装内衣' union +select '鞋包配饰' union +select '手机数码' +select * from GoodsType +insert into GoodsInfo +select '提花小西装','红色','菲',300,1 union +select '百搭短裤','绿色','哥弟',100,1 union +select '无袖背心','白色','白色',700,1 union +select '低帮休闲鞋','红色','菲曼琪',900,2 union +select '中跟单鞋','绿色','哥弟',400,2 union +select '平底鞋','白色','阿依莲',200,2 union +select '迷你照相机','红色','尼康',500,3 union +select '硬盘','黑色','希捷',600,3 union +select '显卡','黑色','技嘉',800,3 +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 名称, max(GoodsMoney) 价格,GoodsColor 颜色 from GoodsInfo group by GoodsName ,GoodsColor, GoodsMoney order by GoodsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格, avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor = '红色' and GoodsMoney between 300 and 600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select b.TypeName 类别名称,avg(GoodsMoney)平均价格 from GoodsInfo a join GoodsType b on a.TypeID=b.TypeID group by b.TypeName +--7、查询每种颜色的商品数 +select GoodsColor,count(*)数 from GoodsInfo group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add 备注 nvarchar(200) +insert into GoodsType (备注) +select '我是商品类型信息备注' union +select '我是商品类型信息备注' union +select '我是商品类型信息备注' +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop constraint FK__GoodsInfo__TypeI__1273C1CD +--10、删除所有商品类型记录 +delete from GoodsType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2301.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2301.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d66b4402a711ff43738dacb9958e29e69033dc13 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2301.sql" @@ -0,0 +1,93 @@ +use master +go +create database GoodsDB +on +( +name='GoodsDB', +filename='C:\SQL\GoodsDB.mdf', +size=5MB, +maxsize=50MB, +filegrowth=1MB +) +log on +( +name='GoodsDB_log', +filename='C:\SQL\GoodsDB_log.ldf', +size=5MB, +maxsize=50MB, +filegrowth=1MB +) +go +use GoodsDB +go +create table GoodsType--商品类型表 +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) +) +go +use GoodsDB +go +create table GoodsInfo--商品信息表 +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) +go +insert into GoodsType (TypeName) values ('服装内衣') +insert into GoodsType (TypeName) values ('鞋包配饰') +insert into GoodsType (TypeName) values ('手机数码') + +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('提花小西装','红色','菲曼琪','300','1') +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('百搭短裤','绿色','哥弟','100','1') +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('无袖背心','白色','阿依莲','700','1') +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('低帮休闲鞋','红色','菲曼琪','900','2') +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('中跟单鞋','绿色','哥弟','400','2') +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('平底鞋','白色','阿依莲','200','2') +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('迷你照相机','红色','尼康','500','3') +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('硬盘','黑色','希捷','600','3') +insert into GoodsInfo (GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values ('显卡','黑色','技嘉','800','3') + +select * from GoodsType--商品类型表 +select * from GoodsInfo--商品信息表 +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 名称,GoodsColor 颜色,GoodsMoney 价格 +from GoodsInfo +order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型,MAX(GoodsMoney) 最高价, MIN(GoodsMoney) 最低价,AVG(GoodsMoney) 平均价 +from GoodsInfo +group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsBrand 商品品牌,GoodsMoney 价格,TypeID 商品类型 +from GoodsInfo +where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 + +--6、查询每类商品的平均价格,要显示商品的类别名称(TypeName)和平均价格。 +select GoodsType.TypeName 类别名称,AVG(GoodsMoney) 平均价 +from GoodsType inner join GoodsInfo on GoodsType.TypeID=GoodsInfo.TypeID +group by TypeName + +--7、查询每种颜色的商品数 +select GoodsInfo.GoodsColor 颜色,count(GoodsColor) +from GoodsInfo +group by GoodsColor + +--8、给商品类型表添加一个“ 备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add TypeBZhu text +insert into GoodsType (TypeBZhu) values ('我是商品类型信息备注') +insert into GoodsType (TypeBZhu) values ('我是商品类型信息备注') +insert into GoodsType (TypeBZhu) values ('我是商品类型信息备注') + +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo +drop constraint FK_GoodsType_TypeID + +--10、删除所有商品类型记录 +alter table GoodsType drop column TypeName \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2302.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2302.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6cc682f1a43cc13d5775b1be5a2874f57235900f --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2302.sql" @@ -0,0 +1,92 @@ +use master +go +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='C:\SQL\HOUSE_DB.mdf', +size=5MB, +maxsize=50MB, +filegrowth=1MB +) +log on +( +name='HOUSE_DB_log', +filename='C:\SQL\HOUSE_DB_log.ldf', +size=5MB, +maxsize=50MB, +filegrowth=1MB +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( +Type_ID int primary key identity(1,1) not null, +Type_Name varchar(50) unique not null +) +go +use HOUSE_DB +go +create table HOUSE +( +House_ID int primary key identity(1,1) not null, +House_Name varchar(50) not null, +House_Price float default('0') not null, +Type_ID int references HOUSE_TYPE(Type_ID) not null +) +go +insert into HOUSE_TYPE (Type_Name) values('小户型') +insert into HOUSE_TYPE (Type_Name) values('经济型') +insert into HOUSE_TYPE (Type_Name) values('别墅') + +insert into HOUSE (House_Name,House_Price,Type_ID) values('蘑菇屋','3500',1) +insert into HOUSE (House_Name,House_Price,Type_ID) values('苹果屋','4000',1) +insert into HOUSE (House_Name,House_Price,Type_ID) values('大大屋','4500',2) +insert into HOUSE (House_Name,House_Price,Type_ID) values('小小屋','3500',2) +insert into HOUSE (House_Name,House_Price,Type_ID) values('一二三','4000',3) +insert into HOUSE (House_Name,House_Price,Type_ID) values('四五六','3800',3) + +--添加查询 +select * from HOUSE_TYPE--类型 +select * from HOUSE--房屋信息 +--1.查询所有房屋信息 +select * from HOUSE_TYPE +select * from HOUSE + +--2.使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where Type_Name like '%型%' + +--3.查询出房屋的名称和租金,并且按照租金降序排序 +select House_Name 名称,House_Price 租金 +from HOUSE +order by House_Price desc + +--4.使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select HOUSE_TYPE.Type_Name 房屋类型,HOUSE.House_Name 房屋名称 +from HOUSE inner join HOUSE_TYPE on HOUSE_TYPE.Type_ID=HOUSE.Type_ID + +--5.查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 House_Name 房屋名称,max(House_Price) 租金 +from HOUSE +group by House_Name,House_Price +order by House_Price desc + +--6.查询每种房屋类型的房屋数量 +select TYPE_ID 房屋类型编号,count(Type_ID) 数量 +from HOUSE +group by Type_ID + +--7.修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +alter table HOUSE drop column House_Name +alter table HOUSE add House_Name nvarchar(30) + +--8.给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add DiZhi text + +--9.把房屋价格的默认值0改为100 + +--10.查询出名字最长的房屋信息。 +select top 1 len(House_Name),House_Name,House_Price +from HOUSE +order by len(House_Name) desc diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2303.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2303.sql" new file mode 100644 index 0000000000000000000000000000000000000000..88387c64a4028c68a7b848ab6c4e8e4d92f3e8aa --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\345\244\215\344\271\240\351\242\2303.sql" @@ -0,0 +1,95 @@ +use master +go +create database StarManagerDB +on +( +name='StarManagerDB', +filename='C:\SQL\StarManagerDB.mdf', +size=5MB, +maxsize=50MB, +filegrowth=1MB +) +log on +( +name='StarManagerDB_log', +filename='C:\SQL\StarManagerDB_log.ldf', +size=5MB, +maxsize=50MB, +filegrowth=1MB +) +go +use StarManagerDB +go +create table StarType--明星类型表 +( +T_NO int primary key identity(1,1) not null, +T_NAME nvarchar(20) +) +go +use StarManagerDB +go +create table StarInfo--明星信息表 +( +S_NO int primary key identity(1,1) not null, +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int references StarType(T_NO) +) + +insert into StarType (T_NAME) values ('体育明星') +insert into StarType (T_NAME) values ('IT明星') +insert into StarType (T_NAME) values ('相声演员') + +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('梅西','30','射门','阿根廷',1) +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('科比','35','过人','美国',1) +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('蔡景现','40','敲代码','中国',2) +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('马斯克','36','造火箭','外星人',2) +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('郭德纲','50','相声','中国',3) +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('黄铮','41','拼多多','中国',2) + +select * from StarType--明星类型表 +select * from StarInfo--明星信息表 +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯,S_AGE 年龄 +from StarInfo +group by S_NAME,S_HOBBY,S_NATIVE,S_AGE +order by S_AGE desc + +--4、按 明星类型编号 分类查询 明星人数 ,明星平均年龄,显示 明星人数大于2的分组信息 ,要求使用别名显示列名。 +select S_T_NO 类型编号,count(S_NO) 明星人数,avg(S_AGE) 平均年龄 +from StarInfo +group by S_T_NO having count(S_NO)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯,S_AGE 年龄 +from StarInfo +where S_T_NO=1 +order by S_AGE desc + +--6、请统计每种明星类型的人数 +select S_T_NO 房屋类型编号,count(S_T_NO) 数量 +from StarInfo +group by S_T_NO + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add ShouRu int default('10000000') + +--8、查询每个国家的明星人数。 +select S_NATIVE 国家,count(S_NATIVE) 人数 +from StarInfo +group by S_NATIVE + +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select StarType.T_NAME 明星类型,StarInfo.S_NAME 明星姓名,StarInfo.S_HOBBY 特长,StarInfo.S_AGE 年龄,StarInfo.S_NATIVE 籍贯 +from StarInfo inner join StarType on StarInfo.S_T_NO=StarType.T_NO +order by StarInfo.S_AGE desc + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select StarInfo.*,StarType.T_NAME 类型 +from StarInfo inner join StarType on StarInfo.S_T_NO=StarType.T_NO +where S_NAME like '_西' + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add StarTime time default(getdate()) diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\345\244\215\344\271\240\351\242\230123.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\345\244\215\344\271\240\351\242\230123.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a28f12c623fa5675ba00f8087834c5f8c273022e --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\345\244\215\344\271\240\351\242\230123.sql" @@ -0,0 +1,53 @@ +--复习题1 + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select GT.TypeName,AVG(GoodsMoney)平均价格 from GoodsInfo GI join GoodsType GT on GI.TypeID=GT.TypeID group by GT.TypeName + +--7、查询每种颜色的商品数 +select GoodsColor,COUNT(*)商品数 from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add postscript text default('我是商品类型信息备注') + +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop FK__GoodsInfo__TypeI__173876EA + +--10、删除所有商品类型记录 +delete from GoodsType where TypeName=TypeName + +--复习题2 +--查询每种房屋类型的房屋数量 +select type_id,COUNT(*)数量 from HOUSE group by type_id + +--修改房屋类型表的房屋名称字段,把数据类型改成nvarchar(30) +alter table HOUSE_TYPE alter column type_name nvarchar(30) + +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add site nvarchar(30) + +--把房屋价格的默认值0改为100 +alter table HOUSE add house_price cdefault(100) + +--查询出名字最长的房屋信息 +select house_name from HOUSE where len(house_name)=(select max(len(house_name)) from HOUSE) + + +--复习题3 +--6、请统计每种明星类型的人数 +select S_T_NO,COUNT(*)人数 from StarInfo group by S_T_NO + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add AnnualIncome nchar(20) default(10000000) + +--8、查询每个国家的明星人数。 +select S_NATIVE,COUNT(*)人数 from StarInfo group by S_NATIVE + +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select * from StarInfo join StarType on S_T_NO=T_NO order by S_NAME ,S_AGE + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarInfo where S_NAME like '__' and S_NAME like '%西%' + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add CreationTime datetime default(getdate()) +alter table StrType add CreationTime datetime default(getdate()) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\345\244\215\344\271\2401.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\345\244\215\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5708725a623369dd52afa24d1bfe19a652db33ff --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\345\244\215\344\271\2401.sql" @@ -0,0 +1,74 @@ +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf' +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.mdf' +) +use GoodsDB +go +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20)not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) +insert into GoodsType +select '服装内衣' union +select '鞋包配饰' union +select '手机数码' +select * from GoodsType +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +select '提花小西装', '红色', '菲曼琪', '300', 1 union +select '百搭短裤', '绿色', '哥弟', '100 ', 1 union +select '无袖背心', '白色', '阿依莲', '700 ', 1 union +select '低帮休闲鞋', '红色', '菲曼琪', '900', 2 union +select '中跟单鞋', '绿色', '哥弟', '400', 2 union +select '平底鞋', '白色', '阿依莲', '200 ', 2 union +select '迷你照相机', '红色', '尼康', '500', 3 union +select '硬盘', '黑色', '希捷 ', '600 ', 3 union +select '显卡 ', '黑色', '技嘉', '800 ', 3 +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色 ,GoodsMoney 商品价格 from GoodsInfo +where GoodsMoney in (select max(GoodsMoney) from GoodsInfo) +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 +from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select b.TypeName 商品的类别,round(avg(a.GoodsMoney),2)平均价格 from GoodsInfo a inner join GoodsType b on a.TypeID=b.TypeID +group by TypeName + +--7、查询每种颜色的商品数 +select GoodsColor,count(GoodsColor)商品数 from GoodsInfo +group by GoodsColor +select * from GoodsInfo +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +select * from GoodsType +alter table GoodsType add remark text +update GoodsType set remark='我是商品类型信息备注' + +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop FK__GoodsInfo__TypeI__1273C1CD + +--10、删除所有商品类型记录 +delete GoodsType + + + +select * from GoodsType +select * from GoodsInfo \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\345\244\215\344\271\2402.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\345\244\215\344\271\2402.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d1817351cc1b9b11efb650b8f6ccf105cb1b7837 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\345\244\215\344\271\2402.sql" @@ -0,0 +1,77 @@ +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='f:\HOUSE_DB.mdf', + size = 5mb, + maxsize=50mb, + filegrowth=1mb +) +log on +( + name='HOUSE_DB_log', + filename='f:\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int not null primary key identity, + type_name varchar(50) not null unique +) +create table HOUSE +( + ouse_id int not null primary key identity, + house_name varchar(50) not null , + house_price float not null default(0), + type_id int not null foreign key references HOUSE_TYPE(type_id) +) + + +insert into HOUSE_TYPE(type_name) +select '小户型' union +select '经济型' union +select '别墅' +select * from HOUSE_TYPE +insert into HOUSE(house_name,house_price,type_id) +select '平房','500',3 union +select '高楼','1000',2 union +select '总统精装府','2000',1 +select * from HOUSE +--4、添加查询 +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select HOUSE.house_name ,HOUSE_TYPE.type_name from HOUSE_TYPE inner join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price,house_name from HOUSE order by house_price desc +select house_price,house_name from HOUSE where house_price in(select max(house_price) from HOUSE) +--查询每种房屋类型的房屋数量 + +select b.type_name,a.type_id,count(house_name)房屋数量 from HOUSE a +inner join HOUSE_TYPE b on a.type_id=b.type_id +group by a.type_id,house_name,b.type_name + + +--修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +--先删除约束 +alter table HOUSE_TYPE drop UQ__HOUSE_TY__543C4FD94714335E +alter table HOUSE_TYPE alter column type_name nvarchar(30) +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table house add dizhi nvarchar(50) +--把房屋价格的默认值0改为100 +alter table house drop DF__HOUSE__house_pri__276EDEB3 +alter table house add CONSTRAINT DF default(100) for house_price +--查询出名字最长的房屋信息。 +select top 1 ouse_id,house_name,house_price,type_id,dizhi, len(house_name)名字长度 from HOUSE +group by ouse_id ,house_name,house_price,type_id,dizhi order by house_name desc + +select * from HOUSE +select * from HOUSE_TYPE \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\345\244\215\344\271\2403.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\345\244\215\344\271\2403.sql" new file mode 100644 index 0000000000000000000000000000000000000000..85ef0fb83a3077652f5781ac5da48d1b6b632b15 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\220\264\347\205\214/\345\244\215\344\271\2403.sql" @@ -0,0 +1,77 @@ +create database StarManagerDB +on +( +name='StarManagerDB', +filename='f:\StarManagerDB.mdf' + ) + log on + ( + name='StarManagerDB_log', +filename='f:\StarManagerDB_log.ldf' + ) + use StarManagerDB + go + create table StarType + ( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) + ) + create table StarInfo + ( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20)default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO), + ) + + insert into StarType(T_NAME) + select '体育明星' union + select 'IT明星' union + select '相声演员' + select * from StarType + insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) + select '梅西',30,'射门','阿根廷',1 union + select '科比',35,'过人','美国',1 union + select '蔡景现',40,'敲代码','中国',2 union + select '马斯克',36,'造火箭','外星人',2 union + select '郭德纲',50,'相声','中国',3 union + select '黄铮',41,'拼多多','中国',2 + select * from StarInfo + select * from StarType +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄, +--显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 类型编号,count(S_T_NO)明星人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarType + inner join StarInfo on StarType.T_NO=StarInfo.S_T_NO where T_NAME='体育明星' + order by S_AGE desc +--6、请统计每种明星类型的人数 +select b.T_NAME,S_T_NO,count(S_T_NO)人数 from StarInfo a +inner join StarType b on a.S_T_NO=b.T_NO +group by S_T_NO,b.T_NAME + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add nianshouru money +alter table StarInfo add constraint de default(10000000) for nianshouru +--8、查询每个国家的明星人数。 +select S_NATIVE,COUNT(S_NATIVE)人数 from StarInfo GROUP BY S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 + select * from StarInfo order by S_NATIVE,s_age desc + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select a.*,b.T_NAME from StarInfo a +inner join StarType b on a.s_t_no=b.t_no +where a.s_name like '%西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add time datetime +update StarInfo set time= getdate() + +alter table StarType add time datetime +update StarType set time= getdate() + + select * from StarInfo + select * from StarType diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2c058620210ff5ca437b95cc747be7dd1e27b3ee --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" @@ -0,0 +1,74 @@ +create database GoodsDB + + + +use GoodsDB + +go + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) + +drop table GoodsInfo + + +insert GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') + + +insert GoodsInfo values +('提花小西装','红色','菲曼琪','300',1), +('百搭短裤','绿色','哥弟','100',1), +('无袖背心','白色','阿依莲','700',1), +('低帮休闲鞋','红色','菲曼琪','900',2), +('中跟单鞋','绿色','哥弟','400',2), +('平底鞋','白色','阿依莲','200',2), +('迷你照相机','红色','尼康','500',3), +('硬盘','黑色','希捷','600',3), +('显卡','黑色','技嘉','800',3) + + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsMoney 商品价格,GoodsName 商品名称,GoodsColor 商品颜色 from GoodsInfo order by GoodsMoney DESC + + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID,MAX(GoodsMoney),min(GoodsMoney),Round(AVG(GoodsMoney),2) from GoodsInfo +group by TypeID + + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between '300' and '600' + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select TypeName,ROUND(AVG(GoodsMoney),2) from GoodsInfo I inner join GoodsType G on I.TypeID=G.TypeID group by TypeName + +--7、查询每种颜色的商品数 +select GoodsColor,count(*) from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add remark text +update GoodsType set remark='我是商品类型信息备注' + + +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop constraint FK__GoodsInfo__TypeI__182C9B23 + +--10、删除所有商品类型记录 +truncate table GoodsType + diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..057b72f446371788c1a763fc34be18a057802ece --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery2.sql" @@ -0,0 +1,75 @@ +create database HOUSE_DB +on +( + fileName='D:\HOUSE_DB.mdf', + Name='HOUSE_DB', + size=5MB, + Maxsize=50MB, + filegrowth=1MB +) +log on +( + fileName='D:\HOUSE_DB_log.ldf', + Name='HOUSE_DB_log', + size=5MB, + Maxsize=50MB, + filegrowth=1MB +) +go +use HOUSE_DB + +go + +create table HOUSE_TYPE +( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null unique +) + + +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null, + house_price float not null default('0'), + type_id int not null references HOUSE_TYPE(type_id) +) + +insert HOUSE_TYPE values +('小户型'), +('经济型'), +('别墅') + + +insert HOUSE values +('利斯之花','100',1), +('爱敦阁',200,2), +('泽克帕沙河畔别墅',300,3) + +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price DESC +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select b.house_name,a.type_name from HOUSE_TYPE a inner join HOUSE b on a.type_id=b.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price,house_name from HOUSE order by house_price DESC +--查询每种房屋类型的房屋数量 +select type_name,count(*) from HOUSE_TYPE group by type_name +--修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +alter table HOUSE_TYPE alter column type_name varchar(30) null +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE_TYPE add address text + +--把房屋价格的默认值0改为100 +alter table HOUSE drop DF__HOUSE__house_pri__1367E606 +alter table HOUSE add constraint DF_HOUSE_house_price default ('100') for house_price + +exec sp_help 'HOUSE' +--查询出名字最长的房屋信息。 +--select max(len(HOUSE_name)) from HOUSE + +select * from HOUSE where len(HOUSE_name)=(select max(len(HOUSE_name)) from HOUSE) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..96308ee700e6a9c58dbdc4a431e44bfab3e2aa80 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery3.sql" @@ -0,0 +1,37 @@ +create database StarManagerDB +go + +use StarManagerDB + +go + +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +create table StarInfo +( + S_NO int not null primary key identity(1,1), S_NAME nvarchar(20) not null, S_AGE int not null, S_HOBBY nvarchar(20), S_NATIVE nvarchar(20) default('中国大陆'), S_T_NO int references StarType(T_NO) +) + + +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') + + +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo order by S_AGE DESC --4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 select S_T_NO 类型编号,count(*) 明星人数 ,avg(S_AGE) 平均年龄 from StarInfo group by S_T_NO having count(*)>2 --5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 --select T_NO from StarType where T_NAME='体育明星 select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo where S_T_NO in (select T_NO from StarType where T_NAME='体育明星') --6、请统计每种明星类型的人数 select T_NAME,count(*) 人数 from StarInfo inner join StarType on S_T_NO=T_NO group by T_NAME --7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) alter table StarInfo add income money default('10000000') not null --8、查询每个国家的明星人数。 select S_NATIVE 国家,count(*) 人数 from StarInfo group by S_NATIVE --9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 select * from StarInfo inner join StarType on S_T_NO=T_NO order by S_NATIVE,S_AGE DESC --10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 select * from StarInfo inner join StarType on S_T_NO=T_NO where S_NAME like '_西' --11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 alter table StarInfo add Ctime datetime default(getdate()) not null +alter table StarType add Ctime datetime default(getdate()) not null + + diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2301.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2301.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ddce719630121f8fd321ad68e82f26a9b37e11e4 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2301.sql" @@ -0,0 +1,56 @@ +use master +go +create database GoodsDB +go + +use GoodsDB + +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +go + +use GoodsDB + +go + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +insert into GoodsInfo values('提花小西装','红色','菲曼琪','300',1),('百搭短裤','绿色','哥弟','100',1),('无袖背心','白色','阿依莲','700',1), +('低帮休闲鞋','红色','菲曼琪','900',2),('中跟单鞋','绿色','哥弟','400',2),('平底鞋','白色','阿依莲','200',2),('迷你照相机','红色','尼康','500',3), +('硬盘','黑色','希捷','600',3),('显卡','黑色','技嘉','800',3) + + +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney in (select MAX(GoodsMoney) from GoodsInfo) +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID,MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor = '红色'and GoodsMoney >= 300 and GoodsMoney <=600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select GC.TypeID,ROUND(AVG(GoodsMoney),2)平均价格 from GoodsType G inner join GoodsInfo GC on G.TypeID = GC.TypeID group by gc.TypeID +--7、查询 每种颜色 的商 品数 +select GoodsColor,COUNT(*)商品数 from GoodsType G inner join GoodsInfo GC on g.TypeID = GC.TypeID group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add Nots text +update GoodsType set Nots = '我是商品类型信息备注' +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop constraint FK__GoodsInfo__TypeI__1273C1CD +--10、删除所有商品类型记录 +truncate table GoodsType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2302.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2302.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a682c31eb6945dec76d439f38d38e40e2dd0a665 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2302.sql" @@ -0,0 +1,70 @@ +use master + +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename ='D:\HOUSE_DB.mdf', + size=5MB, + maxsize=50MB, + filegrowth=1Mb +) +log on +( + name='HOUSE_DB_log', + filename ='D:\HOUSE_DB_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=1Mb +) +go +use HOUSE_DB + +go + +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique not null +) + +go +use HOUSE_DB +go +create table HOUSE +( + house_id int primary key identity(1,1) not null, + house_name varchar(50) not null, + house_price float default('0') not null, + type_id int references HOUSE_TYPE(type_id) not null +) +insert into HOUSE_TYPE values('别墅'),('经济型'),('小户型') + +insert into HOUSE values('水木年华',1500,3),('大秦帝国之阿房宫',800,1),('风雨亭',1200,2) + +select * from HOUSE +select * from HOUSE_TYPE + +select T.type_id 房屋编号,house_id 房屋编号,house_name 房屋名称,house_price 房租,type_name 房屋类型 +from HOUSE H left join HOUSE_TYPE T on H.type_id = T.type_id + +select * from HOUSE_TYPE where type_name like '%型%' + +select house_name 名称,house_price 租金 from HOUSE order by house_price desc + +select h.type_id 房屋类型,house_name 房屋名称 from HOUSE_TYPE T inner join HOUSE H on h.type_id = T.type_id + +select house_name 房屋名称,house_price 租金 from HOUSE where house_price in(select MAX(house_price) from HOUSE) +-- 查询每种房屋类型的房屋数量 +select h.type_id,COUNT(*)数量 from HOUSE H inner join HOUSE_TYPE HT on H.type_id = HT.type_id group by H.type_id +-- 修改房屋表的房屋名称字段,把数据类型改成nvarchar(30) +alter table HOUSE alter column house_name nvarchar(30) not null +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add address nvarchar(20) +-- 把房屋价格的默认值0改为100 +alter table HOUSE drop constraint [DF__HOUSE__house_pri__173876EA] +alter table HOUSE add constraint DF__HOUSE__house_pri default(100) for house_price +-- 查询出名字最长的房屋信息。 +select * from HOUSE where len(house_name) = (select max(len(house_name)) from HOUSE) diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2303.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2303.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fbc3b7275f550e992e2fed30e483c2d56d36e95c --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\345\244\215\344\271\240\351\242\2303.sql" @@ -0,0 +1,58 @@ +use master +go + +create database StarManagerDB + +go + +use StarManagerDB + +go + +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20) +) +go + +use StarManagerDB + +go + +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +insert into StarType values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 编号,COUNT(S_NAME) 人数,AVG(S_AGE)平均年龄 from StarInfo group by S_T_NO having COUNT(S_NAME) > 2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select S_AGE 年龄,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo where S_AGE in(select MAX(S_AGE) from StarInfo) +--6、请统计每种明星类型的人数 +select * from StarInfo +select * from StarType + +select T_NAME 明星类型,COUNT(*)人数 from StarType S inner join StarInfo ST on S.T_NO = ST.S_T_NO group by T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add annual nvarchar(20) default(10000000) +--8、查询每个国家的明星人数。 +select S_NATIVE 国家,COUNT(*)人数 from StarType S inner join StarInfo ST on S.T_NO = ST.S_T_NO group by S_NATIVE +--9、搜索所有 明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select * from StarInfo S left join StarType ST on S.S_T_NO = ST.T_NO order by S_AGE desc +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarInfo S inner join StarType ST on S.S_T_NO = ST.T_NO where S_NAME like '%西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add time datetime default(getdate()) +alter table StarType add time datetime default(getdate()) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..dc03b862a83e5314ad75ed66f92ae08e28131248 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery2.sql" @@ -0,0 +1,88 @@ +use master +go +create database HOUSE_DB +on +( name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity, + type_name varchar(50) unique not null +) + +create table HOUSE +( + house_id int primary key identity, + house_name varchar(50) not null, + house_price float default(0) not null, + type_id int references HOUSE_TYPE(type_id) not null +) + +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +select*from HOUSE_TYPE +insert into HOUSE values('马云家',100,'1'),('马化腾家',200,'2'),('我家',100000000,'3') + +select*from HOUSE_TYPE +select*from HOUSE + +--4、添加查询 +--查询所有房屋信息 +select * from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id + +--使用模糊查询包含”型“字的房屋类型信息 +select*from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋的名称,house_price 租金 + from HOUSE + order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price 最高月租, house_name 房屋名称 from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id order by house_price desc + + +-- 查询每种房屋类型的房屋数量 +select HOUSE_TYPE.type_name,count(*)房屋数量 from HOUSE join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id group by HOUSE_TYPE.type_name +-- 修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +alter table HOUSE drop constraint FK__HOUSE__type_id__145C0A3F +alter table HOUSE alter column type_id nvarchar(30) + +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add 地址 nvarchar(50) +-- 把房屋价格的默认值0改为100 +alter table HOUSE alter column house_price int --找出约束名称 +alter table HOUSE drop constraint DF__HOUSE__house_pri__1367E606 --删除约束 +alter table HOUSE add constraint dk default(100) for house_price --改约束 +--查询出名字最长的房屋信息。 +select A. type_id ,房屋名字长度,HOUSE_TYPE.type_name 房屋名称 from + (select type_id, len(type_name) 房屋名字长度 from HOUSE_TYPE)A join HOUSE_TYPE on A.type_id=HOUSE_TYPE.type_id + where 房屋名字长度=(select max(房屋名字长度) from ( select len(type_name) 房屋名字长度 from HOUSE_TYPE )B) + + + + + diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..36edabaa718a408103152cc1541681f5c69ff44f --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery3.sql" @@ -0,0 +1,97 @@ +use master +go +create database StarManagerDB +on +( name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity, + T_NAME nvarchar(20) +) + +create table StarInfo +( + S_NO int primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +insert into StarType values('体育明星'),('IT明星'),('相声明星') + +insert into StarInfo values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1), + ('察景现',40,'敲代码',default,2),('马斯克',36,'造火箭','外星人',2), + ('郭德纲',50,'相声',default,3),('黄铮',41,'拼多多',default,2) + + +select*from StarType +select*from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 + from StarInfo + order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO ,count(*) 明星人数,avg(S_AGE) 明星平均年龄 + from StarInfo + group by S_T_NO + having count(*) >2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_AGE 最大的姓名,S_HOBBY 特技,S_NATIVE 籍贯 + from StarType + join StarInfo + on StarInfo.S_T_NO=StarType.T_NO + where T_NAME='体育明星' + order by S_AGE desc + + + select*from StarType +select*from StarInfo +-- 6、请统计每种明星类型的人数 +select S_T_NO 明星编号 ,StarType .T_NAME,A.该类型人数 + from(select S_T_NO ,count(*) 该类型人数 from StarInfo join StarType on StarType.T_NO=StarInfo.S_T_NO group by S_T_NO) A + join StarType on StarType.T_NO=A.S_T_NO + + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add 年收入 money default(10000000) + +--8、查询每个国家的明星人数。 +select S_NATIVE 国家名称,count(*) 人数 from StarInfo group by S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select * from StarInfo + join StarType + on StarInfo.S_T_NO=StarType.T_NO + order by S_NATIVE,S_AGE + desc + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarInfo + join StarType + on StarInfo.S_T_NO=StarType.T_NO + where S_NAME like '__' and S_NAME like'%西' + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +select*from StarType +select*from StarInfo + +alter table StarType add 创建时间 datetime default(getdate()) +alter table StarInfo add 创建时间 datetime default(getdate()) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery4.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0fa988bba3e1a378a68ae6ae237764f1ff743562 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery4.sql" @@ -0,0 +1,81 @@ +use master +go +create database GoodsDB +on +( name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +go +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity, + TypeName nvarchar(20) not null + +) + +create table GoodsInfo +( + GoodsID int primary key identity, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null, + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +insert into GoodsInfo values('提花小西装', '红色', '菲曼琪', '300', 1),('百搭短裤', '绿色', '哥弟', '100', 1), + ('无袖背心', '白色', '阿依莲', '700', 1),('低帮休闲鞋', '红色', '菲曼琪', '900', 2), + ('中跟单鞋', '绿色', '哥弟', '400', 2),('平底鞋', '白色', '阿依莲','200', 2), + ('迷你照相机', '红色', '尼康', '500', 3),('硬盘', '黑色', '希捷', '600', 3), + ('显卡', '黑色', '技嘉','800', 3) +select*from GoodsType +select*from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + select GoodsName 商品名称, GoodsColor 商品颜色, GoodsMoney 商品价格 + from GoodsInfo + where GoodsMoney=(select max(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格, avg(GoodsMoney) 平均价格 + from GoodsInfo + join GoodsType + on GoodsType.TypeID=GoodsInfo.TypeID + group by TypeName + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and (GoodsMoney between 300 and 600) + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select GoodsType.TypeName ,平均价格 from +(select TypeID ,avg(GoodsMoney)平均价格 from GoodsInfo group by TypeID )A +join +GoodsType on A.TypeID=GoodsType.TypeID +--7、查询每种颜色的商品数 +select GoodsColor 颜色,count(*) 商品数 from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add 备注 nvarchar(50) +update GoodsType set 备注='我是商品类型信息备注' + +--9、在商品信息表中删除商品类型的外键约束 +select * from GoodsInfo +alter table GoodsInfo alter column TypeID char(50) --找出约束名称 +alter table GoodsInfo drop constraint FK__GoodsInfo__TypeI__1273C1CD +--10、删除所有商品类型记录 +alter table GoodsInfo drop column TypeID \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery10.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery10.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a2fc72c55e2544857742450fb5c2edcce93eef32 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery10.sql" @@ -0,0 +1,71 @@ +create database GoodsDB +on +( +name='GoodsDB_data', +filename='C:\app\GoodsDB_data.mdf', +size=10mb, +maxsize=100mb, +filegrowth=10% +) +log on +( +name='GoodsDB_log', +filename='C:\app\GoodsDB_log.ldf', +size=10mb, +filegrowth=1mb +) +USE GoodsDB +go +create table GoodsType +( +TypeID int primary key IDENTITY(1,1) not null, +TypeName nvarchar(20), +TZB nvarchar(20) DEFAULT '我是商品类型信息备注', +)set IDENTITY_INSERT GoodsType off +insert GoodsType(TypeID,TypeName) +select 1,'服装内衣' union +select 2,'鞋包配饰' union +select 3,'手机数码' + +go +create table GoodsInfo +( +GoodsID int primary key IDENTITY(1,1) not null, +GoodsName nvarchar(20), +GoodsColor nvarchar(20), +GoodsBrand nvarchar(20), +GoodsMoney money, +TypeID int foreign key references GoodsType(TypeID) +)set IDENTITY_INSERT GoodsInfo on +insert GoodsInfo(GoodsID,GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +select 1,'提花小西装','红色','菲曼琪',300,1 union +select 2,'百搭短裤','绿色','哥弟',100,1 union +select 3,'无袖背心','白色','阿依莲',700,1 union +select 4,'低帮休闲鞋','红色','菲曼琪',900,2 union +select 5,'中跟单鞋','绿色','哥弟',400,2 union +select 6,'平底鞋','白色','阿依莲',200,2 union +select 7,'迷你照相机','红色','尼康',500,3 union +select 8,'硬盘','黑色','希捷',600,3 union +select 9,'显卡','黑色','技嘉',800,3 + +Select * from GoodsType +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName,GoodsColor,GoodsMoney from GoodsInfo where GoodsMoney=(select max(GoodsMoney) from GoodsInfo ) +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select max(GoodsMoney),min(GoodsMoney),avg(GoodsMoney) from GoodsInfo inner join GoodsType on GoodsInfo.TypeID=GoodsType.TypeID group by TypeName +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select AVG(TypeName),GoodsMoney from GoodsType inner join GoodsInfo on GoodsType.TypeName=GoodsInfo.GoodsName group by TypeName +--7、查询每种颜色的商品数 +select COUNT(GoodsName),GoodsColor from GoodsInfo group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” + +--9、在商品信息表中删除商品类型的外键约束 +ALTER TABLE GoodsInfo +DROP CONSTRAINT DF_TypeID +--10、删除所有商品类型记录 +DROP TABLE GoodsType +go \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery8.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery8.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7e3b3266a9ab1deef4eb9335ec7cf65599e52467 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery8.sql" @@ -0,0 +1,82 @@ +Create Database StarManagerDB +on +( +name='StarManagerDB_data', +filename='C:\app\StarManagerDB_data.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) +log on +( +name='StarManagerDB_log', +filename='C:\app\StarManagerDB_log.ldf', +size=5mb, +filegrowth=100mb +) +USE StarManagerDB +go +CREATE TABLE StarType +( +T_NO int primary key identity(1,1)not null, +T_NAME nvarchar(20), +T_CJSJ datetime DEFAULT GETDATE() +) +set IDENTITY_INSERT StarType off +INSERT StarType(T_NO,T_NAME) +SELECT 1,'体育明星' UNION +SELECT 2,'IT明星' UNION +SELECT 3,'相声演员' + + +go +CREATE TABLE StarInfo +( +S_NO int primary key identity(1,1)not null, +S_NAME nvarchar(20), +S_AGE int, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) DEFAULT '中国大陆', +S_T_NO int foreign key references StarType(T_NO), +S_NSN char(1) default 10000000 not null , +S_CJSJ datetime DEFAULT GETDATE() +) +set IDENTITY_INSERT StarInfo on +INSERT StarInfo(S_NO,S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +SELECT 1,'梅西',30,'射门','阿根廷',1 UNION +SELECT 2,'科比',35,'过人','美国',1 UNION +SELECT 3,'蔡景现',40,'敲代码','中国',2 UNION +SELECT 4,'马斯克',36,'造火箭','外星人',2 UNION +SELECT 5,'郭德纲',50,'相声','中国',3 UNION +SELECT 6,'黄铮',41,'拼多多','中国',2 + +Select * from StarType +Select * from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3(S_AGE),S_NAME,S_NATIVE from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO,AVG(S_AGE),COUNT(S_T_NO) from StarInfo group by S_T_NO having Count(S_T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +Select top 1 S_NAME ,S_AGE,S_HOBBY,S_NATIVE +from StarInfo inner join StarType on StarInfo.S_T_NO=StarType.T_NO where T_NAME='体育明星' order by S_AGE desc +--6、请统计每种明星类型的人数 +select count(a.S_NAME),b.T_NAME from StarInfo a inner join StarType b on a.S_NAME=b.T_NAME group by b.T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) + +--8、查询每个国家的明星人数。 +select S_NATIVE,Count(S_NAME) from StarInfo group by S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select*From StarInfo inner join StarType on StarInfo.S_T_NO=StarType.T_NO order by S_NATIVE desc,S_AGE desc +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select S_NAME,T_NAME from StarType inner join StarInfo on StarType.T_NO=StarInfo.S_T_NO where S_NAME LIKE'_西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 + + + + + + + + + diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery9.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery9.sql" new file mode 100644 index 0000000000000000000000000000000000000000..49b3e938176375c2d486da446c1fcf7caa61676e --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery9.sql" @@ -0,0 +1,64 @@ +create database HOUSE_DB +on +( +name='HOUSE_DB_data', +filename='C:\app\HOUSE_DB_data.mdf', +size=5mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_log', +filename='C:\app\HOUSE_DB_log.ldf', +size=5mb, +filegrowth=1mb +) +USE HOUSE_DB +go +CREATE TABLE HOUSE_TYPE +( +type_id int not null primary key, +type_name varchar(30) unique (type_name) +) +INSERT HOUSE_TYPE(type_id,type_name) +SELECT 1,'小户型' UNION +SELECT 2,'经济型' UNION +SELECT 3,'别墅' + +go +CREATE TABLE HOUSE +( +house_id int not null primary key, +house_name varchar(50), +house_price varchar(100), +type_id int foreign key references HOUSE_TYPE, +DZ varchar(50) +) +INSERT HOUSE(house_id,house_name,house_price,type_id) +SELECT 1,'易居',1380,1 UNION +SELECT 2,'桐心居',2650,3 UNION +SELECT 3,'屈居',1650,2 + +Select * from HOUSE_TYPE +Select * from HOUSE + +--查询所有房屋信息 +Select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name LIKE '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +Select type_name,house_name from HOUSE_TYPE inner join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1(house_price),house_name from HOUSE order by house_price desc +--查询每种房屋类型的房屋数量 +select type_id,COUNT(house_name) from HOUSE group by type_id +--修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) + +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 + +--把房屋价格的默认值0改为100 + +--查询出名字最长的房屋信息。 +select house_id,house_price,type_id,house_name,len(house_name) from HOUSE where LEN(house_name)=(select max(len(house_name))from HOUSE ) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..29bbc0036cb16fb28b1fec12d0bc68090a5ce5d0 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" @@ -0,0 +1,94 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=10, + maxsize=50, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=10, + maxsize=50, + filegrowth=10% +) +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') + +insert into GoodsInfo values +('提花小西装','红色','菲曼琪','300',1), +('百搭短裤','绿色','哥弟','100',1), +('无袖背心','白色','阿依莲','700',1), +('低帮休闲鞋','红色','菲曼琪','900',2), +('中跟单鞋', '绿色', '哥弟', '400', 2), +('平底鞋', '白色', '阿依莲', '200', 2), +('迷你照相机','红色','尼康','500',3), +('硬盘','黑色','希捷','600',3), +('显卡','黑色','技嘉','800',3) + + +select * from GoodsType +select * from GoodsInfo + + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo +group by GoodsName,GoodsColor,GoodsMoney order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类别,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 +from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select b.TypeName 类别名称,AVG(GoodsMoney) 平均价格 +from GoodsInfo a join GoodsType b on a.TypeID=b.TypeID +group by b.TypeName + +--7、查询每种颜色的商品数 +select GoodsColor 颜色,COUNT(*) 数量 from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add 备注 nvarchar(200) +insert into GoodsType (备注) values +('我是商品类型信息备注'), +('我是商品类型信息备注'), +('我是商品类型信息备注') + +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop constraint FK__GoodsInfo__TypeI__1273C1CD +--10、删除所有商品类型记录 +delete from GoodsType + +select * from GoodsType +select * from GoodsInfo diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cf9e65fcccb64d3443aa98bb20fd772783ab4c25 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery2.sql" @@ -0,0 +1,86 @@ +use master +go +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +go +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique +) +create table HOUSE +( + house_id int primary key identity(1,1) not null, + house_name varchar(50) not null, + house_price float not null default(0), + type_id int references HOUSE_TYPE(type_id) +) + +insert into HOUSE_TYPE values +('小户型'), +('经济型'), +('别墅') + +insert into HOUSE values +('闽大厕所',0,1), +('闽大操场',100,2), +('闽大公寓',300,3) + + +select * from HOUSE +select * from HOUSE_TYPE + +--查询所有房屋信息 +select * from HOUSE_TYPE a inner join HOUSE b on a.type_id=b.type_id + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select HOUSE.type_id,house_name,type_name from HOUSE +inner join HOUSE_TYPE on HOUSE.type_id= HOUSE_TYPE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_id,house_name,house_price from HOUSE +where house_price in (select max(house_price) from HOUSE ) + +select * from HOUSE +select * from HOUSE_TYPE + +-- 查询每种房屋类型的房屋数量 +select type_name 房屋类型,count(*) 数量 from HOUSE_TYPE group by type_name + +-- 修改房屋表的房屋名称字段,把数据类型改成nvarchar(30) +alter table HOUSE alter column house_name nvarchar(30) + +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add addres text + +-- 把房屋价格的默认值0改为100 +alter table HOUSE drop constraint FK__HOUSE__type_id__286302EC +alter table HOUSE add constraint FK__HOUSE__type_id__286302EC default(100)for house_price + +--查询出名字最长的房屋信息。 + + diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..177b14ea1475ef64704d367385d18792778c9ca2 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery3.sql" @@ -0,0 +1,68 @@ +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20) +) + +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) + +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') + +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + +select * from StarType +select * from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技, S_NATIVE 籍贯 from StarInfo order by S_AGE DESC + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select count(S_NO) 明星人数,AVG(S_AGE) 明星平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 max(S_AGE)年龄,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 +from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO WHERE B.T_NAME='体育明星' +GROUP BY S_NAME,S_HOBBY,S_NATIVE + +--6、请统计每种明星类型的人数 +select T_NAME 明星类型,COUNT(*) 数量 from StarType a join StarInfo b on a.T_NO=b.S_T_NO group by a.T_NAME + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add 年收入 varchar(200) not null default('10000000') + +--8、查询每个国家的明星人数。 +select S_NATIVE 国家,COUNT(S_NATIVE)人数 from StarInfo GROUP BY S_NATIVE + +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 + select * from StarInfo order by S_NATIVE,s_age desc + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarType A inner join StarInfo B on A.T_NO=B.S_T_NO where S_NAME like '_西' + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarType add 创建时间 datetime not null default(getdate()) +alter table StarInfo add 创建时间 datetime not null default(getdate()) diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7babe930869c38a64b01ebe415adee89c72af294 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" @@ -0,0 +1,83 @@ +use master +go +create database HOUSE_DB +on +( name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity, + type_name varchar(50) unique not null +) + +create table HOUSE +( + house_id int primary key identity, + house_name varchar(50) not null, + house_price float default(0) not null, + type_id int references HOUSE_TYPE(type_id) not null +) + +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +select*from HOUSE_TYPE +insert into HOUSE values('平家',100,'1'),('小家',200,'2'),('小楼房',100000000,'3') + +select*from HOUSE_TYPE +select*from HOUSE + +--4、添加查询 +--查询所有房屋信息 +select * from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id + +--使用模糊查询包含”型“字的房屋类型信息 +select*from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋的名称,house_price 租金 + from HOUSE + order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price 最高月租, house_name 房屋名称 from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id order by house_price desc + + +-- 查询每种房屋类型的房屋数量 +select HOUSE_TYPE.type_name,count(*)房屋数量 from HOUSE join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id group by HOUSE_TYPE.type_name +-- 修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +alter table HOUSE drop constraint FK__HOUSE__type_id__145C0A3F +alter table HOUSE alter column type_id nvarchar(30) + +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add 地址 nvarchar(50) +-- 把房屋价格的默认值0改为100 +alter table HOUSE alter column house_price int +alter table HOUSE drop constraint DF__HOUSE__house_pri__276EDEB3 +alter table HOUSE add constraint dk default(100) for house_price +--查询出名字最长的房屋信息。 +select A. type_id ,房屋名字长度,HOUSE_TYPE.type_name 房屋名称 from + (select type_id, len(type_name) 房屋名字长度 from HOUSE_TYPE)A join HOUSE_TYPE on A.type_id=HOUSE_TYPE.type_id + where 房屋名字长度=(select max(房屋名字长度) from ( select len(type_name) 房屋名字长度 from HOUSE_TYPE )B) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..79f76971c09cf93a852e6d562e377fca0085f23d --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" @@ -0,0 +1,96 @@ +use master +go +create database StarManagerDB +on +( name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity, + T_NAME nvarchar(20) +) + +create table StarInfo +( + S_NO int primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +insert into StarType values('体育明星'),('IT明星'),('相声明星') + +insert into StarInfo values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1), + ('察景现',40,'敲代码',default,2),('马斯克',36,'造火箭','外星人',2), + ('郭德纲',50,'相声',default,3),('黄铮',41,'拼多多',default,2) + + +select*from StarType +select*from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 + from StarInfo + order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO ,count(*) 明星人数,avg(S_AGE) 明星平均年龄 + from StarInfo + group by S_T_NO + having count(*) >2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_AGE 最大的姓名,S_HOBBY 特技,S_NATIVE 籍贯 + from StarType + join StarInfo + on StarInfo.S_T_NO=StarType.T_NO + where T_NAME='体育明星' + order by S_AGE desc + + + select*from StarType +select*from StarInfo +-- 6、请统计每种明星类型的人数 +select S_T_NO 明星编号 ,StarType .T_NAME,A.该类型人数 + from(select S_T_NO ,count(*) 该类型人数 from StarInfo join StarType on StarType.T_NO=StarInfo.S_T_NO group by S_T_NO) A + join StarType on StarType.T_NO=A.S_T_NO + + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add 年收入 money default(10000000) + +--8、查询每个国家的明星人数。 +select S_NATIVE 国家名称,count(*) 人数 from StarInfo group by S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select * from StarInfo + join StarType + on StarInfo.S_T_NO=StarType.T_NO + order by S_NATIVE,S_AGE + desc + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarInfo + join StarType + on StarInfo.S_T_NO=StarType.T_NO + where S_NAME like '__' and S_NAME like'%西' + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +select*from StarType +select*from StarInfo +alter table StarType add 创建时间 datetime default(getdate()) +alter table StarInfo add 创建时间 datetime default(getdate()) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4f848a67e08afee98d9aa5400a977804de4524c4 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery3.sql" @@ -0,0 +1,81 @@ +use master +go +create database GoodsDB +on +( name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +go +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity, + TypeName nvarchar(20) not null + +) + +create table GoodsInfo +( + GoodsID int primary key identity, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null, + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +insert into GoodsInfo values('提花小西装', '红色', '菲曼琪', '300', 1),('百搭短裤', '绿色', '哥弟', '100', 1), + ('无袖背心', '白色', '阿依莲', '700', 1),('低帮休闲鞋', '红色', '菲曼琪', '900', 2), + ('中跟单鞋', '绿色', '哥弟', '400', 2),('平底鞋', '白色', '阿依莲','200', 2), + ('迷你照相机', '红色', '尼康', '500', 3),('硬盘', '黑色', '希捷', '600', 3), + ('显卡', '黑色', '技嘉','800', 3) +select*from GoodsType +select*from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + select GoodsName 商品名称, GoodsColor 商品颜色, GoodsMoney 商品价格 + from GoodsInfo + where GoodsMoney=(select max(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格, avg(GoodsMoney) 平均价格 + from GoodsInfo + join GoodsType + on GoodsType.TypeID=GoodsInfo.TypeID + group by TypeName + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and (GoodsMoney between 300 and 600) + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select GoodsType.TypeName ,平均价格 from +(select TypeID ,avg(GoodsMoney)平均价格 from GoodsInfo group by TypeID )A +join +GoodsType on A.TypeID=GoodsType.TypeID +--7、查询每种颜色的商品数 +select GoodsColor 颜色,count(*) 商品数 from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add 备注 nvarchar(50) +update GoodsType set 备注='我是商品类型信息备注' + +--9、在商品信息表中删除商品类型的外键约束 +select * from GoodsInfo +alter table GoodsInfo alter column TypeID char(50) +alter table GoodsInfo drop constraint FK__GoodsInfo__TypeI__1273C1CD +--10、删除所有商品类型记录 +alter table GoodsInfo drop column TypeID \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e21eb8e8c3a242270fe13ee7c7dce22cc6cc9f86 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery1.sql" @@ -0,0 +1,72 @@ +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\HOUSE_DB.mdf', +size=5mb, +filegrowth=1%, +maxsize=50mb +) +log on +( +name='HOUSE_DB_log', +filename='D:\HOUSE_DB_log.ldf', +size=5mb, +filegrowth=1%, +maxsize=50mb +) +go +use HOUSE_DB +go +drop table HOUSE_TYPE +create table HOUSE_TYPE +( +type_id int primary key not null identity(1,1), +type_name varchar(50) unique not null +) +drop table HOUSE +create table HOUSE +( +house_id int not null primary key identity(1,1) , +house_name varchar(50) not null, +house_price float default(0) not null, +type_id int foreign key references HOUSE_TYPE(type_id), +) +select * from HOUSE_TYPE +insert into HOUSE_TYPE +select '小户型' union +select '经济型' union +select '别墅' +select * from HOUSE +insert into HOUSE(house_name,house_price,type_id) +select '豪华',1000,1union +select '梦幻型',900,2union +select '古典型',500,3 + +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 名称,house_price 租金 from HOUSE +group by house_name,house_price order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name from HOUSE inner join +HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 max(house_price) 最高租金,house_name 房屋名称 from HOUSE +group by house_price,house_name order by house_price desc +-- 查询每种房屋类型的房屋数量 +select type_name,count(house_name) 数量 from HOUSE_TYPE +inner join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id +group by type_name,house_name +-- 修改房屋表的房屋名称字段,把数据类型改成nvarchar(30) +alter table HOUSE_TYPE drop constraint UQ__HOUSE_TY__543C4FD9764E19E2 +alter table HOUSE_TYPE alter column type_name nvarchar(30) +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add houseAddress nvarchar(50) +-- 把房屋价格的默认值0改为100 +alter table house drop DF__HOUSE__house_pri__53385258 +alter table house add constraint df default(100) for house_price +--查询出名字最长的房屋信息。 +select * from HOUSE where house_name like '___' \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..79cda56a3592b02281e33dbfe32c1cf2e7ad89c5 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" @@ -0,0 +1,82 @@ +create database StarManagerDB +on +( +name='StarManagerDB', +filename='D:\StarManagerDB.mdf', +size=500mb, +filegrowth=15%, +maxsize=15mb +) +log on +( +name='StarManagerDB_log', +filename='D:\StarManagerDB_log.ldf', +size=500mb, +filegrowth=15%, +maxsize=158mb +) +go +use StarManagerDB +go +drop table StarType +create table StarType +( +T_NO int not null primary key , +T_NAME nvarchar(20) +) +drop table StarInfo +create table StarInfo +( +S_NO int not null primary key, +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int foreign key references StarType(T_NO) +) +select * from StarType +insert into StarType +select 1,'体育明星' union +select 2,'IT 明星' union +select 3,'相声演员' + +select * from StarInfo +insert into StarInfo(S_NO,S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +select 1,'梅西',30,'射门','阿根廷',1 union +select 2,'科比',35,'过人','美国',1 union +select 3,'蔡景现',40,'敲代码','中国',2 union +select 4,'马斯克',36,'造火箭','外星人',2 union +select 5,'郭德纲',50,'相声','中国',3 union +select 6,'黄铮',41,'拼多多','中国',2 + + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 max(S_AGE),S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo +group by S_AGE,S_NAME,S_HOBBY,S_NATIVE order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO,count(S_NAME) 明星人数,avg(S_AGE) 平均年龄 from StarInfo +group by S_T_NO having count(S_NAME)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名, S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO +where StarType.T_NAME='体育明星' order by S_AGE DESC +--6、请统计每种明星类型的人数 +select T_NAME,COUNT(S_NAME) 人数 from StarInfo inner join +StarType on StarInfo.S_T_NO=StarType.T_NO group by T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add s_monry money default(10000000.00) not null +--8、查询每个国家的明星人数。 +select S_NATIVE,COUNT(S_NAME) 人数 from StarInfo group by S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select T_NAME 类型,StarInfo.* from StarInfo inner join +StarType on StarInfo.S_T_NO=StarType.T_NO order by S_NATIVE +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select StarInfo.*,StarType.T_NAME 类型 from StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO where S_NAME like '%西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +select * from StarType +alter table StarType add da text +update StarType set da='系统当前时间' +select * from StarInfo +alter table StarInfo add ta text +update StarInfo set ta='系统当前时间' \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery4.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a82ad817fcff745f32783b1f008cd167cd1cfc27 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery4.sql" @@ -0,0 +1,80 @@ +create database GoodsDB +on +( +name='GoodsDB', +filename='D:\GoodsDB.mdf', +size=500mb, +filegrowth=10%, +maxsize=1000mb +) +log on +( +name='GoodsDB_log', +filename='D:\GoodsDB_log.ldf', +size=500mb, +filegrowth=10%, +maxsize=1000mb +) +go +use GoodsDB +go +drop table GoodsType +create table GoodsType +( +TypeID int primary key not null, +TypeName nvarchar(20) not null +) +drop table GoodsInfo +create table GoodsInfo +( +GoodsID int primary key not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20), +GoodsBrand nvarchar(20) not null, +GoodsMoney money not null, +TypeID int foreign key references GoodsType(TypeID) +) + +select * from GoodsType 商品类型表 +insert into GoodsType +select 1,'服装内衣' union +select 2,'鞋包配饰' union +select 3,'手机数码' + +select * from GoodsInfo 商品信息表 +insert into GoodsInfo(GoodsID,GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +select 1,'提花小西装','红色','菲曼琪',300,1 union +select 2,'百搭短裤','绿色','哥弟',100,1 union +select 3,'无袖背心','白色','阿依莲',700,1 union +select 4,'低帮休闲鞋','红色','菲曼琪',900,2 union +select 5,'中跟单鞋','绿色','哥弟',400,2 union +select 6,'平底鞋','白色','阿依莲',200,2 union +select 7,'迷你照相机','红色','尼康',500,3 union +select 8,'硬盘','黑色','希捷 ',600,3 union +select 9,'显卡','黑色','技嘉',800,3 + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 max(GoodsMoney),GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 +from GoodsInfo group by GoodsMoney,GoodsName,GoodsColor order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select top 1 max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 +from GoodsInfo +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select GoodsColor,GoodsMoney from GoodsInfo +where GoodsColor=('红色') and GoodsMoney between 300 and 600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select * from GoodsInfo +select avg(A.GoodsMoney) 平均价格,A.GoodsBrand 商品的类别 from GoodsInfo + A inner join GoodsType B on A.TypeID=B.TypeID group by A.GoodsMoney,A.GoodsBrand +--7、查询每种颜色的商品数 +select GoodsColor,count(GoodsName) from GoodsInfo group by GoodsColor,GoodsName +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add md text +update GoodsType set md='我是商品类型信息备注' +select * from GoodsType +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop constraint foreign_key_TypeID + +--10、删除所有商品类型记录 +drop table GoodsType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3faf4d6cf885f9280f64feab2b0022c2824e6128 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" @@ -0,0 +1,72 @@ +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='C:\学习\数据库\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=20% +) +log on +( + name='HOUSE_DB_log', + filename='C:\学习\数据库\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=20% +) +go +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity(1,1), + type_name varchar(50) unique +) + +create table HOUSE +( + house_id int primary key identity(1,1), + house_name varchar(50), + house_price float constraint DF_HOUSE_house_price default(0), + type_id int references HOUSE_TYPE(type_id) +) + +insert into HOUSE_TYPE values +('小户型'),('经济型'),('别墅') + +insert into HOUSE values +('天元山','30000',1), +('奥比岛','20000',2), +('摩尔庄园','10000',3) + + +--查询所有房屋信息 +select * from HOUSE full join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price DESC +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name from HOUSE full join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_price,house_name from HOUSE where house_price in (select max(house_price) from HOUSE) +--查询每种房屋类型的房屋数量 +select type_id,count(*) from HOUSE +where type_id in (select type_id from HOUSE_TYPE) +group by type_id +--修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +alter table HOUSE_TYPE drop constraint UQ__HOUSE_TY__543C4FD90C08ED92 +alter table HOUSE_TYPE alter column type_name nvarchar(30) +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add 地址 nvarchar(10) +--把房屋价格的默认值0改为100 +alter table HOUSE drop constraint DF_HOUSE_House_price +alter table HOUSE add constraint DF_HOUSE_HousePri default(100) for house_price +--查询出名字最长的房屋信息。 +select * from HOUSE where len(house_name)=(select max(len(house_name)) from HOUSE ) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3890bcd9cb8b718dcd42bdcae5c7894f6cc30247 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" @@ -0,0 +1,91 @@ +use master +go + +create database StarManagerDB +on +( + name='StarManagerDB', + filename='C:\学习\数据库\StarManagerDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=20% +) +log on +( + name='StarManagerDB_log', + filename='C:\学习\数据库\StarManagerDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=20% +) +go +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') + +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo where S_AGE in (select top 3 S_AGE from StarInfo order by S_AGE DESC) +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 类型编号,count(S_T_NO)人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarType inner join StarInfo on StarInfo.S_T_NO=StarType.T_NO +where T_NAME='体育明星' and S_AGE in (select max(S_AGE) from StarInfo where S_T_NO=1) + +--6、请统计每种明星类型的人数 +select T_NAME,count(*)人数 from StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO +group by T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add priceYear int +go +alter table starinfo add constraint DF_StarInfo_priceYear default(10000000) for priceYear +--8、查询每个国家的明星人数。 +select S_NATIVE,count(*) 明星人数 from StarInfo group by S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select * from StarInfo A inner join StarType B on A.S_T_NO=b.T_NO +order by S_NATIVE + +select A.*,B.* from StarInfo A inner join StarType B on A.S_T_NO=b.T_NO +order by A.S_NATIVE,S_AGE DESC +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarInfo A +inner join StarType B on A.S_T_NO=b.T_NO +where S_NAME like '_西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add createTime datetime default(getdate()) +go +alter table StarType add createTime datetime default(getdate()) +go +update StarInfo set createTime=getdate() +update Startype set createTime=getdate() +select * from StarInfo +select * from StarType + diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8c6148bddd190475e2dbba4d92924f9910089ace --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery3.sql" @@ -0,0 +1,80 @@ +use master +go + +create database GoodsDB +on +( + name='GoodsDB', + filename='C:\学习\数据库\GoodsDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='C:\学习\数据库\GoodsDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') + +insert into GoodsInfo values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +select * from GoodsInfo +select * from GoodsType +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney in (select max(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号 分组查询 商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型编号,max(GoodsMoney) 商品最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney)平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 +--6、查询 每类商品的 平均价格,要显示商品的类别名称和平均价格。 +select round(avg(GoodsMoney),2),typeName from GoodsInfo A inner join GoodsType B on A.TypeID=B.TypeID +group by TypeName + +--7、查询每种颜色的商品数 +select GoodsColor,count(*) from GoodsInfo group by GoodsColor +select * from GoodsInfo +select * from GoodsType +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add remake text +alter table GoodsType add constraint DF_GoodsType_remake default('我是商品类型信息备注') for remake +update GoodsType set remake = '我是商品类型信息备注' +select * from GoodsType +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop FK__GoodsInfo__TypeI__267ABA7A +--10、删除所有商品类型记录 +truncate table GoodsType diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..306b84ef0bd8416dffeee025784fe847bcca05d4 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" @@ -0,0 +1,110 @@ +create database HOUSE_DB + +drop database HOUSE_DB + +on + +( + + name='HOUSE_DB', + + filename='E:\HOUSE_DB.mdf', + + size=5mb, + + maxsize=50mb, + + filegrowth=1% + +) + +log on + +( + + name='HOUSE_DB_log', + + filename='E:\HOUSE_DB_log.ldf', + + size=5mb, + + maxsize=50mb, + + filegrowth=1% + +) + +go + +use HOUSE_DB + +go + + +create table HOUSE_TYPE +( +type_id int not null primary key, --类型编号 +type_name varchar(50) not null unique --类型名称 + +) +drop table house +create table HOUSE--房屋信息表 +( +houseID int not null primary key ,--房屋编号 +house_name varchar(50) not null ,--房屋名称 +houseprice float not null default(0), --房租 +type_id int not null foreign key references HOUSE_TYPE(type_id ) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE_TYPE +select 1,'小户型' union +select 2,'经济型' union +select 3,'别墅' + +insert into HOUSE +select 1,'厕所',0,1 union +select 2,'奥里给',0 ,2union +select 3,'八嘎压路',0 ,3 + +select * from HOUSE + +--4、添加查询 +--查询所有房屋信息 +select *from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select* from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋名称,houseprice 租金 from house order by houseprice desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select M.house_name 房屋名称,L.type_name 类型名称 from HOUSE M inner join HOUSE_TYPE L on M.type_id=L.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 max(houseprice) 月租最高,house_name 房屋名称 from HOUSE group by house_name,houseprice order by houseprice desc + + + --查询每种房屋类型的房屋数量 + select * from HOUSE_TYPE + select *from HOUSE + + select count(*)房屋类型的房屋数量 ,type_name 类型名称 from HOUSE_TYPE Y inner join HOUSE M on Y.type_id=M.houseID group by type_name + + --修改房屋表的房屋名称字段,把数据类型改成nvarchar(30) + alter table house alter column house_name nvarchar(30) + + --给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 + alter table HOUSE add menglin text + update house set menglin ='地址' + select * from house + + --把房屋价格的默认值0改为100 + update HOUSE set houseprice = '100' where houseid between 1 and 3 + select * from HOUSE + +--查询出名字最长的房屋信息。 +select house_name 房屋名称 from HOUSE where house_name like '%____%' diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4b6badd8216ac8dd709eb222b0026bd05945fd79 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery2.sql" @@ -0,0 +1,112 @@ +create database StarManagerDB + + + +on + +( + + name='StarManagerDB', + + filename='E:\StarManagerDB.mdf', + + size=5mb, + + maxsize=50mb, + + filegrowth=10% + +) + +log on + +( + + name='StarManagerDB', + + filename='E:\StarManagerDB_log.ldf', + + size=5mb, + + maxsize=50mb, + + filegrowth=10% + +) + +go + +use HOUSE_DB +go + +drop table starType +create table starType --明星类型表 +( +T_NO int not null primary key ,--明星类型编号 +T_NAME nvarchar(20) --明星类型 +) + +drop table StarInfo + +create table StarInfo --(明星信息表) +( +S_NO int not null primary key , --明星编号 +S_NAME nvarchar(20) not null , +S_AGE int not null, --年龄 +S_HOBB nvarchar(20) , --特技 +S_NATIVE nvarchar(20) default('中国大陆'),-- 籍贯 +S_T_NO int foreign key references starType(T_NO) --明星类型编号 +) + +insert into starType +select 1,'体育明星' union +select 2,'IT明星' union +select 3,'相声演员' + +select * from starType + +insert into StarInfo +select 1,'梅西',30,'射门','阿根廷',1 union +select 2,'科比',35,'过人','美国',1 union +select 3,'蔡景现',40,'敲代码','中国',2 union +select 4,'马斯克',36,'造火箭','外星人',2 union +select 5,'郭德纲',50,'相声','中国',3 union +select 6,'黄铮',50,'拼多多','中国',2 union +select 7,'杨梦林',21,'奥特曼变身第一人','光之国',2 + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 max(S_AGE) 年龄最大,S_NAME 明星的姓名,S_HOBB 特技,S_NATIVE 籍贯 from StarInfo group by S_NAME,S_HOBB , S_NATIVE,S_NAME + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select count(*)明星人数,avg(S_AGE) 明星平均年龄 from StarInfo having count(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 max(M.S_AGE) 年龄最大,M.S_NAME 明星的姓名,M.S_HOBB 特技,M.S_NATIVE 籍贯,L.T_NAME 明星类型 from StarInfo M inner join starType L on M.S_T_NO=L.T_NO + where L.T_NAME = '体育明星' group by M.S_NAME ,M.S_HOBB,M.S_NATIVE,L.T_NAME,M.S_AGE + + --6、请统计每种明星类型的人数 + select * from starType + select * from StarInfo + + select T_NAME 明星类型 ,count(*) 人数 from StarInfo Y inner join starType M on Y.S_T_NO=M.T_NO group BY T_NAME + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add year text +update StarInfo set year = '10000000' +select * from StarInfo + +--8、查询每个国 家的明星人数。\ +select S_native 国家,count(*) 人数 from StarInfo group by S_native + +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select S_AGE 年龄,S_NAME 明星的姓名,S_NATIVE 籍贯 from StarInfo group by S_NAME,S_HOBB , S_NATIVE,S_NAME,S_AGE order by S_AGE desc + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarInfo where S_NAME like '_西' + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add time text +update StarInfo set time = '系统当前时间' +select * from StarInfo + + diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery5.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a6d161638c7aca4bd82a975ec5ec957ec56606cb --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery5.sql" @@ -0,0 +1,124 @@ +create database GoodsDB + + + +on + +( + + name='GoodsDB', + + filename='E:\GoodsDB.mdf', + + size=5mb, + + maxsize=50mb, + + filegrowth=10% + +) + +log on + +( + + name='GoodsDB_log', + + filename='E:\GoodsDB_log.ldf', + + size=5mb, + + maxsize=50mb, + + filegrowth=10% + +) + +go + +use GoodsDB + +go +drop database GoodsDB +drop table GoodsType +create table GoodsType --商品类型表 +( +TypeID int primary key not null ,--商品类型编号 + +TypeName nvarchar(20) not null --商品类型名称 +) + + + +create table GoodsInfo --商品信息表 +( +GoodsID int primary key not null,--商品编号 +GoodsName nvarchar(20) not null, --商品名称 +GoodsColor nvarchar(20) not null, --商品颜色 +GoodsBrand nvarchar(20) ,--商品品牌 +GoodsMoney money not null ,--商品价格 +TypeID int foreign key references GoodsType(TypeID) --商品类型编号 +) + +insert into GoodsType +select 1,'服装内衣'union +select 2,'鞋包配饰' union +select 3,'手机数码' + + +select * from GoodsType + + +insert into GoodsInfo +select 1,'提花小西装',' 红色', '菲曼琪', 300, 1 union +select 2,'百搭短裤', '绿色', '哥弟', 100 ,1 union +select 3,'无袖背心', '白色', '阿依莲', 700 ,1 union +select 4,'低帮休闲鞋', '红色', '菲曼琪', 900 ,2 union +select 5,'中跟单鞋', '绿色', '哥弟', 400 ,2 union +select 6,'平底鞋', '白色', '阿依莲', 200 ,2 union +select 7,'迷你照相机', '红色', '尼康', 500 ,3 union +select 8,'硬盘', '黑色', '希捷', 600 ,3 union +select 9,'显卡', '黑色', '技嘉', 80,3 + +drop table GoodsInfo +select * from GoodsInfo + + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsMoney 价格最贵,GoodsName 商品名称, GoodsColor 商品颜色 from GoodsInfo order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select GoodsID 商品类型编号,MAX(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by GoodsID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo where GoodsColor ='红色' and GoodsMoney between 300 and 600 + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 + +select * from GoodsInfo +select avg(Y.GoodsMoney) 平均价格,Y.GoodsBrand 商品的类别 from GoodsInfo Y inner join GoodsType M on Y.TypeID=M.TypeID group by Y.GoodsBrand + +--7、查询每种颜色的商品数 +select count(GoodsColor) 颜色的商品数, GoodsColor 颜色 from GoodsInfo group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” + +alter table GoodsType add giao text +update GoodsType set giao='我是商品类型信息备注' + +select * from GoodsType + + + +--添加创建时间的字段 +alter table GoodsType add time text + + +--9、在商品信息表中删除商品类型的外键约束 +select * from GoodsType +select * from GoodsInfo +alter table GoodsInfo drop column typeid +alter table GoodsInfo drop FK__GoodsInfo__TypeI__19FFD4FC +--10、删除所有商品类型记录 +drop table GoodsInfo diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e97e3432ea3d9058b01bd9c50309cb9c1f235188 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery2.sql" @@ -0,0 +1,60 @@ +use master +go + +create database GoodsDB1 +go + +use GoodsDB1 +go +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) + +insert into GoodsType +values ('服装内衣'),('鞋包配饰'),('手机数码') +select* from GoodsType + +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +values ('提花小西装',' 红色',' 菲曼琪', 300, 1),('百搭短裤',' 绿色',' 哥弟', 100 ,1),('无袖背心',' 白色 ','阿依莲' ,700, 1), +('低帮休闲鞋',' 红色',' 菲曼琪',900, 2),('中跟单鞋','绿色',' 哥弟', 400 ,2),('平底鞋',' 白色',' 阿依莲', 200, 2), +('迷你照相机',' 红色',' 尼康', 500 ,3),('硬盘',' 黑色', '希捷' ,600 ,3),('显卡',' 黑色',' 技嘉', 800, 3) +select * from GoodsInfo +select* from GoodsType +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select TOP 1 GoodsMoney 商品价格,GoodsName 商品名称,GoodsColor 商品颜色 from GoodsInfo + group by GoodsName,GoodsMoney,GoodsColor order by GoodsMoney DESC + +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodsMoney) 商品最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID + +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + +select * from GoodsInfo +select* from GoodsType +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select a.TypeID,a.TypeName, avg(GoodsMoney) from GoodsType a join GoodsInfo s on a.TypeID=s.TypeID group by a.TypeID, a.TypeName + +--7、查询每种颜色的商品数 +select GoodsColor,count(GoodsColor)每种颜色的商品数 from GoodsInfo group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add remark text default ('我是商品类型信息备注') +update GoodsType set remark=default +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop constraint FK_TypeID +--10、删除所有商品类型记录 +alter table GoodsType drop column TypeName + + diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bf9db41a248aee8fedaec4d77a65080c1a933a4b --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery3.sql" @@ -0,0 +1,84 @@ +create database HOUSE_DB +on +( + name='HOUSE_DB', + size=5, + filename='D:\HOUSE_DB.mdf', + filegrowth=1, + maxsize=50 +) +log on +( + name='HOUSE_DB_log', + size=5, + filename='D:\HOUSE_DB_log.ldf', + filegrowth=1, + maxsize=50 +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int not null primary key identity, + type_name varchar(50) not null unique, +) +create table HOUSE +( + house_id int not null primary key identity, + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null foreign key references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +insert into HOUSE values('五五开',500,1),('PDD',800,3),('大司马',600,2) +select * from HOUSE +select * from HOUSE_TYPE where type_name like '%型%' +select house_name,house_price from HOUSE order by house_price desc +select house_name,type_name from HOUSE t inner join HOUSE_TYPE x on t.type_id=x.type_id +select top 1 house_name,house_price from HOUSE order by house_price desc +select x.type_name,count(t.type_id) 房屋数量 from HOUSE t join HOUSE_TYPE x on t.type_id=x.type_id +group by x.type_name +--修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +select * from HOUSE +alter table HOUSE alter column house_name nvarchar(30) +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add address nvarchar(200) +--把房屋价格的默认值0改为100 +alter table house drop DF__HOUSE__house_pri__276EDEB3 +alter table house add CONSTRAINT DF default(100) for house_price +--查询出名字最长的房屋信息. +select * from HOUSE where len(house_name) = (select max(len(house_name)) from HOUSE) + +--查询所有房屋信息 +select * from HOUSE_TYPE s1 inner join HOUSE s2 on s1.type_id=s2.type_id +--使用模糊查询包含”型“字的房屋类型信息 +select type_name 房屋类型 from HOUSE_TYPE where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋的名称,house_price 租金 from HOUSE group by house_name,house_price order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select type_name 房屋类型,house_name 房屋名称 from HOUSE_TYPE s1 inner join HOUSE s2 on s1.type_id=s2.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_name 房屋名称,max(house_price)最高的租金 from HOUSE group by house_name + +select * from HOUSE_TYPE +select * from HOUSE + +--查询每种房屋类型的房屋数量 +select type_name,count(house_name) 房屋数量 from HOUSE_TYPE a join HOUSE s on a.type_id=s.type_id +group by type_name +--修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +alter table HOUSE alter column house_name nvarchar(30) +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add Address nvarchar(30) +--把房屋价格的默认值0改为100 +alter table HOUSE add constraint house_price default (100) for house_name +--查询出名字最长的房屋信息。 + +SELECT *from +(select type_id,len(house_name ) 房屋名字长度 from HOUSE )B +WHERE +房屋名字长度=(select max(房屋名称长度) from(select len(house_name )房屋名称长度 from HOUSE)A) +------------- +select max(房屋名称长度)房屋名字长度 from(select len(house_name )房屋名称长度 from HOUSE)A +select top 1 type_id,len(house_name )房屋名称长度 from HOUSE order by 房屋名称长度 desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery4.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..908962e5f5c9d41b2b063445bf28a78d15eda73b --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery4.sql" @@ -0,0 +1,55 @@ +create database StarManagerDB +on +( + name='StarManagerDB', + size=5, + filename='D:\StarManagerDB.mdf', + filegrowth=1, + maxsize=50 +) +log on +( + name='StarManagerDB_log', + size=5, + filename='D:\StarManagerDB_log.ldf', + filegrowth=1, + maxsize=50 +) +go +use StarManagerDB +go +create table StarType +( + T_NO int not null primary key identity, + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int not null primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) +insert into StarType values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo order by S_AGE desc +select count(S_NAME) 明星人数,avg(S_AGE) 明星平均年龄 from StarInfo group by S_T_NO having count(S_NAME)>2 +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo where S_T_NO=1 order by S_AGE desc +select T_NAME,count(S_T_NO) 明星类型的人数 from StarType t join StarInfo x on t.T_NO=x.S_T_NO group by T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add nsr money +alter table StarInfo add constraint de default(10000000) for nsr +--8、查询每个国家的明星人数。 +select S_NATIVE,COUNT(S_NATIVE)人数 from StarInfo GROUP BY S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 + select * from StarInfo order by S_NATIVE,s_age desc +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select a.*,b.T_NAME from StarInfo a +inner join StarType b on a.s_t_no=b.t_no +where a.s_name like '%西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarType add 创建时间 datetime default(getdate()) +alter table StarInfo add 创建时间 datetime default(getdate()) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/.keep" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\345\244\215\344\271\240\344\270\200\350\215\211\347\250\277.txt" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\345\244\215\344\271\240\344\270\200\350\215\211\347\250\277.txt" new file mode 100644 index 0000000000000000000000000000000000000000..f6f5ecf734003d8ef5bcb1e11a77ebdc805109db --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\345\244\215\344\271\240\344\270\200\350\215\211\347\250\277.txt" @@ -0,0 +1,119 @@ +use master +go + +create database GoodsDB +on primary +( +name='GoodsDB', +filename='D:\.mdf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) + +log on +( +name='GoodsDB_log', +filename='D:\_log.ldf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +go + +use GoodsDB + +----字段名 说明 类型 长度 可否为空 约束 +----TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +----TypeName 商品类型名称 nvarchar 20 否 + +create table GoodsType --建立商品类型表 +( +TypeID int primary key identity not null, +TypeName nvarchar(20) not null +) +go + +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +create table GoodsInfo --建立商品信息表 +( +GoodsID int primary key identity not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int foreign key (TypeID) references GoodsType(TypeID) +) +go + +select * from GoodsType + +insert into GoodsType(TypeName) values +('服饰内衣'), +('鞋包配饰'), +('手机数码') + +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 + +select * from GoodsInfo + +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) + +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select top 1 s1.GoodsName 商品名称,s1.GoodsColor 商品颜色,s1.GoodsMoney 商品价格 from GoodsInfo s1 order by s1.GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select s2.TypeID,count(s2.TypeID)数量,max(s2.GoodsMoney) 最高价格,min(s2.GoodsMoney) 最低价格,avg(s2.GoodsMoney) 平均价格 from GoodsInfo s2 group by s2.TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from(select top 6 s1.GoodsName,s1.GoodsColor,s1.GoodsMoney from GoodsInfo s1 order by s1.GoodsMoney) s2 where s2.GoodsColor='红色' +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 + +select s1.TypeName 类别名称,avg(s2.GoodsMoney) 平均价格 from GoodsType s1,GoodsInfo s2 where s1.TypeID=s2.TypeID group by s1.TypeName + +--7、查询每种颜色的商品数 + +select s1.GoodsColor 颜色,count(s1.GoodsColor) 数量 from GoodsInfo s1 group by s1.GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” + +alter table GoodsType add bz varchar(20) default('我是商品类型信息备注') + +--9、在商品信息表中删除商品类型的外键约束 + +alter table GoodsInfo drop constraint FK__GoodsInfo__TypeI__1273C1CD + +--10、删除所有商品类型记录 + +drop table GoodsType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\345\244\215\344\271\240\351\242\230\344\270\211.txt" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\345\244\215\344\271\240\351\242\230\344\270\211.txt" new file mode 100644 index 0000000000000000000000000000000000000000..73c39a6a0d9d3d651788a05bb8af28f815230829 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\345\244\215\344\271\240\351\242\230\344\270\211.txt" @@ -0,0 +1,103 @@ +use master +go + +create database StarManagerDB +on primary +( +name='D:\', +filename='D:\.mdf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +log on +( +name='D:\_log', +filename='D:\_log.ldf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +go + +--瀛楁鍚 璇存槑 绫诲瀷 闀垮害 鍙惁涓虹┖ 绾︽潫 +--T_NO 鏄庢槦绫诲瀷缂栧彿 int 鍚 涓婚敭绾︽潫锛岃嚜澧烇紝鏍囪瘑绉嶅瓙鍜屾爣璇嗗閲忛兘鏄1 +--T_NAME 鏄庢槦绫诲瀷 nvarchar 20 +use StarManagerDB + +create table StarType --寤虹珛鏄庢槦绫诲瀷琛 +( +T_NO int primary key identity not null, +T_NAME nvarchar(20) +) +go + +--瀛楁鍚 璇存槑 绫诲瀷 闀垮害 鍙惁涓虹┖ 绾︽潫 +--S_NO 鏄庢槦缂栧彿 int 鍚 涓婚敭绾︽潫锛岃嚜澧烇紝鏍囪瘑绉嶅瓙鍜屾爣璇嗗閲忛兘鏄1 +--S_NAME 鏄庢槦濮撳悕 nvarchar 20 鍚 +--S_AGE 鏄庢槦骞撮緞 int 鍚 +--S_HOBBY 鐗规妧 nvarchar 20 +--S_NATIVE 鏄庢槦绫嶈疮 nvarchar 20 榛樿绾︽潫锛氫腑鍥藉ぇ闄 +--S_T_NO 鏄庢槦绫诲瀷缂栧彿 int 澶栭敭锛屽弬鐓tarType琛ㄧ殑涓婚敭T_NO + +create table StarInfo --寤虹珛鏄庢槦淇℃伅琛 +( +S_NO int primary key identity not null, +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default('涓浗澶ч檰'), +S_T_NO int foreign key (S_T_NO) references StarType(T_NO) +) +go + +select * from StarType +insert into StarType(T_NAME) values +('浣撹偛鏄庢槦'), +('IT鏄庢槦'), +('鐩稿0婕斿憳') + +select * from StarInfo +insert into StarInfo values +('姊呰タ',30,'灏勯棬','闃挎牴寤',1), +('绉戞瘮',35,'杩囦汉','缇庡浗',1), +('钄℃櫙鐜',40,'鏁蹭唬鐮','涓浗',2), +('椹柉鍏',36,'閫犵伀绠','澶栨槦浜',2), +('閮痉绾',50,'鐩稿0','涓浗',3), +('榛勯摦',41,'鎷煎澶','涓浗',2) + + +--3銆佹煡璇㈠勾榫勬渶澶х殑3涓槑鏄熺殑濮撳悕锛岀壒鎶鍜岀睄璐俊鎭紝瑕佹眰浣跨敤鍒悕鏄剧ず鍒楀悕銆 + +select s1.S_NAME 濮撳悕,s1.S_HOBBY 鐗规妧,s1.S_NATIVE 绫嶈疮淇℃伅 from StarInfo s1 where s1.S_AGE=(select max(s2.S_AGE) from StarInfo s2) + +--4銆佹寜鏄庢槦绫诲瀷缂栧彿鍒嗙被鏌ヨ鏄庢槦浜烘暟锛屾槑鏄熷钩鍧囧勾榫勶紝鏄剧ず鏄庢槦浜烘暟澶т簬2鐨勫垎缁勪俊鎭紝瑕佹眰浣跨敤鍒悕鏄剧ず鍒楀悕銆 + +select s1.S_T_NO,count(s1.S_T_NO) 浜烘暟,avg(s1.S_AGE) 骞冲潎骞撮緞 from StarInfo s1 group by s1.S_T_NO having count(s1.S_T_NO)>=2 + +--5銆佹煡璇㈡槑鏄熺被鍨嬩负鈥滀綋鑲叉槑鏄熲濅腑骞撮緞鏈澶х殑濮撳悕銆佺壒鎶銆佺睄璐俊鎭紝瑕佹眰鏄剧ず鍒楀埆鍚嶃 + +select top 1 s2.S_NAME,s2.S_HOBBY,s2.S_NATIVE from StarType s1 inner join StarInfo s2 on s1.T_NO=s2.S_T_NO where s1.T_NO=1 order by s2.S_AGE desc +--6銆佽缁熻姣忕鏄庢槦绫诲瀷鐨勪汉鏁 + +select s1.S_T_NO,count(s1.S_T_NO) from StarInfo s1 group by s1.S_T_NO + +--7銆佺粰鏄庢槦琛ㄥ鍔犱竴涓滃勾鏀跺叆鈥濆瓧娈碉紝骞惰缃粯璁ゅ间负10000000锛1鍗冧竾锛 + +alter table StarInfo add nsr int default(10000000) + +--8銆佹煡璇㈡瘡涓浗瀹剁殑鏄庢槦浜烘暟銆 + +select s1.S_NATIVE,count(s1.S_NATIVE) from StarInfo s1 group by s1.S_NATIVE + +--9銆佹悳绱㈡墍鏈夋槑鏄熶俊鎭佹槑鏄熺被鍨嬪悕绉帮紝鎸夌睄璐帓搴忥紝鑻ョ睄璐竴鏍凤紝鎸夊勾榫勯檷搴忔帓搴忋 + +select * from StarInfo s1 order by s1.S_NATIVE,s1.S_AGE desc + +--10銆佹悳绱㈠悕瀛椾负涓や釜瀛楋紝涓旀渶鍚庝竴涓瓧涓衡滆タ鈥濈殑鏄庢槦淇℃伅銆佹槑鏄熺被鍨嬪悕绉 + +select s1.T_NAME,s2.* from StarType s1,StarInfo s2 where s2.S_NAME like'%瑗' and s1.T_NO=s2.S_T_NO + +--11銆佺粰涓や釜琛ㄩ兘鏂板涓涓瓧娈碘滃垱寤烘椂闂粹濓紝榛樿鍊兼槸绯荤粺褰撳墠鏃堕棿銆 + +alter table StarType add sj getdate() diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\345\244\215\344\271\240\351\242\230\344\272\214.txt" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\345\244\215\344\271\240\351\242\230\344\272\214.txt" new file mode 100644 index 0000000000000000000000000000000000000000..016dcf7a1123dbf7e208449d6459445fad7bb3bc --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\345\244\215\344\271\240\351\242\230\344\272\214.txt" @@ -0,0 +1,107 @@ +use master +go + +create database HOUSE_DB +on primary +( +name='HOUSE_DB', +filename='D:\.mdf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) + +log on +( +name='HOUSE_DB_log', +filename='D:\_log.ldf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +go + +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 + +use HOUSE_DB + +create table HOUSE_TYPE +( +type_id int primary key identity not null, +type_name varchar(50) unique not null +) +go + +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 + +create table HOUSE +( +house_id int primary key identity, +house_name varchar(50) not null, +house_price float default(0) not null, +type_id int foreign key (type_id) references HOUSE_TYPE(type_id) +) +go + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +insert into HOUSE_TYPE values +('小户型'), +('经济型'), +('别墅') + +--HOUSE表中添加至少3条数据,不能全都为同一类型 + +insert into HOUSE values +('茅草房',100,1), +('套房',800,2), +('景江别院',20000,3) + +--4、添加查询 +select * from HOUSE_TYPE +select * from HOUSE +--查询所有房屋信息 + +select * from HOUSE_TYPE s1 join HOUSE s2 on s1.type_id=s2.type_id + +--使用模糊查询包含”型“字的房屋类型信息 + +select * from HOUSE_TYPE s1 join HOUSE s2 on s1.type_id=s2.type_id where type_name like'%型' + +--查询出房屋的名称和租金,并且按照租金降序排序 + +select s1.house_name,s1.house_price from HOUSE s1 order by s1.house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + +select s2.house_name,s1.type_name from HOUSE_TYPE s1 join HOUSE s2 on s1.type_id=s2.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + +select * from HOUSE s1 where s1.house_price=(select max(s2.house_price) from HOUSE s2) + +-- 查询每种房屋类型的房屋数量 + +select s1.type_id,count(s1.type_id) from HOUSE s1 group by s1.type_id + +-- 修改房屋表的房屋名称字段,把数据类型改成nvarchar(30) + +alter table HOUSE modify house_name varchar(30) + +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 + +alter table HOUSE add dz varchar(30) + +-- 把房屋价格的默认值0改为100 + +alter table HOUSE alter column house_price set default 100 + +--查询出名字最长的房屋信息。 + + select * from HOUSE s1 where s1.house_name=(select max(s2.house_name) from HOUSE s2 ) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/\344\275\234\344\270\232/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0d6307e4a613cc0e902ef5bb986fb9f474e343d8 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,62 @@ +use master +go +create database FoodsDB +on +( +name='FoodsDB', +filename='D:\SQL\FoodsDB.mdf', +size=5mb, +maxsize=100mb, +filegrowth=5mb +) +log on +( +name='FoodsDB_log', +filename='D:\SQL\FoodsDB_log.ldf', +size=5mb, +maxsize=100mb, +filegrowth=5mb +) +go +use FoodsDB +go +create table GoodsType +( +TypeID int not null primary key identity, +TypeName nvarchar(20) not null +) +go +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int constraint FK_GoodsInfo_TypeID references GoodsType(TypeID) +) +insert into GoodsType values ('服装内衣'),('鞋包数码'),('手机数码') +insert into GoodsInfo values('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟 ',400,2), +('平底鞋','白色','阿依莲',200,2),('迷你照相机','红色 ','尼康',500 ,3), +('硬盘','黑色','希捷',600,3),('显卡 ','黑色','技嘉',800,3) +select * from GoodsInfo +select * from GoodsType +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney=(select max(GoodsMoney) from GoodsInfo) +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型编号,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select TypeName,AVG(GoodsMoney) from GoodsInfo A inner join GoodsType B on A.TypeID=B.TypeID group by TypeName +--7、查询每种颜色的商品数 +select GoodsColor,count(GoodsColor) from GoodsInfo group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add remark text +insert into GoodsType(remark) values('我是商品类型信息备注') +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop [FK_GoodsInfo_TypeID] +--10、删除所有商品类型记录 +delete GoodsInfo diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/\344\275\234\344\270\232/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/\344\275\234\344\270\232/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2b45e113ad1fc872e45df6ecdeba975414e43232 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/\344\275\234\344\270\232/SQLQuery2.sql" @@ -0,0 +1,54 @@ +use master +go +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\SQL\HOUSE_DB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_log', +filename='D:\SQL\HOUSE_DB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +use HOUSE_DB +go +create table HOUSE_TYPE +( +type_id int primary key identity(1,1) not null, +type_name varchar(50) unique not null, +) +create table HOUSE +( +house_id int not null primary key identity, +house_name varchar(20) not null, +house_price float not null default(0), +type_id int not null references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +insert into HOUSE values('茅屋',600,1),('山洞',1000,2),('臭水沟',1500,3) +select * from HOUSE +select * from HOUSE_TYPE where type_name like '%型' +select house_name,house_price from HOUSE order by house_price DESC +select house_name,type_name from HOUSE_TYPE A inner join HOUSE B on A.type_id=B.type_id +select house_price,house_name from HOUSE where house_price=(select max(house_price) from HOUSE ) +select * from HOUSE_TYPE +select * from HOUSE +--查询每种房屋类型的房屋数量 +select type_id,count(*) from HOUSE group by type_id +--修改房屋信息表的类型名称字段,把数据类型改成nvarchar(30) +alter table HOUSE alter column HOUSE_NAME nvarchar(30) +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add dizhi text +--把房屋价格的默认值0改为100 +alter table HOUSE drop [DF__HOUSE__house_pri__1367E606] +go +alter table HOUSE add constraint house_price default(100) for house_price +--查询出名字最长的房屋信息。 +select * from HOUSE where house_id=(select max(len(HOUSE_name)) from HOUSE) diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/\344\275\234\344\270\232/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/\344\275\234\344\270\232/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ec31cbbbdcb2c5825fab825b12988e003c4721e6 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/\344\275\234\344\270\232/SQLQuery3.sql" @@ -0,0 +1,59 @@ +use master +go +create database StarManagerDB +on +( +name='StarManagerDB', +filename='D:\SQL\StarManagerDB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='StarManagerDB_log', +filename='D:\SQL\StarManagerDB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +use StarManagerDB +go +create table StarType +( +T_NO int not null primary key identity(1,1), +T_NAME nvarchar(20) +) +create table StarInfo +( +S_NO int not null primary key identity(1,1), +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int references StarType(T_NO) +) +insert into StarType values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2),('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) +select top 3 S_NAME 姓名,S_HOBBY 年龄,S_NATIVE 籍贯 from StarInfo order by S_AGE DESC +select count(S_NAME) 人数,AVG(S_AGE) 平均年龄 from StarInfo group by S_T_NO having count(S_NAME)>2 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_T_NO=1 and S_AGE=(select max(S_AGE) from StarInfo) +select * from StarInfo +select * from StarType +--6、请统计每种明星类型的人数 +select T_NAME,count(S_T_NO) from StarInfo A inner join StarType B on A.S_T_NO=B.T_NO group by T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add nianshouru int +go +alter table StarInfo add constraint DF_StarInfo_nianshouru default(10000000) for nianshouru +--8、查询每个国家的明星人数。 +select S_NATIVE,count(S_NATIVE) from StarInfo group by S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select A.*,T_NAME from StarInfo A inner join StarType B on A.S_T_NO=B.T_NO order by S_NATIVE,S_AGE DESC +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select A.*,T_NAME from StarInfo A inner join StarType B on A.S_T_NO=B.T_NO where S_NAME like '_西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add chuangjianshijian datetime +alter table StarType add chuangjianshijian datetime +alter table StarInfo add constraint DF_StarInfo_chuangjianshijian default(getdate()) for chuangjianshijian +alter table StarType add constraint DF_StarType_chuangjianshijian default(getdate()) for chuangjianshijian \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ea84bb1dca306cb45e89389306df2c7e3495f050 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" @@ -0,0 +1,104 @@ +create database GoodsDB +on +( +name='GoodsDB', +filename='D:\GoodsDB.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) +log on +( +name='GoodsDB_log', +filename='D:\GoodsDB_log.ldf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) +use GoodsDB + +create table GoodsType +( +TypeID int not null primary key identity(1,1), +TypeName nvarchar (20) not null +) + +create table GoodsInfo + +( +GoodsID int not null primary key identity (1,1), +Goodsname nvarchar (20) not null , +GoodsColor Nvarchar (20) not null, +GoodsBrand nvarchar (20) , +GoodsMoney money not null , +TypeID int foreign key references GoodsType(TypeID) + ) + + insert into GoodsType values + + ('服装内衣'), + + ('鞋包配饰'), + + ('手机数码') + + insert into GoodsInfo values + + ('提花小西装','红色','菲曼琪',300,1), + + ('百搭短裤 ','绿色','哥弟',100,1), + + (' 无袖背心','白色','阿依莲',700,1), + + (' 低帮休闲鞋','红色','菲曼琪',900,2), + + (' 中跟单鞋','绿色','哥弟',400,2), + + (' 平底鞋','白色','阿依莲',200,2), + + (' 迷你照相机','红色','尼康',500,3), + + (' 硬盘','黑色','阿依莲',600,3), + + ('显卡','黑色','技嘉',800,3) + + drop table GoodsInfo + + select * from GoodsType,GoodsInfo + + select * from GoodsInfo + + --3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + select * from Goodsinfo where goodsid in( select goodsid from GoodsInfo where goodsmoney in( + select max(goodsmoney) from GoodsInfo)) + + + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格, avg(GoodsMoney) 平均价格 + from GoodsInfo + join GoodsType + on GoodsType.TypeID=GoodsInfo.TypeID + group by TypeName + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and (GoodsMoney between 300 and 600) + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select GoodsType.TypeName ,平均价格 from +(select TypeID ,avg(GoodsMoney)平均价格 from GoodsInfo group by TypeID )A +join +GoodsType on A.TypeID=GoodsType.TypeID +--7、查询每种颜色的商品数 +select GoodsColor 颜色,count(*) 商品数 from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add 备注 nvarchar(50) +update GoodsType set 备注='我是商品类型信息备注' + +--9、在商品信息表中删除商品类型的外键约束 +select * from GoodsInfo +alter table GoodsInfo alter column TypeID char(50) --找出约束名称 +alter table GoodsInfo drop constraint FK__GoodsInfo__TypeI__1273C1CD +--10、删除所有商品类型记录 +alter table GoodsInfo drop column TypeID \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bfc362a7c6d8bb328818baa22009a738958da33e --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery2.sql" @@ -0,0 +1,88 @@ +use master +go +create database HOUSE_DB +on +( name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity, + type_name varchar(50) unique not null +) + +create table HOUSE +( + house_id int primary key identity, + house_name varchar(50) not null, + house_price float default(0) not null, + type_id int references HOUSE_TYPE(type_id) not null +) + +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +select*from HOUSE_TYPE +insert into HOUSE values('公园长椅',100,'1'),('黑心网吧',200,'2'),('黑心宾馆',100000000,'3') + +select*from HOUSE_TYPE +select*from HOUSE + +--4、添加查询 +--查询所有房屋信息 +select * from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id + +--使用模糊查询包含”型“字的房屋类型信息 +select*from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋的名称,house_price 租金 + from HOUSE + order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price 最高月租, house_name 房屋名称 from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id order by house_price desc + + +-- 查询每种房屋类型的房屋数量 +select HOUSE_TYPE.type_name,count(*)房屋数量 from HOUSE join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id group by HOUSE_TYPE.type_name +-- 修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +alter table HOUSE drop constraint FK__HOUSE__type_id__145C0A3F +alter table HOUSE alter column type_id nvarchar(30) + +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add 地址 nvarchar(50) +-- 把房屋价格的默认值0改为100 +alter table HOUSE alter column house_price int --找出约束名称 +alter table HOUSE drop constraint DF__HOUSE__house_pri__1367E606 --删除约束 +alter table HOUSE add constraint dk default(100) for house_price --改约束 +--查询出名字最长的房屋信息。 +select A. type_id ,房屋名字长度,HOUSE_TYPE.type_name 房屋名称 from + (select type_id, len(type_name) 房屋名字长度 from HOUSE_TYPE)A join HOUSE_TYPE on A.type_id=HOUSE_TYPE.type_id + where 房屋名字长度=(select max(房屋名字长度) from ( select len(type_name) 房屋名字长度 from HOUSE_TYPE )B) + + + + + diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..81fe9a0e83bf1ea5025aa91e11b7985c50d2b373 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery3.sql" @@ -0,0 +1,96 @@ +use master +go +create database StarManagerDB +on +( name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity, + T_NAME nvarchar(20) +) + +create table StarInfo +( + S_NO int primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +insert into StarType values('体育明星'),('IT明星'),('相声明星') + +insert into StarInfo values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1), + ('察景现',40,'敲代码',default,2),('马斯克',36,'造火箭','外星人',2), + ('郭德纲',50,'相声',default,3),('黄铮',41,'拼多多',default,2) + + +select*from StarType +select*from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 + from StarInfo + order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO ,count(*) 明星人数,avg(S_AGE) 明星平均年龄 + from StarInfo + group by S_T_NO + having count(*) >2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_AGE 最大的姓名,S_HOBBY 特技,S_NATIVE 籍贯 + from StarType + join StarInfo + on StarInfo.S_T_NO=StarType.T_NO + where T_NAME='体育明星' + order by S_AGE desc + + + select*from StarType +select*from StarInfo +-- 6、请统计每种明星类型的人数 +select S_T_NO 明星编号 ,StarType .T_NAME,A.该类型人数 + from(select S_T_NO ,count(*) 该类型人数 from StarInfo join StarType on StarType.T_NO=StarInfo.S_T_NO group by S_T_NO) A + join StarType on StarType.T_NO=A.S_T_NO + + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add 年收入 money default(10000000) + +--8、查询每个国家的明星人数。 +select S_NATIVE 国家名称,count(*) 人数 from StarInfo group by S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select * from StarInfo + join StarType + on StarInfo.S_T_NO=StarType.T_NO + order by S_NATIVE,S_AGE + desc + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarInfo + join StarType + on StarInfo.S_T_NO=StarType.T_NO + where S_NAME like '__' and S_NAME like'%西' + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 + + +alter table StarType add 创建时间 datetime default(getdate()) +alter table StarInfo add 创建时间 datetime default(getdate()) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3e2bbf879c12f3a6e7cfb0cfe9b0130eeb3cedc5 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" @@ -0,0 +1,75 @@ +create database GoodsDB +on +( + name='E:\GoodsDB', + filename='E:\GoodsDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log +on +( + name='E:\GoodsDB_log', + filename='E:\GoodsDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +use GoodsDB + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) + +insert into GoodsType values +('服饰内衣'), +('鞋包配饰'), +('手机数码') +insert into GoodsInfo values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +select * from GoodsType +select * from GoodsInfo + +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 + from GoodsInfo order by GoodsMoney desc + +select TypeID 编号,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 + from GoodsInfo group by TypeID + + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney<=600 and GoodsMoney>=300 + + +select GoodsType.TypeName 名称 ,AVG(GoodsMoney) 平均价格 from GoodsInfo +inner join GoodsType on GoodsInfo.TypeID=GoodsType.TypeID group by GoodsType.TypeName + + select GoodsColor,count(GoodsColor)商品数 from GoodsInfo +group by GoodsColor + + +alter table GoodsType add mark nvarchar(50) default('我是商品类型信息备注') not null + + +alter table GoodsInfo drop constraint FK_TypeID + + +delete GoodsType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e8184c8a4049fab7bce91058597f401881de85ac --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery2.sql" @@ -0,0 +1,92 @@ +create database HOUSE_DB +on +( + name='D:\HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log +on +( + name='D:\HOUSE_DB-log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +use HOUSE_DB + +create table HOUSE_TYPE +( + type_id int not null primary key identity, + type_name varchar(50) not null unique +) +create table HOUSE +( + house_id int not null primary key identity, + house_name varchar(50) not null , + house_price float not null default(0), + type_id int not null foreign key references HOUSE_TYPE(type_id) +) + +insert into HOUSE_TYPE values +('小户型'), +('经济型'), +('别墅') +insert into HOUSE values +('坟',6000,1), +('公园长椅',2000,2), +('四合院',30000,3) + +select * from HOUSE_TYPE +select * from HOUSE + +--查询所有房屋信息 + +select * from HOUSE_TYPE left join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id + +--使用模糊查询包含”型“字的房屋类型信息 + +select * from HOUSE_TYPE where type_name like '__型' + +--查询出房屋的名称和租金,并且按照租金降序排序 + +select house_name 名称,house_price 租金 from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + +select type_name 房屋类型,house_name 房屋名称 from HOUSE_TYPE right join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + + +select top 1 house_price 租金,house_name 名称 from HOUSE order by house_price desc + + +--查询每种 房屋类型 的 房屋数量 + +select HOUSE_TYPE.type_name,COUNT(*) 数量 from HOUSE +inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id group by HOUSE_TYPE.type_name + +-- 修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) + +alter table HOUSE_TYPE drop UQ__HOUSE_TY__543C4FD94714335E +alter table HOUSE_TYPE alter column type_name nvarchar(30) + +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 + +alter table HOUSE add houseAddress nvarchar(50) + +-- 把房屋价格的默认值0改为100 + +alter table HOUSE drop constraint DF_house_price + +--alter table 表名 add constraint 约束名字 DEFAULT 默认值 for 字段名称 + +alter table HOUSE add constraint DF_house_price default(100) for house_price + +--查询出名字最长的房屋信息。 + +select HOUSE.* from HOUSE where len(house_name)=(select max(len(house_name)) from HOUSE) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..53b2d528d5587fb653a956fa946ccb08e5e9c092 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery3.sql" @@ -0,0 +1,97 @@ +create database StarManagerDB +on +( + name='D:\StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log +on +( + name='D:\StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +use StarManagerDB + +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) + +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄峥',41,'拼多多','中国',2) + +select * from StarType +select * from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 +from StarInfo order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + +select T_NO,count(*)人数,avg(S_AGE)平均年龄 from StarType join StarInfo on T_NO=S_T_NO group by T_NO having count(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + + +select * from StarInfo +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo join StarType +on T_NO=S_T_NO +where T_NAME='体育明星' +order by S_AGE desc + +--6、请统计每种明星类型的人数 + +select StarType.T_NAME,COUNT(*) 人数 from StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO group by StarType.T_NAME + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) + +alter table StarInfo add s_monry money default(10000000.00) not null + +--8、查询每个国家的明星人数。 + +select S_NATIVE,COUNT(*) 人数 from StarInfo group by S_NATIVE + +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 + +select StarType.T_NAME 类型,StarInfo.* from StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO order by S_NATIVE,S_AGE DESC + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 + +select StarInfo.*,StarType.T_NAME 类型 from StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO where S_NAME like '_西' + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 + +alter table StarType add s_time datetime default(getdate()) not null +alter table StarInfo add s_time datetime default(getdate()) not null + + \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a96bd35bcba25d21f0043c5f63da71985db8ff0f --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" @@ -0,0 +1,54 @@ +create database GoodsDB +on +( name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=5mb, + maxsize=100mb, + filegrowth=5mb +) +log on +( name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=5mb +) +go +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity, + TypeName nvarchar(20) not null + +) + +create table GoodsInfo +( + GoodsID int primary key identity, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null, + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +insert into GoodsInfo values('提花小西装', '红色', '菲曼琪', '300', 1),('百搭短裤', '绿色', '哥弟', '100', 1), + ('无袖背心', '白色', '阿依莲', '700', 1),('低帮休闲鞋', '红色', '菲曼琪', '900', 2), + ('中跟单鞋', '绿色', '哥弟', '400', 2),('平底鞋', '白色', '阿依莲','200', 2), + ('迷你照相机', '红色', '尼康', '500', 3),('硬盘', '黑色', '希捷', '600', 3), + ('显卡', '黑色', '技嘉','800', 3) +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc +select max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 +select TypeID 类别名称,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +select GoodsColor,count(GoodsColor)商品数 from GoodsInfo group by GoodsColor +alter table GoodsInfo add bz nvarchar(200) +update GoodsInfo set bz=('我是商品类型信息备注') +--9、在商品信息表中删除商品类型的外键约束 + alter table GoodsInfo drop constraint FK_TypeID +--10、删除所有商品类型记录 + alter table GoodsType drop column TypeName \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a67cc4eefffb2fcb4d44707281f59401a75e8f9a --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" @@ -0,0 +1,51 @@ +create database HOUSE_DB +on +( + name='HOUSE_DB', + size=5, + filename='D:\HOUSE_DB.mdf', + filegrowth=1, + maxsize=50 +) +log on +( + name='HOUSE_DB_log', + size=5, + filename='D:\HOUSE_DB_log.ldf', + filegrowth=1, + maxsize=50 +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int not null primary key identity, + type_name varchar(50) not null unique, +) +create table HOUSE +( + house_id int not null primary key identity, + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null foreign key references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +insert into HOUSE values('五五开',500,1),('PDD',800,3),('大司马',600,2) +select * from HOUSE +select * from HOUSE_TYPE where type_name like '%型%' +select house_name,house_price from HOUSE order by house_price desc +select house_name,type_name from HOUSE t inner join HOUSE_TYPE x on t.type_id=x.type_id +select top 1 house_name,house_price from HOUSE order by house_price desc +select x.type_name,count(t.type_id) 房屋数量 from HOUSE t join HOUSE_TYPE x on t.type_id=x.type_id +group by x.type_name +--修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +select * from HOUSE +alter table HOUSE alter column house_name nvarchar(30) +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add address nvarchar(200) +--把房屋价格的默认值0改为100 +alter table house drop DF__HOUSE__house_pri__276EDEB3 +alter table house add CONSTRAINT DF default(100) for house_price +--查询出名字最长的房屋信息. +select * from HOUSE where len(house_name) = (select max(len(house_name)) from HOUSE) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d0559eaa6eb1db26cae9ab14ef6c2fbc8d1600ef --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery3.sql" @@ -0,0 +1,55 @@ +create database StarManagerDB +on +( + name='StarManagerDB', + size=5, + filename='D:\StarManagerDB.mdf', + filegrowth=1, + maxsize=50 +) +log on +( + name='StarManagerDB_log', + size=5, + filename='D:\StarManagerDB_log.ldf', + filegrowth=1, + maxsize=50 +) +go +use StarManagerDB +go +create table StarType +( + T_NO int not null primary key identity, + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int not null primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) +insert into StarType values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo order by S_AGE desc +select count(S_NAME) 明星人数,avg(S_AGE) 明星平均年龄 from StarInfo group by S_T_NO having count(S_NAME)>2 +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo where S_T_NO=1 order by S_AGE desc +select T_NAME,count(S_T_NO) 明星类型的人数 from StarType t join StarInfo x on t.T_NO=x.S_T_NO group by T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add nsr money +alter table StarInfo add constraint de default(10000000) for nsr +--8、查询每个国家的明星人数。 +select S_NATIVE,COUNT(S_NATIVE)人数 from StarInfo GROUP BY S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 + select * from StarInfo order by S_NATIVE,s_age desc +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select a.*,b.T_NAME from StarInfo a +inner join StarType b on a.s_t_no=b.t_no +where a.s_name like '%西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarType add 创建时间 datetime default(getdate()) +alter table StarInfo add 创建时间 datetime default(getdate()) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..278cdfbda2e7f8c24c16c6b495332cd9002e9854 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" @@ -0,0 +1,111 @@ +use master +go +create database StarManagerDB +on +( name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity, + T_NAME nvarchar(20) +) + +create table StarInfo +( + S_NO int primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +insert into StarType values('体育明星'),('IT明星'),('相声明星') + +insert into StarInfo values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1), + ('察景现',40,'敲代码',default,2),('马斯克',36,'造火箭','外星人',2), + ('郭德纲',50,'相声',default,3),('黄铮',41,'拼多多',default,2) + + +select*from StarType +select*from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 + from StarInfo + order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO ,count(*) 明星人数,avg(S_AGE) 明星平均年龄 + from StarInfo + group by S_T_NO + having count(*) >2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_AGE 最大的姓名,S_HOBBY 特技,S_NATIVE 籍贯 + from StarType + join StarInfo + on StarInfo.S_T_NO=StarType.T_NO + where T_NAME='体育明星' + order by S_AGE desc + + + select*from StarType +select*from StarInfo +-- 6、请统计每种明星类型的人数 + + + + + + + + + + +select S_T_NO 明星编号 ,StarType .T_NAME,A. 该类型人数 + from(select S_T_NO ,count(*) 该类型人数 from StarInfo join StarType on StarType.T_NO=StarInfo.S_T_NO group by S_T_NO) A + join StarType on StarType.T_NO=A.S_T_NO + + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add 年收入 money default(10000000) + +update StarInfo set 年收入='10000000' + +--8、查询每个国家的明星人数。 +select S_NATIVE 国家名称,count(*) 人数 from StarInfo group by S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 + +select * +from StarInfo a join StarType b on a.S_T_NO=b.T_NO +order by S_NATIVE,S_AGE +desc + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 + + +select * +from StarInfo a join StarType b on a.S_T_NO=b.T_NO +where S_NAME like '__' and S_NAME like '%西' + + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +select*from StarType +select*from StarInfo + +alter table StarType add 创建时间 datetime default(getdate()) +alter table StarInfo add 创建时间 datetime default(getdate()) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5cf63d5369884c943e1fde0281361ce16b33af9b --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" @@ -0,0 +1,91 @@ +USE master +GO +CREATE DATABASE GoodsDB +on +( +name='wq', +filename='D:\wq.mdf', +size=5 + +) +log on +( +name='wq_log', +filename='D:\wq_log.ldf', +size=5 +) +GO +use GoodsDB +GO +CREATE TABLE GoodsType +( +TypeID INT NOT NULL IDENTITY(1,1), +TypeName NVARCHAR(20) NOT NULL +) +GO +ALTER TABLE GoodsType add constraint DK_GoodsType_TYPEID PRIMARY KEY(TYPEID) +SELECT * FROM GoodsType +INSERT INTO GoodsType(TypeName) +values('服装内衣'), +('鞋包配饰'), +('手机数码') +CREATE TABLE GoodsInfo +( +GoodsID INT NOT NULL IDENTITY(1,1), +GoodsName NVARCHAR(20) NOT NULL, +GoodsColor NVARCHAR(20) NOT NULL , +GoodsBrand NVARCHAR(20) , +GoodsMoney MONEY NOT NULL, +TypeID INT +) +GO +ALTER TABLE GoodsInfo ADD CONSTRAINT GK_GoodsInfo_GoodsID primary key(GoodsID) +ALTER TABLE GoodsInfo ADD CONSTRAINT JK_GoodsInfo_TypeID FOREIGN KEY(TYPEID) references GoodsType(TypeID) +SELECT * FROM GoodsInfo +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +values ('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100 ,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +SELECT top 1 GoodsName 名称,GoodsColor 颜色,max(GoodsMoney) 价格 FROM GoodsInfo group by GoodsName,GoodsColor order by max(GoodsMoney) desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select TypeID 编号,MAX(GoodsMoney)最高,min(GoodsMoney)最低,avg(GoodsMoney)平均 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + + +SELECT * FROM GoodsType +SELECT * FROM GoodsInfo + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 + +SELECT * FROM GoodsInfo +SELECT TypeName,round(AVG(GoodsMoney),2) 平均价格 FROM GoodsInfo inner join GoodsType on GoodsType.TypeID=GoodsInfo.TypeID group by TypeName + +SELECT TypeName,AVG(GoodsMoney) 平均价格 FROM GoodsInfo inner join GoodsType on GoodsType.TypeID=GoodsInfo.TypeID group by TypeName + +--7、查询每种颜色的商品数 + +SELECT GoodsColor,COUNT(GoodsID) 数量 FROM GoodsInfo GROUP BY GoodsColor + + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsInfo add 备注 nvarchar(20) +update GoodsInfo set 备注='我是商品类型信息备注' +--9、在商品信息表中删除商品类型的外键约束 + +alter table GoodsInfo drop constraint jk_GoodsInfo_TypeID + +--10、删除所有商品类型记录 +delete from GoodsInfo \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8e0fed332e7ff56cfb889c9efe3867df75bd7b6d --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery3.sql" @@ -0,0 +1,88 @@ +use master +go +create database HOUSE_DB +on +( name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=500, + filegrowth=1 +) + log on +( name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=500, + filegrowth=1 +) +go +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity, + type_name varchar(50) unique not null +) + +create table HOUSE +( + house_id int primary key identity, + house_name varchar(50) not null, + house_price float default(0) not null, + type_id int references HOUSE_TYPE(type_id) not null +) + +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +select*from HOUSE_TYPE +insert into HOUSE values('马云家',100,'1'),('马化腾家',200,'2'),('我家',100000000,'3') + +select*from HOUSE_TYPE +select*from HOUSE + +--4、添加查询 +--查询所有房屋信息 +select * from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id + +--使用模糊查询包含”型“字的房屋类型信息 +select*from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋的名称,house_price 租金 + from HOUSE + order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price 最高月租, house_name 房屋名称 from HOUSE + join HOUSE_TYPE + on HOUSE_TYPE.type_id=HOUSE.type_id order by house_price desc + +-- 查询每种房屋类型的房屋数量 + +select type_name,COUNT(house_name) from HOUSE a inner join HOUSE_TYPE b on a.type_id=b.type_id group by type_name + +-- 修改房屋表的房屋名称字段,把数据类型改成nvarchar(30) + +alter table house alter column house_name nvarchar(30) + +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 + +alter table HOUSE add 地址 NVARCHAR(30) + +-- 把房屋价格的默认值0改为100 +alter table HOUSE alter column house_price int +alter table HOUSE drop constraint DF_HOUSE_house_pri_1367E606 +alter table HOUSE add constraint dk default(100) for house_price + + + +--查询出名字最长的房屋信息。 +select A. type_id ,房屋名字长度,HOUSE_TYPE.type_name 房屋名称 from (select type_id, len(type_name) 房屋名字长度 from HOUSE_TYPE)A join HOUSE_TYPE on A.type_id=HOUSE_TYPE.type_id + where 房屋名字长度=(select max(房屋名字长度) from ( select len(type_name) 房屋名字长度 from HOUSE_TYPE )B) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\244\215\344\271\2401.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\244\215\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c6a71f878adb74f9fca59fe88c0778cc3d5e59d2 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\244\215\344\271\2401.sql" @@ -0,0 +1,61 @@ +use master +go +create database GoodsDB +on( + name='GoodsDB', + filename='C:\TEXT\GoodsDB.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on ( + name='GoodsDB_log', + filename='C:\TEXT\GoodsDB_log.ldf', + size=5, + maxsize=100, + filegrowth=10% +) +go +use GoodsDB +go +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null, +) +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int , +) + alter table GoodsInfo add constraint FK_TypeID foreign key(TypeID)references GoodsType(TypeID) + select * from GoodsType + select * from GoodsInfo + insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + insert into GoodsInfo values('提花小西装','红色','菲曼琪',300.00,1),('百搭短裤','绿色','哥弟',100.00,1), + ('无袖背心','白色','阿依莲',700.00,1),('低帮休闲鞋','红色','菲曼琪',900.00,2), + ('中跟单鞋', '绿色', '哥弟', 400.00, 2),('平底鞋', '白色', '阿依莲', 200, 2), + ('迷你照相机','红色','尼康',500.00,3),('硬盘','黑色','希捷',600.00,3), + ('显卡','黑色','技嘉',800.00,3) +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + select top 1 GoodsName 姓名,GoodsColor 颜色,GoodsMoney 价格 from GoodsInfo order by GoodsMoney DESC +--4、按商品 类型编号分组查询 商品最高价格,最低价格和平均价格,要求使用别名显示列名 + select TypeID,MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 +--6、查询 每类商品 的 平均价格,要显示商品的 类别名称 和平均价格。 + select GoodsType.TypeName 名称 ,AVG(GoodsMoney) 平均价格 from GoodsInfo + inner join GoodsType on GoodsInfo.TypeID=GoodsType.TypeID group by GoodsType.TypeName +--7、查询 每种颜色 的商品数 + select GoodsColor,COUNT(*)商品数量 from GoodsInfo group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” + alter table GoodsType add mark nvarchar(50) default('我是商品类型信息备注') not null +--9、在商品信息表中删除商品类型的外键约束 + alter table GoodsInfo drop constraint FK_TypeID +--10、删除所有商品类型记录 + alter table GoodsType drop column TypeName diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\244\215\344\271\2402.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\244\215\344\271\2402.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cbb76cc21f6b2255e8a3312184778e195d987d6c --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\244\215\344\271\2402.sql" @@ -0,0 +1,64 @@ +use master +go +create database HOUSE_DB +on( + name='HOUSE_DB', + filename='C:\TEXT\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=1% +) +log on( + name='HOUSE_DB_log', + filename='C:\TEXT\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1% +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) not null, +) +alter table HOUSE_TYPE add constraint UK_type_name unique (type_name) +create table HOUSE +( + house_id int primary key identity(1,1) not null, + house_name varchar(50) not null, + house_price float not null , + type_id int references HOUSE_TYPE(type_id) not null, +) +alter table HOUSE add constraint DF_house_price default(0)for house_price +select * from HOUSE_TYPE +select * from HOUSE +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +insert into HOUSE values('宝山花园大别墅','12000',3),('山水花苑','2500',1),('香山居','3000',1),('骊山花园','5000',2), +('湘水居','2000',1) +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 名称,house_price 租金 from HOUSE order by house_price DESC +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,HOUSE_TYPE.type_name 房屋类型铭名称 from HOUSE +inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +--查询所有房屋 中月租最高 的房屋,显示最高的租金和房屋名称 +select top 1 house_name 房屋名称,house_price 房屋租金 from HOUSE order by house_price DESC +--查询每种 房屋类型 的 房屋数量 +select HOUSE_TYPE.type_name,COUNT(*) 数量 from HOUSE +inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id group by HOUSE_TYPE.type_name +-- 修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +alter table HOUSE_TYPE drop constraint UK_type_name +alter table HOUSE_TYPE alter column type_name nvarchar(30) not null +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add houseAddress nvarchar(50) +-- 把房屋价格的默认值0改为100 +alter table HOUSE drop constraint DF_house_price +--alter table 表名 add constraint 约束名字 DEFAULT 默认值 for 字段名称 +alter table HOUSE add constraint DF_house_price default(100) for house_price +--查询出名字最长的房屋信息。 +select HOUSE.* from HOUSE where len(house_name)=(select max(len(house_name)) from HOUSE) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\244\215\344\271\2403.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\244\215\344\271\2403.sql" new file mode 100644 index 0000000000000000000000000000000000000000..41470b59e06361586ede066dba8cd648b58837f9 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\345\244\215\344\271\2403.sql" @@ -0,0 +1,67 @@ +use master +go +create database StarManagerDB +on( + name='StarManagerDB', + filename='C:\TEXT\StarManagerDB.mdf', + size=5, + maxsize=50, + filegrowth=1% +) +log on( + name='StarManagerDB_log', + filename='C:\TEXT\StarManagerDB_log.ldf', + size=5, + maxsize=50, + filegrowth=1% +) +go +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20), +) +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO), +) +select * from StarType +select * from StarInfo +insert into StarType values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41, '拼多多','中国',2) +--查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE DESC +--按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 明星类型编号,COUNT(*)明星人数,AVG(S_AGE) 平均年龄 from StarInfo group by S_T_NO having COUNT(*)>2 +--查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名, S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO +where StarType.T_NAME='体育明星' order by S_AGE DESC +--6、请统计每种明星类型的人数 +select StarType.T_NAME,COUNT(*) 人数 from StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO group by StarType.T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add s_monry money default(10000000.00) not null +--8、查询每个国家的明星人数。 +select S_NATIVE,COUNT(*) 人数 from StarInfo group by S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select StarType.T_NAME 类型,StarInfo.* from StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO order by S_NATIVE,S_AGE DESC +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select StarInfo.*,StarType.T_NAME 类型 from StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO where S_NAME like '_西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarType add s_time datetime default(getdate()) not null +alter table StarInfo add s_time datetime default(getdate()) not null \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery423432423.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery423432423.sql" new file mode 100644 index 0000000000000000000000000000000000000000..720cc799d6b532dc12efe303fe4a83ab6419ff47 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery423432423.sql" @@ -0,0 +1,664 @@ +use master +go +create database GoodsDB +on( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on ( + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=5, + maxsize=100, + filegrowth=10% +) +go +use GoodsDB +go +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null, +) +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int references GoodsType(TypeID), +) + + insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + insert into GoodsInfo values('提花小西装','红色','菲曼琪',300.00,1),('百搭短裤','绿色','哥弟',100.00,1), + ('无袖背心','白色','阿依莲',700.00,1),('低帮休闲鞋','红色','菲曼琪',900.00,2), + ('中跟单鞋', '绿色', '哥弟', 400.00, 2),('平底鞋', '白色', '阿依莲', 200, 2), + ('迷你照相机','红色','尼康',500.00,3),('硬盘','黑色','希捷',600.00,3), + ('显卡','黑色','技嘉',800.00,3) + +--3、查询 价格最贵 的商品名称,商品颜色和商品价格,要求使用别名显示列名 + select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品金额 from GoodsInfo order by GoodsMoney DESC + select top 1 GoodsName as 商品名称,GoodsColor as 商品颜色,GoodsMoney as 商品金额 from GoodsInfo order by GoodsMoney DESC + select top 1 商品名称=GoodsName , 商品颜色=GoodsColor , 商品金额=GoodsMoney from GoodsInfo order by GoodsMoney DESC +--4、按商品 类型编号 分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + select TypeID 类型编号,MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 + select * from GoodsType + select * from GoodsInfo + select TypeName,avg(GoodsMoney) from GoodsType A inner join GoodsInfo B on A.TypeID=B.TypeID group by TypeName +--7、查询每种颜色的商品数 + select GoodsColor ,count(GoodsColor) from GoodsInfo group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” + alter table GoodsType add 备注 nvarchar(200) not null default('我是商品类型信息备注') +--9、在商品信息表中删除商品类型的外键约束 + alter table GoodsInfo drop constraint FK__GoodsInfo__TypeI__239E4DCF +--10、删除所有商品类型记录 + delete GoodsInfo + +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5Mb, + maxsize=50Mb, + filegrowth=1Mb +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5Mb, + maxsize=50Mb, + filegrowth=1Mb +) +go + +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity not null, + type_name varchar(50) unique not null +) + +create table HOUSE +( + house_id int primary key identity not null, + house_name varchar(50), + house_price float default('0') not null, + type_id int references HOUSE_TYPE(type_id) +) + +insert into HOUSE_TYPE values +('小户型'), +('经济型'), +('别墅') + +insert into HOUSE values +('123',1000,1), +('132',5000,2), +('321',99999,3) + +--查询所有房屋信息 +select * from HOUSE inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select type_name,house_price from HOUSE inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +order by house_price +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select type_name,house_name from HOUSE inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price,house_name from HOUSE order by house_price DESC +--查询每种房屋类型的房屋数量 +select * from HOUSE +select * from HOUSE_TYPE +select type_name,count(*) from HOUSE A inner join HOUSE_TYPE B on A.type_id=B.type_id group by type_name +--修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +alter table HOUSE_TYPE drop constraint UQ__HOUSE_TY__543C4FD9D1D6C8AC +alter table HOUSE_TYPE alter column type_name nvarchar(30) +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add 地址 nvarchar(100) +--把房屋价格的默认值0改为100 +alter table HOUSE drop constraint DF__HOUSE__house_pri__1367E606 +alter table HOUSE add default('100') for house_price +--查询出名字最长的房屋信息。 +select * from HOUSE group by house_id,house_name,house_price,type_id,HOUSE.地址 having len(house_name)=max(len(house_name)) + + +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20) +) + +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) + +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') + +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的姓名,特技和特技信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技, S_NATIVE 特技 from StarInfo order by S_AGE DESC +--4、按明星类型编号分类 查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 明星人数大于2的分组,count(S_NO) 明星人数,AVG(S_AGE) 明星平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order BY S_AGE DESC + +--6、请统计每种明星类型的人数 +select * from StarType +select * from StarInfo +select T_NAME,count(S_T_NO) from StarType A inner join StarInfo B on A.T_NO=B.S_T_NO group by T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add 年收入 varchar(200) not null default('10000000') +--8、查询每个国家的明星人数。 +select S_NATIVE ,count(S_NATIVE) from StarInfo group by S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select * from StarType A inner join StarInfo B on A.T_NO=B.S_T_NO order by S_NATIVE,S_AGE desc +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarType A inner join StarInfo B on A.T_NO=B.S_T_NO where S_NAME like '_西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarType add 创建时间 datetime not null default(getdate()) +alter table StarInfo add 创建时间 datetime not null default(getdate()) + + + + + + +-- 练习题目: +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers +select * from CourseInfo +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select C.*,A.Score 语文,B.Score 数学 from StudentCourseScore A join StudentCourseScore B on A.StudentId=B.StudentId join StudentInfo C on C.Id=A.StudentId +where A.CourseId=1 and B.CourseId=2 and A.Score=60 + +-- 3.查询在 成绩 表存在成绩的学生信息 +select * from StudentInfo + +select * from StudentCourseScore A join StudentInfo B on A.StudentId=B.StudentCode + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select StudentCode 学生编号,StudentName 学生姓名,count(CourseId) 选课总数,sum(Score) 总成绩 from StudentInfo join StudentCourseScore on StudentCourseScore.StudentId=StudentInfo.StudentCode +group by StudentCode,StudentName +-- 4.1 查有成绩的学生信息 + +select * from StudentCourseScore A join StudentInfo B on A.StudentId=B.StudentCode + +-- 5.查询「李」姓老师的数量 + +select count(TeacherName) 李姓老师的数量 from Teachers where TeacherName like '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 + + +select * from +(select * from StudentCourseScore where CourseId=(select TeacherId from CourseInfo +join Teachers on CourseInfo.TeacherId=Teachers.Id where TeacherName='张三')) A +left join StudentInfo B on B.StudentCode=A.StudentId + + +-- 7.查询没有学全所有课程的同学的信息 + + +select * from StudentInfo where StudentCode not in (select StudentCode from StudentInfo where +StudentCode in (select StudentId from StudentCourseScore where CourseId='1') +and StudentCode in (select StudentId from StudentCourseScore where CourseId='2') +and StudentCode in (select StudentId from StudentCourseScore where CourseId='3')) + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select * from StudentCourseScore +select * from StudentInfo +select * from Teachers +select * from CourseInfo + +select * from StudentInfo A left join StudentCourseScore B on A.StudentCode=B.StudentId where CourseId in +(select CourseId from StudentCourseScore where StudentId='1') and StudentCode!='01' + + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select * from StudentInfo where StudentCode in +(select CourseId from StudentCourseScore A join StudentInfo B on A.StudentId=B.StudentCode where StudentCode='01') and StudentCode!='01' + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select StudentName from StudentInfo where StudentCode not in(select StudentId from StudentCourseScore where CourseId=(select TeacherId from CourseInfo +join Teachers on CourseInfo.TeacherId=Teachers.Id where TeacherName='张三')) + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 + + + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + + +-- 14.查询各科成绩最高分、最低分和平均分: + + + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ +select * from StudentCourseScore + + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 + + + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + + +-- 18.查询各科成绩前三名的记录 + + + +-- 19.查询每门课程被选修的学生数 + + + +-- 20.查询出只选修两门课程的学生学号和姓名 + + + +-- 21.查询男生、女生人数 + + + +-- 22.查询名字中含有「风」字的学生信息 + + + +-- 23.查询同名同性学生名单,并统计同名人数 + + + +-- 24.查询 1990 年出生的学生名单 + + + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + + +-- 30.查询不及格的课程 + + + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + + + +-- 32.求每门课程的学生人数 + + + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + +-- 36.查询每门功成绩最好的前两名 + + + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + + +-- 38.检索至少选修两门课程的学生学号 + + + +-- 39.查询选修了全部课程的学生信息 + + + +-- 40.查询各学生的年龄,只按年份来算 + + + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + + +-- 42.查询本周过生日的学生 + + + +-- 43.查询下周过生日的学生 + + + +-- 44.查询本月过生日的学生 + + + +-- 45.查询下月过生日的学生 +use master +go + +create database ClassicDb +on + ( + name = 'ClassicDb', + filename = 'D:\ClassicDb.mdf', + size = 10mb, + maxsize = 50mb, + filegrowth = 10mb + ) + log on + ( + name = 'ClassicDb_log', + filename = 'D:\ClassicDb_log.ldf', + size = 10mb, + maxsize = 50mb, + filegrowth = 10mb + ) +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/0408.1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/0408.1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a7436d4a5a933c34a8e40c7ca87d53c5a45b2b43 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/0408.1.sql" @@ -0,0 +1,85 @@ +--创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +create table GoodsInfo( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int foreign key(TypeID) references GoodsType(TypeID) +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 +insert GoodsType(TypeName) values +('服装内衣'), +('鞋包配饰'), +('手机数码') + +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 + +--set identity_insert GoodsType ON--打开 +--set identity_insert GoodsInfo ON--打开 + +insert into GoodsInfo values +('提花小西装','红色','菲曼琪','300',1), +('百搭短裤','绿色','哥弟','100',1), +('无袖背心','白色','阿依莲','700',1), +('低帮休闲鞋','红色','菲曼琪','900',2), +('中跟单鞋','绿色','哥弟','400',2), +('平底鞋','白色','阿依莲','200',2), +('迷你照相机','红色','尼康','500',3), +('硬盘','黑色','希捷','600',3), +('显卡','黑色','技嘉','800',3) + +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsMoney 商品价格,GoodsName 商品名称,GoodsColor 商品颜色 from GoodsInfo order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and (GoodsMoney>=300 and GoodsMoney<=600) + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select B.TypeID,avg(A.GoodsMoney) 平均价格 from GoodsInfo A left join GoodsType B on A.TypeID=B.TypeID group by B.TypeID + +--7、查询每种颜色的商品数 +select GoodsColor,count(GoodsColor) 商品数 from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +select * from GoodsType +alter table GoodsType add remark text +update GoodsType set remark = '我是商品类型信息备注' +--9、在商品信息表中删除商品类型的外键约束 +select * from GoodsInfo +alter table GoodsInfo drop FK__GoodsInfo__TypeI__1273C1CD + +--10、删除所有商品类型记录 +delete GoodsType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/0408.2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/0408.2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..08a269e076e7723237fe191b50e09d74ca422e6c --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/0408.2.sql" @@ -0,0 +1,86 @@ +create database HOUSEDB +on( + name='HOUSEDB', + filename='D:\HOUSEDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) + +log on( +name='HOUSE_DB', +filename='D:\HOUSEDB.ldf' +) + +use HOUSEDB +go + +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 + +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 +create table HOUSE_TYPE( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null unique +) + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 + +create table HOUSE( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null foreign key references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 + +insert HOUSE_TYPE(type_name) values +('小户型'),('经济型'),('别墅') + +insert HOUSE values +('上海陆家嘴',1000,1), +('北京老胡同',5000,2), +('西双版纳别野',2000,3) + + +--查询所有房屋信息 +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where TYPE_NAME like '%型' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select A.house_name,A.house_price from HOUSE A order by A.house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select * from HOUSE +select * from HOUSE_TYPE +select A.house_name,B.type_name from HOUSE A inner join HOUSE_TYPE B on A.house_id = B.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price,house_name from HOUSE order by house_price desc + +-- 查询每种房屋类型的房屋数量 +select A.type_name ,count(*) from HOUSE_TYPE A group by type_name + +-- 修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +alter table HOUSE_TYPE drop UQ__HOUSE_TY__543C4FD94714335E +alter table HOUSE_TYPE alter column type_name nvarchar(30) + +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table House add dizhi nvarchar(30) +-- 把房屋价格的默认值0改为100 +alter table house drop DF__HOUSE__house_pri__276EDEB3 +alter table house add CONSTRAINT DF default(100) for house_price +--查询出名字最长的房屋信息。 +select top 1 len(house_name) ,A.* from HOUSE A + order by len(house_name) desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/0408.3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/0408.3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..74842f5c1f85feb3172a366996c5aa81eb638bb7 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\203\241\350\266\212/0408.3.sql" @@ -0,0 +1,100 @@ +--1、创建明星数据库(StarManagerDB),然后建立两张表,StarType(明星类型表),StarInfo(明星信息表),表结构分别如下: + +create database StarManagerDB +go +use StarManagerDB +go + +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 + +create table StarType( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO + +create table StarInfo( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) + +--使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 + +insert StarType values +('体育明星'), +('IT明星'), +('相声演员') + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + +insert StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄, +--显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 类型编号,count(S_T_NO)明星人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarType + inner join StarInfo on StarType.T_NO=StarInfo.S_T_NO where T_NAME='体育明星' + order by S_AGE desc +--6、请统计每种明星类型的人数 +select b.T_NAME,S_T_NO,count(S_T_NO)人数 from StarInfo a +inner join StarType b on a.S_T_NO=b.T_NO +group by S_T_NO,b.T_NAME + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add 年收入 int +alter table StarInfo drop FK__StarInfo__S_T_NO__1367E606 +alter table StarInfo add CONSTRAINT DF default(10000000) for 年收入 + +--8、查询每个国家的明星人数。 +select S_NATIVE,COUNT(S_NATIVE)人数 from StarInfo GROUP BY S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 + select * from StarInfo order by S_NATIVE,s_age desc + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select a.*,b.T_NAME from StarInfo a +inner join StarType b on a.s_t_no=b.t_no +where a.s_name like '%西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add time datetime +update StarInfo set time= getdate() + +alter table StarType add time datetime +update StarType set time= getdate() diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/0408.1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/0408.1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..70dfa3288d71dcc6dca7472b1394509a81d8a38e --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/0408.1.sql" @@ -0,0 +1,85 @@ +--创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +create table GoodsInfo( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int foreign key(TypeID) references GoodsType(TypeID) +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 +insert GoodsType(TypeName) values +('服装内衣'), +('鞋包配饰'), +('手机数码') + +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 + +--set identity_insert GoodsType ON--打开 +--set identity_insert GoodsInfo ON--打开 + +insert into GoodsInfo values +('提花小西装','红色','菲曼琪','300',1), +('百搭短裤','绿色','哥弟','100',1), +('无袖背心','白色','阿依莲','700',1), +('低帮休闲鞋','红色','菲曼琪','900',2), +('中跟单鞋','绿色','哥弟','400',2), +('平底鞋','白色','阿依莲','200',2), +('迷你照相机','红色','尼康','500',3), +('硬盘','黑色','希捷','600',3), +('显卡','黑色','技嘉','800',3) + +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsMoney 商品价格,GoodsName 商品名称,GoodsColor 商品颜色 from GoodsInfo order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and (GoodsMoney>=300 and GoodsMoney<=600) + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select B.TypeID,avg(A.GoodsMoney) 平均价格 from GoodsInfo A left join GoodsType B on A.TypeID=B.TypeID group by B.TypeID + +--7、查询每种颜色的商品数 +select GoodsColor,count(GoodsColor) 商品数 from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +select * from GoodsType +alter table GoodsType add remark text +update GoodsType set remark = '我是商品类型信息备注' +--9、在商品信息表中删除商品类型的外键约束 +select * from GoodsInfo +alter table GoodsInfo drop FK__GoodsInfo__TypeI__1273C1CD + +--10、删除所有商品类型记录 +delete GoodsType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/0408.2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/0408.2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e805ed8c581d1d11d8fc8674f0bae6e522c9cc94 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/0408.2.sql" @@ -0,0 +1,86 @@ +create database HOUSEDB +on( + name='HOUSEDB', + filename='D:\HOUSEDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) + +log on( +name='HOUSE_DB', +filename='D:\HOUSEDB.ldf' +) + +use HOUSEDB +go + +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 + +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 +create table HOUSE_TYPE( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null unique +) + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 + +create table HOUSE( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null foreign key references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 + +insert HOUSE_TYPE(type_name) values +('小户型'),('经济型'),('别墅') + +insert HOUSE values +('上海陆家嘴',1000,1), +('北京老胡同',5000,2), +('西双版纳别野',2000,3) + + +--查询所有房屋信息 +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where TYPE_NAME like '%型' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select A.house_name,A.house_price from HOUSE A order by A.house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select * from HOUSE +select * from HOUSE_TYPE +select A.house_name,B.type_name from HOUSE A inner join HOUSE_TYPE B on A.house_id = B.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price,house_name from HOUSE order by house_price desc + +-- 查询每种房屋类型的房屋数量 +select A.type_name ,count(*) from HOUSE_TYPE A group by type_name + +-- 修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +alter table HOUSE_TYPE drop UQ__HOUSE_TY__543C4FD94714335E +alter table HOUSE_TYPE alter column type_name nvarchar(30) + +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table House add dizhi nvarchar(30) +-- 把房屋价格的默认值0改为100 +alter table house drop DF__HOUSE__house_pri__276EDEB3 +alter table house add CONSTRAINT DF default(100) for house_price +--查询出名字最长的房屋信息。 +select top 1 len(house_name) ,A.* from HOUSE A + order by len(house_name) desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/0408.3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/0408.3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..eeed54bf20a3345bd7d944aa9000d2fb1934911c --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/0408.3.sql" @@ -0,0 +1,100 @@ +--1、创建明星数据库(StarManagerDB),然后建立两张表,StarType(明星类型表),StarInfo(明星信息表),表结构分别如下: + +create database StarManagerDB +go +use StarManagerDB +go + +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 + +create table StarType( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO + +create table StarInfo( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) + +--使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 + +insert StarType values +('体育明星'), +('IT明星'), +('相声演员') + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + +insert StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄, +--显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 类型编号,count(S_T_NO)明星人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarType + inner join StarInfo on StarType.T_NO=StarInfo.S_T_NO where T_NAME='体育明星' + order by S_AGE desc +--6、请统计每种明星类型的人数 +select b.T_NAME,S_T_NO,count(S_T_NO)人数 from StarInfo a +inner join StarType b on a.S_T_NO=b.T_NO +group by S_T_NO,b.T_NAME + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add 年收入 int +alter table StarInfo drop FK__StarInfo__S_T_NO__1367E606 +alter table StarInfo add CONSTRAINT DF default(10000000) for 年收入 + +--8、查询每个国家的明星人数。 +select S_NATIVE,COUNT(S_NATIVE)人数 from StarInfo GROUP BY S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 + select * from StarInfo order by S_NATIVE,s_age desc + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select a.*,b.T_NAME from StarInfo a +inner join StarType b on a.s_t_no=b.t_no +where a.s_name like '%西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add time datetime +update StarInfo set time= getdate() + +alter table StarType add time datetime +update StarType set time= getdate() diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\344\275\234\344\270\232/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0d6307e4a613cc0e902ef5bb986fb9f474e343d8 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,62 @@ +use master +go +create database FoodsDB +on +( +name='FoodsDB', +filename='D:\SQL\FoodsDB.mdf', +size=5mb, +maxsize=100mb, +filegrowth=5mb +) +log on +( +name='FoodsDB_log', +filename='D:\SQL\FoodsDB_log.ldf', +size=5mb, +maxsize=100mb, +filegrowth=5mb +) +go +use FoodsDB +go +create table GoodsType +( +TypeID int not null primary key identity, +TypeName nvarchar(20) not null +) +go +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int constraint FK_GoodsInfo_TypeID references GoodsType(TypeID) +) +insert into GoodsType values ('服装内衣'),('鞋包数码'),('手机数码') +insert into GoodsInfo values('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟 ',400,2), +('平底鞋','白色','阿依莲',200,2),('迷你照相机','红色 ','尼康',500 ,3), +('硬盘','黑色','希捷',600,3),('显卡 ','黑色','技嘉',800,3) +select * from GoodsInfo +select * from GoodsType +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney=(select max(GoodsMoney) from GoodsInfo) +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型编号,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select TypeName,AVG(GoodsMoney) from GoodsInfo A inner join GoodsType B on A.TypeID=B.TypeID group by TypeName +--7、查询每种颜色的商品数 +select GoodsColor,count(GoodsColor) from GoodsInfo group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add remark text +insert into GoodsType(remark) values('我是商品类型信息备注') +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop [FK_GoodsInfo_TypeID] +--10、删除所有商品类型记录 +delete GoodsInfo diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\344\275\234\344\270\232/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\344\275\234\344\270\232/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2b45e113ad1fc872e45df6ecdeba975414e43232 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\344\275\234\344\270\232/SQLQuery2.sql" @@ -0,0 +1,54 @@ +use master +go +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\SQL\HOUSE_DB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_log', +filename='D:\SQL\HOUSE_DB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +use HOUSE_DB +go +create table HOUSE_TYPE +( +type_id int primary key identity(1,1) not null, +type_name varchar(50) unique not null, +) +create table HOUSE +( +house_id int not null primary key identity, +house_name varchar(20) not null, +house_price float not null default(0), +type_id int not null references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +insert into HOUSE values('茅屋',600,1),('山洞',1000,2),('臭水沟',1500,3) +select * from HOUSE +select * from HOUSE_TYPE where type_name like '%型' +select house_name,house_price from HOUSE order by house_price DESC +select house_name,type_name from HOUSE_TYPE A inner join HOUSE B on A.type_id=B.type_id +select house_price,house_name from HOUSE where house_price=(select max(house_price) from HOUSE ) +select * from HOUSE_TYPE +select * from HOUSE +--查询每种房屋类型的房屋数量 +select type_id,count(*) from HOUSE group by type_id +--修改房屋信息表的类型名称字段,把数据类型改成nvarchar(30) +alter table HOUSE alter column HOUSE_NAME nvarchar(30) +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add dizhi text +--把房屋价格的默认值0改为100 +alter table HOUSE drop [DF__HOUSE__house_pri__1367E606] +go +alter table HOUSE add constraint house_price default(100) for house_price +--查询出名字最长的房屋信息。 +select * from HOUSE where house_id=(select max(len(HOUSE_name)) from HOUSE) diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\344\275\234\344\270\232/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\344\275\234\344\270\232/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ec31cbbbdcb2c5825fab825b12988e003c4721e6 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\344\275\234\344\270\232/SQLQuery3.sql" @@ -0,0 +1,59 @@ +use master +go +create database StarManagerDB +on +( +name='StarManagerDB', +filename='D:\SQL\StarManagerDB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='StarManagerDB_log', +filename='D:\SQL\StarManagerDB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +use StarManagerDB +go +create table StarType +( +T_NO int not null primary key identity(1,1), +T_NAME nvarchar(20) +) +create table StarInfo +( +S_NO int not null primary key identity(1,1), +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int references StarType(T_NO) +) +insert into StarType values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2),('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) +select top 3 S_NAME 姓名,S_HOBBY 年龄,S_NATIVE 籍贯 from StarInfo order by S_AGE DESC +select count(S_NAME) 人数,AVG(S_AGE) 平均年龄 from StarInfo group by S_T_NO having count(S_NAME)>2 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_T_NO=1 and S_AGE=(select max(S_AGE) from StarInfo) +select * from StarInfo +select * from StarType +--6、请统计每种明星类型的人数 +select T_NAME,count(S_T_NO) from StarInfo A inner join StarType B on A.S_T_NO=B.T_NO group by T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add nianshouru int +go +alter table StarInfo add constraint DF_StarInfo_nianshouru default(10000000) for nianshouru +--8、查询每个国家的明星人数。 +select S_NATIVE,count(S_NATIVE) from StarInfo group by S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select A.*,T_NAME from StarInfo A inner join StarType B on A.S_T_NO=B.T_NO order by S_NATIVE,S_AGE DESC +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select A.*,T_NAME from StarInfo A inner join StarType B on A.S_T_NO=B.T_NO where S_NAME like '_西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add chuangjianshijian datetime +alter table StarType add chuangjianshijian datetime +alter table StarInfo add constraint DF_StarInfo_chuangjianshijian default(getdate()) for chuangjianshijian +alter table StarType add constraint DF_StarType_chuangjianshijian default(getdate()) for chuangjianshijian \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye04.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye04.sql" new file mode 100644 index 0000000000000000000000000000000000000000..793ec9d7d3d066313f37efed3a1636018c8add3c --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye04.sql" @@ -0,0 +1,58 @@ +use master +go + +create database GoodsDB +go + +use GoodsDB +go +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) + +insert into GoodsType +values ('服装内衣'),('鞋包配饰'),('手机数码') +select* from GoodsType + +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +values ('提花小西装',' 红色',' 菲曼琪', 300, 1),('百搭短裤',' 绿色',' 哥弟', 100 ,1),('无袖背心',' 白色 ','阿依莲' ,700, 1), +('低帮休闲鞋',' 红色',' 菲曼琪',900, 2),('中跟单鞋','绿色',' 哥弟', 400 ,2),('平底鞋',' 白色',' 阿依莲', 200, 2), +('迷你照相机',' 红色',' 尼康', 500 ,3),('硬盘',' 黑色', '希捷' ,600 ,3),('显卡',' 黑色',' 技嘉', 800, 3) +select * from GoodsInfo +select* from GoodsType +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select TOP 1 GoodsMoney 商品价格,GoodsName 商品名称,GoodsColor 商品颜色 from GoodsInfo + group by GoodsName,GoodsMoney,GoodsColor order by GoodsMoney DESC + +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodsMoney) 商品最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID + +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + +select * from GoodsInfo +select* from GoodsType +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select a.TypeID,a.TypeName, avg(GoodsMoney) from GoodsType a join GoodsInfo s on a.TypeID=s.TypeID group by a.TypeID, a.TypeName + +--7、查询每种颜色的商品数 +select GoodsColor,count(GoodsColor)每种颜色的商品数 from GoodsInfo group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add remark text default ('我是商品类型信息备注') +update GoodsType set remark=default +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop constraint FK_TypeID +--10、删除所有商品类型记录 +alter table GoodsType drop column TypeName \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..77cce6ca721f6c9ec344d12bbbe542c8025f4dcf --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye1.sql" @@ -0,0 +1,77 @@ +use master +go +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\test\HOUSE_DB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1% +) +log on +( +name='HOUSE_DB_log', +filename='D:\test\HOUSE_DB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1% +) +use HOUSE_DB +go +create table HOUSE_TYPE +( +type_id int not null primary key identity(1,1), +type_name varchar(50) not null unique +) +create table HOUSE +( +house_id int not null primary key identity (1,1), +house_name varchar(50) not null, +house_price float not null default (0), +type_id int not null references HOUSE_TYPE (type_id), + +) +insert into HOUSE_TYPE (type_name) values ('小户型'),('经济型'),('别墅') +insert into HOUSE (house_name,house_price,type_id) +values('小户',1000,1), +('经济房',2000,2), +('豪华房',3000,3) +select * from HOUSE_TYPE +select * from HOUSE + +use HOUSE_DB +go + +--查询所有房屋信息 +select * from HOUSE_TYPE s1 inner join HOUSE s2 on s1.type_id=s2.type_id +--使用模糊查询包含”型“字的房屋类型信息 +select type_name 房屋类型 from HOUSE_TYPE where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋的名称,house_price 租金 from HOUSE group by house_name,house_price order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select type_name 房屋类型,house_name 房屋名称 from HOUSE_TYPE s1 inner join HOUSE s2 on s1.type_id=s2.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_name 房屋名称,max(house_price)最高的租金 from HOUSE group by house_name + +select * from HOUSE_TYPE +select * from HOUSE + +--查询每种房屋类型的房屋数量 +select type_name,count(house_name) 房屋数量 from HOUSE_TYPE a join HOUSE s on a.type_id=s.type_id +group by type_name +--修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +alter table HOUSE alter column house_name nvarchar(30) +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add Address nvarchar(30) +--把房屋价格的默认值0改为100 +alter table HOUSE add constraint house_price default (100) for house_name +--查询出名字最长的房屋信息。 + +SELECT *from +(select type_id,len(house_name ) 房屋名字长度 from HOUSE )B +WHERE +房屋名字长度=(select max(房屋名称长度) from(select len(house_name )房屋名称长度 from HOUSE)A) +------------- +select max(房屋名称长度)房屋名字长度 from(select len(house_name )房屋名称长度 from HOUSE)A +select top 1 type_id,len(house_name )房屋名称长度 from HOUSE order by 房屋名称长度 desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3d0b8ec290b5205efdcfb0920bd3150f7343312d --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye2.sql" @@ -0,0 +1,52 @@ +use master +go +create database StarManagerDB +use StarManagerDB +go +create table StarType +( +T_NO int not null primary key identity (1,1), +T_NAME nvarchar(20) +) +create table StarInfo +( +S_NO int not null primary key identity(1,1), +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default ('中国大陆'), +S_T_NO int references StarType(T_NO) +) +insert into StarType (T_NAME) values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) +select * from StarType +select * from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select TOP 3 S_NAME 姓名, S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo group by S_NAME,S_AGE,S_HOBBY,S_NATIVE +order by S_AGE DESC +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select T_NAME,count(S_T_NO)类型编号 ,avg(S_AGE)平均年龄 from StarType s1 inner join StarInfo s2 on s1.T_NO=s2.S_T_NO +group by T_NAME +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名, S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯,T_NAME 类型 from StarInfo s1 inner join StarType s2 on s1.S_T_NO=s2.T_NO where S_T_NO=1 group by S_NAME,S_AGE,S_HOBBY,S_NATIVE ,T_NAME +order by S_AGE desc +--6、请统计每种明星类型的人数 +select T_NAME,count(S_T_NO) 人数 from StarType a join StarInfo b on a.T_NO=b.S_T_NO +group by T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add income money default (10000000) +update StarInfo set income=default +select*from StarInfo +--8、查询每个国家的明星人数。 +select S_NATIVE,count (S_NATIVE)明星人数 from StarInfo group by S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select T_NAME,S_NAME,S_HOBBY,S_T_NO S_NATIVE,S_AGE from StarType a join StarInfo s on a.T_NO=s.S_T_NO order by S_NATIVE,S_AGE desc +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarInfo a join StarType s on a.S_T_NO=s.T_NO where S_NAME like '_西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarType add shijian time default (getdate()) diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b104ec9e683cf56f256d7c9e54c64636792f9046 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" @@ -0,0 +1,75 @@ +create database GoodsDB +on +( + name='E:\GoodsDB', + filename='E:\GoodsDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log +on +( + name='E:\GoodsDB_log', + filename='E:\GoodsDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +use GoodsDB + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) + +insert into GoodsType values +('服饰内衣'), +('鞋包配饰'), +('手机数码') +insert into GoodsInfo values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +select * from GoodsType +select * from GoodsInfo + +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 + from GoodsInfo order by GoodsMoney desc + +select TypeID 编号,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 + from GoodsInfo group by TypeID + + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney<=600 and GoodsMoney>=300 + + +select GoodsType.TypeName 名称 ,AVG(GoodsMoney) 平均价格 from GoodsInfo +inner join GoodsType on GoodsInfo.TypeID=GoodsType.TypeID group by GoodsType.TypeName + + select GoodsColor,count(GoodsColor)商品数 from GoodsInfo +group by GoodsColor + + +alter table GoodsType add mark nvarchar(50) default('我是商品类型信息备注') not null + + +alter table GoodsInfo drop constraint FK_TypeID + + +delete GoodsType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7c9e0aadf72d97d66633ffacc140372fb1f23f52 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery2.sql" @@ -0,0 +1,92 @@ +create database HOUSE_DB +on +( + name='D:\HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log +on +( + name='D:\HOUSE_DB-log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +use HOUSE_DB + +create table HOUSE_TYPE +( + type_id int not null primary key identity, + type_name varchar(50) not null unique +) +create table HOUSE +( + house_id int not null primary key identity, + house_name varchar(50) not null , + house_price float not null default(0), + type_id int not null foreign key references HOUSE_TYPE(type_id) +) + +insert into HOUSE_TYPE values +('小户型'), +('经济型'), +('别墅') +insert into HOUSE values +('坟',6000,1), +('公园长椅',2000,2), +('四合院',30000,3) + +select * from HOUSE_TYPE +select * from HOUSE + +--查询所有房屋信息 + +select * from HOUSE_TYPE left join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id + +--使用模糊查询包含”型“字的房屋类型信息 + +select * from HOUSE_TYPE where type_name like '__型' + +--查询出房屋的名称和租金,并且按照租金降序排序 + +select house_name 名称,house_price 租金 from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + +select type_name 房屋类型,house_name 房屋名称 from HOUSE_TYPE right join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + + +select top 1 house_price 租金,house_name 名称 from HOUSE order by house_price desc + + +--查询每种 房屋类型 的 房屋数量 + +select HOUSE_TYPE.type_name,COUNT(*) 数量 from HOUSE +inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id group by HOUSE_TYPE.type_name + +-- 修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) + +alter table HOUSE_TYPE drop UQ__HOUSE_TY__543C4FD94714335E +alter table HOUSE_TYPE alter column type_name nvarchar(30) + +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 + +alter table HOUSE add houseAddress nvarchar(50) + +-- 把房屋价格的默认值0改为100 + +alter table HOUSE drop constraint DF_house_price + +--alter table 表名 add constraint 约束名字 DEFAULT 默认值 for 字段名称 + +alter table HOUSE add constraint DF_house_price default(100) for house_price + +--查询出名字最长的房屋信息。 + +select HOUSE.* from HOUSE where len(house_name)=(select max(len(house_name)) from HOUSE) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..276465746ffc358555ac437f58bff29c4106cecf --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery3.sql" @@ -0,0 +1,97 @@ +create database StarManagerDB +on +( + name='D:\StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log +on +( + name='D:\StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +use StarManagerDB + +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) + +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄峥',41,'拼多多','中国',2) + +select * from StarType +select * from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 +from StarInfo order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + +select T_NO,count(*)人数,avg(S_AGE)平均年龄 from StarType join StarInfo on T_NO=S_T_NO group by T_NO having count(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + + +select * from StarInfo +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo join StarType +on T_NO=S_T_NO +where T_NAME='体育明星' +order by S_AGE desc + +--6、请统计每种明星类型的人数 + +select StarType.T_NAME,COUNT(*) 人数 from StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO group by StarType.T_NAME + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) + +alter table StarInfo add s_monry money default(10000000.00) not null + +--8、查询每个国家的明星人数。 + +select S_NATIVE,COUNT(*) 人数 from StarInfo group by S_NATIVE + +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 + +select StarType.T_NAME 类型,StarInfo.* from StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO order by S_NATIVE,S_AGE DESC + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 + +select StarInfo.*,StarType.T_NAME 类型 from StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO where S_NAME like '_西' + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 + +alter table StarType add s_time datetime default(getdate()) not null +alter table StarInfo add s_time datetime default(getdate()) not null + + \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8963b992899d5b663c1a571ec2f88208762b7f08 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" @@ -0,0 +1,70 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go +use GoodsDB +go +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null, + +) +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null, + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +insert into GoodsInfo values('提花小西装','红色','菲曼琪' ,'300', 1),('百搭短裤','绿色','哥弟' ,'100', 1), +('无袖背心', '白色', '阿依莲', '700', 1),('低帮休闲鞋', '红色',' 菲曼琪',' 900', 2),('中跟单鞋','绿色','哥弟','400', 2), +('平底鞋','白色','阿依莲','200', 2),('迷你照相机','红色','尼康','500',3),('硬盘','黑色','希捷', '600', 3), +('显卡','黑色','技嘉','800',3) + +select *from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称, GoodsColor 商品颜色, +GoodsMoney 商品价格 from GoodsInfo where GoodsMoney in(select max(GoodsMoney) from GoodsInfo ) + + + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select B.TypeID, max(GoodsMoney) 最高价格, min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo A +join GoodsType B on A.TypeID=B.TypeID group by B.TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select GoodsType.TypeName, avg(GoodsMoney) from GoodsInfo join GoodsType on GoodsInfo.TypeID=GoodsType.TypeID +group by GoodsType.TypeName +--7、查询每种颜色的商品数 +select count(GoodsID) 商品数,GoodsColor from GoodsInfo group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add 备注 varchar(20) +update GoodsType set 备注='我是商品类型信息备注' +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop constraint FK__GoodsInfo__TypeI__1273C1CD +--10、删除所有商品类型记录 +delete from GoodsType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..acb73854f546901ae728591d3377098571d59fa7 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery3.sql" @@ -0,0 +1,85 @@ +use master +go +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5MB, + maxsize=50MB, + filegrowth=1MB +) +log on +( + name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=1MB +) +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity (1,1) not null, + T_NAME nvarchar(20) +) +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default '中国大陆', + S_T_NO int references StarType(T_NO) +) + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 +insert into StarType values ('体育明星'),('IT明星'),('相声演员') +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 +insert into StarInfo values('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) +select * from StarType +select * from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_AGE 年龄, S_NAME 特技,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo order by S_AGE Desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select count(S_T_NO)明星人数,avg(S_AGE)平均年龄,T_NO 明星类型编号 from StarInfo join StarType on StarInfo.S_T_NO=StarType.T_NO group by T_NO having count(S_T_NO)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_AGE 年龄,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo where S_T_NO in (select T_NO from StarType where T_NAME='体育明星') order by S_AGE desc +--6、请统计每种明星类型的人数 +select count(S_NO) 人数,T_NAME from StarInfo join StarType on StarType.T_NO=StarInfo.S_T_NO group by T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add shouru int +alter table StarInfo add constraint de default(10000000) for shouru + +--8、查询每个国家的明星人数。 +select count(S_NO)明星人数,S_NATIVE 国家 from StarInfo group by S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select A.*,T_NAME from StarInfo A join StarType B on A.S_T_NO=B.T_NO +order by S_AGE,S_NATIVE desc +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select A.*,T_NAME from StarInfo A join StarType B on A.S_T_NO=B.T_NO where S_NAME like'_西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add time datetime +update StarInfo set time=GETDATE() +alter table StarType add time datetime +update StarType set time=GETDATE() \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery4.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..59002f017da5ce6700462056f5dbe990276e2623 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery4.sql" @@ -0,0 +1,65 @@ +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5MB, + maxsize=50MB, + filegrowth=1MB +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=1MB +) + +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) not null unique, + +) +create table HOUSE +( + house_id int primary key identity(1,1) not null, + house_name varchar(50) not null , + house_price float not null default('0'), + type_id int references HOUSE_TYPE (type_id) not null +) +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') +insert into HOUSE values ('小家','1800',1),('瓦房','1500',2),('小洋楼','2500',3) + +select * from HOUSE +--4、添加查询 +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型%' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price Desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name from HOUSE join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_price 最高的租金, house_name 房屋名称 from HOUSE + where house_price in (select max(house_price) from HOUSE) +-- 查询每种房屋类型的房屋数量 +select count(house_id) 房屋数量,type_name from HOUSE join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id group by type_name +-- 修改房屋表的房屋名称字段,把数据类型改成nvarchar(30) +alter table HOUSE_TYPE drop UQ__HOUSE_TY__543C4FD930FA770C +alter table HOUSE_TYPE alter column type_name nvarchar(30) +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add address varchar(40) +-- 把房屋价格的默认值0改为100 +alter table HOUSE drop DF__HOUSE__house_pri__1367E606 +alter table HOUSE add constraint df default ('100') for house_price + +--查询出名字最长的房屋信息。 +select top 1 * from HOUSE order by len(house_name) Desc diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..98444a6e444dfb232ef8cd0f5678535f35cb6462 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery1.sql" @@ -0,0 +1,46 @@ +use master +go +create database FoodsDB +go +use FoodsDB +go +create table GoodsType +( +TypeID int not null primary key identity, +TypeName nvarchar(20) not null +) +go +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int constraint FK_GoodsInfo_TypeID references GoodsType(TypeID) +) +insert into GoodsType values ('服装内衣'),('鞋包数码'),('手机数码') +insert into GoodsInfo values('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟 ',400,2), +('平底鞋','白色','阿依莲',200,2),('迷你照相机','红色 ','尼康',500 ,3), +('硬盘','黑色','希捷',600,3),('显卡 ','黑色','技嘉',800,3) +select * from GoodsInfo +select * from GoodsType +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney=(select max(GoodsMoney) from GoodsInfo) +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型编号,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select TypeName,AVG(GoodsMoney) from GoodsInfo A inner join GoodsType B on A.TypeID=B.TypeID group by TypeName +--7、查询每种颜色的商品数 +select GoodsColor,count(GoodsColor) from GoodsInfo group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add remark text +update GoodsType set remark='我是商品类型信息' +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop [FK_GoodsInfo_TypeID] +--10、删除所有商品类型记录 +delete GoodsInfo \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2c2073f1ac637677a6540a7d056213426ba4a15c --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery2.sql" @@ -0,0 +1,37 @@ +use master +go +create database HOUSE_DB +go +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int not null primary key identity, + type_name varchar(50) not null unique, +) +create table HOUSE +( + house_id int not null primary key identity, + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null foreign key references HOUSE_TYPE(type_id) +) +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') +insert into HOUSE values('五五开',500,1),('PDD',800,3),('大司马',600,2) +select * from HOUSE +select * from HOUSE_TYPE where type_name like '%型%' +select house_name,house_price from HOUSE order by house_price desc +select house_name,type_name from HOUSE t inner join HOUSE_TYPE x on t.type_id=x.type_id +select top 1 house_name,house_price from HOUSE order by house_price desc +select x.type_name,count(t.type_id) 房屋数量 from HOUSE t join HOUSE_TYPE x on t.type_id=x.type_id +group by x.type_name +--修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +select * from HOUSE +alter table HOUSE alter column house_name nvarchar(30) +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add address nvarchar(200) +--把房屋价格的默认值0改为100 +alter table house drop DF__HOUSE__house_pri__276EDEB3 +alter table house add CONSTRAINT DF default(100) for house_price +--查询出名字最长的房屋信息. +select * from HOUSE where len(house_name) = (select max(len(house_name)) from HOUSE)OUSE) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c062313e2b1d35176c5fa10b8e8552b604e69852 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery3.sql" @@ -0,0 +1,44 @@ +use master +go +create database StarManagerDB +go +use StarManagerDB +go +create table StarType +( +T_NO int not null primary key identity(1,1), +T_NAME nvarchar(20) +) +create table StarInfo +( +S_NO int not null primary key identity(1,1), +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int references StarType(T_NO) +) +insert into StarType values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2),('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) +select top 3 S_NAME 姓名,S_HOBBY 年龄,S_NATIVE 籍贯 from StarInfo order by S_AGE DESC +select count(S_NAME) 人数,AVG(S_AGE) 平均年龄 from StarInfo group by S_T_NO having count(S_NAME)>2 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_T_NO=1 and S_AGE=(select max(S_AGE) from StarInfo) +select * from StarInfo +select * from StarType +--6、请统计每种明星类型的人数 +select T_NAME,count(S_T_NO) from StarInfo A inner join StarType B on A.S_T_NO=B.T_NO group by T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add nianshouru int +go +alter table StarInfo add constraint DF_StarInfo_nianshouru default(10000000) for nianshouru +--8、查询每个国家的明星人数。 +select S_NATIVE,count(S_NATIVE) from StarInfo group by S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select A.*,T_NAME from StarInfo A inner join StarType B on A.S_T_NO=B.T_NO order by S_NATIVE,S_AGE DESC +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select A.*,T_NAME from StarInfo A inner join StarType B on A.S_T_NO=B.T_NO where S_NAME like '_西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add chuangjianshijian datetime +alter table StarType add chuangjianshijian datetime +alter table StarInfo add constraint DF_StarInfo_chuangjianshijian default(getdate()) for chuangjianshijian +alter table StarType add constraint DF_StarType_chuangjianshijian default(getdate()) for chuangjianshijian \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/.keep" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..154934bea5b7b28ec709e5315eed01d72132b41c --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery1.sql" @@ -0,0 +1,51 @@ +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +go + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int constraint FK_GoodsInfo_TypeID references GoodsType(TypeID) +) +go +insert into GoodsType values ('服装内衣'),('鞋包数码'),('手机数码') +insert into GoodsInfo values('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟 ',400,2), +('平底鞋','白色','阿依莲',200,2),('迷你照相机','红色 ','尼康',500 ,3), +('硬盘','黑色','希捷',600,3),('显卡 ','黑色','技嘉',800,3) +select * from GoodsInfo +select * from GoodsType +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo +where GoodsMoney = (select MAX(GoodsMoney) from GoodsInfo) +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select typeid 商品类型编号,max(goodsmoney) 商品最高价格 ,min(goodsmoney)最低价格,avg(goodsmoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where goodscolor='红色' and GoodsMoney between 300 and 600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select goodsinfo.typeid, typename 商品的类别,avg(goodsmoney)平均价格 from GoodsInfo +inner join GoodsType on GoodsInfo.TypeID= GoodsType.TypeID group by goodsinfo.typeid, typename +--7、查询每种颜色的商品数 +select goodscolor 颜色, count(goodsname)商品数 from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add remark text +insert into GoodsType(remark) values ('我是商品类型信息备注') +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop [FK_GoodsInfo_TypeID] +--10、删除所有商品类型记录 +delete GoodsInfo \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..021be69076648b2a16f7edae005d373adfbd3f94 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery2.sql" @@ -0,0 +1,52 @@ +use master +go +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb + +) +log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb + +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique not null +) +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null, + house_price float not null default(0), + type_id int references HOUSE_TYPE not null +) +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') +insert into HOUSE values ('御龙湾',3000,2),('汤臣一品',5000,3),('近郊园',2000,1) +select * from HOUSE_TYPE +select * from HOUSE +-- 查询每种房屋类型的房屋数量 +select TYPE_NAME 房屋类型,count(house_name)房屋数量 from HOUSE +inner join HOUSE_TYPE on HOUSE_TYPE.type_id= house.house_id group by TYPE_NAME +-- 修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +alter table HOUSE alter column HOUSE_NAME nvarchar(30) +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add dizhi text +-- 把房屋价格的默认值0改为100 +alter table HOUSE drop [DF__HOUSE__house_pri__1367E606] +go +--查询出名字最长的房屋信息。 +select * from HOUSE where house_id=(select max(len(HOUSE_name)) from HOUSE) diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ec31cbbbdcb2c5825fab825b12988e003c4721e6 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery3.sql" @@ -0,0 +1,59 @@ +use master +go +create database StarManagerDB +on +( +name='StarManagerDB', +filename='D:\SQL\StarManagerDB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='StarManagerDB_log', +filename='D:\SQL\StarManagerDB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +use StarManagerDB +go +create table StarType +( +T_NO int not null primary key identity(1,1), +T_NAME nvarchar(20) +) +create table StarInfo +( +S_NO int not null primary key identity(1,1), +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int references StarType(T_NO) +) +insert into StarType values('体育明星'),('IT明星'),('相声演员') +insert into StarInfo values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2),('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) +select top 3 S_NAME 姓名,S_HOBBY 年龄,S_NATIVE 籍贯 from StarInfo order by S_AGE DESC +select count(S_NAME) 人数,AVG(S_AGE) 平均年龄 from StarInfo group by S_T_NO having count(S_NAME)>2 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_T_NO=1 and S_AGE=(select max(S_AGE) from StarInfo) +select * from StarInfo +select * from StarType +--6、请统计每种明星类型的人数 +select T_NAME,count(S_T_NO) from StarInfo A inner join StarType B on A.S_T_NO=B.T_NO group by T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add nianshouru int +go +alter table StarInfo add constraint DF_StarInfo_nianshouru default(10000000) for nianshouru +--8、查询每个国家的明星人数。 +select S_NATIVE,count(S_NATIVE) from StarInfo group by S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select A.*,T_NAME from StarInfo A inner join StarType B on A.S_T_NO=B.T_NO order by S_NATIVE,S_AGE DESC +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select A.*,T_NAME from StarInfo A inner join StarType B on A.S_T_NO=B.T_NO where S_NAME like '_西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add chuangjianshijian datetime +alter table StarType add chuangjianshijian datetime +alter table StarInfo add constraint DF_StarInfo_chuangjianshijian default(getdate()) for chuangjianshijian +alter table StarType add constraint DF_StarType_chuangjianshijian default(getdate()) for chuangjianshijian \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9e6ed1badc8747ebd7da3752d0f013ba626d9479 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" @@ -0,0 +1,65 @@ +use master +go + +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) +go + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int constraint FK_GoodsInfo_TypeID references GoodsType(TypeID) +) +go + +insert into GoodsType(TypeName) values ('服装内衣'),('鞋包数码'),('手机数码') +go + +insert into GoodsInfo values('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟 ',400,2), +('平底鞋','白色','阿依莲',200,2),('迷你照相机','红色 ','尼康',500 ,3), +('硬盘','黑色','希捷',600,3),('显卡 ','黑色','技嘉',800,3) +go + +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo +where GoodsMoney in(select MAX(GoodsMoney) from GoodsInfo) + +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select MAX(GoodsMoney)最高价格,MIN(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo group by TypeID + +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor = '红色' and GoodsMoney between 300 and 600 + +--查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select TypeName,AVG(GoodsMoney)平均价格 from GoodsType GT left join GoodsInfo GI on GT.TypeID = GI.TypeID group by TypeName + +--查询每种颜色的商品数 +select GoodsColor,count(*)商品数量 from GoodsInfo group by GoodsColor + +--给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add remark text +go +alter table GoodsType add constraint DF_GoodsType_remark default('我是商品类型信息备注') for remark +update GoodsType(remark) set remark = ('我是商品类型信息备注') + +--在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop [FK_GoodsInfo_TypeID] + +--删除所有商品类型记录 +truncate table GoodsType + diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..189f93637a9f805b42ee7a785048efc26882f0c4 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" @@ -0,0 +1,77 @@ +use master +go + +create database HOUSE_DB +on primary +( + name = 'House', + filename = 'D:\Document\MSSQLDATABASE\House\house.mdf', + size = 5MB, + maxsize = 50MB, + filegrowth = 1MB +) +log on +( + name = 'House_log', + filename = 'D:\Document\MSSQLDATABASE\House\house_log.ldf', + size = 5MB, + maxsize = 10MB, + filegrowth = 1MB +) +go + +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, --类型编号 + type_name varchar(50) unique(type_name) not null, --类型名称 +) +go + +create table HOUSE +( + house_id int primary key identity(1,1) not null, --房屋编号 + house_name varchar(50) not null, --房屋名称 + house_price float default(0) not null, --房租 + type_id int references HOUSE_TYPE(type_id) not null +) +go + +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') +go + +insert into HOUSE values ('小户型',1000.0,1),('经济型',2000.5,2),('别墅',4000.9,3) +go + +--查询所有房屋信息 +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE where house_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name from HOUSE HS inner join HOUSE_TYPE HT on HS.type_id = HT.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_name,house_price from HOUSE where house_price in(select MAX(house_price) from HOUSE) + +--查询每种房屋类型的房屋数量 +select type_id,count(*)数量 from HOUSE group by type_id + +--修改房屋信息表的房屋名称字段,把数据类型改成nvarchar(30) +alter table HOUSE alter column HOUSE_NAME nvarchar(30) + +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add address text + +--把房屋价格的默认值0改为100 +alter table HOUSE drop [DF__HOUSE__house_pri__1367E606] +go +alter table HOUSE add constraint DF_HOUSE_HousePri default(100) for house_price +--查询出名字最长的房屋信息。 +select * from HOUSE where len(house_name) = (select max(len(house_name)) from HOUSE) diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4aedef00939a89057a7ec4a767bb73f3a5482d55 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery3.sql" @@ -0,0 +1,69 @@ +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity(1,1) not null,--明星类型编号 + T_NAME nvarchar(20) +) +go + +create table StartInfo +( + S_NO int primary key identity(1,1) not null,--明星编号 + S_NAME nvarchar(20) not null,--明星姓名 + S_AGE int not null,--明星年龄 + S_HOBBY nvarchar(20),--特技 + S_NATIVE nvarchar(20) default('中国大陆'), --籍贯 + S_T_NO int references StarType(T_NO) --明星类型编号 +) +go + +insert into StarType values ('体育明星'),('IT明星'),('相声演员') +go + +insert into StartInfo values ('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2),('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) +go + +--查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StartInfo + +--按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 类型编号,count(*) 人数,AVG(S_AGE)平均年龄 from StartInfo group by S_T_NO having COUNT(*) > 2 + +--查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select * from StartInfo ST where S_T_NO =(select T_NO from StarType where T_NAME = '体育明星') + +--请统计每种明星类型的人数 +select T_NAME,count(*)人数 from StartInfo SI +join StarType ST on SI.S_T_NO = ST.T_NO +group by T_name + +--给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StartInfo add priceYear int +go +alter table StartInfo add constraint DF_StarInfo_priceYear default(10000000) for priceYear + +--查询每个国家的明星人数。 +select S_NATIVE,COUNT(*)人数 from StartInfo group by S_NATIVE + +--搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select SI.*,ST.T_NAME from StartInfo SI +join StarType ST on SI.S_T_NO = T_NO +order by S_NATIVE,S_AGE asc + +--搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StartInfo where S_NAME like '_西' + +--给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StartInfo add createTime datetime +go +alter table StartInfo add constraint DF_StarInfo_createTime default(getdate()) for createTime diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7be57b57cdd0675b75b560a0d57384623077c7ac --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery1.sql" @@ -0,0 +1,72 @@ +use master +go +create database GoodsDB +on( + name='GoodsDB', + filename='D:\sql\GoodsDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on( + name='GoodsDB_log', + filename='D:\sql\GoodsDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use GoodsDB +go +create table GoodsType( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) +create table GoodsInfo( + GoodsID INT primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) +insert into GoodsType (TypeName) +values ('服装内衣'), ('鞋包配饰'), +( '手机数码') +select * from GoodsType + + insert into GoodsInfo + values ('提花小西装','红色','菲曼琪','300',1), + ('百搭短裤','绿色','哥弟','100',1),('无心背心','白色','阿依莲','700',1),('低帮休闲鞋','红色','菲曼琪','900',2),('中跟单鞋','绿色','哥弟','400',2), + ('平底鞋','白色','阿依莲','200',2),('迷你照相机','红色','尼康','500',3),('硬盘','黑色','希捷','600',3),('显卡','黑色','技嘉','800',3) + + select*from GoodsInfo + select * from GoodsType +-- 3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsMoney 价格,GoodsName 商品名称,GoodsColor 商品颜色,max(GoodsMoney)价格 + from GoodsInfo group by GoodsName ,GoodsColor,GoodsMoney order by GoodsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 编号,max( GoodsMoney)最高价格,min( GoodsMoney)最低价格,avg( GoodsMoney)平均价格 from GoodsInfo +group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select*from GoodsInfo + select * from GoodsType +select * from GoodsInfo where GoodsColor like '红色%' and GoodsMoney>=300 and GoodsMoney<=600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select b.TypeName 商品的类别,round(avg(a.GoodsMoney),2)平均价格 from GoodsInfo a inner join GoodsType b on a.TypeID=b.TypeID +group by TypeName +--7、查询每种颜色的商品数 +select GoodsColor,count(GoodsColor)商品数 from GoodsInfo +group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +select * from GoodsType +alter table GoodsType add remark text +update GoodsType set remark='我是商品类型信息备注' + +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop FK__GoodsInfo__TypeI__1273C1CD +--10、删除所有商品类型记录 +delete GoodsType + \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bbc5bae51d3e578ee62b39d1bc45d1b7a8dcd1fc --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery2.sql" @@ -0,0 +1,69 @@ +use master +go +create database HOUSE_DB +on( + name='HOUSE_DB', + filename='D:\sql\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on( + name='HOUSE_DB_log', + filename='D:\sql\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name nvarchar(50) unique not null, +) + +create table HOUSE( + house_id int primary key identity(1,1) not null, + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null foreign key references HOUSE_TYPE +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +insert into HOUSE_TYPE +values('小户型'),('经济型'),('别墅') +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE +values ('学区房','666',1),('小区房','500',2),('海景房','888',3) + +--4、添加查询 +--查询所有房屋信息 +select house_name 房屋名称,b.type_id 类型, house_price 价格 from HOUSE a inner join HOUSE_TYPE b +on a.type_id=b.type_id group by house_price,house_name,b.type_id +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select HOUSE.house_name ,HOUSE_TYPE.type_name from HOUSE_TYPE inner join HOUSE +on HOUSE_TYPE.type_id=HOUSE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_price,house_name from HOUSE order by house_price desc +select house_price,house_name from HOUSE +where house_price in(select max(house_price) from HOUSE) +-- 查询每种房屋类型的房屋数量 +select b.type_name,a.type_id,count(house_name)房屋数量 from HOUSE a +inner join HOUSE_TYPE b on a.type_id=b.type_id +group by a.type_id,house_name,b.type_name + +-- 修改房屋类型表的房屋名称字段,把数据类型改成nvarchar(30) +alter table HOUSE_TYPE drop UQ__HOUSE_TY__543C4FD94714335E +alter table HOUSE_TYPE alter column type_name nvarchar(30) +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table house add dizhi nvarchar(50) +alter table house drop DF__HOUSE__house_pri__276EDEB3 +alter table house add CONSTRAINT DF default(100) for house_price +--查询出名字最长的房屋信息。 +select top 1 house_id,house_name,house_price,type_id,dizhi, len(house_name)名字长度 from HOUSE +group by house_id ,house_name,house_price,type_id,dizhi order by house_name desc diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..956c6b1c1028db2d7546da96db82b00beca7c741 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery3.sql" @@ -0,0 +1,75 @@ +use master +go +create database StarManagerDB +on( + name='StarManagerDB', + filename='D:\sql\StarManagerDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on( + name='StarManagerDB_log', + filename='D:\sql\StarManagerDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use StarManagerDB +go +create table StarType( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) DEFAULT('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) + +) +INSERT INTO StarType +values('体育明星'),('IT明星'),('相声演员') + +insert into StarInfo +values('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2), +('马克斯',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) +select * from StarInfo + select * from StarType +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄, +--显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 类型编号,count(S_T_NO)明星人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarType + inner join StarInfo on StarType.T_NO=StarInfo.S_T_NO where T_NAME='体育明星' + order by S_AGE desc +--6、请统计每种明星类型的人数 +select b.T_NAME,S_T_NO,count(S_T_NO)人数 from StarInfo a +inner join StarType b on a.S_T_NO=b.T_NO +group by S_T_NO,b.T_NAME + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add nianshouru money +alter table StarInfo add constraint de default(10000000) for nianshouru +--8、查询每个国家的明星人数。 +select S_NATIVE,COUNT(S_NATIVE)人数 from StarInfo GROUP BY S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 + select * from StarInfo order by S_NATIVE,s_age desc + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select a.*,b.T_NAME from StarInfo a +inner join StarType b on a.s_t_no=b.t_no +where a.s_name like '%西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add time datetime +update StarInfo set time= getdate() + +alter table StarType add time datetime +update StarType set time= getdate() \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..42e706e159834088a9e0de43bf735a023c738e77 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" @@ -0,0 +1,77 @@ +use master +go +create database market +on( + name='market', + filename='D:\cs\market.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on( + name='market_log', + filename='D:\cs\market_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use market +go +create table GoodsType( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) +create table GoodsInfo( + GoodsID INT primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) +insert into GoodsType (TypeName) +values +('服装内衣'), + ('鞋包配饰'), +( '手机数码') +select * from GoodsType + + insert into GoodsInfo + values ('提花小西装','红色','菲曼琪','300',1), + ('百搭短裤','绿色','哥弟','100',1), + ('无心背心','白色','阿依莲','700',1), + ('低帮休闲鞋','红色','菲曼琪','900',2), + ('中跟单鞋','绿色','哥弟','400',2), + ('平底鞋','白色','阿依莲','200',2), + ('迷你照相机','红色','尼康','500',3), + ('硬盘','黑色','希捷','600',3), + ('显卡','黑色','技嘉','800',3) + + select*from GoodsInfo + select * from GoodsType +-- 3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsMoney 价格,GoodsName 商品名称,GoodsColor 商品颜色,max(GoodsMoney)价格 + from GoodsInfo group by GoodsName ,GoodsColor,GoodsMoney order by GoodsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 编号,max( GoodsMoney)最高价格,min( GoodsMoney)最低价格,avg( GoodsMoney)平均价格 from GoodsInfo +group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo where GoodsColor like '红色%' and GoodsMoney>=300 and GoodsMoney<=600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select b.TypeName 商品的类别,round(avg(a.GoodsMoney),2)平均价格 from GoodsInfo a inner join GoodsType b on a.TypeID=b.TypeID +group by TypeName +--7、查询每种颜色的商品数 +select GoodsColor,count(GoodsColor)商品数 from GoodsInfo +group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add remark text +update GoodsType set remark='我是商品类型信息备注' + +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop FK__GoodsInfo__TypeI__1273C1CD +--10、删除所有商品类型记录 +delete GoodsType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6a6f04a9875c0a05da926f2127eb23a08cc011ab --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery2.sql" @@ -0,0 +1,55 @@ +create database HOUSE +on +( + name='HOUSE', + filename='D:\fz\HOUSE.mdf', + size=10mb, + filegrowth=2mb + +) +log on +( + name='HOUSE_log', + filename='D:\fz\HOUSE_log.ldf', + size=10mb, + filegrowth=2mb + + +) +use HOUSE +go +create table HOUSE_TYPE +( + typeid int not null primary key identity(1,1) , + typename varchar(50) not null unique , + +) +create table HOUSE +( + houseid int not null primary key identity , + housename varchar(50) not null, + houseprice float not null default('0'), + typeid int not null foreign key(typeid)references HOUSE_TYPE(typeid) + +) +insert into HOUSE_TYPE(typename)values('小型户'),('经济型'),('别墅') +select * from HOUSE_TYPE +insert into HOUSE(housename,houseprice,typeid)values('碧桂园',6000,1), +('好利园',5000,1), +('家园',4000,2), +('华人园',3000,3), +('老家园',2000,3) +select * from HOUSE +--查询所有房屋信息 +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE + +--查询出房屋的名称和租金,并且按照租金降序排序 +select housename,houseprice from HOUSE + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select housename,typename from HOUSE inner join HOUSE_TYPE on HOUSE.typeid=HOUSE_TYPE.typeid + + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 max(houseprice) 房租,housename from HOUSE group by housename,houseprice order by houseprice desc diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b6b37f06023fef14a9d093c8b4e03eb59073562b --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery3.sql" @@ -0,0 +1,51 @@ +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\mx\StarManagerDB.mdf' + +) +log on +( + name='StarManagerDB_log', + filename='D:\mx\StarManagerDB_log.ldf' + + +) +use StarManagerDB +go +create table StarType +( + t_no int not null primary key identity(1,1), + t_name nvarchar(20) + +) +create table StarInfo + ( + s_no int not null primary key identity(1,1), + s_name nvarchar(20) not null , + s_age int not null, + s_hobby nvarchar(20), + s_native nvarchar(20) default('中国大陆'), + t_no int foreign key(t_no)references StarType(t_no) + + ) + insert into StarType(t_name)values('体育明星'),('IT明星'),('相声明星') + select * from StarType + insert into StarInfo(s_name,s_age,s_hobby,s_native,t_no)values + ('梅西',30,'射门','阿根廷',1), + ('科比',35,'过人','美国',1), + ('菜景现',40,'敲代码','中国',2), + ('马斯克',36,'造火箭','外星人',2), + ('郭德纲',30,'相声','中国',3), + ('黄铮',41,'拼多多','中国',2) + +select * from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 s_name 姓名,s_hobby 特技,s_native 籍贯 from StarInfo order by s_age desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select t_no 明星类型编号, count(*)人数, avg(s_age)平均年龄 from StarInfo group by t_no having count(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + select top 1 s_name 姓名,s_hobby 特技,s_native 籍贯 from StarInfo where t_no='1'order by s_age desc diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\344\275\234\344\270\232\344\270\211.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\344\275\234\344\270\232\344\270\211.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2a59d9d12f1b0c95a84f7bb31ec49c101d6ae120 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\344\275\234\344\270\232\344\270\211.sql" @@ -0,0 +1,94 @@ +use master +go +create database StarManagerDB +on +( +name='StarManagerDB', +filename='C:\test\StarManagerDB.mdf', +size=5MB, +maxsize=50MB, +filegrowth=1MB +) +log on +( +name='StarManagerDB_log', +filename='C:\test\StarManagerDB_log.ldf', +size=5MB, +maxsize=50MB, +filegrowth=1MB +) +go +use StarManagerDB +go +create table StarType +( +T_NO int primary key identity(1,1) not null, +T_NAME nvarchar(20) +) +insert into StarType (T_NAME) values ('体育明星') +insert into StarType (T_NAME) values ('IT明星') +insert into StarType (T_NAME) values ('相声演员') + +create table StarInfo +( +S_NO int primary key identity(1,1) not null, +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NATIVE nvarchar(20) default('中国大陆'), +S_T_NO int references StarType(T_NO) +) + + + +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('梅西','30','射门','阿根廷',1) +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('科比','35','过人','美国',1) +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('蔡景现','40','敲代码','中国',2) +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('马斯克','36','造火箭','外星人',2) +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('郭德纲','50','相声','中国',3) +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values ('黄铮','41','拼多多','中国',2) + + + +go +use StarManagerDB +go + + +select * from StarType +select * from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + select top 3 S_AGE as 年龄 ,S_NAME as 名字,S_HOBBY as 特技,S_NATIVE as 籍贯 from StarInfo order by S_AGE desc +--4、按明星类型编号 分类查询 明星人数 ,明星平均年龄 ,显示明星人数大于2的分组信息,要求使用别名显示列名。 + select S_T_NO, count(S_T_NO) as 明星人数,avg(S_AGE)as平均年龄 from StarInfo + group by S_T_NO + having count(S_T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + select top 1 S_AGE as 年龄,S_NAME as 名字,S_HOBBY as 特技,S_NATIVE as 籍贯 from StarType + inner join StarInfo on StarInfo.S_T_NO=StarType.T_NO + where T_NO ='1' + group by S_AGE ,S_HOBBY,S_NATIVE,S_NAME + order by S_AGE desc +-- 6、请统计每种明星类型的人数 + select count(StarType.T_NAME) as 明星类型 ,StarType.T_NAME from StarType + inner join StarInfo on StarInfo.S_T_NO=StarType.T_NO + group by StarType.T_NAME,StarType.T_NAME +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) + alter table StarInfo add annual nvarchar(4000) + alter table StarInfo add constraint DF_StarInfo_annual default (10000000) for annual +--8、查询每个国家的明星人数。 + select count(S_NATIVE) as 国家明星人数 ,S_NATIVE from StarInfo + group by S_NATIVE +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 + select StarInfo.S_NAME,StarInfo.S_AGE,StarInfo.S_HOBBY,StarInfo.S_NATIVE,StarInfo.S_NO ,StarType.T_NAME from StarInfo + inner join StarType on StarType.T_NO=StarInfo.S_T_NO + group by StarInfo.S_NAME,StarInfo.S_AGE,StarInfo.S_HOBBY,StarInfo.S_NATIVE,StarInfo.S_NO ,StarType.T_NAME + order by S_NATIVE,S_AGE desc +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 + select StarInfo.*,StarType.T_NAME 类型 from StarInfo + inner join StarType on StarInfo.S_T_NO=StarType.T_NO + where S_NAME like '_西' +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 + alter table StarType add s_time datetime default(getdate()) not null + alter table StarInfo add s_time datetime default(getdate()) not null diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\344\275\234\344\270\232\344\272\214.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\344\275\234\344\270\232\344\272\214.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e8dc0d81f1ec351fa3a7f2e602281b33fe48eb97 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\344\275\234\344\270\232\344\272\214.sql" @@ -0,0 +1,78 @@ +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='C:\test\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=1 + ) + log on + ( + name='HOUSE_DB_log', + filename='C:\test\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1 + ) + + go + use HOUSE_DB + go + + create table HOUSE_TYPE + ( + type_id int primary key identity (1,1) not null, + type_name varchar(50) unique not null, + ) + + insert into HOUSE_TYPE(type_name) values ('小户型'),('经济型'),('别墅') + + select * from HOUSE_TYPE + + create table HOUSE + ( + house_id int primary key identity (1,1), + house_name varchar(50) not null, + house_price float(2) default ('0') not null, + type_id int references HOUSE_TYPE(type_id) + ) + + insert into HOUSE(house_name,house_price,type_id) values ('水林天禧','1234','1'),('龙门御天下','7894','2'),('蓝天花园','9674','3') + + select *from HOUSE + +-- 4、添加查询 +--查询所有房屋信息 + select * from HOUSE_TYPE + select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 + select * from HOUSE_TYPE + inner join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id + where type_name like '__型' +--查询出房屋的名称和租金,并且按照租金降序排序 + select house_name as 房屋名称,house_price as 租金 from HOUSE + group by house_name,house_price order by house_price desc +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + select HOUSE_TYPE.type_name,HOUSE.house_name as 房屋名称 from HOUSE_TYPE + inner join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + select top 1 house_price as 租金 , HOUSE.house_name as 房屋名称 from HOUSE order by house_price desc +-- 查询每种房屋类型的房屋数量 + select count(HOUSE_TYPE.type_id) as 房屋数量 from HOUSE_TYPE + inner join HOUSE on HOUSE.type_id=HOUSE_TYPE.type_id + group by HOUSE_TYPE.type_id +-- 修改房屋类型表的房屋名称字段,把数据类型改成nvarchar(30) + alter table HOUSE drop column house_name + alter table HOUSE add house_name nvarchar(30) +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 + alter table HOUSE add address ntext +-- 把房屋价格的默认值0改为100 + alter table HOUSE drop constraint DF_house_price + alter table HOUSE add constraint DF_house_price default(100) for house_price +--查询出名字最长的房屋信息。 + select top 1 len(house_name),house_name,house_price from HOUSE order by len(house_name) desc + diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\345\244\215\344\271\240\351\242\2301.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\345\244\215\344\271\240\351\242\2301.sql" new file mode 100644 index 0000000000000000000000000000000000000000..aecdcd61778d7f70dbe9f350d27e524be574aa33 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\345\244\215\344\271\240\351\242\2301.sql" @@ -0,0 +1,87 @@ +use master +go + +create database GoodsDB +on +( + name='GoodsDB', + filename='C:\test\GoodsDB.mdf', + size=20, + maxsize=200, + filegrowth=10% + ) + log on +( + name='GoodsDB_log', + filename='C:\test\GoodsDB_log.ldf', + size=20, + maxsize=200, + filegrowth=10% + ) + + go + use GoodsDB + go + +create table GoodsType + +( + TypeID int primary key identity (1,1) not null, + TypeName nvarchar (20) not null, + ) + + insert into GoodsType(TypeName) values ('服装内衣'),('鞋包配饰'),('手机数码') + + select * from GoodsType + + create table GoodsInfo +( + GoodsID int primary key identity (1,1), + GoodsName nvarchar (20) not null, + GoodsColor nvarchar (20) not null, + GoodsBrand nvarchar (20) not null, + GoodsMoney money not null, + TyoeID int references GoodsType(TypeID) + ) + + insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TyoeID) + values ('提花小西装','红色','菲曼琪','300','1') , + ('百搭短裤','绿色','哥弟','300','1'), + ('无袖背心','白色','阿依莲','700','1'), + ('低帮休闲鞋','红色','菲曼琪','900','2'), + ('中跟单鞋','绿色','哥弟','400','2'), + ('平底','白色','阿依莲','700','1'), + ('迷你照相机','红色','尼康','500','3') + + select * from GoodsInfo + select * from GoodsType + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + + select top 1 GoodsName as 商品名称,GoodsColor as 商品颜色,GoodsMoney as 商品价格 from GoodsInfo + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + + select GoodsName as 商品名称 ,max(GoodsMoney) as 最高价格,min(GoodsMoney)as 最低价格,avg(GoodsMoney)as 平均价格 from GoodsInfo + group by GoodsMoney,GoodsName + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + select GoodsName as 商品名称,GoodsColor as 商品颜色,GoodsMoney as 商品价格 from GoodsInfo + where GoodsColor='红色' and GoodsMoney >=300 and GoodsMoney <=600 + + select GoodsName as 商品名称,GoodsColor as 商品颜色,GoodsMoney as 商品价格 from GoodsInfo + where GoodsColor='红色' and GoodsMoney between 300 and 600 + +--6、查询 每类商品 的 平均价格,要显示商品的 类别名称 和平均价格。 + select GoodsType.TypeName,GoodsInfo.GoodsName,avg(GoodsInfo.GoodsMoney) from GoodsInfo + inner join GoodsType on GoodsType.TypeID=GoodsInfo.TyoeID + group by GoodsInfo.GoodsMoney,GoodsType.TypeName,GoodsInfo.GoodsName,GoodsInfo.GoodsMoney +--7、查询 每种颜色 的商品数 + select count(GoodsColor) as 商品数,GoodsColor from GoodsInfo + group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” + alter table GoodsType add tip nvarchar(50) default('我是商品类型信息备注') not null +--9、在商品信息表中删除商品类型的外键约束 + alter table GoodsInfo drop constraint FK_TypeID +--10、删除所有商品类型记录 + alter table GoodsType drop column TypeName \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/.keep" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6ad0815817a0da6d6b317eecc7714129d10f8498 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery1.sql" @@ -0,0 +1,73 @@ +use master +go +create database HOUSE_DB2 + +go + +use HOUSE_DB2 +go +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique(type_name) not null +) +go + + +use HOUSE_DB2 +go +create table HOUSE +( + house_id int primary key identity(1,1) not null, + house_name varchar(50) not null, + house_price float default(0) not null, + type_id int constraint FK_HOUSE_type_id references HOUSE_TYPE(type_id) +) +go + + +insert into HOUSE_TYPE values('小户型'),('经济型'),('别墅') + +insert into HOUSE values('---','500','1'),('***','800','2'),('青青草原','1000','3') + +select * from HOUSE_TYPE +select * from HOUSE + + +--添加查询 +--查询所有房屋信息 +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%型' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋名称,house_price 租金 from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE +inner join HOUSE_TYPE on HOUSE.type_id = HOUSE_TYPE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select house_name 房屋名称,house_price 租金 from HOUSE where house_price in +(select top 1 house_price from HOUSE order by house_price desc) + +select top 1 house_price from HOUSE order by house_price desc + +--查询每种房屋类型的房屋数量 +select type_id 类型,count(type_id)数量 from HOUSE group by type_id + +--修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) +select * from HOUSE +alter table HOUSE alter column house_name nvarchar(30) + +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +select * from HOUSE +alter table HOUSE add address text + +--把房屋价格的默认值0改为100 +alter table HOUSE drop DF_HOUSE_house_price +alter table HOUSE add constraint DF default(100) for house_price + +--查询出名字最长的房屋信息. +select * from HOUSE where len(house_name) = (select max(len(house_name)) from HOUSE) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..66a36851c8f9ef10c535e9113e1a7ae8d8a901e8 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery2.sql" @@ -0,0 +1,76 @@ +use master +go +create database StarManagerDB + +go + +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20) +) +go + +use StarManagerDB +go +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int constraint FK_StarInfo_S_NATIVE references StarType(T_NO) +) +go + +insert into StarType values('体育明星'),('IT明星'),('相声演员') + +insert into StarInfo values('梅西','30','射门','阿根廷','1'),('科比','35','过人','美国','1'), +('蔡景现','40','敲代码','中国','2'),('马斯克','36','造火箭','外星人','2'), +('郭德纲','50','相声','中国','3'),('黄铮','41','拼多多','中国','2') + + +select * from StarInfo +select * from StarType + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_AGE from StarInfo order by S_AGE desc +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo where S_AGE in (select top 3 S_AGE from StarInfo order by S_AGE desc) + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 编号,count(*) 人数,avg(S_AGE)平均年龄 from StarInfo group by S_T_NO having count(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select T_NAME 类型,S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo +inner join StarType on StarInfo.S_T_NO = StarType.T_NO +where S_AGE in (select top 1 S_AGE from StarInfo inner join StarType on StarInfo.S_T_NO = StarType.T_NO where T_NAME='体育明星' order by S_AGE desc) + +--6、请统计每种明星类型的人数 +select T_NAME,count(S_T_No)人数 from StarType A inner join StarInfo B +on A.T_NO = B.S_T_NO group by T_NAME + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +alter table StarInfo add income int +alter table StarInfo add constraint DF_StarInfo_income default(10000000) for income + +--8、查询每个国家的明星人数。 +select S_NATIVE 国家,count(S_NATIVE)人数 from StarInfo group by S_NATIVE + +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select distinct A.*,B.T_NAME from StarInfo A inner join StarType B +on A.S_T_NO = B.T_NO order by S_NATIVE,S_AGE desc + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select A.*,T_NAME from StarInfo A inner join StarType B +on A.S_T_NO = B.T_NO where S_NAME like '_西' + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add CreateTime datetime +alter table StarInfo add constraint DK_StarInfo_CreateTime default(getdate()) for CreateTime + +alter table StarType add CreTime datetime +alter table StarType add constraint DK_StarType_CreTime default(getdate()) for CreTime + diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery\345\225\206\345\223\201.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery\345\225\206\345\223\201.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d7de4f0dfa704f0dbb35630f48346b14aad52bb9 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery\345\225\206\345\223\201.sql" @@ -0,0 +1,85 @@ + +use master +go + +create database GoodsDBs +on +( + name='GoodsDBs', + filename='E:\数据库文件\数据库跟目录文件\4.08复习\GoodsDBs.mdf', + size=5, + maxsize=50, + filegrowth=10% +) + +log on +( + name='GoodsDBs_log', + filename='E:\数据库文件\数据库跟目录文件\4.08复习\GoodsDBs_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +go + +use GoodsDBs + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null + +) +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int foreign key references GoodsType(TypeID) +) + +--使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 +insert into GoodsType(TypeName)values('服饰内衣'),('鞋包配饰'),('手机数码') + + +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID)values('提花小西装 ','红色 ','菲曼琪 ',300 ,1) +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID)values('百搭短裤 ','绿色 ','哥弟 ',100 ,1) +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID)values('无袖背心 ','白色 ','阿依莲 ',700 ,1) +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID)values('低帮休闲鞋 ','红色 ','菲曼琪 ',900 ,2) +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID)values('中跟单鞋 ','绿色 ','哥弟 ',400 ,2) +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID)values('平底鞋 ','白色 ','阿依莲 ',200 ,2) +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID)values('迷你照相机 ','红色 ','尼康 ',500 ,3) +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID)values('硬盘 ','黑色 ','希捷 ',600 ,3) +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID)values('显卡 ','黑色 ','技嘉 ',800 ,3) + +--drop table GoodsType +--drop table GoodsInfo + +select*from GoodsInfo +select*from GoodsType +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格, avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select*from GoodsInfo where GoodsColor='红色'and GoodsMoney>=300and GoodsMoney<=600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select TypeName,avg(GoodsMoney)平均价格 from GoodsType A inner join GoodsInfo B +on A.TypeID = B.TypeID group by TypeName +--7、查询每种颜色的商品数 +select GoodsColor ,count(GoodsColor)商品数 from GoodsInfo group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add 备注 nvarchar(10) not null default('我是商品类型信息备注') + +--9、在商品信息表中删除商品类型的外键约束 + + +--10、删除所有商品类型记录 +delete GoodsType diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ea9e301503c4cfac31dda629c49aaf0dc78acc1b --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" @@ -0,0 +1,72 @@ +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\HOUSE_DB.mdf', +size=5MB, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_log', +filename='D:\HOUSE_DB_log.ldf', +size=5MB, +maxsize=50mb, +filegrowth=1mb +) +use HOUSE_DB +go +create table HOUSE_TYPE +( +type_id int primary key identity not null, +type_name varchar(50) unique not null +) +create table HOUSE +( +house_id int primary key identity not null, +house_name varchar(50) not null, +house_price float default(0) not null, +type_id int foreign key references HOUSE_TYPE not null +) + +--添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 + insert into HOUSE_TYPE values + ('小户型'), + ('经济型'), + ('别墅') + select * from HOUSE_TYPE + insert into HOUSE values + ('天上人间',200,3), + ('地狱边境',300,2), + ('时间尽头',400,1) + select * from HOUSE + + + + +-- 添加查询 +--查询所有房屋信息 +select * from HOUSE +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where type_name like '%%型' +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name,house_price from HOUSE order by house_price DESC +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name,type_name from HOUSE +inner join HOUSE_TYPE on HOUSE.type_id=HOUSE_TYPE.type_id +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 (house_price),house_name from HOUSE order by house_price desc +--查询每种房屋类型的房屋数量 +select type_name 房屋类型,count(*) 数量 from HOUSE_TYPE group by type_name +--修改房屋表的房屋名称字段,把数据类型改成nvarchar(30) +alter table HOUSE alter column house_name nvarchar(30) +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add 地址 varchar(100) + +--把房屋价格的默认值0改为100 + +--查询出名字最长的房屋信息。 +select house_name,LEN(house_name)名字长度 from HOUSE WHERE LEN(house_name) =(select max(len(house_name)) from HOUSE) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..18b145b3b608243aaafacb873fff52df827a3b02 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery2.sql" @@ -0,0 +1,85 @@ +--1、创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 +create database GoodsDB +go +use GoodsDB +go +create table GoodsType +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null +) +create table GoodsInfo +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int foreign key references GoodsType(TypeID) +) + + +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 +insert into GoodsInfo values + ('提花小西装','红色','菲曼琪',300,1), + ('百搭短裤','绿色','哥弟',100,1), + ('无袖背心','白色','阿依莲',700,1), + ('低帮休闲鞋','红色','菲曼琪',900,2), + ('中跟单鞋','绿色','哥弟',400,2), + ('平底鞋','白色','阿依莲',200,2), + ('迷你照相机','红色','尼康',500,3), + ('硬盘','黑色','希捷',600,3), + ('显卡','黑色','技嘉',800,3) + select * from GoodsInfo + select * from GoodsType +--商品信息表(GoodsType) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsMoney,GoodsName,GoodsColor from GoodsInfo group by GoodsName,GoodsColor,GoodsMoney order by GoodsMoney DESC +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID,max(GoodsMoney)最高值,min(GoodsMoney)最低值,avg(GoodsMoney)平均值 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>300 and GoodsMoney<600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select TypeID,avg(goodsMoney) from GoodsInfo group by TypeID order by TypeID +--7、查询每种颜色的商品数 +select GoodsColor,count(GoodsColor)颜色数 from GoodsInfo group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table Goodstype add notes text +update GoodsType set notes = '我是商品类型信息备注' +--9、在商品信息表中删除商品类型的外键约束 +alter table GoodsInfo drop constraint FK__GoodsInfo__TypeI__1273C1CD +--10、删除所有商品类型记录 +truncate table GoodsType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c1ab38901a0be6bbeaabc036f7930392d6c0589a --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery3.sql" @@ -0,0 +1,59 @@ +create database StarManagerDB +use StarManagerDB +go +create table StarType +( +T_NO int primary key identity(1,1) not null , +T_NAME nvarchar(20) +) + +create table StarInfo +( +S_NO int primary key identity(1,1) not null, +S_NAME nvarchar(20) not null, +S_AGE int not null, +S_HOBBY nvarchar(20), +S_NARIVE nvarchar(20) default('中国大陆'), +S_T_NO int foreign key references StarType(T_NO) +) + +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') +select * from StarType + +insert into StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + +select * from StarInfo + +--查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_AGE 年龄, S_HOBBY 特技,S_NARIVE 籍贯 from StarInfo order by S_AGE DESC +--按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select COUNT(*)明星人数,AVG(S_AGE)平均年龄 from StarInfo +inner join StarType ON StarType.T_NO = StarInfo.S_T_NO group by T_NAME having count(*)>2 +--查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + +--请统计每种明星类型的人数 +select count(*),b.T_NAME from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO group by b.T_NAME +--给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +select * from StarInfo +alter table StarInfo add 年收入 int +alter table StarInfo add constraint dawda default(10000000) for 年收入 +--查询每个国家的明星人数。 + +--搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 + +--搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO WHERE S_NAME LIKE '_西' +--给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add 创建时间 datetime +alter table StarType add 创建时间 datetime +alter table StarType add constraint dadwadaw default(getdate()) for 创建时间 +alter table StarInfo add constraint dwada default(getdate()) for 创建时间 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQL1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQL1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7b8d23c3a3c2f8285a84d554895ff2c0a6522444 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQL1.sql" @@ -0,0 +1,98 @@ +--1、创建明星数据库(StarManagerDB) +use master +go + +create database StarManagerDB +go + +use StarManagerDB +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NatIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) + + + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 +insert into StarType values ('体育明星'),('IT明星'),('相声明星') + +insert into StarInfo (S_NAME,S_AGE,S_HOBBY,S_NatIVE,S_T_NO) +values ('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景观',40,'敲代码','中国',2), +('马斯克',36,'外星人','中国',2),('郭德纲',50,'相声','中国',3),('黄峥',41,'拼多多','中国',2) + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + + +select * from StarType +select * from StarInfo +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select TOP 3 S_AGE 年龄 ,S_NAME 姓名,S_HOBBY 特技, S_NatIVE 籍贯 FROM StarInfo order by S_AGE DESC + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +SELECT S_T_NO 类型编号 ,COUNT(*) 明星人数 ,AVG(S_AGE)明星平均年龄 FROM StarInfo GROUP BY S_T_NO HAVING COUNT(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +SELECT TOP 1 S_AGE 年龄 ,S_NAME 姓名, S_HOBBY 特技 ,S_NatIVE 籍贯 FROM StarInfo +inner join StarType on StarInfo.S_T_NO=StarType.T_NO +where T_NAME='体育明星' order by S_AGE DESC + +--6、请统计每种明星类型的人数 +select count(*),b.T_NAME +from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO +group by b.T_NAME + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) +select * from StarInfo +alter table StarInfo add 年收入 int +alter table StarInfo add constraint dawda default(10000000) for 年收入 + +--8、查询每个国家的明星人数。 +select S_HATIVE 人数,count(*) from StarInfo group by S_HATIVE + +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 +select * from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO order by S_HATIVE,S_AGE + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarInfo a inner join StarType b on a.S_T_NO=b.T_NO WHERE S_NAME LIKE '_西' + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 +alter table StarInfo add 创建时间 datetime +alter table StarType add 创建时间 datetime +alter table StarType add constraint dadwadaw default(getdate()) for 创建时间 +alter table StarInfo add constraint dwada default(getdate()) for 创建时间 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f75bf01a75d693a8a0b308c7488f59cc2f8de048 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" @@ -0,0 +1,89 @@ +use master +go + +--1、创建商品数据库(GoodsDB), +--然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表) +create database GoodsDB +go +use GoodsDB +go +--然后建立两张表,GoodsType(商品类型表)GoodsInfo(商品信息表) +create table GoodsType +--TypeID商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +( +TypeID int not null primary key identity(1,1), +--TypeName商品类型名称 nvarchar20 否 +TypeName nvarchar(20) not null +) + +--GoodsInfo(商品信息表) +create table GoodsInfo +( +--GoodsID商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +GoodsID int not null primary key identity(1,1), +--GoodsName商品名称 nvarchar20 否 +GoodsName nvarchar(20) not null, +--GoodsColor商品颜色 nvarchar20 否 +GoodsColor nvarchar(20) not null, +--GoodsBrand商品品牌 nvarchar20 +GoodsBrand nvarchar(20), +--GoodsMoney商品价格 money 否 +GoodsMoney money not null, +--TypeID商品类型编号 int 外键,参照GoodsType中的TypeID +TypeID int foreign key references GoodsType(TypeID) +) + +--使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +insert into GoodsType(TypeName) values +('服装内衣'), +('鞋包配饰'), +('手机数码') + +--商品信息表(GoodsInfo) +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) + +select * from GoodsType +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 +from GoodsInfo +order by GoodsMoney +desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 +from GoodsInfo +group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * +from GoodsInfo +where GoodsColor = '红色' and GoodsMoney >=300 and GoodsMoney<=600 +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 +select TypeName 类别,avg(GoodsMoney)平均价格 +from GoodsInfo GI +inner join GoodsType GT on GI.TypeID=GT.TypeID +group by GI.TypeID,TypeName +--7、查询每种颜色的商品数 +select GoodsColor 颜色,count(GoodsName)数量 from GoodsInfo +group by GoodsColor +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add Notes text +update GoodsType set Notes='我是商品类型信息备注' +--9、在商品信息表中删除商品类型的外键约束 +table GoodsInfo drop constraint FK__GoodsInfo__TypeI__1273C1CD +--10、删除所有商品类型记录 +truncate table GoodsType +--select * from GoodsType +--select * from GoodsInfo +--drop database GoodsDB \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8c2a4e9280a2b5d1343b3c94be92448179b54832 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery2.sql" @@ -0,0 +1,90 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +use master +go + +create database HOUSE_DB +on +( +name='HOUSE_DB', +filename='D:\HOUSE_DB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +( +name='HOUSE_DB_ldf', +filename='D:\HOUSE_DB_ldf.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) + +use HOUSE_DB + +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 +create table HOUSE_TYPE +( +type_id int not null primary key identity(1,1), +type_name varchar(50) not null unique +) + + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 +create table HOUSE +( +house_id int not null primary key identity(1,1), +house_name varchar(50) not null , +house_price float not null default(0), +tyoe_id int not null foreign key references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') + +insert into HOUSE(house_name,house_price,tyoe_id) values ('A',5000,1) , ('B',6000,2) ,('C',8000,3) + +SELECT * FROM HOUSE_TYPE + +--4、添加查询 +--查询所有房屋信息 + SELECT * FROM HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +SELECT * FROM HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 名称, house_price 租金 from HOUSE order by house_price DESC + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select house_name 房屋名称,type_name 房屋类型名称 from HOUSE_TYPE +inner join HOUSE on HOUSE_TYPE.type_id =HOUSE.tyoe_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select Top 1 house_price 最高的租金, house_name 房屋名称 from HOUSE order by house_price DESC + +--查询每种房屋类型的房屋数量 +select tyoe_id 房屋类型,count(house_name)房屋数量 from HOUSE group by tyoe_id + +--修改房屋表的房屋名称,把数据类型改成nvarchar(30) +alter table HOUSE alter column house_name nvarchar(30) + +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add address text +--把房屋价格的默认值0改为100 +--查询出名字最长的房屋信息。 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery2.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6c557bab77bd1801cef3a02b1468d18b4ba57987 --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery2.sql" @@ -0,0 +1,77 @@ +use master +go + +create database GoodsDB +go + +use GoodsDB +go + +drop table GoodsType +drop table GoodsInfo + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) +) + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodMonty money not null, + TypeID int references GoodsType(TypeID) +) + +select * from GoodsType + +insert into GoodsType +values ('服装内衣'), + ('鞋包配饰'), + ('手机数码') + +select * from GoodsInfo +insert into GoodsInfo +values('提花小西装','红色','菲曼琪','300',1), + ('百搭短裤','绿色','哥弟','100',1), + ('无袖背心','白色','阿依莲','700',1), + ('低帮休闲鞋','红色','菲曼琪','900',2), + ('中跟单鞋','绿色','哥弟','400',2), + ('平底鞋','白色','阿依莲','200',2), + ('迷你照相机','红色','尼康','500',3), + ('硬盘','黑色','希捷','600',3), + ('显卡','黑色','技嘉','800',2) + +--3、查询 价格 最贵 的 商品名称, 商品颜色 和 商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodMonty 商品价格 from GoodsInfo +order by GoodMonty desc + +--4、按商品类型编号 分组查询 商品最高价格 ,最低价格和 平均价格,要求使用别名显示列名 +select TypeID 商品类型编号,max(GoodMonty) 商品最高价格,min(GoodMonty) 商品最低价格,avg(GoodMonty) 平均价格 +from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodMonty>=300 and GoodMonty<=600 + +--6、查询每类商品的 平均价格,要显示 商品的类别名称 和 平均价格。] +select TypeName,avg(GoodMonty) from GoodsType inner join GoodsInfo on GoodsType.TypeID=GoodsInfo.TypeID +group by TypeName + +--7、查询每种颜色的商品数 +select GoodsColor 商品颜色,count(GoodsColor) 商品数 from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” +alter table GoodsType add Typetext text +select* from GoodsType + +update GoodsType set Typetext='我是商品类型信息备注' + +--9、在商品信息表中删除商品类型的外键约束 +select * from GoodsInfo +alter table GoodsInfo drop FK__GoodsInfo__TypeI__300424B4 + +--10、删除所有商品类型记录 +DELETE GoodsType \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery3.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..16f5fdc71a934615a0fa7ac7ed3d7ceebbc42fef --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery3.sql" @@ -0,0 +1,84 @@ +use master +go + +create database HOUSE_DB +on( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +log on( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +go + +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int not null primary key identity(1,1), + type_name varchar(50) not null unique +) + +create table HOUSE +( + house_id int not null primary key identity(1,1), + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null references HOUSE_TYPE(type_id) +) + +insert into HOUSE_TYPE +values ('小户型'), + ('经济型'), + ('别墅') + +drop table HOUSE +insert into HOUSE +values ('海边公寓','1000',2), + ('花园小房','500',1), + ('海边别墅','2000',3), + ('薰衣草公馆','1000',3) +select * from HOUSE + + + +--查询所有房屋信息 +--使用模糊查询包含”型“字的房屋类型信息 +select* from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select house_name 房屋名称,house_price 租金 FROM HOUSE order by house_price DESC + +--使用连接查询,查询信息,显示 房屋名称 和 房屋类型名称 +select house_name 房屋名称 , type_name 房屋类型名称 from HOUSE_TYPE inner join HOUSE +on HOUSE_TYPE.type_id=HOUSE.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 house_name 房屋名称,house_price 租金 FROM HOUSE order by house_price DESC + +-- 查询每种房屋类型的房屋数量 +select*from HOUSE + +select type_name 房屋名称 , count(house_name) 房屋数量 from HOUSE_TYPE +inner join HOUSE on HOUSE_TYPE.type_id=HOUSE.type_id group by type_name + +-- 修改房屋类型表的房屋名称字段,把数据类型改成nvarchar(30) +alter table HOUSE alter column house_name nvarchar(30) + +-- 给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 +alter table HOUSE add house_add nvarchar(200) + +-- 把房屋价格的默认值0改为100 + + +--查询出名字最长的房屋信息。 +select* from HOUSE +select top 1 house_id,house_name,house_price, type_id, len(house_name) 房屋名字长度 from HOUSE order by len(house_name) desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery4.sql" "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2df4432968ece7d3b5a173dc296b4fe432bb909a --- /dev/null +++ "b/\347\254\254\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery4.sql" @@ -0,0 +1,69 @@ +use master +go +create database StarManagerDB +go +use StarManagerDB +go + +create table StarType --明星类型表 +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) not null +) + +create table StarInfo --明星信息表 +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +insert into StarType +values('体育明星'), + ('IT明星'), + ('相声明星') +select* from StarType + + +insert into StarInfo +values('梅西',30,'射门','阿根廷',1), + ('科比',35,'过人','美国',1), + ('蔡景现',40,'敲代码','中国',2), + ('马斯克',36,'造火箭','外星人',3), + ('郭德纲',50,'相声','中国',3), + ('黄铮',41,'拼多多','中国',2) +select* from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo + +--4、按 明星类型编号 分类查询 明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select T_NO 明星类型编号,count(T_NO) 明星人数,avg(S_AGE) 明星平均年龄 from StarType A +inner join StarInfo B on A.T_NO=B.S_T_NO +group by T_NO having count(T_NO)>2 +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 T_NAME 明星类型 , S_NAME 姓名,S_AGE 年龄,S_HOBBY 特技,S_NATIVE 籍贯信息 from StarInfo +left join StarType on StarInfo.S_T_NO=StarType.T_NO where T_NAME='体育明星' +order by S_AGE desc +--6、请统计每种明星类型的人数 +select T_NO 明星类型编号,count(T_NO) 明星人数 from StarType A +inner join StarInfo B on A.T_NO=B.S_T_NO +group by T_NO + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) + + +--8、查询每个国家的明星人数。 +select S_NATIVE,count(S_NATIVE) from StarInfo group by S_NATIVE + +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 + + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 +select * from StarInfo inner join StarType on StarInfo.S_T_NO=StarType.T_NO +where S_NAME like '%西%' and len(S_NAME)=2 + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 \ No newline at end of file