02b - SQL-DDL - CSC 343
02b - SQL-DDL - CSC 343
1
More Elementary Domains
Bit Approximate Numeric Domains
Single Boolean values or strings of Boolean Approximate numeric domains
values (may be variable in length); Approximate real values
Syntax: Based on a floating point representation
bit [ varying ] [ (Length) ] float [ ( Precision ) ] e.g., 0.17E16, 0.41E-6
Exact numeric domains double precision
Exact values, integer or with a fractional part real — behaves like float, but has variable
Four alternatives: numeric(6,3) precision
numeric [ ( Precision [, Scale ] ) ]
decimal [ ( Precision [, Scale ] ) ]
integer
smallint # of significant digits decimal digits
CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 5 CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 6
2
Default Domain Values Schema Definition
Define the value that the attribute must assume
when a value is not specified during row A schema is a collection of objects: domains,
insertion. tables, indexes, assertions, views, privileges
Syntax: A schema has a name and an owner (who
default < GenericValue | user | null > determines authorization privileges)
GenericValue represents a value compatible Syntax:
with the domain, in the form of a constant or an create schema [ SchemaName ]
expression. [ [ authorization ] Authorization ]
user is the login name of the user who assigns { SchemaElementDefinition }
a value to this attribute.
CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 9 CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 10
3
Intra-Relational Constraints Example of Intra-Relational
Constraints
Constraints are conditions that must be verified by
every database instance Each pair of FirstName and Surname uniquely
Intra-relational constraints involve a single relation identifies each element
not null (on single attributes) FirstName char(20) not null,
unique: permits the definition of keys; syntax: Surname char(20) not null,
unique(FirstName,Surname)
for single attributes: unique, after the domain
Note the difference with the following (stricter)
for multiple: unique ( Attribute {, Attribute } )
definition:
primary key: defines the primary key (once for FirstName char(20) not null unique,
each table; implies not null); syntax like unique Surname char(20) not null unique,
check: described later …
CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 13 CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 14
4
Example Schema Updates
create table Employee
( Two SQL statements:
RegNo char(6),
FirstName char(20) not null, alter (alter domain...,alter table …)
Surname char(20) not null, drop < schema | domain | table | view |
Dept char(15),
Salary numeric(9) default 0,
assertion >
City char(15), How else could we
ComponentName [ restrict | cascade ]
primary key(RegNo), have identified the
Examples:
foreign key(Dept) primary key?
references Department(DeptName) alter table Department
How else could we on delete set null add column NoOfOffices numeric(4)
have identified the on update cascade,
primary key?
unique(FirstName,Surname) drop table TempTable cascade
)
CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 17 CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 18
Column
TableNm ColName Pos Default Nullable
Relational Catalogues Employee RegNo 1 Null N
Employee Name 2 Null Y
A relational catalogue contains the data
Employee Dept 3 Null Y
dictionary, i.e., a description of the relational A
schema D of the database. Employee Sal 4 0 Y
Relational
It is based on a relational schema MD whose Dept Name 1 Null N
5
Practise
What is the DDL for the database schema store containing Employee and Dept on the
previous slide?
create
1.) DDL to schemacreate
store {
schema
2.) create table Employee
create Employee( table
RegNo (We
char(6),
can take this from a previous slide)
FirstName char(20) not null,
Surname char(20) not null,
Dept char(15),
Salary numeric(9) default 0,
City char(15),
primary key(RegNo),
foreign key(Dept)
references Dept(DeptName)
on delete set null
on update cascade,
unique(FirstName,Surname)
)
3.) create table Dept((
create Dept table
Name char(20)
add fields primary key,
Head char(6)
rememberreferences Employee(RegNo)
to add keys, foreign references, and
on delete set null
onto update
constraints cascade,
ensure integrity
Address char(20)
)
}
CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 21