Introduction To SQL II: Data Definition Language (DDL) Data Manipulation Language (DML)
Introduction To SQL II: Data Definition Language (DDL) Data Manipulation Language (DML)
Introduction to SQL II
• Data Definition Language (DDL)
• Data Manipulation Language (DML)
DDL WITH TABLES.
• Creating Tables with Table constraints
• Modifying Table structures
Create Table – using table and column
constraints
Constraints enforce rules on data whenever a row is
inserted, updated, or deleted from a table. The constraints
have to be satisfied for the operation to succeed.
Data Integrity Constraints:
Not null: specifies that the column cannot contain a
null value.
Unique: specifies that a column or combination of
columns whose values must be unique for all rows in
the table.
Create Table – using table and column
constraints cont’d
Check: specifies a condition that must be true
Primary key: uniquely identifies each row of the table
Foreign key: establishes and enforces a foreign key
relationship between the column and a column of the
referenced table.
Constraints can be divided at one of the two levels
i. Column constraint:
References a single column and is defined within
the column definition.
Can define any type of integrity constraint.
Create Table – using table and column
constraints Cont’d
o Table constraint:
References one or more columns and is defined
after the column list.
Can define any constraint except not null.
OR
[using table constraints]
Notes:
The not null constraint is added by modifying the column
that is to be defined as not null
Example:
ALTER TABLE students
MODIFY weight int not null;
Example:
DROP TABLE students;
Quiz
Write all SQL statements that will create the relational DB schema:
Data Constraints:
A department name value must never be repeated.
No person should earn a salary that is more than 5000.
Note:
A foreign key is preceded by an asterisk (*);
Mgr references EmpNo.
Quiz Cont’d
1. Assuming that Sex has data type char, modify the personnel
table such that it can accept values of varied lengths.
2. Modify the staff table such that each staff member has a
position.
Manipulating Data In a Table
• UPDATE statement
• DELETE Statement
DML Overview
This is a language that provides a set of operations to
support the basic data manipulation operations on the
data held in the database.
Example:
DELETE FROM departments
WHERE department_id = 60;