diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery1.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..180832bbb9627951fe5be026e83f6633f38c46ab --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery1.sql" @@ -0,0 +1,61 @@ +use master +go +create database GoodsDB +on( +name='GoodsDB', +filename='D:\GoodsDB_ldf', +size=5, +maxsize=100, +filegrowth=5% + + +) +log on ( +name='GoodsDB_log', +filename='D:\GoodsDB_log_mdf', +size=5, +maxsize=100, +filegrowth=5% + +) +go +use GoodsDB +go +create table GoodsType +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null +) +create table GoodsInfo +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) + +) +insert into GoodsType values('服装内衣'),('鞋包服'),('手机数码') +insert into GoodsInfo values('提花小西装', '红色','菲曼琪', '300', 1), +('无袖背心',' 白色', '阿依莲', '700', 1), +('低帮休闲鞋', '红色', '菲曼琪', '900', 2), +('提花小西装', '红色','菲曼琪', '300', 1), +('中跟单鞋' ,'绿色' ,'哥弟 ','400 ',2), + ('平底鞋', '白色', '阿依莲', '200', 2), +(' 迷你照相机', '红色','尼康', '500', 3), +('硬盘', '黑色', '希捷', '600', 3), +(' 显卡', '黑色', '技嘉','800', 3) + + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select MAX(GoodsMoney) from GoodsInfo +select GoodsName 商品名称,GoodsMoney 商品价格 from GoodsInfo inner join GoodsType on GoodsInfo.TypeID=GoodsType.TypeID where GoodsMoney=(select MAX(GoodsMoney) from GoodsInfo ) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select GoodsInfo.TypeID 商品编号,MAX(GoodsMoney)最高价格,min(GoodsMoney)最低价格 ,avg(GoodsMoney)平均价格 from GoodsInfo inner join GoodsType on GoodsInfo.TypeID=GoodsType.TypeID group by GoodsInfo.TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney in(300 , 600) \ No newline at end of file diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery2.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1fb1473ae8ebc218ff35dcacb679e8218f9c4f00 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery2.sql" @@ -0,0 +1,288 @@ + + + + +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + + + +go + +-- 练习题目: +select * from StudentInfo +select * from Teachers +select * from CourseInfo +select * from StudentCourseScore +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +SELECT StudentName,B.StudentId,B.Score 数学,A.Score 语文 FROM +(select CourseId,Score,StudentId from StudentCourseScore WHERE CourseId='2')A INNER JOIN +(select CourseId,Score,StudentId from StudentCourseScore WHERE CourseId='1')B INNER JOIN +StudentInfo ON B.StudentId=StudentInfo.id +ON A.studentid=B.StudentId +WHERE A.Score60 +-- 3.查询在 成绩 表存在成绩的学生信息 + +select * from StudentCourseScore S LEFT join StudentInfo ST on S.StudentId=ST.Id + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select StudentInfo.Id 学生编号,StudentInfo.StudentName 学生姓名,count(*) 选课总数, sum(Score)总分 from StudentCourseScore + inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id +group by StudentInfo.Id,StudentInfo.StudentName + +-- 4.1 查有成绩的学生信息 + +select * from StudentCourseScore inner join StudentInfo on StudentCourseScore.Id=StudentInfo.Id + +-- 5.查询「李」姓老师的数量 + +SELECT * FROM Teachers WHERE TeacherName LIKE '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 + +select * from StudentCourseScore inner join StudentInfo on StudentCourseScore.StudentId=StudentInfo.Id inner join Teachers on StudentCourseScore.CourseId=Teachers.Id where Teachers.Id=1 + +-- 7.查询没有学全所有课程的同学的信息 +select distinct(s.StudentId),StudentInfo.StudentName, s.*,sc1.score,sc2.score,sc3.score from StudentCourseScore s +left join StudentCourseScore sc1 on s.studentid = sc1.studentid and sc1.CourseId = '1' +left join StudentCourseScore sc2 on s.studentid = sc2.studentid and sc2.CourseId = '2' +left join StudentCourseScore sc3 on s.studentid = sc3.studentid and sc3.CourseId = '3' +inner join StudentInfo on s.StudentId=StudentInfo.Id +where sc1.score is null or sc2.score is null or sc3.score is null + + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId) > 0 +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + + +select StudentCode ,StudentName from StudentCourseScore A + inner join StudentInfo B on A.StudentId = B.StudentCode + group by StudentCode ,StudentName + having count(CourseId)=(select COUNT(CourseName) from CourseInfo) + +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 + +select distinct studentName from StudentCourseScore A + inner join CourseInfo B on A.CourseId = B.Id + inner join StudentInfo C on A.StudentId = C.StudentCode + inner join Teachers D on B.TeacherId = D.Id + where TeacherName != '张三' + diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery3.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..11b06ac7e296af040556d3a0a6b74bd192e71ea7 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery3.sql" @@ -0,0 +1,86 @@ +--1、创建数据库HOUSE_DB, +--要求: +--指定数据文件大小:5兆, +--指定文件最大值:50兆, +--文件增长率:1兆 +use master +go + +create database HOUSE_DB +on +( + name = HOUSE_DB, + filename ='D:\HOUSE_DB.mfg', + size = 5MB, + maxsize = 50MB, + filegrowth = 1MB +) +use HOUSE_DB + +--2、创建数据表 +--房屋类型表(HOUSE_TYPE) +--字段名 类型 是否可为空 约束 说明 +--type_id int N 主键,自增长 类型编号 +--type_name varchar(50) N 唯一约束 类型名称 + +create table HOUSE_TYPE +( + type_id int primary key identity , + type_name varchar(20) unique +) + +--房屋信息表(HOUSE) +--字段名 类型 是否可为空 约束 说明 +--house_id int N 主键,自增长 房屋编号 +--house_name varchar(50) N 房屋名称 +--house_price float N 默认值0 房租 +--type_id int N 外键依赖HOUSE_TYPE表 房屋类型 + +create table HOUSE +( + house_id int primary key identity , + house_name varchar(50) , + house_price float default('0'), + type_id int references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +--HOUSE表中添加至少3条数据,不能全都为同一类型 + +insert into HOUSE_TYPE(type_name) +select '小户型' union +select '经济型' union +select '别墅' + +insert into HOUSE(house_name,house_price,type_id) +select '蔡','5000','3' union +select '东','10000','2' union +select '生','50000','1' + +--4、添加查询 +--查询所有房屋信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--使用模糊查询包含”型“字的房屋类型信息 + +select * from HOUSE A + inner join HOUSE_TYPE B on a.type_id = B.type_id + where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 + +select house_name 名字,house_price 租金 from HOUSE + order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + +select house_name 名称,type_name 类型名称 from HOUSE A + inner join HOUSE_TYPE B on A.type_id = B.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + +select house_price ,house_name from HOUSE where house_price = (select MAX(house_price) from HOUSE) + diff --git "a/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery4.sql" "b/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cd3d67f97d3cb4545623816fb8e8c9fd1d4fcae9 --- /dev/null +++ "b/2021 04 02 \344\275\234\344\270\232/\347\216\213\351\200\270\351\243\236/SQLQuery4.sql" @@ -0,0 +1,88 @@ +--1、创建明星数据库(StarManagerDB),然后建立两张表,StarType(明星类型表),StarInfo(明星信息表),表结构分别如下: +--StarType(明星类型表) +--字段名 说明 类型 长度 可否为空 约束 +--T_NO 明星类型编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--T_NAME 明星类型 nvarchar 20 + +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go + +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +--StarInfo(明星信息表) +--字段名 说明 类型 长度 可否为空 约束 +--S_NO 明星编号 int 否 主键约束,自增,标识种子和标识增量都是1 +--S_NAME 明星姓名 nvarchar 20 否 +--S_AGE 明星年龄 int 否 +--S_HOBBY 特技 nvarchar 20 +--S_NATIVE 明星籍贯 nvarchar 20 默认约束:中国大陆 +--S_T_NO 明星类型编号 int 外键,参照StarType表的主键T_NO + +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +--2、使用插入语句为两张表添加数据 + +--明星类型表(StarType) +--明星类型编号 明星类型 +--1 体育明星 +--2 IT明星 +--3 相声演员 + +insert into StarType +select '体育明星' union +select 'IT明星' union +select '相声演员' + +--明星表(StarInfo) +--明星编号 姓名 年龄 特技 籍贯 明星类型编号 +--1 梅西 30 射门 阿根廷 1 +--2 科比 35 过人 美国 1 +--3 蔡景现 40 敲代码 中国 2 +--4 马斯克 36 造火箭 外星人 2 +--5 郭德纲 50 相声 中国 3 +--6 黄铮 41 拼多多 中国 2 + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +select '梅西',30,'射门','阿根廷',1 union +select '科比',35,'过人','美国',1 union +select '蔡景现',40,'敲代码','中国',2 union +select '马斯克',36,'造火箭','外星人',2 union +select '郭德纲',50,'相声','中国',3 union +select '黄铮',41,'拼多多','中国',2 + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + +select top 3 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + +select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo + group by S_T_NO + + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + +select top 1 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo + where S_T_NO = 1 + order by S_AGE desc + +