diff --git "a/08\345\273\226\346\237\217\346\210\220/2022-9-16\344\275\234\344\270\232/2022-09-15-\345\273\272\345\272\223\345\273\272\350\241\250\347\272\246\346\235\237.sql" "b/08\345\273\226\346\237\217\346\210\220/2022-9-16\344\275\234\344\270\232/2022-09-15-\345\273\272\345\272\223\345\273\272\350\241\250\347\272\246\346\235\237.sql" new file mode 100644 index 0000000000000000000000000000000000000000..50734a655915b8033939d0c91b95fa9b86dfc839 --- /dev/null +++ "b/08\345\273\226\346\237\217\346\210\220/2022-9-16\344\275\234\344\270\232/2022-09-15-\345\273\272\345\272\223\345\273\272\350\241\250\347\272\246\346\235\237.sql" @@ -0,0 +1,79 @@ +--创建使用数据库 DBTEST +CREATE DATABASE DBTEST; +USE dbtest +if exists (select * from sys.databases where name='sectionInfo') +drop table sectionInfo + + --部门信息表(sectionInfo) + --部门编号 sectionID int 标识列 主键 + --部门名称 sectionName varchar(10) 不能为空 + +create table sectionInfo( + sectionID int primary key identity(1,1), + sectionName varchar(10) not null, +); + +--插入数据 + insert into sectionInfo(sectionName) values('工程部'); + insert into sectionInfo(sectionName) values('科技部'); + insert into sectionInfo(sectionName) values('医疗部'); + insert into sectionInfo(sectionName) values('烹饪部'); + insert into sectionInfo(sectionName) values('生物部'); + + + + +--员工信息表(userInfo) +--员工编号 userNo int 标识列 主键 不允许为空 +--员工姓名 userName varchar(10) 唯一约束 不允许为空 长度必须大于4 +--员工性别 userSex varchar(2) 不允许为空 只能是男或女 +--员工年龄 userAge int 不能为空 范围在1-100之间 +--员工地址 userAddress varchar(50) 默认值为“湖北” +--员工部门 userSection int 外键,引用部门信息表的部门编号 + +create table userInfo( + userNo int identity(0,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(1<=userAge or userAge<=100), + userAddress varchar(50) default('湖北'), + userSection int references sectionInfo(sectionID) +); + +--插入数据 +insert into userInfo(userName,userSex,userAge,userAddress,userSection) values +('张三努努里','男','6','天津','0'), +('乌鸦坐飞机','女','3','上海','1'), +('苏胡亚军吴','男','65','南京','2'), +('真麻烦咖啡','男','24','天津','3'), +('遵纪守法龙','女','23','拉萨','4'); + + +--员工考勤表(workInfo) +--考勤编号 workId int 标识列 主键 不能为空 +--考勤员工 userId int 外键 引用员工信息表的员工编号 不能为空 +--考勤时间 workTime datetime 不能为空 +--考勤说明 workDescription varchar(40) 不能为空 内容只能是“迟到”,“早退”,“早退”,“病假”,“事假”中的一种 + +create table workInfo( + workId int primary key identity(1,1) not null, + userId int references userInfo(userNo) not null, + workTime date not null , + workDescription varchar(40) not null check(workDescription='迟到' or workDescription='早退'or workDescription='病假'or workDescription='事假'), +); + +--插入数据 + insert into workInfo(userId,workTime,workDescription) values ('1','2021-9-15','迟到'); + insert into workInfo(userId,workTime,workDescription) values ('3','2021-4-5','迟到'); + insert into workInfo(userId,workTime,workDescription) values ('5','2021-9-25','迟到'); + insert into workInfo(userId,workTime,workDescription) values ('2','2021-5-3','迟到'); + insert into workInfo(userId,workTime,workDescription) values ('4','2021-7-6','迟到'); + + + select * from sectionInfo; + select * from userInfo; + select * from workInfo; + + + + diff --git "a/08\345\273\226\346\237\217\346\210\220/2022-9-16\347\254\224\350\256\260/\346\225\260\346\215\256\345\272\223.txt" "b/08\345\273\226\346\237\217\346\210\220/2022-9-16\347\254\224\350\256\260/\346\225\260\346\215\256\345\272\223.txt" new file mode 100644 index 0000000000000000000000000000000000000000..a93071a3b70d3b08e77a6c20beb5e370397b12d3 --- /dev/null +++ "b/08\345\273\226\346\237\217\346\210\220/2022-9-16\347\254\224\350\256\260/\346\225\260\346\215\256\345\272\223.txt" @@ -0,0 +1,72 @@ + +鍒涘缓鏁版嵁搴擄細 +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) + + + --鍒犻櫎琛:drop table 琛ㄥ悕 + drop table StuInfo + + + --鎻掑叆鏁版嵁锛歩nsert into + + + + + + + + + + + +