- Entity integrity,
- Referential integrity and
- Domain integrity.
Entity Integrity
- The primary
key for a row is unique; it does not match the primary key of any other
row in the table.
- The primary
key is not null, no component of the primary key may be set
to null.
Referential Integrity
- A valid
foreign key value must always reference an existing primary key or contain
a null
- No
operation (INSERT, UPDATE) can create a non-null foreign key
unless a corresponding primary key exists.
- No
operation (UPDATE, DELETE) can remove or change a primary key while a
referencing foreign keys exist.
Here, the related table
contains a foreign key value that doesn’t exist in the primary key field of the
primary table (i.e. the “CompanyId” field). This has resulted in an “orphaned
record”.
- The datatype and the length
- The NULL value acceptance
- The allowable values, through techniques like
constraints or rules
- The default value.
NOT NULL
The NOT
NULL constraint enforces a column to NOT accept NULL values. Any modification to database that would cause a null to be inserted in an attribute declared to be not null generates an error.
UNIQUE
The UNIQUE constraint ensures that all values in a column are different.
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns.
A PRIMARY
KEY constraint automatically has a UNIQUE constraint.
CHECK
The CHECK constraint
is used to limit the value range that can be placed in a column.
If a CHECK constraint
is defined on a column it will allow only certain values for this column.
If a CHECK constraint
is defined on a table it can limit the
values in certain columns based on values in other columns in the row.
Ex:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
No comments:
Post a Comment