可以在创建表,修改表时规定约束,主要有
- NOT NULL
- UNIQUE
- PRIMARY KEY
- FOREIGN KEY
- CHECK
- DEFAULT
NOT NUL
not null约束强制列不接受null值,强制字段始终包含值。即不向对应字段添加值,就无法插入新记录或更新记录。
create table persons ( id int not null, lastname varchar(20) not null, firstname varchar(20), address varchar(200), city varchar(40) )
UNIQUE
唯一标识表中的每条记录,与PRIMARY KEY均为列或列集合提供唯一保证,后者拥有自动定义的UNIQUE约束。每个表可以有多个UNIQUE约束,但只能有一个PRIMRY KEY.
-- on create table create table persons ( id int not null unique, lastname varchar(20) not null, firstname varchar(20), address varchar(200), city varchar(40) ) create table persons ( id int not null, lastname varchar(20) not null, firstname varchar(20), address varchar(200), city varchar(40), constraint uc_personID unique(id,lastname) ) -- on alter table alter table persons add unique (id) alter table persons add constraint uc_personID unique(id,lastname) -- drop index alter table persons drop constraint uc_personID
PRIMARY KEY
唯一标识数据库表中每条记录,主键必须包含唯一值,不能包含NULL值。每个表都应该有一个主键并且只能有一个主键。
-- on create table create table persons ( id int not null primary key, lastname varchar(20) not null, firstname varchar(20), address varchar(200), city varchar(40) ) create table persons ( id int not null, lastname varchar(20) not null, firstname varchar(20), address varchar(200), city varchar(40), constraint pk_personID primary key(id,lastname) ) -- on alter table alter table persons add primary key(id) alter table persons add constraint pk_personID primary key (id,lastname) -- drop index alter table persons drop constraint pk_personID
FOREIGN KEY
指向另一个表中的primary key,用来预防破坏表之间的连接和防止非法数据插入外键列。
-- on create table create table orders ( id int not null primary key, orderno int not null, personid int foreign key references persons(id) ) create table orders ( id int not null primary key, orderno int not null, personid int , constraint fk_personorders foreign key(personid) rerferences persons(id) ) -- on alter table alter table orders add foreign key(id) alter table persons add constraint fk_personID foreign key (id,lastname) -- drop index alter table persons drop constraint fk_personID
CHECK
限制列中的取值范围,如果对一表中的单个列应用,则对该列起作用,如果对一个表应用,则该约束会在特定列中对值进行限定。
-- on create table create table persons ( id int not null check(id>0), lastname varchar(20) not null, firstname varchar(20), address varchar(200), city varchar(40) ) create table persons ( id int not null, lastname varchar(20) not null , firstname varchar(20), address varchar(200), city varchar(40), constraint chk_person check(id>0 and city='Sandnes') ) -- on alter table alter table persons add check(id>0) alter table persons add constraint chk_person check(id>0 and city='Sandnes') -- drop index alter table persons drop constraint chk_person
DEFAULT
用于向列中插入默认值,如果没有指定值,将默认值添加到所有的新记录。
-- on create table create table persons ( id int not null, lastname varchar(20) not null , firstname varchar(20), address varchar(200), city varchar(40) default 'Sandnes' ) -- on alter table alter table persons alter column city set default 'Sandnes' -- drop index alter table persons alter column city drop default