SQL Key Constraints
SQL 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.
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.