0% found this document useful (0 votes)
113 views

02b - SQL-DDL - CSC 343

SQL is a standard language for querying, manipulating, and defining data. It originated as SEQUEL in 1974 and became a standard in 1986 which has been revised over time. SQL supports both data definition and data manipulation. Domains define allowable values for attributes and include elementary domains like character, numeric, date, and user-defined domains. Schemas group related database objects and are used to define tables which consist of attributes with domains and constraints.

Uploaded by

Partha Kuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

02b - SQL-DDL - CSC 343

SQL is a standard language for querying, manipulating, and defining data. It originated as SEQUEL in 1974 and became a standard in 1986 which has been revised over time. SQL supports both data definition and data manipulation. Domains define allowable values for attributes and include elementary domains like character, numeric, date, and user-defined domains. Schemas group related database objects and are used to define tables which consist of attributes with domains and constraints.

Uploaded by

Partha Kuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SQL

Week 2: Part II ƒ The name is an acronym for Structured Query


SQL I – Data Definition Language Language. It is actually far richer than a query
language: supports both a DML and a DDL.
Domains, ƒ First proposal: SEQUEL (IBM Research, 1974);
First implementation in SQL/DS (IBM, 1981)
Schema Definitions, and
Constraints ƒ Standardization crucial for its diffusion
ƒ Since 1983, de facto standard;
ƒ First official standard, 1986; revised in 1989;
ƒ Second standard, 1992 (SQL-2 or SQL-92);
ƒ Third standard, 1999 (SQL-3 or SQL-99)
ƒ Most relational DBMS support the base functionality
of the standard and offer proprietary extensions.
CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 1 CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 2

Domains Elementary Domains — Character


ƒ Character
ƒ Domains specify allowable values for attributes.
ƒ Single characters or strings;
ƒ Two categories:
ƒ Strings may be of variable length;
ƒ Elementary (predefined by the standard);
ƒ A Character set different from the default one
ƒ User-defined. can be used (e.g., Latin, Greek, Cyrillic, etc.)
ƒ Syntax:
character [ varying ] [ (Length) ]
[ character set CharSetName ]
ƒ It is possible to use char and varchar, for
character and character varying
respectively
CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 3 CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 4

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

Temporal Instant Domains


User-Defined Domains
ƒ Temporal instants
date has fields year,month,day ƒ Comparable to definitions of variable types in
time [ ( Precision) ] [ with time zone ] programming languages.
has fields hour,minute,second ƒ A domain is characterized by name, elementary
timestamp [ ( Precision) ] [ with time zone ] domain, default value, set of constraints
ƒ Temporal intervals ƒ Syntax:
interval FirstUnitOfTime [ to LastUnitOfTime ] create domain DomainName
as ElementaryDomain [ DefaultValue ] [
ƒ Units of time are divided into two groups: (i) Constraints ]
year, month, (ii) day, hour, minute, second
ƒ Example:
ƒ For example, year(5) to month allows create domain Mark as smallint
intervals up to 99999yrs + 11mo default null
CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 7 CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 8

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

Table Definition Example of create table


ƒ An SQL table consists of an ordered set of create table Employee
attributes, and a (possibly empty) set of constraints (
RegNo character(6) primary key,
ƒ Statement create table defines a relation FirstName character(20) not null,
schema, creating an empty instance. Surname character(20) not null,
ƒ Syntax: Dept character (15)
references Department(DeptName)
create table TableName
on delete set null
( AttributeName Domain [ DefaultValue ] [ on update cascade,
Constraints ] Salary numeric(9) default 0,
{, AttributeName Domain [ DefaultValue ] [ City character(15),
Constraints ] } unique(Surname,FirstName)
[ OtherConstraints ] ) )
CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 11 CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 12

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

Inter-Relational Constraints Reaction Policies


Constraints may involve several relations: Violations arise from
ƒ check: checks whether an assertion is true; ƒ (a) updates on referred attribute or
ƒ (b) row deletions.
ƒ references and foreign key permit the
Reactions operate on internal table, after changes to
definition of referential integrity constraints; an external table.
ƒ Syntax for single attributes ƒ Reactions are:
references after the domain ƒ cascade: propagate the change;
ƒ set null: nullify the referring attribute;
ƒ Syntax for multiple attributes ƒ set default: assign default value to the
foreign key ( Attribute {, Attribute } ) referring attribute;
references ... ƒ no action: forbid the change on external table.
ƒ It is possible to associate reaction policies to ƒ Reactions may depend on the event; syntax:
violations of referential integrity constraints. on < delete | update >
< cascade | set null | set default | no action >
CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 15 CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 16

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

relations describe the relations, columns,


Dept Head 2 Null Y Catalogue
domains in D but also MD (reflectivity). Dept Address 3 Null Y
Column TableNm 1 Null N
ƒ The SQL-2 standard describes a
Column ColName 2 Null N
Definition_Schema (composed of tables) and an
Column Pos 3 Null N
Information_Schema (composed of views).
Column Default 4 Null Y
Column Nullable 5 Y N
CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 19 CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 20

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

You might also like