0% found this document useful (0 votes)
4 views

SQL Key Constraints

Key constraints in a database ensure that data entered into columns adheres to specified rules, such as uniqueness and non-null values. Types of key constraints include NOT NULL, UNIQUE, DEFAULT, CHECK, PRIMARY KEY, FOREIGN KEY, CANDIDATE KEY, and SUPER KEY, each serving different purposes for data integrity. A candidate key uniquely identifies rows, while a super key is a broader set that can also uniquely identify tuples.

Uploaded by

Patel Priyank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL Key Constraints

Key constraints in a database ensure that data entered into columns adheres to specified rules, such as uniqueness and non-null values. Types of key constraints include NOT NULL, UNIQUE, DEFAULT, CHECK, PRIMARY KEY, FOREIGN KEY, CANDIDATE KEY, and SUPER KEY, each serving different purposes for data integrity. A candidate key uniquely identifies rows, while a super key is a broader set that can also uniquely identify tuples.

Uploaded by

Patel Priyank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Key Constraints

 Constraints or nothing but the rules that are to be followed while entering data into columns of the
database table .
 Constraints ensure that data entered by the user into columns must be within the criteria specified by
the condition.
 For example, if you want to maintain only unique IDs in the employee table or if you want to enter
only age under 18 in the student table etc .
 Types of key constraints in DBMS
o NOT NULL: ensures that the specified column doesn’t contain a NULL value.
o UNIQUE: provides a unique/distinct values to specified columns.
o DEFAULT: provides a default value to a column if none is specified.
o CHECK: checks for the predefined conditions before inserting the data inside the
table.
o PRIMARY KEY: it uniquely identifies a row in a table.
o FOREIGN KEY: ensures referential integrity of the relationship
o CANDIDATE KEY: a set of one or more columns that can uniquely identify a row within a
table
o SUPER KEY: combination of attributes is uniquly identified with candidate key
EXAMPLE
CANDIDATE KEY:

A candidate key is a set of one or more columns that can be used to uniquely identify a row within
a table.

No two rows within a table can have the same values for the columns that make up a candidate
key.

A Table can have multiple candidate keys, but only one of them can be chosen as the primary key.

The primary key is used as the official unique identifier for a row within the table.

CREATE TABLE Employees (


EmployeeID INT NOT NULL PRIMARY KEY,
FirstName VARCHAR(255) NOT NULL,
LastName VARCHAR(255) NOT NULL,
Email VARCHAR(255) NOT NULL
);
Super Key
Super key is an attribute set that can uniquely identify a tuple. A super key is a superset of a
candidate key.

For example: In the above EMPLOYEE table, for(EMPLOEE_ID, EMPLOYEE_NAME), the name of two
employees can be the same, but their EMPLYEE_ID can't be the same. Hence, this combination can
also be a key.

The super key would be EMPLOYEE-ID (EMPLOYEE_ID, EMPLOYEE-NAME), etc.

You might also like