Академический Документы
Профессиональный Документы
Культура Документы
SQL allow us to define the ways in which we can automatically enforce the
integrity of a database.
Constraints define rules regarding permissible values allowed in columns and are
the standard mechanism for enforcing integrity.
Types of Constraints:
There are a number of different ways to implement constraints, but each of them falls into
one of these three categories: entity, domain, and referential integrity constraints.
Entity Constraints: Entity constraints are all about individual rows. This constraint
ignores the column as a whole and focuses on a particular row. This can be best
exemplified by a constraint that requires every row to have a unique value for a column
or a combination of columns. This is to ensure that for a particular row, the same value
does not already exist in some other row. We’ll see this kind of constraint in dealing with
Primary key and Unique constraints.
By Dinesh
1) NOT NULL
2) UNIQUE
3) CHECK
4) Primary Key
5) Foreign Key
NOT NULL:
By Dinesh
UNIQUE:
The UNIQUE constraint ensures that all values in a column are distinct.
Often, Unique constraints are referred to as Alternate Keys. Alternate keys are not
considered to be the unique identifier of a record in a table.
Once we establish a Unique constraint, every value in the named column must be unique.
SQL Server will show an error if you try to update or insert a row with a value that
already exists in a column with a Unique constraint.
A Unique key will not automatically prevent us from entering NULL values.
In the Screenshot below I have tried to insert a null value for ID. But as per the unique
constraint NULL values cannot be entered into the table for that attribute.
In other case I tried to insert same value for ID, as a result the row was not inserted.
By Dinesh
The CHECK constraint ensures that all values in a column satisfy certain conditions.
By Dinesh
DEFAULT:
Like all constraints, Default constraints become an integral part of the table definition.
It defines what to do when a row is inserted with data for the column on which you have
not determined a Default constraint.
By Dinesh
When multiple fields are used as a primary key, they are called a composite key.
Primary keys can be specified either when the table is created (using CREATE
TABLE) or by changing the existing table structure (using ALTER TABLE).
(or)
NOTE: Primary key includes some constraints within itself like, NOT NULL,
UNIQUE.
By Dinesh
By Dinesh
Note we cannot have multiple PRIMARY KEY inside the sql table.
Here “id’ & “acno” are two primary key for the table sample_8.
By Dinesh
Syntax:
NOTE:
If you Drop the table which contains primary key, the primary key will also be
dropped.
By Dinesh
A foreign key is a field (or fields) that points to the primary key of another table.
The purpose of the foreign key is to ensure referential integrity of the data. In other
words, only values that are supposed to appear in the database are permitted.
);
Child table:
create table club_member
( refno int primary key,
name char(20),
mid int references club(mid)
);
(or)
By Dinesh
CASCADING CONSTRAINTS:
The CASCADE CONSTRAINTS clause is used along with the DROP COLUMN
clause.
Eg:
VIEWING CONSTRAINTS:
By Dinesh