Wednesday, April 11, 2007

Constraints

A constraint is a property assigned to a column or the set of columns in a table that prevents certain types of inconsistent data values from being placed in the column(s). Constraints are used to enforce the data integrity. This ensures the accuracy and reliability of the data in the database.

Some of the constraints are

A PRIMARY KEY constraint is a unique identifier for a row within a database table. Every table should have a primary key constraint to uniquely identify each row and only one primary key constraint can be created for each table. The primary key constraints are used to enforce entity integrity.

create table table_name(column_name1 type not null primary key);

A UNIQUE constraint enforces the uniqueness of the values in a set of columns, so no duplicate values are entered. The unique key constraints are used to enforce entity integrity as the primary key constraints.

create table table_name(column_name1 type unique);

A FOREIGN KEY constraint prevents any actions that would destroy link between tables with the corresponding data values. A foreign key in one table points to a primary key in another table. Foreign keys prevent actions that would leave rows with foreign key values when there are no primary keys with that value. The foreign key constraints are used to enforce
referential integrity.

create table table_name(column_name1 type not null foreign key references reference_table_name(primary_column_name));

A CHECK constraint is used to limit the values that can be placed in a column. The check constraints are used to enforce domain integrity.

create table table_name(column_name1 type constraint check_columnname check (column_name > 10));

A NOT NULL constraint enforces that the column will not accept null values. The not null constraints are used to enforce domain integrity, as the check constraints.

create table table_name(column_name1 type not null);

A Candidate key uniquely identifies rows in a table. Any of the identified candidate keys can be used as the table's primary key. Any of the candidate keys that is not part of the primary key is called an Alternate key. One can describe a Candidate Key as a Super Key that contains only the minimum number of columns necessary to determine uniqueness.

No comments: