diff --git "a/13\345\217\267/.keep" "b/13\345\217\267/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/13\345\217\267/13\345\217\267\351\203\221\346\226\207\346\272\220.sql" "b/13\345\217\267/13\345\217\267\351\203\221\346\226\207\346\272\220.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0d81c57ac6e251b83c21e4d333f3ad143769880c --- /dev/null +++ "b/13\345\217\267/13\345\217\267\351\203\221\346\226\207\346\272\220.sql" @@ -0,0 +1,53 @@ +CREATE DATABASE DBTEST +create table sectionInfo( + sectionID int primary key identity(1,1), + sectionName varchar(10) NOT NULL + ); + + create table userInfo( + userNo int primary key identity(1,1) NOT NULL, + userName varchar(10) NOT NULL UNIQUE, + userSex varchar(2) not null default('男') check(userSex='男' or userSex='女'), + userAge INT NOT NULL CHECK(userAge>=1 AND userAge<=100), + userAddress VARCHAR(50) DEFAULT('湖北') NOT NULL, + userSection INT + ); + Alter table userInfo add constraint FK_StuInfo_ClassId foreign key(userSection) references sectionInfo(sectionID) + CREATE TABLE workInfo( + workId INT primary key identity(1,1), + userId INT, + workTime DATETIME NOT NULL, + workDescription varchar(40) CHECK(workDescription='迟到' OR workDescription='早退' OR workDescription='旷工'OR workDescription='病假'OR workDescription='事假') + ); + Alter table workInfo add constraint FK_workInfo_ClassId foreign key(userId) references userInfo(userNo); + insert into sectionInfo VALUES('运行部'); + INSERT INTO userInfo + ( + userName, + userSex, + userAge, + userAddress, + userSection + ) + VALUES + ( '老婆', -- userName - varchar(10) + '女', -- userSex - varchar(2) + 20, -- userAge - int + '零零', -- userAddress - varchar(50) + 2 -- userSection - int + ); + INSERT INTO workInfo + ( + userId, + workTime, + workDescription + ) + VALUES + ( 1, -- userId - int + GETDATE(), -- workTime - datetime + '迟到' -- workDescription - varchar(40) + ) + + SELECT * FROM sectionInfo; + SELECT * FROM userInfo; + SELECT * FROM workInfo; \ No newline at end of file diff --git "a/13\345\217\267/\347\254\224\350\256\260.txt" "b/13\345\217\267/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000000000000000000000000000000000000..36316652f69789067b898d8bac1144044e7bed69 --- /dev/null +++ "b/13\345\217\267/\347\254\224\350\256\260.txt" @@ -0,0 +1,58 @@ +--鍏崇郴鍨嬫暟鎹簱:SQL server, Mysql, Oracle +--鍒涘缓鏁版嵁搴擄細create database 鏁版嵁搴撳悕 +--database:鏁版嵁搴 + +if exists (select * from sys.databases where name='DBTEST') + drop database DBTEST + + create database DBTEST + + --浣跨敤鏁版嵁搴 + use dbtest + + --鍒涘缓鐝骇琛 + create table ClassInfo( + ClassId int primary key identity(1,1), + ClassName varchar(20) + ); + + --鎻掑叆鏁版嵁锛 insert [into] 琛ㄥ悕(瀛楁鍚) values(鍊) + insert into ClassInfo( ClassName) values('杞欢1鐝'); + + insert ClassInfo values('杞欢2鐝') + + select * from ClassInfo + + --鍒涘缓鏁版嵁琛 + create table StuInfo( + stuId int primary key identity(1001,1), --瀛︾敓ID + --娣诲姞涓涓鏌ョ害鏉燂紝鍒ゆ柇鐢ㄦ埛鎻掑叆/鏂板鐨勬暟鎹紝鎬у埆瀛楁鏄笉鏄敺鎴栬呭コ + --default:榛樿绾︽潫 + --check + stugender varchar(2) not null default('鐢') check(stugender='鐢' or stugender='濂'), --瀛︾敓鎬у埆 + stuphone char(11) check(len(stuphone)=11) unique, + --鍒涘缓鐝骇澶栭敭 + --ClassID int references ClassInfo(ClassID) + ClassID int + + ); + + + --澧炲姞澶栭敭 + --淇敼琛ㄧ粨鏋 琛ㄥ悕 add constraint 绾︽潫鍚 foreign key(瑕佸紩鐢ㄧ殑瀛楁) references 涓婚敭琛(瀛楁) + Alter table StuInfo add constraint FK_StuInfo_ClassId foreign key(ClassID) references ClassInfo(ClassID) + + + --鏂板濮撳悕鍒 + alter table StuInfo add stuName varchar(20) + + + + --濡傛灉娌$粰鍑哄垪鍚嶏紝榛樿鏄寜鐓ч『搴忎竴涓釜娣诲姞 + --insert StuInfo values('濂',13888888888) + + --insert StuInfo(stuphone) values(15888888888) + + + + select * from StuInfo; \ No newline at end of file diff --git "a/13\345\217\267\351\203\221\346\226\207\346\272\220/.keep" "b/13\345\217\267\351\203\221\346\226\207\346\272\220/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/.keep" "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/2022.9.16\344\275\234\344\270\232.sql" "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/2022.9.16\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ebf1aee7770fc08980360baa000a4dc69ad64bbc --- /dev/null +++ "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/2022.9.16\344\275\234\344\270\232.sql" @@ -0,0 +1,81 @@ +--查询出巨蟹 6.22--7.22 的员工信息 +select * from People +where datename(month,PeopleBirth) = 6 and day(PeopleBirth) between 22 and 30 +or datename(month,PeopleBirth) = 7 and day(PeopleBirth) between 1 and 22; + + +--查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) +select *, case +when year(PeopleBirth)%12 = 0 +then '猴' +when year(PeopleBirth)%12 = 1 +then '鸡' +when year(PeopleBirth)%12 = 2 +then '狗' +when year(PeopleBirth)%12 = 3 +then '猪' +when year(PeopleBirth)%12 = 4 +then '鼠' +when year(PeopleBirth)%12 = 5 +then '牛' +when year(PeopleBirth)%12 = 6 +then '虎' +when year(PeopleBirth)%12 = 7 +then '兔' +when year(PeopleBirth)%12 = 8 +then '龙' +when year(PeopleBirth)%12 = 9 +then '蛇' +when year(PeopleBirth)%12 = 10 +then '马' +when year(PeopleBirth)%12 = 11 +then '羊' +end '生肖' +from People + + +--1. 查询出武汉地区所有的员工信息,要求显示部门名称以及员工的详细资料 +select p.*,d.DepartmentName from People p +inner join Department d +on p.DepartmentId = d.DepartmentId +where p.PeopleAddress = '武汉'; + + +--2. 查询出武汉地区所有的员工信息,要求显示部门名称,职级名称以及员工的详细资料 +select p.*,d.DepartmentName,r.RankName from People p +inner join Department d +on p.DepartmentId = d.DepartmentId +inner join Rank r on r.RankId = p.RankId +where p.PeopleAddress = '武汉'; + + +--3. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资。 +select count(D.DepartmentName)部门员工人数,sum(p.PeopleSalary)员工工资总和,convert(decimal(10,2), +avg(p.PeopleSalary))平均工资,max(p.PeopleSalary)最高工资,min(p.PeopleSalary)最低工资 +from People p +inner join Department d +on p.DepartmentId = d.DepartmentId +group by d.DepartmentName + + +--4. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,平均工资在10000 以下的不参与统计, +--并且根据平均工资降序排列。 +select count(D.DepartmentName)部门员工人数,sum(p.PeopleSalary)员工工资总和,convert(decimal(10,2), +avg(p.PeopleSalary))平均工资,max(p.PeopleSalary)最高工资,min(p.PeopleSalary)最低工资 +from People p +inner join Department d +on p.DepartmentId = d.DepartmentId +group by d.DepartmentName +having avg(p.PeopleSalary) > 10000 +order by 平均工资 desc + + +--5. 根据部门名称,然后根据职位名称,分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资 +select d.DepartmentName,r.RankName,count(*)员工人数, +sum(p.PeopleSalary)员工工资总和,convert(decimal(10,2), +avg(p.PeopleSalary))平均工资,max(p.PeopleSalary)最高工资,min(p.PeopleSalary)最低工资 +from People p +inner join Department d +on p.DepartmentId = d.DepartmentId +inner join Rank r on r.RankId = p.RankId +group by d.DepartmentName,r.RankName \ No newline at end of file diff --git "a/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/.keep" "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/2022.9.16\347\254\224\350\256\260.md" "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/2022.9.16\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..1e7e72891899843b8ed78de6eb7eb342d5f947a7 --- /dev/null +++ "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/2022.9.16\347\254\224\350\256\260.md" @@ -0,0 +1,45 @@ +# 1.妯$硦鏌ヨ + +```sql +[]锛氫唬琛ㄥ尮閰嶈寖鍥村唴 +[^]锛氫唬琛ㄥ尮閰嶄笉鍦ㄨ寖鍥村唴 +``` + +```sql +select * from Department where DepartmentId like '[1-3]' + +// [1-3] == 1,2,3 + +select * from Department where DepartmentId like '[^1-3]' + +//[^1-3] == 4 +``` + +# 2.CONVERT()涓嶤AST()鍑芥暟: + +```sql + +convert(decimal(13,2),12.45454) +cast(12.45454 as decimal(13,2)) +``` + +# 3.鏃堕棿鍑芥暟 + +```sql +select DATEDIFF(day, '2019-08-20', getDate()); --鑾峰彇鎸囧畾鏃堕棿鍗曚綅鐨勫樊鍊 +SELECT DATEADD(MINUTE,-5,GETDATE()) --鍔犲噺鏃堕棿,姝ゅ涓鸿幏鍙栦簲鍒嗛挓鍓嶇殑鏃堕棿,MINUTE 琛ㄧず鍒嗛挓锛屽彲涓 YEAR,MONTH,DAY,HOUR +select DATENAME(month, getDate()); --褰撳墠鏈堜唤 +select DATENAME(WEEKDAY, getDate()); --褰撳墠鏄熸湡鍑 +select DATEPART(month, getDate()); --褰撳墠鏈堜唤 +select DAY(getDate()); --杩斿洖褰撳墠鏃ユ湡澶╂暟 +select MONTH(getDate()); --杩斿洖褰撳墠鏃ユ湡鏈堟暟 +select YEAR(getDate()); --杩斿洖褰撳墠鏃ユ湡骞存暟 + +SELECT CONVERT(VARCHAR(22),GETDATE(),20) --2020-01-09 14:46:46 +SELECT CONVERT(VARCHAR(24),GETDATE(),21) --2020-01-09 14:46:55.91 +SELECT CONVERT(VARCHAR(22),GETDATE(),23) --2020-01-09 +SELECT CONVERT(VARCHAR(22),GETDATE(),24) --15:04:07 +Select CONVERT(varchar(20),GETDATE(),14) --15:05:49:330 +``` + +# \ No newline at end of file