diff --git "a/13 \346\264\252\347\201\277\350\212\263/\344\275\234\344\270\232/8.29 day1\344\275\234\344\270\232.md" "b/13 \346\264\252\347\201\277\350\212\263/\344\275\234\344\270\232/8.29 day1\344\275\234\344\270\232.md" deleted file mode 100644 index 53287ad92ea15328101b987271a6c1f2d56a6909..0000000000000000000000000000000000000000 --- "a/13 \346\264\252\347\201\277\350\212\263/\344\275\234\344\270\232/8.29 day1\344\275\234\344\270\232.md" +++ /dev/null @@ -1,54 +0,0 @@ -# 8.29 day1作业 - -```sql -create database DBTEST -go -use DBTEST -go -create table sectionInfo( - sectionID int primary key identity(1,1), - sectionName varchar(10) not null -) -go -create table userinfo( - userNo int primary key identity(1,1), - 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 references sectionInfo(sectionID) -) -go -create table workInfo( - workId int identity(1,1) 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='病假' or workDescription='事假') -) -go -insert sectionInfo values - ('营销部'), - ('市场部'), - ('销售部'), - ('开发部'), - ('售后部') -go -insert userinfo values - ('aaaaa','女',20,'江西','1'), - ('bbbbb','女',20,'江西','1'), - ('ccccc','女',20,'江西','1'), - ('ddddd','女',20,'江西','1'), - ('eeeee','女',20,'江西','1') -go -insert workInfo values - (13,'2022/2/2','迟到'), - (14,'2022/2/2','迟到'), - (15,'2022/2/2','迟到'), - (16,'2022/2/2','迟到'), - (17,'2022/2/2','迟到') - -select * from sectionInfo -select * from userinfo -select * from workInfo -``` - diff --git "a/13 \346\264\252\347\201\277\350\212\263/\344\275\234\344\270\232/8.30 day2 \344\275\234\344\270\232.md" "b/13 \346\264\252\347\201\277\350\212\263/\344\275\234\344\270\232/8.30 day2 \344\275\234\344\270\232.md" deleted file mode 100644 index 1883762e8c0515a5510e2023b48278377a629a11..0000000000000000000000000000000000000000 --- "a/13 \346\264\252\347\201\277\350\212\263/\344\275\234\344\270\232/8.30 day2 \344\275\234\344\270\232.md" +++ /dev/null @@ -1,78 +0,0 @@ -# 8.30 day2 作业 - -```sql ---1. 查询所有行所有列 -select * from People ---2. 指定列查询(姓名,性别,月薪,电话) -select PeopleName,PeopleSex,PeopleSalary,PeoplePhone from People ---3. 指定列查询,并自定义中文列名(姓名,性别,月薪,电话) -select PeopleName 姓名,PeopleSex 性别,PeopleSalary 月薪,PeoplePhone 电话 from People ---4. 查询公司员工所在城市(不需要重复数据) -select distinct PeopleAddress from People ---5. 假设工资普调10%,查询原始工资和调整后的工资,显示(姓名,性别,月薪,加薪后的月薪)(添加列查询)。 -select PeopleName,PeopleSex,PeopleSalary,PeopleSalary*1.1 调整后 from People ---1. 根据指定列(姓名,性别,月薪,电话)查询性别为女的员工信息,并自定义中文列名 -select PeopleName,PeopleSex,PeopleSalary,PeoplePhone from People -where PeopleSex='女' ---2. 查询月薪大于等于10000 的员工信息( 单条件 ) -select PeopleName,PeopleSex,PeopleSalary,PeoplePhone from People -where PeopleSalary >10000 ---3. 查询月薪大于等于10000 的女员工信息(多条件) -select PeopleName,PeopleSex,PeopleSalary,PeoplePhone from People -where PeopleSex='女' and PeopleSalary >10000 ---4. 显示出出身年月在1980-1-1之后,而且月薪大于等于10000的女员工信息。 -select * from People -where PeopleSex='女' and PeopleSalary >10000 and PeopleBirth>1980-1-1 ---5. 显示出月薪大于等于15000 的员工,或者月薪大于等于8000的女员工信息。 -select * from People where PeopleSalary >= 15000 or PeopleSalary >8000 and PeopleSex='女' ---6. 查询月薪在10000-20000 之间员工信息( 多条件 ) -select * from people where PeopleSalary >=10000 and PeopleSalary <= 20000 ---7. 查询出地址在北京或者上海的员工信息 -select * from People where PeopleAddress ='北京' or PeopleAddress='上海' ---8. 查询所有员工信息(根据工资排序,降序排列) -select * from People -order by PeopleSalary desc ---9. 显示所有的员工信息,按照名字的长度进行倒序排列 -select * from People -order by len(PeopleName) desc ---10. 查询工资最高的5个人的信息 -select top 5 * from People -order by PeopleSalary desc ---11. 查询工资最高的10%的员工信息 - ---12. 查询出地址没有填写的员工信息 -select * from People where PeopleAddress is null ---13. 查询出地址已经填写的员工信息 -select * from People where PeopleAddress is not null ---14. 查询所有的80后员工信息 -select * from People -where PeopleBirth>1980-1-1 ---15. 查询年龄在30-40 之间,并且工资在15000-30000 之间的员工信息 -select * from people -where (YEAR(GETDATE())-YEAR(PeopleBirth)) between 30 and 40 ---16. 查询出巨蟹 6.22--7.22 的员工信息 -select * from People where (MONTH(PeopleBirth)=6 and day(PeopleBirth)>=22)or (month(PeopleBirth)=7 and day(PeopleBirth)<=22) ---17. 查询工资比赵云高的人 -select * from People where PeopleSalary > (select PeopleSalary from People where PeopleName = '赵云') ---18. 查询出和赵云在同一个城市的人 -select * from People where PeopleAddress = (select PeopleAddress from People where PeopleName = '赵云') ---19. 查询出生肖为鼠的人员信息 -select * from people where (year(PeopleBirth)%12)-3 =1 ---20. 查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) -select *, - case - when (year(peoplebirth)%12)-3 =1 then '鼠' - when (year(peoplebirth)%12)-3 =2 then '牛' - when (year(peoplebirth)%12)-3 =3 then '虎' - when (year(peoplebirth)%12)-3 =4 then '兔' - when (year(peoplebirth)%12)-3 =5 then '龙' - when (year(peoplebirth)%12)-3 =6 then '蛇' - when (year(peoplebirth)%12)-3 =7 then '马' - when (year(peoplebirth)%12)-3 =8 then '羊' - when (year(peoplebirth)%12)-3 =9 then '猴' - when (year(peoplebirth)%12)-3 =10 then '鸡' - when (year(peoplebirth)%12)-3 =11 then '狗' - when (year(peoplebirth)%12)-3 =12 then '猪' - else '' end 生肖 from People -``` - diff --git "a/19\351\231\210\345\277\227\346\242\201/2022-09-04 (2).png" "b/19\351\231\210\345\277\227\346\242\201/2022-09-04 (2).png" new file mode 100644 index 0000000000000000000000000000000000000000..4f945a4b3d77d275e33209c9da8431e6ad8b746c Binary files /dev/null and "b/19\351\231\210\345\277\227\346\242\201/2022-09-04 (2).png" differ diff --git "a/19\351\231\210\345\277\227\346\242\201/2022-09-04 (3).png" "b/19\351\231\210\345\277\227\346\242\201/2022-09-04 (3).png" new file mode 100644 index 0000000000000000000000000000000000000000..40459f0fb69023efdce812a045c8c72d9a74080a Binary files /dev/null and "b/19\351\231\210\345\277\227\346\242\201/2022-09-04 (3).png" differ diff --git "a/19\351\231\210\345\277\227\346\242\201/DBTEST.sql" "b/19\351\231\210\345\277\227\346\242\201/DBTEST.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d7ed797e53998f8be51c22d842bc73109f5aca9a --- /dev/null +++ "b/19\351\231\210\345\277\227\346\242\201/DBTEST.sql" @@ -0,0 +1,303 @@ +if exists(select * from sys.databases where name='DBTEST') +drop database DBTEST; +go + +create database DBTEST; +go + +use DBTEST; +go + +--ű +create table Department +( + --ű;int;primary key;identity(1,1)1ʼΪ1; + DepartmentId int primary key identity(1,1), + --;nvarchar(50)50ַ;not nullΪ; + DepartmentName nvarchar(50) not null, + --textı; + DepartmentRemark text +); +go + +--ְrankΪϵͳؼ֣˴ʹ[]Զ֣ϵͳؼ +create table [Rank] +( + RankId int primary key identity(1,1), + RankName nvarchar(50) not null, + RankRemark text +); +go + +--ԱϢ +create table People +( + PeopleId int primary key identity(1,1), + --references,ֶαԼ + DepartmentId int references Department(DepartmentId) not null, + RankId int references [Rank](RankId) not null, + PeopleName nvarchar(50) not null, + --defaultֶĬֵ; checkԹ涨ֵֶԼ; + PeopleSex nvarchar(1) default('') check(PeopleSex='' or PeopleSex='Ů') not null, + PeopleBirth datetime not null, + PeopleSalary decimal(12,2) check(PeopleSalary>= 1000 and PeopleSalary <= 100000) not null, + --uniqueΨһԼΪṩΨһԱ֤; + PeoplePhone nvarchar(20) unique not null, + PeopleAddress nvarchar(100), + --datetimesmalldatetimeԱʾʱͣgetdate()ڻȡϵͳǰʱ + PeopleAddTime smalldatetime default(getdate()) +); +go + + + +------------------------------ݲ------------------------------ +--ű +insert into Department values('','˾IJ'); +go + +--һԲ +insert into Department(DepartmentName,DepartmentRemark) +select 'г','ţIJ' union +select 'Ʒ','ǿյIJ' union +select 'ܾ','쵼IJ' ; +go + +-------ְ +insert into [Rank](RankName,RankRemark) +values('','') +insert into [Rank](RankName,RankRemark) +values('м','߱') +insert into [Rank](RankName,RankRemark) +values('߼','߱Դȫ'); +go + +---------Ա +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,3,'','','1984-7-9',20000,'13554785452','ɶ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'','Ů','1987-7-9',15000,'13256854578','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'','','1988-8-8',12000,'13985745871','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'ŷ','','1990-8-8',8000,'13535987412','˲',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'','','1989-4-8',9000,'13845789568','˲',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'','','1995-4-8',9500,'13878562568','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'Ƹ','','1989-4-20',8500,'13335457412','人',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,1,'','Ů','1989-4-20',6500,'13437100050','人',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,2,'ܲ','','1987-12-20',25000,'13889562354','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'','','1981-11-11',9000,'13385299632','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'Τ','','1978-1-13',8000,'13478545263','Ϻ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'','','1998-12-12',7500,'13878523695','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,3,'','','1968-11-22',9000,'13698545841','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'','','1988-1-22',11000,'13558745874','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'Ȩ','','1990-2-21',12000,'13698745214','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'','Ů','1995-2-21',13000,'13985478512','Ϻ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'С','Ů','1996-2-21',13500,'13778787874','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'','','1992-10-11',8000,'13987455214','人',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'³','','1984-9-10',5500,'13254785965','ɶ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'','','1987-5-19',8500,'13352197364','ɶ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'½ѷ','','1996-5-19',7500,'13025457392','Ͼ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'̫ʷ','','1983-6-1',7500,'13077778888','Ϻ',getdate()) + + +---ѯ +select * from Department +select * from [Rank] +select * from People + +--1. ָУԱн绰ѯԱΪŮԱϢ,Զ +select PeopleName ,PeopleSex Ա,PeopleSalary н,PeoplePhone 绰 from People where PeopleSex = 'Ů' +--2. ѯнڵ10000 ԱϢ( ) +select * from People where PeopleSalary>=10000 +--3. ѯнڵ10000 ŮԱϢ() +select * from People where PeopleSalary>=10000 and PeopleSex = 'Ů' +--4. ʾ1980-1-1֮󣬶нڵ10000ŮԱϢ +select * from People where PeopleBirth>='1980-1-1' and PeopleSalary>=10000 +--5. ʾнڵ15000 Ա,нڵ8000ŮԱϢ +select * from People where PeopleSalary>=15000 or (PeopleSalary >=8000 and PeopleSex = 'Ů') +--6. ѯн10000-20000 ֮ԱϢ( ) +select * from People where PeopleSalary between 10000 and 20000 +--7. ѯַڱϺԱϢ +select * from People where PeopleAddress ='' or PeopleAddress ='Ϻ' +--8. ѯԱϢ(ݹ򣬽) +select * from People order by PeopleSalary desc +--9. ʾеԱϢֵijȽе +select * from People order by len(PeopleName) desc +--10. ѯߵ5˵Ϣ +select top 5 * from People order by PeopleSalary desc +--11. ѯߵ10%ԱϢ +select top 10 percent * from People order by PeopleSalary +--12. ѯַûдԱϢ +select * from People where PeopleAddress is null +--13. ѯַѾдԱϢ +select * from People where PeopleAddress is not null +--14. ѯе80ԱϢ +select * from People where PeopleBirth >='1980-1-1' +--15. ѯ30-40 ֮䣬ҹ15000-30000 ֮ԱϢ +select * from People where (PeopleSalary between 15000 and 30000) +--16. ѯз 6.22--7.22 ԱϢ +select * from People where (month(PeopleBirth)=6 and day(PeopleBirth)>=22) or (month(PeopleBirth)=7 and day(PeopleBirth)<=22) +--17. ѯʱƸߵ +select PeopleName from People where PeopleSalary >(select PeopleSalary from People where PeopleName = '') +--18. ѯͬһе +select PeopleName from People where PeopleAddress = (select PeopleAddress from People where PeopleName = '') + +--19. ѯФΪԱϢ +select * from People + where year(PeopleBirth)%12=4 +--20. ѯԱϢһʾ(,ţ,,,,,,,,,,) + + +--1. ѯԱϢ +select * from People where PeopleName like'%' +--2. ѯк " " ԱϢ +select * from People where PeopleName like'%%' +--3. ʾкССߡʷԱϢ +select * from People where PeopleName like'%%' or PeopleName like'%ʷ%' +--4. ѯԱ2 +select * from People where PeopleName like'_' +--5. ѯһ㣬һֵԱϢ +select * from People where PeopleName like'__' +--6. ѯ绰뿪ͷ138ԱϢ +select * from People where PeoplePhone like'138%' +--7. ѯ绰뿪ͷ138ԱϢ,4λ78 һ5 +select * from People where PeoplePhone like'138[7,8]%5' +--8. ѯ绰뿪ͷ133ԱϢ,4λ2-5֮ һ벻23 +select * from People where PeoplePhone like'133[2-5]%[^2-3]' + +--1. Ա +select count(PeoplePhone) from People +--2. ֵ߹ +select max(PeopleSalary) ߹ from People +--3. СֵС +select min(PeopleSalary) С from People +--4. ͣԱĹܺ +select sum(PeopleSalary) ܺ from People +--5. ƽֵԱƽ +select avg(PeopleSalary) ƽ from People +--6. ֵСֵܺͣƽֵһʾ +select count(PeoplePhone) , max(PeopleSalary) ߹ , min(PeopleSalary) С ,avg(PeopleSalary) ƽ from People +--7. ѯ人Աܹʣ߹ʣ͹ʺƽ +select PeopleAddress ,count(PeoplePhone) , max(PeopleSalary) ߹ , min(PeopleSalary) С ,avg(PeopleSalary) ƽ from People +where PeopleAddress = '人' +group by PeopleAddress +--8. ʱƽʸߵԱϢ +select * from People where PeopleSalary>(select avg(PeopleSalary) from People) +--9. ֵСֵܺͣƽֵһʾ +select count(*) , max (year(getdate())-year(PeopleBirth)) , min (year(getdate())-year(PeopleBirth)) С ,avg (year(getdate())-year(PeopleBirth)) ƽ from People +--10. н10000 ϵԱ䣬Сƽ +select max(year(getdate())-year(PeopleBirth)) , min(year(getdate())-year(PeopleBirth)) С ,avg(year(getdate())-year(PeopleBirth)) ƽ from People +where PeopleSex ='' and PeopleSalary>10000 +--11. ͳƳڵڡ人ϺŮԱԼ䣬Сƽ +select max(year(getdate())-year(PeopleBirth)) , min(year(getdate())-year(PeopleBirth)) С ,avg(year(getdate())-year(PeopleBirth)) ƽ from People +where PeopleSex ='Ů' and PeopleAddress ='人' or PeopleAddress ='Ϻ' +--12. ƽߵԱϢ +select * from People where year(getdate())-year(PeopleBirth) >(select avg(year(getdate())-year(PeopleBirth)) from People) + +select * from Department +select * from [Rank] +select * from People + + +--1. ԱڵͳԱ ,Աܺ ,ƽʣ߹ʺ͹ +select PeopleAddress , count(*) ,sum(PeopleSalary) ܺ,avg(PeopleSalary) ƽ,max(PeopleSalary)߹,min(PeopleSalary) ͹ from People +group by PeopleAddress +--2. ԱڵͳԱԱܺͣƽʣ߹ʺ͹ʣ1985 꼰ԺԱͳơ +select PeopleAddress , PeopleBirth,count(*) ,sum(PeopleSalary) ܺ,avg(PeopleSalary) ƽ,max(PeopleSalary)߹,min(PeopleSalary) ͹ from People +where PeopleBirth <='1985-1-1' +group by PeopleAddress,PeopleBirth +--3. ԱڵͳԱԱܺͣƽʣ߹ʺ͹ʣҪɸѡԱ2˼ϵļ¼1985꼰ԺԱͳơ +select PeopleAddress ,count(*) ,sum(PeopleSalary) ܺ,avg(PeopleSalary) ƽ,max(PeopleSalary)߹,min(PeopleSalary) ͹ from People +where PeopleBirth <='1985-1-1' +group by PeopleAddress +having count(*)>=2 + +--1. ѯ人еԱϢҪʾԼԱϸ +select * from People +inner join Department on Department.DepartmentId = People.DepartmentId +where PeopleAddress = '人' +--2. ѯ人еԱϢҪʾƣְԼԱϸ +select * from People +inner join Department on Department.DepartmentId = People.DepartmentId +inner join Rank on Rank.RankId = People.RankId +where PeopleAddress = '人' +--3. ݲŷͳԱԱܺͣƽʣ߹ʺ͹ʡ +select DepartmentId,count(*) ,sum(PeopleSalary) ܺ,avg(PeopleSalary) ƽ,max(PeopleSalary)߹,min(PeopleSalary) ͹ from People +group by DepartmentId +--4. ݲŷͳԱԱܺͣƽʣ߹ʺ͹ʣƽ10000 µIJͳƣҸƽʽС +select DepartmentName,count(*) ,sum(PeopleSalary) ܺ,avg(PeopleSalary) ƽ,max(PeopleSalary)߹,min(PeopleSalary) ͹ from People +left join Department on Department.DepartmentId = People.DepartmentId +group by Department.DepartmentId,DepartmentName +having avg(PeopleSalary) >=10000 +order by avg(PeopleSalary) desc +--5. ݲƣȻְλƣͳԱԱܺͣƽʣ߹ʺ͹ +select DepartmentName,RankName,count(*) ,sum(PeopleSalary) ܺ,avg(PeopleSalary) ƽ,max(PeopleSalary)߹,min(PeopleSalary) ͹ from People +left join Rank on rank.RankId = People.RankId +left join Department on Department.DepartmentId = People.DepartmentId +group by Department.DepartmentId,DepartmentName,Rank.RankId,RankName + +select * from Department +select * from [Rank] +select * from People \ No newline at end of file diff --git "a/19\351\231\210\345\277\227\346\242\201/SQLQuery2.sql" "b/19\351\231\210\345\277\227\346\242\201/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..34e643f143c6b7f82bf4cae74d1f2164e1665ec6 --- /dev/null +++ "b/19\351\231\210\345\277\227\346\242\201/SQLQuery2.sql" @@ -0,0 +1,45 @@ +create database Bank +go +use Bank +go + +create table CcountInfo( + CcId int primary key identity(1,1), + CcName nvarchar(50), + CcBalance float not null, + CcPassWord int not null +) +insert into CcountInfo values('',263.03,21354) +create table CcountCard( + CardId int primary key identity(1,1), + CardNumber int not null, + CardDraw float not null, + CardBalance float not null, + CardPassWord int not null +) +insert into CcountCard values(22262,25623.23,568,2156) +create table CcountTrad( + CcountId int primary key identity(1,1), + CardNumber int not null, + CcountDraw float not null, + CcountBalance float not null, + CcountPassWord int not null +) +insert into CcountTrad values(65451,23.03,956.00,123456) +create table CcountTrader( + CcountId int primary key identity(1,1), + CardNumber int not null, + CardTrader int not null, +) +insert into CcountTrader values(96215,23151) +create table CcountCheck( + CcountId int primary key identity(1,1), + CardNumber nvarchar(50) check(CardNumber = '' or CardNumber = 'ʧ' or CardNumber = '' or CardNumber = 'ע') + ) + insert into CcountCheck values('') + + select * from CcountInfo + select * from CcountCard + select * from CcountTrad + select * from CcountTrader + select * from CcountCheck \ No newline at end of file diff --git "a/19\351\231\210\345\277\227\346\242\201/\347\254\224\350\256\260.md" "b/19\351\231\210\345\277\227\346\242\201/\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..f9d68a6ded9a02f1cb33fe41ad24ac7491b22be0 --- /dev/null +++ "b/19\351\231\210\345\277\227\346\242\201/\347\254\224\350\256\260.md" @@ -0,0 +1,139 @@ +1.--default代表字段默认值; check可以规定字段值的约束条件; + PeopleSex nvarchar(1) default('男') check(PeopleSex='男' or PeopleSex='女') not null, + +2.decimal: + +​ PeopleSalary decimal(12,2) check(PeopleSalary>= 1000 and PeopleSalary <= 100000) not null, +​ + +3.--unique代表唯一约束,为数据提供唯一性保证; + PeoplePhone nvarchar(20) unique not null, + +4.--datetime和smalldatetime都可以表示时间类型,getdate()用于获取系统当前时间 + PeopleAddTime smalldatetime default(getdate()) + +5.插入数据 +insert into Department values('行政部','公司主管行政工作的部门'); + +6.between ...and ... + +select * from People where PeopleSalary between 10000 and 20000 + +7.降序\升序 + +select * from People order by PeopleSalary desc \ asc + + + +8.模糊查询 like + +模糊查询 +%:代表匹配0个字符、1个字符或多个字符。 +_:代表匹配有且只有1个字符。 +[]:代表匹配范围内 +[^]:代表匹配不在范围内 + +1.)查询姓刘 + +select * from People where PeopleName like'刘%' + +2.)含有“尚”或者“史”的员工信息 + +where PeopleName like'%尚%' or PeopleName like'%史%' + +3.)名字是2个字 + +where PeopleName like'刘_' + +4.)名字一共三个字的员工信息 + +where PeopleName like'__香' + +5.)开头138的员工信息 + +where PeoplePhone like'138%' + +6.)开头138的员工信息,第4位可能是7,可能8 ,最后一个号码是5 + +where PeoplePhone like'138[7,8]%5' + +7.)开头133的员工信息,第4位是2-5之间的数字 ,最后一个号码不是2和3 + +where PeoplePhone like'133[2-5]%[^2-3] + + + +9.限制长度:len +userName varchar(20) unique not null check(len(userName)>4), + +10.round(num,len,[type]) +其中: +num表示需要处理的数字,len表示需要保留的长度,type处理类型(0是默认值代表四舍五入,非0代表直接截取) + +11.year(getdate()) 获取当前时间 + +12.顺序: + +​ select + +​ left jion + +​ group by + +​ having + +​ order by desc/asc + + + +13.:范式 + +**第一范式(1NF):** + +![img](https://img2020.cnblogs.com/blog/944376/202104/944376-20210429144806987-1479266927.png) + + +  列1唯一确定列2, 列3, 列4, ...,即列2, 列3, 列4, ...不能再分裂出其它列。 + +  假设有关系模式列1: 订单名; 列2: 商品。一个订单下可以有多个商品,即列2: 商品可以分裂成商品A, 商品B, 商品C, ...,所以列1: 订单名; 列2: 商品这样的关系模式不符合第一范式。 + +#  第二范式(2NF): + +![img](https://img2020.cnblogs.com/blog/944376/202104/944376-20210429145214099-1241070153.png) + + + + + + + + + + +  满足2NF的前提是必须满足1NF。 + +  此外,关系模式需要包含两部分内容。一是必须有一个(及以上)主键;二是没有包含在主键中的列必须全部依赖于全部主键,而不能只依赖于主键的一部分而不依赖全部主键。 + +  定义听起来有点绕,不慌,直接看图,只有全部的非主键列依赖于全部主键,才满足第二范式。 + +  **第三范式(3NF):** + +![img](https://img2020.cnblogs.com/blog/944376/202104/944376-20210429145526908-1013297767.png) + + + + + + + + +  满足3NF的前提是必须满足2NF。 + +  另外关系模式的非主键列必须直接依赖于主键,不能存在传递依赖。即不能存在:非主键列m既依赖于全部主键,又依赖于非主键列n的情况。 + +  定义听起来还是有点绕,不慌,直接看图,只要非主键内部存在传递依赖,就不满足第三范式。 + +  假设存在关系模式主键1: 课程编号; 列1: 教师名; 列2: 教师家庭地址。显然满足第一范式和第二范式,但是教师家庭地址传递依赖于教师名,所以不满足第三范式。 + + + diff --git "a/33\350\260\267\345\205\206\346\230\216/33\350\260\267\345\205\206\346\230\216.md" "b/33\350\260\267\345\205\206\346\230\216/33\350\260\267\345\205\206\346\230\216.md" deleted file mode 100644 index 178e9eb4ab84223f22d5026fde9121a62ce10b0f..0000000000000000000000000000000000000000 --- "a/33\350\260\267\345\205\206\346\230\216/33\350\260\267\345\205\206\346\230\216.md" +++ /dev/null @@ -1,64 +0,0 @@ -```sql -create database DBTEST -go - -use DBTEST -go - ---*部门信息表* -create table sectionInfo( -sectionID int primary key identity(1,1),--部门编号 -sectionName varchar(10) not null --部门名称 -) - -insert sectionInfo(sectionName) -values('技术部'), - ('卫生部'), - ('财务部'), - ('人事部'), - ('会计部') - - ---*员工信息表* -create table userInfo( -userNo int primary key identity(100,1),--员工编号 -userName varchar(10) unique not null check(len(userName)>4),--员工姓名 -userSex varchar(2) check(userSex='男' or userSex='女') not null,--员工性别 -userAge int check(userAge between 0 and 100) not null , --员工年龄 -userAddress varchar(50) default('湖北') not null, --员工地址 -userSection int foreign key references sectionInfo(sectionID) --员工部门 -) - -insert userInfo(userName,userSex,userAge,userAddress,userSection) -values('张三das','男',20,'河南省',2), - ('李四sda','女',18,'北京',3), - ('王五asd','男',20,'福建省',2), - ('赵六sad','女',30,'河南省',1), - ('田七sad','男',19,'湖南省',2) - ---*员工考勤表* -create table workInfo( -workId int primary key identity(1,1),--考勤编号 -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 --考勤说明 - ) - -insert workInfo(userId,workTime,workDescription) -values (106,'2022-08-17 20:00:00','迟到'), - (107,'2022-08-17 20:00:00','迟到'), - (109,'2022-07-17 20:00:00','迟到'), - (108,'2022-04-16 20:00:00','迟到'), - (108,'2022-03-17 20:00:00','迟到') - - -select * from sectionInfo -select * from userInfo -select * from workInfo - -``` -