--约束
约束用于确保数据库数据满足特定的商业规则
在sql server中,约束包括:not null,unique,primary key,foreign key,和check五种
1.not null 如果在列上定义了not null,那么当插入数据时,必须为列提供数据
--创建一张表
create table test
(
testId int primary key identity(1,1),
teatname varchar(30) not null, --不为空
testpass varchar(30) not null,
testage int
)
insert into test(testage) values(30)--会报错误,因为有两个字段是不允许为空的,如果将not null 去掉可以创建
insert into test(testname,testpass,testage)values('','',5)
--在没有加not null限制时,上面两条语句的效果是不一样的,注意体会
2.unique 当定义了唯一约束后,该列值是不能重复的,但是可以为null(主键是不能放空值的).
--创建一张表
create table test1
(
testId int primary key identity(1,1),
teatname varchar(30) unique, --唯一
testpass varchar(30) ,
testage int
)
insert into test2 (testname,testpass,testage) values('aa','1234','45')
当在加入一条相同的数据时,是不允许的
insert into test2 (testpass,testage) values('1234','45')
testname给空值是可以的,但是同样不能够连续给值
3.primary key 用于唯一的标示表行的数据,当定义主键约束后,该列不但不能重复而且不能为null,
需要说明的是一张表只能有一个主键,但是可以有多个unique约束。
4.foreign key
用于定义主表和从表之间的关系,外键约束要定义在从表上,主表则必须有主键约束或是unique约束,当定义外键约束后,要求外键列数据必须在主表的主键列存在或只是为null
5.check 用于强制行数据必须满足的条件,假定在sal列上定义了check约束,并要求sal列值在1000-2000之间,如果不在1000-2000之间,就会提示错误。
create table test2
(
testId int ,
teatname varchar(30),
testpass varchar(30),
sal int check (sal>=1000 and sal
)
insert into test2 values(4,'aa','aa',1000)--对的
--default使用
create table mes
(mesId int primary key identity(1,1),
mescon varchar(2000) not null,
mesDate datetime default getdate()
)
insert into mes (mescon,mesDate) values('你好么')--显示系统默认时间
insert into mes (mescon,mesDate) values('你好么','2000-11-11')--显示给定的时间,也就是default时,如果不给定则显示系统的,给定的话则显示给定的信息
--商店售货系统表设计案例
现有一个商店的数据库,记录客户及其购物情况,由下面三个表组成:
商品goods(商品号goodsId,商品名goodsName,单价unitprice,商品类category,供应商provider);
客户customer(客户号customerId,姓名customername,住址customeraddress,电邮email,性别sex,身份证cardId);
购买purchase(客户号customerId,商品号goodesId,购买数量nums);
请用sql 语言完成下列功能:
1.见表,在定义中要求声明:
(1)每个表的主外建
(2)客户的姓名不能为空值
(3)单价必须大于0,购买数量必须在1到30之间
(4)电邮不能重复
(5)客户的性别必须是 男或者女,默认是男
(6)商品类别是'食物'‘日用品’
--goods表
create table goods
(
goodsId nvarchar(50) primaty key,
goodsName nvarchar(80) not null,
unitprice numeric(10,2) check (unitprice>0),
category nvarchar(3) check (category in('食物'‘日用品’)),
provider nvarchar(50)
)
--customer
create table customer
(
customerId nvarchar(50) primaty key,
cusName nvarchar(50) not null,
address nvarchar(100),
email nvarchar(100), unique,
sex nchar(1) check(sex in('男','女')) default ‘男’
cardId nvarchar(18)
)
--purchase
create table purchase
(
customerId nvarchar(50) foreign key reherences customer(customerId),
goodsId nvarchar(50) foreign key reherences goods(goodsId),
nums int check (nums>0)
)
--约束
约束用于确保数据库数据满足特定的商业规则
在sql server中,约束包括:not null,unique,primary key,foreign key,和check五种
1.not null 如果在列上定义了not null,那么当插入数据时,必须为列提供数据
--创建一张表
create table test
(
testId int primary key identity(1,1),
teatname varchar(30) not null, --不为空
testpass varchar(30) not null,
testage int
)
insert into test(testage) values(30)--会报错误,因为有两个字段是不允许为空的,如果将not null 去掉可以创建
insert into test(testname,testpass,testage)values('','',5)
--在没有加not null限制时,上面两条语句的效果是不一样的,注意体会
2.unique 当定义了唯一约束后,该列值是不能重复的,但是可以为null(主键是不能放空值的).
--创建一张表
create table test1
(
testId int primary key identity(1,1),
teatname varchar(30) unique, --唯一
testpass varchar(30) ,
testage int
)
insert into test2 (testname,testpass,testage) values('aa','1234','45')
当在加入一条相同的数据时,是不允许的
insert into test2 (testpass,testage) values('1234','45')
testname给空值是可以的,但是同样不能够连续给值
3.primary key 用于唯一的标示表行的数据,当定义主键约束后,该列不但不能重复而且不能为null,
需要说明的是一张表只能有一个主键,但是可以有多个unique约束。
4.foreign key
用于定义主表和从表之间的关系,外键约束要定义在从表上,主表则必须有主键约束或是unique约束,当定义外键约束后,要求外键列数据必须在主表的主键列存在或只是为null
5.check 用于强制行数据必须满足的条件,假定在sal列上定义了check约束,并要求sal列值在1000-2000之间,如果不在1000-2000之间,就会提示错误。
create table test2
(
testId int ,
teatname varchar(30),
testpass varchar(30),
sal int check (sal>=1000 and sal
)
insert into test2 values(4,'aa','aa',1000)--对的
--default使用
create table mes
(mesId int primary key identity(1,1),
mescon varchar(2000) not null,
mesDate datetime default getdate()
)
insert into mes (mescon,mesDate) values('你好么')--显示系统默认时间
insert into mes (mescon,mesDate) values('你好么','2000-11-11')--显示给定的时间,也就是default时,如果不给定则显示系统的,给定的话则显示给定的信息
--商店售货系统表设计案例
现有一个商店的数据库,记录客户及其购物情况,由下面三个表组成:
商品goods(商品号goodsId,商品名goodsName,单价unitprice,商品类category,供应商provider);
客户customer(客户号customerId,姓名customername,住址customeraddress,电邮email,性别sex,身份证cardId);
购买purchase(客户号customerId,商品号goodesId,购买数量nums);
请用sql 语言完成下列功能:
1.见表,在定义中要求声明:
(1)每个表的主外建
(2)客户的姓名不能为空值
(3)单价必须大于0,购买数量必须在1到30之间
(4)电邮不能重复
(5)客户的性别必须是 男或者女,默认是男
(6)商品类别是'食物'‘日用品’
--goods表
create table goods
(
goodsId nvarchar(50) primaty key,
goodsName nvarchar(80) not null,
unitprice numeric(10,2) check (unitprice>0),
category nvarchar(3) check (category in('食物'‘日用品’)),
provider nvarchar(50)
)
--customer
create table customer
(
customerId nvarchar(50) primaty key,
cusName nvarchar(50) not null,
address nvarchar(100),
email nvarchar(100), unique,
sex nchar(1) check(sex in('男','女')) default ‘男’
cardId nvarchar(18)
)
--purchase
create table purchase
(
customerId nvarchar(50) foreign key reherences customer(customerId),
goodsId nvarchar(50) foreign key reherences goods(goodsId),
nums int check (nums>0)
)