diff --git a/SQLQuery1.sql b/SQLQuery1.sql new file mode 100644 index 0000000000000000000000000000000000000000..b049466eacbf74062c37e062e0825c924e99ed9f --- /dev/null +++ b/SQLQuery1.sql @@ -0,0 +1,186 @@ +use master + +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 varchar(50) 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 into bbsUsers(useID,uName,uSex,uAge,uPoint) values(1,'小雨点','女',20,0) +insert into bbsUsers(useID,uName,uSex,uAge,uPoint) values(2,'逍遥','男',18,4) +insert into bbsUsers(useID,uName,uSex,uAge,uPoint) values(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个板块 + --名称 版主名 + --技术交流 小雨点 + --读书世界 七年级生 + --生活百科 小雨点 + --八卦区 七年级生 +select * from bbsSection + +insert bbsSection values(1,'技术交流',1) +insert bbsSection values(2,'读书世界',2) +insert bbsSection values(3,'生活百科',3) +insert bbsSection values(4,'八卦区',3) +--向主贴和回帖表中添加几条记录 + --主贴: + --发帖人 板块名 帖子标题 帖子内容 发帖时间 回复数量 + --逍遥 八卦区 范跑跑 谁是范跑跑 2008-7-8 1 + --七年级生 技术交流 .NET 与JAVA的区别是什么呀? 2008-9-1 2 + --小雨点 生活百科 今年夏天最流行什么 有谁知道今年夏天最流行什么呀? 2008-9-10 0 +select * from bbsTopic + +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 + +--回帖: + --分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 +select * from bbsReply + +insert into bbsReply(rMsg,rTime) +select '我是范跑跑','2008-7-10' union +select '没区别','2008-9-2' union +select '最流行冰激凌','2008-9-10' + + +--1.在主贴表中统计每个版块的发帖总数 +select * from bbsTopic + +select (tTitle),sum(tCount)发帖总数 from bbsTopic group by tTitle having sum(tCount)>=0 + +--2.在回帖表中统计每个主贴的回帖总数量 +select * from bbsReply + +select (rID) from bbsReply group by rID + +--3.在主贴表中统计每个用户的发的主帖的总数 +select * from bbsTopic + +select (tTitle),sum(tCount)发帖总数 from bbsTopic group by tTitle having sum(tCount)>=0] + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select * from bbsUsers + +select uName 用户名,uSex 性别,uAge 年龄,max(uPoint) 积分 from bbsUsers group by uName,uPoint,uSex,uAge having max(uPoint)>2 + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) + + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers + +select useID,uName,uSex,uAge,uPoint from bbsUsers where uName='小__' or uName='__大' +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 + +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select * from bbsTopic + +select tUID,tCount from bbsTopic where tMsg='!' diff --git a/SQLQuery2.sql b/SQLQuery2.sql new file mode 100644 index 0000000000000000000000000000000000000000..44dbc46944cdbf1f3ad96f0001b2b866770e7da5 --- /dev/null +++ b/SQLQuery2.sql @@ -0,0 +1,100 @@ +use master + +go + +create database DD --建立数据库 +on primary +( +name='D:\home', +filename='D:\home.mdf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +log on +( +name='D:\home_log', +filename='D:\home_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 sum(theNumber)数量总和 from orderltem + +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 + +select (orderId)订单编号,avg(theMoney)平均单价,count(theNumber)数量 from orderltem group by orderId having orderId<3 and avg(theMoney)<10 + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 + +select (orderId)订单编号,count(theNumber)数量总数,avg(theMoney)平均单价 from orderltem group by orderId having orderId>50 and avg(theMoney)<10 + +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 + +select (itemType)类别,count(*)订购次数 from orderltem group by itemType + +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 + +select (itemType)类别,sum(theNumber)订购总数量,avg(theMoney)平均单价 from orderltem group by itemType having sum(theNumber)>100 + + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: + +select (itemType)类别,count(itemType)订购次数,sum(theNumber)总数量,avg(theMoney)平均单价 from orderltem group by itemType + +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/.keep" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..14a2398702be85d9869b4cfb26d22a84d55d15a9 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,81 @@ +use master +go +create database bank +on +( + name=bank, + filename='D:\SQL\bank.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name=bank_log, + filename='D:\SQL\bank_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=15% +) +use bank +go +create table userInfo2 +( + customerID int primary key identity(1,1), + customerName nvarchar(15) not null, + PID varchar(18) not null unique check(len (PID)=15 or len (PID)=18), + telephone char(13) not null check (len (telephone)=13 and telephone like'____-________'), + address nvarchar(20), +) +go +create table cardInfo +( + cardID char(18) primary key check(cardID like '1010 3576 ____ ___'), + curType varchar(30) not null default ( 'RMB'), + savingType nvarchar(4) check(savingType='活期'or savingType='定活两便' or savingType='定期' ), + openDate datetime not null default(getdate()), + balance money not null check(balance>=1), + pass char(6) default (88888888) , + IsReportLoss char(2) check(IsReportLoss ='是'or IsReportLoss='否')default('否') , + customerID int references userInfo2(customerID), +) +create table transInfo +( + transId int primary key identity, + transDate datetime not null default(getdate()), + cardID char(18) not null check(cardID like '1010 3576 ____ ___') references cardInfo(cardID),--这里 + transType nvarchar(2) not null check(transType='存入'or transType='支出'), + transMoney money not null check(transMoney>0), + remark text, +) +select*from userInfo2 +insert into userInfo2(customerName,PID,telephone,address) values('孙悟空开户','123456789012345' ,'0716-78989783','北京海淀'), +('沙和尚开户','421345678912345678' ,'0478-44223333',''),('唐僧开户','321245678912345678' ,'0478-44443333','') +insert into cardInfo (cardID,savingType,balance,customerID)values('1010 3576 1234 567','活期',1000,1),('1010 3576 1212 117','定期',1,2) +,('1010 3576 1212 113','定期',1,3) +select *from cardInfo +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass='611234' where customerID=1 +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 +insert into transInfo(cardID,transType,transMoney) values('1010 3576 1234 567','支出',200) +update cardInfo set balance=balance-200 where customerID=1 + +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) +insert into transInfo(cardID,transType,transMoney) values('1010 3576 1212 117','存入',300) +update cardInfo set balance=balance+300 where customerID=2 +--4.唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” +update cardInfo set IsReportLoss='是'where customerID=3 +--5. 查询出最近10天开户的银行卡的信息 +select*from cardInfo where openDate>='2021-3-20'and openDate<=2021-3-30 +--6. 查询交易金额最大的银行卡信息,子查询实现 +select max (transMoney) from transInfo + +--7.再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) +-- 显示效果: +-- 总交易金额:1400.00 +-- 支取交易金额:200.00 +-- 存入交易金额:1200.00 +select sum (transMoney) from transInfo +select sum (transMoney) from transInfo where transType='支出' +select sum (transMoney) from transInfo where transType='存入' \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..02358068281c5657285cdb41856ab907875394c4 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" @@ -0,0 +1,154 @@ +--某银行拟开发一套ATM取款机系统,实现如下功能: +--1、开户(到银行填写开户申请单,卡号自动生成) +--2、取钱 +--3、存钱 +--4、查询余额 +--5、转账(如使用一卡通代缴手机话费、个人股票交易等) +use master +go +--第一阶段:建库、建表、建约束、添加数据 +--A. 现要求对“ATM柜员机系统”进行数据库的设计并实现,数据库保存在D:\bank目录下,文件增长率为15% 。 +create database ATM +on( + name = 'ATM_mdf', + filename='D:\bank\ATM_mdf.mdf', + size=5mb, + maxsize=1000mb, + filegrowth=15% +) +log on( + name = 'ATM_ldf', + filename='D:\bank\ATM_ldf.ldf', + size=5mb, + maxsize=1000mb, + filegrowth=15% +) +go + +use ATM +go + +--B. 根据下图创建表,约束种类参考下列表的说明 +--用户信息表:userInfo : +--字段名称 说 明 +--customerID 顾客编号 自动编号(标识列),从1开始,主键 +--customerName 开户名 必填 +--PID 身份证号 必填,只能是18位或15位,身份证号唯一约束 +--telephone 联系电话 必填,格式为xxxx-xxxxxxxx或手机号13位 +--address 居住地址 可选输入 +create table userInfo +( + customerID int identity(1,1) primary key , + customerName nvarchar(10) not null, + PID nvarchar(20) not null check(len(PID)=18 or len(PID)=15) unique, + telephone nvarchar(20) not null check(telephone like '____-________' or len(telephone)=13 ), + address text +) + +--银行卡信息表:cardInfo +--字段名称 说 明 +--cardID 卡号 必填,主健,银行的卡号规则和电话号码一样,一般前8位代表特殊含义,如某总行某支行等。 +--假定该行要求其营业厅的卡号格式为:1010 3576 xxxx xxx开始,每4位号码后有空格,卡号一般是随机产生。 +--curType 货币种类 必填,默认为RMB +--savingType 存款类型 活期/定活两便/定期 +--openDate 开户日期 必填,默认为系统当前日期 +--balance 余额 必填, +--pass 密码 必填,6位数字,开户时默认为6个“8” +--IsReportLoss 是否挂失 必填,是/否值,默认为”否” +--customerID 顾客编号 外键,必填,表示该卡对应的顾客编号,一位顾客允许办理多张卡号 + +create table cardInfo +( + cardID nvarchar(18) not null primary key check(substring(cardID,1,9)='1010 3576 'and len(cardID)=18), + curType nvarchar(10) default('RMB'), + savingType nvarchar(20) check(savingType='活期' or savingType='定活两便' or savingType='定期'), + openDate datetime not null default(getdate()), + balance int not null check(balance>=1), + pass int not null default(888888) check(len(pass)=6), + IsReportLoss nvarchar(2) not null default('否') check(IsReportLoss='是' or IsReportLoss='否'), + customerID int not null references userInfo(customerID) +) + +--交易信息表:transInfo +--字段名称 说 明 +--transId 交易编号 标识列、主键 +--transDate 交易日期 必填,默认为系统当前日期 +--cardID 卡号 必填,外健, +--transType 交易类型 必填,只能是存入/支取 +--transMoney 交易金额 必填,大于0 +--remark 备注 可选输入,其他说明 + +create table transInfo +( + transId int identity(1,1) primary key, + transDate datetime not null default(getdate()), + cardID nvarchar(18) not null references cardInfo(cardID), + transType nvarchar(4) not null check(transType='存入' or transType='支取'), + transMoney int not null check(transMoney>0), + remark text +) +--C. 根据下列条件插入和更新测试数据 +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +-- 开户金额:1000 活期 卡号:1010 3576 1234 567 +select *from userInfo + +insert into userInfo(customerName,PID,telephone,address) +values +('孙悟空',123456789012345,'0716-78989783','北京海淀'), +('沙和尚',421345678912345678,'0478-44223333',''), +('唐僧',321245678912345678,'0478-44443333','') + +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 117 + +--唐僧开户,身份证:321245678912345678,电话:0478-44443333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 113 +select * from cardInfo +insert into cardInfo(cardID,savingType,balance,customerID) +values('1010 3576 1234 567','活期',1000,11), +('1010 3576 1212 117','定期',1,12), +('1010 3576 1212 113','定期',1,13) + + +--第二阶段:增、删、改、查 + +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass=611234 where cardID='1010 3576 1234 567' + +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作, +--先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号, +--再根据银行卡号来插入交易记录和修改账上余额 +select * from transInfo +select customerID from userInfo where customerName='孙悟空' +select cardID from cardInfo where customerID=11 +select cardID from cardInfo where customerID=(select customerID from userInfo where customerName='孙悟空') + +insert into transInfo (cardID,transType,transMoney) values +('1010 3576 1234 567','支取',200) +update cardInfo set balance=balance-200 where cardID='1010 3576 1234 567' +select * from cardInfo + +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) +select customerID from userInfo where customerName='沙和尚' +select cardID from cardInfo where customerID=12 +insert into transInfo (cardID , transType,transMoney) values +('1010 3576 1212 117','存入',300) +update cardInfo set balance=balance+300 where cardID='1010 3576 1212 117' + +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” +update cardInfo set IsReportLoss='是' where customerID='13' + +--5. 查询出最近10天开户的银行卡的信息 +select * from cardInfo where openDate>=dateadd(day,-10,getdate()) + +--6. 查询交易金额最大的银行卡信息,子查询实现 +select * from cardInfo where cardID=(select cardID from transInfo where transMoney=(select max(transMoney)from transInfo )) + +------7. 再交易信息表中,将总的交易金额,支取的交易金额, +------存入的交易金额查询出来并输出显示(可以用变量实现) +------ 显示效果: +------ 总交易金额:1400.00 +------ 支取交易金额:200.00 +------ 存入交易金额:1200.00 + diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery7.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7b8bd585ada31b6b5dda057b2ee9d5e95c8fb5a9 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery7.sql" @@ -0,0 +1,116 @@ +use master +go + +create database ATM +on +( + name='ATM', + filename='D:\bank\ATM.mdf', + size=5mb, + maxsize=50mb, + filegrowth=15% +) + +log on +( + name='ATM_log', + filename='D:\bank\ATM_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=15% +) +go + +use ATM +go +create table userInfo +( + customerID int primary key identity(1,1), + customerName nchar(10) not null, + PID char(20) check(len(PID)=15 or len(PID)=18) unique(PID) not null, + telephone char(13) check(len(telephone)=13 and telephone like '____-________') not null, + address nchar(25) +) +go + +use ATM +create table cardInfo +( + cardID char(20) primary key check(substring(cardID,1,9)='1010 3576' and len(cardID)=18) not null, + curType nchar(10) default('RMB') not null, + savingType char(8) check(savingType='活期' or savingType='定活两便' or savingType='定期'), + openDate date default(getdate()) not null, + balance money check(balance>=1) not null, + pass char(6) default(888888) not null, + IsReportLoss char(2) check(IsReportLoss='是' or IsReportLoss='否') default('否') not null, + customerID int constraint FK_cardInfo_customerID foreign key references userInfo(customerID) not null +) +go + +use ATM +create table transInfo +( + transId int primary key identity(1,1), + transDate date default(getdate()) not null, + cardID char(20) constraint FK_transInfo_cardID references cardInfo(cardID) not null, + transType char(4) check(transType='存入'or transType='支取') not null, + transMoney money check(transMoney>0) not null, + remark text +) +go + +--根据下列条件插入和更新测试数据 +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +-- 开户金额:1000 活期 卡号:1010 3576 1234 567 + +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 117 + +--唐僧开户,身份证:321245678912345678,电话:0478-44443333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 113 + +insert into userInfo values ('孙悟空',123456789012345,'0716-78989783','北京海淀') +insert into userInfo(customerName,PID,telephone) +select '沙和尚',421345678912345678,'0478-44223333' union +select '唐僧',321245678912345678,'0478-44443333' + +insert into cardInfo(balance,savingType,cardID,customerID) +select 1000,'活期','1010 3576 1234 567',1 union +select 1,'定期','1010 3576 1212 117',2 union +select 1,'定期','1010 3576 1212 113',3 + +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass=611234 where cardID='1010 3576 1234 567' + +--2.用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 +insert into transInfo(transDate,cardID,transType,transMoney) +select getdate(),'1010 3576 1234 567','支取',200 +update cardInfo set balance -= 200 where cardID='1010 3576 1234 567' + +--3.用同上题一样的方法实现沙和尚存钱的操作(存300) +insert into transInfo(transDate,cardID,transType,transMoney) +select getdate(),'1010 3576 1212 117','存入',300 +update cardInfo set balance += 300 where cardID='1010 3576 1212 117' + +--4.唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的 +update cardInfo set IsReportLoss='是' where cardID='1010 3576 1212 113' + +--5.查询出最近10天开户的银行卡的信息 +select * from cardInfo where DateDiff(dd,openDate,getdate())<=10 + +--6.查询交易金额最大的银行卡信息, +select max(transMoney)交易金额最大 from transInfo + +--7. 再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) +-- 显示效果: +-- 总交易金额:1400.00 +-- 支取交易金额:200.00 +-- 存入交易金额:1200.00 +select sum(transMoney)总交易金额 from transInfo +select transMoney 支取交易金额 from transInfo where transId=1 +select transMoney 存入交易金额 from transInfo where transId=2 + +select * from cardInfo--银行卡信息表 +select * from userInfo--用户信息表 +select * from transInfo--交易信息表 diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\234\344\270\232.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..63cf4ed78dc72801cf974e228b838aabe46c7ab3 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\234\344\270\232.sql" @@ -0,0 +1,100 @@ +create database ATM +on +( + name='AMT', + filename='D:\bank\AMT.mfd', + size=10MB, + filegrowth=15% +) +log on +( + name='AMT_log', + filename='D:\bank\AMT_log.ldf', + size=10MB, + filegrowth=15% +) +go + +use ATM +go +--用户信息表:userInfo : +create table userInfo +( + customerID int identity(1,1) primary key, + customerName varchar(32) not null, + PID varchar(18) unique check(len(PID)=18 or len(PID)=15), + telephone char(13) check(telephone like '____-________' or len(telephone)=13), + address varchar(200) +) +--银行卡信息表:cardInfo +create table cardInfo +( + cardID char(18) check(cardID like '1010 3576 ____ ___') primary key, + --cardID char(18) check(substring(cardID,1,10)='1010 3576 ' and len(cardID)=18) + curType varchar(30) not null default('RMB'), + savingType nvarchar(4) check(savingType in('活期','定活两便','定期')) not null, + openDate date default(getdate()), + balance money check(balance>0) not null, + pass char(6) not null default('888888'), + IsReportLoss char(2) check(IsReportLoss in('是','否')) default('否') not null, + customerID int references userInfo(customerID) +) +--交易信息表:transInfo +create table transInfo +( + transId int identity(1,1) primary key, + transDate datetime default(getdate()) not null, + cardID char(18) check(cardID like '1010 3576 ____ ___') + references cardInfo(cardID) not null,--not unique + transType nvarchar(2) check(transType in('存入','支取')) not null, + transMoney money check(transMoney>0), + remark varchar(200) +) +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +-- 开户金额:1000 活期 卡号:1010 3576 1234 567 +insert into userInfo values ('孙悟空','123456789012345','0716-78989783','北京海淀') +insert into cardInfo(cardID,savingType,balance,customerID) values ('1010 3576 1234 567','活期',1000,1) +select * from userInfo +select * from cardInfo +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 117 +insert into userInfo values ('沙和尚','421345678912345678','0478-44223333',' ') +insert into cardInfo(cardID,savingType,balance,customerID) values ('1010 3576 1212 117','定期',1,2) + +--唐僧开户,身份证:321245678912345678,电话:0478-44443333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 113 +insert into userInfo values ('唐僧','321245678912345678','0478-44443333',' ') +insert into cardInfo(cardID,savingType,balance,customerID) values ('1010 3576 1212 113','定期',1,3) +update userInfo set address=' ' where customerID=2 +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass='611234' where customerID=1 +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 +select * from userInfo +select * from cardInfo +select * from transInfo +insert into transInfo(cardID,transType,transMoney) values ('1010 3576 1234 567','支取',200) +update cardInfo set balance=balance-200 where customerID=1 + +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) +insert into transInfo(cardID,transType,transMoney) values ('1010 3576 1212 117','存入',300) +update cardInfo set balance=balance+300 where customerID=2 + +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” +update cardInfo set IsReportLoss='是' where customerID=3 + +--5. 查询出最近10天开户的银行卡的信息 +select * from cardInfo where openDate>='2021-03-09' and openDate<='2021-03-19' + + +--6. 查询交易金额最大的银行卡信息,子查询实现 +select * from cardInfo where cardID=(select cardID from transInfo where transMoney=(select max(transMoney) from transInfo)) + +--7. 再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) +select sum(transMoney) from transInfo +select sum(transMoney) from transInfo where transType='存入' +select sum(transMoney) from transInfo where transType='支取' +-- 显示效果: +-- 总交易金额:1400.00 +-- 支取交易金额:200.00 +-- 存入交易金额:1200.00 \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\275.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\275.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5935d6969dc235b364050f710e2ee6de82c3e134 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\275.sql" @@ -0,0 +1,87 @@ +create database ATM +go +use ATM +go + + +--用户信息表 +create table userInfo +( + customerID int identity, + customerName char(10) not null, + PID varchar(18) not null check(len(PID)=15 or len(PID)=18), + telephone char(13) not null check(len(telephone)=13 and telephone like'____-________'), + address text , +) +--添加主键 +alter table userInfo add constraint PK_userInfo_customerID primary key(customerID) +--唯一约束 +alter table userInfo add constraint UK_userInfo_PID unique(PID) + +--银行卡信息表 +create table cardInfo +( + cardID varchar(20) not null, + curType varchar(10) not null default('RMB'), + savingType varchar(10) check(savingType='活期' or savingType='定活两便' or savingType='定期'), + openDate datetime not null default(Getdate()), + balance int not null check(balance>=1), + pass int not null default('888888') check(len(pass)=6), + IsReportLoss char(4) not null default('否') check(IsReportLoss='是' or IsReportLoss='否'), + customerID int not null, +) +--添加主键 +alter table cardInfo add constraint PK_cardInfo_cardID primary key(cardID) +--添加外键约束关联用户信息表的顾客编号 +alter table cardInfo add constraint FK_cardInfo_customerID foreign key(customerID) references userInfo(customerID) + +--交易信息表 + +create table transInfo +( + transId int identity, + transDate time not null default('2021-3-19'), + cardID varchar(18) not null, + transType char(4) not null check(transType='存入' transType='支取'), + transMoney int not null check(transMoney>0), + remark text , +) +--插入外键与银行卡信息表卡号关联 +alter table transInfo add constraint FK_transInfo_cardID foreign key(cardID) references cardInfo(cardID) + +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +--开户金额:1000 活期 卡号:1010 3576 1234 567 +insert into userInfo(customerName,PID,telephone,address) values ('孙悟空','123456789012345','0716-78989783','北京海淀') +insert into cardInfo(balance,savingType,cardID,customerID) values(1000,'活期','1010 3576 1234 567',1) + +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333, +--开户金额: 1 定期 卡号:1010 3576 1212 117 +insert into userInfo(customerName,PID,telephone) values('沙和尚','421345678912345678','0478-44223333') +insert into cardInfo(balance,savingType,cardID,customerID) values(1,'定期','1010 3576 1212 117',2) + +--唐僧开户,身份证:321245678912345678,电话:0478-44443333, +--开户金额: 1 定期 卡号:1010 3576 1212 113 +insert into userInfo(customerName,PID,telephone) values('唐僧','321245678912345678','0478-44443333') +insert into cardInfo(balance,savingType,cardID,customerID) values(1,'定期','1010 3576 1212 113',3) +select * from cardInfo + +--1.将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass='611234' where customerID=1 + +--2.用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 +insert into transInfo(cardID,transType,transMoney) values ('1010 3576 1234 567','支取',200) +update cardInfo set balance=balance-200 where customerID=1 + +--3.用同上题一样的方法实现沙和尚存钱的操作(存300) +insert into transInfo(cardID,transType,transMoney) values ('1010 3576 1212 117','存入',300) +update cardInfo set balance=balance-200 where customerID=2 + +--4.唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” +update cardInfo set IsReportLoss='是' where customerID=3 + +--5.查询出最近10天开户的银行卡的信息 +select top 10 * from cardInfo + +--6.查询交易金额最大的银行卡信息,子查询实现 +select max(transMoney)交易金额最大 from transInfo \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery.sql" new file mode 100644 index 0000000000000000000000000000000000000000..afcbf5d13d3b838237f4faed1ae2d2e8555d545f --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery.sql" @@ -0,0 +1,109 @@ +use master +go + +create database bank +on +( + name='bank', + filename='D:\bank\bank.mdf', + size=5mb, + maxsize=100mb, + filegrowth=15% +) +log on +( + name='bank_log', + filename='D:\bank\bank_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=15% +) + +use bank +go + +create table userlnfo --用户信息表 +( + customerID int primary key identity(1,1), + customerName varchar(20) not null, + PID char(18) not null check(len(PID)=18 or len(PID)=15) unique, + telephone varchar(13) not null check(len(telephone)=13 and telephone like '____-________'), + address nvarchar(20) +) + +create table cardInfo --银行卡信息表 +( + cardID varchar(18) primary key ,--卡号 + curType varchar(20) not null default('RMB'), + savingType nchar(10) check(savingType='活期'or savingType='定活两便' or savingType='定期'), + openDate datetime not null default(getdate()), + balance int not null check(balance>=1), + pass varchar(6) check(len(pass)=6) default('888888'), + IsReportLoss varchar(2) not null default('否') check(IsReportLoss='是' or IsReportLoss='否'), + customerID int references userInfo(customerID) not null +) + +create table transInfo --交易信息表 +( + transId int primary key identity, + transDate datetime default('2021年3月19日') not null, + cardID nchar(21) not null references cardInfo(cardID), + transType varchar(4) not null check(transType='存入' or transType='支取'), + transMoney money check(transMoney>0), + remark text +) + +--根据下列条件插入和更新测试数据 +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +--开户金额:1000 活期 卡号:010 3576 1234 567 + +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333, +--开户金额:1000 定期 卡号:1010 3576 1212 117 + +--唐僧开户,身份证:321245678912345678,电话:0478-44443333, +--开户金额:1000 定期 卡号:100 3576 1212 113 + +select * from userInfo +insert into userInfo values('孙悟空开户','123456789012345','0716-78989783','北京海淀') +insert into userInfo(customerName,PID,telephone) values('沙和尚开户','421345678912345678','0478-44223333') +insert into userInfo(customerName,PID,telephone) values('唐僧开户','321245678912345678','0478-44443333') + +select * from cardInfo +insert into cardInfo(balance,savingType,cardID,customerID) +values (1000,'活期','1000 3576 1234 567',1) +insert into cardInfo(balance,savingType,cardID,customerID) +values (1,'定期','1000 3576 1212 117',2) +insert into cardInfo(balance,savingType,cardID,customerID) +values (1,'定期','1000 3576 1212 113',3) + +--将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass='611234' where customerID=1 + +--用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 +select * from transInfo +insert into transInfo(cardID,transType,transMoney) +values ('1010 3576 1234 567','支取','200') +update cardInfo set balance=balance-200 where customerID=1 + +--用同上题一样的方法实现沙和尚存钱的操作(存300) +select * from transInfo +insert into transInfo(cardID,transType,transMoney) +values ('1010 3576 1212 117','存入','300') +update cardInfo set balance=balance+300 where customerID=2 + +--唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为 +update cardInfo set IsReportLoss='是' where customerID=3 + +--询出最近10天开户的银行卡的信息 +select top 10 * from cardInfo where openDate>='2021-3-10' + + +--查询交易金额最大的银行卡信息,子查询实现 +select top 1 * from cardInfo where balance>1 order by balance DESC + +--再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) +select sum (transMoney)总交易金额 from transInfo +select sum (transMoney)支取交易金额 from transInfo where transType='支取' +select sum (transMoney)存入交易金额 from transInfo where transType='存入' +No newline at end of file \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy7.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3b0a5036508411243e56399ce9e7df52cc637039 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy7.sql" @@ -0,0 +1,79 @@ +create database ATMsystem +on +( + name='ATMsystem', + filename='D:\bank.mdf', + filegrowth=15% +) +log on +( + name='ATMsystem_log', + filename='D:\bank_log.mdf', + filegrowth=15% +) +go +use ATMsystem +go + +create table userInfo +( + customerID int identity(1,1) primary key, + customerName varchar(32) not null, + PID varchar(18) unique check(len(PID)=18 or len(PID)=15), + telephone char(13) check(telephone like '____-________' or len(telephone)=13), + address varchar(200) +) + +create table cardInfo +( + cardID char(18) check(cardID like '1010 3576 ____ ___') primary key, + curType varchar(30) not null default('RMB'), + savingType nvarchar(4) check(savingType in('活期','定活两便','定期')) not null, + openDate date default(getdate()), + balance money check(balance>0) not null, + pass char(6) not null default('888888'), + IsReportLoss char(2) check(IsReportLoss in('是','否')) default('否') not null, + customerID int references userInfo(customerID) +) +create table transInfo +( + transId int identity(1,1) primary key, + transDate datetime default(getdate()) not null, + cardID char(18) check(cardID like '1010 3576 ____ ___') + references cardInfo(cardID) not null, + transType nvarchar(2) check(transType in('存入','支取')) not null, + transMoney money check(transMoney>0), + remark varchar(200) +) +insert into userInfo values ('孙悟空','123456789012345','0716-78989783','北京海淀') +insert into cardInfo(customerID,balance,savingType,cardID) values(1,1000,'活期','1010 3576 1234 567') +select * from userInfo +select * from cardInfo +insert into userInfo values ('沙和尚','421345678912345678','0478-44223333','') +insert into cardInfo(customerID,balance,savingType,cardID) values(2,1,'定期','1010 3576 1212 117') +select * from userInfo +select * from cardInfo +insert into userInfo values ('唐僧','321245678912345678','0478-44443333','') +insert into cardInfo(customerID,balance,savingType,cardID) values(3,1,'定期','1010 3576 1212 113') +select * from userInfo +select * from cardInfo + +update cardInfo set pass='611234' where customerID=1 + +select * from userInfo +select * from cardInfo +select * from transInfo +insert into transInfo(cardID,transType,transMoney) values ('1010 3576 1234 567','支取',200) +update cardInfo set balance=balance-200 where customerID=1 +insert into transInfo(cardID,transType,transMoney) values ('1010 3576 1212 117','存入',300) +update cardInfo set balance=balance+300 where customerID=2 + +update cardInfo set IsReportLoss='是' where customerID=3 + +select * from cardInfo where openDate>='2021-03-09' and openDate<='2021-03-19' + +select * from cardInfo where cardID=(select cardID from transInfo where transMoney=(select max(transMoney) from transInfo)) + +select sum(transMoney) from transInfo +select sum(transMoney) from transInfo where transType='存入' +select sum(transMoney) from transInfo where transType='支取' \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery11.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery11.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d44847cf7fd78c1f54329d9e8beef1ee92b3b98a --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery11.sql" @@ -0,0 +1,88 @@ +use master +go + +create database ATM +on +( +name='ATM', +filename='D:\bank.mdf', +size=5MB, +filegrowth=15MB, +maxsize=50MB +) +log on +( +name='ATM_log', +filename='D:\bank.ldf', +size=5MB, +filegrowth=15MB, +maxsize=50MB +) +go + +use ATM +go + +create table userInfo +( +customerID int primary key identity(1,1), +customerName varchar(20) not null, +PID varchar(20) check(len(PID)=15 or len(PID)=18) unique not null, +telephone varchar(20) check(telephone like'____-________' and len(telephone)=13), +address varchar(20) +) + +create table cardInfo +( +cardID varchar(20) primary key check(substring(cardID,1,9)='1010 3576' and len(cardID)=18), +curType varchar(20) default('RMB') not null, +savingType varchar(20), +openDate datetime default(getdate()) not null, +balance money check(balance>=1) not null, +pass varchar(10) default('888888') check(len(pass)=6), +IsReportLoss char(2) check(IsReportLoss='是' or IsReportLoss='否') default('否') not null, +customerID int foreign key references userInfo(customerID) +) + +create table transInfo +( +transId int primary key identity(1,1), +transDate datetime default(getdate()) not null, +cardID varchar(20) foreign key references cardInfo(cardID) not null, +transType varchar(20) check(transType='存入' or transType='支取') not null, +transMoney money check(transMoney>0) not null, +remark text +) + +insert into userInfo(customerName,PID,telephone,address) values +('孙悟空','123456789012345','0716-78989783','北京海淀') +insert into userInfo(customerName,PID,telephone) values +('沙和尚','421345678912345678','0478-44223333'), +('唐僧','321245678912345678','0478-44443333') + +insert into cardInfo(customerID,balance,savingType,cardID) values +(1,'1000','活期','1010 3576 1234 567'), +(2,'1','定期','1010 3576 1212 117'), +(3,'1','定期','1010 3576 1212 113') + +update cardInfo set pass='611234' where balance=1000.00 + +select customerID from userInfo where customerName='孙悟空' +select cardID from cardInfo where customerID=1 +update cardInfo set balance=800 where cardID='1010 3576 1234 567' +alter table transInfo drop constraint FK__transInfo__cardI__20C1E124 +insert into transInfo(cardID,transType,transMoney) values(1,'支取',200) + +select customerID from userInfo where customerName='沙和尚' +select cardID from cardInfo where customerID=2 +update cardInfo set balance=301 where cardID='1010 3576 1212 117' +insert into transInfo(cardID,transType,transMoney) values(3,'存入',300) + +update cardInfo set IsReportLoss='是' where customerID=3 + + +select * from cardInfo where openDate>=2021-03-09 or openDate>=2021-03-19 + +select max(balance) from cardInfo + +select sum(transMoney) from transInfo diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery3.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..043741964a1cc6195a54ed4b835e0283c3f1ee3b --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery3.sql" @@ -0,0 +1,78 @@ +use master +go + +create database ATM +on +( +name='ATM', +filename='D:\bank\ATM.mdf', +size=5mb, +maxsize=50mb, +filegrowth=15mb +) +log on +( +name='ATM_log', +filename='D:\bank\ATM_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=15mb +) +go +use ATM +go +create table userInfo +( +customerID int identity(1,1) primary key, +customerName varchar(20) not null, +PID int not null check(len(PID)=15 or len(PID)=18) unique, +telephone char(13) not null check(len(telephone)=13 and telephone like '____-________'), +address varchar(20) +) +create table cardInfo +( +cardID char(13) not null primary key check(substring(cardID,1,9)='1010 3576' and len(cardID)=18), +curType varchar(20) default('RMB') not null, +savingType varchar(20) check(savingType='活期' and savingType='定活两便' and savingType='定期'), +openDate datetime not null default(getdate()), +balance money check(balance>=1) not null, +pass int not null default('888888') check(len(pass)=6), +IsReportLoss char(2) check(IsReportLoss='是' or IsReportLoss='否') default('否'), +customerID int foreign key references userInfo(customerID) +) +create table transInfo +( +transId int identity(1,1) primary key, +transDate datetime default(getdate()) not null, +cardID char(13) not null foreign key references cardInfo(cardID), +transType varchar(20) check(transType='存入' or transType='支取') not null, +transMoney money check(transMoney>0), +remark text +) + +insert into userInfo(customername,PID,telephone,address) values +('孙悟空','123456789012345','0716-78989783','北京海滨') +insert into userInfo(customername,PID,telephone) values +('沙和尚','421345678912345678','0478-44223333') +insert into userInfo(customername,PID,telephone) values +('唐僧','32145678912345678','0478-44443333') + +insert into cardInfo(customerID,savingType,cardID) values +(1,'1000','活期','1010 3576 1234 567'), +(2,'1','定期','1010 3576 1212 117'), +(3,'1','定期','1010 3576 1212 113') + +update cardInfo set pass='611234' where customerID=1 + +select * from userInfo +select * from cardInfo +select * from transInfo +insert into transInfo(cardID,transType,transMoney) values ('1010 3576 1234 567','支取',200) +update cardInfo set balance=balance-200 where customerID=1 + +insert into transInfo(cardID,transType,transMoney) values ('1010 3576 1212 117','存入',300) +update cardInfo set balance=balance+300 where customerID=2 + +update cardInfo set IsReportLoss='是' where customerID=3 + +select * from cardInfo where openDate>='2021-03-09' and openDate<='2021-03-19' diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f5bccc247ab4b09c1e44e294cbb0c0cd73e20a46 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" @@ -0,0 +1,89 @@ +use master +go +create database bank +on( +name='bank', +filename='D:\bank\bank.mdf', +size=5, +maxsize=10, +filegrowth=15% +) +log on( +name='bank_log', +filename='D:\bank\bank_log.ldf', +size=5, +maxsize=10, +filegrowth=15% +) +go +use bank +go +create table userInfo +( +customerID int primary key identity(1,1), +customerName nvarchar(5) not null, +PID nvarchar(18) unique check(len(PID)=18 OR len(PID)=15) not null, +telephone nvarchar(14) check(telephone like '____-________' and len(telephone)=13) not null, +address text, +) +create table cardInfo +( +cardID nvarchar(20) not null primary key check(cardID like '1010 3576 ____ ___'),--check(subsrting(cardID,1,9='1010 3576')) +curType nvarchar(20) not null default('RMB'), +savingType NVARCHAR(20) check(savingType='活期' or savingType='定期' or savingType='定活两便'), +openDate DATETIME not null default(getdate()), +balance int not null check(balance>=1), +pass int not null check(len(pass)=6) default('888888'), +IsReportLoss nchar(1) not null check(IsReportLoss='是' or IsReportLoss='否') default('否'), +customerID int not null references userInfo(customerID) +) + +--销户 +delete from cardInfo where balance<1 +create table transInfo +( +transId int primary key identity(1,1), +transDate datetime not null default(getdate()), +cardID nvarchar(20) , +transType nchar(2) not null check(transType='存入' or transType='支取'), +transMoney int not null check(transMoney>0), +remark text +) +insert into userInfo +select '孙悟空','123456789012345','0716-78989783','北京海淀' union +select '沙和尚','421345678912345678','0478-44223333',''union +select '唐僧','321245678912345678','0478-44443333','' + +select * from userInfo +insert into cardInfo(cardID,savingType,balance,customerID) +select '1010 3576 1234 567','活期','1000','1' union +select '1010 3576 1212 117','定期','1','2' union +select '1010 3576 1212 113','定期','1','3' +select * from cardInfo +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass=611234 where savingType='活期' +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 +select customerID from userInfo where customerName='孙悟空' +select cardID from cardInfo where customerID=2 +insert into transInfo(transDate,cardID,transType,transMoney) +SELECT getdate(),'1010 3576 1234 567','存入','200' +update transInfo set transMoney=1 where cardID ='1010 3576 1234 567' +select * from transInfo +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) +select customerID from userInfo where customerName='沙和尚' +select cardID from cardInfo where customerID=1 +insert into transInfo(transDate,cardID,transType,transMoney) +select getdate(),'1010 3576 1212 117','存入','200' +update transInfo set transMoney=1 where cardID='1010 3576 1212 117' +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” +update cardInfo set IsReportLoss='是' where cardID='1010 3576 1212 113' +--5. 查询出最近10天开户的银行卡的信息 +select * from cardInfo where DateDiff(dd,openDate,getdate())<=10 +--6. 查询交易金额最大的银行卡信息,子查询实现 +select max(transMoney) from transInfo + +select transDate from transInfo +--7. 再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) +-- 显示效果: +-- 总交易金额:1400.00 diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..004e7c4490dc85728c3c28c65064d667ec731f99 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery1.sql" @@ -0,0 +1,94 @@ +use master +go + +create database ATM +on +( +name='ATM', +filename='C:\SQL\ATM.mdf', +size=10MB, +maxsize=100MB, +filegrowth=15% +) +log on +( +name='ATM_log', +filename='C:\SQL\ATM_log.ldf', +size=10MB, +maxsize=100MB, +filegrowth=15% +) +go +use ATM +go +create table UserInfo +( +CustomerID int primary key identity(1,1), +CustomerName nvarchar(10) not null, +PID varchar(20) check(len(PID)=15 or len(PID)=18) unique not null, +TelePhone char(13) check(len(TelePhone)=13 and TelePhone like '____-________') not null, +Address text +) +go +use ATM +go +create table CardInfo +( +CardID char(18) primary key check(len(CardID)=18 and CardID like '1010 3576 ____ ___') not null, +CurType char(10) default('RMB') not null, +SavingType char(8) check(SavingType='活期' or SavingType='定活两便' or SavingType='定期'), +OpenDate datetime default(getdate()) not null, +BaLance int check(BaLance>=1) not null, +Pass char(6) default('888888') not null, +IsReportLoss char(2) check(IsReportLoss='是' or IsReportLoss='否') default('否') not null, +CustomerID int references UserInfo(CustomerID) not null, +) +go +use ATM +go +create table TransInfo +( +TransId int primary key identity(1,1) , +TransDate datetime default(getdate()) not null, +CardID char(18) references CardInfo(CardID) not null, +TransType char(4) check(TransType='存入' or TransType='支出') not null, +TransMoney int check(TransMoney>0) not null, +Remark text +) + +select * from UserInfo +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +-- 开户金额:1000 活期 卡号:1010 3576 1234 567 +insert into UserInfo (CustomerName , PID , TelePhone , Address) values('孙悟空','123456789012345','0716-78989783','北京海定') +insert into CardInfo (CardID , SavingType , BaLance , CustomerID) values('1010 3576 1234 567','活期','1000',1) + + +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333,河底下 +-- 开户金额: 1 定期 卡号:1010 3576 1212 117 +insert into UserInfo (CustomerName , PID , TelePhone , Address) values('沙和尚','421345678912345678','0478-44223333','河底下') +insert into CardInfo (CardID , SavingType , BaLance , CustomerID) values('1010 3576 1212 117','定期','1',2) + +--唐僧开户,身份证:321245678912345678,电话:0478-44443333,东土大唐 +-- 开户金额: 1 定期 卡号:1010 3576 1212 113 +insert into UserInfo (CustomerName , PID , TelePhone , Address) values('唐僧','321245678912345678','0478-44443333','东土大唐') +insert into CardInfo (CardID , SavingType , BaLance , CustomerID) values('1010 3576 1212 113','定期','1',3) + + +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” +update CardInfo SET Pass='611234' where CustomerID=1 + +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 +select * from TransInfo +insert into TransInfo(CardID,TransType,TransMoney) values ('1010 3576 1234 567','支出','200') +update CardInfo SET BaLance=BaLance-200 where CustomerID=1 + +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) +insert into TransInfo(CardID,TransType,TransMoney) values ('1010 3576 1212 117','存入','300') +update CardInfo SET BaLance=BaLance+300 where CustomerID=2 + +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” +update CardInfo SET IsReportLoss='是' where CustomerID=3 + +--5. 查询出最近10天开户的银行卡的信息 +select top 10 * from CardInfo where OpenDate>='2021-3-10' \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/SQLQuery.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/SQLQuery.sql" new file mode 100644 index 0000000000000000000000000000000000000000..afcbf5d13d3b838237f4faed1ae2d2e8555d545f --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/SQLQuery.sql" @@ -0,0 +1,109 @@ +use master +go + +create database bank +on +( + name='bank', + filename='D:\bank\bank.mdf', + size=5mb, + maxsize=100mb, + filegrowth=15% +) +log on +( + name='bank_log', + filename='D:\bank\bank_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=15% +) + +use bank +go + +create table userlnfo --用户信息表 +( + customerID int primary key identity(1,1), + customerName varchar(20) not null, + PID char(18) not null check(len(PID)=18 or len(PID)=15) unique, + telephone varchar(13) not null check(len(telephone)=13 and telephone like '____-________'), + address nvarchar(20) +) + +create table cardInfo --银行卡信息表 +( + cardID varchar(18) primary key ,--卡号 + curType varchar(20) not null default('RMB'), + savingType nchar(10) check(savingType='活期'or savingType='定活两便' or savingType='定期'), + openDate datetime not null default(getdate()), + balance int not null check(balance>=1), + pass varchar(6) check(len(pass)=6) default('888888'), + IsReportLoss varchar(2) not null default('否') check(IsReportLoss='是' or IsReportLoss='否'), + customerID int references userInfo(customerID) not null +) + +create table transInfo --交易信息表 +( + transId int primary key identity, + transDate datetime default('2021年3月19日') not null, + cardID nchar(21) not null references cardInfo(cardID), + transType varchar(4) not null check(transType='存入' or transType='支取'), + transMoney money check(transMoney>0), + remark text +) + +--根据下列条件插入和更新测试数据 +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +--开户金额:1000 活期 卡号:010 3576 1234 567 + +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333, +--开户金额:1000 定期 卡号:1010 3576 1212 117 + +--唐僧开户,身份证:321245678912345678,电话:0478-44443333, +--开户金额:1000 定期 卡号:100 3576 1212 113 + +select * from userInfo +insert into userInfo values('孙悟空开户','123456789012345','0716-78989783','北京海淀') +insert into userInfo(customerName,PID,telephone) values('沙和尚开户','421345678912345678','0478-44223333') +insert into userInfo(customerName,PID,telephone) values('唐僧开户','321245678912345678','0478-44443333') + +select * from cardInfo +insert into cardInfo(balance,savingType,cardID,customerID) +values (1000,'活期','1000 3576 1234 567',1) +insert into cardInfo(balance,savingType,cardID,customerID) +values (1,'定期','1000 3576 1212 117',2) +insert into cardInfo(balance,savingType,cardID,customerID) +values (1,'定期','1000 3576 1212 113',3) + +--将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass='611234' where customerID=1 + +--用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 +select * from transInfo +insert into transInfo(cardID,transType,transMoney) +values ('1010 3576 1234 567','支取','200') +update cardInfo set balance=balance-200 where customerID=1 + +--用同上题一样的方法实现沙和尚存钱的操作(存300) +select * from transInfo +insert into transInfo(cardID,transType,transMoney) +values ('1010 3576 1212 117','存入','300') +update cardInfo set balance=balance+300 where customerID=2 + +--唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为 +update cardInfo set IsReportLoss='是' where customerID=3 + +--询出最近10天开户的银行卡的信息 +select top 10 * from cardInfo where openDate>='2021-3-10' + + +--查询交易金额最大的银行卡信息,子查询实现 +select top 1 * from cardInfo where balance>1 order by balance DESC + +--再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) +select sum (transMoney)总交易金额 from transInfo +select sum (transMoney)支取交易金额 from transInfo where transType='支取' +select sum (transMoney)存入交易金额 from transInfo where transType='存入' +No newline at end of file \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/\347\273\203\344\271\240.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/\347\273\203\344\271\240.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2fc3a090f49add168090197fef49de1f6861bc0b --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/\347\273\203\344\271\240.sql" @@ -0,0 +1,101 @@ +create database ATM +on +( + name='ATM', + filename='D:\bank\ATM.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +log on +( + name='ATM_log', + filename='D:\bank\ATM_log.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +go + +use ATM +go +create table userInfo +( + customerID int primary key identity(1,1), + customerName nvarchar(20) not null, + PID varchar(18) check(len(PID)>=15 or PID >=18) unique not null, + telephone varchar(20) check(len(telephone)=13 and telephone like '____-________') not null, + address nvarchar(50) +) +create table cardInfo +( + cardID nvarchar(30) primary key check(len(cardID)=18 and cardID like '1010 3576 ____ ___'), + curType nvarchar(10) default('RMB'), + openDate datetime default(getdate()), + balance money not null check(balance>=1), + pass varchar(6) not null check(len(pass)=6) default('888888'), + IsReportLoss char(2) not null default('鍚'), + customerID int foreign key references userInfo(customerID) +) +drop table cardInfo +alter table cardInfo add savingType varchar(4) check (savingType in ('娲绘湡','瀹氭椿涓や究','瀹氭湡')) +create table transInfo +( + transId int identity(1,1) primary key, + transDate date not null default(getdate()), + cardID nvarchar(30) not null foreign key references cardInfo(cardID), + transType varchar(4) check(transType='瀛樺叆' or transType='鏀彇'), + transMoney money not null check(transMoney>0), + remark ntext +) +drop table transInfo +--瀛欐偀绌哄紑鎴凤紝韬唤璇侊細123456789012345锛岀數璇濓細0716-78989783锛屽湴鍧锛氬寳浜捣娣 +-- 寮鎴烽噾棰濓細1000 娲绘湡 -鍗″彿锛1010 3576 1234 567 + +--閾惰寮鎴疯〃 +insert into userInfo(customerName,PID,telephone,address) +select '瀛欐偀绌',123456789012345,'0716-78989783','鍖椾含娴锋穩'union +select '娌欏拰灏',421345678912345678,'0478-44223333' ,null union +select '鍞愬儳', 321245678912345678,'0478-44443333',null +select * from userInfo +--閾惰鍗′俊鎭〃 +select * from cardInfo + +insert into cardInfo(cardID,savingType,balance,customerID) +select '1010 3576 1234 567','娲绘湡',1000,1 union +select '1010 3576 1212 117','瀹氭湡',1,2 union +select '1010 3576 1212 113','瀹氭湡',1,3 + +select * from transInfo + +--1.灏嗙敤鎴封滃瓩鎮熺┖鈥濆紑鍗℃椂鐨勫垵濮嬪瘑鐮佹洿鏀逛负鈥611234鈥 +update cardInfo set pass ='611234' where customerID='1' +--2. 鐢ㄤ袱鏉QL璇彞瀹炵幇瀛欐偀绌鸿鍙栭挶(鍙200)鐨勬搷浣滐紝鍏堝悜浜ゆ槗淇℃伅琛ㄦ彃鍏ヤ竴鏉″彇閽辩殑浜ゆ槗璁板綍锛岀劧鍚庡湪瀛欐偀绌鸿处涓婄殑浣欓鍑200 +--娉ㄦ剰锛氬厛瑕佸皢鐢ㄦ埛瀛欐偀绌虹殑鐢ㄦ埛缂栧彿鎵惧埌锛屽啀鏍规嵁鐢ㄦ埛缂栧彿鎵惧埌鍗″彿锛屽啀鏍规嵁閾惰鍗″彿鏉ユ彃鍏ヤ氦鏄撹褰曞拰淇敼璐︿笂浣欓 +--鏇存柊閾惰鍗′俊鎭〃 +select * from cardInfo +insert into transInfo(cardID,transType ,transMoney) +select '1010 3576 1234 567','鏀彇',200 +update cardInfo set balance=balance-200 where cardID='1010 3576 1234 567' + +--3. 鐢ㄥ悓涓婇涓鏍风殑鏂规硶瀹炵幇娌欏拰灏氬瓨閽辩殑鎿嶄綔(瀛300) +select * from cardInfo +insert into transInfo (cardID,transType,transMoney) +select '1010 3576 1212 117','瀛樺叆',300 +update cardInfo set balance=balance+300 where cardID='1010 3576 1212 113' + + +--4. 鍞愬儳鐨勫崱涓簡锛岄渶瑕佹寕澶憋紝灏嗗攼鍍х殑閾惰鍗$殑鏄惁鎸傚け瀛楁鐨勫兼敼涓衡滄槸鈥 +update cardInfo set IsReportLoss='鏄' where customerID='3' +--5. 鏌ヨ鍑烘渶杩10澶╁紑鎴风殑閾惰鍗$殑淇℃伅 +select * from cardInfo where openDate>='2021-03-09' or openDate<= '2021-03-19' +--6. 鏌ヨ浜ゆ槗閲戦鏈澶х殑閾惰鍗′俊鎭紝瀛愭煡璇㈠疄鐜 +select max(transMoney) from transInfo +select*from transInfo where transMoney in(select max(transMoney) from transInfo) +--7. 鍐嶄氦鏄撲俊鎭〃涓紝灏嗘荤殑浜ゆ槗閲戦锛屾敮鍙栫殑浜ゆ槗閲戦锛屽瓨鍏ョ殑浜ゆ槗閲戦鏌ヨ鍑烘潵骞惰緭鍑烘樉绀 +select sum(transMoney) from transInfo +select sum(transMoney) from transInfo where transType='瀛樺叆' +select sum(transMoney) from transInfo where transType='鏀彇' + + + diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery7.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c08e0d1a2c3511d26830d0d41bbf9fb8beec7e9a --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery7.sql" @@ -0,0 +1,196 @@ +create database ATMSystem + +on + +( + + FileName='D:\bank\ATMSystem.mdf', + + Name='ATMSystem', + + size=5MB, + + MAXsize=10MB, + + filegrowth=15% + +) + +log on + +( + + FileName='D:\bank\ATMSystem_log.ldf', + + Name='ATMSystem_log', + + size=5MB, + + MAXsize=10MB, + + filegrowth=15% + +) + +go + + + +use ATMSystem + +go + + + +create table userInfo + +( + + customerID int identity(1,1) primary key, + + customerName nvarchar(10) not null, + + PID nvarchar(18) not null check(len(PID)=18 or len(PID)=15), + + telephone nvarchar(15) not null check(telephone like '____-________' and len(telephone)=13), + + address nvarchar(20) + +) + + + + + +create table cardInfo + +( + + cardID nvarchar(18) not null primary key check(substring(cardID,1,9)='1010 3576' and len(cardID)=18) ,--1010 3576 xxxx xxx + + curType nvarchar(10) not null default('RMB'), + + savingType nvarchar(10) check(savingType in('活期','定活两便','定期')), + + openDate datetime not null default(getdate()), + + balance int not null check(balance>=1),--? + + pass varchar(6) not null check(len(pass)=6) default('888888'), + + IsReportLoss nvarchar(1) not null default('否'), + + customerID int not null foreign key references userInfo(customerID) + +) + + + + + +create table transInfo + +( + + transID int identity(1,1) primary key, + + transDate datetime not null default(getdate()), + + cardID nvarchar(18) not null foreign key references cardInfo(cardID),--? + + transType nvarchar(2) not null check(transType in ('存入','支取')), + + transMoney int not null check(transMoney>0), + + remark text + +) + + + +insert into userInfo values + +('孙悟空','123456789012345','0716-78989783','北京海淀'), + +('沙和尚','421345678912345678','0478-44223333',''), + +('唐僧','321245678912345678','0478-44443333','') + + + + + +insert into cardInfo (cardID,balance,customerID) values + +('1010 3576 1234 567',1000,1), + +('1010 3576 1212 117',1,2), + +('1010 3576 1212 113',1,3) + + + + + + + + + + + +select * from userInfo + + + +select * from cardInfo + +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” + +update cardInfo set pass=611234 where customerID=1 + +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 + +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 + +--select customerID from userInfo where customerName='孙悟空'用户编号 + +--select cardID from cardInfo where customerID=(select customerID from userInfo where customerName='孙悟空')卡号 + +update cardInfo set balance=balance-200 where cardID=(select cardID from cardInfo where customerID=(select customerID from userInfo where customerName='孙悟空')) + +--用同上题一样的方法实现沙和尚存钱的操作(存300) + +--select customerID from userInfo where customerName='沙和尚' + +--select cardID form cardInfo where customerID=(select customerID from userInfo where customerName='沙和尚') + +update cardInfo set balance=balance+300 where cardID=(select cardID from cardInfo where customerID=(select customerID from userInfo where customerName='沙和尚')) + +--4.唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” + +update cardInfo set IsReportLoss='是' where customerID=3 + +--5.查询出最近10天开户的银行卡的信息 + +select * from cardInfo where openDate>=dateadd(dd,-10,getdate()) + +--6.查询交易金额最大的银行卡信息,子查询实现 +insert into transInfo (cardID,transType,transMoney) values +('1010 3576 1234 567','支取',200), +('1010 3576 1212 117','存入',300) + +--select MAX(transMoney) from transInfo最大金额 +--select cardID from transInfo where transMoney=(select MAX(transMoney) from transInfo)最大金额卡号 + +select * from cardInfo where cardID=(select cardID from transInfo where transMoney=(select MAX(transMoney) from transInfo)) + +--7.再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现)显示效果: +--总交易金额:1400.00 +--支取交易金额:200.00 +--存入交易金额:1200.00 + +select sum(transMoney) 总交易金额 from transInfo + +select transMoney 支取的交易金额 from transInfo where transType='存入' + +select transMoney 存入的交易金额 from transInfo where transType='支取' diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\344\275\234\344\270\232.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e0a7edd0b2e92226860b1c6a870ffb2101383138 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\344\275\234\344\270\232.sql" @@ -0,0 +1,113 @@ +use master + +go +create database ATM +on +( + name='ATM', + filename='D:\DATA\ATM.mdf', + size=5MB, + maxsize=50MB, + filegrowth=15% +) +log on +( +name='ATM_log', + filename='D:\DATA\ATM_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=15% +) + +go +use ATM + +go + +create table userInfo +( + customerID int primary key identity(1,1), + customerName nvarchar(10) not null, + PID char(18) check(len(PID) in(18,15)) unique(PID) not null, + telephone varchar(13) check(len(telephone)=13 and telephone like '____-________') not null, + address nvarchar(200) +) +go +use ATM + +go + +create table cardInfo +( + cardID varchar(20) primary key check(substring(cardID,1,9)='1010 3576' and len(cardID)=18) not null, + curType nvarchar(10) default('人民币') not null, + savingType varchar(10) check(savingType in('活期','定活两期','定期')), + openDate date default(getdate()) not null, + balance money not null, + pass int check(len(pass)=6) default(888888) not null, + IsReportLoss nvarchar(2) check(IsReportLoss='是' and IsReportLoss='否') default('否'), + customerID int constraint FK_cardInfo_custromerID foreign key references userInfo(customerID) +) +go +use ATM + +go + +create table transInfo +( + transId int primary key identity(1,1), + transDate date default(getdate()), + cardID varchar(20) references cardInfo(cardID) not null, + transType nvarchar(2) check(transType='存入' or transType='支取') not null, + transMoney money check(transMoney>0) not null, + remark text +) + + + +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +-- 开户金额:1000 活期 卡号:1010 3576 1234 567 +insert into userInfo values('孙悟空','123456789012345','0716-78998783','北京海滨') +insert into cardInfo(balance,savingType,cardID) values(1000,'活期','1010 3576 1234 567') + +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 117 +insert into userInfo values('沙和尚','421345678912345678','0478-44223333','四川成都') +insert into cardInfo(balance,savingType,cardID) values(1,'定期','1010 3576 1212 117') + +--唐僧开户,身份证:321245678912345678,电话:0478-44443333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 113 +insert into userInfo values('唐僧','321245678912345678','0478-44443333','陕西西安') +insert into cardInfo(balance,savingType,cardID) values(1,'定期','1010 3576 1212 113') + +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” + +update cardInfo set pass='611234' where cardID='1010 3576 1212 113' + +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 + +insert into transInfo(cardID,transType,transMoney) values('1010 3576 1234 567','支取',200) +update cardInfo set balance -= 200 where cardID = 1 + +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) +insert into transInfo(cardID,transType,transMoney) values('1010 3576 1212 117','存入',300) +update cardInfo set balance +=300 where cardID = 2 +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” +update cardInfo set IsReportLoss='是' where cardID = 3 +--5. 查询出最近10天开户的银行卡的信息 +select * from cardInfo where DateDiff(dd,openDate,getdate())<=10 +--6. 查询交易金额最大的银行卡信息,子查询实现 +select max(transMoney) from transInfo +--7. 再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) +-- 显示效果: +-- 总交易金额:1400.00 +-- 支取交易金额:200.00 +-- 存入交易金额:1200.00 +select sum(transMoney) from transInfo +select transType from transInfo where transId=1 +select transType from transInfo where transId=2 + +select*from userInfo +select*from cardInfo +select*from transInfo \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6414c5149c648afd2d2ecbc66af5b097a5df1ddc --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" @@ -0,0 +1,95 @@ +use master +go +create database bank1 +on +( name=' bank1', + filename='D:\bank\ bank1.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=15% + +) + log on +( name=' bank1_log', + filename='D:\bank\bank1_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=15% + +) +go +use bank1 +go +create table userInfo +( customerID int primary key identity(1,1), + customerName nvarchar(5) not null, + PID varchar(18) unique check(len(PID)=18 or len(PID)=15), + telephone char(13) check(telephone like '____-________' or len(telephone)=13), + address1 nvarchar(200) +) + +create table cardInfo +( cardID char(20) not null primary key check(substring(cardID,1,9)='1010 3576' and len(cardID)=18), + curType nvarchar(10) not null default('RMB'), + savingType nvarchar(4), + openDate datetime not null default(getdate()), + balance bigint not null check(balance>1), + pass int not null default('888888') check(len(pass)=6), + IsReportLoss nchar(1) not null default('否'), + customerID int references userInfo(customerID) not null + +) +create table transInfo +( transId int primary key identity(1,1), + transDate datetime not null default(getdate()), + cardID char(20) references cardInfo(cardID) not null, + transType nchar(2) not null check(transType='存入'or transType='支取' ), + transMoney bigint not null check(transMoney>0), + remark text +) + +select*from userInfo +select*from cardInfo +select*from transInfo + + insert into userInfo values('孙悟空',123456789012345,'0716-78989783','北京海淀 '), + ('沙和尚',421345678912345678,'478-044223333','湖南'), + ('唐僧',321245678912345678,'0478-44443333','武汉') + + insert into cardInfo values( '1010 3576 1234 567', default ,'活期',default,1000,default,'是',4), + ( '1010 3576 1212 117', default ,'活期',default,3,default,'是',5), + ( '1010 3576 1212 113', default ,'活期',default,5,default,'是',6) + +-- 第二阶段:增、删、改、查 +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass=611234 where customerID=4 + +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 + select*from userInfo where customerName='孙悟空' + select cardID from cardInfo where customerID in ( select customerID from userInfo where customerName='孙悟空') + +--插入交易金额和修改账上余额: + insert into transInfo values(default,'1010 3576 1234 567','支取',200,'在今天花了200元') + update cardInfo set balance=balance-200 where customerID in(select customerID from userInfo where customerName='孙悟空') + + +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) + select*from userInfo where customerName='沙和尚' + select cardID from cardInfo where customerID in ( select customerID from userInfo where customerName='沙和尚') + + + insert into transInfo values(default,'1010 3576 1212 117','存入',300,'在今天存了300元') + update cardInfo set balance=balance+300 where customerID in(select customerID from userInfo where customerName='沙和尚') + +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” + update cardInfo set IsReportLoss='是' where customerID=( select customerID from userInfo where customerName='唐僧') +--5. 查询出最近10天开户的银行卡的信息 +select*from cardInfo where openDate between '2021-03-18'and '2021-03-20' + +--6. 查询交易金额最大的银行卡信息,子查询实现 +select*from transInfo where transMoney=(select max(transMoney) from transInfo ) +--7. 再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) +-- 显示效果: +-- 总交易金额:1400.00 +select sum(transMoney) as 总交易金额 from transInfo diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6cf3adb4e4a2335733e35c307c4f629b2c4015d2 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" @@ -0,0 +1,101 @@ +create database ATM +on +( + name='ATM', + filename='D:\bank\ATM.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +log on +( + name='ATM_log', + filename='D:\bank\ATM_log.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +go + +use ATM +go +create table userInfo +( + customerID int primary key identity(1,1), + customerName nvarchar(20) not null, + PID varchar(18) check(len(PID)>=15 or PID >=18) unique not null, + telephone varchar(20) check(len(telephone)=13 and telephone like '____-________') not null, + address nvarchar(50) +) +create table cardInfo +( + cardID nvarchar(30) primary key check(len(cardID)=18 and cardID like '1010 3576 ____ ___'), + curType nvarchar(10) default('RMB'), + openDate datetime default(getdate()), + balance money not null check(balance>=1), + pass varchar(6) not null check(len(pass)=6) default('888888'), + IsReportLoss char(2) not null default('否'), + customerID int foreign key references userInfo(customerID) +) +drop table cardInfo +alter table cardInfo add savingType varchar(4) check (savingType in ('活期','定活两便','定期')) +create table transInfo +( + transId int identity(1,1) primary key, + transDate date not null default(getdate()), + cardID nvarchar(30) not null foreign key references cardInfo(cardID), + transType varchar(4) check(transType='存入' or transType='支取'), + transMoney money not null check(transMoney>0), + remark ntext +) +drop table transInfo +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +-- 开户金额:1000 活期 -卡号:1010 3576 1234 567 + +--银行开户表 +insert into userInfo(customerName,PID,telephone,address) +select '孙悟空',123456789012345,'0716-78989783','北京海淀'union +select '沙和尚',421345678912345678,'0478-44223333' ,null union +select '唐僧', 321245678912345678,'0478-44443333',null +select * from userInfo +--银行卡信息表 +select * from cardInfo + +insert into cardInfo(cardID,savingType,balance,customerID) +select '1010 3576 1234 567','活期',1000,1 union +select '1010 3576 1212 117','定期',1,2 union +select '1010 3576 1212 113','定期',1,3 + +select * from transInfo + +--1.将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass ='611234' where customerID='1' +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 +--更新银行卡信息表 +select * from cardInfo +insert into transInfo(cardID,transType ,transMoney) +select '1010 3576 1234 567','支取',200 +update cardInfo set balance=balance-200 where cardID='1010 3576 1234 567' + +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) +select * from cardInfo +insert into transInfo (cardID,transType,transMoney) +select '1010 3576 1212 117','存入',300 +update cardInfo set balance=balance+300 where cardID='1010 3576 1212 113' + + +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” +update cardInfo set IsReportLoss='是' where customerID='3' +--5. 查询出最近10天开户的银行卡的信息 +select * from cardInfo where openDate>='2021-03-09' or openDate>= '2021-03-19' +--6. 查询交易金额最大的银行卡信息,子查询实现 +select max(transMoney) from transInfo +select*from transInfo where transMoney in(select max(transMoney) from transInfo) +--7. 再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示 +select sum(transMoney) from transInfo +select sum(transMoney) from transInfo where transType='存入' +select sum(transMoney) from transInfo where transType='支取' + + + diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..94d87442d1682efcaa2b87de225d6e23b25cf911 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" @@ -0,0 +1,91 @@ +use master +go +create database bank +on +( +name='bank', +filename='D:\ljcbank\bank.mdf', +size=5, +maxsize=20, +filegrowth=15% +) +log on +( +name='bank_log', +filename='D:\ljcbank\bank_log.ldf', +size=5, +maxsize=20, +filegrowth=15% +) +go +use bank +go +create table userInfo +( +customerID int primary key identity(1,1), +customerName text not null, +PID int check(PID>=15 or PID<=18) unique not null, +telephone char(13) not null check(len(telephone)=13 and telephone like'____-________'), +address text +) +create table cardInfo +( +cardID int primary key not null check(cardID like'1010 3576 ____ ___'), +curType monEY not null default('RMB'), +savingType NVARCHAR(20) check(savingType='定活两便' or savingType='定期' or savingType='活期'), +openDate DATETIME not null default (getdate()), +balance int not null check(balance>=1), +pass int not null check(len(pass)=6) default('888888'), +IsReportLoss nchar(1) not null check(IsReportloss='是' or IsReportloss='否') default('否'), +customerID int not null references userInfo(customerID) +) +create table transInfo +( +transId int primary key identity(1,1), +transDate datetime not null default(getdate()), +cardID nvarchar(20), +transType nchar(2) not null check(transType='存入' or transType='支取'), +transMoney int not null check(transMoney>0), +remark text +) +insert into userInfo +select '孙悟空','123456789012345','0716-78989783','北京海淀' union +select '沙和尚','421345678912345678','0478-44223333',''union +select '唐僧','321245678912345678','0478-44443333','' + +select * from userInfo +insert into cardInfo(cardID,savingType,balance,customerID) +select '1010 3576 1234 567','活期','1000','1' union +select '1010 3576 1212 117','定期','1','2' union +select '1010 3576 1212 113','定期','1','3' +select * from cardInfo + +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” + +update cardInfo set pass=611234 where savingType='活期' + +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 + +select customerID from userInfo where customerName='孙悟空' +select cardID from cardInfo where customerID=2 +insert into transInfo(transDate,cardID,transType,transMoney) +SELECT getdate(),'1010 3576 1234 567','存入','200' +update transInfo set transMoney=1 where cardID ='1010 3576 1234 567' +select * from transInfo + +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) + +select customerID from userInfo where customerName='沙和尚' +select cardID from cardInfo where customerID=1 +insert into transInfo(transDate,cardID,transType,transMoney) +select getdate(),'1010 3576 1212 117','存入','200' +update transInfo set transMoney=1 where cardID='1010 3576 1212 117' + +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” + +update cardInfo set IsReportLoss='是' where cardID='1010 3576 1212 113' + +--5. 查询出最近10天开户的银行卡的信息 + +select * from cardInfo where DateDiff(dd,openDate,getdate())<=10 diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9bc295b91f9faf713baf63fa2b90476636a4185e --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" @@ -0,0 +1,88 @@ +use master +go +create database bank +on +( + name ='bank', + filename='D:\bank.mdf', + size=5, + maxsize=1500, + filegrowth=15% +) +log on +( + name ='bank_log', + filename='D:\bank_log.ldf', + size=5, + maxsize=1500, + filegrowth=15% +) +go +use bank +go +create table userInfo +( + customerID int identity(1,1) primary key, + customerName nvarchar(10) not null, + PID char(18) unique check(len(PID) = 15 or len(PID) = 18), + telephone char(13) check(len(telephone)=13 or (telephone like '____-________')), + address nvarchar(80) +) +create table cardInfo +( + cardID nvarchar(18) primary key check(len(cardID)=18 and cardID like '1010 3576 %'), + curType CHAR(3) default('RMB') not null, + savingType nvarchar(4) check(savingType in('活期','定活两便','定期' )), + openDate datetime not null default(getdate()), + balance money not null , + pass int check(len(pass)=6) default(888888), + IsReportLoss varchar(2) not null check(IsReportLoss in('是','否')) default('否'), + customerID int references userInfo(customerID) +) +create table transInfo +( + transId int primary key identity, + transDate datetime not null default(getdate()), + cardID nvarchar(18) not null references cardInfo(cardID), + transType nchar(2) not null check(transType in ('存入','支出')), + transMoney int not null check(transMoney>0), + remark text +) +insert into userInfo +(customerName,PID,telephone,address) +values +('孙悟空','123456789012345','0716-78989783','北京海淀'), +('沙和尚','421345678912345678','0478-44223333',null), +('唐僧','321245678912345678','0478-44443333',null) +insert into cardInfo +(balance,savingType,cardID,customerID) +values +(1000,'活期','1010 3576 1234 567',1), +(1,'定期','1010 3576 1212 117',2), +(1,'定期','1010 3576 1212 113',3) +select *from userInfo +select *from transInfo + +update cardInfo set pass ='611234' where customerID='1' + + +select * from cardInfo +insert into transInfo(cardID,transType ,transMoney) +select '1010 3576 1234 567','支出',200 +update cardInfo set balance=balance-200 where cardID='1010 3576 1234 567' + +select * from cardInfo +insert into transInfo (cardID,transType,transMoney) +select '1010 3576 1212 117','存入',300 +update cardInfo set balance=balance+300 where cardID='1010 3576 1212 113' + +update cardInfo set IsReportLoss='是' where customerID='3' + +select * from cardInfo where openDate>='2021-03-10' or openDate>= '2021-03-20' + +select max(transMoney) from transInfo +select*from transInfo where transMoney in(select max(transMoney) from transInfo) + +select sum(transMoney) from transInfo +select sum(transMoney) from transInfo where transType='存入' +select sum(transMoney) from transInfo where transType='支出' diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\2521/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\2521/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9e8a7911d0e28fe6873658559decf54c9989f673 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\2521/SQLQuery1.sql" @@ -0,0 +1,56 @@ +use master +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 userInfo +( +customerID int primary key identity, +custerName text not null, +PID int check(PID=18 and PID=15) unique, +telephone char(13) not null check(len(telephone)=13 and telephone like'____-________'), +address text +) +create table cardInfo +( +cardID varchar(20) not null check(len(cardID)=20 and cardID like'____ ____') primary key, +curType money not null default('RMB') , +savingType nvarchar(3) check(savingType='活期' and savingType='定期'), +openDate char(15) default('当前日期') not null, +balance int check(balance>=1) not null, +pass int check(pass=6) default('888888') not null, +IsReportLoss nvarchar check(IsReportLoss='是' and IsReportLoss='否') default('否') not null, +customerID int foreign key references userInfo(customerID) +) +create table transInfo +( +tranndsId int primary key identity(1,1), +transDate char(15) default('当前日期') not null, +cardId varchar(20) not null foreign key references cardInfo(cardID), +transType nvarchar default('存入') not null, +transMonkey int check(transMonkey>0), +remark nvarchar check(remark='输入') +) +insert into userInfo values('孙悟空','123456789012345','0716-78989783','北京海淀') +insert into cardInfo(customerID,balance,cardID) values('001','活期','1010 3576 1234 567') +insert into userInfo values('沙和尚','421345678912345678','0478-44223333') +insert into cardInfo(customerID,balance,cardID) values('002','定期','1010 3576 1212 117') +insert into userInfo values('唐僧','321245678912345678','0478-44443333') +insert into cardInfo(customerID,balance,cardID) values('003','定期','1010 3576 1212 113') + diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery5.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8dc82f936043ab7719ba8cb13a4ce7b67fcaed7c --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery5.sql" @@ -0,0 +1,101 @@ +use master +go + +create database ATM +on +( + name='ATM', + filename='D:\bank\ATM.mdf', + size=5mb, + maxsize=50mb, + filegrowth=15% +) +log on +( + name='ATM_log', + filename='D:\bank\ATM_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=15% +) +go +use ATM +go + +create table userInfo +( + customerID int identity(1,1) primary key, + customerName nvarchar(10) not null, + PID varchar(18) check(len(PID)=18 or len(PID)=15)not null, + telephone char(13) not null check(len(telephone)=13 and telephone like '____-________'), + address nvarchar(20) +) +create table cardInfo +( + cardID varchar(20) primary key check(cardID like '1010 3576 ____ ___') , + curType nvarchar(10) not null default('RMB'), + savingType nvarchar(4) check(savingType='活期' or savingType='定活两便'or savingType='定期' ), + openDate datetime default(getdate()), + balance decimal(38,2) not null check(balance>=1), + pass char(6) not null default(888888), + IsReportLoss varchar(2) default('否') check(IsReportLoss='是' or IsReportLoss='否' ), + customerID int foreign key references userInfo(customerID) +) +create table transInfo +( + transID int primary key identity(1,1), + transDate datetime not null default(getdate()), + cardID varchar(20) foreign key references cardInfo(cardID) not null, + transType nvarchar(2) check(transType='存入' or transType='支取') not null, + transMoney decimal(38,2) not null check(transMoney>0), + remark text +) + +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +-- 开户金额:1000 活期 卡号:1010 3576 1234 567 +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 117 + +--唐僧开户,身份证:321245678912345678,电话:0478-44443333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 113 + + insert into userInfo + select '孙悟空','123456789012345','0716-78989783','北京海淀' union + select'沙和尚','421345678912345678','0478-44223333',null union + select'唐僧','321245678912345678','0478-44443333',null + + insert into cardInfo values + ('1010 3576 1234 567',default,'活期',default,1000,default,default,1), + ('1010 3576 1212 117',default,'定期',default,1,default,default,2), + ('1010 3576 1212 113',default,'定期',default,1,default,default,3) + + update cardInfo set pass='611234' where cardID='1010 3576 1234 567' + +------ 2.用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录, +------然后在孙悟空账上的余额减200 +------注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号, +----再根据银行卡号来插入交易记录和修改账上余额 +insert into transInfo values +(default,'1010 3576 1234 567','支取',200,null) + +update cardInfo set balance=balance-200 where cardID='1010 3576 1234 567' + +insert into transInfo values +(default,'1010 3576 1212 117','存入',300,null) + +update cardInfo set balance=balance+300 where cardID='1010 3576 1212 117' + +update cardInfo set IsReportLoss='是' where cardID='1010 3576 1212 113' + +select * from cardInfo where DateDiff(dd,openDate,getdate())<=10 + +select max(transMoney) 交易金额最大 from transInfo + +select sum(transMoney) 总交易金额 from transInfo + +select sum(transMoney) 支取交易金额 from transInfo where transType='支取' +select sum(transMoney) 存入交易金额 from transInfo where transType='存入' + + + + \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..49d17be9b6ee00cbaa6010dc4404fbc402cbaae4 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" @@ -0,0 +1,101 @@ +use master + + +create database bank + +on + +( + + name='bank', + + filename='D:\bank.mdf', + + size=6MB, + + maxsize=100MB, + + filegrowth=15% + +) + + +log on + +( + + name='blank_log', + + filename='D:\bank_log.ldf', + + size=6MB, + + maxsize=100MB, + + filegrowth=15% + +) + +go + +use bank + +go +--用户信息表 +create table userInfo +( +customerID int not null primary key identity(1,1), +customerName nvarchar(20) default('杨梦林') unique not null, +PID varchar(30) unique check(len(PID)=18 or len (PID)=15) , +telephone nvarchar(13) unique check(len(telephone)=13 and telephone like '___-_________'), +address text +) +--插入数据 +insert into userInfo(customerName,PID,telephone,address) values('杨梦林','123456789123456','071-678989783','北京后花园') +insert into userInfo (customerName,PID,telephone) values ('杜海彪','421345678912345678','047-123456789') +insert into userInfo (customerName,PID,telephone) values ('吴皇','321245678912345678','307-444443333') +--银行可信息表 +create table cardInfo +( +cardID varchar(18) not null primary key check(len(cardID)=18 and cardID like '1010 3576 ____ ___'), +curType varchar(5) not null default('RMB'), +savingType nvarchar(20) , +openDate nvarchar(50) not null default(Getdate()), +balance int not null check(balance>=1) , +pass varchar(16) not null check(len(pass)=6) default('888888') , +IsReportLoss nvarchar(2) not null check(IsReportLoss = '是 ' or IsReportLoss ='否') default('否'), +customerID int not null foreign key references userInfo(customerID) +) +select * from userInfo +drop table cardInfo + +--插入数据 +insert into cardInfo(cardID,savingType,balance,customerID)values('1010 3576 1234 567','活期',1000,1) +insert into cardInfo(cardID,savingType,balance,customerID)values('1010 3576 1212 117','定期',1,2) +insert into cardInfo(cardID,savingType,balance,customerID)values('1010 3576 1212 113','定期',1,6) + +--交易信息表 +create table transInfo +( +transId int not null primary key identity(1,1), +transDate nvarchar(50)not null default(Getdate()), +cardID varchar(18) not null foreign key references cardInfo(cardID), +transType nvarchar(4) not null default('支取'), +ransMoney nvarchar(10) not null check(ransMoney>10), +remark nvarchar(20) check(remark='可选输入'or remark = '其它说明') +) +drop table transInfo +--更新数据(梦林) +update cardInfo set pass ='611234' where customerID='1' + +-- 存钱不会做 + +--吴皇卡丢了 + +update cardInfo set IsReportLoss='是' where customerID='6' + +--最大金额也不会 + + + + diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c31e4e9a0994c376d3e910716d31465afcbbe0d8 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" @@ -0,0 +1,62 @@ +use master +go + +create database atm1 +on +( + name = 'atm', + filename = 'D:\bank\atm.mdf', + filegrowth = 15% +) + +log on +( + name = 'atm_log', + filename = 'D:\bank\atm_log.ldf' +) + +use atm +go + +create table userInfo +( + customerID int identity primary key, + customerName nvarchar(10) not null, + PID varchar(18) not null unique check(len(PID)=18 or len(PID)=15), + telephone varchar(20) not null check(len(telephone)=13 or telephone like '____-________'), + address text +) + +create table cardInfo +( + cardID varchar(20) not null primary key check(cardID like '1010 3576 ____ ____'), + curType varchar(10) not null default('RMB'), + savingType nvarchar(4) check(savingType in ('活期','定活两便','定期')), + openDate datetime not null default(getdate()), + balance varchar(13) not null check (balance>1), + pass int not null check (len(pass)=6) default(888888), + IsReportLoss nvarchar(1) not null default('否') check (IsReportLoss in('是','否')), + customerID int references userInfo(customerID) not null +) + +create table transInfo +( + transId int identity primary key, + transDate datetime not null default(getdate()), + cardID varchar(20) not null references cardInfo(cardID), + transType nvarchar(2) check (transType in ('存入','支取')), + transMoney varchar(15) not null check(transMoney>=1), + remark text +) + +insert userInfo values +('孙悟空','123456789012345','0716-78989783','北京海淀'), +('沙和尚','421345678912345678','0478-44223333',''), +('唐僧','321245678912345678','0478-44443333','') + +select * from userInfo + +insert into cardInfo values +('1010 3576 1234 5671','RMB','活期','','1000','888888','否',1), +('1010 3576 1212 1127','RMB','定期','','2','888888','否',2), +('1010 3576 1212 1137','RMB','定期','','2','888888','否',3) diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f179fed7fa7882fcdd06cc9b65ba53993c0e2a6f --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" @@ -0,0 +1,82 @@ +create database ATM +on +( +name='ATM', +filename='D:\bank\ATM.mdf', +size= 50mb, +maxsize=100mb, +filegrowth=15% +) +log on( +name='ATM_log', +filename='D:\bank\ATM_log.ldf', +size= 50mb, +maxsize=100mb, +filegrowth=15% +) +create table userInfo( +customerID int primary key identity(1,1), +customerName nvarchar(10) not null, +PID int not null check(len(PID)=15 or len(PID)=18 ), +telephone varchar(20) check(len(telephone)=13 or telephone like '____-________'), +address nvarchar(20), +) +create table cardInfo( +--必填,主健,银行的卡号规则和电话号码一样, +--一般前8位代表特殊含义,如某总行某支行等。假定该行要求其营业厅的卡号格式为: +--1010 3576 xxxx xxx开始,每4位号码后有空格,卡号一般是随机产生。 +cardID int primary key not null check(cardID like '1010 3576 ____ ___ '), +curType varchar(10) not null default ('RMB'), +savingType nvarchar(10) check(savingType='活期'or savingType='定活两便'or savingType='定期'), +openDate datetime default('current datetime'), +Balance int check(Balance>=1), +Pass int not null check(len(Pass)=6) default(888888), +IsReportLoss nvarchar(1) not null default('否') check(IsReportLoss='是' or IsReportLoss='否'), +customerID int not null foreign key references userInfo(customerID) +) +create table transInfo( +transId int primary key identity(1,1), +transDate datetime default('System datetime'), +cardID int not null foreign key references cardInfo(cardID) , +transType nvarchar(10) not null check(transType='存入'or transType='支取'), +transMoney int not null check(transMoney>0), +Remark text +) + +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +-- 开户金额:1000 活期 卡号:1010 3576 1234 567 + +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 117 + +--唐僧开户,身份证:321245678912345678,电话:0478-44443333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 113 + + +insert into userInfo(customerName,PID,telephone,address) values ('孙悟空','123456789012345','0716-78989783','北京海淀'), +('沙和尚','421345678912345678','0478-44223333',''), +('唐僧','321245678912345678','0478-44443333','') +insert into cardInfo(cardID,balance) values ('1010 3576 1234 567',1000), +('1010 3576 1212 117',1), +('1010 3576 1212 113',1) +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass='611234' where cardID='1010 3576 1234 567' +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +select (balance-200) from cardInfo where cardID='1010 3576 1234 567' +update cardInfo set balance=(select (balance-200) from cardInfo where cardID='1010 3576 1234 567') where cardID='1010 3576 1234 567' +insert into transInfo(cardID,transType,transMoney) values('1010 3576 1234 567','支出',200) +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 +select (balance+300) from cardInfo where cardID='1010 3576 1212 117' +update cardInfo set balance=(select (balance+300) from cardInfo where cardID='1010 3576 1212 117') where cardID='1010 3576 1212 117' +insert into transInfo(cardID,transType,transMoney) values('1010 3576 1212 117','存入',300) + +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” +update cardInfo set IsReportLoss='是' where cardID='1010 3576 1212 113' +--5. 查询出最近10天开户的银行卡的信息 +select * from cardInfo where DATEDIFF(day,openDate,getdate())<10 +--6. 查询交易金额最大的银行卡信息,子查询实现 +select * from userInfo +--7. 再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) +select * from cardInfo +--总交易金额:1400.00 +select * from transInfo \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..45d512d3ad71c8ef62fb8f6e2525848b50990cfb --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/SQLQuery1.sql" @@ -0,0 +1,107 @@ +use master +go + +create database ATM +on primary +( +name='ATM', +filename='D:\SQL作业,mdf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +log on +( +name='ATM_log', +filename='D:\SQL作业_log.ldf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +go + +use ATM + +create table userInfo +( --建立用户信息表 +customerID int identity primary key, +customerName varchar(10) not null, +PID varchar(18) check(len(PID)=18 or len(PID)=15) unique, +telephone varchar(13) not null check(len(telephone)=13 or telephone like'____-________'), +address varchar(30) +) +go + +create table cardInfo --建立银行卡信息表 +( +cardID varchar(18) primary key check(len(cardID)=18 or cardID like'1010 3576 ____ ___'), +curType varchar(3) check(curType='RMB'), +savingType varchar(8) check(savingType='活期'or savingType='定活两便' or savingType='定期'), +openDate date default getdate(), +balance varchar(25) not null check(balance>=1), +pass int not null check(len(pass)=6) default('888888'), +IsReportLoss varchar(2) check(IsReportLoss='是' or IsReportLoss='否') default('否'), +customerID int not null foreign key(customerID) references userInfo(customerID) +) +go + +create table transInfo --建立交易信息表 +( +transId int identity primary key, +transDate date default getdate() not null, +cardID varchar(18) foreign key(cardID) references cardInfo(cardID), +transType varchar(4) check(transType='存入' or transType='支取') not null, +transMoney int check(transMoney>0) not null, +remark varchar(50) +) +go + +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址北京海淀 开户金额1000 活期 卡号1010 3576 1234 567 +select * from userInfo +select * from cardInfo + +insert into userInfo(customerName,PID,telephone,address) values ('孙悟空','123456789012345','0716-78989783','北京海淀') +insert into cardInfo(balance,savingType,cardID,customerID) values ('1000','活期','1010 3576 1234 567',1) + +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333,开户金额1 定期 卡号:1010 3576 1212 117 + +insert into userInfo(customerName,PID,telephone) values ('沙和尚','421345678912345678','0478-44223333') +insert into cardInfo(balance,savingType,cardID,customerID) values ('1','定期','1010 3576 1212 117',2) + +--唐僧开户,身份证:321245678912345678,电话:0478-44443333,开户金额1 定期 卡号1010 3576 1212 113 + +insert into userInfo(customerName,PID,telephone) values ('唐僧','321245678912345678','0478-44443333') +insert into cardInfo(balance,savingType,cardID,customerID) values ('1','定期','1010 3576 1212 113',3) + +--1.将用户“孙悟空”开卡时的初始密码更改为“611234” + +update cardInfo set pass=611234 where customerID=1 +select * from cardInfo + +--2.用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 + +select * from transInfo +insert into transInfo(cardID,transType,transMoney) values('1010 3576 1234 567','支取','200') +update cardInfo set balance=balance-200 where customerID=1 + +--3.用同上题一样的方法实现沙和尚存钱的操作(存300) + +insert into transInfo(cardID,transType,transMoney) values('1010 3576 1212 117','存入','300') +update cardInfo set balance=balance+300 where customerID=2 + +--4.唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” + +update cardInfo set IsReportLoss='是' where customerID=3 + +--5.查询出最近10天开户的银行卡的信息 + +select count(*) from cardInfo where date_sub(curdate(),interval 10 day)<=date(openDate) + +--6.查询交易金额最大的银行卡信息 + +select max(transMoney)最大交易金额 from transInfo + +--7.再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) +--显示效果: +--总交易金额:1400.00 + diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/banks.sql.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/banks.sql.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c1941240cac6968b9e416bdd8c9b77e6d76649c9 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/banks.sql.sql" @@ -0,0 +1,92 @@ +use master +go +create database bank +on +( +name='bank', +filename='D:\test\bank.mdf', +size=5, +maxsize=50, +filegrowth=15% +) +log on +( +name='bank_log', +filename='D:\test\bank_log.ldf', +size=5, +maxsize=50, +filegrowth=10% +) +use bank +go +create table userInfo +( +customerID int primary key identity(1,1), +customerName nvarchar(10) not null, +PID nvarchar(20) not null unique check(len(PID)in (15,18)), +telephone nvarchar(15) not null check(len(telephone)=13 and telephone like '____-________'), +address nvarchar(50) +) +create table cardInfo +( +cardID nvarchar(20) not null check(substring(cardID,1,9)='1010 3576' and len(cardID)=18) primary key, +curType nvarchar(10) not null default ('RMB'), +savingType nvarchar(10) check(savingType in ('活期','定活两便','定期')), +openDate datetime default GETDATE() NOT NULL, +balance money check (balance>=1), +pass char (6) not null default (888888), +IsReportLoss nvarchar(1) not null default ('否') check (IsReportLoss in('是','否')), +customerID int not null references userInfo (customerID) +) + create table transInfo + ( + transId int primary key identity(1,1), + transDate datetime default GETDATE() not null, + cardID nvarchar(20) not null references cardInfo(cardID), + transType nvarchar(4) not null check(transType='存入' or transType='支取'), + transMoney money not null check (transMoney>0), + remark nvarchar(50) + ) +-- C. 根据下列条件插入和更新测试数据 +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +insert into userInfo(customerName,PID,telephone,address) values('孙悟空','123456789012345','0716-78989783','北京海淀') +-- 开户金额:1000 活期 卡号:1010 3576 1234 567 +insert into cardInfo (customerID,balance,savingType,cardID) values('1','1000','活期','1010 3576 1234 567') +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333, +insert into userInfo (customerName,PID,telephone) values ('沙和尚','421345678912345678','0478-44223333') +-- 开户金额: 1 定期 卡号:1010 3576 1212 117 +insert into cardInfo (customerID, balance,savingType,cardID) values ('2','1','定期','1010 3576 1212 117') +--唐僧开户,身份证:321245678912345678,电话:0478-44443333, +insert into userInfo (customerName,PID,telephone) values ('唐僧','321245678912345678','0478-44443333') +-- 开户金额: 1 定期 卡号:1010 3576 1212 113 +insert into cardInfo (customerID, balance,savingType,cardID) values ('3','1','定期','1010 3576 1212 113') + +select * from userInfo +select * from cardInfo +select * from transInfo +--第二阶段:增、删、改、查 + +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass='611234' where cardID='1010 3576 1234 567' + +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 +insert into transInfo(cardID,transType,transMoney) values('1010 3576 1234 567','支取',200.00) +update cardInfo set balance=balance-200 where customerID=1 +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) +insert into transInfo(cardID,transType,transMoney) values('1010 3576 1212 117','存入',300.00) +update cardInfo set balance=balance+300 where customerID=2 +update cardInfo set IsReportLoss='是' where customerID=3 +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” +update cardInfo set IsReportLoss='是' where cardID='1010 3576 1212 113' +--5. 查询出最近10天开户的银行卡的信息 + +--6. 查询交易金额最大的银行卡信息,子查询实现 +select max(balance)最大金额 from cardInfo +select sum(transMoney)总支出金额 from transInfo where transType='支取' +--7. 再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) +select * from transInfo +-- 显示效果: +-- 总交易金额:1400.00 +-- 支取交易金额:200.00 +-- 存入交易金额:1200.00 diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/MDXQuery1.mdx" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/MDXQuery1.mdx" new file mode 100644 index 0000000000000000000000000000000000000000..3516bc92efcb20ab4b87ef2a0450d4fa7af5e01c --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/MDXQuery1.mdx" @@ -0,0 +1,62 @@ +create database ATM +on +( + name='ATM', + filename='D:\bank\ATM.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +log on +( + name='ATM_log', + filename='D:\bank\ATM_log.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +go + +use ATM +go +create table userInfo +( + customerID int primary key identity(1,1), + customerName nvarchar(20) not null, + PID varchar(18) check(len(PID)>=15 or PID >=18) unique not null, + telephone varchar(20) check(len(telephone)=13 and telephone like '____-________') not null, + address nvarchar(50) +) +create table cardInfo +( + cardID nvarchar(30) primary key check(len(cardID)=18 and cardID like '1010 3576 ____ ___'), + curType nvarchar(10) default('RMB'), + openDate datetime default(getdate()), + balance money not null check(balance>=1), + pass varchar(6) not null check(len(pass)=6) default('888888'), + IsReportLoss char(2) not null default('否'), + customerID int foreign key references userInfo(customerID) +) +drop table cardInfo +alter table cardInfo add savingType varchar(4) check (savingType in ('活期','定活两便','定期')) +create table transInfo +( + transId int identity(1,1) primary key, + transDate date not null default(getdate()), + cardID nvarchar(30) not null foreign key references cardInfo(cardID), + transType varchar(4) check(transType='存入' or transType='支取'), + transMoney money not null check(transMoney>0), + remark ntext +) +drop table transInfo +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +-- 开户金额:1000 活期 -卡号:1010 3576 1234 567 + +--银行开户表 +insert into userInfo(customerName,PID,telephone,address) +select '孙悟空',123456789012345,'0716-78989783','北京海淀'union +select '沙和尚',421345678912345678,'0478-44223333' ,null union +select '唐僧', 321245678912345678,'0478-44443333',null +select * from userInfo +--银行卡信息表 +select * from cardInfo \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5ccc4464b32901193534a64f63acafb11dd8a9a1 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" @@ -0,0 +1,87 @@ +create database ATM +on +( + name='ATM', + filename='E:\bank\ATM.mdf', + size=5mb, + maxsize=50mb, + filegrowth=15% +) +log on +( + name='ATM_log', + filename='E:\bank\ATM_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=15% +) +use ATM +create table userInfo +( + customerID int identity(1,1) primary key, + customerName varchar(10) not null, + PID varchar(18) check(len (PID)=18 or len (PID)=15) unique, + telephone varchar(13) check(telephone like '____-________' and len (telephone)=13), + address nvarchar(50) +) +create table cardInfo +( + cardID varchar(20) not null primary key check(cardID like '1010 3576 ____ ___' and len(cardID)=18), + curType varchar(10) not null default('RMB'), + savingType varchar(4) check(savingType='活期' or savingType='定活两便' or savingType='定期'), + openDate datetime not null default(getdate()), + balance money not null check(balance>=1), + pass varchar(6) not null check(len (pass)=6) default('888888'), + IsReportLoss varchar(2) not null check(IsReportLoss='是' or IsReportLoss='否') default('否'), + customerID int foreign key references userInfo(customerID) +) +create table transInfo +( + transId int identity primary key, + teansDate datetime not null default(getdate()), + cardID varchar(20) not null foreign key references cardInfo(cardID), + transType varchar(4) not null check(transType='存入' or transType='支取'), + transMoeny money not null check(transMoeny>0), + remark ntext +) +insert into userInfo values('孙悟空开户',123456789012345,'0716-78989783','北京海淀') +insert into userInfo(customerName,PId,telephone) values('沙和尚开户',421345678912345678,'0478-44223333') +insert into userInfo(customerName,PId,telephone) values('唐僧开户',321245678912345678,'0478-44443333') +select * from userInfo +insert into cardInfo(balance,savingType,cardID,customerID) +values(1000,'活期','1010 3576 1234 567',1) +insert into cardInfo(balance,savingType,cardID,customerID) +values(1,'定期','1010 3576 1212 117',2) +insert into cardInfo(balance,savingType,cardID,customerID) +values(1,'定期','1010 3576 1212 113',3) +select * from cardInfo + +select * from transInfo + +update cardInfo set pass='611234' where customerID=1 + +insert into transInfo(cardID,transType,transMoeny) +values('1010 3576 1234 567','支取',200) +update cardInfo set balance=balance-200 where cardID='1010 3576 1234 567' + + +insert into transInfo(cardID,transType,transMoeny) +values('1010 3576 1212 117','存入',300) +update cardInfo set balance=balance+300 where cardID='1010 3576 1212 117' + +update cardInfo set IsReportLoss='是' where customerID=3 + + +select * from cardInfo where openDate>='2021-03-11' or openDate<='2021-03-21' + + +select max(transMoeny) from transInfo + + +select * from transInfo where transMoeny in(select max(transMoeny) from transInfo) + + + +select sum(transMoeny) from transInfo +select sum(transMoeny) from transInfo where transType='存入' +select sum(transMoeny) from transInfo where transType='支取' diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0660c54431eb3f4ed85a3ecf8c29014f08504381 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" @@ -0,0 +1,61 @@ +create database bank +on +( + name='bank', + size=5, + filename='D:\bank.mdf', + maxsize=100, + filegrowth=15% +) +log on +( + name='bank_log', + size=5, + filename='D:\bank_log.ldf', + maxsize=100, + filegrowth=15% +) +go +use bank +go +create table userInfo +( + customerID int identity(1,1) primary key, + customerName varchar(32) not null, + PID varchar(18) unique check(len(PID)=18 or len(PID)=15), + telephone char(13) check(telephone like '____-________' or len(telephone)=13), + address varchar(200) +) +create table cardInfo +( + cardID nvarchar(20) primary key check(substring(cardID,1,9)='1010 3576' and len(cardID)=18), + curType varchar(30) not null default('rmb'), + savingType nvarchar(10) check(savingType in('活期','定活两便','定期')), + openDate date not null default(getdate()), + balance money check(balance>=1) not null, + pass nvarchar(8) check(len(pass)=6) default('888888') not null, + IsReportLoss char(2) default('否') check(IsReportLoss='是' or IsReportLoss='否') not null, + customerID int foreign key references userInfo(customerID) not null +) +create table transInfo +( + transId int primary key identity , + transDate date default(getdate()) not null, + cardID nvarchar(20) foreign key references cardInfo(cardID)not null, + transType nvarchar(2) check(transType='存入' or transType='支取') not null, + transMoney money check(transMoney>0) not null, + remark text +) +insert into userInfo values ('孙悟空','123456789012345','0716-78989783','北京海淀'),('唐僧','321245678912345678','0478-44443333',' '),('沙和尚','421345678912345678','0478-44223333',' ') +insert into cardInfo(cardID,savingType,balance,customerID) values ('1010 3576 1234 567','活期',1000,1),('1010 3576 1212 117','定期',1,2),('1010 3576 1212 113','定期',1,3) +update cardInfo set pass='611234' where cardID='1010 3576 1234 567' +insert into transInfo(cardID,transType,transMoney) values('1010 3576 1234 567','支取',200) +update cardInfo set balance=balance-200 where cardID='1010 3576 1234 567' +insert into transInfo(cardID,transType,transMoney) values ('1010 3576 1212 117','存入',300) +update cardInfo set balance=balance+300 where cardID='1010 3576 1212 117' +update cardInfo set IsReportLoss='是' where cardID='1010 3576 1212 113' +select * from cardInfo where openDate>=dateadd(day,-10,getdate()) +select * from cardInfo where cardID=(select cardID from transInfo where transMoney=(select max(transMoney) from transInfo)) +select sum(transMoney)总交易金额 from transInfo +select sum(transMoney)支取交易金额 from transInfo where transType='存入' +select sum(transMoney)存入交易金额 from transInfo where transType='支取' diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2d79c497a95f39220a2a426d96a42f4b440318f1 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" @@ -0,0 +1,99 @@ +use master +go +create database ATM +on +( +name='bank', +size=5, +filename='D:\bank.mdf', +Maxsize=100, +filegrowth=1 +) +log on +( +name='bank_log', +size=5, +filename='D:\bank_log.ldf', +Maxsize=100, +filegrowth=1 +) +go +use ATM +go +create table userInfol +( +customerID int identity(1,1) primary key, +customerName nvarchar(5) not null, +PID varchar(18) not null check(len(PID)=15 or len(PID)=18) unique, +telephone varchar(20) not null check(len(telephone)=13 and telephone like'____-________'), +address text +) +go +create table cardInfo +( +cardID varchar(20) not null primary key check(substring(cardID,1,9)='1010 3576'), +curType varchar(20) not null default('RMB'), +savingType varchar(20), +openDate datetime default(getdate()) not null, +balance int not null check(balance>1 ), +pass varchar(20) not null check(len(pass)=6) default('888888'), +IsReportLoss varchar(4) not null check(IsReportLoss='是' or IsReportLoss='否') default('否'), +customerID int foreign key references userInfol(customerID) +) +go +create table transInfo +( +transId int primary key identity(1,1), +transDate datetime default(getdate()) not null, +cardID varchar(20) not null foreign key references cardInfo(cardID), +transType varchar(4) not null check(transType='存入' or transType ='取出'), +transMoney int not null check(transMoney>0), +remark nvarchar(20) +) +go +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +-- 开户金额:1000 活期 卡号:1010 3576 1234 567 +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333, + -- 开户金额: 1 定期 卡号:1010 3576 1212 117 + +--唐僧开户,身份证:321245678912345678,电话:0478-44443333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 113 + + +insert into userInfol(customerName,PID,telephone,address) +select'孙悟空','123456789012345','0716-78989783','北京海淀' +insert into userInfol(customerName,PID,telephone) +select'沙和尚','421345678912345678','0478-44223333'union +select'唐僧','321245678912345678','0478-44443333' + +select * from userInfol + +insert into cardInfo(customerID,savingType,cardID,balance) +select 1,'活期','1010 3576 1212 113',1000 +insert into cardInfo(customerID,savingType,cardID,balance) +select 2,'活期','1010 3576 1212 117',1000 union +select 3,'活期','1010 3576 1212 114',1000 +select * from cardInfo +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass=611234 where cardID='1010 3576 1212 113' +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 +update cardInfo set balance=balance-200 where cardID='1010 3576 1212 113' +insert into transInfo values(getdate(),'1010 3576 1212 113','取出',200,null) + + +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) +update cardInfo set balance=balance+300 where cardID='1010 3576 1212 117' +insert into transInfo values(getdate(),'1010 3576 1212 117','存入',300,null) +select * from cardInfo +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” +update cardInfo set IsReportLoss='是' where cardID='1010 3576 1212 114' + +--5. 查询出最近10天开户的银行卡的信息 +select * from cardInfo where DateDiff(dd,opendate,getdate())<=10 +--6. 查询交易金额最大的银行卡信息,子查询实现 +select max(transMoney)最大金额 from transInfo +--7. 再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) +select sum(transMoney)交易中和 from transInfo +-- 显示效果: +-- 总交易金额:1400.00 diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery7.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..41c6cbad853a8947e616291ff38c5f8244d6931c --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery7.sql" @@ -0,0 +1,67 @@ +use master +go +create database bank +on( + name='bank', + filename='C:\bank\bank.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on( + name='bank_log', + filename='C:\bank\bank_log.ldf', + size=5, + maxsize=100, + filegrowth=10% +) +go +use bank +go +create table userInfo +( + customerID int primary key identity (1,1), + customerName nvarchar(10) not null, + PID varchar(20) unique check(len(PID)=15 or len(PID)=18) not null, + telephone varchar(20) check(len(telephone)=13 and telephone like '____-________') not null, + address nvarchar(50), +) +create table cardInfo +( + --cardID varchar(20) primary key check(substring(cardID,1,10)='1010 3576 ' and len(cardID)=18 ) not null, + cardID varchar(20) primary key check(cardID like '1010 3576 ____ ___' ) not null, + curType varchar(4) default('RMB') not null, + savingType nvarchar(10) check(savingType in ('活期','定活两便','定期')), + openDate datetime default(getdate()) not null, + balance money check(balance>=1) not null, + pass varchar(6) default('888888') not null, + IsReportLoss nvarchar(1) default('否') check(IsReportLoss in ('是','否')) not null, + customerID int references userInfo(customerID) not null, + +) +create table transInfo +( + transId int primary key identity not null, + transDate datetime default(getdate() ) not null, + cardID varchar(20) references cardInfo(cardID ) not null, + transType nvarchar(2) check(transType in ('存入','支出')) not null, + transMoney money check(transMoney>0) not null, + remark text, +) +use bank +go +select * from userInfo +select * from cardInfo +select * from transInfo +insert into userInfo values('孙悟空','123456789012345','0716-78989783','北京海淀') +insert into userInfo(customerName,PID,telephone) values('沙和尚','421345678912345678','0478-44223333'),('唐僧','321245678912345678','0478-44443333') +insert into cardInfo(cardID,savingType,balance,customerID) values('1010 3576 1234 567','活期',1000.00,1),('1010 3576 1212 117','定期',1.00,2),('1010 3576 1212 113','定期',1.00,3) +update cardInfo set pass=611234 where customerID=1 +insert into transInfo(cardID,transType,transMoney) values('1010 3576 1234 567','支出',200.00) +update cardInfo set balance=balance-200 where customerID=1 +insert into transInfo(cardID,transType,transMoney) values('1010 3576 1212 117','存入',300.00) +update cardInfo set balance=balance+300 where customerID=2 +update cardInfo set IsReportLoss='是' where customerID=3 +select * from cardInfo where DATEDIFF(dd,openDate, getdate())<=10 +select max(balance)最大金额 from cardInfo +select sum(transMoney)总支出金额 from transInfo where transType='支出' \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ac9d6645560c249fbc21ebf7b61c842eb68c2b9e --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" @@ -0,0 +1,74 @@ +use master +go + +create database ATM +on +( + name='bank', + filename='D:\bank\bank.mdf', + size=10MB, + Maxsize=50mb, + filegrowth=15% +) +log on +( + name='bank_log', + filename='D:\bank\bank+log.ldf', + size=10MB, + Maxsize=50mb, + filegrowth=15% +) +go + +use ATM +go + +create table userInfo +( + customerID int primary key identity, + customerName nvarchar(5) not null, + PID varchar(18) check(len(PID)>=15) unique, + telephone varchar(13) check(len(telephone)=13 and telephone like '____ ________') not null, + address varchar(20) +) + +create table cardInfo +( + cardID varchar(20) primary key check(len(cardID)=18 and substring(cardID,1,9)='1010 3576'), + curType varchar(3) not null default('RMB'), + savingType nvarchar(10) check(savingType='活期' or savingType='定期' or savingType='定活两变'), + openDate datetime default(getdate()) not null, + balance money check(balance>=1) not null, + pass varchar(6) default('888888') not null, + IsReportLoss varchar(2) default('否') check(IsReportLoss='是' or IsReportLoss='否') not null, + customerID int foreign key references userInfo(customerID) +) +create table transInfo +( + transId int primary key identity, + transDate datetime not null default(getdate()), + cardID varchar(20) not null references cardInfo(cardID), + transType nvarchar(4) not null check(transType='存入' or transType='取出'), + transMoney money check(transMoney>0) not null, + remark nvarchar(50) +) + insert into userInfo values('孙悟空','123456789012345','0716 78989783','北京海淀'), + ('沙和尚','421345678912345678','0478 44223333',''), + ('唐僧','321245678912345678','0478 44443333','') + +insert into cardInfo (cardID,savingType,balance) values('1010 3576 1234 567','活期','1000'), +('1010 3576 1212 117','定期','1'), +('1010 3576 1212 113','定期','1') + +update cardInfo set pass=611234 where cardID='1010 3576 1234 567' + +update cardInfo set balance=balance-200 where cardID='1010 3576 1234 567' +insert into transInfo values(getdate(),'1010 3576 1234 567','取出','200',null) + +update cardInfo set balance=balance+300 where cardID='1010 3576 1212 117' +insert into transInfo values(getdate(),'1010 3576 1212 117','存入','300',null) + +update cardInfo set IsReportLoss='是' where cardID='1010 3576 1212 113' + + +select * from cardInfo where DateDiff(dd,openDate,getdate())<=10 \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery5.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1d70130a36b1ada9b2356c80a81be1ddf00e131e --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery5.sql" @@ -0,0 +1,93 @@ +create database ATM on( + name='ATM', + filename='D:\bank\ATM.mdf', + size=5MB, + maxsize=50MB, + filegrowth=15% +) +log on( + name='ATM_log', + filename='D:\bank\ATM.ldf', + size=5MB, + maxsize=50MB, + filegrowth=15% +) +go + +use ATM +go + +--用户信息表:userInfo : +create table userInfo( +--customerID 顾客编号 自动编号(标识列),从1开始,主键 + customerID int primary key identity(1,1), +--customerName 开户名 必填 + customerName varchar(10) not null, +--PID 身份证号 必填,只能是18位或15位,身份证号唯一约束 + PID varchar(18) not null unique check(len(PID)=18 or len(PID)=15), +--telephone 联系电话 必填,格式为xxxx-xxxxxxxx或手机号13位 + telephone char(13) not null check(telephone like '____-________' or len(telephone)=13), +--address 居住地址 可选输入 + address varchar(50) +) + +--银行卡信息表:cardInfo +create table cardInfo( +--cardID 卡号 必填,主健,银行的卡号规则和电话号码一样,一般前8位代表特殊含义,如某总行某支行等。 +--假定该行要求其营业厅的卡号格式为:1010 3576 xxxx xxx开始,每4位号码后有空格, + cardID char(20) not null primary key check(cardID like'1010 3576 ____ ___'), +--curType 货币种类 必填,默认为RMB + curType nchar(10) default('RMB'), +--savingType 存款类型 活期/定活两便/定期 + savingType nchar(10) check(savingType='活期' or savingType='定活两便' or savingType='定期'), +--openDate 开户日期 必填,默认为系统当前日期 + openDate datetime not null default(getdate()), +--balance 余额 必填,不低于1元 + balance varchar(100) not null check(balance>1), +--pass 密码 必填,6位数字,开户时默认为6个“8” + pass int not null check(len(pass)=6) default(888888), +--IsReportLoss 是否挂失 必填,是/否值,默认为”否” + IsReportLoss char(2) not null check(IsReportLoss='是' or IsReportLoss='否') default('否'), +--customerID 顾客编号 外键,必填,表示该卡对应的顾客编号,一位顾客允许办理多张卡号 + customerID int not null references userInfo (customerID) +) + +--交易信息表:transInfo +create table transInfo( +--transId 交易编号 标识列、主键 + transId int primary key identity(1,1), +--transDate 交易日期 必填,默认为系统当前日期 + transDate datetime not null default(getdate()), +--cardID 卡号 必填,外健 + cardID char(20) not null references cardInfo(cardID), +--transType 交易类型 必填,只能是存入/支取 + transType varchar(10) not null check(transType='存入' or transType='支取'), +--transMoney 交易金额 必填,大于0 + transMoney nchar(1000) not null check(transMoney>0), +--remark 备注 可选输入,其他说明 + remark text +) +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +-- 开户金额:1000 活期 卡号:1010 3576 1234 567 +insert into userInfo +select '孙悟空',123456789012345,'0716-78989783','北京海淀 ' +insert into cardInfo values('1010 3576 1234 567','RMB','活期',getdate(),1000,666666,'否',7) +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 117 +insert into userInfo values('沙和尚',421345678912345678,'0478-44223333','河南') +insert into cardInfo values('1010 3576 1212 117','RMB','定期',getdate(),2,888888,'否',9) +--唐僧开户,身份证:321245678912345678,电话:0478-44443333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 113 +insert into userInfo values('唐僧',321245678912345678,'0478-44223333','北京') +insert into cardInfo values('1010 3576 1212 113','RMB','定期',getdate(),2,888888,'否',10) +--1.将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass=611234 +--2.用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录, +--然后在孙悟空账上的余额减200注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号, +--再根据银行卡号来插入交易记录和修改账上余额 +insert into transInfo values(getdate(),'1010 3576 1234 567','支取',200 ,'取钱' ) +--3.用同上题一样的方法实现沙和尚存钱的操作(存300) +insert into transInfo values(getdate(),'1010 3576 1212 117','存入',300,'存') +insert into transInfo values(getdate(),'1010 3576 1212 117','存入',300,'存') +--4.唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” +update cardInfo set IsReportLoss='是' diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c3d791b396977651aa3e1f61c8ede6913eeabda9 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" @@ -0,0 +1 @@ +--某银行拟开发一套ATM取款机系统,实现如下功能: --1、开户(到银行填写开户申请单,卡号自动生成) --2、取钱 --3、存钱 --4、查询余额 --5、转账(如使用一卡通代缴手机话费、个人股票交易等) --第一阶段:建库、建表、建约束、添加数据 --A. 现要求对“ATM柜员机系统”进行数据库的设计并实现,数据库保存在D:\bank目录下,文件增长率为15% 。 create database ATM on( name = ATM, filename = 'D:\bank\ATM.mdf', size =5mb, filegrowth=15%, maxsize =100mb ) log on( name = ATM_ldf, filename = 'D:\bank\ATM.ldf', size =5mb, filegrowth=15%, maxsize =100mb ) use ATM go --B. 根据下图创建表,约束种类参考下列表的说明 --用户信息表:userInfo : --字段名称 说 明 --customerID 顾客编号 自动编号(标识列),从1开始,主键 --customerName 开户名 必填 --PID 身份证号 必填,只能是18位或15位,身份证号唯一约束 --telephone 联系电话 必填,格式为xxxx-xxxxxxxx或手机号13位 --address 居住地址 可选输入 create table userInfo ( customerID int primary key identity, customerName nvarchar(20) not null, PID nvarchar(18) not null unique check(len(PID)=15 or len(PID)=18), telephone varchar(20) not null check(len(telephone)=13 or telephone like '____-________'), address nvarchar(1000) ) --银行卡信息表:cardInfo --字段名称 说 明 --cardID 卡号 必填,主健,银行的卡号规则和电话号码一样,一般前8位代表特殊含义, --如某总行某支行等。假定该行要求其营业厅的卡号格式为:1010 3576 xxxx xxx开始,每4位号码后有空格,卡号一般是随机产生。 --curType 货币种类 必填,默认为RMB --savingType 存款类型 活期/定活两便/定期 --openDate 开户日期 必填,默认为系统当前日期 --balance 余额 必填,不低于1元,否则将销户 --pass 密码 必填,6位数字,开户时默认为6个“8” --IsReportLoss 是否挂失 必填,是/否值,默认为”否” --customerID 顾客编号 外键,必填,表示该卡对应的顾客编号,一位顾客允许办理多张卡号 create table cardInfo ( cardID varchar(20) not null primary key check(substring(cardID,1,9)= '1010 3576' and len(cardID)=18), curType nvarchar(20) not null default('RMB'), savingType nvarchar(20) check(savingType='活期' or savingType='定活两便' or savingType='定期'), openDate smalldatetime not null default(getdate()), balance int not null check(balance>=1),--不低于1元,否则将销户 pass int default(123456), IsReportLoss nvarchar(1) check(IsReportLoss='是'or IsReportLoss='否') default('否'), customerID int foreign key references userInfo(customerID) ) --交易信息表:transInfo --字段名称 说 明 --transId 交易编号 标识列、主键 --transDate 交易日期 必填,默认为系统当前日期 --cardID 卡号 必填,外健,可重复索引 --transType 交易类型 必填,只能是存入/支取 --transMoney 交易金额 必填,大于0 --remark 备注 可选输入,其他说明 create table transInfo ( transId int primary key identity, transDate smalldatetime default(getdate()), cardID varchar(20) not null foreign key references cardInfo(cardID), transType nvarchar(2) not null check(transType='存入' or transType='支取'), transMoney int not null check(transMoney>0), remark nvarchar(20) ) --C. 根据下列条件插入和更新测试数据 --孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 -- 开户金额:1000 活期 卡号:1010 3576 1234 567 insert into userInfo(customerName,PID,telephone,address)values --沙和尚开户,身份证:421345678912345678,电话:0478-44223333, -- 开户金额: 1 定期 卡号:1010 3576 1212 117 ('孙悟空','123456789012345','0716-78989783','北京海淀'), ('沙和尚','421345678912345678','0478-44223333',''), ('唐僧','321245678912345678','0478-44443333','') SELECT * FROM userInfo --唐僧开户,身份证:321245678912345678,电话:0478-44443333, -- 开户金额: 1 定期 卡号:1010 3576 1212 113 insert into cardInfo(balance,savingType,cardID)values (1000,'活期','1010 3576 1234 567'), (1,'定期','1010 3576 1212 117'), (1,'定期','1010 3576 1212 113') SELECT * FROM cardInfo --第二阶段:增、删、改、查 --1. 将用户“孙悟空”开卡时的初始密码更改为“611234” update cardInfo set pass=611234 where cardID='1010 3576 1234 567' --2. 用两条SQL语句实现孙悟空要取钱(取200)的操作, --先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 --注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号, --再根据银行卡号来插入交易记录和修改账上余额 select customerID from userInfo where customerName='孙悟空' select cardID from cardInfo where customerID=(select customerID from userInfo where customerName='孙悟空') insert into transInfo(cardID,transType,transMoney)values('1010 3576 1234 567','支取',200) update cardInfo set balance=balance-200 where cardID='1010 3576 1234 567' --3. 用同上题一样的方法实现沙和尚存钱的操作(存300) select cardID from cardInfo where customerID=(select customerID from userInfo where customerName='沙和尚') insert into transInfo(cardID,transType,transMoney)values('1010 3576 1212 117','存入',300) update cardInfo set balance=balance+300 where cardID='1010 3576 1212 117' --4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” update cardInfo set IsReportLoss ='是' where cardID='1010 3576 1212 113' --5. 查询出最近10天开户的银行卡的信息 select * from userInfo --6. 查询交易金额最大的银行卡信息,子查询实现 select max(transMoney) from transInfo select cardID from transInfo where transMoney=(select max(transMoney) from transInfo) select * from cardInfo where cardID=(select cardID from transInfo where transMoney=(select max(transMoney) from transInfo)) --7. 再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) -- 显示效果: -- 总交易金额:1400.00 -- 支取交易金额:200.00 -- 存入交易金额:1200.00 \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/.keep" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4f08795700f567f3ad3019f1d9fb0d47e6c33d77 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" @@ -0,0 +1,62 @@ +use master +go +create database ATM +on +( +name='ATM', +filename='D:\UFO\ATM.mdf', +size=5mb, +maxsize=50mb, +filegrowth=15% +) +log on +( +name='ATM_log', +filename='D:\UFO\ATM_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=15% +) +use ATM +go +create table userInfo +( +customerID int primary key identity(1,1), +customerName nvarchar(20) not null, +PID char(18) unique check(len(PID) in(15,18)) not null, +telephone char(13) not null check(len(telephone)=13 or telephone like '____-________'), +address nvarchar(50) +) +create table cardInfo +( +cardID nvarchar(18) primary key not null check(substring(cardID,1,9)='1010 3576' and len(cardID)=18), +curType char(10) default('RMB') not null, +savingType nvarchar(4) check(savingType in('','','')), +openDate date default(getdate()) not null, +balance money not null, +pass int check(len(pass)=6) default(888888) not null, +IsReportLoss nvarchar(1) check(IsReportLoss in('','')) default('') not null, +customerID int constraint FK_userInfo_customerID references userInfo(customerID) not null +) +create table transInfo +( +transId int primary key identity, +transDate date not null default(getdate()), +cardID nvarchar(18) constraint FK_cardInfo_cardID references cardInfo(cardID) not null, +transType nvarchar(2) not null check(transType in('','支取')), +transMoney money check(transMoney>0) not null, +remark text +) +insert into userInfo(customerName,PID,telephone,address) values('','123456789012345','0716-78989783',''),('沙','421345678912345678','0478-44223333','沙'),('僧','321245678912345678','0478-44443333','茅') +insert into cardInfo(balance,savingType,cardID,customerID) values(1000,'','1010 3576 1234 567',1),(1,'','1010 3576 1212 117',2),(1,'','1010 3576 1212 113',3) +update cardInfo set pass='611234' where customerID=1 +insert into transInfo(cardID,transType,transMoney) values ('1010 3576 1234 567','支取',200) +update cardInfo set balance-=200 where customerID=1 +insert into transInfo(cardID,transType,transMoney) values ('1010 3576 1212 117','',300) +update cardInfo set balance+=300 where customerID=2 +update cardInfo set isReportLoss='' where customerID = 3 +select * from cardInfo where DateDiff(dd,openDate,getdate())<=10 +select max(transMoney)浜ゆ槗閲戦鏈澶 from transInfo +select 芙捉 = sum(transMoney) from transInfo +select 支取捉 = sum(transMoney) from transInfo where transType = '支取' +select 虢蛔 = sum(transMoney) from transInfo where transType = '' diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/bank.sql.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/bank.sql.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c1941240cac6968b9e416bdd8c9b77e6d76649c9 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/bank.sql.sql" @@ -0,0 +1,92 @@ +use master +go +create database bank +on +( +name='bank', +filename='D:\test\bank.mdf', +size=5, +maxsize=50, +filegrowth=15% +) +log on +( +name='bank_log', +filename='D:\test\bank_log.ldf', +size=5, +maxsize=50, +filegrowth=10% +) +use bank +go +create table userInfo +( +customerID int primary key identity(1,1), +customerName nvarchar(10) not null, +PID nvarchar(20) not null unique check(len(PID)in (15,18)), +telephone nvarchar(15) not null check(len(telephone)=13 and telephone like '____-________'), +address nvarchar(50) +) +create table cardInfo +( +cardID nvarchar(20) not null check(substring(cardID,1,9)='1010 3576' and len(cardID)=18) primary key, +curType nvarchar(10) not null default ('RMB'), +savingType nvarchar(10) check(savingType in ('活期','定活两便','定期')), +openDate datetime default GETDATE() NOT NULL, +balance money check (balance>=1), +pass char (6) not null default (888888), +IsReportLoss nvarchar(1) not null default ('否') check (IsReportLoss in('是','否')), +customerID int not null references userInfo (customerID) +) + create table transInfo + ( + transId int primary key identity(1,1), + transDate datetime default GETDATE() not null, + cardID nvarchar(20) not null references cardInfo(cardID), + transType nvarchar(4) not null check(transType='存入' or transType='支取'), + transMoney money not null check (transMoney>0), + remark nvarchar(50) + ) +-- C. 根据下列条件插入和更新测试数据 +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +insert into userInfo(customerName,PID,telephone,address) values('孙悟空','123456789012345','0716-78989783','北京海淀') +-- 开户金额:1000 活期 卡号:1010 3576 1234 567 +insert into cardInfo (customerID,balance,savingType,cardID) values('1','1000','活期','1010 3576 1234 567') +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333, +insert into userInfo (customerName,PID,telephone) values ('沙和尚','421345678912345678','0478-44223333') +-- 开户金额: 1 定期 卡号:1010 3576 1212 117 +insert into cardInfo (customerID, balance,savingType,cardID) values ('2','1','定期','1010 3576 1212 117') +--唐僧开户,身份证:321245678912345678,电话:0478-44443333, +insert into userInfo (customerName,PID,telephone) values ('唐僧','321245678912345678','0478-44443333') +-- 开户金额: 1 定期 卡号:1010 3576 1212 113 +insert into cardInfo (customerID, balance,savingType,cardID) values ('3','1','定期','1010 3576 1212 113') + +select * from userInfo +select * from cardInfo +select * from transInfo +--第二阶段:增、删、改、查 + +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass='611234' where cardID='1010 3576 1234 567' + +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 +insert into transInfo(cardID,transType,transMoney) values('1010 3576 1234 567','支取',200.00) +update cardInfo set balance=balance-200 where customerID=1 +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) +insert into transInfo(cardID,transType,transMoney) values('1010 3576 1212 117','存入',300.00) +update cardInfo set balance=balance+300 where customerID=2 +update cardInfo set IsReportLoss='是' where customerID=3 +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” +update cardInfo set IsReportLoss='是' where cardID='1010 3576 1212 113' +--5. 查询出最近10天开户的银行卡的信息 + +--6. 查询交易金额最大的银行卡信息,子查询实现 +select max(balance)最大金额 from cardInfo +select sum(transMoney)总支出金额 from transInfo where transType='支取' +--7. 再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) +select * from transInfo +-- 显示效果: +-- 总交易金额:1400.00 +-- 支取交易金额:200.00 +-- 存入交易金额:1200.00 diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..94d87442d1682efcaa2b87de225d6e23b25cf911 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" @@ -0,0 +1,91 @@ +use master +go +create database bank +on +( +name='bank', +filename='D:\ljcbank\bank.mdf', +size=5, +maxsize=20, +filegrowth=15% +) +log on +( +name='bank_log', +filename='D:\ljcbank\bank_log.ldf', +size=5, +maxsize=20, +filegrowth=15% +) +go +use bank +go +create table userInfo +( +customerID int primary key identity(1,1), +customerName text not null, +PID int check(PID>=15 or PID<=18) unique not null, +telephone char(13) not null check(len(telephone)=13 and telephone like'____-________'), +address text +) +create table cardInfo +( +cardID int primary key not null check(cardID like'1010 3576 ____ ___'), +curType monEY not null default('RMB'), +savingType NVARCHAR(20) check(savingType='定活两便' or savingType='定期' or savingType='活期'), +openDate DATETIME not null default (getdate()), +balance int not null check(balance>=1), +pass int not null check(len(pass)=6) default('888888'), +IsReportLoss nchar(1) not null check(IsReportloss='是' or IsReportloss='否') default('否'), +customerID int not null references userInfo(customerID) +) +create table transInfo +( +transId int primary key identity(1,1), +transDate datetime not null default(getdate()), +cardID nvarchar(20), +transType nchar(2) not null check(transType='存入' or transType='支取'), +transMoney int not null check(transMoney>0), +remark text +) +insert into userInfo +select '孙悟空','123456789012345','0716-78989783','北京海淀' union +select '沙和尚','421345678912345678','0478-44223333',''union +select '唐僧','321245678912345678','0478-44443333','' + +select * from userInfo +insert into cardInfo(cardID,savingType,balance,customerID) +select '1010 3576 1234 567','活期','1000','1' union +select '1010 3576 1212 117','定期','1','2' union +select '1010 3576 1212 113','定期','1','3' +select * from cardInfo + +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” + +update cardInfo set pass=611234 where savingType='活期' + +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 + +select customerID from userInfo where customerName='孙悟空' +select cardID from cardInfo where customerID=2 +insert into transInfo(transDate,cardID,transType,transMoney) +SELECT getdate(),'1010 3576 1234 567','存入','200' +update transInfo set transMoney=1 where cardID ='1010 3576 1234 567' +select * from transInfo + +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) + +select customerID from userInfo where customerName='沙和尚' +select cardID from cardInfo where customerID=1 +insert into transInfo(transDate,cardID,transType,transMoney) +select getdate(),'1010 3576 1212 117','存入','200' +update transInfo set transMoney=1 where cardID='1010 3576 1212 117' + +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” + +update cardInfo set IsReportLoss='是' where cardID='1010 3576 1212 113' + +--5. 查询出最近10天开户的银行卡的信息 + +select * from cardInfo where DateDiff(dd,openDate,getdate())<=10 diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery4.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9997415e1016bcaf741e8a5655b8e1a554b5b908 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery4.sql" @@ -0,0 +1,99 @@ +use master +go +create database aaa +on +( +name='aaa', +filename='D:\aaa.mdf', +size=5, +maxsize=100, +filegrowth=15% +) +log on +( +name='aaa_log', +filename='D:\aaa_log.ldf', +size=5, +maxsize=100, +filegrowth=15% +) +go + +use aaa +go + +create table userInfo +( +customerID int primary key identity, +customerName varchar(10) not null, +PID char(18) not null check(len (PID)<=18 and len(PID)>=15) unique, +telephone varchar(15) not null check(len (telephone)=13 and telephone like '____-________'), +address varchar(20) +) + + + + + + +create table cardInfo +( +cardID varchar(18) not null primary key check(len (cardID)<=18 and len (cardID)>=15 and cardID like '____ ____ ____ ___' ),--卡号 +curType varchar(10) not null default('RMB'),--货币种类 +savingType varchar(10) not null default('活期') check(savingType='定期'or savingType='活期'or savingType='定活两便'), +openDate datetime not null default(getdate()),--时间 +balance money not null check(balance>=1),--开户金额 +pass varchar(20) not null default('888888') check(len(pass)=6),--密码 +IsReportLoss varchar(2) not null check(IsReportLoss='否' or IsReportLoss='是') default('否'), +customerID int foreign key references userInfo(customerID) identity not null +) + + + +create table transInfo +( +transId int primary key identity, +transDate datetime not null default(getdate()), +cardID varchar(18) foreign key references cardInfo(cardID) not null, +transType varchar(10) not null check(transType='支出' or transType='存入') , +transMoney money not null check(transMoney>0), +remark varchar(20) + +) +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +-- 开户金额:1000 活期 卡号:1010 3576 1234 567 + +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 117 + +--唐僧开户,身份证:321245678912345678,电话:0478-44443333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 113 + + +insert into userInfo(customerName,PID,telephone,address) values ('孙悟空','123456789012345','0716-78989783','北京海淀'), +('沙和尚','421345678912345678','0478-44223333',''), +('唐僧','321245678912345678','0478-44443333','') +insert into cardInfo(cardID,balance) values ('1010 3576 1234 567',1000), +('1010 3576 1212 117',1), +('1010 3576 1212 113',1) + +update cardInfo set pass='611234' where cardID='1010 3576 1234 567' + +select (balance-200) from cardInfo where cardID='1010 3576 1234 567' +update cardInfo set balance=(select (balance-300) from cardInfo where cardID='1010 3576 1234 567') where cardID='1010 3576 1234 567' +insert into transInfo(cardID,transType,transMoney) values('1010 3576 1234 567','支出',200) + +select (balance+300) from cardInfo where cardID='1010 3576 1212 117' +update cardInfo set balance=(select (balance+300) from cardInfo where cardID='1010 3576 1212 117') where cardID='1010 3576 1212 117' +insert into transInfo(cardID,transType,transMoney) values('1010 3576 1212 117','存入',300) + + +update cardInfo set IsReportLoss='是' where cardID='1010 3576 1212 113' + +select * from cardInfo where DATEDIFF(day,openDate,getdate())<10 + +select * from userInfo + +select * from cardInfo + +select * from transInfo \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery2.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..77c9997fc6eacebd81c2cc53eb7933b76fb3eb6d --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery2.sql" @@ -0,0 +1,95 @@ +use master +go +create database bank1 +on +( name=' bank1', + filename='D:\bank\ bank1.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=15% + +) + log on +( name=' bank1_log', + filename='D:\bank\bank1_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=15% + +) +go +use bank1 +go +create table userInfo +( customerID int primary key identity(1,1), + customerName nvarchar(5) not null, + PID varchar(18) unique check(len(PID)=18 or len(PID)=15), + telephone char(13) check(telephone like '____-________' or len(telephone)=13), + address1 nvarchar(200) +) + +create table cardInfo +( cardID char(20) not null primary key check(substring(cardID,1,9)='1010 3576' and len(cardID)=18), + curType nvarchar(10) not null default('RMB'), + savingType nvarchar(4), + openDate datetime not null default(getdate()), + balance bigint not null check(balance>1), + pass int not null default('888888') check(len(pass)=6), + IsReportLoss nchar(1) not null default('否'), + customerID int references userInfo(customerID) not null + +) +create table transInfo +( transId int primary key identity(1,1), + transDate datetime not null default(getdate()), + cardID char(20) references cardInfo(cardID) not null, + transType nchar(2) not null check(transType='存入'or transType='支取' ), + transMoney bigint not null check(transMoney>0), + remark text +) + +select*from userInfo +select*from cardInfo +select*from transInfo + + insert into userInfo values('孙悟空',123456789012345,'0716-78989783','北京海淀 '), + ('沙和尚',421345678912345678,'478-044223333','湖南'), + ('唐僧',321245678912345678,'0478-44443333','武汉') + + insert into cardInfo values( '1010 3576 1234 567', default ,'活期',default,1000,default,'是',4), + ( '1010 3576 1212 117', default ,'活期',default,3,default,'是',5), + ( '1010 3576 1212 113', default ,'活期',default,5,default,'是',6) + +-- 第二阶段:增、删、改、查 +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass=611234 where customerID=4 + +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 + select*from userInfo where customerName='孙悟空' + select cardID from cardInfo where customerID in ( select customerID from userInfo where customerName='孙悟空') + +--插入交易金额和修改账上余额: + insert into transInfo values(default,'1010 3576 1234 567','支取',200,'在今天花了200元') + update cardInfo set balance=balance-200 where customerID in(select customerID from userInfo where customerName='孙悟空') + + +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) + select*from userInfo where customerName='沙和尚' + select cardID from cardInfo where customerID in ( select customerID from userInfo where customerName='沙和尚') + + + insert into transInfo values(default,'1010 3576 1212 117','存入',300,'在今天存了300元') + update cardInfo set balance=balance+300 where customerID in(select customerID from userInfo where customerName='沙和尚') + +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” + update cardInfo set IsReportLoss='是' where customerID=( select customerID from userInfo where customerName='唐僧') +--5. 查询出最近10天开户的银行卡的信息 +select*from cardInfo where openDate between '2021-03-18'and '2021-03-20' + +--6. 查询交易金额最大的银行卡信息,子查询实现 +select*from transInfo where transMoney=(select max(transMoney) from transInfo ) +--7. 再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) +-- 显示效果: +-- 总交易金额:1400.00 +select sum(transMoney) as 总交易金额 from transInfo \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/.keep" "b/\347\254\254\344\270\203\346\254\241\350\257\276\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\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery7.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e57cd26d41eb23289efb06d4f5831ed9473adbfb --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery7.sql" @@ -0,0 +1,79 @@ +create database ATMsystem +on +( + name='ATMsystem', + filename='D:\bank.mdf', + filegrowth=15% +) +log on +( + name='ATMsystem_log', + filename='D:\bank_log.mdf', + filegrowth=15% +) +go +use ATMsystem +go + +create table userInfo +( + customerID int identity(1,1) primary key, + customerName varchar(32) not null, + PID varchar(18) unique check(len(PID)=18 or len(PID)=15), + telephone char(13) check(telephone like '____-________' or len(telephone)=13), + address varchar(200) +) + +create table cardInfo +( + cardID char(18) check(cardID like '1010 3576 ____ ___') primary key, + curType varchar(30) not null default('RMB'), + savingType nvarchar(4) check(savingType in('活期','定活两便','定期')) not null, + openDate date default(getdate()), + balance money check(balance>0) not null, + pass char(6) not null default('888888'), + IsReportLoss char(2) check(IsReportLoss in('是','否')) default('否') not null, + customerID int references userInfo(customerID) +) +create table transInfo +( + transId int identity(1,1) primary key, + transDate datetime default(getdate()) not null, + cardID char(18) check(cardID like '1010 3576 ____ ___') + references cardInfo(cardID) not null, + transType nvarchar(2) check(transType in('存入','支取')) not null, + transMoney money check(transMoney>0), + remark varchar(200) +) +insert into userInfo values ('孙悟空','123456789012345','0716-78989783','北京海淀') +insert into cardInfo(customerID,balance,savingType,cardID) values(1,1000,'活期','1010 3576 1234 567') +select * from userInfo +select * from cardInfo +insert into userInfo values ('沙和尚','421345678912345678','0478-44223333','') +insert into cardInfo(customerID,balance,savingType,cardID) values(2,1,'定期','1010 3576 1212 117') +select * from userInfo +select * from cardInfo +insert into userInfo values ('唐僧','321245678912345678','0478-44443333','') +insert into cardInfo(customerID,balance,savingType,cardID) values(3,1,'定期','1010 3576 1212 113') +select * from userInfo +select * from cardInfo + +update cardInfo set pass='611234' where customerID=1 + +select * from userInfo +select * from cardInfo +select * from transInfo +insert into transInfo(cardID,transType,transMoney) values ('1010 3576 1234 567','支取',200) +update cardInfo set balance=balance-200 where customerID=1 +insert into transInfo(cardID,transType,transMoney) values ('1010 3576 1212 117','存入',300) +update cardInfo set balance=balance+300 where customerID=2 + +update cardInfo set IsReportLoss='是' where customerID=3 + +select * from cardInfo where openDate>='2021-03-09' and openDate<='2021-03-19' + +select * from cardInfo where cardID=(select cardID from transInfo where transMoney=(select max(transMoney) from transInfo)) + +select sum(transMoney) from transInfo +select sum(transMoney) from transInfo where transType='存入' +select sum(transMoney) from transInfo where transType='支取' diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..332f84df0d9d462590d1874641c0f197afe769bf --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" @@ -0,0 +1,104 @@ +use master +go + +create database ATM +on primary +( + name = 'ATM', + filename = 'D:\bank\ATM.mdf', + size = 5MB, + maxsize = 50MB, + filegrowth = 15% +) +log on +( + name = 'ATM_log', + filename = 'D:\bank\ATM_log.ldf', + size = 1MB, + maxsize = 10MB, + filegrowth = 10% +) +go + +use ATM +go + +create table userInfo +( + customerID int identity(1,1) primary key, + customerName nvarchar(5) not null, + PID char(18) check(len(PID) in(18,15)) unique(PID) not null, + telephone char(13) check(telephone like ('____-_______') or len(telephone) = 13) not null, + address nvarchar(50) +) +go + +create table cardInfo +( + cardID char(18) check(len(cardID) = 18 and cardID like ('1010 3576 ____ ___')) primary key, + --cardID char(18) check(substring(cardID,1,9) = '1010 3576' and len(cardID) = 18) + curType char(3) default('RMB') not null, + savingType nvarchar(4) check(savingType in('娲绘湡','瀹氭湡','瀹氭椿涓や究')), + openDate date default(getdate()) not null, + balance money not null, + pass int check(len(pass) = 6) default(888888) not null, + isReportLoss nchar(1) check(isReportLoss in('鏄','鍚')) default('鍚') not null, + custromerID int constraint FK_cardInfo_custromerID foreign key references userInfo(customerID) not null +) + + +go + +create table transInfo +( + transID int identity(1,1) primary key, + transDate date default(getdate()) not null, + cardID char(18) constraint FK_transInfo_cardID references cardInfo(cardID) not null, + transType nchar(2) check(transType in('瀛樺叆','鏀彇')) not null, + transMoney money check(transMoney > 0) not null, + remark ntext +) +go + +insert into userInfo(customerName,PID,telephone,address) values ('瀛欐偀绌','123456789012345','0716-78989783','鑳屾櫙娴锋穩') +insert into userInfo(customerName,PID,telephone) values ('娌欏拰灏','421345678912345678','0478-44223333'), +('鍞愬儳','321245678912345678','0478-44443333') +insert into cardInfo(cardID,savingType,balance,custromerID) values ('1010 3576 1234 567','娲绘湡',1000.00,1), +('1010 3576 1212 117','瀹氭湡',1.00,2), +('1010 3576 1212 113','瀹氭湡',1.00,3) + +update cardInfo set pass = 611234 where custromerID = 1 --鏀瑰瘑鐮 + + +insert into transInfo(cardID,transType,transMoney) values ((select cardID from cardInfo where custromerID = (select customerID from userInfo where customerName = '瀛欐偀绌')),'鏀彇',300) +go +update cardInfo set balance -= 200 where custromerID = (select customerID from userInfo where customerName = '瀛欐偀绌') +go + +insert into transInfo(cardID,transType,transMoney) values ((select cardID from cardInfo where custromerID = (select customerID from userInfo where customerName = '娌欏拰灏')),'瀛樺叆',300) +go +update cardInfo set balance += 300 where custromerID = (select customerID from userInfo where customerName = '娌欏拰灏') +go + +update cardInfo set isReportLoss = '鏄' where custromerID = 3 +--鏈杩10澶╁紑鎴风殑淇℃伅 +select * from cardInfo where DATEDIFF(dd,getdate(),openDate) <= 10 +--浜ゆ槗閲戦鏈澶х殑閾惰鍗′俊鎭 +select * from cardInfo where cardID = (select top 1 cardID from transInfo where transMoney = (select top 1 max(transMoney) from transInfo)) + +<<<<<<< HEAD +select 总交易金额 = sum(transMoney) from transInfo +select 支取交易金额 = sum(transMoney) from transInfo where transType = '支取' +select 存入交易金额 = sum(transMoney) from transInfo where transType = '存入' +go +======= +select 鎬讳氦鏄撻噾棰 = sum(transMoney) from transInfo +select 鏀彇浜ゆ槗閲戦 = sum(transMoney) from transInfo where transType = '鏀彇' +select 瀛樺叆浜ゆ槗閲戦 = sum(transMoney) from transInfo where transType = '瀛樺叆' + +>>>>>>> 254a12f3a92623aa4b74b508daaad46b5a45a9b0 + + + + + diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery1 3.19.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery1 3.19.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ef61074e8d26d7cd12aa31446ce78e2804e68694 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery1 3.19.sql" @@ -0,0 +1,109 @@ +use master +go + +create database ATM +on( + name='ATM', + filename='D:\bank\ATM.mdf', + size=8mb, + maxsize=80mb, + filegrowth=15% +) + +log on( + name='ATM_log', + filename='D:\bank\ATM_log.ldf', + size=8mb, + maxsize=80mb, + filegrowth=15% +) + +go +use ATM +go + +create table userInfo +( + customerID int primary key identity(1,1), + customerName varchar(20) not null, + PID varchar(20) check(len(PID)=15 or len(PID)=18) unique not null, + telephone varchar(13) not null check (len(telephone)=13 and telephone like '____-________'), + address varchar(20) not null +) + +create table cardInfo +( + cardID varchar(18) primary key not null check(substring(cardID ,1,9)='1010 3576' and len(cardID)=18), + curType varchar(20) not null default('RMB'), + savingType char(20),check(savingType='活期' or savingType='定活两便' or savingType='定期'), + openDate datetime not null default (getdate()),--getedate()当前日期 + balance varchar(60) not null check(balance>=1), + pass varchar(60) not null default('888888'), + IsReportLoss char(20) default('否') check(IsReportLoss='是' or IsReportLoss='否'), + customerID int references userInfo(customerID) not null, + +) +drop table cardInfo +alter table transInfo drop constraint FK__transInfo__cardI__34C8D9D1 +alter table transInfo add constraint fk foreign key(cardID) references cardInfo(cardID) +create table transInfo +( + transId int primary key identity(1,1), + transDate datetime default(getdate()) not null, + cardID varchar(18) not null references cardInfo(cardID) , + transType varchar(10) not null check(transType='存入' or transType='支取'), + transMoney int not null check(transMoney>=0), + remark text +) + +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +-- 开户金额:1000 活期 卡号:1010 3576 1234 567 +select*from userInfo +insert into userInfo values('孙悟空','123456789012345','0716-78989783','北京海淀') + +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 117 +insert into userInfo(customerName,PID,telephone,address) values('沙和尚','421345678912345678','0478-44223333',' ') +--唐僧开户,身份证:321245678912345678,电话:0478-44443333, +insert into userInfo(customerName,PID,telephone,address) values('唐僧','321245678912345678','0478-44443333',' ') +-- 开户金额: 1 定期 卡号:1010 3576 1212 113 +select * from cardInfo +insert into cardInfo values ('1010 3576 1234 567',default,'活期',default,1000,default,default,1) +insert into cardInfo values ('1010 3576 1212 117',default,'定期',default,1,default,default,2) +insert into cardInfo values ('1010 3576 1212 113',default,'定期',default,1,default,default,3) + + + + + + +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass='611234' where customerID=1 + +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 +select * from transInfo +insert into transInfo(cardID,transType,transMoney) +values ('1010 3576 1234 567','支取','200') +update cardInfo set balance=balance-200 where customerID=1 +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) +select * from transInfo +insert into transInfo(cardID,transType,transMoney) +values ('1010 3576 1212 117','存入','300') +update cardInfo set balance=balance+300 where customerID=2 + +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” +update cardInfo set IsReportLoss='是' where customerID=3 +--5. 查询出最近10天开户的银行卡的信息 +select top 10 * from cardInfo where openDate>='2021-3-10' +--6. 查询交易金额最大的银行卡信息,子查询实现 +select top 1 * from cardInfo where balance>1 order by balance DESC +--7. 再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) +-- 显示效果: +-- 总交易金额:1400.00 +-- 支取交易金额:200.00 +-- 存入交易金额:1200.00 +select sum (transMoney)总交易金额 from transInfo +select sum (transMoney)支取交易金额 from transInfo where transType='支取' +select sum (transMoney)存入交易金额 from transInfo where transType='存入' + No newline at end of file \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0e8ff972a748e78a31a24013cb331ba6315fb3bd --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" @@ -0,0 +1,93 @@ +create database ATM +on +( + name='ATM', + filename='D:\bank\ATM.mdf', + size=10mb, + filegrowth=15mb +) +log on +( + name='ATM_log', + filename='D:\bank\ATM_log.ldf', + size=10mb, + filegrowth=15mb +) +use ATM +go +create table userInfo +( + customerID int identity(1,1) primary key, + customerName nchar(10) not null, + PID CHAR(18) check(len(PID) =15 or len(PID)=18) not null unique, + telephone char(13) not null check(len(telephone)=13 and telephone like'____-________'), + address nchar(20) + +) +create table cardInfo +( + cardID varchar(40) primary key not null check(cardID like'1010 3576 ____ ___') , + curType varchar(10) not null default('RMB'), + savingType varchar(10),check(savingType in('活期','定期','定活两期')), + openDate datetime not null default(getdate()) , + balance int not null check(balance>=1), + pass varchar(10) check(len(pass)=6) default('888888') not null, + IsReportLoss char(2) not null check(IsReportLoss in('是','否')) default('否'), + customerID int references userInfo(customerID), +) + +create table transInfo +( + transId int primary key identity, + transDate date not null default(getdate()) , + cardID nchar(20) not null, + transType varchar(4) not null check(transType in('存入','支出')), + transMoney int not null check(transMoney>0) , + remark text + +) +--C. 根据下列条件插入和更新测试数据 +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 + -- 开户金额:1000 活期 卡号:1010 3576 1234 567 + insert into userInfo values ('孙悟空','123456789012345','0716-78989783','北京海淀') + insert into cardInfo(balance,savingType,cardID) values( 1000,'活期','1010 3576 1234 567') + select * from cardInfo +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333, + -- 开户金额: 1 定期 卡号:1010 3576 1212 117 + insert into userInfo values ('沙和尚','421345678912345678','0478-44223333',' ') + insert into cardInfo (balance,savingType,cardID)values(1,' 活期','1010 3576 1234 567') +--唐僧开户,身份证:321245678912345678,电话:0478-44443333, + -- 开户金额: 1 定期 卡号:1010 3576 1212 113 + insert into userInfo values('唐僧','321245678912345678','0478-44443333','') + insert into cardInfo(balance,savingType,cardID) values(1 ,'定期','1010 3576 1212 113') + + select * from userInfo + +--第二阶段:增alter、删drop、改update、查select + +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” +select * from cardInfo +update cardInfo set pass='611234' where cardID='1010 3576 1234 567' + +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 +insert into transInfo(cardID,transType,transMoney) values ('1010 3576 1234 567','支出',200) +select * from transInfo +update cardInfo set balance=balance-200 where cardID='1010 3576 1234 567' +select * from cardInfo +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) +insert into transInfo(cardID,transType,transMoney) values ('1010 3576 1234 567','存入',300) +select * from transInfo +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” +update cardInfo set IsReportLoss='是' where cardID='1010 3576 1212 113' +select * from cardInfo +--5. 查询出最近10天开户的银行卡的信息 + +--6. 查询交易金额最大的银行卡信息,子查询实现 + +select cardID ,sum(balance) as balance from cardInfo +group by cardID + +--7. 再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示 + -- 显示效果: + -- 总交易金额:1400.00 diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ca2a5659eabe5031561ec84a55f0c1e52c6b1aad --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232.sql" @@ -0,0 +1,72 @@ +use master +go +create database ATM +on +( name='ATM', + filename='C:\test\ATM.mdf', + size=20, + maxsize=200, + filegrowth=10% + ) + log on + ( name='ATM_log', + filename='C:\test\ATM_log.mdf', + size=20, + maxsize=200, + filegrowth=10% + ) + go + use ATM + go + create table userInfo + ( + customerID int primary key identity, + customerName varchar(20) not null, + PID varchar(20) check(len(PID)=15 or len(PID)=18) not null, + telephone char(13) check(len(telephone)=13 and telephone like '____-________') not null, + address text + ) + + create table cardInfo + ( + cardID char(18) primary key check(len(cardID)=18 and cardID like '1010 3576 ____ ___') not null , + curType varchar(20) default('RMB')not null, + savingType nchar(4) check(savingType='活期'or savingType='定活两便'or savingType='定期'), + openDate date default(getdate()) not null, + balance money check(balance>=1) not null, + pass char(6) default('888888') check(len(pass)=6 ) not null , + IsReportLoss nchar(1) default('否') check (IsReportLoss='是'or IsReportLoss='否') not null, + customerID int foreign key (customerID) references userInfo (customerID) + ) + create table transInfo + ( + transId int primary key identity(1,1), + transDate date default(getdate()) not null, + cardID char(18) foreign key references cardInfo(cardID), + transType int check (transType='存入'or transType='支取') not null, + transMoney Money check(transMoney>0) not null, + remark text + ) + + insert into userInfo(customerName,PID,telephone,address) values('孙悟空','123456789012345','0716-78989783','北京海淀') + insert into userInfo(customerName,PID,telephone,address) values('沙和尚','421345678912345678','0478-44223333','流沙河') + insert into userInfo(customerName,PID,telephone,address) values('唐僧','321245678912345678','0478-44443333','东土大唐') + + insert into cardInfo(balance,savingType,cardID,customerID) values('1000 ','活期','1010 3576 1234 567','1') + insert into cardInfo(balance,savingType,cardID,customerID) values('1 ','定期','1010 3576 1212 117','2') + insert into cardInfo(balance,savingType,cardID,customerID) values('1 ','定期','1010 3576 1212 113','3') + + Update cardInfo set pass='611234' where customerID=1 + + insert into transInfo(cardID,transType,transMoney)values ('1010 3576 1234 567','支取','200') + Update cardInfo set balance=balance-200 where customerID=1 + + insert into transInfo(cardID,transType,transMoney)values ('1010 3576 1212 117','存入','300') + Update cardInfo set balance=balance+300 where customerID=2 + + Update cardInfo set IsReportLoss='是' where customerID=3 + + select*from cardInfo where DATEDIFF(DD,openDate,getdate())<10 + + select max(balance) from cardInfo + select sum(transMoney) from transInfo where transType='支出' \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/.keep" "b/\347\254\254\344\270\203\346\254\241\350\257\276\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\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery2.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e81d755bbe9eff68ed266224a8db88b3f98bd145 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery2.sql" @@ -0,0 +1,116 @@ + +--第一阶段:建库、建表、建约束、添加数据 +--A. 现要求对“ATM柜员机系统”进行数据库的设计并实现,数据库保存在D:\bank目录下,文件增长率为15% 。 + + +if exists(select*from sys.data_spaces where name='ATM柜员机系统') + drop database ATM柜员机系统 + +create database ATM柜员机系统 +on +( + name='ATM柜员机系统', + filename='E:\数据库文件\数据库跟目录文件\bank\ATM柜员机系统.mdf', + size=5, + filegrowth=15%, + maxsize=100 +) +log on +( + name='ATM柜员机系统_log', + filename='E:\数据库文件\数据库跟目录文件\bank\ATM柜员机系统_log.ldf', + size=5, + filegrowth=15%, + maxsize=100 +) +go + +use ATM柜员机系统 +go + + +--B. 根据下图创建表,约束种类参考下列表的说明 +--用户信息表:userInfo : +--字段名称 说 明 +--customerID 顾客编号 自动编号(标识列),从1开始,主键 +--customerName 开户名 必填 +--PID 身份证号 必填,只能是18位或15位,身份证号唯一约束 +--telephone 联系电话 必填,格式为xxxx-xxxxxxxx或手机号13位 +--address 居住地址 可选输入 + +create table userInfo +( customerID int primary key identity(1,1), + customerName nvarchar(5) not null, + PID varchar(18) unique check(len(PID)=18 or len(PID)=15), + telephone char(13) check(telephone like '____-________' or len(telephone)=13), + address1 nvarchar(200) +) + +create table cardInfo +( cardID char(20) not null primary key check(substring(cardID,1,9)='1010 3576' and len(cardID)=18), + curType nvarchar(10) not null default('RMB'), + savingType nvarchar(4), + openDate datetime not null default(getdate()), + balance bigint not null check(balance>1), + pass int not null default('888888') check(len(pass)=6), + IsReportLoss nchar(1) not null default('否'), + customerID int references userInfo(customerID) not null + +) +go +drop database cardInfo + +create table transInfo +( transId int primary key identity(1,1), + transDate datetime not null default(getdate()), + cardID char(20) references cardInfo(cardID) not null, + transType nchar(2) not null check(transType='存入'or transType='支取' ), + transMoney bigint not null check(transMoney>0), + remark text +) + +select*from userInfo +select*from cardInfo +select*from transInfo + + insert into userInfo values('孙悟空',123456789012345,'0716-78989783','北京海淀 '), + ('沙和尚',421345678912345678,'478-044223333','湖南'), + ('唐僧',321245678912345678,'0478-44443333','武汉') + + insert into cardInfo values( '1010 3576 1234 567', default ,'活期',default,1000,default,'是',4), + ( '1010 3576 1212 117', default ,'活期',default,3,default,'是',5), + ( '1010 3576 1212 113', default ,'活期',default,5,default,'是',6) + +-- 第二阶段:增、删、改、查 +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass=611234 where customerID=4 + +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号,再根据银行卡号来插入交易记录和修改账上余额 + select*from userInfo where customerName='孙悟空' + select cardID from cardInfo where customerID in ( select customerID from userInfo where customerName='孙悟空') + +--插入交易金额和修改账上余额: + insert into transInfo values(default,'1010 3576 1234 567','支取',200,'在今天花了200元') + update cardInfo set balance=balance-200 where customerID in(select customerID from userInfo where customerName='孙悟空') + + +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) + select*from userInfo where customerName='沙和尚' + select cardID from cardInfo where customerID in ( select customerID from userInfo where customerName='沙和尚') + + + insert into transInfo values(default,'1010 3576 1212 117','存入',300,'在今天存了300元') + update cardInfo set balance=balance+300 where customerID in(select customerID from userInfo where customerName='沙和尚') + +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” + update cardInfo set IsReportLoss='是' where customerID=( select customerID from userInfo where customerName='唐僧') +--5. 查询出最近10天开户的银行卡的信息 +select*from cardInfo where openDate between '2021-03-18'and '2021-03-20' + +--6. 查询交易金额最大的银行卡信息,子查询实现 +select*from transInfo where transMoney=(select max(transMoney) from transInfo ) +--7. 再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) +-- 显示效果: +-- 总交易金额:1400.00 +select sum(transMoney) as 总交易金额 from transInfo \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..49cba0b8ec033c507023b26fd7fd46a3e9a78365 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" @@ -0,0 +1,53 @@ +create database ATMsystem +on +( + name='ATMsystem', + filename='D:\bank.mdf', + filegrowth=15% +) +log on +( + name='ATMsystem_log', + filename='D:\bank_log.mdf', + filegrowth=15% +) +go +use ATMsystem +go +create table userInfo +( + customerID int primary key identity(1,1), + customerName nvarchar(32) not null, + PID varchar(18) not null unique check (len(PID)=15 or len(PID)=18), + telephone char (13) not null check (len(telephone)=13 or telephone like '____-________'), + address nchar(50) +) +create table cardInfo +( +cardID varchar(18) check(substring(cardID,1,9)='1010 3576' and substring(cardID,10,18)='____ ___') primary key not null , +curType money default ('RMB') not null, +savingType nchar(2) check(savingType in('活期','定活两便','定期')), +openDate date default(getdate()) not null, +balance money not null, +pass varchar(6) default('888888'), +IsReportLoss nchar(1) check(IsReportLoss in ('是','否')) default('否') not null, +customerID int references userInfo(customerID) not null +) +create table tranInfo +( +transId int primary key identity, +transDate date default(getdate()) not null, +cardID varchar(18) references cardInfo(cardID) not null, +transType nchar(2) check(transType in ('是','否')), +transMoney money not null, +remark nchar(200) +) +insert into userInfo values('孙悟空','123456789012345','0716-78989783','北京海淀') +insert into cardInfo(customerID,balance,savingType,cardID) values(1,1000,'活期','1010 3576 1234 567') +select * from userInfo +insert into userInfo values('沙和尚','421345678912345','0478-44223333') +insert into cardInfo(customerID,balance,savingType,cardID) values(2,1,'定期','1010 3576 1212 117') +select * from userInfo +Insert into userInfo values('唐僧','321245678912345','0478-44443333') +insert into cardInfo(customerID,balance,savingType,cardID) values(3,1,'定期','1010 3576 1212 113') +select * from userInfo \ No newline at end of file diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..02358068281c5657285cdb41856ab907875394c4 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" @@ -0,0 +1,154 @@ +--某银行拟开发一套ATM取款机系统,实现如下功能: +--1、开户(到银行填写开户申请单,卡号自动生成) +--2、取钱 +--3、存钱 +--4、查询余额 +--5、转账(如使用一卡通代缴手机话费、个人股票交易等) +use master +go +--第一阶段:建库、建表、建约束、添加数据 +--A. 现要求对“ATM柜员机系统”进行数据库的设计并实现,数据库保存在D:\bank目录下,文件增长率为15% 。 +create database ATM +on( + name = 'ATM_mdf', + filename='D:\bank\ATM_mdf.mdf', + size=5mb, + maxsize=1000mb, + filegrowth=15% +) +log on( + name = 'ATM_ldf', + filename='D:\bank\ATM_ldf.ldf', + size=5mb, + maxsize=1000mb, + filegrowth=15% +) +go + +use ATM +go + +--B. 根据下图创建表,约束种类参考下列表的说明 +--用户信息表:userInfo : +--字段名称 说 明 +--customerID 顾客编号 自动编号(标识列),从1开始,主键 +--customerName 开户名 必填 +--PID 身份证号 必填,只能是18位或15位,身份证号唯一约束 +--telephone 联系电话 必填,格式为xxxx-xxxxxxxx或手机号13位 +--address 居住地址 可选输入 +create table userInfo +( + customerID int identity(1,1) primary key , + customerName nvarchar(10) not null, + PID nvarchar(20) not null check(len(PID)=18 or len(PID)=15) unique, + telephone nvarchar(20) not null check(telephone like '____-________' or len(telephone)=13 ), + address text +) + +--银行卡信息表:cardInfo +--字段名称 说 明 +--cardID 卡号 必填,主健,银行的卡号规则和电话号码一样,一般前8位代表特殊含义,如某总行某支行等。 +--假定该行要求其营业厅的卡号格式为:1010 3576 xxxx xxx开始,每4位号码后有空格,卡号一般是随机产生。 +--curType 货币种类 必填,默认为RMB +--savingType 存款类型 活期/定活两便/定期 +--openDate 开户日期 必填,默认为系统当前日期 +--balance 余额 必填, +--pass 密码 必填,6位数字,开户时默认为6个“8” +--IsReportLoss 是否挂失 必填,是/否值,默认为”否” +--customerID 顾客编号 外键,必填,表示该卡对应的顾客编号,一位顾客允许办理多张卡号 + +create table cardInfo +( + cardID nvarchar(18) not null primary key check(substring(cardID,1,9)='1010 3576 'and len(cardID)=18), + curType nvarchar(10) default('RMB'), + savingType nvarchar(20) check(savingType='活期' or savingType='定活两便' or savingType='定期'), + openDate datetime not null default(getdate()), + balance int not null check(balance>=1), + pass int not null default(888888) check(len(pass)=6), + IsReportLoss nvarchar(2) not null default('否') check(IsReportLoss='是' or IsReportLoss='否'), + customerID int not null references userInfo(customerID) +) + +--交易信息表:transInfo +--字段名称 说 明 +--transId 交易编号 标识列、主键 +--transDate 交易日期 必填,默认为系统当前日期 +--cardID 卡号 必填,外健, +--transType 交易类型 必填,只能是存入/支取 +--transMoney 交易金额 必填,大于0 +--remark 备注 可选输入,其他说明 + +create table transInfo +( + transId int identity(1,1) primary key, + transDate datetime not null default(getdate()), + cardID nvarchar(18) not null references cardInfo(cardID), + transType nvarchar(4) not null check(transType='存入' or transType='支取'), + transMoney int not null check(transMoney>0), + remark text +) +--C. 根据下列条件插入和更新测试数据 +--孙悟空开户,身份证:123456789012345,电话:0716-78989783,地址:北京海淀 +-- 开户金额:1000 活期 卡号:1010 3576 1234 567 +select *from userInfo + +insert into userInfo(customerName,PID,telephone,address) +values +('孙悟空',123456789012345,'0716-78989783','北京海淀'), +('沙和尚',421345678912345678,'0478-44223333',''), +('唐僧',321245678912345678,'0478-44443333','') + +--沙和尚开户,身份证:421345678912345678,电话:0478-44223333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 117 + +--唐僧开户,身份证:321245678912345678,电话:0478-44443333, +-- 开户金额: 1 定期 卡号:1010 3576 1212 113 +select * from cardInfo +insert into cardInfo(cardID,savingType,balance,customerID) +values('1010 3576 1234 567','活期',1000,11), +('1010 3576 1212 117','定期',1,12), +('1010 3576 1212 113','定期',1,13) + + +--第二阶段:增、删、改、查 + +--1. 将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass=611234 where cardID='1010 3576 1234 567' + +--2. 用两条SQL语句实现孙悟空要取钱(取200)的操作, +--先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +--注意:先要将用户孙悟空的用户编号找到,再根据用户编号找到卡号, +--再根据银行卡号来插入交易记录和修改账上余额 +select * from transInfo +select customerID from userInfo where customerName='孙悟空' +select cardID from cardInfo where customerID=11 +select cardID from cardInfo where customerID=(select customerID from userInfo where customerName='孙悟空') + +insert into transInfo (cardID,transType,transMoney) values +('1010 3576 1234 567','支取',200) +update cardInfo set balance=balance-200 where cardID='1010 3576 1234 567' +select * from cardInfo + +--3. 用同上题一样的方法实现沙和尚存钱的操作(存300) +select customerID from userInfo where customerName='沙和尚' +select cardID from cardInfo where customerID=12 +insert into transInfo (cardID , transType,transMoney) values +('1010 3576 1212 117','存入',300) +update cardInfo set balance=balance+300 where cardID='1010 3576 1212 117' + +--4. 唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” +update cardInfo set IsReportLoss='是' where customerID='13' + +--5. 查询出最近10天开户的银行卡的信息 +select * from cardInfo where openDate>=dateadd(day,-10,getdate()) + +--6. 查询交易金额最大的银行卡信息,子查询实现 +select * from cardInfo where cardID=(select cardID from transInfo where transMoney=(select max(transMoney)from transInfo )) + +------7. 再交易信息表中,将总的交易金额,支取的交易金额, +------存入的交易金额查询出来并输出显示(可以用变量实现) +------ 显示效果: +------ 总交易金额:1400.00 +------ 支取交易金额:200.00 +------ 存入交易金额:1200.00 + diff --git "a/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery1.sql" "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9b5540470e5224b0cb903e9636b475c4cf3d38a1 --- /dev/null +++ "b/\347\254\254\344\270\203\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery1.sql" @@ -0,0 +1,103 @@ +use master +go + +create database ATM +on +( + name='ATM', + filename='D:\bank\ATM.mdf', + size=10mb, + maxsize=100mb, + filegrowth=15mb +) +log on +( + name='ATM_log', + filename='D:\bank\ATM_log.ldf', + size=10mb, + maxsize=100mb, + filegrowth=15mb +) +go + +use ATM +go + +create table userInfo +( + customerID int primary key identity(1,1), + customerName nvarchar(20) not null, + PID char(20) check(len(PID)=15 or len(PID)=18) unique not null, + telephone varchar(13) check(len(telephone)=13 and telephone like '____-________') not null, + address nvarchar(200) +) + +create table cardInfo +( + cardID varchar(18) primary key check(substring(cardID,1,9)='1010 3576' and len(cardID)=18 ) not null, + curType varchar(20) not null default('RMB'), + savingType char(20) check(savingType='活期' or savingType='定活两便' or savingType='定期'), + openDate datetime not null default(getdate()),--getdate()当前日期 + balance int not null check(balance>=1), + pass char(6) not null check(len(pass)=6) default('888888'), + IsReportLoss char(2) not null default('否') check(IsReportLoss='是' or IsReportLoss='否'), + customerID int references userInfo(customerID) not null +) + +create table transInfo +( + transId int primary key identity(1,1), + trabsDate datetime not null default(getdate()), + cardID varchar(18) not null references cardInfo(cardID), + transType char(4) not null check(transType='支取' or transType='存入'), + transMoney int check(transMoney>0) not null, + remark text +) + +--插入数据开户 +select * from userInfo +insert into userInfo values('孙悟空开户','123456789012345','0716-78989783','北京海淀') +insert into userInfo(customerName,PID,telephone) values('沙和尚开户','421345678912345678','0478-44223333') +insert into userInfo(customerName,PID,telephone) values('唐僧开户','321245678912345678','0478-44443333') + +--插入银行数据 +select * from cardInfo +insert into cardInfo(balance,savingType,cardID,customerID) +values (1000,'活期','1010 3576 1234 567',1) +insert into cardInfo(balance,savingType,cardID,customerID) +values (1,'定期','1010 3576 1212 117',2) +insert into cardInfo(balance,savingType,cardID,customerID) +values (1,'定期','1010 3576 1212 113',3) + +--1.将用户“孙悟空”开卡时的初始密码更改为“611234” +update cardInfo set pass='611234' where customerID=1 + +--2.用两条SQL语句实现孙悟空要取钱(取200)的操作,先向交易信息表插入一条取钱的交易记录,然后在孙悟空账上的余额减200 +select * from transInfo +insert into transInfo(cardID,transType,transMoney) +values ('1010 3576 1234 567','支取','200') +update cardInfo set balance=balance-200 where customerID=1 + +--3.用同上题一样的方法实现沙和尚存钱的操作(存300) +select * from transInfo +insert into transInfo(cardID,transType,transMoney) +values ('1010 3576 1212 117','存入','300') +update cardInfo set balance=balance+300 where customerID=2 + +--4.唐僧的卡丢了,需要挂失,将唐僧的银行卡的是否挂失字段的值改为“是” +update cardInfo set IsReportLoss='是' where customerID=3 + +--5.查询出最近10天开户的银行卡的信息 +select top 10 * from cardInfo where openDate>='2021-3-10' + +--查询交易金额最大的银行卡信息 +select top 1 * from cardInfo where balance>1 order by balance DESC + +--7.再交易信息表中,将总的交易金额,支取的交易金额,存入的交易金额查询出来并输出显示(可以用变量实现) + --显示效果: + --总交易金额:1400.00 + --支取交易金额:200.00 + --存入交易金额:1200.00 +select sum (transMoney)总交易金额 from transInfo +select sum (transMoney)支取交易金额 from transInfo where transType='支取' +select sum (transMoney)存入交易金额 from transInfo where transType='存入' \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery3sql.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery3sql.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b802f6a81e6fb2b35f50395dbe8d8d135bb104bb --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery3sql.sql" @@ -0,0 +1,64 @@ +use master +go + +create database Student +on +( + name='Student_mdf', + filename='D:\SQL代码\Student.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='Student_log', + filename='D:\SQL代码\Student_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go + +use Student +go + +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null +) + +create table Student +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) Check(StuSex in('男','女')), + StuBirthday date , + StuPhone nvarchar(11) unique + +) + +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int not null default(1) check(CourseCredit>=1 and CourseCredit<=5) +) + +create table Score +( + ScoreID int identity(1,1), + StuID int , + CourseID int , + Score decimal(5,2) unique not null +) + +alter table Student add StuAddress nvarchar(200) + +alter table Score add constraint FK_Score_Sccore primary key(ScoreID) + +alter table Score add constraint FK_Score_StuID foreign key(StuID) references Student (StuID) + +alter table Score add constraint FK_Score_Course foreign key(CourseID) references Course(CourseID) \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..71f6be9c34fea7f2ea25615704c1184dc10292b1 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery1.sql" @@ -0,0 +1,66 @@ +use master +create database Student +on primary +( + name=Student, + filename='D:\SQL作业\SQL作业2\Student.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) + +log on +( + name=Student_log, + filename='D:\SQL作业\SQL作业2\Student_log.ldf', + size=1mb, + maxsize=10mb, + filegrowth=10% +) +go + +use Student +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique(ClassName) not null +) +go + +use Student +create table Student +( + StuID int primary key identity(1,1), + ClassID int constraint FK_Class_ClassID references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) check(StuSex in('男','女')), + StuBirthday date, + StuPhone nvarchar(11) unique(StuPhone) +) +go + +use Student +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar(50) unique(CourseName) not null, + CourseCredit int default(1) check(CourseCredit >= 1 and CourseCredit <= 5) +) +go + +use Student +create table Score +( + ScoreID int primary key identity(1,1), + StuID int, + CourseID int, + Score decimal(5,2) unique(Score) not null +) +go + +use Student +alter table Student add StuAddress nvarchar(200) +alter table Score add foreign key (StuID) references Student(StuID) +alter table Score add foreign key (CourseID) references Course(CourseID) + + diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9ca38dc903c20bd9efa56d848cef4f02cb922635 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275.sql" @@ -0,0 +1,39 @@ +create database Student +go +use Student +go +create table Class +( + --字段名 数据类型 设置主键 自增 + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null, +) +create table Student +( + StuID int primary key identity(1,1), + --字段名 数据类型 设置外键关联class表的classID + ClassID int foreign key (ClassID) references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) check(StuSex='男' or StuSex='女') not null, + StuBirthday date, + StuPhone nvarchar(11) unique, +) +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int not null default('1') check(CourseCredit='1' or CourseCredit='1,2,3,4,5,') +) +create table Score +( + ScoreID int identity(1,1), + StuID int , + CourseID int, + Score decimal(5,2) unique not null, +) +--学生信息表加字段 +alter table Student add StuAddress nvarchar(200) +--成绩信息表添加约束 +alter table Score add constraint PK_Score_ScoreID primary key(ScoreID) +alter table Score add constraint FK_Score_StuID foreign key(StuID) references Student(StuID) +alter table Score add constraint FK_Score_CourseID foreign key(CourseID) references Course(CourseID) \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery3.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8168efbc35af32288fb83fc57df0a3069acee7f5 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery3.sql" @@ -0,0 +1,64 @@ +use master +go + +create database Student +on +( + name='Student', + filename='E:\Student.mdf', + size=10mb, + maxsize=50mb, + filegrowth=20% +) +log on +( + name='Student_log', + filename='E:\Student.mdf', + size=10mb, + maxsize=50mb, + filegrowth=20% +) +go + +use Student +go + +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null +) + +create table Student +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) Check(StuSex in('鐢','濂')), + StuBirthday date , + StuPhone nvarchar(11) unique + +) + +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int not null default(1) check(CourseCredit>=1 and CourseCredit<=5) +) + +create table Score +( + ScoreID int identity(1,1), + StuID int , + CourseID int , + Score decimal(5,2) unique not null +) + +alter table Student add StuAddress nvarchar(200) + +alter table Score add constraint FK_Score_Sccore primary key(ScoreID) + +alter table Score add constraint FK_Score_StuID foreign key(StuID) references Student (StuID) + +alter table Score add constraint FK_Score_Course foreign key(CourseID) references Course(CourseID) \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f7139ecbac9ca05acfd8812e841fee77a6262056 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" @@ -0,0 +1,61 @@ +Create database Student +on( +name = 'Student', +filename ='D:\sqlzuoye.mdf', +size = 5MB, +maxsize = 50MB, +filegrowth = 10MB +) +log on +( +name = 'Student_log', +filename ='D:\Student_log.ldf', +size = 5MB, +maxsize = 50MB, +filegrowth = 10MB +) +go + +use Student +go + +create table Class +( +ClassID int primary key identity(1,1), +ClassName nvarchar(20) unique not null +) + +create table Student +( +StuID int primary key identity(1,1), +ClassID int foreign key references Class(ClassID), +StuName nvarchar(20) not null, +StuSex nvarchar(20) default('男') check(StuSex='男' or StuSex='女'), +StuBirthday date, +StuPhone nvarchar(11) unique +) + +create table Course +( +CourseID int primary key identity(1,1), +CourseName nvarchar(50) unique not null, +CourseCredit int default('1') check(CourseCredit >= 1 and CourseCredit <= 5) +) + +create table Score +( +ScoreID int , +StuID int , +CourseID int , +Score decimal(5,2) unique not null +) + +use Student +go + +alter table Student add StuAddress nvarchar(200) +alter table Score alter column ScoreID int not null +alter table Score add constraint PK_ScoreID primary key(ScoreID) +alter table Score add constraint CK_StuID foreign key (StuID) references Student(StuID) +alter table Score add constraint DF_CourseID foreign key (CourseID) references Course(CourseID) + diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery11.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery11.sql" new file mode 100644 index 0000000000000000000000000000000000000000..55d3bc14e2406ebfe4d3f28785e92c5d9e4b3e1f --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery11.sql" @@ -0,0 +1,52 @@ +USE master +go +create database Student +on( +name='student', +filename='D:\', +size=5mb, +filegrowth=1mb, +maxsize=10mb +) +LOG ON( +name='student_log', +filename='D:\', +size=5mb, +maxsize=10mb, +filegrowth=1mb +) +go +use Student +go +create table Class +( +ClassID int primary key IDENTITY(1,1), +ClassName nvarchar(20) unique not null, + + +) +create table Student +( +StuID int primary key IDENTITY(1,1), +ClassID int foreign key references Class(ClassID), +StuName nvarchar(20) not null, +StuSex nvarchar(1) default('男') Check(StuSex ='男' or StuSex='女'), +StuBirthday date , +StuPhone nvarchar(11) unique, +StuAddress nvarchar(200) + +) +create table Coures +( +CourseID int primary key IDENTITY(1,1), +CourseName nvarchar(50) unique not null, +CourseCredit int default(1) check(CourseCredit=1 or CourseCredit=2 or CourseCredit=3 or CourseCredit=4 or CourseCredit=5) + +) +create table Score +( +ScoreID int primary key identity(1,1), +StuID int foreign key references Student(StuID), +CourseID int foreign key references Coures(CourseID), +Score decimal(5,2) unique not null +) \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery3.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4c622a677a254d0fc4f45088a71f5111f4287e55 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery3.sql" @@ -0,0 +1,54 @@ +USE master +go +create database Student +on( +name='student', +filename='D:\作业\SQL作业', +size=5mb, +filegrowth=1mb, +maxsize=10mb +) +LOG ON( +name='student_log', +filename='D:\作业\SQL作业', +size=5mb, +maxsize=10mb, +filegrowth=1mb +) +go +use Student +go +create table Class +( +ClassID int primary key IDENTITY(1,1), +ClassName nvarchar(20) unique not null, + + +) +create table Student +( +StuID int primary key IDENTITY(1,1), +ClassID int foreign key references Class(ClassID), +StuName nvarchar(20) not null, +StuSex nvarchar(1) default('男') Check(StuSex ='男' or StuSex='女'), +StuBirthday date , +StuPhone nvarchar(11) unique, +StuAddress nvarchar(200) + +) +create table Coures +( +CourseID int primary key IDENTITY(1,1), +CourseName nvarchar(50) unique not null, +CourseCredit int default(1) check(CourseCredit=1 or CourseCredit=2 or CourseCredit=3 or CourseCredit=4 or CourseCredit=5) + +) +create table Score +( +ScoreID int primary key identity(1,1), +StuID int foreign key references Student(StuID), +CourseID int foreign key references Coures(CourseID), +Score decimal(5,2) unique not null +) +insert into Class (ClassID ,ClassName )value('2044010220','张三') +insert into Student (StuID,ClassID ,StuName,StuSex,StuBirthday,StuPhone,StuAddress)value('1','2044010221','张三','男','20011030','100832232','无') \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5b94447d4b1187bf25ac0f71045021259aa4796e --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery1.sql" @@ -0,0 +1,62 @@ +锘緾reate database Student01 +on +( +fileName='D:\homework\Student01.mdf', +Name='Student01', +Size=1MB, +Maxsize=5MB, +filegrowth=1MB +) +log on +( +fileName='D:\homework\Student01_log.ldf', +Name='Student01_log.ldf', +size=1MB, +Maxsize=5MB, +filegrowth=1MB +) +go + +use Students +go + +create table ClassInfo +( +ClassID int primary key identity(1,1), +ClassName nvarchar(20) unique not null +) + +create table Student01 +( +StuID int primary key identity(1,1), +ClassID int foreign key references ClassInfo(ClassID), +StuName nvarchar not null, +StuSex nvarchar(1) check(StuSex='' or StuSex='女'), +StuBirthday date, +StuPhone nvarchar unique, +) + + +create table Course +( +CourseID int primary key identity(1,1), +CourseName nvarchar unique not null, +CourseCredit int default(1) check(CourseCredit>=1 or CourseCredit<=5), +) + + +create table Score +( +ScoreID int identity(1,1), +StuID int, +CourseID int , +Score decimal(5,2) unique not null +) +-- +alter table Student01 add StuAddress nvarchar +-- +alter table Score add constraint PK_Score_ScoreID primary key(ScoreID) +-- +alter table Score add constraint FK_Score_StuID foreign key(StuID) references Student(StuID) +-- +alter table Score add constraint FK_Score_CourseID foreign key(CourseID) references Course(CourseID) \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6cd8bb18e45d9bb44c2dba733e791a109c70413e --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" @@ -0,0 +1,72 @@ +create database Student +on +( + name='Student', --逻辑名称 + filename='F:\Student.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='Student_log', --逻辑名称 + filename='F:\Student_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) + +create table Class +( + ClassID int primary key identity(1,1), --主键并且自增 + ClassName nvarchar(20) unique not null, --约束:唯一,非空 +) + +create table Student +( + StuID int primary key identity(1,1), --主键且自增 + ClassID int foreign key references Class(ClassID), --添加外键 + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女'), --检查约束 + StuBirthday date , + StuPhone nvarchar(11) unique --唯一约束 + ) + + --这是地址字段 + alter table Student add StuAddress nvarchar(200) + + create table Course + ( + CourseID int primary key identity, --主键且自增 + CourseName nvarchar(50) unique not null, --唯一,非空 + CourseCredit int not null check(CourseCredit='1' or CourseCredit='1,2,3,4,5,') --非空,检查,取值1-5 + ) + + create table Score + ( + ScoreID int, + StuID int , + CourseID int , + Score decimal(5,2) + ) + + use Student + go + + --给ScoreID 添加非空 + alter table Score alter column ScoreID int not null + + --给Score添加主键 + alter table Score add constraint PK_ScoreID primary key(ScoreID) + + --给CourseID添加外键且关联 + alter table Score add constraint FK_CourseID_StuID foreign key (StuID) references Student(StuID) + + --删除Score约束 + alter table Score drop constraint UK_Score + + --给Score添加非空 + alter table Score alter column Score decimal not null + + --给Score添加唯一 + alter table Score add constraint UK_Score unique (Score) diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..345cf7ced909f37515586fa68c1fdec3b4a0bb9e --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" @@ -0,0 +1,62 @@ +Create database Student +on +( +fileName='D:\homework\Student.mdf', +Name='Student', +Size=1MB, +Maxsize=5MB, +filegrowth=1MB +) +log on +( +fileName='D:\homework\Student_log.ldf', +Name='Student_log.ldf', +size=1MB, +Maxsize=5MB, +filegrowth=1MB +) +go + +use Student +go + +create table ClassInfo +( +ClassID int primary key identity(1,1), +ClassName nvarchar(20) unique not null +) + +create table Student +( +StuID int primary key identity(1,1), +ClassID int foreign key references ClassInfo(ClassID), +StuName nvarchar not null, +StuSex nvarchar(1) check(StuSex='男' or StuSex='女'), +StuBirthday date, +StuPhone nvarchar unique, +) + + +create table Course +( +CourseID int primary key identity(1,1), +CourseName nvarchar unique not null, +CourseCredit int default(1) check(CourseCredit>=1 or CourseCredit<=5), +) + + +create table Score +( +ScoreID int identity(1,1), +StuID int, +CourseID int , +Score decimal(5,2) unique not null +) +-- +alter table Student add StuAddress nvarchar +-- +alter table Score add constraint PK_Score_ScoreID primary key(ScoreID) +-- +alter table Score add constraint FK_Score_StuID foreign key(StuID) references Student(StuID) +-- +alter table Score add constraint FK_Score_CourseID foreign key(CourseID) references Course(CourseID) diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8f61fbcbf4bd412c743c07bd8e4ec83685f7e358 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244.sql" @@ -0,0 +1,71 @@ +use master +go +create database Students +on primary +( + name='Students', + filename='D:\TEXT\Students.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='Students_log', + filename='D:\TEXT\Students_log.ldf', + size=5MB, + maxsize=100MB, + filegrowth=10Mb +) +go + +use Students +go + +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique(ClassName) not null +) +go + +use Students +go + +create table Student +( + StuID int primary key identity(1,1), + ClassID int references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nchar(1) default('男') check(StuSex in('男','女')), + StuBirthday date, + StuPhone nvarchar(11) unique(StuPhone) +) +go + +use Students +go + +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar unique(CourseName) not null, + CourseCredit int default(1) check(CourseCredit>=1 or CourseCredit<=5) +) +go +use Students +go +create table Score +( + ScoreID int identity(1,1), + StuID int, + CourseID int, + Score decimal(5,2) unique not null +) +go +use Students +go +alter table Student add StuAddress nvarchar +alter table Score add constraint PK_Score_ScoreID primary key(ScoreID) +alter table Score add constraint FK_Score_StuID foreign key(StuID) references Student(StuID) +alter table Score add constraint FK_Score_CourseID foreign key(CourseID) references Course(CourseID) \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d54a083d13241deab47e7d05e0e30892f74804a8 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" @@ -0,0 +1,66 @@ + + use master + go +create database Student +on +( + name='student', + filename='D:\test\Student.mdf' +) +log on +( + name='student_log', + filename='D:\test\Student.ldf' +) + go + use student + go + create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null, + + ) + + use Student + go + create table Student +( StuID int primary key identity(1,1), + ClassID int , + constraint FK_Student_ClassID foreign key(ClassID ) references Class(ClassID ), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex in('男','女')), + StuBirthday date, + StuPhone nvarchar(11) unique + ) + use Student + go + create table Course +( CourseID int primary key , + CourseName nvarchar(50) unique not null, +) +use Student +go +create table Score +( ScoreID int not null, + StuID int , + CourseID int , + Score decimal(5,2) +) + alter table Score add constraint Pk_Score_ScoreID primary key(ScoreID) + + alter table Score add constraint Fk_Score_StuID foreign key(StuID) references Student(StuID) + + alter table Score drop constraint Fk_Score_StuID + + alter table Student add StuAddress nvarchar(200) + + alter table Course drop constraint Ck_Course_CourseCredit + + alter table Course drop column CourseCredit + + alter table Course add CourseCredit int default(1)not null + + alter table Course add constraint Ik_Course_CourseCredit check(CourseCredit<6) + + \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.txt" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.txt" new file mode 100644 index 0000000000000000000000000000000000000000..0c954b558fb40b80a3d40e0ea7f984ae6f61585a Binary files /dev/null and "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.txt" differ diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery3.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9cb984fe182895337acb498f51f7e0f7eb546bc0 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery3.sql" @@ -0,0 +1,54 @@ +USE master +go +create database Student +on( +name='student', +filename='D:\sql数据库\Students.mdf', +size=5mb, +filegrowth=1mb, +maxsize=10mb +) +LOG ON( +name='student_log', +filename='D:\sql数据库\Students_log.ldf', +size=5mb, +maxsize=10mb, +filegrowth=1mb +) +go +use Student +go +create table Class +( +ClassID int primary key IDENTITY(1,1), +ClassName nvarchar(20) unique not null, + + +) +create table Student +( +StuID int primary key IDENTITY(1,1), +ClassID int foreign key references Class(ClassID), +StuName nvarchar(20) not null, +StuSex nvarchar(1) default('男') Check(StuSex ='男' or StuSex='女'), +StuBirthday date , +StuPhone nvarchar(11) unique, +StuAddress nvarchar(200) + +) +create table Coures +( +CourseID int primary key IDENTITY(1,1), +CourseName nvarchar(50) unique not null, +CourseCredit int default(1) check(CourseCredit=1 or CourseCredit=2 or CourseCredit=3 or CourseCredit=4 or CourseCredit=5) + +) +create table Score +( +ScoreID int primary key identity(1,1), +StuID int foreign key references Student(StuID), +CourseID int foreign key references Coures(CourseID), +Score decimal(5,2) unique not null +) +insert into Class (ClassID ,ClassName )value('2044010220','张三') +insert into Student (StuID,ClassID ,StuName,StuSex,StuBirthday,StuPhone,StuAddress)value('1','2044010221','张三','男','20011030','100832232','无') \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1144d34de0837103b38f3532fe7e106ddda350dd --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" @@ -0,0 +1,40 @@ +use master +go +create database Student +go +use Student +go +create table Class +( + ClassID int primary key identity(1,1), + ClassNam nvarchar(20) unique not null + +) +create table Student +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID) not null, + StuName nvarchar(20) not null, + Stusex nvarchar(1) check(Stusex='男'or Stusex='女'), + StuBirthday date , + StuPhone nvarchar(11) unique, +) +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar(50) unique, + CourseCredit int not null default('1') check(CourseCredit='1'or CourseCredit='1,2,3,4,5') +) +create table Score +( + ScoreID int identity(1,1), + StuID int , + CourseID int, + Score decimal(5,2) unique not null, +) +use Student +go +alter table Student add StuAddress nvarchar(200) +alter table Score add constraint PK_Score_ScoreID primary key (ScoreID) +alter table Score add constraint FK_Score_StuID foreign key (StuID) references Student (StuID) +alter table Score add constraint FK_Score_CourseID foreign key (CourseID) references Course (CourseID) \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5dcca3219871b56d4771a527939214fdd18d6014 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" @@ -0,0 +1,59 @@ +select * from sysdatabases +use master +go +create database Students +on primary +( + name='Students', + filename='C:\Users\33054\Desktop', + size=5mb, + maxsize=50mb, + filegrowth=10% + ) + log on + ( + name='Students_log', + filename='C:\Users\33054\Desktop', + size=5mb, + maxsize=50mb, + filegrowth=10% + ) + create table Class + + ( + + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null, + + ) + + create table Student + ( + + StuID int primary key identity(1,1), + SclassID int foreign key references Class( ClassID ), + StuName nvarchar(10) not null, + StuSex nchar(1) default('男') check(StuSex='男' or StuSex='女'), + StuBirthday date null, + StuPhone nvarchar(11) unique null, + StuAddress nvarchar(200) null, + + ) + + + create table Course + ( + CourseID int primary key identity, + CourseName nvarchar(50) unique not null, + CourseCredit int not null check(CourseCredit='1' or CourseCredit='1,2,3,4,5') + + ) + + create table Score + ( + ScoreID int primary key identity(1,1), + StuID int foreign key references Student( StuID) , + CourseID int foreign key references Course(CourseID), + Score decimal(5,2) unique not null, + ) + diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cbc9603ed207f66f54b22eb715dc6765c4b36420 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" @@ -0,0 +1,59 @@ +use master +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 Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique(ClassName) not null, +) +go + +create table Student +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex in('男','女')), + StuBirthday date, + StuPhone nvarchar(11) unique +) +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int default(1) check(CourseCredit>=1 and CourseCredit<=5) +) +create table Score +( + ScoreID int identity(1,1), + StuID int , + CourseID int , + Score decimal(5,2) unique not null +) +alter table Student add StuAddress nvarchar(200) + +alter table Score add constraint PK_Score_ScoreID primary key (ScoreID) + +alter table Score add constraint FK_Score_StuID foreign key (StuID) references Student(StuID) + +alter table Score add constraint FK_Score_CourseID foreign key (CourseID) references Course(CourseID) diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a895234f636c3c598c319273cb751d25a076ca38 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227.sql" @@ -0,0 +1,59 @@ +锘縮elect * from sysdatabases +use master +go +create database Students +on primary +( + name='Students', + filename='D:\test\Students.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% + ) + log on + ( + name='Students_log', + filename='D:\test\Students_log.lfd', + size=5mb, + maxsize=50mb, + filegrowth=10% + ) + create table Class + + ( + + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null, + + ) + + create table Student + ( + + StuID int primary key identity(1,1), + SclassID int foreign key references Class( ClassID ), + StuName nvarchar(10) not null, + StuSex nchar(1) default('鐢') check(StuSex='鐢' or StuSex='濂'), + StuBirthday date null, + StuPhone nvarchar(11) unique null, + StuAddress nvarchar(200) null, + + ) + + + create table Course + ( + CourseID int primary key identity, + CourseName nvarchar(50) unique not null, + CourseCredit int not null check(CourseCredit='1' or CourseCredit='1,2,3,4,5') + + ) + + create table Score + ( + ScoreID int primary key identity(1,1), + StuID int foreign key references Student( StuID) , + CourseID int foreign key references Course(CourseID), + Score decimal(5,2) unique not null, + ) + diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1620cc90a79ef6275f9fdd558afd3fa528fdee61 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" @@ -0,0 +1,54 @@ +use master +go + +create database Student +on +( + name = 'Student', + filename = 'D:\数据库\Student.mdf' +) + +log on +( + name = 'Student_log', + filename = 'D:\数据库\Student.ldf' +) + +use Student +go + +create table Class +( + ClassID int primary key, + ClassName nvarchar(20) unique not null +) + +create table Student +( + StuID int primary key identity (1,1), + ClassID int references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) check( StuSex in('男','女')), + StuBirthday date , + StuPhone nvarchar(11) unique, +) + +create table Course +( + CourseID int identity(1,1) primary key, + CourseName nvarchar(50) unique not null, + CourseCredit int not null default('1') check(CourseCredit in(1,2,3,4,5)) +) + +create table Score +( + ScoreID int identity(1,1), + StuID int , + CourseID int , + Score decimal(5,2) unique not null +) + +alter table Student add StuAddress nvarchar(20) +alter table Score add constraint pk_ScoreID primary key (ScoreID) +alter table Score add constraint fk_StuID foreign key (StuID) references Student(StuID) +alter table Score add constraint fk_CourseID foreign key (CourseID) references Course(CourseID) diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery3.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9ab8c5d3438941cf9a32411209bc7158424cd1ff --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery3.sql" @@ -0,0 +1,75 @@ +create database Student +on +( + name='Student', + filename='D:\SQL作业.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='Student_log', + filename='D:\SQL作业_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go + +use Student --切换数据库 +go +create table Class( + ClassId int primary key identity (1,1), + ClassName nvarchar(20) constraint unique_1 unique(ClassName) not null + --设置唯一约束(constraint(约束)+约束名(自己可辨识就可以)+unique(列名)) +) + +go + + +create table Student( + StuId int primary key identity (1,1), + ClassId int constraint [Fk_Students_ClassId] foreign key([ClassId]) references [Class]([ClassId]) not null, + --建表时创建外键关联约束(constraint +[约束名]+foreign key +([关联外键名])+references [外键表]([外键中的主键])) + StuName nvarchar(20) not null, + StuSex nvarchar(1) default'男'check (StuSex in('男','女')), + StuBirthday date null, --date为时间专用字段类型 + StuPhone nvarchar(11) constraint unique_2 unique(StuPhone) null, + + + ) + + go + + alter table Student add StuAddress nvarchar(20) + --添加列固定语句 alter table +表名 add +列名+字段类型 + + select * from Student--确定列是否添加成功 + +create table Course( + CourseId int primary key identity (1,1), + CourseName nvarchar(50) constraint unique_3 unique(CourseName) not null, + CourseCredit int default 1 check (CourseCredit>=1 and CourseCredit<=5) + + --default 为默认值 check括号内为取值范围,该列的取值范围表达为列名>=x and 列名<=y +) + +create table Score( + ScoreID int not null, + StuId int constraint [Fk_Score_StuId] foreign key([StuId]) references [Students]([StuId]) not null, + CourseId int not null, + Score decimal(5,2) constraint unique_4 unique(Score) not null + +) + +alter table Score add constraint PK_ScoreId primary key (ScoreId) + +--添加主键约束,alter table 表名 add constraint 约束名 primary key (字段名) + +alter table Score add constraint [Fk_Score_CourseId] foreign key([CourseId]) references [Course]([CourseId]) + +--建表后添加外键约束,alter table 表名 add constraint +[约束名]+foreign key +([关联外键名])+references [外键表]([外键中的主键]) + + + diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6cd8bb18e45d9bb44c2dba733e791a109c70413e --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" @@ -0,0 +1,72 @@ +create database Student +on +( + name='Student', --逻辑名称 + filename='F:\Student.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='Student_log', --逻辑名称 + filename='F:\Student_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) + +create table Class +( + ClassID int primary key identity(1,1), --主键并且自增 + ClassName nvarchar(20) unique not null, --约束:唯一,非空 +) + +create table Student +( + StuID int primary key identity(1,1), --主键且自增 + ClassID int foreign key references Class(ClassID), --添加外键 + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女'), --检查约束 + StuBirthday date , + StuPhone nvarchar(11) unique --唯一约束 + ) + + --这是地址字段 + alter table Student add StuAddress nvarchar(200) + + create table Course + ( + CourseID int primary key identity, --主键且自增 + CourseName nvarchar(50) unique not null, --唯一,非空 + CourseCredit int not null check(CourseCredit='1' or CourseCredit='1,2,3,4,5,') --非空,检查,取值1-5 + ) + + create table Score + ( + ScoreID int, + StuID int , + CourseID int , + Score decimal(5,2) + ) + + use Student + go + + --给ScoreID 添加非空 + alter table Score alter column ScoreID int not null + + --给Score添加主键 + alter table Score add constraint PK_ScoreID primary key(ScoreID) + + --给CourseID添加外键且关联 + alter table Score add constraint FK_CourseID_StuID foreign key (StuID) references Student(StuID) + + --删除Score约束 + alter table Score drop constraint UK_Score + + --给Score添加非空 + alter table Score alter column Score decimal not null + + --给Score添加唯一 + alter table Score add constraint UK_Score unique (Score) diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..685d22d98c3ec5b781e40d06b4d6260e3462648f --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" @@ -0,0 +1,51 @@ +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 Class +( + ClassID int primary key identity(1,1) not null, + ClassName nvarchar(20) unique not null +) +create table Student +( + StuID int primary key identity(1,1) not null, + ClassID int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check (StuSex='男' or StuSex='女'), + StuBirthday date, + StuPhone nvarchar(11) unique +) +create table Course +( + CourseID int primary key identity(1,1) not null, + CourseName nvarchar(50) unique not null, + CourseCredit int default(1) check (CourseCredit>1 and CourseCredit<5) not null +) +create table Score +( + ScoreID int identity(1,1) not null, + StuID int , + CourseID int , + Score decimal(5,2) unique not null +) +alter table Student add StuAddress nvarchar(200) +alter table Score add constraint FK_Score_StuID foreign key (StuID) references Student(StuID) +alter table Score add constraint FK_Score_CourseID foreign key (CourseID) references Course(CourseID) +alter table Score add constraint FK_Score_ScoreID primary key (ScoreID) \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f78971ae53d8f7c825a1bdbe8d351f39083d1341 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" @@ -0,0 +1,72 @@ +create database Student +on +( + name='Student', --逻辑名称 + filename='D:\Student.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='StuAge_log', --逻辑名称 + filename='F:\StuAge_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) + +create table Class +( + ClassID int primary key identity(1,1), --主键并且自增 + ClassName nvarchar(20) unique not null, --约束:唯一,非空 +) + +create table Student +( + StuID int primary key identity(1,1), --主键且自增 + ClassID int foreign key references Class(ClassID), --添加外键 + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女'), --检查约束 + StuBirthday date , + StuPhone nvarchar(11) unique --唯一约束 + ) + + --这是地址字段 + alter table Student add StuAddress nvarchar(200) + + create table Course + ( + CourseID int primary key identity, --主键且自增 + CourseName nvarchar(50) unique not null, --唯一,非空 + CourseCredit int not null check(CourseCredit='1' or CourseCredit='1,2,3,4,5,') --非空,检查,取值1-5 + ) + + create table Score + ( + ScoreID int, + StuID int , + CourseID int , + Score decimal(5,2) + ) + + use mioer + go + + --给ScoreID 添加非空 + alter table Score alter column ScoreID int not null + + --给Score添加主键 + alter table Score add constraint PK_ScoreID primary key(ScoreID) + + --给CourseID添加外键且关联 + alter table Score add constraint FK_CourseID_StuID foreign key (StuID) references Student(StuID) + + --删除Score约束 + alter table Score drop constraint UK_Score + + --给Score添加非空 + alter table Score alter column Score decimal not null + + --给Score添加唯一 + alter table Score add constraint UK_Score unique (Score) \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..18daf372183c46b7941815aebc794d346e565f58 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" @@ -0,0 +1,63 @@ +use master +go +create database Students +on( + name='Students', + filename='C:\TEXT\Students.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) +log on( + name='Students_log', + filename='C:\TEXT\Students_log.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) +go +use Students +go +create table Class +( + ClassID int not null , + ClassName nvarchar(20) unique not null, +) +create table Student +( + StuID int not null , + ClassID int, + StuName nvarchar(20) not null, + StuSex nvarchar(1) check(StuSex='男' or StuSex='女'), + StuBirthday date, + StuPhone nvarchar(11) unique, + +) +create table Course +( + CourseID int not null, + CourseName nvarchar(50) unique not null, + CourseCredit int default(1) not null, +) +create table Score +( + ScoreID int not null, + StuID int , + CourseID int, + Score decimal(5,2) unique not null, +) +use Students +go +alter table Student add StuAddress nvarchar(200) +alter table Class add constraint PK_Class_ClassID primary key(ClassID) +alter table Student add constraint PK_Student_StuID primary key(StuID) +alter table Course add constraint PK_Course_CourseID primary key(CourseID) +alter table Score add constraint PK_Score_ScoreID primary key(ScoreID) +alter table Student add constraint FK_Student_ClassID foreign key(StuID) references Class(ClassID) +alter table Score add constraint FK_Score_StuID foreign key(StuID) references Student(StuID) +alter table Score add constraint FK_Score_CourseID foreign key(CourseID) references Course(CourseID) + + + + + diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f22958b03df49d3f1b452bdbfe501c24b9689c2c --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" @@ -0,0 +1,66 @@ +use master +go + +create database Students +on +( + name='Students', + filename='D:\sql数据库\Students.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='Students_log', + filename='D:\sql数据库\Students_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go + +use Students +go + +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) not null +) + +create table Student +( + StuID int primary key identity(1,1), + ClassID int references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女'), + StuBirthday date, + StuPhone nvarchar(11) unique, + StuAddress nvarchar(200), +) + +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int default(1) check(1=1 and CourseCredit<=5) + + --default 为默认值 check括号内为取值范围,该列的取值范围表达为列名>=x and 列名<=y +) + +create table Score( + ScoreID int not null, + StuId int constraint [Fk_Score_StuId] foreign key([StuId]) references [Students]([StuId]) not null, + CourseId int not null, + Score decimal(5,2) constraint unique_4 unique(Score) not null + +) + +alter table Score add constraint PK_ScoreId primary key (ScoreId) + +--添加主键约束,alter table 表名 add constraint 约束名 primary key (字段名) + +alter table Score add constraint [Fk_Score_CourseId] foreign key([CourseId]) references [Course]([CourseId]) + +--建表后添加外键约束,alter table 表名 add constraint +[约束名]+foreign key +([关联外键名])+references [外键表]([外键中的主键]) + + + + diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ed0beb052c91111b2687c18a04e444c47621cdf1 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" @@ -0,0 +1,62 @@ +Create database Student +on +( +fileName='D:\homework\Student.mdf', +Name='Student', +Size=1MB, +Maxsize=5MB, +filegrowth=1MB +) +log on +( +fileName='D:\homework\Student_log.ldf', +Name='Student_log.ldf', +size=1MB, +Maxsize=5MB, +filegrowth=1MB +) +go + +use Student +go + +create table ClassInfo +( +ClassID int primary key identity(1,1), +ClassName nvarchar(20) unique not null +) + +create table Student +( +StuID int primary key identity(1,1), +ClassID int foreign key references ClassInfo(ClassID), +StuName nvarchar not null, +StuSex nvarchar(1) check(StuSex='男' or StuSex='女'), +StuBirthday date, +StuPhone nvarchar unique, +) + + +create table Course +( +CourseID int primary key identity(1,1), +CourseName nvarchar unique not null, +CourseCredit int default(1) check(CourseCredit>=1 or CourseCredit<=5), +) + + +create table Score +( +ScoreID int identity(1,1), +StuID int, +CourseID int , +Score decimal(5,2) unique not null +) +-- +alter table Student add StuAddress nvarchar +-- +alter table Score add constraint PK_Score_ScoreID primary key(ScoreID) +-- +alter table Score add constraint FK_Score_StuID foreign key(StuID) references Student(StuID) +-- +alter table Score add constraint FK_Score_CourseID foreign key(CourseID) references Course(CourseID) diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c7a03953bf4948c95b82e503b10d6aaa2ecb17e8 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" @@ -0,0 +1,68 @@ +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 Class +( +ClassID int primary key identity(1,1), +ClassName nvarchar(20) unique(ClassName) not null, +) +go + +use Student +create table Student +( + StuID int primary key identity(1,1), + ClassID int constraint FK_Class_ClassID references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) Check(StuSex in('男','女')), + StuBrithday date, + StuPhone nvarchar(11) unique(StuPhone) +) +go + +use Student +create table Course +( + CourseID int primary key identity(1,1), + ScoreID int, + CourseCredit int default(1) check(CourseCredit >= 1 and CourseCredit <= 5) +) +go + +use Student +create table Score +( + ScoreID int identity(1,1), + StuID int, + CourseID int, + Score decimal(5,2) unique(Score) +) +go + +use Student +alter table Student add StuAddress nvarchar(200) +alter table Score add primary key(ScoreID) +alter table Score add foreign key (StuID) references Student(StuID) +alter table Score add foreign key (CourseID) references Course(CourseID) \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye01.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye01.sql" new file mode 100644 index 0000000000000000000000000000000000000000..eae353a8a5708f0c5c91d1f1e9d920e9a2f0588f --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye01.sql" @@ -0,0 +1,54 @@ +use master +go + +create database zuoye01 +on +( +name='zuoye01', +filename='D:\test\zuoye.mdf', +size=5MB, +maxsize=50MB, +filegrowth=10% +) +log on +( +name='zuoye01_log', +filename='D:\test\zuoye_log.ldf', +size=5MB, +maxsize=50MB, +filegrowth=10% +) +go + +use zuoye01 +go +create table Class +( +ClassID int primary key identity(1,1), +ClassName nvarchar(20) not null unique, +) +create table student +( +StuID int primary key identity(1,1), +ClassID int references Class(ClassID), +StuName nvarchar(20) not null, +StuSex char(1) default('男') check(StuSex='男' or StuSex='女'), +StuBirthday date, +StuPhone char(11) +) + +create table Course +( +CourseID int primary key identity(1,1), +CourseName nvarchar(50) unique not null, +CourseCredit int unique not null +) +create table Score +( +ScoreID int , +StuID int references Class(ClassID), +CourseID int references Course(CourseID), +Score decimal not null unique, +) +alter table student add StuAddress int not null + \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b300d90d262515d45fe62b2c0659e7c98fb3f2d6 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" @@ -0,0 +1,78 @@ +use master +create database Student +on primary +( + name=Student, + filename='D:\Document\MSSQLDatabase\Student\Student.mdf', + size=5MB, + maxsize=50MB, + filegrowth=1MB +) +log on +( + name=Student_Log, + filename='D:\Document\MSSQLDatabase\Student\Student_log.ldf', + size=1MB, + maxsize=10MB, + filegrowth=10% +) +go + +use Student +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique(ClassName) not null +) +go + +use Student +create table Student +( + StuID int primary key identity(1,1), + ClassID int constraint FK_Class_ClassID references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) Check(StuSex in('男','女')), + StuBrithday date, + StuPhone nvarchar(11) unique(StuPhone) +) +go + +use Student +create table Course +( + CourseID int primary key identity(1,1), + ScoreID int, + CourseCredit int default(1) check(CourseCredit >= 1 and CourseCredit <= 5) +) +go + +use Student +create table Score +( + ScoreID int identity(1,1), + StuID int, + CourseID int, + Score decimal(5,2) unique(Score) +) +go + +use Student +alter table Student add StuAddress nvarchar(200) +alter table Score add primary key(ScoreID) +alter table Score add foreign key (StuID) references Student(StuID) +alter table Score add foreign key (CourseID) references Course(CourseID) + +insert Student(StuName,StuSex,StuPhone) +values +('张三','男','13067371579') + + +select * +from Student + +select * +from class + +select * +from Score \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e1ff9de49cbc0981a55d9f0d1ee2135bf8517458 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" @@ -0,0 +1,55 @@ +use master +go + +create database Student +on +( + name='Student', + filename='D:\Student.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='Student-log', + filename='D:\Student-log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go +use Student +go +create table Class +( + ClassID int primary key identity(1,1), + ClassNam nvarchar(20) unique not null + +) +create table Student +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID) not null, + StuName nvarchar(20) not null, + Stusex nvarchar(1) check(Stusex='男'or Stusex='女'), + StuBirthday date , + StuPhone nvarchar(11) unique, +) +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar(50) unique, + CourseCredit int not null default('1') check(CourseCredit='1'or CourseCredit='1,2,3,4,5') +) +create table Score +( + ScoreID int identity(1,1), + StuID int , + CourseID int, + Score decimal(5,2) unique not null, +) +alter table Student add StuAddress nvarchar(200) +alter table Score add constraint PK_Score_ScoreID primary key (ScoreID) +alter table Score add constraint FK_Score_StuID foreign key (StuID) references Student (StuID) +alter table Score add constraint FK_Score_CourseID foreign key (Course) references Course (CourseID) \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery2.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ffb82a5b3214355f25ccac44752b4d30f5316983 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery2.sql" @@ -0,0 +1,64 @@ +锘縰se master +go + +create database Student +on +( + name='Student', + filename='D:\SQL\Student.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='Student_log', + filename='D:\SQL\Student_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go + +use Student +go + +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null +) + +create table Student +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) Check(StuSex in('','女')), + StuBirthday date , + StuPhone nvarchar(11) unique + +) + +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int not null default(1) check(CourseCredit>=1 and CourseCredit<=5) +) + +create table Score +( + ScoreID int identity(1,1), + StuID int , + CourseID int , + Score decimal(5,2) unique not null +) + +alter table Student add StuAddress nvarchar(200) + +alter table Score add constraint FK_Score_Sccore primary key(ScoreID) + +alter table Score add constraint FK_Score_StuID foreign key(StuID) references Student (StuID) + +alter table Score add constraint FK_Score_Course foreign key(CourseID) references Course(CourseID) \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..884afee6c3d67d33923a06a80a974c2bb4abc99b --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" @@ -0,0 +1,78 @@ +use master +create database Student +on primary +( + name=Student, + filename='D:\Document\MSSQLDatabase\Student\Student.mdf', + size=5MB, + maxsize=50MB, + filegrowth=1MB +) +log on +( + name=Student_Log, + filename='D:\Document\MSSQLDatabase\Student\Student_log.ldf', + size=1MB, + maxsize=10MB, + filegrowth=10% +) +go + +use Student +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique(ClassName) not null +) +go + +use Student +create table Student +( + StuID int primary key identity(1,1), + ClassID int constraint FK_Class_ClassID references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) Check(StuSex in('男','女')), + StuBrithday date, + StuPhone nvarchar(11) unique(StuPhone) +) +go + +use Student +create table Course +( + CourseID int primary key identity(1,1), + ScoreID int, + CourseCredit int default(1) check(CourseCredit >= 1 and CourseCredit <= 5) +) +go + +use Student +create table Score +( + ScoreID int identity(1,1), + StuID int, + CourseID int, + Score decimal(5,2) unique(Score) +) +go + +use Student +alter table Student add StuAddress nvarchar(200) +alter table Score add primary key(ScoreID) +alter table Score add foreign key (StuID) references Student(StuID) +alter table Score add foreign key (CourseID) references Course(CourseID) + +insert Student(StuName,StuSex,StuPhone) +values +('张三','男','13067371579') + + +select * +from Student + +select * +from class + +select * +from Score \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237.sql" new file mode 100644 index 0000000000000000000000000000000000000000..46026dfda9c84198c47fc984dbc11974eef65039 Binary files /dev/null and "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237.sql" differ diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/.keep" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2723ce1a3362388ae6a81b7f569d1d1d7408b093 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.sql" @@ -0,0 +1,72 @@ +use master +go + +create database Student +on +( + name='Student', + filename='D:\test\Student.mdf', + size=5MB, + maxsize=15MB, + filegrowth=10% +) +log on +( + name='Student_log', + filename='D:\test\Student_log.ldf', + size=5MB, + maxsize=15MB, + filegrowth=10% +) +go + +use Student +go + +create table Class +( + ClassID int identity(1,1), + ClassName nvarchar(20) unique not null +) +create table Student +( + StuID int identity(1,1), + ClassID int, + StuName nvarchar(20) not null, + StuSex nvarchar(1) check(StuSex in('男','女')), + StuBirthday date, + StuPhone nvarchar(11) unique +) +create table Course +( + CourseID int identity(1,1), + CourseName nvarchar(50) unique, + CoueseCredit int default(1) check(CoueseCredit in(1,2,3,4,5)) +) +create table Score +( + ScoreID int identity(1,1), + StuID int , + CourseID int, + Score decimal(5,2) unique +) +go + +use Student +go + +alter table Student add StuAddress nvarchar(200) + +alter table Class add constraint PK_Class_ClassID primary key(ClassID) + +alter table Student add constraint PK_Student_StuID primary key(StuID) + +alter table Student add constraint FK_Student_ClassName foreign key(StuID) references Class(ClassID) + +alter table Course add constraint PK_Course_CourseID primary key(CourseID) + +alter table Score add constraint PK_Score_ScoreID primary key(ScoreID) + +alter table Score add constraint FK_Score_StuID foreign key(StuID) references Student(StuID) + +alter table CourseID add constraint FK_Score_CourseID foreign key(CourseID) references Course(CourseID) \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241.sql" new file mode 100644 index 0000000000000000000000000000000000000000..253b0e082e4dc8adccc04ab10207c71a0046e507 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241.sql" @@ -0,0 +1,50 @@ +use master +go +create database Students02 +on +( + name='Students02', + filename='C:\test\Students02.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% + ) + + log on + ( + name='Students02_log', + filename='C:\test\Students02_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% + ) + go + use Students02 + go + create table ClassInfo + (ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null, + ) + create table StudentInfo + (StuID int primary key identity(1,1), + ClassID int foreign key references ClassInfo(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default ('男')check(StuSex='男' or StuSex='女') not null, + StuBirthday date, + StuPhone nvarchar(11) unique, + ) + create table CourseInfo + (CourseID int primary key identity (1,1), + CourseName nvarchar(50) unique, + CourseCredit int default('1') check(CourseCredit='1' or CourseCredit='1,2,3,4,5,')not null, + ) +create table ScoreInfo +(ScoreID int identity (1,1), + StuID int , + CourseID int , + Score decimal(5,2) unique not null, +) +alter table StudentInfo add StuAddress nvarchar(200) +alter table ScoreInfo add constraint PK_ScoreInfo_ScoreID primary key(ScoreID) +alter table ScoreInfo add constraint FK_ScoreInfo_StuID foreign key(StuID) references ScoreInfo(StuID) +alter table ScoreInfo add constraint FK_ScoreInfo_CourseID foreign key(CourseID) references CourseInfo(CourseID) diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d54a083d13241deab47e7d05e0e30892f74804a8 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" @@ -0,0 +1,66 @@ + + use master + go +create database Student +on +( + name='student', + filename='D:\test\Student.mdf' +) +log on +( + name='student_log', + filename='D:\test\Student.ldf' +) + go + use student + go + create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null, + + ) + + use Student + go + create table Student +( StuID int primary key identity(1,1), + ClassID int , + constraint FK_Student_ClassID foreign key(ClassID ) references Class(ClassID ), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex in('男','女')), + StuBirthday date, + StuPhone nvarchar(11) unique + ) + use Student + go + create table Course +( CourseID int primary key , + CourseName nvarchar(50) unique not null, +) +use Student +go +create table Score +( ScoreID int not null, + StuID int , + CourseID int , + Score decimal(5,2) +) + alter table Score add constraint Pk_Score_ScoreID primary key(ScoreID) + + alter table Score add constraint Fk_Score_StuID foreign key(StuID) references Student(StuID) + + alter table Score drop constraint Fk_Score_StuID + + alter table Student add StuAddress nvarchar(200) + + alter table Course drop constraint Ck_Course_CourseCredit + + alter table Course drop column CourseCredit + + alter table Course add CourseCredit int default(1)not null + + alter table Course add constraint Ik_Course_CourseCredit check(CourseCredit<6) + + \ No newline at end of file diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ed0beb052c91111b2687c18a04e444c47621cdf1 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" @@ -0,0 +1,62 @@ +Create database Student +on +( +fileName='D:\homework\Student.mdf', +Name='Student', +Size=1MB, +Maxsize=5MB, +filegrowth=1MB +) +log on +( +fileName='D:\homework\Student_log.ldf', +Name='Student_log.ldf', +size=1MB, +Maxsize=5MB, +filegrowth=1MB +) +go + +use Student +go + +create table ClassInfo +( +ClassID int primary key identity(1,1), +ClassName nvarchar(20) unique not null +) + +create table Student +( +StuID int primary key identity(1,1), +ClassID int foreign key references ClassInfo(ClassID), +StuName nvarchar not null, +StuSex nvarchar(1) check(StuSex='男' or StuSex='女'), +StuBirthday date, +StuPhone nvarchar unique, +) + + +create table Course +( +CourseID int primary key identity(1,1), +CourseName nvarchar unique not null, +CourseCredit int default(1) check(CourseCredit>=1 or CourseCredit<=5), +) + + +create table Score +( +ScoreID int identity(1,1), +StuID int, +CourseID int , +Score decimal(5,2) unique not null +) +-- +alter table Student add StuAddress nvarchar +-- +alter table Score add constraint PK_Score_ScoreID primary key(ScoreID) +-- +alter table Score add constraint FK_Score_StuID foreign key(StuID) references Student(StuID) +-- +alter table Score add constraint FK_Score_CourseID foreign key(CourseID) references Course(CourseID) diff --git "a/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery1.sql" "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2daba26514e1cc2dda766b1886f7db94e8109865 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery1.sql" @@ -0,0 +1,53 @@ +use master +go + +create database Student +on +( + name='Student', + filename='F:\Student.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='Student_log', + filename='F:\Student_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go + +use Student +go + +create table Class +( + ClassID int primary key identity(1,1) not null, + ClassName nvarchar(20) unique not null-- +) +create table Student +( + StuID int primary key identity(1,1) not null, + ClassID int references Class(ClassID) ,-- + StuName nvarchar(20) not null, + StuSex nvarchar(1) Check( StuSex in('男','女')), + StuBirthday date , + StuPhone nvarchar(11) unique,-- + StuAddress nvarchar(20)--!! +) +create table Course +( + CourseID int primary key identity(1,1) not null, + CourseName nvarchar(50) unique,-- + CourseCredit int default(1) check(CourseCredit>=1 or CourseCredit<=5) -- +) +create table Score--!! +( + ScoreID int primary key identity(1,1)not null,--!! + StuID int references Student(StuID) ,-- + CourseID int references Course(CourseID),-- + Score decimal(5,2) unique not null-- +) \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/.keep" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245.txt" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245.txt" new file mode 100644 index 0000000000000000000000000000000000000000..b465c44465aba02393964f08a48a46ae80626ea6 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245.txt" @@ -0,0 +1,43 @@ +select * from ClassInfo --班级表 +select * from CourseInfo --科目信息表 +select * from Scores --成绩表 +select * from StuInfo --学生信息表 +--统计每个班的男生数 +select ClassId 班级, Count(*)男生数 from StuInfo + group by ClassId ,StuSex having StuSex='男' + +--统计每个班的男、女生数 +select ClassId 班级,StuSex 性别 , Count(*) 人数 from StuInfo + group by ClassId ,StuSex + +--统计每个班的福建人数 +select ClassId 班级, StuProvince,Count(*)人数 from StuInfo + group by ClassId ,StuProvince having StuProvince='福建省' + +--统计每个班的各个省的总人数 +select ClassId 班级, StuProvince 省份,Count(*)人数 from StuInfo + group by ClassId ,StuProvince order by ClassId + +--统计每个省的女生数 +select ClassId 班级 , StuSex 性别 , StuProvince 省份 , Count(*) 人数 from StuInfo + group by ClassId , StuProvince,StuSex having StuSex='女' order by ClassId + +--统计每个省的男、女生数 +select ClassId 班级 ,StuProvince 省份 ,StuSex 性别 ,Count(*)人数 from StuInfo + group by ClassId ,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 CourseId 科目ID ,max(Score) 最高分,min(Score) 最低分 from Scores + group by CourseId + +--统计出每个学生的各门成绩的平均分 +select StuId 学生,CourseId 科目名,avg(Score) 平均分 from Scores + group by StuId,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/\346\237\245\350\257\242.txt" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/\346\237\245\350\257\242.txt" new file mode 100644 index 0000000000000000000000000000000000000000..9348027057a5ca64c878165c7c1ef9d7a004c41b --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/\346\237\245\350\257\242.txt" @@ -0,0 +1,41 @@ + +--统计每个班的男生数 + +select ClassId,StuSex, count(*) from StuInfo where StuSex='男' group by ClassId,StuSex + +--统计每个班的男、女生数 + +select ClassId,Stusex, count(*)人数 from StuInfo group by ClassId,StuSex order by ClassId + +--统计每个班的福建人数 +select ClassId,StuProvince,count(*)人数 from StuInfo where StuProvince='福建省' group by ClassId,StuProvince + +--统计每个班的各个省的总人数 +select ClassId,StuProvince,count(StuProvince)总人数 from StuInfo group by ClassId,StuProvince + +--统计每个省的女生数 +select StuProvince,count(*)女生人数 from StuInfo where StuSex='女' group by StuProvince + +--统计每个省的男、女生数 +select StuProvince 省,StuSex,count(*)人数 from StuInfo group by StuProvince,StuSex order by StuProvince + +--统计每个学生的考试总分、平均分 +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 CourseId,max(Score)最高分,min(Score)最低分 from Scores group by CourseId order by StuId + + + +--统计出每个学生的各门成绩的平均分 +select StuId,CourseId,avg(Score)平均分 from Scores group by StuId,CourseId order by StuId + + +select * from StuInfo + +select * from Scores \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery2.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d6000e8ae4cb3e38b47266b1ed89392d96858b1b --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/SQLQuery2.sql" @@ -0,0 +1,43 @@ +use master +go +use TestDB +go + +----1.统计每个班的男生数 +select * from StuInfo +select ClassId,count(*) 男生数 from StuInfo where stusex='男' group by ClassId + + +----2.统计每个班的男、女生数 +select * from StuInfo +select ClassId,StuSex,count(*) from StuInfo group by StuSex,ClassId + +----3.统计每个班的福建省人数 +select * from StuInfo +select ClassId,count(*) 福建人数 from StuInfo where stuprovince='福建省' group by ClassId + +----4.统计每个班的各个省的总人数 +select * from StuInfo +select ClassId,Stuprovince,count(*)各个省总人数 from StuInfo group by Stuprovince,ClassId + +----5.统计每个省的女生数 +select Stuprovince,count(*)女生数 from StuInfo where stusex='女' group by Stuprovince + +----6.统计每个省的男、女生数 +select Stuprovince,StuSex,count(*) from StuInfo group by StuSex,Stuprovince + +----7.--统计每个学生的考试总分、平均分 +select * from Scores +select StuId,sum(Score)考试总分,avg(Score)平均分 from Scores group by StuId + + +----8.统计出考试总分大于620的学生的考试总分 +select StuId,sum(Score)考试总分 from Scores group by StuId having sum(Score)>620 + + +----9.统计出每门考试成绩最高分和最低分 +select * from Scores +select StuId,max(Score)成绩最高分,min(Score)成绩最低分 from Scores group by StuId + +----10.统计出每个学生的各门成绩的平均分 +select StuId,courseID avg(Score) from Scores group by StuId,courseID \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery01.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery01.sql" new file mode 100644 index 0000000000000000000000000000000000000000..61a8f331307b2a3926d65649f6cb9a9f4a93026f --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery01.sql" @@ -0,0 +1,33 @@ +--缁熻姣忎釜鐝殑鐢风敓鏁 +select ClassId,Stusex, COUNT(*)鐢风敓鏁 from StuInfo where Stusex='鐢' group by +ClassId,StuSex order by ClassId + +--缁熻姣忎釜鐝殑鐢枫佸コ鐢熸暟 +select ClassId,Stusex, COUNT(*)鐢峰コ鐢熸暟 from StuInfo group by +ClassId,StuSex order by ClassId + +--缁熻姣忎釜鐝殑绂忓缓浜烘暟 +select ClassId,StuProvince,COUNT(*)绂忓缓浜烘暟 from StuInfo where StuProvince='绂忓缓鐪' +group by ClassId,StuProvince order by ClassId + +--缁熻姣忎釜鐝殑鍚勪釜鐪佺殑鎬讳汉鏁 +select ClassId,StuProvince,COUNT(*)鎬讳汉鏁 from StuInfo group by ClassId,StuProvince + +--缁熻姣忎釜鐪佺殑濂崇敓鏁 +select StuProvince,StuSex,COUNT(*)浜烘暟 from StuInfo where StuSex='濂' group by StuProvince,StuSex + +--缁熻姣忎釜鐪佺殑鐢枫佸コ鐢熸暟 +select StuProvince,StuSex,COUNT(*)浜烘暟 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 CourseId,max(Score)鏈楂樺垎,min(Score)鏈浣庡垎 from Scores group by CourseId + +--缁熻鍑烘瘡涓鐢熺殑鍚勯棬鎴愮哗鐨勫钩鍧囧垎 +select StuId,CourseId,AVG(Score)骞冲潎鍒 from Scores group by StuId,CourseId order by StuId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\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/\347\254\254\344\271\235\346\254\241\350\257\276\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/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b77a638cc08b25280355898fb6227fc701404a26 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery2.sql" @@ -0,0 +1,22 @@ +use TestDB +go + +select ClassId 班级,count(*)男生人数 from StuInfo where StuSex = '男' group by ClassId + +select ClassId 班级,count(*)人数 from StuInfo group by ClassId + +select ClassId 班级,COUNT(*)人数 from StuInfo where StuProvince = '福建省' group by ClassId + +select ClassId 班级,StuProvince 省份,count(*) from StuInfo group by ClassId,StuProvince + +select StuProvince 省份,count(*)女生人数 from StuInfo where StuSex ='女' group by StuProvince + +select Stuprovince 省份,count(*) 人数 from StuInfo group by Stuprovince + +select StuId 学号,AVG(Score) 平均分,sum(Score) 总分 from Scores group by StuId + +select StuId 学号,sum(Score) 总分 from Scores group by StuId having sum(Score)>620 + +select CourseId 学科编号,max(Score) 最高分,min(Score) 最低分 from Scores group by CourseId + +select StuId 学号,CourseId 科目编号,avg(Score) 平均分 from Scores group by StuId,CourseId diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery6.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d2949500ea26b70d9850c1c3c7978aad458cb392 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery6.sql" @@ -0,0 +1,26 @@ +use TestDB +select * from ClassInfo +select * from CourseInfo +select * from Scores +select * from StuInfo + +--统计每个班的男生数 +select ClassId 班级, count(*)男生数 from StuInfo where StuSex='男' group by ClassId +--统计每个班的男、女生数 +select ClassId 班级,count(*)人数 from StuInfo group by ClassId +--统计每个班的福建人数 +select ClassId 班级,StuSex 人数,count(*) from StuInfo where StuProvince='福建省' group by ClassId,StuSex +--统计每个班的各个省的总人数 +select ClassId 班级,StuProvince 省份,count(*)总人数 from StuInfo group by ClassId,StuProvince +--统计每个省的女生数 +select StuProvince 省份,count(*)女生人数 from StuInfo where StuSex='女' group by StuProvince +--统计每个省的男、女生数 +select StuProvince 省份,StuSex 人数,count(*) from StuInfo group by StuProvince,StuSex +--统计每个学生的考试总分、平均分 +select StuId 学号,AVG(Score) 平均分,sum(Score)总分 from Scores group by StuId +--统计出考试总分大于620的学生的考试总分 +select StuId 学号,sum(Score)总分 from Scores group by StuId having sum(Score)>620 +--统计出每门考试成绩最高分和最低分 +select CourseId 学科编号,max(Score) 最高分,min(Score) 最低分 from Scores group by CourseId +--统计出每个学生的各门成绩的平均分 +select StuId 学号, CourseId 学科编号, AVG(Score) 平均分 from Scores group by StuId,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d586a1ae4ad4335f7c95cb8d6171d6d461448d5b --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" @@ -0,0 +1,23 @@ +--统计每个班的男生数 +select * from StuInfo +select ClassId,StuSex,count(StuSex) from StuInfo where StuSex='男' group by StuSex,ClassId +--统计每个班的男、女生数 +select ClassId,StuSex,count(StuSex) from StuInfo group by StuSex,ClassId +--统计每个班的福建人数 +select ClassId 班,count(StuProvince)人数 from StuInfo where StuProvince='福建省'group by ClassId +--统计每个班的各个省的总人数 +select ClassId 班,StuProvince,count(StuProvince)人数 from StuInfo group by ClassId ,StuProvince +--统计每个省的女生数 +select * from StuInfo +select StuSex,count(StuSex) 人数,StuProvince 省 from StuInfo where StuSex='女'group by StuSex,StuProvince +--统计每个省的男、女生数 +select StuProvince,StuSex,count(*)人数 from StuInfo group by StuProvince,StuSex +--统计每个学生的考试总分、平均分 +select * from Scores +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 CourseId,max(Score) 最高分,min(Score) 最低分 from Scores group by CourseId +--统计出每个学生的各门成绩的平均分 +select StuId,CourseId,avg(Score) 平均分 from Scores group by CourseId ,StuId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\232.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..04c20deff7c2113d0088af7bd8778e3cc9214869 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\232.sql" @@ -0,0 +1,33 @@ +select * from ClassInfo +select * from CourseInfo +select * from Scores +select * from StuInfo +--统计每个班的男生数 +select ClassID 班级,count(StuSex) as 男生人数 from StuInfo where StuSex='男' group by ClassID + +--统计每个班的男、女生数 +select ClassID 班级,StuSex 性别,count(StuSex) as 男女生人数 from StuInfo group by ClassID,StuSex + +--统计每个班的福建人数 +select ClassID 班级,count(StuProvince) as 福建省的人数 from StuInfo where StuProvince='福建省' group by ClassID + +--统计每个班的各个省的总人数 +select ClassID 班级,StuProvince 省份,count(*) as 各省的人数 from StuInfo group by ClassID,StuProvince + +--统计每个省的女生数 +select StuProvince 省份,count(StuSex) as 女生人数 from StuInfo where StuSex='女' group by StuProvince + +--统计每个省的男、女生数 +select StuProvince 省份,StuSex 性别,count(StuSex) as 男女生人数 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 CourseID 考试科目,max(Score) 最高分,min(Score) 最低分 from Scores group by CourseID + +--统计出每个学生的各门成绩的平均分 +select StuID 学生,CourseID 考试科目,avg(Score) 平均分 from Scores group by StuID,CourseID diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/SQLQuery01.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/SQLQuery01.sql" new file mode 100644 index 0000000000000000000000000000000000000000..61a8f331307b2a3926d65649f6cb9a9f4a93026f --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/SQLQuery01.sql" @@ -0,0 +1,33 @@ +--缁熻姣忎釜鐝殑鐢风敓鏁 +select ClassId,Stusex, COUNT(*)鐢风敓鏁 from StuInfo where Stusex='鐢' group by +ClassId,StuSex order by ClassId + +--缁熻姣忎釜鐝殑鐢枫佸コ鐢熸暟 +select ClassId,Stusex, COUNT(*)鐢峰コ鐢熸暟 from StuInfo group by +ClassId,StuSex order by ClassId + +--缁熻姣忎釜鐝殑绂忓缓浜烘暟 +select ClassId,StuProvince,COUNT(*)绂忓缓浜烘暟 from StuInfo where StuProvince='绂忓缓鐪' +group by ClassId,StuProvince order by ClassId + +--缁熻姣忎釜鐝殑鍚勪釜鐪佺殑鎬讳汉鏁 +select ClassId,StuProvince,COUNT(*)鎬讳汉鏁 from StuInfo group by ClassId,StuProvince + +--缁熻姣忎釜鐪佺殑濂崇敓鏁 +select StuProvince,StuSex,COUNT(*)浜烘暟 from StuInfo where StuSex='濂' group by StuProvince,StuSex + +--缁熻姣忎釜鐪佺殑鐢枫佸コ鐢熸暟 +select StuProvince,StuSex,COUNT(*)浜烘暟 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 CourseId,max(Score)鏈楂樺垎,min(Score)鏈浣庡垎 from Scores group by CourseId + +--缁熻鍑烘瘡涓鐢熺殑鍚勯棬鎴愮哗鐨勫钩鍧囧垎 +select StuId,CourseId,AVG(Score)骞冲潎鍒 from Scores group by StuId,CourseId order by StuId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c9860ad93bc0bf018d6939abb5b6f79567265a0d --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" @@ -0,0 +1,471 @@ +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 * from ClassInfo +select * from StuInfo +select * from Scores +select * from CourseInfo + +--ClassInfo 班级表 StuInfo 学生表 Scores 成绩表 CourseInfo 课程表 +----统计每个班的男生数 +select * from StuInfo +select ClassId,count(StuSex)男生数 from StuInfo where StuSex='男' group by ClassId +----统计每个班的男、女生数 +select ClassId,StuSex,count(StuSex)男女生数 from StuInfo group by ClassId ,StuSex +----统计每个班的福建人数 +select ClassId,count(StuProvince)福建人数 from StuInfo where StuProvince ='福建省'group by ClassId,StuProvince +----统计每个班的各个省的总人数 +select ClassId 班级,StuProvince 省,count(StuProvince)各个省的总人数 from StuInfo +group by ClassId,StuProvince order by ClassId asc +----统计每个省的女生数 +select StuProvince,count(StuSex)女生人数 from StuInfo where StuSex='女' group by StuProvince,StuSex +----统计每个省的男、女生数 +select StuProvince,StuSex,count(StuSex) from StuInfo group by StuProvince,StuSex +----统计每个学生的考试总分、平均分 +select * from Scores +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 CourseId,max(Score)最高分,min(Score)最低分 from Scores group by CourseId +----统计出每个学生的各门成绩的平均分 +select StuId 学生,CourseId 课程编号,avg(Score)平均分 from Scores group by StuId,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c4c3d2cbfcf7c85bcaa336a67f1ed249e6cb0555 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" @@ -0,0 +1,495 @@ +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 + + + + + + + + + + + + + + + + + + + + + + + diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/3.25\344\275\234\344\270\232.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/3.25\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ccc06ab36b83284255334b0d09cadf109740b650 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/3.25\344\275\234\344\270\232.sql" @@ -0,0 +1,45 @@ +select * from ClassInfo +select * from CourseInfo +select * from Scores +select * from StuInfo + +----统计每个班的男生数 + +select ClassId,COUNT(StuSex)男生人数 from StuInfo where StuSex = '男' group by ClassId + +----统计每个班的男、女生数 + +select StuSex,ClassId,COUNT(*)人数 from StuInfo group by StuSex, ClassId order by ClassId + + +----统计每个班的福建人数 + +select COUNT(StuProvince)人数 from StuInfo where StuProvince = '福建省' + +----统计每个班的各个省的总人数 + +select ClassId,COUNT(StuProvince)各省 from StuInfo group by StuProvince,ClassId order by ClassId + +----统计每个省的女生数 + +select StuProvince 省份,StuSex 性别,COUNT(*)人数 from StuInfo where StuSex = '女' group by StuProvince,StuSex + +----统计每个省的男、女生数 + +select StuProvince 省份,StuSex 性别,COUNT(*)人数 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 CourseId,max(Score)最高分,MIN(Score) 最低分 from Scores group by CourseId + +------统计出每个学生的各门成绩的平均分 + +select StuId 学生,CourseId 科目,AVG(Score) 平均分 from Scores group by CourseId,StuId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\344\275\234\344\270\232.txt" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\344\275\234\344\270\232.txt" new file mode 100644 index 0000000000000000000000000000000000000000..3124f63a1d5fcc4ce114e8285a5fb7b5c230c5c4 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\344\275\234\344\270\232.txt" @@ -0,0 +1,19 @@ +--统计每个班的男生数 + +--统计每个班的男、女生数 + +--统计每个班的福建人数 + +--统计每个班的各个省的总人数 + +--统计每个省的女生数 + +--统计每个省的男、女生数 + +--统计每个学生的考试总分、平均分 + +--统计出考试总分大于620的学生的考试总分 + +--统计出每门考试成绩最高分和最低分 + +--统计出每个学生的各门成绩的平均分 \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\346\225\260\346\215\256\345\210\235\345\247\213\345\214\226.txt" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\346\225\260\346\215\256\345\210\235\345\247\213\345\214\226.txt" new file mode 100644 index 0000000000000000000000000000000000000000..4ac0a3894da3b0df5768f1c98dfc01931abb4050 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/\346\225\260\346\215\256\345\210\235\345\247\213\345\214\226.txt" @@ -0,0 +1,439 @@ +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:\Program Files\Microsoft SQL Server\MSSQL12.ZYS\MSSQL\DATA\TestDB.mdf' , SIZE = 4288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) + LOG ON +( NAME = N'TestDB_log', FILENAME = N'D:\Program Files\Microsoft SQL Server\MSSQL12.ZYS\MSSQL\DATA\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 diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3256e93ca5b1cb3e8541ce80cd9e8c1a46753b60 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" @@ -0,0 +1,36 @@ + +select*from [dbo].[ClassInfo] +select*from [dbo].[CourseInfo] +select*from [dbo].[Scores] +select*from [dbo].[StuInfo] + +--统计每个班的男生数 +select ClassId ,count(*) from stuInfo where StuSex='男' group by ClassId + +--统计每个班的男、女生数 +select ClassId, StuSex,count(StuSex) from stuInfo group by ClassId,StuSex + +--统计每个班的福建人数 +select ClassId, count(StuName) 福建人数 from StuInfo where StuProvince='福建省' group by ClassId + +--统计每个班的各个省的总人数 +select ClassId, StuProvince, count(StuProvince) 省的总人数 from StuInfo group by ClassId ,StuProvince + +--统计每个省的女生数 +select StuProvince,count(StuSex)as 女生人数 from StuInfo where StuSex='女' group by StuProvince + +--统计每个省的男、女生数 +select StuProvince, StuSex,count(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 CourseId ,max(score) 最高分 ,min(score) 最低分 from scores group by CourseId + +--统计出每个学生的各门成绩的平均分 +select StuId ,CourseId 课程编号,avg(score) 各门成绩的平均分 From scores group by StuId,CourseId + diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ab3a209994b59f1a1ad413e32fb32b3584b1495a --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" @@ -0,0 +1,460 @@ +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, StuSex +--统计每个班的男、女生数 +select ClassId,COUNT(StuSex)女生人数 from StuInfo where StuSex='女' group by ClassId, StuSex +--统计每个班的福建人数 +select ClassId,COUNT(StuProvince)福建人数 from StuInfo where StuProvince='福建省' group by ClassId +--统计每个班的各个省的总人数 +select ClassId , StuProvince, COUNT(*)总人数 from StuInfo StuProvince group by ClassId ,StuProvince +--统计每个省的女生数 +select StuProvince,COUNT(StuSex)女生人数 from StuInfo where StuSex='女' group by StuProvince ,StuSex +--统计每个省的男、女生数 +select StuProvince,COUNT(StuSex)女生人数 from StuInfo where StuSex='女' group by StuProvince ,StuSex +select StuProvince,COUNT(StuSex)男生人数 from StuInfo where StuSex='男' group by StuProvince ,StuSex +--统计每个学生的考试总分、平均分 +select StuId,SUM(Score),AVG(Score) from Scores group by StuiD ,Score +--统计出考试总分大于620的学生的考试总分 +select StuId,SUM(Score) from Scores group by StuId having SUM(Score)>620 +--统计出每门考试成绩最高分和最低分 +Select CourseId,MAX(Score),MIN(Score) from Scores group by CourseId,Score +--统计出每个学生的各门成绩的平均分 +Select StuId,CourseId,AVG(Score) from Scores group by StuId,CourseId diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7aff1ae26485ebc49a579bb3b7f8222a244823f0 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" @@ -0,0 +1,22 @@ + +--统计每个班的男生数 +select*from [dbo].[StuInfo] +select [ClassId] 班级,count (StuId) 男生总数 from [dbo].[StuInfo] where StuSex='男' group by [ClassId] +--统计每个班的男、女生数 +select [ClassId],StuSex ,count(StuSex) from [dbo].[StuInfo] group by [ClassId],StuSex +--统计每个班的福建人数 +select [ClassId], count(StuId) from [dbo].[StuInfo] where StuProvince='福建省' group by [ClassId] +--统计每个班的各个省的总人数 +select [ClassId], StuProvince ,count(StuId) from [dbo].[StuInfo] group by [ClassId], StuProvince +--统计每个省的女生数 +select StuProvince ,count(StuId) 女生人数 from [dbo].[StuInfo] where StuSex='女' group by StuProvince +--统计每个省的男、女生数 +select StuProvince, StuSex ,count(StuSex) from [dbo].[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 CourseId ,max(Score) 最高分,min(Score) 最低分 from Scores group by CourseId +--统计出每个学生的各门成绩的平均分 +select StuId 学生, CourseId 科目 ,avg(Score) 平均分 from Scores group by StuId ,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cd44cd0900937a84a56ffa45aed12e45374b3276 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" @@ -0,0 +1,24 @@ +--统计每个班的男生数 +select ClassId,COUNT(StuSex) 男生数 from StuInfo where StuSex='男' group by ClassId +--统计每个班的男、女生数 +select ClassId,StuSex,count(StuSex) from StuInfo group by ClassId ,StuSex +--统计每个班的福建人数 +select ClassId,StuProvince from StuInfo where StuProvince='福建省' +select ClassId,count(StuProvince)人数 from StuInfo where StuProvince='福建省' group by ClassId +--统计每个班的各个省的总人数 +select ClassId,StuProvince,count(StuProvince)from StuInfo group by StuProvince, ClassId +--统计每个省的女生数 +select StuProvince,COUNT(StuSex) 女生数 from StuInfo where StuSex='男' group by StuProvince +--统计每个省的男、女生数 +select StuProvince,StuSex,count(StuSex) from StuInfo group by StuProvince ,StuSex + +--统计每个学生 的考试总分、平均分 +select StuId,sum(Score),avg(Score) from Scores group by StuId + +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 CourseId,max(Score),min(Score) from Scores group by CourseId +--统计出每个学生的各门成绩的平均分 +select StuId,CourseId ,avg(Score)from Scores group by StuId,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..122693fb2472ac4fae1644ae742d049069d207af --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" @@ -0,0 +1,26 @@ +use TestDB +go +select * from ClassInfo +select * from CourseInfo +select * from Scores +select * from StuInfo +--统计每个班的男生数 +select ClassId ,count(StuSex) 男生数 from StuInfo where StuSex='男' group by ClassId +--统计每个班的男、女生数 +select ClassId,StuSex,count(StuSex) 人数 from StuInfo group by ClassId,StuSex +--统计每个班的福建人数 +select ClassId,COUNT(*) 福建人数 from StuInfo where StuProvince='福建省' group by ClassId +--统计每个班的各个省的总人数 +select ClassId,StuProvince,count(*)from StuInfo group by ClassId,StuProvince +--统计每个省的女生数 +select StuProvince,count(StuSex) 女生数 from StuInfo where StuSex='女' group by StuProvince +--统计每个省的男、女生数 +select StuProvince,StuSex,count(StuSex) from StuInfo group by StuProvince,StuSex +--统计每个学生的考试总分、平均分 +select sum(Score) 考试总分,AVG(Score) 平均分 from Scores group by StuId +--统计出考试总分大于620的学生的考试总分 +select StuId,sum(Score) from Scores group by StuId having sum(Score)>620 +--统计出每门考试成绩最高分和最低分 +select CourseId,max(Score) 最高分,min(Score) 最低分 from Scores group by CourseId +--统计出每个学生的各门成绩的平均分 +select StuId,CourseId,avg(Score) 平均分 from Scores group by StuId,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..50756a5c2390fd99abafda74e107bc331f48ab13 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" @@ -0,0 +1,479 @@ +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:\Program Files\Microsoft SQL Server\MSSQL12.ZYS\MSSQL\DATA\TestDB.mdf' , SIZE = 4288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) + LOG ON +( NAME = N'TestDB_log', FILENAME = N'D:\Program Files\Microsoft SQL Server\MSSQL12.ZYS\MSSQL\DATA\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 + +--统计每个班的男生数 +select * from [dbo].[StuInfo] + +select count (*) 人数,ClassID 班级编号,StuSex 性别 from [dbo].[StuInfo] where Stusex ='男' group by StuSex,ClassID +--统计每个班的男、女生数 +select count (*) 人数,ClassID 班级编号,StuSex 性别 from [dbo].[StuInfo] group by ClassID ,Stusex + +--统计每个班的福建人数 +select count (*)人数,StuProvince 省份 , ClassID 班级编号 from [dbo].[StuInfo] where StuProvince ='福建省' group by StuProvince ,ClassID + +--统计每个班的各个省的总人数 + +select count(*)人数,StuProvince 省份 , ClassID 班级编号 from [dbo].[StuInfo] group by StuProvince , ClassID + +--统计每个省的女生数 +select count(*)人数,StuSex 性别 ,StuProvince 省份 from [dbo].[StuInfo] where StuSex ='女生' group by StuProvince ,StuSex + +--统计每个省的男、女生数 +select count(*)人数,StuSex 性别 ,StuProvince 省份 from [dbo].[StuInfo] group by StuProvince ,StuSex + + + + + + + + +--统计每个学生的考试总分、平均分 +select *from [dbo].[Scores] + +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 CourseId 科目, max(Score) 最高分,min(Score) 最低分 from Scores group by CourseId + +--统计出每个学生的各门成绩的平均分 + +select StuId 编号,CourseId 科目 ,avg(Score)平均分 from Scores group by StuId ,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6d4e81aaa71c238b12556292409fa98a614618b7 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" @@ -0,0 +1,45 @@ +use TestDB +go + +select * from ClassInfo +select * from CourseInfo +select * from Scores +select * from StuInfo +--统计每个班的男生数 +select ClassId, count(ClassId) 男生数 from StuInfo where StuSex = '男' +group by ClassId, StuSex + +--统计每个班的男、女生数 +select ClassId,StuSex,count(ClassId)人数 from StuInfo where StuSex = '男'or StuSex = '女' +group by ClassId, StuSex + +--统计每个班的福建人数 +select ClassId,count(StuName)人数 from StuInfo where StuProvince='福建省' +group by StuProvince,ClassId + +--统计每个班的各个省的总人数 +select ClassId,StuProvince 省份 ,count(StuName)人数 from StuInfo +group by StuProvince,ClassId + +--统计每个省的女生数 +select StuProvince 省份 ,count(StuName)人数 from StuInfo where StuSex = '女' +group by StuProvince + +--统计每个省的男、女生数 +select StuProvince 省份,count(StuName)人数,StuSex 性别 from StuInfo where StuSex = '男' or StuSex = '女' +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 CourseId,max(Score)最高分,min(Score)最低分 from Scores +group by CourseId + +--统计出每个学生的各门成绩的平均分 +select StuId,CourseId,avg(Score)平均分 from Scores +group by CourseId,StuId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery3.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9c978ea85c98c44f4c147fe7a70f6e2c2f7186d6 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery3.sql" @@ -0,0 +1,22 @@ +--统计每个班的男生数 +select ClassId,count(StuSex) from StuInfo where StuSex='男'group by ClassId +--统计每个班的男、女生数 +select * from StuInfo +select ClassId,StuSex ,count(StuSex)from StuInfo group by ClassId,StuSex +--统计每个班的福建人数 +select ClassId,count(StuProvince)from StuInfo where StuProvince='福建省' group by ClassId +--统计每个班的各个省的总人数 +select StuProvince,ClassId ,count(StuProvince) from StuInfo group by StuProvince,ClassId +--统计每个省的女生数 +select StuProvince,count(StuSex) from StuInfo where StuSex='女'group by StuProvince +--统计每个省的男、女生数 +select StuProvince,StuSex ,count(StuSex)from StuInfo group by StuProvince,StuSex +--统计每个学生的考试总分、平均分 +select * from Scores +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 CourseId ,max(Score),min(Score) from Scores group by CourseId +--统计出每个学生的各门成绩的平均分 +select StuId, CourseId,avg(Score) from Scores group by StuId,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" new file mode 100644 index 0000000000000000000000000000000000000000..b058276c9c161c93ceb09018db84eda7864e8847 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" @@ -0,0 +1,43 @@ +use TestDB +go +--缁熻姣忎釜鐝殑鐢风敓鏁 +select * from StuInfo + +select ClassId,StuSex,count(*) from StuInfo group by ClassId,StuSex having StuSex='鐢' + +--缁熻姣忎釜鐝殑鐢枫佸コ鐢熸暟 + +select CLassId,StuSex,count(*) from StuInfo group by ClassId,StuSex having StuSex='鐢' or StuSex='濂' order by ClassId + +--缁熻姣忎釜鐝殑绂忓缓浜烘暟 + +select ClassId,StuProvince,count(*)浜烘暟 from StuInfo group by ClassId,StuProvince having StuProvince='绂忓缓鐪' + +--缁熻姣忎釜鐝殑鍚勪釜鐪佺殑鎬讳汉鏁 + +select ClassId,StuProvince,count(*) from StuInfo group by ClassId,StuProvince order by ClassId + +--缁熻姣忎釜鐪佺殑濂崇敓鏁 + +select StuProvince,count(*)濂崇敓浜烘暟 from StuInfo group by StuProvince,StuSex having StuSex='濂' + +--缁熻姣忎釜鐪佺殑鐢枫佸コ鐢熸暟 + +select StuProvince,StuSex,count(*) from StuInfo group by StuProvince,StuSex having StuSex='鐢' or StuSex='濂' order by StuProvince + +--缁熻姣忎釜瀛︾敓鐨勮冭瘯鎬诲垎銆佸钩鍧囧垎 +select * from Scores + +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 CourseId,max(Score)鏈楂樺垎,min(Score)鏈浣庡垎 from Scores group by CourseId order by CourseId + +--缁熻鍑烘瘡涓鐢熺殑鍚勯棬鎴愮哗鐨勫钩鍧囧垎 + +select StuId,avg(Score)骞冲潎鍒 from Scores group by StuId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/Giao.txt" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/Giao.txt" new file mode 100644 index 0000000000000000000000000000000000000000..32d837a401c200d0a9e3688017662ff8afcadba0 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/Giao.txt" @@ -0,0 +1,20 @@ +--统计每个班的男生数 + select ClassId,StuSex,count (StuSex) from StuInfo group by ClassId,StuSex having StuSex='男' +--统计每个班的男、女生数 + select ClassId,StuSex,count (StuSex) from StuInfo group by ClassId,StuSex +--统计每个班的福建人数 + select ClassId,StuProvince,count (StuProvince) from StuInfo group by ClassId,StuProvince having StuProvince='福建省' +--统计每个班的各个省的总人数 + select ClassId,StuProvince,count (StuProvince) from StuInfo group by ClassId,StuProvince +--统计每个省的女生数 + select StuProvince,StuSex,count (StuSex) from StuInfo group by StuProvince,StuSex having StuSex='女' +--统计每个省的男、女生数 + select StuProvince,StuSex,count (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 StuId,max(score)最高分,min(score)最低分 from Scores group by CourseId +--统计出每个学生的各门成绩的平均分 + select StuId 学生,CourseId 科目,avg(Score)平均分 from scores group by StuId,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery2.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a1bcc065766166597d7e18196c182b7ad361012b --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery2.sql" @@ -0,0 +1,50 @@ +select *from StuInfo + +--统计每个班的男生数 + +select ClassId 班级名,count(stusex )男生数 from StuInfo where StuSex='男' order by ClassId desc + + +--统计每个班的男、女生数 + +select ClassId 班级名,stusex 性别,count(*)人数 from StuInfo group by ClassId,StuSex + + +--统计每个班的福建人数 + +select classId 班级名,count (stuprovince) 福建省人数 from StuInfo where stuprovince='福建省' group by classId + +--统计每个班的各个省的总人数 + +select ClassId 班级名,count(*)人数,stuprovince 省份 from StuInfo group by ClassId , stuprovince + +--统计每个省的女生数 + +select stuprovince 省份, count(stusex)女生数 from StuInfo where StuSex='女' group by stuprovince + +--统计每个省的男、女生数 + +select stuprovince 省份,stusex 性别,count(*)人数 from StuInfo group by stuprovince,StuSex + + + +select * from Scores + + + +--统计每个学生的考试总分、平均分 + +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 Courseid 科目, max (score) 最高分 ,min (score)最低分 from Scores group by CourseId + +--统计出每个学生的各门成绩的平均分 + +select stuid 学生编号, courseid 科目 ,avg(score) 平均分 from Scores group by StuId , CourseId diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..210853d6d4c94fb25bf19792a0f33f825d52d706 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" @@ -0,0 +1,51 @@ +use TestDB +select * from ClassInfo +select * from CourseInfo +select * from Scores +select * from StuInfo + + + +--统计每个班的男生数 + +select * from StuInfo +select ClassID,count(*)男生人数 from StuInfo where StuSex='男' group by ClassID + +--统计 每个班 的男、女生数 + +select * from StuInfo +select ClassId,StuSex,count(*)人数 from StuInfo group by ClassId,StuSex + +--统计每个班的福建人数 + +select ClassId,StuProvince,count(*)人数 from StuInfo where StuProvince='福建省' group by StuProvince,ClassId + +--统计每个班的各个省的总人数 + +select ClassId,StuProvince,count(*)人数 from StuInfo group by ClassId,StuProvince order by ClassId +--统计每个省的女生数 + +select StuProvince,count(*)女生人数 from StuInfo where StuSex='女' group by StuProvince + +--统计每个省的男、女生数 + +select StuProvince,StuSex,count(*)人数 from StuInfo group by StuProvince,StuSex + +--统计每个学生的考试总分、平均分 + +select StuId,sum(Score)成绩总和,avg(Score)平均成绩 from Scores group by StuId +select * from Scores +select * from StuInfo + +--统计出考试总分大于620的学生的考试总分 + +select StuId,sum(Score)总分 from Scores group by StuId having sum(Score)>620 + +--统计出每门考试成绩最高分和最低分 + +select * from Scores +select StuId,CourseId,max(Score)最高成绩,min(Score)最低成绩 from Scores group by StuId,CourseId + +--统计出每个学生的各门成绩的平均分 + +select StuId,CourseId,avg(Score)平均分 from Scores group by StuId,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b71955ef659be37170fc743d691a9b3424711f0d --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" @@ -0,0 +1,12 @@ +select * from StuInfo +select ClassId,count(StuSex)性别 from StuInfo where StuSex ='男' group by ClassId +select ClassId,count(StuSex)人数,StuSex from StuInfo group by ClassId,StuSex +select ClassId,count(StuProvince)省份 from StuInfo where StuProvince='福建省' group by ClassId +select ClassId,StuProvince 省份,count(StuProvince)数量 from StuInfo group by ClassId,StuProvince order by ClassId +select ClassId,StuProvince 省份,count(StuProvince)数量,StuSex from StuInfo where StuSex ='女' group by ClassId,StuProvince,StuSex order by ClassId +select StuProvince 省份,count(StuProvince)数量,StuSex from StuInfo group by StuProvince,StuSex +select * from Scores +select StuId,avg(score)平均分,sum(score)总分 from Scores group by StuId +select StuId,sum(score)总分 from Scores group by StuId having sum(score)>620 +select CourseId,max(score)最高分,min(score)最低分 from Scores group by CourseId +select StuId,CourseId,avg(score)平均分 from Scores group by StuId,CourseId order by StuId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..29fc99682da5dab8f927e598fce5d0cbf0b94e5c --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" @@ -0,0 +1,22 @@ + +--统计每个班的男生数 +select ClassId,StuSex,count(StuSex) from stuInfo where StuSex='男' group by StuSEX,ClassId +--统计每个班的男、女生数 +select ClassId,StuSex,count(*) from StuInfo group by ClassId,StuSex order by ClassId +--统计每个班的福建人数 +select * from stuInfo +select ClassId,StuProvince,count(StuProvince) from stuInfo where StuProvince='福建省' group by ClassId,StuProvince +--统计每个班的各个省的总人数 +select ClassId,stuProvince,count(*) from stuInfo group by ClassId,stuProvince order by ClassId +--统计每个省的女生数 +select StuSex,stuProvince,count(StuSex) from stuInfo where stuSex='女' group by StuSex,stuProvince order by stuProvince +--统计每个省的男、女生数 +select StuSex,stuProvince,count(*) from stuInfo group by StuSex,stuProvince order by stuProvince +--统计每个学生的考试总分、平均分 +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 CourseId,max(score) from scores group by CourseId +--统计出每个学生的各门成绩的平均分 +select CourseId,stuId,avg(score) from scores group by CourseId,stuId order by stuId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/group by.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/group by.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cdb7db0b9d3b74239f8afe5c0bf2f8858ff19d9d --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/group by.sql" @@ -0,0 +1,28 @@ +use master +go +use TestDB +go +select * from ClassInfo +select * from CourseInfo +select * from Scores +select * from StuInfo +--统计每个班的男生数 +select ClassId,COUNT(*)男生人数 from StuInfo where StuSex='男' group by ClassId +--统计每个班的男、女生数 +select ClassId,StuSex,COUNT(*)人数 from StuInfo group by StuSex,ClassId +--统计每个班的福建人数 +select ClassId,COUNT(*) from StuInfo where StuProvince='福建省' group by ClassId +--统计每个班的各个省的总人数 +select ClassId,StuProvince,COUNT(*)各个省份总人数 from StuInfo group by StuProvince,ClassId +--统计每个省的女生数 +select StuProvince,COUNT(*)各个省份女生人数 from StuInfo where StuSex='女' group by StuProvince +--统计每个省的男、女生数 +select StuProvince,StuSex,COUNT(*) from StuInfo group by StuSex,StuProvince +--统计每个学生的考试总分、平均分 +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 CourseId,max(Score)最高分,MIN(Score)最低分 from Scores group by CourseId +--统计出每个学生的各门成绩的平均分 +select StuId,AVG(Score)每门成绩的平均分 from Scores group by StuId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery2.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7a6582b645ef5299e13fc4e4eac007792288a3e0 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery2.sql" @@ -0,0 +1,27 @@ +use TestDB +go +select * from StuInfo +select * from ClassInfo +select * from CourseInfo +select * from Scores +--统计每个班的男生数 +select ClassId,StuSex,count(*) from StuInfo where StuSex='男' group by ClassId,StuSex +--统计每个班的男、女生数 +select ClassId,StuSex,count(*) from StuInfo group by StuSex,ClassId +--统计每个班的福建人数 +select StuProvince,ClassId, count(*) from StuInfo where StuProvince='福建省' group by StuProvince,ClassId +--统计每个班的各个省的总人数 +select ClassId,StuProvince, count(StuProvince) from StuInfo group by StuProvince,ClassId +--统计每个省的女生数 +select StuProvince,StuSex,count(*) from StuInfo where StuSex='女' group by StuProvince,StuSex +--统计每个省的男、女生数 +select StuProvince,StuSex,count(*) from StuInfo group by StuSex,StuProvince +--统计每个学生的考试总分、平均分 +select StuId, AVG(Score) 平均分,sum(Score) 总分 from Scores group by StuId +--统计出考试总分大于620的学生的考试总分 +select top 1 StuId,sum(Score) from Scores group by StuId having sum(Score)>620 +--统计出每门考试成绩最高分和最低分 +select top 1 Score from Scores +select top 1 Score from Scores order by Score DESC +--统计出每个学生的各门成绩的平均分 +select StuId,CourseId, AVG(Score) from Scores group by StuId,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/9.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/9.sql" new file mode 100644 index 0000000000000000000000000000000000000000..66faf675cb4b29a1dcebe720a330a70f54dc1f65 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/9.sql" @@ -0,0 +1,473 @@ +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 + +use TestDB +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 +ALTER DATABASE [TestDB] SET READ_WRITE +GO + + +----统计每个班的男生数 +select * from StuInfo +select ClassId ,count(StuSex)男生人数 from StuInfo where StuSex='男' group by ClassId + +----统计每个班的男、女生数 +select ClassId,StuSex,count(StuSex) from StuInfo group by ClassId,StuSex + +----统计每个班的福建人数 +select ClassId,count(StuProvince)福建人数 from StuInfo where StuProvince='福建省'group by ClassId + +----统计每个班的各个省的总人数 +select ClassId,StuProvince,count(StuProvince)各个省的总人数 from StuInfo group by ClassId,StuProvince order by ClassId asc + +----统计每个省的女生数 +select StuProvince,count(StuSex) from StuInfo where StuSex='女' group by StuProvince,StuSex + +----统计每个省的男、女生数 +select StuProvince,StuSex,count(StuSex) from StuInfo group by StuProvince,StuSex + +----统计每个学生的考试总分、平均分 +select * from Scores +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 CourseId,MAX(Score)最高分,MIN(Score)最低分 from Scores group by CourseId + +----统计出每个学生的各门成绩的平均分 +select StuId 学生,CourseId,AVG(Score)平均分 from Scores group by CourseId,StuId diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/\344\275\234\344\270\232 .txt" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/\344\275\234\344\270\232 .txt" new file mode 100644 index 0000000000000000000000000000000000000000..33e8c7bc1a36c0691a74fadd1a231a0951bae3fd --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/\344\275\234\344\270\232 .txt" @@ -0,0 +1,20 @@ +--统计每个班的男生数 +select classid,stusex, count(stusex)男生数 from stuinfo group by classid,stusex having stusex='男' +--统计每个班的男、女生数 +select stusex 性别,classid 班级, count(stusex)数量 from stuinfo group by stusex, classid +--统计 每个班 的 福建人数 +select classid ,count(stuprovince)福建人数 from stuinfo where stuprovince='福建省' group by classid +--统计 每个班的 各个省 的 总人数 +select classid,stuprovince , count(*)人数 from stuinfo group by classid,stuprovince +--统计 每个省 的 女生数 +select stusex, stuprovince ,count(stusex)人数 from stuinfo group by stusex,stuprovince having stusex='女' +--统计每个省的男、女生数 +select stusex, stuprovince ,count(stusex)人数 from stuinfo group by stusex,stuprovince +--统计 每个学生 的 考试总分、平均分 +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 courseid, max(score)最高分,min(score)最低分 from scores group by courseid +--统计出 每个学生的 各门成绩 的平均分 +select stuid 学生,courseid 科目,avg(score)平均分 from scores group by stuid,courseid \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/.keep" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..12d49d053019a6cc544efc65240ce36f4ef245bf --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" @@ -0,0 +1,21 @@ +--缁熻姣忎釜鐝殑鐢风敓鏁 +select * from Scores +select ClassId,count(StuSex) 鐢风敓浜烘暟 from StuInfo where StuSex='鐢' group by ClassId +--缁熻姣忎釜鐝殑鐢枫佸コ鐢熸暟 +select ClassId,StuSex,count(StuSex) 浜烘暟 from StuInfo group by ClassId,StuSex +--缁熻姣忎釜鐝殑绂忓缓浜烘暟 +select ClassId,count(Stuprovince) 绂忓缓鐪佷汉鏁 from StuInfo where Stuprovince='绂忓缓鐪' group by ClassId +--缁熻姣忎釜鐝殑鍚勪釜鐪佺殑鎬讳汉鏁 +select ClassId,Stuprovince,count(Stuprovince) 浜烘暟 from StuInfo group by ClassId,Stuprovince +--缁熻姣忎釜鐪佺殑濂崇敓鏁 +select Stuprovince,count(StuSex) 濂崇敓浜烘暟 from StuInfo where StuSex='濂' group by Stuprovince +--缁熻姣忎釜鐪佺殑鐢枫佸コ鐢熸暟 +select Stuprovince,StuSex,count(*) 浜烘暟 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 CourseId,max(Score) 鏈楂樺垎,min(Score) 鏈浣庡垎 from Scores group by CourseId +--缁熻鍑烘瘡涓鐢熺殑鍚勯棬鎴愮哗鐨勫钩鍧囧垎 +select StuId,CourseId,AVG(Score) 骞冲潎鍒 from Scores group by StuId,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e4eadd140226cef280a0f0f9ac836599d3faf276 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye.sql" @@ -0,0 +1,23 @@ + +select * from Stuinfo +--统计每个班的男生数 +select ClassID 班级 ,count(StuSex)男生人数 from stuinfo where StuSex='男' group by ClassID +--统计每个班的男、女生数 +select ClassID 班级,StuSex, count(StuSex) from Stuinfo group by ClassID ,StuSex +--统计每个班的福建人数 +select ClassID 班级,StuProvince 省份,count(*) 人数 from Stuinfo where StuProvince='福建省'group by ClassID,StuProvince +--统计每个班的各个省的总人数 +select ClassID 班级 ,StuProvince 省份,count(*) 人数 from Stuinfo group by ClassID,StuProvince +--统计每个省的女生数 +select StuProvince 省份,count(StuSex)女 from stuinfo where StuSex='女' group by StuProvince,StuSex +--统计每个省的男、女生数 +select StuSex,StuProvince 省份,count(*) from stuinfo group by StuProvince,StuSex +--统计每个学生的考试总分、平均分 +select * from Scores +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 CourseId 科目,max(Score) 最高分,min(Score) 最低分 from Scores group by CourseId +--统计出每个学生的各门成绩的平均分 +select StuId,CourseId,avg(Score)平均分 from Scores group by StuId,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c9860ad93bc0bf018d6939abb5b6f79567265a0d --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" @@ -0,0 +1,471 @@ +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 * from ClassInfo +select * from StuInfo +select * from Scores +select * from CourseInfo + +--ClassInfo 班级表 StuInfo 学生表 Scores 成绩表 CourseInfo 课程表 +----统计每个班的男生数 +select * from StuInfo +select ClassId,count(StuSex)男生数 from StuInfo where StuSex='男' group by ClassId +----统计每个班的男、女生数 +select ClassId,StuSex,count(StuSex)男女生数 from StuInfo group by ClassId ,StuSex +----统计每个班的福建人数 +select ClassId,count(StuProvince)福建人数 from StuInfo where StuProvince ='福建省'group by ClassId,StuProvince +----统计每个班的各个省的总人数 +select ClassId 班级,StuProvince 省,count(StuProvince)各个省的总人数 from StuInfo +group by ClassId,StuProvince order by ClassId asc +----统计每个省的女生数 +select StuProvince,count(StuSex)女生人数 from StuInfo where StuSex='女' group by StuProvince,StuSex +----统计每个省的男、女生数 +select StuProvince,StuSex,count(StuSex) from StuInfo group by StuProvince,StuSex +----统计每个学生的考试总分、平均分 +select * from Scores +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 CourseId,max(Score)最高分,min(Score)最低分 from Scores group by CourseId +----统计出每个学生的各门成绩的平均分 +select StuId 学生,CourseId 课程编号,avg(Score)平均分 from Scores group by StuId,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6c8b77612a5745f5871929b00aaef8a836502d3f --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" @@ -0,0 +1,28 @@ + + +select *from StuInfo +--统计每个班的男生数 +select count(stuid) 男生人数, classid 班级 from StuInfo where stusex='男' group by classid +--统计每个班的男、女生数 +select count(stusex)人数,stusex 性别,classid 班级 from StuInfo group by classid , stusex +--统计每个班的福建人数 +select count(*)人数,classid 班级,stuprovince from StuInfo where stuprovince='福建省' group by classid,stuprovince +--统计每个班的各个省的总人数 +select count(*)人数,stuprovince 省会,classid 班级 from StuInfo group by stuprovince,classid +--统计每个省的女生数 +select count(*)人数,stuprovince 省 ,stusex from StuInfo where stusex='女' group by stuprovince,stusex +--统计每个省的男、女生数 +select count(*) 人数, stuprovince 省,stusex 性别 from StuInfo group by stuprovince,stusex +--统计每个学生的考试总分、平均分 +select *from scores +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 *from courseinfo +select courseid ,max(score)最高, min(score)最低 from scores group by courseid +--统计出每个学生的各门成绩的平均分 +select stuid 学生, avg(score) 平均分 ,courseid 门科 from scores group by stuid,courseid +select * from scores +select *from classinfo +select *from StuInfo diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery3.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3c58ce733235dc2f82e8a6ee3ab31eab7f27d583 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery3.sql" @@ -0,0 +1,30 @@ +select*from ClassInfo; +select*from CourseInfo; +select*from Scores; +select*from StuInfo; +--统计每个班的男生数 +select ClassId 班级, count(*)男生人数 from StuInfo where StuSex='男' group by ClassId + +--统计每个班的男、女生数 +select ClassId 班级, StuSex,count(*)女生人数 from StuInfo group by ClassId , StuSex ; + +--统计每个班的福建人数 +select ClassId 班级,count(*)福建人数 from StuInfo where StuProvince='福建省' group by ClassId + +--统计每个班的各个省的总人数 +select ClassId 班级, StuProvince,count(*)总人数 from StuInfo StuProvince group by ClassId,StuProvince +--统计每个省的女生数 +select StuProvince,count(*)女生人数 from StuInfo where StuSex='女' group by StuProvince +--统计每个省的男、女生数 +select StuProvince,StuSex, count(*)女生人数 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 StuId 学生,max(Score)最高分,min(Score)最低分 from Scores group by StuId + +--统计出每个学生的各门成绩的平均分 +select StuId 学生,CourseId 科目,avg(Score)平均分 from Scores group by StuId,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/.keep" "b/\347\254\254\344\271\235\346\254\241\350\257\276\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\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery9.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery9.sql" new file mode 100644 index 0000000000000000000000000000000000000000..81fc0607e4e07403bc45b8eab368204e27c0796e --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery9.sql" @@ -0,0 +1,15 @@ + +select * from StuInfo +select * from CourseInfo +select * from scores +select * from ClassInfo +select ClassId, count(StuSex)'男生数' from StuInfo where StuSex='男' group by ClassId +select ClassId, StuSex,count(StuSex)'人数' from StuInfo group by ClassId,StuSex +select ClassId, StuProvince ,count(*)'人数' from StuInfo where StuProvince='福建省' group by ClassId,StuProvince +select ClassId,StuProvince ,count(*)'人数'from StuInfo group by ClassId,StuProvince +select StuProvince, count(StuSex)'女生数' from StuInfo where StuSex='女' group by StuProvince +select StuProvince,StuSex, count(StuSex)'人数' from StuInfo group by StuProvince,StuSex +select StuId,SUM(score),avg(score) from scores group by StuId +select StuId,SUM(score)'考试总分' from scores group by StuId having SUM(score)>620 +select courseid,MAX(score)最高分,min(score)最低分 from scores group by courseid +select stuid,courseid, avg(score) from scores group by stuid,courseid \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5622453404ff3da23d63433f81a97647ddc50a99 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" @@ -0,0 +1,23 @@ +use TestDB +go +--统计每个班的男生数 +select ClassId,COUNT(*) 班级男生数 from StuInfo where StuSex = '男' group by ClassId +--统计每个班的男、女生数 +select ClassId,stuSex,count(*) 人数 from StuInfo group by StuSex,ClassId +--统计每个班的福建人数 +select ClassId,COUNT(*) 福建学生人数 from StuInfo where StuProvince like '%福建%' group by ClassId +--统计每个班的各个省的总人数 +select ClassId,StuProvince,COUNT(*) 该省学生人数 from StuInfo group by ClassId,StuProvince +--统计每个省的女生数 +select StuProvince,COUNT(*) 女生人数 from StuInfo where StuSex = '女' group by StuProvince +--统计每个省的男、女生数 +select StuProvince,StuSex,COUNT(*) 人数 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 CourseId,MAX(Score) 最高分,MIN(Score) 最低分 from Scores group by CourseId +--统计出每个学生的各门成绩的平均分 +select StuId,CourseId,AVG(score) 平均分 from Scores group by StuId,CourseId + diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/\344\275\234\344\270\232.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..65d63e4d8ea8ba638e42e2bfde1f40c7b8261563 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/\344\275\234\344\270\232.sql" @@ -0,0 +1,45 @@ + +--统计每个班的男生数 +select * from StuInfo + +select ClassID, count(StuSex)每班男生总人数 from StuInfo where StuSex='男' group by ClassID +--统计每个班的男、女生数 +select * from StuInfo +select ClassID 班级名称, StuSex 性别, count(StuSex)每班男生女生人数 from StuInfo group by ClassID, StuSex + + +--统计每个班的福建人数 +select * from StuInfo +select ClassID 班级名称,count(StuId)福建省人数 from StuInfo where StuProvince='福建省' group by ClassID,StuId + + +--统计每个班的各个省的总人数 + +select * from StuInfo + +select ClassID 班级名称, StuProvince 省份, count(*)各个省份总人数 from StuInfo group by ClassID,StuProvince + +--统计每个省的女生数 + +select * from StuInfo +select StuProvince 省份,count(StuProvince)女生总人数 from StuInfo where StuSex='女' group by StuProvince,StuSex + + +--统计每个省的男、女生数 + +select * from StuInfo +select StuProvince 省份 ,StuSex, count(StuSex)男或女生总人数 from StuInfo group by StuProvince,StuSex + +--统计每个学生的考试总分、平均分 +select * from Scores +select StuId 学生, sum(Score)考试总分,avg(Score)平均分 from Scores group by StuId,Score +--统计出考试总分大于620的学生的考试总分 +select * from Scores +select StuId 学生,sum(Score)考试总分 from Scores group by StuId having sum(Score)>620 +--统计出每门考试成绩最高分和最低分 +select * from Scores +select CourseId 每门考试, max(Score)最高分,min(Score)最低分 from Scores group by CourseId +--统计出每个学生的各门成绩的平均分 +select * from Scores +select * from StuInfo +select StuId 学生,CourseId 每门考试,avg(Score)平均分 from Scores group by StuId,CourseId diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/2021-3-25.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/2021-3-25.sql" new file mode 100644 index 0000000000000000000000000000000000000000..60fca7c864c991d2a9959df2ca5e1efd4028d160 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/2021-3-25.sql" @@ -0,0 +1,26 @@ +use TestDB +go +select *from ClassInfo +select *from CourseInfo +select *from Scores +select *from StuInfo +--统计每个班的男生数 +select ClassID 班级编号,count(*) 男生人数 from StuInfo where StuSex='男' group by ClassID +--统计每个班的男、女生数 +select ClassID 班级编号,StuSex,count(*) 女生人数 from StuInfo group by ClassID,StuSex +--统计每个班的福建人数 +select ClassID 班级编号,count(*) 福建省人数 from StuInfo where StuProvince='福建省' group by ClassID +--统计每个班的各个省的总人数 +select ClassID 班级编号,StuProvince,count(*) 各省人数 from StuInfo group by ClassID,StuProvince +--统计每个省的女生数 +select StuProvince,count(*) 各省女生人数 from StuInfo where StuSex='女' group by StuProvince +--统计每个省的男、女生数 +select StuProvince,StuSex,count(*) 各省人数 from StuInfo group by StuProvince,StuSex +--统计每个学生的考试总分、平均分 +select StuId,sum(Score) 总分,avg(Score) 平均分 from Scores group by StuId +--统计出考试总分大于620的学生的考试总分 +select StuId 总分大于620的学生,sum(Score) 总分 from Scores group by StuId having sum(Score)>620 +--统计出每门考试成绩最高分和最低分 +select CourseId 课程编号 ,max(Score) 最高分,min(Score) 最低分 from Scores group by CourseId +--统计出每个学生的各门成绩的平均分 +select StuId 学生编号,CourseId 课程编号 ,avg(Score) 平均分 from Scores group by StuId,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ff073ba7f20e64fc1dd95e87e26bb8d54c6dc4ff --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" @@ -0,0 +1,23 @@ + + +--统计每个班的男生数 +select ClassId,COUNT(*)男生人数 from StuInfo where StuSex='男' group by ClassId +--统计每个班的男、女生数 +select classId,Stusex,COUNT(*)学生人数 from StuInfo group by classId,Stusex +--统计每个班的福建人数 +select classId ,StuProvince,count(*) from StuInfo where StuProvince='福建省' group by classId,StuProvince +--统计每个班的各个省的总人数 +select ClassId,StuProvince 省份,count(StuProvince)数量 from StuInfo group by ClassId,StuProvince order by ClassId +--统计每个省的男、女生数 +select StuProvince 省份,count(StuProvince)数量,StuSex from StuInfo group by StuProvince,StuSex + +--统计每个学生的考试总分、平均分 +select StuId,avg(score)平均分,sum(score)总分 from Scores group by StuId + +--统计出考试总分大于620的学生的考试总分 +select StuId,sum(score)总分 from Scores group by StuId having sum(score)>620 + +--统计出每门考试成绩最高分和最低分 +select CourseId,max(score)最高分,min(score)最低分 from Scores group by CourseId +--统计出每个学生的各门成绩的平均分 +select StuId,CourseId,avg(score)平均分 from Scores group by StuId,CourseId order by StuId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/SQLQuery1.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5330bdd14d1ba68c850d8263a85e10a3f65172a5 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/SQLQuery1.sql" @@ -0,0 +1,11 @@ + +select ClassId,count(StuSex) As 男生人数 from StuInfo group by ClassId,StuSex having StuSex='男' +select ClassId,StuSex,count(StuSex) As 男女生人数 from StuInfo group by ClassId,StuSex +select ClassId,StuProvince,count(StuProvince) AS 福建人数 from StuInfo group by ClassId,StuProvince having StuProvince='福建省' +select ClassId,StuProvince,count(StuProvince) As 各省总人数 from StuInfo group by ClassId,StuProvince +select ClassId,StuProvince,StuSex,count(StuProvince) As 女生的人数 from StuInfo group by ClassId,StuProvince,StuSex having StuSex='女' +select ClassId,StuProvince,StuSex,count(StuProvince) As 男女生人数 from StuInfo group by ClassId,StuProvince,StuSex +select StuId,sum(Score) As 总成绩,avg(Score) AS 平均成绩 from Scores group by StuId +select StuId,sum(Score) As 总成绩 from Scores group by StuId having sum(Score)>620 +select StuId,max(Score) As 最高分,min(Score) As 最低分,CourseId As 科目 from Scores group by StuId,CourseId +select StuId,CourseId,avg(Score)AS 平均成绩 from Scores group by StuId,CourseId diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/.keep" "b/\347\254\254\344\271\235\346\254\241\350\257\276\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\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQueryStudent\344\277\241\346\201\257.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQueryStudent\344\277\241\346\201\257.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fde752b0d3e706708e9e8219e4272aee714760fe --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQueryStudent\344\277\241\346\201\257.sql" @@ -0,0 +1,466 @@ +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'E:\数据库文件\数据库跟目录文件\源代码\TestDB.mdf' , SIZE = 4288KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) + LOG ON +( NAME = N'TestDB_log', FILENAME = N'E:\数据库文件\数据库跟目录文件\源代码\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*from ClassInfo +select*from CourseInfo +select*from Scores +select*from StuInfo +--统计每个班的男生数 +select ClassId,COUNT(*)男生人数 from StuInfo where StuSex='男' group by ClassId +--统计每个班的男、女生数 +select classId,Stusex,COUNT(*)学生人数 from StuInfo group by classId,Stusex +--统计每个班的福建人数 +select classId ,StuProvince,count(*) from StuInfo where StuProvince='福建省' group by classId,StuProvince +--统计每个班的各个省的总人数 +select ClassId,StuProvince 省份,count(StuProvince)数量 from StuInfo group by ClassId,StuProvince order by ClassId +--统计每个省的男、女生数 +select StuProvince 省份,count(StuProvince)数量,StuSex from StuInfo group by StuProvince,StuSex + +--统计每个学生的考试总分、平均分 +select StuId,avg(score)平均分,sum(score)总分 from Scores group by StuId + +--统计出考试总分大于620的学生的考试总分 +select StuId,sum(score)总分 from Scores group by StuId having sum(score)>620 + +--统计出每门考试成绩最高分和最低分 +select CourseId,max(score)最高分,min(score)最低分 from Scores group by CourseId +--统计出每个学生的各门成绩的平均分 +select StuId,CourseId,avg(score)平均分 from Scores group by StuId,CourseId order by StuId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/\351\273\204\346\235\260\347\203\250.txt" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/\351\273\204\346\235\260\347\203\250.txt" new file mode 100644 index 0000000000000000000000000000000000000000..59b01687f8a818ac628b13b93c5c94fd70de5b46 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/\351\273\204\346\235\260\347\203\250.txt" @@ -0,0 +1,22 @@ +--缁熻姣忎釜鐝殑鐢风敓鏁 +select ClassId,StuSex,count(StuSex) from StuInfo group by ClassId,StuSex having StuSex='鐢' +--缁熻姣忎釜鐝殑鐢枫佸コ鐢熸暟 +select ClassId,StuSex,count(StuSex) from StuInfo group by ClassId,StuSex +--缁熻姣忎釜鐝殑绂忓缓浜烘暟 +select * from StuInfo +select * from Scores +select ClassId,StuProvince,count(StuProvince) from StuInfo group by ClassId,StuProvince having StuProvince='绂忓缓鐪' +--缁熻姣忎釜鐝殑鍚勪釜鐪佺殑鎬讳汉鏁 +select ClassId,StuProvince,count(StuProvince) from StuInfo group by ClassId,StuProvince +--缁熻姣忎釜鐪佺殑濂崇敓鏁 +select StuProvince,StuSex,count(StuSex) from StuInfo group by StuProvince,StuSex having StuSex='濂' +--缁熻姣忎釜鐪佺殑鐢枫佸コ鐢熸暟 +select StuProvince,StuSex,count(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 StuId,max(Score)鏈楂樺垎,min(Score)鏈浣庡垎 from Scores group by StuId +--缁熻鍑烘瘡涓鐢熺殑鍚勯棬鎴愮哗鐨勫钩鍧囧垎 +select StuId,CourseId,avg(Score)骞冲潎鍒 from Scores group by StuId,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/\344\275\234\344\270\232.txt" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/\344\275\234\344\270\232.txt" new file mode 100644 index 0000000000000000000000000000000000000000..96f30c80b8a2e67c7ca8d131b3a52b8f10699dba --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/\344\275\234\344\270\232.txt" @@ -0,0 +1,45 @@ +use TestDB +go +select *from StuInfo +select *from Scores +select *from CourseInfo + +--统计每个班的男生数 +select ClassId 班级, count(StuId)男生人数 from StuInfo +group by ClassId,StuSex having StuSex='男' + +--统计每个班的男、女生数 +select ClassId 班级,StuSex 性别, count(StuId)人数 from StuInfo +group by ClassId,StuSex + +--统计每个班的福建人数 +select ClassId 班级,count(StuId)来自福建省人数 from StuInfo +group by ClassId,StuProvince having StuProvince='福建省' + +--统计每个班的各个省的总人数 +select ClassID 班级,StuProvince 省份,count(StuId)人数 from StuInfo +group by ClassId,StuProvince order by ClassId + +--统计每个省的女生数 +select StuProvince 省份,StuSex 性别, count(StuId) from StuInfo +group by StuProvince,StuSex having StuSex='女' + +--统计每个省的男、女生数 +select StuProvince 省份,StuSex 性别, count(StuId) from StuInfo +group by StuProvince,StuSex order by StuProvince + +--统计每个学生的考试总分、平均分 +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 CourseId 科目ID, max(Score)最高分,min(Score)最低分 from Scores +group by CourseId + +--统计出每个学生的各门成绩的平均分 +select StuId 学生ID,CourseId 科目名, avg(Score)平均分 from Scores +group by StuId,CourseId \ No newline at end of file diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/\346\225\260\346\215\256.txt" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/\346\225\260\346\215\256.txt" new file mode 100644 index 0000000000000000000000000000000000000000..352b217b89ccada30eaa4288879976414278957e --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/\346\225\260\346\215\256.txt" @@ -0,0 +1,439 @@ +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 diff --git "a/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery3.sql" "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..26f6edd1cc8f5bf5ba11d4b751caa921b672a500 --- /dev/null +++ "b/\347\254\254\344\271\235\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery3.sql" @@ -0,0 +1,32 @@ +use TestDB +go +select * from StuInfo +--统计 每个班 的 男生数 +select ClassId 班级, count(StuSex)男生人数 from StuInfo where StuSex='男' group by ClassId +--统计 每个班 的男、女生数 +select ClassId 班级,StuSex,count(StuSex)男女生人数 from StuInfo group by ClassId,StuSex +--统计 每个班 的 福建人数 +select ClassId 班级,count(*)福建人数 from StuInfo where StuProvince='福建省' group by ClassID +--统计 每个班的 各个省的 总人数 +select ClassID 班级,StuProvince,count(*)各个省的总人数 from StuInfo group by ClassId,StuProvince--null值是不列入count计算的 +--统计 每个省 的 女生数 +select StuProvince 每个省,count(StuSex)每个省的女生数 from StuInfo where StuSex='女' group by StuProvince +--统计 每个省 的 男、女生数 +select StuProvince 每个省, StuSex 性别,count(StuSex)男女生数 from StuInfo group by StuProvince,StuSex +--统计 每个学生的 考试总分、平均分 +select * from Scores where StuId=1 +select * FROM CourseInfo + +select StuId 学生ID, sum(Score)考试总分数,avg(Score)平均分 from Scores group by StuId + +--统计出 学生的 考试总分 考试总分大于620的 +select StuId 学生ID, sum(Score) 考试总分数 from Scores group by StuId +having sum(Score)>620 +--统计出 每门考试成绩 最高分 和 最低分 +select * from Scores where StuId=1 +select * FROM CourseInfo + +select CourseId 考试科目,max(Score)最高分,min(Score) 最低分 from Scores group by CourseId + +--统计出 每个学生 的 各门成绩的 平均分 +select StuId 学生id,CourseId 考试科目, avg(Score)平均分 from Scores group by StuId,CourseId diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery2.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..23f76d686e6e0f514194d14dc7cd1d6e9402e458 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery2.sql" @@ -0,0 +1,45 @@ +use master +go +create database Students +on +( + name='Students', + filename='D:\TEXT\Students.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='Students_log', + filename='D:\TEXT\Students_log.ldf', + size=5MB, + maxsize=100MB, + filegrowth=10Mb +) + +go +use Students +go + +create table StuInfo +( + StuID int primary key identity(1,1) not null, + StuNum char(10) not null, + StuName nchar(20) not null, + StuSex char(2) default('男') check(StuSex='男' or StuSex='女') not null, + StuPhone char(11) check(StuPhone=11) + +) +go +use Students +go + +create table ClassInfo +( + ClassID int primary key identity(1,1) not null, + ClassNum char(15) not null, + ClassName nchar(30) not null, + ClassRemark TEXT, + StuID int +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery3.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c541d4963cf50ceeac5a6c26ee6f358b01bfc06f --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery3.sql" @@ -0,0 +1,41 @@ +锘縰se master +go if exists(select * from sys.databases where name='Students') + drop database Students +create database Students +on +( + name='Students', + filename='C:\Users\33054\Desktop', + size=6MB, + maxsize=100MB, + filegrowth=10MB +) +log on +( + name='Students', + filename='C:\Users\33054\Desktop', + size=6MB, + maxsize=100MB, + filegrowth=10MB +) +go + +use Students +go +create table StuInfo +( + StuID int primary key identity(1,1) not null, + StuNum varchar(10) not null, + StuName nvarchar(20) not null, + StuSex char(2) default('') check(StuSex=''or StuSex='女') not null, + StuPhone varchar(11) not null +) +use Students + go + create table ClassInfo + ( + ClassID int primary key identity(1,1) not null, + ClassNum char(15) not null, + ClassName char(30) not null, + ClassRemark text + ) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..da183c6ae5a6d1a31e5eba73c4cc1d9fde6680d8 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" @@ -0,0 +1,43 @@ +锘 + +create database Students +on +( + name='Students', + filename='D:\SQL浠g爜\Students.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10MB +) + +log on +( + name='Students_log', + filename='D:\SQL浠g爜\Students_log.ldf', + size=5MB, + maxsize=100MB, + filegrowth=10MB +) +go + +use Students +go + +create table StuInfo +( +StuId int primary key identity(1,1), +StuNum varchar(10) not null, +StuName nvarchar(20) not null, +Stusex char(2) default('') check(Stusex='' or Stusex='女'), +StuPhone bigint , +) +create table ClassInfo +( + ClassID int primary key identity(1,1), + ClassNum varchar(15) not null, + Classname nvarchar(30) not null, + ClassRemark ntext, + StuID int, + + +) diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..55a950a37af2c09a7e7a5a7b35bfd725d49f58c2 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery1.sql" @@ -0,0 +1,46 @@ +use master +go +create database Students +on +( + name=Students, + filename='D:\SQl\Students.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) + +log on +( + name=Students_Log, + filename='D:\SQl\Students_Log.ldf', + size=1mb, + maxsize=10mb, + filegrowth=10% +) + +go + +use Students +create table StuInfo +( + StuID int identity(1,1) primary key, + StuNum char(10) not null, + StuName char(20) not null, + StuSex char(2) default('男') check(StuSex='女' and StuSex='男') not null, + StuPhone int check(StuPhone=11 and StuPhone=7), +) + +go + +use Students +create table ClassInfo +( + ClassID int identity(1,1) primary key, + ClassNum char(15) not null, + ClassName char(30) not null, + ClassRemark text not null, + StuID int, +) + +go \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\234\344\270\232.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..074a095561935f6a7b8c776b4c70d7556badcd7b Binary files /dev/null and "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\234\344\270\232.sql" differ diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ffe9bc010684c9eb6a9db422433a0bdb22a9c875 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275.sql" @@ -0,0 +1,20 @@ +create database hzt02 +go +use hzt02 +go +create table Stulnfo +( --列名,数据类型,设置主键,自增(起始1,增量1),设置非空 + StuID int primary key identity(1,1) not null, + StuNum char(10) not null, + StuName nvarchar(20) not null, + StuSex char(2) default('男') check(StuSex='男' or StuSex='女') not null, + StuPhone text, +) +create table Classlnfo +( + ClassID int primary key identity(1,1) not null, + ClassNum char(15) not null, + ClassName char(30) not null, + ClassRemark text, + StuID nchar, +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3715e83e709c94f1d91cd394ab637419e60a2e7a --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" @@ -0,0 +1,31 @@ +create database qaaq +on +( +name = 'qaaq', +filename = 'D:\sql作业', +size = 5MB, +maxsize = 50MB, +filegrowth = 10MB +) +log on +( +name = 'qaaq', +filename = 'D:\sql作业', +size = 5MB, +maxsize = 50MB, +filegrowth = 10MB +) +go + +use qaaq +go + +create table baab +( +ID int primary key identity(1,1) not null, +name nvarchar(20) not null, +sex char(2) not null, +wages money check (wages >=0 or wages <= 100000000) not null + + +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery2.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..84d55dc18b84b85186ed843d52dff3282f1c3b7e --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery2.sql" @@ -0,0 +1,26 @@ + +use master go +Create database demo678 +on( +name='demo678', +filename='D:\', +size='2mb', +filegrowth='1mb', +maxsize='5mb' +) +log on( +name='demo678', +filename='D:\', +size='2mb', +filegrowth='1mb', +maxsize='5mb' +) +go +use demo678 +go +Create table Student +( +StudentID int check(StudentID >0) not null, +StudentAge int check(StudentAge>18 and StudentAge<23) not null, +StudentSex char(2) default('男') not null, +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..00379fa92db50111a041d0c42af10f99a7d570c3 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" @@ -0,0 +1,28 @@ + + +use master go +create database daigua +on( + name = 'daigua', + Filename='D:\SQLdemo', + size='5mb', + maxsize='10mb', + filegrowth='2mb' +) +log on( + name = 'daigua', + Filename='D:\SQLdemo', + size='5mb', + maxsize='10mb', + filegrowth='2mb' +) +go +use daigua +go + +create table xiao +( + studengtid int not null, + studentname nvarchar not null, + studentage int not null, +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\232.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..623a549e8247551c033bd9f58f49006252ba8fd8 Binary files /dev/null and "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\232.sql" differ diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..222ad091dde6dc75c716a4c5931fe7e74c367f58 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" @@ -0,0 +1,43 @@ + + +create database Students +on +( + name='Students', + filename='F:\Students.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10MB +) + +log on +( + name='Students_log', + filename='F:\Students_log.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10MB +) +go + +use Students +go + +create table StuInfo +( +StuId int primary key identity(1,1), +StuNum varchar(10) not null, +StuName nvarchar(20) not null, +Stusex char(2) default('男') check(Stusex='男' or Stusex='女'), +StuPhone bigint , +) +create table ClassInfo +( + ClassID int primary key identity(1,1), + ClassNum varchar(15) not null, + Classname nvarchar(30) not null, + ClassRemark ntext, + StuID int, + + +) diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..29c79f245a144810024868d15fd6d7ec4582296e --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" @@ -0,0 +1,33 @@ +create database Students +on PRIMARY +(filename='D:\homework\Students.mdf', + name='Lesson2homework', + size=6MB, + maxsize=8MB, + filegrowth=10% +) + +log on +( +name='Students_log', +filename='D:\homework\Students_log.ldf', +size=6MB, +maxsize=8MB, +filegrowth=1MB +) +go + +--create a form +use Students +go +create table StuInfo +(StuNo int primary key identity(1,1) not null, +StuName nvarchar(20) not null, +StuSex char(2) default('') not null, +StuPocketMoney money check(StuPocketMoney>=0 and StuPocketMoney<=1500), +) + +create table ClassInfo +(ClaNo int primary key identity(1,1) not null, +ClaNumPe int not null +) diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery17.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery17.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6e38bdebb29882b09b8478f4e32e26e027fba04b Binary files /dev/null and "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery17.sql" differ diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..71cdebc3ea531e01cde456f1332054dc875a7f5a --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" @@ -0,0 +1,37 @@ + +create database Students +on +( + name='Students', + filename='F:\Students.mdf', + size=6mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='Students_log', + filename='F:\Students_log.ldf', + size=6mb, + maxsize=100mb, + filegrowth=10mb +) +go + +use Students +go +create table StuInfo +( + StuID int primary key identity(1,1) not null, + StuNum varchar(10) not null, + StuName nvarchar(20) not null, + StuSex char(2) default('男') check(StuSex='男' or StuSex='女') not null, + StuPhone char(11), +) +create table ClassInfo +( + ClassID int primary key identity(1,1) not null, + ClassNum nvarchar(15) not null, + ClassName nvarchar(30) not null, + ClassRemark text, +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..82b9507a20acf4fc099c1b3a7bb445caef98f521 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" @@ -0,0 +1,41 @@ +use master +go +create database Students +on +( + name='Students', + filename='D:\Students.mdf', + size=5MB, + maxsize=100MB, + filegrowth=100MB +) +log on +( + name='Students_log', + filename='D:\Students_log.ldf', + size=5MB, + maxsize=100MB, + filegrowth=100MB +) +go + +use Students +go +create table StuInfo +( + StuID int primary key identity(1,1) not null, + StuNum char(20) not null, + StuName char(40) not null, + StuSex char(2) default('男') check(StuSex='男'or StuSex='女') not null, + StuPhone varchar(11) +) +use Students + go + create table ClassInfo + ( + ClassID int primary key identity(1,1) not null, + ClassNum char(15) not null, + ClassName char(30) not null, + ClassRemark text, + StuID int not null + ) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery3.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..af3d0c8cb4c0c949b40540870ee505c01e6681ec --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery3.sql" @@ -0,0 +1,43 @@ +锘縰se master +go if exists(select * from sys.databases where name='Students') + drop database Students +create database Students +on +( + name='Students', + filename='C:\Users\33054\Desktop', + size=6MB, + maxsize=100MB, + filegrowth=10MB +) +log on +( + name='Students', + filename='C:\Users\33054\Desktop', + size=6MB, + maxsize=100MB, + filegrowth=10MB +) +go + +use Students + +create table StuInfo +( + StuID int primary key identity(1,1) not null, + StuNum varchar(10) not null, + StuName nvarchar(20) not null, + StuSex char(2) default('') check(StuSex=''or StuSex='女') not null, + StuPhone varchar(11) not null +) +go + +use Students + + create table ClassInfo + ( + ClassID int primary key identity(1,1) not null, + ClassNum char(15) not null, + ClassName char(30) not null, + ClassRemark text + ) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d2fea41af136d10e09d7ffa795c7120e6904f573 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" @@ -0,0 +1,44 @@ +if exists(select * from sys.databases where name='Students') + drop database Students +create database Students +on +( + name='Students', + filename='C:\学习\数据库\Students.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10MB +) +log on +( + name='Students_log', + filename='C:\学习\数据库\Students_log.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10MB +) +go + +use Students +go + +create table StuInfo +( + StuID int primary key identity(1,1) not null, + StuNum char(10) not null, + StuName nvarchar(20) not null, + StuSex nchar(1) default('男') check(StuSex='男'or StuSex='女') not null, + Stuphone char(11), +) +go + +use Students +create table ClassInfo +( + ClassID int primary key identity(1,1) not null, + ClassNum char(15) not null, + ClassName nchar(30) not null, + ClassRemark text, + StuID int not null +) + diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..222ad091dde6dc75c716a4c5931fe7e74c367f58 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" @@ -0,0 +1,43 @@ + + +create database Students +on +( + name='Students', + filename='F:\Students.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10MB +) + +log on +( + name='Students_log', + filename='F:\Students_log.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10MB +) +go + +use Students +go + +create table StuInfo +( +StuId int primary key identity(1,1), +StuNum varchar(10) not null, +StuName nvarchar(20) not null, +Stusex char(2) default('男') check(Stusex='男' or Stusex='女'), +StuPhone bigint , +) +create table ClassInfo +( + ClassID int primary key identity(1,1), + ClassNum varchar(15) not null, + Classname nvarchar(30) not null, + ClassRemark ntext, + StuID int, + + +) diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQL\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/SQL2.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQL\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/SQL2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e2d3692aa4b12711fa4e66f360597d0a2dfbafaf --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQL\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/SQL2.sql" @@ -0,0 +1,21 @@ +create database Students +on +( + name='Students', + filename='D:\SQL安装包\MSSQL15.MSSQLSERVER\MSSQL\DATA.mdf' + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='Students_log', + filename='D:\SQL安装包\MSSQL15.MSSQLSERVER\MSSQL\DATA_log.ldf' + size=6MB, + maxsize=100MB, + filegrowth=10Mb +) +go + +use Students +go diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQL\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/SQLQuery7.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQL\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f4b3595586ed14eb99119bf62c5c356cb298b485 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQL\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/SQLQuery7.sql" @@ -0,0 +1,9 @@ +create table Stuinfo +( +--列名 数据类型 约束, + StuID int primary key identity(1,1) not null, + StuNum nvarchar(10) not null, + StuName nvarchar(20) not null, + StuSex char(2) default('男') check(StuSex='男' or StuSex='女') not null, + Stuiphone varchar(11), +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQL\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/SQLQuery8.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQL\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/SQLQuery8.sql" new file mode 100644 index 0000000000000000000000000000000000000000..027119316acd0931fae1b385d5cb08508fdf1c8b --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQL\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/SQLQuery8.sql" @@ -0,0 +1,9 @@ +create table Classinfo +( + --列名 数据类型 约束, + ClassID int primary key identity(1,1) not null, + ClassName nvarchar(20) not null, + ClassNum varchar(15), + ClassRemark nvarchar(50), + StuID int, +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\255\244\347\224\265\350\204\221 - \345\277\253\346\215\267\346\226\271\345\274\217.lnk" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\255\244\347\224\265\350\204\221 - \345\277\253\346\215\267\346\226\271\345\274\217.lnk" new file mode 100644 index 0000000000000000000000000000000000000000..baec170f62dd8aaf366183f46fbe2d67be7ed431 Binary files /dev/null and "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\255\244\347\224\265\350\204\221 - \345\277\253\346\215\267\346\226\271\345\274\217.lnk" differ diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8d649f679a9496675643fcfbfad66bb85aebb89d --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" @@ -0,0 +1,43 @@ + + +create database GIAO +on +( + name='GIAO', + filename='D:\GIAO.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10MB +) + +log on +( + name='GIAO', + filename='D:\GIAO.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10MB +) +go + +use GIAO +go + +create table StuInfo +( +StuId int primary key identity(1,1), +StuNum varchar(10) not null, +StuName nvarchar(20) not null, +Stusex char(2) default('nan') check(Stusex='nan' or Stusex='nv'), +StuPhone bigint , +) +create table ClassInfo +( + ClassID int primary key identity(1,1), + ClassNum varchar(15) not null, + Classname nvarchar(30) not null, + ClassRemark ntext, + StuID int, + + +) diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6d34c80fe8d3ab71ceef33e730f66957c82e60c3 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" @@ -0,0 +1,41 @@ +use master +go +create database Students +on +( + name='Students', + filename='D:\Students.mdf', + size=5MB, + maxsize=100MB, + filegrowth=100MB +) +log on +( + name='Students_log', + filename='D:\Students_log.ldf', + size=5MB, + maxsize=100MB, + filegrowth=100MB +) +go + +use Students +go +create table StuInfo +( + StuID int primary key identity(1,1) not null, + StuNum char(20) not null, + StuName char(40) not null, + StuSex char(2) default('男') check(StuSex='男'or StuSex='女') not null, + StuPhone char(11) +) +use Students + go + create table ClassInfo + ( + ClassID int primary key identity(1,1) not null, + ClassNum char(15) not null, + ClassName char(30) not null, + ClassRemark text, + StuID int not null + ) diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery.sql" new file mode 100644 index 0000000000000000000000000000000000000000..84fb48581c80d111f0df8258738accfb9da2cc79 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery.sql" @@ -0,0 +1,39 @@ +create database Students +on +( + name='Students', + filename='F:\Students.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10MB +) + +log on +( + name='Students_log', + filename='F:\Students_log.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10MB +) +go + +use Students +go + +create table StuInfo +( +StuId int primary key identity(1,1), +StuNum varchar(10) not null, +StuName nvarchar(20) not null, +Stusex char(2) default('男') check(Stusex='男' or Stusex='女'), +StuPhone bigint , +) +create table ClassInfo +( + ClassID int primary key identity(1,1), + ClassNum varchar(15) not null, + Classname nvarchar(30) not null, + ClassRemark ntext, + StuID int, +) diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fd391dca614986618a5f61be10208b1ce91902a2 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" @@ -0,0 +1,37 @@ + +create database Students +on +( + name='Students', + filename='D:\Students.mdf', + size=8mb, + maxsize=64mb, + filegrowth=10mb +) +log on +( + name='Students_log', + filename='D:\Students_log.ldf', + size=8mb, + maxsize=64mb, + filegrowth=10mb +) +go + +use Students +go +create table StuInfo +( + StuID int primary key identity(1,1) not null, + StuNum varchar(10) not null, + StuName nvarchar(20) not null, + StuSex char(2) default('男') check(StuSex='男' or StuSex='女') not null, + StuPhone nvarchar(11), +) +create table ClassInfo +( + ClassID int primary key identity(1,1) not null, + ClassNum nvarchar(15) not null, + ClassName nvarchar(30) not null, + ClassRemark text, +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4ed12cd950db1c882f879d189011a2a6cd8c3985 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" @@ -0,0 +1,26 @@ + +use master +Create database mioer +on( +name='mioer', +filename='D:\mioer.mdf', +Size=5Mb, +Maxsize=10Mb, +Filegrowth=5Mb +) +log on( +name='mioer_LOG', +filename='D:\mioer_log.ldf', +Size=5Mb, +Maxsize=10Mb, +Filegrowth=5Mb +) +go +use master +go +Create table mioer +( +StudentID int check(StudentID >0) not null, +StudentAge int check(StudentAge>18 and StudentAge<23) not null, +StudentSex char(2) default('男') not null, +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..862b49a197c0001619b741abc7df7d97abbea483 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" @@ -0,0 +1,40 @@ +use master +go + +create database Students +on( + name='Students', + filename='C:\TEXT\Students.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10MB +) +log on( + name='Students_log', + filename='C:\TEXT\Students_log.ldf', + size=5MB, + maxsize=100MB, + filegrowth=10MB +) +go +use Students +go +create table StuInfo +( + StuID int primary key identity(1,1) not null, + StuNum varchar(10) not null, + StuName nvarchar(20) not null, + StuSex char(2) default('男') check(StuSex='男' or StuSex='女') not null, + StuPhone varchar(20) not null +) +go +use Students +go +create table ClassInfo +( + ClassID int primary key identity(1,1) not null, + ClassNum varchar(15) not null, + ClassName nvarchar(30) not null, + ClassRemark ntext not null, + StuID int not null +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery4.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ac49c7824871587a3766acd301c72ccd5885e63c --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery4.sql" @@ -0,0 +1,44 @@ +use master +go + +if exists(select * from sys.databases where name='Students') + drop database Students + +create database Students +on +( + name='Students', + filename='D:\sql数据库demo\Students.mdf', + size=10MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='Students_log', + filename='D:\sql数据库demo\Students_log.ldf', + size=10MB, + maxsize=100MB, + filegrowth=10Mb +) +go + +use Students +go + +create table StuInfo +( + StuID int primary key identity(1,1) not null, + StuNum nvarchar(10) not null, + StuName nvarchar(20) not null, + Stusex char(2) default('男') check(Stusex='男' or Stusex='女') not null, + StuPhone char(11) +) +create table ClassInfo +( + ClassID int primary key identity(1,1) not null, + ClassNum nvarchar(15) not null, + ClassName nvarchar(30) not null, + ClassRemark text, + StuID int not null, +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/\344\270\200\345\272\223\344\270\244\350\241\250.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/\344\270\200\345\272\223\344\270\244\350\241\250.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6d1f39365b83a06d7fa6dbe4105e91229fae9ebb --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/\344\270\200\345\272\223\344\270\244\350\241\250.sql" @@ -0,0 +1,26 @@ +create database Students +go +use Students +go + +create table StuIfo +( + StuId int primary key identity(1,1), + StuNum char(10) not null, + StuName varchar(20) not null, + StuSex char(2) default '男' check (StuSex in ('男','女')), + StuPhone varchar(20) +) + +go + +create table ClassInfo +( + ClassId int primary key identity(1,1), + ClassNum char(15) not null, + ClassName varchar(30) not null, + ClassRemark varchar(max), + StuId int not null +) + +go \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e0413c4f8e3e2c4f5299136f2274bdd03a9510c7 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" @@ -0,0 +1,33 @@ +create database Students +on PRIMARY +(filename='D:\homework\Students.mdf', + name='Lesson2homework', + size=6MB, + maxsize=8MB, + filegrowth=10% +) + +log on +( +name='Students_log', +filename='D:\homework\Students_log.ldf', +size=6MB, +maxsize=8MB, +filegrowth=1MB +) +go + +--create a form +use Students +go +create table StuInfo +(StuNo int primary key identity(1,1) not null, +StuName nvarchar(20) not null, +StuSex char(2) default('') not null, +StuPocketMoney money check(StuPocketMoney>=0 and StuPocketMoney<=1500), +) + +create table ClassInfo +(ClaNo int primary key identity(1,1) not null, +ClaNumPe int not null +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery2.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ad0ca1cfab56673aa558d8ddd60b41dd9647865a --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery2.sql" @@ -0,0 +1,44 @@ +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 +( + StuID int primary key identity(1,1), + StuNum varchar(10) not null, + StuName nvarchar(20) not null, + StuSex char(2) default('男') check(StuSex='男' or StuSex='女') not null, + StuPhone char(11) check(StuPhone=11 and StuPhone=7) not null, +) +go + +use Student + +create table ClassInfo +( + ClassID int primary key identity(1,1), + ClassNum varchar(15) not null, + ClassName nchar(30) not null, + ClassRemark text not null, +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye.sql" new file mode 100644 index 0000000000000000000000000000000000000000..983656757b143a7dc4bb1b6820dbed8c91c2ba65 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye.sql" @@ -0,0 +1,43 @@ +锘 +use master +go +create database zuoye +on +( + name='zuoye', + filename='D:\test\zuoye.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10MB +) +log on +( + name='zuoye_log', + filename='D:\test\zuoye_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10MB +) +go + +use zuoye + +create table StuInfo +( + StuID int primary key identity(1,1), + StuNum varchar(10) not null, + StuName nvarchar(20) not null, + StuSex char(2) default('') check(StuSex='' or StuSex='女') not null, + StuPhone char(11) check(StuPhone=11 and StuPhone=7) not null, +) +go + +use zuoye + +create table ClassInfo +( + ClassID int primary key identity(1,1), + ClassNum varchar(15) not null, + ClassName nchar(30) not null, + ClassRemark text not null, +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fd391dca614986618a5f61be10208b1ce91902a2 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" @@ -0,0 +1,37 @@ + +create database Students +on +( + name='Students', + filename='D:\Students.mdf', + size=8mb, + maxsize=64mb, + filegrowth=10mb +) +log on +( + name='Students_log', + filename='D:\Students_log.ldf', + size=8mb, + maxsize=64mb, + filegrowth=10mb +) +go + +use Students +go +create table StuInfo +( + StuID int primary key identity(1,1) not null, + StuNum varchar(10) not null, + StuName nvarchar(20) not null, + StuSex char(2) default('男') check(StuSex='男' or StuSex='女') not null, + StuPhone nvarchar(11), +) +create table ClassInfo +( + ClassID int primary key identity(1,1) not null, + ClassNum nvarchar(15) not null, + ClassName nvarchar(30) not null, + ClassRemark text, +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery4.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c5cb91772785ab13b1cda33b10624c916e5019de --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery4.sql" @@ -0,0 +1,37 @@ +if exists(select * from sys.databases where name ='Students') + drop database Students +create database Students +on( + name='Students', + filename='D:\Students.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10MB +) +log on +( +name='Students_log', + filename='D:\Students_log.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10MB +) +go +use Students +go +create table StuInfo +( +StuID int primary key identity(1,1) not null, +StuNum nvarchar(20) not null, +StuName nvarchar(20) not null, +Stusex char(2) default('男') check(Stusex='男' or Stusex='女') not null, +StuPhone char(11) +) +create table ClassInfo +( +ClassID int primary key identity(1,1) not null, +ClassNum nvarchar(15) not null, +ClassName nvarchar(30) not null, +ClassRemark text, +StuID int not null, +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6bee555f5f501309c0404269fb2af8856dbe52dc --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery1.sql" @@ -0,0 +1,40 @@ +use master +go +create database Students +on( + name='Students', + filename='D:\SQL\Students.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10MB +) +log on( + name='Students_log', + filename='D:\SQL\Students_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10MB +) +go +use Students +go +create table StuInfo +( + StuID int primary key identity(1,1) not null, + StuNum char(10) not null, + StuName nchar(20) not null, + StuSex char(2), + StuPhone char(11) not null + +) +go +use Students + +create table ClassInfo +( + ClassID int primary key identity(1,1), + ClassNum varchar(15) not null, + ClassName nchar(30) not null, + ClassRemark text not null, +) +go \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9a0117c4c8b0caff8f9d3fb45aee925e6ca31743 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" @@ -0,0 +1,45 @@ +use master +create database Students +on primary +( + name=Students, + filename='D:\Document\MSSQLDatabase\Students\Students.mdf', + size=5MB, + maxsize=50MB, + filegrowth = 1Mb +) + +log on +( + name=Students_Log, + filename='D:\Document\MSSQLDatabase\Students\Students_Log.ldf', + size=1MB, + maxsize=10MB, + filegrowth=10% +) + +go + +use Students +create table StuInfo +( + StuID int identity(1,1) primary key, + StuNum char(10) not null, + StuName nchar(20) not null, + StuSex nchar(1) default('男') check(StuSex='男' or StuSex='女'), + StuPhone bigint check(StuPhone>=1000000 and StuPhone<=999999999 or StuPhone >=10000000000 and StuPhone <=99999999999) +) + +go + +use Students +create table ClassInfo +( + ClassID int identity(1,1) primary key, + ClassNum char(15) not null, + ClassName nchar(30) not null, + ClassRemark text, + StuID int constraint FK_StuID_StuInfo references StuInfo(StuID) +) + +go diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..03489358a474cfd98be4f5c854003871d3e94958 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/SQLQuery1.sql" @@ -0,0 +1,24 @@ +if exists(select * from sys.databases where name='Students') + drop database Students + +create database Students +go +use Students +go +create table Stulnfo +( + StuID int primary key identity(1,1), + StuNum varchar(10) not null, + StuSex nchar(1) default('男') check(StuSex='男' or StuSex='女'), + ClassName nvarchar(30) not null, + ClassRemark ntext +) +create table Classlnfo +( + ClassID int primary key identity(1,1), + ClassNum varchar(15)not null, + ClassName nvarchar(30) not null, + ClassRemark ntext , + StuID int, + foreign key (StuID) references Stulnfo(StuID) +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6cc626b60ec20f1879e6f8084252f964b366677b --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241.sql" @@ -0,0 +1,50 @@ +use master +go +create database Students +on( +name= 'Students', +filennaem='C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\Students.mdf', +size=5MB, +maxsize=100MB, +filegrowth=10MB +) +log on( +name='Students_log', +filennaem='C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\Students_log.ldf', +size=5MB, +maxsize=100MB, +filegrowth=10MB + ) +go +use Students +go +create table StuInfo +( +StuID int primary key identity(1,1) not null, +StuNum varchar(10) not null, + +StuName nvarchar (20) not null, + +StuSex char (2) default(' 男') check (StuSex=' 男' or StuSex='女')not null, +StuPhone varchar (20) not null +) +go + +use Students + +go + +create table ClassInfo +( +ClassID int primary key identity(1,1) not null,ClassNum varchar(15) not null, + +ClassName nvarchar (30) not null, + +ClassRemark ntext not null, + +StuID int not null + +) + + + diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery3.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..350e6f8a9aada2ee7b405f6af5affdf4edf4c413 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery3.sql" @@ -0,0 +1,20 @@ +create database sutdentInfo +go +use sutdentInfo +go +create table Stulnfo +( --列名,数据类型,设置主键,自增(起始1,增量1),设置非空 + StuID int primary key identity(1,1) not null, + StuNum char(10) not null, + StuName nvarchar(30) not null, + StuSex char(2) default('男') check(StuSex='男' or StuSex='女') not null, + StuPhone text, +) +create table Classlnfo +( + ClassID int primary key identity(1,1) not null, + ClassNum char(30) not null, + ClassName char(60) not null, + ClassRemark text, + StuID nchar, +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..222ad091dde6dc75c716a4c5931fe7e74c367f58 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" @@ -0,0 +1,43 @@ + + +create database Students +on +( + name='Students', + filename='F:\Students.mdf', + size=6MB, + maxsize=100MB, + filegrowth=10MB +) + +log on +( + name='Students_log', + filename='F:\Students_log.ldf', + size=6MB, + maxsize=100MB, + filegrowth=10MB +) +go + +use Students +go + +create table StuInfo +( +StuId int primary key identity(1,1), +StuNum varchar(10) not null, +StuName nvarchar(20) not null, +Stusex char(2) default('男') check(Stusex='男' or Stusex='女'), +StuPhone bigint , +) +create table ClassInfo +( + ClassID int primary key identity(1,1), + ClassNum varchar(15) not null, + Classname nvarchar(30) not null, + ClassRemark ntext, + StuID int, + + +) diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery2.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..84d55dc18b84b85186ed843d52dff3282f1c3b7e --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery2.sql" @@ -0,0 +1,26 @@ + +use master go +Create database demo678 +on( +name='demo678', +filename='D:\', +size='2mb', +filegrowth='1mb', +maxsize='5mb' +) +log on( +name='demo678', +filename='D:\', +size='2mb', +filegrowth='1mb', +maxsize='5mb' +) +go +use demo678 +go +Create table Student +( +StudentID int check(StudentID >0) not null, +StudentAge int check(StudentAge>18 and StudentAge<23) not null, +StudentSex char(2) default('男') not null, +) \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.sql" "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0ae6c680cd923b6c706a01c8818a30e715ddc66b --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.sql" @@ -0,0 +1,45 @@ + +--事务,表示数据库操作的基本单元,一波操作,原子性 +if exists(select * from sys.databases where name='Students') + drop database Students + --判断这个数据库是否存在,如果存在则删除原有数据库 + +Create database Students +--新建数据库 +on( + name='Students', + filename='F:\Students.mdf', + size=10MB, + maxsize=100MB, + filegrowth=10MB +) +log on +( + name='Students_log', + filename='F:\Students_log.ldf', + size=10MB, + maxsize=100MB, + filegrowth=10MB +) +--定义数据库的属性 + +use Students --直接建在这个数据库里 +go + +create table StuInfo +( + --列名,数据类型,约束 + StuID int primary key identity(1,1) not null, + StuNum varchar(10)not null , + StuName nvarchar(20) not null, + StuSex char(2) default('男') check(StuSex='男' or StuSex='女') not null, + StuPhone bigint +) +create table ClassInfo +( + ClassID int primary key identity(1,1)not null, + ClassNum varchar(15)not null, + ClassName nvarchar(30)not null, + CkassRenark text , + StuID int, +) \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery.sql" new file mode 100644 index 0000000000000000000000000000000000000000..60940881524fcd0f3732701e1f315da164ffa282 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery.sql" @@ -0,0 +1,154 @@ +use master +go + +create database Students +on +( + name = 'Students', + filename = 'D:\数据库\Students.mdf' +) + +log on +( + name = 'Students_log', + filename = 'D:\数据库\Students_log.ldf' +) + +use Students +go + +--按图片参考,进行数据表的建立和数据插入,然后进行以下查询操作 +create table StuInfo( + stuNo nvarchar(10) primary key, + stuName nvarchar(10)not null, + stuAge int not null, + stuAddress nvarchar(20) null, + stuSeat int not null, + stuSex int not null +) +go + +insert into Student(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) +go + +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 +) +go + +insert into Exam(stuNo,writtenExam,labExam) values +('s2501',50,70),('s2502',60,65),('s2503',86,85), +('s2504',40,80),('s2505',70,90),('s2506',85,90) + +go + + +--1.查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 +select stuNo as 学号 ,stuName as 姓名,stuAge as 年龄,stuAddress as 地址,stuSeat as 座位号,stuSex as 性别 from StuInfo +--在本来的名字基础上,在后面+as 中文名称即可 + +--2.查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 +select stuName,stuAge,stuAddress from StuInfo +--套用方法:select * from 表名 然后将*改为你需要的列即可,列名之间要逗号隔开 + +--3.查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字注意:要用三种方法 +select stuNo as 学号,writtenExam as 笔试,labExam as 机试 from stuExam + +--4.查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱” +select stuNo,stuName,stuAddress ,stuName+'@'+stuAddress as 邮箱 from StuInfo + +--5.查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分(笔试+机试)这四列的信息 +select stuNo,writtenExam,labExam,writtenExam+labExam as 总分 from stuExam + +--套用方法:select * from 表名 然后将*改为你需要的列即可,列名之间要逗号隔开,将要求列相加as重命名即可 + +--6.查询学生信息表(stuInfo)中学生来自哪几个地方 +select stuName, stuAddress from StuInfo + +--7.查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名'所有年龄' +select stuAge as 年龄, count(*) as 所有年龄 from StuInfo group by stuAge + + +--select 所需列名 ,(逗号隔开) count(*)表示为这一列所以信息 from 表 group by 聚合函数,进行统计分类 +--8.查询学生信息表(stuInfo)中前3行记录 +select top 3 * from StuInfo + +--套用方法:select * from 表名 在*前加上top 要几行就写几 + +--9.查询学生信息表(stuInfo)中前4个学生的姓名和座位号 +select top 4 stuName as 姓名,stuSeat as 座位号 from StuInfo + +--10.查询学生信息表(stuInfo)中一半学生的信息 + +select top 50 percent * from StuInfo + + + +--11.将地址是湖北武汉,年龄是20的学生的所有信息查询出来 +select * from StuInfo where stuAddress='湖北武汉' and stuAge=20 + +--条件查询 where后面接所需的条件,多个条件用and隔开 + +--12.将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列 +select * from stuExam where labExam between 60 and 80 order by labExam desc + +--条件查询 取值范围用between...and...链接,order by进行排序,默认为升序排序(ASC)可不写,降序为(desc) + +--13.查询来自湖北武汉或者湖南长沙的学生的所有信息(用两种方法实现,提示;or和in) +select * from StuInfo where stuAddress='湖北武汉' or stuAddress= '湖南长沙' + +--同一条件多个参数用or隔开 + +select * from StuInfo where stuAddress in ('湖北武汉','湖南长沙') + + +--14.查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列 +select * from stuExam where writtenExam not between 70 and 90 order by writtenExam asc + +--15.查询年龄没有写的学生所有信息 +select * from StuInfo where stuAge=null or stuAge=' ' + +--16.查询年龄写了的学生所有信息 +select * from StuInfo where stuAge is not null + +--is not null为不是空的 + + +--17.查询姓张的学生信息 +select * from StuInfo where stuName like '%张%' + +--模糊查询用like 单引号内为前后各一个百分号 + +--18.查询学生地址中有‘湖’字的信息 +select * from StuInfo where stuAddress like '%湖%' + +--19.查询姓张但名为一个字的学生信息 +select * from StuInfo where stuName like '%张_' + +--模糊查询中,_下划线为占位符,表达为后面一个字,或者前面几个字 + +--20.查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 +select * from StuInfo where stuName like '%__俊%' + +--21.按学生的年龄降序显示所有学生信息 +select * from StuInfo order by stuAge desc + +--22.按学生的年龄降序和座位号升序来显示所有学生的信息 +select * from StuInfo order by stuAge desc,stuSeat + +--23显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 + +select top 1 * from stuExam order by writtenExam desc + +--将排名进行降序排序找到第一个就是第一名 + + +--24.显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from stuExam order by labExam \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c25d253227d66deddbe707f4fb040ce49ffae795 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,59 @@ +use master +go + +create database Student +go +use Student +create table stuinfo +( +StuNo char(5), +StuName nvarchar(5), +StuAge int, +StuAddress nvarchar(20), +StuSeat int, +StuSex nvarchar(1) default('1') check(StuSex in('1','0')) +) +insert into stuinfo values ('$2501','张秋利',20,'美国硅谷',1,1),('$2502','李斯文',18,'湖北武汉',2,0), +('$2503','马文才',22,'湖南长沙',3,1),('$2504','欧阳俊雄',21,'湖北武汉',4,0),('$2505','梅超风',20,'湖北武汉',5,1), +('$2506','陈旋风',19,'美国硅谷',6,1),('$2507','陈风',20,'美国硅谷',7,1) +go +use Student +create table StuMark +( +examNo int, +StuNO char(5), +writtenExam int, +labExam int +) +insert into StuMark 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 StuMark +select StuNo 编号,StuName 名字,StuAge 年龄,StuAddress 地址,StuSeat 学号,StuSex 性别 from stuinfo +select StuAge,StuAddress,StuSex from stuinfo +select StuNo 学号, writtenExam 笔试, labExam 机试 from StuMark +select 学号=StuNO ,笔试=writtenExam,机试=labExam from StuMark +select StuNO as 学号,writtenExam as 笔试,labExam as 机试 from StuMark +select StuNO+StuName+StuAddress 学号名字地址 from stuinfo +select StuName+'@'+StuAddress as 邮箱 from stuinfo +select StuNo 学号, writtenExam 笔试, labExam 机试,writtenExam + labExam 总分 from StuMark +select distinct StuAddress from stuinfo +select distinct StuAge,stuage 全部年龄 from stuinfo +select * from stuinfo where StuSeat<=3 +select StuNo 座位号,StuName 名字 from stuinfo where StuSeat<=4 +select top 50 percent * from stuinfo +select * from stuinfo where StuAge=20 and StuAddress='湖北武汉' +select * from StuMark 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 StuMark where writtenExam<70 or writtenExam>90 order by writtenExam ASC +select * from stuinfo where StuAge is null or StuAge='' +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, StuNo ASC +select top 1* from StuMark order by writtenExam DESC +select top 1* from StuMark order by writtenExam ASC \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..823bf69eb272e039c5caa6f42b06278764753473 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" @@ -0,0 +1,83 @@ +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 diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery3.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..29d551212c02e76748be6bc768444f7f57db5d88 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery3.sql" @@ -0,0 +1,148 @@ +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,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) +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 + + +--1.查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 +select StuNo as 学号 ,StuName as 姓名,StuAge as 年龄,StuAdress as 地址,StuSeat as 座位号,StuSex as 性别 from StuInfo +--在本来的名字基础上,在后面+as 中文名称即可 + +--2.查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 +select StuName,StuAge,StuAdress from StuInfo +--套用方法:select * from 表名 然后将*改为你需要的列即可,列名之间要逗号隔开 + +--3.查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字注意:要用三种方法 +select StuNo as 学号,WrittenExam as 笔试,LabExam as 机试 from Score + +--4.查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱” +select StuNo+StuName+StuAdress +StuName+'@'+StuAdress as 邮箱 from StuInfo + +--5.查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分(笔试+机试)这四列的信息 +select StuNo,WrittenExam,LabExam,WrittenExam+LabExam as 总分 from Score +--套用方法:select * from 表名 然后将*改为你需要的列即可,列名之间要逗号隔开,将要求列相加as重命名即可 + +--6.查询学生信息表(stuInfo)中学生来自哪几个地方 +select StuName, StuAdress from StuInfo + +--7.查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名'所有年龄' +select StuAge as 所有年龄 from StuInfo + +--8.查询学生信息表(stuInfo)中前3行记录 +select top 3 * from StuInfo +--套用方法:select * from 表名 在*前加上top 要几行就写几 + +--9.查询学生信息表(stuInfo)中前4个学生的姓名和座位号 +select top 4 StuName as 姓名,StuSeat as 座位号 from StuInfo + +--10.查询学生信息表(stuInfo)中一半学生的信息 +select top 50 percent * from StuInfo + +--11.将地址是湖北武汉,年龄是20的学生的所有信息查询出来 +select * from StuInfo where stuAdress='湖北武汉' and StuAge=20 +--条件查询 where后面接所需的条件,多个条件用and隔开 + +--12.将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列 +select * from Score where LabExam between 60 and 80 order by LabExam desc +--条件查询 取值范围用between...and...链接,order by进行排序,默认为升序排序(ASC)可不写,降序为(desc) + +--13.查询来自湖北武汉或者湖南长沙的学生的所有信息(用两种方法实现,提示;or和in) +select * from StuInfo where stuAdress='湖北武汉' or stuAdress= '湖南长沙' +--同一条件多个参数用or隔开 +select * from StuInfo where stuAdress in ('湖北武汉','湖南长沙') + +--14.查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列 +select * from Score where WrittenExam not between 70 and 90 order by WrittenExam asc + +--15.查询年龄没有写的学生所有信息 +select * from StuInfo where StuAge=null or StuAge=' ' + +--16.查询年龄写了的学生所有信息 +select * from StuInfo where StuAge is not null +--is not null为不是空的 + +--17.查询姓张的学生信息 +select * from StuInfo where StuName like '%张%' +--模糊查询用like 单引号内为前后各一个百分号 + +--18.查询学生地址中有‘湖’字的信息 +select * from StuInfo where StuAdress like '%湖%' + +--19.查询姓张但名为一个字的学生信息 +select * from StuInfo where StuName like '%张_' +--模糊查询中,_下划线为占位符,表达为后面一个字,或者前面几个字 + +--20.查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 +select * from StuInfo where StuName like '%__俊%' + +--21.按学生的年龄降序显示所有学生信息 +select * from StuInfo order by StuAge desc + +--22.按学生的年龄降序和座位号升序来显示所有学生的信息 +select * from StuInfo order by StuAge desc,StuSeat + +--23显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from Score order by WrittenExam desc +--将排名进行降序排序找到第一个就是第一名 + +--24.显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from Score order by LabExam \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f68793cc2ac719f3f70be9e40b991bd6dd948790 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy.sql" @@ -0,0 +1,86 @@ +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) + + 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.查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 + select stuNo 学号,stuName 姓名,stuAge 年龄,stuAddress 地址,stuSeat 座位号,stuSex 性别 from StuInfo + --2.查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 + select stuName,stuAge,stuAddress from StuInfo + --3.查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字 注意:要用三种方法 + select examNo 学号,writtenExam 笔试,labExam 机试 from StuExam + -- + -- + --4.查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱” + alter table StuInfo alter column stuAddress char(8) + select stuName+'@'+stuAddress 邮箱 from StuInfo + --5.查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分(笔试+机试)这四列的信息 + select examNo 学号,writtenExam 笔试,labExam 机试,writtenExam+labExam 总分 from StuExam + --6.查询学生信息表(stuInfo)中学生来自哪几个地方 + select distinct stuAddress from StuInfo + --7.查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名'所有年龄' + select distinct stuAge 所有年龄 from StuInfo + --8.查询学生信息表(stuInfo)中前3行记录 + select top 3 * from StuInfo + --9.查询学生信息表(stuInfo)中前4个学生的姓名和座位号 + select top 4 stuName,stuSeat from StuInfo + --10.查询学生信息表(stuInfo)中一半学生的信息 + select top 4 * from stuInfo + --11.将地址是湖北武汉,年龄是20的学生的所有信息查询出来 + select * from StuInfo where stuAge=20 and stuAddress='湖北武汉' + --12.将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列 + select * from StuExam where labExam>=60 and labExam<=80 order by labExam DESC + --13.查询来自湖北武汉或者湖南长沙的学生的所有信息(用两种方法实现,提示;or和in) + select * from StuInfo where stuAddress='湖北武汉' or stuAddress='湖南长沙' + select * from StuInfo where stuAddress in('湖北武汉','湖南长沙') + --14.查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列 + select * from StuExam where not writtenExam>=70 and writtenExam<=90 order by writtenExam ASC + --15.查询年龄没有写的学生所有信息 + select * from StuInfo where stuAge is null or stuAge='' + --16.查询年龄写了的学生所有信息 + select * from StuInfo where stuAge is not null and not stuAge='' +--17.查询姓张的学生信息18.查询学生地址中有‘湖’字的信息 + select * from StuInfo where stuName like '张%' and stuAddress like '湖%' +--19.查询姓张但名为一个字的学生信息 + select * from StuInfo where stuName like '张_' +--20.查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 + select * from StuInfo where stuName like '%俊_' +--21.按学生的年龄降序显示所有学生信息 + select * from StuInfo order by stuAge DESC +--22.按学生的年龄降序和座位号升序来显示所有学生的信息 + select * from StuInfo order by stuAge ASC +--23显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 + select top 1 * from StuExam order by writtenExam DESC +--24.显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 + select top 1 * from StuExam order by labExam ASC \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery5.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..69b4c220cc11eacd2af95d57b04d97076771f91e --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery5.sql" @@ -0,0 +1,67 @@ +use Student2 +go + +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','男') + +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 diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0ec223bee7e859c4ebb3dc6a1ea6d58681a050a9 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery1.sql" @@ -0,0 +1,92 @@ +use master +go +create database task +go +use task +go +create table Stun0 +( +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 Stun0 +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 Stun0 +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 + + +select * from Stun0 +select stuid as 学号, stuname as 姓名, stuage as 年龄, stuaddress as 地址,stuseat as 座号,stusex as 性别 from Stun0 + +select stuname,stuage,stuaddress from Stun0 + +select stuid as 学号, writtenexam as 笔试 ,labexam as 机试 from result +select stuid 学号,writtenexam 笔试, labexam 机试 from result + +select stuid+stuname+'@'+ stuname+stuaddress as 邮箱 from Stun0 + +select stuid,writtenexam,labexam,writtenexam +labexam as 总分 from result + +select stuaddress from Stun0 + +select stuage as 所有年龄 from Student ORDER By stuage + +select TOP 3 * from Stun0 + +SELECT TOP 4 stuname,stusex from Stun0 + +select top 50percent * from Stun0 + +select *from Student WHERE stuaddress='湖北武汉' or stuage='20' + +select labexam from result where labexam>=60 and labexam<=80 order by labexam + +select * from Student where stuaddress='湖北武汉' or stuaddress='湖南长沙' +select * from Student where stuaddress in('湖北武汉','湖南长沙') + +select writtenexam from result where not writtenexam>=70 and writtenexam<=90 order by writtenexam desc + +select * from Student where stuage is null or stuage='' + +select* from Student where stuage is not null or 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 + +select * from Student order by stuage desc , stuseat asc + +select top 1 * from result order by writtenexam desc + +select top 1 * from result order by labexam diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery5.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b247a20329a0592fee186483b536fda51b1e6231 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery5.sql" @@ -0,0 +1,94 @@ +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.查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 +select * from Student +select stuid as 学号, stuname as 姓名, stuage as 年龄, stuaddress as 地址,stuseat as 座号,stusex as 性别 from Student +--2.查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 +select stuname,stuage,stuaddress from Student +--3.查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字 +-- 注意:要用三种方法 +select stuid as 学号, writtenexam as 笔试 ,labexam as 机试 from result +select stuid 学号,writtenexam 笔试, labexam 机试 from result +--4.查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱” +select stuid+stuname+'@'+ stuname+stuaddress as 邮箱 from Student +--5.查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分(笔试+机试)这四列的信息 +select stuid,writtenexam,labexam,writtenexam +labexam as 总分 from result +--6.查询学生信息表(stuInfo)中学生来自哪几个地方 +select stuaddress from Student +--7.查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名'所有年龄' +select stuage as 所有年龄 from Student ORDER By stuage +--8.查询学生信息表(stuInfo)中前3行记录 +select TOP 3 * from Student +--9.查询学生信息表(stuInfo)中前4个学生的姓名和座位号 +SELECT TOP 4 stuname,stusex from Student +--10.查询学生信息表(stuInfo)中一半学生的信息 +select top 50percent * from Student +--11.将地址是湖北武汉,年龄是20的学生的所有信息查询出来 +select *from Student WHERE stuaddress='湖北武汉' or stuage='20' +--12.将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列 +select labexam from result where labexam>=60 and labexam<=80 order by labexam +--13.查询来自湖北武汉或者湖南长沙的学生的所有信息(用两种方法实现,提示 +--;or和in) +select * from Student where stuaddress='湖北武汉' or stuaddress='湖南长沙' +select * from Student where stuaddress in('湖北武汉','湖南长沙') +--14.查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列 +select writtenexam from result where not writtenexam>=70 and writtenexam<=90 order by writtenexam desc +--15.查询年龄没有写的学生所有信息 +select * from Student where stuage is null or stuage='' +--16.查询年龄写了的学生所有信息 +select* from Student where stuage is not null or not stuage='' +--17.查询姓张的学生信息 +select * from Student where stuname like '张%' +--18.查询学生地址中有‘湖’字的信息 +select * from Student where stuaddress like '%湖%' +--19.查询姓张但名为一个字的学生信息 +select * from Student where stuname like '张_' +--20.查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 +select * from Student where stuname like '__俊%' +--21.按学生的年龄降序显示所有学生信息 +select * from Student order by stuage +--22.按学生的年龄降序和座位号升序来显示所有学生的信息 +select * from Student order by stuage desc , stuseat asc +--23显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from result order by writtenexam desc +--24.显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from result order by labexam diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\232.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a6b9178287ca7dcb51ddba07e0a4caf04c38ce91 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\232.sql" @@ -0,0 +1,83 @@ +use master +go + +create database Student03 +go + +use Student03 +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 \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f82adab24cfe6d7c6724a3cd7ee19fbc4834ec97 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery1.sql" @@ -0,0 +1,118 @@ +create database Studentinfo + +on +( + name='Studentinfo', + filename='F:\SQL\Studentinfo.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Studentinfo_log', + filename='F:\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 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' + +--指定别名 +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 diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..be222c31831e6d8f01b172a942ba4733732d006e --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery1.sql" @@ -0,0 +1,120 @@ +create database Student +on +( + FileName='F:\homework\Student.mdf', + Name='Student', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +log on +( + FileName='F:\homework\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) + +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) + + +select * from Score +--1.查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 +select StuNo 学号,StuName 姓名,StuAge 年龄,StuAddress 地址,StuSeat 座位号,StuSex 性别 from Student +--2.查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 +select StuName,StuAge,StuAddress from Student +--3.查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字 +--注意:要用三种方法 +select StuNo 学号 from Score +-- +select 笔试=WrittenExam from Score +-- +select LabExam as 机试 from Score +--4.查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱” +select StuNo 学号,StuName 姓名,StuAddress 地址, StuName+'@'+StuAddress 邮箱 from Student +--5.查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分(笔试+机试)这四列的信息 +select StuNo,writtenExam,LabExam,writtenExam+LabExam as 总分 from Score +--6.查询学生信息表(stuInfo)中学生来自哪几个地方 +select distinct StuAddress from Student +--7.查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名'所有年龄' +select distinct 所有年龄=StuAge from Student +--8.查询学生信息表(stuInfo)中前3行记录 +select top 3 * from Student +--9.查询学生信息表(stuInfo)中前4个学生的姓名和座位号 +select top 4 StuName,StuSeat from Student +--10.查询学生信息表(stuInfo)中一半学生的信息 +select top 50 percent * from Student +--11.将地址是湖北武汉,年龄是20的学生的所有信息查询出来 +select * from Student where StuAge=20 and StuAddress='湖北武汉' +--12.将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列 +select * from Score where LabExam>=60 and LabExam<=80 order by LabExam DESC +--13.查询来自湖北武汉或者湖南长沙的学生的所有信息(用两种方法实现,提示;or和in) +select * from Student where StuAddress='湖北武汉' or StuAddress='湖南长沙' +select * from Student where StuAddress in ('湖北武汉','湖南长沙') +--14.查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列 +select * from Score where WrittenExam<=70 or WrittenExam>=90 order by WrittenExam ASC +--15.查询年龄没有写的学生所有信息 +select * from Student where StuAge is null +--16.查询年龄写了的学生所有信息 +select * from Student where StuAge is not null +--17.查询姓张的学生信息 +select * from Student where StuName like '张%' +--18.查询学生地址中有‘湖’字的信息 +select * from Student where StuAddress like '%湖%' +--19.查询姓张但名为一个字的学生信息 +select * from Student where StuName like '张_' +--20.查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 +select * from Student where StuName like '__俊%' +--21.按学生的年龄降序显示所有学生信息 +select * from Student order by StuAge DESC +--22.按学生的年龄降序和座位号升序来显示所有学生的信息 --? +select * from Student order by StuSeat,StuAge DESC +--23显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from Score order by WrittenExam DESC +--24.显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from Score order by WrittenExam ASC diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery4.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e5b4e56c5fe9ca97a4be4404915c3e0ba35dc7bb --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery4.sql" @@ -0,0 +1,110 @@ +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 int primary key identity(1,1), + StuName nchar(10) check(StuName>=2), + StuAge int check(StuAge>=0 and StuAge<100) not null, + StuAddress nchar(200), + StuSeat char(2) not null, + StuSex nchar(1) default('1') check(StuSex='1' or StuSex='0') + +) +go +use Stundents + +go + +create table ExamInfo +( + ExamNo int primary key identity(1,1), + StuNo int references StuInfo(StuNo), + WrittenExam int check(WrittenExam>=0 and WrittenExam<=100) not null, + LabExam int check(LabExam>=0 and LabExam<=100) not null +) +go + +use Stundents +insert into StuInfo(StuNo,StuName ,StuAge ,StuAddress ,StuSeat ,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 +insert into ExamInfo(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 ExamInfo + +select StuNo,stuName,StuAddress,邮箱 = StuName + '@' + StuAddress from StuInfo + +select StuNo,WrittenExam,LabExam,总分 = WrittenExam + LabExam from ExamInfo + +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 ExamInfo 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 ExamInfo 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 ExamInfo order by WrittenExam desc + +select top 1 * from ExamInfo order by LabExam asc \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3b39c7bb37e82333b9119ba8551cfc0ea393306e --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1.sql" @@ -0,0 +1,101 @@ +use studentinfo +go +create table student5 + +( stuno char(5) primary key , + stuname nvarchar(20), + stuage int, + stuaddress nchar(4), + stuseat int, + stusex int +) +insert into student5 values('$2501','张秋利',20,'美国硅谷',1,1),('$2502','李斯文',18,'湖北武汉',2,0),('$2503','马文才',22,'湖南长沙',3,1),('$2504','欧阳俊雄',21,'湖北武汉',4,0),('$2505','梅超风',20,'湖北武汉',5,1),('$2506','陈旋风',19,'美国硅谷',6,1),('$2507','陈风',20,'美国硅谷',7,1) + + + +create table student6 + +( examno int primary key identity(1,1), + stuno char(5) , + constraint Fk_student6_stuno foreign key(stuno) references student5(stuno), + writtenexam int , + labxam int +) +insert into student6 values('$2501',50,70),('$2502',60,65),('$2503',86,85),('$2504',40,70),('$2505',70,70),('$2506',85,90) + + +1.查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 + select stuno as '学号', stuage '年龄','姓名 和 地址'=stuname+stuaddress,stuseat as '座位号',stusex as '性别 ' from student5 +2.查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 + select stuname,stuage,stuaddress from student5 +3.查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字 + 注意:要用三种方法 + 1.select stuno as '学号',writtenexam as '笔试成绩',labxam as '机试成绩'from student6 + 2.select stuno '学号',writtenexam '笔试成绩',labxam '机试成绩'from student6 + --3.select stuno as '学号', writtenexam+'0/600'+labxam as '笔试成绩 和 机试成绩'from student6 (数字的添加结果是运算的加) +4.查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱” + select stuno '学号', '姓名 和 地址'=stuname+stuaddress from student5 + select stuno '学号', '姓名 和 地址'=stuname+stuaddress, stuname+'@'+stuaddress as '邮箱' from student5 +5.查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分(笔试+机试)这四列的信息 + select stuno as '学号',writtenexam as '笔试成绩',labxam as '机试成绩','总分'=writtenexam+labxam from student6 + +6.查询学生信息表(stuInfo)中学生来自哪几个地方 + select distinct stuaddress from student5 + +7.查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名'所有年龄' + select distinct stuage as '所有年龄' from student5 + +8.查询学生信息表(stuInfo)中前3行记录 + select top 3 * from student5 + +9.查询学生信息表(stuInfo)中前4个学生的姓名和座位号 + select stuname,stuseat from student5 where stuno<$2505 + +10.查询学生信息表(stuInfo)中一半学生的信息 + select top 50 percent *from student5 + +11.将地址是湖北武汉,年龄是20的学生的所有信息查询出来 + select*from student5 where stuaddress='湖北武汉' or stuage=20 + +12.将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列 + select labxam from student6 where labxam>=60 and labxam<=80 order by labxam desc + +13.查询来自湖北武汉或者湖南长沙的学生的所有信息(用两种方法实现,提示;or和in) + select*from student5 where stuaddress='湖北武汉'or stuaddress='湖南长沙' + select*from student5 where stuaddress in('湖北武汉','湖南长沙') + +14.查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列 + select writtenexam from student6 where writtenexam<70 or writtenexam>90 order by writtenexam + select writtenexam from student6 where writtenexam like '[^7-9]%' + +15.查询年龄没有写的学生所有信息 + select*from student5 where stuage is null or stuage='' + +16.查询年龄写了的学生所有信息 + select*from student5 where stuage is not null and not stuage='' + +17.查询姓张的学生信息 + select*from student5 where stuname like '张%' + +18.查询学生地址中有‘湖’字的信息 + select stuaddress from student5 where stuaddress like '%湖%' + +19.查询姓张但名为一个字的学生信息 + select*from student5 where stuname like '张_' + +20.查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 + select*from student5 where stuname like '__俊%' + +21.按学生的年龄降序显示所有学生信息 + select*from student5 order by stuage desc + +22.按学生的年龄降序和座位号升序来显示所有学生的信息 + select*from student5 order by stuage desc,stuseat asc + +23显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 + select top 1 *from student6 order by writtenexam desc + +24.显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 + select top 1* from student6 order by labxam + + diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250.sql" new file mode 100644 index 0000000000000000000000000000000000000000..60940881524fcd0f3732701e1f315da164ffa282 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250.sql" @@ -0,0 +1,154 @@ +use master +go + +create database Students +on +( + name = 'Students', + filename = 'D:\数据库\Students.mdf' +) + +log on +( + name = 'Students_log', + filename = 'D:\数据库\Students_log.ldf' +) + +use Students +go + +--按图片参考,进行数据表的建立和数据插入,然后进行以下查询操作 +create table StuInfo( + stuNo nvarchar(10) primary key, + stuName nvarchar(10)not null, + stuAge int not null, + stuAddress nvarchar(20) null, + stuSeat int not null, + stuSex int not null +) +go + +insert into Student(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) +go + +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 +) +go + +insert into Exam(stuNo,writtenExam,labExam) values +('s2501',50,70),('s2502',60,65),('s2503',86,85), +('s2504',40,80),('s2505',70,90),('s2506',85,90) + +go + + +--1.查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 +select stuNo as 学号 ,stuName as 姓名,stuAge as 年龄,stuAddress as 地址,stuSeat as 座位号,stuSex as 性别 from StuInfo +--在本来的名字基础上,在后面+as 中文名称即可 + +--2.查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 +select stuName,stuAge,stuAddress from StuInfo +--套用方法:select * from 表名 然后将*改为你需要的列即可,列名之间要逗号隔开 + +--3.查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字注意:要用三种方法 +select stuNo as 学号,writtenExam as 笔试,labExam as 机试 from stuExam + +--4.查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱” +select stuNo,stuName,stuAddress ,stuName+'@'+stuAddress as 邮箱 from StuInfo + +--5.查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分(笔试+机试)这四列的信息 +select stuNo,writtenExam,labExam,writtenExam+labExam as 总分 from stuExam + +--套用方法:select * from 表名 然后将*改为你需要的列即可,列名之间要逗号隔开,将要求列相加as重命名即可 + +--6.查询学生信息表(stuInfo)中学生来自哪几个地方 +select stuName, stuAddress from StuInfo + +--7.查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名'所有年龄' +select stuAge as 年龄, count(*) as 所有年龄 from StuInfo group by stuAge + + +--select 所需列名 ,(逗号隔开) count(*)表示为这一列所以信息 from 表 group by 聚合函数,进行统计分类 +--8.查询学生信息表(stuInfo)中前3行记录 +select top 3 * from StuInfo + +--套用方法:select * from 表名 在*前加上top 要几行就写几 + +--9.查询学生信息表(stuInfo)中前4个学生的姓名和座位号 +select top 4 stuName as 姓名,stuSeat as 座位号 from StuInfo + +--10.查询学生信息表(stuInfo)中一半学生的信息 + +select top 50 percent * from StuInfo + + + +--11.将地址是湖北武汉,年龄是20的学生的所有信息查询出来 +select * from StuInfo where stuAddress='湖北武汉' and stuAge=20 + +--条件查询 where后面接所需的条件,多个条件用and隔开 + +--12.将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列 +select * from stuExam where labExam between 60 and 80 order by labExam desc + +--条件查询 取值范围用between...and...链接,order by进行排序,默认为升序排序(ASC)可不写,降序为(desc) + +--13.查询来自湖北武汉或者湖南长沙的学生的所有信息(用两种方法实现,提示;or和in) +select * from StuInfo where stuAddress='湖北武汉' or stuAddress= '湖南长沙' + +--同一条件多个参数用or隔开 + +select * from StuInfo where stuAddress in ('湖北武汉','湖南长沙') + + +--14.查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列 +select * from stuExam where writtenExam not between 70 and 90 order by writtenExam asc + +--15.查询年龄没有写的学生所有信息 +select * from StuInfo where stuAge=null or stuAge=' ' + +--16.查询年龄写了的学生所有信息 +select * from StuInfo where stuAge is not null + +--is not null为不是空的 + + +--17.查询姓张的学生信息 +select * from StuInfo where stuName like '%张%' + +--模糊查询用like 单引号内为前后各一个百分号 + +--18.查询学生地址中有‘湖’字的信息 +select * from StuInfo where stuAddress like '%湖%' + +--19.查询姓张但名为一个字的学生信息 +select * from StuInfo where stuName like '%张_' + +--模糊查询中,_下划线为占位符,表达为后面一个字,或者前面几个字 + +--20.查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 +select * from StuInfo where stuName like '%__俊%' + +--21.按学生的年龄降序显示所有学生信息 +select * from StuInfo order by stuAge desc + +--22.按学生的年龄降序和座位号升序来显示所有学生的信息 +select * from StuInfo order by stuAge desc,stuSeat + +--23显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 + +select top 1 * from stuExam order by writtenExam desc + +--将排名进行降序排序找到第一个就是第一名 + + +--24.显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from stuExam order by labExam \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..027713a77dd6ca6d9be284fa1a5e708e268f74b5 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" @@ -0,0 +1,113 @@ +use master +go + +create database Students +on +( + name='Students', + filename='D:\sql数据库\Students.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='Students_log', + filename='D:\sql数据库\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') + +--1.查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 +select * from Student +select stuid as 学号, stuname as 姓名, stuage as 年龄, stuaddress as 地址,stuseat as 座号,stusex as 性别 from Student +--2.查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 +select stuname,stuage,stuaddress from Student +--3.查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字 +-- 注意:要用三种方法 +select stuid as 学号, writtenexam as 笔试 ,labexam as 机试 from result +select stuid 学号,writtenexam 笔试, labexam 机试 from result +--4.查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱” +select stuid+stuname+'@'+ stuname+stuaddress as 邮箱 from Student +--5.查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分(笔试+机试)这四列的信息 +select stuid,writtenexam,labexam,writtenexam +labexam as 总分 from result +--6.查询学生信息表(stuInfo)中学生来自哪几个地方 +select stuaddress from Student +--7.查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名'所有年龄' +select stuage as 所有年龄 from Student ORDER By stuage +--8.查询学生信息表(stuInfo)中前3行记录 +select TOP 3 * from Student +--9.查询学生信息表(stuInfo)中前4个学生的姓名和座位号 +SELECT TOP 4 stuname,stusex from Student +--10.查询学生信息表(stuInfo)中一半学生的信息 +select top 50percent * from Student +--11.将地址是湖北武汉,年龄是20的学生的所有信息查询出来 +select *from Student WHERE stuaddress='湖北武汉' or stuage='20' +--12.将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列 +select labexam from result where labexam>=60 and labexam<=80 order by labexam +--13.查询来自湖北武汉或者湖南长沙的学生的所有信息(用两种方法实现,提示 +--;or和in) +select * from Student where stuaddress='湖北武汉' or stuaddress='湖南长沙' +select * from Student where stuaddress in('湖北武汉','湖南长沙') +--14.查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列 +select writtenexam from result where not writtenexam>=70 and writtenexam<=90 order by writtenexam desc +--15.查询年龄没有写的学生所有信息 +select * from Student where stuage is null or stuage='' +--16.查询年龄写了的学生所有信息 +select* from Student where stuage is not null or not stuage='' +--17.查询姓张的学生信息 +select * from Student where stuname like '张%' +--18.查询学生地址中有‘湖’字的信息 +select * from Student where stuaddress like '%湖%' +--19.查询姓张但名为一个字的学生信息 +select * from Student where stuname like '张_' +--20.查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 +select * from Student where stuname like '__俊%' +--21.按学生的年龄降序显示所有学生信息 +select * from Student order by stuage +--22.按学生的年龄降序和座位号升序来显示所有学生的信息 +select * from Student order by stuage desc , stuseat asc +--23显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from result order by writtenexam desc +--24.显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from result order by labexam diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..10d871b0fe9afa35e69b18952c00a4ab41f295be --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" @@ -0,0 +1,59 @@ +use master +go + +create database Student +go +use Student +create table stuinfo +( +StuNo char(5), +StuName nvarchar(5), +StuAge int, +StuAddress nvarchar(20), +StuSeat int, +StuSex nvarchar(1) default('1') check(StuSex in('1','0')) +) +insert into stuinfo values ('$2501','张秋利',20,'美国硅谷',1,1),('$2502','李斯文',18,'湖北武汉',2,0), +('$2503','马文才',22,'湖南长沙',3,1),('$2504','欧阳俊雄',21,'湖北武汉',4,0),('$2505','梅超风',20,'湖北武汉',5,1), +('$2506','陈旋风',19,'美国硅谷',6,1),('$2507','陈风',20,'美国硅谷',7,1) +go +use Student +create table StuMark +( +examNo int, +StuNO char(5), +writtenExam int, +labExam int +) +insert into StuMark 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 StuMark +select StuNo 编号,StuName 名字,StuAge 年龄,StuAddress 地址,StuSeat 学号,StuSex 性别 from stuinfo +select StuAge,StuAddress,StuSex from stuinfo +select StuNo 学号, writtenExam 笔试, labExam 机试 from StuMark +select 学号=StuNO ,笔试=writtenExam,机试=labExam from StuMark +select StuNO as 学号,writtenExam as 笔试,labExam as 机试 from StuMark +select StuNO+StuName+StuAddress 学号名字地址 from stuinfo +select StuName+'@'+StuAddress as 邮箱 from stuinfo +select StuNo 学号, writtenExam 笔试, labExam 机试,writtenExam + labExam 总分 from StuMark +select distinct StuAddress from stuinfo +select distinct StuAge,stuage 全部年龄 from stuinfo +select * from stuinfo where StuSeat<=3 +select StuNo 座位号,StuName 名字 from stuinfo where StuSeat<=4 +select top 50 percent * from stuinfo +select * from stuinfo where StuAge=20 and StuAddress='湖北武汉' +select * from StuMark 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 StuMark where writtenExam<70 or writtenExam>90 order by writtenExam ASC +select * from stuinfo where StuAge is null or StuAge='' +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, StuNo ASC +select top 1* from StuMark order by writtenExam DESC +select top 1* from StuMark order by writtenExam ASC \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c25d253227d66deddbe707f4fb040ce49ffae795 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252.sql" @@ -0,0 +1,59 @@ +use master +go + +create database Student +go +use Student +create table stuinfo +( +StuNo char(5), +StuName nvarchar(5), +StuAge int, +StuAddress nvarchar(20), +StuSeat int, +StuSex nvarchar(1) default('1') check(StuSex in('1','0')) +) +insert into stuinfo values ('$2501','张秋利',20,'美国硅谷',1,1),('$2502','李斯文',18,'湖北武汉',2,0), +('$2503','马文才',22,'湖南长沙',3,1),('$2504','欧阳俊雄',21,'湖北武汉',4,0),('$2505','梅超风',20,'湖北武汉',5,1), +('$2506','陈旋风',19,'美国硅谷',6,1),('$2507','陈风',20,'美国硅谷',7,1) +go +use Student +create table StuMark +( +examNo int, +StuNO char(5), +writtenExam int, +labExam int +) +insert into StuMark 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 StuMark +select StuNo 编号,StuName 名字,StuAge 年龄,StuAddress 地址,StuSeat 学号,StuSex 性别 from stuinfo +select StuAge,StuAddress,StuSex from stuinfo +select StuNo 学号, writtenExam 笔试, labExam 机试 from StuMark +select 学号=StuNO ,笔试=writtenExam,机试=labExam from StuMark +select StuNO as 学号,writtenExam as 笔试,labExam as 机试 from StuMark +select StuNO+StuName+StuAddress 学号名字地址 from stuinfo +select StuName+'@'+StuAddress as 邮箱 from stuinfo +select StuNo 学号, writtenExam 笔试, labExam 机试,writtenExam + labExam 总分 from StuMark +select distinct StuAddress from stuinfo +select distinct StuAge,stuage 全部年龄 from stuinfo +select * from stuinfo where StuSeat<=3 +select StuNo 座位号,StuName 名字 from stuinfo where StuSeat<=4 +select top 50 percent * from stuinfo +select * from stuinfo where StuAge=20 and StuAddress='湖北武汉' +select * from StuMark 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 StuMark where writtenExam<70 or writtenExam>90 order by writtenExam ASC +select * from stuinfo where StuAge is null or StuAge='' +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, StuNo ASC +select top 1* from StuMark order by writtenExam DESC +select top 1* from StuMark order by writtenExam ASC \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..645bdb4c5ade6dbdee2575d19a38625855042101 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" @@ -0,0 +1,89 @@ +use master +go +create database StuScore +on +( + name='StuScore', + filename='C:\学习\数据库\StuScore.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='StuScore_log', + filename='C:\学习\数据库\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 \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1ad5a83c7faafce253771f236eca75254fa9986e --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227.sql" @@ -0,0 +1,118 @@ +锘縞reate database Studentinfo + +on +( + name='Studentinfo', + filename='F:\SQL\Studentinfo.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Studentinfo_log', + filename='F:\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 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' + +--鎸囧畾鍒悕 +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 \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..170b32d0f690eefa6410de365cf3c889bcb10158 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" @@ -0,0 +1,154 @@ +use master +go + +create database Students +on +( + name = 'Students', + filename = 'D:\数据库\Students.mdf' +) + +log on +( + name = 'Students_log', + filename = 'D:\数据库\Students_log.ldf' +) + +use Students +go + +--按图片参考,进行数据表的建立和数据插入,然后进行以下查询操作 +create table StuInfo( + stuNo nvarchar(10) primary key, + stuName nvarchar(10)not null, + stuAge int not null, + stuAddress nvarchar(20) null, + stuSeat int not null, + stuSex int not null +) +go + +insert into Student(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) +go + +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 +) +go + +insert into Exam(stuNo,writtenExam,labExam) values +('s2501',50,70),('s2502',60,65),('s2503',86,85), +('s2504',40,80),('s2505',70,90),('s2506',85,90) + +go + + +--1.查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 +select stuNo as 学号 ,stuName as 姓名,stuAge as 年龄,stuAddress as 地址,stuSeat as 座位号,stuSex as 性别 from StuInfo +--在本来的名字基础上,在后面+as 中文名称即可 + +--2.查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 +select stuName,stuAge,stuAddress from StuInfo +--套用方法:select * from 表名 然后将*改为你需要的列即可,列名之间要逗号隔开 + +--3.查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字注意:要用三种方法 +select stuNo as 学号,writtenExam as 笔试,labExam as 机试 from stuExam + +--4.查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱” +select stuNo,stuName,stuAddress ,stuName+'@'+stuAddress as 邮箱 from StuInfo + +--5.查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分(笔试+机试)这四列的信息 +select stuNo,writtenExam,labExam,writtenExam+labExam as 总分 from stuExam + +--套用方法:select * from 表名 然后将*改为你需要的列即可,列名之间要逗号隔开,将要求列相加as重命名即可 + +--6.查询学生信息表(stuInfo)中学生来自哪几个地方 +select stuName, stuAddress from StuInfo + +--7.查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名'所有年龄' +select stuAge as 年龄, count(*) as 所有年龄 from StuInfo group by stuAge + + +--select 所需列名 ,(逗号隔开) count(*)表示为这一列所以信息 from 表 group by 聚合函数,进行统计分类 +--8.查询学生信息表(stuInfo)中前3行记录 +select top 3 * from StuInfo + +--套用方法:select * from 表名 在*前加上top 要几行就写几 + +--9.查询学生信息表(stuInfo)中前4个学生的姓名和座位号 +select top 4 stuName as 姓名,stuSeat as 座位号 from StuInfo + +--10.查询学生信息表(stuInfo)中一半学生的信息 + +select top 50 percent * from StuInfo + + + +--11.将地址是湖北武汉,年龄是20的学生的所有信息查询出来 +select * from StuInfo where stuAddress='湖北武汉' and stuAge=20 + +--条件查询 where后面接所需的条件,多个条件用and隔开 + +--12.将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列 +select * from stuExam where labExam between 60 and 80 order by labExam desc + +--条件查询 取值范围用between...and...链接,order by进行排序,默认为升序排序(ASC)可不写,降序为(desc) + +--13.查询来自湖北武汉或者湖南长沙的学生的所有信息(用两种方法实现,提示;or和in) +select * from StuInfo where stuAddress='湖北武汉' or stuAddress= '湖南长沙' + +--同一条件多个参数用or隔开 + +select * from StuInfo where stuAddress in ('湖北武汉','湖南长沙') + + +--14.查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列 +select * from stuExam where writtenExam not between 70 and 90 order by writtenExam asc + +--15.查询年龄没有写的学生所有信息 +select * from StuInfo where stuAge=null or stuAge=' ' + +--16.查询年龄写了的学生所有信息 +select * from StuInfo where stuAge is not null + +--is not null为不是空的 + + +--17.查询姓张的学生信息 +select * from StuInfo where stuName like '%张%' + +--模糊查询用like 单引号内为前后各一个百分号 + +--18.查询学生地址中有‘湖’字的信息 +select * from StuInfo where stuAddress like '%湖%' + +--19.查询姓张但名为一个字的学生信息 +select * from StuInfo where stuName like '%张_' + +--模糊查询中,_下划线为占位符,表达为后面一个字,或者前面几个字 + +--20.查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 +select * from StuInfo where stuName like '%__俊%' + +--21.按学生的年龄降序显示所有学生信息 +select * from StuInfo order by stuAge desc + +--22.按学生的年龄降序和座位号升序来显示所有学生的信息 +select * from StuInfo order by stuAge desc,stuSeat + +--23显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 + +select top 1 * from stuExam order by writtenExam desc + +--将排名进行降序排序找到第一个就是第一名 + + +--24.显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from stuExam order by labExam \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4f4b20de567f50973722aae3d16aed20ad56f111 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" @@ -0,0 +1,70 @@ +create database Student +on +( + name='Student', + filename='D:\SQL作业.mdf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='Student_log', + filename='D:\SQL作业_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 * from stuinfo +select stuNO as 学号, stuName as 姓名 ,stuAddress as 地址,stuSeat as 座位号,stuSex as 性别 from stuinfo +select stuName,stuAge,stuAddress from stuinfo +select stuno,writtenexam,labexam from stuexam +select 学号=stuno , 笔试=writtenexam,机试=labexam from stuexam +select stuno as 学号 , writtenexam as 笔试, labexam as 机试 from stuexam +select stuno 学号 ,writtenexam 笔试,labexam 机试 from stuexam +select stuNO,stuName,stuAddress from stuinfo +select stuName+'@'+stuAddress as '邮箱' from stuinfo +select stuno,writtenexam,labexam,writtenexam+labexam from stuexam +select distinct stuAddress from stuinfo +select distinct stuAge as '所有年龄' from stuinfo +select top 3 * from stuinfo +select top 4 stuName,stuSeat from stuinfo +select top 50 percent *from stuinfo +select*from stuinfo where stuAddress='湖北武汉' or stuAge=20 +select labexam 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 writtenexam 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 stuAddress 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 \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f82adab24cfe6d7c6724a3cd7ee19fbc4834ec97 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" @@ -0,0 +1,118 @@ +create database Studentinfo + +on +( + name='Studentinfo', + filename='F:\SQL\Studentinfo.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Studentinfo_log', + filename='F:\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 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' + +--指定别名 +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 diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..192ba1fae3ca58d71c5b533eeeeabf548983750b --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" @@ -0,0 +1,70 @@ +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 * from stuinfo +select stuNO as 学号, stuName as 姓名 ,stuAddress as 地址,stuSeat as 座位号,stuSex as 性别 from stuinfo +select stuName,stuAge,stuAddress from stuinfo +select stuno,writtenexam,labexam from stuexam +select 学号=stuno , 笔试=writtenexam,机试=labexam from stuexam +select stuno as 学号 , writtenexam as 笔试, labexam as 机试 from stuexam +select stuno 学号 ,writtenexam 笔试,labexam 机试 from stuexam +select stuNO,stuName,stuAddress from stuinfo +select stuName+'@'+stuAddress as '邮箱' from stuinfo +select stuno,writtenexam,labexam,writtenexam+labexam from stuexam +select distinct stuAddress from stuinfo +select distinct stuAge as '所有年龄' from stuinfo +select top 3 * from stuinfo +select top 4 stuName,stuSeat from stuinfo +select top 50 percent *from stuinfo +select*from stuinfo where stuAddress='湖北武汉' or stuAge=20 +select labexam 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 writtenexam 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 stuAddress 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 \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/~vs2EFE.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/~vs2EFE.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e36c268e11f4705156fbb0b9526d3652f890d3e1 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" @@ -0,0 +1,77 @@ +use master +go +create database Students +on( + name='Students', + filename='C:\TEXT\students.maf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on( + name='Students_log', + filename='C:\TEXT\students_log.maf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go +use students +go +create table stuinfo +( + stuNO varchar(50) primary key not null, + stuName nvarchar(20) not null, + stuAge int not null, + stuAddress nvarchar(50) not null, + stuSeat int not null, + stuSex int check(stuSex=1 or stuSex=0) not null +) +create table stuexam +( + examNo int primary key identity(1,1), + stuNO varchar(50) references stuinfo(stuNO), + writtenExam int not null, + labExam int not null, +) +use students +go +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 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 stuNO as 学号, stuName as 姓名,stuAge as 年龄, stuAddress as 地址,stuSeat as 座位号,stuSex as 性别 from stuinfo +select stuName,stuAge,StuAddress from stuinfo +select stuNO as 学号,writtenExam as 笔试,labExam as 机试 from stuexam +select stuNO 学号,writtenExam 笔试,labExam 机试 from stuexam +select 学号=stuNO ,笔试=writtenExam,机试=labExam from stuexam +select stuName+'@'+stuAddress as 邮箱 from stuinfo +select stuNO as 学号,writtenExam as 笔试,labExam as 机试,writtenExam+labExam as 总分 from stuexam +select distinct stuAddress from stuinfo +select distinct stuAge as 所有年龄 from stuinfo +select top 3 *from stuinfo +select top 50 percent * from stuinfo +select * from stuexam where labExam>=60 and labExam<=80 order by labExam DESC +select * from stuinfo where stuAddress in('湖北武汉','湖南长沙') +select * from stuinfo where stuAddress='湖北武汉' or stuAddress='湖南长沙' +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 \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f156286f761180b436aae6bbc2f7d85affaf9c4f --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" @@ -0,0 +1,87 @@ +use master +go + +create database Students +on +( + name='Students', + filename='D:\sql数据库\1\Students.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='Students_log', + filename='D:\sql数据库\1\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 StuName,StuAge,StuAddress from StuInfo +select StuNO 学号,WittenExam 笔试,LabExam 机试 from ExamInfo +select StuNO AS 学号,WittenExam AS 笔试,LabExam AS 机试 from ExamInfo +select StuNO = '学号',WittenExam = '笔试',LabExam = '机试' from ExamInfo +select StuNO,StuName,StuAddress,StuName+'@'+StuAddress AS '邮箱' from StuInfo +select StuNO,WittenExam,LabExam,WittenExam+LabExam AS '总分' from ExamInfo +select StuAddress from StuInfo +select StuAge 所有年龄 from StuInfo +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 ExamInfo where LabExam>=60 and LabExam<=80 order by LabExam +select * from StuInfo where StuAddress='湖北武汉' or StuAddress='湖南长沙' +select * from StuInfo where StuAddress in('湖北武汉','湖南长沙') +select * from ExamInfo where WittenExam<=70 or WittenExam>=90 order by WittenExam +select * from StuInfo where StuAge is null or StuAge='' +select * from StuInfo where StuAge is not null and 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 ExamInfo order by WittenExam +select top 1 * from ExamInfo order by LabExam ASC \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/\347\254\254\344\272\224\345\205\255\347\253\240\344\275\234\344\270\232.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/\347\254\254\344\272\224\345\205\255\347\253\240\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..02bd7c3f1b9be11aae639f6144cad53fade73cf6 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/\347\254\254\344\272\224\345\205\255\347\253\240\344\275\234\344\270\232.sql" @@ -0,0 +1,161 @@ +create database Students + +go + +use Students + +go + +--按图片参考,进行数据表的建立和数据插入,然后进行以下查询操作 + +create table StuInfo( + stuNo nvarchar(10) primary key, + stuName nvarchar(10)not null, + stuAge int not null, + stuAddress nvarchar(20) null, + stuSeat int not null, + stuSex int not null +) + +go + +insert into Student(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) + +go + +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 + + + +) + +go + +insert into Exam(stuNo,writtenExam,labExam) values +('s2501',50,70),('s2502',60,65),('s2503',86,85), +('s2504',40,80),('s2505',70,90),('s2506',85,90) + + +go + + +--1.查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 +select stuNo as 学号 ,stuName as 姓名,stuAge as 年龄,stuAddress as 地址,stuSeat as 座位号,stuSex as 性别 from StuInfo +--在本来的名字基础上,在后面+as 中文名称即可 + +--2.查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 +select stuName,stuAge,stuAddress from StuInfo +--套用方法:select * from 表名 然后将*改为你需要的列即可,列名之间要逗号隔开 + +--3.查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字注意:要用三种方法 +select stuNo as 学号,writtenExam as 笔试,labExam as 机试 from stuExam + +--4.查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱” +select stuNo,stuName,stuAddress ,stuName+'@'+stuAddress as 邮箱 from StuInfo + +--5.查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分(笔试+机试)这四列的信息 +select stuNo,writtenExam,labExam,writtenExam+labExam as 总分 from stuExam + +--套用方法:select * from 表名 然后将*改为你需要的列即可,列名之间要逗号隔开,将要求列相加as重命名即可 + +--6.查询学生信息表(stuInfo)中学生来自哪几个地方 +select stuName, stuAddress from StuInfo + +--7.查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名'所有年龄' +select stuAge as 年龄, count(*) as 所有年龄 from StuInfo group by stuAge + + +--select 所需列名 ,(逗号隔开) count(*)表示为这一列所以信息 from 表 group by 聚合函数,进行统计分类 +--8.查询学生信息表(stuInfo)中前3行记录 +select top 3 * from StuInfo + +--套用方法:select * from 表名 在*前加上top 要几行就写几 + +--9.查询学生信息表(stuInfo)中前4个学生的姓名和座位号 +select top 4 stuName as 姓名,stuSeat as 座位号 from StuInfo + +--10.查询学生信息表(stuInfo)中一半学生的信息 + +select top 50 percent * from StuInfo + + + +--11.将地址是湖北武汉,年龄是20的学生的所有信息查询出来 +select * from StuInfo where stuAddress='湖北武汉' and stuAge=20 + +--条件查询 where后面接所需的条件,多个条件用and隔开 + +--12.将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列 +select * from stuExam where labExam between 60 and 80 order by labExam desc + +--条件查询 取值范围用between...and...链接,order by进行排序,默认为升序排序(ASC)可不写,降序为(desc) + +--13.查询来自湖北武汉或者湖南长沙的学生的所有信息(用两种方法实现,提示;or和in) +select * from StuInfo where stuAddress='湖北武汉' or stuAddress= '湖南长沙' + +--同一条件多个参数用or隔开 + +select * from StuInfo where stuAddress in ('湖北武汉','湖南长沙') + + +--14.查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列 +select * from stuExam where writtenExam not between 70 and 90 order by writtenExam asc + +--15.查询年龄没有写的学生所有信息 +select * from StuInfo where stuAge=null or stuAge=' ' + +--16.查询年龄写了的学生所有信息 +select * from StuInfo where stuAge is not null + +--is not null为不是空的 + + +--17.查询姓张的学生信息 +select * from StuInfo where stuName like '%张%' + +--模糊查询用like 单引号内为前后各一个百分号 + +--18.查询学生地址中有‘湖’字的信息 +select * from StuInfo where stuAddress like '%湖%' + +--19.查询姓张但名为一个字的学生信息 +select * from StuInfo where stuName like '%张_' + +--模糊查询中,_下划线为占位符,表达为后面一个字,或者前面几个字 + +--20.查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 +select * from StuInfo where stuName like '%__俊%' + +--21.按学生的年龄降序显示所有学生信息 +select * from StuInfo order by stuAge desc + +--22.按学生的年龄降序和座位号升序来显示所有学生的信息 +select * from StuInfo order by stuAge desc,stuSeat + +--23显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 + +select top 1 * from stuExam order by writtenExam desc + +--将排名进行降序排序找到第一个就是第一名 + + +--24.显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from stuExam order by labExam + +--将排名进行升序排序找到第一个就是第最后一名 + + + + + + + + diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1 (1).sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1 (1).sql" new file mode 100644 index 0000000000000000000000000000000000000000..823bf69eb272e039c5caa6f42b06278764753473 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1 (1).sql" @@ -0,0 +1,83 @@ +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 diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a8a4b404aa34df684810b8e2530a2c6f9f568f54 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" @@ -0,0 +1,81 @@ +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 stuNO 学号,stuName 姓名,stuAge 年龄,stuAddress 地址,stuSeat 座位号,stuSex 性别 from StuInfo +select stuName,stuAge,StuAddress from stuinfo +select stuNO as 学号,writtenExam as 笔试,labExam as 机试 from stuexam +select stuNO 学号,writtenExam 笔试,labExam 机试 from stuexam +select 学号=stuNO ,笔试=writtenExam,机试=labExam from stuexam +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 50 percent * from stuinfo +select * from stuinfo where stuAddress in('湖北武汉') and stuAge=20 +select * from stuexam where labExam>=60 and labExam<=80 order by labExam DESC +select * from stuinfo where stuAddress in('湖北武汉','湖南长沙') +select * from stuinfo where stuAddress='湖北武汉' or stuAddress='湖南长沙' +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 \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye03.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye03.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f95c80b9ea34370938d0e7e1ea138adbc2cd661c --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye03.sql" @@ -0,0 +1,75 @@ +use master +go + +create database zuoye03 +on +( + name='zuoye03', + filename='D:\test\zuoye03.maf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='zuoye03_log', + filename='D:\test\zuoye03_log.maf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go +use zuoye03 +create table stuinfo +( +StuNo char(5), +StuName nvarchar(5), +StuAge int, +StuAddress nvarchar(20), +StuSeat int, +StuSex nvarchar(1) default('1') check(StuSex in('1','0')) +) +insert into stuinfo values ('$2501','张秋利',20,'美国硅谷',1,1),('$2502','李斯文',18,'湖北武汉',2,0), +('$2503','马文才',22,'湖南长沙',3,1),('$2504','欧阳俊雄',21,'湖北武汉',4,0),('$2505','梅超风',20,'湖北武汉',5,1), +('$2506','陈旋风',19,'美国硅谷',6,1),('$2507','陈风',20,'美国硅谷',7,1) +go +use zuoye03 +create table StuMark +( +examNo int, +StuNO char(5), +writtenExam int, +labExam int +) +insert into StuMark 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 StuMark +select StuNo 编号,StuName 名字,StuAge 年龄,StuAddress 地址,StuSeat 学号,StuSex 性别 from stuinfo +select StuAge,StuAddress,StuSex from stuinfo +select StuNo 学号, writtenExam 笔试, labExam 机试 from StuMark +select 学号=StuNO ,笔试=writtenExam,机试=labExam from StuMark +select StuNO as 学号,writtenExam as 笔试,labExam as 机试 from StuMark +select StuNO+StuName+StuAddress 学号名字地址 from stuinfo +select StuName+'@'+StuAddress as 邮箱 from stuinfo +select StuNo 学号, writtenExam 笔试, labExam 机试,writtenExam + labExam 总分 from StuMark +select distinct StuAddress from stuinfo +select distinct StuAge,stuage 全部年龄 from stuinfo +select * from stuinfo where StuSeat<=3 +select StuNo 座位号,StuName 名字 from stuinfo where StuSeat<=4 +select top 50 percent * from stuinfo +select * from stuinfo where StuAge=20 and StuAddress='湖北武汉' +select * from StuMark 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 StuMark where writtenExam<70 or writtenExam>90 order by writtenExam ASC +select * from stuinfo where StuAge is null or StuAge='' +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, StuNo ASC +select top 1* from StuMark order by writtenExam DESC +select top 1* from StuMark order by writtenExam ASC \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..46a386fc2cbacbe2c1620f63c8b44f10cbb15c1a --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" @@ -0,0 +1,118 @@ +create database Studentinfo + +on +( + name='Studentinfo', + filename='F:\SQL\Studentinfo.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Studentinfo_log', + filename='F:\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 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' + +--指定别名 +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 diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b2538dbbd6e5122ffc324956962266b0813d2ca9 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" @@ -0,0 +1,59 @@ +use master +go + +create database Student +go +use Student +create table stuinfo +( +StuNo char(5), +StuName nvarchar(5), +StuAge int, +StuAddress nvarchar(20), +StuSeat int, +StuSex nvarchar(1) default('1') check(StuSex in('1','0')) +) +insert into stuinfo values ('$2501','张秋楠',20,'美国硅谷',1,1),('$2502','李斯',18,'湖北武汉',2,0), +('$2503','马才',22,'湖南长沙',3,1),('$2504','欧阳娜娜',21,'湖北武汉',4,0),('$2505','超风',20,'湖北武汉',5,1), +('$2506','陈凯',19,'美国硅谷',6,1),('$2507','陈风',20,'美国硅谷',7,1) +go +use Student +create table StuMark +( +examNo int, +StuNO char(5), +writtenExam int, +labExam int +) +insert into StuMark values('1','2501','50','70'),('2','2502','60','65'),('3','2503','86','85'),('4','2504','40','80'), +('5','2505','70','90'),('6','2506','85','90') +select * from stuinfo +select * from StuMark +select StuNo 编号,StuName 名字,StuAge 年龄,StuAddress 地址,StuSeat 学号,StuSex 性别 from stuinfo +select StuAge,StuAddress,StuSex from stuinfo +select StuNo 学号, writtenExam 笔试, labExam 机试 from StuMark +select 学号=StuNO ,笔试=writtenExam,机试=labExam from StuMark +select StuNO as 学号,writtenExam as 笔试,labExam as 机试 from StuMark +select StuNO+StuName+StuAddress 学号名字地址 from stuinfo +select StuName+'@'+StuAddress as 邮箱 from stuinfo +select StuNo 学号, writtenExam 笔试, labExam 机试,writtenExam + labExam 总分 from StuMark +select distinct StuAddress from stuinfo +select distinct StuAge,stuage 全部年龄 from stuinfo +select * from stuinfo where StuSeat<=3 +select StuNo 座位号,StuName 名字 from stuinfo where StuSeat<=4 +select top 50 percent * from stuinfo +select * from stuinfo where StuAge=20 and StuAddress='湖北武汉' +select * from StuMark 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 StuMark where writtenExam<70 or writtenExam>90 order by writtenExam ASC +select * from stuinfo where StuAge is null or StuAge='' +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, StuNo ASC +select top 1* from StuMark order by writtenExam DESC +select top 1* from StuMark order by writtenExam ASC \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3ab7c839d852ac04cff9f1cf42876e0faa837ff8 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery1.sql" @@ -0,0 +1,83 @@ +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 \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery5.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..192ba1fae3ca58d71c5b533eeeeabf548983750b --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery5.sql" @@ -0,0 +1,70 @@ +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 * from stuinfo +select stuNO as 学号, stuName as 姓名 ,stuAddress as 地址,stuSeat as 座位号,stuSex as 性别 from stuinfo +select stuName,stuAge,stuAddress from stuinfo +select stuno,writtenexam,labexam from stuexam +select 学号=stuno , 笔试=writtenexam,机试=labexam from stuexam +select stuno as 学号 , writtenexam as 笔试, labexam as 机试 from stuexam +select stuno 学号 ,writtenexam 笔试,labexam 机试 from stuexam +select stuNO,stuName,stuAddress from stuinfo +select stuName+'@'+stuAddress as '邮箱' from stuinfo +select stuno,writtenexam,labexam,writtenexam+labexam from stuexam +select distinct stuAddress from stuinfo +select distinct stuAge as '所有年龄' from stuinfo +select top 3 * from stuinfo +select top 4 stuName,stuSeat from stuinfo +select top 50 percent *from stuinfo +select*from stuinfo where stuAddress='湖北武汉' or stuAge=20 +select labexam 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 writtenexam 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 stuAddress 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 \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b76819f884f5c9aa15eea9ae46da9f3e1416e2fd --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" @@ -0,0 +1,91 @@ +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 + +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 StuExam where labExam between 60 and 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 StuExam where writtenExam not between 70 and 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 + +select * from StuExam where labExam = (select max(labExam) from StuExam) + +select * from StuExam where writtenExam = (select min(writtenExam) from StuExam) diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..be8fe4f4d1d4b95e174e9eb7d5a51e9fb11cb0e1 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.sql" @@ -0,0 +1,83 @@ +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 \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/.keep" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\254\254\344\272\224\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\254\254\344\272\224\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..73e100337499137ee7e352ed792f3888ce521e6f --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\254\254\344\272\224\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.sql" @@ -0,0 +1,87 @@ +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) + + 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.查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 + select stuNo 学号,stuName 姓名,stuAge 年龄,stuAddress 地址,stuSeat 座位号,stuSex 性别 from StuInfo + --2.查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 + select stuName,stuAge,stuAddress from StuInfo + --3.查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字 注意:要用三种方法 + select examNo 学号,writtenExam 笔试,labExam 机试 from StuExam + -- + -- + --4.查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱” + alter table StuInfo alter column stuAddress char(8) + select stuName+'@'+stuAddress 邮箱 from StuInfo + --5.查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分(笔试+机试)这四列的信息 + select examNo 学号,writtenExam 笔试,labExam 机试,writtenExam+labExam 总分 from StuExam + --6.查询学生信息表(stuInfo)中学生来自哪几个地方 + select distinct stuAddress from StuInfo + --7.查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名'所有年龄' + select distinct stuAge 所有年龄 from StuInfo + --8.查询学生信息表(stuInfo)中前3行记录 + select top 3 * from StuInfo + --9.查询学生信息表(stuInfo)中前4个学生的姓名和座位号 + select top 4 stuName,stuSeat from StuInfo + --10.查询学生信息表(stuInfo)中一半学生的信息 + select top 4 * from stuInfo + --11.将地址是湖北武汉,年龄是20的学生的所有信息查询出来 + select * from StuInfo where stuAge=20 and stuAddress='湖北武汉' + --12.将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列 + select * from StuExam where labExam>=60 and labExam<=80 order by labExam DESC + --13.查询来自湖北武汉或者湖南长沙的学生的所有信息(用两种方法实现,提示;or和in) + select * from StuInfo where stuAddress='湖北武汉' or stuAddress='湖南长沙' + select * from StuInfo where stuAddress in('湖北武汉','湖南长沙') + --14.查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列 + select * from StuExam where not writtenExam>=70 and writtenExam<=90 order by writtenExam ASC + --15.查询年龄没有写的学生所有信息 + select * from StuInfo where stuAge is null or stuAge='' + --16.查询年龄写了的学生所有信息 + select * from StuInfo where stuAge is not null and not stuAge='' +--17.查询姓张的学生信息18.查询学生地址中有‘湖’字的信息 + select * from StuInfo where stuName like '张%' and stuAddress like '湖%' +--19.查询姓张但名为一个字的学生信息 + select * from StuInfo where stuName like '张_' +--20.查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 + select * from StuInfo where stuName like '%俊_' +--21.按学生的年龄降序显示所有学生信息 + select * from StuInfo order by stuAge DESC +--22.按学生的年龄降序和座位号升序来显示所有学生的信息 + select * from StuInfo order by stuAge ASC +--23显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 + select top 1 * from StuExam order by writtenExam DESC +--24.显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 + select top 1 * from StuExam order by labExam ASC + diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/SQLQuery2.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ad4ad3854198134fc6bf42643223f8d81556f027 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/SQLQuery2.sql" @@ -0,0 +1,76 @@ +use master +go +create database Students04 +on +( +name= 'Students04', +filename='C:\test\Students04.mdf', +size=5MB, +maxsize=100MB, +filegrowth=10MB +) +log on +( +name='Students04_log', +filename='C:\test\Students04_log.ldf', +size=5MB, +maxsize=100MB, +filegrowth=10MB +) +create database Students +go +use Students +go + +create table StuInfo( + stuNo nvarchar(20) primary key not null, + stuName nvarchar(10)not null, + stuAge int not null, + stuAddress nvarchar(20) null, + stuSeat int not null, + stuSex int check (stuSex=1 or stuSex=0)not null +) +go +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) +go +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 +) +go +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) +go +select stuNo as 学号 ,stuName as 姓名,stuAge as 年龄,stuAddress as 地址,stuSeat as 座位号,stuSex as 性别 from StuInfo +select stuName,stuAge,stuAddress from StuInfo +select stuNo as 学号,writtenExam as 笔试,labExam as 机试 from stuExam +select stuNo,stuName,stuAddress ,stuName+'@'+stuAddress as 邮箱 from StuInfo +select stuNo,writtenExam,labExam,writtenExam+labExam as 总分 from stuExam +select stuName, stuAddress from StuInfo +select stuAge as 年龄, count(*) as 所有年龄 from StuInfo group by stuAge +select top 3 * from StuInfo +select top 4 stuName as 姓名,stuSeat as 座位号 from StuInfo +select top 50 percent * from StuInfo +select * from StuInfo where stuAddress='湖北武汉' and stuAge=20 +select * from stuExam where labExam between 60 and 80 order by labExam desc +select * from StuInfo where stuAddress='湖北武汉' or stuAddress= '湖南长沙' +select * from StuInfo where stuAddress in ('湖北武汉','湖南长沙') +select * from stuExam where writtenExam not between 70 and 90 order by writtenExam asc +select * from StuInfo where stuAge=null or stuAge=' ' +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 +select top 1 * from stuExam order by writtenExam desc +select top 1 * from stuExam order by labExam + diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1 (1).sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1 (1).sql" new file mode 100644 index 0000000000000000000000000000000000000000..823bf69eb272e039c5caa6f42b06278764753473 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1 (1).sql" @@ -0,0 +1,83 @@ +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 diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..170b32d0f690eefa6410de365cf3c889bcb10158 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" @@ -0,0 +1,154 @@ +use master +go + +create database Students +on +( + name = 'Students', + filename = 'D:\数据库\Students.mdf' +) + +log on +( + name = 'Students_log', + filename = 'D:\数据库\Students_log.ldf' +) + +use Students +go + +--按图片参考,进行数据表的建立和数据插入,然后进行以下查询操作 +create table StuInfo( + stuNo nvarchar(10) primary key, + stuName nvarchar(10)not null, + stuAge int not null, + stuAddress nvarchar(20) null, + stuSeat int not null, + stuSex int not null +) +go + +insert into Student(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) +go + +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 +) +go + +insert into Exam(stuNo,writtenExam,labExam) values +('s2501',50,70),('s2502',60,65),('s2503',86,85), +('s2504',40,80),('s2505',70,90),('s2506',85,90) + +go + + +--1.查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 +select stuNo as 学号 ,stuName as 姓名,stuAge as 年龄,stuAddress as 地址,stuSeat as 座位号,stuSex as 性别 from StuInfo +--在本来的名字基础上,在后面+as 中文名称即可 + +--2.查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 +select stuName,stuAge,stuAddress from StuInfo +--套用方法:select * from 表名 然后将*改为你需要的列即可,列名之间要逗号隔开 + +--3.查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字注意:要用三种方法 +select stuNo as 学号,writtenExam as 笔试,labExam as 机试 from stuExam + +--4.查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱” +select stuNo,stuName,stuAddress ,stuName+'@'+stuAddress as 邮箱 from StuInfo + +--5.查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分(笔试+机试)这四列的信息 +select stuNo,writtenExam,labExam,writtenExam+labExam as 总分 from stuExam + +--套用方法:select * from 表名 然后将*改为你需要的列即可,列名之间要逗号隔开,将要求列相加as重命名即可 + +--6.查询学生信息表(stuInfo)中学生来自哪几个地方 +select stuName, stuAddress from StuInfo + +--7.查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名'所有年龄' +select stuAge as 年龄, count(*) as 所有年龄 from StuInfo group by stuAge + + +--select 所需列名 ,(逗号隔开) count(*)表示为这一列所以信息 from 表 group by 聚合函数,进行统计分类 +--8.查询学生信息表(stuInfo)中前3行记录 +select top 3 * from StuInfo + +--套用方法:select * from 表名 在*前加上top 要几行就写几 + +--9.查询学生信息表(stuInfo)中前4个学生的姓名和座位号 +select top 4 stuName as 姓名,stuSeat as 座位号 from StuInfo + +--10.查询学生信息表(stuInfo)中一半学生的信息 + +select top 50 percent * from StuInfo + + + +--11.将地址是湖北武汉,年龄是20的学生的所有信息查询出来 +select * from StuInfo where stuAddress='湖北武汉' and stuAge=20 + +--条件查询 where后面接所需的条件,多个条件用and隔开 + +--12.将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列 +select * from stuExam where labExam between 60 and 80 order by labExam desc + +--条件查询 取值范围用between...and...链接,order by进行排序,默认为升序排序(ASC)可不写,降序为(desc) + +--13.查询来自湖北武汉或者湖南长沙的学生的所有信息(用两种方法实现,提示;or和in) +select * from StuInfo where stuAddress='湖北武汉' or stuAddress= '湖南长沙' + +--同一条件多个参数用or隔开 + +select * from StuInfo where stuAddress in ('湖北武汉','湖南长沙') + + +--14.查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列 +select * from stuExam where writtenExam not between 70 and 90 order by writtenExam asc + +--15.查询年龄没有写的学生所有信息 +select * from StuInfo where stuAge=null or stuAge=' ' + +--16.查询年龄写了的学生所有信息 +select * from StuInfo where stuAge is not null + +--is not null为不是空的 + + +--17.查询姓张的学生信息 +select * from StuInfo where stuName like '%张%' + +--模糊查询用like 单引号内为前后各一个百分号 + +--18.查询学生地址中有‘湖’字的信息 +select * from StuInfo where stuAddress like '%湖%' + +--19.查询姓张但名为一个字的学生信息 +select * from StuInfo where stuName like '%张_' + +--模糊查询中,_下划线为占位符,表达为后面一个字,或者前面几个字 + +--20.查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 +select * from StuInfo where stuName like '%__俊%' + +--21.按学生的年龄降序显示所有学生信息 +select * from StuInfo order by stuAge desc + +--22.按学生的年龄降序和座位号升序来显示所有学生的信息 +select * from StuInfo order by stuAge desc,stuSeat + +--23显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 + +select top 1 * from stuExam order by writtenExam desc + +--将排名进行降序排序找到第一个就是第一名 + + +--24.显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from stuExam order by labExam \ No newline at end of file diff --git "a/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery1.sql" "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3ba18ee77ceedf9e735dcf95ec4a25f52e5d0115 --- /dev/null +++ "b/\347\254\254\344\272\224\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery1.sql" @@ -0,0 +1,83 @@ +use master +go + +create database Student05 +go + +use Student05 +go + +create table StulS +( + StuNO nvarchar(10) unique not null, + StuName nvarchar(10) not null, + StuAge int not null, + StuAddress nvarchar(200), + StuSeat nvarchar(8) not null, + StuSex char(1) default('男') check (StuSex='女' or StuSex='男') +) + +insert into StulS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2501','张秋利','20','美国硅谷','1','女') +insert into StulS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2502','李斯文','18','湖北武汉','2','男') +insert into StulS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2503','马文才','22','湖北长沙','3','男') +insert into StulS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2504','欧阳俊雄','21','湖北武汉','3','女') +insert into StulS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2505','梅超风','20','湖北武汉','4','女') +insert into StulS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2506','陈旋风','19','美国硅谷','5','女') +insert into StulS(StuNO,StuName,StuAge,StuAddress,StuSeat,StuSex) +values('s2507','陈风','20','美国硅谷','7','女') + +select *from StulS +select 学号=StuNO ,姓名=StuName,年龄=StuAge,地址=StuAddress,座位号=StuSeat,性别=StuSex from StulS +select Stuname,StuAge,StuAddress from StulS +select 学号=StuNO ,姓名=StuName,地址=StuAddress,邮箱=StuName+StuAddress from StulS +select distinct StuAddress from StulS +select distinct StuAge as 所有年龄 from StulS +select top 3 *from StulS +select top 4 StuName,StuSeat from StulS +select top 50 percent *from StulS +select * from StulS where StuAddress in ('湖北武汉','湖南长沙') +select * from StulS where StuAddress='湖北武汉' or StuAddress ='湖南长沙' +select * from StulS where StuAge is null +select * from StulS where StuAge is not null +select * from StulS where StuName like '张%' +select * from StulS where StuAddress like '%湖%' +select * from StulS where StuName like '张_' +select * from StulS where StuName like '__俊%' +select * from StulS order by StuAge DESC +select * from StulS order by StuAge DESC , StuSeat ASC + + +create table Grate +( + ExamNo int primary key identity(1,1), + StuNo nvarchar(10) references StulS(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 \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/.keep" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/hzy/\344\275\234\344\270\2323.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/hzy/\344\275\234\344\270\2323.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a53f0d880abea39543871c6d23b89167f96656d7 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/hzy/\344\275\234\344\270\2323.sql" @@ -0,0 +1,89 @@ +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), + 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 + set identity_insert bbsSection ON--打开 + insert into bbsSection values (5,' 技术交流',1),(6,'读书世界' ,3),(7,' 生活百科',1),(8,'八卦区' ,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 '%!' diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/hzy/\344\275\234\344\270\232\344\272\214.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/hzy/\344\275\234\344\270\232\344\272\214.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9437f2e386f5045ab4ca68d6c90ad310685feb9a --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/hzy/\344\275\234\344\270\232\344\272\214.sql" @@ -0,0 +1,64 @@ +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 diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f7a02052d09d4c1b6b4f5878f498af402916bd9a --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery1.sql" @@ -0,0 +1,88 @@ +create database OrderIno +on +( + Name='OrderIno', + FileName='D:\SQL代码\OrderIno.mdf', + size=5MB, + Maxsize=50MB, + Filegrowth=10% +) +log on +( + Name='OrderForm_log', + FileName='D:\SQL代码\OrderIno_log.ldf', + size=5MB, + Maxsize=50MB, + Filegrowth=10% +) +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 --订购项目表 + +--1.查询所有订单订购的所有物品数量总和 +select sum(theNumber) from orderltem +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select orderID 订单编号,theNumber 物品的数量,AVG(theMoney)平均单价 +from orderltem group by orderID, theNumber having orderID<3 and AVG(theMoney)<10 + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select orderID 订单编号,sum(theNumber) 物品数量 , AVG(theMoney) 平均单价 +from orderltem group by orderID having AVG(theMoney)<10 and sum(theNumber)>50 +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select itemType 类别,count(*)订购次数 from orderltem group by itemType + +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select itemType 类别, sum(theNumber) 订购总数量,avg(theMoney) 平均单价 from orderltem group by itemType having sum(theNumber)>=100 + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 + +select itemname 产品名称,count(*) 订购次数,sum(theNumber) 订购总数量,avg(theMoney) 平均单价 +from orderltem group by itemname diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2d0d58c2f2f2a56a93c21afa70ad9840fe1fadc2 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery2.sql" @@ -0,0 +1,157 @@ +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% +) + +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 + +use bbs +go + +select * from bbsReply --回帖表 +select * from bbsSection --版块表 +select * from bbsTopic --主帖表 +select * from bbsUsers --用户信息表 + +--1.在主贴表中统计每个版块的发帖总数 +select tSID 版块,count(tID) 数量 from bbsTopic group by tSID + +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID 主贴,count(rID) 回帖总数量 from bbsReply group by rTID + +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID 用户,count(tID) 主帖的总数 from bbsTopic group by tUID + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select tUID 用户,tID 主帖,sum(tCount) 回复数量总和 from bbsTopic group by tUID,tID + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select tUID 用户,tID 主帖,avg(tCount) 回复数量总和 from bbsTopic group by tUID,tID having avg(tCount)>3 + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select uName 用户名, uSex 性别 , uAge 年龄, uPoint 积分 from bbsUsers where uPoint=(select max(uPoint) from bbsUsers) +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic where rMsg like '%快乐%' or tTitle like '%快乐%' + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers where uAge>=15 and uAge<=20 and uPoint>=10 +select * from bbsUsers where uAge between 15 and 20 and uPoint>=10 + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where uName like '小_大%' + +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来, +--并且为列取上对应的中文列名 +select tTitle 标题,rMsg 内容 from bbsTopic where rTime between '2008-9-10 12:00:00' and getdate() and tCount>10 + +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tCount 回复数量,tUID 发帖人编号 from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-bbs.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-bbs.sql" new file mode 100644 index 0000000000000000000000000000000000000000..266400c469ec198654e7ad49e3254c56379e3fad --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-bbs.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/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-order1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-order1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ac2255e6fac1408878369d4d9597a206c723fc11 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery-order1.sql" @@ -0,0 +1,84 @@ +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'),('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 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 + select orderId,sum(theNumber),avg(theMoney)平均单价 from orderItem group by orderId + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select orderId,theNumber,avg(theMoney)平均单价 from orderItem group by orderId,theNumber having avg(theMoney)<10 and theNumber>50 + +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select itemType 产品类别,count(itemType)订购次数 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(orderId)订购次数,sum(theNumber) 总数量,avg(theMoney)平均单价 from orderItem group by itemName \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\2751.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\2751.sql" new file mode 100644 index 0000000000000000000000000000000000000000..99cce41d208e424a91c42910f02b0f745c9581a4 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\2751.sql" @@ -0,0 +1,143 @@ +create database bbs +go +use bbs +go + +--用户信息表 +create table bbsUser +( + UID int 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), +) +--添加约束 +--主键约束 +alter table bbsUser add constraint PK_bbsUser_UID primary key (UID) +--唯一约束 +alter table bbsUser add constraint UK_bbsUser_uName unique(uName) + +--版块表 +create table bbsSection +( + sID int identity(1,1) primary key, + sName varchar(10) not null, + sUid int, +) +--外键 引用用户信息表的用户编号 +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUser(UID) + +--主贴表 +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, +) +--添加约束 +--外键约束 引用用户信息表的用户编号 +alter table bbsTopic add constraint FK_bbsTopic_tUID foreign key(tUID) references bbsUser(UID) +--外键约束 引用板块表的版块编号 +alter table bbsTopic add constraint FK foreign key (tSID) references bbsSection(sID) + +--回贴表 +create table bbsReply +( + rID int primary key identity(1,1), + rUID int, + rTID int, + rMsg text not null, + rTime datetime, +) +--外键约束 引用用户信息表的用户编号 +alter table bbsReply add constraint FK_bbsReply_rUID foreign key (rUID) references bbsUser(UID) + +--外键约束 引用主贴表的主贴编号 +alter table bbsReply add constraint FK_bbsReply_rTID foreign key (rTID) references bbsTopic(tID) + +--现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +--小雨点 女 20 0 +--逍遥 男 18 4 +--七年级生 男 19 2 + +insert into bbsUser(uName,uSex,uAge,uPoint) values ('小雨点','女',20,0),('逍遥','男',18,4),('七年级','男',19,2) + +--2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 +--插入多行: +select uName,uPoint into bbsPoint from bbsUsers + +--给论坛开设4个板块 +--名称 版主名 +--技术交流 小雨点 +--读书世界 七年级生 +--生活百科 小雨点 +--八卦区 七年级生 + +insert into bbsSection(sName,sUid) values ('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + +--4.向主贴和回帖表中添加几条记录 +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(2,4,'范跑跑','谁是范跑跑',2008-7-8,1) ,(3,2,'.NET','与Java的区别是什么?',2008-9-1,2),(1,4,'今年夏天流行什么','有谁知道今年夏天最流行什么?',2008-9-10,0) +insert into bbsReply(rMsg,rTime) values('逃跑的老师',2008-7-8),('??',2008-7-9),('流行看美女',2008-7-10) +select * from bbsUser +--5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户, +--请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) +delete from bbsTopic where tUID=2 +delete from bbsReply where rUID=2 +delete from bbsReply where rID=1 +delete from bbsUsers where uName='逍遥' + +--6.因为小雨点发帖较多,将其积分增加10分 +update bbsUsers set upoint=30 where uName='小雨点' +--7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) +delete from bbsTopic where tSID=3 +delete from bbsSection where sName='生活百科' + +-- 8.因回帖积累太多,现需要将所有的回帖删除 +delete from bbsReply + + + + +--作业三 +--1.在主贴表中统计每个版块的发帖总数 +select tSID,count(*)发帖总数 from bbsTopic group by tSID + +--2.在回帖表中统计每个主贴的回帖总数量 +select rID,count(*)回帖总数量 from bbsReply group by rID + +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID,count(*)主贴的总数 from bbsTopic group by tUID + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select sum(tCount) 回复数量总和 from bbsTopic + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select tCount,avg(tCount)平均回复量 from bbsTopic group by tCount +having avg(tCount)>1 order by tCount + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select * FROM bbsUser +select top 1 * from bbsUser order by uPoint DESC + +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic +select * from bbsTopic where tTitle='快乐' + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在1分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUser where uAge>=15 and uAge<=20 and uPoint>1 + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUser +select * from bbsUser where uName like '小_大%' + +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select * from bbsTopic +select tTitle 标题, tMsg 内容 from bbsTopic where tTime='2008-9-10 12:00:00' and tCount>10 + +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID,tCount from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\275\350\201\232\345\220\210\345\207\275\346\225\260.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\275\350\201\232\345\220\210\345\207\275\346\225\260.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9d5f11f9b7082eb0ae85ea1bfdbca515131e723f --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\275\350\201\232\345\220\210\345\207\275\346\225\260.sql" @@ -0,0 +1,58 @@ +create database HZT +go +use HZT +go + +--订单表 +create table orders +( + orderId int primary key, + orderDate datetime , +) +--插入数据 +insert into orders(orderId,orderDate) values(1,'2008-01-12'),(2,'2008-02-10'),(3,'2008-02-15'),(4,'2008-03-10') +select * from orders +--订购项目表 +create table orderItem +( + ItemiD int identity, + orderId int, + itemType nvarchar(10), + itemName nvarchar(10), + theNumber int , + theMoney int , +) +--插入数据 +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 +select ItemiD 项目编号,orderId 订单编号,itemType 产品类别, +itemName 产品名称,theNumber 订购数量,theMoney 订购单价 from orderItem +--1.查询所有订单订购的所有物品数量总和 +select sum(theNumber)物品数量总和 from orderItem + +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select orderId,avg(theMoney)平均单价,sum(theNumber)数量和 from orderItem where orderId<3 group by orderId +having avg(theMoney)<10 order by orderId + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select orderId,avg(theMoney)平均单价,sum(theNumber)数量和 from orderItem orderId group by orderId +having sum(theNumber)>50 order by orderId + +--4.查询每种类别的产品分别订购了几次,例如: +--文具 9 +--体育用品 3 +--日常用品 3 +select itemType, count(*)数量 from orderItem group by itemType order by itemType desc + +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select itemType,sum(theNumber)订购总数量,avg(theMoney)平均单价 from orderItem group by itemType +having sum(theNumber)>100 order by itemType + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: +--产品名称 订购次数 总数量 平均单价 +--笔 3 120 2 + +select itemName,count(*)订购次数,sum(theNumber) 订购总数量,avg(theMoney)平均单价 from orderItem group by itemName diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1ef53ec683b97fa0a147e195d128f8d23a7ad8d3 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery1.sql" @@ -0,0 +1,86 @@ +--先创建如图所示表 + +use master +go + +create database ordersystem +on +( + name='ordersystem', + filename='E:\数据库\ordersystem.mdf', + size=5mb, + maxsize=100mb, + filegrowth=15% +) + +log on +( + name='bank_log', + filename='E:\数据库\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(1,1), + 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') +--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 订单编号,avg(theMoney)平均单价,sum(theNumber)数量和 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)>100 + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: +--产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 +select itemName,COUNT(*)订购次数,sum(theNumber)总数量,avg(theMoney)平均单价 from orderItem group by itemName + + diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ea7fb04e00f04b880f61d034ad56769e5f560dc3 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery2.sql" @@ -0,0 +1,140 @@ +use master +go + +create database bbs +on +( + name='bbs01', + filename='E:\数据库\bbs.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10% + +) +, +( + name='bbs02', + filename='E:\数据库\bbs02.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='E:\数据库\bbs_log.ldf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) + +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) +) + +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--用户信息表 +insert into bbsUsers values ('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) + +select * from bbsSection--版块表 +insert into bbsSection values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + +select *from bbsTopic--主贴表 +insert into bbsTopic values +(2,5,'范跑跑','谁是范跑跑','2008-7-8',1), +(3,2,'.NET','与JAVA的区别是什么','2008-9-1',2), +(1,4,'今年夏天流行什么','有谁知道今年夏天流行什么呀?','2008-9-10',0) + +select * from bbsReply--回帖表 +insert into bbsReply values +(1,3,'范跑跑','2008-10-2'), +(2,2,'区别不大','2008-10-2'), +(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.在主贴表中统计每个版块的发帖总数 +select tSID, COUNT(tSID) from bbsTopic group by tSID + +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID 主贴编号, COUNT(rID)回帖总数量 from bbsReply group by rTID + +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID 发帖人编号, COUNT(tSID)发帖总次数 from bbsTopic group by tUID + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select tUID 发帖人编号, SUM(tCount) 回复数量总和 from bbsTopic group by tUID + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +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 + +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic +select * from bbsTopic where tTitle like '%快乐' and tMsg like '%快乐' + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers +select * from bbsUsers where uAge>=15 and uAge<=20 and uPoint>=10 + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where uName like '小__' or uName like '__大' + +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select * from bbsTopic +select rTime 时间,tCount 回复数量,tTitle 标题,rMsg 内容 from bbsTopic where tCount>10 + +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID 发帖人编号,tCount 回复数量 from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1e08360ffb850516e0541f914153ec33c1e0422f --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy1.sql" @@ -0,0 +1,64 @@ +create database Giao +on +( + name='Giao', + filename='D:\Giao.mdf' +) +log on +( + name='Giao_log', + filename='D:\Giao_log.ldf' +) +go + +use Giao +go + +create table orders +( + orderId int primary key, + orderDate date +) + +create table orderItem +( + ItemiD int identity(1,1) primary key, + orderId int, + itemType nvarchar(5), + 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), + (2,'文具','尺',10,1), + (3,'体育用具','篮球',1,56), + (4,'文具','笔',36,2), + (5,'文具','固体胶',20,3), + (6,'日常用品','透明胶',2,1), + (7,'体育用具','羽毛球',20,3), + (8,'文具','订书机',20,3), + (9,'文具','订书针',10,3), + (10,'文具','裁纸刀',5,5), + (11,'文具','笔',20,2), + (12,'文具','信纸',50,1), + (13,'日常用品','毛巾',4,5), + (14,'日常用品','透明胶',30,1), + (15,'体育用具','羽毛球',20,3) +--查询所有订单订购的所有物品数量总和 +select sum(theNumber) 订购物品总量 from orderItem +--查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select orderId,sum(theNumber) 订购物品总量,avg(theMoney) 平均单价 from orderItem group by orderId having orderId<3and avg(theMoney)<10 +--查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select orderId,sum(theNumber) 订购物品总量,avg(theMoney) 平均单价 from orderItem group by orderId having avg(theMoney)<10 and sum(theNumber)>50 +--查询每种类别的产品分别订购了几次 +select itemType'类别',count(*)'分别订购了几次' from orderItem group by itemType +--查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select itemType'类别',sum(theNumber)'订购物品总量',avg(theMoney)'平均单价' from orderItem group by itemType having sum(theNumber)>100 +--查询每种产品的订购次数,订购总数量和订购的平均单价 +select itemName'产品名称',count(*)'订购次数',sum(theNumber)'订购总量',avg(theMoney)'平均单价' from orderItem group by itemName \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cf9dfdab10da0ddd2125565470c4c224440d2504 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\344\270\226\350\276\211/zy2.sql" @@ -0,0 +1,44 @@ +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, +) \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery12.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery12.sql" new file mode 100644 index 0000000000000000000000000000000000000000..916706b13df1958b2fafa8381b9c2d7f3c2523cd --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery12.sql" @@ -0,0 +1,68 @@ +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 + + + diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery13.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery13.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fde46a58f7c1504452200985e29110db33fa5def --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery13.sql" @@ -0,0 +1,28 @@ +use bbs +go + +select tSID 板块编号 ,count(tCount)发帖总数 from bbsTopic group by tSID + +select rTID 主贴编号, count(rID) 回帖编号 from bbsReply group by rTID + +select tUID 发帖人编号, count(tSID)主贴总数 from bbsTopic group by tUID + +select tUID 发帖人编号,sum(tCount)回复数量 from bbsTopic group by tUID + +select tSID 板块编号 , AVG(tCount) 平均数量 from bbsTopic group by tSID having AVG(tCount)>3 + +select top 1 uName 用户名, uSex 性别, uAge 年龄, uPoint 积分 from bbsUsers order by uPoint desc + +select tTitle , tMsg from bbsTopic where tTitle like '%快乐%' or tMsg like '%快乐%' + +select uName 用户名,uAge 年龄,uPoint 积分 from bbsUsers group by uName ,uAge ,uPoint having uAge>=15 and uAge <=20 and uPoint>10 +select * from bbsUsers where uAge between 15 and 20 and uPoint>10 + +select * from bbsUsers where uName like '小_大%' + +select tTime 时间,tCount 回复数量 ,tTitle 标题 ,tMsg 内容 from bbsTopic where tCount>10 + +select tUID 发帖人编号 ,tCount 回复数量 from bbsTopic where tTitle like'%!' + + + diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\344\275\234\344\270\2322\345\210\230\345\273\272\345\263\260.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\344\275\234\344\270\2322\345\210\230\345\273\272\345\263\260.sql" new file mode 100644 index 0000000000000000000000000000000000000000..86b31e60289ab867a58259b5ee191219689ad631 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\344\275\234\344\270\2322\345\210\230\345\273\272\345\263\260.sql" @@ -0,0 +1,85 @@ +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 sum(theNumber) from orderltem +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select orderld 订单编号,theNumber 物品数量,AVG(theMoney) 平均单价 +from orderltem group by orderld, theNumber having orderld<3 and AVG(theMoney)<10 + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select orderld 订单编号,sum(theNumber) 物品数量 , AVG(theMoney) 平均单价 +from orderltem group by orderld having AVG(theMoney)<10 and sum(theNumber)>50 +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select itemType 类别,count(*)订购次数 from orderltem group by itemType + +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select itemType 类别, sum(theNumber) 订购总数量,avg(theMoney) 平均单价 from orderltem group by itemType having sum(theNumber)>=100 + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 + +select itemname 产品名称,count(*) 订购次数,sum(theNumber) 订购总数量,avg(theMoney) 平均单价 +from orderltem group by itemname + \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\344\275\234\344\270\2323\345\210\230\345\273\272\345\263\260.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\344\275\234\344\270\2323\345\210\230\345\273\272\345\263\260.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b1ba9e0aa3b6dd9b573d0e107bb411e9d640d484 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/\344\275\234\344\270\2323\345\210\230\345\273\272\345\263\260.sql" @@ -0,0 +1,143 @@ +use master +go + +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10% + +) +log on +( + name='bbs2_log', + filename='D:\bbs2_log.ldf', + size=1MB, + maxsize=100MB, + filegrowth=10% +) + +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') + + +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.在主贴表中统计每个版块的发帖总数 +select tSID 版块,count(tID) 数量 from bbsTopic group by tSID + +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID 主贴,count(rID) 回帖总数量 from bbsReply group by rTID + +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID 用户,count(tID) 主帖的总数 from bbsTopic group by tUID + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select tUID 用户,tID 主帖,sum(tCount) 回复数量总和 from bbsTopic group by tUID,tID + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select tUID 用户,tID 主帖,avg(tCount) 回复数量总和 from bbsTopic group by tUID,tID having avg(tCount)>3 + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select uName 用户名, uSex 性别 , uAge 年龄, uPoint 积分 from bbsUsers where uPoint=(select max(uPoint) from bbsUsers) +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic where rMsg like '%快乐%' or tTitle like '%快乐%' + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers where uAge>=15 and uAge<=20 and uPoint>=10 +select * from bbsUsers where uAge between 15 and 20 and uPoint>=10 + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where uName like '小_大%' + +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来, +--并且为列取上对应的中文列名 +select tTitle 标题,rMsg 内容 from bbsTopic where rTime between '2008-9-10 12:00:00' and getdate() and tCount>10 + +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tCount 回复数量,tUID 发帖人编号 from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4057bda962dab1fc08215379226b65292a53cc2e --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" @@ -0,0 +1,115 @@ +create database bbs +on +( + name='bbs', + filename='F:\bbs.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='F:\bbs_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use bbs +go + +create table bbsUsers +( + uuID 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 primary key (uuID) +alter table bbsUsers add constraint UK unique(uName) +alter table bbsUsers add constraint CK check(uSex='男' or uSex='女' ) +alter table bbsUsers add constraint CK1 check(uAge>=15 or uAge<=60 ) +alter table bbsUsers add constraint CK2 check(upoint>=0) + +create table bbsSection +( + sID int 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(uuID) + + + +create table bbsTopic +( + tID int primary key, + tUID int foreign key references bbsUsers(uuID), + tSID int foreign key references bbsSection(sID), + +) +alter table bbsTopic add tTitle varchar(100) not null +alter table bbsTopic add tMsg text not null +alter table bbsTopic add tTime datetime +alter table bbsTopic add tCount int + +create table bbsReply +( + rID int primary key, + rUID int foreign key references bbsUsers(uuID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女',20,0 union +select'逍遥','男',18,4 union +select'七年级生','男',19,2 +select * from bbsUsers + + +select uName,uPoint into bbsPoint from bbsUsers + +insert into bbsSection(sName, sID) +select '技术交流' , 1union +select '读书世界' ,2 union +select '生活百科' ,3 union +select ' 八卦区' ,4 +select * from bbsSection +insert into bbsTopic( tid,tUID,tSID,tTitle,tMsg,tTime,tCount ) +select 1,3, 4,'范跑跑','谁是范跑跑','2008-7-8',1 union +select 2,2, 1,' .NET','与JAVA的区别是什么呀?','2008-9-1',2 union +select 3,1, 3,' 今年夏天最流行什么','有谁知道今年夏天最流行','2008-9-10',0 +select * from bbsTopic + + + + + +--1.在主贴表中统计每个版块的发帖总数 +select tSID, count(tSID) 发帖总数 from bbsTopic group by tSID +select * from bbsTopic +--2.在回帖表中统计每个主贴的回帖总数量 +select sum(rID)回帖总数量 from bbsReply group by rID +--3.在主贴表中统计每个用户的发的主帖的总数 +select count(*)主帖的总数 from bbsTopic group by tUID +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select sum(tCount)总回复数 from bbsTopic where tCount>=1 +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select tSID,tCount from bbsTopic group by tSID,tCount having AVG(tCount)>3 +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select top 1 uName , uSex , uAge ,uPoint from bbsUsers group by uName , uSex , uAge ,uPoint order by uPoint desc +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select tTitle from bbsTopic where tTitle='%快乐%' and tMsg='%快乐%' group by tTitle +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers where uAge>=15 and uAge<=20 and uPoint>=10 +select uAge,uPoint from bbsUsers where uAge between '15' and '20' and uPoint>=10 group by uAge,uPoint +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where uName ='小_大' +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select tTitle 标题,tMsg 内容 from bbsTopic where DateDiff(dd,tTime,getdate())=2008-9-10 and tCount>=10 group by tTitle,tMsg +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID,tCount from bbsTopic where tTitle='%!' group by tUID,tCount \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0c596f186dca74b47f1ec52d26a991388247c666 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" @@ -0,0 +1,63 @@ +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 + +--1.查询所有订单订购的所有物品数量总和 +select sum(theNumber) from orderItem +select * from orderItem +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select orderId,sum(theNumber),avg(theMoney) from orderItem where orderId<3 group by orderId,theNumber,theMoney having AVG(theMoney)<10 +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select sum(theNumber), avg(theMoney) from orderItem group by theMoney having AVG(theMoney)<10 and sum(theNumber)>50 +-- +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select itemType 产品名称,count(itemType)次数 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(theNumber)次数,sum(theNumber)总数量,avg(theMoney)平均单价 from orderItem group by itemName \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c23f951afb4046b23940238fd0743e387e8fd9c6 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/SQLQuery2.sql" @@ -0,0 +1,195 @@ +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 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) + +go +use BBS +go +create table Orders +( +OrderID int primary key not null, +OrderDate datetime +) +go +use BBS +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') + +--作业二 +--1.查询所有订单订购的所有物品数量总和 +select SUM(TheNumber) from Orderltem + +--2.查询订单编号小于3的,平均单价小于10的每个订单订购的所有物品的数量和以及平均单价 +select OrderID from Orderltem where OrderID<3 +union +select TheMoney from Orderltem where TheMoney<10 + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select TheMoney from Orderltem where TheMoney<10 +union +select TheNumber from Orderltem where TheNumber>50 + +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select ItemType 产品名称,count(ItemType)次数 from Orderltem group by ItemType + +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select ItemType,sum(TheNumber),avg(TheMoney) from Orderltem group by ItemType having sum(TheNumber)>100 + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 +select ItemName 名称,count(TheNumber)次数,sum(TheNumber)总数量,avg(TheMoney)平均单价 from Orderltem group by ItemName + +--作业三 +--在论坛数据库中完成以下题目 +--1.在主贴表中统计每个版块的发帖总数 +select TSID, count(TSID)发帖总数 from BBSTopic group by TSID + +--2.在回帖表中统计每个主贴的回帖总数量 +select sum(RID)回帖总数量 from BBSReply group by RID + +--3.在主贴表中统计每个用户的发的主帖的总数 +select count(*)主帖的总数 from BBSTopic group by TUID + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select sum(Tcount)总回复数 from BBSTopic where Tcount>=1 + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select TSID,Tcount from BBSTopic where AVG(Tcount)>3 + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select top 1 UName, USex,UAge,UPoint from BBSUsers + +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select TTitle from BBSTopic where TTitle='%快乐%' and TMsg='%快乐%' group by TTitle + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from BBSUsers where UAge>=15 and UAge<=20 and UPoint>=10 + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from BBSUsers where UName ='小_大' + +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 + + +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select TUID 发帖人编号,Tcount 回复数量 from BBSTopic where TTitle like '%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1ef53ec683b97fa0a147e195d128f8d23a7ad8d3 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/SQLQuery1.sql" @@ -0,0 +1,86 @@ +--先创建如图所示表 + +use master +go + +create database ordersystem +on +( + name='ordersystem', + filename='E:\数据库\ordersystem.mdf', + size=5mb, + maxsize=100mb, + filegrowth=15% +) + +log on +( + name='bank_log', + filename='E:\数据库\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(1,1), + 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') +--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 订单编号,avg(theMoney)平均单价,sum(theNumber)数量和 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)>100 + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: +--产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 +select itemName,COUNT(*)订购次数,sum(theNumber)总数量,avg(theMoney)平均单价 from orderItem group by itemName + + diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ea7fb04e00f04b880f61d034ad56769e5f560dc3 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/SQLQuery2.sql" @@ -0,0 +1,140 @@ +use master +go + +create database bbs +on +( + name='bbs01', + filename='E:\数据库\bbs.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10% + +) +, +( + name='bbs02', + filename='E:\数据库\bbs02.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='E:\数据库\bbs_log.ldf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) + +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) +) + +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--用户信息表 +insert into bbsUsers values ('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) + +select * from bbsSection--版块表 +insert into bbsSection values('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + +select *from bbsTopic--主贴表 +insert into bbsTopic values +(2,5,'范跑跑','谁是范跑跑','2008-7-8',1), +(3,2,'.NET','与JAVA的区别是什么','2008-9-1',2), +(1,4,'今年夏天流行什么','有谁知道今年夏天流行什么呀?','2008-9-10',0) + +select * from bbsReply--回帖表 +insert into bbsReply values +(1,3,'范跑跑','2008-10-2'), +(2,2,'区别不大','2008-10-2'), +(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.在主贴表中统计每个版块的发帖总数 +select tSID, COUNT(tSID) from bbsTopic group by tSID + +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID 主贴编号, COUNT(rID)回帖总数量 from bbsReply group by rTID + +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID 发帖人编号, COUNT(tSID)发帖总次数 from bbsTopic group by tUID + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select tUID 发帖人编号, SUM(tCount) 回复数量总和 from bbsTopic group by tUID + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +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 + +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic +select * from bbsTopic where tTitle like '%快乐' and tMsg like '%快乐' + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers +select * from bbsUsers where uAge>=15 and uAge<=20 and uPoint>=10 + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where uName like '小__' or uName like '__大' + +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select * from bbsTopic +select rTime 时间,tCount 回复数量,tTitle 标题,rMsg 内容 from bbsTopic where tCount>10 + +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID 发帖人编号,tCount 回复数量 from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/\344\275\234\344\270\2321.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\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..284d92677bbb8d0af19f26c038c52d56b2af07b2 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/\344\275\234\344\270\2321.sql" @@ -0,0 +1,86 @@ +create database cc +on +( + name='cc', + filename='F:\cc.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +log on +( + name='cc_log', + filename='F:\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 + +--1.查询所有订单订购的所有物品数量总和 +select sum(theNumber)所有物品数量总和 from orderItem +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select orderId 订单编号,sum(theNumber) 所有物品数量和,avg(theMoney)平均单价 from orderItem where orderId<3 group by orderId having 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(itemType)订购数量 from orderItem group by itemType order by itemType desc +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单 +select itemType 产品名称,sum(theNumber)订购总数量,avg(theMoney) 平均单价 from orderItem group by itemType having sum(theNumber)>=100 +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: +select itemName 产品名称,count(theNumber) 订购次数,sum(theNumber) 订购总数量 ,avg(theMoney) 平均单价 from orderItem group by itemName + + + +select * from orderItem +select * from orders \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/\344\275\234\344\270\2322.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\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..59d30c06cb47adfdd6c4b0c915e3fe427ed7f73f --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/\344\275\234\344\270\2322.sql" @@ -0,0 +1,140 @@ +-- 一、 +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 +( + uuID 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 primary key (uuID) +alter table bbsUsers add constraint UK unique(uName) +alter table bbsUsers add constraint CK check(uSex='男' or uSex='女' ) +alter table bbsUsers add constraint CK1 check(uAge>=15 or uAge<=60 ) +alter table bbsUsers add constraint CK2 check(upoint>=0) + +create table bbsSection +( + ssID int not null , + sName varchar(10) not null, + sUid int +) +alter table bbsSection add constraint PK_bbsSection_ssID primary key(ssID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key (sUid)references bbsUsers(uuID) + + + +create table bbsTopic +( + tID int primary key, + tUID int foreign key references bbsUsers(uuID), + tSID int foreign key references bbsSection(ssID), + tTime datetime , + tCount int +) +alter table bbsTopic add tTitle varchar(100) not null +alter table bbsTopic add tMsg text not null + + + +create table bbsReply +( + rID int primary key identity(1,1), + rUID int foreign key references bbsUsers(uuID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +select * from bbsReply +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女',20,0 union +select'逍遥','男',18,4 union +select'七年级生','男',19,2 +select * from bbsUsers + +--2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中, +--提示查询部分列:select 列名1,列名2 from 表名 +select uName,uPoint into bbsPoint from bbsUsers + +insert into bbsSection(sName, ssID) +select '技术交流' , 1union +select '读书世界' ,2 union +select '生活百科' ,3 union +select ' 八卦区' ,4 +select * from bbsSection + +-- 4.向主贴和回帖表中添加几条记录 +-- 发帖人 板块名 帖子标题 +-- 帖子内容 帖时间 回复数量 +insert into bbsTopic( tid,tUID,tSID,tTitle,tMsg,tTime,tCount ) +select 1,3, 4,'范跑跑','谁是范跑跑','2008-7-8',1 union +select 2,2, 1,' .NET','与JAVA的区别是什么呀?','2008-9-1',2 union +select 3,1, 3,' 今年夏天最流行什么','有谁知道今年夏天最流行','2008-9-10',0 +select * from bbsTopic + + +select * from bbsReply +insert into bbsReply(rTID ,rUID,rMsg,rTime) +select 1,2,'八嘎牙路是范跑跑','2008-7-8'union +select 2,1,'没有区别就是最大的区别','2008-7-8'union +select 3,3,'今年流行上海滩','2008-7-8' + +--6.因为小雨点发帖较多,将其积分增加10分 +update bbsUsers set uPoint=uPoint+10 where uName ='小雨点' + + +--在论坛数据库中完成以下题目 +--主贴表(bbsTopic) 回帖表(bbsReply)版块表(bbsSection) +--1.在主贴表中统计每个版块的发帖总数 +select * from bbsTopic +select tSID 板块编号 ,count(tCount)发帖总数 from bbsTopic group by tSID +--2.在回帖表中统计每个主贴的回帖总数量 +select * from bbsReply +select rTID 主贴编号,count(rID)回帖总数量 from bbsReply group by rTID +--3.在主贴表中统计每个用户的发的主帖的总数 +select * from bbsTopic +select tUID 用户,count(tSID ) 发帖总数 from bbsTopic group by tUID +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select tUID 用户,sum(tCount ) 回复数量总和 from bbsTopic group by tUID +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select tSID 版块 ,avg(tCount)平均 from bbsTopic group by tSID having avg(tCount)>3 +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select top 1 uName,uSex ,uAge ,uPoint from bbsUsers order by uPoint desc +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic +select * 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 in( select uAge 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 where uName like '小_点' +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来, +--并且为列取上对应的中文列名 +select tTitle 标题,tMsg 内容 from bbsTopic where tTime between '2008-9-10 12:00:00' and getdate() and tCount>10 +ss--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID 发帖人编号 ,tCount 回复数量 from bbsTopic where tTitle like'%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery8.1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery8.1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2a2852d12cb4f34f3fa617d15660f47f08e6a541 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery8.1.sql" @@ -0,0 +1,94 @@ +create database OrderForm +on +( + FileName='F:\homework\OrderForm.mdf', + Name='OrderForm', + size=5MB, + Maxsize=5MB, + Filegrowth=1MB +) +log on +( + FileName='F:\homework\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 sum(theNumber) from orderltem + +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select theNumber 物品的数量,orderID 订单编号,AVG(theMoney)平均单价 from orderltem group by theNumber,orderID having orderID<3 and AVG(theMoney)<10 + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select theNumber 物品数量,AVG(theMoney) 平均单价 from orderltem group by theNumber having AVG(theMoney)<10 and theNumber>50 + + +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 + +select itemType 产品,count(itemType) 订购数量 from orderltem group by itemType + + +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 + +select itemType,sum(theNumber) 订购数量,AVG(theMoney) 平均单价 from orderltem group by itemType having sum(theNumber)>100 + + + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: + +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 + + +select itemType 产品名称,count(itemType) 订购次数,sum(theNumber) 总数量,AVG(theMoney) 平均单价 from orderltem group by itemType + diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery8.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery8.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b878ad78f96ac1b4d824233bbfba73ef6b7a39f5 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery8.sql" @@ -0,0 +1,155 @@ +create database bbs +on +( + fileName='F:\homework\bbs.mdf', + Name='bbs', + size=5MB, + Maxsize=5MB, + filegrowth=1MB +) +log on +( + fileName='F:\homework\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 tSID 版块,count(tID) 数量 from bbsTopic group by tSID + +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID 主贴,count(rID) 回帖总数量 from bbsReply group by rTID + +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID 用户,count(tID) 主帖的总数 from bbsTopic group by tUID + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select tUID 用户,tID 主帖,sum(tCount) 回复数量总和 from bbsTopic group by tUID,tID + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select tSID 版块,tID 主贴,AVG(tCount) 平均回复数量 from bbsTopic group by tSID,tID having AVG(tCount)>3 + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select uName 用户名,uSex 性别,uAge 年龄,uPoint 积分 from bbsUsers where uPoint=(select MAX(uPoint) from bbsUsers) +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic where tMsg like '%快乐%' or tTitle like '%快乐%' + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +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 where uName like '小_大%' + +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select tTitle 标题,tMsg 内容 from bbsTopic where tTime between '2008-9-10 12:00:00' and getdate() and tCount>10 + +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tCount 回复数量,tUID 发帖人编号 from bbsTopic where tTitle like '%!' + + + + + + + + + + + + + + + + + + + + + + diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e7f8deef5472bdda696c745f04bbd3af0b6191eb --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery1.sql" @@ -0,0 +1,68 @@ +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 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 \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery6.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..20bca8d92fb2611181da44818fa8234d8d4af63c --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery6.sql" @@ -0,0 +1,233 @@ +--一、先创建数据库和表以 +-- 1.创建一个数据库用来存放某论坛的用户和发帖信息,数据库的名称为bbs,包含1个数据文件1个日志 +-- 文件,数据文件和日志文件全部存放在E盘中,初始大小,增长和最大大小自己设定 +use master + +go + +create database bbs +on +( + name='bbs', + filename='D:\TEXT\bbs.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='D:\TEXT\bbs_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +-- 2.创建表 + +-- 注意以下4张表的创建顺序,要求在创建bbsUser和bbsSection表时不添加约束,创建完后单独添加约束,其它的表创建时添加约束 + +-- 用户信息表(bbsUsers) +-- 用户编号 UID int 主键 标识列 +-- 用户名 uName varchar(10) 唯一约束 不能为空 +-- 性别 uSex varchar(2) 不能为空 只能是男或女 +-- 年龄 uAge int 不能为空 范围15-60 +-- 积分 uPoint int 不能为空 范围 >= 0 +go +use bbs + +go + +create table bbsUsers +( + UID int identity, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) + +-- 版块表(bbsSection) +-- 版块编号 sID int 标识列 主键 +-- 版块名称 sName varchar(10) 不能为空 +-- 版主编号 sUid int 外键 引用用户信息表的用户编号 +go +use bbs + +go + +create table bbsSection +( + sID int identity, + sName varchar(10) not null, + sUid int constraint FK_bbsUsers_UID references bbsUsers(UID) +) + + +--+ 主贴表(bbsTopic) +-- 主贴编号 tID int 主键 标识列, +-- 发帖人编号 tUID int 外键 引用用户信息表的用户编号 +-- 版块编号 tSID int 外键 引用版块表的版块编号 (标明该贴子属于哪个版块) +-- 贴子的标题 tTitle varchar(100) 不能为空 +-- 帖子的内容 tMsg text 不能为空 +-- 发帖时间 tTime datetime +-- 回复数量 tCount int + +go +use bbs + +go + +create table bbsTopic +( + tID int primary key identity, + tUID int references bbsUsers(UID), + tSID int references bbsSection(sID), + tTitle varchar(100), + tMsg text not null, + tTime datetime, + tCount int +) +--+ 回帖表(bbsReply) +-- 回贴编号 rID int 主键 标识列, +-- 回帖人编号 rUID int 外键 引用用户信息表的用户编号 +-- 对应主贴编号 rTID int 外键 引用主贴表的主贴编号 (标明该贴子属于哪个主贴) +-- 回帖的内容 rMsg text 不能为空 +-- 回帖时间 rTime datetime + +go +use bbs + +go + +create table bbsReply +( + rID int primary key identity, + rUID int references bbsUsers(UID), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + + + +-- 用户编号 UID int 主键 标识列 +-- 用户名 uName varchar(10) 唯一约束 不能为空 +-- 性别 uSex varchar(2) 不能为空 只能是男或女 +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 DK_bbsUsers_uSex default('男') for uSex + + +--- 版块编号 sID int 标识列 主键 + +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) + + +--二、在上面的数据库、表的基础上完成下列题目: + +-- 1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +-- 小雨点 女 20 0 +-- 逍遥 男 18 4 +-- 七年级生 男 19 2 +insert into bbsUsers(uName,uSex,uAge,uPoint) values ('小雨点','女','20','0'),('逍遥','男','18','4'),('七年级生','男','19','2') +-- 2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中, + --提示查询部分列:select 列名1,列名2 from 表名 +create table bbsPoint +( + uName varchar(10) not null, + uPoint int not null +) +select uName uPoint from bbsPoint + +-- 3.给论坛开设4个板块 版块表(bbsSection)版块名称 sName 版主编号 sUid +-- 名称 版主名 +-- 技术交流 小雨点 +-- 读书世界 七年级生 +-- 生活百科 小雨点 +-- 八卦区 七年级生 + +insert into bbsSection(sName,sUid) values('技术交流','1'),('读书世界','3'),('生活百科','1'), +('八卦区','七年级生') + +-- 4.向主贴和回帖表中添加几条记录 + +-- 主贴: + +-- 发帖人 板块名 帖子标题 帖子内容 发帖时间 回复数量 +-- 逍遥 八卦区 范跑跑 谁是范跑跑 2008-7-8 1 +-- 七年级生 技术交流 .NET 与JAVA的区别是什么呀? 2008-9-1 2 +-- 小雨点 生活百科 今年夏天最流行什么 有谁知道今年夏天最流行 2008-9-10 0 +-- + +select * from bbsUsers +select * from bbsSection + + +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values('2','4','范跑跑','谁是范跑跑','2008-7-8','1'), +('3','2','.NET','与Java的区别是什么呀?','2008-9-1','2'),('4','4','今年夏天最流行','有谁知道今年夏天最流行','2008-9-10','0') + + +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 + +-- 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) + +delete from bbsTopic where tUID='2' +delete from bbsUsers where uName='逍遥' + +-- 6.因为小雨点发帖较多,将其积分增加10分 + +update bbsUsers set uPoint=12 where uName='小雨点' + +-- 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) +delete from bbsTopic where tSID=4 +-- 8.因回帖积累太多,现需要将所有的回帖删除 +delete from bbsReply + + +--在论坛数据库中完成以下题目 + +--1.在主贴表中统计每个版块的发帖总数 + +select SUM(tSID)发帖总数 from bbsTopic + +--2.在回帖表中统计每个主贴的回帖总数量 + +select rTID 主贴,sum(rID)回帖总数量 from bbsReply + +--3.在主贴表中统计每个用户的发的主帖的总数 + +select sum(tID)主帖总数,tUID 用户 from bbsTopic + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 + +select sum(tCount)回复数量总和,tUID 用户 from bbsTopic + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 + +select tSID 版块,tID 主贴,sum(tCount)回复数量 from bbsTopic group by tSID,tID having sum(tCount)>3 + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 + +select uName 用户名,uPoint 积分,uAge 年龄,uSex 性别 from bbsUsers where uPoint = (select max(uPoint) from bbsUsers) + +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 + +select tMsg 内容 from bbsTopic where tMsg = '快乐' + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) + +select uAge 年龄,uPoint 积分 from bbsUsers where uAge = (select uAge >= 15 and uAge <= 20 from bbsUsers) and uPoint > 10 + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 + +select * from bbsUsers where uName like "小_大" + +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 + +select tTime 时间,tCount 回复数量,tMsg 标题 from bbsTopic where DATEDIFF(HH, 2008-9-10 12:00:00,tTime)<0 and >10 + +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 + +select tUID,tCount from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery\347\273\203\344\271\2401.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery\347\273\203\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..edb18dbfa24dcf324cfa9980a8614bd5f122dfcf --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery\347\273\203\344\271\2401.sql" @@ -0,0 +1,150 @@ +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 +) + +--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 tSID as 板块编号 , count( tID ) 发帖数量 from bbsTopic group by tSID + + +--2.在回帖表中统计每个主贴的回帖总数量 + select rTID as 主贴号, count( rID) 回帖数量 from bbsReply group by rTID + + +--3.在主贴表中统计每个用户的发的主帖的总数 + select tUID as 用户号 ,count( tSID) 对应的帖数 from bbsTopic group by tUID + + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 + select tUID as 用户号 ,sum(tCount) 回帖数量 from bbsTopic group by tUID,tCount + + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 + select count( tID) 每个板块的主贴编号总数, sum(tCount) 每个板块回复的总数 , sum(tCount)/count( tID) 每个板块的平均回复数量 from bbsTopic group by tSID having (sum(tCount)/count( tID))>3 + + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 + select top 1 用户名=uName, 性别 = uSex, 年龄= uAge, 积分=uPoint from bbsUsers order by uPoint desc + select 用户名=uName, 性别 = uSex, 年龄= uAge, 积分=uPoint from bbsUsers where uPoint=(select max(uPoint) from bbsUsers) +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 + select*from bbsTopic where tTitle='%快乐%' or tMsg='%快乐%' + + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) + select*from bbsUsers where uAge between 15 and 20 and uPoint>10 + select*from bbsUsers where uPoint>10 and ( uAge like '1[5-9]%' and uAge=20) + select uName ,sum(uPoint) from bbsUsers where uAge between 15 and 30 group by uName having sum(upoint)>10 + + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 + select*from bbsUsers where uName like '小_大%' + + +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 + select 贴子的标题 = tTitle,帖子的内容 = tMsg from bbsTopic where tCount>10 + -- rTime >'2008'and rTime >'9'and rTime >'10'and rTime >'12' + + +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select 发帖人编号= tUID, 回复数量=tCount from bbsTopic where tTitle like '&!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery\347\273\203\344\271\2402.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery\347\273\203\344\271\2402.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bd7aaf99107f6dc2b28570d9deaa39b0db7168d4 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery\347\273\203\344\271\2402.sql" @@ -0,0 +1,71 @@ +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) + + +--1.查询所有订单订购的所有物品数量总和 + select sum(theNumber) from orderItem + +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 + select orderId , sum(theNumber)总数量 , avg(theMoney) 平均单价 from orderItem where orderId<=3 group by orderId + + + +--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(itemName) 产品数量 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(itemName)订购次数, sum(theNumber)总数量, avg(theMoney)平均价格 from orderItem group by itemName \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..529f378d67bd9d2d080701918782782c8aa91ab5 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" @@ -0,0 +1,70 @@ +create database Students +on +( +name='Students_data', +filename='C:\app\Students_data.mdf', +size=5mb, +maxsize=100mb, +filegrowth=10% +) +log on +( +name='Students_log', +filename='C:\app\Student_log.ldf', +size=1mb, +filegrowth=1mb +) +go +USE Students +go +create table orders +( +orderld int not null primary key, +orderDate datetime default(getdate()), +) +go +create table orderltem +( +ltemiD int not null primary key, +orderId varchar(9)not null, +itemType varchar(10), +itemName char(10), +theNumber int, +theMoney int +) +INSERT orderltem(ltemiD,orderId,itemType,itemName,theNumber,theMoney) +SELECT 1,'文具','笔',72,2 union +SELECT 1,'文具','尺',10,1 union +SELECT 1,'体育用品','篮球',1,56 union +SELECT 1,'文具','笔',36,2 union +SELECT 1,'文具','固体胶',20,3 union +SELECT 1,'日常用品','透明胶',2,1 union +SELECT 1,'体育用品','羽毛球',20,3 union +SELECT 1,'文具','订书机',20,3 union +SELECT 1,'文具','订书机',10,3 union +SELECT 1,'文具','裁纸刀',5,5 union +SELECT 1,'文具','笔',20,2 union +SELECT 1,'文具','信纸',50,1 union +SELECT 1,'日常用品','毛巾',4,5 union +SELECT 1,'日常用品','透明胶',30,1 union +SELECT 1,'体育用品','羽毛球',20,3 +SELECT*FROM orderltem +--1.查询所有订单订购的所有物品数量总和 +select sum(theNumber) from orderltem +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +sele orderld ,sum(theNumber),avg(theMoney) from orderItem where orderId<3 group by orderId having avg(theMoney)<10 + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select orderld ,sum(theNumber),avg (theMoney) from orderltem group by orderId having avg(theMoney)<10 and sun(theNumber)>50 +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select itemType , count(itemType)订购数量 from orderItem group by itemType order by itemType desc +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select itemType 产品名称,sum(theNumber)订购总数量,avg(theMoney) 平均单价 from orderItem group by itemType having sum(theNumber)>=100 +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: +select itemName 产品名称,count(theNumber) 订购次数,sum(theNumber) 订购总数量 ,avg(theMoney) 平均单价 from orderItem group by itemName + +select * from orderItem +select * from orders \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..071657148106b550a8bafa484a8c90e459216e60 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" @@ -0,0 +1,139 @@ +create database bbs +on +( + name='sa', + filename='C:\app\sa.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='sa_log', + filename='C:\app\Sa_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use sa +go + +create table bbsUsers +( + uuID 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 primary key (uuID) +alter table bbsUsers add constraint UK unique(uName) +alter table bbsUsers add constraint CK check(uSex='男' or uSex='女' ) +alter table bbsUsers add constraint CK1 check(uAge>=15 or uAge<=60 ) +alter table bbsUsers add constraint CK2 check(upoint>=0) + +create table bbsSection +( + ssID int not null , + sName varchar(10) not null, + sUid int +) +alter table bbsSection add constraint PK_bbsSection_ssID primary key(ssID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key (sUid)references bbsUsers(uuID) + + + +create table bbsTopic +( + tID int primary key, + tUID int foreign key references bbsUsers(uuID), + tSID int foreign key references bbsSection(ssID), + tTime datetime , + tCount int +) +alter table bbsTopic add tTitle varchar(100) not null +alter table bbsTopic add tMsg text not null + + + +create table bbsReply +( + rID int primary key identity(1,1), + rUID int foreign key references bbsUsers(uuID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + +select * from bbsReply +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女',20,0 union +select'逍遥','男',18,4 union +select'七年级生','男',19,2 +select * from bbsUsers + +--2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中, +--提示查询部分列:select 列名1,列名2 from 表名 +select uName,uPoint into bbsPoint from bbsUsers + +insert into bbsSection(sName, ssID) +select '技术交流' , 1union +select '读书世界' ,2 union +select '生活百科' ,3 union +select ' 八卦区' ,4 +select * from bbsSection + +-- 4.向主贴和回帖表中添加几条记录 +-- 发帖人 板块名 帖子标题 +-- 帖子内容 帖时间 回复数量 +insert into bbsTopic( tid,tUID,tSID,tTitle,tMsg,tTime,tCount ) +select 1,3, 4,'范跑跑','谁是范跑跑','2008-7-8',1 union +select 2,2, 1,' .NET','与JAVA的区别是什么呀?','2008-9-1',2 union +select 3,1, 3,' 今年夏天最流行什么','有谁知道今年夏天最流行','2008-9-10',0 +select * from bbsTopic + + +select * from bbsReply +insert into bbsReply(rTID ,rUID,rMsg,rTime) +select 1,2,'八嘎牙路是范跑跑','2008-7-8'union +select 2,1,'没有区别就是最大的区别','2008-7-8'union +select 3,3,'今年流行上海滩','2008-7-8' + +--6.因为小雨点发帖较多,将其积分增加10分 +update bbsUsers set uPoint=uPoint+10 where uName ='小雨点' + + +--在论坛数据库中完成以下题目 +--主贴表(bbsTopic) 回帖表(bbsReply)版块表(bbsSection) +--1.在主贴表中统计每个版块的发帖总数 +select * from bbsTopic +select tSID 板块编号 ,count(tCount)发帖总数 from bbsTopic group by tSID +--2.在回帖表中统计每个主贴的回帖总数量 +select * from bbsReply +select rTID 主贴编号,count(rID)回帖总数量 from bbsReply group by rTID +--3.在主贴表中统计每个用户的发的主帖的总数 +select * from bbsTopic +select tUID 用户,count(tSID ) 发帖总数 from bbsTopic group by tUID +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select tUID 用户,sum(tCount ) 回复数量总和 from bbsTopic group by tUID +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select tSID 版块 ,avg(tCount)平均 from bbsTopic group by tSID having avg(tCount)>3 +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select top 1 uName,uSex ,uAge ,uPoint from bbsUsers order by uPoint desc +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic +select * 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 in( select uAge 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 where uName like '小_点' +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来, +--并且为列取上对应的中文列名 +select tTitle 标题,tMsg 内容 from bbsTopic where tTime between '2008-9-10 12:00:00' and getdate() and tCount>10 +ss--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID 发帖人编号 ,tCount 回复数量 from bbsTopic where tTitle like'%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bcecafa67fa1e6b96865188eda171a723ce0063f --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" @@ -0,0 +1,98 @@ +use master +go +create database bbs +on( + name='bbs', + filename='D:\ljc\bbs.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on( + name='bbs_log', + filename='D:\ljc\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 , +) +go +use bbs +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.在主贴表中统计每个版块的发帖总数 +select tSID,COUNT(*) from bbsTopic group by tSID +--2.在回帖表中统计每个主贴的回帖总数量 +select tCount,COUNT(*) from bbsReply group by tCount +--3.在主贴表中统计每个用户的发的主帖的总数 +select count(*)主帖的总数 from bbsTopic group by tUID +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select sum(tCount) from bbsTopic group by tCount +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select avg(tCount) from bbsTopic group by tCount having avg(tCount)>3 +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select top 1 max(uPoint)积分,uName,uSex,uAge from bbsUsers group by uPoint,uName,uSex,uAge +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select tMsg,tTitle from bbsTopic where tTitle like '%快乐%' or tMsg like '%快乐%' +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +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 where uName ='小_大' +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select tTitle 标题,tMsg 内容 from bbsTopic where tTime between '2008-9-10 12:00:00' and getdate() and tCount>10 +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tTitle,tUID,tCount from bbsTopic where tTitle like'%!' group by tTitle,tUID,tCount + diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fe45ce4f7ea1c546005283873a528c3af9c01d1b --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery2.sql" @@ -0,0 +1,68 @@ +--先创建如图所示表 +use master +go +--订单表(orders)列为:订单编号(orderId 主键) 订购日期(orderDate) +create table orders +( + orderId int primary key identity, + 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' + +--订购项目表(orderItem),列为: +--项目编号(ItemiD)订单编号(orderId)产品类别(itemType) +--产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) +create table orderItem +( + ItemiD int primary key identity(1,1), + orderId int references orders(orderId), + itemType nvarchar(10), + itemName nvarchar(10), + theNumber int, + theMoney int +) +insert into orderItem +select 1,'文具','笔',72,2 union +select 1,'文具','尺',10,1 union +select 1,'体育用品','篮球',1,36 union +select 2,'文具','笔',1,56 union +select 2,'文具','固体胶',36,2 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 + +--1.查询所有订单订购的所有物品数量总和 +select sum(theNumber)数量总和 from orderItem + +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select orderId 编号,sum(theNumber)数量总和,avg(theMoney)平均单价 from orderItem where orderId<3 group by orderId,theNumber,theMoney having AVG(theMoney)<10 + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select sum(theNumber)数量总和, avg(theMoney)平均单价 from orderItem group by theMoney having AVG(theMoney)<10 and sum(theNumber)>50 + +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select itemType 产品名称,count(itemType)次数 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 itemType 产品名称,count(itemType)订购次数,sum(theNumber)总数量,AVG(theMoney)平均单价 from orderItem group by itemType \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..86b17206dc0552e3a201339d7a951819209ed4b1 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" @@ -0,0 +1,65 @@ +use master +go +create database orde +go +use orde +go +create table orders +( + orderId int primary key identity(1,1), + orderDate datetime , +) +create table orderItem +( + ItemiD int identity(1,1), + orderId int , + itemType char(20), + itemName char(20), + theNumber 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 ) + select *from orderItem + select *from orders + + +-- 1.查询所有订单订购的所有物品数量总和 + select sum(theNumber) from orderItem +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 + select orderId ,sum(theNumber) 数量, avg(theMoney) 平均价格 from orderItem where orderId<3 group by orderId having 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(itemType) 订购次数 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(itemName) 订购次数 ,sum(theNumber) 数量,avg(theMoney) 平均价格 from orderItem group by itemName \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..69c15a4009643bff6d2bb720582d8db7eec338d1 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" @@ -0,0 +1,169 @@ +use master +go +create database bbs +on +( name='bbs', + filename='D:\bbs.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='bbs_log', + filename='D:\bbs_log.ldf', + size=5mb, + maxsize=5000mb, + 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 +) +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_uPoint 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) + +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) +alter table bbsSection add constraint Fk_bbsSection_sUid foreign key(sUid) references bbsUsers(UID) +-- 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,2,'谁问的谁就是范跑跑','20210316'), + (1,3,'这个更简单','20210316'), + (1,3,'今年夏天最流行内裤外穿','20210316') +select*from bbsReply + +-- 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) +select*from bbsUsers +select*from bbsSection --sUid +select*from bbsTopic --tUID +delete from bbsTopic where tUID=2 +select*from bbsReply --rUID + +delete from bbsUsers where uName='逍遥' + +-- 6.因为小雨点发帖较多,将其积分增加10分 + select uPoint from bbsUsers where uName='小雨点' + update bbsUsers set uPoint=12 where uName='小雨点' + +-- 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) + select*from bbsTopic + alter table bbsReply drop constraint FK__bbsReply__rTID__35BCFE0A + delete from bbsTopic where tSID=3 + + + + + + + + + + + + +-------------------------------------------------------------------------------------- + + + + + +--1.在主贴表中统计每个版块的发帖总数 +select * from bbsTopic +select tSID 板块, count(tCount) 发帖总数 from bbsTopic group by tSID +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID 主贴 ,count (rID) 回帖总数 from bbsReply group by rTID +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID 用户,count(tSID ) 发帖总数 from bbsTopic group by tUID + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select tUID 用户, sum(tCount)回复数量总和 from bbsTopic group by tUID + + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select avg(tCount )平均数量 ,tSID 主贴 from bbsTopic group by tSID having avg(tCount )>3 + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select top 1 uPoint, uName,uSex,uAge from bbsUsers order by uPoint desc + +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select *from bbsTopic +select tTitle,tMsg from bbsTopic where tTitle like '%快乐%'or tMsg like '%快乐%' + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select uAge ,uPoint from bbsUsers where uAge>15and uAge<20 and uPoint>10 + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select uName from bbsUsers where uName like '小%点' +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select tTime 时间, tMsg 内容,tCount 回复数量,tTitle 标题 from bbsTopic where tCount>10 +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID 编号 ,tCount 数量 from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2deb19994db4bf4e5d2253611f0eb597e2c777c9 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery1.sql" @@ -0,0 +1,60 @@ +use master +go +create database hr +on +( +name='hr', +filename='D:\test\hr.mdf', +size=6mb, +maxsize=2000mb, +filegrowth=1mb +) +log on +( +name='hr', +filename='D:\test\hr_log.ldf', +size=6mb, +maxsize=2000mb, +filegrowth=1mb +) +go +use hr +go +create table orders +( +orderld int primary key, +drderDate nvarchar(20), +) +create table orderItem +( +ltemiD int, +orderld int, +itemType nvarchar(6), +itemName nvarchar(6), +theNumber int, +theMoney int, +) +insert into orderItem +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,4,'体育用品','羽毛球',20,3 + +select * from orderItem +select SUM(theNumber) 物品数量总和 from orderItem +select orderld,sum(theNumber) 物品数量总和,avg(theMoney) 平均单价 from orderItem where orderld<3 group by orderld having avg(theMoney)<10 +select orderld,SUM(theNumber) 物品数量总和,avg(theMoney) 平均单价 from orderItem group by orderld having avg(theMoney)<10 and SUM(theNumber)>50 +select itemType,count(itemName)次数 from orderItem group by itemType +select itemType,SUM(theNumber) 物品数量总和,avg(theMoney) 平均单价 from orderItem group by itemType having SUM(theNumber)>100 +select itemName,count(itemName) 次数,SUM(theNumber) 总数量,avg(theMoney) 平均单价 from orderItem group by itemName \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1912569d22b4fe03c85da9edac36c1ee8200f034 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" @@ -0,0 +1,99 @@ +use master +go + +create database bbs1 +on +( +name='bbs11', +filename='D:\testbbs1.mdf', +size=5mb, +maxsize=500mb, +filegrowth=1mb +) +log on +( +name='bbs1', +filename='D:\testbbs1_log.ldf', +size=5mb, +maxsize=500mb, +filegrowth=1mb +) +create table bbs1users +( +uid int primary key identity(1,1), +uname varchar(10) unique not null, +usex varchar(2) not null check(usex in('男','女')), +uage int not null check(uage>=15 and uage<=60), +upoint int not null check(upoint>=0) +) +create table bbs1section +( +sid int primary key identity(1,1), +sname varchar(10) not null, +suid int foreign key references bbsusers(uid) +) +create table bbs1topic +( +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 not null, +tcount int +) +create table bbs1reply +( +rld int primary key identity(1,1), +ruld int foreign key references bbsusers(uid), +rtld int foreign key references bbssection(sid), +rmsg text not null, +rtime datetime +) +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) +select * from bbsusers +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) +select * from bbsusers +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(ruld,rmsg,rtime) values(1,'我就是','2008-7-8') +insert into bbsreply(ruld,rmsg,rtime) values(6,'哦flak积分','2008-7-8') +insert into bbsreply(ruld,rmsg,rtime) values(1,'爱科技','2008-7-8') +select * from bbsUsers +delete bbsusers where uname ='逍遥' +alter table bbssection drop NK +alter table bbstopic drop FK__bbstopic__tuid__5315624 +update bbsusers set upoint ='10' where uname='小雨点' + +select * from bbssection +delete bbssection where sname='生活百科' + +select * from bbsreply +delete bbsreply +select tsid,count(*) 发帖总数 from bbs1topic group by tsid +select rtid,count(*) 回帖总数量 from bbs1reply group by rtid +select tuid,count(*) 主贴的总数 from bbstopic group by tuid +select tuid,sum(tcount) 回复数量和 from bbstopic group by tsid having avg(tcount)>3 +select top 1 uname '用户名',uSex '性别',uAge '年龄',upoint '积分' from bbsUsers order by uPoint desc +select tTitle,rmsg from bbsTopic where tTitle like '%快乐%' or rmsg like '%快乐%' +select * from bbsusers where uage>=15 and uage<=20 and upoint>10 +select * from bbsusers where uage between 15 and 20 and upoint>10 +select * from bbsusers where uid =1 +select * from bbsusers where uname like '小_大' +select tid '主贴编号',tuid '发帖人编号',tsid '版块编号',ttitle '贴子的标题',rmsg '帖子的内容',rtime '发帖时间',tcount '回复数量' from bbsTopic where rtime>'2008-9-10 12:00:00' and tCount>10 +select tuid '发帖人编号',tcount '回复数量' from bbsTopic where tTitle like '%!' + diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6496aac3d4ee55c4e3821cf378253bf5aa9778a0 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" @@ -0,0 +1,77 @@ +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), + oderId int foreign key references orders(orderId), + itemType nvarchar(20), + itemName nvarchar(20), + theNumer int, + theMoney varchar(20), +) +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 oderId having oderId<3 and avg(theMoney)<10 +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select sum(theNumer) 所有物品的数量,avg(theMoney) 平均单价 from orderItem group by oderId 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 diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..72effc0f5cfa2a28dcf1cc345aa29db71bedee1b --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" @@ -0,0 +1,125 @@ +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 text not null, + tMsg text 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 text 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=uPoint+10 where uName='小雨点' +alter table bbsTopic drop FK_bbsTopic_tSID +delete bbsSection where sName='生活百科' + +delete bbsReply + +--在论坛数据库中完成以下题目 + +--1.在主贴表中统计每个版块的发帖总数 +select distinct tSID,count(tSID) 发帖总数 from bbsTopic group by tSID +--2.在回帖表中统计每个主贴的回帖总数量 +select distinct rTID,count(rID) 回帖总数量 from bbsReply group by rTID +--3.在主贴表中统计每个用户的发的主帖的总数 +select distinct tUID,count(tUID) 主贴总数 from bbsTopic group by tUID +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select distinct tUID, sum(tCount) from bbsTopic group by tUID +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select distinct tSID 板块,avg(tCount) 平均回复数量 from bbsTopic group by tSID having avg(tCount)>3 +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select top 1* from bbsUsers order by uPoint DESC --不考虑并列第一 +select * from bbsUsers where uPoint=(select max(uPoint) from bbsUsers) +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select tTitle,tMsg from bbsTopic where tTitle like'%快乐%' or tMsg like '%快乐%' +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select uName 优秀用户 from bbsUsers where uAge>=15 and uAge<=20 and uPoint>10 +select uName 优秀用户 from bbsUsers where (uAge between 15 and 20)and uPoint>10 +select uName 优秀用户 from bbsUsers where uAge in (select uAge from bbsUsers where uAge>=15 and uAge<=20) and uPoint in(select uPoint from bbsUsers where uPoint>10) +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where uName like '小_大%' +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select tTitle 标题,tMsg 内容 from bbsTopic where tTime>'2008-9-10 12:00:00' and tCount>10 +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID 发帖人编号,tCount 回复数量 from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..560f9759fbe0e1112b2f0e0c10542512175aa936 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" @@ -0,0 +1,105 @@ +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 not null primary key identity(1,1),--订单编号 + orderDate varchar(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 + + --1查询所有订单订购的所有物品数量总和 + + select sum(theNumber) from orderItem + + --2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 + +select orderId 订单,itemName 数量,sum(theNumber)数量和,avg(theMoney)平均单价 from orderItem where orderId<3 group by orderId,itemName having avg (theMoney)<10 + +--查询平均单价小于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 count(itemType)订购次数,itemType 商品名称 from orderItem group by itemType + +--.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select orderId 订单, itemType 类别产品,sum(theNumber) 数量和,avg(theMoney)平均单价 from orderItem group by orderId,itemType having sum(theNumber)>100 + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: + + --产品名称 订购次数 总数量 平均单价 + -- 笔 3 120 2 + select itemName 商品名称,count(theNumber)订购次数,sum(theNumber)总数量,avg(theMoney) 平均单价 from orderItem group by itemName diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery2sql.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery2sql.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2745df4ef48ac43a876c11834dab0cd5f1093c3e --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery2sql.sql" @@ -0,0 +1,139 @@ + +create database bbs +on +( + +name =bbs, +filename='D:\bbs.mdf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) + + + +log on + +( + + name='bbs_log', + + filename='D:\bbs_log.ldf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) +go +use bbs +go +--用户信息表 +create table bbsusers +( +uid int identity(1,1) not null , +unaem varchar(10), +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 UN_ unique (unaem) +alter table bbsusers add constraint CK_ check(usex='男'or usex='女') +alter table bbsusers add constraint CK_ check(uage>=15 or uage<=60) +--板块表 +create table bbssection +(sid int identity(1,1), +snaeme varchar(10)not null , +suid int +) +alter table bbssection add constraint PK_sid primary key (sid) +alter table bbssection add constraint FK foreign key (suid) references bbsusers(uid) + + +--主贴表 +create table bbstopic +( +tid int not null 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 +) +insert into bbstopic(tuid,tsid,ttitle,tmsg ,ttime ,tcount ) values(2, 0,'马巧晶','谁是马巧晶',2008-7-8,1), + (3, 4,'.NET ',' 与JAVA的区别是什么呀? ','2008-9-1 ','2'), + (5, 6,'今年夏天最流行什么 ',' 有谁知道今年夏天最流行','2008-9-10','0') + + +--回帖表 +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 bbsReply(rUID,rTID ,rMsg,rTime) values(1,2,'二班长的最丑的就是范跑跑',0123), + (4,3,'我挂科的不知道这么深奥的问题',0123), + (5,6,'这个夏天最流行的当然是黑丝啊破洞的那种',0123) + + +insert into bbsusers(unaem,usex,uage,upoint) values('杜海彪','女',0,0) +insert into bbsusers(unaem,usex,uage,upoint) values('马巧晶','女',18,4) +insert into bbsusers(unaem,usex,uage,upoint) values('大一新生','男',19,2) + +--备份 + +select unaem ,upoint into bbspoint from bbsusers + +--开设 +insert into bbssection(snaeme ,suid) values('技术交流','0'),( '读书世界', '0'),('生活百科' , '0'),('八卦区' ,'0') + + delete bbsusers where unaem ='杜海彪' + --增加积分 + update bbsusers set upoint='10' where unaem='马巧晶' + select * from bbssection + --1.在主贴表中统计每个版块的发帖总数 +select tsid, count(*)from bbstopic + +--在回帖表中统计每个主贴的回帖总数量 +select rid ,count(* ) from bbsreply + +--在主贴表中统计每个用户的发的主帖的总数 +select tuid, count(*) from bbstopic + +--在主贴表中统计每个用户发的主贴的回复数量总和 + +select sum( tcount) from bbstopic +--在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 + +select tid ,avg(tcount) from bbstopic group by tid having avg(tcount)>3 + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 + select max(upoint),usex,unaem,uage from bbsusers + +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select ttitle ,tmsg from bbsTopic where ttitle like '快乐'and tmsg like '快乐' + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers where uage between 15 and 20 and upoint>10 +select * from bbsUsers where uage>15 and uage <20 + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where unaem like '小_大' + +--在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 + +select tCount 回复数量 ,tmsg 内容 , tTime 时间 from bbsTopic where tCount>10 and tTime >'2008-9-10 12:00:00' +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tuid,tcount from bbsTopic where ttitle like '%!' diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f12a44686ca2d6d6406e9cd4fced3a2bd3b426f7 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" @@ -0,0 +1,80 @@ +use master +go + +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 sum(theNumber) 所有物品数量总和 from orderItem + +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select sum(theNumber) 订单总数 ,avg(theMoney)平均单价,orderId from orderItem where orderId<3 group by orderId having avg(theMoney)<10 + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select sum(theNumber) 订单总数 , avg(theMoney) 平均单价 from orderItem group by orderId having sum(theNumber)>50 and avg(theMoney)<10 + +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select itemType 类别 , count(itemType) 订购次数 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(theNumber) 订购次数, sum(theNumber)总数量, avg(theMoney)平均单价 from orderItem group by itemName diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery5.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..13d80f21d32d1cf878ee48d079ad8583618bfcc6 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery5.sql" @@ -0,0 +1,131 @@ +use master +go + +create database bbs +on +( + name = 'bbs', + filename = 'D:\数据库\bbs.mdf' +) + +log on +( + name = 'bbsUser_log', + filename = 'D:\数据库\bbs_log.ldf' +) + +use bbs + +create table bbsUsers +( + UID int identity 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_s primary key(UID) +alter table bbsUsers add constraint UK_b unique(uName) +alter table bbsUsers add constraint CK_b check(uSex in ('男','女')) +alter table bbsUsers add constraint CK_a check( uAge>=15 and uAge<=60) +alter table bbsUsers add constraint CK_c check( uPoint>=0) + +create table bbsTopic +( + tID int identity, + tUID int , + tSID int , + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) + +alter table bbsTopic add constraint FK_S foreign key(tUID) references bbsUsers(UID) + +create table bbsReply +( + rID int identity, + rUID int, + rTID int, + rMsg text not null, + rTime datetime +) +alter table bbsReply add constraint PK_z primary key(rID) +alter table bbsReply add constraint FK_d foreign key(rUID) references bbsUsers(UID) + +create table bbsSection +( + sID int identity, + sName varchar(10) not null, + sUid int, +) +alter table bbsTopic add constraint FK_l foreign key(tSID) references bbsSection(sID) +alter table bbsSection add constraint PK_q primary key(sID) +alter table bbsSection add constraint PK_p foreign key(sUid) references bbsUsers(UID) + +insert into bbsUsers (uName,uSex,uAge,uPoint) +select '小雨点','女',20 ,0 union +select '逍遥','男',18 ,4 union +select '七年级生','男',19 ,2 + +select uName,uPoint into bbsPoint from bbsUsers +select * from bbsPoint + +select * from bbsUsers +select * from bbsSection + +insert into bbsSection (sName, sUid)values('技术交流',3),( '读书世界',1),( '生活百科',3),('八卦区',1) + +select * from bbsSection +select * from bbsTopic + +insert into bbsTopic (tUID,tSID,tTitle,tMsg,tTime,tCount)values +(2,4,'范跑跑','谁是范跑跑','2008-7-8',1), +(1,1,'.NET ','与JAVA的区别是什么呀?','2008-9-1','2'), +(3,3,'今年夏天最流行什么','今年夏天最流行什么','2008-9-10',0) +select * from bbsTopic + +insert into bbsReply (rUID,rTID,rMsg,rTime)values +(1,4,'范跑跑是一位老师','2008-07-09'), +(2,1,'.NET与JAVA区别很大','2008-10-09'), +(2,1,'.JAVA是一门编程语言','2008-10-09') +select * from bbsReply + + +--在论坛数据库中完成以下题目 + +--1.在主贴表中统计每个版块的发帖总数 +select count(tTime)发帖总数,tSID 板块编号 from bbsTopic group by tSID + +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID ,count(rUID)回帖数量 from bbsReply group by rTID + +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID, count(tUID) 用户发帖的总数 from bbsTopic group by tUID + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select sum(tCount) 回帖数量总和 from bbsTopic group by tUID + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select tSID 板块编号,avg(tCount)回复数量 from bbsTopic group by tSID having avg(tCount)>3 + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select top 1 uName 姓名,uSex 性别, uAge 年龄, uPoint 积分 from bbsUsers order by uPoint desc +select max(uPoint)积分最高 from bbsUsers +select uName 姓名,uSex 性别, uAge 年龄, uPoint 积分 from bbsUsers where Upoint in(select max(uPoint) from bbsUsers) + +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic where tTitle like '%快乐%' or tMsg like '%快乐%' + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers where uAge in( select uAge from bbsUsers where uAge between 15 and 20 and uPoint>=10) + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where uName like '小_点' +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select * from bbsTopic +select tTime 时间,tCount 回复数量 ,tTitle 标题 ,tMsg 内容 from bbsTopic where tCount>10 +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID 发帖人编号 ,tCount 回复数量 from bbsTopic where tTitle like'%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..92d3c163bf6dbb8aa16140f0d6bd06a03d898cb4 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" @@ -0,0 +1,75 @@ +create database shop +on +( + name='shop', + filename='D:\SQL\shop.mdf' +) +log on +( + name='shop_log', + filename='D:\SQL\shop_log.ldf' +) +go + +use shop +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') +select * from orders +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,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 sum(theNumber) >50 and avg(theMoney)<10 +--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 diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..620407cd599a3af2781f38041d293e9635ed73da --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery2.sql" @@ -0,0 +1,145 @@ +create database bbs +on +( + name='bbs', + filename='D:\SQL\bbs.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='D:\SQL\bbs_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use bbs +go + +create table bbsUsers +( + uuID 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 primary key (uuID) +alter table bbsUsers add constraint UK unique(uName) +alter table bbsUsers add constraint CK check(uSex='男' or uSex='女' ) +alter table bbsUsers add constraint CK1 check(uAge>=15 or uAge<=60 ) +alter table bbsUsers add constraint CK2 check(upoint>=0) + +create table bbsSection +( + ssID int not null , + sName varchar(10) not null, + sUid int +) +alter table bbsSection add constraint PK_bbsSection_ssID primary key(ssID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key (sUid)references bbsUsers(uuID) + + + +create table bbsTopic +( + tID int primary key, + tUID int foreign key references bbsUsers(uuID), + tSID int foreign key references bbsSection(ssID), + +) +alter table bbsTopic add tTitle varchar(100) not null +alter table bbsTopic add tMsg text not null +alter table bbsTopic add tTime datetime +alter table bbsTopic add tCount int + + +create table bbsReply +( + rID int primary key identity(1,1), + rUID int foreign key references bbsUsers(uuID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女',20,0 union +select'逍遥','男',18,4 union +select'七年级生','男',19,2 +select * from bbsUsers + +--2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中, +--提示查询部分列:select 列名1,列名2 from 表名 +select uName,uPoint into bbsPoint from bbsUsers + +insert into bbsSection(sName, ssID) +select '技术交流' , 1union +select '读书世界' ,2 union +select '生活百科' ,3 union +select ' 八卦区' ,4 +select * from bbsSection + +-- 4.向主贴和回帖表中添加几条记录 +-- 发帖人 板块名 帖子标题 +-- 帖子内容 帖时间 回复数量 +insert into bbsTopic( tid,tUID,tSID,tTitle,tMsg,tTime,tCount ) +select 1,3, 4,'范跑跑','谁是范跑跑','2008-7-8',1 union +select 2,2, 1,' .NET','与JAVA的区别是什么呀?','2008-9-1',2 union +select 3,1, 3,' 今年夏天最流行什么','有谁知道今年夏天最流行','2008-9-10',0 +select * from bbsTopic + + +select * from bbsReply +insert into bbsReply(rTID ,rUID,rMsg,rTime) +select 1,2,'八嘎牙路是范跑跑','2008-7-8'union +select 2,1,'没有区别就是最大的区别','2008-7-8'union +select 3,3,'今年流行上海滩','2008-7-8' +--.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户, +--请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) +alter table bbsTopic drop FK__bbsTopic__tUID__2D27B809 +alter table bbsReply drop FK__bbsReply__rUID__30F848ED +delete bbsUsers where uName ='逍遥' + +--6.因为小雨点发帖较多,将其积分增加10分 +update bbsUsers set uPoint=uPoint+10 where uName ='小雨点' +--7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) +select * from bbsSection +alter table bbsTopic drop FK__bbsTopic__tSID__2E1BDC42 +delete bbsSection where sName ='生活百科' +--在论坛数据库中完成以下题目 +--主贴表(bbsTopic) 回帖表(bbsReply)版块表(bbsSection) +--1.在主贴表中统计每个版块的发帖总数 +select * from bbsTopic +select tSID 板块编号 ,count(tCount)发帖总数 from bbsTopic group by tSID +--2.在回帖表中统计每个主贴的回帖总数量 +select * from bbsReply +select rTID 主贴编号,count(rID)回帖总数量 from bbsReply group by rTID +--3.在主贴表中统计每个用户的发的主帖的总数 +select * from bbsTopic +select tUID 用户,count(tSID ) 发帖总数 from bbsTopic group by tUID +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select tUID 用户,sum(tCount ) 回复数量总和 from bbsTopic group by tUID +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select tSID 版块 ,avg(tCount)平均 from bbsTopic group by tSID having avg(tCount)>3 +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select top 1 uName,uSex ,uAge ,uPoint from bbsUsers order by uPoint desc +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic +select * 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 in( select uAge 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 where uName like '小_点' +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来, +--并且为列取上对应的中文列名 +select * from bbsTopic +select tTime 时间,tCount 回复数量 ,tTitle 标题 ,tMsg 内容 from bbsTopic where tCount>10 +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID 发帖人编号 ,tCount 回复数量 from bbsTopic where tTitle like'%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1e08360ffb850516e0541f914153ec33c1e0422f --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery1.sql" @@ -0,0 +1,64 @@ +create database Giao +on +( + name='Giao', + filename='D:\Giao.mdf' +) +log on +( + name='Giao_log', + filename='D:\Giao_log.ldf' +) +go + +use Giao +go + +create table orders +( + orderId int primary key, + orderDate date +) + +create table orderItem +( + ItemiD int identity(1,1) primary key, + orderId int, + itemType nvarchar(5), + 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), + (2,'文具','尺',10,1), + (3,'体育用具','篮球',1,56), + (4,'文具','笔',36,2), + (5,'文具','固体胶',20,3), + (6,'日常用品','透明胶',2,1), + (7,'体育用具','羽毛球',20,3), + (8,'文具','订书机',20,3), + (9,'文具','订书针',10,3), + (10,'文具','裁纸刀',5,5), + (11,'文具','笔',20,2), + (12,'文具','信纸',50,1), + (13,'日常用品','毛巾',4,5), + (14,'日常用品','透明胶',30,1), + (15,'体育用具','羽毛球',20,3) +--查询所有订单订购的所有物品数量总和 +select sum(theNumber) 订购物品总量 from orderItem +--查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select orderId,sum(theNumber) 订购物品总量,avg(theMoney) 平均单价 from orderItem group by orderId having orderId<3and avg(theMoney)<10 +--查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select orderId,sum(theNumber) 订购物品总量,avg(theMoney) 平均单价 from orderItem group by orderId having avg(theMoney)<10 and sum(theNumber)>50 +--查询每种类别的产品分别订购了几次 +select itemType'类别',count(*)'分别订购了几次' from orderItem group by itemType +--查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select itemType'类别',sum(theNumber)'订购物品总量',avg(theMoney)'平均单价' from orderItem group by itemType having sum(theNumber)>100 +--查询每种产品的订购次数,订购总数量和订购的平均单价 +select itemName'产品名称',count(*)'订购次数',sum(theNumber)'订购总量',avg(theMoney)'平均单价' from orderItem group by itemName \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..72effc0f5cfa2a28dcf1cc345aa29db71bedee1b --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery2.sql" @@ -0,0 +1,125 @@ +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 text not null, + tMsg text 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 text 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=uPoint+10 where uName='小雨点' +alter table bbsTopic drop FK_bbsTopic_tSID +delete bbsSection where sName='生活百科' + +delete bbsReply + +--在论坛数据库中完成以下题目 + +--1.在主贴表中统计每个版块的发帖总数 +select distinct tSID,count(tSID) 发帖总数 from bbsTopic group by tSID +--2.在回帖表中统计每个主贴的回帖总数量 +select distinct rTID,count(rID) 回帖总数量 from bbsReply group by rTID +--3.在主贴表中统计每个用户的发的主帖的总数 +select distinct tUID,count(tUID) 主贴总数 from bbsTopic group by tUID +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select distinct tUID, sum(tCount) from bbsTopic group by tUID +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select distinct tSID 板块,avg(tCount) 平均回复数量 from bbsTopic group by tSID having avg(tCount)>3 +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select top 1* from bbsUsers order by uPoint DESC --不考虑并列第一 +select * from bbsUsers where uPoint=(select max(uPoint) from bbsUsers) +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select tTitle,tMsg from bbsTopic where tTitle like'%快乐%' or tMsg like '%快乐%' +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select uName 优秀用户 from bbsUsers where uAge>=15 and uAge<=20 and uPoint>10 +select uName 优秀用户 from bbsUsers where (uAge between 15 and 20)and uPoint>10 +select uName 优秀用户 from bbsUsers where uAge in (select uAge from bbsUsers where uAge>=15 and uAge<=20) and uPoint in(select uPoint from bbsUsers where uPoint>10) +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where uName like '小_大%' +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select tTitle 标题,tMsg 内容 from bbsTopic where tTime>'2008-9-10 12:00:00' and tCount>10 +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID 发帖人编号,tCount 回复数量 from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..94313e65901e0321d1b748ffcb988c14b4f18b12 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery2.sql" @@ -0,0 +1,65 @@ +create database JXC +on +( + name='JXC', + filename='D:\JXC.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +log on +( + name='JXC_log', + filename='D:\JXC.ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +go +use JXC +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 diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8c7c3194f92d9f0207971d6436fec55005d3d565 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery1.sql" @@ -0,0 +1,76 @@ +create database AKA +on( + name='D:\AKA', + filename='D:\AKA.mdf', + size=5, + maxsize=50, + filegrowth=15% +) +log on +( + name='D:\AKA_log', + filename='D:\AKA_log.ldf', + size=5, + maxsize=50, + filegrowth=15% +) +use AKA + +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, +) +select * from orders +select * from orderltem +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 sum(theNumber) from orderltem +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select orderld 订单编号,sum(theNumber)所有物品的数量和,avg(theMoney)平均单价 from orderltem group by orderld having avg(theMoney)<10 and orderld<3 +select * from orderltem +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select orderld 订单编号,avg(theMoney)平均单价,sum(theNumber)总数量 from orderltem group by orderld having avg(theMoney)<10 and sum(theNumber)>50 +select * from orderltem +--4.查询每种类别的产品分别订购了几次,例如: + --文具 9 + --体育用品 3 + --日常用品 3 +select itemType 产品总类, count(*)订购了几次 from orderltem group by itemType + +-- 5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select itemType 产品类别,sum(theNumber)订购总数量,avg(theMoney)平均单价 from orderltem group by itemType having sum(theNumber)>100 +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: + + --产品名称 订购次数 总数量 平均单价 + -- 笔 3 120 2 +select itemName 产品,count(*)订购次数,sum(theNumber)总数量,avg(theMoney)平均单价 from orderltem group by itemName diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..96b0792e98f0acda9563f3ddb329cda84d4198a4 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/SQLQuery2.sql" @@ -0,0 +1,110 @@ +create database bbs +on( + name='E:\bbs', + filename='E:\bbs.mdf', + size=5, + maxsize=50, + filegrowth=5% +) +log +on( + name='E:\bbs_log', + filename='E:\bbs_log.mdf', + size=5, + maxsize=50, + filegrowth=5% +) +use bbs + +create table bbsUsers +( + uuID 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 primary key(uuID) +alter table bbsUsers add constraint UK unique(uName) +alter table bbsUsers add constraint CK check(uSex='男' or uSex='女') +alter table bbsUsers add constraint CK1 check(uAge>=15 or uAge<=60) +alter table bbsUsers add constraint CK2 check(upoint>=0) + +create table bbsSection +( + ssID int not null, + sName varchar(10) not null, + sUid int +) +alter table bbsSection add constraint PK_bbsSection_ssID primary key(ssID) +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid)references bbsUsers(uuID) + + +create table bbsTopic +( + tID int primary key identity not null, + tUID int foreign key references bbsUsers(uuID) not null, + tSID int foreign key references bbsSection(ssID), + 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 not null, + rUID int foreign key references bbsUsers(uuID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) +select * from bbsSection +select * from bbsReply +select * from bbsTopic +select * from bbsUsers + +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, ssID)values +('技术交流' , 1), +('读书世界' ,2 ), +('生活百科' ,3 ), +(' 八卦区' ,4) +select * from bbsSection + + + +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 bbsTopic + +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='小雨点' +select * from bbsReply + + +select tSID,count(*)发帖总数 from bbsTopic group by tSID + +select rTID,count(*)回帖数量 from bbsReply group by rTID + +select tUID,count (*)发帖数量 from bbsTopic group by tUID + +select sum(tCount)每个用户发帖总数 from bbsTopic + +select AVG(tCount)平均回复数 from bbsTopic group by tCount having AVG(tCount)>3 + +select top 1 * from bbsUsers order by uPoint DESC + +select * from bbsTopic where tTitle='快乐' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..99da5af25d9e26a07483e953db5181bc7c2b7e27 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" @@ -0,0 +1,48 @@ +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) +select sum(theNumber) from orderItem + +select orderId,sum(theNumber),avg(theMoney) from orderItem group by orderId having orderId<3 and avg(theMoney)<10 + +select sum(theNumber)物品数量,avg(theMoney)平均单价 from orderItem group by orderID having avg(theMoney)<10 and sum(theNumber)>50 + +select itemType,count(*)订购 from orderItem group by itemType + +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 \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a025bb55dfea9da23d2f931a01cf76031a1d1319 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" @@ -0,0 +1,83 @@ +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 tSID,count(tID) from bbsTopic group by tSID +select rTID,count(rID) from bbsReply group by rTID +select tUID 用户,count(tID) 主帖的总数 from bbsTopic group by tUID +select tUID 用户,tID 主帖,sum(tCount) 回复数量总和 from bbsTopic group by tUID,tID +select tUID 用户,tID 主帖,avg(tCount) 回复数量总和 from bbsTopic group by tUID,tID having avg(tCount)>3 +select uName 用户名, uSex 性别 , uAge 年龄, uPoint 积分 from bbsUsers where uPoint=(select max(uPoint) from bbsUsers) +select * from bbsTopic where tMsg like '%快乐%' or tTitle like '%快乐%' +select * from bbsUsers where uAge>=15 and uAge<=20 and uPoint>10 +select * from bbsUsers where uAge between 15 and 20 and uPoint>10 +select uName,sum(uPoint),uAge from bbsUsers where uAge>=15 and uAge<=20 group by uName,uPoint,uAge having sum(uPoint)>10 +select * from bbsUsers where uName like '小_大%' +select tTitle 标题,tMsg 内容 from bbsTopic where tTime between '2008-9-10 12:00:00' and getdate() and tCount>10 +select tCount 回复数量,tUID 发帖人编号 from bbsTopic where tTitle like '%!' diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b6ea9fb5e0dacb77c69172b5be0136709b2a1668 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery1.sql" @@ -0,0 +1,84 @@ +use master +go +create database dindan +on +( +name='bbse', +filename='D:\bbse.mdf', +size=5, +filegrowth=1, +maxsize=50 +) +log on +( +name='bbse_log', +filename='D:\bbse_log.ldf', +size=5, +filegrowth=1, +maxsize=50 +) +go +use dindan +go + +--订单表(orders)列为:订单编号(orderId 主键) 订购日期(orderDate) +create table orders +( +orderId varchar(20) primary key, +orderDate varchar(30) +) +go +--订购项目表(orderItem),列为: +create table orderItem +( +ItemiD int primary key identity(1,1), +orderId varchar(20) , +itemType nvarchar(10), +itemname nvarchar(10), +theNumber int , +theMoney int +) +go +--项目编号(ItemiD)订单编号(orderId)产品类别(itemType) +--产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) +insert into orderItem (orderId,itemType,itemname,theNumber,theMoney) +values(1,'文具','笔',72,2), +(1,'文具','尺',10,1), +(1,'体育用品','篮球',1,56), +(1,'文具','笔',36,2), +(2,'文具','固体胶',20,3), +(2,'日常用品','透明胶',2,1), +(2,'体育用品','羽毛球',20,3), +(2,'文具','订书机',20,3), +(3,'文具','订书机',10,3), +(3,'文具','裁纸刀',5,5), +(3,'文具','笔',20,2), +(4,'文具','信纸',50,1), +(4,'日常用品','毛巾',4,5), +(4,'日常用品','透明胶',30,1), +(4,'体育用品','羽毛球',20,3) +select * from orderItem + +insert into orders (orderId,orderDate) +values('1','2008-01-12 00:00:00.000'), +('2','2008-01-12 00:00:00.000'), +('3','2008-01-12 00:00:00.000'), +('4','2008-01-12 00:00:00.000') +select * from orders +--1.查询所有订单订购的所有物品数量总和 +select sum(theNumber)所购物品数量总和 from orderItem +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select orderId,sum(theNumber),avg(theMoney) from orderItem where orderId<3 group by orderId,theNumber,theMoney having AVG(theMoney)<10 +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select sum(theNumber), avg(theMoney) from orderItem group by theMoney having AVG(theMoney)<10 and sum(theNumber)>50 +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select itemType 产品名称,count(itemType)次数 from orderItem group by itemType +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select itemType,sum(theNumber),AVG(theMoney) from orderItem group by itemType having SUM(theNumber)>100 +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: +select itemName 名称,count(theNumber)次数,sum(theNumber)总数量,avg(theMoney)平均单价 from orderItem group by itemName +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..53f0a80d5348890ce019b436f8a06af73b1be6dc --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery2.sql" @@ -0,0 +1,148 @@ +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 +) + +--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') + +select * from bbsReply +--人数不够,外键有约束 +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 tSID as 板块编号 , count( tID ) 发帖数量 from bbsTopic group by tSID +--2.在回帖表中统计每个主贴的回帖总数量 + + select tCount 回帖数 from bbsTopic group by tCount +--3.在主贴表中统计每个用户的发的主帖的总数 + select tUID as 用户号 ,count( tSID) 对应的帖数 from bbsTopic group by tUID + --select count(*)主帖的总数 from bbsTopic group by tUID + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 + select tUID as 用户号 ,sum(tCount) 回帖数量 from bbsTopic group by tUID,tCount + select sum(tCount)总回复数 from bbsTopic where tCount>=1 + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 + select count( tID) 每个板块的主贴编号总数, sum(tCount) 每个板块回复的总数 , sum(tCount)/count( tID) 每个板块的平均回复数量 from bbsTopic group by tSID having (sum(tCount)/count( tID))>3 + --select tSID,tCount from bbsTopic group by tSID,tCount having AVG(tCount)>3 + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 + -- select top 1 用户名=uName, 性别 = uSex, 年龄= uAge, 积分=uPoint from bbsUsers order by uPoint desc + --select 用户名=uName, 性别 = uSex, 年龄= uAge, 积分=uPoint from bbsUsers where uPoint=(select max(uPoint) from bbsUsers) + select top 1 uName , uSex , uAge ,uPoint from bbsUsers group by uName , uSex , uAge ,uPoint order by uPoint desc +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 + select*from bbsTopic where tTitle='%快乐%' or tMsg='%快乐%' + --select tTitle from bbsTopic where tTitle='%快乐%' and tMsg='%快乐%' group by tTitle + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) + select*from bbsUsers where uAge between 15 and 20 and uPoint>10 + select uName ,sum(uPoint) from bbsUsers where uAge between 15 and 30 group by uName having sum(upoint)>10 + + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 + select*from bbsUsers where uName like '小_大%' + -- select * from bbsUsers where uName ='小_大' + +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 + select 贴子的标题 = tTitle,帖子的内容 = tMsg from bbsTopic where tCount>10 + -- rTime >'2008'and rTime >'9'and rTime >'10'and rTime >'12' + --select tTitle 标题,tMsg 内容 from bbsTopic where DateDiff(dd,tTime,getdate())=2008-9-10 and tCount>=10 group by tTitle,tMsg + +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select 发帖人编号= tUID, 回复数量=tCount from bbsTopic where tTitle like '&!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery7.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..50a62dee612e320b89e06b9d6b26f79ed5485374 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery7.sql" @@ -0,0 +1,95 @@ +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 bbsSection +select * from bbsReply +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 tSID,count(*)发帖数量 from bbsTopic group by tSID +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID,count(*)回帖数量 from bbsReply group by rTID +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID,count (*)发帖数量 from bbsTopic group by tUID +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select sum(tCount)每个用户发帖总数 from bbsTopic +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select AVG(tCount)平均回复数 from bbsTopic group by tCount having AVG(tCount)>3 +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select top 1 * from bbsUsers order by uPoint DESC +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic where tTitle='快乐' +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers where uAge in(15,20) and uPoint>=10 +select * from bbsUsers where uAge>=15 and uAge<=20 and uPoint>=10 +select * from bbsUsers where uAge between 15 and 20 and uPoint>=10 +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where uName like '小_大%' +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select tTitle 标题,tMsg 内容 from bbsTopic where tTime='2008-9-10 12:00:00' and tCount>=10 +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID,tCount from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\347\216\213\344\275\263\346\226\207.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\347\216\213\344\275\263\346\226\207.sql" new file mode 100644 index 0000000000000000000000000000000000000000..163e161a0158446d1d17c460faac220d9e34b566 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/\347\216\213\344\275\263\346\226\207.sql" @@ -0,0 +1,53 @@ +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) + +select sum(theNumber)订购商品数量总和 from orderItem +select orderId,AVG(theMoney)平均单价,sum(theNumber)数量和 from orderItem group by orderId +having AVG(theMoney)<10 and orderId<3 order by orderId +select orderId,AVG(theMoney)平均单价,SUM(theNumber)数量和 from orderItem where theNumber>50 group by orderId +having AVG(theMoney)<10 order by orderId +select itemType, count(*)数量 from orderItem group by itemType order by itemType DESC +select itemName,count(*) 购买次数,sum(theNumber)数量总和,AVG(theMoney)平均单价 from orderItem group by itemName + + + + \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8af857e29f128cf939d0daf062a1669fedcaeeee --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" @@ -0,0 +1,162 @@ +use master +go +create database bbs +on( + name='bbs', + filename='D:\11\bbs.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on( + name='bbs_log', + filename='D:\11\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 , +) +go +use bbs +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 tSID, count(*) from bbsTopic group by tSID +--2.鍦ㄥ洖甯栬〃涓粺璁℃瘡涓富璐寸殑鍥炲笘鎬绘暟閲 +select rTID, count(*) from bbsReply group by rTID +--3.鍦ㄤ富璐磋〃涓粺璁℃瘡涓敤鎴风殑鍙戠殑涓诲笘鐨勬绘暟 +select tUID, count(*) from bbsTopic group by tUID +--4.鍦ㄤ富璐磋〃涓粺璁℃瘡涓敤鎴峰彂鐨勪富璐寸殑鍥炲鏁伴噺鎬诲拰 +select tCount,tUID, count(*) from bbsTopic group by tCount,tUID order by tUID +--5.鍦ㄤ富璐磋〃涓煡璇㈡瘡涓増鍧楃殑涓昏创鐨勫钩鍧囧洖澶嶆暟閲忓ぇ浜3鐨勭増鍧楃殑骞冲潎鍥炲鏁伴噺 +select tCount,tSID, count(*),AVG(tCount) from bbsTopic group by tCount,tSID having AVG(tCount)>3 +--6.鍦ㄧ敤鎴蜂俊鎭〃涓煡璇㈠嚭绉垎鏈楂樼殑鐢ㄦ埛鐨勭敤鎴峰悕锛屾у埆锛屽勾榫勫拰绉垎 +select TOP 1 * from bbsUsers order by uPoint DESC +--7.鍦ㄤ富璐磋〃涓紙bbsTopic锛変腑灏嗗笘瀛愮殑鍐呭鎴栨爣棰樹腑鏈夆滃揩涔愨濅袱瀛楃殑璁板綍鏌ヨ鍑烘潵 +select * from bbsTopic where tTitle like '%蹇箰%' or tMsg like '%蹇箰%' +--8.鍦ㄧ敤鎴蜂俊鎭〃锛坆bsUsers锛変腑灏嗙敤鎴峰勾榫勫湪15-20涔嬮棿骞朵笖绉垎鍦10鍒嗕互涓婄殑浼樼鐢ㄦ埛鏌ヨ鍑烘潵锛堢敤澶氱鏂规硶瀹炵幇锛 +select * from bbsUsers where uAge>=15 and uAge<=20 and uPoint>10 +select top 1 * from bbsUsers where uAge>=15 and uAge<=20 order by uPoint DESC +--9.鍦ㄧ敤鎴蜂俊鎭〃锛坆bsUsers锛変腑灏嗙敤鎴峰悕鐨勭涓涓瓧涓衡滃皬鈥濓紝绗笁瀛椾负鈥滃ぇ鈥濈殑鐢ㄦ埛淇℃伅鏌ヨ鍑烘潵 +select * from bbsUsers where uName like '灏廮澶%' +--10.鍦ㄤ富璐磋〃锛坆bsTopic锛変腑灏嗗湪2008-9-10 12:00:00 浠ュ悗鍙戠殑骞朵笖鍥炲鏁伴噺鍦10浠ヤ笂鐨勫笘瀛愮殑鏍囬鍜屽唴瀹规煡璇㈠嚭鏉ワ紝骞朵笖涓哄垪鍙栦笂瀵瑰簲鐨勪腑鏂囧垪鍚 +select tTitle 鏍囬,tMsg 鍐呭 from bbsTopic where tTime between '2008-9-10 12:00:00' and getdate() and tCount>10 +--11.鍦ㄤ富璐磋〃锛坆bsTopic锛変腑灏嗗笘瀛愮殑鏍囬鏄互鈥橈紒鈥欑粨灏剧殑甯栧瓙鐨勫彂甯栦汉缂栧彿鍜屽洖澶嶆暟閲忔煡璇㈠嚭鏉 +select tUID,tCount from bbsTopic where tTitle like '%!' + +--鍏堝垱寤哄鍥炬墍绀鸿〃 + +--璁㈠崟琛紙orders锛夊垪涓猴細璁㈠崟缂栧彿锛坥rderId 涓婚敭锛 璁㈣喘鏃ユ湡锛坥rderDate锛 + +--璁㈣喘椤圭洰琛紙orderItem锛夛紝鍒椾负锛 +--椤圭洰缂栧彿锛圛temiD锛夎鍗曠紪鍙凤紙orderId锛変骇鍝佺被鍒紙itemType锛 +--浜у搧鍚嶇О锛坕temName锛 璁㈣喘鏁伴噺锛坱heNumber锛 璁㈣喘鍗曚环锛坱heMoney锛 +use bbs +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 +) +select * from orders +select * from orderItem +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') +--1.鏌ヨ鎵鏈夎鍗曡璐殑鎵鏈夌墿鍝佹暟閲忔诲拰 +select SUM(theNumber) from orderItem +--2.鏌ヨ璁㈠崟缂栧彿灏忎簬3鐨勶紝骞冲潎鍗曚环灏忎簬10 姣忎釜璁㈠崟璁㈣喘鐨勬墍鏈夌墿鍝佺殑鏁伴噺鍜屼互鍙婂钩鍧囧崟浠 +select theNumber 鐗╁搧鐨勬暟閲,orderId 璁㈠崟缂栧彿,AVG(theMoney)骞冲潎鍗曚环 from orderltem group by theNumber,orderId having orderId<3 and AVG(theMoney)<10 +--3.鏌ヨ骞冲潎鍗曚环灏忎簬10骞朵笖鎬绘暟閲忓ぇ浜 50 姣忎釜璁㈠崟璁㈣喘鐨勬墍鏈夌墿鍝佹暟閲忓拰浠ュ強骞冲潎鍗曚环 +select theNumber 鐗╁搧鏁伴噺,AVG(theMoney) 骞冲潎鍗曚环 from orderltem group by theNumber having AVG(theMoney)<10 and 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 itemType 浜у搧鍚嶇О,count(itemType) 璁㈣喘娆℃暟,sum(theNumber) 鎬绘暟閲,AVG(theMoney) 骞冲潎鍗曚环 from orderItem group by itemType \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/shop.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/shop.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2f01a0b9d46a504eab1bd0e6c52d66eb738702d0 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/shop.sql" @@ -0,0 +1,77 @@ +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 + +--1.查询所有订单订购的所有物品数量总和 +select SUM(theNumber) from orderItem + +--2.查询订单编号小于3的,平均单价小于10 ,每个订单订购的所有物品的数量和以及平均单价 +select orderId,SUM(theNumber),AVG(theMoney) from orderItem where orderId<3 group by orderId having 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(orderId) 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(orderId) 订购次数,sum(theNumber)总数量, avg(theMoney)平均单价 from orderItem group by itemName \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/\345\260\217\351\233\250\347\202\271.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/\345\260\217\351\233\250\347\202\271.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a4c3935d4e84d6cb4feedb885b977d8ae72fd535 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/\345\260\217\351\233\250\347\202\271.sql" @@ -0,0 +1,131 @@ +use master +go + +create database bbs +on +( + name = 'bbs', + filename = 'D:\bbs.mdf' +) + +log on +( + name = 'bbsUser_log', + filename = 'D:\bbs_log.ldf' +) + +use bbs + +create table bbsUsers +( + UID int identity 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_s primary key(UID) +alter table bbsUsers add constraint UK_b unique(uName) +alter table bbsUsers add constraint CK_b check(uSex in ('男','女')) +alter table bbsUsers add constraint CK_a check( uAge>=15 and uAge<=60) +alter table bbsUsers add constraint CK_c check( uPoint>=0) + +create table bbsTopic +( + tID int identity, + tUID int , + tSID int , + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) + +alter table bbsTopic add constraint FK_S foreign key(tUID) references bbsUsers(UID) + +create table bbsReply +( + rID int identity, + rUID int, + rTID int, + rMsg text not null, + rTime datetime +) +alter table bbsReply add constraint PK_z primary key(rID) +alter table bbsReply add constraint FK_d foreign key(rUID) references bbsUsers(UID) + +create table bbsSection +( + sID int identity, + sName varchar(10) not null, + sUid int, +) +alter table bbsTopic add constraint FK_l foreign key(tSID) references bbsSection(sID) +alter table bbsSection add constraint PK_q primary key(sID) +alter table bbsSection add constraint PK_p foreign key(sUid) references bbsUsers(UID) + +insert into bbsUsers (uName,uSex,uAge,uPoint) +select '小雨点','女',20 ,0 union +select '逍遥','男',18 ,4 union +select '七年级生','男',19 ,2 + +select uName,uPoint into bbsPoint from bbsUsers +select * from bbsPoint + +select * from bbsUsers +select * from bbsSection + +insert into bbsSection (sName, sUid)values('技术交流',3),( '读书世界',1),( '生活百科',3),('八卦区',1) + +select * from bbsSection +select * from bbsTopic + +insert into bbsTopic (tUID,tSID,tTitle,tMsg,tTime,tCount)values +(2,4,'范跑跑','谁是范跑跑','2008-7-8',1), +(1,1,'.NET ','与JAVA的区别是什么呀?','2008-9-1','2'), +(3,3,'今年夏天最流行什么','今年夏天最流行什么','2008-9-10',0) +select * from bbsTopic + +insert into bbsReply (rUID,rTID,rMsg,rTime)values +(1,4,'范跑跑是一位老师','2008-07-09'), +(2,1,'.NET与JAVA区别很大','2008-10-09'), +(2,1,'.JAVA是一门编程语言','2008-10-09') +select * from bbsReply + + +--在论坛数据库中完成以下题目 + +--1.在主贴表中统计每个版块的发帖总数 +select count(tTime)发帖总数,tSID 板块编号 from bbsTopic group by tSID + +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID ,count(rUID)回帖数量 from bbsReply group by rTID + +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID, count(tUID) 用户发帖的总数 from bbsTopic group by tUID + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select sum(tCount) 回帖数量总和 from bbsTopic group by tUID + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select tSID 板块编号,avg(tCount)回复数量 from bbsTopic group by tSID having avg(tCount)>3 + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select top 1 uName 姓名,uSex 性别, uAge 年龄, uPoint 积分 from bbsUsers order by uPoint desc +select max(uPoint)积分最高 from bbsUsers +select uName 姓名,uSex 性别, uAge 年龄, uPoint 积分 from bbsUsers where Upoint in(select max(uPoint) from bbsUsers) + +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic where tTitle like '%快乐%' or tMsg like '%快乐%' + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers where uAge in( select uAge from bbsUsers where uAge between 15 and 20 and uPoint>=10) + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where uName like '小_点' +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select * from bbsTopic +select tTime 时间,tCount 回复数量 ,tTitle 标题 ,tMsg 内容 from bbsTopic where tCount>10 +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID 发帖人编号 ,tCount 回复数量 from bbsTopic where tTitle like'%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ee23ae27299dbaace0cd0cc916a42cb42497c4be --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" @@ -0,0 +1,85 @@ +--先创建如图所示表 +use master +go + +create database Sql +on +( + name=Sql, + filename='D:\作业\sql作业\Sql.mdf', + size = 5mb, + maxsize =100mb, + filegrowth =10% +) +log on +( + name=Sql_log, + filename='D:\作业\sql作业\Sql.ldf', + size = 5mb, + maxsize =100mb, + filegrowth =10% +) +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) +--1.查询所有订单订购的所有物品数量总和 +select sum(theNumber) from orderItem +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select theNumber 订购数量 ,orderId 订单编号, avg(theMoney) from orderItem group by theNumber,orderId having orderId<3 and avg(theMoney)<10 + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select theNumber 订购数量, avg(theMoney) 平均单价 from orderItem group by theNumber having avg(theMoney)<10 and theNumber>50 +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select itemType 产品, count(theNumber)订购次数 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(theNumber)订购次数, sum(theNumber)总数量,avg(theMoney)平均单价 from orderItem group by itemName diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..11011364573a93dc4f9946a6b189f6fc8a8af538 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery2.sql" @@ -0,0 +1,120 @@ +create database bbs +on +( + fileName='D:\作业\sql作业bbs.mdf', + Name='bbs', + size=5MB, + Maxsize=5MB, + filegrowth=1MB +) +log on +( + fileName='D:\作业\sql作业\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 tSID 版块,count(tID) 数量 from bbsTopic group by tSID +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID 主贴,count(rID) 回帖总数量 from bbsReply group by rTID + +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID 用户,count(tID) 主帖的总数 from bbsTopic group by tUID +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select tUID 用户,tID 主帖,sum(tCount) 回复数量总和 from bbsTopic group by tUID,tID +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select tSID 版块,tID 主贴,AVG(tCount) 平均回复数量 from bbsTopic group by tSID,tID having AVG(tCount)>3 +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select uName 用户名,uSex 性别,uAge 年龄,uPoint 积分 from bbsUsers where uPoint=(select MAX(uPoint) from bbsUsers) +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic where tMsg like '%快乐%' or tTitle like '%快乐%' + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +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 where uName like '小_大%' +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select tTitle 标题,tMsg 内容 from bbsTopic where tTime between '2008-9-10 12:00:00' and getdate() and tCount>10 +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tCount 回复数量,tUID 发帖人编号 from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/.keep" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9f4c05ae9a1d1df27549b8b9038c40f9830ebd8c --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" @@ -0,0 +1,80 @@ +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) +alter table bbsTopic drop constraint FK_bbsUsers1_UID +alter table bbsReply drop constraint FK_bbsUsers_UID +delete from bbsUsers where uID=2 +insert into bbsUsers(uName,uPoint,uSex,uAge) values('小雨点',10,'女',20) +alter table bbsTopic drop constraint FK_bbsSection_sID +delete from bbsSection where sName='生活百科' +delete from bbsReply +select * from bbsTopic +select sName,count(*) from bbsSection group by sName +select rUID,count(*) from bbsReply group by rUID +select tUID,count(*) from bbsTopic group by tUID +select rTID,count(*) from bbsReply group by rTID +select tID,tcount from bbsTopic group by tID,tCount having AVG(tCount)>3 +select uName,uSex,uAge,uPoint from bbsUsers where uPoint=(select max(uPoint) from bbsUsers) +select tTitle,tMsg from bbsTopic where tTitle like '%快乐%' or tMsg like '%快乐%' +select uName from bbsUsers where uAge between 15 and 20 and uPoint>=10 +select uName from bbsUsers where uAge>=15 and uAge<=20 and uPoint>=10 +select uName,uSex,uAge,uPoint from bbsUsers where uName like '小%' or uName like '__大%' +select tTitle 标题,tMsg 内容 from bbsTopic where tTime>'2008-9-10 12:00:00' and tCount>10 +select tUID,tCount from bbsTopic where tTitle='%!' diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d6601426827ff3afff2211b1ada35a26d08a5fdf --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery2.sql" @@ -0,0 +1,37 @@ +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 sum(theNumber) 订单数量 from orderItem +select sum(theNumber) 所有物品的数量和,AVG(theMoney) 平均单价 from orderItem where orderId<3 and theMoney<10 +select sum(theNumber) 所有物品的数量和,AVG(theMoney) 平均单价 from orderItem where theNumber>50 and theMoney<10 +select itemType,count(itemType) 购买次数 from orderItem group by itemType +select itemType,sum(theNumber) 订购总数量,AVG(theMoney) 平均单价 from orderItem group by itemType having sum(theNumber)>100 +select itemName 产品名称,count(itemType) 订购次数,sum(theNumber) 总数量,AVG(theMoney) 平均单价 from orderItem group by itemName \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6f08afbee6393f4cd16ea78cd0367baa80d7a8bb --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye1.sql" @@ -0,0 +1,98 @@ +use master +go +create database bb +on +( +name='bb', +filename='D:\text\bb.mdf', +size=5mb, +maxsize=50mb, +filegrowth=10% +) +log on +( +name='bb_log', +filename='D:\text\bb_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=10% +) +use bb +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 sum(theNumber) from orderItem +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select orderID 订单编号,theNumber 物品的数量,AVG(theMoney)平均单价 +from orderItem group by orderID, theNumber 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)>=100 + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 +select itemname 产品名称,count(*) 订购次数,sum(theNumber) 订购总数量,avg(theMoney) 平均单价 +from orderltem group by itemname + + + + + + + + + + +select itemName 产品名称,count(*) 订购次数,sum(theNumber) 订购总数量,avg(theMoney) 平均单价 +from orderItem group by itemName \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..39d02394db1dc7c6b5085819304e180798fd3fcb --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye2.sql" @@ -0,0 +1,155 @@ +create database bbs +on +( + name='bbs', + filename='D:\test\bbs.mdf' +) +log on +( + name='bbs_log', + filename='D:\test\bbs_log.ldf' +) +-- 2.创建表 +-- 注意以下4张表的创建顺序,要求在创建bbsUser和bbsSection表时不添加约束,创建完后单独添加约束,其它的表创建时添加约束 +-- 用户信息表(bbsUsers) +use bbs +go +create table bbsUsers +( +-- 用户编号 UID int 主键 标识列 + UID int identity(1,1), +-- 用户名 uName varchar(10) 唯一约束 不能为空 + uName varchar(10) not null, +-- 性别 uSex varchar(2) 不能为空 只能是男或女 + uSex varchar(2) not null, +-- 年龄 uAge int 不能为空 范围15-60 + uAge int not null, +-- 积分 uPoint int 不能为空 范围 >= 0 + 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 in('男','女')) +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) + +-- 版块表(bbsSection) +create table bbsSection +( +-- 版块编号 sID int 标识列 主键 + sID int identity(1,1), +-- 版块名称 sName varchar(10) 不能为空 + sName varchar(10) not null, +-- 版主编号 sUid int 外键 引用用户信息表的用户编号 + 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) + +--+ 主贴表(bbsTopic) +create table bbsTopic +( +-- 主贴编号 tID int 主键 标识列, + tID int primary key(tID) identity(1,1), +-- 发帖人编号 tUID int 外键 引用用户信息表的用户编号 + tUID int, +-- 版块编号 tSID int 外键 引用版块表的版块编号 (标明该贴子属于哪个版块) + tSID int, +-- 贴子的标题 tTitle varchar(100) 不能为空 + tTitle varchar(100) not null, +-- 帖子的内容 tMsg text 不能为空 + tMsg text not null, +-- 发帖时间 tTime datetime + tTime datetime, +-- 回复数量 tCount int + tCount int +) +alter table bbsTopic add constraint FK_bbsTopic_tUID foreign key(tUID) references bbsUsers(UID) +alter table bbsTopic add constraint FK_bbsTopic_tSID foreign key(tSID) references bbsSection(sID) + +--+ 回帖表(bbsReply) +create table bbsReply +( +-- 回贴编号 rID int 主键 标识列, + rID int primary key(rID) identity(1,1), +-- 回帖人编号 rUID int 外键 引用用户信息表的用户编号 + rUID int, +-- 对应主贴编号 rTID int 外键 引用主贴表的主贴编号 (标明该贴子属于哪个主贴) + rTID int, +-- 回帖的内容 rMsg text 不能为空 + rMsg text not null, +-- 回帖时间 rTime datetime + rTime datetime, +) +alter table bbsReply add constraint FK_bbsReply_rUID foreign key(rUID) references bbsUsers(UID) +alter table bbsReply add constraint FK_bbsReply_rTID foreign key(rTID) references bbsSection(sID) + +--二、在上面的数据库、表的基础上完成下列题目: +use bbs +go + +-- 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个板块 +-- 名称 版主名 +-- 技术交流 小雨点 +-- 读书世界 七年级生 +-- 生活百科 小雨点 +-- 八卦区 七年级生 +insert into bbsSection values ('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + +-- 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,4,'范跑跑','谁是范跑跑','2008-7-8',1),(3,1,'.NET','与JAVA的区别是什么呀?','2008-9-1',2),(1,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10',0) +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 +insert into bbsReply values(1,1,'不知道','2008-7-9'),(1,2,'不知道','2008-9-2'),(1,2,'问百度','2008-9-2') +-- 用户信息表 +select*from bbsUsers +-- 版块表 +select*from bbsSection +--+ 主贴表 +select*from bbsTopic +--+ 回帖表 +select*from bbsReply +--在论坛数据库中完成以下题目 + +--1.在主贴表中统计每个版块的发帖总数 +select tSID '板块编号',count(*) '发帖总数' from bbsTopic group by tSID +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID '主贴编号',count(*) '回帖总数' from bbsReply group by rTID +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID '用户编号',count(*) '发帖总数' from bbsTopic group by tUID +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select tUID '用户编号',sum(tCount) '回帖总数' from bbsTopic group by tUID +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select tSID '主贴编号',avg(tCount) '平均回复数量' from bbsTopic group by tSID having avg(tCount)>3 +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select top 1 uName '用户名',uSex '性别',uAge '年龄',uPoint '积分' from bbsUsers order by uPoint DESC +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select tTitle,tMsg from bbsTopic where tTitle like '%快乐%' or tMsg like '%快乐%' +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers where uAge>=15 and uAge<=20 and uPoint>10 +select * from bbsUsers where uAge between 15 and 20 and uPoint>10 +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where UID =1 +select * from bbsUsers where uName like '小_大' +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select tID '主贴编号',tUID '发帖人编号',tSID '版块编号',tTitle '贴子的标题',tMsg '帖子的内容',tTime '发帖时间',tCount '回复数量' +from bbsTopic where tTime>'2008-9-10 12:00:00' and tCount>10 +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID '发帖人编号',tCount '回复数量' from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2420ed4a77b67fd5f628122466fccf5564b8e83c --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" @@ -0,0 +1,155 @@ +/** +* 作业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 + goto sel +sel: + select * from bbsPoint + select * from bbsReply + select * from bbsSection + select * from bbsTopic + select * from bbsUsers + go +/** +* 后面的就是这次作业的作业3 +**/ + +select tSID,count(*) from bbsTopic group by tSID--在主贴表中统计每个版块的发帖总数 + +select rTID,COUNT(rID)回复数量 from bbsReply group by rTID--在回帖表中统计每个主贴的回帖总数量 + +select tUID,COUNT(tID)发帖数量 from bbsTopic group by tUID--在主贴表中统计每个用户的发的主帖的总数 + +select tUID,sum(tCount) from bbsTopic group by tUID--在主贴表中统计每个用户发的主贴的回复数量总和 + +select tSID,tCount from bbsTopic group by tSID,tCount having AVG(tCount) > 3--在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 + +select uName,uSex,uAge,uPoint from bbsUsers where uPoint = (select max(uPoint) from bbsUsers)--在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 + +select * from bbsTopic where tTitle like '%快乐%' or tMsg like '%快乐%'--在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 + +--在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +/*where*/select * from bbsUsers where (uAge between 15 and 20) and uPoint > 10 +/*子查询*/select * from bbsUsers where uPoint in (select uPoint from bbsUsers where uPoint > 10 and uAge in (select uAge from bbsUsers where uAge between 15 and 20)) +--在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where uName like '小_大%' +--在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select tTitle 标题,tMsg 内容 from bbsTopic where DATEDIFF(HH,'2008-9-10 12:00:00',tTime) < 0 and tCount > 10 +--在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID,tCount from bbsTopic where tTitle like '%!' + diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..16130069e65c984e2ed0402e26501226aff535b3 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery2.sql" @@ -0,0 +1,53 @@ +/** +* 作业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 +--查询所有订单订购的所有物品数量总和 +select SUM(theNumber) 所有订单物品数量和 from orderItem +--查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select SUM(theNumber) 物品数量, AVG(theMoney) 平均单价 from orderItem where orderID < 3 and theMoney < 10 +--查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select COUNT(theNumber) 物品数量,AVG(theMoney) 平均单价 from orderItem where theMoney < 10 and theNumber > 50 +--查询每种类别的产品分别订购了几次 +select itemName,COUNT(theNumber) 订购次数 from orderItem group by itemName +--查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select itemType,SUM(theNumber) 订购总数,AVG(theMoney) 平均单价 from orderItem group by itemType having SUM(theNumber) > 100 +--查询每种产品的订购次数,订购总数量和订购的平均单价 +select itemName,COUNT(itemName) 订购次数,SUM(theNumber) 订购总数,theMoney 平均单价 from orderItem group by itemName,theMoney \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5ed910462717f19634797de95977e0d3e910f69a --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" @@ -0,0 +1,161 @@ +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% +) +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 UK unique(uName) +alter table bbsUsers add constraint CK check(uSex='男' or uSex='女' ) +alter table bbsUsers add constraint CK1 check(uAge>=15 or uAge<=60 ) +alter table bbsUsers add constraint CK2 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(tID) identity(1,1), + tUID int, + tSID int, + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +alter table bbsTopic add constraint FK_bbsTopic_tUID foreign key(tUID) references bbsUsers(UID) +alter table bbsTopic add constraint FK_bbsTopic_tSID foreign key(tSID) references bbsSection(sID) + +create table bbsReply +( + + rID int primary key(rID) identity(1,1), + + rUID int, + rTID int, + rMsg text not null, + + rTime datetime, +) +alter table bbsReply add constraint FK_bbsReply_rUID foreign key(rUID) references bbsUsers(UID) +alter table bbsReply add constraint FK_bbsReply_rTID foreign key(rTID) references bbsSection(sID) +use bbs +go +--1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +-- 小雨点 女 20 0 +-- 逍遥 男 18 4 +-- 七年级生 男 19 2 + +insert into bbsUsers values('小雨点','女',20,0),('逍遥','男',18,4),('七年级生','男',19,2) +select*from bbsUsers + +-- 2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 + +select uName, upoint into bbsPoint from bbsUsers + + +-- 3.给论坛开设4个板块 + --名称 版主名 + --技术交流 小雨点 + --读书世界 七年级生 + --生活百科 小雨点 + --八卦区 七年级生 +insert into bbsSection(sName,sUid) values ('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) +-- 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,4,'谁范跑跑','20210316'), + (1,3,'简单','20210316'), + (1,3,'断兵线','20210316') +select*from bbsReply + +-- 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) +select*from bbsUsers +select*from bbsSection +select*from bbsTopic +delete from bbsTopic where tUID=2 +select*from bbsReply + +delete from bbsUsers where uName='逍遥' + +-- 6.因为小雨点发帖较多,将其积分增加10分 + select uPoint from bbsUsers where uName='小雨点' + update bbsUsers set uPoint=10 where uName='小雨点' + +-- 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) + select*from bbsSection + alter table bbsTopic drop constraint FK_bbsReply_tSID + delete from bbsSection where sName='生活百科' + + + + +--1.在主贴表中统计每个版块的发帖总数 +select *from bbsTopic +select tSID 板块, count(tCount) 发帖总数 from bbsTopic group by tSID +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID 主贴 ,count (rID) 回帖总数 from bbsReply group by rTID +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID 用户,count(tSID ) 发帖总数 from bbsTopic group by tUID + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select tUID 用户, sum(tCount)回复数量总和 from bbsTopic group by tUID + + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select avg(tCount )平均数量 ,tSID 主贴 from bbsTopic group by tSID having avg(tCount )>3 + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select top 1 uPoint, uName,uSex,uAge from bbsUsers order by uPoint desc + +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select *from bbsTopic +select tTitle,tMsg from bbsTopic where tTitle like '%快乐%'or tMsg like '%快乐%' + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select uAge ,uPoint from bbsUsers where uAge>15and uAge<20 and uPoint>10 + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select uName from bbsUsers where uName like '小%点' +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select tTime 时间, tMsg 内容,tCount 回复数量,tTitle 标题 from bbsTopic where tCount>10 +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID 编号 ,tCount 数量 from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1d390c1a49655a174ce555592cc3903413ef4268 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" @@ -0,0 +1,70 @@ +use master +go +create database orders +on +( + name='orders', + filename = 'D:\order.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='orders_log', + filename = 'D:\order_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go +use orders +go +create table orders +( + orderId int primary key identity, + orderDate datetime , +) + +create table orderItem +( + ItemiD int primary key identity, + orderId int foreign key references orders(orderId), + itemType varchar(20), + itemName varchar(20), + theNumber int , + theMoney int , +) +go +insert into orders(orderDate) values('2008-01-12'),('2008-02-10'),('2008-02-15'),('2008-03-10') +select *from orders +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 *from orders + +select orderId 订单号, avg (theMoney) 平均单价,sum(theNumber) 数量和 from orderItem where orderId<3 group by orderId + having avg (theMoney)<10 + --3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 + + select *from orderItem + select avg (theMoney)平均单价,sum(theNumber)总数量,orderId from orderItem group by orderId having avg (theMoney)<10 and sum( theNumber)>50 + --4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select *from orderItem +select count(*),itemType from orderItem group by itemType +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select *from orderItem +select itemType,sum(theNumber) 总数量,avg (theMoney)平均单价 from orderItem group by itemType having sum(theNumber)>100 +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: + +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 +select itemName, count(itemName) 订购次数,sum(theNumber) 数量, avg(theMoney)平均单价 from orderItem group by itemName diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..21af7eec5bf724596add53735accd522f5e7c516 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery1.sql" @@ -0,0 +1,72 @@ +use master +go +create table orders +( + orderID int primary key, + orderDate datetime not null +) +select*from orders +create table orderItem +( + ItemiD int primary key, + orderId char(20) not null, + itemType varchar(10) not null, + itemName char(10) not null, + theNumber int not null, + theMoney int not null +) +select*from orderItem +insert into orders(orderID,orderDate) values +(1,'2008-01-12'), +(2,'2008-02-10'), +(3,'2008-02-15'), +(4,'2008-03-10') +select*from orders +insert into orderItem(ItemiD,orderId,itemType,itemName,theNumber,theMoney) 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 orderItem + +--1.查询所有订单订购的所有物品数量总和 +select*from orderItem +select SUM(theNumber) 数量总和 from orderItem + +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select*from orderItem +select orderId 订单编号,SUM(theNumber) 数量总和,avg(theMoney) 平均单价 from orderItem group by orderId having orderId<3 and avg(theMoney)<10 + + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select*from orderItem +select orderId 订单编号,avg(theMoney) 平均单价 ,sum(theNumber)数量总和 from orderItem group by orderId having AVG(theMoney)<10 and SUM(theNumber)>50 + +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select*from orderItem +select itemType 类别 ,count(*) 订购次数 from orderItem group by itemType + + +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select*from orderItem +select itemType 类别 ,sum(theNumber) 总数量 ,avg(theMoney) 平均单价 from orderItem group by itemType having sum(theNumber)>100 + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: + +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 +select*from orderItem diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e341d1e7a03c4929f6369db73a3323894b8990ae --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/SQLQuery2.sql" @@ -0,0 +1,159 @@ +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% +) + +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 + +use bbs +go + +select * from bbsReply --回帖表 +select * from bbsSection --版块表 +select * from bbsTopic --主帖表 +select * from bbsUsers --用户信息表 + +--1.在主贴表中统计每个版块的发帖总数 +select tSID 版块,count(tID) 数量 from bbsTopic group by tSID + +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID 主贴,count(rID) 回帖总数量 from bbsReply group by rTID + +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID 用户,count(tID) 主帖的总数 from bbsTopic group by tUID + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select tUID 用户,tID 主帖,sum(tCount) 回复数量总和 from bbsTopic group by tUID,tID + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select tUID 用户,tID 主帖,avg(tCount) 回复数量总和 from bbsTopic group by tUID,tID having avg(tCount)>3 + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select uName 用户名, uSex 性别 , uAge 年龄, uPoint 积分 from bbsUsers where uPoint=(select max(uPoint) from bbsUsers) +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic where rMsg like '%快乐%' or tTitle like '%快乐%' + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers where uAge>=15 and uAge<=20 and uPoint>=10 +select * from bbsUsers where uAge between 15 and 20 and uPoint>=10 + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where uName like '小_大%' + +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来, +--并且为列取上对应的中文列名 +select tTitle 标题,rMsg 内容 from bbsTopic where rTime between '2008-9-10 12:00:00' and getdate() and tCount>10 + +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tCount 回复数量,tUID 发帖人编号 from bbsTopic where tTitle like '%!' +\ No newline at end of file +暂无评论 \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/.keep" "b/\347\254\254\345\205\253\346\254\241\350\257\276\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\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery8.1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery8.1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0d6bc61bac5a2697da0d435605dd8a8bad1ca72e --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery8.1.sql" @@ -0,0 +1,67 @@ +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 sum(theNumber)数量总和 from orderItem +select orderId,sum(theNumber)物品的数量,avg(theMoney)平均单价 from orderItem group by orderId +having orderId<3 and avg(theMoney)<10 +select orderId,sum(theNumber) 物品总量,avg(theMoney) 平均单价 from orderItem group by orderId +having avg(theMoney)<10 and sum(theNumber)>50 +select itemType 产品类别,count(*) 分别订购了几次 from orderItem group by itemType +select itemType 产品类别,sum(theNumber) 物品总量,avg(theMoney) 平均单价 from orderItem group by itemType +having sum(theNumber)>100 +select itemName 产品名称 , count(*)订购次数, sum(theNumber) 物品总量,avg(theMoney) 平均单价 from orderItem group by itemName diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery8.2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery8.2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d9d991ff49dbc020cb3ebe2ecedbb49b9c276260 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery8.2.sql" @@ -0,0 +1,131 @@ +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 + +--在论坛数据库中完成以下题目 +select tSID '板块编号',count(*) '发帖总数' from bbsTopic group by tSID +select rTID '主贴编号',count(*) '回帖总数量' from bbsReply group by rTID +select tUID '用户编号',count(*) '主帖的总数' from bbsTopic group by tUID +select tUID '用户编号',sum(tCount) '回复数量总和' from bbsTopic group by tUID +select tSID '板块编号',avg(tCount) '平均回复数量' from bbsTopic group by tSID having avg(tCount)>3 +select top 1 uName '用户名',uSex '性别',uAge '年龄',uPoint '积分' from bbsUsers order by uPoint DESC +select tTitle,rMsg from bbsTopic where tTitle like '%快乐%' or rMsg like '%快乐%' +select * from bbsUsers where uAge>=15 and uAge<=20 and uPoint>10 +select * from bbsUsers where uAge between 15 and 20 and uPoint>10 +select * from bbsUsers where UID =1 +select * from bbsUsers where uName like '小_大' +select tID '主贴编号',tUID '发帖人编号',tSID '版块编号',tTitle '贴子的标题',rMsg '帖子的内容',rTime '发帖时间',tCount '回复数量' from bbsTopic where rTime>'2008-9-10 12:00:00' and tCount>10 +select tUID '发帖人编号',tCount '回复数量' from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..774b20902d0beab2e887e86c8f0f4e254493ba25 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" @@ -0,0 +1,155 @@ +/** +* 作业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 + goto sel +sel: + select * from bbsPoint + select * from bbsReply + select * from bbsSection + select * from bbsTopic + select * from bbsUsers + go +/** +* 后面的就是这次作业的作业3 +**/ + +select tSID,count(*) from bbsTopic group by tSID--在主贴表中统计每个版块的发帖总数 + +select rTID,COUNT(rID)回复数量 from bbsReply group by rTID--在回帖表中统计每个主贴的回帖总数量 + +select tUID,COUNT(tID)发帖数量 from bbsTopic group by tUID--在主贴表中统计每个用户的发的主帖的总数 + +select tUID,sum(tCount) from bbsTopic group by tUID--在主贴表中统计每个用户发的主贴的回复数量总和 + +select tSID,tCount from bbsTopic group by tSID,tCount having AVG(tCount) > 3--在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 + +select uName,uSex,uAge,uPoint from bbsUsers where uPoint = (select max(uPoint) from bbsUsers)--在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 + +select * from bbsTopic where tTitle like '%快乐%' or tMsg like '%快乐%'--在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 + +--在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +/*where*/select * from bbsUsers where (uAge between 15 and 20) and uPoint > 10 +/*子查询*/select * from bbsUsers where uPoint in (select uPoint from bbsUsers where uPoint > 10 and uAge in (select uAge from bbsUsers where uAge between 15 and 20)) +--在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where uName like '小_大%' +--在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select tTitle 标题,tMsg 内容 from bbsTopic where DATEDIFF(HH,'2008-9-10 12:00:00',tTime) < 0 and tCount > 10 +--在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID,tCount from bbsTopic where tTitle like '%!' + diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2f81a8eea07c77b32e929d4985ce3e2d404f7d83 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" @@ -0,0 +1,53 @@ +/** +* 作业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 +--查询所有订单订购的所有物品数量总和 +select SUM(theNumber) 所有订单物品数量和 from orderItem +--查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select SUM(theNumber) 物品数量, AVG(theMoney) 平均单价 from orderItem where orderID < 3 and theMoney < 10 +--查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select COUNT(theNumber) 物品数量,AVG(theMoney) 平均单价 from orderItem where theMoney < 10 and theNumber > 50 +--查询每种类别的产品分别订购了几次 +select itemName,COUNT(theNumber) 订购次数 from orderItem group by itemName +--查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select itemType,SUM(theNumber) 订购总数,AVG(theMoney) 平均单价 from orderItem group by itemType having SUM(theNumber) > 100 +--查询每种产品的订购次数,订购总数量和订购的平均单价 +select itemName,COUNT(itemName) 订购次数,SUM(theNumber) 订购总数,theMoney 平均单价 from orderItem group by itemName,theMoney \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQL 2 \344\275\234\344\270\232.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQL 2 \344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..543834e375eec60415df44bf70d2a578a029a71a --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQL 2 \344\275\234\344\270\232.sql" @@ -0,0 +1,73 @@ +--先创建如图所示表 +use master +go + +create database xiangmu +on( + name='xiangmu', + filename='D:\sql\xiangmu.mdf', + size=8mb, + maxsize=80mb, + filegrowth=10% +) +log on( + name='xiangmu_log', + filename='D:\sql\xiangmu_log.ldf', + size=8mb, + maxsize=80mb, + filegrowth=10% +) +go +use xiangmu +go +--订单表(orders)列为:订单编号(orderId 主键) 订购日期(orderDate) +create table orders +( + orderId int primary key, + orderDate datetime not null, +) + +drop table orders +select * from orders +insert into orders(orderId,orderDate) +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') + + +--订购项目表(orderItem),列为: +--项目编号(ItemiD)订单编号(orderId)产品类别(itemType) +--产品名称(itemName) 订购数量(theNumber) 订购单价(theMoney) + +create table orderItem +( + ItemiD int primary key not null, + orderId nvarchar(20) not null, + itemType nvarchar(5) not null, + itemName nvarchar(5) not null, + theNumber varchar(10) not null, + theMoney varchar(10) not null +) +drop table orderItem +select * from orderItem +insert into orderItem +--1.查询所有订单订购的所有物品数量总和 +select * from orderItem +select COUNT(theNumber)数量总和 from orderItem group by theNumber + +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 + + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 + +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 + +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: + +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 + + diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/\347\273\203\344\271\2403.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/\347\273\203\344\271\2403.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5a33b5892f86f1fe4fc76aedd2380d4ff6847e1f --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/\347\273\203\344\271\2403.sql" @@ -0,0 +1,146 @@ +--create database bbs +--on( +-- name='bbs', +-- filename='D:\sql\bbs.mdf', +-- size=8mb, +-- maxsize=80mb, +-- filegrowth=10% +--) + +--log on( +-- name='bbs_log', +-- filename='D:\sql\bbs_log.ldf', +-- size=8mb, +-- maxsize=80mb, +-- filegrowth=10% +--) + +--go + +--use bbs +--go + +--create table bbsUsers +--( +-- uuID int identity(1,1), +-- uName varchar(10) not null, +-- uSex nvarchar(2) not null, +-- uAge int not null, +-- uPoint int not null + +--) +--drop table 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_Sex 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 +--( +-- ssID int not null, +-- sName varchar(10) not null, +-- sUid int +--) +--alter table bbsSection add constraint PK_bbsSection_ssID primary key(ssID) +--alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUsers(UID) + + +--create table bbsTopic +--( +-- tID int primary key, +-- tUID int foreign key references bbsUsers(uuID), +-- tSID int foreign key references bbsSection(ssID), + +--) +--alter table bbsTopic add tTitle varchar(100) not null +--alter table bbsTopic add tMsg text not null +--alter table bbsTopic add tTime datetime +--alter table bbsTopic add tCount int + + +--create table bbsReply +--( +-- rID int primary key identity(1,1), +-- rUID int foreign key references bbsUsers(uuID), +-- rTID int foreign key references bbsTopic(tID), +-- rMsg text not null, +-- rTime datetime +--) +--insert into bbsUsers(uName,uSex,uAge,uPoint) +--select'小雨点','女',20,0 union +--select'逍遥','男',18,4 union +--select'七年级生','男',19,2 +--select * from bbsUsers +----2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中, +----提示查询部分列:select 列名1,列名2 from 表名 +--select uName,uPoint into bbsPoint from bbsUsers + +--insert into bbsSection(sName, ssID) +--select '技术交流' , 1union +--select '读书世界' ,2 union +--select '生活百科' ,3 union +--select ' 八卦区' ,4 +--select * from bbsSection + +---- 4.向主贴和回帖表中添加几条记录 +---- 发帖人 板块名 帖子标题 +---- 帖子内容 帖时间 回复数量 +--insert into bbsTopic( tid,tUID,tSID,tTitle,tMsg,tTime,tCount ) +--select 1,3, 4,'范跑跑','谁是范跑跑','2008-7-8',1 union +--select 2,2, 1,' .NET','与JAVA的区别是什么呀?','2008-9-1',2 union +--select 3,1, 3,' 今年夏天最流行什么','有谁知道今年夏天最流行','2008-9-10',0 +--select * from bbsTopic + + +--select * from bbsReply +--insert into bbsReply(rTID ,rUID,rMsg,rTime) +--select 1,2,'八嘎牙路是范跑跑','2008-7-8'union +--select 2,1,'没有区别就是最大的区别','2008-7-8'union +--select 3,3,'今年流行上海滩','2008-7-8' +----.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户, +----请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) +--alter table bbsTopic drop FK__bbsTopic__tUID__2D27B809 +--alter table bbsReply drop FK__bbsReply__rUID__30F848ED +--delete bbsUsers where uName ='逍遥' + +----6.因为小雨点发帖较多,将其积分增加10分 +--update bbsUsers set uPoint=uPoint+10 where uName ='小雨点' +----7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) +--select * from bbsSection +--alter table bbsTopic drop FK__bbsTopic__tSID__2E1BDC42 +--delete bbsSection where sName ='生活百科' +----在论坛数据库中完成以下题目 +----主贴表(bbsTopic) 回帖表(bbsReply)版块表(bbsSection) +----1.在主贴表中统计每个版块的发帖总数 +--select * from bbsTopic +--select tSID 板块编号 ,count(tCount)发帖总数 from bbsTopic group by tSID +----2.在回帖表中统计每个主贴的回帖总数量 +--select * from bbsReply +--select rTID 主贴编号,count(rID)回帖总数量 from bbsReply group by rTID +----3.在主贴表中统计每个用户的发的主帖的总数 +--select * from bbsTopic +--select tUID 用户,count(tSID ) 发帖总数 from bbsTopic group by tUID +----4.在主贴表中统计每个用户发的主贴的回复数量总和 +--select tUID 用户,sum(tCount ) 回复数量总和 from bbsTopic group by tUID +----5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +--select tSID 版块 ,avg(tCount)平均 from bbsTopic group by tSID having avg(tCount)>3 +----6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +--select top 1 uName,uSex ,uAge ,uPoint from bbsUsers order by uPoint desc +----7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +--select * from bbsTopic +--select * 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 in( select uAge 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 where uName like '小_点' +----10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来, +----并且为列取上对应的中文列名 +--select * from bbsTopic +--select tTime 时间,tCount 回复数量 ,tTitle 标题 ,tMsg 内容 from bbsTopic where tCount>10 +----11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +--select tUID 发帖人编号 ,tCount 回复数量 from bbsTopic where tTitle like'%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/.keep" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2401.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\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..0dd317fb02441ef9261f4be31e368e73fca6715a --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\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,73 @@ +create database Assignment +on +( + name='Assignment', + filename='E:\SQL\Assignment.mdf' +) +log on +( + name='Assignment_log', + filename='E:\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 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 diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2402.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\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..b4778618b773538e45e5c0b7f94b860d4adca902 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\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,29 @@ +use bbs +go +--在论坛数据库中完成以下题目 +select * from bbsTopic +--1.在主贴表中统计每个版块的发帖总数 +select tSID '板块编号',count(*) '发帖总数' from bbsTopic group by tSID +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID '主贴编号',count(*) '回帖总数' from bbsReply group by rTID +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID '用户编号',count(*) '发帖总数' from bbsTopic group by tUID +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select tUID '用户编号',sum(tCount) '回帖总数' from bbsTopic group by tUID +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select tSID '主贴编号',avg(tCount) '平均回复数量' from bbsTopic group by tSID having avg(tCount)>3 +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select top 1 uName '用户名',uSex '性别',uAge '年龄',uPoint '积分' from bbsUsers order by uPoint DESC +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select tTitle,tMsg from bbsTopic where tTitle like '%快乐%' or tMsg like '%快乐%' +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers where uAge>=15 and uAge<=20 and uPoint>10 +select * from bbsUsers where uAge between 15 and 20 and uPoint>10 +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where UID =1 +select * from bbsUsers where uName like '小_大' +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select tID '主贴编号',tUID '发帖人编号',tSID '版块编号',tTitle '贴子的标题',tMsg '帖子的内容',tTime '发帖时间',tCount '回复数量' +from bbsTopic where tTime>'2008-9-10 12:00:00' and tCount>10 +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID '发帖人编号',tCount '回复数量' from bbsTopic where tTitle like '%!' diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5be28e902ca73f9044e469137ae8f4a510b682cc --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery2.sql" @@ -0,0 +1,161 @@ +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 sum(theNumber) from orderItem +select * from orderItem +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select orderId,sum(theNumber),avg(theMoney) from orderItem where orderId<3 group by orderId,theNumber,theMoney having AVG(theMoney)<10 +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select sum(theNumber), avg(theMoney) from orderItem group by theMoney having AVG(theMoney)<10 and sum(theNumber)>50 +-- +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select itemType 产品名称,count(itemType)次数 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(theNumber)次数,sum(theNumber)总数量,avg(theMoney)平均单价 from orderItem group by itemName + + +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% +) +use bbs +go + +create table bbsUsers +( + uuID 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 primary key (uuID) +alter table bbsUsers add constraint UK unique(uName) +alter table bbsUsers add constraint CK check(uSex='男' or uSex='女' ) +alter table bbsUsers add constraint CK1 check(uAge>=15 or uAge<=60 ) +alter table bbsUsers add constraint CK2 check(upoint>=0) + +create table bbsSection +( + sID int 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(uuID) + + + +create table bbsTopic +( + tID int primary key, + tUID int foreign key references bbsUsers(uuID), + tSID int foreign key references bbsSection(sID), + +) +alter table bbsTopic add tTitle varchar(100) not null +alter table bbsTopic add tMsg text not null +alter table bbsTopic add tTime datetime +alter table bbsTopic add tCount int + +create table bbsReply +( + rID int primary key, + rUID int foreign key references bbsUsers(uuID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女',20,0 union +select'逍遥','男',18,4 union +select'七年级生','男',19,2 +select * from bbsUsers + + +select uName,uPoint into bbsPoint from bbsUsers + +insert into bbsSection(sName, sID) +select '技术交流' , 1union +select '读书世界' ,2 union +select '生活百科' ,3 union +select ' 八卦区' ,4 +select * from bbsSection +insert into bbsTopic( tid,tUID,tSID,tTitle,tMsg,tTime,tCount ) +select 1,3, 4,'范跑跑','谁是范跑跑','2008-7-8',1 union +select 2,2, 1,' .NET','与JAVA的区别是什么呀?','2008-9-1',2 union +select 3,1, 3,' 今年夏天最流行什么','有谁知道今年夏天最流行','2008-9-10',0 +select * from bbsTopic \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/.keep" "b/\347\254\254\345\205\253\346\254\241\350\257\276\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\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery2\350\256\272\345\235\233sql.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery2\350\256\272\345\235\233sql.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e96c52ff5e8865ffdd5485358cb341f3b821a523 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery2\350\256\272\345\235\233sql.sql" @@ -0,0 +1,51 @@ +use master +go + +create database forum +on +( + + name='forum', + fileName='E:\数据库文件\数据库跟目录文件\论坛\forum.mdf', + size=5MB, + Maxsize=100MB, + filegrowth=10MB +) +log on +( + name='forum_log', + fileName='E:\数据库文件\数据库跟目录文件\论坛\forum_log.ldf', + size=5MB, + Maxsize=100MB, + filegrowth=10MB +) +go + +use forum +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) + +drop table bbsSection +drop table bbsUsers diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery\350\256\242\345\215\225\351\241\271\347\233\256.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery\350\256\242\345\215\225\351\241\271\347\233\256.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4fb53523b2bbb345ea92ce667823bbf95b2c0dd7 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery\350\256\242\345\215\225\351\241\271\347\233\256.sql" @@ -0,0 +1,86 @@ +--先创建如图所示表 +use master +go + +create database management +on +( + name='management', + filename='E:\数据库文件\数据库跟目录文件\订单管理\management.mdf', + size = 5mb, + maxsize =100mb, + filegrowth =10mb +) +log on +( + name='management_log', + filename='E:\数据库文件\数据库跟目录文件\订单管理\management_log.ldf', + size = 5mb, + maxsize =100mb, + filegrowth =10mb +) +go + +use management +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 +) +select*from orders +select*from orderItem +insert into orders (orderDate) 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(theNumber) from orderItem +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select theNumber 订购数量 ,orderId 订单编号, avg(theMoney) from orderItem group by theNumber,orderId having orderId<3 and avg(theMoney)<10 + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select theNumber 订购数量, avg(theMoney) 平均单价 from orderItem group by theNumber having avg(theMoney)<10 and theNumber>50 +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select itemType 产品, count(theNumber)订购次数 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(theNumber)订购次数, sum(theNumber)总数量,avg(theMoney)平均单价 from orderItem group by itemName \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a626ee953fbd54e937d6e4286ac796cf45194d1d --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery1.sql" @@ -0,0 +1,66 @@ +create database wdnmd +on +( +name='wdnmd', +filename='D:\wdnmd.mdf' +) +log on +( +name='wdnmd_log', +filename='D:\wdnmd_log.ldf' +) +go +use wdnmd +go + +create table orders +( +orderID int primary key, +orderDate date +) + +create table orderItem +( +ItemID int identity(1,1) primary key, +orderId int , +itemType nchar(5), +itemName nchar(5), +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), + (2,'文具','尺',10,1), + (3,'体育用品','篮球',1,56), + (4,'文具','笔',36,2), + (5,'文具','固体胶',20,3), + (6,'日常用品','透明胶',2,1), + (7,'体育用品','羽毛球',20,3), + (8,'文具','订书机',20,3), + (9,'文具','订书针',10,3), + (10,'文具','裁纸刀',5,5), + (11,'文具','笔',20,2), + (12,'文具','信纸',50,1), + (13,'日常用品','毛巾',4,5), + (14,'日常用品','透明胶',30,1), + (15,'体育用品','羽毛球',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)>100 + --6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: + --产品名称 订购次数 总数量 平均单价 + --笔 3 120 2 + select itemName'产品名称',count(*)'订购次数',sum(theNumber)'订购总量',avg(theMoney)'平均单价' from orderItem group by itemName \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..45159384e0cd52674834d97c755856a52e4739c0 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" @@ -0,0 +1,94 @@ +use master +go + +create database Mar22nd1 +on +( + name='Mar22nd1_mdf', + filename='D:\SQLMar\Mar22nd1_mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +log on +( + name='Mar22nd1_ldf', + filename='D:\SQLMar\Mar22nd1_ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) + +use Mar22nd1 +go + +--先创建如图所示表 + +--订单表(orders)列为:订单编号(orderId 主键) 订购日期(orderDate) + +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(1,1), + orderID int references orders(orderId), + itemType nvarchar(10) not null, + itemName nvarchar(10) not null, + theNumber int not null, + theMoney int not null +) +select * from orders + +--插入数据 +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-3-10 00:00:00.000') +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) + +--1.查询所有订单订购的所有物品数量总和 +select sum(theNumber) from orderltem +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select orderID 订单编号,theNumber 物品的数量,AVG(theMoney)平均单价 +from orderltem group by orderID, theNumber having orderID<3 and AVG(theMoney)<10 + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select orderID 订单编号,sum(theNumber) 物品数量 , AVG(theMoney) 平均单价 +from orderltem group by orderID having AVG(theMoney)<10 and sum(theNumber)>50 +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select itemType 类别,count(*)订购次数 from orderltem group by itemType + +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select itemType 类别, sum(theNumber) 订购总数量,avg(theMoney) 平均单价 from orderltem group by itemType having sum(theNumber)>=100 + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 + +select itemname 产品名称,count(*) 订购次数,sum(theNumber) 订购总数量,avg(theMoney) 平均单价 +from orderltem group by itemname2 \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery2.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..abf19c046eb1a782c641581b9b1c5a40edcb0a03 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery2.sql" @@ -0,0 +1,157 @@ +use master +go + +create database bbs +on +( + name='bbs', + filename='D:\SQLMar\bbs.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10% + +) +, +( + name='bbs2_DATA', + filename='D:\SQLMar\bbs2_DATA.Ndf', + size=1MB, + maxsize=100MB, + filegrowth=10% +) +log on +( + name='bbs_ldf', + filename='D:\SQLMar\bbs_log.ldf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) + +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 + +use bbs +go + +select * from bbsReply --回帖表 +select * from bbsSection --版块表 +select * from bbsTopic --主帖表 +select * from bbsUsers --用户信息表 + +--1.在主贴表中统计每个版块的发帖总数 +select tSID 版块,count(tID) 数量 from bbsTopic group by tSID + +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID 主贴,count(rID) 回帖总数量 from bbsReply group by rTID + +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID 用户,count(tID) 主帖的总数 from bbsTopic group by tUID + +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select tUID 用户,tID 主帖,sum(tCount) 回复数量总和 from bbsTopic group by tUID,tID + +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +select tUID 用户,tID 主帖,avg(tCount) 回复数量总和 from bbsTopic group by tUID,tID having avg(tCount)>3 + +--6.在用户信息表中查询出积分最高的用户的用户名,性别,年龄和积分 +select uName 用户名, uSex 性别 , uAge 年龄, uPoint 积分 from bbsUsers where uPoint=(select max(uPoint) from bbsUsers) +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic where rMsg like '%快乐%' or tTitle like '%快乐%' + +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers where uAge>=15 and uAge<=20 and uPoint>=10 +select * from bbsUsers where uAge between 15 and 20 and uPoint>=10 + +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where uName like '小_大%' + +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来, +--并且为列取上对应的中文列名 +select tTitle 标题,rMsg 内容 from bbsTopic where rTime between '2008-9-10 12:00:00' and getdate() and tCount>10 + +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tCount 回复数量,tUID 发帖人编号 from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\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\205\253\346\254\241\350\257\276\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..2dfc8673a8a2d9e47a98c3b32740f57a0ef9df41 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\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,70 @@ +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 orders +select * from orderItem + +--1.查询所有订单订购的所有物品数量总和 +select sum(theNumber)订购数量总和 from orderItem +--.查询订单编号小于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,avg(theMoney)平均单价,sum(theNumber)数量和 from orderItem group by orderId +having avg(theMoney)<10 and sum(theNumber)>50 +--4.查询每种类别的产品分别订购了几次,例如: +--文具 9 +--体育用品 3 +--日常用品 3 +select * from orderItem--查询orderItem表 +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 \ No newline at end of file diff --git "a/\347\254\254\345\205\253\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\344\275\234\344\270\2323.sql" "b/\347\254\254\345\205\253\346\254\241\350\257\276\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..08b16fa8bcd582070b869946e23069ca883652fe --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\350\257\276\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,159 @@ +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 tSID 主贴编号, count(tCount)发帖总数 from bbsTopic group by tSID +--2.在回帖表中统计每个主贴的回帖总数量 +select rTID 主贴编号, count(rID)回帖总数量 from bbsReply group by rTID +--3.在主贴表中统计每个用户的发的主帖的总数 +select tUID 发帖人编号, count(tSID) 发帖总次数 from bbsTopic group by tUID +--4.在主贴表中统计每个用户发的主贴的回复数量总和 +select tUID 发帖人编号, SUM(tCount) 回复数量总和 from bbsTopic group by tUID +--5.在主贴表中查询每个版块的主贴的平均回复数量大于3的版块的平均回复数量 +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 +--7.在主贴表中(bbsTopic)中将帖子的内容或标题中有“快乐”两字的记录查询出来 +select * from bbsTopic +select * from bbsTopic where tTitle like '%快乐' and tMsg like '%快乐' +--8.在用户信息表(bbsUsers)中将用户年龄在15-20之间并且积分在10分以上的优秀用户查询出来(用多种方法实现) +select * from bbsUsers +select * from bbsUsers where uAge>=15 and uAge<=20 and uPoint>=10 +--9.在用户信息表(bbsUsers)中将用户名的第一个字为“小”,第三字为“大”的用户信息查询出来 +select * from bbsUsers where uName like '小__' or uName like '__大' +--10.在主贴表(bbsTopic)中将在2008-9-10 12:00:00 以后发的并且回复数量在10以上的帖子的标题和内容查询出来,并且为列取上对应的中文列名 +select * from bbsTopic +select tTime 时间,tCount 回复数量,tTitle 标题,tMsg 内容 from bbsTopic where tCount>10 +--11.在主贴表(bbsTopic)中将帖子的标题是以‘!’结尾的帖子的发帖人编号和回复数量查询出来 +select tUID 发帖人编号,tCount 回复数量 from bbsTopic where tTitle like '%!' \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7fc7a120d585f510962e863a56876d9423f5b3a7 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,131 @@ +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 + +create table bbsUsers +( +UID int primary key 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 primary key (uuID) +alter table bbsUsers add constraint UK unique(uName) +alter table bbsUsers add constraint CK check(uSex='男' or uSex='女' ) +alter table bbsUsers add constraint CK1 check(uAge>=15 or uAge<=60 ) +alter table bbsUsers add constraint CK2 check(upoint>=0) + +insert into bbsUsers(uName,uSex,uAge,uPoint) +select '小雨点', '女', 20, 0 union +select '逍遥' , '男' ,18,4 union +select '七年级生' ,'男' ,19, 2 + +select * from bbsUsers + +create table bbsSection +( +sID int primary key 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) + +insert into bbsSection(sName,sUid) values('技术交流' ,1), + ('读书世界',3), + ('生活百科',1), + ('八卦区',3) + + + +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 +insert into bbsTopic(tuID,tSID,tTitle ,tMsg,tTime,tCount) + values(1, 2,'范跑跑','谁是范跑跑','2008-7-8','1'), + (3, 4,'.NET ',' 与JAVA的区别是什么呀? ','2008-9-1 ','2'), + (5, 6,'今年夏天最流行什么 ',' 有谁知道今年夏天最流行','2008-9-10','0') + + +select uName, upoint into bbsPoint from bbsUsers + +create table bbsReply +( +rID int primary key, +rUID int foreign key references bbsUsers(UID), +rTID int foreign key references bbsTopic(tID ), +rMsg text not null, +rTime datetime , + + +) +select * from bbsReply +insert into bbsReply(rUID,rTID ,rMsg,rTime) values(1,2,'二班长的最丑的就是范跑跑',0123), + (4,3,'我挂科的不知道这么深奥的问题',0123), + (5,6,'这个夏天最流行的当然是黑丝啊破洞的那种',0123) + +delete bbsUsers where uName='逍遥' +alter table bbsReply drop FK__bbsReply__rUID__4AB81AF0 +alter table bbsTopic drop FK__bbsTopic__tUID__46E78A0C + +update bbsUsers set upoint=10 where uName='小雨点' + +alter table bbsTopic drop FK__bbsTopic__tSID__47DBAE45 +delete bbsSection where sName='生活百科' + +delete bbsReply + + + + + + + + + + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/~vsC7B.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/~vsC7B.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e89f7aac944936839eb5b75eda174baab0339c96 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/~vsC7B.sql" @@ -0,0 +1,244 @@ +use TestDB + + +create database TestDB + +on + +( + + name='TestDB1', + + filename='E:\TestDB1.mdf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +),--第二个主数据文件 +( + + name='TestDB2', + + filename='E:\TestDB2.mdf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) + + + +log on + +( + + name='TestDB_log', + + filename='E:\TestDB_log.ldf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) + +go +select * from typeInfo + +create table typeInfo +( +typeId int not null primary key identity(1,1), +tyoeName varchar(10) not null , +) +create table loginlnfo +( +Loginld int not null primary key identity(1,1), +LoginName varchar(10) not null unique, +LoginPwd varchar(20) not null default(123456), +LoginSex varchar(2), +LoginBir int , +LoginType int, +) + + + +create database company +on +( + name='company', + + filename='E:\company.mdf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) + + +log on + +( + + name='company_log', + + filename='E:\company_log.ldf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) + +go + +create table sectionlnfo + +( +sectionID int primary key not null, +sectionName varchar(10) not null, +) +create table userinfo +( +serNo int primary key not null , +userName varchar(10) not null unique check(userName>4), +userSex varchar(2) not null default('男') check(userSex ='男'or userSex='女'), +userAge int not null check(userAge >=1 and userAge <=100), +userAddress varchar(50) check(userAddress='贵州'), +userSection int foreign key references sectionlnfo(sectionID) +) +create table worklnfo +( +workld int primary key not null , +userld int foreign key references userinfo (serNo) not null, +workTime datetime not null , +workDescription varchar(50) check(workDescription ='迟到' or workDescription = '早退'or workDescription ='矿工' or workDescription ='病假' or workDescription ='事假' ), + +) +--学校数据库 +create database studentlnfo +on +( + name='studentlnfo', + + filename='E:\studentlnfo.mdf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + ) + + +log on + +( + + name='studentlnfo_log', + + filename='E:\studentlnfo_log.ldf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) + +go +--班级表 +create table classlnfo +( +classid int primary key not null , + classname varchar(5) not null unique, + classremark ntext +) +--学生信息表 +create table information +( +infNumber int primary key not null, +infName varchar(10)check(len(infName)>2) unique, +infSex varchar(2) not null default('男') check(infSex ='男'or infSex='女'), +infAge int check(infAge>=15 and infAge<=40) not null, +infHome nvarchar(50) default('贵州毕节'), +infclass int references classlnfo(classid) +) +--课程表 +create table course +(couNumber int primary key not null, +couName nchar not null unique, +couDescribe text, +) +--成绩表 +create table mark +( +marNumber int primary key not null, +marclass int not null, +marCourse int not null, +marGrade int check(marGrade>=0 and marGrade<=100), +) +--房屋数据库 +create database house +on +( + name='house', + + filename='E:\house.mdf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + ) + + +log on + +( + + name='house_log', + + filename='E:\house_log.ldf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) + +go +create table tblUser +( + userID int primary key not null, + urtName nvarchar(10) default ('梦林'), + userCall varchar(11) default('123456'), + + ) + create table tblQx + ( + qxId int primary key not null, + qxName nvarchar check (qxName ='武昌' and qxName ='汉阳' and qxName ='汉口'), + ) + create table tbHouseType + ( + typeID int primary key not null, + typName nvarchar check(typName ='别墅' and typName ='普通住宅' and typName ='平方' and typName ='地下室'), + ) + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\347\273\203\344\271\240\344\270\200.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\347\273\203\344\271\240\344\270\200.sql" new file mode 100644 index 0000000000000000000000000000000000000000..22fc985c4461eb7909f48a61075946a2fc986542 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\347\273\203\344\271\240\344\270\200.sql" @@ -0,0 +1,157 @@ +use master +go + +create database TestDB +on +( + name=TestDB, + filename='D:\SQL代码\TestDB.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) +log on +( + name=TestDB_ldf, + filename='D:\SQL代码\TestDB.ldf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) + +use TestDB +go + +create table typelnfo +( + typeld int primary key identity(1,1), + typeName varchar(10) not null, +) + +create table loginInfo +( + LoginId int primary key identity(1,1), + LoginName nvarchar(10) not null unique , + LoginPwd varchar(20) default(123456), + LoginSex nvarchar(1) check(LoginSex='男' or LoginSex='女'), + LoginBir date , + LoginSort text not null +) + +create database Company +on( + name=Company, + filename='D:\SQL代码\Company.mdf', + size=5MB, + maxsize=5MB, + filegrowth=10% +) + log on( + name=Company_ldf, + filename='D:\SQL代码\Company.ldf', + size=5MB, + maxsize=5MB, + filegrowth=10% +) + +use Company +go + +create table SectionInfo +( + SectionID int primary key identity(1,1) not null, + SectionNamme varchar(10) not null, +) + +create table UserInfo +( + UserNo int identity(1,1) primary key not null, + UserName varchar(10) unique not null check(len(UserName)>=4), + UserSex varchar not null check(UserSex='男' or Usersex='女'), + UserAge int not null check(UserAge>=1 or UserAge<=100), + UserAddress varchar(50) default('湖北'), + UserSection int foreign key references SectionInfo(SectionID), +) + +create table WorkInfo +( + workld int identity(1,1) primary key not null, + userId int foreign key references UserInfo(UserNo), + WorkTime datetime Not null , + WorkDescription varchar(40) not null check(WorkDescription='迟到' or WorkDescription='早退' or WorkDescription='旷课' or WorkDescription='病假' or WorkDescription='事假') +) + +create database StudentInfo +go + +use StudentInfo +go + +create table ClassInfo +( + ClassID int primary key identity(1,1), + ClassName nvarchar(10) not null unique, + ClassTime time not null, + ClassDes text +) + +create table StudentInfo +( + StuID int primary key identity(1,1), + StuName nvarchar(10) not null unique check(len(StuName)>2), + StuSex nvarchar(2) not null default('男') check(StuSex='男' or StuSex='女'), + StuAdress varchar(12) default('湖北武汉'), + ClassID int foreign key references ClassInfo(ClassID) +) + +create table CourseInfo +( + CourseID int primary key identity(1,1), + CourseName nvarchar(10) not null unique, + CourseDes text +) + +create table Score +( + ScoreID int primary key identity(1,1), + ScoreStuID int foreign key references StudentInfo(StuID), + ScoreClassID int foreign key references ClassInfo(ClassID), + Score int check(Score>=1 and Score<=100) not null +) + +create database HomeInfo +go + +use HomeInfo +go + +create table TbIUser +( + UserID int primary key identity(1,1), + UserName nvarchar(10) not null, + UserTel nvarchar(11) not null check(len(UserTel)=11) +) + +create table tblHouseType +( + TypelD int primary key identity(1,1), + Typname nvarchar(10) not null check(Typname='地下室' or Typname='平房' or Typname='普通住宅' or Typname='别墅') +) + +create table TbIQx +( + QxID int primary key identity(1,1), + Qxname nvarchar(10) not null check(Qxname='武昌' or Qxname='汉阳' or Qxname='汉口') +) + +create table TblHouseInfo +( + HomeID int primary key identity(1,1), + HomeDesc nvarchar(10), + UserID int foreign key references TbIUser(UserID), + Homezj int not null, + HomeShi int not null, + HomeTing int not null, + TypeID int foreign key references tblHouseType(TypelD), + QxID int foreign key references TbIQx(Qxid) +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\347\273\203\344\271\240\344\272\214.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\347\273\203\344\271\240\344\272\214.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4336009f54c1c7545791cbff49d748260a1d4ea6 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/\347\273\203\344\271\240\344\272\214.sql" @@ -0,0 +1,116 @@ +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% +) + +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 + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fd49f2919fe1d5e8b07fb4814920a710888fdfcb --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery1.sql" @@ -0,0 +1,218 @@ +use master +go + +create database TestDB +on +( + name='TestDB', + filename='D:\SQL作业\SQL作业6\TestDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='TestDB_log', + filename='D:\SQL作业\SQL作业6\TestDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use TestDB +go +create table typeInfo +( + typeId int primary key identity(1,1), + typeName varchar(10) not null +) +go + +use TestDB +create table loginInfo +( + LoginId int primary key identity(1,1), + LoginName char(10) unique(LoginName) not null, + LoginPwd char(20) default(123456) not null, + Sex char(2) not null, + Birthday char(12) not null, + Category nchar(15) not null +) +go + + +use master +create database company +on +( + name='company', + filename='D:\SQL作业\SQL作业6\company.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='company_log', + filename='D:\SQL作业\SQL作业6\company_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use company +create table sectionInfo +( + sectionID int primary key identity(1,1), + sectionName varchar(10) not null +) +go + +use company +create table userInfo +( + userNo int primary key identity(1,1) not null, + userName varchar(10) unique(userName) check(len(userName)>=4) not null, + userSex varchar(2) check(userSex='男' or userSex='女') not null, + userAge int check(userAge>=1 and userAge<=100) not null, + userAddress varchar(50) default('湖北'), + userSection int constraint FK_sectionInfo_sectionID references sectionInfo(sectionID) +) +go + +use company +create table workInfo +( + workId int primary key identity(1,1) not null, + userId int constraint FK_userInfo_userNo references userInfo(userNo) not null, + workTime datetime not null, + workDescription varchar(40) check(workDescription='迟到' or workDescription='早退' or workDescription='旷工' or workDescription='病假' or workDescription='事假') +) +go + + +use master +create database manage +on +( + name='manage', + filename='D:\SQL作业\SQL作业6\manage.mdf', + size=5mb, + maxsize=5mb, + filegrowth=10% +) + +log on +( + name='manage_log', + filename='D:\SQL作业\SQL作业6\manage_log.ldf', + size=5mb, + maxsize=5mb, + filegrowth=10% +) +go + +use manage +create table classInfo +( + classid int primary key identity(1,1), + classname char(15) unique(classname) not null, + opentime datetime not null, + classdescribe text +) +go + +use manage +create table stuinfo +( + stuid int primary key identity(1,1), + stuname char(8) unique(stuname) check(len(stuname)>2), + stusex char(2) default('男') check(stusex='男' or stusex='女') not null, + stuage char(5) check(len(stuage)>=15 and len(stuage)<=40) not null, + homeadress nchar(30) default('湖北武汉'), + classid int +) +go + +use manage +create table CourseInfo +( + CourseID int primary key identity(1,1), + CourseName nchar(10) unique(CourseName) not null, + CourseDes text +) +go + +use manage +create table ScoreInfo +( + ScoreID int primary key identity(1,1), + ScoreStuID int not null, + ScoreCourID int not null, + Score char(5) check(score>=1 and score<=100) +) +go + + +use master +create database Home +on +( + name='Home', + filename='D:\SQL作业\SQL作业6\Home.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='Home_log', + filename='D:\SQL作业\SQL作业6\Home_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go + +use Home +create table tblUser +( + userId int primary key identity(1,1), + userName nchar(10) not null, + userTel char(20) +) +go + +use Home +create table tblHouseType +( + typeId int primary key identity(1,1), + typName nchar(10) check(typName='别墅' or typName='普通住宅' or typName='平房' or typName='地下室') not null +) +go + +use Home +create table tblQx +( + qxId int primary key identity(1,1), + qxName char(20) check(qxName='武昌' or qxName='汉阳' or qxName='汉口') not null +) +go + +use Home +create table tblHouseInfo +( + id int primary key identity(1,1), + userId int constraint FK_tblUser_userId references tblUser(userId), + zj money not null, + shi varchar(5), + ting varchar(5), + typeId int constraint FK_tblHouseType_typeId references tblHouseType(typeId), + qxId int constraint FK_tblQx_qxId references tblQx(qxId) +) +go \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cb26e084914b3c8b144dc9bf9e815635f9e63067 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery2.sql" @@ -0,0 +1,121 @@ +use master +go +create database bbs +on +( + name='bbs', + filename='D:\SQL作业\SQL作业6\2\bbs.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='bbs_log', + filename='D:\SQL作业\SQL作业6\2\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 diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\275.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\275.sql" new file mode 100644 index 0000000000000000000000000000000000000000..578c82fe94b27fc5377a08561fc99f78234c52e4 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\275.sql" @@ -0,0 +1,89 @@ +create database TestDB +go +use TestDB +go +create table typeInfo +( + typeId int primary key identity(1,1), + typeName nvarchar(10) not null, +) +create table loginInfo +( + LoginId int primary key identity(1,1), + LoginName text not null unique, + LoginPwd text not null default('1,2,3,4,5,6'), + LoginSex char(2), + LoginSr time, + LoginHy char, +) +create table sectionInfo +( + sectionID int identity(1,1) primary key, + sectionName varchar(10) not null, +) +create table userInfo +( + userNo int identity(1,1) primary key not null, + userName varchar(10) unique not null, + userSex varchar(2) not null check(userSex='男'or userSex='女'), + userAge int not null check(userAge>=1 and userAge<=100), + userAddress varchar(50) default('湖北'), + userSection int foreign key(userSection) references sectionInfo(sectionID) , +) +create table workInfo +( + workId int identity(1,1) primary key not null, + userId int foreign key(userId) references userInfo(userNo) not null, + workTime datetime not null, + workDescription varchar(40) not null check(workDescription='迟到' or workDescription='早退' or workDescription='旷工' or workDescription='病假' or workDescription='事假'), +) +create database StudenInfo +go +use StudenInfo +go +create table Classinfo +( + classid int primary key identity(1,1), + classname int not null unique, + classtime datetime not null, + classms text not null, +) +create table Stuinfo +( + StuId int primary key identity(1,1), + Stuname int check(Stuname>2) not null, + Stusex char(2) default('男') check(Stusex='男'or Stusex='女') not null, + Stuaeg int check(Stuaeg>=15 and Stuaeg<=40) not null, + StuDiZhi varchar default('湖北武汉'), +) +create table kecheng +( + KCid int primary key identity(1,1), + KCname nchar not null unique, + KCms text, +) +create table CJinfo +( + CJid int primary key identity (1,1), + XSid int not null, + KCid int not null, + Cj int check(CJ>=0 and CJ<=100), +) +create table tblUser +( + userId int primary key identity (1,1), + userName varchar not null, + userTel int not null, +) +create table tblHouseType +( + typeId int not null, + typName varchar not null, + tblQx varchar not null, + qxId int not null, + qxName varchar, +) +create table tblHouseInfo +( + id int primary key identity(1,1), +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\2751.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\2751.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2a06241dea36b6747a1ba5b8f436fbaba19809fa --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\2751.sql" @@ -0,0 +1,101 @@ +create database bbs +go +use bbs +go + +--用户信息表 +create table bbsUser +( + UID int 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), +) +--添加约束 +--主键约束 +alter table bbsUser add constraint PK_bbsUser_UID primary key (UID) +--唯一约束 +alter table bbsUser add constraint UK_bbsUser_uName unique(uName) + +--版块表 +create table bbsSection +( + sID int identity(1,1) primary key, + sName varchar(10) not null, + sUid int, +) +--外键 引用用户信息表的用户编号 +alter table bbsSection add constraint FK_bbsSection_sUid foreign key(sUid) references bbsUser(UID) + +--主贴表 +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, +) +--添加约束 +--外键约束 引用用户信息表的用户编号 +alter table bbsTopic add constraint FK_bbsTopic_tUID foreign key(tUID) references bbsUser(UID) +--外键约束 引用板块表的版块编号 +alter table bbsTopic add constraint FK foreign key (tSID) references bbsSection(sID) + +--回贴表 +create table bbsReply +( + rID int primary key identity(1,1), + rUID int, + rTID int, + rMsg text not null, + rTime datetime, +) +--外键约束 引用用户信息表的用户编号 +alter table bbsReply add constraint FK_bbsReply_rUID foreign key (rUID) references bbsUser(UID) + +--外键约束 引用主贴表的主贴编号 +alter table bbsReply add constraint FK_bbsReply_rTID foreign key (rTID) references bbsTopic(tID) + +--现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +--小雨点 女 20 0 +--逍遥 男 18 4 +--七年级生 男 19 2 + +insert into bbsUser(uName,uSex,uAge,uPoint) values ('小雨点','女',20,0),('逍遥','男',18,4),('七年级','男',19,2) + +--2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 +--插入多行: +select uName,uPoint into bbsPoint from bbsUsers + +--给论坛开设4个板块 +--名称 版主名 +--技术交流 小雨点 +--读书世界 七年级生 +--生活百科 小雨点 +--八卦区 七年级生 + +insert into bbsSection(sName,sUid) values ('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + +--4.向主贴和回帖表中添加几条记录 +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values(2,4,'范跑跑','谁是范跑跑',2008-7-8,1) ,(3,2,'.NET','与Java的区别是什么?',2008-9-1,2),(1,4,'今年夏天流行什么','有谁知道今年夏天最流行什么?',2008-9-10,0) +insert into bbsReply(rMsg,rTime) values('逃跑的老师',2008-7-8),('??',2008-7-9),('流行看美女',2008-7-10) +select * from bbsUser +--5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户, +--请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) +delete from bbsTopic where tUID=2 +delete from bbsReply where rUID=2 +delete from bbsReply where rID=1 +delete from bbsUsers where uName='逍遥' + +--6.因为小雨点发帖较多,将其积分增加10分 +update bbsUsers set upoint=30 where uName='小雨点' +--7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) +delete from bbsTopic where tSID=3 +delete from bbsSection where sName='生活百科' + +-- 8.因回帖积累太多,现需要将所有的回帖删除 +delete from bbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..31147235c16cf39276a71f71a4ecee78c6d38a5f --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery1.sql" @@ -0,0 +1,142 @@ +锘縰se master +go + +create database TestDB +on +( + name='TestDB', + filename='D:\鏁版嵁搴揬TestDB.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +log on +( + name='TestDB_log', + filename='D:\鏁版嵁搴揬TestDB_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) + +use TestDB +go + +create table typeInfo +( + typeId int primary key identity(1,1), + typeName varchar(10) not null +) + +create table loginInfo +( + LoginId int primary key identity(1,1), + LoginName char(10) not null unique, + LoginPwd char(20) default(123456) not null, + Sex char(1), + Birthday char(10), + MemType varchar(10) +) + + + + + + +create database company +on +( + name='company', + filename='D:\鏁版嵁搴揬company.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +log on +( + name='company_log', + filename='D:\鏁版嵁搴揬company_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) + +use company +go + +create table sectionInfo +( + sectionID int primary key identity(1,1), + sectionName varchar(10) not null +) + +create table userInfo +( + userNo int primary key identity(1,1) not null, + userName varchar(10) not null unique check(len(userName)>4), + userSex varchar(2) check(userSex='鐢' or userSex='濂') not null, + userAge int not null check(userAge>=1 and userAge<=100),--鑼冨洿鍦1-100涔嬮棿 + userAddress varchar(50) default('婀栧寳'), + userSection int references sectionInfo(sectionID) +) + +create table workInfo +( + workId int primary key not null, + userId int references userInfo(userNo) not null, + workTime datetime not null, + workDescription varchar(40) check(workDescription='鏃╅'or + workDescription='鏃峰伐'or workDescription='鐥呭亣' or workDescription='浜嬪亣') not null +) + +create database HomeInfo +on +( + name='HomeInfo', + filename='D:\鏁版嵁搴揬HomeInfo.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +log on +( + name='HomeInfo_log', + filename='D:\鏁版嵁搴揬HomeInfo_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) + +use HomeInfo +go + +create table TbIUser +( + UserID int primary key identity(1,1), + UserName nvarchar(10) not null, + UserTel nvarchar(11) not null check(len(UserTel)=11) +) + +create table tblHouseType +( + TypelD int primary key identity(1,1), + Typname nvarchar(10) not null check(Typname='' or Typname='平' or Typname='通住宅' or Typname='') +) + +create table TbIQx +( + QxID int primary key identity(1,1), + Qxname nvarchar(10) not null check(Qxname='' or Qxname='' or Qxname='') +) + +create table TblHouseInfo +( + HomeID int primary key identity(1,1), + HomeDesc nvarchar(10), + UserID int foreign key references TbIUser(UserID), + Homezj int not null, + HomeShi int not null, + HomeTing int not null, + TypeID int foreign key references tblHouseType(TypelD), + QxID int foreign key references TbIQx(Qxid) +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d9bb9d711833ce1a606127f301dbb80a643113b7 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\347\204\225\344\270\232/SQLQuery2.sql" @@ -0,0 +1,158 @@ +use master +go + +create database Xsglxt--学生管理系统 +on( + name='Xsglxt', + filename='D:\数据库\Xsglxt.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) + +log on +( + name='Xsglxt_log', + filename='D:\数据库\Xsglxt_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) + +use Xsglxt +go + +create table ClassInfo +( + ClassNum char(10) primary key not null, + ClassName char(10)unique not null, + Opentime datetime not null, + ClassRemark text +) + +create table StuInfo +( + StuName varchar(10) unique not null check(len(StuName)>2), + StuSex varchar(2) default('男') check(StuSex='男' or StuSex='女') not null, + StuAge varchar(4) not null check(StuAge>=15 and StuAge<=40), + homeaddress nvarchar(20) default('湖北武汉'), + szdClassid char(10) not null +) + +create table CourseInfo +( + LoginId int primary key identity(1,1), + CurriculumName varchar(10) unique not null,--unique 唯一约束,不能重复 + Description text +) + +create table StuGrade +( + Recordnumber int primary key identity(1,1), + Student int not null, + CourseNumber varchar(10) not null, + grade int check(grade>=0 and grade<=100) +) + +create database bbs +on( + name='bbs', + filename='E:\数据库\bbs.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='E:\数据库\bbs_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) + +use bbs +go + +create table bbsUser +( + 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) +) + +create table bbsSection +( + sID int primary key identity(1,1), + sName varchar(10) not null, + sUid int references bbsUser(UID) +) + +create table bbsTopic +( + tID int primary key identity(1,1), + tUID int references bbsUser(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 bbsUser(UID), + rTID int 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 \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery5.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..425314a3ef5b21520fb3ac48a1a6dccb87c9f693 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery5.sql" @@ -0,0 +1,190 @@ +create database TestDB +on +( +name='TestDB', +filename='D:\sql.mdf', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) +log on +( +name='TestDB_log', +filename='D:\sql.ldf', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) +go + +use TestDB +go + +create table typeInfo +( +typeld int primary key identity(1,1), +typeName varchar(10) not null, +) + +create table loginInfo +( +LoginId int primary key identity(1,1), +LoginName varchar(10)unique not null, +LoginPwd varchar(20) default('123456') not null, +LoginSex char(2), +LoginBirth varchar(20), +LoginType varchar(20) +) + + + + +create database company +on +( +name='company', +filename='D:\sql1.mdf', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) +log on +( +name='company_log', +filename='D:\sql1.ldf', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) +go + +use company +go + +create table sectionInfo +( +sectionID int primary key, +sectionName varchar(10) unique not null +) + +create table userInfo +( +userNo int primary key not null, +userName varchar(10) unique not null, +userSex varchar(2) check(userSex='男'or userSex='女') not null, +userAge int check(userAge>=1 and userAge<=100) not null, +userAddress varchar(50) default('湖北'), +userSection int foreign key references sectionInfo(sectionID) +) + +create table workInfo +( +workId int primary key not null, +userId int foreign key references userInfo(userNo) not null, +workTime datetime not null, +workDescription varchar(40) check(workDescription='迟到' or workDescription='早退' or workDescription='旷工' or workDescription='病假' or workDescription='事假') not null +) + +create database Student +on +( +name='Student', +filename='D:\sql2.mdf', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) +log on +( +name='Student_log', +filename='D:\sql2.ldf', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) +go + +use Student +go + +create table class +( +classid int primary key identity(1,1), +classtime datetime not null, +) + +create table student +( +stuName varchar(20) unique, +stuSex char(2) default('男') check(stuSex='男' or stuSex='女') not null, +stuAge int check(stuAge>=15 and stuAge<=40) not null, +stuAddress varchar(20) default('湖北武汉'), +classNO int +) + +create table course +( +courseID int primary key , +courseName varchar(20) unique not null, +coursedescribe varchar(20) +) + +create table score +( +scoreID int primary key, +stuID int not null, +courseID int not null, +score int check(score>=0 and score<=100) +) + + + + +create database tbl +on +( +name='tbl', +filename='D:\sql3.mdf', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) +log on +( +name='tbl_log', +filename='D:\sql3.ldf', +size=5mb, +maxsize=50mb, +filegrowth=10mb +) +go + +use tbl +go + +create table tblUser +( +userId int primary key, +userName varchar(20) not null, +userTel int unique not null, +) + +create table tblHouseType +( +typeld money check(typeld>=0 and typeld<=2000000) not null, +typName varchar(20) not null +) + +create table tblQX +( +qxld varchar(20) not null, +qxName varchar(20) unique not null, +) + +create table tblHouseInfo +( +id int primary key, +userid int unique not null, +typeid int unique not null, +qxld varchar(20) not null +) diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery7.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..16961cfc18d10d4e86df61c468c2d4deeff6235b --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery7.sql" @@ -0,0 +1,102 @@ +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 \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/ljf1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/ljf1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a6f7afb63f23d8e59a997600eb2600a668212d72 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/ljf1.sql" @@ -0,0 +1,169 @@ +use master +go +create database TestDB +go +create database TestDB +on +( +name='students', +filename='d:\students.mdf' +) +log on +( +name='students_log', +filename='d:\studnts_log.ldf' +) +create table typelnfo +( +typeld int primary key identity(1,1), +typeName varchar(10) not null +) + +create table loginlnfo +( +Loginld int primary key identity(1,1), +Loginname char(10) not null unique, +LoginPwd char(20) not null default('123456'), +xingbei bit, +shengre datetime, +leibei nvarchar(10) +) +use master +go +create database company +go +create database company +on +( +name='home1', +filename='d:\home1.mdf', +size=3mb, +filegrowth=10%, +maxsize=50mb +) +log on +( +name='home1_log', +filename='d:\home1_log.ldf', +size=3mb, +filegrwth=10%, +maxsize=50mb +) +create table sectionlnfo +( +sectionID INT IDENTITY(1,1) primary key, + +sectionName varchar(10) not null +) +create table userlnfo +( +userNo int identity(1,1) primary key not null, +userName VARCHAR(10) check(len(userName)>4), +userSex varchar(2) not null check(userSex='男' or userSex='女'), +userAge int not null check(userAge>=1 and userAge<=100), +userAddress varchar(50) default('湖北'), +userSection int foreign key references sectionlnfo(sectionID) +) +create table worklnfo +( +workld int identity(1,1) primary key not null, +userld int foreign key references userlnfo(userNo), +workTime datetime not null, +workDescription varchar(40) not null check(workDescription='迟到' or workDescription='早退' or workDescription='旷工' or workDescription= '病假' or workDescription='事假') +) +use master +go +create database guanli +go +create database guanli +on +( +name='guanli', +filename='d:\guanli.mdf', +size=3mb, +maxsize=50mb, +filegrowth=10% +) +log on +( +name='guanli_log', +filename='d:\guanli_log.ldf', +size=3md, +maxsize=50mb, +filegrowth=10% +) +create table banji +( +classid int identity(1,1) primary key, +t1 int not null unique, +shijian int not null +) +create table xsheng +( +xhao int identity(1,1) primary key, +xingming int unique, +xingbe varchar(2) not null check(xingbe='男' or xingbe='女')default('男'), +nianling int check(nianling>=15 and nianling<=40) not null, +dizhi char default('湖北武汉'), +xshengxinxi int foreign key references banji(classid) +) +create table kecheng +( +bianhao int primary key identity(1,1), +kechenming char not null unique, +miaoshu varchar(50) +) +create table chengji +( +bianhao int primary key identity(1,1), +number int foreign key references xsheng(xhao) not null, +course int foreign key references kecheng(bianhao), +performance int check(performance>=0 and performance<=100) +) + +create database fangwuxing +on +( +name='fangwu', +filename='d:\fangwu.mdf', +size=5mb, +maxsize=50mb, +filegrowth=10% +) +log on +( +name='fangwu_log', +filename='d:\fangwu_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=10% +) +use fangwuxing +go +create table tblUser +( +userId int primary key identity(1,1), +userName varchar, +userTel nvarchar(10) +) +create table tblHouseType +( +typeId int primary key identity(1,1), +typName varchar check(typName='别墅'or typName='普通住宅'or typName='平房' or typName='地下室') not null +) +create table tblQx +( +qxId int primary key identity(1,1), +qxName varchar check(qxName='武昌'or qxName='汉阳' or qxName='汉口') not null +) +create table tblHouseInfo +( +id int primary key identity(1,1), +userId int foreign key references tblUser (userId), +zj money, +shi varchar, +ting int, +typeId int foreign key references tblHouseType (typeId), +qxId int foreign key references tblQx (qxId) +) + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/ljf2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/ljf2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..66a668360a762acebf6f77ff6a8abcdbbebb5b61 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/ljf2.sql" @@ -0,0 +1,109 @@ +use master +go +create database bbs +go +create database bbs +on +( +name='home', +filename='d:\home.mdf', +size=3mb, +maxsize=50mb, +filegrowth=10% +) +log on +( +name='home_log', +filename='d:\home_log.ldf', +size=3mb, +maxsize=50mb, +filegrowth=10% +) +create table bbsUsers +( +UID int, +uName varchar(10) not null, +uSex varchar(2) not null, +uAge int not null, +uPoint int not null, +) + +alter table bbsUsers alter column UID 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 bbsSection +( +sID int 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(uuID) +create table bbsTopic + +( + tID int primary key, + tUID int foreign key references bbsUsers(uuID), + tSID int foreign key references bbsSection(sID) +) +alter table bbsTopic add tTitle varchar(100) not null +alter table bbsTopic add tMsg text not null +alter table bbsTopic add tTime datetime +alter table bbsTopic add tCount int + + + +create table bbsReply +( +rID int primary key, +rUID int foreign key references bbsUsers(uuID), +rTID int foreign key references bbsTopic(tID), +rMsg text not null, +rTime datetime +) + +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女',20,0 union +select'逍遥','男',18,4 union +select'七年级生','男',19,2 +select * from bbsUsers +select uName,uPoint into bbsPoint from bbsUsers + +insert into bbsSection(sName, sID) +select '技术交流' , 1union +select '读书世界' ,2 union +select '生活百科' ,3 union +select ' 八卦区' ,4 +select * from bbsSection + + +insert into bbsTopic( tid,tUID,tSID,tTitle,tMsg,tTime,tCount ) +select 1,3, 4,'范跑跑','谁是范跑跑','2008-7-8',1 union +select 2,2, 1,' .NET','与JAVA的区别是什么呀?','2008-9-1',2 union +select 3,1, 3,' 今年夏天最流行什么','有谁知道今年夏天最流行','2008-9-10',0 +select * from bbsTopic +select * from bbsReply + +insert into bbsReply(rid,rUID,rMsg,rTime) +select 1,2,'是范跑跑','2008-7-8'union +select 2,1,'没有区别就是最大的区别','2008-7-8'union +select 3,3,'今年流行上海滩','2008-7-8' + +delete bbsUsers where uName='逍遥' --深感抱歉 删错人了 +alter table bbsReply drop FK__bbsReply__rUID__4AB81AF0 +alter table bbsTopic drop FK__bbsTopic__tUID__46E78A0C +select * from bbsReply + +--给逍遥加分10 +update bbsUsers set upoint=12 where uName='小雨点' +--删除生活百科板块 +alter table bbsTopic drop FK__bbsTopic__tSID__47DBAE45 +delete bbsSection where sName='生活百科' +--删除所有回帖 +delete bbsReply + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..37642a6aeeefeb7a5f78acf89242385decb6e792 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery1.sql" @@ -0,0 +1,35 @@ +use master +go +create database TestDB +on( +name='typeInfo', +filename='D:\新建文件夹\typeInfo.mdf', +size=5, +maxsize=10, +filegrowth=1 +) +log on( +name='typeInfo_LOG', +filename='D:\新建文件夹\typeInfo.ldf', +size=5, +maxsize=10, +filegrowth=1 +) +go +use TestDB +go +create table typeInfo +( +typeId int primary key identity(1,1), +typeName varchar(10) not null +) +create table loginInfo +( +LoginId int primary key identity(1,1), +LoginName text not null unique , +LoginPwd text not null default('123456'), +Logsex nvarchar(1) default('男'), +birthday datetime, +member nvarchar +) + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..950b234068520120456499bb570a2be2d09f7dab --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery2.sql" @@ -0,0 +1,40 @@ +use master +go +create database company +on( +name='company', +filename='D:\新建文件夹\company.mdf', +size = 5, +maxsize=10, +filegrowth = 1 + +) +log on +( +name='company_log', +filename='D:\新建文件夹\company_log.ldf', +size = 5, +maxsize=10, +filegrowth = 1 +) +create table sectionInfo +( +sectionID int primary key, +sectionName varchar(10) not null +) +create table userInfo +( +userNo int primary key not null, +userName varchar(10) unique not null, +userSex varchar(2) not null check(userSex='男' or userSex='女'), +userAge int not null check(userAge>=0 and userAge<=100), +userAddress varchar(50) default('湖北'), +userSection int foreign key references sectionInfo(sectionID) +) +create table workInfo +( +workId int primary key not null, +userId int foreign key references userInfo(userNo), +workTime datetime not null, +workDescription varchar(40) not null check(workDescription='迟到' or workDescription='早退' or workDescription='矿工' or workDescription='事假' ) +) diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery3.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ab36c200e6d3796f5f02c1edfe3534ea5fd3bb97 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery3.sql" @@ -0,0 +1,28 @@ +use master +go +create database stumanagement +go +use stumanagement +go +create table classmanagement +( +classid int primary key identity(1,1), +stuname nvarchar(5) not null unique, +classtime datetime not null, +classdescribe text + +) +create table stumessage +( +stuid int primary key identity(1,1), +stuname nvarchar check(len(stuname)>2) not null, +stusex nvarchar(1) check(stusex='男' or stusex='女') default('男'), +stuage int check(stuage>=15 or stuage<=40), +stuaddress text default('湖北武汉') +) +create table coursemessage +( +courseid int primary key identity(1,1), +coursename nvarchar(5)not null, +describe text +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery4.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b464842e5cc9449faffedb7e462bc1c0dfa45db7 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery4.sql" @@ -0,0 +1,96 @@ +use master +go +create database bbs +on( +name='bbs', +filename='C:\Users\Administrator\Desktop\zy\bbs.mdf', +size=5, +maxsize=10, +filegrowth=1 + +) +log on( +name='bbs_log', +filename='C:\Users\Administrator\Desktop\zy\bbs_log.ldf', +size=5, +maxsize=10, +filegrowth=1 +) +go +use bbs +go +create table bbsUsers +( +bbsUID int, +uName varchar(10), +uSex varchar(2), +uAge int, +uPoint int +) +alter table bbsUsers add constraint dk primary key(bbsUID) +alter table bbsUsers alter column bbsUID int not null +alter table bbsUsers add constraint dw unique(uName) +alter table bbsUsers alter column uName varchar(10) not null +alter table bbsUsers add constraint de check(uSex='男' or uSex='女') +alter table bbsUsers add constraint dr check(uAge>15 or uAge<=60) +alter table bbsUsers alter column uAge int not null +alter table bbsUsers add constraint dd check(uPoint>=0) +alter table bbsUsers alter column uPoint int not null +create table bbsTopic +( +tID int primary key identity(1,1), +tUID int foreign key references bbsUsers(bbsUID), +tSID int foreign key references bbsSection(bbssID), +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(bbsUID), +rTID int foreign key references bbsSection(bbssID), +rMsg text not null, +rTime datetime +) +create table bbsSection +( +bbssID int, +sName varchar(10), +sUid int +) +alter table bbssID add constraint da primary key(bbssID) +alter table sName alter column sName varchar(10) not null +alter table sUid add constraint daa foreign key references bbsUsers(bbsUID) + +insert into bbsUsers +select '小雨点' , '女 ' ,'20',' 0' union +select '逍遥' , '男', ' 18',' 4'union +select '七年级生' , '男', ' 19',' 2' +backup database bbs to disk='C:\Users\Administrator\Desktop' +select uName,uPoint into bbsPoint from bbsUsers +insert into bbsSection(sName, bbssID) +select ' 技术交流 ',' 小雨点'union +select ' 读书世界 ',' 七年级生'union +select ' 生活百科 ',' 小雨点'union +select ' 八卦区 ',' 七年级生' +--主贴编号 tID int 主键 标识列, +-- 发帖人编号 tUID int 外键 引用用户信息表的用户编号 + --版块编号 tSID int 外键 引用版块表的版块编号 (标明该贴子属于哪个版块) + --贴子的标题 tTitle varchar(100) 不能为空 +-- 帖子的内容 tMsg text 不能为空 + --发帖时间 tTime datetime + --回复数量 tCount int +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) +select '逍遥','八卦区','范跑跑','谁是范跑跑',' 2008-7-8','1'union +select '七年级生','技术交流','.NET','与JAVA的区别是什么呀?','2008-9-1','2'union +select '小雨点','生活百科','今年夏天最流行什么','有谁知道今年夏天最流行','2008-9-10','0' +ALTER table bbsTopic drop constraint dk +alter table bbsTopic drop constraint FK_bbsTopic +alter table bbsTopic drop column sUID +update bbsUsers set uPoint= 30 WHERE uPoint =20 +ALTER table bbsUsers alter Column uPoint int +ALTER TABLE bbsSection DROP CONSTRAINT FK_bbsReply +delete from bbsSection where sName='生活百科' +alter table bbsTopic drop column tCount \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\347\273\203\344\271\2401.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\347\273\203\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5e1a859ca2c288c7c8bc05cd168e7b2e2a3b9097 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\347\273\203\344\271\2401.sql" @@ -0,0 +1,142 @@ +use master +go + +create database TestDB +on +( + name='TestDB', + filename='C:\SQL\TestDB.mdf', + size=10MB, + maxsize=100MB, + filegrowth=10MB +) +log on +( + name='TestDB_log', + filename='C:\SQL\TestDB_log.ldf', + size=10MB, + maxsize=100MB, + filegrowth=10MB +) +go +use TestDB +go +create table TypInfo +( +TypedID int primary key identity(1,1) not null, +TypedName varchar(10) not null +) +go +create table LoginInfo +( +LoginID int primary key identity(1,1) not null, +LoginName varchar(10) unique not null, +LoginPwd varchar(20) default('123456') not null, +LoginSex char(2) check(LoginSex='男' or LoginSex='女') not null, +LoginDate varchar(10) not null +) + + +create database ComPany +on +( +name='ComPany', +filename='C:\SQL\ComPany.mdf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +log on +( +name='ComPany_log', +filename='C:\SQL\ComPany_log.ldf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +go +use ComPany +go +create table SectionInfo +( +SectionID int primary key identity(1,1) not null, +SectionName varchar(10) not null, +) +go +use ComPany +go +create table UserInfo +( +UserNO int primary key identity(1,1) not null, +UserName varchar(10) unique check(UserName>4) not null, +UserSex varchar(2) check(UserSex='男' or UserSex='女') not null, +UserAge int check(UserAge>=1 and UserAge<=100) not null, +UserAddress varchar(50) default('湖北') not null, +UesrSection int references SectionInfo(SectionID) not null +) +go +use ComPany +go +create table WorkInfo +( +WorkID int primary key identity(1,1) not null, +UseID int references UserInfo(UserNO) not null, +WorkTime datetime not null, +WorkDescription varchar(40) check(WorkDescription='迟到' or WorkDescription='早退' or WorkDescription='旷工' or WorkDescription='病假' or WorkDescription='事假') not null +) + +create database GuanLi +on +( +name='GuanLi', +filename='C:\SQL\GuanLi.mdf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +log on +( +name='GuanLi_log', +filename='C:\SQL\GuanLi_log.ldf', +size=10MB, +maxsize=100MB, +filegrowth=10MB +) +go +use GuanLi +go +create table ClassInfo +( +ClassID int primary key identity(1,1) not null, +ClassName varchar(10) unique not null, +ClassDate datetime not null, +) +go +use GuanLi +go +create table StudenInfo +( +StuID int primary key identity(1,1) not null, +StuName varchar(10) unique check(StuName>2) not null, +StuSex char(2) default('男') check(StuSex='男' or StuSex='女') not null, +StuAge int check(StuAge>=15 and StuAge<=40) not null, +StuAddress varchar(50) default('湖北武汉') not null +) +go +use GuanLi +go +create table CourInfo +( +CourID int primary key identity(1,1) not null, +CourName varchar(10) unique not null, +) +go +use GuanLi +go +create table AchInfo +( +AchID int primary key identity(1,1) not null, +AchNO int references StudenInfo(StuID) not null, +AchCour int references CourInfo(CourID) not null, +AchIeve int check(AchIeve>=0 and AchIeve<=100) not null, +) diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\347\273\203\344\271\2402.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\347\273\203\344\271\2402.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c004845958c417e2a1fe6a6d29a2d098a9f6fb24 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\347\273\203\344\271\2402.sql" @@ -0,0 +1,97 @@ +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 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 BBSSection +delete BBSSection where SName='生活百科' + +select * from BBSReply +delete BBSReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\347\273\203\344\271\2401..sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\347\273\203\344\271\2401..sql" new file mode 100644 index 0000000000000000000000000000000000000000..3529499fb3d7521acda3cd5033d0ee2b30f8c314 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\347\273\203\344\271\2401..sql" @@ -0,0 +1,179 @@ +create database TestDB +on +( + name='TestDB', + filename='F:\TestDB.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) + +log on +( + name='TestDB_log', + filename='F:\TestDB_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +use TestDB +go + +-- 1、 +create table typeInfo +( + typeId int primary key identity(1,1), + typeName varchar(10) not null, +) + +create table loginInfo +( + LoginId int primary key identity(1,1), + LoginName varchar(10) not null unique, + LoginPwd varchar(20) not null default('1,2,3,4,5,6'), + Logsex char(1), + Logbrithday datetime, + Loghuiyuan varchar +) + +-- 2、 +create database company +on +( + name='company', + filename='F:\company.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='company_log', + filename='F:\company_log.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use company +go + +create table sectionInfo +( + sectionID int primary key, + sectionName varchar(10) not null +) +create table userInfo +( + userNo int primary key not null, + userName varchar(10) unique not null check(userName>4), + userSex varchar(2) not null check(userSex='男' or userSex='女' ), + userAge int not null check(userAge>=1 or userAge<=100), + userAddress varchar(50) default('湖北'), + userSection int foreign key references sectionInfo(sectionID) +) +create table workInfo +( + workId int primary key not null, + userId int foreign key references userInfo(userNo), + workTime datetime not null, + workDescription varchar(40) not null check(workDescription='迟到'or workDescription='早退'or workDescription='矿工' or workDescription='病假'or workDescription='事假') +) + +-- 3、 +create database School +on +( + name='School', + filename='F:\School.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='School_log', + filename='F:\School_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use School +go +create table Classinfo +( + classid int primary key, + classname varchar(10) not null unique, + classremark ntext +) +create table studentinfo +( + stunum int primary key, + stuname varchar check (len(stuname)>2) unique, + stusex varchar(2) default('男') check(stusex='男' or stusex='女'), + stuage int check(stuage>=15 or stuage<=40) not null, + stuaddress nvarchar(50) default('湖北武汉'), + stuclassid int references Classinfo(classid) +) +create table information +( + number int primary key, + kechenname varchar unique not null, + miaoshu ntext +) +create table score +( + scorenum int primary key, + scorestuid int references studentinfo(stunum) not null, + scoreid int references information(number) not null, + score int check(score>=0 or score<=100) +) +--4、 +create database fangwu +on +( + name='fangwu', + filename='F:\fangwu.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='fangwu_log', + filename='F:\fangwu_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use fangwu +go + +create table tblUser +( + userId int primary key identity(1,1), + userName varchar, + userTel nvarchar(10) +) +create table tblHouseType +( + typeId int primary key identity(1,1), + typName varchar check(typName='别墅'or typName='普通住宅'or typName='平房' or typName='地下室') not null +) +create table tblQx +( + qxId int primary key identity(1,1), + qxName varchar check(qxName='武昌'or qxName='汉阳' or qxName='汉口') not null +) + +create table tblHouseInfo +( + id int primary key identity(1,1), + userId int foreign key references tblUser (userId), + zj money, + shi varchar, + ting int, + typeId int foreign key references tblHouseType (typeId), + qxId int foreign key references tblQx (qxId) +) + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\347\273\203\344\271\2402.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\347\273\203\344\271\2402.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b53734d6127d8ca15969198be3e33f7807bd2ebb --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\276\231\345\206\260/\347\273\203\344\271\2402.sql" @@ -0,0 +1,111 @@ +----练习2 +-- 一、 +create database bbs +on +( + name='bbs', + filename='F:\bbs.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='F:\bbs_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use bbs +go + +create table bbsUsers +( + uuID 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 primary key (uuID) +alter table bbsUsers add constraint UK unique(uName) +alter table bbsUsers add constraint CK check(uSex='男' or uSex='女' ) +alter table bbsUsers add constraint CK1 check(uAge>=15 or uAge<=60 ) +alter table bbsUsers add constraint CK2 check(upoint>=0) + +create table bbsSection +( + sID int 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(uuID) + + + +create table bbsTopic +( + tID int primary key, + tUID int foreign key references bbsUsers(uuID), + tSID int foreign key references bbsSection(sID), + +) +alter table bbsTopic add tTitle varchar(100) not null +alter table bbsTopic add tMsg text not null +alter table bbsTopic add tTime datetime +alter table bbsTopic add tCount int + +create table bbsReply +( + rID int primary key, + rUID int foreign key references bbsUsers(uuID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女',20,0 union +select'逍遥','男',18,4 union +select'七年级生','男',19,2 +select * from bbsUsers + +--2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中, +--提示查询部分列:select 列名1,列名2 from 表名 +select uName,uPoint into bbsPoint from bbsUsers + +insert into bbsSection(sName, sID) +select '技术交流' , 1union +select '读书世界' ,2 union +select '生活百科' ,3 union +select ' 八卦区' ,4 +select * from bbsSection + +-- 4.向主贴和回帖表中添加几条记录 +-- 发帖人 板块名 帖子标题 +-- 帖子内容 帖时间 回复数量 +insert into bbsTopic( tid,tUID,tSID,tTitle,tMsg,tTime,tCount ) +select 1,3, 4,'范跑跑','谁是范跑跑','2008-7-8',1 union +select 2,2, 1,' .NET','与JAVA的区别是什么呀?','2008-9-1',2 union +select 3,1, 3,' 今年夏天最流行什么','有谁知道今年夏天最流行','2008-9-10',0 +select * from bbsTopic + +select * from bbsReply +insert into bbsReply(rid,rUID,rMsg,rTime) +select 1,2,'钟老师是范跑跑','2008-7-8'union +select 2,1,'没有区别就是最大的区别','2008-7-8'union +select 3,3,'今年流行上海滩','2008-7-8' + +delete bbsUsers where uName='小雨点' --深感抱歉 删错人了 +alter table bbsReply drop FK__bbsReply__rUID__4AB81AF0 +alter table bbsTopic drop FK__bbsTopic__tUID__46E78A0C +select * from bbsReply +--给逍遥加分10 +update bbsUsers set upoint=12 where uName='逍遥' +--删除生活百科板块 +alter table bbsTopic drop FK__bbsTopic__tSID__47DBAE45 +delete bbsSection where sName='生活百科' +--删除所有回帖 +delete bbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/\347\273\203\344\271\2401..sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\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..3529499fb3d7521acda3cd5033d0ee2b30f8c314 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/\347\273\203\344\271\2401..sql" @@ -0,0 +1,179 @@ +create database TestDB +on +( + name='TestDB', + filename='F:\TestDB.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) + +log on +( + name='TestDB_log', + filename='F:\TestDB_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +use TestDB +go + +-- 1、 +create table typeInfo +( + typeId int primary key identity(1,1), + typeName varchar(10) not null, +) + +create table loginInfo +( + LoginId int primary key identity(1,1), + LoginName varchar(10) not null unique, + LoginPwd varchar(20) not null default('1,2,3,4,5,6'), + Logsex char(1), + Logbrithday datetime, + Loghuiyuan varchar +) + +-- 2、 +create database company +on +( + name='company', + filename='F:\company.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='company_log', + filename='F:\company_log.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use company +go + +create table sectionInfo +( + sectionID int primary key, + sectionName varchar(10) not null +) +create table userInfo +( + userNo int primary key not null, + userName varchar(10) unique not null check(userName>4), + userSex varchar(2) not null check(userSex='男' or userSex='女' ), + userAge int not null check(userAge>=1 or userAge<=100), + userAddress varchar(50) default('湖北'), + userSection int foreign key references sectionInfo(sectionID) +) +create table workInfo +( + workId int primary key not null, + userId int foreign key references userInfo(userNo), + workTime datetime not null, + workDescription varchar(40) not null check(workDescription='迟到'or workDescription='早退'or workDescription='矿工' or workDescription='病假'or workDescription='事假') +) + +-- 3、 +create database School +on +( + name='School', + filename='F:\School.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='School_log', + filename='F:\School_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use School +go +create table Classinfo +( + classid int primary key, + classname varchar(10) not null unique, + classremark ntext +) +create table studentinfo +( + stunum int primary key, + stuname varchar check (len(stuname)>2) unique, + stusex varchar(2) default('男') check(stusex='男' or stusex='女'), + stuage int check(stuage>=15 or stuage<=40) not null, + stuaddress nvarchar(50) default('湖北武汉'), + stuclassid int references Classinfo(classid) +) +create table information +( + number int primary key, + kechenname varchar unique not null, + miaoshu ntext +) +create table score +( + scorenum int primary key, + scorestuid int references studentinfo(stunum) not null, + scoreid int references information(number) not null, + score int check(score>=0 or score<=100) +) +--4、 +create database fangwu +on +( + name='fangwu', + filename='F:\fangwu.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='fangwu_log', + filename='F:\fangwu_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use fangwu +go + +create table tblUser +( + userId int primary key identity(1,1), + userName varchar, + userTel nvarchar(10) +) +create table tblHouseType +( + typeId int primary key identity(1,1), + typName varchar check(typName='别墅'or typName='普通住宅'or typName='平房' or typName='地下室') not null +) +create table tblQx +( + qxId int primary key identity(1,1), + qxName varchar check(qxName='武昌'or qxName='汉阳' or qxName='汉口') not null +) + +create table tblHouseInfo +( + id int primary key identity(1,1), + userId int foreign key references tblUser (userId), + zj money, + shi varchar, + ting int, + typeId int foreign key references tblHouseType (typeId), + qxId int foreign key references tblQx (qxId) +) + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/\347\273\203\344\271\2402.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\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..b53734d6127d8ca15969198be3e33f7807bd2ebb --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/\347\273\203\344\271\2402.sql" @@ -0,0 +1,111 @@ +----练习2 +-- 一、 +create database bbs +on +( + name='bbs', + filename='F:\bbs.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='F:\bbs_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use bbs +go + +create table bbsUsers +( + uuID 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 primary key (uuID) +alter table bbsUsers add constraint UK unique(uName) +alter table bbsUsers add constraint CK check(uSex='男' or uSex='女' ) +alter table bbsUsers add constraint CK1 check(uAge>=15 or uAge<=60 ) +alter table bbsUsers add constraint CK2 check(upoint>=0) + +create table bbsSection +( + sID int 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(uuID) + + + +create table bbsTopic +( + tID int primary key, + tUID int foreign key references bbsUsers(uuID), + tSID int foreign key references bbsSection(sID), + +) +alter table bbsTopic add tTitle varchar(100) not null +alter table bbsTopic add tMsg text not null +alter table bbsTopic add tTime datetime +alter table bbsTopic add tCount int + +create table bbsReply +( + rID int primary key, + rUID int foreign key references bbsUsers(uuID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女',20,0 union +select'逍遥','男',18,4 union +select'七年级生','男',19,2 +select * from bbsUsers + +--2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中, +--提示查询部分列:select 列名1,列名2 from 表名 +select uName,uPoint into bbsPoint from bbsUsers + +insert into bbsSection(sName, sID) +select '技术交流' , 1union +select '读书世界' ,2 union +select '生活百科' ,3 union +select ' 八卦区' ,4 +select * from bbsSection + +-- 4.向主贴和回帖表中添加几条记录 +-- 发帖人 板块名 帖子标题 +-- 帖子内容 帖时间 回复数量 +insert into bbsTopic( tid,tUID,tSID,tTitle,tMsg,tTime,tCount ) +select 1,3, 4,'范跑跑','谁是范跑跑','2008-7-8',1 union +select 2,2, 1,' .NET','与JAVA的区别是什么呀?','2008-9-1',2 union +select 3,1, 3,' 今年夏天最流行什么','有谁知道今年夏天最流行','2008-9-10',0 +select * from bbsTopic + +select * from bbsReply +insert into bbsReply(rid,rUID,rMsg,rTime) +select 1,2,'钟老师是范跑跑','2008-7-8'union +select 2,1,'没有区别就是最大的区别','2008-7-8'union +select 3,3,'今年流行上海滩','2008-7-8' + +delete bbsUsers where uName='小雨点' --深感抱歉 删错人了 +alter table bbsReply drop FK__bbsReply__rUID__4AB81AF0 +alter table bbsTopic drop FK__bbsTopic__tUID__46E78A0C +select * from bbsReply +--给逍遥加分10 +update bbsUsers set upoint=12 where uName='逍遥' +--删除生活百科板块 +alter table bbsTopic drop FK__bbsTopic__tSID__47DBAE45 +delete bbsSection where sName='生活百科' +--删除所有回帖 +delete bbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery6.1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery6.1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c8aa234c8fc21096e73bd65cde3629fe9c2d1572 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery6.1.sql" @@ -0,0 +1,118 @@ +create database bbs +on +( + fileName='F:\homework\bbs.mdf', + Name='bbs', + size=5MB, + Maxsize=5MB, + filegrowth=1MB +) +log on +( + fileName='F:\homework\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) + + +alter table bbsTopic drop constraint FK__bbsTopic__tUID__1920BF5C + +alter table bbsReply drop constraint FK__bbsReply__rUID__1CF15040 + + +delete from bbsUsers where uName='逍遥' + +update bbsUsers set uPoint=10 where uName='小雨点' + +alter table bbsTopic drop constraint FK__bbsTopic__tSID__1A14E395 + +delete from bbsSection where sName='生活百科' + +delete from bbsReply + + + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery6.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ef11463c4a692ca753079f745b9fef00abc01a7f --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery6.sql" @@ -0,0 +1,201 @@ +create database TestDB +on +( + FileName='F:\homework\TestDB.mdf', + Name='TestDB', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +log on +( + FileName='F:\homework\TestDB_log.ldf', + Name='TestDB_log', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +go + +use TestDB + +go + +create table typeInfo +( + typeID int primary key identity(1,1), + typeName varchar(10) not null, +) + +create table loginInfo +( + LoginID int primary key identity(1,1), + LoginName varchar(10) not null unique, + LoginPwd varchar(20) not null default(123456), + LogSex nvarchar(1), + Logbirthday date, + LogVIP nvarchar(10), +) + + +create database Company +on +( + FileName='F:\homework\Company.mdf', + Name='Company', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +log on +( + FileName='F:\homework\Company_log.ldf', + Name='Company_log', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +go +use Company +go + + +create table sectionInfo +( + sectionID int identity(1,1) primary key , + sectionName varchar(10) not null, +) + +create table userInfo +( + userNo int identity(1,1) primary key not null, + userName varchar(10) unique not null check(len(userName)>4), + userSex varchar(2) not null check(userSex in('男','女')), + userAge int not null check(userAge>=1 or userAge<=100), + userAddress varchar(50) default('湖北'), + userSection int foreign key references sectionInfo(sectionID) +) + +create table workInfo +( + workID int identity(1,1) primary key not null, + userID int foreign key references userInfo(userNo), + workTime datetime not null, + workDescription varchar(40) not null check(workDescription in ('迟到','早退','旷工','病假','事假')) +) + + + +create database SMS +on +( + FileName='F:\homework\SMS.mdf', + Name='SMS', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +log on +( + FileName='F:\homework\SMS_log.mdf', + Name='SMS_log', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +go +use SMS +go + +create table ClassInfo +( + ClassID int primary key identity(1,1), + ClassName nvarchar(10) not null unique, + StartTime date not null, + ClassDescribe text, +) + +create table StudentInfo +( + StuID int primary key identity(1,1), + StuName nvarchar(10) unique check(len(StuName)>2), + StuSex nvarchar(1) check(StuSex in ('男','女')) default('男') not null, + StuAge int check(StuAge>=15 or StuAge<=40) not null, + StuAddress nvarchar(30) default('湖北武汉'), + ClassID int +) + + +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar not null unique, + CourseS text +) + +create table Score +( + ScoreID int primary key identity(1,1), + StudentID int not null, + Course int not null, + Score int check(Score>=0 or Score<=100) +) + + +create database HouseRental +on +( + fileName='F:\homework\HouseRental.mdf', + Name='HouseRental', + size=5MB, + Maxsize=5MB, + filegrowth=1MB +) +log on +( + fileName='F:\homework\HouseRental_log.mdf', + Name='HouseRental_log', + size=5MB, + Maxsize=5MB, + filegrowth=1MB +) + +go + +use HouseRental + +go + + +create table tblHouseInfo +( + HouseID int primary key identity(1,1), + Housedesc text , + UserID int not null, + HouseRental money not null, + Houseshi int not null, + Houseting int not null, + TypeID int not null, + QxUD int not null +) + +create table tblUser +( + UserID int, + UserName nvarchar(10) not null, + UserTel nvarchar(11) +) + +create table tblHouseType +( + TypeID int not null, + TypeName nvarchar(10) +) + +create table tblQx +( + QxUD int not null, + QxName nvarchar(10) +) + + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery5.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e285ebd0851615f9a1131ab4b5836ada945300e2 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery5.sql" @@ -0,0 +1,310 @@ +--用SQL实现以下的题目: + +--1. 先创建一个数据库,数据库名为TestDB,要求有两个数据文件,一个日志文件,注意命名规范,文件存放在E盘下 +-- 再在上面的数据库下创建表,结构如下: +use master + +go + +create database TestDB +on +( + name='TestDB', + filename='D:\TEXT\TestDB.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='TestDB_log', + filename='D:\TEXT\TestDB_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +-- 会员类别表(typeInfo): +-- 类别编号(typeId):主键、自动编号 +-- 类别名(typeName): varchar(10) 不能为空 + +go +use TestDB + +go + +create table typeInfo +( + typeId int primary key identity(1,1), + typeName varchar(10) not null, +) +-- 登录用户表(loginInfo): +-- 编号(LoginId),数据类型(int),主键、自动编号 +-- 账户(LoginName),文本,长度为10,非空,必须唯一,不能重复 +-- 密码(LoginPwd),文本,长度为20,非空、默认值为‘123456’ +-- 性别(自定类型) +-- 生日(自定类型) +-- 会员类别(自定类型) +go +use TestDB + +go +create table loginInfo +( + LoginId int primary key identity(1,1), + LoginName nvarchar(10) unique not null, + LoginPwd nvarchar(20) default(123456) not null, + LoginSex nvarchar(1) default('男') check(LoginSex='男' or LoginSex='女'), + LoginBirthday date, + LoginMember nvarchar(30) +) + +--2. 先创建一个数据库用来存放某公司的员工信息,数据库的名称为company,包含2个数据文件1个日志 +--文件,数据文件和日志文件全部存放在E盘中,初始大小,增长和最大大小自己设定 +create database company +on +( + name='company', + filename='D:\TEXT\company.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='company_log', + filename='D:\TEXT\company_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +-- 再创建表: + +-- 部门信息表(sectionInfo) +-- 部门编号 sectionID int 标识列 主键 +-- 部门名称 sectionName varchar(10) 不能为空 +go +use company + +go + +create table sectionInfo +( + sectionID int identity primary key, + sectionName varchar(10) not null +) +-- 员工信息表(userInfo) +-- 员工编号 userNo int 标识列 主键 不允许为空 +-- 员工姓名 userName varchar(10) 唯一约束 不允许为空 长度必须大于4 +-- 员工性别 userSex varchar(2) 不允许为空 只能是男或女 +-- 员工年龄 userAge int 不能为空 范围在1-100之间 +-- 员工地址 userAddress varchar(50) 默认值为“湖北” +-- 员工部门 userSection int 外键,引用部门信息表的部门编号 +go +use company + +go + +create table userInfo +( + userNo int identity primary key not null, + userName varchar(10) unique check(userName>4) not null, + userSex varchar(2) default('男') check(userSex='男' or userSex='女') not null, + userAge int check(userAge>=1 and userAge<=100) not null, + userAddress varchar(50) default('湖北'), + userSection int references sectionInfo(sectionID) +) + +-- 员工考勤表(workInfo) +-- 考勤编号 workId int 标识列 主键 不能为空 +-- 考勤员工 userId int 外键 引用员工信息表的员工编号 不能为空 +-- 考勤时间 workTime datetime 不能为空 +-- 考勤说明 workDescription varchar(40) 不能为空 内容只能是“迟到”,“早退”,“旷工”,“病假”,“事假”中的一种 + +go +use company + +go + +create table workInfo +( + workId int identity primary key not null, + userId int references userInfo(userNo) not null, + workTime datetime not null, + workDescription varchar(40) check(workDescription in('迟到','早退','矿工','病假','事假')) not null, +) +--3. 为学校开发一个学生管理系统,请为该系统创建数据库,主要存放的信息有:班级信息、学生信息、课程信息、学生考试成绩 +create database stundent +on +( + name='stundent', + filename='D:\TEXT\stundent.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='stundent_log', + filename='D:\TEXT\stundent_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% + ) +-- 班级信息:班级编号 classid (主键、标识列) +-- 班级名称(例如:T1、T2、D09等等):不能为空,不能重复 +-- 开办时间:不能为空 +-- 班级描述 +go +use stundent + +go + +create table ClassInfo +( + ClassID int primary key identity, + ClassName nvarchar(10) unique not null, + ClassTime date not null, + ClassDescribe nvarchar(200), +) + +-- 学生信息:学号:主键、标识列 +-- 姓名:长度大于2,不能重复 +-- 性别:只能是‘男’或‘女’,默认为男,不能为空 +-- 年龄:在15-40之间,不能为空 +-- 家庭地址:默认为“湖北武汉” +-- 所在的班级编号 +go +use stundent + +go + +create table StuInfo +( + StuID int primary key identity, + StuName nvarchar(2) unique, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女'), + StuAge int check(StuAge>=15 and StuAge<=40) not null, + StuAddress nvarchar(100) default('湖北武汉'), + ClassID int +) +-- 课程信息:编号:主键、标识列 +-- 课程名:不能为空,不能重复 +-- 课程描述: +go +use stundent + +go + +create table course +( + CourseID int primary key identity, + CourseName nvarchar(10) unique not null, + CourseDescribe nvarchar(100) +) +-- 成绩信息:成绩编号:主键、标识列 +-- 成绩所属于的学生编号,不能为空 +-- 成绩所属的课程编号,不能为空 +-- 成绩:在0-100之间 +go +use stundent + +go + +create table resultInfo +( + ResultID int primary key identity, + StuID int not null, + ClassID int not null, + Result int check(Result>=0 and Result<=100) +) +--4. 为一个房屋出租系统创建一个数据库,数据库中主要存放房屋的信息,包括:房屋的编号,房屋的描述,发布人的信息(姓名和联系电话),房屋的租金,房屋的室数,房屋的厅数,房屋的类型(别墅、普通住宅、平房、地下室),房屋所属的区县(武昌、汉阳、汉口),请根据上面的描述设计表,并设计表的关系,以及列的约束 +create database home +on +( + name='home', + filename='D:\TEXT\home.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='home_log', + filename='D:\TEXT\home_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% + ) +--tblUser --发布人信息表 +--userId +--userName +--userTel +go +use home + +go + +create table tblUser +( + userId int primary key identity, + userName nvarchar(4) not null, + userTel int not null +) +--tblHouseType --房屋的类型 +--typeId +--typName +go +use home + +go + +create table tblHouseType +( + typeId int primary key identity, + typName nvarchar(10) check(typName>2) +) +--tblQx --区县 +--qxId +--qxName +go +use home + +go + +create table tblQx +( + qxIdint int primary key identity, + qxName nvarchar(10) check(qxName>2) +) +--tblHouseInfo--房屋信息表 +--id +--desc +--userId -- +--zj +--shi +--ting +--typeId -- +--qxId -- + +go +use home + +go + +create table tblHouseInfo +( + id int primary key identity, + userId int references tblUser(userId), + zj int, + shi nvarchar(20), + ting nvarchar(10), + typeId int references tblHouseType(typeId), + qxId int, +) + + + + + + + \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery6.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d843fce0315b479b4f921648dfc88ba060f68e81 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery6.sql" @@ -0,0 +1,190 @@ +--一、先创建数据库和表以 +-- 1.创建一个数据库用来存放某论坛的用户和发帖信息,数据库的名称为bbs,包含1个数据文件1个日志 +-- 文件,数据文件和日志文件全部存放在E盘中,初始大小,增长和最大大小自己设定 +use master + +go + +create database bbs +on +( + name='bbs', + filename='D:\TEXT\bbs.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='D:\TEXT\bbs_log.ldf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +-- 2.创建表 + +-- 注意以下4张表的创建顺序,要求在创建bbsUser和bbsSection表时不添加约束,创建完后单独添加约束,其它的表创建时添加约束 + +-- 用户信息表(bbsUsers) +-- 用户编号 UID int 主键 标识列 +-- 用户名 uName varchar(10) 唯一约束 不能为空 +-- 性别 uSex varchar(2) 不能为空 只能是男或女 +-- 年龄 uAge int 不能为空 范围15-60 +-- 积分 uPoint int 不能为空 范围 >= 0 +go +use bbs + +go + +create table bbsUsers +( + UID int identity, + uName varchar(10) not null, + uSex varchar(2) not null, + uAge int not null, + uPoint int not null +) + +-- 版块表(bbsSection) +-- 版块编号 sID int 标识列 主键 +-- 版块名称 sName varchar(10) 不能为空 +-- 版主编号 sUid int 外键 引用用户信息表的用户编号 +go +use bbs + +go + +create table bbsSection +( + sID int identity, + sName varchar(10) not null, + sUid int constraint FK_bbsUsers_UID references bbsUsers(UID) +) + + +--+ 主贴表(bbsTopic) +-- 主贴编号 tID int 主键 标识列, +-- 发帖人编号 tUID int 外键 引用用户信息表的用户编号 +-- 版块编号 tSID int 外键 引用版块表的版块编号 (标明该贴子属于哪个版块) +-- 贴子的标题 tTitle varchar(100) 不能为空 +-- 帖子的内容 tMsg text 不能为空 +-- 发帖时间 tTime datetime +-- 回复数量 tCount int + +go +use bbs + +go + +create table bbsTopic +( + tID int primary key identity, + tUID int references bbsUsers(UID), + tSID int references bbsSection(sID), + tTitle varchar(100), + tMsg text not null, + tTime datetime, + tCount int +) +--+ 回帖表(bbsReply) +-- 回贴编号 rID int 主键 标识列, +-- 回帖人编号 rUID int 外键 引用用户信息表的用户编号 +-- 对应主贴编号 rTID int 外键 引用主贴表的主贴编号 (标明该贴子属于哪个主贴) +-- 回帖的内容 rMsg text 不能为空 +-- 回帖时间 rTime datetime + +go +use bbs + +go + +create table bbsReply +( + rID int primary key identity, + rUID int references bbsUsers(UID), + rTID int references bbsTopic(tID), + rMsg text not null, + rTime datetime +) + + + +-- 用户编号 UID int 主键 标识列 +-- 用户名 uName varchar(10) 唯一约束 不能为空 +-- 性别 uSex varchar(2) 不能为空 只能是男或女 +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 DK_bbsUsers_uSex default('男') for uSex + + +--- 版块编号 sID int 标识列 主键 + +alter table bbsSection add constraint PK_bbsSection_sID primary key(sID) + + +--二、在上面的数据库、表的基础上完成下列题目: + +-- 1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +-- 小雨点 女 20 0 +-- 逍遥 男 18 4 +-- 七年级生 男 19 2 +insert into bbsUsers(uName,uSex,uAge,uPoint) values ('小雨点','女','20','0'),('逍遥','男','18','4'),('七年级生','男','19','2') +-- 2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中, + --提示查询部分列:select 列名1,列名2 from 表名 +create table bbsPoint +( + uName varchar(10) not null, + uPoint int not null +) +select uName uPoint from bbsPoint + +-- 3.给论坛开设4个板块 版块表(bbsSection)版块名称 sName 版主编号 sUid +-- 名称 版主名 +-- 技术交流 小雨点 +-- 读书世界 七年级生 +-- 生活百科 小雨点 +-- 八卦区 七年级生 + +insert into bbsSection(sName,sUid) values('技术交流','1'),('读书世界','3'),('生活百科','1'), +('八卦区','七年级生') + +-- 4.向主贴和回帖表中添加几条记录 + +-- 主贴: + +-- 发帖人 板块名 帖子标题 帖子内容 发帖时间 回复数量 +-- 逍遥 八卦区 范跑跑 谁是范跑跑 2008-7-8 1 +-- 七年级生 技术交流 .NET 与JAVA的区别是什么呀? 2008-9-1 2 +-- 小雨点 生活百科 今年夏天最流行什么 有谁知道今年夏天最流行 2008-9-10 0 +-- + +select * from bbsUsers +select * from bbsSection + + +insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount) values('2','4','范跑跑','谁是范跑跑','2008-7-8','1'), +('3','2','.NET','与Java的区别是什么呀?','2008-9-1','2'),('4','4','今年夏天最流行','有谁知道今年夏天最流行','2008-9-10','0') + + +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 + +-- 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) + +delete from bbsTopic where tUID='2' +delete from bbsUsers where uName='逍遥' + +-- 6.因为小雨点发帖较多,将其积分增加10分 + +update bbsUsers set uPoint=12 where uName='小雨点' + +-- 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) +delete from bbsTopic where tSID=4 +-- 8.因回帖积累太多,现需要将所有的回帖删除 +delete from bbsReply + + + + \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1d3908c53862db37c5e58d7014d0199f2d0a73b6 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.sql" @@ -0,0 +1,296 @@ + +use master +go +create database TestDB +on +( name='TestDB', + filename='D:\test\TestDB.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='company_log', + filename='D:\test\TestDB_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +go +use TestDB +go +create table TestDB +( typeld int primary key, + typeName varchar(10) not null +) + + +create table loginInfo +( LoginId int primary key , + LoginName nvarchar(10) not null unique , + LoginPwd nvarchar(20) not null default(123456), + LoginSex nchar(1) default('男') check(LoginSex='男'or LoginSex='女'), + LoginBrithday date, + LogintypeName varchar(10) not null +) +use master +go +create database company +on +( name='company', + filename='D:\test\company.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='company_log', + filename='D:\test\company_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +go +use company +go +create table sectionInfo +( sectionID int primary key identity(1,1), + sectionName varchar(10) not null +) + + +create table userInfo +( userNo int primary key not null identity(1,1), + userName varchar(10) unique not null check(userName>4), + userSex varchar(2) not null check(userSex='男'or userSex='女'), + userAge int not null check(userAge>=1 and userAge<=100), + userAddress varchar(50) default('湖北'), + userSection int references sectionInfo(sectionID), +) + + +create table workInfo +( workId int primary key not null, + userId int references userInfo( userNo) not null, + workTime datetime not null, + workDescription varchar(40) not null check(workDescription in('迟到','早退','旷工','病假')) +) + +use master +go +create database studentmanage +on +( name='studentmanage', + filename='D:\test\studentmanage.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='studentmanage_log', + filename='D:\test\studentmanage_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +go +use studentmanage +go +create table classinfo +( classid int primary key identity(1,1), + classname char(5) not null unique, + timeday date not null, + classspeak text +) +create table studentinfo +( stuid int primary key identity(1,1), + stuname nchar(10) unique check(stuname>2), + stusex nchar(1) not null default('男') check(stusex in('男','女')), + stuage int not null check(stuage>=15 and stuage<=40), + stuaddress nvarchar(4) default('湖北武汉'), + stuclassid int references classinfo(classid) +) + +create table courseinfo +( courseid int primary key identity(1,1), + coursename nchar(10) not null unique, + coursespeak text +) +create table scoreinfo +( scoreid int primary key identity(1,1), + scorestuid int not null, + scorecourseid int not null, + score int check(score>=0 and score<=100) +) +----------------- +use master +go +create database house +on +( name='house', + filename='D:\test\house.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='house_log', + filename='D:\test\house_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +go +use house +go +create table tblUser +( userId int primary key identity(1,1), + userName nvarchar(10) not null, + userTel char(11) not null +) +create table tblHouseType +( typeId int primary key identity(1,1), + typName nchar(4) not null +) +create table tblQx +( qxId int primary key identity(1,1), + qxName nchar(2) not null +) +create table tblHouseInfo +( id int primary key identity(1,1), + desc1 nvarchar(10) not null, + userId int, + zj int, + shi int, + ting int, + typeId int, + qxId int +) + + + +------------------ +use master +go +create database bbs +on +( name='bbs', + filename='D:\test\bbs.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='bbs_log', + filename='D:\test\bbs_log.ldf', + size=5mb, + maxsize=5000mb, + 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 +) +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,4,'谁问的谁就是范跑跑','20210316'), + (1,3,'这个更简单','20210316'), + (1,3,'今年夏天最流行内裤外穿','20210316') +select*from bbsReply + +-- 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) +select*from bbsUsers +select*from bbsSection --sUid +select*from bbsTopic --tUID +delete from bbsTopic where tUID=2 +select*from bbsReply --rUID + +delete from bbsUsers where uName='逍遥' + +-- 6.因为小雨点发帖较多,将其积分增加10分 + select uPoint from bbsUsers where uName='小雨点' + update bbsUsers set uPoint=12 where uName='小雨点' + +-- 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) + select*from bbsTopic + alter table bbsReply drop constraint FK__bbsReply__rTID__1DE57479 + delete from bbsTopic where tSID=3 + +-- 8.因回帖积累太多,现需要将所有的回帖删除 +select*from bbsReply +truncate table bbsReply diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..320245f2796a2fbf889bdb7788e2bc1b5b5f2221 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery1.sql" @@ -0,0 +1,182 @@ +CREATE DATABASE Students +ON +(NAME='Students_data', +FILENAME='C:\app\Students_data.mdf', +SIZE=5MB, +MAXSIZE=100MB, +FILEGROWTH=10% +) +LOG ON +( +NAME='Students_log', +FILENAME='C:\app\Students_log.ldf', +SIZE=1MB, +FILEGROWTH=5MB +) +use Students +go + + + +-- 1、 +create table typeInfo +( +typeId int primary key identity(1,1), +typeName varchar(10) not null, +) +create table loginInfo +( +LoginId int primary key identity(1,1), +LoginName varchar(10) not null unique, +LoginPwd varchar(20) not null default('1,2,3,4,5,6'), +Logsex char(1), +Logbrithday datetime, +Loghuiyuan varchar +) + + +-- 2、 +create database company +on +( +name='company_data', +filename='C:\xxx\company_data.mdf', +size=5mb, +maxsize=50mb, +filegrowth=10% +) + +log on +( +name='company_log', +filename='C:\xxx\company_log.ldf', + +size=5mb, +maxsize=50mb, +filegrowth=10% +) + +use company +go + +create table sectionInfo +( +sectionID int primary key, +sectionName varchar(10) not null +) +create table userInfo +( +userNo int primary key not null, +userName varchar(10) unique not null check(userName>4), +userSex varchar(2) not null check(userSex='男'or userSex='女'), + userAge int not null check(userAge>=1 or userAge<=100), + userAddress varchar(50) default('湖北'), + userSection int foreign key references sectionInfo(sectionID) +) +create table workInfo +( + workId int primary key not null, + userId int foreign key references userInfo(userNo), + workTime datetime not null, + workDescription varchar(40) not null check(workDescription='迟到'or workDescription='早退'or workDescription='矿工' or workDescription='病假'or workDescription='事假') +) + +-- 3、 +create database School +on +( + name='School', + filename='F:\School.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='School_log', + filename='F:\School_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use School +go +create table Classinfo +( + classid int primary key, + classname varchar(10) not null unique, + classremark ntext +) +create table studentinfo +( + stunum int primary key, + stuname varchar check (len(stuname)>2) unique, + stusex varchar(2) default('男') check(stusex='男' or stusex='女'), + stuage int check(stuage>=15 or stuage<=40) not null, + stuaddress nvarchar(50) default('湖北武汉'), + stuclassid int references Classinfo(classid) +) +create table information +( + number int primary key, + kechenname varchar unique not null, + miaoshu ntext +) +create table score +( + scorenum int primary key, + scorestuid int references studentinfo(stunum) not null, + scoreid int references information(number) not null, + score int check(score>=0 or score<=100) +) +--4、 +create database fangwu +on +( + name='fangwu', + filename='F:\fangwu.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='fangwu_log', + filename='F:\fangwu_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use fangwu +go + +create table tblUser +( + userId int primary key identity(1,1), + userName varchar, + userTel nvarchar(10) +) +create table tblHouseType +( + typeId int primary key identity(1,1), + typName varchar check(typName='别墅'or typName='普通住宅'or typName='平房' or typName='地下室') not null +) +create table tblQx +( + qxId int primary key identity(1,1), + qxName varchar check(qxName='武昌'or qxName='汉阳' or qxName='汉口') not null +) + +create table tblHouseInfo +( + id int primary key identity(1,1), + userId int foreign key references tblUser (userId), + zj money, + shi varchar, + ting int, + typeId int foreign key references tblHouseType (typeId), + qxId int foreign key references tblQx (qxId) +) + + + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6a0b548075ea1d002a485372f8ce7de2bfa6954d --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\346\261\237\346\273\250/SQLQuery2.sql" @@ -0,0 +1,110 @@ +-- 一、 +create database bbs +on +( + name='bbs', + filename='F:\bbs.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='F:\bbs_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use bbs +go + +create table bbsUsers +( + uuID 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 primary key (uuID) +alter table bbsUsers add constraint UK unique(uName) +alter table bbsUsers add constraint CK check(uSex='男' or uSex='女' ) +alter table bbsUsers add constraint CK1 check(uAge>=15 or uAge<=60 ) +alter table bbsUsers add constraint CK2 check(upoint>=0) + +create table bbsSection +( + sID int 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(uuID) + + + +create table bbsTopic +( + tID int primary key, + tUID int foreign key references bbsUsers(uuID), + tSID int foreign key references bbsSection(sID), + +) +alter table bbsTopic add tTitle varchar(100) not null +alter table bbsTopic add tMsg text not null +alter table bbsTopic add tTime datetime +alter table bbsTopic add tCount int + +create table bbsReply +( + rID int primary key, + rUID int foreign key references bbsUsers(uuID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女',20,0 union +select'逍遥','男',18,4 union +select'七年级生','男',19,2 +select * from bbsUsers + +--2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中, +--提示查询部分列:select 列名1,列名2 from 表名 +select uName,uPoint into bbsPoint from bbsUsers + +insert into bbsSection(sName, sID) +select '技术交流' , 1union +select '读书世界' ,2 union +select '生活百科' ,3 union +select ' 八卦区' ,4 +select * from bbsSection + +-- 4.向主贴和回帖表中添加几条记录 +-- 发帖人 板块名 帖子标题 +-- 帖子内容 帖时间 回复数量 +insert into bbsTopic( tid,tUID,tSID,tTitle,tMsg,tTime,tCount ) +select 1,3, 4,'范跑跑','谁是范跑跑','2008-7-8',1 union +select 2,2, 1,' .NET','与JAVA的区别是什么呀?','2008-9-1',2 union +select 3,1, 3,' 今年夏天最流行什么','有谁知道今年夏天最流行','2008-9-10',0 +select * from bbsTopic + +select * from bbsReply +insert into bbsReply(rid,rUID,rMsg,rTime) +select 1,2,'钟老师是范跑跑','2008-7-8'union +select 2,1,'没有区别就是最大的区别','2008-7-8'union +select 3,3,'今年流行上海滩','2008-7-8' + +delete bbsUsers where uName='小雨点' --深感抱歉 删错人了 +alter table bbsReply drop FK__bbsReply__rUID__4AB81AF0 +alter table bbsTopic drop FK__bbsTopic__tUID__46E78A0C +select * from bbsReply +--给逍遥加分10 +update bbsUsers set upoint=12 where uName='逍遥' +--删除生活百科板块 +alter table bbsTopic drop FK__bbsTopic__tSID__47DBAE45 +delete bbsSection where sName='生活百科' +--删除所有回帖 +delete bbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..19283a0b7391d7830725729fc5cd6330f839297f --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery1.sql" @@ -0,0 +1,34 @@ +use master +go +create database TestDB +on( +name='typeInfo', +filename='D:\318\typeInfo.mdf', +size=5, +maxsize=10, +filegrowth=1 +) +log on( +name='typeInfo_log', +filename='D:\318\typeInfo_log.ldf', +size=5, +maxsize=10, +filegrowth=1 +) +go +use TestDB +go +create database typeInfo +( +typeId int primary key identity(1,1), +typeName varchar(10) not null +) +create database loginInfo +( +LoginId int primary key identity(1,1), +LoginName text not null unique, +LoginPwd text not null default('123456'), +Logsex nvarchar(1) default('男'), +birthday datetime, +member nvarchar +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b44bb74d2a465fd0c2d135641aa9cc6d8e728f37 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery2.sql" @@ -0,0 +1,38 @@ +use master +go +create database company +on( +name='company', +filename='D:\ljc\company.mdf', +size = 5, +maxsize=10, +filegrowth = 1 +) +log on( +name='company_log', +filename='D:\ljc\company_log.ldf', +size = 5, +maxsize=10, +filegrowth = 1 +) +create table sectionInfo +( +sectionID int primary key, +sectionName varchar(10) not null +) +create table userInfo +( +userNo int primary key not null, +userName varchar(10) unique not null, +userSex varchar(2) not null check(userSex='男'or userSex='女'), +userAge int not null check(userAge>=0 and userAge<=100), +UserAddress varchar(50) default('湖北'), +UserSection int foreign key references SectionInfo(SectionId) +) +create table WorkInfo +( +WorkId int primary key identity(1,1) not null, +UserId int foreign key references UserInfo(UserNo) not null, +WorkTime datetime not null, +WorkDescription varchar(40) not null check(WorkDescription='迟到' or WorkDescription='早退'or WorkDescription='旷工' or WorkDescription='病假' or WorkDescription='事假') +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery3.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..87176afabcab471be1c9932d418cfdfe4fc5b771 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery3.sql" @@ -0,0 +1,51 @@ +use master +go +create database Class +on +( +name='Class', +filename='D:\ljc\Class.mdf', +size=10, +maxsize=50, +filegrowth=10% +) +log on +( +name='Class_log', +filename='D:\ljc\Class_log.ldf', +size=10, +maxsize=50, +filegrowth=10% +) +go +use Class +go +create table ClassInfo +( +ClassId int primary key identity(1,1), +ClassName varchar(4) unique not null, +OpenTime datetime not null, +ClassDescribe nvarchar(50) +) + +create table StuInfo +( +StuNo int primary key identity(1,1), +StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女') not null, +StuAge int check(StuAge>=15 and StuAge<=40) not null, +StuAdress nvarchar(4) default('湖北武汉'), +ClassId int foreign key references ClassInfo(ClassId) +) +create table Course +( +CourseNo int primary key identity(1,1), +CourseName nvarchar(4) unique not null, +CourseDescribe nvarchar(50) +) +create table Credit +( +CreditNo int primary key identity(1,1), +StuNo int foreign key references StuInfo(StuNo), +CourseNo int foreign key references Course(CourseNo), +Credit int check(Credit>=0 and Credit<=100) +) diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery4.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..78534ae8f24b3ae88040793cafb927c38a78a15a --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery4.sql" @@ -0,0 +1,51 @@ +use master +go +create database House +on +( +name='House', +filename='D:\ljc\House.mdf', +size=10Mb, +maxsize=50Mb, +filegrowth=10% +) +log on +( +name='House_log', +filename='D:\ljc\House_log.ldf', +size=10Mb, +maxsize=50Mb, +filegrowth=10% +) +go +use House +go +create table TblUser +( +UserId int primary key identity(1,1), +UserName nvarchar(4) unique not null, +UserTel varchar(11) unique not null +) + +create table TblHouseType +( +TypeId int primary key identity(1,1), +TypeName nvarchar(4) unique not null +) + +create table TblQx +( +QxId int primary key identity(1,1), +QxName nvarchar(4) unique not null +) +create table HouseInfo +( +Id int primary key identity(1,1), +Desc1 nvarchar(50), +UserId int foreign key references TblUser(UserID), +zj money, +shi int, +ting int, +TypeId int foreign key references TblHouseType(TypeId), +QxId int foreign key references TblQx(QxId) +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery5.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d73f97480aebbf2fca4072e6ae4e28d4cbe4fd4d --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\230\211\345\237\216/SQLQuery5.sql" @@ -0,0 +1,94 @@ +use master +go +create database bbs +on +( +name='bbs', +filename='D:\ljc\bbs.mdf', +size=10Mb, +maxsize=50Mb, +filegrowth=10% +) +log on +( +name='bbs_log', +filename='D:\ljc\bbs_log.ldf', +size=10Mb, +maxsize=50Mb, +filegrowth=10% +) +go +use bbs +go +create table BbsUsers +( +UserId int identity(1,1), +UserName varchar(10) not null, +UserSex varchar(2) not null, +UserAge int not null, +UserPoint int not null +) +alter table BbsUsers add constraint PK_BbsUser_UserId primary key(UserId) +alter table BbsUsers add constraint UK_BbsUser_UserName unique(UserName) +alter table BbsUsers add constraint CK_BbsUser_UserSex check(UserSex='男' or UserSex='女') +alter table BbsUsers add constraint CK_BbsUser_UserAge check(UserAge>=15 and UserAge<=60) +alter table BbsUsers add constraint CK_BbsUser_UserPoint check(UserPoint>=0) + +create table BbsTopic +( +TopicId int primary key identity(1,1), +TopicUserId int foreign key references BbsUsers(UserId), +TopicSId int foreign key references BbsSection(SectionId), +TopicTitle varchar(100) not null, +TopicMsg text not null, +TopicTime datetime, +TopicCount int +) +create table BbsReply +( +ReplyId int primary key identity(1,1), +RepluUid int foreign key references BbsUsers(UserId), +ReplyTid int foreign key references BbsTopic(TopicId), +ReplyMsg text not null, +ReplyTime datetime +) +create table BbsSection +( +SectionId int Identity(1,1), +SectionName varchar(10) not null, +SectionUid int +) +alter table BbsSection add constraint PK_BbsSection_SectionId primary key(SectionId) +alter table BbsSection add constraint FK_BbsSection_SectionUid foreign key(SectionUid) references BbsUsers(UserId) + +insert into BbsUsers values +('小雨点','女','20','0'), +('逍遥','男','18','4'), +('七年级生','男','19','2') + +select UserName,UserPoint into BbsPoint from BbsUsers + +insert into BbsSection values +('技术交流',3), +('读书世界',5), +('生活百科',3), +('八卦区',5) +select * from BbsTopic +insert into BbsTopic values +(4,6,'范跑跑','谁是范跑跑','2008-7-8',1), +(5,3,'.NET','与JAVA的区别是什么呀?','2008-9-1',2), +(3,5,'今年夏天最流行什么','有谁知道及那年夏天最流行什么呀?','2008-9-10',0) +insert into BbsReply values +(3,5,'你是飯桶','2008-7-9'), +(3,6,'本質一樣','2008-9-2'), +(4,7,'最流行吃飯','2008-9-11') +alter table BbsReply drop constraint FK__BbsReply__RepluU__1FCDBCEB +alter table BbsTopic drop constraint FK__BbsTopic__TopicU__1BFD2C07 +delete BbsUsers where UserName='逍遥' + +update bbsUsers set UserPoint=12 where UserName='小雨点' + +alter table BbsTopic drop constraint FK__BbsTopic__TopicS__1CF15040 +delete BbsSection where SectionName='生活百科' + +delete BbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6eb22008914f67d3f530aee2c0158b3384f0cddb --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery2.sql" @@ -0,0 +1,163 @@ +use master +go +create database TestDB +on +( + name='TestDB', + filename='D:\TestDB.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on +( + name='TestDB_log', + filename='D:\TestDB_log.ldf', + size=5, + maxsize=100, + filegrowth=10% +) +go +use TestDB +go +create table typeInfo +( + typelf int primary key identity(1,1), + typeName varchar(10) not null, +) +create table loginInfo +( + Loginld int primary key identity(1,1), + LoginName char(10) not null unique, + LoginPwd char(20) not null default(123456), + loginSex char(1) check(loginSex= '男' or loginSex= '女') default('男'), + Birthday datetime not null, + Member int not null +) +use master +go +create database company +on +( + name ='company', + filename='D:\company.mdf', + size =5, + maxsize =100, + filegrowth=10% +) +log on +( + name ='company_log', + filename='D:\company_log.ldf', + size =5, + maxsize =100, + filegrowth=10% +) +use company +go +create table sectionInfo +( + sectionID int primary key identity(1,1), + srctionName varchar(10) not null +) +create table userInfo +( + userNo int primary key identity(1,1), + userName varchar(10) unique not null check(userName >=4), + userSex varchar(2) not null check(userSex='男'or userSex='女'), + userAge int not null check(userAge>1 and userAge<=100), + userAddress varchar(50) default('湖北'), + userSection int constraint co_sectionID foreign key references sectionInfo (sectionID) +) +create table workInfo +( + workId int primary key identity(1,1), + userId int constraint id_userNo foreign key references userInfo (userNo) not null, + workTime datetime not null, + workDescription varchar(40) not null check(workDescription='迟到' or workDescription='早退' or workDescription='旷工' or workDescription='病假' or workDescription='事假') +) +create table ClassO +( + classid int primary key identity (1,1), + classname nvarchar(20) unique not null, + datatime datetime not null, + classDE text +) +use master +go +create database Student +use Student +go +create table ClassO +( + classid int primary key identity (1,1), + classname nvarchar(20) unique not null, + datatime datetime not null, + classDE text +) +create table stInfo +( + stuid int primary key identity (1,1), + stuName varchar(20) check(stuName>=4), + stuSex char(2) check(stuSex='男'or stuSex='女') default('男') not null, + stuAge int check(stuAge>=15 and stuAge<=40), + stuAddress text default('湖北武汉'), + classID int +) +create table classInfo +( + classId int primary key identity(1,1), + className char(40) unique, + CourseInfo ntext +) +create table performance +( + performanceId int primary key identity(1,1), + stuId int not null, + classId int not null, + grade int check(grade>=0 and grade <=100 ) +) +create database fangwu +on +( + name='fangwu', + filename='F:\fangwu.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='fangwu_log', + filename='F:\fangwu_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +create table tblUser +( + userId int primary key identity(1,1), + userName varchar, + userTel nvarchar(10) +) +create table tblHouseType +( + typeId int primary key identity(1,1), + typName varchar check(typName='别墅'or typName='普通住宅'or typName='平房' or typName='地下室') not null +) +create table tblQx +( + qxId int primary key identity(1,1), + qxName varchar check(qxName='武昌'or qxName='汉阳' or qxName='汉口') not null +) + +create table tblHouseInfo +( + id int primary key identity(1,1), + userId int foreign key references tblUser (userId), + zj money, + shi varchar, + ting int, + typeId int foreign key references tblHouseType (typeId), + qxId int foreign key references tblQx (qxId) +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery3.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8a945893d062f49832acfeda19f2046b822383bb --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery3.sql" @@ -0,0 +1,124 @@ +use master +go +create database bbs +on +( name='bbs', + filename='D:\bbs.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='bbs_log', + filename='D:\bbs_log.ldf', + size=5mb, + maxsize=5000mb, + 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 +) +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,4,'谁问的谁就是范跑跑','20210316'), + (1,3,'这个更简单','20210316'), + (1,3,'今年夏天最流行内裤外穿','20210316') +select*from bbsReply + +-- 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) +select*from bbsUsers +select*from bbsSection --sUid +select*from bbsTopic --tUID +delete from bbsTopic where tUID=2 +select*from bbsReply --rUID + +delete from bbsUsers where uName='逍遥' + +-- 6.因为小雨点发帖较多,将其积分增加10分 + select uPoint from bbsUsers where uName='小雨点' + update bbsUsers set uPoint=12 where uName='小雨点' + +-- 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) + select*from bbsTopic + alter table bbsReply drop constraint FK__bbsReply__rTID__1DE57479 + delete from bbsTopic where tSID=3 + +-- 8.因回帖积累太多,现需要将所有的回帖删除 +select*from bbsReply +truncate table bbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..76311c5360017cae56bb7cf21b256e809e53fa43 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery2.sql" @@ -0,0 +1,84 @@ +use master +go +create database bbs +on +( +name='bbs', +filename='D:\testbbs.mdf', +size=5mb, +maxsize=500mb, +filegrowth=1mb +), +( +name='bbs', +filename='D:\testbbs_log.ldf', +size=5mb, +maxsize=500mb, +filegrowth=1mb +) +create table bbsusers +( +uid int primary key identity(1,1), +uname varchar(10) unique not null, +usex varchar(2) not null check(usex in('男','女')), +uage int not null check(uage>=15 and uage<=60), +upoint int not null check(upoint>=0) +) +create table bbssection +( +sid int primary key identity(1,1), +sname varchar(10) not null, +suid int foreign key 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 not null, +tcount int +) +create table bbsreply +( +rld int primary key identity(1,1), +ruld int foreign key references bbsusers(uid), +tsld int foreign key references bbssection(sid), +rmsg text not null, +rtime datetime +) +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) +select * from bbsusers +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) +select * from bbsusers +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(ruld,rmsg,rtime) values(1,'我就是','2008-7-8') +insert into bbsreply(ruld,rmsg,rtime) values(6,'哦flak积分','2008-7-8') +insert into bbsreply(ruld,rmsg,rtime) values(1,'爱科技','2008-7-8') +select * from bbsUsers +delete bbsusers where uname ='逍遥' +alter table bbssection drop NK +alter table bbstopic drop FK__bbstopic__tuid__5315624 +update bbsusers set upoint ='10' where uname='小雨点' + +select * from bbssection +delete bbssection where sname='生活百科' + +select * from bbsreply +delete bbsreply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery4.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8e2c8343710f4af033ddf503ad4447c9775a91cb --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\234\346\265\267\345\275\252/SQLQuery4.sql" @@ -0,0 +1,157 @@ +use master +go +create database testdb +on +( +name='testdb', +filename='D:\testdb.mdf', +size=8mb, +maxsize=2000mb, +filergrowth=1mb +) +log on +( +name='testdb', +filename='D:\testdb_log.ldf', +size=8mb, +maxsize=2000mb, +filergrowth=1mb +) +create table typeinfo +( +typeld int primary key identity(1,1) not null, +typename varchar(10) not null, +) +create table logininfo +( +loginld int primary key identity(1,1) not null, +loginname char(10) not null unique, +loginpwd char(20) not null default(123456), +loginsex char(2) check(loginsex='男' or loginsex='女'), +loginbrithday int, +LogintypeName char(10) not null +) +create database company +on +( +name='company', +filename='D:\company.mdf', +size=8mb, +maxsize=2000mb, +filergrowth=1mb +), +( +name='company', +filename='D:\company_log.1df', +size=8mb, +maxsize=2000mb, +filergrowth=1mb +) +create table sectioninfo +( +sectionid int primary key identity(1,1), +sectionname varchar(10) not null +) +create table userinfo +( +userno int primary key not null identity(1,1), +username varchar(10) unique not null check(username>4), +usersex varchar(2) not null check(usersex='男'or usersex='女'), +useraddress varchar(50) default('湖北'), +usersction int references sectioninfo(sectionid), +) +create table worklnfo +( +workld int primary key not null, +userld int references userinfo(userno) not null, +worktime datetime not null, +workdescription varchar(40) not null check(workdescription in('迟到','早退','旷工','病假','事假')), +) +create database studentmange +on +( +name='studentmange', +filename='D:\studentmange.mdf', +size=8mb, +maxsize=2000mb, +filergrowth=1mb +), +( +name='studentmange', +filename='D:\studentmange_log.ldf', +size=8mb, +maxsize=2000mb, +filergrowth=1mb +) +create table classlnfo +( +classid int primary key identity(1,1), +classname char(6) not null unique, +time date not null, +classspeak text +) +create table studentinfo +( +stuid int primary key identity(1,1), +stuname nchar(10) check(stuname>2), +stusex nchar(1) not null default('男') check(stusex in('男','女')), +stuage int not null check(stuage>=15 and stuage<=40), +stuaddress nvarchar(4) default('湖北武汉'), +stuclassid int references classinfo(classid), +) +create table courseinfo +( +courseid int primary key identity(1,1), +coursename nchar(10) not null unique, +coursespeak text +) +create table scoreninfo +( +scoreid int primary key identity(1,1), +scorestuid int not null, +scorecourseid int not null, +score int check(score>=0 and score<=100), +) +create database house +on +( +name='house', +filename='D:\house.mdf', +size=8mb, +maxsize='2000mb', +filegrowth=1mb +), +( +name='house', +filename='D:\house_log.1df', +size=8mb, +maxsize='2000mb', +filegrowth=1mb +) +create table tbluser +( +userid int primary key identity(1,1), +username nvarchar(10) not null, +usertel char(11) not null, +) +create table tblhousetype +( +typed int primary key identity(1,1), +qxname nchar(4) not null +) +create table tbllqx +( +qxid int primary key identity(1,1), +qxname nchar(2) not null +) +create table tblhouseinfo +( +id int primary key identity(1,1), +descl nvarchar(10) not null, +userid int, +zj int, +shi int, +ting int, +typeid int, +qxid int, +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3d7248165b59d3206bfd41cee0bea9765d4bdadd --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery1.sql" @@ -0,0 +1,185 @@ +use master +go + +create database TestDB +on +( + name='TestDB', + filename='D:\TestDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='TestDB_log.ldf', + filename='D:\TestDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use TestDB +go +create table typeInfo +( + typeld int primary key identity(1,1), + typeName varchar(10) not null +) + +create table loginInfo +( + Loginld int primary key identity(1,1), + LoginName nvarchar(10) not null unique, + LoginPwd nchar(20) not null default('123456'), + Sex nvarchar(1) check(Sex='女'or Sex='男'), + Birthday nvarchar(10), + MemType nvarchar(5) +) +go +use master +go +create database company +on +( + name='company', + filename='D:\company.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='company_log', + filename='D:\company_log.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use company +go +create table sectionInfo +( + sectionID int primary key identity(1,1), + sectionName varchar(10) not null +) + +create table userInfo +( + userNo int primary key identity(1,1) not null, + userName varchar(10) unique not null check(len(userName)>4), + userSex varchar(2) not null check(userSex='男'or userSex='女'), + userAge int not null check(userAge>=1 and userAge <=100), + userAddress varchar(50) default('湖北'), + userSection int constraint FK_userInfo_userSection references sectionInfo(sectionID) +) + +create table workInfo +( + workld int identity(1,1) primary key not null, + userld int constraint FK_workInfo_userld references userInfo(userNo), + workTime datetime not null, + workDescription varchar(40) not null check (workDescription='迟到' or workDescription='早退' or workDescription='旷工' or workDescription='病假' or workDescription='事假') +) +go +use master +go +create database Student +on +( + name='Student', + filename='D:\student.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Student_log', + filename='D:\student_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use Student +go +create table classInfo +( + classID int primary key identity(1,1), + className varchar(20) not null unique, + openTime varchar(10) not null, + classDescription text +) + +create table studentInfo +( + stuID int primary key not null, + Sex varchar(2) default('男') check(Sex='男' or Sex='女'), + stuAge int check(stuAge>=15 or stuAge<=40) not null, + stuAddress nvarchar(20) default('湖北武汉'), + classID int constraint FK_studentInfo_classID references classInfo(classID) +) +create table courseInfo +( + courseID int primary key identity(1,1), + courseName nvarchar(10) not null unique, + courseDescription text +) +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int constraint FK_scoreInfo_stuID references studentInfo(stuID), + courseID int constraint FK_scoreInfo_courseID references courseInfo(courseID), + Score int check(Score>=0 and Score<=100) +) +go +use master +go +create database House +on +( + name='House', + filename='D:\House.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='House_log', + filename='D:\House_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use House +go +create table tblUser +( + userID int primary key identity(1,1), + userName nvarchar(5) not null, + userTel char(11) not null +) +create table tblHouseType +( + typeID int primary key identity(1,1), + typName nvarchar(20) not null +) +create table tblQx +( + qxID int primary key identity(1,1), + qxName nvarchar(20) not null +) +create table tblHouseInfo +( + id int primary key identity(1,1), + userID int constraint FK_tblHouseInfo_userID references tblUser(userID), + zj int not null, + shi int not null, + ting int not null, + typeID int constraint FK_tblHouseInfo_typeID references tblHouseType(typeID), + qxID int constraint FK_tblHouseInfo_qxID references tblQx(qxID) +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..69c4c0b9449e4c6d0583361abdc5c12104f382aa --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\226\207\350\215\243/SQLQuery2.sql" @@ -0,0 +1,99 @@ +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 text 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 text 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='生活百科' + +delete bbsReply + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a9799b468943878965a18c3936e0f331da35906a --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery1.sql" @@ -0,0 +1,255 @@ +use master +create database TestDB1 +on + +( + + name='TestDB1 ', + + filename='D:\TestDB1.mdf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) + + + +log on + +( + + name='TestDB1_log', + + filename='D:\TestDB1_log.ldf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) + +go + +use TestDB1 +go + +create table typelnfo +( +typeld int primary key not null identity(1,1), +typeName varchar(10) not null , +) +create table loginlnfo +( +loginld int primary key not null identity(1,1), +loginName nvarchar(10) not null unique, +loginpwd nvarchar(20) not null default('123456'), +loginSex char(2)default('男'), +loginday int , +loginvip int, +) +--公司数据库 +create database company +on +( + + + name='company', + + filename='D:\company.mdf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) + + + +log on + +( + + name='company_log', + + filename='D:\company_log.ldf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) + +go + +use company +go +--部门表 +create table sectionlnfo +( +sectionID int primary key not null , +sectionName varchar(10) not null , +) +--员工信息表 +create table userlnfo +( +userNo int primary key not null , +userName varchar(10) unique not null check(len(userName)>=4), +userSex varchar(2) not null check(userSex='男' or userSex='女'), +userAge int not null check(userAge>=1 and userAge<=100), +userSection int foreign key references sectionlnfo(sectionID) +) +--员工考勤表 +create table worklnfo +( +workld int primary key not null , +userld int foreign key references userlnfo(userNo) not null, +workTime datetime not null, +workDescription varchar(40) not null check (workDescription='迟到'or workDescription='矿工' or workDescription= '病假' or workDescription= '事假' or workDescription='早退'), +) +--学校数据库 +create database shoolinfo +on +( + + + name='shoolinfo', + + filename='D:\shoolinfo.mdf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) + + + +log on + +( + + name='shoolinfo_log', + + filename='D:\shoolinfo_log.ldf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) + +go + +use shoolinfo +go + +create table class +( +classid int primary key not null, +classname char not null unique , +classtime int not null , +classdepict varchar not null , +) + +create table student +( +stuinfo int primary key not null , +stuname nvarchar(10) check(len(stuname)>=2) not null , +stusex char default('男') not null check(stusex ='男'or stusex ='女'), +stuage int check(stuage>=15 and stuage<=40) not null , +stuhome nvarchar default('贵州'), +stuhao int not null , +) +--课程表 +create table course +( +courseinfo int primary key not null , +coursename char not null unique, +coursedepict varchar(50) not null , +) +--成绩表 +create table grate +( +grateinfo int primary key not null, +gratehao int not null, +grateke int not null, +grateing int check(grateing>=1 or grateing <=100) +) +--房屋数据库 +create database house +on +( +name =house, +filename='D:\house.mdf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) + + + +log on + +( + + name='house_log', + + filename='D:\house_log.ldf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) +go +use house +go +--房屋主人表 +create table tbluser +( +userld int not null primary key , +username char default('杨梦林') not null, +usertel int not null default('8848'), +) +--房屋类型表 +create table tbhousetype +( +typeld int primary key not null , +typname char default('梦林妙妙屋') not null, +tycounty nvarchar(30) default('北京天安门') not null , +tycountyname nvarchar(20) default('天安门小厕所'), +) +--房屋信息表 +create table tbhouseinfo +( +tbid int primary key not null , +tbdesc varchar(10) check(tbdesc >=100) not null , +tbrent int not null check(tbrent>=100000000000000 and tbrent<=100000000000000000000), +tbliving nvarchar(10) check(tbliving>11000000000000000000000000000000), +) + + + + + + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ce5880a25c44a69deed37f6e6d0725efe6dc332d --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery2.sql" @@ -0,0 +1,109 @@ +use master +create database bbs +on +( + +name =bbs, +filename='D:\bbs.mdf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) + + + +log on + +( + + name='bbs_log', + + filename='D:\bbs_log.ldf', + + size=6MB, + + maxsize=100MB, + + filegrowth=10MB + +) +go +use bbs +go +--用户信息表 +create table bbsusers +( +uid int identity(1,1) not null , +unaem varchar(10), +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 UN_ unique (unaem) +alter table bbsusers add constraint CK_ check(usex='男'or usex='女') +alter table bbsusers add constraint CK_ check(uage>=15 or uage<=60) +--板块表 +create table bbssection +(sid int identity(1,1), +snaeme varchar(10)not null , +suid int +) +alter table bbssection add constraint PK_sid primary key (sid) +alter table bbssection add constraint FK foreign key (suid) references bbsusers(uid) + + +--主贴表 +create table bbstopic +( +tid int not null 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 +) +insert into bbstopic(tuid,tsid,ttitle,tmsg ,ttime ,tcount ) values(2, 0,'马巧晶','谁是马巧晶',2008-7-8,1), + (3, 4,'.NET ',' 与JAVA的区别是什么呀? ','2008-9-1 ','2'), + (5, 6,'今年夏天最流行什么 ',' 有谁知道今年夏天最流行','2008-9-10','0') + + +--回帖表 +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 bbsReply(rUID,rTID ,rMsg,rTime) values(1,2,'二班长的最丑的就是范跑跑',0123), + (4,3,'我挂科的不知道这么深奥的问题',0123), + (5,6,'这个夏天最流行的当然是黑丝啊破洞的那种',0123) + + +insert into bbsusers(unaem,usex,uage,upoint) values('杜海彪','女',0,0) +insert into bbsusers(unaem,usex,uage,upoint) values('马巧晶','女',18,4) +insert into bbsusers(unaem,usex,uage,upoint) values('大一新生','男',19,2) + +--备份 + +select unaem ,upoint into bbspoint from bbsusers + +--开设 +insert into bbssection(snaeme ,suid) values('技术交流','0'),( '读书世界', '0'),('生活百科' , '0'),('八卦区' ,'0') + + delete bbsusers where unaem ='杜海彪' + --增加积分 + update bbsusers set upoint='10' where unaem='马巧晶' + select * from bbssection +delete bbssection where snaeme='生活百科' --删表回贴表 + delete bbsreply + truncate + \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..034649e943f1fdd630cf01554dad921faf091522 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery1.sql" @@ -0,0 +1,179 @@ +--1 +use master +go + +create database TestDB +on +( + name = TestDB, + filename = 'D:\数据库\TestDB.mdf' +) +log on +( + name = TestDB_log, + filename = 'D:\数据库\TestDB_log.ldf' +) + +use TestDB + +create table typeInfo +( + typeId int primary key identity(1,1), + typeName nvarchar(20) not null +) + +create table loginInfo +( + LoginId int primary key identity, + LoginName nvarchar(10) not null unique, + LoginPwd nvarchar(20) not null default(123456), + LoginSex nvarchar(1) check(LoginSex in ('男','女')), + LoginBirDay date, + LoginSort nvarchar(10) +) + + +--2 +use master +go + +create database company +on +( + name = 'company', + filename = 'D:\数据库\company.mdf', + size = 5, + maxsize = 50, + filegrowth = 10% +) +log on +( + name = 'company_log', + filename = 'D:\数据库\company_log.ldf' +) + +use company + +create table sectionInfo +( + sectionID int primary key identity, + sectionName varchar(10) not null +) + +create table userInfo +( + userNo int identity primary key not null, + userName varchar(10) unique not null check(len(userName)>4) , + userSex varchar(2) not null check(userSex in ('男','女')), + userAge int not null check(userAge>1 and userAge<=100), + userAddress varchar(50) default('湖北'), + userSection int foreign key references sectionInfo(sectionID) +) + +create table workInfo +( + workId int identity primary key not null, + userId int foreign key references userInfo(userNo), + workTime datetime not null, + workDescription varchar(40) not null check(workDescription in ('迟到','早退','旷工','病假','事假')) +) + + +--3 +use master +go + +create database class +on +( + name = 'class', + filename = 'D:\数据库\class.mdf' +) + +log on +( + name = 'class_log', + filename = 'D:\数据库\class_log.ldf' +) + +create table classInfo +( + classID varchar(10) not null unique, + classStart datetime not null, + classdescription text +) + +create table student +( + ID int primary key identity, + Name nvarchar(20) check(len(Name)>2) unique, + Sex nvarchar(1) check(Sex in ('男','女')) not null, + Age int check(Age>15), + Home nvarchar(10) default('湖北武汉'), + classID int references classInfo(classID) +) + +create table course +( + course int primary key identity, + subjects nvarchar(10) not null unique, + describe text +) + +create table resullt +( + resulltID int primary key identity , + ID int not null , + courseID int not null references course(course), + report int check(report>=0 and report<= 100) +) + +--4 +use master +go + +create database house +on +( + name = 'house', + filename = 'D:\数据库\house.mdf' +) + +log on +( + name = 'house_log', + filename = 'D:\数据库\house_log.ldf' +) + +use house + +create table tbUser +( + userId int identity(1,1) primary key , + useName nvarchar(15) not null, + userTel varchar(11) not null +) + +create table tblHouseType +( + typeId int primary key , + typName nvarchar(10) check(typName in ('别墅','普通住宅','平房','地下室')), +) + +create table tblQx +( + qxId int primary key , + qxName nvarchar(2) check(qxName in ('武昌','汉阳','汉口')) +) + +create table tblHouseInfo +( + id int , + userid int references tbUser(userid), + zj int not null , + shi int not null, + ting int not null, + typeld int references tblHouseType(typeId), + qxId int references tblQx(qxId) +) + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7efb84e93df862768feb7e4008c6f4411d520b09 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery2.sql" @@ -0,0 +1,113 @@ +use master +go + +create database bbs +on +( + name = 'bbs', + filename = 'D:\数据库\bbs.mdf' +) + +log on +( + name = 'bbsUser_log', + filename = 'D:\数据库\bbs_log.ldf' +) + +use bbs + +create table bbsUsers +( + UID int identity 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_s primary key(UID) +alter table bbsUsers add constraint UK_b unique(uName) +alter table bbsUsers add constraint CK_b check(uSex in ('男','女')) +alter table bbsUsers add constraint CK_a check( uAge>=15 and uAge<=60) +alter table bbsUsers add constraint CK_c check( uPoint>=0) + +create table bbsTopic +( + tID int identity, + tUID int , + tSID int , + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) + +alter table bbsTopic add constraint FK_S foreign key(tUID) references bbsUsers(UID) + +create table bbsReply +( + rID int identity, + rUID int, + rTID int, + rMsg text not null, + rTime datetime +) +alter table bbsReply add constraint PK_z primary key(rID) +alter table bbsReply add constraint FK_d foreign key(rUID) references bbsUsers(UID) + +create table bbsSection +( + sID int identity, + sName varchar(10) not null, + sUid int, +) +alter table bbsTopic add constraint FK_l foreign key(tSID) references bbsSection(sID) +alter table bbsSection add constraint PK_q primary key(sID) +alter table bbsSection add constraint PK_p foreign key(sUid) references bbsUsers(UID) + +insert into bbsUsers (uName,uSex,uAge,uPoint) +select '小雨点','女',20 ,0 union +select '逍遥','男',18 ,4 union +select '七年级生','男',19 ,2 + +select uName,uPoint into bbsPoint from bbsUsers +select * from bbsPoint + +select * from bbsUsers +select * from bbsSection + +insert into bbsSection (sName, sUid)values('技术交流',3),( '读书世界',1),( '生活百科',3),('八卦区',1) + +select * from bbsSection +select * from bbsTopic + +insert into bbsTopic (tUID,tSID,tTitle,tMsg,tTime,tCount)values +(2,4,'范跑跑','谁是范跑跑','2008-7-8',1), +(1,1,'.NET ','与JAVA的区别是什么呀?','2008-9-1','2'), +(3,3,'今年夏天最流行什么','今年夏天最流行什么','2008-9-10',0) +select * from bbsTopic + +insert into bbsReply (rUID,rTID,rMsg,rTime)values +(1,4,'范跑跑是一位老师','2008-07-09'), +(2,1,'.NET与JAVA区别很大','2008-10-09'), +(2,1,'.JAVA是一门编程语言','2008-10-09') + +select * from bbsReply + +alter table bbsTopic drop constraint FK_S +alter table bbsReply drop constraint FK_d +delete from bbsUsers where uName = '逍遥' +select * from bbsUsers + +update bbsUsers set uPoint = 10 where uName = '小雨点' +select * from bbsUsers + +alter table bbsSection drop constraint PK_q +alter table bbsSection drop constraint PK_p +delete from bbsSection where sName = '生活百科' +select * from bbsSection + +alter table bbsReply drop constraint PK_z +delete from bbsReply where rUID = 1 +delete from bbsReply where rUID = 2 +select * from bbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..de027961dd43a16d1ecc3dd586dbd8d07caaeeda --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" @@ -0,0 +1,312 @@ +use master +go + +create database TestDB +on +( + name='TestDB', + filename='D:\TestDB.mdf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +log on +( + + name='TestDB_log', + filename='D:\TestDB_log.ldf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +go + +use TestDB +go + +create table TypeInfo +( + TypeId int primary key identity(1,1), + TypeName varchar(10) not null +) + +create table LoginInfo +( + LoginId int primary key identity(1,1), + LoginName nvarchar(10) unique not null, + LoginPwd nvarchar(20) default('123456') not null, + LoginSex varchar(1) default('男') check(LoginSex='男' or LoginSex='女'), + LoginBirthday datetime, + LoginType nvarchar(10) +) + +use master +go + +create database Company +on +( + name='Company', + filename='D:\Company.mdf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +log on +( + + name='Company_log', + filename='D:\Company_log.ldf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +go + +use Company +go + +create table SectionInfo +( + SectionId int primary key identity(1,1), + SectionName varchar(10) not null +) + +create table UserInfo +( + UserNo int primary key identity(1,1) not null, + UserName varchar(10) unique not null check(len(UserName)>4), + UserSex varchar(2) check(UserSex='男' or UserSex='女') not null, + UserAge int check(UserAge>=1 and UserAge<=100) not null, + UserAddress varchar(50) default('湖北'), + UserSection int foreign key references SectionInfo(SectionId) +) + +create table WorkInfo +( + WorkId int primary key identity(1,1) not null, + UserId int foreign key references UserInfo(UserNo) not null, + WorkTime datetime not null, + WorkDescription varchar(40) not null check(WorkDescription='迟到' or WorkDescription='早退'or WorkDescription='旷工' or WorkDescription='病假' or WorkDescription='事假') +) + +use master +go + +create database Class +on +( + name='Class', + filename='D:\Class.mdf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +log on +( + + name='Class_log', + filename='D:\Class_log.ldf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +go + +use Class +go + +create table ClassInfo +( + ClassId int primary key identity(1,1), + ClassName varchar(4) unique not null, + OpenTime datetime not null, + ClassDescribe nvarchar(50) +) + +create table StuInfo +( + StuNo int primary key identity(1,1), + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女') not null, + StuAge int check(StuAge>=15 and StuAge<=40) not null, + StuAdress nvarchar(4) default('湖北武汉'), + ClassId int foreign key references ClassInfo(ClassId) +) + +create table Course +( + CourseNo int primary key identity(1,1), + CourseName nvarchar(4) unique not null, + CourseDescribe nvarchar(50) +) + +create table Credit +( + CreditNo int primary key identity(1,1), + StuNo int foreign key references StuInfo(StuNo), + CourseNo int foreign key references Course(CourseNo), + Credit int check(Credit>=0 and Credit<=100) +) + +use master +go + +create database House +on +( + name='House', + filename='D:\House.mdf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +log on +( + + name='House_log', + filename='D:\House_log.ldf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +go + +use House +go + +create table TblUser +( + UserId int primary key identity(1,1), + UserName nvarchar(4) unique not null, + UserTel varchar(11) unique not null +) + +create table TblHouseType +( + TypeId int primary key identity(1,1), + TypeName nvarchar(4) unique not null +) + +create table TblQx +( + QxId int primary key identity(1,1), + QxName nvarchar(4) unique not null +) + +create table HouseInfo +( + Id int primary key identity(1,1), + Desc1 nvarchar(50), + UserId int foreign key references TblUser(UserID), + zj money, + shi int, + ting int, + TypeId int foreign key references TblHouseType(TypeId), + QxId int foreign key references TblQx(QxId) +) + +use master +go + +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +log on +( + + name='bbs_log', + filename='D:\bbs_log.ldf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +go + +use bbs +go + +create table BbsUsers +( + UserId int identity(1,1), + UserName varchar(10) not null, + UserSex varchar(2) not null, + UserAge int not null, + UserPoint int not null +) + +alter table BbsUsers add constraint PK_BbsUser_UserId primary key(UserId) +alter table BbsUsers add constraint UK_BbsUser_UserName unique(UserName) +alter table BbsUsers add constraint CK_BbsUser_UserSex check(UserSex='男' or UserSex='女') +alter table BbsUsers add constraint CK_BbsUser_UserAge check(UserAge>=15 and UserAge<=60) +alter table BbsUsers add constraint CK_BbsUser_UserPoint check(UserPoint>=0) + +create table BbsTopic +( + TopicId int primary key identity(1,1), + TopicUserId int foreign key references BbsUsers(UserId), + TopicSId int foreign key references BbsSection(SectionId), + TopicTitle varchar(100) not null, + TopicMsg text not null, + TopicTime datetime, + TopicCount int +) + +create table BbsReply +( + ReplyId int primary key identity(1,1), + RepluUid int foreign key references BbsUsers(UserId), + ReplyTid int foreign key references BbsTopic(TopicId), + ReplyMsg text not null, + ReplyTime datetime +) + +create table BbsSection +( + SectionId int Identity(1,1), + SectionName varchar(10) not null, + SectionUid int +) + +alter table BbsSection add constraint PK_BbsSection_SectionId primary key(SectionId) +alter table BbsSection add constraint FK_BbsSection_SectionUid foreign key(SectionUid) references BbsUsers(UserId) + +insert into BbsUsers values +('小雨点','女','20','0'), +('逍遥','男','18','4'), +('七年级生','男','19','2') + + +select UserName,UserPoint into BbsPoint from BbsUsers + +insert into BbsSection values +('技术交流',3), +('读书世界',5), +('生活百科',3), +('八卦区',5) +select * from BbsTopic +insert into BbsTopic values +(4,6,'范跑跑','谁是范跑跑','2008-7-8',1), +(5,3,'.NET','与JAVA的区别是什么呀?','2008-9-1',2), +(3,5,'今年夏天最流行什么','有谁知道及那年夏天最流行什么呀?','2008-9-10',0) + +insert into BbsReply values +(3,5,'计划司法机会大','2008-9-1'), +(3,6,'家私房价','2008-10-1'), +(4,7,'山东矿机拉速度快','2008-10-10') + +alter table BbsReply drop constraint FK__BbsReply__RepluU__1FCDBCEB +alter table BbsTopic drop constraint FK__BbsTopic__TopicU__1BFD2C07 +delete BbsUsers where UserName='逍遥' + +update bbsUsers set UserPoint=12 where UserName='小雨点' + +alter table BbsTopic drop constraint FK__BbsTopic__TopicS__1CF15040 +delete BbsSection where SectionName='生活百科' + +delete BbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\347\273\203\344\271\240\344\270\200.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\347\273\203\344\271\240\344\270\200.sql" new file mode 100644 index 0000000000000000000000000000000000000000..08ce6aa00bfb13628cbfe9b4ebf11fe04a0cb395 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\347\273\203\344\271\240\344\270\200.sql" @@ -0,0 +1,259 @@ +create database TestDB--创建数据库 +on primary +( +name='TestDB', +filename='D:SQL作业.mdf', +size=5MB, +filegrowth=5MB, +maxsize=unlimited +) +log on +( +name='TestDB_log', +filename='D:SQL作业_log.ldf', +size=5MB, +maxsize=5MB +) +go + +use TestDB + +--类别编号:主键 自动编号 +-- 类别名(typeName): varchar(10) 不能为空 +create table typeInfo --创建会员类别表 +( +typeId int identity(1,1) primary key, +typeName varchar(10) not null +) +go + +-- 编号(LoginId),数据类型(int),主键、自动编号 +-- 账户(LoginName),文本,长度为10,非空,必须唯一,不能重复 +--密码(LoginPwd),文本,长度为20,非空、默认值为‘123456’ +create table loginInfo --创建登入用户表 +( +LoginId int identity(1,1) primary key, +LoginName varchar(10) not null unique, +Loginpwd varchar(20) not null default('123456'), +sex varchar(2), +birthday varchar(15), +hycategory varchar(20) +) +go + + + +create database company +on primary +( +name='company', +filename='D:SQL作业.mdf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +log on +( +name='company_log', +filename='D:SQL作业.mdf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +go + +use company + +--部门编号 sectionID int 标识列 主键 +--部门名称 sectionName varchar(10) 不能为空 + +create table sectionInfo --建立部门信息表 +( +sectionID int primary key, +sectionName varchar(10) not null +) +go + +--员工编号 userNo int 标识列 主键 不允许为空 +--员工姓名 userName varchar(10) 唯一约束 不允许为空 长度必须大于4 +--员工性别 userSex varchar(2) 不允许为空 只能是男或女 +--员工年龄 userAge int 不能为空 范围在1-100之间 +--员工地址 userAddress varchar(50) 默认值为“湖北” +--员工部门 userSection int 外键,引用部门信息表的部门编号 + +create table userInfo --建立员工信息表 +( +userNo int primary key not null, +userName varchar(10) unique check(userName>4), +userSex varchar(2) check(usersex = '男' or usersex = '女'), +userAge int check(userAge between 1 and 100) not null, +userAddress varchar(50) default('湖北'), + userSection int foreign key(userSection) references sectionInfo(sectionID) +) +go + +--考勤编号 workId int 标识列 主键 不能为空 +--考勤员工 userId int 外键 引用员工信息表的员工编号 不能为空 +--考勤时间 workTime datetime 不能为空 +--考勤说明 workDescription varchar(40) 不能为空 内容只能是“迟到”,“早退”,“旷工”,“病假”,“事假”中的一种 +create table workInfo +( +workId int primary key not null, +userId int foreign key(userId) references userInfo(userNo) not null, +workTime datetime not null, +workDescription varchar(40) check(workDescription='迟到'or workDescription='早退'or workDescription='旷工'or workDescription='病假'or workDescription='事假') +) +go + + + +create database stuMS +on primary +( +name='stuMS', +filename='D:SQL作业.mdf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +log on +( +name='stuMS_log', +filename='D:SQL作业_log.ldf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +go + + +use stuMS +--班级编号 classid (主键、标识列 +--班级名称(例如:T1、T2、D09等等):不能为空,不能重复 +--开办时间:不能为空 +--班级描述 +create table classInfo --建立班级信息表 +( +classid int primary key, +classname varchar(10) unique not null, +crankuptime varchar(16) not null, +classdescription nvarchar(100) +) +go + +--学号:主键、标识列 +--姓名:长度大于2,不能重复 +--性别:只能是‘男’或‘女’,默认为男,不能为空 +--年龄:在15-40之间,不能为空 +--家庭地址:默认为“湖北武汉” +--所在的班级编号 +create table stuInfo +( +StudentID int primary key not null, +stuname nvarchar(15) unique check(stuname>2) not null, +stusex varchar(2) check(stusex='男'or stusex='女') default('男') not null, +stuage varchar(4) check(stuage between 15 and 40) not null, +studress nvarchar(40) default('湖北武汉'), +classnumber int +) +go + + +--编号:主键、标识列 +--课程名:不能为空,不能重复 +--课程描述: +create table courseInfo +( +serialnumber int primary key, +CourseName nvarchar(10) unique not null, +coursedescription nvarchar(50) +) +go + +--成绩编号:主键、标识列 +--成绩所属于的学生编号,不能为空 +--成绩所属的课程编号,不能为空 +--成绩:在0-100之间 + +create table achievementInfo --建立成绩表 +( +ScoreId int primary key, +StudentNO int not null, +coursenumber int not null +) +go + + + +create database CZ +on primary +( +name='CZ', +filename='D:SQL作业.mdf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +log on +( +name='CZ_log', +filename='D:SQL作业_log.ldf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +go + +--tblUser 发布人信息表 +--userId userName userTel +create table tblUser +( +userId varchar(10) not null, +userName nvarchar(5) not null, +userTel nvarchar(15) not null +) +go + +--tblHouseType 房屋的类型 +--typeId +--typName + +create table tblHouseType --建立房屋类型表 +( +typeId varchar(10) not null, +typeName varchar(10) not null +) +go + +--tblQx 区县 +--qxId +--qxName + +create table tblQx +( +qxId varchar(10) not null, +qxName varchar(20) not null +) +go + + +--tblHouseInfo--房屋信息表 +--id +--desc +--userId +--zj +--shi +--ting +--typeId +--qxId + +create table tblHouseInfo +( +id int, +userId varchar(10), +zj int not null, +shi int not null, +ting int not null, +typeId varchar(10), +qxId varchar(15) +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\347\273\203\344\271\240\344\272\214.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\347\273\203\344\271\240\344\272\214.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6ebb70e9bce2986d692f99256b43a506eb599a60 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\264\244/\347\273\203\344\271\240\344\272\214.sql" @@ -0,0 +1,152 @@ +create database bbs +on primary +( +name='bbs', +fliename='D:\home.mdf', +size=5MB, +filegrowth=1MB, +maxsize=5MB +) +log on +( +name='bbs_log', +filename='D:\home_log.ldf', +size=5MB, +filesize=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(tTitle,tMsg,tTime,tCount) +select '范跑跑','谁是范跑跑','2008-7-8',1 union +select '.NET','与JAVA的区别是什么呀?','2008-9-1',2 union +select '今年夏天最流行什么呀?','有谁知道今年夏天最流行什么呀?','2008-9-10',0 + +--回帖: + --分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 + +insect into bbsReply(rMsg,rTime) +select '我是范跑跑','2008-7-10' union +select '没区别','2008-9-2' union +select '最流行冰激凌','2008-9-10' + + --5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) + + + --6.因为小雨点发帖较多,将其积分增加10分 + + + --7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) + +delete from bbsSection where sName='生活百科' + + --8.因回帖积累太多,现需要将所有的回帖删除 + +drop table bbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/\345\243\271.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/\345\243\271.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a2d29aa45dc94a790998cdbd93122f3174c4886a --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/\345\243\271.sql" @@ -0,0 +1,155 @@ +create database TestDB +on +( + name='TestDB', + filename='D:\TestDB.mdf' +) + +log on +( + name='TestDB_log', + filename='F:\TestDB_log.ldf' +) +use TestDB +go + +--1つ +create table typeinfo +( + typeId int primary key identity(1,1), + typeName varchar(10) not null, +) + +create table logininfo +( + LoginId int primary key identity(1,1), + LoginName varchar(10) not null unique, + LoginPwd varchar(20) not null default('1,2,3,4,5,6'), + Loginsex char(1), + Loginbrithday datetime, + LoginVIP varchar +) + +-- 二番目 +create database company +on +( + name='company', + filename='D:\company.mdf' +) + +log on +( + name='company_log', + filename='F:\company_log.mdf' +) +use company +go + +create table sectioninfo +( + sectionID int primary key, + sectionName varchar(10) not null +) +create table userinfo +( +userNo int primary key not null, +userName varchar(10) unique not null check(userName>4), +userSex varchar(2) not null check(userSex='男' or userSex='女' ), +userAge int not null check(userAge>=1 or userAge<=100), +userAddress varchar(50) default('湖北'), +userSection int foreign key references sectionInfo(sectionID) +) +create table workinfo +( +workId int primary key not null, +userId int foreign key references userInfo(userNo), +workTime datetime not null, + workDescription varchar(40) not null check(workDescription='迟到'or workDescription='早退'or workDescription='矿工' or workDescription='病假'or workDescription='事假') +) + +-- 三番目 +create database School +on +( + name='School', + filename='D:\School.mdf' +) +log on +( + name='School_log', + filename='D:\School_log.ldf' +) +use School +go +create table Classinfo +( + classid int primary key, + classname varchar(10) not null unique, + classremark ntext +) +create table studentinfo +( + stunum int primary key, + stuname varchar check (len(stuname)>2) unique, + stusex varchar(2) default('男') check(stusex='男' or stusex='女'), + stuage int check(stuage>=15 or stuage<=40) not null, + stuaddress nvarchar(50) default('湖北武汉'), + stuclassid int references Classinfo(classid) +) +create table information +( + number int primary key, + kechenname varchar unique not null, + miaoshu ntext +) +create table score +( + scorenum int primary key, + scorestuid int references studentinfo(stunum) not null, + scoreid int references information(number) not null, + score int check(score>=0 or score<=100) +) +--四番目 +create database house +on +( + name='house', + filename='D:\house.mdf' +) +log on +( + name='house_log', + filename='D:\house_log.ldf' +) +use house +go + +create table tblUser +( + userId int primary key identity(1,1), + userName varchar, + userTel nvarchar(10) +) +create table tblHouseType +( + typeId int primary key identity(1,1), + typName varchar check(typName='别墅'or typName='普通住宅'or typName='平房' or typName='地下室') not null +) +create table tblQx +( + qxId int primary key identity(1,1), + qxName varchar check(qxName='武昌'or qxName='汉阳' or qxName='汉口') not null +) + +create table tblHouseInfo +( + id int primary key identity(1,1), + userId int foreign key references tblUser (userId), + zj money, + shi varchar, + ting int, + typeId int foreign key references tblHouseType (typeId), + qxId int foreign key references tblQx (qxId) +) + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/\350\264\260.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/\350\264\260.sql" new file mode 100644 index 0000000000000000000000000000000000000000..396bb8770ee063a18e5777e1b6b2dcedd77c9dc8 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/\350\264\260.sql" @@ -0,0 +1,105 @@ +-- 1つ +create database bbs +on +( + name='bbs', + filename='E:\bbs.mdf' +) +log on +( + name='bbs_log', + filename='E:\bbs_log.ldf' +) +use bbs +go + +create table bbsUsers +( + uuID 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 primary key (uuID) +alter table bbsUsers add constraint UK unique(uName) +alter table bbsUsers add constraint CK check(uSex='男' or uSex='女' ) +alter table bbsUsers add constraint CK1 check(uAge>=15 or uAge<=60 ) +alter table bbsUsers add constraint CK2 check(upoint>=0) + +create table bbsSection +( + sID int 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(uuID) + + + +create table bbsTopic +( + tID int primary key, + tUID int foreign key references bbsUsers(uuID), + tSID int foreign key references bbsSection(sID), + +) +alter table bbsTopic add tTitle varchar(100) not null +alter table bbsTopic add tMsg text not null +alter table bbsTopic add tTime datetime +alter table bbsTopic add tCount int + +create table bbsReply +( + rID int primary key, + rUID int foreign key references bbsUsers(uuID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女',20,0 union +select'逍遥','男',18,4 union +select'七年级生','男',19,2 +select * from bbsUsers + +--题目 +--2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中, +--提示查询部分列:select 列名1,列名2 from 表名 +select uName,uPoint into bbsPoint from bbsUsers + +insert into bbsSection(sName, sID) +select '技术交流' , 1union +select '读书世界' ,2 union +select '生活百科' ,3 union +select ' 八卦区' ,4 +select * from bbsSection + +-- 4.向主贴和回帖表中添加几条记录 +-- 发帖人 板块名 帖子标题 +-- 帖子内容 帖时间 回复数量 +insert into bbsTopic( tid,tUID,tSID,tTitle,tMsg,tTime,tCount ) +select 1,3, 4,'范跑跑','谁是范跑跑','2008-7-8',1 union +select 2,2, 1,' .NET','与JAVA的区别是什么呀?','2008-9-1',2 union +select 3,1, 3,' 今年夏天最流行什么','有谁知道今年夏天最流行什么鸭','2008-9-10',0 +select * from bbsTopic + +select * from bbsReply +insert into bbsReply(rid,rUID,rMsg,rTime) +select 1,2,'我们都是范跑跑','2008-7-8'union +select 2,1,'你猜(滑稽)','2008-7-8'union +select 3,3,'母鸡啊','2008-7-8' + +delete bbsUsers where uName='逍遥' +alter table bbsReply drop FK__bbsReply__rUID__4AB81AF0 +alter table bbsTopic drop FK__bbsTopic__tUID__46E78A0C +select * from bbsReply +--给那啥雨点加分10 +update bbsUsers set upoint=12 where uName='逍遥' +--删除生活百科板块 +alter table bbsTopic drop FK__bbsTopic__tSID__47DBAE45 +delete bbsSection where sName='生活百科' +--删除所有回帖 +delete bbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e30a231459cf4ce5df7b4b25e2d75dda15788d04 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\226\260\344\274\240/SQLQuery1.sql" @@ -0,0 +1,295 @@ +use master +go +create database TestDB +on +( name='TestDB', + filename='D:\TestDB.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='company_log', + filename='D:\TestDB_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +go +use TestDB +go +create table TestDB +( typeld int primary key, + typeName varchar(10) not null +) + + +create table loginInfo +( LoginId int primary key , + LoginName nvarchar(10) not null unique , + LoginPwd nvarchar(20) not null default(123456), + LoginSex nchar(1) default('男') check(LoginSex='男'or LoginSex='女'), + LoginBrithday date, + LogintypeName varchar(10) not null +) +use master +go +create database company +on +( name='company', + filename='D:\company.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='company_log', + filename='D:\company_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +go +use company +go +create table sectionInfo +( sectionID int primary key identity(1,1), + sectionName varchar(10) not null +) + + +create table userInfo +( userNo int primary key not null identity(1,1), + userName varchar(10) unique not null check(userName>4), + userSex varchar(2) not null check(userSex='男'or userSex='女'), + userAge int not null check(userAge>=1 and userAge<=100), + userAddress varchar(50) default('湖北'), + userSection int references sectionInfo(sectionID), +) + + +create table workInfo +( workId int primary key not null, + userId int references userInfo( userNo) not null, + workTime datetime not null, + workDescription varchar(40) not null check(workDescription in('迟到','早退','旷工','病假')) +) + +use master +go +create database studentmanage +on +( name='studentmanage', + filename='D:\studentmanage.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='studentmanage_log', + filename='D:\studentmanage_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +go +use studentmanage +go +create table classinfo +( classid int primary key identity(1,1), + classname char(5) not null unique, + timeday date not null, + classspeak text +) +create table studentinfo +( stuid int primary key identity(1,1), + stuname nchar(10) unique check(stuname>2), + stusex nchar(1) not null default('男') check(stusex in('男','女')), + stuage int not null check(stuage>=15 and stuage<=40), + stuaddress nvarchar(4) default('湖北武汉'), + stuclassid int references classinfo(classid) +) + +create table courseinfo +( courseid int primary key identity(1,1), + coursename nchar(10) not null unique, + coursespeak text +) +create table scoreinfo +( scoreid int primary key identity(1,1), + scorestuid int not null, + scorecourseid int not null, + score int check(score>=0 and score<=100) +) +----------------- +use master +go +create database house +on +( name='house', + filename='D:\house.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='house_log', + filename='D:\house_log.ldf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +go +use house +go +create table tblUser +( userId int primary key identity(1,1), + userName nvarchar(10) not null, + userTel char(11) not null +) +create table tblHouseType +( typeId int primary key identity(1,1), + typName nchar(4) not null +) +create table tblQx +( qxId int primary key identity(1,1), + qxName nchar(2) not null +) +create table tblHouseInfo +( id int primary key identity(1,1), + desc1 nvarchar(10) not null, + userId int, + zj int, + shi int, + ting int, + typeId int, + qxId int +) + + + +------------------ +use master +go +create database bbs +on +( name='bbs', + filename='D:\bbs.mdf', + size=5mb, + maxsize=5000mb, + filegrowth=1mb +) +log on +( name='bbs_log', + filename='D:\bbs_log.ldf', + size=5mb, + maxsize=5000mb, + 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 +) +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,4,'谁问的谁就是范跑跑','20210316'), + (1,3,'这个更简单','20210316'), + (1,3,'今年夏天最流行内裤外穿','20210316') +select*from bbsReply + +-- 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) +select*from bbsUsers +select*from bbsSection --sUid +select*from bbsTopic --tUID +delete from bbsTopic where tUID=2 +select*from bbsReply --rUID + +delete from bbsUsers where uName='逍遥' + +-- 6.因为小雨点发帖较多,将其积分增加10分 + select uPoint from bbsUsers where uName='小雨点' + update bbsUsers set uPoint=12 where uName='小雨点' + +-- 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) + select*from bbsTopic + alter table bbsReply drop constraint FK__bbsReply__rTID__1DE57479 + delete from bbsTopic where tSID=3 + +-- 8.因回帖积累太多,现需要将所有的回帖删除 +select*from bbsReply +truncate table bbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/\347\273\203\344\271\2401.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/\347\273\203\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9fd3864f1198dc93babf31adb1cf9bdc202b3246 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\261\237\346\264\213/\347\273\203\344\271\2401.sql" @@ -0,0 +1,185 @@ +create database TestDB +on( + name='TestDB', + filename='E:\TestDB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log +on( + name='TestDB_log', + filename='E:\TestDB_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use TestDB +create table typelnfo +( + typeId int primary key identity, + typeName varchar(10) not null, +) +create table loginlnfo +( + LoginId int primary key identity, + LoginName varchar(10) not null unique, + LoginPwd varchar(20) not null default('123456'), + LoginSex char not null, + LoginBrithd datetime not null, + LoginVip varchar(10) not null +) + + + + + + + + +create database company +on +( + name='company', + filename='E:\company.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='company_log', + filename='E:\company_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +go +use company +go + +create table sectionlnfo +( + sectionID int identity primary key, + sectionName varchar(10) not null +) +create table userlnfo +( + userNo int identity primary key not null, + userName varchar(10) unique not null check (len (userName)>4), + userSex varchar(2) not null check(userSex='男' or userSex='女'), + userAge int not null check(userAge>=1 or userAge<=100), + userAddress varchar(50) default('湖北'), + userSection int references sectionlnfo(sectionID) +) +create table worklnfo +( + workld int identity primary key not null, + userld int references userlnfo(userNo) not null, + workTime datetime not null, + workDescription varchar(40) not null check(workDescription='迟到' or workDescription='早退' or workDescription='矿工' or workDescription='病假' or workDescription='事假' ) +) + + + + + + + + +create database School +on( + name='School', + filename='E:\School.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='School_log', + filename='E:\School_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +create table banji +( + classid int primary key identity, + classN1 varchar(10) not null unique, + classKB datetime not null, + classMS text not null +) +create table xuesheng +( + sdtXH int primary key identity, + sdtXM varchar(10) check(len (sdtXM)>2) unique, + sdtXB varchar(2) default('男') not null check(sdtXB=('男')or sdtXB=('女')), + sdtNL int check(sdtNL>=15 or sdtNL<=40) not null, + sdtZZ varchar(30) default('湖北武汉'), + classid int references banji(classid), +) +create table kecheng +( + bianhao int primary key identity, + kechengming varchar(20) not null unique, + kechengmiaoshu text not null +) +create table chengji +( + chengjiID int primary key identity, + sdtXH int references xuesheng(sdtXH) not null, + bianhao int references kecheng(bianhao) not null, + chengjiqujian int check(chengjiqujian>=0 or chengjiqujian<=100) +) + + + + + + + + +create database fangwu +on +( + name='fangwu', + filename='E:\fangwu.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='fangwu_log', + filename='E:\fangwu_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +create table tbIUser +( + userID int primary key identity, + userName varchar, + userTel nvarchar(10) +) +create table tbIHouseType +( + typeID int primary key identity, + typName varchar check(typName='别墅' or typName='普通主宰' or typName='平房' or typName='地下室') not null, +) +create table tbIQx +( + qxID int primary key identity, + qxname varchar check(qxName='武昌' or qxName='汉阳' or qxName='汉口') not null, +) +create table tbIhouseInfo +( + id int primary key identity, + userID int foreign key references tbIUser(userID), + zj money, + shi varchar, + ting int , + typeID int foreign key references tbIHouseType(typeID), + qxID int foreign key references tbIQx(qxId) +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..dc238f6aa4d6a08f71a5cfb957d78f57750d805e --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" @@ -0,0 +1,177 @@ +create database TestDB +on +( + name='TestDB', + filename='D:\TestDB.mdf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +), +( + name='TestDB_ndf', + filename='D:\TestDB_ndf.ndf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='TestDB_log', + filename='D:\TestDB_log.ldf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +go +use TestDB +go + +create table typeInfo +( + typeId int primary key identity(1,1), + typeName varchar(10) not null +) +create table loginInfo +( + LoginId int primary key identity(1,1), + LoginName nvarchar(10) unique not null, + LoginPwd nvarchar(20) default(123456) not null, + Loginsex nvarchar(1), + Loginbady date, + Loginhy nvarchar(20) +) +create database company +on +( + name='company', + filename='D:\company.mdf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +), +( + name='company_ndf', + filename='D:\company_ndf.ndf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='company_log', + filename='D:\company_log.ldf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +go +use company +go + +create table sectionInfo +( + sectionID int identity primary key, + sectionName varchar(10) not null +) +create table userinfo +( + userNo int identity primary key not null, + userName varchar(10) unique check(userName>4) not null, + userSex varchar(2) check(userSex='男' or userSex='女') not null, + userAge int check(userAge>=1 and userAge<=100) not null, + userAddress varchar(50) default('湖北'), + userSection int foreign key references sectionInfo(sectionID) +) +create table workinfo +( + workld int identity primary key not null, + userld int foreign key references userinfo(userNo), + workTime datetime not null, + workDescription varchar(40) check(workDescription in('迟到','早退','旷工','病假')) not null +) +create database xsglxt +on +( + name='xsglxt', + filename='D:\xsglxt.mdf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +), +( + name='xsglxt_ndf', + filename='D:\xsglxt_ndf.ndf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='xsglxt_log', + filename='D:\xsglxt_log.ldf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +go +use xsglxt +go + +create table bjxx +( + classid int identity primary key , + bjmc nvarchar(20) unique not null, + kbsj date not null, + bjms nvarchar(200) +) +create table classxsxx +( + xh int identity primary key, + xm varchar(8) check(xm>2) unique , + sex nvarchar(1) default('男') check(sex='男' or sex='女') not null, + age int check(age>=15 and age<=40) not null, + stuaddress nvarchar(200) default('湖北武汉'), + bjbh int references bjxx(classid) +) +create table classkcxx +( + id int identity primary key, + kcm nvarchar(20) unique not null, + kcms nvarchar(200) +) +create table classcjxx +( + cjid int identity primary key, + xsbh int not null, + ckbh int not null, + cj int check(cj>=0 and cj<=100) +) + + +create table tblUser +( + userId int identity primary key, + userName nvarchar(8) not null, + userTel char(11) not null +) +create table tblHouseType +( + typeId int identity primary key, + typName nvarchar(8) not null +) +create table tblQx +( + qxId int identity primary key, + qxName nvarchar(8) not null +) +create table tblHouseInfo +( + id int identity primary key, + descc nvarchar(8) not null, + userld int, + zj int, + shi int, + ting int, + typeld int, + qxld int +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e0ecfb2ea64feed9d807fdd1d427bdc59ba25d91 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery2.sql" @@ -0,0 +1,77 @@ +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') +delete from bbsReply where rTID=1 +delete from bbsTopic where tUID=2 +delete from bbsUsers where uName='逍遥' +update bbsUsers set uPoint=12 where uName='小雨点' +delete from bbsReply where rTID=3 +delete from bbsTopic where tSID=3 +truncate table bbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery7.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..52ec49daf87771bb5f86db378ada69064a2efb42 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery7.sql" @@ -0,0 +1,13 @@ +use master +go +create database TestDB +on +( +name='student', +filename='D:\.mdf' +) +log on +( +name='student_log', +filename='D:\student_log.ldf' +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery8.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery8.sql" new file mode 100644 index 0000000000000000000000000000000000000000..123750d0feccec40a686311686cebcb1bbce237e --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery8.sql" @@ -0,0 +1,97 @@ +use master +go +create database bbs +on +( +name='bss', +filename='D:\bbs.mdf', +maxsize=50, +filegrowth=1, +size=5 +) +log on +( +name='bss_log', +filename='D:\bbs_log.ldf', +maxsize=50, +filegrowth=1, +size=5 +) +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) +) +alter table bbsUsers add constraint fk_bbsUsers primary key(UID) +alter table bbsUsers add constraint sk_bbsUsers unique(uName) +alter table bbsUsers add constraint nk_bbsUsers check(uSex='男' or uSex='女' ) +alter table bbsUsers add constraint ak_bbsUsers check(uAge>=15 and uAge<=60) +alter table bbsUsers add constraint pk_bbsUsers 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), +TID int foreign key references bbsTopic(tID), +rMsg text not null, +Time datetime +) +create table bbsSection +( +sID int primary key identity(1,1), +sName varchar(10) not null, +sUid int foreign key references bbsUsers(UID) +) +alter table bbsSection add constraint ik_bbsSection primary key(sID) +alter table bbsSection add constraint mk_bbsSection primary key(sUid) + +insert into bbsUsers +select '小雨点','女','20','0'union +select ' 逍遥 ','男','18','4'union +select '七年级生','男','19','2' + +select uName,uPoint into bbsPoint from bbsUsers + +insert into bbsPoint select uName,uPoint from bbsUsers + +insert into bbsSection(sName,sUid) +select '技术交流',1union +select '读书世界',2union +select '生活百科',3union +select '八卦区',4 +select * from bbsSection +insert into BbsTopic values +(4,6,'范跑跑','谁是范跑跑','2008-7-8',1), +(5,3,'.NET','与JAVA的区别是什么呀?','2008-9-1',2), +(3,5,'今年夏天最流行什么','有谁知道及那年夏天最流行什么呀?','2008-9-10',0) + +insert into BbsReply values +(3,5,'计划司法机会大','2008-9-1'), +(3,6,'家私房价','2008-10-1'), +(4,7,'山东矿机拉速度快','2008-10-10') + +alter table BbsReply drop constraint FK__BbsReply__RepluU__1FCDBCEB +alter table BbsTopic drop constraint FK__BbsTopic__TopicU__1BFD2C07 +delete BbsUsers where UserName='逍遥' + +update bbsUsers set UserPoint=12 where UserName='小雨点' + +alter table BbsTopic drop constraint FK__BbsTopic__TopicS__1CF15040 +delete BbsSection where SectionName='生活百科' + +delete BbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..65f792a33a962b2226b9bef8deb30d459182387f --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" @@ -0,0 +1,33 @@ +use master +go +create database TestDB +on( + name='TestDB', + filename='C:\TEXT\TestDB.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on( + name='TestDB_log', + filename='C:\TEXT\TestDB_log.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +go +use TestDB +go +create table typeInfo +( + typeId int primary key identity(1,1), + typeName varchar(10) not null, +) +create table loginInfo +( + LoginId int primary key identity(1,1), + LoginName nvarchar(10) unique not null, + LoginPwd nvarchar(20) default('123456') not null, + LoginBirthday datetime not null, + LoginVIP nvarchar(10) not null, +) diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a6a5671728988ec5d4160610754498903fff9723 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery2.sql" @@ -0,0 +1,43 @@ +use master +go +create database company +on( + name='company', + filename='C:\TEXT\company.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on( + name='company_log', + filename='C:\TEXT\company_log.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +go +use company +go +create table sectionInfo +( + sectionID int primary key identity(1,1), + sectionName varchar(10) not null, +) +create table userInfo +( + userNo int primary key not null, + userName varchar(10) unique not null, + userSex varchar(2) check(userSex='男' or userSex='女')not null, + userAge int check(userAge>=1 or userAge<=100) not null, + userAddress varchar(50) default('湖北'), + userSection int references sectionInfo(sectionID), +) +create table workInfo +( + workId int primary key identity(1,1) not null, + userId int references userInfo(userNo) not null, + workTime datetime not null, + workDescription varchar(40) check(workDescription='迟到' or workDescription='早退' or workDescription='旷工' or workDescription='病假' or workDescription='事假'), + + +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery3.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b46289be309602bde71098bcac6e142c1c7ad570 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery3.sql" @@ -0,0 +1,49 @@ +use master +go +create database students +on( + name='students', + filename='C:\TEXT\students.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on( + name='students_log', + filename='C:\TEXT\students_log.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +go +use Students +go +create table class +( + classid int primary key identity(1,1), + className nvarchar(10) unique not null, + classTime datetime not null, + classText text not null, +) +create table student +( + stuID int primary key identity(1,1), + stuName nvarchar(10) unique not null, + stuSex nvarchar(2) default('男') check(stuSex='男' or stuSex='女'), + stuAge int check(stuAge>=14 or stuAge<=40)not null, + stuAddress nvarchar(20) default ('湖北武汉') not null, + classid int references class(classid), +) + create table course + ( + courseid int primary key identity(1,1), + courseName nvarchar(10) unique not null, + courseText text not null, + ) + create table score + ( + scoreID int primary key identity(1,1), + stuID int references student(stuID) not null, + courseid int references course(courseid) not null, + score int check(score>=1 or score<=100) + ) diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery5.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7896ee15fe95443e06c97026bddc883d072bd8c7 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery5.sql" @@ -0,0 +1,41 @@ +use master +go +create database tbl +on( + name='tbl', + filename='C:\TEXT\tbl.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +log on( + name='tbl_log', + filename='C:\TEXT\tbl_log.mdf', + size=5, + maxsize=100, + filegrowth=10% +) +go +use tbl +go +create table tblUser +( + userId int primary key identity(1,1), + userName nvarchar(10) not null, + userTel varchar(11) unique not null, +) +create table tblHouseType +( + typeId int primary key identity(1,1), + typName nvarchar(10) not null, + tblQx nvarchar(10) not null, + qxId int not null, + qxName nvarchar(10) not null, +) +create table tblHouseInfo +( + id int primary key identity(1,1), + userId int references tblUser(userId), + typeId int references tblHouseType(typeId), + qxId int not null, +) diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery6.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..67139b93ee0359a0fbe383d318b7f748a186dd6b --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery6.sql" @@ -0,0 +1,84 @@ +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 , +) + +go +use bbs +go + + +select * from bbsSection +select * from bbsReply +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,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='小雨点' +delete from bbsTopic where tUID=2 +delete from bbsReply where rUID=2 +delete from bbsReply where rID=1 +delete from bbsUsers where uName='逍遥' +delete from bbsTopic where tSID=3 +delete from bbsSection where sName='生活百科' +delete from bbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9af81e677c3878cb5c5375bb0d634ce7581b77a7 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery1.sql" @@ -0,0 +1,312 @@ +use master +go + +create database TestDB +on +( + name='TestDB', + filename='D:\11\TestDB.mdf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +log on +( + + name='TestDB_log', + filename='D:\11\TestDB_log.ldf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +go + +use TestDB +go + +create table TypeInfo +( + TypeId int primary key identity(1,1), + TypeName varchar(10) not null +) + +create table LoginInfo +( + LoginId int primary key identity(1,1), + LoginName nvarchar(10) unique not null, + LoginPwd nvarchar(20) default('123456') not null, + LoginSex varchar(1) default('男') check(LoginSex='男' or LoginSex='女'), + LoginBirthday datetime, + LoginType nvarchar(10) +) + +use master +go + +create database Company +on +( + name='Company', + filename='D:\11\Company.mdf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +log on +( + + name='Company_log', + filename='D:\11\Company_log.ldf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +go + +use Company +go + +create table SectionInfo +( + SectionId int primary key identity(1,1), + SectionName varchar(10) not null +) + +create table UserInfo +( + UserNo int primary key identity(1,1) not null, + UserName varchar(10) unique not null check(len(UserName)>4), + UserSex varchar(2) check(UserSex='男' or UserSex='女') not null, + UserAge int check(UserAge>=1 and UserAge<=100) not null, + UserAddress varchar(50) default('湖北'), + UserSection int foreign key references SectionInfo(SectionId) +) + +create table WorkInfo +( + WorkId int primary key identity(1,1) not null, + UserId int foreign key references UserInfo(UserNo) not null, + WorkTime datetime not null, + WorkDescription varchar(40) not null check(WorkDescription='迟到' or WorkDescription='早退'or WorkDescription='旷工' or WorkDescription='病假' or WorkDescription='事假') +) + +use master +go + +create database Class +on +( + name='Class', + filename='D:\11\Class.mdf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +log on +( + + name='Class_log', + filename='D:\11\Class_log.ldf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +go + +use Class +go + +create table ClassInfo +( + ClassId int primary key identity(1,1), + ClassName varchar(4) unique not null, + OpenTime datetime not null, + ClassDescribe nvarchar(50) +) + +create table StuInfo +( + StuNo int primary key identity(1,1), + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女') not null, + StuAge int check(StuAge>=15 and StuAge<=40) not null, + StuAdress nvarchar(4) default('湖北武汉'), + ClassId int foreign key references ClassInfo(ClassId) +) + +create table Course +( + CourseNo int primary key identity(1,1), + CourseName nvarchar(4) unique not null, + CourseDescribe nvarchar(50) +) + +create table Credit +( + CreditNo int primary key identity(1,1), + StuNo int foreign key references StuInfo(StuNo), + CourseNo int foreign key references Course(CourseNo), + Credit int check(Credit>=0 and Credit<=100) +) + +use master +go + +create database House +on +( + name='House', + filename='D:\11\House.mdf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +log on +( + + name='House_log', + filename='D:\11\House_log.ldf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +go + +use House +go + +create table TblUser +( + UserId int primary key identity(1,1), + UserName nvarchar(4) unique not null, + UserTel varchar(11) unique not null +) + +create table TblHouseType +( + TypeId int primary key identity(1,1), + TypeName nvarchar(4) unique not null +) + +create table TblQx +( + QxId int primary key identity(1,1), + QxName nvarchar(4) unique not null +) + +create table HouseInfo +( + Id int primary key identity(1,1), + Desc1 nvarchar(50), + UserId int foreign key references TblUser(UserID), + zj money, + shi int, + ting int, + TypeId int foreign key references TblHouseType(TypeId), + QxId int foreign key references TblQx(QxId) +) + +use master +go + +create database bbs +on +( + name='bbs', + filename='D:\11\bbs.mdf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +log on +( + + name='bbs_log', + filename='D:\11\bbs_log.ldf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +go + +use bbs +go + +create table BbsUsers +( + UserId int identity(1,1), + UserName varchar(10) not null, + UserSex varchar(2) not null, + UserAge int not null, + UserPoint int not null +) + +alter table BbsUsers add constraint PK_BbsUser_UserId primary key(UserId) +alter table BbsUsers add constraint UK_BbsUser_UserName unique(UserName) +alter table BbsUsers add constraint CK_BbsUser_UserSex check(UserSex='男' or UserSex='女') +alter table BbsUsers add constraint CK_BbsUser_UserAge check(UserAge>=15 and UserAge<=60) +alter table BbsUsers add constraint CK_BbsUser_UserPoint check(UserPoint>=0) + +create table BbsTopic +( + TopicId int primary key identity(1,1), + TopicUserId int foreign key references BbsUsers(UserId), + TopicSId int foreign key references BbsSection(SectionId), + TopicTitle varchar(100) not null, + TopicMsg text not null, + TopicTime datetime, + TopicCount int +) + +create table BbsReply +( + ReplyId int primary key identity(1,1), + RepluUid int foreign key references BbsUsers(UserId), + ReplyTid int foreign key references BbsTopic(TopicId), + ReplyMsg text not null, + ReplyTime datetime +) + +create table BbsSection +( + SectionId int Identity(1,1), + SectionName varchar(10) not null, + SectionUid int +) + +alter table BbsSection add constraint PK_BbsSection_SectionId primary key(SectionId) +alter table BbsSection add constraint FK_BbsSection_SectionUid foreign key(SectionUid) references BbsUsers(UserId) + +insert into BbsUsers values +('小雨点','女','20','0'), +('逍遥','男','18','4'), +('七年级生','男','19','2') + + +select UserName,UserPoint into BbsPoint from BbsUsers + +insert into BbsSection values +('技术交流',3), +('读书世界',5), +('生活百科',3), +('八卦区',5) +select * from BbsTopic +insert into BbsTopic values +(4,6,'范跑跑','谁是范跑跑','2008-7-8',1), +(5,3,'.NET','与JAVA的区别是什么呀?','2008-9-1',2), +(3,5,'今年夏天最流行什么','有谁知道及那年夏天最流行什么呀?','2008-9-10',0) + +insert into BbsReply values +(3,5,'计划司法机会大','2008-9-1'), +(3,6,'家私房价','2008-10-1'), +(4,7,'山东矿机拉速度快','2008-10-10') + +alter table BbsReply drop constraint FK__BbsReply__RepluU__1FCDBCEB +alter table BbsTopic drop constraint FK__BbsTopic__TopicU__1BFD2C07 +delete BbsUsers where UserName='逍遥' + +update bbsUsers set UserPoint=12 where UserName='小雨点' + +alter table BbsTopic drop constraint FK__BbsTopic__TopicS__1CF15040 +delete BbsSection where SectionName='生活百科' + +delete BbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..860c7ddb9f32abe11f0799f72abc530abdd6432a --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery1.sql" @@ -0,0 +1,60 @@ +create database company + +on + +( + + name='company', + + filename='D:\company.mdf', + + size=10, + + maxsize=100, + + filegrowth=10 + +) + +log on + +( + + name='company_log', + + filename='D:\company_log.ldf', + + size=10, + + maxsize=100, + + filegrowth=10 + +) +go + +use company + +go + +create table sectionInfo +( + sectionID int identity(1,1) primary key, + sectionName varchar(10) not null + +) +create table userInfo +( + userNo int identity(1,1) primary key not null, + userName varchar(10) not null unique check(len( userName)>4), + userSex varchar(2) check(usersex=''or usersex='女' ) not null, + userAge int not null check(userAge>0 and userAge<=100), + userAddress varchar(50) default(''), + userSection int references sectionInfo(sectionID) +) +create table workInfo +( workId int identity(1,1) primary key not null, + userId int references userInfo(userNo) null, + workTime datetime null, + workDescription varchar(40) null check(workDescription='璇峰亣'or workDescription='杩熷埌'or workDescription='鏃╅'or workDescription='缂哄嫟'or workDescription='鏃疯') +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..53f62a4a30ecc0f4ecbc061ad445c690b9f4ae60 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery2.sql" @@ -0,0 +1,57 @@ +create database TestDB + +on + +( + + name='TestDB', + + filename='D:\TestDB.mdf', + + size=10, + + maxsize=100, + + filegrowth=10 + +) + +log on + +( + + name='TestDB_log', + + filename='D:\TestDB_log.ldf', + + size=10, + + maxsize=100, + + filegrowth=10 + +) +go + +use TestDB + +go +create table typeInfo +( + + typeId int primary key, + + typeName varchar(10) not null unique, + + + +) + create table loginInfo + ( + LoginId int primary key, + LoginName varchar(10) not null unique, + LoginPwd varchar(20) not null default('123456'), + StuSex nvarchar(1) default('') check(StuSex='鐢' or StuSex='濂' ) not null, + shengrr int , + huiyuanleibie int + ) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery3.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..640cbb6bebe9e7818e3a2d54edc87061bba27d59 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery3.sql" @@ -0,0 +1,67 @@ +create database student +on + +( + + name='student', + + filename='D:\student.mdf', + + size=10, + + maxsize=100, + + filegrowth=10 + +) + +log on + +( + + name='student_log', + + filename='D:\student_log.ldf', + + size=10, + + maxsize=100, + + filegrowth=10 + +) + + +use student + +go + +create table ban +( classid int identity(1,1) primary key, + banjim varchar(20)not null, + kaiban int null, + banjimiao int + +) +create table xue + +( xuehao int identity(1,1) primary key, + xing varchar(10) not null unique check(len( xing)>2), + userSex varchar(1) default('') check(usersex='鐢'or usersex='濂' ) not null, + userAge int not null check(userAge>=15 and userAge<=40), + userAddress varchar(10) default('123456'), + userSection int references ban(classid) +) +create table ke +( keche int identity(1,1) primary key, + kename varchar(20) not null, + kechengmiaosgy int +) +create table cheng +( bian int identity(1,1) primary key, + sadsad int references xue(xuehao) null, + sadsadss int references ke(keche) null, + sadf int check(sadf>0 and sadf<=100) + + +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery4.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..db904bcdb6e0108e1ae510e9358bd8ef45ac44f2 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery4.sql" @@ -0,0 +1,84 @@ +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf', + size=20, + maxsize=300, + filegrowth=50 +) +log on +( + name='bbs_log', + filename='D:\bbs_log.ldf', + size=20, + maxsize=300, + filegrowth=50 +) +go + +use bbs +go + +create table bbsUsers +( +UID int identity(1,1) , +uName varchar(10) null, +uSex varchar(2) , +uAge int null , +uPoint int null +) + +create table bbsTopic +( tID int identity(1,1) , + tUID int , + tSID int , + tTitle varchar(100) null , + tMsg text null, + tTime datetime, + tCount int +) +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 in('鐢','濂')) +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 bbsTopic add constraint PK_bbsTopic_tID primary key (tID) +alter table bbsTopic add constraint FK_bbsTopic_tUID foreign key(tUID) REFERENCES bbsUsers([UID]) +alter table bbsTopic add constraint FK_bbsTopic_tSID foreign key(tSID) references bbsSection([sID]) + + + +create table bbsReply +(rID int identity(1,1)primary key, + rUID int references bbsUsers(UID), + rTID int references bbsTopic(tID), + rMsg text null, + rTime datetime +) + +create table bbsSection +( sID int identity(1,1)primary key, + sName varchar(10) null, + sUid int foreign key 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', '鍏崷鍖' , '鑼冭窇璺' , ' 璋佹槸鑼冭窇璺' , ' 2008-7-8', '1'),('3', '鎶鏈氦娴' , '.NET' , ' 涓嶫AVA鐨勫尯鍒槸浠涔堝憖锛' , '2008-9-1' , '2'),( '1', '鐢熸椿鐧剧', '浠婂勾澶忓ぉ鏈娴佽浠涔 ', ' 鏈夎皝鐭ラ亾浠婂勾澶忓ぉ鏈娴佽浠涔堝憖锛', '2008-9-10', 0) + insert into bbsReply(rID ,rUID,rMsg ,rTime )values('2','2','鍛靛懙','2020'),('1','3','鍛靛懙鍛','2020'),('3','1','鍛靛懙鍛靛懙','2020') + alter table bbsUsers drop constraint FK_bbsUsers_UID + alter table bbsUsers drop constraint FK_bbsUsers_uName + alter table bbsTopic drop constraint FK_bbsTopic_tUID + alter table bbsTopic drop constraint FK_bbsTopic_tSID + update bbsPoint set uPoint=(uPoint + 10) where uName='灏忛洦鐐' + delete from bbsReply + delete from bbsTopic + delete from bbsSection + delete from bbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery5.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3f0f67b8ff11c2dfff785b6debd7574c58399f24 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\350\266\212/SQLQuery5.sql" @@ -0,0 +1,45 @@ +create database Student2 + +on + +( + + name='Student2', + + filename='D:\Student2.mdf', + + size=10, + + maxsize=100, + + filegrowth=10 + +) + +log on + +( + + name='Student2_log', + + filename='D:\Student2_log.ldf', + + size=10, + + maxsize=100, + + filegrowth=10 + +) +go + +use Student2 + +go +create table tblUser +( +userId int identity(1,1) primary key not null, +userName varchar(20), +userTel varchar(20) not null, +userTeldsa int check(len(userTeldsa)=11) +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery6.1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery6.1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c8aa234c8fc21096e73bd65cde3629fe9c2d1572 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery6.1.sql" @@ -0,0 +1,118 @@ +create database bbs +on +( + fileName='F:\homework\bbs.mdf', + Name='bbs', + size=5MB, + Maxsize=5MB, + filegrowth=1MB +) +log on +( + fileName='F:\homework\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) + + +alter table bbsTopic drop constraint FK__bbsTopic__tUID__1920BF5C + +alter table bbsReply drop constraint FK__bbsReply__rUID__1CF15040 + + +delete from bbsUsers where uName='逍遥' + +update bbsUsers set uPoint=10 where uName='小雨点' + +alter table bbsTopic drop constraint FK__bbsTopic__tSID__1A14E395 + +delete from bbsSection where sName='生活百科' + +delete from bbsReply + + + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery6.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ef11463c4a692ca753079f745b9fef00abc01a7f --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery6.sql" @@ -0,0 +1,201 @@ +create database TestDB +on +( + FileName='F:\homework\TestDB.mdf', + Name='TestDB', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +log on +( + FileName='F:\homework\TestDB_log.ldf', + Name='TestDB_log', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +go + +use TestDB + +go + +create table typeInfo +( + typeID int primary key identity(1,1), + typeName varchar(10) not null, +) + +create table loginInfo +( + LoginID int primary key identity(1,1), + LoginName varchar(10) not null unique, + LoginPwd varchar(20) not null default(123456), + LogSex nvarchar(1), + Logbirthday date, + LogVIP nvarchar(10), +) + + +create database Company +on +( + FileName='F:\homework\Company.mdf', + Name='Company', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +log on +( + FileName='F:\homework\Company_log.ldf', + Name='Company_log', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +go +use Company +go + + +create table sectionInfo +( + sectionID int identity(1,1) primary key , + sectionName varchar(10) not null, +) + +create table userInfo +( + userNo int identity(1,1) primary key not null, + userName varchar(10) unique not null check(len(userName)>4), + userSex varchar(2) not null check(userSex in('男','女')), + userAge int not null check(userAge>=1 or userAge<=100), + userAddress varchar(50) default('湖北'), + userSection int foreign key references sectionInfo(sectionID) +) + +create table workInfo +( + workID int identity(1,1) primary key not null, + userID int foreign key references userInfo(userNo), + workTime datetime not null, + workDescription varchar(40) not null check(workDescription in ('迟到','早退','旷工','病假','事假')) +) + + + +create database SMS +on +( + FileName='F:\homework\SMS.mdf', + Name='SMS', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +log on +( + FileName='F:\homework\SMS_log.mdf', + Name='SMS_log', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +go +use SMS +go + +create table ClassInfo +( + ClassID int primary key identity(1,1), + ClassName nvarchar(10) not null unique, + StartTime date not null, + ClassDescribe text, +) + +create table StudentInfo +( + StuID int primary key identity(1,1), + StuName nvarchar(10) unique check(len(StuName)>2), + StuSex nvarchar(1) check(StuSex in ('男','女')) default('男') not null, + StuAge int check(StuAge>=15 or StuAge<=40) not null, + StuAddress nvarchar(30) default('湖北武汉'), + ClassID int +) + + +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar not null unique, + CourseS text +) + +create table Score +( + ScoreID int primary key identity(1,1), + StudentID int not null, + Course int not null, + Score int check(Score>=0 or Score<=100) +) + + +create database HouseRental +on +( + fileName='F:\homework\HouseRental.mdf', + Name='HouseRental', + size=5MB, + Maxsize=5MB, + filegrowth=1MB +) +log on +( + fileName='F:\homework\HouseRental_log.mdf', + Name='HouseRental_log', + size=5MB, + Maxsize=5MB, + filegrowth=1MB +) + +go + +use HouseRental + +go + + +create table tblHouseInfo +( + HouseID int primary key identity(1,1), + Housedesc text , + UserID int not null, + HouseRental money not null, + Houseshi int not null, + Houseting int not null, + TypeID int not null, + QxUD int not null +) + +create table tblUser +( + UserID int, + UserName nvarchar(10) not null, + UserTel nvarchar(11) +) + +create table tblHouseType +( + TypeID int not null, + TypeName nvarchar(10) +) + +create table tblQx +( + QxUD int not null, + QxName nvarchar(10) +) + + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\347\273\203\344\271\2401.txt" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\347\273\203\344\271\2401.txt" new file mode 100644 index 0000000000000000000000000000000000000000..584c33045caf477148cba6efe0030048ddf1486f --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\347\273\203\344\271\2401.txt" @@ -0,0 +1,101 @@ +用SQL实现以下的题目: + +1. 先创建一个数据库,数据库名为TestDB,要求有两个数据文件,一个日志文件,注意命名规范,文件存放在E盘下 + 再在上面的数据库下创建表,结构如下: + + 会员类别表(typeInfo): + 类别编号(typeId):主键、自动编号 + 类别名(typeName): varchar(10) 不能为空 + + 登录用户表(loginInfo): + 编号(LoginId),数据类型(int),主键、自动编号 + 账户(LoginName),文本,长度为10,非空,必须唯一,不能重复 + 密码(LoginPwd),文本,长度为20,非空、默认值为‘123456’ + 性别(自定类型) + 生日(自定类型) + 会员类别(自定类型) + + +2. 先创建一个数据库用来存放某公司的员工信息,数据库的名称为company,包含2个数据文件1个日志 +文件,数据文件和日志文件全部存放在E盘中,初始大小,增长和最大大小自己设定 + 再创建表: + + 部门信息表(sectionInfo) + 部门编号 sectionID int 标识列 主键 + 部门名称 sectionName varchar(10) 不能为空 + + + 员工信息表(userInfo) + 员工编号 userNo int 标识列 主键 不允许为空 + 员工姓名 userName varchar(10) 唯一约束 不允许为空 长度必须大于4 + 员工性别 userSex varchar(2) 不允许为空 只能是男或女 + 员工年龄 userAge int 不能为空 范围在1-100之间 + 员工地址 userAddress varchar(50) 默认值为“湖北” + 员工部门 userSection int 外键,引用部门信息表的部门编号 + + + 员工考勤表(workInfo) + 考勤编号 workId int 标识列 主键 不能为空 + 考勤员工 userId int 外键 引用员工信息表的员工编号 不能为空 + 考勤时间 workTime datetime 不能为空 + 考勤说明 workDescription varchar(40) 不能为空 内容只能是“迟到”,“早退”,“旷工”,“病假”,“事假”中的一种 + + +3. 为学校开发一个学生管理系统,请为该系统创建数据库,主要存放的信息有:班级信息、学生信息、课程信息、学生考试成绩 + 班级信息:班级编号 classid (主键、标识列) + 班级名称(例如:T1、T2、D09等等):不能为空,不能重复 + 开办时间:不能为空 + 班级描述 + + 学生信息:学号:主键、标识列 + 姓名:长度大于2,不能重复 + 性别:只能是‘男’或‘女’,默认为男,不能为空 + 年龄:在15-40之间,不能为空 + 家庭地址:默认为“湖北武汉” + 所在的班级编号 + + 课程信息:编号:主键、标识列 + 课程名:不能为空,不能重复 + 课程描述: + + + 成绩信息:成绩编号:主键、标识列 + 成绩所属于的学生编号,不能为空 + 成绩所属的课程编号,不能为空 + 成绩:在0-100之间 + + +4. 为一个房屋出租系统创建一个数据库,数据库中主要存放房屋的信息,包括:房屋的编号,房屋的描述,发布人的信息(姓名和联系电话),房屋的租金,房屋的室数,房屋的厅数,房屋的类型(别墅、普通住宅、平房、地下室),房屋所属的区县(武昌、汉阳、汉口),请根据上面的描述设计表,并设计表的关系,以及列的约束 + +tblUser --发布人信息表 +userId +userName +userTel + + +tblHouseType --房屋的类型 +typeId +typName + +tblQx --区县 +qxId +qxName + +tblHouseInfo--房屋信息表 +id +desc +userId -- +zj +shi +ting +typeId -- +qxId -- + + + + + + + + + \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\347\273\203\344\271\2402.txt" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\347\273\203\344\271\2402.txt" new file mode 100644 index 0000000000000000000000000000000000000000..14010d3f0d8b8e57a34a90fef875dcfb56d5692f --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\347\273\203\344\271\2402.txt" @@ -0,0 +1,86 @@ +一、先创建数据库和表以及约束 + + 1.创建一个数据库用来存放某论坛的用户和发帖信息,数据库的名称为bbs,包含1个数据文件1个日志 + 文件,数据文件和日志文件全部存放在E盘中,初始大小,增长和最大大小自己设定 + + + 2.创建表 + + 注意以下4张表的创建顺序,要求在创建bbsUser和bbsSection表时不添加约束,创建完后单独添加约束,其它的表创建时添加约束 + + 用户信息表(bbsUsers) + 用户编号 UID int 主键 标识列 + 用户名 uName varchar(10) 唯一约束 不能为空 + 性别 uSex varchar(2) 不能为空 只能是男或女 + 年龄 uAge int 不能为空 范围15-60 + 积分 uPoint int 不能为空 范围 >= 0 + + + ++ 主贴表(bbsTopic) + 主贴编号 tID int 主键 标识列, + 发帖人编号 tUID int 外键 引用用户信息表的用户编号 + 版块编号 tSID int 外键 引用版块表的版块编号 (标明该贴子属于哪个版块) + 贴子的标题 tTitle varchar(100) 不能为空 + 帖子的内容 tMsg text 不能为空 + 发帖时间 tTime datetime + 回复数量 tCount int + + + ++ 回帖表(bbsReply) + 回贴编号 rID int 主键 标识列, + 回帖人编号 rUID int 外键 引用用户信息表的用户编号 + 对应主贴编号 rTID int 外键 引用主贴表的主贴编号 (标明该贴子属于哪个主贴) + 回帖的内容 rMsg text 不能为空 + 回帖时间 rTime datetime + + + + 版块表(bbsSection) + 版块编号 sID int 标识列 主键 + 版块名称 sName varchar(10) 不能为空 + 版主编号 sUid int 外键 引用用户信息表的用户编号 + + +二、在上面的数据库、表的基础上完成下列题目: + + 1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: + 小雨点 女 20 0 + 逍遥 男 18 4 + 七年级生 男 19 2 + + 2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 + + 3.给论坛开设4个板块 + 名称 版主名 + 技术交流 小雨点 + 读书世界 七年级生 + 生活百科 小雨点 + 八卦区 七年级生 + + 4.向主贴和回帖表中添加几条记录 + + 主贴: + + 发帖人 板块名 帖子标题 帖子内容 发帖时间 回复数量 + 逍遥 八卦区 范跑跑 谁是范跑跑 2008-7-8 1 + 七年级生 技术交流 .NET 与JAVA的区别是什么呀? 2008-9-1 2 + 小雨点 生活百科 今年夏天最流行什么 有谁知道今年夏天最流行 2008-9-10 0 + 什么呀? + + 回帖: + 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 + + 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) + + 6.因为小雨点发帖较多,将其积分增加10分 + + 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) + + 8.因回帖积累太多,现需要将所有的回帖删除 + + + + + \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\350\224\241\351\233\252\345\274\272/\344\275\234\344\270\2321.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\350\224\241\351\233\252\345\274\272/\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..48443131302c8727f031be15e6fc28c0b69c1f76 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\350\224\241\351\233\252\345\274\272/\344\275\234\344\270\2321.sql" @@ -0,0 +1,144 @@ +use master +go +create database TestDB +on +( +name='TestDB', +filename='D:\SQL\TestDB.mdf' +) +log on +( +name='TestDB_log', +filename='D:\SQL\TestDB_log.ldf' +) +use TestDB +go +create table typeInfo +( +typeId int primary key identity(1,1), +typeName varchar(10) not null, +) +create table loginInfo +( +LoginId int primary key identity(1,1), +LoginName varchar(10) unique not null, +LoginPwd varchar(20) not null default(123456), +LogSex char(1) default('') check(LogSex='' or LogSex='女'), +Logbrithday datetime, +huiyuan varchar(10) +) +create database company +on +( +name='company', +filename='D:\SQL\company.mdf' +) +log on +( +name='company_log', +filename='D:\SQL\company_log.ldf' +) +use company +go +create table sectionInfo +( + sectionID int primary key, + sectionName varchar(10) not null +) +create table userInfo +( + userNo int primary key not null, + userName varchar(10) unique not null check(len(userName)>4), + userSex varchar(2) not null check(userSex='' or userSex='女' ), + userAge int not null check(userAge>=1 or userAge<=100), + userAddress varchar(50) default(''), + userSection int constraint FK_sectionInfo_section references sectionInfo(sectionID) +) +create table workInfo +( + workId int primary key not null, + userId int constraint FK_userInfo_userNo references userInfo(userNo), + workTime datetime not null, + workDescription varchar(40) not null check(workDescription='俚'or workDescription=''or workDescription='' or workDescription=''or workDescription='录') +) +create database Student +on +( +name='Student', +filename='D:\SQL\Student.mdf' +) +log on +( +name='Student_log', +filename='D:\SQL\Student_log.ldf' +) +use Student +go +create table Classinfo +( + classid int primary key, + classname varchar(10) not null unique, + classremark ntext +) +create table studentinfo +( + stunum int primary key, + stuname varchar check (len(stuname)>2) unique, + stusex varchar(2) default('') check(stusex='' or stusex='女'), + stuage int check(stuage>=15 or stuage<=40) not null, + stuaddress nvarchar(50) default('浜'), + stuclassid int constraint FK_Classinfo_classid references Classinfo(classid) +) +create table information +( + number int primary key, + kechenbiao varchar unique not null, + miaoshu ntext +) +create table score +( + scorenum int primary key, + scorestuid int constraint FK_studentinfo_stunum references studentinfo(stunum) not null, + scoreid int constraint FK_information_number references information(number) not null, + score int check(score>=0 or score<=100) +) +create database fangwuchuzhu +on +( +name='fangwuchuzhu', +filename='D:\SQL\fangwuchuzhu.mdf' +) +log on +( +name='fangwuchuzhu_log', +filename='D:\SQL\fangwuchuzhu_log.ldf' +) +use fangwuchuzhu +go +create table tblUser +( + userId int primary key identity(1,1), + userName varchar, + userTel nvarchar(10) +) +create table tblHouseType +( + typeId int primary key identity(1,1), + typName varchar check(typName='莘1'or typName='莘2'or typName='莘3' or typName='莘4') not null +) +create table tblQx +( + qxId int primary key identity(1,1), + qxName varchar check(qxName=''or qxName='' or qxName='') not null +) + +create table tblHouseInfo +( + id int primary key identity(1,1), + userId int constraint FK_tblUser_userId references tblUser(userId), + zj money, + shi varchar(20), + ting int, + typeId int constraint FK_tblHouseType_typeId references tblHouseType(typeId), + qxId int constraint FK_tblQx_qxId references tblQx(qxId) +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\350\224\241\351\233\252\345\274\272/\344\275\234\344\270\2322.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\350\224\241\351\233\252\345\274\272/\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..215591412c278ff8a7550f0ae7d4d1b4f39df02d --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/\350\224\241\351\233\252\345\274\272/\344\275\234\344\270\2322.sql" @@ -0,0 +1,62 @@ + 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' +) +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) +) +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 +) +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 +) +create table bbsSection +( +sID int primary key identity(1,1), +sName varchar(10) not null, +sUid int, +) +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) +alter table bbsTopic drop constraint FK_bbsUsers1_UID +alter table bbsReply drop constraint FK_bbsUsers_UID +delete from bbsUsers where uID=2 +insert into bbsUsers(uName,uPoint) values('小雨点',10) +alter table bbsTopic drop constraint FK_bbsSection_sID +delete from bbsSection where sName='生活百科' +delete from bbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/Student.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/Student.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b0274d6a609ab7aef762b6a7673003d954453650 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/Student.sql" @@ -0,0 +1,47 @@ +create database Student +on +( + name='TestDB', + filename='D:\test\Student.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10MB +) + +log on +( + name='Student_log', + filename='D:\test\Student_log.ldf', + size=5MB, + maxsize=100MB, + filegrowth=10MB +) +create table class +( +classID char(10) not null unique, +radie datetime, +classdescrible nvarchar(50) +) +create table stuinfo +( +stuID int primary key, +stuName char(2) unique, +stuSex char(1) default ('男') check (stuSex in ('男','女')), +stuAge int not null check (stuAge>=15 and stuAge<=40), +stuAddress nvarchar(20) default ('湖北武汉'), +classID int +) +create table classchedule +( +classld int primary key , +scheduleID varchar(10) not null unique, +descrinle nvarchar(50) +) +create table mark +( +markID int primary key , +stuMark int not null, +classMark int not null, +Mark int check (Mark>=0 and Mark>=100) +) + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/TestDB.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/TestDB.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7e458f80fdd649a66cb83eb9939fe49fd6f59a24 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/TestDB.sql" @@ -0,0 +1,35 @@ +create database TestDB +on +( + name='TestDB', + filename='D:\test\TestDB.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10MB +) + +log on +( + name='TestDB_log', + filename='D:\test\TestDB_log.ldf', + size=5MB, + maxsize=100MB, + filegrowth=10MB +) +use TestDB +go +create table typrlnfo +( +typeld int, +typeName varchar(10) not null +) +create table loginlngo +( +Loginld int primary key identity (1,1), +LoginName varchar(10) not null unique, +LoginPwd varchar(20) not null default('1,2,3,4'), +LogSex char(1) default ('男') check (LogSex in ('男','女')), +Logbirthday date, +LogFind int + +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/bbs.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/bbs.sql" new file mode 100644 index 0000000000000000000000000000000000000000..16fe0598af0cbea45fb30e8ff70a7270025985db --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/bbs.sql" @@ -0,0 +1,112 @@ +create database bbs +on +( + name='bbs', + filename='D:\text\bbs.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='bbs_log', + filename='D:\text\bbs_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +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 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) +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_UID references bbsUsers(UID), + tSID int constraint FK_bbsSection_sID references bbsSection(sID), + tTitle varchar(100), + tMsg text , + tTime datetime, + tCount int +) +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, +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 +-- insert into bbsPoint select uName,uPoint from bbsUsers +-- select uName,uPoint into bbsPoint from bbsUsers + +-- 3.给论坛开设4个板块 +-- 名称 版主名 +-- 技术交流 小雨点 +-- 读书世界 七年级生 +-- 生活百科 小雨点 +-- 八卦区 七年级生 +insert into bbsSection values ('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) +-- 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) +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 +insert into bbsReply (rUID,rMsg)values +('李小','无') +-- 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) +alter table bbsTopic drop constraint FK_bbsSection_sID +delete from bbsUsers where uName='逍遥' +-- 6.因为小雨点发帖较多,将其积分增加10分 +update bbsUsers set uPoint=30 where uName='小雨点' +-- 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) +alter table bbsTopic drop constraint FK_bbsSection_sID +delete from bbsSection where sName='生活百科' +-- 8.因回帖积累太多,现需要将所有的回帖删除 +delete from bbsReply +--drop table bbsReply +--delete from bbsReply +--truncate table bbsReply diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/company.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/company.sql" new file mode 100644 index 0000000000000000000000000000000000000000..295834a45bbe7abaf0833d32e1f3791213c7c692 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/company.sql" @@ -0,0 +1,41 @@ +create database company +on +( +name='company', +filename='D:\test\company.mdf', +size=5MB, +maxsize=100MB, +filegrowth=10% + +) +log on +( +name='company_log.ldf', +filename='D:\test\company_log.ldf', +size=5MB, +maxsize=100MB, +filegrowth=10% +) +create table sectionlnfo +( +sectionID int primary key identity(1,1), +sectionName varchar(10) not null, + +) +create table userlnfo +( +userNo int primary key identity (1,1) not null, +userName varchar (10) unique not null , +userSex varchar(2) not null default('男') check (userSex in ('男','女')), +useAge int not null , +userAddress varchar(50) default('湖北'), +userSection int references sectionlnfo(sectionID) +) +create table worklonfo +( +workld int primary key not null, +userld int references userlnfo(userNo), +workTime datetime not null, +workDescription varchar(40) not null default('迟到') + +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..59f0fa9cd2954c7925dca24080a1f9a18260c216 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery1.sql" @@ -0,0 +1,118 @@ +create database bbs +on +( + fileName='F:\homework\bbs.mdf', + Name='bbs', + size=5MB, + Maxsize=5MB, + filegrowth=1MB +) +log on +( + fileName='F:\homework\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) + + +alter table bbsTopic drop constraint FK__bbsTopic__tUID__1920BF5C + +alter table bbsReply drop constraint FK__bbsReply__rUID__1CF15040 + + +delete from bbsUsers where uName='逍遥' + +update bbsUsers set uPoint=10 where uName='小雨点' + +alter table bbsTopic drop constraint FK__bbsTopic__tSID__1A14E395 + +delete from bbsSection where sName='生活百科' + +delete from bbsReply + + + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2722ba5099f5f46a1591acd65401950d7e3e396b --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery2.sql" @@ -0,0 +1,201 @@ +create database TestDB +on +( + FileName='F:\homework\TestDB.mdf', + Name='TestDB', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +log on +( + FileName='F:\homework\TestDB_log.ldf', + Name='TestDB_log', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +go + +use TestDB + +go + +create table typeInfo +( + typeID int primary key identity(1,1), + typeName varchar(10) not null, +) + +create table loginInfo +( + LoginID int primary key identity(1,1), + LoginName varchar(10) not null unique, + LoginPwd varchar(20) not null default(123456), + LogSex nvarchar(1), + Logbirthday date, + LogVIP nvarchar(10), +) + + +create database Company +on +( + FileName='F:\homework\Company.mdf', + Name='Company', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +log on +( + FileName='F:\homework\Company_log.ldf', + Name='Company_log', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +go +use Company +go + + +create table sectionInfo +( + sectionID int identity(1,1) primary key , + sectionName varchar(10) not null, +) + +create table userInfo +( + userNo int identity(1,1) primary key not null, + userName varchar(10) unique not null check(len(userName)>4), + userSex varchar(2) not null check(userSex in('男','女')), + userAge int not null check(userAge>=1 or userAge<=100), + userAddress varchar(50) default('湖北'), + userSection int foreign key references sectionInfo(sectionID) +) + +create table workInfo +( + workID int identity(1,1) primary key not null, + userID int foreign key references userInfo(userNo), + workTime datetime not null, + workDescription varchar(40) not null check(workDescription in ('迟到','早退','旷工','病假','事假')) +) + + + +create database SMS +on +( + FileName='F:\homework\SMS.mdf', + Name='SMS', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +log on +( + FileName='F:\homework\SMS_log.mdf', + Name='SMS_log', + size=5MB, + maxsize=5MB, + filegrowth=3MB +) +go +use SMS +go + +create table ClassInfo +( + ClassID int primary key identity(1,1), + ClassName nvarchar(10) not null unique, + StartTime date not null, + ClassDescribe text, +) + +create table StudentInfo +( + StuID int primary key identity(1,1), + StuName nvarchar(10) unique check(len(StuName)>2), + StuSex nvarchar(1) check(StuSex in ('男','女')) default('男') not null, + StuAge int check(StuAge>=15 or StuAge<=40) not null, + StuAddress nvarchar(30) default('湖北武汉'), + ClassID int +) + + +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar not null unique, + CourseS text +) + +create table Score +( + ScoreID int primary key identity(1,1), + StudentID int not null, + Course int not null, + Score int check(Score>=0 or Score<=100) +) + + +create database HouseRental +on +( + fileName='F:\homework\HouseRental.mdf', + Name='HouseRental', + size=5MB, + Maxsize=5MB, + filegrowth=1MB +) +log on +( + fileName='F:\homework\HouseRental_log.mdf', + Name='HouseRental_log', + size=5MB, + Maxsize=5MB, + filegrowth=1MB +) + +go + +use HouseRental + +go + + +create table tblHouseInfo +( + HouseID int primary key identity(1,1), + Housedesc text , + UserID int not null, + HouseRental money not null, + Houseshi int not null, + Houseting int not null, + TypeID int not null, + QxUD int not null +) + +create table tblUser +( + UserID int, + UserName nvarchar(10) not null, + UserTel nvarchar(11) +) + +create table tblHouseType +( + TypeID int not null, + TypeName nvarchar(10) +) + +create table tblQx +( + QxUD int not null, + QxName nvarchar(10) +) + + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0338c79f402eb838a9f9f483b5769ec7bd832c90 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery2.sql" @@ -0,0 +1,137 @@ +use master +go +create database TestDB +on +( + name='TestDB', + filename='D:\TestDB.mdf', + size=5MB, + maxsize=50MB, + filegrowth =10% +) +log on +( + name='TestDB_log', + filename='D:\TestDB.ldf', + size=5MB, + maxsize=50MB, + filegrowth =10% +) +use TestDB +go + +create table typeInfo +( + typeId int primary key identity (1,1), + typeName varchar(10) not null, +) +create table loginInfo +( + LoginId int primary key identity (1,1), + LoginName nvarchar(10) unique not null, + LoginPwd nvarchar(20) default 123456 not null, + sex nvarchar(1) check (sex='男' or sex='女'), + Birthday datetime not null, + Member int not null, +) +create database company +on +( + name='company', + filename='D:\company.mdf', + size=5MB, + maxsize=50MB, + filegrowth =10% +) +log on +( + name='company_log', + filename='D:\company.ldf', + size=5MB, + maxsize=50MB, + filegrowth =10% +) +create table sectionInfo +( + sectionID int primary key identity (1,1), + sectionName varchar(10) not null, +) +create table userInfo +( + userNo int primary key identity (1,1) not null, + userName varchar(10) unique check( userName>4) not null, + userSex varchar(2) check(userSex='男'or userSex='女') not null, + userAge int check (userAge>1and userAge<100) not null, + userAddress varchar(50) default'湖北', + userSection int references sectionInfo(sectionID), +) +create table workInfo +( + workId int primary key identity (1,1) not null, + userId int references userInfo(userNo) not null, + workTime datetime not null, + workDescription varchar(40) not null check (workDescription= '早退'or workDescription='迟到'or workDescription='旷工' or workDescription='病假' or workDescription='事假') + +) +go +create database Student +use Student +go +create table Class +( + classid int primary key identity (1,1), + classname nvarchar(20) unique not null, + Opentime datetime not null, + classdescription ntext, +) +create table Students +( + stunumber int primary key identity (1,1), + stuname nvarchar(10) unique check(stuname>2), + stusex nvarchar(1) default'男' check(stusex='男'or stusex='女') not null, + stuage int check(stuage>=15and stuage<=40) not null, + stuaddress nvarchar(20) default'湖北武汉', + classid int references Class(classid), +) +create table course +( + courseid int primary key identity (1,1), + coursename nvarchar(10) unique not null, + course text , +) +create table performance +( + performanceid int primary key identity (1,1), + stunumber int references Students(stunumber) not null, + courseid int references course(courseid) not null, + score int check (score>=0 and score<=100), +) +create database house +go +create table tblUser +( + userId int primary key identity(1,1), + userName nvarchar(20), + userTel char(20), +) +create table tblHouseType +( + typeId int primary key identity(1,1), + typName nvarchar(10) check(typName='别墅'or typName='普通住宅' or typName='平房'or typName='地下室' ), +) +create table tblQx +( + qxId int primary key identity(1,1), + qxName nvarchar(20) check (qxName='武昌'or qxName='汉阳'or qxName='汉口'), +) +create table tblHouseInfo +( + tblHouseid int primary key identity(1,1), + describe text, + userId int foreign key references tblUser(userId), + rent int, + shishu int, + tingshu int, + typeId int foreign key references tblHouseType(typeId), + qxId int foreign key references tblQx(qxId) +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery3.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8a6d1c78093eebf89121e84c2d59fd4b4bfc8492 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery3.sql" @@ -0,0 +1,136 @@ +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% +) +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 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_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(tID) identity(1,1), + tUID int, + tSID int, + tTitle varchar(100) not null, + tMsg text not null, + tTime datetime, + tCount int +) +alter table bbsTopic add constraint FK_bbsTopic_tUID foreign key(tUID) references bbsUsers(UID) +alter table bbsTopic add constraint FK_bbsTopic_tSID foreign key(tSID) references bbsSection(sID) + + +create table bbsReply +( + + rID int primary key(rID) identity(1,1), + + rUID int, + rTID int, + rMsg text not null, + + rTime datetime, +) +alter table bbsReply add constraint FK_bbsReply_rUID foreign key(rUID) references bbsUsers(UID) +alter table bbsReply add constraint FK_bbsReply_rTID foreign key(rTID) references bbsSection(sID) + + +use bbs +go + +--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,4,'谁范跑跑','20210316'), + (1,3,'简单','20210316'), + (1,3,'断兵线','20210316') +select*from bbsReply + +-- 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) +select*from bbsUsers +select*from bbsSection sUid +select*from bbsTopic tUID +delete from bbsTopic where tUID=2 +select*from bbsReply rUID + +delete from bbsUsers where uName='逍遥' + +-- 6.因为小雨点发帖较多,将其积分增加10分 + select uPoint from bbsUsers where uName='小雨点' + update bbsUsers set uPoint=12 where uName='小雨点' + +-- 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) + select*from bbsTopic + alter table bbsReply drop constraint FK__bbsReply__rTID__1DE57479 + delete from bbsTopic where tSID=3 + +-- 8.因回帖积累太多,现需要将所有的回帖删除 +select*from bbsReply +truncate table bbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7be09087af9b0f207e6623ff09e09c630f72cd7c --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255/SQLQuery1.sql" @@ -0,0 +1,312 @@ +use master +go + +create database TestDB +on +( + name='TestDB', + filename='D:\TestDB.mdf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +log on +( + + name='TestDB_log', + filename='D:\TestDB_log.ldf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +go + +use TestDB +go + +create table TypeInfo +( + TypeId int primary key identity(1,1), + TypeName varchar(10) not null +) + +create table LoginInfo +( + LoginId int primary key identity(1,1), + LoginName nvarchar(10) unique not null, + LoginPwd nvarchar(20) default('123456') not null, + LoginSex varchar(1) default('男') check(LoginSex='男' or LoginSex='女'), + LoginBirthday datetime, + LoginType nvarchar(10) +) + +use master +go + +create database Company +on +( + name='Company', + filename='D:\Company.mdf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +log on +( + + name='Company_log', + filename='D:\Company_log.ldf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +go + +use Company +go + +create table SectionInfo +( + SectionId int primary key identity(1,1), + SectionName varchar(10) not null +) + +create table UserInfo +( + UserNo int primary key identity(1,1) not null, + UserName varchar(10) unique not null check(len(UserName)>4), + UserSex varchar(2) check(UserSex='男' or UserSex='女') not null, + UserAge int check(UserAge>=1 and UserAge<=100) not null, + UserAddress varchar(50) default('湖北'), + UserSection int foreign key references SectionInfo(SectionId) +) + +create table WorkInfo +( + WorkId int primary key identity(1,1) not null, + UserId int foreign key references UserInfo(UserNo) not null, + WorkTime datetime not null, + WorkDescription varchar(40) not null check(WorkDescription='迟到' or WorkDescription='早退'or WorkDescription='旷工' or WorkDescription='病假' or WorkDescription='事假') +) + +use master +go + +create database Class +on +( + name='Class', + filename='D:\Class.mdf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +log on +( + + name='Class_log', + filename='D:\Class_log.ldf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +go + +use Class +go + +create table ClassInfo +( + ClassId int primary key identity(1,1), + ClassName varchar(4) unique not null, + OpenTime datetime not null, + ClassDescribe nvarchar(50) +) + +create table StuInfo +( + StuNo int primary key identity(1,1), + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女') not null, + StuAge int check(StuAge>=15 and StuAge<=40) not null, + StuAdress nvarchar(4) default('湖北武汉'), + ClassId int foreign key references ClassInfo(ClassId) +) + +create table Course +( + CourseNo int primary key identity(1,1), + CourseName nvarchar(4) unique not null, + CourseDescribe nvarchar(50) +) + +create table Credit +( + CreditNo int primary key identity(1,1), + StuNo int foreign key references StuInfo(StuNo), + CourseNo int foreign key references Course(CourseNo), + Credit int check(Credit>=0 and Credit<=100) +) + +use master +go + +create database House +on +( + name='House', + filename='D:\House.mdf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +log on +( + + name='House_log', + filename='D:\House_log.ldf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +go + +use House +go + +create table TblUser +( + UserId int primary key identity(1,1), + UserName nvarchar(4) unique not null, + UserTel varchar(11) unique not null +) + +create table TblHouseType +( + TypeId int primary key identity(1,1), + TypeName nvarchar(4) unique not null +) + +create table TblQx +( + QxId int primary key identity(1,1), + QxName nvarchar(4) unique not null +) + +create table HouseInfo +( + Id int primary key identity(1,1), + Desc1 nvarchar(50), + UserId int foreign key references TblUser(UserID), + zj money, + shi int, + ting int, + TypeId int foreign key references TblHouseType(TypeId), + QxId int foreign key references TblQx(QxId) +) + +use master +go + +create database bbs +on +( + name='bbs', + filename='D:\bbs.mdf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +log on +( + + name='bbs_log', + filename='D:\bbs_log.ldf', + size=10Mb, + maxsize=50Mb, + filegrowth=10% +) +go + +use bbs +go + +create table BbsUsers +( + UserId int identity(1,1), + UserName varchar(10) not null, + UserSex varchar(2) not null, + UserAge int not null, + UserPoint int not null +) + +alter table BbsUsers add constraint PK_BbsUser_UserId primary key(UserId) +alter table BbsUsers add constraint UK_BbsUser_UserName unique(UserName) +alter table BbsUsers add constraint CK_BbsUser_UserSex check(UserSex='男' or UserSex='女') +alter table BbsUsers add constraint CK_BbsUser_UserAge check(UserAge>=15 and UserAge<=60) +alter table BbsUsers add constraint CK_BbsUser_UserPoint check(UserPoint>=0) + +create table BbsTopic +( + TopicId int primary key identity(1,1), + TopicUserId int foreign key references BbsUsers(UserId), + TopicSId int foreign key references BbsSection(SectionId), + TopicTitle varchar(100) not null, + TopicMsg text not null, + TopicTime datetime, + TopicCount int +) + +create table BbsReply +( + ReplyId int primary key identity(1,1), + RepluUid int foreign key references BbsUsers(UserId), + ReplyTid int foreign key references BbsTopic(TopicId), + ReplyMsg text not null, + ReplyTime datetime +) + +create table BbsSection +( + SectionId int Identity(1,1), + SectionName varchar(10) not null, + SectionUid int +) + +alter table BbsSection add constraint PK_BbsSection_SectionId primary key(SectionId) +alter table BbsSection add constraint FK_BbsSection_SectionUid foreign key(SectionUid) references BbsUsers(UserId) + +insert into BbsUsers values +('小雨点','女','20','0'), +('逍遥','男','18','4'), +('七年级生','男','19','2') + + +select UserName,UserPoint into BbsPoint from BbsUsers + +insert into BbsSection values +('技术交流',3), +('读书世界',5), +('生活百科',3), +('八卦区',5) +select * from BbsTopic +insert into BbsTopic values +(4,6,'范跑跑','谁是范跑跑','2008-7-8',1), +(5,3,'.NET','与JAVA的区别是什么呀?','2008-9-1',2), +(3,5,'今年夏天最流行什么','有谁知道及那年夏天最流行什么呀?','2008-9-10',0) + +insert into BbsReply values +(3,5,'计划司法机会大','2008-9-1'), +(3,6,'家私房价','2008-10-1'), +(4,7,'山东矿机拉速度快','2008-10-10') + +alter table BbsReply drop constraint FK__BbsReply__RepluU__1FCDBCEB +alter table BbsTopic drop constraint FK__BbsTopic__TopicU__1BFD2C07 +delete BbsUsers where UserName='逍遥' + +update bbsUsers set UserPoint=12 where UserName='小雨点' + +alter table BbsTopic drop constraint FK__BbsTopic__TopicS__1CF15040 +delete BbsSection where SectionName='生活百科' + +delete BbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/\347\273\203\344\271\240\344\270\200/SQLQuery2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/\347\273\203\344\271\240\344\270\200/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cdca72591ca7fb8620a1e0f427594f71105b58b4 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/\347\273\203\344\271\240\344\270\200/SQLQuery2.sql" @@ -0,0 +1,157 @@ +锘縰se master +go + +create database TestDB +on +( + name=TestDB, + filename='D:\SQL\TestDB.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) +log on +( + name=TestDB_ldf, + filename='D:\SQL\TestDB.ldf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) + +use TestDB +go + +create table typelnfo +( + typeld int primary key identity(1,1), + typeName varchar(10) not null, +) + +create table loginInfo +( + LoginId int primary key identity(1,1), + LoginName nvarchar(10) not null unique , + LoginPwd varchar(20) default(123456), + LoginSex nvarchar(1) check(LoginSex='' or LoginSex='女'), + LoginBir date , + LoginSort text not null +) + +create database Company +on( + name=Company, + filename='D:\SQL\Company.mdf', + size=5MB, + maxsize=5MB, + filegrowth=10% +) + log on( + name=Company_ldf, + filename='D:\SQL\Company.ldf', + size=5MB, + maxsize=5MB, + filegrowth=10% +) + +use Company +go + +create table SectionInfo +( + SectionID int primary key identity(1,1) not null, + SectionNamme varchar(10) not null, +) + +create table UserInfo +( + UserNo int identity(1,1) primary key not null, + UserName varchar(10) unique not null check(len(UserName)>=4), + UserSex varchar not null check(UserSex='' or Usersex='女'), + UserAge int not null check(UserAge>=1 or UserAge<=100), + UserAddress varchar(50) default(''), + UserSection int foreign key references SectionInfo(SectionID), +) + +create table WorkInfo +( + workld int identity(1,1) primary key not null, + userId int foreign key references UserInfo(UserNo), + WorkTime datetime Not null , + WorkDescription varchar(40) not null check(WorkDescription='俚' or WorkDescription='' or WorkDescription='' or WorkDescription='' or WorkDescription='录') +) + +create database StudentInfo +go + +use StudentInfo +go + +create table ClassInfo +( + ClassID int primary key identity(1,1), + ClassName nvarchar(10) not null unique, + ClassTime time not null, + ClassDes text +) + +create table StudentInfo +( + StuID int primary key identity(1,1), + StuName nvarchar(10) not null unique check(len(StuName)>2), + StuSex nvarchar(2) not null default('') check(StuSex='' or StuSex='女'), + StuAdress varchar(12) default('浜'), + ClassID int foreign key references ClassInfo(ClassID) +) + +create table CourseInfo +( + CourseID int primary key identity(1,1), + CourseName nvarchar(10) not null unique, + CourseDes text +) + +create table Score +( + ScoreID int primary key identity(1,1), + ScoreStuID int foreign key references StudentInfo(StuID), + ScoreClassID int foreign key references ClassInfo(ClassID), + Score int check(Score>=1 and Score<=100) not null +) + +create database HomeInfo +go + +use HomeInfo +go + +create table TbIUser +( + UserID int primary key identity(1,1), + UserName nvarchar(10) not null, + UserTel nvarchar(11) not null check(len(UserTel)=11) +) + +create table tblHouseType +( + TypelD int primary key identity(1,1), + Typname nvarchar(10) not null check(Typname='' or Typname='平' or Typname='通住宅' or Typname='') +) + +create table TbIQx +( + QxID int primary key identity(1,1), + Qxname nvarchar(10) not null check(Qxname='' or Qxname='' or Qxname='') +) + +create table TblHouseInfo +( + HomeID int primary key identity(1,1), + HomeDesc nvarchar(10), + UserID int foreign key references TbIUser(UserID), + Homezj int not null, + HomeShi int not null, + HomeTing int not null, + TypeID int foreign key references tblHouseType(TypelD), + QxID int foreign key references TbIQx(Qxid) +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/\347\273\203\344\271\240\344\272\214/SQLQuery4.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/\347\273\203\344\271\240\344\272\214/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bce551ddcca0ce73ac31dad25774e17377e752d1 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\227\255\346\270\205/\347\273\203\344\271\240\344\272\214/SQLQuery4.sql" @@ -0,0 +1,115 @@ +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% +) + +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 diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery6.1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery6.1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7f834de5ad4a54833dd7b0b85f6f0f1df288f23a --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery6.1.sql" @@ -0,0 +1,172 @@ +create database TestDB +on +( + name='TestDB', + filename='D:\TestDB.mdf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +), +( + name='TestDB_ndf', + filename='D:\TestDB_ndf.ndf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='TestDB_log', + filename='D:\TestDB_log.ldf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +go +use TestDB +go +create table typeInfo +( + typeId int primary key identity(1,1), + typeName varchar(10) not null, +) +create table loginInfo +( LoginId int primary key identity(1,1), + LoginName nvarchar(10) unique not null, + LoginPwd nvarchar(10) default(123456) not null, + LoginSex nvarchar(1), + LoginBirth date, + LoginMember nvarchar(20), +) +create database company +on +( + name='company', + filename='D:\company.mdf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +), +( + name='company_ndf', + filename='D:\company_ndf.ndf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='company_log', + filename='D:\company_log.ldf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +go +use company +go +create table sectionInfo +( + sectionID int identity primary key, + sectionName varchar(10) not null, +) +create table userInfo +( + userNo int identity primary key not null, + userName varchar(10) unique check (userName>4) not null, + userSex varchar(2) check(userSex='男' or userSex='女') not null, + userAge int check(userAge>=1 and userAge<=100) not null, + userAdress varchar(50) default('湖北'), + userSection int foreign key references sectionInfo(sectionID) +) +create table workInfo +( + workID int identity primary key not null, + userID int foreign key references userInfo(userNo) not null, + workTime datetime not null, + workDescription varchar(40) check(workDescription in('迟到','早退','旷工','病假','事假')) not null, + +) +create database studentManSys +on +( + name='studentManSys', + filename='D:\studentManSys.mdf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +), +( + name='studentManSys_ndf', + filename='D:\studentManSys_ndf.ndf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='studentManSys_log', + filename='D:\studentManSys_log.ldf', + size=8mb, + maxsize=100mb, + filegrowth=10mb +) +go +use studentManSys +go +create table classInfo +( + classId int identity primary key, + className varchar(20) unique not null, + classStart date not null, + classMs text +) +create table studentInfo +( + stuID int identity primary key, + stuName varchar(8) check(stuName>2) unique, + stuSex nvarchar(1) default('男') check (stuSex='男' or stuSex='女') not null, + stuAge int check(stuAge>=15 and stuAge<=40) not null, + stuAdress nvarchar(200) default('湖北武汉'), + bjbh int references classinfo(classid) + ) + create table courseInfo + ( + courseID int identity primary key, + courseName varchar(20) unique not null, + kcms nvarchar(200) + ) + create table markInfo + ( + markID int identity primary key, + stuID int not null, + courseID int not null, + mark int check(mark>=0 and mark<=100) + ) + create table tblUser +( + userId int identity primary key, + userName nvarchar(8) not null, + userTel char(11) not null +) +create table tblHouseType +( + typeId int identity primary key, + typName nvarchar(8) not null +) +create table tblQx +( + qxId int identity primary key, + qxName nvarchar(8) not null +) +create table tblHouseInfo +( + id int identity primary key, + descc nvarchar(8) not null, + userld int, + zj int, + shi int, + ting int, + typeld int, + qxld int +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery6.2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery6.2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e4625daef222aee4a2d2449005d5579a354e31cf --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery6.2.sql" @@ -0,0 +1,83 @@ +use master +go +create database bbs +on( + name='bbs', + filename='D:\bbs.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +log on( + name='bbs_log', + filename='D:\bbs_log.ldf', + size=5mb, + maxsize=100mb, + 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 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 , +) +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) +go +use bbs +go + + +select * from bbsSection +select * from bbsReply +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,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='小雨点' +delete from bbsTopic where tUID=2 +delete from bbsReply where rUID=2 +delete from bbsReply where rID=1 +delete from bbsUsers where uName='逍遥' +delete from bbsTopic where tSID=3 +delete from bbsSection where sName='生活百科' +delete from bbsReply diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0c1c6437bb895896453bac0f109e5a080e1901cc --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" @@ -0,0 +1,167 @@ +use master +go + +create database TestDB +on primary +( + name = TestDB, + filename = 'D:\SQL\TestDB\TestDB.mdf',--没有E盘,就丢这了 + size = 5MB, + Maxsize = 50MB, + filegrowth = 1MB +) +log on +( + name = TestDB_log, + filename = 'D:\SQL\TestDB\TestDB_log.ldf',--没有E盘,就丢这了 + size = 1MB, + Maxsize = 10MB, + filegrowth = 10% +) +go + +use TestDB +create table TypeInfo +( + typeID int primary key identity(1,1), + typeName varchar(10) not null +) + +create table loginInfo +( + loginID int primary key identity(1,1), + LoginName nvarchar(10) not null unique(LoginName), + LoginPwd nvarchar(20) not null default('123456'), + LoginSex char(1) check(LoginSex = 0 and LoginSex = 1), + LoginBirthday date, + LoginType int constraint FK_TypeInfo_typeID references TypeInfo(typeID) +) +go + +use master +go +create database company +on +( + name = company, + filename = 'D:\SQL\company\company.mdf',--没有E盘! + size = 5MB, + maxsize = 50MB, + filegrowth = 1MB +) + +log on +( + name = company_log, + filename = 'D:\SQL\company\company_log.ldf',--没有E盘! + size = 1MB, + maxsize = 10MB, + filegrowth = 10% +) +go + +use company +go + +create table sectionInfo +( + sectionID int identity(1,1) primary key, + sectionName varchar(10) not null +) + +create table userInfo +( + userNo int identity(1,1) primary key not null, + userName varchar(10) unique(userName) check(len(userName)>4) not null, + userSex varchar(2) check(userSex = '男' or userSex = '女') not null, + userAge int check(userAge between 1 and 100) not null, + userAddress varchar(50) default('湖北'), + userSection int constraint FK_sectionInfo_sectionID references sectionInfo(sectionID) +) + +create table workInfo +( + workID int identity(1,1) primary key not null, + userID int constraint FK_userInfo_userNo references userInfo(userNo), + workTime datetime not null, + workDescription varchar(40) check(workDescription = '迟到' or workDescription = '早退' or workDescription = '旷工' or workDescription = '病假' or workDescription = '事假') +) +go + +use master +go +create database Student + +use Student +go + +create table classInfo +( + classID int identity(1,1) primary key, + className char(10) unique(className) not null, + startTime datetime not null, + classRemark ntext +) + +create table StuInfo +( + stuID int primary key identity(1,1), + stuName char(10) check(len(stuName) > 2) unique(stuName), + stuSex nchar(1) default('男') check(stuSex = '男' or stuSex = '女') not null, + stuAge int check(stuAge between 15 and 40) not null, + stuAddress nchar(20) default('湖北武汉'), + classID int constraint FK_classInfo_classID references classInfo(classID) +) + +create table lessonInfo +( + lessonID int primary key identity(1,1), + lessonName nchar(20) unique(lessonName) not null, + lessonRemark ntext +) + +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int constraint FK_stuInfo_stuID references stuInfo(stuID), + lessonID int constraint FK_lessonInfo_lessonID references lessonInfo(lessonID), + score int check(score between 0 and 100) +) +go + +use master +go +create database house + +use house +create table tblUser +( + tblID int primary key identity(1,1), + userName nchar(5), + userTel int +) + +create table tblHouseType +( + typeID int primary key identity(1,1), + typeName nchar(20) +) + +create table tblQx +( + qxID int primary key identity(1,1), + qxName nchar(20) +) + +create table tblHouseInfo +( + id int primary key identity(1,1), + userID int constraint FK_tblUser_tblID references tblUser(tblID), + zj char(30), --这啥 + shi char(40), --这又啥 + ting char(30), --emm + typeID int constraint FK_tblHouseType_typeID references tblHouseType(typeID), + qxid int constraint FK_tblQx_qxID references tblQx(qxID) +) +go + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a77ddf49f9dd8c7013e7531bbf1e28ca70398c5a --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery2.sql" @@ -0,0 +1,122 @@ +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,tTitle,tMSG,tTime,tCount) values (2,'范跑跑','谁是范跑跑','2008-07-08',1), + (3,'.NET','与JAVA的区别是什么啊','2008-09-01',2), + (2,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','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) + goto delSections +delSections: + alter table bbsTopic drop constraint FK_bbsSection_sID + delete from bbsSection where sID = 3 + goto cleReply +cleReply: + delete from bbsReply + goto sel +sel: + select * from bbsPoint + select * from bbsReply + select * from bbsSection + select * from bbsTopic + select * from bbsUsers + go diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/\347\273\203\344\271\2401.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/\347\273\203\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..355951de1dc208660a69bbfde361c9320f080755 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/\347\273\203\344\271\2401.sql" @@ -0,0 +1,297 @@ +--用SQL实现以下的题目: + +--1. 先创建一个数据库,数据库名为TestDB,要求有两个数据文件,一个日志文件,注意命名规范,文件存放在E盘下 +-- 再在上面的数据库下创建表,结构如下: +use master +go + + +create database TestDB + +on( + name='TestDB.mdf', + filename='D:\tex.\TestDB.mdf', + size=8mb, + maxsize=80mb, + filegrowth=10% +) + +log on( + name='TesDB_log', + filename='D:\tex.\TesDB_log.ldf', + size=8mb, + maxsize=80mb, + filegrowth=10% +) +go +use TestDB +go + +create table typeInfo +( + typeId int primary key identity(1,1), + typeName varchar(10) not null, + +) +go + +create table loginInfo +( + LoginId int primary key identity(1,1), + LoginName varchar(10) not null unique , + LoginPwd varchar(20) not null default('123456'), + typeSex nvarchar(1) default('男') check(typeSex='男' or typeSex='女' ), + typebirthday datetime not null, + typestle nvarchar(6) not null + +) + + +-- 会员类别表(typeInfo): +-- 类别编号(typeId):主键、自动编号 +-- 类别名(typeName): varchar(10) 不能为空 + +-- 登录用户表(loginInfo): +-- 编号(LoginId),数据类型(int),主键、自动编号 +-- 账户(LoginName),文本,长度为10,非空,必须唯一,不能重复 +-- 密码(LoginPwd),文本,长度为20,非空、默认值为‘123456’ +-- 性别(自定类型) +-- 生日(自定类型) +-- 会员类别(自定类型) + + +--2. 先创建一个数据库用来存放某公司的员工信息,数据库的名称为company,包含2个数据文件1个日志 +--文件,数据文件和日志文件全部存放在E盘中,初始大小,增长和最大大小自己设定 +-- 再创建表: +create database company +on( + name='company.mdf', + filename='D:\tex.tex\company.mdf', + size=8mb, + maxsize=80mb, + filegrowth=10% + +) +log on +( + name='company_log', + filename='D:\tex.tex\company_log.ldf', + size=8mb, + maxsize=80mb, + filegrowth=10% +) +go + +use company +go + +create table sectionInfo +( + sectionID int primary key , + sectionName varchar(10) not null, +) +go + +create table userInfo +( + userNo int primary key not null, + userName varchar(10) unique not null check (userName>=4), + userSex varchar(2) not null default('男') check(userSex='男' or userSex='女' ), + userAge int not null check(userAge>=1 and userAge<=100), + userAddress varchar(50) default('湖北'), + userSection int references sectionInfo(sectionID) + +) + + + + +-- 部门信息表(sectionInfo) +-- 部门编号 sectionID int 标识列 主键 +-- 部门名称 sectionName varchar(10) 不能为空 check + + +-- 员工信息表(userInfo) +-- 员工编号 userNo int 标识列 主键 不允许为空 +-- 员工姓名 userName varchar(10) 唯一约束 不允许为空 长度必须大于4 +-- 员工性别 userSex varchar(2) 不允许为空 只能是男或女 +-- 员工年龄 userAge int 不能为空 范围在1-100之间 +-- 员工地址 userAddress varchar(50) 默认值为“湖北” +-- 员工部门 userSection int 外键,引用部门信息表的部门编号 + + + + + +-- 员工考勤表(workInfo) +-- 考勤编号 workId int 标识列 主键 不能为空 +-- 考勤员工 userId int 外键 引用员工信息表的员工编号 不能为空 +-- 考勤时间 workTime datetime 不能为空 +-- 考勤说明 workDescription varchar(40) 不能为空 内容只能是“迟到”,“早退”,“旷工”,“病假”,“事假”中的一种 +create table workInfo +( + workId int primary key not null, + userId int references userInfo(userNo) not null, + workTime datetime not null, + workDescription varchar(40) not null check(workDescription='迟到'or workDescription='早退' or workDescription='旷工' or workDescription='病假',or workDescription='事假'), + +) + + + + +--3. 为学校开发一个学生管理系统,请为该系统创建数据库,主要存放的信息有:班级信息、学生信息、课程信息、学生考试成绩 +create database StudentOrder +on( + name='StudentOrder.mdf', + filename='D:\tex.\StudentOrder.mdf', + size=6mb, + maxsize=60mb, + filegrowth=10% +) +log on( + + name='StudentOrder_log', + filename='D:\tex.tex\StudentOrder_log.ldf', + size=6mb, + maxsize=60mb, + filegrowth=10% + +) +go +use StudentOrder +go + +create table Classinfo +( + classid int primary key identity(1,1), + ClassName nvarchar(5) not null unique, + ClassTime datetime not null, + Classdescribe text + +) + +-- 班级信息:班级编号 classid (主键、标识列) +-- 班级名称(例如:T1、T2、D09等等):不能为空,不能重复 +-- 开办时间:不能为空 +-- 班级描述 + +-- 学生信息:学号:主键、标识列 +-- 姓名:长度大于2,不能重复 +-- 性别:只能是‘男’或‘女’,默认为男,不能为空 +-- 年龄:在15-40之间,不能为空 +-- 家庭地址:默认为“湖北武汉” +-- 所在的班级编号 + +create table Studentinfo + +( + StudentId int primary key identity(1,1), + StudentName varchar(10) unique check (StudentName>=2), + StudentSex varchar(2) default('男') check(StudentSex='男' or StudentSex='女') not null, + StudentAge varchar(100) check(StudentAge>=15 and StudentAge<=40) not null, + StudentAddress text default('湖北武汉'), + ClassId varchar(5) default('软件2班') not null, +) + +-- 课程信息:编号:主键、标识列 +-- 课程名:不能为空,不能重复 +-- 课程描述: +create table coursemessage + + ( + courseid int primary key identity(1,1), + coursename nvarchar(5)not null, + coursedescribe text + ) + +-- 成绩信息:成绩编号:主键、标识列 +-- 成绩所属于的学生编号,不能为空 +-- 成绩所属的课程编号,不能为空 +-- 成绩:在0-100之间 + +create table scoreinfo +( + scoreid int primary key identity(1,1), + scorestuid int references Studentinfo(StudentId) not null, + scorestu int references coursemessage(courseid) not null, + score int check(score>=0 or score<=100) +) + +--4. 为一个房屋出租系统创建一个数据库,数据库中主要存放房屋的信息,包括:房屋的编号,房屋的描述,发布人的信息(姓名和联系电话),房屋的租金,房屋的室数,房屋的厅数,房屋的类型(别墅、普通住宅、平房、地下室),房屋所属的区县(武昌、汉阳、汉口),请根据上面的描述设计表,并设计表的关系,以及列的约束 + +create database fangwu +on( + name='fangwu', + filename='D:\tex.\fangwu.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='fangwu_log', + filename='D:\fangwu_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +--tblUser --发布人信息表 +--userId +--userName +--userTel +use fangwu +go + +create table tblUser +( + userId int primary key identity(1,1), + userName varchar, + userTel nvarchar(10) +) + +--tblHouseType --房屋的类型 +--typeId +--typName +create table tblHouseType +( + typeId int primary key identity(1,1), + typName varchar check(typName='别墅'or typName='普通住宅'or typName='平房' or typName='地下室') not null +) +--tblQx --区县 +--qxId +--qxName +create table tblQx +( + qxId int primary key identity(1,1), + qxName varchar check(qxName='百色'or qxName='贵港' or qxName='桂林') not null +) + +--tblHouseInfo--房屋信息表 +--id +--desc +--userId -- +--zj +--shi +--ting +--typeId -- +--qxId -- +create table tblHouseInfo +( + id int primary key identity(1,1), + userId int foreign key references tblUser (userId), + zj money, + shi varchar, + ting int, + typeId int foreign key references tblHouseType (typeId), + qxId int foreign key references tblQx (qxId) +) + + + + + + + + + \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/\347\273\203\344\271\2402.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/\347\273\203\344\271\2402.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8464d0992427f39701a368cfddadedaee3bebb7e --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/\347\273\203\344\271\2402.sql" @@ -0,0 +1,190 @@ +----练习2 +--一、先创建数据库和表以及约束 + +-- 1.创建一个数据库用来存放某论坛的用户和发帖信息,数据库的名称为bbs,包含1个数据文件1个日志 +-- 文件,数据文件和日志文件全部存放在D盘中,初始大小,增长和最大大小自己设定 +use master +go + +create database bbs +on( + + name='bbs.mdf', + filename='D:\sql\bbs.mdf', + size=8mb, + maxsize=80mb, + filegrowth=10% +) +log on( + name='bbs_log', + filename='D:\sql\bbs_log.ldf', + size=8mb, + maxsize=80mb, + filegrowth=10% + +) +go + +use bbs +go +alter table bbsUsers add constraint FK unique (uName) +alter table bbsSection add constraint NK foreign key (sUid) references bbsUsers( UID) +-- 2.创建表 + +-- 注意以下4张表的创建顺序,要求在创建bbsUser和bbsSection表时不添加约束,创建完后单独添加约束,其它的表创建时添加约束 + +-- 用户信息表(bbsUsers) +-- 用户编号 UID int 主键 标识列 +-- 用户名 uName varchar(10) 唯一约束 不能为空 +-- 性别 uSex varchar(2) 不能为空 只能是男或女 +-- 年龄 uAge int 不能为空 范围15-60 +-- 积分 uPoint int 不能为空 范围 >= 0 +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), +) + +-- 1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +-- 小雨点 女 20 0 +-- 逍遥 男 18 4 +-- 七年级生 男 19 2 +insert into bbsUsers(uName,uSex,uAge,uPoint) +values('小雨点','女','20',0), +('逍遥','男','18',4), +('七年级','男','19',2) + + +create table bbsSection +( + sID int primary key identity(1,1), + sName varchar(10) not null, + sUid int foreign key references bbsUsers(UID) +) + + + +--+ 主贴表(bbsTopic) +-- 主贴编号 tID int 主键 标识列, +-- 发帖人编号 tUID int 外键 引用用户信息表的用户编号 +-- 版块编号 tSID int 外键 引用版块表的版块编号 (标明该贴子属于哪个版块) +-- 贴子的标题 tTitle varchar(100) 不能为空 +-- 帖子的内容 tMsg text 不能为空 +-- 发帖时间 tTime datetime +-- 回复数量 tCount int +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 + +) + + +--+ 回帖表(bbsReply) +-- 回贴编号 rID int 主键 标识列, +-- 回帖人编号 rUID int 外键 引用用户信息表的用户编号 +-- 对应主贴编号 rTID int 外键 引用主贴表的主贴编号 (标明该贴子属于哪个主贴) +-- 回帖的内容 rMsg text 不能为空 +-- 回帖时间 rTime datetime +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 + +) + + +-- 版块表(bbsSection) +-- 版块编号 sID int 标识列 主键 +-- 版块名称 sName varchar(10) 不能为空 +-- 版主编号 sUid int 外键 引用用户信息表的用户编号 + + + +--二、在上面的数据库、表的基础上完成下列题目: + +-- 1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下: +-- 1, 小雨点 女 20 0 +-- 逍遥 男 18 4 +-- 七年级生 男 19 2 + +-- 2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,提示查询部分列:select 列名1,列名2 from 表名 +-- --插入多行: +-- 先创建好空表,然后再插入数据, +-- 直接插入数据,然后自动生成表。 +-- insert into bbsPoint select uName,uPoint from bbsUsers +-- select uName,uPoint into bbsPoint from bbsUsers + +select uName,uPoint into bbsPoint from bbsUsers + +insert into bbsSection(sName,sUid) +select'技术交流',1 union +select'读书世界',2 union +select'生活百科',3 union +select'八卦区',4 +select * from bbsSection + +-- 3.给论坛开设4个板块 +-- 名称 版主名 +-- 技术交流 小雨点 +-- 读书世界 七年级生 +-- 生活百科 小雨点 +-- 八卦区 七年级生 + +-- 4.向主贴和回帖表中添加几条记录 + +-- 主贴: + +-- 发帖人 板块名 帖子标题 帖子内容 发帖时间 回复数量 +-- 逍遥 八卦区 范跑跑 谁是范跑跑 2008-7-8 1 +-- 七年级生 技术交流 .NET 与JAVA的区别是什么呀? 2008-9-1 2 +-- 小雨点 生活百科 今年夏天最流行什么 有谁知道今年夏天最流行 2008-9-10 0 +-- 什么呀? +insert into bbsTopic( tid,tUID,tSID,tTitle,tMsg,tTime,tCount ) +select 1,3, 4,'范跑跑','谁是范跑跑',' 2008-7-8 ',1 union +select 2,2, 1,' .NET ','与JAVA的区别是什么呀?','2008-9-1',2 union +select 3,1, 3,' 今年夏天最流行是什么呀','有谁知道今年夏天最流行','2008-9-10',0 +select * from bbsTopic + +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 + +-- 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) + +select * from bbsUsers +delete bbsUsers where uName ='逍遥' + + +update bbsUsers set uPoint ='10' where uName='小雨点' + +select * from bbsSection +delete bbsSection where sName='生活百科' + +select * from bbsReply +delete bbsReply +-- 6.因为小雨点发帖较多,将其积分增加10分 + +-- 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) + +-- 8.因回帖积累太多,现需要将所有的回帖删除 + +--drop table bbsReply +--delete from bbsReply +--truncate table bbsReply + + + + + + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/.keep" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2401.1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2401.1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9c3557088af6c8f074da394ddee309cee858949a --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2401.1.sql" @@ -0,0 +1,44 @@ +use master +go + +--1. 先创建一个数据库,数据库名为TestDB,要求有两个数据文件,一个日志文件,注意命名规范,文件存放在E盘下 +-- 再在上面的数据库下创建表,结构如下: +create database TestDB +on +( + name='TestDB', + filename='D:\test\TestDB.mdf' +) +log on +( + name='TestDB_log', + filename='D:\test\TestDB_log.ldf' +) +go + +use TestDB +go +-- 会员类别表(typeInfo): +create table TypeInfo +( +-- 类别编号(typeId):主键、自动编号 + TypeID int primary key(TypeID) identity(1,1), +-- 类别名(typeName): varchar(10) 不能为空 + TypeName varchar(10) not null +) +-- 登录用户表(loginInfo): +create table LoginInfo +( +-- 编号(LoginId),数据类型(int),主键、自动编号 + LoginID int primary key(LoginID) identity(1,1), +-- 账户(LoginName),文本,长度为10,非空,必须唯一,不能重复 + LoginName nvarchar(10) unique not null, +-- 密码(LoginPwd),文本,长度为20,非空、默认值为‘123456’ + LoginPwd nvarchar(20) not null default('123456'), +-- 性别(自定类型) + LoginSex varchar(1) default('男') check(LoginSex in('男','女')), +-- 生日(自定类型) + Birthday date, +-- 会员类别(自定类型) + VIPType nvarchar(10) +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2401.2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2401.2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f19e7ec091f5f3cea0d6c41fdb3b149e184c0fdd --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2401.2.sql" @@ -0,0 +1,56 @@ +--2. 先创建一个数据库用来存放某公司的员工信息,数据库的名称为company,包含2个数据文件1个日志 +--文件,数据文件和日志文件全部存放在E盘中,初始大小,增长和最大大小自己设定 +-- 再创建表: +create database Company +on +( + name='Company', + filename='D:\test\Company.mdf' +) +log on +( + name='Company_log', + filename='D:\test\Company_log.ldf' +) +go + +use Company +go + +create table sectionInfo +( +-- 部门信息表(sectionInfo)-- 部门编号 sectionID int 标识列 主键 + sectionID int primary key(sectionID) identity(1,1), +-- 部门名称 sectionName varchar(10) 不能为空 + sectionName varchar(10) not null +) + +-- 员工信息表(userInfo) +create table userInfo +( +-- 员工编号 userNo int 标识列 主键 不允许为空 + userNo int primary key (userNo) not null, +-- 员工姓名 userName varchar(10) 唯一约束 不允许为空 长度必须大于4 + userName varchar(10) unique not null check(len(userName)>4), +-- 员工性别 userSex varchar(2) 不允许为空 只能是男或女 + userSex varchar(2) not null check(userSex in('男','女')), +-- 员工年龄 userAge int 不能为空 范围在1-100之间 + userAge int not null check(userAge>=1 and userAge<=100), +-- 员工地址 userAddress varchar(50) 默认值为“湖北”' + userAddress varchar(50) default('湖北'), +-- 员工部门 userSection int 外键,引用部门信息表的部门编号 + userSection int foreign key(userSection) references sectionInfo(sectionID) +) + +-- 员工考勤表(workInfo) +create table workInfo +( +-- 考勤编号 workId int 标识列 主键 不能为空 + workId int identity(1,1) primary key(workId) not null, +-- 考勤员工 userId int 外键 引用员工信息表的员工编号 不能为空 + userId int foreign key(userId) references userInfo(userNo), +-- 考勤时间 workTime datetime 不能为空 + workTime datetime not null, +-- 考勤说明 workDescription varchar(40) 不能为空 内容只能是“迟到”,“早退”,“旷工”,“病假”,“事假”中的一种 + workDescription varchar(40) not null check(workDescription in('迟到','早退','旷工','病假','事假')) +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2401.3.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2401.3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9209f11a547489509232d219b42b90ba1aa3b55d --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2401.3.sql" @@ -0,0 +1,62 @@ +--3. 为学校开发一个学生管理系统,请为该系统创建数据库,主要存放的信息有:班级信息、学生信息、课程信息、学生考试成绩 +create database StuInfo +on +( + name='StuInfo', + filename='D:\test\StuInfo.mdf' +) +log on +( + name='StuInfo_log', + filename='D:\test\StuInfo_log.ldf' +) +-- 班级信息: +create table class1 +( +-- 班级编号 classid (主键、标识列) + classid int primary key(classid) identity(1,1), +-- 班级名称(例如:T1、T2、D09等等):不能为空,不能重复 + classname varchar(10) not null unique, +-- 开办时间:不能为空 + OpeningTime date not null, +-- 班级描述 + classtext text +) +-- 学生信息: +create table StuInfo1 +( +-- 学号:主键、标识列 + stuID int primary key(stuID) identity(1,1), +-- 姓名:长度大于2,不能重复 + stuName varchar(20) unique check(len(stuName)>4), +-- 性别:只能是‘男’或‘女’,默认为男,不能为空 + stuSex char(2) default('男') check(stuSex in('男','女')), +-- 年龄:在15-40之间,不能为空 + stuAge int check(stuAge>=15 and stuAge<=40), +-- 家庭地址:默认为“湖北武汉” + sruAddress varchar(20) default('湖北武汉'), +-- 所在的班级编号 + classid int foreign key(classid) references Class1(classid) +) + +-- 课程信息:编号:主键、标识列 +create table course1 +( +-- 课程名:不能为空,不能重复 + coursename varchar(10) unique not null, +-- 课程描述: + coursetext text +) + +-- 成绩信息: +create table StuExam1 +( +-- 成绩编号:主键、标识列 + ExamID int primary key(ExamID), +-- 成绩所属于的学生编号,不能为空 + stuID int not null foreign key(stuID) references StuInfo1(stuID), +-- 成绩所属的课程编号,不能为空 + classid int not null foreign key(classid) references Class1(classid), +-- 成绩:在0-100之间 + Exam int check(Exam>=0 and Exam<=100) +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2401.4.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2401.4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..de0a0336c0e53bcac2c3d66a497097bef86091a7 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2401.4.sql" @@ -0,0 +1,49 @@ +--4. 为一个房屋出租系统创建一个数据库,数据库中主要存放房屋的信息, +--包括:房屋的编号,房屋的描述,发布人的信息(姓名和联系电话),房屋的租金, +--房屋的室数,房屋的厅数,房屋的类型(别墅、普通住宅、平房、地下室),房屋所属的区县(武昌、汉阳、汉口), +--请根据上面的描述设计表,并设计表的关系,以及列的约束 +create database HouseRental +on +( + name='HouseRental', + filename='D:\test\HouseRental.mdf' +) +log on +( + name='HouseRental_log', + filename='D:\test\HouseRental_log.ldf' +) +--发布人信息表 +use HouseRental +go +create table tblUser +( + userId int primary key(userId) identity(1,1), + userName varchar(10) not null, + userTel char(11) not null +) +--房屋的类型 +create table tblHouseType +( + typeId int primary key(typeId) identity(1,1), + typName varchar(10) not null, +) + +--区县 +create table tblQx +( + qxId int primary key(qxId) identity(1,1), + qxName varchar(10) not null, +) +--房屋信息表 +create table tblHouseInfo +( + tblHouseid int primary key(tblHouseid) identity(1,1), + describe text, + userId int foreign key(userId) references tblUser(userId), + rent int, + shishu int, + tingshu int, + typeId int foreign key(typeId) references tblHouseType(typeId), + qxId int foreign key(qxId) references tblQx(qxId) +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\273\203\344\271\2402.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\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..96068dd3253d9be84d39d43136c69dc63a6a5287 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\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,134 @@ +--一、先创建数据库和表以及约束 + +-- 1.创建一个数据库用来存放某论坛的用户和发帖信息,数据库的名称为bbs,包含1个数据文件1个日志 +-- 文件,数据文件和日志文件全部存放在E盘中,初始大小,增长和最大大小自己设定 +create database bbs +on +( + name='bbs', + filename='D:\test\bbs.mdf' +) +log on +( + name='bbs_log', + filename='D:\test\bbs_log.ldf' +) +-- 2.创建表 +-- 注意以下4张表的创建顺序,要求在创建bbsUser和bbsSection表时不添加约束,创建完后单独添加约束,其它的表创建时添加约束 +-- 用户信息表(bbsUsers) +use bbs +go +create table bbsUsers +( +-- 用户编号 UID int 主键 标识列 + UID int identity(1,1), +-- 用户名 uName varchar(10) 唯一约束 不能为空 + uName varchar(10) not null, +-- 性别 uSex varchar(2) 不能为空 只能是男或女 + uSex varchar(2) not null, +-- 年龄 uAge int 不能为空 范围15-60 + uAge int not null, +-- 积分 uPoint int 不能为空 范围 >= 0 + 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 in('男','女')) +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) + +-- 版块表(bbsSection) +create table bbsSection +( +-- 版块编号 sID int 标识列 主键 + sID int identity(1,1), +-- 版块名称 sName varchar(10) 不能为空 + sName varchar(10) not null, +-- 版主编号 sUid int 外键 引用用户信息表的用户编号 + 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) + +--+ 主贴表(bbsTopic) +create table bbsTopic +( +-- 主贴编号 tID int 主键 标识列, + tID int primary key(tID) identity(1,1), +-- 发帖人编号 tUID int 外键 引用用户信息表的用户编号 + tUID int, +-- 版块编号 tSID int 外键 引用版块表的版块编号 (标明该贴子属于哪个版块) + tSID int, +-- 贴子的标题 tTitle varchar(100) 不能为空 + tTitle varchar(100) not null, +-- 帖子的内容 tMsg text 不能为空 + tMsg text not null, +-- 发帖时间 tTime datetime + tTime datetime, +-- 回复数量 tCount int + tCount int +) +alter table bbsTopic add constraint FK_bbsTopic_tUID foreign key(tUID) references bbsUsers(UID) +alter table bbsTopic add constraint FK_bbsTopic_tSID foreign key(tSID) references bbsSection(sID) + +--+ 回帖表(bbsReply) +create table bbsReply +( +-- 回贴编号 rID int 主键 标识列, + rID int primary key(rID) identity(1,1), +-- 回帖人编号 rUID int 外键 引用用户信息表的用户编号 + rUID int, +-- 对应主贴编号 rTID int 外键 引用主贴表的主贴编号 (标明该贴子属于哪个主贴) + rTID int, +-- 回帖的内容 rMsg text 不能为空 + rMsg text not null, +-- 回帖时间 rTime datetime + rTime datetime, +) +alter table bbsReply add constraint FK_bbsReply_rUID foreign key(rUID) references bbsUsers(UID) +alter table bbsReply add constraint FK_bbsReply_rTID foreign key(rTID) references bbsSection(sID) + +--二、在上面的数据库、表的基础上完成下列题目: +use bbs +go + +-- 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个板块 +-- 名称 版主名 +-- 技术交流 小雨点 +-- 读书世界 七年级生 +-- 生活百科 小雨点 +-- 八卦区 七年级生 +insert into bbsSection values ('技术交流',1),('读书世界',3),('生活百科',1),('八卦区',3) + +-- 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,4,'范跑跑','谁是范跑跑','2008-7-8',1),(3,1,'.NET','与JAVA的区别是什么呀?','2008-9-1',2),(1,3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10',0) +-- 回帖: +-- 分别给上面三个主贴添加对应的回帖,回帖的内容,时间,回帖人自定 +insert into bbsReply values(1,1,'不知道','2008-7-9'),(1,2,'不知道','2008-9-2'),(1,2,'问百度','2008-9-2') +-- 5.因为会员“逍遥”发表了非法帖子,现将其从论坛中除掉,即删除该用户,请用语句实现(注意主外键,要删除主键,先要将引用了该主键的外键数据行删除) +select * from bbsUsers --查看逍遥的用户ID +delete from bbsUsers where UID=2 +-- 6.因为小雨点发帖较多,将其积分增加10分 +update bbsUsers set uPoint=10 where uName='小雨点' +-- 7.因为板块“生活百科”灌水的人太少,现决定取消该板块,即删除(注意主外键) +alter table bbsTopic drop constraint FK_bbsTopic_tSID +delete from bbsSection where sName='生活百科' +-- 8.因回帖积累太多,现需要将所有的回帖删除 +delete from bbsReply diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b678d4fcf40b8afc5ab6f535333b21c540b0eeeb --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\217\266\345\270\205/SQLQuery1.sql" @@ -0,0 +1,194 @@ +create database TestDB +on +( + name='TestDB', + filename='D:\test\TestDB.mdf', + size=5mb, + maxsize=100mb, + filegrowth=2mb +) + +log on +( + name='TestDB_log', + filename='D:\test\TestDB_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=2mb +) +use TestDB +go + +-- 1、 +create table typeInfo +( + typeId int primary key identity(1,1), + typeName varchar(10) not null, +) + +create table loginInfo +( + LoginId int primary key identity(1,1), + LoginName varchar(10) not null unique, + LoginPwd varchar(20) not null default('1,2,3,4,5,6'), + Logsex char(1), + Logbrithday datetime, + Loghuiyuan varchar +) + +-- 2、 +create database company +on +( + name='company', + filename='D:\test\company.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10mb +) + +log on +( + name='company_log', + filename='D:\test\company_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=5mb +) +use company +go + +create table sectionInfo +( + sectionID int primary key, + sectionName varchar(10) not null +) +create table userInfo +( + userNo int primary key not null, + userName varchar(10) unique not null check(userName>4), + userSex varchar(2) not null check(userSex='男' or userSex='女' ), + userAge int not null check(userAge>=1 or userAge<=100), + userAddress varchar(50) default('湖北'), + userSection int foreign key references sectionInfo(sectionID) +) +create table workInfo +( + workId int primary key not null, + userId int foreign key references userInfo(userNo), + workTime datetime not null, + workDescription varchar(40) not null check(workDescription='迟到'or workDescription='早退'or workDescription='矿工' or workDescription='病假'or workDescription='事假') +) + +-- 3、 +create database School +on +( + name='School', + filename='D:\test\School.mdf', + size=5mb, + maxsize=50mb, + filegrowth=5mb +) +log on +( + name='School_log', + filename='D:\test\School_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=5mb +) +use School +go +create table Classinfo +( + classid int primary key, + classname varchar(10) not null unique, + classremark ntext +) +create table studentinfo +( + stunum int primary key, + stuname varchar check (len(stuname)>2) unique, + stusex varchar(2) default('男') check(stusex='男' or stusex='女'), + stuage int check(stuage>=15 or stuage<=40) not null, + stuaddress nvarchar(50) default('湖北武汉'), + stuclassid int references Classinfo(classid) +) +create table information +( + number int primary key, + kechenname varchar unique not null, + miaoshu ntext +) +create table score +( + scorenum int primary key, + scorestuid int references studentinfo(stunum) not null, + scoreid int references information(number) not null, + score int check(score>=0 or score<=100) +) +--4、 +create database house +on +( + name='fangwu', + filename='D:\test\house.mdf', + size=5mb, + maxsize=50mb, + filegrowth=5mb +) +log on +( + name='fangwu_log', + filename='D:\test\house_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=5mb +) +use house +go + +create table tblUser +( + userId int primary key identity(1,1), + userName varchar, + userTel nvarchar(10) +) +create table tblHouseType +( + typeId int primary key identity(1,1), + typName varchar check(typName='别墅'or typName='普通住宅'or typName='平房' or typName='地下室') not null +) +create table tblQx +( + qxId int primary key identity(1,1), + qxName varchar check(qxName='武昌'or qxName='汉阳' or qxName='汉口') not null +) + +create table tblHouseInfo +( + id int primary key identity(1,1), + userId int foreign key references tblUser (userId), + zj money, + shi varchar, + ting int, + typeId int foreign key references tblHouseType (typeId), + qxId int foreign key references tblQx (qxId) +) +CREATE database bbs +on +( + name='bbs', + filename='D:\test\bbs.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10mb +) +log on +( name='bbs_log', + filename='D:\test\bbs_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10mb + ) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\273\203\344\271\2401/\347\273\203\344\271\2401.1.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\273\203\344\271\2401/\347\273\203\344\271\2401.1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..56407a2ade8b30524f7130b3faf9bf8c018aa9c5 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\273\203\344\271\2401/\347\273\203\344\271\2401.1.sql" @@ -0,0 +1,36 @@ +use master +go +create database TestDB +on +( + name='TestDB', + filename='D:\test\TestDB.mdf', + size=20MB, + maxsize=100MB, + filegrowth=10% +) +log on +( + name='TestDB_log', + filename='D:\test\TestDB_log.ldf', + size=20MB, + maxsize=100MB, + filegrowth=10% +) +go +use TestDB +go +create table typeInfo +( +typeld int primary key identity (1 ,1), + +typeName varchar(10) not null, +) +create table loginInfo +( +LoginId int primary key identity (1 ,1), +LoginName varchar(10) unique not null, +LoginPwd varchar(20) default ('123456'), +Sex nvarchar(1) default('男')check(Sex='男'or Sex='女'), +birthday date, +) diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\273\203\344\271\2401/\347\273\203\344\271\2401.2.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\273\203\344\271\2401/\347\273\203\344\271\2401.2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a3cdb5768a2f4cd3f93b247edb8ffda9e9237071 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\273\203\344\271\2401/\347\273\203\344\271\2401.2.sql" @@ -0,0 +1,44 @@ +use master +go +create database company +on +( + name='company', + filename='E:\test\company.mdf', + size=20MB, + maxsize=100MB, + filegrowth=10% +) +log on +( + name='company_log', + filename='E:\test\company_log.ldf', + size=20MB, + maxsize=100MB, + filegrowth=10% +) +go +use company +go +create table sectionInfo +( + sectionID int primary key , + +sectionName varchar(10) unique not null, +) +create table useInfo +( +useNo int primary key identity (1 ,1) not null, +useName varchar(10) unique not null check (Len (useName)>4), +useSex nvarchar(2)check(useSex='男'or useSex='女'), +useAge int check(useAge >= 1 and useAge <= 100 ), +useAddress varchar(50) default ('湖北'), +userSection int foreign key references sectionInfo(sectionID), +) +create table workInfo +( +workId int primary key identity (1 ,1) not null, +userId int foreign key references useInfo(useNo) not null, +workTime datetime not null, +workDescription varchar(40) check(workDescription='迟到'or workDescription='早退'or workDescription='旷工'or workDescription='病假'or workDescription='事假'), +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\273\203\344\271\2401/\347\273\203\344\271\2401.3.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\273\203\344\271\2401/\347\273\203\344\271\2401.3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d5db95dd1e0d1503d98ce6faf1ca1b418f73b74f --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\273\203\344\271\2401/\347\273\203\344\271\2401.3.sql" @@ -0,0 +1,45 @@ + +create database bbs +on +( + name='bbs', + filename='E:\test\Class.mdf', + size=20MB, + maxsize=100MB, + filegrowth=10% +) +log on +( + name='Class_log', + filename='E:\test\Class_log.ldf', + size=20MB, + maxsize=100MB, + filegrowth=10% +) +create table ClassInfo +( +classid int primary key identity (1 ,1), +ClassName nvarchar(20) unique not null, +Starttime date not null, +Classdescription ntext +) + create table StudentInfo + (StuID int primary key identity(1,1), + StuName nvarchar(20) unique check (Len ( StuName)>=2), + StuSex nvarchar(1) default ('男')check(StuSex='男' or StuSex='女') not null, + Stuage int check(Stuage >= 15 and Stuage <= 40 ), + StuAddress int default ('湖北'), + ClassID int foreign key references ClassInfo(classid), + ) + create table CourseInfo + (CourseID int primary key identity (1,1), + CourseName nvarchar(50) unique not null, + Coursedescription ntext +) + create table ScoreInfo +( + ScoreID int primary key identity (1 ,1) not null, + StuID int not null, + CourseID int not null, + Score int check( Score >= 0 and Score <= 100 ), +) diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\273\203\344\271\2401/\347\273\203\344\271\2401.4.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\273\203\344\271\2401/\347\273\203\344\271\2401.4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cd68c0002904a28b7fee8dcddd89c2c336315c27 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\273\203\344\271\2401/\347\273\203\344\271\2401.4.sql" @@ -0,0 +1,44 @@ +create database House +on +( + name='House', + filename='E:\test\House.mdf', + size=20MB, + maxsize=100MB, + filegrowth=10% +) +log on +( + name='House_log', + filename='E:\test\House_log.ldf', + size=20MB, + maxsize=100MB, + filegrowth=10% +) +create table tblUser +( +userId int primary key identity (1 ,1), +userName nvarchar(20) not null, +userTel nvarchar(20) not null, +) +create table tblHouseType +( +typeId int primary key identity (1 ,1), +typName nvarchar(20) check(typName='别墅'or typName='普通住宅'or typName='平房'or typName='病假地下室'), +) +create table tblQx +( +qxId int primary key identity (1 ,1), +qxName nvarchar(20) check(qxName='武昌'or qxName='汉阳'or qxName='汉口'), +) +create table tblHouseInfo +( +id int primary key identity (1 ,1), +Housedesc ntext, +userId int foreign key(userId) references tblUser(userId), +zj money, +shi int, +ting int, +typeId int foreign key(typeId) references tblHouseType(typeId), +qxId int foreign key(qxId) references tblQx(qxId) +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\273\203\344\271\2402/\347\273\203\344\271\2402.0.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\273\203\344\271\2402/\347\273\203\344\271\2402.0.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d03c3fac531be564000b1422248a798d89d48397 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\273\203\344\271\2402/\347\273\203\344\271\2402.0.sql" @@ -0,0 +1,77 @@ +create database bbs +on + +( + name='bbs', + filename='E:\test\bbs.mdf', + size=20MB, + maxsize=100MB, + filegrowth=10% +) +log on +( + name='bbs_log', + filename='E:\test\bbs_log.ldf', + size=20MB, + maxsize=100MB, + filegrowth=10% + ) +go +use bbs +go +create table bbsUser +( + uID int identity, + 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_uAge 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 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, + rUID int foreign key references bbsUsers (uID), + rTID int foreign key references bbsTopic(sID), + rMsg text not null, + rTime datetime + ) + + insert into bbsUsers(uName,uSex,uAge,uPoint) values (' 小雨点,女,20,0'),('逍遥,男,18,4'),('七年级生,男,19,2') + select uName,uPoint into bbsPoint from bbsUsers + insert into BBSSection (sID,sName,sUID) values(4,'技术交流',1),(5,'读书世界',3),(6,' 生活百科',1),(7,'八卦区',3) + insert into bbsTopic(tUID,tSID,tTitle,tMsg,tTime,tCount)values(2,7,'范跑跑 ',' 谁是范跑跑 ','2008-7-8','1'),(2,4,'.NET ',' 与JAVA的区别是什么呀? ','2008-9-1 ','2'),(0,6,'今年夏天最流行什么 ',' 有谁知道今年夏天最流行 ','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 \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/.keep" "b/\347\254\254\345\205\255\346\254\241\350\257\276\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\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery5.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f14f0a83ac59bd71621b783086eba4735535166f --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\226\207\350\201\252/SQLQuery5.sql" @@ -0,0 +1,180 @@ +create database TestDB +on +( + name='TestDB', + filename='E:\鏁版嵁搴撴枃浠禱鏁版嵁搴撹窡鐩綍鏂囦欢.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10mb +) + +log on +( + name='TestDB_log', + filename='E:\鏁版嵁搴撴枃浠禱鏁版嵁搴撹窡鐩綍鏂囦欢_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +use TestDB +go + +-- 1銆 +create table typeInfo +( + typeId int primary key identity(1,1), + typeName varchar(10) not null, +) + +create table loginInfo +( + LoginId int primary key identity(1,1), + LoginName varchar(10) not null unique, + LoginPwd varchar(20) not null default('1,2,3,4,5,6'), + Logsex char(1), + Logbrithday datetime, + Loghuiyuan varchar +) + +-- 2銆 +create database company +on +( + name='company', + filename='E:\鏁版嵁搴撴枃浠禱鏁版嵁搴撹窡鐩綍鏂囦欢\company.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='company_log', + filename='E:\鏁版嵁搴撴枃浠禱鏁版嵁搴撹窡鐩綍鏂囦欢\company_log.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use company +go + +create table sectionInfo +( + sectionID int primary key, + sectionName varchar(10) not null +) +create table userInfo +( + userNo int primary key not null, + userName varchar(10) unique not null check(userName>4), + userSex varchar(2) not null check(userSex='鐢' or userSex='濂' ), + userAge int not null check(userAge>=1 or userAge<=100), + userAddress varchar(50) default('婀栧寳'), + userSection int foreign key references sectionInfo(sectionID) +) +create table workInfos +( + workId int primary key not null, + userId int foreign key references userInfo(userNo), + workTime datetime not null, + workDescription varchar(40) not null check(workDescription='杩熷埌'or + workDescription='鏃╅'or workDescription='鐭垮伐' or workDescription='鐥呭亣'or + workDescription='浜嬪亣') +) + +-- 3銆 +create database systems +on +( + name='system', + filename='E:\鏁版嵁搴撴枃浠禱鏁版嵁搴撹窡鐩綍鏂囦欢\system.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='system_log', + filename='E:\鏁版嵁搴撴枃浠禱鏁版嵁搴撹窡鐩綍鏂囦欢\system_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use systems +go +create table Classinfo +( + classid int primary key, + classname varchar(10) not null unique, + classremark ntext +) +create table studentinfo +( + stunum int primary key, + stuname varchar check (len(stuname)>2) unique, + stusex varchar(2) default('鐢') check(stusex='鐢' or stusex='濂'), + stuage int check(stuage>=15 or stuage<=40) not null, + stuaddress nvarchar(50) default('婀栧寳姝︽眽'), + stuclassid int references Classinfo(classid) +) +create table information +( + number int primary key, + kechenname varchar unique not null, + miaoshu ntext +) +create table score +( + scorenum int primary key, + scorestuid int references studentinfo(stunum) not null, + scoreid int references information(number) not null, + score int check(score>=0 or score<=100) +) +--4銆 +create database Home +on +( + name='Home', + filename='E:\鏁版嵁搴撴枃浠禱鏁版嵁搴撹窡鐩綍鏂囦欢\Home.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Home_log', + filename='E:\鏁版嵁搴撴枃浠禱鏁版嵁搴撹窡鐩綍鏂囦欢\Home_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use Home +go + +create table tblUser +( + userId int primary key identity(1,1), + userName varchar, + userTel nvarchar(10) +) +create table tblHouseType +( + typeId int primary key identity(1,1), + typName varchar check(typName='鍒'or typName='鏅氫綇瀹'or typName='骞虫埧' or typName='鍦颁笅瀹') not null +) +create table tblQx +( + qxId int primary key identity(1,1), + qxName varchar check(qxName='姝︽槍'or qxName='姹夐槼' or qxName='姹夊彛') not null +) + +create table tblHouseInfo +( + id int primary key identity(1,1), + userId int foreign key references tblUser (userId), + zj money, + shi varchar, + ting int, + typeId int foreign key references tblHouseType (typeId), + qxId int foreign key references tblQx (qxId) +) \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/1\343\201\244.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/1\343\201\244.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a2d29aa45dc94a790998cdbd93122f3174c4886a --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/1\343\201\244.sql" @@ -0,0 +1,155 @@ +create database TestDB +on +( + name='TestDB', + filename='D:\TestDB.mdf' +) + +log on +( + name='TestDB_log', + filename='F:\TestDB_log.ldf' +) +use TestDB +go + +--1つ +create table typeinfo +( + typeId int primary key identity(1,1), + typeName varchar(10) not null, +) + +create table logininfo +( + LoginId int primary key identity(1,1), + LoginName varchar(10) not null unique, + LoginPwd varchar(20) not null default('1,2,3,4,5,6'), + Loginsex char(1), + Loginbrithday datetime, + LoginVIP varchar +) + +-- 二番目 +create database company +on +( + name='company', + filename='D:\company.mdf' +) + +log on +( + name='company_log', + filename='F:\company_log.mdf' +) +use company +go + +create table sectioninfo +( + sectionID int primary key, + sectionName varchar(10) not null +) +create table userinfo +( +userNo int primary key not null, +userName varchar(10) unique not null check(userName>4), +userSex varchar(2) not null check(userSex='男' or userSex='女' ), +userAge int not null check(userAge>=1 or userAge<=100), +userAddress varchar(50) default('湖北'), +userSection int foreign key references sectionInfo(sectionID) +) +create table workinfo +( +workId int primary key not null, +userId int foreign key references userInfo(userNo), +workTime datetime not null, + workDescription varchar(40) not null check(workDescription='迟到'or workDescription='早退'or workDescription='矿工' or workDescription='病假'or workDescription='事假') +) + +-- 三番目 +create database School +on +( + name='School', + filename='D:\School.mdf' +) +log on +( + name='School_log', + filename='D:\School_log.ldf' +) +use School +go +create table Classinfo +( + classid int primary key, + classname varchar(10) not null unique, + classremark ntext +) +create table studentinfo +( + stunum int primary key, + stuname varchar check (len(stuname)>2) unique, + stusex varchar(2) default('男') check(stusex='男' or stusex='女'), + stuage int check(stuage>=15 or stuage<=40) not null, + stuaddress nvarchar(50) default('湖北武汉'), + stuclassid int references Classinfo(classid) +) +create table information +( + number int primary key, + kechenname varchar unique not null, + miaoshu ntext +) +create table score +( + scorenum int primary key, + scorestuid int references studentinfo(stunum) not null, + scoreid int references information(number) not null, + score int check(score>=0 or score<=100) +) +--四番目 +create database house +on +( + name='house', + filename='D:\house.mdf' +) +log on +( + name='house_log', + filename='D:\house_log.ldf' +) +use house +go + +create table tblUser +( + userId int primary key identity(1,1), + userName varchar, + userTel nvarchar(10) +) +create table tblHouseType +( + typeId int primary key identity(1,1), + typName varchar check(typName='别墅'or typName='普通住宅'or typName='平房' or typName='地下室') not null +) +create table tblQx +( + qxId int primary key identity(1,1), + qxName varchar check(qxName='武昌'or qxName='汉阳' or qxName='汉口') not null +) + +create table tblHouseInfo +( + id int primary key identity(1,1), + userId int foreign key references tblUser (userId), + zj money, + shi varchar, + ting int, + typeId int foreign key references tblHouseType (typeId), + qxId int foreign key references tblQx (qxId) +) + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/\344\272\214\347\225\252\347\233\256.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/\344\272\214\347\225\252\347\233\256.sql" new file mode 100644 index 0000000000000000000000000000000000000000..396bb8770ee063a18e5777e1b6b2dcedd77c9dc8 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/\344\272\214\347\225\252\347\233\256.sql" @@ -0,0 +1,105 @@ +-- 1つ +create database bbs +on +( + name='bbs', + filename='E:\bbs.mdf' +) +log on +( + name='bbs_log', + filename='E:\bbs_log.ldf' +) +use bbs +go + +create table bbsUsers +( + uuID 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 primary key (uuID) +alter table bbsUsers add constraint UK unique(uName) +alter table bbsUsers add constraint CK check(uSex='男' or uSex='女' ) +alter table bbsUsers add constraint CK1 check(uAge>=15 or uAge<=60 ) +alter table bbsUsers add constraint CK2 check(upoint>=0) + +create table bbsSection +( + sID int 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(uuID) + + + +create table bbsTopic +( + tID int primary key, + tUID int foreign key references bbsUsers(uuID), + tSID int foreign key references bbsSection(sID), + +) +alter table bbsTopic add tTitle varchar(100) not null +alter table bbsTopic add tMsg text not null +alter table bbsTopic add tTime datetime +alter table bbsTopic add tCount int + +create table bbsReply +( + rID int primary key, + rUID int foreign key references bbsUsers(uuID), + rTID int foreign key references bbsTopic(tID), + rMsg text not null, + rTime datetime +) +insert into bbsUsers(uName,uSex,uAge,uPoint) +select'小雨点','女',20,0 union +select'逍遥','男',18,4 union +select'七年级生','男',19,2 +select * from bbsUsers + +--题目 +--2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中, +--提示查询部分列:select 列名1,列名2 from 表名 +select uName,uPoint into bbsPoint from bbsUsers + +insert into bbsSection(sName, sID) +select '技术交流' , 1union +select '读书世界' ,2 union +select '生活百科' ,3 union +select ' 八卦区' ,4 +select * from bbsSection + +-- 4.向主贴和回帖表中添加几条记录 +-- 发帖人 板块名 帖子标题 +-- 帖子内容 帖时间 回复数量 +insert into bbsTopic( tid,tUID,tSID,tTitle,tMsg,tTime,tCount ) +select 1,3, 4,'范跑跑','谁是范跑跑','2008-7-8',1 union +select 2,2, 1,' .NET','与JAVA的区别是什么呀?','2008-9-1',2 union +select 3,1, 3,' 今年夏天最流行什么','有谁知道今年夏天最流行什么鸭','2008-9-10',0 +select * from bbsTopic + +select * from bbsReply +insert into bbsReply(rid,rUID,rMsg,rTime) +select 1,2,'我们都是范跑跑','2008-7-8'union +select 2,1,'你猜(滑稽)','2008-7-8'union +select 3,3,'母鸡啊','2008-7-8' + +delete bbsUsers where uName='逍遥' +alter table bbsReply drop FK__bbsReply__rUID__4AB81AF0 +alter table bbsTopic drop FK__bbsTopic__tUID__46E78A0C +select * from bbsReply +--给那啥雨点加分10 +update bbsUsers set upoint=12 where uName='逍遥' +--删除生活百科板块 +alter table bbsTopic drop FK__bbsTopic__tSID__47DBAE45 +delete bbsSection where sName='生活百科' +--删除所有回帖 +delete bbsReply \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/\347\273\203\344\271\2401.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/\347\273\203\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..06ee7d80860d975ef068b71aa00b29049547b048 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/\347\273\203\344\271\2401.sql" @@ -0,0 +1,201 @@ +锘縰se master +go +--鍏堝垱寤轰竴涓暟鎹簱锛屾暟鎹簱鍚嶄负TestDB锛岃姹傛湁涓涓暟鎹枃浠讹紝涓涓棩蹇楁枃浠讹紝娉ㄦ剰鍛藉悕瑙勮寖锛屾枃浠跺瓨鏀惧湪D鐩樹笅 +create database TestDB +on +( + name=TestDB, + filename='D:\SIX1\TestDB.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) +log on +( + name=TestDB_ldf, + filename='D:\SIX1\TestDB.ldf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) + +use TestDB +go +--浼氬憳绫诲埆琛(typeInfo)锛 +create table typelnfo +( + --绫诲埆缂栧彿(typeId)锛氫富閿佽嚜鍔ㄧ紪鍙 + typeld int primary key identity(1,1), + --绫诲埆鍚(typeName): varchar(10) 涓嶈兘涓虹┖ + typeName varchar(10) not null, +) +--鐧诲綍鐢ㄦ埛琛(loginInfo)锛 +create table loginInfo +( + --缂栧彿(LoginId)锛屾暟鎹被鍨(int)锛屼富閿佽嚜鍔ㄧ紪鍙 + LoginId int primary key identity(1,1), + --璐︽埛(LoginName)锛屾枃鏈紝闀垮害涓10锛岄潪绌猴紝蹇呴』鍞竴锛屼笉鑳介噸澶 + LoginName nvarchar(10) not null unique , + --瀵嗙爜(LoginPwd)锛屾枃鏈紝闀垮害涓20锛岄潪绌恒侀粯璁ゅ间负鈥123456 + LoginPwd varchar(20) default(123456), + -- 鎬у埆(鑷畾绫诲瀷) + LoginSex nvarchar(1) check(LoginSex='鐢' or LoginSex='濂'), + --鐢熸棩(鑷畾绫诲瀷) + LoginBir date , + --浼氬憳绫诲埆(鑷畾绫诲瀷) + LoginSort text not null +) +--. 鍏堝垱寤轰竴涓暟鎹簱鐢ㄦ潵瀛樻斁鏌愬叕鍙哥殑鍛樺伐淇℃伅锛屾暟鎹簱鐨勫悕绉颁负company锛 +--鍖呭惈2涓暟鎹枃浠1涓棩蹇楁枃浠讹紝鏁版嵁鏂囦欢鍜屾棩蹇楁枃浠跺叏閮ㄥ瓨鏀惧湪D鐩樹腑锛屽垵濮嬪ぇ灏忥紝澧為暱鍜屾渶澶уぇ灏忚嚜宸辫瀹 +create database Company +on( + name=Company, + filename='D:\SIX1\Company.mdf', + size=5MB, + maxsize=5MB, + filegrowth=10% +) + log on( + name=Company_ldf, + filename='D:\SIX1\Company.ldf', + size=5MB, + maxsize=5MB, + filegrowth=10% +) + +use Company +go +--鍐嶅垱寤鸿〃锛氶儴闂ㄤ俊鎭〃锛坰ectionInfo锛 +create table SectionInfo +( + --閮ㄩ棬缂栧彿 sectionID int 鏍囪瘑鍒 涓婚敭 + SectionID int primary key identity(1,1) not null, + --閮ㄩ棬鍚嶇О sectionName varchar(10) 涓嶈兘涓虹┖ + SectionNamme varchar(10) not null, +) +--鍛樺伐淇℃伅琛紙userInfo锛 +create table UserInfo +( + --鍛樺伐缂栧彿 userNo int 鏍囪瘑鍒 涓婚敭 涓嶅厑璁镐负绌 + UserNo int identity(1,1) primary key not null, + --鍛樺伐濮撳悕 userName varchar(10) 鍞竴绾︽潫 涓嶅厑璁镐负绌 闀垮害蹇呴』澶т簬4 + UserName varchar(10) unique not null check(len(UserName)>=4), + --鍛樺伐鎬у埆 userSex varchar(2) 涓嶅厑璁镐负绌 鍙兘鏄敺鎴栧コ + UserSex varchar not null check(UserSex='鐢' or Usersex='濂'), + --鍛樺伐骞撮緞 userAge int 涓嶈兘涓虹┖ 鑼冨洿鍦1-100涔嬮棿 + UserAge int not null check(UserAge>=1 or UserAge<=100), + --鍛樺伐鍦板潃 userAddress varchar(50) 榛樿鍊间负鈥滄箹鍖椻 + UserAddress varchar(50) default('婀栧寳'), + --鍛樺伐閮ㄩ棬 userSection int 澶栭敭锛屽紩鐢ㄩ儴闂ㄤ俊鎭〃鐨勯儴闂ㄧ紪鍙 + UserSection int foreign key references SectionInfo(SectionID), +) +--鍛樺伐鑰冨嫟琛紙workInfo锛 +create table WorkInfo +( + --鑰冨嫟缂栧彿 workId int 鏍囪瘑鍒 涓婚敭 涓嶈兘涓虹┖ + workld int identity(1,1) primary key not null, + --鑰冨嫟鍛樺伐 userId int 澶栭敭 寮曠敤鍛樺伐淇℃伅琛ㄧ殑鍛樺伐缂栧彿 涓嶈兘涓虹┖ + userId int foreign key references UserInfo(UserNo), + --鑰冨嫟鏃堕棿 workTime datetime 涓嶈兘涓虹┖ + WorkTime datetime Not null , + --鑰冨嫟璇存槑 workDescription varchar(40) 涓嶈兘涓虹┖ + --鍐呭鍙兘鏄滆繜鍒扳濓紝鈥滄棭閫鈥濓紝鈥滄椃宸モ濓紝鈥滅梾鍋団濓紝鈥滀簨鍋団濅腑鐨勪竴绉 + WorkDescription varchar(40) not null + check(WorkDescription='杩熷埌' or WorkDescription='鏃╅' or WorkDescription='鏃峰伐' or WorkDescription='浜嬪亣' or WorkDescription='录') +) +--涓哄鏍″紑鍙戜竴涓鐢熺鐞嗙郴缁燂紝璇蜂负璇ョ郴缁熷垱寤烘暟鎹簱锛屼富瑕佸瓨鏀剧殑淇℃伅鏈夛細鐝骇淇℃伅銆佸鐢熶俊鎭佽绋嬩俊鎭佸鐢熻冭瘯鎴愮哗 +create database AdminiInfo +go + +use AdminiInfo +go +--鐝骇淇℃伅 +create table ClassInfo +( + --鐝骇缂栧彿 classid (涓婚敭銆佹爣璇嗗垪) + ClassID int primary key identity(1,1), + --鐝骇鍚嶇О(渚嬪锛歍1銆乀2銆丏09绛夌瓑):涓嶈兘涓虹┖锛屼笉鑳介噸澶 + ClassName nvarchar(10) not null unique, + --寮鍔炴椂闂达細涓嶈兘涓虹┖ + ClassTime time not null, + -- 鐝骇鎻忚堪 + ClassDes text +) +--瀛︾敓淇℃伅 +create table StudentInfo +( + --瀛﹀彿锛氫富閿佹爣璇嗗垪 + StuID int primary key identity(1,1), + --濮撳悕锛氶暱搴﹀ぇ浜2锛屼笉鑳介噸澶 + StuName nvarchar(10) not null unique check(len(StuName)>2), + --鎬у埆锛氬彧鑳芥槸鈥樼敺鈥欐垨鈥樺コ鈥欙紝榛樿涓虹敺锛屼笉鑳戒负绌 + StuSex nvarchar(2) not null default('鐢') check(StuSex='' or StuSex='濂'), + --骞撮緞锛氬湪15-40涔嬮棿锛屼笉鑳戒负绌 + StuAge int not null, + --瀹跺涵鍦板潃锛氶粯璁や负鈥滄箹鍖楁姹夆 + StuAdress varchar(12) default('婀栧寳姝︽眽'), + --鎵鍦ㄧ殑鐝骇缂栧彿 + ClassID int foreign key references ClassInfo(ClassID) +) +--璇剧▼淇℃伅锛氱紪鍙凤細涓婚敭銆佹爣璇嗗垪 +create table CourseInfo +( + --缂栧彿锛氫富閿佹爣璇嗗垪 + CourseID int primary key identity(1,1), + --璇剧▼鍚嶏細涓嶈兘涓虹┖锛屼笉鑳介噸澶 + CourseName nvarchar(10) not null unique, + -- 璇剧▼鎻忚堪锛 + CourseDes text +) +--鎴愮哗淇℃伅锛 +create table Score +( + --鎴愮哗缂栧彿锛氫富閿佹爣璇嗗垪 + ScoreID int primary key identity(1,1), + --鎴愮哗鎵灞炰簬鐨勫鐢熺紪鍙凤紝涓嶈兘涓虹┖ + ScoreStuID int foreign key references StudentInfo(StuID), + --鎴愮哗鎵灞炵殑璇剧▼缂栧彿锛屼笉鑳戒负绌 + ScoreClassID int foreign key references ClassInfo(ClassID), + --鎴愮哗锛氬湪0-100涔嬮棿 + Score int check(Score>=1 and Score<=100) not null +) +--涓轰竴涓埧灞嬪嚭绉熺郴缁熷垱寤轰竴涓暟鎹簱锛屾暟鎹簱涓富瑕佸瓨鏀炬埧灞嬬殑淇℃伅 +create database HomeInfo +go + +use HomeInfo +go +--tblUser --鍙戝竷浜轰俊鎭〃 +create table TbIUser +( + --鍙戝竷浜虹殑淇℃伅(濮撳悕鍜岃仈绯荤數璇) + UserID int primary key identity(1,1), + UserName nvarchar(10) not null, + UserTel nvarchar(11) not null check(len(UserTel)=11) +) +--tblHouseType --鎴垮眿鐨勭被鍨 +create table tblHouseType +( + TypelD int primary key identity(1,1), + Typname nvarchar(10) not null check(Typname='' or Typname='平' or Typname='通住宅' or Typname='') +) + +create table TbIQx +( + QxID int primary key identity(1,1), + Qxname nvarchar(10) not null check(Qxname='' or Qxname='' or Qxname='') +) +--鎴垮眿鎵灞炵殑鍖哄幙(姝︽槍銆佹眽闃炽佹眽鍙)锛 +create table TblHouseInfo +( + HomeID int primary key identity(1,1), + HomeDesc nvarchar(10), + UserID int foreign key references TbIUser(UserID), + Homezj int not null, + HomeShi int not null, + HomeTing int not null, + TypeID int foreign key references tblHouseType(TypelD), + QxID int foreign key references TbIQx(Qxid) +) + + diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/\347\273\203\344\271\2402.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/\347\273\203\344\271\2402.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ade4f74ba7c8b76e95355ce4377162a4a281553c --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/\347\273\203\344\271\2402.sql" @@ -0,0 +1,128 @@ +use master +go +--先创建数据库和表以及约束 +--创建一个数据库用来存放某论坛的用户和发帖信息,数据库的名称为bbs,包含1个数据文件1个日志文件 +--全部存放在D盘中,初始大小,增长和最大大小自己设定 +--注意以下4张表的创建顺序,要求在创建bbsUser和bbsSection表时不添加约束,创建完后单独添加约束,其它的表创建时添加约束 +create database bbs +on +( + name='bbs', + filename='D:\SIX2\bbs.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10% + +) +, +( + name='bbs2_DATA', + filename='D:\SIX2\bbs2_DATA.Ndf', + size=1MB, + maxsize=100MB, + filegrowth=10% +) +log on +( + name='bbs_ldf', + filename='D:\SIX2\bbs_log.ldf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) + +use bbs +go +--用户信息表(bbsUsers) +create table bbsUsers +( + --用户编号 UID int 主键 标识列 + UID int primary key identity(1,1), + --用户名 uName varchar(10) 唯一约束 不能为空 + uName varchar(10) unique not null, + --性别 uSex varchar(2) 不能为空 只能是男或女 + uSex varchar(2) not null check (uSex='男' or uSex='女'), + --年龄 uAge int 不能为空 范围15-60 + uAge int not null check(uAge>=15 and uAge<=60), + --积分 uPoint int 不能为空 范围 >= 0 + uPoint int not null check(uPoint>=0) +) + +select *from bbsUsers +--版块表(bbsSection) +create table bbsSection +( + --版块编号 sID int 标识列 主键 + sID int identity(1,1) primary key, + --版块名称 sName varchar(10) 不能为空 + sName varchar(10) not null, + --发帖人编号 tUID int 外键 引用用户信息表的用户编号 + 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 diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\347\273\203\344\271\2401.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\347\273\203\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..457486e75880c31bf8faecc6fc0e1bcf74dfd547 --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\347\273\203\344\271\2401.sql" @@ -0,0 +1,121 @@ +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 +) +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 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) + +insert into bbsReply(rUID,rTID,rMsg,rTime) +values(3,2,'范跑跑','2008-7-8') + +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 \ No newline at end of file diff --git "a/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\347\273\203\344\271\2402.sql" "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\347\273\203\344\271\2402.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1260c992d523eca749f5a19ee1080bde955d6a0b --- /dev/null +++ "b/\347\254\254\345\205\255\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/\347\273\203\344\271\2402.sql" @@ -0,0 +1,163 @@ +use master +go + +create database TestDB +on +( + name='TestDB', + filename='E:\TestDB.mdf', + size=10mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='TestDB_log', + filename='E:\TestDB_log.ldf', + size=10mb, + maxsize=100mb, + filegrowth=10mb +) +go +use TestDB +go +create table typeInfo +( + typeld int primary key identity(1,1), + typeName varchar(10) not null +) + +create table loginInfo +( + LoginId int primary key identity(1,1), + LoginName nvarchar(10) not null unique , + LoginPwd nvarchar(20) not null default('123456'), + LoginSex char(2) default('男') check(LoginSex='男' or LoginSex='女'), + LoginBirthday date, + LoginHuiyuan nvarchar(20) +) +go +use master +go + +create database company +on +( + name='company', + filename='E:\company.mdf', + size=10mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='company_log', + filename='E:\company_log.ldf', + size=10mb, + maxsize=100mb, + filegrowth=10mb +) +go +use company +go +create table sectionInfo +( + sectionID int primary key identity(1,1), + ectionName varchar(10) not null +) + +create table userInfo +( + userNo int primary key identity(1,1) not null, + userName varchar(10) unique check(userName>4) not null, + userSex varchar(2) not null check(userSex='男' or userSex='女'), + userAge int not null check(userAge>=1 and userAge<=100), + userAddress varchar(50) default('湖北'), + userSection int references sectionInfo(sectionID) +) +create table workInfo +( + workId int primary key identity(1,1) not null, + userId int references userInfo(userNo) not null, + workTime datetime not null, + workDescription varchar(40) not null check(workDescription='迟到' or workDescription='早退' or workDescription='旷工' or workDescription='病假' or workDescription='事假') +) +go +use master +go +create database Studen01 +go + +use Studen01 +go + +create table classInfo +( + classID int primary key identity(1,1), + className nvarchar(20) not null unique, + Opentime datetime not null, + classDescription text +) + +create table StuInfo +( + stuID int primary key identity (1,1), + stuName nvarchar(10) check(stuName>2) unique, + stuSex char(2) default('男') check(stuSex='男' or stuSex='女') not null, + stuAag int check(stuAag>=15 and stuAag<=40) not null, + stuAddress nvarchar(200) default('湖北武汉') +) + +create table CourInfo +( + CourID int primary key identity(1,1), + CourName nvarchar(10) not null unique, + CourDescription text +) + +create table PerInfo +( + PerNO int primary key identity(1,1), + stuID int references StuInfo(stuID) not null, + CourID int references CourInfo(CourID) not null, + Perscore int check(Perscore>=0 and Perscore<=100) +) +go + +use master +go + +create database Rentalhousing +go +use Rentalhousing +go + +create table tblUser +( + userId int primary key identity(1,1), + userName nvarchar(20) not null unique, + userTel varchar(20) not null +) + +create table tblHouseType +( + typeId int primary key identity(1,1), + typName nvarchar(20) not null unique +) + +create table tblQx +( + qxID int primary key identity(1,1), + qxName nvarchar(20) not null unique +) + +create table tblHouseInfo +( + id int primary key identity(1,1), + descv nvarchar(20) not null, + userld nvarchar(20) not null, + zj nvarchar(20) not null, + ting nvarchar(20) not null, + typeId int references tblHouseType(typeId), + qxId int references tblQx(qxId) +) \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/Student.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/Student.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8a49cd380796ffe308fa8c927d793b0bb3ec9963 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/Student.sql" @@ -0,0 +1,98 @@ +Use master +go +create database Student +on +( + name='Student', + filename='D:\Student.mdf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +log on +( + name='Student_log', + filename='D:\Student_log.ldf', + size=6MB, + maxsize=28MB, + filegrowth=3MB +) +go +use Student +go +create table Classinfo +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null, +) +insert into Classinfo(ClassName) values ('1鐝'),('2鐝'),('3鐝'),('4鐝'), +('5鐝'),('6鐝'),('7鐝'),('8鐝'),('9鐝'),('10鐝') +update ClassInfo set ClassName='11鐝' where ClassID=1 +delete from Classinfo where ClassID=10 +go + +create table Student + +( + StuID int primary key identity(1,1), + ClassID int foreign key references Classinfo(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('鐢') check(StuSex in('鐢','濂')), + StuBirthday date , + StuPhone nvarchar(11) unique, + StuAddress nvarchar(200), + +) +insert into Student(ClassID,StuName,StuSex,StuPhone) values (1,'鐜嬩簲','鐢',12345678901), +(2,'鐜嬩簲','鐢',12345678902),(3,'鐜嬩簲','鐢',12345678903), +(4,'鐜嬩簲','鐢',12345678905),(5,'鐜嬩簲','鐢',12345678904), +(6,'鐜嬩簲','鐢',12345678906),(7,'鐜嬩簲','鐢',12345678907), +(8,'鐜嬩簲','鐢',12345678909),(9,'鐜嬩簲','鐢',12345678908), +(10,'鐜嬩簲','鐢',12345678911),(11,'鐜嬩簲','鐢',12345678912),(14,'寮犱竴','鐢',12345678913), +(12,'鐜嬩簲','鐢',12345678914),(13,'鐜嬩簲','鐢',12345678921), +(15,'鐜嬩簲','鐢',12345678915),(16,'鐜嬩簲','鐢',12345678920), +(18,'鐜嬩簲','鐢',12345678916),(17,'鐜嬩簲','鐢',12345678919), +(20,'鐜嬩簲','鐢',12345678917),(19,'鐜嬩簲','鐢',12345678918) + +alter table Student add CreateDate datetime default(getdate()) + +update Student set CreateDate=getdate() + +delete from Student where ClassID=8 +go + +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int not null Default(1) check(CourseCredit>0 and CourseCredit<6), + CourseType nvarchar(10) check(CourseType='涓撲笟璇' or CourseType='鍏叡璇') + +) +insert into Course (CourseName) values ('涓撲笟璇'),('鑻辫璇'), +('楂樻暟璇'),('鎬濅慨璇'),('浣撹偛璇'),('鑱岀礌璇') + +select CourseName from Course + +update Course set CourseCredit=2 where CourseID=1 +go + +create table Score +( + ScoreID int primary key identity(1,1), + StuID int foreign key references Student(StuID), + CourseID int foreign key references Course(CourseID), + Score decimal(5,2) unique not null +) +alter table Score add constraint CK_Score_Score check(Score>=0 and Score<=100) + + +insert into Score (StuID,CourseID,Score) values (1,1,80),(2,3,31), +(3,3,62),(4,4,63),(5,5,64),(6,6,65), +(7,1,66),(8,2,37),(10,4,69),(11,5,70), +(12,6,71),(13,1,52),(14,2,73),(15,3,74), +(16,4,75),(17,5,66),(19,1,88),(20,2,90) + +delete from Score where StuID=1 +delete from Score where CourseID=1 + diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery3.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..10fae00e179acf515984c62e5acca369981c0eb7 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SQLQuery3.sql" @@ -0,0 +1,136 @@ +use master--1 +go--2 +--3 +create database Student04--4 +on(--5 + name='Student04_mdf', + filename='D:\SQL代码\Student04.mdf', + size=3MB, + maxsize=50MB, + filegrowth=10% +)--11 +log on(--12 + name='Student04_ldf', + filename='D:\SQL代码\Student04_ldf.ldf', + size=3MB, + maxsize=50MB, + filegrowth=10% +)--18 +go + +use Student04 +go + +create table Class--24 +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null +)--28 +insert into Class(ClassName) select ('软件1班') union all +select ('软件2班') union all +select ('软件3班') union all +select ('软件4班') union all +select ('软件5班') union all +select ('软件6班') union all +select ('软件7班') union all +select ('软件8班') union all +select ('软件9班') union all +select ('软件10班') +--39 +select * from Class +update Class set ClassName='软件11班' where ClassID=1 +delete Class where ClassID=10 + +create table Student--43 +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex in('男','女')), + StuBirthday date , + StuPhone nvarchar(11) unique , + StuAddress nvarchar(200) , + +)--53 +insert into Student (ClassID,StuName, StuSex, StuBirthday,StuPhone,StuAddress) +values (1, '张三' ,'男','2002-05-24','153453557','江西南昌') , +(2,'李四','男','2002-11-25','153053554','江西九江') + insert into Student (ClassID,StuName, StuSex, StuBirthday,StuPhone,StuAddress) + values(3, '张三' ,'男','2002-04-24','153442557','江西南昌') , +(4,'张六','男','2002-05-27','1535355469','江西抚州') , + (5,'张七','男','2002-05-28','15345355417','江西新余') , +(6,'张八','女','2002-05-29','15345355427','江西南昌') , + (7,'张九','男','2002-06-01','15345355437','江西南昌'), + (8,'张十','男','2002-05-14','1534535477','江西九江'), + (9,'李七','女','2002-05-04','1534535447','江西南昌'), + (1,'李五','女','2002-05-13','1234035377','江西南昌') , +(1,'陈六','男','2002-06-24','15745356747','江西南昌') , +(2,'陈七','男','2002-08-25','14584535466','江西九江'), + (3,'程八','女','2002-07-26','1574554648','江西赣州') , +(4,'程九','男','2002-04-27','1546554679','江西抚州') , +(5,'程十','男','2002-03-28','21345355461','江西新余') , +(6,'王一','女','2002-02-29','3453546207','江西南昌') , +(7,'王九','男','2002-01-01','1534564637','江西南昌') , +(8,'王十','男','2002-11-14','15457554577','江西九江'), +(9,'王四','女','2002-12-04','1545654477','江西南昌') , +(2,'王五','女','2002-05-01','1534535477','江西南昌') + +delete Student where ClassID=1 +alter table Student add CreateDate datetime default(getdate()) +update Student set CreateDate='2021-03-11 13:21:42.417' +select * from Student + +create table Course +( + Courseld int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int not null default(1) check(CourseCredit>=1 and CourseCredit<=5), + CourseType nvarchar(10) check(CourseType in + ('专业课' , '公共课')) +) + +insert into Course(CourseName) +select '数学' union +select '英语' union +select '体育' union +select '语文' union +select 'html' union +select 'java' + +select * from Course +update Course set CourseCredit=5 where CourseName='java' + +create table Score +( + Scoreld int primary key identity(1,1), + StuID int foreign key references Student(StuID), + Courseld int foreign key references Course(Courseld), + Score decimal(5,2) unique not null +) +insert into Score(Score) +select 88 union +select 61 union +select 92 union +select 63 union +select 74 union +select 65 union +select 66 union +select 57 union +select 68 union +select 69 union +select 70 union +select 71 union +select 72 union +select 63 union +select 74 union +select 75 union +select 86 union +select 77 union +select 58 union +select 79 + +select * from Score +update Course set CourseCredit=5 where CourseName='英语' +delete Score where StuId=1 +delete Score where Courseld=1 +alter table Score add constraint DF default(0) for Score, check(Score>=0 or Score<=100) diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery7.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b7dd1ca02c4d8a38473a9b320c1ca7eb9205f298 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\231\346\200\235\346\235\260/SQLQuery7.sql" @@ -0,0 +1,117 @@ +use master +create database Student1 +on primary +( + name=Student1, + filename='D:\SQL作业\SQL作业3\Student1.mdf', + size=5mb, + filegrowth=1mb +) + +log on +( + name=Student1_log, + filename='D:\SQL作业\SQL作业3\Student1_log.ldf', + size=1mb, + maxsize=10mb, + filegrowth=10% +) +go + +use Student1 +create table Class +( + ClassId int primary key identity(1,1), + ClassName nvarchar(20) not null +) +go + +insert into Class(ClassName) values ('一班'),('二班'), +('三班'),('四班'),('五班'),('六班'),('七班'),('八班'), +('九班'),('十班') +go + +update Class set ClassName = ('1班') where ClassId = 1 + +delete Class where ClassId = 10 + +use Student1 +go +select * from Class + +use Student1 +create table Student +( + StuId int primary key identity(1,1), + ClassId int constraint FK_Class_ClassId references Class(ClassId), + StuName nvarchar(20) not null, + StuSex nvarchar(1) Check( StuSex in('男','女')) not null, + StuBirthday date, + StuPhone nvarchar(11) unique(StuPhone), + StuAddress nvarchar(200) +) +go + +insert into Student(StuName,StuSex,StuPhone,ClassId) +values +('一一','男','12345678912',1),('二二','男','12345678913',2),('三三','女','12345678914',3),('四四','女','12345678915',4), +('五五','男','12345678916',5),('六六','女','12345678917',6),('八八','男','12345678918',7),('九九','女','12345678919',8), +('小明','男','12345678922',9),('小李','男','12345678932',6),('小红','女','12345678942',1),('小丽','女','12345678952',2), +('小余','男','12345678962',8),('小陈','男','12345678972',1),('小杨','男','12345678982',5),('小毛','女','12345678992',6), +('小小','女','22345678912',1),('大大','男','32345678912',8),('少少','男','42345678912',9),('多多','女','52345678912',1) + +alter table Student add CreateDate datetime default(getdate()) + +delete Student where ClassId = 2 + +use Student1 +go +select * from Student + +use Student1 +create table Course +( + CourseId int primary key identity(1,1), + CourseName nvarchar(50) unique(CourseName) not null, + CourseCredit int default(1) check(CourseCredit >=1 and CourseCredit <=5 ), + CourseType nvarchar(10) check(CourseType = '专业课' or CourseType = '文化课') +) +go + +insert into Course(CourseName) +values +('语文'),('高数'),('英语'),('思修'),('体育'),('职素') + +update Course set CourseCredit = 2 where CourseName = '高数' + +use Student1 +go +select * from Course + +use Student1 +create table Score +( + ScoreId int primary key identity(1,1), + StuId int constraint FK_Student_StuId references Student(StuId), + CourseId int constraint FK_Course_CourseId references Course(CourseId), + Score decimal(5,2) unique(Score) not null +) +go + +insert into Score(StuId,CourseId,Score) +values +(13,4,86.5),(14,1,89.5),(15,3,94.5),(16,1,62.5),(17,4,60.0), +(18,3,84.5) + +delete Score where StuId = 1 +delete Score where CourseId = 1 +alter table Score add constraint CK_Score_Score check(Score >= 0 and Score <= 100) +alter table Score add constraint DK_Score_Score default'0' for Score + +use Student1 +go +select * from Score + +use Student1 +go +select * from Student \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\275.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\275.sql" new file mode 100644 index 0000000000000000000000000000000000000000..eb0f1621da06fcab5a94766c095528371f060d8f --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\207\214\345\256\217\344\270\275/\345\207\214\345\256\217\344\270\275.sql" @@ -0,0 +1,41 @@ + create database Student + go + use Student + go + create table Class + ( ClassId int primary key identity(1,1), + ClassName nvarchar(20) unique not null, + ) + --添加记录 + insert into Class(ClassName) + select 'a' union + select 'b' union + select 'c' union + select 'd' union + select 'e' union + select 'f' union + select 'g' union + select 'h' union + select 'i' union + select 'j' + --修改编号 + update Class set ClassName='a' where ClassId='1' + --删除编号 + delete Class where ClassId='10' + create table Student + ( + StuId int primary key identity(1,1), + ClassId int foreign key references Class(ClassId), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女'), + StuBirthday date , + StuPhone nvarchar(11) not null unique, + StuAddress nvarchar(200), + CreateDate datetime, + ) + --创建时间字段 + alter table Student add CreateDate datetime default(getdate()) + update Student set CreateDate='2021-03-10 16:35:10.568' + --删除编号 + delete Student where ClassId='2' + set identity_insert Student off diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e691ec74d31c980a454312e6862dfd3b8f44cbfd --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\230\211\344\277\212/SQLQuery1.sql" @@ -0,0 +1,38 @@ +use Student +go + +set identity_insert Student on + +insert into Class (ClassName) values ('软件1班'),('软件2班'),('软件3班'),('软件4班'),('软件5班'),('软件6班'),('软件7班'),('软件8班'),('软件9班'),('软件10班') +update Class set ClassName = '软件11班' where ClassID = '1' +delete from Class where ClassID = '10' + +insert into Student (StuID,ClassID,StuName,StuSex,StuBirthday,StuPhone,StuAddress) values ('5','1','张三','男','2001.12.22','123456','福州') +insert into Student (StuID,ClassID,StuName,StuSex,StuBirthday,StuPhone,StuAddress) values ('7','2','李四','男','2001.12.25','123457','闽清') +insert into Student (StuID,ClassID,StuName,StuSex,StuBirthday,StuPhone,StuAddress) values ('4','3','赵五','男','2001.12.10','123458','贵州') +insert into Student (StuID,ClassID,StuName,StuSex,StuBirthday,StuPhone,StuAddress) values ('1','4','老六','男','2001.11.22','123459','广州') +insert into Student (StuID,ClassID,StuName,StuSex,StuBirthday,StuPhone,StuAddress) values ('6','5','小芳','女','2001.10.23','123410','长沙') +insert into Student (StuID,ClassID,StuName,StuSex,StuBirthday,StuPhone,StuAddress) values ('10','6','小红','女','2001.12.12','123411','厦门') +insert into Student (StuID,ClassID,StuName,StuSex,StuBirthday,StuPhone,StuAddress) values ('11','7','小贾','女','2001.10.22','123422','上海') +insert into Student (StuID,ClassID,StuName,StuSex,StuBirthday,StuPhone,StuAddress) values ('22','8','不懂','男','2000.12.22','123433','西安') +insert into Student (StuID,ClassID,StuName,StuSex,StuBirthday,StuPhone,StuAddress) values ('15','9','李洁','女','2001.11.12','123477','北京') +insert into Student (StuID,ClassID,StuName,StuSex,StuBirthday,StuPhone,StuAddress) values ('9','9','q宝','男','2001.11.8','123499','福州') + +alter table Student add CreateDate datetime default getdate() +update Student set CreateDate = getdate() where CreateDate is null +delete from Student where ClassID = '9' + +insert into Course (CourseName) values ('数学'),('语文'),('英语'),('物理'),('化学'),('生物') +select * from Course +update Course set CourseCredit = '3' where CourseName = '数学' + +insert into Score (ScoreID,StuID,CourseID,Score) values ('1','1','3','90.5') +insert into Score (ScoreID,StuID,CourseID,Score) values ('2','4','3','91') +insert into Score (ScoreID,StuID,CourseID,Score) values ('3','5','3','92.5') +insert into Score (ScoreID,StuID,CourseID,Score) values ('4','9','3','88') +insert into Score (ScoreID,StuID,CourseID,Score) values ('6','7','3','92') + +delete from Score where StuID = '1' +delete from Score where CourseID = '1' +alter table Score add constraint CK_Score default('0') +alter table Score add constraint DF_Score check(Score>=0 and Score<=100) diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery1.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..63c5e88e3afa5471bca11d92ea856f373d291b16 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\345\273\272\345\263\260/SQLQuery1.sql" @@ -0,0 +1,107 @@ +use master +go +create database Student1 +on( +name='Student2', +filename='D:\作业\SQL作业', +size=5, +maxsize=10, +filegrowth=1 +) +log on( +name='Student2_log', +filename='D:\作业\SQL作业', +size=5, +maxsize=10, +filegrowth=1 +) +go +use Student1 +go +create table Class +( +ClassId int primary key identity(1,1), +ClassName nvarchar(20) unique not null +) +--insert into Class (ClassName) Values ('软件1班'),('软件2班'),('软件3班'),('软件4班'),('软件5班'),('软件6班'),('软件7班'),('软件8班'),('软件9班'),('软件10班') +insert into Class (ClassName) +select ('软件1班')union +select ('软件2班')union +select ('软件3班')union +select ('软件4班')union +select ('软件5班')union +select ('软件6班')union +select ('软件7班')union +select ('软件8班')union +select ('软件9班')union +select ('软件10班') +select*from Class +truncate table Class +UPdate Class set ClassName='软件101班' where ClassId=1 +delete from Class +create table Student +( +StuId int primary key identity(1,1), +ClassId int foreign key references Class(ClassId), +StuName nvarchar(20) not null, +Stusex nvarchar(1) default('男') check(Stusex='男' or Stusex='女'), +Stubirthday date, +Stuphone nvarchar(11) unique, +StuAddress nvarchar(200), +Createdate datetime default getdate() +) +insert into Student (ClassId,StuName,Stusex,Createdate) values ('','zs','男','2021/3/10') +update Student set Createdate +alter table Student drop constraint FK__Student__ClassId__2B3F6F97 +select*from Student +delete from Student where ClassId=0 +create table Course +( +CourseId int primary key identity(1,1), +CourseName nvarchar(50) unique not null, +CourseCredit int default('1') check(CourseCredit>=1 or CourseCredit<=5), +CourseCredit2 nvarchar(10) CHECK(CourseCredit2='专业课' OR CourseCredit2='公共课') +) +insert into Course(CourseCredit2) +select ('专业课') union +select ('公共课') union +select ('专业课') union +select ('公共课') union +select ('专业课') union +select ('公共课') + +select CourseCredit2 from Course +update Course SET CourseCredit='100' where CourseName='公共课' +create table Score +( +ScoreId int primary key identity(1,1), +StuId int foreign key references Student(StuId), +CourseId int foreign key references Course(CourseId), +Score1 decimal(5,2) unique not null +) +select*from Score +drop table Score +--insert into Score(CourseId,Score) values('1','100'),('1','100'),('1','100'),('1','100'),('1','100'),('1','100'); +insert into Score(Score1) +select ('100')union +select ('90')union +select ('100')union +select ('90')union +select ('100')union +select ('90')union +select ('100')union +select ('90')union +select ('100')union +select ('90')union +select ('100')union +select ('90')union +select ('100')union +select ('90')union +select ('100')union +select ('90') +truncate table Score +delete from Score WHERE ScoreId=1 +DELETE FROM Score where CourseId=1 +alter table Score add constraint fffk CHECK(Score1>=0 or Score1<=100) +alter table Course add constraint fqfk default(0) +UPDATE Score SET Score1='10' WHERE CourseId='2' \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery4.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..87e183b03560f0827c67c5a71dd94d5ddd36e703 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\346\226\207\345\274\272/SQLQuery4.sql" @@ -0,0 +1,107 @@ +use master +go +create database Student1 +on( +name='Student2', +filename='D:\作业\SQL作业', +size=5, +maxsize=10, +filegrowth=1 +) +log on( +name='Student2_log', +filename='D:\作业\SQL作业', +size=5, +maxsize=10, +filegrowth=1 +) +go +use Student1 +go +create table Class +( +ClassId int primary key identity(1,1), +ClassName nvarchar(20) unique not null +) +--insert into Class (ClassName) Values ('软件1班'),('软件2班'),('软件3班'),('软件4班'),('软件5班'),('软件6班'),('软件7班'),('软件8班'),('软件9班'),('软件10班') +insert into Class (ClassName) +select ('软件1班')union +select ('软件2班')union +select ('软件3班')union +select ('软件4班')union +select ('软件5班')union +select ('软件6班')union +select ('软件7班')union +select ('软件8班')union +select ('软件9班')union +select ('软件10班') +select*from Class +truncate table Class +UPdate Class set ClassName='软件101班' where ClassId=1 +delete from Class +create table Student +( +StuId int primary key identity(1,1), +ClassId int foreign key references Class(ClassId), +StuName nvarchar(20) not null, +Stusex nvarchar(1) default('男') check(Stusex='男' or Stusex='女'), +Stubirthday date, +Stuphone nvarchar(11) unique, +StuAddress nvarchar(200), +Createdate datetime default getdate() +) +insert into Student (ClassId,StuName,Stusex,Createdate) values ('','zs','男','2021/3/10') +update Student set Createdate +alter table Student drop constraint FK__Student__ClassId__2B3F6F97 +select*from Student +delete from Student where ClassId=0 +create table Course +( +CourseId int primary key identity(1,1), +CourseName nvarchar(50) unique not null, +CourseCredit int default('1') check(CourseCredit>=1 or CourseCredit<=5), +CourseCredit2 nvarchar(10) CHECK(CourseCredit2='专业课' OR CourseCredit2='公共课') +) +insert into Course(CourseCredit2) +select ('专业课') union +select ('公共课') union +select ('专业课') union +select ('公共课') union +select ('专业课') union +select ('公共课') + +select CourseCredit2 from Course +update Course SET CourseCredit='100' where CourseName='公共课' +create table Score +( +ScoreId int primary key identity(1,1), +StuId int foreign key references Student(StuId), +CourseId int foreign key references Course(CourseId), +Score1 decimal(5,2) unique not null +) +select*from Score +drop table Score +--insert into Score(CourseId,Score) values('1','100'),('1','100'),('1','100'),('1','100'),('1','100'),('1','100'); +insert into Score(Score1) +select ('100')union +select ('90')union +select ('100')union +select ('90')union +select ('100')union +select ('90')union +select ('100')union +select ('90')union +select ('100')union +select ('90')union +select ('100')union +select ('90')union +select ('100')union +select ('90')union +select ('100')union +select ('90') +truncate table Score +delete from Score WHERE ScoreId=1 +DELETE FROM Score where CourseId=1 +alter table Score add constraint fffk CHECK(Score1>=0 or Score1<=100) +alter table Course add constraint fqfk default(0) +UPDATE Score SET Score1='10' WHERE CourseId='2' \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\232.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c5ec1e6a7edc89dfd1bc99861a6ee91e03430fdc --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\210\230\351\237\265\345\251\267/\344\275\234\344\270\232.sql" @@ -0,0 +1,131 @@ +create database Student01 +on +( + name='Student01', + filename='F:\SQL\Student01.mdf', + size=10mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='Student01_log', + filename='F:\SQL\Student01_log.ldf', + size=10mb, + maxsize=50mb, + filegrowth=10% +) +use Student01 +go + +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null, +) + +insert into Class(ClassName) +select '1班' union +select '2班' union +select '3班' union +select '4班' union +select '5班' union +select '6班' union +select '7班' union +select '8班' union +select '9班' union +select '10班' + +update Class set ClassName='1班' where ClassID='1' + +delete Class where ClassID='10' + +select * from Class + +create table Student +( + StuId int primary key identity(1,1), + ClassId int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) check(StuSex='男' or StuSex='女')default('男'), + StuBirthday date , + StuPhone nvarchar(11) unique , + StuAddress nvarchar(200) +) + +set IDENTITY_INSERT Student ON + +insert into Student(StuId,ClassId,StuName,StuSex,StuBirthday,StuPhone,StuAddress) +select '10086', 1,'小一','男', '2020/5/6',15168,'福建省' union +select '10087', 2,'小二','男', '2020/10/6',15169,'福建省' union +select '10088', 3,'小三','男', '2021/3/6',15170,'福建省' union +select '10089', 4,'小四','男', '2021/8/6',15171,'福建省' union +select '10090', 5,'小五','男', '2022/1/6',15172,'福建省' union +select '10091', 6,'小六','女', '2022/6/6',15173,'福建省' union +select '10092', 7,'小七','女', '2022/11/6',15174,'福建省' union +select '10093', 8,'小八','女', '2023/4/6',15175,'福建省' union +select '10094', 9,'小九','女', '2023/9/6',15176,'福建省' + +select * from Student +alter table Student add CreateDate datetime default(getdate()) + +delete Student where ClassId='2' + +set IDENTITY_INSERT Student OFF + +create table Course +( + CourseId int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int not null default(1) check(CourseCredit>=1 or CourseCredit<=5), + Classtypes nvarchar(10) check(Classtypes in('职素课' , '政治课')) +) + +insert into Course(CourseName) +select '语文' union +select '数学' union +select '英语' union +select '专业' union +select '思修' union +select '体育' + +select * from Course + +update Course set CourseCredit=2 where CourseName='体育' + +create table Score +( + ScoreId int primary key identity(1,1), + StuId int foreign key references Student(StuId), + CourseId int foreign key references Class(ClassId), + Score decimal(5,2) unique not null +) + +insert into Score(Score) +select 60 union +select 61 union +select 62 union +select 63 union +select 64 union +select 65 union +select 66 union +select 67 union +select 68 union +select 69 union +select 70 union +select 71 union +select 72 union +select 73 union +select 74 union +select 75 union +select 76 union +select 77 union +select 78 union +select 79 + +select * from Score + +update Score set Score=100 where ScoreId=20 +delete Score where StuId=1 +delete Score where CourseId=1 +alter table Score add constraint DF default(0) for Score, check(Score>=0 or Score<=100) \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery3.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7519d343e7e61e486fdbcaaaee9ef18f5068dc42 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\347\205\214/SQLQuery3.sql" @@ -0,0 +1,155 @@ +create database Student12 +on +( + name='Student12', + filename='F:\SQL\Student12.mdf', + size=10mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='Student12_log', + filename='F:\SQL\Student12_log.ldf', + size=10mb, + maxsize=50mb, + filegrowth=10% +) +use Student12 +go + +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null, +) + +--insert 数据 +insert into Class(ClassName) +select '软件1班' union +select '软件2班' union +select '软件3班' union +select '软件4班' union +select '软件5班' union +select '软件6班' union +select '软件7班' union +select '软件8班' union +select '软件9班' union +select '软件10班' + +--修改编号1 +update Class set ClassName='软件一班' where ClassID='1' + +--删除编号10 +delete Class where ClassID='10' + +select * from Class + +create table Student +( + StuId int primary key identity(1,1), + ClassId int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女'), + StuBirthday date , + StuPhone nvarchar(11) unique , + StuAddress nvarchar(200) +) + +set IDENTITY_INSERT Student ON +--插入数据 +insert into Student(StuId,ClassId,StuName,StuSex,StuBirthday,StuPhone,StuAddress) +select '10086', 1, '张三1', '男', '2020/5/6', 15168,'福建省' union +select '10087', 2, '张三2', '男', '2020/10/6', 15169,'福建省' union +select '10088', 3, '张三3', '男', '2021/3/6', 15170,'福建省' union +select '10089', 4, '张三4', '男', '2021/8/6', 15171,'福建省' union +select '10090', 5, '张三5', '男', '2022/1/6', 15172,'福建省' union +select '10091', 6, '张三6', '女', '2022/6/6', 15173,'福建省' union +select '10092', 7, '张三7', '女', '2022/11/6', 15174,'福建省' union +select '10093', 8, '张三8', '女', '2023/4/6', 15175,'福建省' union +select '10094', 9, '张三9', '女', '2023/9/6', 15176,'福建省' + +insert into Student(StuId,ClassId,StuName,StuSex,StuBirthday,StuPhone,StuAddress) +select '10095', 1, '张三1', '男', '2020/5/6', 15177,'福建省' union +select '10096', 2, '张三2', '男', '2020/10/6', 15178,'福建省' union +select '10097', 3, '张三3', '男', '2021/3/6', 15179,'福建省' union +select '10098', 4, '张三4', '男', '2021/8/6', 15180,'福建省' union +select '10099', 5, '张三5', '男', '2022/1/6', 15181,'福建省' union +select '10100', 6, '张三6', '女', '2022/6/6', 15182,'福建省' union +select '10101', 7, '张三7', '女', '2022/11/6', 15183,'福建省' union +select '10102', 8, '张三8', '女', '2023/4/6', 15184,'福建省' union +select '10103', 9, '张三9', '女', '2023/9/6', 15185,'福建省' union +select '10104', 9, '张三9', '女', '2023/9/6', 15186,'福建省' union +select '10105', 9, '张三9', '女', '2023/9/6', 15187,'福建省' + +select * from Student +--添加创建时间的字段 +alter table Student add CreateDate datetime default(getdate()) + +update Student set CreateDate='2021-03-10 18:21:42.417 ' + +--删除编号2 +delete Student where ClassId='2' + +set IDENTITY_INSERT Student OFF + +create table Course +( + CourseId int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int not null default(1) check(CourseCredit>=1 or CourseCredit<=5), + Classtypes nvarchar(10) check(Classtypes in('公共课' , '专业课')) +) + +insert into Course(CourseName) +select '数学' union +select '英语' union +select '体育' union +select '语文' union +select 'html' union +select 'java' + +--查看 +select * from Course + +--修改学分信息 +update Course set CourseCredit=2 where CourseName='java' + +create table Score +( + ScoreId int primary key identity(1,1), + StuId int foreign key references Student(StuId), + CourseId int foreign key references Class(ClassId), + Score decimal(5,2) unique not null +) + +insert into Score(Score) +select 60 union +select 61 union +select 62 union +select 63 union +select 64 union +select 65 union +select 66 union +select 67 union +select 68 union +select 69 union +select 70 union +select 71 union +select 72 union +select 73 union +select 74 union +select 75 union +select 76 union +select 77 union +select 78 union +select 79 + +select * from Score + +update Score set Score=100 where ScoreId=20 +delete Score where StuId=1 +delete Score where CourseId=1 +alter table Score add constraint DF default(0) for Score, check(Score>=0 or Score<=100) + diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery3.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..68bd83361bfa67318e1b269eb54a81550b206283 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\224\220\344\274\237\345\273\272/SQLQuery3.sql" @@ -0,0 +1,106 @@ +create database Student +on +( + fileName='D:\homework\Student.mdf', + Name='Student', + size=1MB, + Maxsize=5MB, + filegrowth=5MB +) + +log on +( + FileName='D:\homework\Student_log.ldf', + Name='Studnet_log', + size=1MB, + Maxsize=5MB, + Filegrowth=5MB +) +go + +use Student +go + +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null +) + +insert into Class (ClassName) +select '软件1班' union +select '软件2班' union +select '软件3班' union +select '软件4班' union +select '软件5班' union +select '软件6班' union +select '软件7班' union +select '软件8班' union +select '软件9班' union +select '软件十班' + +--update 表名 set 列名=*** where 列名=*** +update Class set ClassName='软件yi班' where ClassID=1 + +delete from Class where ClassID=10 + + +create table Student +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex in('男','女')), + StuBirthday datetime, + StuPhone nvarchar(11) unique, + StuAddress nvarchar(20), +) + +insert into Student (ClassID,StuName,StuSex,StuBirthday,StuPhone,StuAddress) +select 1,'吴星宇','男','2000-3-10',1231,'杜王町' union +select 1,'张会坡','男','2000-4-10',1232,'杜王町' union +select 2,'杨昌建','男','2000-5-10',1233,'杜王町' union +select 2,'郝振亚','男','2000-6-10',1234,'杜王町' union +select 3,'时秉翰','男','2000-7-10',1235,'杜王町' union +select 3,'阮建煌','男','2000-8-10',1236,'杜王町' union +select 4,'李接斌','男','2000-9-10',1237,'杜王町' union +select 4,'郭银俊','男','2000-10-10',1238,'杜王町' union +select 5,'陈苏萍','男','2000-11-10',1239,'杜王町' union +select 5,'尹少毅','男','2000-12-10',12310,'杜王町' union +select 6,'任鸿轩','男','2000-4-11',12311,'杜王町' union +select 6,'丁振宇','男','2000-4-12',12312,'杜王町' union +select 7,'李涵生','男','2000-4-13',12313,'杜王町' union +select 7,'马卓君','男','2000-4-14',12314,'杜王町' union +select 8,'付磊晶','男','2000-4-15',12315,'杜王町' union +select 8,'朱敏华','男','2000-4-16',12316,'杜王町' union +select 9,'徐雁军','男','2000-4-17',12317,'杜王町' union +select 9,'陈江超','男','2000-4-18',12318,'杜王町' union +select 9,'梁詩榮','男','2000-4-19',12319,'杜王町' union +select 9,'郭银俊','男','2000-4-20',12320,'杜王町' + +alter table Student add CreateDate datetime default(getdate()) + +update Student set CreateDate='2021-03-10 13:21:42.417' + +delete from Student where ClassID=2 + +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int default(1) check(CourseCredit>=1 or CourseCredit<=5) not null, + CourseType nvarchar(10) check(CourseType in('专业课','公共课')) +) + + +insert into Course (CourseName) +select '专业课' union +select '英语课' union +select '数学课' union +select '体育课' union +select '政治课' union +select '物理课' + +select * from Course + +update Course set CourseCredit=5 where CourseName='数学课' diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery3.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2220f4aa3a30f89dbeff43afb12fa22f2fe53ace --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\237\344\273\244\345\235\244/SQLQuery3.sql" @@ -0,0 +1,98 @@ +use master +create database Students +on primary +( + name = Students, + filename = 'D:\TEXT\Students.mdf', + size = 5MB, + maxsize = 20MB, + filegrowth = 1MB +) +log on +( + name = Students_log, + filename = 'D:\TEXT\Students_Log.ldf', + size = 1MB, + maxsize = 10MB, + filegrowth = 10% +) +go + +use Students +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique(ClassName) +) +go + +insert into Class(ClassName) values ('一班'),('二班'),('三班'),('四班'),('五班'), +('六班'),('七班'),('八班'),('九班'),('十班') +go + +update Class set ClassName = ('1班') where ClassID = 1 + +delete Class where ClassID = 10 + +use Students +create table Student +( + StuID int primary key identity(1,1), + ClassID int constraint FK_Class_ClassID references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) not null, + StuBirthday date, + StuPhone nvarchar(11) unique(StuPhone), + StuAddress nvarchar(200), +) +go + +insert into Students(StuName,StuSex,StuPhone,ClassID) +values +('张三','男','10248191092',1),('李四','男','10248191063',2),('王五','男','10248190691',1),('赵六','女','10248191091',3),('钱七','男','10248190784',4), +('小明','男','10248190801',9),('小红','女','10248190840',8),('小王','男','10248190919',7),('小赵','男','10248190876',6),('小李','男','10248191003',5), +('老王','女','10248190767',2),('老明','男','10248190756',3),('老阿姨','男','10248191072',4),('这啥名','男','10248190613',5),('我不知道','男','10248191129',6), +('编不下去了','男','10248190866',4),('真的','女','10248190626',5),('莓办法','男','10248191107',2),('稿不了','女','10248191054',3),('再见','男','10248190856',7) + +alter table Students add CreateDate datetime default(getdate()) + +delete Students where ClassID = 1 + +use Students +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar(50) unique(CourseName) not null, + CourseCredit int default(1) check(CourseCredit >= 1 and CourseCredit <=5), + CourseType nvarchar(10) Check(CourseType = '专业课' or CourseType = '文化课') +) +go + +insert into Course(CourseName) values ('高数'), +('英语'),('概论'),('职素'),('专业'),('体育') + +select * from Course + +update Course set CourseCredit = 2 where CourseName = '专业' + +use Students +create table Score +( + ScoreID int primary key identity(1,1), + StuID int constraint FK_Student_StuID references Student(StuID), + CourseID int constraint FK_Course_CourseID references Course(CourseID), + Score decimal(5,2) unique(Score) not null +) +go + +insert into Score(StuID,CourseID,Score) +values +(36,1,60.5),(39,1,59.0),(34,3,64.5),(33,1,78.0),(27,1,80.5), +(27,1,81.0),(27,1,56.0),(29,1,85.0),(24,1,90.5),(26,2,65.5), +(36,2,83.5),(42,2,84.0),(29,2,90.0),(30,3,89.2),(31,4,88.3), +(32,2,99.5),(33,2,66.0),(34,6,76.5),(35,5,67.0),(42,3,60.0) + +delete Score where StuID = 1 +delete Score where CourseID = 1 +alter table Score add constraint CK_Score_Score check(Score >= 0 and Score <= 100) +alter table Score add constraint DK_Score_Score default'0' for Score \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..33131181d2f85d2b27329a94759bfc426d012247 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\347\233\212\351\243\236/SQLQuery1\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.sql" @@ -0,0 +1,71 @@ +use master +go +create database student +on +( name='student', + fileName='D:\test\student.mdf' +) +log on +( name='student_log', + fileName='D:\test\student_log.ldf' +) +use student +go +create table class +( classld int primary key identity(1,1), + classname nvarchar(20) unique not null +) + +insert into class values('一班'),('二班'),('三班'),('四班'),('五班'),('六班'),('七班'),('八班'),('九班'),('十班') +select*from class +update class set classname='二班最帅' where classld=1 +delete from class where classld=10 +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +create table student +( stuld int primary key identity(1,1), + classld int, + constraint Fk_student_classld foreign key(classld) references class(classld), + stuname nvarchar(20) not null, + stusex nvarchar(1) default('男') check(StuSex='男' or StuSex='女'), + stubirthday date, + stuphone nvarchar(11) unique, + stuaddress nvarchar(200) +) +insert into student(classld,stuname,stusex,stubirthday,stuphone) values(1,'张1',default,'2021-03-08',1234),(2,'张2',default,'2021-03-08',2234),(3,'张3',default,'2021-03-08',3234),(4,'张4',default,'2021-03-08',4234),(5,'张5',default,'2021-03-08',5234),(6,'张6',default,'2021-03-08',6234),(7,'张7',default,'2021-03-08',7234),(8,'张8',default,'2021-03-08',8234),(9,'张9',default,'2021-03-08',9234) +alter table student add createdate datetime default(getdate()) +update student set stubirthday=getdate() +delete from student where classld>8 +select*from student +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +create table course +( courseld int primary key identity(1,1), + coursename nvarchar(50) unique not null, + coursescore int not null default(1), + coursecredit nvarchar(10) +) + +alter table course add constraint Ck_course_coursecredit check (coursecredit in('专业课','公共课')) +insert into course(coursename,coursecredit) select '物理','公共课'union select '数学','公共课'union select '化学','公共课'union select '英语','专业课'union select '语文','专业课'union select '专业','专业课' +update course set coursescore='100' where coursename ='专业' +select*from course +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +create table score +( scoreld int primary key identity(1,1), + stuld int , + constraint Fk_score_student foreign key(stuld) references student(stuld), + courseld int, + constraint Fk_score_course foreign key(courseld) references course(courseld), + score decimal(5,2) unique not null default(0) +) + +alter table score add constraint Ck_score_score check(score<=100) +insert into score(stuld,courseld,score) values(11,2,099.99),(12,3,099.98),(13,4,099.97),(14,5,099.96),(15,6,099.95),(16,7,default) +update score set score=100.00 where courseld=6 +delete from score where courseld=2 (之前表3课程信息表Course里CourseId没1的编号 只能删除2了) +select*from score + + + diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..932415cd7dcae5dad0b24296d7f077a8f4093682 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\345\272\206\346\211\215/SQLQuery1.sql" @@ -0,0 +1,95 @@ +use master +go +create database Students +use Students +go +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null +) + +insert into Class (ClassName) values ('一班'),('二班'),('三班'),('四班'),('五班'),('六班'),('七班'),('八班'),('九班'),('十班') +update Class set ClassName='一班多' where ClassID=1 +delete from Class where ClassID=10 + +create table Student +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女'), + StuBirthday date, + StuPhone nvarchar(11) unique not null, + StuAddress nvarchar(200), +) +insert into Student (ClassID,StuName,StuSex,Stubirthday,Stuphone) values +('1','我','男','20000101','12121212121'), +('1','我1','男','20000101','12121212122'), +('1','还2','男','20000101','12121212123'), +('1','我3','男','20000101','12121212125'), +('1','我4','男','20000101','12121212126'), +('1','我5','男','20000101','12121212127'), +('1','我6','男','20000101','12121212128'), +('1','我7','男','20000101','12121212129'), +('1','我8','男','20000101','12121212130'), +('1','我9','男','20000101','12121212131'), +('1','我10','男','20000101','12121212132'), +('1','我11','男','20000101','12121212133'), +('1','我12','男','20000101','12121212134'), +('1','我13','男','20000101','12121212135'), +('1','我14','男','20000101','12121842136'), +('1','我15','男','20000101','12121212137'), +('1','我16','男','20000101','12151212138'), +('1','我17','男','20000101','12121212139'), +('1','我18','男','20000101','12121212140'), +('1','我19','男','20000101','12121882141') + +alter table Student add CreaDate datetime default(getdate()) + +delete from Student where ClassID=1 + +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int default(1) check(1<=CourseCredit and CourseCredit<=5), + CourseType nvarchar(10) check(CourseType='专业课' or CourseType='公共课') +) +insert into Course (CourseName) values +('音乐'),('体育'),('思修'),('数学'),('美术'),('英语') +select CourseName from Course +update Course set CourseCredit=4 where CourseName='数学' + +create table Score +( + ScoreID int primary key identity(1,1), + StuID int foreign key references Student(StuID), + CourseID int foreign key references Course(CourseID), + Score decimal(5,2) unique not null +) +insert into Score (Score) values +('1'), +('2'), +('3'), +('4'), +('5'), +('6'), +('7'), +('8'), +('9'), +('10'), +('11'), +('12'), +('13'), +('14'), +('15'), +('16'), +('17'), +('18'), +('19'), +('20') +update Score set Score=4 where CourseID=3 +delete from Score where StuID=1 +delete from Score where CourseID=1 +alter table Score add constraint UK_Score_Score default(0) for Score,check(1=1 and CourseScore<=5 ), + CourseCredit nvarchar(10) check(CourseCredit='专业课' or CourseCredit='公共课') +) +create table Score +( + ScoreId int primary key identity(1,1), + StuId int foreign key references Student(StuId), + CourseId int foreign key references Course(CourseId), + Score decimal(5,2) unique not null +) +insert into Class(ClassName) values('软件1班'),('软件2班'),('软件3班'),('软件4班'),('软件5班'),('软件6班'),('软件7班'),('软件8班'),('软件9班'),('软件10班') + +update Class set ClassName =('软件1班') where ClassId =1 + +delete from Class where ClassId =10 + +select * from Class + +insert into Student(StuName,StuSex,StuPhone,ClassID) +values +('张三','男','10248191092',1),('李四','男','10248191063',2),('王五','男','10248190691',1),('赵六','女','10248191091',3),('钱七','男','10248190784',4), +('小明','男','10248190801',9),('小红','女','10248190840',8),('小王','男','10248190919',7),('小赵','男','10248190876',6),('小李','男','10248191003',5), +('老王','女','10248190767',2),('老明','男','10248190756',3),('老阿姨','男','10248191072',4),('这啥名','男','10248190613',5),('我不知道','男','10248191129',6), +('编不下去了','男','10248190866',4),('真的','女','10248190626',5),('莓办法','男','10248191107',2),('稿不了','女','10248191054',3),('再见','男','10248190856',7) + + +alter table Student add CreateDate datetime default(getdate()) + +update Student set CreateDate=(getdate()) + +delete Student where ClassId = 2 + +select*from Student + +insert into Course(CourseName) +values +('数学'),('英语'),('语文'), +('体育'),('政治'),('职素') + +select CourseName from Course + +update Course set CourseScore=5 where CourseName=('数学') + +select*from Student + +insert into Score(Score) +values +(1.0),(2.0),(3.0),(4.0),(5.0), +(12.0),(23.0),(84.0),(24.0),(25.0), +(31.0),(32.0),(33.0),(34.0),(35.0), +(41.0),(42.0),(43.0),(44.0),(45.0) +select * from Score + +delete Score where StuId=1 + +delete Score where CourseId=1 + +alter table Score add constraint DF default(0) for Score, check(Score>=0 or Score<=100) + + + + + diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery3.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a0b13ce521e627551fff2a321cf7b0c4caddcaf2 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\346\242\246\346\236\227/SQLQuery3.sql" @@ -0,0 +1,86 @@ +锘縰se mioer +go + +insert into mioer(StuName) values +('杞欢1鐝'), +('杞欢浜岀彮'), +('杞欢涓夌彮'), +('杞欢鍥涚彮'), +('杞欢浜旂彮'), +('杞欢鍏彮'), +('杞欢涓冪彮'), +('杞欢鍏彮'), +('杞欢涔濈彮'), +('杞欢鍗佺彮') + +update Stu set StuName = '杞欢涓鐝' where StuID = '1' + +delete from Class where StuID = '10' + +alter table Student add CreateDate datetime default(getdate()) + +insert into Student(StuID,StuName,StuSex,StuBirthday,StuPhone,StuAddress) values + (1,'涓鍙','鐢','2002-1-1',12345670001,'璐靛窞鍗'), + (2,'浜屽彿','濂','2002-1-2',12345670010,'璐靛窞瑗'), + (3,'涓夊彿','鐢','2002-1-17',12345670100,'璐靛窞甯'), + (4,'鍥涘彿','鐢','2002-4-16',12345671000,'璐靛窞甯'), + (5,'浜斿彿','濂','2002-1-15',12345671001,'璐靛窞甯'), + (6,'鍏彿','鐢','2002-1-1',12345671010,'璐靛窞甯'), + (7,'涓冨彿','鐢','2002-1-1',12345671100,'璐靛窞甯'), + (8,'鍏彿','鐢','2002-4-18',12345671101,'璐靛窞甯'), + (9,'涔濆彿','濂','2002-1-1',12345671110,'璐靛窞甯'), + (1,'鍗佸彿','鐢','2002-7-1',12345671111,'璐靛窞甯'), + (2,'澹瑰彿','鐢','2002-1-9',00001234567,'璐靛窞甯'), + (3,'璐板彿','鐢','2002-2-6',00011234567,'璐靛窞甯'), + (4,'鍙佸彿','濂','2002-3-4',00101234567,'璐靛窞甯'), + (5,'鑲嗗彿','鐢','2002-5-1',01001234567,'璐靛窞甯'), + (6,'浼嶅彿','鐢','2002-3-6',10001234567,'璐靛窞甯'), + (7,'闄嗗彿','鐢','2002-8-1',10011234567,'璐靛窞甯'), + (8,'鏌掑彿','鐢','2002-11-1',10101234567,'璐靛窞甯'), + (9,'鎹屽彿','濂','2002-12-1',11001234567,'璐靛窞甯'), + (1,'鐜栧彿','鐢','2002-9-1',11011234567,'璐靛窞甯'), + (2,'鎷惧彿','濂','2002-1-14',11101234567,'璐靛窞甯') + +update StuAge set CreateDate=getdate() + +delete from StuAge Where StuID='6' + +alter table Course add CourseType nvarchar(10) check(CourseType in ('涓撲笟璇','鍏叡璇')) + +insert into Course(StuName) values('璇枃'), +('鏁板'), +('鑻辫'), +('浣撹偛'), +('鍖栧'), +('鐗╃悊') + +select * from Course + +update Course set CoueseCredit=4 where StuName='鑻辫' + +insert into Score(StuId,CourseId,Score) values + (1,1,99), + (2,1,89), + (3,1,98.23), + (4,2,49), + (5,2,69), + (6,2,79), + (7,3,98), + (8,3,17), + (9,3,49.5), + (10,3,61), + (11,3,92), + (12,4,83), + (13,4,46), + (14,4,67), + (15,4,58), + (16,4,79.5), + (17,5,68), + (18,5,72), + (19,5,63), + (20,6,74) + +delete from IX_table_1 where StuID ='1' or CourseID='1' + +alter table Score add constraint DK_Score_Score default(0) +alter table Score add constraint CK_Score_Score check(Score>=0 and Score<=100) diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery3.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7519d343e7e61e486fdbcaaaee9ef18f5068dc42 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\346\266\233/SQLQuery3.sql" @@ -0,0 +1,155 @@ +create database Student12 +on +( + name='Student12', + filename='F:\SQL\Student12.mdf', + size=10mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='Student12_log', + filename='F:\SQL\Student12_log.ldf', + size=10mb, + maxsize=50mb, + filegrowth=10% +) +use Student12 +go + +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null, +) + +--insert 数据 +insert into Class(ClassName) +select '软件1班' union +select '软件2班' union +select '软件3班' union +select '软件4班' union +select '软件5班' union +select '软件6班' union +select '软件7班' union +select '软件8班' union +select '软件9班' union +select '软件10班' + +--修改编号1 +update Class set ClassName='软件一班' where ClassID='1' + +--删除编号10 +delete Class where ClassID='10' + +select * from Class + +create table Student +( + StuId int primary key identity(1,1), + ClassId int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女'), + StuBirthday date , + StuPhone nvarchar(11) unique , + StuAddress nvarchar(200) +) + +set IDENTITY_INSERT Student ON +--插入数据 +insert into Student(StuId,ClassId,StuName,StuSex,StuBirthday,StuPhone,StuAddress) +select '10086', 1, '张三1', '男', '2020/5/6', 15168,'福建省' union +select '10087', 2, '张三2', '男', '2020/10/6', 15169,'福建省' union +select '10088', 3, '张三3', '男', '2021/3/6', 15170,'福建省' union +select '10089', 4, '张三4', '男', '2021/8/6', 15171,'福建省' union +select '10090', 5, '张三5', '男', '2022/1/6', 15172,'福建省' union +select '10091', 6, '张三6', '女', '2022/6/6', 15173,'福建省' union +select '10092', 7, '张三7', '女', '2022/11/6', 15174,'福建省' union +select '10093', 8, '张三8', '女', '2023/4/6', 15175,'福建省' union +select '10094', 9, '张三9', '女', '2023/9/6', 15176,'福建省' + +insert into Student(StuId,ClassId,StuName,StuSex,StuBirthday,StuPhone,StuAddress) +select '10095', 1, '张三1', '男', '2020/5/6', 15177,'福建省' union +select '10096', 2, '张三2', '男', '2020/10/6', 15178,'福建省' union +select '10097', 3, '张三3', '男', '2021/3/6', 15179,'福建省' union +select '10098', 4, '张三4', '男', '2021/8/6', 15180,'福建省' union +select '10099', 5, '张三5', '男', '2022/1/6', 15181,'福建省' union +select '10100', 6, '张三6', '女', '2022/6/6', 15182,'福建省' union +select '10101', 7, '张三7', '女', '2022/11/6', 15183,'福建省' union +select '10102', 8, '张三8', '女', '2023/4/6', 15184,'福建省' union +select '10103', 9, '张三9', '女', '2023/9/6', 15185,'福建省' union +select '10104', 9, '张三9', '女', '2023/9/6', 15186,'福建省' union +select '10105', 9, '张三9', '女', '2023/9/6', 15187,'福建省' + +select * from Student +--添加创建时间的字段 +alter table Student add CreateDate datetime default(getdate()) + +update Student set CreateDate='2021-03-10 18:21:42.417 ' + +--删除编号2 +delete Student where ClassId='2' + +set IDENTITY_INSERT Student OFF + +create table Course +( + CourseId int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int not null default(1) check(CourseCredit>=1 or CourseCredit<=5), + Classtypes nvarchar(10) check(Classtypes in('公共课' , '专业课')) +) + +insert into Course(CourseName) +select '数学' union +select '英语' union +select '体育' union +select '语文' union +select 'html' union +select 'java' + +--查看 +select * from Course + +--修改学分信息 +update Course set CourseCredit=2 where CourseName='java' + +create table Score +( + ScoreId int primary key identity(1,1), + StuId int foreign key references Student(StuId), + CourseId int foreign key references Class(ClassId), + Score decimal(5,2) unique not null +) + +insert into Score(Score) +select 60 union +select 61 union +select 62 union +select 63 union +select 64 union +select 65 union +select 66 union +select 67 union +select 68 union +select 69 union +select 70 union +select 71 union +select 72 union +select 73 union +select 74 union +select 75 union +select 76 union +select 77 union +select 78 union +select 79 + +select * from Score + +update Score set Score=100 where ScoreId=20 +delete Score where StuId=1 +delete Score where CourseId=1 +alter table Score add constraint DF default(0) for Score, check(Score>=0 or Score<=100) + diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6087b722e1db02ac0acb4d6f688274e254b3282b --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\237\257\346\226\207\351\276\231/SQLQuery1.sql" @@ -0,0 +1,114 @@ +create database Student + +go + +use Student + +go + +create table Class( + ClassId int primary key identity (1,1), + ClassName nvarchar(20) constraint unique_1 unique(ClassName) not null + --设置唯一约束(constraint(约束)+约束名(自己可辨识就可以)+unique(列名)) +) + +go + +insert into Class(ClassName) values ('软件一班'),('软件二班'),('软件三班'),('软件四班'),('软件五班'), + ('软件六班'),('软件七班'),('软件八班'),('软件九班'),('软件十班') + +go +--数据增删改查部分只能逐一执行 +select * from Class order by ClassId asc +--查询数据是否插入成功,并升序排序(asc),每次执行完操作,都可查询是否成功 + +-- 更新语法: update 表名 set 列名1=新数据1,列名2=新数据2列名3=新数据3, where 查询条件 +update Class set ClassName='软件1班' where ClassId=1 + +--删除语法: delete from 表名where 查询条件; +delete from Class where ClassId=10 + + +create table Students( + StuId int primary key identity (1,1), + ClassId int constraint [Fk_Students_ClassId] foreign key([ClassId]) references [Class]([ClassId]) not null, + --建表时创建外键关联约束(constraint +[约束名]+foreign key +([关联外键名])+references [外键表]([外键中的主键])) + StuName nvarchar(20) not null, + StuSex nvarchar(1) default'男'check (StuSex in('男','女')), + StuBirthday date null, --date为时间专用字段类型 + StuPhone nvarchar(11) constraint unique_2 unique(StuPhone) null, + StuAddress nvarchar(20) null, + + + ) + + go + + + + select * from Students + + insert into Students(ClassId,StuName,StuSex,StuBirthday,StuPhone) values + (1,'小胡','女','2002-09-16',1235544445),(1,'小陈','女','2001-09-11',12345345),(3,'小刘','男','2001-08-16',123445), + (2,'小王','男','2000-09-16',1234566),(4,'小胡','女','2002-09-16',124545345),(8,'小陆','女','2001-06-16',112345), + (9,'小胡','男','2000-03-19',123345),(2,'小李','男','2000-01-16',122345),(4,'小花','女','2001-01-11',1422345), + (6,'小王','男','2000-01-16',1234125),(7,'小华','女','2001-09-16',12324245),(5,'小楚','女','2001-09-19',14562345), + (1,'小赖','男','2000-06-19',12345235),(4,'小段','男','2000-01-16',1233523545),(2,'小柯','男','2002-01-16',12323523545), + (8,'小施','女','2002-09-16',1234524532),(6,'小易','男','2000-09-16',1234525),(7,'小罗','男','2002-09-16',122532345), + (5,'小许','男','2000-09-16',12325345),(2,'小徐','女','2002-09-16',123425235) + --插入数据,由于有唯一约束影响,所以手机列数据必须不同 + + alter table Students add CreateDate datetime default (getdate()) + --插入列CreateDate:alter table 表名 add 列名 列名类型 默认值 getdate()--》系统时间 + + update Students set CreateDate=getdate() + --此条直接更新表里CreateDate中所有数据 + + delete from Students where ClassId=1 + --删除1班所有学生信息 + + --每次执行完成,最好进行一次数据查询,好确定是否成功执行 + + +create table Course( + CourseId int primary key identity (1,1), + CourseName nvarchar(50) constraint unique_3 unique(CourseName) not null, + CourseCredit int default 1 check (CourseCredit>=1 and CourseCredit<=5), + + --default 为默认值 check括号内为取值范围,该列的取值范围表达为列名>=x and 列名<=y + CourseCredits nvarchar(10) check(CourseCredits in('专业课','公共课')) +) + +go + +insert into Course (CourseName,CourseCredits) values +('Java','专业课'),('Html5','专业课'),('英语','公共课'), +('思修','公共课'),('SQL','专业课'),('体育','公共课') + +select * from Course + +update Course set CourseCredit=4 where CourseName='Java' + + +create table Score( + ScoreId int primary key identity (1,1), + StuId int constraint [Fk_Score_StuId] foreign key([StuId]) references [Students]([StuId]) not null, + CourseId int constraint [Fk_Score_CourseId] foreign key([CourseId]) references [Course]([CourseId]) not null, + Score decimal(5,2) constraint unique_4 unique(Score) not null + +) + +insert into Score(StuId,CourseId,Score)values + + (1,3,52.8),(2,4,95.5),(3,5,90),(4,2,10),(5,6,15),(6,5,20),(7,4,25), +(8,3,30),(9,1,35),(10,5,80),(11,4,70),(12,2,41),(13,1,5), +(14,2,75),(15,5,100),(16,4,69),(17,3,60.5),(18,3,70.3), +(19,6,80.6),(20,6,91) + +delete from Score where StuId=1 + +delete from Score where CourseId=1 + +alter table Score add constraint CK_Score check (Score between 0 and 100) + +alter table Score add constraint DF_Score default 0 for Score \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery3.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ccdbd6fc4defade8c3717d7a0487055b3e9a5609 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\265\345\227\243\345\207\257/SQLQuery3.sql" @@ -0,0 +1,136 @@ +use master--1 +go--2 +--3 +create database Student04--4 +on(--5 + name='Student04_mdf', + filename='D:\SQL代码\Student04.mdf', + size=3MB, + maxsize=50MB, + filegrowth=10% +)--11 +log on(--12 + name='Student04_ldf', + filename='D:\SQL代码\Student04_ldf.ldf', + size=3MB, + maxsize=50MB, + filegrowth=10% +)--18 +go + +use Student04 +go + +create table Class--24 +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null +)--28 +insert into Class(ClassName) select ('软件1班') union all +select ('软件2班') union all +select ('软件3班') union all +select ('软件4班') union all +select ('软件5班') union all +select ('软件6班') union all +select ('软件7班') union all +select ('软件8班') union all +select ('软件9班') union all +select ('软件10班') +--39 +select * from Class +update Class set ClassName='软件11班' where ClassID=1 +delete Class where ClassID=10 + +create table Student--43 +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex in('男','女')), + StuBirthday date , + StuPhone nvarchar(11) unique , + StuAddress nvarchar(200) , + +)--53 +insert into Student (ClassID,StuName, StuSex, StuBirthday,StuPhone,StuAddress) +values (1, '张三' ,'男','2002-05-24','153453557','江西南昌') , +(2,'李四','男','2002-11-25','153053554','江西九江') + insert into Student (ClassID,StuName, StuSex, StuBirthday,StuPhone,StuAddress) + values(3, '张三' ,'男','2002-04-24','153442557','江西南昌') , +(4,'张六','男','2002-05-27','1535355469','江西抚州') , + (5,'张七','男','2002-05-28','15345355417','江西新余') , +(6,'张八','女','2002-05-29','15345355427','江西南昌') , + (7,'张九','男','2002-06-01','15345355437','江西南昌'), + (8,'张十','男','2002-05-14','1534535477','江西九江'), + (9,'李七','女','2002-05-04','1534535447','江西南昌'), + (1,'李五','女','2002-05-13','1234035377','江西南昌') , +(1,'陈六','男','2002-06-24','15745356747','江西南昌') , +(2,'陈七','男','2002-08-25','14584535466','江西九江'), + (3,'程八','女','2002-07-26','1574554648','江西赣州') , +(4,'程九','男','2002-04-27','1546554679','江西抚州') , +(5,'程十','男','2002-03-28','21345355461','江西新余') , +(6,'王一','女','2002-02-29','3453546207','江西南昌') , +(7,'王九','男','2002-01-01','1534564637','江西南昌') , +(8,'王十','男','2002-11-14','15457554577','江西九江'), +(9,'王四','女','2002-12-04','1545654477','江西南昌') , +(2,'王五','女','2002-05-01','1534535477','江西南昌') + +delete Student where ClassID=1 +alter table Student add CreateDate datetime default(getdate()) +update Student set CreateDate='2021-03-11 13:21:42.417' +select * from Student + +create table Course +( + Courseld int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int not null default(1) check(CourseCredit>=1 and CourseCredit<=5), + CourseType nvarchar(10) check(CourseType in + ('专业课' , '公共课')) +) + +insert into Course(CourseName) +select '数学' union +select '英语' union +select '体育' union +select '语文' union +select 'html' union +select 'java' + +select * from Course +update Course set CourseCredit=5 where CourseName='java' + +create table Score +( + Scoreld int primary key identity(1,1), + StuID int foreign key references Student(StuID), + Courseld int foreign key references Course(Courseld), + Score decimal(5,2) unique not null +) +insert into Score(Score) +select 88 union +select 61 union +select 92 union +select 63 union +select 74 union +select 65 union +select 66 union +select 57 union +select 68 union +select 69 union +select 70 union +select 71 union +select 72 union +select 63 union +select 74 union +select 75 union +select 86 union +select 77 union +select 58 union +select 79 + +select * from Score +update Course set CourseCredit=5 where CourseName='英语' +delete Score where StuId=1 +delete Score where Courseld=1 +alter table Score add constraint DF default(0) for Score, check(Score>=0 or Score<=100) diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1985fc0e78d5f1eeed31902ab057385e1b8470e5 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\273\225\351\221\253/SQLQuery1.sql" @@ -0,0 +1,70 @@ +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 Class +( + ClassId int primary key identity(1,1) not null, + ClassName nvarchar(20) unique not null +) +create table Student +( + StuId int primary key identity(1,1) not null, + ClassId int foreign key references Class(ClassId), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check (StuSex='男' or StuSex='女'), + StuBirthday date null, + StuPhone nvarchar(11) unique null, + StuAddress nvarchar(200) null, +) +create table Course +( + CourseId int primary key identity(1,1) not null, + CourseName nvarchar(50) unique not null, + CourseCredit int default(1) check (CourseCredit>1 and CourseCredit<6) not null, + CourseType nvarchar(10) check(CourseType='专业课' or CourseType='公共课') +) +create table Score +( + ScoreId int primary key identity(1,1) not null, + StuId int foreign key references Student(StuId), + CourseId int foreign key references Course(CourseId), + Score decimal(5,2) unique not null +) +insert into Class values ('软件1班'),('软件2班'),('软件3班'),('软件4班'),('软件5班'),('软件6班'),('软件7班'),('软件8班'),('软件9班'),('软件10班') +update Class set ClassName='应用1班' where ClassId=1 +delete from Class where ClassId=10 + +insert into Student(ClassId,StuName,StuSex,StuBirthday,StuPhone,StuAddress) values(1,'张一','男',2002-1-18,101,'龙头'),(2,'张二','男',2002-2-18,102,'龙头'),(3,'张三','男',2002-3-18,103,'龙头'),(4,'张四','男',2002-4-18,104,'龙头'),(5,'张五','男',2002-5-18,105,'龙头'), +(6,'张六','男',2002-6-18,106,'龙头'),(7,'张七','男',2002-7-18,107,'龙头'),(8,'张八','男',2002-8-18,108,'龙头'),(9,'张九','男',2002-9-18,109,'龙头'),(10,'张十','男',2002-10-18,110,'龙头'),(11,'张十一','男',2002-11-18,111,'龙头'),(12,'张十二','男',2002-12-18,112,'龙头'),(13,'张十三','男',2002-1-19,113,'龙头'), +(14,'张十四','男',2002-2-19,114,'龙头'),(15,'张十五','男',2002-3-19,115,'龙头'),(16,'张十六','男',2002-4-19,116,'龙头'),(17,'张十七','男',2002-5-19,117,'龙头'),(18,'张十八','男',2002-6-19,118,'龙头'),(19,'张十九','男',2002-7-19,119,'龙头'),(20,'张二十','男',2002-8-19,120,'龙头') +alter table Student add CreateDate datetime default(getdate()) +update Student set CreateDate=getdate() +delete from Student where ClassId=5 + +insert into Course(CourseName) values('语文'),('数学'),('英语'),('音乐'),('体育'),('专业') +select * from Course +update Course set CourseId=4 where CourseName='思修' + +insert into Score(StuId,CourseId,Score) values (1,50,1.1),(2,50,1.1),(3,50,1.1),(4,50,1.1),(5,50,1.1),(6,50,1.1),(7,50,1.1),(8,50,1.1), +(9,50,1.1),(10,50,1.1),(11,50,1.1),(12,50,1.1),(13,50,1.1),(14,50,1.1),(15,50,1.1),(16,50,1.1),(17,50,1.1),(18,50,1.1),(19,50,1.1),(20,50,1.1) +update Score set Score=70.1 where CourseId=5 +delete from Score where StuId=1 +delete from Score where CourseId=1 +alter table Score add constraint FK_Score_Score default(0)for Score, check (Score>=0 and Score<=100) \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery3.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2e671d75d47a06cfc2396197be03de34688050ac --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\345\256\207/SQLQuery3.sql" @@ -0,0 +1,71 @@ +use mioer +go + +insert into mioer(StuName) values ('软件1班'),('软件二班'),('软件三班'),('软件四班'),('软件五班'),('软件六班'),('软件七班'),('软件八班'),('软件九班'),('软件十班') + +update Stu set StuName = '软件一班' where StuID = '1' + +delete from Class where StuID = '10' + +alter table Student add CreateDate datetime default(getdate()) + +insert into Student(StuID,StuName,StuSex,StuBirthday,StuPhone,StuAddress) values + (1,'一号','男','2002-1-1',12345670001,'bejing'), + (2,'二号','女','2002-1-2',12345670010,'tianjin'), + (3,'三号','男','2002-1-17',12345670100,'shanghai'), + (4,'四号','男','2002-4-16',12345671000,'bejing'), + (5,'五号','女','2002-1-15',12345671001,'fujian'), + (6,'六号','男','2002-1-1',12345671010,'guangdong'), + (7,'七号','男','2002-1-1',12345671100,'guangxi'), + (8,'八号','男','2002-4-18',12345671101,'jiangxi'), + (9,'九号','女','2002-1-1',12345671110,'bejing'), + (1,'十号','男','2002-7-1',12345671111,'shanxi'), + (2,'壹号','男','2002-1-9',00001234567,'bejing'), + (3,'贰号','男','2002-2-6',00011234567,'taiwan'), + (4,'叁号','女','2002-3-4',00101234567,'bejing'), + (5,'肆号','男','2002-5-1',01001234567,'bejing'), + (6,'伍号','男','2002-3-6',10001234567,'hunan'), + (7,'陆号','男','2002-8-1',10011234567,'bejing'), + (8,'柒号','男','2002-11-1',10101234567,'henan'), + (9,'捌号','女','2002-12-1',11001234567,'bejing'), + (1,'玖号','男','2002-9-1',11011234567,'bejing'), + (2,'拾号','女','2002-1-14',11101234567,'hebe') + +update StuAge set CreateDate=getdate() + +delete from StuAge Where StuID='6' + +alter table Course add CourseType nvarchar(10) check(CourseType in ('专业课','公共课')) + +insert into Course(StuName) values('语文'),('数学'),('英语'),('体育'),('化学'),('物理') + +select * from Course + +update Course set CoueseCredit=4 where StuName='英语' + +insert into Score(StuId,CourseId,Score) values + (1,1,99), + (2,1,89), + (3,1,98.23), + (4,2,49), + (5,2,69), + (6,2,79), + (7,3,98), + (8,3,17), + (9,3,49.5), + (10,3,61), + (11,3,92), + (12,4,83), + (13,4,46), + (14,4,67), + (15,4,58), + (16,4,79.5), + (17,5,68), + (18,5,72), + (19,5,63), + (20,6,74) + +delete from IX_table_1 where StuID ='1' or CourseID='1' + +alter table Score add constraint DK_Score_Score default(0) +alter table Score add constraint CK_Score_Score check(Score>=0 and Score<=100) \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2ca37ee61822b7d4bbc8f7a7a484a33454c80d16 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\344\275\263\346\226\207/SQLQuery1.sql" @@ -0,0 +1,71 @@ +use master +go +create database Students +on( + name='Students', + filename='C:\TEXT\Students.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) +log on( + name='Students_log', + filename='C:\TEXT\Students_log.mdf', + size=5MB, + maxsize=100MB, + filegrowth=10% +) +go +use Students +go +create table Class +( + ClassID int primary key , + ClassName nvarchar(20) unique not null, +) +create table Student +( + StuID int primary key identity(1,1) not null , + ClassID int references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女'), + StuBirthday date, + StuAddress nvarchar(200), + CreateDate datetime default(getdate()) + +) +create table Course +( + CourseID int primary key identity not null, + CourseName nvarchar(50) unique not null, + CourseCredit int default(1) check(CourseCredit=1 or CourseCredit=2 or CourseCredit=3 or CourseCredit=4 or CourseCredit=5 ) not null, + CourseTypes nvarchar(10) check(CourseTypes='公共课' or CourseTypes='专业课 '), +) +create table Score +( + ScoreID int primary key identity not null, + StuID int references Student(StuID) , + CourseID int references Class(ClassID), + Score decimal(5,2) unique not null, +) +use students +go +select * from Class +insert into Class values(1, '软件一班'),(2, '软件二班'),(3, '软件三班'),(4, '软件四班'),(5, '软件五班'),(6, '软件六班'),(7, '软件七班'),(8, '软件八班'),(9, '软件九班'),(10, '软件十班') +update Class set ClassName='软件十一班'where ClassID=1 +delete from Class where ClassID=10 + +select * from Student +insert into Student(ClassID,StuName,StuSex,StuBirthday,StuAddress) values(1,'张三','男','2002-2-2','福建省'),(2,'李四','女','2002-3-2','福建省'),(3,'张二三','女','2002-2-12','福建省'),(4,'王五','男','2002-2-2','福建省'),(5,'王小二','男','2002-2-2','福建省'),(6,'张大三','男','2002-2-2','福建省'),(7,'张小四','女','2002-2-2','福建省'),(8,'张三三','女','2002-2-2','福建省'),(1,'王二虎','男','2002-2-2','福建省'),(1,'张二蛋','男','2002-2-2','福建省'),(2,'李二四','女','2002-2-2','福建省'),(4,'张四三','女','2002-2-2','福建省'),(1,'张三','男','2002-2-2','福建省'),(5,'李四','女','2002-2-2','福建省'),(8,'张三','女','2002-2-2','福建省'),(6,'张三','男','2002-2-2','福建省'),(9,'王三','男','2002-2-2','福建省'),(1,'王五','女','2002-2-2','福建省'),(9,'王三','女','2002-2-2','福建省'),(4,'小唐','男','2002-2-2','福建省') +update Student set CreateDate=getdate() +delete from Student where ClassID=1 + +select * from Course +insert into Course(CourseName) values('语文'),('数学'),('英语'),('物理'),('化学'),('生物') +update Course set CourseCredit=2 where CourseName='语文' + +select * from Score +alter table Score add constraint DK_Score_Score default(0) for Score +insert into Score(StuID,CourseID,Score) values(2,1,90),(3,2,55),(4,1,25),(5,1,40),(6,5,70),(7,4,30),(8,1,100),(8,5,91),(7,3,67),(11,1,78),(12,1,60),(14,3,86),(15,1,66),(16,6,50),(17,2,88),(19,4,69),(20,4,98),(2,1,92),(3,1,96),(4,1,95) +delete from Score where StuID=1 +delete from Score where CourseID=1 diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery3.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ab8e6bb9a571bed71a3e4fee4e78d838cfee926c --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\347\275\227\345\256\207\346\226\260/SQLQuery3.sql" @@ -0,0 +1,120 @@ +use master +go + +create database Students +on +( + name='Students', + filename='D:\sql数据库\1\Students.mdf', + size=10MB, + maxsize=100MB, + filegrowth=10Mb +) +log on +( + name='Students_log', + filename='D:\sql数据库\1\Students_log.ldf', + size=10MB, + maxsize=100MB, + filegrowth=10Mb +) +go + +use Students +go + +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null +) + +insert into Class (ClassName) values ('一班'),('二班'),('三班'),('四班'),('五班'),('六班'),('七班'),('八班'),('九班'),('十班') +update Class set ClassName='一班多' where ClassID=1 +delete from Class where ClassID=10 + +create table Student +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女'), + StuBirthday date, + StuPhone nvarchar(11) unique not null, + StuAddress nvarchar(200), +) +insert into Student (ClassID,StuName,StuSex,Stubirthday,Stuphone) values +('1','我还','男','20000101','12121212121'), +('1','我还1','男','20000101','12121212122'), +('1','我还2','男','20000101','12121212123'), +('1','我还3','男','20000101','12121212125'), +('1','我还4','男','20000101','12121212126'), +('1','我还5','男','20000101','12121212127'), +('1','我还6','男','20000101','12121212128'), +('1','我还7','男','20000101','12121212129'), +('1','我还8','男','20000101','12121212130'), +('1','我还9','男','20000101','12121212131'), +('1','我还0','男','20000101','12121212132'), +('1','我还11','男','20000101','12121212133'), +('1','我还12','男','20000101','12121212134'), +('1','我还13','男','20000101','12121212135'), +('1','我还14','男','20000101','12121212136'), +('1','我还15','男','20000101','12121212137'), +('1','我还16','男','20000101','12121212138'), +('1','我还17','男','20000101','12121212139'), +('1','我还18','男','20000101','12121212140'), +('2','我还19','男','20000101','12121212141') + +alter table Student add CreaDate datetime default(getdate()) +update Student set CreaDate='2021/3/10 10:32' +delete from Student where ClassID=1 + +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int default(1) check(1<=CourseCredit and CourseCredit<=5), + CourseType nvarchar(10) check(CourseType='专业课' or CourseType='公共课') +) +insert into Course (CourseName) values +('音乐'), +('思修'), +('美术'), +('体育'), +('数学'), +('英语') +select CourseName from Course +update Course set CourseCredit=4 where CourseName='数学' + +create table Score +( + ScoreID int primary key identity(1,1), + StuID int foreign key references Student(StuID), + CourseID int foreign key references Course(CourseID), + Score decimal(5,2) unique not null +) +insert into Score (Score) values +('1'), +('52'), +('53'), +('54'), +('55'), +('56'), +('57'), +('58'), +('59'), +('60'), +('70'), +('80'), +('90'), +('10'), +('20'), +('30'), +('40'), +('45'), +('66'), +('77') +update Score set Score=4 where CourseID=3 +delete from Score where StuID=1 +delete from Score where CourseID=1 +alter table Score add constraint UK_Score_Score default(0) for Score,check(1=1 and CourseCredit<=5), + + --default 为默认值 check括号内为取值范围,该列的取值范围表达为列名>=x and 列名<=y + CourseCredits nvarchar(10) check(CourseCredits in('专业课','公共课')) +) + +go + +insert into Course (CourseName,CourseCredits) values +('Java','专业课'),('Html5','专业课'),('英语','公共课'), +('思修','公共课'),('SQL','专业课'),('体育','公共课') + +select * from Course + +update Course set CourseCredit=4 where CourseName='Java' + + +create table Score( + ScoreId int primary key identity (1,1), + StuId int constraint [Fk_Score_StuId] foreign key([StuId]) references [Students]([StuId]) not null, + CourseId int constraint [Fk_Score_CourseId] foreign key([CourseId]) references [Course]([CourseId]) not null, + Score decimal(5,2) constraint unique_4 unique(Score) not null + +) + +insert into Score(StuId,CourseId,Score)values + + (1,3,52.8),(2,4,95.5),(3,5,90),(4,2,10),(5,6,15),(6,5,20),(7,4,25), +(8,3,30),(9,1,35),(10,5,80),(11,4,70),(12,2,41),(13,1,5), +(14,2,75),(15,5,100),(16,4,69),(17,3,60.5),(18,3,70.3), +(19,6,80.6),(20,6,91) + +delete from Score where StuId=1 + +delete from Score where CourseId=1 + +alter table Score add constraint CK_Score check (Score between 0 and 100) + +alter table Score add constraint DF_Score default 0 for Score \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..aa7a01cc52662e9c2909af56911e91495e20cc3f --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\350\222\262\346\231\223\351\252\217/SQLQuery1.sql" @@ -0,0 +1,136 @@ +use master--1 +go--2 +--3 +create database Student04--4 +on(--5 + name='Student04_mdf', + filename='D:\SQL代码\Student04.mdf', + size=3MB, + maxsize=50MB, + filegrowth=10% +)--11 +log on(--12 + name='Student04_ldf', + filename='D:\SQL代码\Student04_ldf.ldf', + size=3MB, + maxsize=50MB, + filegrowth=10% +)--18 +go + +use Student04 +go + +create table Class--24 +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null +)--28 +insert into Class(ClassName) select ('软件1班') union all +select ('软件2班') union all +select ('软件3班') union all +select ('软件4班') union all +select ('软件5班') union all +select ('软件6班') union all +select ('软件7班') union all +select ('软件8班') union all +select ('软件9班') union all +select ('软件10班') +--39 +select * from Class +update Class set ClassName='软件11班' where ClassID=1 +delete Class where ClassID=10 + +create table Student--43 +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex in('男','女')), + StuBirthday date , + StuPhone nvarchar(11) unique , + StuAddress nvarchar(200) , + +)--53 +insert into Student (ClassID,StuName, StuSex, StuBirthday,StuPhone,StuAddress) +values (1, '张三' ,'男','2002-05-24','153453557','江西南昌') , +(2,'李四','男','2002-11-25','153053554','江西九江') + insert into Student (ClassID,StuName, StuSex, StuBirthday,StuPhone,StuAddress) + values(3, '张三' ,'男','2002-04-24','153442557','江西南昌') , +(4,'张六','男','2002-05-27','1535355469','江西抚州') , + (5,'张七','男','2002-05-28','15345355417','江西新余') , +(6,'张八','女','2002-05-29','15345355427','江西南昌') , + (7,'张九','男','2002-06-01','15345355437','江西南昌'), + (8,'张十','男','2002-05-14','1534535477','江西九江'), + (9,'李七','女','2002-05-04','1534535447','江西南昌'), + (1,'李五','女','2002-05-13','1234035377','江西南昌') , +(1,'陈六','男','2002-06-24','15745356747','江西南昌') , +(2,'陈七','男','2002-08-25','14584535466','江西九江'), + (3,'程八','女','2002-07-26','1574554648','江西赣州') , +(4,'程九','男','2002-04-27','1546554679','江西抚州') , +(5,'程十','男','2002-03-28','21345355461','江西新余') , +(6,'王一','女','2002-02-29','3453546207','江西南昌') , +(7,'王九','男','2002-01-01','1534564637','江西南昌') , +(8,'王十','男','2002-11-14','15457554577','江西九江'), +(9,'王四','女','2002-12-04','1545654477','江西南昌') , +(2,'王五','女','2002-05-01','1534535477','江西南昌') + +delete Student where ClassID=1 +alter table Student add CreateDate datetime default(getdate()) +update Student set CreateDate='2021-03-11 13:21:42.417' +select * from Student + +create table Course +( + Courseld int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int not null default(1) check(CourseCredit>=1 and CourseCredit<=5), + CourseType nvarchar(10) check(CourseType in + ('专业课' , '公共课')) +) + +insert into Course(CourseName) +select '数学' union +select '英语' union +select '体育' union +select '语文' union +select 'html' union +select 'java' + +select * from Course +update Course set CourseCredit=5 where CourseName='java' + +create table Score +( + Scoreld int primary key identity(1,1), + StuID int foreign key references Student(StuID), + Courseld int foreign key references Course(Courseld), + Score decimal(5,2) unique not null +) +insert into Score(Score) +select 88 union +select 61 union +select 92 union +select 63 union +select 74 union +select 65 union +select 66 union +select 57 union +select 68 union +select 69 union +select 70 union +select 71 union +select 72 union +select 63 union +select 74 union +select 75 union +select 86 union +select 77 union +select 58 union +select 79 + +select * from Score +update Course set CourseCredit=5 where CourseName='英语' +delete Score where StuId=1 +delete Score where Courseld=1 +alter table Score add constraint DF default(0) for Score, check(Score>=0 or Score<=100) \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..35d8fa8890a9a720532613e241be18887c56713d --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\351\233\252\345\274\272/SQLQuery1.sql" @@ -0,0 +1,68 @@ +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 Class +( +ClassId int primary key identity(1,1), +ClassName nvarchar(20) unique(ClassName) not null, +) +insert into Class(ClassName) values('一班'),('二班'),('三班'),('四班'),('五班'),('六班'),('七班'),('八班'),('九班'),('十班') +update Class set ClassName = ('1班') where ClassID = 1 +delete Class where ClassID = 10 +create table Student +( +StuId int primary key identity(1,1), +ClassId int constraint FK_Class_ClassId references Class(ClassId), +StuName nvarchar(20) not null, +StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女'), +StuBirthday date, +StuPhone nvarchar(11) unique, +StuAddress nvarchar(200) +) +insert into Student(StuName,StuSex,StuBirthday,StuPhone,StuAddress) values('狗1','男','2020/1/1','100001','狗窝'),('狗2','男','2020/1/1','100002','狗窝'),('狗3','男','2020/1/1','100200','狗窝'),('狗4','男','2020/1/1','10200','狗窝'),('狗5','男','2020/1/1','10123000','狗窝'),('狗6','男','2020/1/1','102200','狗窝'),('狗7','男','2020/1/1','130000','狗窝'),('狗8','男','2020/1/1','1004300','狗窝'),('狗9','男','2020/1/1','1002100','狗窝'),('狗10','男','2020/1/1','10546000','狗窝') +alter table Student add CreateDate datetime default(getdate()) +update Student set CreateDate='2021/3/11' +delete Student where ClassId='10' +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar(50) unique(CourseName) not null, + CourseCredit int default(1) check(CourseCredit >= 1 and CourseCredit <=5), + CourseType nvarchar(10) Check(CourseType = '专业课' or CourseType = '文化课') +) +insert into Course(CourseName) values ('数学'), +('英语'),('语文'),('体育'),('音乐'),('美术') +select * from Course +update Course set CourseCredit = 2 where CourseName = '体育' +create table Score +( + ScoreID int primary key identity(1,1), + StuID int constraint FK_Student_StuID references Student(StuID), + CourseID int constraint FK_Course_CourseID references Course(CourseID), + Score decimal(5,2) unique(Score) not null +) +insert into Score(StuID,CourseID,Score) values (1,1,1),(1,1,2),(1,1,3),(1,1,4),(1,1,5),(1,1,6) +delete Score where StuID = 1 +delete Score where CourseID = 1 +alter table Score add constraint CK_Score_Score check(Score >= 0 and Score <= 100) +alter table Score add constraint DK_Score_Score default'0' for Score \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye02.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye02.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e52ca306a0bd2f42f187fa9c3d522a7816510e0a --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\221\351\202\265\346\230\240/zuoye02.sql" @@ -0,0 +1,90 @@ +use master +go + +create database zuoye02 +on +( +name='zuoye02', +filename='D:\test\zuoye02.mdf', +size=5MB, +maxsize=50MB, +filegrowth=10% +) +log on +( +name='zuoye02'_log, +filename='D:\test\zuoye02_log.ldf', +size=5MB, +maxsize=50MB, +filegrowth=10% +) +go +use zuoye02 +create table Class +( +ClassID int primary key identity (1,1), +ClassName nvarchar(20) unique not null +) +insert into Class (ClassName) values ('一班'),('二班'),('三班'),('四班'),('五班'),('六班'),('七班'),('八班'),('九班'),('十班') +update Class set ClassName='十一班' where ClassID=1 +delete from Class where ClassID=10 +go +use zuoye02 +create table Student +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex in('男','女')), + StuBirthday date , + StuPhone nvarchar(11) unique , + StuAddress nvarchar(200) , + +) +insert into Student (ClassID,StuName, StuSex, StuBirthday,StuPhone,StuAddress) +values (1, '张' ,'男','2002-05-24','153453557','江西南昌') , +(2,'李','男','2002-11-25','153053554','江西'), + (3, '三' ,'男','2002-04-24','153442557','江西') , +(4,'六','男','2002-05-27','1535355469','江西') , + (5,'七','男','2002-05-28','15345355417','江西') , +(6,'八','女','2002-05-29','15345355427','江西') , + (7,'九','男','2002-06-01','15345355437','江西') + +delete Student where ClassID=1 +alter table Student add CreateDate datetime default(getdate()) + +select * from Student +go +use zuoye02 + +create table Course +( + Courseld int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int not null default(1) check(CourseCredit>=1 and CourseCredit<=5), + CourseType nvarchar(10) check(CourseType in + ('专业课' , '公共课')) +) +insert into Course (CourseName) +select'张'union +select'是'union +select'的' union +select'给' union +select'人' union +select'发' + +select * from Course +go +use zuoye02 +create table Score +( +ScoreId int primary key identity(1,1), +StuId int references Student(StuID), +Courseld int references Course (Courseld), +Score decimal(5,2)unique not null +) +select * from Score +update Course set CourseCredit=5 where CourseName='英语' +delete Score where StuId=1 +delete Score where Courseld=1 +alter table Score add constraint DF default(0) for Score, check(Score>=0 or Score<=100) \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery3.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ccdbd6fc4defade8c3717d7a0487055b3e9a5609 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\350\211\272\346\263\211/SQLQuery3.sql" @@ -0,0 +1,136 @@ +use master--1 +go--2 +--3 +create database Student04--4 +on(--5 + name='Student04_mdf', + filename='D:\SQL代码\Student04.mdf', + size=3MB, + maxsize=50MB, + filegrowth=10% +)--11 +log on(--12 + name='Student04_ldf', + filename='D:\SQL代码\Student04_ldf.ldf', + size=3MB, + maxsize=50MB, + filegrowth=10% +)--18 +go + +use Student04 +go + +create table Class--24 +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null +)--28 +insert into Class(ClassName) select ('软件1班') union all +select ('软件2班') union all +select ('软件3班') union all +select ('软件4班') union all +select ('软件5班') union all +select ('软件6班') union all +select ('软件7班') union all +select ('软件8班') union all +select ('软件9班') union all +select ('软件10班') +--39 +select * from Class +update Class set ClassName='软件11班' where ClassID=1 +delete Class where ClassID=10 + +create table Student--43 +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex in('男','女')), + StuBirthday date , + StuPhone nvarchar(11) unique , + StuAddress nvarchar(200) , + +)--53 +insert into Student (ClassID,StuName, StuSex, StuBirthday,StuPhone,StuAddress) +values (1, '张三' ,'男','2002-05-24','153453557','江西南昌') , +(2,'李四','男','2002-11-25','153053554','江西九江') + insert into Student (ClassID,StuName, StuSex, StuBirthday,StuPhone,StuAddress) + values(3, '张三' ,'男','2002-04-24','153442557','江西南昌') , +(4,'张六','男','2002-05-27','1535355469','江西抚州') , + (5,'张七','男','2002-05-28','15345355417','江西新余') , +(6,'张八','女','2002-05-29','15345355427','江西南昌') , + (7,'张九','男','2002-06-01','15345355437','江西南昌'), + (8,'张十','男','2002-05-14','1534535477','江西九江'), + (9,'李七','女','2002-05-04','1534535447','江西南昌'), + (1,'李五','女','2002-05-13','1234035377','江西南昌') , +(1,'陈六','男','2002-06-24','15745356747','江西南昌') , +(2,'陈七','男','2002-08-25','14584535466','江西九江'), + (3,'程八','女','2002-07-26','1574554648','江西赣州') , +(4,'程九','男','2002-04-27','1546554679','江西抚州') , +(5,'程十','男','2002-03-28','21345355461','江西新余') , +(6,'王一','女','2002-02-29','3453546207','江西南昌') , +(7,'王九','男','2002-01-01','1534564637','江西南昌') , +(8,'王十','男','2002-11-14','15457554577','江西九江'), +(9,'王四','女','2002-12-04','1545654477','江西南昌') , +(2,'王五','女','2002-05-01','1534535477','江西南昌') + +delete Student where ClassID=1 +alter table Student add CreateDate datetime default(getdate()) +update Student set CreateDate='2021-03-11 13:21:42.417' +select * from Student + +create table Course +( + Courseld int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int not null default(1) check(CourseCredit>=1 and CourseCredit<=5), + CourseType nvarchar(10) check(CourseType in + ('专业课' , '公共课')) +) + +insert into Course(CourseName) +select '数学' union +select '英语' union +select '体育' union +select '语文' union +select 'html' union +select 'java' + +select * from Course +update Course set CourseCredit=5 where CourseName='java' + +create table Score +( + Scoreld int primary key identity(1,1), + StuID int foreign key references Student(StuID), + Courseld int foreign key references Course(Courseld), + Score decimal(5,2) unique not null +) +insert into Score(Score) +select 88 union +select 61 union +select 92 union +select 63 union +select 74 union +select 65 union +select 66 union +select 57 union +select 68 union +select 69 union +select 70 union +select 71 union +select 72 union +select 63 union +select 74 union +select 75 union +select 86 union +select 77 union +select 58 union +select 79 + +select * from Score +update Course set CourseCredit=5 where CourseName='英语' +delete Score where StuId=1 +delete Score where Courseld=1 +alter table Score add constraint DF default(0) for Score, check(Score>=0 or Score<=100) diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..be024dbac18afac4d5e9a73d7086671e35cca7f6 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\270\205\350\276\211/SQLQuery1.sql" @@ -0,0 +1,92 @@ +use master +go + +create database Student +on +( + name='Student', + filename='D:\Student.mdf', + size=10MB, + maxsize=100MB, + filegrowth=10MB +) +log on +( + name='Student_log', + filename='D:\Student_log.ldf', + size=10MB, + maxsize=100MB, + filegrowth=10MB +) +go +use Student +go +create table class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null +) +insert into Class(ClassName) values ('软件一班'),('软件二班'),('软件三班'),('软件四班'),('软件五班'), +('软件六班'),('软件七班'),('软件八班'),('软件九班'),('软件十班') +update class set classname=('一班') +delete from Class where ClassName= '软件十班' + + + + +create table Student +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') Check( StuSex in('男','女')), + StuBirthday date, + StuPhone nvarchar(11) unique, + StuAddress nvarchar(200), +) +insert into Student (ClassID ,StuName ,StuSex ,StuBirthday ,StuPhone ,StuAddress) values +('1','张三' ,'男', '2001811','100861','翻斗乐园'), +('2','张三1' ,'男', '2001811','100861','翻斗乐园'), +('3','张三2' ,'男', '2001811','100861','翻斗乐园'), +('4','张三3' ,'男', '2001811','100861','翻斗乐园'), +('5','张三4' ,'男', '2001811','100861','翻斗乐园'), +('6','张三5' ,'男', '2001811','100861','翻斗乐园'), +('7','张三6' ,'男', '2001811','100861','翻斗乐园'), +('8','张三7' ,'男', '2001811','100861','翻斗乐园'), +('9','张三8' ,'男', '2001811','100861','翻斗乐园'), +('110','张三9' ,'男', '2001811','100861','翻斗乐园') +alter table Student add CreateDate datetime default (getdate()) +update student set CreateDate='2021/3/11 11:34' +delete from student where classID=4 + + + + +create table Course +( + CourseId int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int default(1) check(1<=CourseCredit and CourseCredit<=5), + CourseCred nvarchar(10) check (CourseCred='专业课' or CourseCred='公共课'), +) +insert into Course (CourseName) values ('体育','音乐','思修','专业','数学','英语') +select CourseName from Course + update Course set CourseCredit=4 where CourseName='数学' + + + + + +create table Score +( + ScoreId int primary key identity(1,1), + StuId int foreign key references Student(StuID), + CourseId int foreign key references Course(CourseId), + Score decimal (5,2) unique not null, +) +insert into Score(Score) values +(1,2,3,4,5,6,7,8,9,0,11,12,13,14,15,16,17,18,19,20) +update Course set CourseName=2 where CourseId=3 +delete from Score where StuId=1 +delete from Course where CourseId=1 +alter table Score add constraint UK_Score_Score default(0) for Score,check(Score>0 and Score<100) diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery4.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0d984a2bb601029f99d8e5bfb02a6b61501defa0 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\265\251\345\256\207/SQLQuery4.sql" @@ -0,0 +1,135 @@ +use master +go + +create database Student +on( + name='Student_mdf', + filename='D:\SQL代码\Student.mdf', + size=3MB, + maxsize=50MB, + filegrowth=10% +) +log on( + name='Student_ldf', + filename='D:\SQL代码\Student_ldf.ldf', + size=3MB, + maxsize=50MB, + filegrowth=10% +) +go + +use Student +go + +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null +) +insert into Class(ClassName) select ('软件1班') union all +select ('软件2班') union all +select ('软件3班') union all +select ('软件4班') union all +select ('软件5班') union all +select ('软件6班') union all +select ('软件7班') union all +select ('软件8班') union all +select ('软件9班') union all +select ('软件10班') + +select * from Class +update Class set ClassName='软件11班' where ClassID=1 +delete Class where ClassID=10 + +create table Student +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex in('男','女')), + StuBirthday date , + StuPhone nvarchar(11) unique , + StuAddress nvarchar(200) , +) +insert into Student (ClassID,StuName, StuSex, StuBirthday,StuPhone,StuAddress) +values (1, '张三' ,'男','2002-05-24','153453557','江西南昌') , +(2,'李四','男','2002-11-25','153053554','江西九江') + insert into Student (ClassID,StuName, StuSex, StuBirthday,StuPhone,StuAddress) + values(3, '张三' ,'男','2002-04-24','153442557','福建龙岩') , +(4,'张六','男','2002-05-27','1535355469','福建龙岩') , + (5,'张七','男','2002-05-28','15345355417','福建龙岩') , +(6,'张八','女','2002-05-29','15345355427','福建厦门') , + (7,'张九','男','2002-06-01','15345355437','福建厦门'), + (8,'张十','男','2002-05-14','1534535477','福建厦门'), + (9,'李七','女','2002-05-04','1534535447','福建厦门'), + (1,'李五','女','2002-05-13','1234035377','福建三明') , +(1,'陈六','男','2002-06-24','15745356747','福建三明') , +(2,'陈七','男','2002-08-25','14584535466','福建三明'), + (3,'陈八','女','2002-07-26','1574554648','福建三明') , +(4,'陈九','男','2002-04-27','1546554679','福建福州') , +(5,'陈十','男','2002-03-28','21345355461','福建福州') , +(6,'王一','女','2002-02-29','3453546207','福建福州') , +(7,'王九','男','2002-01-01','1534564637','福建福州') , +(8,'王十','男','2002-11-14','15457554577','福建福州'), +(9,'王四','女','2002-12-04','1545654477','福建福州') , +(2,'王五','女','2002-05-01','1534535477','福建福州') + +delete Student where ClassID=1 +alter table Student add CreateDate datetime default(getdate()) +update Student set CreateDate='2021-03-11 13:21:42.417' +select * from Student + +create table Course +( + Courseld int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int not null default(1) check(CourseCredit>=1 and CourseCredit<=5), + CourseType nvarchar(10) check(CourseType in + ('专业课' , '公共课')) +) + +insert into Course(CourseName) +select '高数' union +select '外语' union +select '体育' union +select '语文' union +select 'html' union +select 'java' + +select * from Course +update Course set CourseCredit=5 where CourseName='java' + +create table Score +( + Scoreld int primary key identity(1,1), + StuID int foreign key references Student(StuID), + Courseld int foreign key references Course(Courseld), + Score decimal(5,2) unique not null +) +insert into Score(Score) +select 88 union +select 61 union +select 92 union +select 63 union +select 74 union +select 65 union +select 66 union +select 57 union +select 68 union +select 69 union +select 70 union +select 71 union +select 72 union +select 63 union +select 74 union +select 75 union +select 86 union +select 77 union +select 58 union +select 79 + +select * from Score +update Course set CourseCredit=5 where CourseName='外语' +delete Score where StuId=1 +delete Score where Courseld=1 +alter table Score add constraint DF default(0) for Score, check(Score>=0 or Score<=100) \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..872608b4ed02385e09ea979091e31fa439dc0418 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SQLQuery1.sql" @@ -0,0 +1,98 @@ +use master +create database Student +on primary +( + name = Student, + filename = 'D:\Document\MSSQLDatabase\Student\Student.mdf', + size = 5MB, + maxsize = 20MB, + filegrowth = 1MB +) +log on +( + name = Student_log, + filename = 'D:\Document\MSSQLDatabase\Student\Student_Log.ldf', + size = 1MB, + maxsize = 10MB, + filegrowth = 10% +) +go + +use Student +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique(ClassName) +) +go + +insert into Class(ClassName) values ('一班'),('二班'),('三班'),('四班'),('五班'), +('六班'),('七班'),('八班'),('九班'),('十班') +go + +update Class set ClassName = ('1班') where ClassID = 1 + +delete Class where ClassID = 10 + +use Student +create table Student +( + StuID int primary key identity(1,1), + ClassID int constraint FK_Class_ClassID references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) not null, + StuBirthday date, + StuPhone nvarchar(11) unique(StuPhone), + StuAddress nvarchar(200), +) +go + +insert into Student(StuName,StuSex,StuPhone,ClassID) +values +('张三','男','10248191092',1),('李四','男','10248191063',2),('王五','男','10248190691',1),('赵六','女','10248191091',3),('钱七','男','10248190784',4), +('小明','男','10248190801',9),('小红','女','10248190840',8),('小王','男','10248190919',7),('小赵','男','10248190876',6),('小李','男','10248191003',5), +('老王','女','10248190767',2),('老明','男','10248190756',3),('老阿姨','男','10248191072',4),('这啥名','男','10248190613',5),('我不知道','男','10248191129',6), +('编不下去了','男','10248190866',4),('真的','女','10248190626',5),('莓办法','男','10248191107',2),('稿不了','女','10248191054',3),('再见','男','10248190856',7) + +alter table Student add CreateDate datetime default(getdate()) + +delete Student where ClassID = 1 + +use Student +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar(50) unique(CourseName) not null, + CourseCredit int default(1) check(CourseCredit >= 1 and CourseCredit <=5), + CourseType nvarchar(10) Check(CourseType = '专业课' or CourseType = '文化课') +) +go + +insert into Course(CourseName) values ('高数'), +('英语'),('概论'),('职素'),('专业'),('体育') + +select * from Course + +update Course set CourseCredit = 2 where CourseName = '专业' + +use Student +create table Score +( + ScoreID int primary key identity(1,1), + StuID int constraint FK_Student_StuID references Student(StuID), + CourseID int constraint FK_Course_CourseID references Course(CourseID), + Score decimal(5,2) unique(Score) not null +) +go + +insert into Score(StuID,CourseID,Score) +values +(36,1,60.5),(39,1,59.0),(34,3,64.5),(33,1,78.0),(27,1,80.5), +(27,1,81.0),(27,1,56.0),(29,1,85.0),(24,1,90.5),(26,2,65.5), +(36,2,83.5),(42,2,84.0),(29,2,90.0),(30,3,89.2),(31,4,88.3), +(32,2,99.5),(33,2,66.0),(34,6,76.5),(35,5,67.0),(42,3,60.0) + +delete Score where StuID = 1 +delete Score where CourseID = 1 +alter table Score add constraint CK_Score_Score check(Score >= 0 and Score <= 100) +alter table Score add constraint DK_Score_Score default'0' for Score \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery3.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9341a31cac9c6cb41787e9ae4a290dfdda7f08ed --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\246\344\270\275\346\261\237/SQLQuery3.sql" @@ -0,0 +1,137 @@ +use master +go + +create database Student04 + +on( + name='Student04', + filename='D:\SQL代码\Student04.mdf', + size=3MB, + maxsize=50MB, + filegrowth=10% +) +log on( + name='Student04_log', + filename='D:\SQL代码\Student04_mdf.log', + size=3MB, + maxsize=50MB, + filegrowth=10% +) +go + +use Student04 +go + +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null +)--28 +insert into Class(ClassName) select ('软件1班') union all +select ('软件2班') union all +select ('软件3班') union all +select ('软件4班') union all +select ('软件5班') union all +select ('软件6班') union all +select ('软件7班') union all +select ('软件8班') union all +select ('软件9班') union all +select ('软件10班') + +select * from Class +update Class set ClassName='软件11班' where ClassID=1 +delete Class where ClassID=10 + +create table Student +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex in('男','女')), + StuBirthday date , + StuPhone nvarchar(11) unique , + StuAddress nvarchar(200) , + +) + +insert into Student (ClassID,StuName, StuSex, StuBirthday,StuPhone,StuAddress) +values (1, '张三' ,'男','2002-05-24','153453557','江西南昌') , +(2,'李四','男','2002/11/25','153053554','江西九江') + insert into Student (ClassID,StuName, StuSex, StuBirthday,StuPhone,StuAddress) + values(3, '张三' ,'男',convert(date,'2002/04/24'),'153442557','江西南昌') , +(4,'张六','男',convert(date,'2002/05/27'),'1535355469','江西抚州') , + (5,'张七','男','2002/05/28','15345355417','江西新余') , +(6,'张八','女','2002/05/29','15345355427','江西南昌') , + (7,'张九','男','2002/06/01','15345355437','江西南昌'), + (8,'张十','男','2002/05/14','1534535477','江西九江'), + (9,'李七','女','2002/05/04','1534535447','江西南昌'), + (1,'李五','女','2002/05/13','1234035377','江西南昌') , +(1,'陈六','男','2002/06/24','15745356747','江西南昌') , +(2,'陈七','男','2002/08/25','14584535466','江西九江'), + (3,'程八','女','2002/07/26','1574554648','江西赣州') , +(4,'程九','男','2002/04/27','1546554679','江西抚州') , +(5,'程十','男','2002/03/28','21345355461','江西新余') , +(6,'王一','女','2002/02/29','3453546207','江西南昌') , +(7,'王九','男','2002/01/01','1534564637','江西南昌') , +(8,'王十','男','2002/11/14','15457554577','江西九江'), +(9,'王四','女','2002/12/04','1545654477','江西南昌') , +(2,'王五','女','2002/05/01','1534535477','江西南昌') + +delete Student where ClassID=1 +alter table Student add CreateDate datetime default(getdate()) +update Student set CreateDate='2021/03/11 13:21:42.417' +select * from Student + +create table Course +( + Courseld int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int not null default(1) check(CourseCredit>=1 and CourseCredit<=5), + CourseType nvarchar(10) check(CourseType in + ('专业课' , '公共课')) +) + +insert into Course(CourseName) +select '数学' union +select '英语' union +select '体育' union +select '语文' union +select 'html' union +select 'java' + +select * from Course +update Course set CourseCredit=5 where CourseName='java' +create table Score +( + Scoreld int primary key identity(1,1), + StuID int foreign key references Student(StuID), + Courseld int foreign key references Course(Courseld), + Score decimal(5,2) unique not null +) +insert into Score(Score) +select 88 union +select 61 union +select 92 union +select 63 union +select 74 union +select 65 union +select 66 union +select 57 union +select 68 union +select 69 union +select 70 union +select 71 union +select 72 union +select 63 union +select 74 union +select 75 union +select 86 union +select 77 union +select 58 union +select 79 + +select * from Score +update Course set CourseCredit=5 where CourseName='英语' +delete Score where StuId=1 +delete Score where Courseld=1 +alter table Score add constraint DF default(0) for Score, check(Score>=0 or Score<=100) \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/.keep" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d364b62c10c641a375b0348931350a821b61d2f6 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\255\217\346\265\267\350\215\243/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.sql" @@ -0,0 +1,71 @@ +use Student +go + +insert into Class(ClassName) values ('软件1班'),('软件二班'),('软件三班'),('软件四班'),('软件五班'),('软件六班'),('软件七班'),('软件八班'),('软件九班'),('软件十班') + +update Class set ClassName = '软件一班' where ClassID = '1' + +delete from Class where ClassID = '10' + +alter table Student add CreateDate datetime default(getdate()) + +insert into Student(ClassID,StuName,StuSex,StuBirthday,StuPhone,StuAddress) values + (1,'一号','男','2002-1-1',12345670001,'bejing'), + (2,'二号','女','2002-1-2',12345670010,'tianjin'), + (3,'三号','男','2002-1-17',12345670100,'shanghai'), + (4,'四号','男','2002-4-16',12345671000,'bejing'), + (5,'五号','女','2002-1-15',12345671001,'fujian'), + (6,'六号','男','2002-1-1',12345671010,'guangdong'), + (7,'七号','男','2002-1-1',12345671100,'guangxi'), + (8,'八号','男','2002-4-18',12345671101,'jiangxi'), + (9,'九号','女','2002-1-1',12345671110,'bejing'), + (1,'十号','男','2002-7-1',12345671111,'shanxi'), + (2,'壹号','男','2002-1-9',00001234567,'bejing'), + (3,'贰号','男','2002-2-6',00011234567,'taiwan'), + (4,'叁号','女','2002-3-4',00101234567,'bejing'), + (5,'肆号','男','2002-5-1',01001234567,'bejing'), + (6,'伍号','男','2002-3-6',10001234567,'hunan'), + (7,'陆号','男','2002-8-1',10011234567,'bejing'), + (8,'柒号','男','2002-11-1',10101234567,'henan'), + (9,'捌号','女','2002-12-1',11001234567,'bejing'), + (1,'玖号','男','2002-9-1',11011234567,'bejing'), + (2,'拾号','女','2002-1-14',11101234567,'hebe') + +update Student set CreateDate=getdate() + +delete from Student Where ClassID='6' + +alter table Course add CourseType nvarchar(10) check(CourseType in ('专业课','公共课')) + +insert into Course(CourseName) values('语文'),('数学'),('英语'),('体育'),('化学'),('物理') + +select * from Course + +update Course set CoueseCredit=4 where CourseName='英语' + +insert into Score(StuId,CourseId,Score) values + (1,1,99), + (2,1,89), + (3,1,98.23), + (4,2,49), + (5,2,69), + (6,2,79), + (7,3,98), + (8,3,17), + (9,3,49.5), + (10,3,61), + (11,3,92), + (12,4,83), + (13,4,46), + (14,4,67), + (15,4,58), + (16,4,79.5), + (17,5,68), + (18,5,72), + (19,5,63), + (20,6,74) + +delete from Score where StuID ='1' or CourseID='1' + +alter table Score add constraint DK_Score_Score default(0) +alter table Score add constraint CK_Score_Score check(Score>=0 and Score<=100) \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\254\254\345\233\233\346\254\241.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\254\254\345\233\233\346\254\241.sql" new file mode 100644 index 0000000000000000000000000000000000000000..748201ae02999d748aea26a7ab936a4658f33a3f --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\255\220\346\200\241/\347\254\254\345\233\233\346\254\241.sql" @@ -0,0 +1,59 @@ +use master +go +create database Student03 +on( +name='Student03', + filename='C:\test\Student03.mdf', + size=10MB, + maxsize=100MB, + filegrowth=10% + ) +log on( + name='Student03_log', + filename='C:\test\Student03_log.ldf', + size=10MB, + maxsize=100MB, + filegrowth=10% + ) +go +use Student03 +go +create table ClassInfo + (ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null, + ) + insert into ClassInfo (ClassName) values ('1班'),('2班'),('3班'),('4班'),('5班'),('6班'),('7班'),('8班'),('9班'),('10班') + update ClassInfo set ClassName = ('S班') where ClassId = 1 + delete ClassInfo where ClassId = 10 +create table StudentInfo +( + StuID int primary key identity(1,1) not null , + ClassID int references ClassInfo(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女'), + StuBirthday date, + StuAddress nvarchar(200), + CreateDate datetime default getdate() + ) + insert into StudentInfo(ClassID,StuName,StuSex,StuBirthday,StuAddress) values(1,'散佚','男','2000-8-2','天津市'),(2,'散尔','男','2002-3-12','天津市'),(3,'散叁','女','2002-2-12','天津市'),(4,'散嗣','男','2004-8-9','天津市'),(5,'散乌','男','2007-7-27','天津市'),(6,'散柳','男','2005-2-21','福建省'),(7,'散期','女','2002-2-2','天津市'),(8,'散捌','女','2001-6-18','天津市'),(1,'散酒','男','2001-6-18','天津市'),(1,'散谥','男','2002-2-2','天津市'),(2,'散拾壹','女','2002-9-1','天津市'),(4,'散拾贰','女','2001-6-18','天津市'),(1,'散拾叁','男','2002-3-12','天津市'),(5,'散拾肆','女','2002-3-12','天津市'),(8,'散诗巫','女','2002-2-2','天津市'),(6,'散拾陆','男','2000-8-2','天津市'),(9,'散石岐','男','2002-2-2','天津市'),(1,'散十峇','女','2000-8-2','天津市'),(9,'散时玖','女','2000-8-2','天津市'),(4,'散迩世','男','2000-8-2','天津市') +update StudentInfo set CreateDate=getdate() +delete StudentInfo where ClassID = 1 + create table CourseInfo + (CourseID int primary key identity (1,1), + CourseName nvarchar(50) unique, + CourseCredit int default('1') check(CourseCredit='1' or CourseCredit='1,2,3,4,5,')not null, + CourseType nvarchar(10) check(CourseType='专业课' or CourseType='公共课') + ) + insert into CourseInfo(CourseName) values('毛概'),('数学'),('英语'),('体育'),('职业规划'),('语文') + select * from CourseInfo + update CourseInfo set CourseCredit=3 where CourseName='毛概' + create table ScoreInfo +(ScoreID int identity (1,1), + StuID int foreign key references StudentInfo(StuID), + CourseID int foreign key references CourseInfo(CourseID), + Score decimal(5,2) unique not null, +) +insert into ScoreInfo(StuID,CourseID,Score) values(1,1,8),(2,2,7),(3,3,6),(4,4,5),(5,5,4),(6,6,3),(7,7,2),(8,8,1),(9,9,9),(10,1,1),(11,2,2),(12,3,3),(13,4,6),(14,5,7),(15,6,8),(16,7,9),(17,8,8),(18,1,2),(19,2,3),(20,3,4) +delete ScoreInfo where StuId = 1 +delete ScoreInfo where CourseId = 1 +alter table Score add constraint UK_ScoreInfo_Score default(0) for Score,check(1=1 or CourseCredit<=5), + Classtypes nvarchar(10) check(Classtypes in('公共课' , '专业课')) +) + +insert into Course(CourseName) +select '数学' union +select '英语' union +select '体育' union +select '语文' union +select 'html' union +select 'java' + +--查看 +select * from Course + +--修改学分信息 +update Course set CourseCredit=2 where CourseName='java' + +create table Score +( + ScoreId int primary key identity(1,1), + StuId int foreign key references Student(StuId), + CourseId int foreign key references Class(ClassId), + Score decimal(5,2) unique not null +) + +insert into Score(Score) +select 60 union +select 61 union +select 62 union +select 63 union +select 64 union +select 65 union +select 66 union +select 67 union +select 68 union +select 69 union +select 70 union +select 71 union +select 72 union +select 73 union +select 74 union +select 75 union +select 76 union +select 77 union +select 78 union +select 79 + +select * from Score + +update Score set Score=100 where ScoreId=20 +delete Score where StuId=1 +delete Score where CourseId=1 +alter table Score add constraint DF default(0) for Score, check(Score>=0 or Score<=100) diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery3.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7519d343e7e61e486fdbcaaaee9ef18f5068dc42 --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\346\235\260\347\203\250/SQLQuery3.sql" @@ -0,0 +1,155 @@ +create database Student12 +on +( + name='Student12', + filename='F:\SQL\Student12.mdf', + size=10mb, + maxsize=50mb, + filegrowth=10% +) + +log on +( + name='Student12_log', + filename='F:\SQL\Student12_log.ldf', + size=10mb, + maxsize=50mb, + filegrowth=10% +) +use Student12 +go + +create table Class +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null, +) + +--insert 数据 +insert into Class(ClassName) +select '软件1班' union +select '软件2班' union +select '软件3班' union +select '软件4班' union +select '软件5班' union +select '软件6班' union +select '软件7班' union +select '软件8班' union +select '软件9班' union +select '软件10班' + +--修改编号1 +update Class set ClassName='软件一班' where ClassID='1' + +--删除编号10 +delete Class where ClassID='10' + +select * from Class + +create table Student +( + StuId int primary key identity(1,1), + ClassId int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女'), + StuBirthday date , + StuPhone nvarchar(11) unique , + StuAddress nvarchar(200) +) + +set IDENTITY_INSERT Student ON +--插入数据 +insert into Student(StuId,ClassId,StuName,StuSex,StuBirthday,StuPhone,StuAddress) +select '10086', 1, '张三1', '男', '2020/5/6', 15168,'福建省' union +select '10087', 2, '张三2', '男', '2020/10/6', 15169,'福建省' union +select '10088', 3, '张三3', '男', '2021/3/6', 15170,'福建省' union +select '10089', 4, '张三4', '男', '2021/8/6', 15171,'福建省' union +select '10090', 5, '张三5', '男', '2022/1/6', 15172,'福建省' union +select '10091', 6, '张三6', '女', '2022/6/6', 15173,'福建省' union +select '10092', 7, '张三7', '女', '2022/11/6', 15174,'福建省' union +select '10093', 8, '张三8', '女', '2023/4/6', 15175,'福建省' union +select '10094', 9, '张三9', '女', '2023/9/6', 15176,'福建省' + +insert into Student(StuId,ClassId,StuName,StuSex,StuBirthday,StuPhone,StuAddress) +select '10095', 1, '张三1', '男', '2020/5/6', 15177,'福建省' union +select '10096', 2, '张三2', '男', '2020/10/6', 15178,'福建省' union +select '10097', 3, '张三3', '男', '2021/3/6', 15179,'福建省' union +select '10098', 4, '张三4', '男', '2021/8/6', 15180,'福建省' union +select '10099', 5, '张三5', '男', '2022/1/6', 15181,'福建省' union +select '10100', 6, '张三6', '女', '2022/6/6', 15182,'福建省' union +select '10101', 7, '张三7', '女', '2022/11/6', 15183,'福建省' union +select '10102', 8, '张三8', '女', '2023/4/6', 15184,'福建省' union +select '10103', 9, '张三9', '女', '2023/9/6', 15185,'福建省' union +select '10104', 9, '张三9', '女', '2023/9/6', 15186,'福建省' union +select '10105', 9, '张三9', '女', '2023/9/6', 15187,'福建省' + +select * from Student +--添加创建时间的字段 +alter table Student add CreateDate datetime default(getdate()) + +update Student set CreateDate='2021-03-10 18:21:42.417 ' + +--删除编号2 +delete Student where ClassId='2' + +set IDENTITY_INSERT Student OFF + +create table Course +( + CourseId int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int not null default(1) check(CourseCredit>=1 or CourseCredit<=5), + Classtypes nvarchar(10) check(Classtypes in('公共课' , '专业课')) +) + +insert into Course(CourseName) +select '数学' union +select '英语' union +select '体育' union +select '语文' union +select 'html' union +select 'java' + +--查看 +select * from Course + +--修改学分信息 +update Course set CourseCredit=2 where CourseName='java' + +create table Score +( + ScoreId int primary key identity(1,1), + StuId int foreign key references Student(StuId), + CourseId int foreign key references Class(ClassId), + Score decimal(5,2) unique not null +) + +insert into Score(Score) +select 60 union +select 61 union +select 62 union +select 63 union +select 64 union +select 65 union +select 66 union +select 67 union +select 68 union +select 69 union +select 70 union +select 71 union +select 72 union +select 73 union +select 74 union +select 75 union +select 76 union +select 77 union +select 78 union +select 79 + +select * from Score + +update Score set Score=100 where ScoreId=20 +delete Score where StuId=1 +delete Score where CourseId=1 +alter table Score add constraint DF default(0) for Score, check(Score>=0 or Score<=100) + diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..35d8fa8890a9a720532613e241be18887c56713d --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\351\224\237\345\256\207/SQLQuery1.sql" @@ -0,0 +1,68 @@ +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 Class +( +ClassId int primary key identity(1,1), +ClassName nvarchar(20) unique(ClassName) not null, +) +insert into Class(ClassName) values('一班'),('二班'),('三班'),('四班'),('五班'),('六班'),('七班'),('八班'),('九班'),('十班') +update Class set ClassName = ('1班') where ClassID = 1 +delete Class where ClassID = 10 +create table Student +( +StuId int primary key identity(1,1), +ClassId int constraint FK_Class_ClassId references Class(ClassId), +StuName nvarchar(20) not null, +StuSex nvarchar(1) default('男') check(StuSex='男' or StuSex='女'), +StuBirthday date, +StuPhone nvarchar(11) unique, +StuAddress nvarchar(200) +) +insert into Student(StuName,StuSex,StuBirthday,StuPhone,StuAddress) values('狗1','男','2020/1/1','100001','狗窝'),('狗2','男','2020/1/1','100002','狗窝'),('狗3','男','2020/1/1','100200','狗窝'),('狗4','男','2020/1/1','10200','狗窝'),('狗5','男','2020/1/1','10123000','狗窝'),('狗6','男','2020/1/1','102200','狗窝'),('狗7','男','2020/1/1','130000','狗窝'),('狗8','男','2020/1/1','1004300','狗窝'),('狗9','男','2020/1/1','1002100','狗窝'),('狗10','男','2020/1/1','10546000','狗窝') +alter table Student add CreateDate datetime default(getdate()) +update Student set CreateDate='2021/3/11' +delete Student where ClassId='10' +create table Course +( + CourseID int primary key identity(1,1), + CourseName nvarchar(50) unique(CourseName) not null, + CourseCredit int default(1) check(CourseCredit >= 1 and CourseCredit <=5), + CourseType nvarchar(10) Check(CourseType = '专业课' or CourseType = '文化课') +) +insert into Course(CourseName) values ('数学'), +('英语'),('语文'),('体育'),('音乐'),('美术') +select * from Course +update Course set CourseCredit = 2 where CourseName = '体育' +create table Score +( + ScoreID int primary key identity(1,1), + StuID int constraint FK_Student_StuID references Student(StuID), + CourseID int constraint FK_Course_CourseID references Course(CourseID), + Score decimal(5,2) unique(Score) not null +) +insert into Score(StuID,CourseID,Score) values (1,1,1),(1,1,2),(1,1,3),(1,1,4),(1,1,5),(1,1,6) +delete Score where StuID = 1 +delete Score where CourseID = 1 +alter table Score add constraint CK_Score_Score check(Score >= 0 and Score <= 100) +alter table Score add constraint DK_Score_Score default'0' for Score \ No newline at end of file diff --git "a/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery2.sql" "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c2a25c7ff8158d3ba13ff66d81b3da63afbdb26a --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\216\346\231\250\351\234\236/SQLQuery2.sql" @@ -0,0 +1,139 @@ +use master +go + + if exists (select*from sys .databases where name='Students') + drop database Student +create database Student +on +( + name='Student', + filename='F:\Student.mdf', + size=10mb, + maxsize=100mb, + filegrowth=10mb +) +log on +( + name='Student_log', + filename='F:\Student_log.ldf', + size=10mb, + maxsize=100mb, + filegrowth=10mb +) +go + +use Student +go +create table Class--24 +( + ClassID int primary key identity(1,1), + ClassName nvarchar(20) unique not null +)--28 +insert into Class(ClassName) select ('软件1班') union all +select ('软件2班') union all +select ('软件3班') union all +select ('软件4班') union all +select ('软件5班') union all +select ('软件6班') union all +select ('软件7班') union all +select ('软件8班') union all +select ('软件9班') union all +select ('软件10班') +--39 +select * from Class +update Class set ClassName='软件11班' where ClassID=1 +delete Class where ClassID=10 + +create table Student--43 +( + StuID int primary key identity(1,1), + ClassID int foreign key references Class(ClassID), + StuName nvarchar(20) not null, + StuSex nvarchar(1) default('男') check(StuSex in('男','女')), + StuBirthday date , + StuPhone nvarchar(11) unique , + StuAddress nvarchar(200) , + +)--53 +insert into Student (ClassID,StuName, StuSex, StuBirthday,StuPhone,StuAddress) +values (1, '张三' ,'男','2002-05-24','153453557','江西南昌') , +(2,'李四','男','2002-11-25','153053554','江西九江') + insert into Student (ClassID,StuName, StuSex, StuBirthday,StuPhone,StuAddress) + values(3, '张三' ,'男','2002-04-24','153442557','江西南昌') , +(4,'张六','男','2002-05-27','1535355469','江西抚州') , + (5,'张七','男','2002-05-28','15345355417','江西新余') , +(6,'张八','女','2002-05-29','15345355427','江西南昌') , + (7,'张九','男','2002-06-01','15345355437','江西南昌'), + (8,'张十','男','2002-05-14','1534535477','江西九江'), + (9,'李七','女','2002-05-04','1534535447','江西南昌'), + (1,'李五','女','2002-05-13','1234035377','江西南昌') , +(1,'陈六','男','2002-06-24','15745356747','江西南昌') , +(2,'陈七','男','2002-08-25','14584535466','江西九江'), + (3,'程八','女','2002-07-26','1574554648','江西赣州') , +(4,'程九','男','2002-04-27','1546554679','江西抚州') , +(5,'程十','男','2002-03-28','21345355461','江西新余') , +(6,'王一','女','2002-02-29','3453546207','江西南昌') , +(7,'王九','男','2002-01-01','1534564637','江西南昌') , +(8,'王十','男','2002-11-14','15457554577','江西九江'), +(9,'王四','女','2002-12-04','1545654477','江西南昌') , +(2,'王五','女','2002-05-01','1534535477','江西南昌') + +delete Student where ClassID=1 +alter table Student add CreateDate datetime default(getdate()) +update Student set CreateDate='2021-03-11 13:21:42.417' +select * from Student + +create table Course +( + Courseld int primary key identity(1,1), + CourseName nvarchar(50) unique not null, + CourseCredit int not null default(1) check(CourseCredit>=1 and CourseCredit<=5), + CourseType nvarchar(10) check(CourseType in + ('专业课' , '公共课')) +) + +insert into Course(CourseName) +select '数学' union +select '英语' union +select '体育' union +select '语文' union +select 'html' union +select 'java' + +select * from Course +update Course set CourseCredit=5 where CourseName='java' + +create table Score +( + Scoreld int primary key identity(1,1), + StuID int foreign key references Student(StuID), + Courseld int foreign key references Course(Courseld), + Score decimal(5,2) unique not null +) +insert into Score(Score) +select 88 union +select 61 union +select 92 union +select 63 union +select 74 union +select 65 union +select 66 union +select 57 union +select 68 union +select 69 union +select 70 union +select 71 union +select 72 union +select 63 union +select 74 union +select 75 union +select 86 union +select 77 union +select 58 union +select 79 + +select * from Score +update Course set CourseCredit=5 where CourseName='英语' +delete Score where StuId=1 +delete Score where Courseld=1 +alter table Score add constraint DF default(0) for Score, check(Score>=0 or Score<=100) \ No newline at end of file