diff --git "a/20\347\216\213\344\270\226\350\264\242/\344\275\234\344\270\232/2022.9.15\344\275\234\344\270\232.sql" "b/20\347\216\213\344\270\226\350\264\242/\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/20\347\216\213\344\270\226\350\264\242/\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/20\347\216\213\344\270\226\350\264\242/\347\254\224\350\256\260/2022.9.15\347\254\224\350\256\260.md" "b/20\347\216\213\344\270\226\350\264\242/\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/20\347\216\213\344\270\226\350\264\242/\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/\351\223\276\346\216\245" "b/\351\223\276\346\216\245" new file mode 100644 index 0000000000000000000000000000000000000000..9531489de623cde3db375e5693d4473418d5b8c2 --- /dev/null +++ "b/\351\223\276\346\216\245" @@ -0,0 +1 @@ +https://gitee.com/organizations/level-21-software-class-4/invite?invite=886e51af49d4c8e870217e863b9617a4db8ea8872269409fe67a7010cd42d36e1326d508fe5ad93be766b0d826817bc9 \ No newline at end of file