diff --git "a/\346\270\251\345\271\277\347\224\237/SQLQuery1.sql" "b/\346\270\251\345\271\277\347\224\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4f614a8624f6e300d1ea2cc7218c4a5c822be9ef --- /dev/null +++ "b/\346\270\251\345\271\277\347\224\237/SQLQuery1.sql" @@ -0,0 +1,80 @@ +create database sss +on +( + name='sss', + FILENAME='D:\temp\sss.mdf', + SIZE=5, + MAXSIZE=50, + FILEGROWTH=10% +) +log on +( + NAME='sss_log', + FILENAME='D:\temp\sss_log.ldf', + SIZE=5, + MAXSIZE=50, + FILEGROWTH=10% + +) +go +use sss +go + +create table stuinfo +( + StuNo varchar(20), + stuName varchar(10) not null, + stuAge int not null, + stuAddress varchar(100), + stuSeat int primary key identity, + stuSex char(2) default(1) check(stuSex=0 or stuSex=1) +) +create table stuexam2 +( + examNo2 int primary key identity(1,1), + stuNo2 varchar(20), + writtenExam2 int, + labExam2 int +) + +insert into stuinfo(StuNo,stuName,stuAge,stuAddress,stuSex) +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 + +insert into stuexam2(stuNo2,writtenExam2,labExam2) +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 stuinfo +select * from stuexam2 + +select * from stuinfo SI inner join stuexam2 SE on SI.StuNo=SE.stuNo2 + + + +--1.查询学生的姓名,年龄,笔试成绩和机试成绩 +select StuNO,stuname,writtenExam2,labExam2 from stuinfo inner join stuexam2 +on stuinfo.StuNo=stuexam2.stuNo2 +--2.查询笔试和机试成绩都在60分以上的学生的学号,姓名,笔试成绩和机试成绩 +select StuNO,stuname,writtenExam2,labExam2 from stuinfo inner join stuexam2 +on stuinfo.StuNo=stuexam2.stuNo2 where writtenExam2>60 and labExam2>60 +--3.查询所有学生的学号,姓名,笔试成绩,机试成绩,没有参加考试的学生的成绩以NULL值填充 +---select StuNO,stuname,writtenExam2,labExam2 from stuinfo inner join stuexam2 +on stuinfo.StuNo=stuexam2.stuNo2 +--4.查询年龄在20以上(包括20)的学生的姓名,年龄,笔试成绩和机试成绩,并按笔试成绩降序排列 +select stuname,stuAge,writtenExam2,labExam2 from stuinfo inner join stuexam2 +on stuinfo.StuNo=stuexam2.stuNo2 where stuAge>=20 order by writtenExam2 desc +--5.查询男女生的机试平均分 +--select xb 性别,SUM(yw) 语文总分,SUM(sx) 数学总分,SUM(yy) 英语总分,AVG(yw) 语文平均分,AVG(sx) 数学平均分,AVG(yy) 英语平均分 from xs left join cj on xs.xm=cj.xm group by xb +select stusex 性别,avg(labExam2) from stuinfo inner join stuexam2 on stuinfo.StuNo=stuexam2.stuNo2 group by stuSex +--6.查询男女生的笔试总分 +select stusex 性别,sum(writtenExam2) from stuinfo inner join stuexam2 on stuinfo.StuNo=stuexam2.stuNo2 group by stuSex \ No newline at end of file diff --git "a/\346\270\251\345\271\277\347\224\237/SQLQuery2.sql" "b/\346\270\251\345\271\277\347\224\237/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cb5a02a692abc930e252a10fc5f599edd5a70165 --- /dev/null +++ "b/\346\270\251\345\271\277\347\224\237/SQLQuery2.sql" @@ -0,0 +1,104 @@ +create database a +on +( + name='a', + filename='D:\temp\ATM.mdf', + size=5, + maxsize=10, + filegrowth=15% +) +log on +( + name='a_log', + filename='D:\temp\ATM.ldf', + size=5, + maxsize=10, + filegrowth=15% +) +go +use a +go + +create table orders +( +--订单编号(orderId 主键) 订购日期(orderDate) + orderId int primary key, + orderDate datetime default(getdate()) +) +--订购项目表(orderItem),列为: +--项目编号(ItemiD)订单编号(orderId)产品类别(itemType) +--产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) +create table orderItem +( +--订单编号(orderId 主键) 订购日期(orderDate) + ItemiD int identity(1,1), + orderId int, + itemType varchar(20), + itemName varchar(20), + theNumber int, + theMoney int +) + +select * from orders +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,'文具','笔',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 + +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 +select * from orderItem +--orderId,itemType,itemName,theNumber,theMoney + +--使用上次作业的订单数据库,完成下列题目: + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select I.orderId 订单编号,orderDate 订单日期,itemType 产品的类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价 +from orders I inner join orderItem D on I.orderId=D.orderId +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select I.orderId 订单编号,orderDate 订单日期,itemType 产品的类别,itemName 产品名称 +from orders I inner join orderItem D on I.orderId=D.orderId where theNumber>50 +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select I.orderId 订单编号,orderDate 订单日期,itemType 产品的类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 +from orders I inner join orderItem D on I.orderId=D.orderId +------------------------------------------------------------where uPoint = (select MAX(uPoint) from bbsUsers +select orderId,sum(theMoney)from orderItem group by orderId +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select I.orderId 订单编号,orderDate 订单日期,itemType 产品的类别,itemName 产品名称,theNumber 订购数量,theMoney 订购单价,theNumber*theMoney 订购总价 +from orders I inner join orderItem D on I.orderId=D.orderId where theMoney>=5 and theNumber>=50 + +select orderId,sum(theMoney) from orderItem where theMoney >5 group by orderId having(sum(theMoney)>50) +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select * from orderItem +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 * from orderItem +select orderId 编号,itemType 产品类别,count(*),sum(theNumber) from orderItem group by orderId,itemType order by orderId,itemType desc \ No newline at end of file diff --git "a/\346\270\251\345\271\277\347\224\237/SQLQuery3.sql" "b/\346\270\251\345\271\277\347\224\237/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..aef22e89d8317e2eebb0abdb03c2cdaf9bacace7 --- /dev/null +++ "b/\346\270\251\345\271\277\347\224\237/SQLQuery3.sql" @@ -0,0 +1,122 @@ +create database bbs +on +( + NAME='bbs', + FILENAME='D:\temp\bbss.mdf', + SIZE=5, + MAXSIZE=50, + FILEGROWTH=10% +) +log on +( + NAME='TestDB_log', + FILENAME='D:\temp\bbss_log.ldf', + SIZE=5, + MAXSIZE=50, + FILEGROWTH=10% + +) +go +use bbs +go + +create table bbsUsers +( + UIDa 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<=60), + uPoint int not null check(uPoint>=0) +) +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int foreign key references bbsUSers(UIDa), + tSID int , + tTitle varchar(100) not null, + tMsg varchar(100) not null, + tTime datetime, + tCount int, +) +create table bbsReply +( + rID int primary key identity(1,1), + rUID int foreign key references bbsUsers(UIDa), + rTID int foreign key references bbsTopic(tID), + rMsg varchar(100) not null, + rTime datetime, + +) +create table bbsSection +( + SIDaa int primary key identity(1,1), + sName varchar(10) not null, + sUid int +) + +select *from bbsReply + +--alter table bbsSection add SIDaa int primary key identity(1,1) +alter table bbsSection add constraint FK_UIDa foreign key(SUid) references bbsUsers(UIDa) + +alter table bbsTopic add constraint FK_SIDa foreign key(tSID) references bbsSection(SIDaa) + +select * from bbsUsers +insert into bbsUsers(uName,uSex,uAge,uPoint) +select '小雨点','女',20,0 union +select '逍遥','男',18,4 union +select '七年级生','男',19,2 + +select uName,uPoint from bbsUsers + +select * from bbsSection +insert into bbsSection(sName,sUid) +select '技术交流',3 union +select '读书世界',1 union +select '生活百科',3 union +select '八卦区',1 + +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +select 2,2,'范跑跑','谁是范跑跑','2008-7-8',1 union +select 1,2,'.NET ','与JAVA的区别是什么呀?','2008-9-1',2 union +select 3,2,'今年夏天最流行什么','有谁知道今年夏天最流行','2008-9-10',0 + +select *from bbsTopic +insert into bbsReply(rUID,rTID,rMsg,rTime) +select 3,1,'与JAVA的区别是程序语言','2008-9-10' union +select 1,2,'有好有坏,不要站在道德的制高点哔赖','2008-9-10' union +select 2,3,'当然是短袖呀','2008-9-10' + +--select *from bbsReply + +--delete from bbsUsers where uName='逍遥' + +--alter table bbsSection drop constraint FK_UIDa + +--alter table bbsTopic drop constraint FK__bbsTopic__tUID__15502E78 +--alter table bbsReply drop constraint FK__bbsReply__rUID__182C9B23 + +--update bbsUsers set uPoin t=10 where UIDa=3 + +--select *from bbsUsers +--delete from bbsSection where sName='生活百科' +--select *from bbsSection + + +select *from bbsUsers +select *from bbsTopic +select *from bbsReply + +select *from bbsSection +--在论坛数据库中完成以下题目 +--1.查询出每个版块的版主编号,版主姓名和版块名称 +select UIDa 编号,uName 姓名,sName 板块名称 from bbsUsers U inner join bbsSection T on U.UIDa=T.SIDaa +--2.查询出主贴的发帖时间在2008-9-15以后的主贴的发帖人编号,发帖人姓名,帖子的标题,帖子的内容和发帖时间 +select tUID 编号,tSID 发帖人姓名,tTitle 帖子的标题,tMsg 帖子的内容,tTime 发帖时间 from bbsTopic where tTime>'2008-9-15' +--3.查询出年龄在20以下的版主的编号,版主的名称和版块的名称 +select UIDa 编号,uName 姓名,sName 板块名称 from bbsUsers U inner join bbsSection T on U.UIDa=T.SIDaa where uAge<20 +*--4.查询出回复数量最多的主贴的发帖人编号,发帖人姓名,主贴标题,主贴内容和回复数量 +select UIDa 编号,uName 名称,tTitle 标题,tmsg 内容,tCount 回复次数 from bbsTopic T inner join bbsUsers U on T.tUID=U.UIDa where tCount>1 +--5.在主贴表中查询每个版块中每个用户的发帖总数 +select tSID 板块编号,tUID 用户名,count(*) 总数 from bbsTopic group by tSID,tUID +