NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT
UNIQUE 和 PRIMARY KEY 约束均为列或列集合提供了唯一性的保证。
区别:每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束。
unique
sql unique Constraint on create table(表未被创建)
MySql
CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
UNIQUE (Id_P)
)
CREATE TABLE Persons
(
Id_P int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName)
)
表已被创建
alter table Persons add unique(Id_P)
alter table Persons add constraint uc_PersonID UNIQUE (Id_P,LastName)
MySql
alter table Persons drop index uc_PersonID
SQL Server / Oracle / MS Access:
alter table Persons drop constraint uc_PersonID
MySQL:
CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (Id_P)
)
SQL Server / Oracle / MS Access:
CREATE TABLE Persons
(
Id_P int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
MySQL / SQL Server / Oracle / MS Access(为多个列定义 PRIMARY KEY 约束):
CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT pk_PersonID PRIMARY KEY (Id_P,LastName)
)
表已被创建
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Persons ADD PRIMARY KEY (Id_P)
为多个列定义 PRIMARY KEY 约束
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Persons ADD CONSTRAINT pk_PersonID PRIMARY KEY (Id_P,LastName)
撤销 PRIMARY KEY 约束
MySQL:
ALTER TABLE Persons DROP PRIMARY KEY
SQL Server / Oracle / MS Access:
ALTER TABLE Persons DROP CONSTRAINT pk_PersonID
FOREIGN KEY 约束
FOREIGN KEY 约束用于预防破坏表之间连接的动作。
FOREIGN KEY 约束也能防止非法数据插入外键列,因为它必须是它指向的那个表中的值之一。
SQL FOREIGN KEY Constraint on CREATE TABLE
MySQL:
CREATE TABLE Orders
(
Id_O int NOT NULL,
OrderNo int NOT NULL,
Id_P int,
primary key (Id_O),
foreign key (Id_P) references Persons(Id_P)
)
SQL Server / Oracle / MS Access:
CREATE TABLE Orders
(
Id_O int NOT NULL primary key,
OrderNo int NOT NULL,
Id_P int foreign key references Persons(Id_P)
)
为多个列定义 FOREIGN KEY 约束:
MySQL / SQL Server / Oracle / MS Access:
CREATE TABLE Orders
(
Id_O int NOT NULL,
OrderNo int NOT NULL,
Id_P int,
PRIMARY KEY (Id_O),
constraint fk_PerOrders foreign key (Id_P)
references Persons(Id_P)
)
表已存在
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Orders
ADD foreign KEY (Id_P)
references Persons(Id_P)
为多个列定义 FOREIGN KEY 约束
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Orders
Add constraint fk_PerOrders
Foreign Key (Id_P)
References Persons(Id_P)
撤销 FOREIGN KEY 约束
MySQL:
ALTER TABLE Orders
DROP foreign key fk_PerOrders
SQL Server / Oracle / MS Access:
ALTER TABLE Orders
DROP constraint fk_PerOrders
(CHECK 约束用于限制列中的值的范围。)
如果对单个列定义 CHECK 约束,那么该列只允许特定的值。
如果对一个表定义 CHECK 约束,那么此约束会在特定的列中对值进行限制。
SQL CHECK Constraint on CREATE TABLE
My SQL:
CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
check (Id_P>0)
)
SQL Server / Oracle / MS Access:
CREATE TABLE Persons
(
Id_P int NOT NULL check (Id_P>0),
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
为多个列定义 CHECK 约束:
MySQL / SQL Server / Oracle / MS Access:
create table Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
constraint chk_Person check (Id_P>0 and City='Sandnes')
)
表已存在
MySQL / SQL Server / Oracle / MS Access:
alter table Persons
Add check (Id_P>0)
为多个列定义 CHECK 约束
MySQL / SQL Server / Oracle / MS Access:
alter table Persons
ADD constraint chk_Person CHECK (Id_P>0 AND City='Sandnes')
撤销 CHECK 约束
SQL Server / Oracle / MS Access:
alter table Persons
drop constraint chk_Person
MySQL:
alter table Persons
drop check chk_Person
SQL DEFAULT 约束
DEFAULT 约束用于向列中插入默认值。
如果没有规定其他的值,那么会将默认值添加到所有的新记录。
SQL DEFAULT Constraint on CREATE TABLE
My SQL / SQL Server / Oracle / MS Access:
CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT 'Sandnes'
)
通过使用类似 GETDATE() 这样的函数,DEFAULT 约束也可以用于插入系统值:
CREATE TABLE Orders
(
Id_O int NOT NULL,
OrderNo int NOT NULL,
Id_P int,
OrderDate date DEFAULT GETDATE()
)
表已存在
MySQL:
alter table Persons
alter City set default 'sandes'
SQL Server / Oracle / MS Access:
alter table Persons
alter column City set default 'sandnes'
撤销 DEFAULT 约束
MySQL:
alter table Persons
alter City drop default
SQL Server / Oracle / MS Access:
alter table Persons
alter column city drop default