Comp101 Lect06
Comp101 Lect06
Foundations
of
Information
Systems
Lecture (II)
6: SQL Constraints
Grant
Dick
Department
of
Information
Science
Topics for today
• Quick recap
• CREATE, DROP TABLE (DDL statements)
• INSERT, SELECT (DML statements)
• Integrity
• Constraints:
• Keys
• Checks
1
Quick revision questions (5 min)
2
Recap: Simple DDL Statements
-- Create a table called Student
CREATE TABLE Student (
Student_ID
VARCHAR(10),
First_Name
VARCHAR(50),
Last_Name
VARCHAR(50)
);
4
Key DDL statements
• Remember CRUD:
• (C)reate, (R)etrieve, (U)pdate, (D)elete
7
Integrity: Why?
8
Integrity in the database: Why?
9
Solution: SQL constraints
Business rules:
• A student’s mark in an enrolment cannot be less than zero
or
greater than 100 (but it can be missing)
• Semester can only be: SS (Summer School), S1, S2, FY (full
year)
• Absent can only be (Y)es or (N)o and by default is set to (N)o
11
SQL Demonstration
Structurally complete database without constraints
12
Adding constraints
• Two kinds:
• Key constraints (primary or foreign)
• CHECK constraints (supplied value matches specified
criteria)
• General syntax:
• CONSTRAINT <name> <details of constraint>
• <name> (optional) unique name for the constraint
• <details…> defines the constraint (see following slides)
14
Primary and foreign keys
16
Example: adding a primary key
17
Example: adding a foreign key
18
SQL Demonstration
Adding keys
19
Integrity and business rules
20
Check constraints
values Examples:
•A value must be within a specified set of options
•A value must be unique within the table
•A numeric value must be within a given range
•A date value must be within a given time period
21
Examples
22
SQL Demonstration
Adding integrity
23
Constraints and CREATE TABLE
• Better to integrate constraints into table creation
process rather than adding after (clean, concise,
easy to understand)
24
Example: CREATE TABLE
constraints
CREATE TABLE Enrolment
( Paper_Code CHAR(7), CONSTRAINT
Student_ID FK_Enrolment_Student
VARCHAR2(7), Year FOREIGN KEY (Student_ID)
NUMERIC(4), REFERENCES Student
Semester CHAR(2) (Student_ID)
CONSTRAINT Valid_Semester
CHECK (Semester IN ('SS', 'S1', 'S2', 'FY')),
Mark NUMERIC(3)
CONSTRAINT Valid_Mark CHECK (Mark BETWEEN 0
AND 100), Absent CHAR(1) DEFAULT 'N' NOT NULL
CONSTRAINT Valid_Absent CHECK (Absent IN ('Y',
'N')),
CONSTRAINT PK_Enrolment
PRIMARY KEY (Paper_Code, Student_ID, Year,
Semester),
CONSTRAINT FK_Enrolment_Paper
FOREIGN KEY (Paper_Code) REFERENCES Paper
(Paper_Code),
25
• DEFAULT & NOT NULL always inline
out-of-line constraints
inline constraints • preferred for keys
• preferred for CHECK & UNIQUE where possible • CHECK & UNIQUE involving more
than one column always out-of-line
);
26
General CHECK clauses
(these work for text, number, and date columns)
• Relational comparison:
CHECK (A > 0), CHECK (A < 0), CHECK (A >=
42), …
• Only one value supplied across two columns:
(must be out-of-line)
28
Summary
• SQL is a family of languages for defining
database structure and integrity (DDL) and
data access and manipulation (DML)
31
Thanks!
Questions?