Lec 4
Lec 4
Example:
• NOT NULL: A Constraint that ensures that a column cannot have NULL value.
• DEFAULT: A Constraint that provides a default value for a column when none is
specified.
• UNIQUE : A Constraint that ensures that all values in a column are different.
• PRIMARY Key: A Constraint that uniquely identify each row/record in a database
table (NOT NULL + UNIQUE)
• FOREIGN KEY (FK): A Constraint that ensures referential integrity. A foreign key
from 1 table to another is used link a tuple in the 1st table to a unique tuple in the
2nd table.
• CHECK: A constraint that ensures that all values in a column satisfy a certain
condition.
Create Table: Example
9
10
SQL AUTO
INCREMENT Field
Note:
Identity(seed,increment)
Using Check
11
Create Table:
Check
Constraint
https://www.w3schools.com/sql/sql_check.asp
Using Default Value
12
Create Table:
Default
Constraint
‘Cairo’
Foreign Key Constraint
13
Order ID is the
primary key
Foreign Key Example
14
Foreign Key
Constraint:
Example
15
Foreign Key
Constraint
You can also add how the Foreign key updates will be
handled:
REFERENCES Persons(ID)
ON DELETE CASCADE
ON UPDATE CASCADE
ON DELETE SET NULL);
SQL ALTER TABLE: Add Column
16
Changing Tables
17
17
Student ID Student Name Faculty Department Age
Numeric(5) Varchar(100) Varchar(30) Varchar(30) Integer
Neamat El Tazi
Answer
25
Neamat El Tazi