From d36c483f190dabe4d57fa72f24d1f841e2188914 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=96=E8=B4=A2?= <10033750+wang-shicaishicai@user.noreply.gitee.com> Date: Thu, 15 Sep 2022 08:00:45 +0000 Subject: [PATCH 1/2] =?UTF-8?q?add=20=E9=93=BE=E6=8E=A5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王世财 <10033750+wang-shicaishicai@user.noreply.gitee.com> --- "\351\223\276\346\216\245" | 1 + 1 file changed, 1 insertion(+) create mode 100644 "\351\223\276\346\216\245" diff --git "a/\351\223\276\346\216\245" "b/\351\223\276\346\216\245" new file mode 100644 index 0000000..9531489 --- /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 -- Gitee From fa12011f7d09b9f1977c4f988d41a2b9e0e32ae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=96=E8=B4=A2?= <10033750+wang-shicaishicai@user.noreply.gitee.com> Date: Fri, 16 Sep 2022 08:59:06 +0000 Subject: [PATCH 2/2] =?UTF-8?q?20=E7=8E=8B=E4=B8=96=E8=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王世财 <10033750+wang-shicaishicai@user.noreply.gitee.com> --- .../2022.9.15\344\275\234\344\270\232.sql" | 71 +++++++++++++++++++ .../2022.9.15\347\254\224\350\256\260.md" | 53 ++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 "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" create mode 100644 "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" 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 0000000..e112875 --- /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 òϢIJű + +--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 0000000..1d4f017 --- /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 -- Gitee