diff --git "a/\345\224\220\347\232\223\351\242\226/.keep" "b/\345\224\220\347\232\223\351\242\226/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\345\224\220\347\232\223\351\242\226/2022-09-15-\345\273\272\345\272\223\345\273\272\350\241\250\347\272\246\346\235\237.sql" "b/\345\224\220\347\232\223\351\242\226/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..526c95eeea0bdab3b3c359d4b5cc675ed24bdedc --- /dev/null +++ "b/\345\224\220\347\232\223\351\242\226/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,78 @@ +--关系型数据库:SQL server, Mysql, Oracle +--创建数据库: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) + + + + --如果没给出列名,默认是按照顺序一个个添加 + --insert StuInfo values('女',13888888888) + + --insert StuInfo(stuphone) values(15888888888) + + + + select * from StuInfo; + + --字符串:char(5),varchar(5),nvarchar(5)之间区别是什么 + + + + --删除表:drop table 表名 + drop table StuInfo + + + --非空 + + --约束,自增(标识列) + + --外键 + --主键:默认唯一列 + --默认 + --唯一: + --check检查: + + --插入数据:insert into \ No newline at end of file diff --git "a/\345\224\220\347\232\223\351\242\226/\345\224\220\347\232\223\351\242\226.sql" "b/\345\224\220\347\232\223\351\242\226/\345\224\220\347\232\223\351\242\226.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9ec24ce0c4e419fe559db01ac0258581faa1ab27 --- /dev/null +++ "b/\345\224\220\347\232\223\351\242\226/\345\224\220\347\232\223\351\242\226.sql" @@ -0,0 +1,48 @@ +--鍒涘缓鏁版嵁搴 DBTEST +create database DBTEST; +--浣跨敤鏁版嵁搴 +use dbtest; +--閮ㄩ棬淇℃伅琛 +create table sectioninfo( + sectionID int primary key identity(1,1), + sectionName varchar(10) not null +); +--鍛樺伐淇℃伅琛 +create table userinfo( + userNo int primary key identity(1,1) not null, + userName varchar(10) not null unique check(len(userName)>4), + userSex varchar(2) not null default('鐢') check(userSex='鐢' or userSex='濂'), + userAge int not null check(userAge between 1 and 100), + userAddress varchar(50) default('婀栧寳'), + userSection INT +); +--鍛樺伐鑰冨嫟琛 +create table workinfo( + workId int primary key identity(1,1), + userId int references userinfo(userNo) not null , + workTime datetime not null, + workDescription varchar(40) not null check(workDescription in('杩熷埌','鏃╅','鏃峰伐','鐥呭亣','浜嬪亣')) +); +Alter table workInfo add constraint FK_workInfo_ClassId foreign key(userId) references userInfo(userNo); +--澧炲姞鏁版嵁 +insert into sectionInfo(sectionName) values + ('寮鍙戦儴'), + ('杩愯閮'), + ('浜轰簨閮'), + ('绛栧垝閮'), + ('绠$悊閮'); +select * from sectionInfo; +insert into userInfo(userName,userSex,userAge,userAddress,userSection) values + ('绠″瓙鍝','鐢',8,'绂忓缓',1), + ('鐜嬪瓙','鐢',9,'娉夊窞',2), + ('琚佽吹妫','濂',25,'姹熻タ',3), + ('cp','鐢',38,'鍘﹂棬',4), + ('鍙ゅ痉鐖','鐢',15,'鏈煡',5); +select * from userInfo; +insert into workInfo(userId,workTime,workDescription) values + (1,'2022-01-05 15:25:46','杩熷埌'), + (2,'2022-02-15 16:44:44','鏃╅'), + (3,'2022-03-29 12:25:46','鏃峰伐'), + (4,'2022-04-25 08:45:46','鐥呭亣'), + (5,'2022-06-18 19:25:46','浜嬪亣'); +select * from workInfo; \ No newline at end of file