Constraint statements for SQL

Source: Internet
Author: User
Tags getdate one table sql primary key

SQL constraints (Constraints)

Constraints are used to restrict the type of data that is joined to a table.

You can specify constraints (through the CREATE TABLE statement) When you create the table, or you can (via the ALTER table statement) after the table is created.

We will focus on the following constraints:

    • Not NULL (non-null constraint)
    • Unique (single constraint)
    • PRIMARY key (primary KEY constraint)
    • FOREIGN key (FOREIGN KEY constraint)
    • CHECK (SELECT constraint)
    • Default constraint

SQL not NULL constraint

The NOT NULL constraint enforces that the column does not accept null values.

A NOT NULL constraint forces a field to always contain a value. This means that if you do not add a value to the field, you cannot insert a new record or update the record.

The following SQL statement enforces that the "id_p" column and the "LastName" column do not accept NULL values:

NOT NULLNOT NULL, FirstName varchar (255), Address varchar (255), City varchar (255))

SQL UNIQUE constraints

Unique constraints uniquely identify each record in a database table.

Both the unique and PRIMARY KEY constraints provide a unique guarantee for a column or column collection.

PRIMARY KEY has a UNIQUE constraint that is automatically defined.

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

SQL UNIQUE Constraint on CREATE TABLE

The following SQL creates a UNIQUE constraint in the "id_p" column when the "Persons" table is created:

Mysql:
CREATE TABLE Persons (id_p int not null,lastname varchar (255) not null,firstname varchar (255), Address varchar (255), City VA Rchar (255), UNIQUE (Id_P) )
SQL Server/oracle/ms Access:
UNIQUE, LastName varchar (255) not null,firstname varchar (255), Address varchar (255), City varchar (255))

If you need to name a unique constraint and define a UNIQUE constraint for multiple columns, use the following SQL syntax:

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 VA Rchar (255), CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName) )
SQL UNIQUE Constraint on ALTER TABLE

When a table has been created, to create a UNIQUE constraint in the id_p column, use the following SQL:

Mysql/sql Server/oracle/ms Access:
ALTER TABLE PersonsADD UNIQUE (Id_P)

To name a unique constraint and define a UNIQUE constraint for multiple columns, use the following SQL syntax:

Mysql/sql Server/oracle/ms Access:
ALTER TABLE PersonsADD CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName)
Revoke a UNIQUE constraint

To revoke a UNIQUE constraint, use the following SQL:

Mysql:
ALTER TABLE PersonsDROP INDEX uc_PersonID
SQL Server/oracle/ms Access:
ALTER TABLE PersonsDROP CONSTRAINT uc_PersonID


SQL PRIMARY KEY Constraint

The PRIMARY KEY constraint uniquely identifies each record in a database table.

The primary key must contain a unique value.

Primary key columns cannot contain NULL values.

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

SQL PRIMARY KEY Constraint on CREATE TABLE

The following SQL creates the PRIMARY KEY constraint in the "id_p" column when the "Persons" table is created:

Mysql:
CREATE TABLE Persons (id_p int not null,lastname varchar (255) not null,firstname varchar (255), Address varchar (255), City VA Rchar (255), PRIMARY KEY (Id_P) )
SQL Server/oracle/ms Access:
PRIMARY KEY, LastName varchar (255) not null,firstname varchar (255), Address varchar (255), City varchar (255))

If you need to name the PRIMARY key constraint and define the PRIMARY key constraint for more than one column, use the following SQL syntax:

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 VA Rchar (255), CONSTRAINT pk_PersonID PRIMARY KEY (Id_P,LastName) )
SQL PRIMARY KEY Constraint on ALTER TABLE

If you create a PRIMARY KEY constraint for the "id_p" column if the table already exists, use the following SQL:

Mysql/sql Server/oracle/ms Access:
ALTER TABLE PersonsADD PRIMARY KEY (Id_P)

If you need to name the PRIMARY key constraint and define the PRIMARY key constraint for more than one column, use the following SQL syntax:

Mysql/sql Server/oracle/ms Access:
ALTER TABLE PersonsADD CONSTRAINT pk_PersonID PRIMARY KEY (Id_P,LastName)

Note: If you use the ALTER table statement to add a primary key, you must declare the primary key column to not contain a NULL value (when the table is first created).

Revoke PRIMARY KEY Constraint

To revoke the PRIMARY KEY constraint, use the following SQL:

Mysql:
ALTER TABLE PersonsDROP PRIMARY KEY
SQL Server/oracle/ms Access:
ALTER TABLE PersonsDROP CONSTRAINT pk_PersonID


SQL FOREIGN KEY Constraint

The FOREIGN key in one table points to the PRIMARY key in the other table.

Let's use an example to explain the foreign key. Take a look at the following two tables:

"Persons" table:

City
id_p LastName FirstName Address
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing

"Orders" table:

Id_o OrderNo id_p
1 77895 3
2 44678 3
3 22456 1
4 24562 1

Notice that the Id_p column in Orders points to the id_p column in the Persons table.

The "id_p" column in the "Persons" table is the PRIMARY KEY in the Persons table.

The "id_p" column in the Orders table is the FOREIGN KEY in the Orders table.

The FOREIGN KEY constraint is used to prevent actions that disrupt the connection between tables.

The FOREIGN key constraint can also prevent illegal data from being inserted into the foreign key column, because it must be one of the values in the table it points to.

SQL FOREIGN KEY Constraint on CREATE TABLE

The following SQL creates FOREIGN KEY for the "id_p" column when the Orders table is created:

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

If you need to name the FOREIGN key constraint and define the FOREIGN key constraint for more than one column, use the following SQL syntax:

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) )
SQL FOREIGN KEY Constraint on ALTER TABLE

If you create a FOREIGN KEY constraint for the id_p column if the Orders table already exists, use the following SQL:

Mysql/sql Server/oracle/ms Access:
ALTER TABLE OrdersADD FOREIGN KEY (Id_P)REFERENCES Persons(Id_P)

If you need to name the FOREIGN key constraint and define the FOREIGN key constraint for more than one column, use the following SQL syntax:

Mysql/sql Server/oracle/ms Access:
ALTER TABLE OrdersADD CONSTRAINT fk_PerOrdersFOREIGN KEY (Id_P)REFERENCES Persons(Id_P)
Revoke FOREIGN KEY Constraint

To revoke the FOREIGN KEY constraint, use the following SQL:

Mysql:
ALTER TABLE OrdersDROP FOREIGN KEY fk_PerOrders
SQL Server/oracle/ms Access:
ALTER TABLE OrdersDROP CONSTRAINT fk_PerOrders


SQL CHECK constraints

A CHECK constraint is used to limit the range of values in a column.

If you define a CHECK constraint on a single column, the column only allows a specific value.

If a CHECK constraint is defined on a table, the constraint restricts the value in a specific column.

SQL CHECK Constraint on CREATE TABLE

The following SQL creates a CHECK constraint for the "id_p" column when the "Persons" table is created. The CHECK constraint specifies that the "id_p" column must contain only integers greater than 0.

My SQL:
CREATE TABLE Persons (id_p int not null,lastname varchar (255) not null,firstname varchar (255), Address varchar (255), City VA Rchar (255), CHECK (Id_P>0) )
SQL Server/oracle/ms Access:
CHECK (Id_P>0), LastName varchar (255) not null,firstname varchar (255), Address varchar (255), City varchar (255))

If you need to name a check constraint and define a CHECK constraint for more than one column, use the following SQL syntax:

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 VA Rchar (255), CONSTRAINT chk_Person CHECK (Id_P>0 AND City=‘Sandnes‘) )
SQL CHECK Constraint on ALTER TABLE

If you create a CHECK constraint for the "id_p" column if the table already exists, use the following SQL:

Mysql/sql Server/oracle/ms Access:
ALTER TABLE PersonsADD CHECK (Id_P>0)

If you need to name a check constraint and define a CHECK constraint for more than one column, use the following SQL syntax:

Mysql/sql Server/oracle/ms Access:
ALTER TABLE PersonsADD CONSTRAINT chk_Person CHECK (Id_P>0 AND City=‘Sandnes‘)
Revoke a CHECK constraint

To revoke a check constraint, use the following SQL:

SQL Server/oracle/ms Access:
ALTER TABLE PersonsDROP CONSTRAINT chk_Person
Mysql:
ALTER TABLE PersonsDROP CHECK chk_Person
SQL DEFAULT constraints

The default constraint is used to insert defaults into the column.

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

SQL DEFAULT Constraint on CREATE TABLE

The following SQL creates a DEFAULT constraint for the "City" column when the "Persons" table is created:

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 VA Rchar (255) DEFAULT ' Sandnes ')

By using a function like GETDATE (), the default constraint can also be used to insert system values:

CREATE TABLE Orders (id_o int not Null,orderno int. not null,id_p int,orderdate date DEFAULT GETDATE ())
SQL DEFAULT Constraint on ALTER TABLE

If you create a DEFAULT constraint for the city column if the table already exists, use the following SQL:

Mysql:
ALTER TABLE personsalter City SET DEFAULT ' Sandnes '
SQL Server/oracle/ms Access:
ALTER TABLE personsalter COLUMN city SET DEFAULT ' Sandnes '
Undo DEFAULT Constraint

To undo the DEFAULT constraint, use the following SQL:

Mysql:
ALTER TABLE personsalter City DROP DEFAULT
SQL Server/oracle/ms Access:
ALTER TABLE personsalter COLUMN City DROP DEFAULT

Constraint statements for SQL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.