Seven constraint

Constraints are used to limit the types of added data table.

Constraints can be defined (by when you create a table CREATE TABLE statement), or after the table can also be created (via ALTER TABLE statement).

. 1, the NOT NULL : NULL constraint force does not accept the column value.

2、UNIQUE:

UNIQUE constraint uniquely identifies each record in the database table.

UNIQUE and PRIMARY KEY constraints are column or set of columns to provide a guarantee of uniqueness.

PRIMARY KEY constraint automatically has a UNIQUE definition.

Note that each table can have multiple UNIQUE constraints, but each table can have only one PRIMARY KEY constraint.

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)
)

SQL Server / Oracle / MS Access:

CREATE TABLE Persons
(
Id_P int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
ALTER  TABLE Persons
 ADD  UNIQUE (Id_P)

3、PRIMARY KEY:

PRIMARY KEY constraint uniquely identifies each record in the database table.

Primary keys must contain unique values.

Primary key column can not contain a NULL value.

Each table should have a primary key, and each table can have only one primary key.

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)
)
ALTER TABLE Persons
ADD PRIMARY KEY (Id_P)

4、FOREIGN KEY:

A table pointing FOREIGN KEY PRIMARY KEY in another 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)
)
ALTER TABLE Orders
ADD FOREIGN KEY (Id_P)
REFERENCES Persons(Id_P)

Revocation of constraints:

ALTER  TABLE Orders
 DROP  FOREIGN  KEY fk_PerOrders #MySQL

ALTER TABLE Orders
DROP CONSTRAINT fk_PerOrders  #SQL Server / Oracle / MS Access:

5、CHECK :

CHECK constraints for limiting the range of values ​​in the column.

If CHECK constraints on the definition of a single column, then the value of the specific column only.

If the definition CHECK constraints on a table, then the constraint will limit the value of a particular column.

My SQL:  CHECK(ID_P>0)

      ALTER TABLE Persons ADD CHECK (Id_P>0)

      ALTER TABLE Persons DROP CHECK ID_P #取消约束

SQL Server / Oracle / MS Access:  ID_P int not null CHECK(ID_P>0)

      ALTER TABLE Persons DROP CONSTRAINT ID_P

6、DEFAULT :

DEFAULT constraint for inserting a default value to the column.

If no other value is specified, then the default value will be added to all new records.

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 'SANDNES'

ALTER TABLE Persons
ALTER City DROP DEFAULT

SQL Server / Oracle / MS Access:

ALTER TABLE Persons
ALTER COLUMN City SET DEFAULT 'SANDNES'

ALTER TABLE Persons
ALTER COLUMN City DROP DEFAULT

 

Guess you like

Origin www.cnblogs.com/soberkkk/p/12565770.html