diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\25410\346\254\241\344\275\234\344\270\232/.keep" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25410\346\254\241\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\25410\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25410\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1fc0b65d08f2771e908e1c5ff9cf4f608251ccbb --- /dev/null +++ "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25410\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,83 @@ +create database student8 +go +use student8 +go +create table Class( + ClassId int primary key identity,--班级编号 + ClassName nvarchar(20) --班级名称 +); +go +create table student( + studentId int primary key identity, --学生编号 + studentName nvarchar(50), --学生姓名 + studentSex tinyint not null, --学生性别 + studentBirth date, --出生日期 + studentAddress nvarchar(255) not null,--地址 + classId int --班级编号 +); +insert into Class(ClassName) +values ('软件一班'),('软件二班'),('计算机应用技术班') + +insert into student(studentName, studentSex, studentBirth, studentAddress, classId) +values ('刘正',1,'2000-01-01','广西省桂林市七星区空明西路10号鸾东小区',1), +('黄贵',1,'2001-03-20','江西省南昌市青山湖区艾溪湖南路南150米广阳小区',1), +('陈美',2,'2000-07-08','福建省龙岩市新罗区曹溪街道万达小区',1); +select *from student +select * from Class +insert into student(studentName, studentSex, studentBirth, studentAddress, classId) +values ('江文',1,'2000-08-10','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光',2), +( '钟琪',2,'2001-03-21','湖南省长沙市雨花区红花坡社区',2); + +insert into student(studentName, studentSex, studentBirth, studentAddress, classId) +values ('曾小林',1,'1999-12-10','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光',3), +('欧阳天天',2,'2000-04-05','湖北省武汉市洪山区友谊大道与二环线交汇处融侨悦府',3), +('徐长卿',1,'2001-01-30','江苏省苏州市苏州工业园区独墅湖社区',3), +('李逍遥',1,'1999-11-11','广东省广州市白云区金沙洲岛御洲三街恒大绿洲',3); + +insert into student(studentName, studentSex, studentBirth, studentAddress) +values ('东方不败',3,'1999-12-11' ,'河北省平定州西北四十余里的猩猩滩'), +('令狐冲',1 ,'2000-08-11', '陕西省渭南市华阴市玉泉路南段'); +select * from student +create table course ( +courseid int primary key identity , +coursename nvarchar(50) not null, +coutsecredit tinyint not null default 0 +); +create table classcourse( +classcourseid int primary key identity, +classid int references class(classid), +courseid int references course(courseid) +); +insert into course( coursename, coutsecredit) +values ('数据库高级应用',3), + ('javascript编程基础',3), + ('web前端程序设计基础',4), + ('动态网页设计.net基础',6) +select * from Class +select * from classcourse +select *from course +select * from student +insert into classcourse(classid, courseid) +values (1,1),(1,2),(1,3),(1,4), + (2,1),(2,2),(2,3),(2,4), + (3,1),(3,2),(3,3),(3,4) + +create table score ( +scoreid int primary key identity , +studentid int not null references student(studentid), +courseid int references course(courseid), +score int not null +); + +insert into score( studentid, courseid, score) +values (1,1,80),(1,2,78),(1,3,65),(1,4,90), +(2,1,60),(2,2,77),(2,3,68),(2,4,88), +(3,1,88),(3,2,45),(3,3,66),(3,4,75), +(4,1,56),(4,2,80),(4,3,75),(4,4,66), +(5,1,88),(5,2,79),(5,3,72),(5,4,85), +(6,1,68),(6,2,88),(6,3,73),(6,4,63), +(7,1,84),(7,2,90),(7,3,92),(7,4,78), +(8,1,58),(8,2,59),(8,3,65),(8,4,75), +(9,1,48),(9,2,67),(9,3,56),(9,4,48) +select * from score +Gitee — 基于 Git 的代码托管和研发协作平台 diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\25410\346\254\241\344\275\234\344\270\232/SQLQuery6.sql" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25410\346\254\241\344\275\234\344\270\232/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a361dc31ddfce5912540c603251723478379a586 --- /dev/null +++ "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25410\346\254\241\344\275\234\344\270\232/SQLQuery6.sql" @@ -0,0 +1,90 @@ +create database student6 +go +use student6 + +create table student ( +stuno nvarchar(10) primary key, +stuname nvarchar(20) not null, +stuage int not null, +stuaddress nvarchar(20) not null, +stuseat int identity, +stusex nvarchar(1) not null +); + +insert into student(stuno, stuname, stuage, stuaddress, stusex) +values('s2501','张秋利',20,'美国硅谷',1) +insert into student(stuno, stuname, stuage, stuaddress, stusex) +values('s2502','李斯文',18,'湖北武汉',0), +('s2503','马文才',22,'湖南长沙',1), +('s2504','欧阳俊雄',21,'湖北武汉',0), +('s2505','梅超风',20,'湖北武汉',1), +('s2506','陈旋风',19,'美国硅谷',1), +('s2507','陈风',20,'美国硅谷',0) + +create table exam( +examno int primary key identity, +stuno nvarchar(10) references student (stuno), +writtenexam int not null, +labexam int not null +); + +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) + +select * from exam +select * from student + +select stuno as 学生编号, stuname as 学生名, stuage as 学生年龄, stuaddress as 学生地址, stuseat as 学生座位, stusex as 性别 from student +select stuname,stuage,stuaddress from student + +select stuno 学号, writtenexam 笔试, labexam 机试 from exam +select stuno as 学号, writtenexam as 笔试, labexam as 机试 from exam +select 学号= stuno , 笔试= writtenexam,机试= labexam from exam + +select * ,stuname+'@'+stuaddress as 邮箱 from student +select stuno, writtenexam, labexam ,writtenexam+labexam as 总分 from exam + +select distinct stuaddress from student +select distinct stuage as 年龄 from student +select top 3 * from student +select top 4 stuname,stuseat from student +select top 50 percent * from student +select * from student where stuaddress='湖北武汉'and stuage=20 +select * from exam where labexam>60 and labexam<80 order by labexam desc + + +select * from student where stuaddress='湖北武汉'or stuaddress='湖南长沙' +select * from student where not(stuaddress='美国硅谷') + +select * from exam where writtenexam<70 or labexam>90 order by writtenexam asc + +select * from student where stuage=' ' +select * from student where not(stuage=' ') + +select stuname,stuage,writtenexam,labexam from student +inner join exam on student.stuno=exam.stuno + +select stuname,stuage,writtenexam,labexam from student +inner join exam on student.stuno=exam.stuno +where writtenexam>60 and labexam>60 + +select student.stuno,stuname,stuage,writtenexam,labexam from student +full join exam on student.stuno=exam.stuno + +select stuname,stuage,writtenexam,labexam from student +inner join exam on student.stuno=exam.stuno +where stuage>=20 +order by writtenexam desc + +select avg(writtenexam) from student +inner join exam on student.stuno=exam.stuno +group by stusex + +select sum(writtenexam) from student +inner join exam on student.stuno=exam.stuno +group by stusex \ No newline at end of file diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\25410\346\254\241\344\275\234\344\270\232/SQLQuery7.sql" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25410\346\254\241\344\275\234\344\270\232/SQLQuery7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2055b7fd714220371c2984df39b34f626931e265 --- /dev/null +++ "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25410\346\254\241\344\275\234\344\270\232/SQLQuery7.sql" @@ -0,0 +1,57 @@ +create database dbtest1 +go +use dbtest1 + +create table orders( +orderid int primary key identity, +orderdate datetime not null +); + +insert into orders(orderdate) +values('2008-01-12') + +select * from orders + +insert into orders(orderdate) +values ('2008-02-10'),('2008-02-15'),('2008-03-10') + +create table orderltem( +itemid int primary key identity, +orderid int references orders(orderid) not null, +itentype nvarchar(20) not null, +itemname nvarchar(20) not null, +thenumber int not null, +themoney money +); + +insert into orderltem(orderid, itentype, itemname, thenumber, themoney) +values(1,'文具','笔',72,2) +select * from orderltem + +insert into orderltem(orderid, itentype, itemname, thenumber, themoney) +values(1,'文具','尺',10,1), +(1,'体育文具','篮球',10,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 + +select orderid ,sum(thenumber),avg(themoney) from orderltem group by orderid having orderid<3 and avg(themoney)<10 + + +select orderid, sum(thenumber),avg(themoney) from orderltem group by orderid having avg(themoney)<10 and sum(thenumber)>50 +select itentype,count(*) from orderltem group by itentype + +select sum(thenumber),avg(themoney) from orderltem group by itentype having sum(thenumber)>100 + +select itemname 产品名称,count(*) 订购次数,sum(thenumber)总数量,avg(themoney)平均单价 from orderltem group by itemname \ No newline at end of file diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\25410\346\254\241\344\275\234\344\270\232/SQLQuery8.sql" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25410\346\254\241\344\275\234\344\270\232/SQLQuery8.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3a214e8b4bc823d40a135e233b0ca347a3f17c6c --- /dev/null +++ "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25410\346\254\241\344\275\234\344\270\232/SQLQuery8.sql" @@ -0,0 +1,115 @@ +create database bbs +go +use bbs +create table bbsusers( +[uid] int primary key identity, +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, +sname varchar(10) not null, +suid int references bbsusers(uid) not null +); + +create table bbsreply( +rid int primary key identity , +ruid int references bbsusers([uid]), +rtid int references bbstopic(tid), +rmsg text not null, +rtime datetime +); +create table bbstopic( +tid int primary key identity, +tuid int references bbsusers(uid), +tsid int references bbssection(sid), +ttitle varchar(100) not null, +tmsg text not null, +ttime datetime, +tcoun int +); +insert into bbsusers( uname, usex, uage, upoint) +values('小雨点','女 ' ,20, 0), +('逍遥', '男', 18,4), +('七年级生','男',19 ,2) + +select * from bbsusers +select uname 用户名,upoint 积分 from bbsusers + +insert into bbssection(sname,suid) +values('技术交流', 1), + ('读书世界',3), + ('生活百科',1), + ('八卦区 ',3) + +select * from bbssection + +insert into bbstopic( tuid, tsid, ttitle, tmsg, ttime, tcoun) +values( 2 , 4 , '范跑跑',' 谁是范跑跑',' 2008-7-8',1), + ( 3,1,'.NET ','与JAVA的区别是什么呀?', '2008-9-1',2), + (1 , 3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10',0) + +select * from bbstopic + +insert into bbsreply(ruid, rtid, rmsg, rtime) +values(1,1,'不知道','2023-3-10'), +(1,2,'更难','2023-3-10'), +(1,3,'晴天','2023-3-10') + +select * from bbsreply +select * from bbstopic +select * from bbsusers + +delete from bbstopic where tid='1' +delete from bbsusers where uid='2' +delete from bbsreply where rid='1' + +update bbsusers set upoint='10' +where uid='1' + +select * from bbssection +delete from bbssection where sid='3' +delete from bbstopic where tid='3' +delete from bbsreply where rid='3' + +select * from bbsreply +delete from bbsreply where rid='2' + +select count(*) from bbstopic group by tsid + +select count(*) from bbsreply group by rtid + +select count(tid) from bbstopic group by tuid + +select sum(tcoun) from bbstopic group by tuid + +select top 1 uname,usex,upoint from bbsusers order by upoint desc + +select * from bbstopic where ttitle like '%快乐%' + +select * from bbsusers where usex>15and usex<20and upoint>10 + +select * from bbsusers where uname like '小_大' + +select tid 主贴编号 , tuid 发帖人编号, tsid 版块编号, ttitle 贴子的标题, tmsg 帖子的内容, ttime 发帖时间, tcoun 回复数量 from bbstopic where ttime>'2008.9.10'and tcoun>10 + +select tuid,tcoun from bbstopic where ttitle like '%!' + +select sname,suid,uname from bbssection +join bbsusers on bbssection.suid=bbsusers.uid + +select tuid,uname,ttitle,tmsg,ttime from bbsusers +join bbstopic on bbsusers.uid=bbstopic.tuid + +select sname,suid,uname from bbssection +join bbsusers on bbssection.suid=bbsusers.uid +where uage<20 + +select top 1 tuid,uname,ttitle,tmsg,tcoun from bbsusers +join bbstopic on bbsusers.uid=bbstopic.tuid +order by tcoun desc + +select count(tuid) from bbstopic group by tsid,tuid diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\25411\346\254\241\344\275\234\344\270\232/.keep" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25411\346\254\241\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\25411\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25411\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..997008a8e8a1d1590251d514722c88bdbea34a86 --- /dev/null +++ "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25411\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" @@ -0,0 +1,90 @@ +create database student6 +go +use student6 + +create table student ( +stuno nvarchar(10) primary key, +stuname nvarchar(20) not null, +stuage int not null, +stuaddress nvarchar(20) not null, +stuseat int identity, +stusex nvarchar(1) not null +); + +insert into student(stuno, stuname, stuage, stuaddress, stusex) +values('s2501','张秋利',20,'美国硅谷',1) +insert into student(stuno, stuname, stuage, stuaddress, stusex) +values('s2502','李斯文',18,'湖北武汉',0), +('s2503','马文才',22,'湖南长沙',1), +('s2504','欧阳俊雄',21,'湖北武汉',0), +('s2505','梅超风',20,'湖北武汉',1), +('s2506','陈旋风',19,'美国硅谷',1), +('s2507','陈风',20,'美国硅谷',0) + +create table exam( +examno int primary key identity, +stuno nvarchar(10) references student (stuno), +writtenexam int not null, +labexam int not null +); + +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) + +select * from exam +select * from student + +select stuno as 学生编号, stuname as 学生名, stuage as 学生年龄, stuaddress as 学生地址, stuseat as 学生座位, stusex as 性别 from student +select stuname,stuage,stuaddress from student + +select stuno 学号, writtenexam 笔试, labexam 机试 from exam +select stuno as 学号, writtenexam as 笔试, labexam as 机试 from exam +select 学号= stuno , 笔试= writtenexam,机试= labexam from exam + +select * ,stuname+'@'+stuaddress as 邮箱 from student +select stuno, writtenexam, labexam ,writtenexam+labexam as 总分 from exam + +select distinct stuaddress from student +select distinct stuage as 年龄 from student +select top 3 * from student +select top 4 stuname,stuseat from student +select top 50 percent * from student +select * from student where stuaddress='湖北武汉'and stuage=20 +select * from exam where labexam>60 and labexam<80 order by labexam desc + + +select * from student where stuaddress='湖北武汉'or stuaddress='湖南长沙' +select * from student where not(stuaddress='美国硅谷') + +select * from exam where writtenexam<70 or labexam>90 order by writtenexam asc + +select * from student where stuage=' ' +select * from student where not(stuage=' ') + +select stuname,stuage,writtenexam,labexam from student +inner join exam on student.stuno=exam.stuno + +select stuname,stuage,writtenexam,labexam from student +inner join exam on student.stuno=exam.stuno +where writtenexam>60 and labexam>60 + +select student.stuno,stuname,stuage,writtenexam,labexam from student +full join exam on student.stuno=exam.stuno + +select stuname,stuage,writtenexam,labexam from student +inner join exam on student.stuno=exam.stuno +where stuage>=20 +order by writtenexam desc + +select avg(writtenexam) from student +inner join exam on student.stuno=exam.stuno +group by stusex + +select sum(writtenexam) from student +inner join exam on student.stuno=exam.stuno +group by stusex diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\25411\346\254\241\344\275\234\344\270\232/SQLQuery3.sql" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25411\346\254\241\344\275\234\344\270\232/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a90e97ab15f0f15a2c23014d752f10aa0cd8c60e --- /dev/null +++ "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25411\346\254\241\344\275\234\344\270\232/SQLQuery3.sql" @@ -0,0 +1,72 @@ +create database dbtest1 +go +use dbtest1 + +create table orders( +orderid int primary key identity, +orderdate datetime not null +); + +insert into orders(orderdate) +values('2008-01-12') + +select * from orders + +insert into orders(orderdate) +values ('2008-02-10'),('2008-02-15'),('2008-03-10') + +create table orderltem( +itemid int primary key identity, +orderid int references orders(orderid) not null, +itentype nvarchar(20) not null, +itemname nvarchar(20) not null, +thenumber int not null, +themoney money +); + +insert into orderltem(orderid, itentype, itemname, thenumber, themoney) +values(1,'文具','笔',72,2) +select * from orderltem + +insert into orderltem(orderid, itentype, itemname, thenumber, themoney) +values(1,'文具','尺',10,1), +(1,'体育文具','篮球',10,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 + +select orderid ,sum(thenumber),avg(themoney) from orderltem group by orderid having orderid<3 and avg(themoney)<10 + + +select orderid, sum(thenumber),avg(themoney) from orderltem group by orderid having avg(themoney)<10 and sum(thenumber)>50 +select itentype,count(*) from orderltem group by itentype + +select sum(thenumber),avg(themoney) from orderltem group by itentype having sum(thenumber)>100 + +select itemname 产品名称,count(*) 订购次数,sum(thenumber)总数量,avg(themoney)平均单价 from orderltem group by itemname + +select orderltem.orderid , orderdate,itentype,itemname,thenumber,themoney from orderltem +join orders on orderltem.orderid=orders.orderid + +select orderltem.orderid , orderdate,itentype,itemname from orderltem +join orders on orderltem.orderid=orders.orderid +where thenumber>50 + + +select sum(themoney) from orderltem group by itentype + +select orderid,count(itentype) from orderltem group by orderid + + +select orderid,itentype,count(itentype),sum(thenumber) from orderltem group by itentype,orderid \ No newline at end of file diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\25411\346\254\241\344\275\234\344\270\232/SQLQuery4.sql" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25411\346\254\241\344\275\234\344\270\232/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..49fe5e71680fe11bf79d7a6dc47b30f7eba37be0 --- /dev/null +++ "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25411\346\254\241\344\275\234\344\270\232/SQLQuery4.sql" @@ -0,0 +1,115 @@ +create database bbsqq +go +use bbsqq +create table bbsusers( +[uid] int primary key identity, +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, +sname varchar(10) not null, +suid int references bbsusers(uid) not null +); + +create table bbsreply( +rid int primary key identity , +ruid int references bbsusers([uid]), +rtid int references bbstopic(tid), +rmsg text not null, +rtime datetime +); +create table bbstopic( +tid int primary key identity, +tuid int references bbsusers(uid), +tsid int references bbssection(sid), +ttitle varchar(100) not null, +tmsg text not null, +ttime datetime, +tcoun int +); +insert into bbsusers( uname, usex, uage, upoint) +values('小雨点','女 ' ,20, 0), +('逍遥', '男', 18,4), +('七年级生','男',19 ,2) + +select * from bbsusers +select uname 用户名,upoint 积分 from bbsusers + +insert into bbssection(sname,suid) +values('技术交流', 1), + ('读书世界',3), + ('生活百科',1), + ('八卦区 ',3) + +select * from bbssection + +insert into bbstopic( tuid, tsid, ttitle, tmsg, ttime, tcoun) +values( 2 , 4 , '范跑跑',' 谁是范跑跑',' 2008-7-8',1), + ( 3,1,'.NET ','与JAVA的区别是什么呀?', '2008-9-1',2), + (1 , 3,'今年夏天最流行什么','有谁知道今年夏天最流行什么呀?','2008-9-10',0) + +select * from bbstopic + +insert into bbsreply(ruid, rtid, rmsg, rtime) +values(1,1,'不知道','2023-3-10'), +(1,2,'更难','2023-3-10'), +(1,3,'晴天','2023-3-10') + +select * from bbsreply +select * from bbstopic +select * from bbsusers + +delete from bbstopic where tid='1' +delete from bbsusers where uid='2' +delete from bbsreply where rid='1' + +update bbsusers set upoint='10' +where uid='1' + +select * from bbssection +delete from bbssection where sid='3' +delete from bbstopic where tid='3' +delete from bbsreply where rid='3' + +select * from bbsreply +delete from bbsreply where rid='2' + +select count(*) from bbstopic group by tsid + +select count(*) from bbsreply group by rtid + +select count(tid) from bbstopic group by tuid + +select sum(tcoun) from bbstopic group by tuid + +select top 1 uname,usex,upoint from bbsusers order by upoint desc + +select * from bbstopic where ttitle like '%快乐%' + +select * from bbsusers where usex>15and usex<20and upoint>10 + +select * from bbsusers where uname like '小_大' + +select tid 主贴编号 , tuid 发帖人编号, tsid 版块编号, ttitle 贴子的标题, tmsg 帖子的内容, ttime 发帖时间, tcoun 回复数量 from bbstopic where ttime>'2008.9.10'and tcoun>10 + +select tuid,tcoun from bbstopic where ttitle like '%!' + +select sname,suid,uname from bbssection +join bbsusers on bbssection.suid=bbsusers.uid + +select tuid,uname,ttitle,tmsg,ttime from bbsusers +join bbstopic on bbsusers.uid=bbstopic.tuid + +select sname,suid,uname from bbssection +join bbsusers on bbssection.suid=bbsusers.uid +where uage<20 + +select top 1 tuid,uname,ttitle,tmsg,tcoun from bbsusers +join bbstopic on bbsusers.uid=bbstopic.tuid +order by tcoun desc + +select count(tuid) from bbstopic group by tsid,tuid \ No newline at end of file diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\25412\346\254\241\344\275\234\344\270\232/.keep" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25412\346\254\241\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\25412\346\254\241\344\275\234\344\270\232/SQLQuery5.11sql.sql" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25412\346\254\241\344\275\234\344\270\232/SQLQuery5.11sql.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2e3f9549d7c2f3035cd0d686933961cefa033fa4 --- /dev/null +++ "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25412\346\254\241\344\275\234\344\270\232/SQLQuery5.11sql.sql" @@ -0,0 +1,76 @@ +create database dbtest2 +go +use dbtest2 +--学生信息表(stuInfo) +create table stuinfo( +stuid int primary key identity, +stuname nvarchar(20), +stuage int, +stusex int, +[time] smalldatetime +); + +--课程信息表(courseInfo) +create table courseinfo( +courseid int primary key identity, +coursename nvarchar(20), +coursemarks int +); + +--分数信息表(scoreInfo) + create table scoreinfo ( + scoreid int primary key identity, + stuid int references stuinfo (stuid), + courseid int references courseinfo(courseid), + score int + ); + + insert into stuinfo (stuname,stuage,stusex) + values ('top',19,1) + + select * from stuinfo + + insert into stuinfo (stuname,stuage,stusex) + values ('jack',20,0),('rose',21,1),('lulu',19,1),('lili',21,0) + + insert into stuinfo (stuname,stuage,stusex,[time]) + values ('abc',20,1,getdate()) + + insert into courseinfo(coursename,coursemarks) + values ('javabase',4),('html',2),('javascript',2),('sqlbase',2) + + select * from courseinfo +select * from scoreinfo + insert into scoreinfo(stuid,courseid,score) + values(1,1,80),(1,2,85),(1,4,50),(2,1,75),(2,3,45),(2,4,75),(3,1,45),(4,1,95),(4,2,75),(4,3,90),(4,4,45) + + select * from stuinfo + + --1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + select scoreinfo.stuid, count(*) 课程的数量,avg(score) 平均分 from stuinfo + join scoreinfo on stuinfo.stuid=scoreinfo.stuid + group by scoreinfo.stuid + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseid,count(*),sum(score) from stuinfo join scoreinfo on stuinfo.stuid=scoreinfo.stuid group by courseid + +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuinfo s1 join stuinfo s2 on s1.stusex=s2.stusex +where s1.stuid != s2.stuid and s1.stuage=s2.stuage and s1.stusex=s2.stusex + +--4.查询出学分一样的课程信息 +select * from courseinfo c1 join courseinfo c2 on c1.coursemarks=c2.coursemarks where c1.coursemarks=c2.coursemarks and c1.courseid != c2.courseid +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuinfo.stuid 学号,stuinfo.stuname 姓名,courseinfo.coursename 课程号,scoreinfo.score 分数 from stuinfo join scoreinfo on stuinfo.stuid=scoreinfo.stuid join courseinfo on courseinfo.courseid=scoreinfo.courseid +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuinfo.stuid 学号,stuinfo.stuname 姓名,courseinfo.coursename 课程号,courseinfo.coursemarks 课程分数, scoreinfo.score 分数 from stuinfo join scoreinfo on stuinfo.stuid=scoreinfo.stuid join courseinfo on courseinfo.courseid=scoreinfo.courseid +--7.查询出没有参加考试的学生的学号和姓名 +select stuinfo.stuid 学号,stuinfo.stuname 姓名 from stuinfo where stuinfo.stuid not in(select stuinfo.stuid from stuinfo join scoreinfo on stuinfo.stuid=scoreinfo.stuid) +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuinfo where stuname like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuinfo.stuid,avg(score),count(*) from stuinfo join scoreinfo on stuinfo.stuid=scoreinfo.stuid group by stuinfo.stuid having count(*)>2 and avg(score)>70 +select * from scoreinfo +select * from stuinfo \ No newline at end of file diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\25412\346\254\241\344\275\234\344\270\232/SQLQuery6.sql" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25412\346\254\241\344\275\234\344\270\232/SQLQuery6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f26dd129be10c5e237187acfc5b273a65013ead7 --- /dev/null +++ "b/01 \345\274\240\346\262\273\346\235\203/\347\254\25412\346\254\241\344\275\234\344\270\232/SQLQuery6.sql" @@ -0,0 +1,101 @@ +create database dbtest3112 +go +use dbtest3112 + + +--表一为上网卡信息表(tbl_card) +--id: 卡号,主键 +--passWord:密码 +--balance:卡上的余额 +--userName:该上网卡所属的用户名 +create table tb1_card( +id nvarchar(20) primary key, +[password] int not null, +balance int , +username nvarchar(20) +); +alter table tb1_card alter column [password] nvarchar(20) +--表2为网吧机器说明表(tbl_computer) +--id:机器编号,主键 +--onUse:该机器是否正在使用,0为未使用,1为正在使用 +--note:该机器的说明 +create table tb1_computer( +id nvarchar(10) primary key , +onuse int check(onuse=1 or onuse=0), +note text +); + +--表3为上网记录表(tbl_record): +--id:记录编号,主键 +--cardId:本次上网的卡号,外键 +--ComputerId:本次上网记录所使用的机器号,外键 +--beginTime:本次上网记录的开始时间 +--endTime:本次上网记录的结束时间 +--fee:本次上网的费用 +create table tb1_record( +id int primary key, +cardid nvarchar(20) references tb1_card(id), +computerid nvarchar(10) references tb1_computer(id), +begintime smalldatetime, +endtime smalldatetime, +fee int +); +insert into tb1_card(id, [password], balance, username) +values('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱骏'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'), +('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张俊') +select * from tb1_card + +insert into tb1_computer(id, onuse, note) +values('02',0,'25555'), +('03',1,'55555'), +('04',0,'66666'), +('05',1,'88888'), +('06',0,'688878'), +('B01',0,'558558') +select * from tb1_computer +select * from tb1_card +select * from tb1_record +insert into tb1_record(id, cardid, computerid, begintime, endtime, fee) +values( 23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20) + + +insert into tb1_record(id, cardid, computerid, begintime, endtime, fee) +values( 34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23), +(45 ,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 15:55:00',6), +(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 15:55:00',50), +(48,'0023_ABC','03','2006-1-6 15:26:00','2006-1-6 15:55:00',6), +(55,'0023_ABC','03','2006-7-21 15:26:00','2006-7-21 15:55:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), +(98,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23) + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select * from tb1_card +join tb1_record on tb1_card.id=tb1_record.cardid +where tb1_card.username='张军' +order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select computerid,count(*),sum(fee) from tb1_record group by computerid +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardid,count(*),sum(fee) from tb1_record group by cardid +--4. 查询出从未消费过的上网卡的卡号和用户名 +select tb1_card.id,tb1_card.username from tb1_card + where tb1_card.id not in(select cardid from tb1_card join tb1_record on tb1_card.id=tb1_record.cardid) +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tb1_card t1 join tb1_card t2 on t1.username=t2.username where t1.password=t2.username +--6. 查询出使用次数最多的机器号和使用次数 +select computerid,max(count(*)),count(*) from tb1_record group by computerid +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select cardid,username,computerid,fee from tb1_card join tb1_record on tb1_card.id= tb1_record.cardid where cardid like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 cardid,username,computerid,begintime,endtime,fee from tb1_card join tb1_record on tb1_card.id= tb1_record.cardid order by fee desc + diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\345\233\276\347\211\207_20230217211039.png" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\345\233\276\347\211\207_20230217211039.png" new file mode 100644 index 0000000000000000000000000000000000000000..c8713ee964c3b1163323f2bfb3cfa6c416e8d1f7 Binary files /dev/null and "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\345\233\276\347\211\207_20230217211039.png" differ diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..074867aa4632764caaa06eb76b712dc4cc2e5c3f --- /dev/null +++ "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" @@ -0,0 +1,303 @@ +use master; +go +-- 检测原库是否存在,如果存在则删除 +if exists (select * from sys.databases where name = 'Student') + drop database Student; +go + +-- 创建一个库 +create database Student; +go +-- 使用这个库 +use Student; +go + +-------------------- +-- 建表部分 +-------------------- +-- 创建班级表,存储班级信息,其中字段包含:班级id、班级名称 +create table Class( + ClassId int not null identity(1,1), + ClassName nvarchar(50) not null +); +go + +-- 创建学生表,存储学生信息,其中字段保护:学生id、姓名、性别、生日、家庭住址,所属班级id +create table Student ( + StudentId int not null identity(1, 1), + StudentName nvarchar(50), + StudentSex tinyint not null, + StudentBirth date, + StudentAddress nvarchar(255) not null +); +go + +-- 创建课程表,存储课程信息,其中字段包含:课程id、课程名称、课程学分 +create table Course( + CourseId int identity(1,1), + CourseName nvarchar(50), + CourseCredit int +); +go + +-- 创建班级课程表,存储班级课程信息,其中字段包含:自增id、班级id、课程id +create table ClassCourse( + ClassCourseId int identity(1,1), + ClassId int, + CourseId int +); +go + +-- 创建分数表,存储学生每个课程分数信息,其中字段包含:分数id、学生id、课程id、分数 +create table Score( + ScoreId int identity(1,1), + StudentId int, + CourseId int, + Score int +); +go + +-- 学生表建好了,细想一下少了一个所属班级的字段 +-- 给学生表 Student 增加一个所属班级id字段 +alter table Student add ClassId int not null; +go + +---------------------------------------- +-- 创建约束部分,使用alter进行修改 +---------------------------------------- +-- 班级表 ClassId 字段需要设置为主键(主键约束) +alter table Class add constraint PK_Class_ClassId primary key (ClassId); +-- 学生表 StudentId 字段需要设置为主键(主键约束) +alter table Student add constraint PK_Student_StudentId primary key (StudentId); +-- 课程表 CourseId 字段需要设置为主键(主键约束) +alter table Course add constraint PK_Course_CourseId primary key (CourseId); +-- 班级课程表 ClassCourseId 字段需要设置为主键(主键约束) +alter table ClassCourse add constraint PK_ClassCourse_ClassCourseId primary key (ClassCourseId); +-- 分数表 ScoreId 字段需要设置为主键(主键约束) +alter table Score add constraint PK_Score_ScoreId primary key (ScoreId); + +-- 学生表 StudentName 不允许为空(非空约束) +alter table Student alter column StudentName nvarchar(50) not null; + +-- 班级表 ClassName 需要唯一(唯一约束) +alter table Class add constraint UQ_Class_ClassName unique(ClassName); +-- 课程表 CourseName 需要唯一(唯一约束) +alter table Course add constraint UQ_Course_CourseName unique(CourseName); + +-- 学生表 ClassId 增加默认值为0(默认值约束) +alter table Student add constraint DF_Student_ClassId default(0) for ClassId; + +-- 学生表 StudentSex 只能为1或者2(Check约束) +alter table Student add constraint CK_Student_StudentSex check(StudentSex=1 or StudentSex=2 or StudentSex=3); +-- 分数表 Score 字段只能大于等于0(check约束) +alter table Score add constraint CK_Score_Score check(Score>=0); +-- 课程表 CourseCredit 字段只能大于0(check约束) +alter table Course add constraint CK_Course_CourseCredit check(CourseCredit>=0); + +-- 班级课程表ClassId 对应是 班级表ClassId 的外键 (外键约束) +alter table ClassCourse add constraint FK_ClassCourse_ClassId foreign key (ClassId) references Class(ClassId); +-- 班级课程表CourseId 对应是 课程表CourseId 的外键 (外键约束) +alter table ClassCourse add constraint FK_ClassCourse_CourseId foreign key (CourseId) references Course(CourseId); +-- 分数表StudentId 对应是 学生表StudentId 的外键 (外键约束) +alter table Score add constraint FK_Score_StudentId foreign key (StudentId) references Student(StudentId); +-- 分数表CourseId 对应是 课程表CourseId 的外键 (外键约束) +alter table Score add constraint FK_Score_CourseId foreign key (CourseId) references Course(CourseId); + +--插入班级数据 +insert into Class(ClassName) +values('软件一班'),('软件二班'),('计算应用技术班'); + +select * from Class; + +--插入学生信息 +--软件一班有3个同学 +--- 刘正、男(1)、2000-01-01、广西省桂林市七星区空明西路10号鸾东小区 +--- 黄贵、男、2001-03-20、江西省南昌市青山湖区艾溪湖南路南150米广阳小区 +--- 陈美、女(2)、2000-07-08、福建省龙岩市新罗区曹溪街道万达小区 +insert into Student(StudentName, StudentSex, StudentBirth, StudentAddress, ClassId) +values('刘正',1,'2002-08-02','广西省桂林市七星区空明西路10号鸾东小区',1), + ('黄贵',1,'2003-07-02','江西省南昌市青山湖区艾溪湖南路南150米广阳小区',1), + ('陈美',2,'2002-07-22','福建省龙岩市新罗区曹溪街道万达小区',1); + +-- 软件二班有2个同学,姓名、性别、生日、家庭住址 分别是: +--- 江文、男、2000-08-10、安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光 +--- 钟琪、女、2001-03-21、湖南省长沙市雨花区红花坡社区 +insert into Student(StudentName, StudentSex, StudentBirth, StudentAddress, ClassId) +values('江文',1,'2000-08-10','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光',2); + +--有2个学生尚未分配班级,姓名、性别、生日、家庭住址 分别是: + +--- 东方不败 保密 1999-12-11 河北省平定州西北四十余里的猩猩滩 +--- 令狐冲 男 2000-08-11 陕西省渭南市华阴市玉泉路南段 + +insert into Student(StudentName, StudentSex, StudentBirth, StudentAddress) +values('东方不败',3, '1999-12-11', '河北省平定州西北四十余里的猩猩滩'); + +select * from Class + +select * from student + +--插入课程信息表 + --数据库高级应用、3 + --- javascript编程基础、3 + --- web前端程序设计基础、4 + --- 动态网页设计.net基础、6 + --- 动态网页设计php基础、6 + +insert into Course(CourseName, CourseCredit) +values('数据库高级应用',3), + ('javascript编程基础',3),('web前端程序设计基础',4),('动态网页设计.net基础',6),('动态网页设计php基础',6); + + select * from Course; + +--插入班级课程表 +--软件一班开设课程,课程名称和学分分别为: +--- 数据库高级应用、3 +--- javascript编程基础、3 +--- web前端程序设计基础、4 +--- 动态网页设计.net基础、6 +select * from Class; +select * from Course; +insert into ClassCourse(ClassId,CourseId) +values(1,1),(1,2),(1,3),(1,4); + +--软件二班开设课程,课程名称和学时分别为: +--- 数据库高级应用、3 +--- javascript编程基础、3 +--- web前端程序设计基础、4 +--- 动态网页设计.net基础、6 + +insert into ClassCourse(ClassId,CourseId) +values(2,1),(2,2),(2,3),(2,4); + +--计算机应用技术班开设课程,课程名称和学时分别为: +--- 数据库高级应用、3 +--- javascript编程基础、3 +--- web前端程序设计基础、4 +--- 动态网页设计php基础、6 +insert into ClassCourse(ClassId,CourseId) +values(3,1),(3,2),(3,3),(3,5); + +select * from ClassCourse; +select * from Class; +select * from Course; +select * from Student; + +--插入分数信息 +--- 刘正、数据库高级应用、80 +--- 刘正、javascript编程基础、78 +--- 刘正、web前端程序设计基础、65 +--- 刘正、动态网页设计.net基础、90 +insert into Score(StudentId, CourseId, Score) +values(1,1,80),(1,2,78),(1,3,65),(1,4,90); + +--- 黄贵、数据库高级应用、60 +--- 黄贵、javascript编程基础、77 +--- 黄贵、web前端程序设计基础、68 +--- 黄贵、动态网页设计.net基础、88 +insert into Score(StudentId, CourseId, Score) +values(2,1,60),(2,2,77),(2,3,68),(2,4,88); + +--- 李逍遥、数据库高级应用、48 +--- 李逍遥、javascript编程基础、67 +--- 李逍遥、web前端程序设计基础、71 +--- 李逍遥、动态网页设计.net基础、56 +--- 李逍遥、数据库高级应用、48 +insert into Score(StudentId,CourseId,Score) +values(3,1,48),(3,2,67),(3,3,71),(3,4,56),(3,1,48); + +--修改和删除数据 +/* +1. 计算机应用技术班 的 欧阳天天 生日写错了,正确的生日应该是:2000-04-06,请用sql进行修改 +2. 计算机应用技术班 的 徐长卿 的 javascript编程基础 分数填错,正确的分数应该是:61,请用sql进行修改 +3. 江文 的 性别 信息填写错误,是为女,转入到软件1班,请用sql进行修改 +4. 徐长卿 和 李逍遥 转入到了软件2班,请用sql进行修改 +5. 学校对课程学分进行了调整,每门课程学分+1,请用sql进行修改 +6. 分数表这边 李逍遥 数据库高级应用 成绩 插入重复了,请帮忙删除其中一条数据 +*/ +update Student set StudentBirth='2000-04-06' where StudentId = 7; +update Score set Score = 61 where StudentId = 8 and CourseId = 2; +update Student set StudentSex = 2, ClassId = 1 where StudentId = 4; +update Student set ClassId = 2 where StudentId = 8 or StudentId = 9; +update Course set CourseCredit=CourseCredit+1; +select * from Score order by ScoreId desc; +delete from Score where ScoreId = 13; + +--1. 查询所有的班级 Class 信息 +select * from Class + +--2. 查询所有的学生 Student 信息 +select * from Student + +--3. 查询所有的课程 Course 信息 +select * from Course + +--4. 查询所有的班级课程 ClassCourse 信息 +select * from ClassCourse + +--5. 查询所有的分数 Score 信息 +select * from Score + +--6. 只查询学生的姓名和地址信息 +select StudentName,StudentAddress from Student + +--7. 查询获取学生信息表中20%的学生信息 +select top 20 percent * from Student + +--8. 查询学生信息表中的班级编号有哪些(去除重复值) +select distinct ClassId from Student + +--9. 查询分数表中有成绩的学生编号 +select StudentId from Score where Score is not null + +--10. 查询所有的课程 Course 信息,并且按照学分从大到小排列 +select * from Course order by CourseCredit desc + +--11. 查询所有的分数 Score 信息,并且按照分数从小到大排列 +select * from Score order by Score asc + +--12. 查询所有的学生 Student 信息,并且按照年龄从小到大排列 +select * from Student order by StudentBirth asc + +--13. 查询软件一班所有的学生信息,显示学生id,姓名、性别、生日、住址 +select * from Student where ClassId=1 + +--14. 查询所有的女生,显示学生id,姓名、性别、生日、住址 +select * from Student where StudentSex=2 + +--15. 查询2000年出生的所有学生,显示学生id,姓名、性别、生日、住址 +select * from Student where StudentBirth >= '2000-01-01' and StudentBirth < '2001-01-01' + +--16. 查询姓是欧阳的学生 +select * from Student where StudentName='%欧阳%' + +--17. 查询地址是桂林市的学生 +select * from Student where StudentAddress='%桂林市%' + +--18. 查询姓李的学生,并且是三个字的 +select * from Student where StudentName='李__' + +--19. 查询 软件一班 有几个学生 +select count(0) as 人数 from Student where ClassId=1 + +--20. 查询 软件一班 有几门课程 +select count(0) as 课程数 from ClassCourse where ClassId=1 + +--21. 查询 刘正(学生编号为1) 的总分 +select sum(score) from Score where StudentId = 1 + +--22. 查询所有学生 数据库高级应用 的平均分 +select * from Score where CourseId = 1 +select sum(score) as 总分,count(0) as 学生总数, avg(score) as 平均分 from Score where CourseId = 1 + +--23. 查询 所有学生 的总分 +select StudentId, sum(Score) as 总分 from Score group by StudentId + +--24. 查询 所有学生 的平均分 +select StudentId, avg(Score) as 平均分 from Score group by StudentId + +--25. 查询 所有学生 的平均分,并且按照平均分从高到低排列 +select StudentId, avg(Score) as 平均分 from Score group by StudentId order by avg(Score) desc + +--26. 查询 所有学生中 平均分 大于80分的学生。(聚合查询 + 分组 + having) +select StudentId, avg(Score) as 平均分 from Score group by StudentId having avg(Score) > 80 \ No newline at end of file diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2023-02-20_193301.png" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2023-02-20_193301.png" new file mode 100644 index 0000000000000000000000000000000000000000..ccba89368b6257b9ac12b10a49cf6b16390c706e Binary files /dev/null and "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2023-02-20_193301.png" differ diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c24b3d0b927731e0c95a2889fbccd37c9e4238f8 --- /dev/null +++ "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,34 @@ +create database shenlong +use shenlong +create table Class( +ClassId int primary key identity, --班级编号 +ClassName nvarchar(50) not null, --班级名称 +); + +create table Student( +StudentId int identity not null,--班级名称 +StudentName nvarchar(50) not null,--学生姓名 +StudentSex tinyint not null default 3,--学生性别 +StudentBirth date,--学生生日 +StudentAddress nvarchar(255) not null,--学生地址 +ClassId int not null --所属班级id +); + +create table Course( +CourseId int primary key identity,--课程编号 +CourseName nvarchar(50)not null,--课程名称 +CourseCredit tinyint not null default 0 --课程学分 +); + +create table ClassCourse( +ClassCourseId int primary key identity,--自增编号 +ClassId int not null,--班级编号 +CourseId int not null--课程编号 +); + +create table Score( +ScoreId int primary key identity,--自增编号 +StudentId int not null ,--学生编号 +CourseId int not null ,--课程编号 +Score int not null--分数 +); \ No newline at end of file diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/SQLQuery5.sql" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1918d1de5edd3c789fa6aba9261921d86b802323 --- /dev/null +++ "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/SQLQuery5.sql" @@ -0,0 +1,77 @@ +create database orderDB +go +use orderDB + +create table orders( + orderID int primary key identity, + orderDate datetime +) + +create table orderItem( + ItemiD int identity, + orderId int, + itemType nvarchar(25), + itemName nvarchar(25), + theNumber int, + theMoney int +) + +insert into orders(orderDate) + values('2008-01-12'), + ('2008-02-10'), + ('2008-02-15'), + ('2008-03-10') + +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 itemID as 订单编号,sum(theNumber) as 数量总和,avg(theMoney) as 平均单价 from orderItem where orderId<3 + group by ItemiD + having avg(theMoney)<10 + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select ItemiD as 订单编号,sum(theNumber) as 数量总和,avg(theMoney) as 平均单价 from orderItem + group by ItemiD + having sum(theNumber)>50 and avg(theMoney)<10 + +/*4.查询每种类别的产品分别订购了几次,例如: + 文具 9 + 体育用品 3 + 日常用品 3 +*/ +select itemType as 产品类别,count(*) as 订购次数 from orderItem + group by itemType + +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select itemType as 产品类别,sum(theNumber) as 订购总数,avg(theMoney) as 平均单价 from orderItem + group by itemType + having sum(theNumber)>100 + +/*6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: + 产品名称 订购次数 总数量 平均单价 + 笔 3 120 2 +*/ +select itemName as 产品名称,count(*) as 订购次数,sum(theNumber) as 总数量,avg(theMoney) as 平均单价 from orderItem + group by itemName \ No newline at end of file diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\226\260\345\273\272 DOCX \346\226\207\346\241\243 (2).docx" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\226\260\345\273\272 DOCX \346\226\207\346\241\243 (2).docx" new file mode 100644 index 0000000000000000000000000000000000000000..f52218e73bc1e73035a076da785e9f75ca9e133f Binary files /dev/null and "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\226\260\345\273\272 DOCX \346\226\207\346\241\243 (2).docx" differ diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..afa38da85c44d411259ef6616df3b21cd2cb1d84 --- /dev/null +++ "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,83 @@ +create database student8 +go +use student8 +go +create table Class( + ClassId int primary key identity,--班级编号 + ClassName nvarchar(20) --班级名称 +); +go +create table student( + studentId int primary key identity, --学生编号 + studentName nvarchar(50), --学生姓名 + studentSex tinyint not null, --学生性别 + studentBirth date, --出生日期 + studentAddress nvarchar(255) not null,--地址 + classId int --班级编号 +); +insert into Class(ClassName) +values ('软件一班'),('软件二班'),('计算机应用技术班') + +insert into student(studentName, studentSex, studentBirth, studentAddress, classId) +values ('刘正',1,'2000-01-01','广西省桂林市七星区空明西路10号鸾东小区',1), +('黄贵',1,'2001-03-20','江西省南昌市青山湖区艾溪湖南路南150米广阳小区',1), +('陈美',2,'2000-07-08','福建省龙岩市新罗区曹溪街道万达小区',1); +select *from student +select * from Class +insert into student(studentName, studentSex, studentBirth, studentAddress, classId) +values ('江文',1,'2000-08-10','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光',2), +( '钟琪',2,'2001-03-21','湖南省长沙市雨花区红花坡社区',2); + +insert into student(studentName, studentSex, studentBirth, studentAddress, classId) +values ('曾小林',1,'1999-12-10','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光',3), +('欧阳天天',2,'2000-04-05','湖北省武汉市洪山区友谊大道与二环线交汇处融侨悦府',3), +('徐长卿',1,'2001-01-30','江苏省苏州市苏州工业园区独墅湖社区',3), +('李逍遥',1,'1999-11-11','广东省广州市白云区金沙洲岛御洲三街恒大绿洲',3); + +insert into student(studentName, studentSex, studentBirth, studentAddress) +values ('东方不败',3,'1999-12-11' ,'河北省平定州西北四十余里的猩猩滩'), +('令狐冲',1 ,'2000-08-11', '陕西省渭南市华阴市玉泉路南段'); +select * from student +create table course ( +courseid int primary key identity , +coursename nvarchar(50) not null, +coutsecredit tinyint not null default 0 +); +create table classcourse( +classcourseid int primary key identity, +classid int references class(classid), +courseid int references course(courseid) +); +insert into course( coursename, coutsecredit) +values ('数据库高级应用',3), + ('javascript编程基础',3), + ('web前端程序设计基础',4), + ('动态网页设计.net基础',6) +select * from Class +select * from classcourse +select *from course +select * from student +insert into classcourse(classid, courseid) +values (1,1),(1,2),(1,3),(1,4), + (2,1),(2,2),(2,3),(2,4), + (3,1),(3,2),(3,3),(3,4) + +create table score ( +scoreid int primary key identity , +studentid int not null references student(studentid), +courseid int references course(courseid), +score int not null +); + +insert into score( studentid, courseid, score) +values (1,1,80),(1,2,78),(1,3,65),(1,4,90), +(2,1,60),(2,2,77),(2,3,68),(2,4,88), +(3,1,88),(3,2,45),(3,3,66),(3,4,75), +(4,1,56),(4,2,80),(4,3,75),(4,4,66), +(5,1,88),(5,2,79),(5,3,72),(5,4,85), +(6,1,68),(6,2,88),(6,3,73),(6,4,63), +(7,1,84),(7,2,90),(7,3,92),(7,4,78), +(8,1,58),(8,2,59),(8,3,65),(8,4,75), +(9,1,48),(9,2,67),(9,3,56),(9,4,48) +select * from score +Gitee — 基于 Git 的代码托管和研发协作平台 diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/SQLQuery4.sql" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c6afe6cf010ee1f9490e7626f28f2824dd480223 --- /dev/null +++ "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/SQLQuery4.sql" @@ -0,0 +1,126 @@ +create database Stu; +go + +use Stu; + +--学生信息表 +create table StuInFo( + stuNo nvarchar(25) primary key, + stuName nvarchar(15) not null, + stuAge tinyint not null, + stuAddress nvarchar(30) not null, + stuSeat int not null, + stuSex tinyint default 0 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); + +--学生成绩表 +create table stuexam( + examNo int primary key identity, + stuNo nvarchar(25) references StuInFo(stuNo), + writtenExam int not null, + labExam int not null +) + +--插入学生成绩 +insert into stuexam(stuNo,writtenExam,labExam) + values('s2501',50,70), + ('s2502',60,65), + ('s2503',86,85), + ('s2504',40,80), + ('s2505',70,90), + ('s2506',85,90); + +--查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 +select stuName as 学生姓名,stuAge as 年龄,stuAddress as 住址,stuSeat as 机试,stuSex as 性别 from StuInFo; + +--查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 +select stuName,stuAge,stuAddress from StuInFo; + +--查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字 +select 学号=stuNo,writtenExam as 笔试,labExam 机试 from stuexam; + +--查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱”--查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱” +select 学号=stuNo,stuName as 学生姓名,stuAddress as 住址,stuName + '@' + stuAddress as 邮箱 from StuInFo; + +--查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分这四列的信息 +select stuNo,writtenExam,labExam,writtenExam+labExam as 总分 from stuexam; + +--查询学生信息表(stuInfo)中学生来自哪几个地方 +select distinct stuAddress from StuInFo + +--查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名 +select distinct stuAge as 年龄 from StuInFo + +--查询学生信息表(stuInfo)中前3行记录 +select top 3 * from StuInFo + +--查询学生信息表(stuInfo)中前4个学生的姓名和座位号 +select top 4 stuName,stuSeat from StuInFo + +--查询学生信息表(stuInfo)中一半学生的信息 +select top 50 percent * from StuInFo + +--将地址是湖北武汉,年龄是20的学生的所有信息查询出来 +select * from StuInFo where stuAddress='湖北武汉' and stuAge=20 + +--将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列(用两种方法实现) +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 like '%湖%' --模糊查询 + +--查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列(用两种方法实现) +select * from stuexam where writtenExam not between 70 and 90 order by writtenExam asc +select * from stuexam where writtenExam<70 or writtenExam>90 order by writtenExam asc + +--查询年龄没有写的学生所有信息 +select * from StuInFo where stuAge is null + +--查询年龄写了的学生所有信息 +select * from StuInFo where stuAge is not null + +--查询姓张的学生信息 +select * from StuInFo where stuName like '张%' + +--查询学生地址中有‘湖’字的信息 +select * from StuInFo where stuAddress like '%湖%' + +--查询姓张但名为一个字的学生信息 +select * from StuInFo where stuName like '张_' + +--查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 +select * from StuInFo where stuName like '__俊%' + +--按学生的年龄降序显示所有学生信息 +select * from StuInFo order by stuAge desc + +--按学生的年龄降序和座位号升序来显示所有学生的信息 +select * from StuInFo order by stuAge desc,stuSeat asc + +--显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from stuexam order by writtenExam desc + +--显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from stuexam order by labExam asc + +--查询每个地方的学生的平均年龄 +select stuAddress,avg(stuAge) as 平均年龄 from StuInFo group by stuAddress + +--查询男女生的分别的年龄总和 +select stuSex,sum(stuAge) 年龄总和 from StuInfo group by StuSex + +--查询每个地方的男女生的平均年龄和年龄的总和 +select stuSex,avg(stuAge) 平均年龄,sum(stuAge) as 年龄总和 from StuInfo group by StuSex +Gitee — 基于 Git 的代码托管和研发协作平台 diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/.keep" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0bba86fc2649577f8f31b669744b4ea3fe918a34 --- /dev/null +++ "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,228 @@ +-------------------- +-- 建库部分 +-------------------- +-- 创建学生管理系统数据库,名称为Student +use master; +go +-- 检测原库是否存在,如果存在则删除 +if exists (select * from sys.databases where name = 'Student') + drop database Student; +go + +-- 创建一个库 +create database Student; +go +-- 使用这个库 +use Student; +go + +-------------------- +-- 建表部分 +-------------------- +-- 创建班级表,存储班级信息,其中字段包含:班级id、班级名称 +create table Class( + ClassId int not null identity(1,1), + ClassName nvarchar(50) not null +); +go + +-- 创建学生表,存储学生信息,其中字段保护:学生id、姓名、性别、生日、家庭住址,所属班级id +create table Student ( + StudentId int not null identity(1, 1), + StudentName nvarchar(50), + StudentSex tinyint not null, + StudentBirth date, + StudentAddress nvarchar(255) not null +); +go + +-- 创建课程表,存储课程信息,其中字段包含:课程id、课程名称、课程学分 +create table Course( + CourseId int identity(1,1), + CourseName nvarchar(50), + CourseCredit int +); +go + +-- 创建班级课程表,存储班级课程信息,其中字段包含:自增id、班级id、课程id +create table ClassCourse( + ClassCourseId int identity(1,1), + ClassId int, + CourseId int +); +go + +-- 创建分数表,存储学生每个课程分数信息,其中字段包含:分数id、学生id、课程id、分数 +create table Score( + ScoreId int identity(1,1), + StudentId int, + CourseId int, + Score int +); +go + +-- 学生表建好了,细想一下少了一个所属班级的字段 +-- 给学生表 Student 增加一个所属班级id字段 +alter table Student add ClassId int not null; +go + +---------------------------------------- +-- 创建约束部分,使用alter进行修改 +---------------------------------------- +-- 班级表 ClassId 字段需要设置为主键(主键约束) +alter table Class add constraint PK_Class_ClassId primary key (ClassId); +-- 学生表 StudentId 字段需要设置为主键(主键约束) +alter table Student add constraint PK_Student_StudentId primary key (StudentId); +-- 课程表 CourseId 字段需要设置为主键(主键约束) +alter table Course add constraint PK_Course_CourseId primary key (CourseId); +-- 班级课程表 ClassCourseId 字段需要设置为主键(主键约束) +alter table ClassCourse add constraint PK_ClassCourse_ClassCourseId primary key (ClassCourseId); +-- 分数表 ScoreId 字段需要设置为主键(主键约束) +alter table Score add constraint PK_Score_ScoreId primary key (ScoreId); + +-- 学生表 StudentName 不允许为空(非空约束) +alter table Student alter column StudentName nvarchar(50) not null; + +-- 班级表 ClassName 需要唯一(唯一约束) +alter table Class add constraint UQ_Class_ClassName unique(ClassName); +-- 课程表 CourseName 需要唯一(唯一约束) +alter table Course add constraint UQ_Course_CourseName unique(CourseName); + +-- 学生表 ClassId 增加默认值为0(默认值约束) +alter table Student add constraint DF_Student_ClassId default(0) for ClassId; + +-- 学生表 StudentSex 只能为1或者2(Check约束) +alter table Student add constraint CK_Student_StudentSex check(StudentSex=1 or StudentSex=2 or StudentSex=3); +-- 分数表 Score 字段只能大于等于0(check约束) +alter table Score add constraint CK_Score_Score check(Score>=0); +-- 课程表 CourseCredit 字段只能大于0(check约束) +alter table Course add constraint CK_Course_CourseCredit check(CourseCredit>=0); + +-- 班级课程表ClassId 对应是 班级表ClassId 的外键 (外键约束) +alter table ClassCourse add constraint FK_ClassCourse_ClassId foreign key (ClassId) references Class(ClassId); +-- 班级课程表CourseId 对应是 课程表CourseId 的外键 (外键约束) +alter table ClassCourse add constraint FK_ClassCourse_CourseId foreign key (CourseId) references Course(CourseId); +-- 分数表StudentId 对应是 学生表StudentId 的外键 (外键约束) +alter table Score add constraint FK_Score_StudentId foreign key (StudentId) references Student(StudentId); +-- 分数表CourseId 对应是 课程表CourseId 的外键 (外键约束) +alter table Score add constraint FK_Score_CourseId foreign key (CourseId) references Course(CourseId); + +--插入班级数据 +insert into Class(ClassName) +values('软件一班'),('软件二班'),('计算应用技术班'); + +select * from Class; + +--插入学生信息 +--软件一班有3个同学 +--- 刘正、男(1)、2000-01-01、广西省桂林市七星区空明西路10号鸾东小区 +--- 黄贵、男、2001-03-20、江西省南昌市青山湖区艾溪湖南路南150米广阳小区 +--- 陈美、女(2)、2000-07-08、福建省龙岩市新罗区曹溪街道万达小区 +insert into Student(StudentName, StudentSex, StudentBirth, StudentAddress, ClassId) +values('刘正',1,'2002-08-02','广西省桂林市七星区空明西路10号鸾东小区',1), + ('黄贵',1,'2003-07-02','江西省南昌市青山湖区艾溪湖南路南150米广阳小区',1), + ('陈美',2,'2002-07-22','福建省龙岩市新罗区曹溪街道万达小区',1); + +-- 软件二班有2个同学,姓名、性别、生日、家庭住址 分别是: +--- 江文、男、2000-08-10、安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光 +--- 钟琪、女、2001-03-21、湖南省长沙市雨花区红花坡社区 +insert into Student(StudentName, StudentSex, StudentBirth, StudentAddress, ClassId) +values('江文',1,'2000-08-10','安徽省合肥市庐阳区四里河路与潜山路交汇处万科城市之光',2); + +--有2个学生尚未分配班级,姓名、性别、生日、家庭住址 分别是: + +--- 东方不败 保密 1999-12-11 河北省平定州西北四十余里的猩猩滩 +--- 令狐冲 男 2000-08-11 陕西省渭南市华阴市玉泉路南段 + +insert into Student(StudentName, StudentSex, StudentBirth, StudentAddress) +values('东方不败',3, '1999-12-11', '河北省平定州西北四十余里的猩猩滩'); + +select * from Class + +select * from student + +--插入课程信息表 + --数据库高级应用、3 + --- javascript编程基础、3 + --- web前端程序设计基础、4 + --- 动态网页设计.net基础、6 + --- 动态网页设计php基础、6 + +insert into Course(CourseName, CourseCredit) +values('数据库高级应用',3), + ('javascript编程基础',3),('web前端程序设计基础',4),('动态网页设计.net基础',6),('动态网页设计php基础',6); + + select * from Course; + +--插入班级课程表 +--软件一班开设课程,课程名称和学分分别为: +--- 数据库高级应用、3 +--- javascript编程基础、3 +--- web前端程序设计基础、4 +--- 动态网页设计.net基础、6 +select * from Class; +select * from Course; +insert into ClassCourse(ClassId,CourseId) +values(1,1),(1,2),(1,3),(1,4); + +--软件二班开设课程,课程名称和学时分别为: +--- 数据库高级应用、3 +--- javascript编程基础、3 +--- web前端程序设计基础、4 +--- 动态网页设计.net基础、6 + +insert into ClassCourse(ClassId,CourseId) +values(2,1),(2,2),(2,3),(2,4); + +--计算机应用技术班开设课程,课程名称和学时分别为: +--- 数据库高级应用、3 +--- javascript编程基础、3 +--- web前端程序设计基础、4 +--- 动态网页设计php基础、6 +insert into ClassCourse(ClassId,CourseId) +values(3,1),(3,2),(3,3),(3,5); + +select * from ClassCourse; +select * from Class; +select * from Course; +select * from Student; + +--插入分数信息 +--- 刘正、数据库高级应用、80 +--- 刘正、javascript编程基础、78 +--- 刘正、web前端程序设计基础、65 +--- 刘正、动态网页设计.net基础、90 +insert into Score(StudentId, CourseId, Score) +values(1,1,80),(1,2,78),(1,3,65),(1,4,90); + +--- 黄贵、数据库高级应用、60 +--- 黄贵、javascript编程基础、77 +--- 黄贵、web前端程序设计基础、68 +--- 黄贵、动态网页设计.net基础、88 +insert into Score(StudentId, CourseId, Score) +values(2,1,60),(2,2,77),(2,3,68),(2,4,88); + +--- 李逍遥、数据库高级应用、48 +--- 李逍遥、javascript编程基础、67 +--- 李逍遥、web前端程序设计基础、71 +--- 李逍遥、动态网页设计.net基础、56 +--- 李逍遥、数据库高级应用、48 +insert into Score(StudentId,CourseId,Score) +values(3,1,48),(3,2,67),(3,3,71),(3,4,56),(3,1,48); + +--修改和删除数据 +/* +1. 计算机应用技术班 的 欧阳天天 生日写错了,正确的生日应该是:2000-04-06,请用sql进行修改 +2. 计算机应用技术班 的 徐长卿 的 javascript编程基础 分数填错,正确的分数应该是:61,请用sql进行修改 +3. 江文 的 性别 信息填写错误,是为女,转入到软件1班,请用sql进行修改 +4. 徐长卿 和 李逍遥 转入到了软件2班,请用sql进行修改 +5. 学校对课程学分进行了调整,每门课程学分+1,请用sql进行修改 +6. 分数表这边 李逍遥 数据库高级应用 成绩 插入重复了,请帮忙删除其中一条数据 +*/ +update Student set StudentBirth='2000-04-06' where StudentId = 7; +update Score set Score = 61 where StudentId = 8 and CourseId = 2; +update Student set StudentSex = 2, ClassId = 1 where StudentId = 4; +update Student set ClassId = 2 where StudentId = 8 or StudentId = 9; +update Course set CourseCredit=CourseCredit+1; +select * from Score order by ScoreId desc; +delete from Score where ScoreId = 13; \ No newline at end of file diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2f32277c92146de60c94d3e5f93c1ea8632e650d --- /dev/null +++ "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,45 @@ +create database shenlong +go +use shenlong +--班级信息表 +create table Class( +ClassId int primary key identity, --班级编号 +ClassName nvarchar(50) not null unique, --班级名称 +); +--学生信息表 +create table Student( +StudentId int primary key identity ,--班级名称 +StudentName nvarchar(50) not null,--学生姓名 +StudentSex tinyint not null default 3 check(StudentSex=1 or StudentSex=2 or StudentSex=3),--学生性别 +StudentBirth date,--学生生日 +StudentAddress nvarchar(255) not null,--学生地址 +ClassId int not null default 0 --所属班级id +); +--课程信息表 +create table Course( +CourseId int primary key identity,--课程编号 +CourseName nvarchar(50)not null unique,--课程名称 +CourseCredit tinyint not null default 0 --课程学分 +); +--班级课程表 +create table ClassCourse( +ClassCourseId int primary key identity,--自增编号 +ClassId int not null,--班级编号 +CourseId int unique ,--课程编号 +foreign key(ClassId) references Class(ClassId), +foreign key(CourseId) references Course(CourseId) +); +--分数信息表 +create table Score( +ScoreId int primary key identity,--自增编号 +StudentId int not null ,--学生编号 +CourseId int not null ,--课程编号 +Score int not null check(Score>0)--分数 +foreign key (StudentId) references Student(StudentId), +foreign key (CourseId) references ClassCourse(CourseId) +); +alter table Student +add StudentIdentityCard varchar(20) not null default''-- 身份证 + +alter table Score +add IsResit tinyint not null default 0--是否是补考:0不是,1是 \ No newline at end of file diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..187da9c7153e46a7f3911478a729db83f2a86fe3 --- /dev/null +++ "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/SQLQuery2.sql" @@ -0,0 +1,31 @@ +create database Bank +go +use Bank +create table AccountInfo( +AccountId int primary key identity,--账户编号 +AccountCode varchar(20) not null unique,--身份证号码 +AccountPhone varchar(20) not null,--电话号码 +RealName varchar(20) not null--真实姓名 +); +create table BankCard( +CardNo varchar(30) primary key,--银行卡号 +AccountId int not null,--账户编号 +CardPwd varchar(30) not null,--银行密码 +CardBalance money not null default 0.00,--银行卡余额 +CardState tinyint not null default 1,--银行卡状态:1正常;2挂失;3冻结;4注销;5睡眠; +CardTime varchar(30) not null,--开卡时间 +foreign key(AccountId) references AccountInfo(AccountId) +); +create table CardExchange( +ExchangeId int primary key identity,--交易编号 +CardNo varchar(30) not null,-- 银行卡号 +MoneyInBank money not null check(MoneyInBank>0 and MoneyInBank=0), --存钱金额 +MoneyOutBank money not null check(MoneyOutBank>0 and MoneyOutBank=0), -- 取钱金额 +ExchangeTime smalldatetime not null --交易时间 +foreign key(CardNo) references BankCard(CardNo) +); +alter table AccountInfo +add OpenTime smalldatetime not null default getdate()--开户时间 + +alter table BankCard +alter column CardTime smalldatetime not null -- 开卡时间 diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\345\233\276\347\211\207_20230227122448.png" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\345\233\276\347\211\207_20230227122448.png" new file mode 100644 index 0000000000000000000000000000000000000000..2328a2b8304a81ad18b42a4a8dc2e4b0ce56f5cc Binary files /dev/null and "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\345\233\276\347\211\207_20230227122448.png" differ diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\345\233\276\347\211\207_20230227122455.png" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\345\233\276\347\211\207_20230227122455.png" new file mode 100644 index 0000000000000000000000000000000000000000..a860f5a793606c0e7d23764a4871cd6713b6c04f Binary files /dev/null and "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\345\233\276\347\211\207_20230227122455.png" differ diff --git "a/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\345\233\276\347\211\207_20230227122500.png" "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\345\233\276\347\211\207_20230227122500.png" new file mode 100644 index 0000000000000000000000000000000000000000..1142246c0ddad4f687ef27a9723d9fd061c75aed Binary files /dev/null and "b/01 \345\274\240\346\262\273\346\235\203/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\345\276\256\344\277\241\345\233\276\347\211\207_20230227122500.png" differ