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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 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 0000000000000000000000000000000000000000..4d8667c3078a7dd4d80902fd31ed37b0b7e0a268 --- /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 SERVER.md" "b/21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SERVER.md" new file mode 100644 index 0000000000000000000000000000000000000000..9804f3de587857bb3280e1c1be0b623aea248550 --- /dev/null +++ "b/21\345\272\204\344\272\221/\347\254\224\350\256\260/SQL SERVER.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 +``` +