Course: Introduction to Database Systems
Course Code: IS211
Constraints and SQL (DDL)
Presented by:
Dr. Dina Ezzat
1
Types of DB Constraints
1. Domain constraints
2. Key constraints
3. Integrity constraints
• Entity Integrity Constraint
• Referential Integrity Constraint
• Semantic Integrity Constraints
2
Domains Constraints
• The value of each attribute, A, must be an atomic value from the domain of A.
• Values are indivisible units.
Examples:
“Mobile numbers” are the set of 11 digit phone numbers which are valid in Egypt.
“Dept” of students are the set of faculty department names.
3
Key Constraints
Primary key
Telephone Number?
Super key Why needed?
{Empid, Empname}, {Empid, EmpPassport, Empname}, {EmpLicence, Empname}
Any set of columns which contains EmpLicence or EmpPassport or Empid is a super key of the
4
table
Key Constraints (Cont.)
• Value of a key uniquely identifies a tuple in a relation.
• Super key (K): is a subset of attributes of R that can identify a tuple. It might
contain more than one attribute.
• No 2 tuples have same values for K.
• A key is a minimal superkey; a super key from which we can not remove any
attributes and still be able to uniquely identify tuples in a relation.
• A relational schema may have more than one key
• Each key is called a candidate key
• One is designated as the primary key
5
Key Constraints (Cont.)
• Example: Consider the CAR relation schema:
• CAR(State, Reg#, SerialNo, Make, Model, Year)
• CAR has two keys:
• Key1 = {State, Reg#}
• Key2 = {SerialNo}
• {SerialNo, Make} is a superkey
• In general:
• Any key is a superkey (but not vice versa)
• Any set of attributes that includes a key is a superkey
6
Examples from Premier Database – Primary Key
CustomerNum uniquely identifies the Customer table and is the primary key
of this table
7
Examples from Premier Database – Composite Primary Key
OrderNum and PartNum make up the primary key of the OrderLine table. This is known as a
Composite Primary key, that is, primary key that is made up of more than one attribute
8
Foreign Keys
• A foreign key in R is a set of attributes FK in R such that FK is a
primary key of some other relation R’.
• A foreign key is used to specify a referential integrity constraint.
Employee
Enum Ename phone projectnum
Project Schema
Pnum Pname Location
9
Example
Employee
Enum Ename phone
phone Projectnum
123 Ahmed 01110025878 111
124 Ali 01225929785
127 Ola 01024578960 111
Instances
Project
Pnum Pname Location
111 ABC Giza
112 EFG Cairo
10
Example of Composite FK
Employee
Enum Ename
Enum phone
Ename Pname Location
123 Ahmed 01110025878 ABC Giza
124 Ali 01225929785
127 Ola 01024578960 EFG Cairo
Project
Pname Location
ABC Giza
EFG Cairo
11
New Employee?
12
Company Relational Schema Diagram
• The primary key attribute (or attributes) will be underlined.
• A foreign key (referential integrity) constraint is displayed as a directed arc
(arrow) from the foreign key attribute to the referenced table.
13
Entity Integrity Constraint
- A primary key value can not be null.
- Why?
14
Semantic Integrity Constraints
• Constraints on data values.
• Examples:
➢The salary of an employee must not exceed that of his/her supervisor.
➢A person’s date of birth must be before the current date.
➢What else?
15
SQL
16
SQL Introduction
• SQL stands for Structured Query Language.
• SQL is a standard language for querying and manipulating data.
Product
PName Price Manufacturer A relation or table is a
Gizmo 19.99 GizmoWorks group of tuples having
Tables in SQL the attributes specified
Powergizmo 29.99 GizmoWorks
by the schema
SingleTouch 149.99 Canon
MultiTouch 203.99 Hitachi
17
SQL
• Data definition language (DDL)
➢ Specify data types, structures and constraints for the data to be stored in the
database.
• Data manipulation language (DML)
➢ Ability to query the database to retrieve data, insert, update and delete data.
• Data control language (DCL)
➢ Ability to provide access and security controls over the data.
18
Advantages of SQL
• Standardized language.
• Application portability: SQL databases/applications can move from one device to
another.
19
NULL and NOT NULL
• To say “don’t know the value” we use NULL.
SID Name GPA
123 Ahmed 3.9
Mohamed just enrolled in his first class
143 Mohamed NULL
In SQL, we may constrain a column to be NOT NULL, e.g., “name” in this table
20
Notes
• SQL commands are case insensitive:
• Same: SELECT, Select, select
• Use single quotes for strings:
• Example: ‘abc’
21
Data Definition Language (DDL)
• It is used to create and modify the structure of database objects in
database.
• Includes statements like:
➢ Create: creates a new table or any other database object.
➢ Alter: modifies an existing database object, such as a table.
➢ Drop: removes an entire table or any other object in database.
22
Data Types of Table Attributes
• Common data types for table attributes are:
1. Numeric: integer number (INT and SMALLINT) and floating
number (FLOAT, REAL and DOUBLE).
2. Character: data types are either fixed length (CHAR (n)), where n
is the number of characters or variable length (VARCHAR(n)).
3. Boolean: TRUE or FALSE.
4. Timestamp.
23
Create Database Statement
• CREATE a new database:
CREATE DATABASE <Database_Name>;
Example → CREATE DATABASE university;
• DROP an existing database:
DROP DATABASE <Database_Name>;
Example → Drop DATABASE university;
24
Table Creation
25
Steps in Table Creation
1. Identify attributes
2. Identify data types for attributes
3. Identify columns that can and can not be null
4. Identify columns that must be unique
5. Identify primary key-foreign keys
6. Determine default values
7. Identify constraints on columns
26
Creating Table: Syntax
CREATE TABLE table_name (
column1 datatype constraint_1 constraint_2,
column2 datatype,
column3 datatype constraint_3,
.....
columnN datatype,
constraint_1,
constraint_2,
constraint_3
);
27
Create Table: Constraints
• NOT NULL: a constraint that ensures that a column can not 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
one table to another is used to link a tuple in the first table to a unique tuple in the second
table.
• CHECK: a constraint that ensures that all values in a column satisfy a certain condition.
28
Create Table: Examples
29
Create Table: Examples (Unique Constraint)
Course(CrsCode, Name(
CREATE TABLE Course
(
CrsCode SMALLINT,
Name VARCHAR(45) UNIQUE,
PRIMARY KEY (CrsCode)
);
CREATE TABLE Course
(
CrsCode SMALLINT,
Name VARCHAR(45),
PRIMARY KEY (CrsCode),
CONSTRAINT UC_CrsName UNIQUE (Name)
);
30
Create Table: Examples (Default Constraint)
Student(SSN, Name, Age, City, Major)
CREATE TABLE Student
(
SSN INT Primary Key,
Name VARCHAR(45),
Age INT,
City VARCHAR(15) DEFAULT ‘CAIRO’,
Major VARCHAR(5)
);
31
Create Table: Examples (Check Constraint)
Student(SSN, Name, Age, City, Major)
CREATE TABLE Student
(
SSN INT Primary Key,
Name VARCHAR(45),
Age INT,
City VARCHAR(15) DEFAULT ‘CAIRO’,
Major VARCHAR(5),
CONSTRAINT CHK_Student CHECK (Age>=18 AND City=‘Giza’),
);
32
Foreign Key Constraint
33
Create Table: Example 1 (Foreign Key)
34
Create Table: Example 2 (Foreign Key)
Registered(SSN, CrsCode, Semester)
CREATE TABLE Registered
(
SSN INT FOREIGN KEY REFERENCES Student(SSN),
CrsCode SMALLINT FOREIGN KEY REFERENCES COURSE(Crscode),
Semester VARCHAR (45) NOT NULL,
PRIMARY KEY (SSN, CrsCode)
);
35
Create Table: Example 3 (Foreign Key)
Foreign Key
Constraint
You can also add how the Foreign key updates will be handled:
REFERENCES Persons(PersonID)
ON DELETE CASCADE
ON UPDATE CASCADE
ON DELETE SET NULL);
36
Any Questions?
37