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/\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\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\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\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\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\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/\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\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\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\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\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