6.SQL Intermediate
6.SQL Intermediate
6. SQL: Integrity
Constraints
Department Of Computer Science
Mostly adapted from Database System Concepts, 6th Ed. Silberschatz et. al
Overview of (most of ) the Course
Normalisation theory
Relational Algebra
Content
5
Built-in Data Types in SQL
date: Dates, containing a (4 digit) year, month
and day
• Example: date ‘2005-7-27’
time: Time of day, in hours, minutes and
seconds.
• Example: time ‘09:00:30’ time ‘09:00:30.75’
timestamp: date plus time of day
• Example: timestamp ‘2005-7-27 09:00:30.75’
interval: period of time
• Subtracting a date/time/timestamp value from another gives
an interval value
• Interval values can be added to date/time/timestamp values
6
Built-in Data Types in SQL
• Can extract values of individual fields from
date/time/timestamp
• Example: extract (year from r.starttime)
7
User-Defined Types
• The CREATE TYPE statement allows you to create a
composite type or enumeration
8
User-Defined Types
• The CREATE TYPE statement allows you also to
create a composite type
CREATE TYPE film_summary AS (
film_id INT,
title VARCHAR,
release_year YEAR
);
9
cont.
CREATE OR REPLACE FUNCTION get_film_summary (f_id INT)
RETURNS film_summary AS
$$
SELECT
film_id, title, release_year
FROM
film
WHERE
film_id = f_id ;
$$
LANGUAGE SQL;
SELECT
*
FROM get_film_summary (40);
10
User Defined Domains
• A domain is a data type + optional constraints
(restrictions on the allowed set of values).
• Domain constraints are the most elementary
form of integrity constraint: they test values
inserted in the database.
11
User Defined Domains
• Creates a user-defined data type with constraints such
as NOT NULL, CHECK, etc.
• In this table, both first_name and last_name columns are not null and should
not contain spaces. To make it easier for management, you can create a
contact_name domain as follows:
12
Cont.
And use the contact_name as the data type of the first_name and
last_name columns:
13
User Defined Domains
• New domains can be created from existing data
types
• Example:
create domain Dollars numeric(12, 2)
create domain Pounds numeric(12,2)
• An attempt to assign a value of type Dollars to a
variable of type Pounds results in syntax error
Application: branch_name and customer_name are
both of type strings. How would you forbid queries
comparing branch_name to customer_name?
14
(Explicit)
Integrity Constrains
15
Integrity constraints guard against accidental
damage to the database, by ensuring that
authorized changes to the database do not
result in a loss of data consistency.
19
Unique (examples)
Examples:
20
3. The check clause
When applied to a table creation, the check(P) clause
specifies a predicate P (a propositional formula) that
must be satisfied by every tuple in the relation
(i) Check in the create table command
Example: declare branch_name as the primary key for
branch and ensure that the values of assets are non-
negative
create table branch
(branch_name char(15) not null,
branch_city char(30),
assets integer,
primary key (branch_name),
check (assets >= 0))
21
(ii) Check in the create domain command
Example: use the check clause to ensure that an
hourly_wage domain allows only values greater than
the allowed minimum wage:
create domain hourly_wage numeric(5,2)
constraint value_test check(value > = 4.00)
22
4. Referential Integrity (Foreign Keys)
• Foreign key: ensures that a value that appears in one
table for a given (set of) attributes also appears in
another relation So now we can’t delete this row in branch table;
because it is a foreign key of another table’s row
Example:
• “Perryridge” appears as a branch name in a row in account
table
• Then we want to make sure there exists a row in the branch
table for branch “Perryridge”.
branch
account
23
Example
24
4. Referential Integrity (Foreign Keys)