SQL Constraints

SQL Constraints are rules which regulates how database data is inserted or manipulated. They are usually used to prevent a user from storing invalid data into table. The syntax:

CREATE TABLE tablename
(
columnname1 datatype(size) constraint_name,
columnname2 datatype(size) constraint_name,
columnname3 datatype(size) constraint_name,
columnnamen datatype(size)constraint_name,
);

The following constrains are available in SQL:

  • NOT NULL – Indicates that a column needs to have some value
  • UNIQUE – meaning  that each row for a column must have a unique value
  • PRIMARY KEY – A column/set of columsn whose values uniquely identify every row in a table.
  • FOREIGN KEY – enforce referential integrity
  • CHECK – it ensure that data in a column (or set of columns) meets a set of criteria that is specified
  • DEFAULT – Specifies a default value
Geek University 2022