SQL Constraints
SQL Constraints
CONSTRAIN
T
• The NOT NULL constraint in MySQL serves as
a validation rule for the columns.
• In NOT NULL constraints the column cannot contain the
NULL values.
• This constraint guarantees that each row in the table
must have a value for that particular column.
Syntax
Following is the basic syntax of NOT NULL constraint while creating a table:
In the above syntax, it can be seen that NOT NULL is used after the column name
and data type of that column in the column definition.
For Example :
Synta
x : CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
CONSTRAINT chk_Age CHECK (Age >
18));
PRIMARY KEY
Combination of unique and not null
CREATE TABLE orders ( order_id INT PRIMARY KEY, order_date DATE,customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(id));
EXAMPLE OF FOREIGN KEY
DEFAULT