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); 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 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 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 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) create table TableName references Department(DeptName) 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), 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 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 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 database schema 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