LM SQL Constraints 2
LM SQL Constraints 2
SQL Constraints
Constraints are used to limit the type of data that can go into a table.
Integrity Constraints
Integrity Constraints ensure that changes made to the database by authorized user , do not
result in a loss of data ( consistency) .
Entity Constraints
The entity Integrity is a constraint on primary key values , it states that any
attribute of primary key cannot contain Null values.
If a primary key contain null value , then it is not possible to uniquely identify
record in a table.
It ensures that there are no duplicate rows in a table.
Page 1
SQL Constraints Page 2 By HSP
Domain Constraints
It enforce valid entries for a given column by restricting the type , the format or
the range of possible value.
Domain constraint specifies that the value of an attribute must be from the
domain.
A domain is a set of atomic values , e.g The domain for the attribute AGE for
EMPLOYEE Table will be the set of all possible positive numbers between 18
and 65.
The attribute cannot hold a value other than those specified in the domain.
Referential Constraints
It ensures that value appears in one relation for an attribute also appears for an
attribute of another relation.
The referential constraint is a foreign key constraint.
Foreign key points to the primary key of another table.
Constraints can be specified when a table is created (with the CREATE TABLE
statement) or after the table is created (with the ALTER TABLE statement).
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT
NOT NULL
The NOT NULL constraint enforces a field to always contain a value. This means that
you cannot insert a new record, or update a record without adding a value to this field.
Page 2
SQL Constraints Page 3 By HSP
The following SQL enforces the "P_Id" column and the "LastName" column to not
accept NULL values:
The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness
for a column or set of columns.
Note that you can have many UNIQUE constraints per table, but only one PRIMARY
KEY constraint per table.
MySQL:
Page 3
SQL Constraints Page 4 By HSP
Page 4
SQL Constraints Page 5 By HSP
MySQL:
Each table should have a primary key, and each table can have only ONE primary key.
MySQL:
Page 5
SQL Constraints Page 6 By HSP
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY
constrainton multiple columns, use the following SQL syntax:
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY
constraint on multiple columns, use the following SQL syntax:
Page 6
SQL Constraints Page 7 By HSP
Note: If you use the ALTER TABLE statement to add a primary key, the primary key
column(s) must already have been declared to not contain NULL values (when the table
was first created).
MySQL:
Let's illustrate the foreign key with an example. Look at the following two tables:
Note that the "P_Id" column in the "Orders" table points to the "P_Id" column in the
"Persons" table.
Page 7
SQL Constraints Page 8 By HSP
The "P_Id" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "P_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
The FOREIGN KEY constraint also prevents that invalid data form being inserted into
the foreign key column, because it has to be one of the values contained in the table it
points to.
MySQL:
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY
constraint on multiple columns, use the following SQL syntax:
Page 8
SQL Constraints Page 9 By HSP
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY
constrainton multiple columns, use the following SQL syntax:
MySQL:
Page 9
SQL Constraints Page 10 By HSP
The CHECK constraint is used to limit the value range that can be placed in a column.
If you define a CHECK constraint on a single column it allows only certain values for
this column.
If you define a CHECK constraint on a table it can limit the values in certain columns
based on values in other columns in the row.
My SQL:
Page 10
SQL Constraints Page 11 By HSP
(
P_Id number NOT NULL,
LastName text(255) NOT NULL,
FirstName text(255),
Address text(255),
City text(255),
CONSTRAINT chk_Person CHECK (P_Id>0 AND City='Sandnes')
)
The default value will be added to all new records, if no other value is specified.
Page 11
SQL Constraints Page 12 By HSP
The DEFAULT constraint can also be used to insert system values, by using functions
like GETDATE():
MySQL:
Page 12
SQL Constraints Page 13 By HSP
MySQL:
Page 13