diff --git "a/\346\210\264\344\277\212\351\224\213\357\274\210\344\270\200\357\274\211/.keep" "b/\346\210\264\344\277\212\351\224\213\357\274\210\344\270\200\357\274\211/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\346\210\264\344\277\212\351\224\213\357\274\210\344\270\200\357\274\211/\344\275\234\344\270\232/.keep" "b/\346\210\264\344\277\212\351\224\213\357\274\210\344\270\200\357\274\211/\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\346\210\264\344\277\212\351\224\213\357\274\210\344\270\200\357\274\211/\344\275\234\344\270\232/2022.9.15\344\275\234\344\270\232.sql" "b/\346\210\264\344\277\212\351\224\213\357\274\210\344\270\200\357\274\211/\344\275\234\344\270\232/2022.9.15\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e112875f6bb91ebce91b4dbcfdc5c26227116840 --- /dev/null +++ "b/\346\210\264\344\277\212\351\224\213\357\274\210\344\270\200\357\274\211/\344\275\234\344\270\232/2022.9.15\344\275\234\344\270\232.sql" @@ -0,0 +1,71 @@ +--创建数据库 DBTEST +if exists (select * from sys.databases where name = 'DBTEST') +drop database DBTEST +create database DBTEST; + + +--部门信息表(sectionInfo) +-- 部门编号 sectionID int 标识列 主键 +-- 部门名称 sectionName varchar(10) 不能为空 + +create table sectionInfo( + sectionID int primary key identity(1,1), + sectionName varchar(10) not null +) + +insert sectionInfo values('研发'); +insert sectionInfo values('生产'); +insert sectionInfo values('财务'); +insert sectionInfo values('人事'); +insert sectionInfo values('计划'); + +-- select * from sectionInfo; + +--员工信息表(userInfo) +-- 员工编号 userNo int 标识列 主键 不允许为空 +-- 员工姓名 userName varchar(10) 唯一约束 不允许为空 长度必须大于4 +-- 员工性别 userSex varchar(2) 不允许为空 只能是男或女 +-- 员工年龄 userAge int 不能为空 范围在1-100之间 +-- 员工地址 userAddress varchar(50) 默认值为“湖北” +-- 员工部门 userSection int 外键,引用部门信息表的部门编号 + +--drop table userInfo; + +create table userInfo ( + userNo int identity(1,1) primary key not null, + userName varchar(10) unique not null check(len(userName) > 4), + userSex varchar(2) not null check(userSex = '男' or userSex = '女') , + userAge int not null check(userAge between 1 and 100), + userAddress varchar(50) default '湖北', + userSection int references sectionInfo(sectionId) +); + +insert userInfo (userName,userSex,userAge,userAddress,userSection) +values ('张三123','男','25','福建','1'), + ('李四123','女','30','上海','2'), + ('王五123','女','36','上海','3'), + ('赵六123','女','40','广州','4'), + ('11223','女','27','广州','5'); +--select * from userInfo + +--员工考勤表(workInfo) +-- 考勤编号 workId int 标识列 主键 不能为空 +-- 考勤员工 userId int 外键 引用员工信息表的员工编号 不能为空 +-- 考勤时间 workTime datetime 不能为空 +-- 考勤说明 workDescription varchar(40) 不能为空 内容只能是“迟到”,“早退”,“旷工”,“病假”,“事假”中的一种 + +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) not null check(workDescription = '迟到' or workDescription = '早退' or workDescription = '旷工' or workDescription = '病假' or workDescription = '事假') +) +insert workInfo(userId,workTime,workDescription) +values (1,'2022-1-15 15:15:1','迟到'), + (2,'2022-1-16 15:15:1','早退'), + (3,'2022-1-14 15:15:1','旷工'), + (4,'2022-1-16 15:15:1','病假'), + (5,'2022-1-12 15:15:1','事假'); + + +--select * from workInfo \ No newline at end of file diff --git "a/\346\210\264\344\277\212\351\224\213\357\274\210\344\270\200\357\274\211/\347\254\224\350\256\260/.keep" "b/\346\210\264\344\277\212\351\224\213\357\274\210\344\270\200\357\274\211/\347\254\224\350\256\260/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\346\210\264\344\277\212\351\224\213\357\274\210\344\270\200\357\274\211/\347\254\224\350\256\260/2022.9.15\347\254\224\350\256\260.md" "b/\346\210\264\344\277\212\351\224\213\357\274\210\344\270\200\357\274\211/\347\254\224\350\256\260/2022.9.15\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..1d4f017fa879ef9163b7d6049805bde06c96c12d --- /dev/null +++ "b/\346\210\264\344\277\212\351\224\213\357\274\210\344\270\200\357\274\211/\347\254\224\350\256\260/2022.9.15\347\254\224\350\256\260.md" @@ -0,0 +1,53 @@ +# 1.绾︽潫 + +- 涓婚敭绾︽潫 primary key + + ```sql + create table a( + id int primary key identity(1,1) + ) + ``` + + ```sql + alter table 琛ㄥ悕 alter column 涓婚敭鍒楀悕 涓婚敭绫诲瀷 not null; + ``` + + + +- 澶栭敭绾︽潫 foreig key references + +```sql + --淇敼琛ㄧ粨鏋 琛ㄥ悕 add constraint 绾︽潫鍚 foreign key(瑕佸紩鐢ㄧ殑瀛楁) references 涓婚敭琛(瀛楁) + Alter table StuInfo add constraint FK_StuInfo_ClassId foreign key(ClassID) references ClassInfo(ClassID) +``` + +```sql +ClassID int references ClassInfo(ClassID) + ClassID int +``` + +- 榛樿 default + + ```sql + userAddress varchar(50) default '婀栧寳' + ``` + +- 鍞竴 unique + + ```sql + userName varchar(10) unique + ``` + +- check + +- 濡傛灉瀵瑰崟涓垪瀹氫箟 CHECK 绾︽潫锛岄偅涔堣鍒楀彧鍏佽鐗瑰畾鐨勫笺 + + 濡傛灉瀵逛竴涓〃瀹氫箟 CHECK 绾︽潫锛岄偅涔堟绾︽潫浼氬熀浜庤涓叾浠栧垪鐨勫煎湪鐗瑰畾鐨勫垪涓鍊艰繘琛岄檺鍒躲 + + ```sql + userAge int not null check(userAge between 1 and 100) + ``` + + + +- 闈炵┖ not null \ No newline at end of file diff --git "a/\346\210\264\344\277\212\351\224\213\357\274\210\344\272\214\357\274\211/.keep" "b/\346\210\264\344\277\212\351\224\213\357\274\210\344\272\214\357\274\211/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\346\210\264\344\277\212\351\224\213\357\274\210\344\272\214\357\274\211/2022.9.16\344\275\234\344\270\232.sql" "b/\346\210\264\344\277\212\351\224\213\357\274\210\344\272\214\357\274\211/2022.9.16\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ebf1aee7770fc08980360baa000a4dc69ad64bbc --- /dev/null +++ "b/\346\210\264\344\277\212\351\224\213\357\274\210\344\272\214\357\274\211/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/\346\210\264\344\277\212\351\224\213\357\274\210\344\272\214\357\274\211/\344\275\234\344\270\232/.keep" "b/\346\210\264\344\277\212\351\224\213\357\274\210\344\272\214\357\274\211/\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\346\210\264\344\277\212\351\224\213\357\274\210\344\272\214\357\274\211/\347\254\224\350\256\260/.keep" "b/\346\210\264\344\277\212\351\224\213\357\274\210\344\272\214\357\274\211/\347\254\224\350\256\260/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\346\210\264\344\277\212\351\224\213\357\274\210\344\272\214\357\274\211/\347\254\224\350\256\260/2022.9.16\347\254\224\350\256\260.md" "b/\346\210\264\344\277\212\351\224\213\357\274\210\344\272\214\357\274\211/\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/\346\210\264\344\277\212\351\224\213\357\274\210\344\272\214\357\274\211/\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