From c6298d8b32997d8bcb60b391b8c899e5ec19ecd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BA=84=E4=BA=91?= <10033714+zfairyyyyyyy@user.noreply.gitee.com> Date: Fri, 16 Sep 2022 04:45:27 +0000 Subject: [PATCH 1/5] =?UTF-8?q?=E6=96=B0=E5=BB=BA=2021=E5=BA=84=E4=BA=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "21\345\272\204\344\272\221/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "21\345\272\204\344\272\221/.keep" diff --git "a/21\345\272\204\344\272\221/.keep" "b/21\345\272\204\344\272\221/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From df2953f17ae3d9428faa58426e783e04b966de32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BA=84=E4=BA=91?= <10033714+zfairyyyyyyy@user.noreply.gitee.com> Date: Fri, 16 Sep 2022 04:45:57 +0000 Subject: [PATCH 2/5] =?UTF-8?q?21=E5=BA=84=E4=BA=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 搴勪簯 <> --- ...2\345\272\223\345\273\272\350\241\250.sql" | 56 +++++++++++++++++ .../\347\254\224\350\256\260/SQL SEVER.md" | 60 +++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 "21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-9-15\345\273\272\345\272\223\345\273\272\350\241\250.sql" create mode 100644 "21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SEVER.md" diff --git "a/21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-9-15\345\273\272\345\272\223\345\273\272\350\241\250.sql" "b/21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-9-15\345\273\272\345\272\223\345\273\272\350\241\250.sql" new file mode 100644 index 0000000..4d8667c --- /dev/null +++ "b/21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-9-15\345\273\272\345\272\223\345\273\272\350\241\250.sql" @@ -0,0 +1,56 @@ + +--创建数据库 DBTEST +create database DBTEST; +use DBTEST; + --再创建表: + --1 + --部门信息表(sectionInfo) + create table sectionInfo( + sectionID int primary key identity(1,1),--部门编号 sectionID int 标识列 主键 + sectionName varchar(10) not null--部门名称 sectionName varchar(10) 不能为空 + ); + + --2 + --员工信息表(userInfo) + create table userInfo( + userNo int primary key identity(1,1) not null,--员工编号 userNo int 标识列 主键 不允许为空 + userName varchar(10) check(len(userName)>4) unique not null,--员工姓名 userName varchar(10) 唯一约束 不允许为空 长度必须大于4 + userSex varchar(2) not null check(userSex='男'or userSex='女'),--员工性别 userSex varchar(2) 不允许为空 只能是男或女 + userAge int not null check(userAge>=1 or userAge<=100),--员工年龄 userAge int 不能为空 范围在1-100之间 + userAddress varchar(50) default('湖北'),--员工地址 userAddress varchar(50) 默认值为“湖北” + userSection int references sectionInfo(sectionID)--员工部门 userSection int 外键,引用部门信息表的部门编号 +); + + --3 + --员工考勤表(workInfo) + create table workInfo( + workId int primary key identity(1,1) not null, --考勤编号 workId int 标识列 主键 不能为空 + userId int references userInfo(userNo) not null, --考勤员工 userId int 外键 引用员工信息表的员工编号 不能为空 + workTime datetime not null, --考勤时间 workTime datetime 不能为空 + workDescription varchar(40) not null check(workDescription='迟到' or workDescription='早退' or workDescription='旷工' or workDescription='病假' or workDescription='事假' ) --考勤说明 workDescription varchar(40) 不能为空 内容只能是“迟到”,“早退”,“旷工”,“病假”,“事假”中的一种 + ); + + + + --为每张表添加5条测试数据 + + insert into sectionInfo (sectionName) values + ('市场部'), + ('生产部'), + ('研发部'), + ('销售部'), + ('行政部'); + + insert into userInfo(userName,userSex,userAge,userAddress,userSection) values + ('张三','男',20,'福建',1), + ('李四','男',21,'广东',2), + ('王五','男',25,'上海',3), + ('小李','女',20,'北京',4), + ('小黄','女',26,'湖南',5); + + insert into workInfo(userId,workTime,workDescription) values + (2,'2022-01-05 15:25:46','迟到'), + (5,'2022-01-06 15:25:46','迟到'), + (1,'2022-01-07 15:25:46','迟到'), + (3,'2022-01-08 15:25:46','迟到'), + (4,'2022-01-05 15:25:46','迟到'); diff --git "a/21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SEVER.md" "b/21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SEVER.md" new file mode 100644 index 0000000..9804f3d --- /dev/null +++ "b/21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SEVER.md" @@ -0,0 +1,60 @@ +# SQL SEVER + +### 涓銆佹暟鎹簱 + +#### 鍒涘缓鏁版嵁搴 + +```sql +鍒ゆ柇鏄惁瀛樺湪鍚屽懡鍚嶇殑鏁版嵁搴 +if exists (select * from sys.databases where name='DBTEST') +鍒犻櫎鏁版嵁搴 +drop database DBTEST +鍒涘缓鏁版嵁搴 +create database DBTEST +``` + +#### 浣跨敤鏁版嵁搴 + +```sql +use DBTEST +``` + + + +### 浜屻佽〃 + +#### 鍒涘缓琛 + +```sql +create table StuInfo( +stuId int primary key identity(1001,1), --瀛︾敓ID + stugender varchar(2) not null default('鐢') check(stugender='鐢' or stugender='濂'), --瀛︾敓鎬у埆 + stuphone char(11) check(len(stuphone)=11) unique, + ClassID int references ClassInfo(ClassID) +) + +``` + +#### 鏉′欢 + +##### 鍐呴儴锛 + +- 涓婚敭锛歱rimary key +- 鑷锛歩ndentity锛坸,y锛塠浠巟寮濮嬫瘡娆″鍔爕] +- 闈炵┖锛歯ot null +- 榛樿锛歞efault锛堬級 +- check绾︽潫锛歝henk锛堟潯浠讹級 +- 鍞竴锛歶nique +- 澶栭敭锛(foreign key)references 琛ㄥ悕锛堝瓧娈靛悕锛 + +##### 澶栭儴锛 + +- 澶栭敭锛欰lter table 琛ㄥ悕 add constraint 绾︽潫鍚 foreign key(瀛楁鍚) references 涓婚敭琛(瀛楁) +- 澧炲姞瀛楁锛欰lter table 琛ㄥ悕 add 瀛楁鍚 鏉′欢 + +#### 鍒犻櫎琛 + +```sql + drop table StuInfo +``` + -- Gitee From 8996eb7f2f5d4b278575278c8355187def108ba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BA=84=E4=BA=91?= <10033714+zfairyyyyyyy@user.noreply.gitee.com> Date: Fri, 16 Sep 2022 09:32:38 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=2021=E5=BA=84?= =?UTF-8?q?=E4=BA=91/=E7=AC=94=E8=AE=B0/SQL=20SEVER.md=20=E4=B8=BA=2021?= =?UTF-8?q?=E5=BA=84=E4=BA=91/=E7=AC=94=E8=AE=B0/SQL=20SERVER.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\347\254\224\350\256\260/SQL SERVER.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SEVER.md" => "21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SERVER.md" (100%) diff --git "a/21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SEVER.md" "b/21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SERVER.md" similarity index 100% rename from "21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SEVER.md" rename to "21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SERVER.md" -- Gitee From 1b3cb0fa329edca3e550711797ea1cd9feacf2ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BA=84=E4=BA=91?= <1487274726@qq.com> Date: Sun, 18 Sep 2022 07:15:47 +0000 Subject: [PATCH 4/5] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2021?= =?UTF-8?q?=E5=BA=84=E4=BA=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "21\345\272\204\344\272\221/.keep" | 0 ...2\345\272\223\345\273\272\350\241\250.sql" | 56 ----------------- .../\347\254\224\350\256\260/SQL SERVER.md" | 60 ------------------- 3 files changed, 116 deletions(-) delete mode 100644 "21\345\272\204\344\272\221/.keep" delete mode 100644 "21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-9-15\345\273\272\345\272\223\345\273\272\350\241\250.sql" delete mode 100644 "21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SERVER.md" diff --git "a/21\345\272\204\344\272\221/.keep" "b/21\345\272\204\344\272\221/.keep" deleted file mode 100644 index e69de29..0000000 diff --git "a/21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-9-15\345\273\272\345\272\223\345\273\272\350\241\250.sql" "b/21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-9-15\345\273\272\345\272\223\345\273\272\350\241\250.sql" deleted file mode 100644 index 4d8667c..0000000 --- "a/21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-9-15\345\273\272\345\272\223\345\273\272\350\241\250.sql" +++ /dev/null @@ -1,56 +0,0 @@ - ---创建数据库 DBTEST -create database DBTEST; -use DBTEST; - --再创建表: - --1 - --部门信息表(sectionInfo) - create table sectionInfo( - sectionID int primary key identity(1,1),--部门编号 sectionID int 标识列 主键 - sectionName varchar(10) not null--部门名称 sectionName varchar(10) 不能为空 - ); - - --2 - --员工信息表(userInfo) - create table userInfo( - userNo int primary key identity(1,1) not null,--员工编号 userNo int 标识列 主键 不允许为空 - userName varchar(10) check(len(userName)>4) unique not null,--员工姓名 userName varchar(10) 唯一约束 不允许为空 长度必须大于4 - userSex varchar(2) not null check(userSex='男'or userSex='女'),--员工性别 userSex varchar(2) 不允许为空 只能是男或女 - userAge int not null check(userAge>=1 or userAge<=100),--员工年龄 userAge int 不能为空 范围在1-100之间 - userAddress varchar(50) default('湖北'),--员工地址 userAddress varchar(50) 默认值为“湖北” - userSection int references sectionInfo(sectionID)--员工部门 userSection int 外键,引用部门信息表的部门编号 -); - - --3 - --员工考勤表(workInfo) - create table workInfo( - workId int primary key identity(1,1) not null, --考勤编号 workId int 标识列 主键 不能为空 - userId int references userInfo(userNo) not null, --考勤员工 userId int 外键 引用员工信息表的员工编号 不能为空 - workTime datetime not null, --考勤时间 workTime datetime 不能为空 - workDescription varchar(40) not null check(workDescription='迟到' or workDescription='早退' or workDescription='旷工' or workDescription='病假' or workDescription='事假' ) --考勤说明 workDescription varchar(40) 不能为空 内容只能是“迟到”,“早退”,“旷工”,“病假”,“事假”中的一种 - ); - - - - --为每张表添加5条测试数据 - - insert into sectionInfo (sectionName) values - ('市场部'), - ('生产部'), - ('研发部'), - ('销售部'), - ('行政部'); - - insert into userInfo(userName,userSex,userAge,userAddress,userSection) values - ('张三','男',20,'福建',1), - ('李四','男',21,'广东',2), - ('王五','男',25,'上海',3), - ('小李','女',20,'北京',4), - ('小黄','女',26,'湖南',5); - - insert into workInfo(userId,workTime,workDescription) values - (2,'2022-01-05 15:25:46','迟到'), - (5,'2022-01-06 15:25:46','迟到'), - (1,'2022-01-07 15:25:46','迟到'), - (3,'2022-01-08 15:25:46','迟到'), - (4,'2022-01-05 15:25:46','迟到'); diff --git "a/21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SERVER.md" "b/21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SERVER.md" deleted file mode 100644 index 9804f3d..0000000 --- "a/21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SERVER.md" +++ /dev/null @@ -1,60 +0,0 @@ -# SQL SEVER - -### 涓銆佹暟鎹簱 - -#### 鍒涘缓鏁版嵁搴 - -```sql -鍒ゆ柇鏄惁瀛樺湪鍚屽懡鍚嶇殑鏁版嵁搴 -if exists (select * from sys.databases where name='DBTEST') -鍒犻櫎鏁版嵁搴 -drop database DBTEST -鍒涘缓鏁版嵁搴 -create database DBTEST -``` - -#### 浣跨敤鏁版嵁搴 - -```sql -use DBTEST -``` - - - -### 浜屻佽〃 - -#### 鍒涘缓琛 - -```sql -create table StuInfo( -stuId int primary key identity(1001,1), --瀛︾敓ID - stugender varchar(2) not null default('鐢') check(stugender='鐢' or stugender='濂'), --瀛︾敓鎬у埆 - stuphone char(11) check(len(stuphone)=11) unique, - ClassID int references ClassInfo(ClassID) -) - -``` - -#### 鏉′欢 - -##### 鍐呴儴锛 - -- 涓婚敭锛歱rimary key -- 鑷锛歩ndentity锛坸,y锛塠浠巟寮濮嬫瘡娆″鍔爕] -- 闈炵┖锛歯ot null -- 榛樿锛歞efault锛堬級 -- check绾︽潫锛歝henk锛堟潯浠讹級 -- 鍞竴锛歶nique -- 澶栭敭锛(foreign key)references 琛ㄥ悕锛堝瓧娈靛悕锛 - -##### 澶栭儴锛 - -- 澶栭敭锛欰lter table 琛ㄥ悕 add constraint 绾︽潫鍚 foreign key(瀛楁鍚) references 涓婚敭琛(瀛楁) -- 澧炲姞瀛楁锛欰lter table 琛ㄥ悕 add 瀛楁鍚 鏉′欢 - -#### 鍒犻櫎琛 - -```sql - drop table StuInfo -``` - -- Gitee From e28bedd55afd30f14091d8bad0aa8b02fe583644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BA=84=E4=BA=91?= <1487274726@qq.com> Date: Sun, 18 Sep 2022 15:33:27 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2\345\272\223\345\273\272\350\241\250.sql" | 56 +++++ .../2022-9-18\346\237\245\350\257\242.sql" | 44 ++++ .../\347\254\224\350\256\260/SQL SERVER.md" | 194 ++++++++++++++++++ 3 files changed, 294 insertions(+) create mode 100644 "21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-9-15\345\273\272\345\272\223\345\273\272\350\241\250.sql" create mode 100644 "21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-9-18\346\237\245\350\257\242.sql" create mode 100644 "21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SERVER.md" diff --git "a/21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-9-15\345\273\272\345\272\223\345\273\272\350\241\250.sql" "b/21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-9-15\345\273\272\345\272\223\345\273\272\350\241\250.sql" new file mode 100644 index 0000000..e9e03ce --- /dev/null +++ "b/21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-9-15\345\273\272\345\272\223\345\273\272\350\241\250.sql" @@ -0,0 +1,56 @@ + +--创建数据库 DBTEST +create database DBTEST; +use DBTEST; + --再创建表: + --1 + --部门信息表(sectionInfo) + create table sectionInfo( + sectionID int primary key identity(1,1),--部门编号 sectionID int 标识列 主键 + sectionName varchar(10) not null--部门名称 sectionName varchar(10) 不能为空 + ); + + --2 + --员工信息表(userInfo) + create table userInfo( + userNo int primary key identity(1,1) not null,--员工编号 userNo int 标识列 主键 不允许为空 + userName varchar(10) check(len(userName)>4) unique not null,--员工姓名 userName varchar(10) 唯一约束 不允许为空 长度必须大于4 + userSex varchar(2) not null check(userSex='男'or userSex='女'),--员工性别 userSex varchar(2) 不允许为空 只能是男或女 + userAge int not null check(userAge>=1 or userAge<=100),--员工年龄 userAge int 不能为空 范围在1-100之间 + userAddress varchar(50) default('湖北'),--员工地址 userAddress varchar(50) 默认值为“湖北” + userSection int references sectionInfo(sectionID)--员工部门 userSection int 外键,引用部门信息表的部门编号 +); + + --3 + --员工考勤表(workInfo) + create table workInfo( + workId int primary key identity(1,1) not null, --考勤编号 workId int 标识列 主键 不能为空 + userId int references userInfo(userNo) not null, --考勤员工 userId int 外键 引用员工信息表的员工编号 不能为空 + workTime datetime not null, --考勤时间 workTime datetime 不能为空 + workDescription varchar(40) not null check(workDescription='迟到' or workDescription='早退' or workDescription='旷工' or workDescription='病假' or workDescription='事假' ) --考勤说明 workDescription varchar(40) 不能为空 内容只能是“迟到”,“早退”,“旷工”,“病假”,“事假”中的一种 + ); + + + + --为每张表添加5条测试数据 + + insert into sectionInfo (sectionName) values + ('市场部'), + ('生产部'), + ('研发部'), + ('销售部'), + ('行政部'); + + insert into userInfo(userName,userSex,userAge,userAddress,userSection) values + ('张三','男',20,'福建',1), + ('李四','男',21,'广东',2), + ('王五','男',25,'上海',3), + ('小李','女',20,'北京',4), + ('小黄','女',26,'湖南',5); + + insert into workInfo(userId,workTime,workDescription) values + (2,'2022-01-05 15:25:46','迟到'), + (5,'2022-01-06 15:25:46','迟到'), + (1,'2022-01-07 15:25:46','迟到'), + (3,'2022-01-08 15:25:46','迟到'), + (4,'2022-01-05 15:25:46','迟到'); diff --git "a/21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-9-18\346\237\245\350\257\242.sql" "b/21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-9-18\346\237\245\350\257\242.sql" new file mode 100644 index 0000000..b42e2ef --- /dev/null +++ "b/21\345\272\204\344\272\221/\344\275\234\344\270\232/2022-9-18\346\237\245\350\257\242.sql" @@ -0,0 +1,44 @@ +select * from Department; +select * from People; +select * from Rank; +--1. 查询出武汉地区所有的员工信息,要求显示部门名称以及员工的详细资料 +select p.* ,d.DepartmentName from People p +join Department d on d.DepartmentId=p.DepartmentId + where PeopleAddress='武汉'; +--2. 查询出武汉地区所有的员工信息,要求显示部门名称,职级名称以及员工的详细资料 +select p.* ,d.DepartmentName,r.RankName from People p +join Department d on d.DepartmentId=p.DepartmentId +join Rank r on r.RankId=p.RankId + where PeopleAddress='武汉'; +--3. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资。 +select DepartmentId ,COUNT(*) 员工人数,SUM(PeopleSalary) 员工工资总和 ,AVG(PeopleSalary) 平均工资,MAX(PeopleSalary) 最高工资,MIN(PeopleSalary) 最低工资 from People +group by DepartmentId +--4. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,平均工资在10000 以下的不参与统计,并且根据平均工资降序排列。 +select DepartmentId ,COUNT(*) 员工人数,SUM(PeopleSalary) 员工工资总和 ,AVG(PeopleSalary) 平均工资,MAX(PeopleSalary) 最高工资,MIN(PeopleSalary) 最低工资 from People +group by DepartmentId +having AVG(PeopleSalary) >=10000 +order by 平均工资 desc +--5. 根据部门名称,然后根据职位名称,分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资 +select d.DepartmentName ,COUNT(*) 员工人数,SUM(PeopleSalary) 员工工资总和 ,AVG(PeopleSalary) 平均工资,MAX(PeopleSalary) 最高工资,MIN(PeopleSalary) 最低工资 from People p +join Department d on d.DepartmentId=p.DepartmentId +group by d.DepartmentName,RankId +--查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) +select *,case YEAR(PeopleBirth) % 12 +when 4 then '鼠' +when 5 then '牛' +when 6 then '虎' +when 7 then '兔' +when 8 then '龙' +when 9 then '蛇' +when 10 then '马' +when 11 then '羊' +when 0 then '猴' +when 1 then '鸡' +when 2 then '狗' +when 3 then '猪' +end 属相 + from People +--查询出巨蟹 6.22--7.22 的员工信息 +select * from People where +(month(PeopleBirth) = 6 and DAY(PeopleBirth) >= 22) or +(month(PeopleBirth) = 7 and DAY(PeopleBirth) <= 22) \ No newline at end of file diff --git "a/21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SERVER.md" "b/21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SERVER.md" new file mode 100644 index 0000000..fa8e48e --- /dev/null +++ "b/21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SERVER.md" @@ -0,0 +1,194 @@ +# SQL SERVER + +### 涓銆佹暟鎹簱 + +#### 鍒涘缓鏁版嵁搴 + +```sql +鍒ゆ柇鏄惁瀛樺湪鍚屽懡鍚嶇殑鏁版嵁搴 +if exists (select * from sys.databases where name='DBTEST') +鍒犻櫎鏁版嵁搴 +drop database DBTEST +鍒涘缓鏁版嵁搴 +create database DBTEST +``` + +#### 浣跨敤鏁版嵁搴 + +```sql +use DBTEST +``` + + + +### 浜屻佽〃 + +#### 鍒涘缓琛 + +```sql +create table StuInfo( +stuId int primary key identity(1001,1), --瀛︾敓ID + stugender varchar(2) not null default('鐢') check(stugender='鐢' or stugender='濂'), --瀛︾敓鎬у埆 + stuphone char(11) check(len(stuphone)=11) unique, + ClassID int references ClassInfo(ClassID) +) + +``` + +#### 鏉′欢 + +##### 鍐呴儴锛 + +- 涓婚敭锛歱rimary key +- 鑷锛歩ndentity锛坸,y锛塠浠巟寮濮嬫瘡娆″鍔爕] +- 闈炵┖锛歯ot null +- 榛樿锛歞efault锛堬級 +- check绾︽潫锛歝henk锛堟潯浠讹級 +- 鍞竴锛歶nique +- 澶栭敭锛(foreign key)references 琛ㄥ悕锛堝瓧娈靛悕锛 + +##### 澶栭儴锛 + +- 澶栭敭锛欰lter table 琛ㄥ悕 add constraint 绾︽潫鍚 foreign key(瀛楁鍚) references 涓婚敭琛(瀛楁) +- 澧炲姞瀛楁锛欰lter table 琛ㄥ悕 add 瀛楁鍚 鏉′欢 + +#### 鍒犻櫎琛 + +```sql +鍒犻櫎琛 +drop table StuInfo +娓呯┖琛ㄥ唴瀹 +truncate +``` + +### SQL SERVER鐨勫熀鏈搷浣 + +#### 澧炲垹鏀规煡 + +1. ### 澧 + + ```sql + insert into 琛ㄥ悕() values () + ``` + + + +2. #### 鍒 + + ```sql + delete from 琛ㄥ悕 where 鏉′欢 + ``` + + + +3. #### 鏀 + +4. #### 鏌 + + ##### SQL璇彞鎵ц椤哄簭: + + ```sql + (7) SELECT + (8) DISTINCT + (1) FROM + (3) JOIN + (2) ON + (4) WHERE + (5) GROUP BY + (6) HAVING + (9) ORDER BY + (10) LIMIT + ``` + + +##### **SQL涓父鐢ㄨ繍绠楃** + +```sql +=锛氱瓑浜庯紝姣旇緝鏄惁鐩哥瓑鍙婅祴鍊 +!=锛氭瘮杈冧笉绛変簬 +>锛氭瘮杈冨ぇ浜 +<锛氭瘮杈冨皬浜 +>=锛氭瘮杈冨ぇ浜庣瓑浜 +<=锛氭瘮杈冨皬浜庣瓑浜 +IS NULL锛氭瘮杈冧负绌 +IS NOT NULL锛氭瘮杈冧笉涓虹┖ +in锛氭瘮杈冩槸鍚﹀湪鍏朵腑 +like锛氭ā绯婃煡璇 +BETWEEN...AND...锛氭瘮杈冩槸鍚﹀湪涓よ呬箣闂 50-100 BETWEEN 50 AND 100 +and锛氶昏緫涓庯紙涓や釜鏉′欢鍚屾椂鎴愮珛琛ㄨ揪寮忔垚绔嬶級 +or锛氶昏緫鎴栵紙涓や釜鏉′欢鏈変竴涓垚绔嬭〃杈惧紡鎴愮珛锛 +not锛氶昏緫闈烇紙鏉′欢鎴愮珛锛岃〃杈惧紡鍒欎笉鎴愮珛锛涙潯浠朵笉鎴愮珛锛岃〃杈惧紡鍒欐垚绔嬶級 +``` + +妯$硦鏌ヨ浣跨敤like鍏抽敭瀛楀拰閫氶厤绗︾粨鍚堟潵瀹炵幇锛岄氶厤绗﹀叿浣撳惈涔夊涓嬶細 + +```sql +%锛氫唬琛ㄥ尮閰0涓瓧绗︺1涓瓧绗︽垨澶氫釜瀛楃銆 +_锛氫唬琛ㄥ尮閰嶆湁涓斿彧鏈1涓瓧绗︺ +[]锛氫唬琛ㄥ尮閰嶈寖鍥村唴 +[^]锛氫唬琛ㄥ尮閰嶄笉鍦ㄨ寖鍥村唴 +``` + +SQL SERVER涓仛鍚堝嚱鏁颁富瑕佹湁锛 + +```sql +count:姹傛暟閲 +max:姹傛渶澶у +min:姹傛渶灏忓 +sum:姹傚拰 +avg:姹傚钩鍧囧 +``` + +ROUND鍑芥暟鐢ㄦ硶锛 + +```sql +round(num,len,[type]) +鍏朵腑: +num琛ㄧず闇瑕佸鐞嗙殑鏁板瓧锛宭en琛ㄧず闇瑕佷繚鐣欑殑闀垮害锛宼ype澶勭悊绫诲瀷(0鏄粯璁ゅ间唬琛ㄥ洓鑸嶄簲鍏ワ紝闈0浠h〃鐩存帴鎴彇) +select ROUND(123.45454,3) --123.45500 +select ROUND(123.45454,3,1) --123.45400 +``` + +CONVERT()涓嶤AST()鍑芥暟: + +```sql +--1.淇濈暀灏忔暟 +convert(decimal(13,2),12.45454) +cast(12.45454 as decimal(13,2)) +--2.寮哄埗杞崲绫诲瀷 +``` + +#### SQL涓父鐢ㄧ殑鏃堕棿鍑芥暟 + +```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 +``` + +**鏃堕棿鏍煎紡鎺у埗瀛楃涓诧細** + +| 鍚嶇О | 鏃ユ湡鍗曚綅 | 缂╁啓 | +| ------------ | ----------- | --------- | +| 骞 | year | yyyy 鎴杫y | +| 瀛e害 | quarter | qq,q | +| 鏈 | month | mm,m | +| 涓骞翠腑绗嚑澶 | dayofyear | dy,y | +| 鏃 | day | dd,d | +| 涓骞翠腑绗嚑鍛 | week | wk,ww | +| 鏄熸湡 | weekday | dw | +| 灏忔椂 | Hour | hh | +| 鍒嗛挓 | minute | mi,n | +| 绉 | second | ss,s | +| 姣 | millisecond | ms | -- Gitee