SQL约束

本文详细介绍了SQL中的六种约束:NOT NULL、UNIQUE、PRIMARY KEY、FOREIGN KEY、CHECK及DEFAULT的作用与使用方法,并通过实例展示了如何在创建或修改表时应用这些约束。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  可以在创建表,修改表时规定约束,主要有

  • 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

 

转载于:https://www.cnblogs.com/free-coder/p/4192218.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值