- - PowerPoint PPT Presentation

introduction
SMART_READER_LITE
LIVE PREVIEW

- - PowerPoint PPT Presentation

Introduction slide 150 Data Definition slide 152


slide-1
SLIDE 1

SQL as DDL 1

  • Introduction

slide 150

Data Definition

slide 152

Integrity constraints

slide 159

  • Functionalities :

data definition and data manipulation in the relational format data control

Manipulation language

non procedural borrowed to relational algebra and to the tuple relational calculus

Power of the manipulation language

Relational Algebra + Functions-Aggregates + Sorting

A SQL query (without functions and sorting)

Set of Relational Algebra operations

Introduction

  • History

SEQUEL language of the SYSTEM/R relational DBMS

prototype (74-76)

IBM research lab at San José

Normalization at ISO

The SQL1 norm (1986, 1989) The SQL2 norm (1992) The SQL3 norm

Query language of (quite) all relational DBMSs

ORACLE (Oracle Corporation - 1977) INGRES (Ingres Technology - 1980) DB2 (IBM - 1984) INFORMIX (Informix Inc - 1981) SYBASE (Sybase Inc - 1984) MySQL (1995)

Introduction

slide-2
SLIDE 2

SQL as DDL 2

  • !!

Relation Schema Definition Relational View Definition Integrity Constraint Definition Right Definition Validation Process data storage and index definition

(not normalized => DBMS dependent !!)

Data Definition

"

!!#$% &

A finite set of domains

  • ptional key:

Duplicate rows The projection operator is different (

DISTINCT)

A Relation is not a set

  • '!(

Numeric :

  • INTEGER, SMALLINT
  • DECIMAL (m,n), NUMBER(m,n)
  • FLOAT, REAL

String : CHAR (n), VARCHAR(n) Temporal : DATE (SQL2 ! norm ) Specific DBMS have their own domains NULL : missing value Data Definition

  • %)(

Creation

CREATE TABLE Project ( ProjNumber Integer , ProjName Char(20), PLocation Char(40) )

Evolution

  • add an attribute (SQL2 ! norm)

ALTER TABLE Project ADD COLUMN DepNumber Integer

Delete (SQL2 ! norm )

DROP TABLE Project

Data Definition

slide-3
SLIDE 3

SQL as DDL 3

*

&# + ,(

Movies(id, title, yr, score, votes, director) Directors(id, name) Actors(id, name) Castings(movieid, actorid, ord)

  • &# + ,(

0,n 1,1 0,n 0,n

Movies score id title yr score Actors idactor nameactor Directors iddirector namedirector Castings

  • rd

IsDirectedBy

  • CREATE TABLE CASTINGS (

movieid integer, actorid integer,

  • rd integer)

CREATE TABLE MOVIES ( id integer, title char (40), yr integer , score integer, votes integer, director integer)) CREATE TABLE ACTORS ( id integer, name char(40) ) CREATE TABLE DIRECTORS ( id integer, name char(40))

.,(

  • /

Business rules for data Consistent state of the database Enforced at any time

slide-4
SLIDE 4

SQL as DDL 4

*

/ ,(

Movie database schema

MOVIES(id, title, yr, score, votes, director) ACTORS(id, name) CASTINGS(movieid, actorid, ord) DIRECTORS(id, name) Relational data model:

key uniqueness (id in MOVIES) foreign key (director to DIRECTORS)

Domain:

  • rd between 1 and 10

yr between 1900 and 2005

Not null

actor’s name is mandatory

Behavioral:

the value of votes is always increasing

*

/ !'&

When ?

At schema definition (mainly) During schema lifecycle

How to express IC ?

SQL SQL extensions

What about commercial DBMS ?

Just a few ICs Other ICs supported by programs (triggers) *

/ !'&

Norms:

SQL86 : unicity, not null, view with «check option » SQL89 : domain, key, referential integrity with reject SQL2 (SQL92) : referential integrity with «cascade delete

and update »

Integrity constraints definition

2 different syntaxes:

  • At the end of an attribute definition
  • At the end of a table definition

*"

CREATE TABLE CASTINGS ( movieid integer, actorid integer,

  • rd integer CONSTRAINT ICord

CHECK (ord between 1 and 10)) ALTER TABLE CASTINGS add primary key (movieid, actorid) ALTER TABLE CASTINGS add constraint fkACT foreign key (actorid) references ACTORS(id) on delete cascade ALTER TABLE CASTINGS add constraint fkMOV foreign key (movieid) references MOVIES(id) on delete cascade ALTER TABLE MOVIES add constraint fkDIR foreign key (director) references DIRECTORS(id) on delete cascade CREATE TABLE MOVIES ( id integer PRIMARY KEY, title char (40) NOT NULL, yr integer CONSTRAINT ICyr CHECK (yr between 1900 and 2005), score integer CONSTRAINT ICscore CHECK (score between 0 and 10), votes integer NOT NULL , director integer)) CREATE TABLE ACTORS ( id integer, name char(40) PRIMARY KEY(id) ) CREATE TABLE DIRECTORS ( id integer PRIMARY KEY, name char(40))

.,(

slide-5
SLIDE 5

SQL as DDL 5

*

+( 0)

Consistency of a set of IC

No contradictory rules

Redundancy

Age > 18 and age > 21

Optimisation

Determine the minimum set of data concerned by a IC

enforcement

Determine the minimum set of IC to verify after a database

update