������� ���������������� ����� �������� � Introduction slide 150 � Data Definition slide 152 ��������������� �������� � Integrity constraints slide 159 ��� ��� ���������������� �������������������� Introduction Introduction � History � SEQUEL language of the SYSTEM/R relational DBMS � Functionalities : prototype (74-76) � data definition and data manipulation in the relational format � IBM research lab at San José � data control � Normalization at ISO � Manipulation language � The SQL1 norm (1986, 1989) � non procedural � The SQL2 norm (1992) � borrowed to relational algebra and to the tuple relational calculus � The SQL3 norm � Power of the manipulation language � Query language of (quite) all relational DBMSs Relational Algebra � ORACLE (Oracle Corporation - 1977) � INGRES (Ingres Technology - 1980) + Functions-Aggregates + Sorting � DB2 (IBM - 1984) � INFORMIX (Informix Inc - 1981) � A SQL query (without functions and sorting) � SYBASE (Sybase Inc - 1984) � Set of Relational Algebra operations � MySQL (1995) ��� ��� SQL as DDL 1
!����!��������� ����!!��#�$�%��������� &���� Data Definition � Relation Schema Definition � A finite set of domains � Relational View Definition � optional key: � Duplicate rows � Integrity Constraint Definition � The projection operator is different ( � � Right Definition DISTINCT) � Validation Process � A Relation is not a set � data storage and index definition (not normalized => DBMS dependent !!) ��� ��" '�����!�(���� %����������)�(� Data Definition Data Definition � Numeric : � Creation INTEGER, SMALLINT � CREATE TABLE Project ( DECIMAL (m,n), NUMBER(m,n) � ProjNumber Integer , FLOAT, REAL ProjName Char(20), � � String : CHAR (n), VARCHAR(n) PLocation Char(40) ) � Temporal : DATE (SQL2 ! norm ) � Evolution add an attribute (SQL2 ! norm) � Specific DBMS have their own domains � ALTER TABLE Project ADD COLUMN DepNumber Integer � NULL : missing value � Delete (SQL2 ! norm ) DROP TABLE Project ��� ��� SQL as DDL 2
&�#�� ����+��� �,�(��� &�#�� ����+��� �,�(��� �� � Movies(id, title, yr, score, votes, director ) Movies � Directors(id, name) score Castings Actors 0,n id 0,n idactor ord title � Actors(id, name) nameactor yr score � Castings( movieid, actorid , ord) 1,1 IsDirectedBy 0,n Directors iddirector namedirector ��* ��- .,�(��� /�������� ����������� CREATE TABLE CASTINGS ( CREATE TABLE MOVIES ( movieid integer, id integer, actorid integer, ord integer) title char (40), � Business rules for data yr integer , score integer, � Consistent state of the database votes integer, director integer)) � Enforced at any time CREATE TABLE ACTORS ( id integer, name char(40) ) CREATE TABLE DIRECTORS ( id integer, name char(40)) ��� ��� SQL as DDL 3
/�������� ����������� �,�(���� /�������� ����������� ��� !'&� � When ? Relational data model: � At schema definition (mainly) key uniqueness (id in MOVIES) Movie database schema � During schema lifecycle foreign key (director to DIRECTORS) Domain: � How to express IC ? MOVIES(id, title, yr, score, votes, director) ord between 1 and 10 � SQL yr between 1900 and 2005 � SQL extensions Not null ACTORS(id, name) � What about commercial DBMS ? actor’s name is mandatory Behavioral: CASTINGS(movieid, actorid, ord) � Just a few ICs the value of votes is always increasing � Other ICs supported by programs (triggers) DIRECTORS(id, name) �*� �*� .,�(��� /�������� ����������� ��� !'&���� CREATE TABLE CASTINGS ( CREATE TABLE MOVIES ( movieid integer, id integer PRIMARY KEY, actorid integer, ord integer CONSTRAINT ICord title char (40) NOT NULL, � Norms: CHECK (ord between 1 and 10)) yr integer CONSTRAINT ICyr � SQL86 : unicity, not null, view with «check option » CHECK (yr between 1900 and 2005), ALTER TABLE CASTINGS score integer CONSTRAINT ICscore � SQL89 : domain, key, referential integrity with reject add primary key (movieid, actorid) CHECK (score between 0 and 10), � SQL2 (SQL92) : referential integrity with «cascade delete ALTER TABLE CASTINGS votes integer NOT NULL , and update » add constraint fkACT director integer)) foreign key (actorid) references � Integrity constraints definition ACTORS(id) on delete cascade CREATE TABLE ACTORS ( � 2 different syntaxes: ALTER TABLE CASTINGS id integer, add constraint fkMOV At the end of an attribute definition � name char(40) foreign key (movieid) references At the end of a table definition PRIMARY KEY(id) ) � MOVIES(id) on delete cascade ALTER TABLE MOVIES CREATE TABLE DIRECTORS ( add constraint fkDIR id integer PRIMARY KEY, foreign key (director) references name char(40)) DIRECTORS(id) on delete cascade �*� �*" SQL as DDL 4
���+��(� 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 �*� SQL as DDL 5
Recommend
More recommend