From e266eb153c1038b01ee0248cdf29081a42c4295b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A2=E5=9B=BD=E5=BB=BA?= <168718453@qq.com> Date: Fri, 16 Sep 2022 04:51:08 +0000 Subject: [PATCH] =?UTF-8?q?=E5=8D=A2=E5=9B=BD=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 卢国建 <168718453@qq.com> --- .../\344\275\234\344\270\232.sql" | 98 +++++++++++++++++++ .../\347\254\224\350\256\260.txt" | 19 ++++ 2 files changed, 117 insertions(+) create mode 100644 "\344\275\234\344\270\232/\344\275\234\344\270\232.sql" create mode 100644 "\344\275\234\344\270\232/\347\254\224\350\256\260.txt" diff --git "a/\344\275\234\344\270\232/\344\275\234\344\270\232.sql" "b/\344\275\234\344\270\232/\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..8266a0b --- /dev/null +++ "b/\344\275\234\344\270\232/\344\275\234\344\270\232.sql" @@ -0,0 +1,98 @@ +create database DBTEST; +use DBTEST; + + +--Ϣ +create table sectionInfo( + +--ű + sectionID int primary key identity (1001,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('Բ'); + + +select * from sectionInfo; + + +--ԱϢ +create table userInfo( + +--Ա + userNo int primary key identity (2001,1) not null, + + --Ա + userName varchar(10) not null unique 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(''), + +--ԱţòϢIJű + userSection int +); + + +select * from userInfo; + + +insert into userInfo(userName,userSex,userAge,userAddress,userSection) values('','',22,'',1001); +insert into userInfo(userName,userSex,userAge,userAddress,userSection) values('','',28,'',1002); +insert into userInfo(userName,userSex,userAge,userAddress,userSection) values('','',35,'',1003); +insert into userInfo(userName,userSex,userAge,userAddress,userSection) values('','Ů',22,'',1004); +insert into userInfo(userName,userSex,userAge,userAddress,userSection) values('','Ů',56,'',1005); + + + +--Աڱ +create table workinfo( + +--ڱ +workID int primary key not null identity(3001,1), + +--Ա,,õԱϢԱ +userId int not null, + +--ʱ +workTime datetime not null, + +--˵ֻǡٵˡ١¼١еһ +workDescription varchar(40) not null , + +check(workDescription='ٵ'or workDescription='' or workDescription='' or workDescription='' or workDescription='¼') + +); + + +insert into workinfo(userId,workTime,workDescription) values (2007,2021-2-11,'ٵ'); +insert into workinfo(userId,workTime,workDescription) values (2008,2022-3-13,''); +insert into workinfo(userId,workTime,workDescription) values (2009,2021-1-19,''); +insert into workinfo(userId,workTime,workDescription) values (2010,2022-7-1,''); +insert into workinfo(userId,workTime,workDescription) values (2011,2022-6-24,'¼'); + +select * from workinfo; + + +-- + +--ԱϢű + Alter table userInfo add constraint aaa foreign key (userSection) references sectionInfo(sectionID); + +--ԱڱԱϢ +Alter table workinfo add constraint bbb foreign key (userId) references userInfo(userNo); + + diff --git "a/\344\275\234\344\270\232/\347\254\224\350\256\260.txt" "b/\344\275\234\344\270\232/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..8036792 --- /dev/null +++ "b/\344\275\234\344\270\232/\347\254\224\350\256\260.txt" @@ -0,0 +1,19 @@ +create database(建数据库) DBTEST(数据库名称); +use DBTEST;(使用数据库) + + +create table(建表) userInfo(表名称)(这里是增加列 +语句1 +语句2 +..... + +) +primary key 是主键 +列名+类型+primary key 是主键+identity (1001,1)(这是自增 其中1001是其实点,1是自增量) 非空就直接跟 not null 注:这些语句的顺序是不定的 +check检查 例如 +列名+类型 check(len是长度(列名=4))这就是固定这列的长度为4 +其中假如 需要的需求是大于1小于100是无法check(1<列名<100)的了,得用or(或者)来弄check(1<列名 or 列名>100) + + +添加外键的方法有两种其中 Alter table 的意思是修改表结构 所以是在后面进行添加的 add就是添加的意思 constraint也就是固定结构 后面跟外键名称,foreign key也是固定 () +Alter table userInfo add constraint aaa foreign key (userSection) references sectionInfo(sectionID); \ No newline at end of file -- Gitee