Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Outline SQL : Structured Query Language data definition language Introduction Data Definition Integrity constraints slide 150 slide 152 slide 159 148 SQL presentation Introduction 149 SQL Presentation (2) 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 150 SQL as DDL 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) 151 1 Data Definition Data Definition Relation Schema Definition Relational View Definition Integrity Constraint Definition Right Definition Validation Process data storage and index definition (not normalized => DBMS dependent !!) SQL DDL vs. Relational Model A finite set of domains optional key: Duplicate rows The projection operator is different ( DISTINCT) A Relation is not a set 152 Basic Domains Numeric : Data Definition 153 Relation Schema INTEGER, SMALLINT DECIMAL (m,n), NUMBER(m,n) FLOAT, REAL Data Definition Creation CREATE TABLE Project ( ProjNumber Integer , ProjName Char(20), PLocation Char(40) ) String : CHAR (n), VARCHAR(n) Temporal : DATE (SQL2 ! norm ) Specific DBMS have their own domains NULL : missing value Evolution add an attribute (SQL2 ! norm) ALTER TABLE Project ADD COLUMN DepNumber Integer Delete (SQL2 ! norm ) DROP TABLE Project 154 SQL as DDL 155 2 Movie database example Movie database example (2) Movies(id, title, yr, score, votes, director) Directors(id, name) Actors(id, name) Castings(movieid, actorid, ord) Movies score id title yr score 0,n Castings ord 0,n Actors idactor nameactor 1,1 IsDirectedBy 0,n Directors iddirector namedirector 156 Example CREATE TABLE MOVIES ( id integer, title char (40), yr integer , score integer, votes integer, director integer)) 157 Integrity constraints CREATE TABLE CASTINGS ( movieid integer, actorid integer, ord integer) Business rules for data Consistent state of the database Enforced at any time CREATE TABLE ACTORS ( id integer, name char(40) ) CREATE TABLE DIRECTORS ( id integer, name char(40)) 158 SQL as DDL 159 3 Integrity constraints examples Integrity constraints and DBMS Relational data model: Domain: MOVIES(id, title, yr, score, votes, director) ACTORS(id, name) ord between 1 and 10 yr between 1900 and 2005 Behavioral: the value of votes is always increasing SQL SQL extensions What about commercial DBMS ? DIRECTORS(id, name) At schema definition (mainly) During schema lifecycle How to express IC ? Not null actor’s name is mandatory CASTINGS(movieid, actorid, ord) key uniqueness (id in MOVIES) foreign key (director to DIRECTORS) Movie database schema When ? Just a few ICs Other ICs supported by programs (triggers) 160 Example Integrity constraints and DBMS (2) 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)) 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 162 SQL as DDL 161 CREATE TABLE CASTINGS ( movieid integer, actorid integer, ord 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 CREATE TABLE ACTORS ( id integer, name char(40) PRIMARY KEY(id) ) ALTER TABLE CASTINGS add constraint fkMOV foreign key (movieid) references MOVIES(id) on delete cascade CREATE TABLE DIRECTORS ( id integer PRIMARY KEY, name char(40)) ALTER TABLE MOVIES add constraint fkDIR foreign key (director) references DIRECTORS(id) on delete cascade 163 4 Problems with integrity constraints 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 164 SQL as DDL 5