SQL CHECK 約束
sql check 約束
check 約束用于限制列中的取值范圍。
如果對單個列定義 check 約束,那么該列只允許特定的值。
如果對一個表定義 check 約束,那么此約束會基于行中其他列的值在特定的列中對值進行限制。
1. create table 時的 sql check 約束
下面的 sql 在 "persons" 表創(chuàng)建時在 "p_id" 列上創(chuàng)建 check 約束。check 約束規(guī)定 "p_id" 列必須只包含大于 0 的整數。
mysql:
create table persons
(
p_id int not null,
lastname varchar(255) not null,
firstname varchar(255),
address varchar(255),
city varchar(255),
check (p_id>0)
)
(
p_id int not null,
lastname varchar(255) not null,
firstname varchar(255),
address varchar(255),
city varchar(255),
check (p_id>0)
)