<<

SQL Week 2: Part II ƒ The name is an acronym for Structured Query SQL I – Definition Language Language. It is actually far richer than a : supports both a DML and a DDL. Domains, ƒ First proposal: SEQUEL (IBM Research, 1974); Schema Definitions, and First implementation in SQL/DS (IBM, 1981) 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 — 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 different 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 programming languages. time [ ( Precision) ][ with time zone ] 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: create domain DomainName interval FirstUnitOfTime [ to LastUnitOfTime ] as ElementaryDomain [ DefaultValue ] [ ƒ Units of time are divided into two groups: (i) Constraints ] year, month, (ii) day, hour, minute, second ƒ Example: ƒ For example, allows year(5) to month create domain Mark as smallint intervals up to 99999yrs + 11mo default

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 ƒ 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

ƒ An SQL table consists of an ordered set of create table Employee attributes, and a (possibly empty) set of constraints ( RegNo character(6) , ƒ Statement create table defines a FirstName character(20) not null, schema, creating an empty instance. Surname character(20) not null, ƒ Syntax: Dept character (15) create table TableName references Department(DeptName) on set null ( AttributeName Domain [ DefaultValue ] [ on 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 permit the Reactions operate on internal table, after changes to definition of 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), assertion > Salary numeric(9) default 0, | 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 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 Relational Catalogues TableNm ColName Pos Default Nullable Employee RegNo 1 Null N ƒ A relational catalogue contains the data Employee Name 2 Null Y dictionary, i.e., a description of the relational Employee Dept 3 Null Y A schema D of the database. Employee Sal 4 0 Y Dept Name 1 Null N Relational ƒ It is based on a relational schema MD whose 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 store containing Employee and Dept on the previous slide? 1.)create DDL to schema create store schema { 2.) create table create Employee 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 create Dept(( Dept table Name char(20)add fields primary key, Head char(6)remember references to add keys, foreignEmployee(RegNo) references, and on delete set null constraintson to updateensure integrity cascade, Address char(20) ) } CSC343 Introduction to Databases — University of Toronto SQL I: DDL — 21

6