SQL Data Definition
SQL Data Definition
Session 3
Course Sub Learning Outcomes
(Sub-CLO)
• Objectives of SQL
• History of SQL
• Data types supported by SQL standard
• Purpose of Integrity enhancement feature
• Creating tables
• Altering tables
• Basic concept of View
vtraining-msuhandi.blogspot.com
Objectives of SQL
Objectives of SQL
• An ISO standard now exists for SQL, making it both the formal
and de facto standard language for relational databases.
Objectives of SQL
Domain Constraints
(a) CHECK (Only used in one particular table)
• Column Level
CREATE TABLE STAFF (
…
sex CHAR NOT NULL
CHECK (sex IN (‘M’, ‘F’)),
…)
Or
CREATE TABLE STAFF (
…
sex CHAR NOT NULL
CONSTRAINT C1 CHECK (sex IN (‘M’, ‘F’)),
…)
Domain Constraints
Domain Constraints
(a) CHECK (Only used in one particular table)
• Table Level
CREATE TABLE STAFF (
…
sex CHAR NOT NULL,
…
CHECK (sex IN (‘M’, ‘F’)),
…)
Or
CREATE TABLE STAFF (
…
sex CHAR NOT NULL,
…
CONSTRAINT C1 CHECK (sex IN (‘M’, ‘F’)),
…)
Domain Constraints
For example:
CREATE DOMAIN SexType AS CHAR
CHECK (VALUE IN (‘M’, ‘F’));
• Primary key of a table must contain a unique, non-null value for each
row.
• ISO standard supports PRIMARY KEY clause in CREATE and ALTER
TABLE statements:
CREATE TABLE STAFF (
…
staffNo VARCHAR(5) PRIMARY KEY,
…)
Or
CREATE TABLE STAFF (
staffNo VARCHAR(5) NOT NULL,
…
PRIMARY KEY(staffNo),
…)
IEF - Entity Integrity
ON UPDATE CASCADE:
Branch Staff
branchNo: B001 All Staff records with branchNo: B001
is updated into B011 are also updated into B011
ON DELETE CASCADE:
Branch Staff
branchNo : B001 All Staff records with branchNo: B001
This record is deleted are also deleted
…
staffNo StaffNumber
Constraint StaffNotHandlingTooMuch
CHECK (NOT EXISTS (SELECT staffNo
FROM PropertyForRent
GROUP BY staffNo
HAVING COUNT(*) > 100)),
branchNo BranchNumber NOT NULL,
PRIMARY KEY (propertyNo),
FOREIGN KEY (staffNo) REFERENCES Staff
ON DELETE SET NULL ON UPDATE CASCADE,
FOREIGN KEY (ownerNo) REFERENCES PrivateOwner
ON DELETE NO ACTION ON UPDATE CASCADE,
FOREIGN KEY (branchNo) REFERENCES Branch
ON DELETE NO ACTION ON UPDATE CASCADE
);
ALTER TABLE
http://www.zentut.com/sql-tutorial/sql-alter-table/
Example 7.2(a) - ALTER TABLE
View
Dynamic result of one or more relational operations operating on base
relations to produce another relation.
• Data independence
• Currency
• Improved security
• Reduced complexity
• Convenience
• Customization
• Data integrity
www.bowelcanceraustralia.org
Disadvantages of Views
• Update restriction
• Structure restriction
• Performance
www.bowelcanceraustralia.org