约束(constrain)
2015-12-02 12:00
501 查看
sql 约束
约束用于限制加入表的数据的类型。
可以在创建表时规定约束(通过 create table 语句),或者在表创建之后也可以(通过 alter table语句)。
我们将主要探讨以下几种约束:
1.not null ---------------不为null
2.unique
---------------唯一约束
3.primary key --------------主键约束
4.foreign key -----------外键约束
5.check -------------条件约束
6.default
-------默认约束
下面将详细描述每一种约束(为了不使一篇博客太长部分约束写在下面的地址待完善):
3.primary key --------------主键约束:/article/9918610.html
4.foreign key -----------外键约束:/article/9918611.html
5.check -------------条件约束:/article/9918612.html
6.default
-------默认约束:/article/9918613.html
1.not null约束
描述:not null约束强制列不接受null值 。
not null约束强制字段始终包含值。这意味着,如果不向字段添加值,就无法插入新记录或者更新记录。
案例:
create table persons
(
id int not null,
name varchar(20) not null
)
2.unique约束
描述:unique约束唯一标识数据库表中的每一条记录。
unique和primary key约束均为列或列集合提供了唯一性的保证。
primary key拥有自动定义的unique约束。
注意:每个表可以有多个unique约束,但是每个表只能有一个primary key约束。
案例:
MySql:
create table persons
(
id int not null,
name varchar(20) not null,
unique(id)
)
SQL Server/Oracle/MS Access:
create table persons
(
id int not null unique,
name varchar(20) not null
)
如果需要命名unique约束,以及为多个列定义unique约束,请使用下面sql语法:
Mysql/sql server/oracle/ms access:
create table persons
(
id int not null,
name varchar(255) not null,
address varchar(255),
constraint uc_PersonID unique (id,name)
)
------uc_PersonID为约束名。
当表已经创建时,如需在id列创建unique约束,请使用下面sql:
mySql/sql server/oracle/ms access:
alter table persons add unique (id)
如需要命名unique约束,并定义多个列的unique约束,请使用下面的sql语句:
mySql/sql server/oracle/ms access:
alter table persons
add constraint uc_personID unique (id,name)
--uc_personID是unique约束的名称
撤销unique约束
如需撤销unique约束,请使用下面的sql:
MySQL:
alter table persons
drop index uc_personID
--uc_personID是unique的约束名称
sql Servers/Oracle/Ms access:
alter table persons
drop constraint uc_personID
--uc_personID是unique的约束名称
约束用于限制加入表的数据的类型。
可以在创建表时规定约束(通过 create table 语句),或者在表创建之后也可以(通过 alter table语句)。
我们将主要探讨以下几种约束:
1.not null ---------------不为null
2.unique
---------------唯一约束
3.primary key --------------主键约束
4.foreign key -----------外键约束
5.check -------------条件约束
6.default
-------默认约束
下面将详细描述每一种约束(为了不使一篇博客太长部分约束写在下面的地址待完善):
3.primary key --------------主键约束:/article/9918610.html
4.foreign key -----------外键约束:/article/9918611.html
5.check -------------条件约束:/article/9918612.html
6.default
-------默认约束:/article/9918613.html
1.not null约束
描述:not null约束强制列不接受null值 。
not null约束强制字段始终包含值。这意味着,如果不向字段添加值,就无法插入新记录或者更新记录。
案例:
create table persons
(
id int not null,
name varchar(20) not null
)
2.unique约束
描述:unique约束唯一标识数据库表中的每一条记录。
unique和primary key约束均为列或列集合提供了唯一性的保证。
primary key拥有自动定义的unique约束。
注意:每个表可以有多个unique约束,但是每个表只能有一个primary key约束。
案例:
MySql:
create table persons
(
id int not null,
name varchar(20) not null,
unique(id)
)
SQL Server/Oracle/MS Access:
create table persons
(
id int not null unique,
name varchar(20) not null
)
如果需要命名unique约束,以及为多个列定义unique约束,请使用下面sql语法:
Mysql/sql server/oracle/ms access:
create table persons
(
id int not null,
name varchar(255) not null,
address varchar(255),
constraint uc_PersonID unique (id,name)
)
------uc_PersonID为约束名。
当表已经创建时,如需在id列创建unique约束,请使用下面sql:
mySql/sql server/oracle/ms access:
alter table persons add unique (id)
如需要命名unique约束,并定义多个列的unique约束,请使用下面的sql语句:
mySql/sql server/oracle/ms access:
alter table persons
add constraint uc_personID unique (id,name)
--uc_personID是unique约束的名称
撤销unique约束
如需撤销unique约束,请使用下面的sql:
MySQL:
alter table persons
drop index uc_personID
--uc_personID是unique的约束名称
sql Servers/Oracle/Ms access:
alter table persons
drop constraint uc_personID
--uc_personID是unique的约束名称
相关文章推荐
- hdu 3376 Matrix Again 最大费用流
- copy,retain,assign,strong,weak的区别
- 使用servlet 3.0 新特性 @WebServlet(urlPatterns = "/brainT") 访问 url,服务器报404错误
- POJ 2110 Mountain Walking 二分+bfs
- grails 统一认证加单点登录
- ERROR: transport error 202: bind failed: Address already in use
- cookie中domain和path的解释
- MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main re
- Training->Using Network Service Discovery
- BAE+Python+Django+Wechatpy+Baidu weather api +微信订阅号 = 实现微信查询天气
- IO流--DataInputStream与DataOutputStream
- grails 开发增删改查 on IntelliJ IDEA
- crack will password
- wait()和notify()/notifyAll()
- sys.dm_os_waiting_tasks 引发的疑问(下)
- 静态Staitic
- sys.dm_os_waiting_tasks 引发的疑问(中)
- 多线程的wait和notify协同工作
- sys.dm_os_waiting_tasks 引发的疑问(上)
- leetcode Factorial Trailing Zeroes