Constraints is a mechanism which is used to stop or Restrict invalid data which is entered by the end user.
We can apply the constraints in two situations
- During creation of table
- After creation of table
We can apply the constraints in two level on the table.
- Column level constraints
- Table level constraints
Column level constraints
Applying the constraints after defined the column immediately than those constraints called as column level constraints.
Table level constraint
Applying the constraints after defined all the column of the table or end the table if apply the constraints than these constraints called as table level constraints.
CREATE TABLE table_name ( column_name1 data_type(size) constraint_name, column_name2 data_type(size) constraint_name, column_name3 data_type(size) constraint_name, .... );
What is a KEY?
A KEY is a value used to identify a record in a table uniquely. A KEY could be a single column or combination of multiple columns
Note: Columns in a table that are NOT used to identify a record uniquely are called non-key columns.
Constraints in SQL
- Unique key
- Not NUll
- Primary key
- Foreign key