This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Purpose: process to eliminate redundancy in Purpose: process to eliminate redundancy in relations due to functional or multi-valued relations due to functional or multi-valued dependencies.dependencies.
Decompose relation schema into Normal forms:Decompose relation schema into Normal forms:
– Boyce-Codd Normal Form (BCNF)Boyce-Codd Normal Form (BCNF)
– Third Normal Form (3NF)Third Normal Form (3NF)
– Fourth Normal Form (4NF)Fourth Normal Form (4NF) To obtain the new relations, project the schemas To obtain the new relations, project the schemas
onto the original relation schema (e.g. Movie)onto the original relation schema (e.g. Movie) To recover information (I.e. Movie) from the new To recover information (I.e. Movie) from the new
relations: natural join the new relations. relations: natural join the new relations.
FD’s: title year FD’s: title year length filmType studioName is a BCNF length filmType studioName is a BCNF violation, so Movie not in BCNFviolation, so Movie not in BCNF
To obtain the new relations, project the schemas onto MovieTo obtain the new relations, project the schemas onto Movie
To recover information (I.e. Movie) from the new relations: To recover information (I.e. Movie) from the new relations: natural join the new relations. Does not lose information.natural join the new relations. Does not lose information.
Mutivalued Dependencies (MVD)Mutivalued Dependencies (MVD)Given: relation schema R, and AGiven: relation schema R, and A11AA22…A…Ann and B and B1BB2…B…Bm be subsets be subsets
of attributes of R.of attributes of R.
MVD : AMVD : A11AA22…A…Ann B B1BB2…B…Bm holds in R if : holds in R if :
For each pair of tuples t and u of relation R that agree on For each pair of tuples t and u of relation R that agree on all the A’s, we can find in R some tuple v that agrees:all the A’s, we can find in R some tuple v that agrees:
1.1. With both t and u on the A’s,With both t and u on the A’s,2.2. With t on the B’s, andWith t on the B’s, and3.3. With u on all attributes of R that are not among the With u on all attributes of R that are not among the
A’s or B’sA’s or B’sAA11AA22…A…Ann B B1BB2…B…Bm is an assertion about R that two is an assertion about R that two
attributes or sets of attributes in R are attributes or sets of attributes in R are independentindependent of one of one another.another.
Cause redundancy not related to FD’s in a BCNF schema. Cause redundancy not related to FD’s in a BCNF schema. Most common source: putting 2 or more many-many Most common source: putting 2 or more many-many
relationships in a single relation.relationships in a single relation.
If AIf A11AA22…A…Ann B B1BB2…B…Bm holds for R, then AA11AA22…A…Ann CC1CC2…C…Ck holds where the C’s are the B’s + one or more of the A’s. The converse also hold.
Transitive rule
If AA11AA22…A…Ann B B1BB2…B…Bm and BB11BB22…B…Bmm C C1CC2…C…Ck
then AA11AA22…A…Ann C C1CC2…C…Ck
Splitting rule does not holdE.g. name street city, but not name street city, but not name street street
So, always start with set of attributes on the R.S. because So, always start with set of attributes on the R.S. because splitting rule does not hold.splitting rule does not hold.
More MVD RulesMore MVD Rules Every FD is an MVDEvery FD is an MVD
Because If FD Because If FD AA11AA22…A…Ann BB1BB2…B…Bm, then swapping B’s between , then swapping B’s between tuples that agree on A’s doesn’t create new tuples.tuples that agree on A’s doesn’t create new tuples.
Complementation ruleComplementation rule
If If X X Y, then X Y, then X Z, where Z is all attributes not in X Z, where Z is all attributes not in X or Yor Y
e.g. Star_Star_In {name, street, city, title, year}e.g. Star_Star_In {name, street, city, title, year}
Given: relation R, and nontrivial MVD X Given: relation R, and nontrivial MVD X Y that violate Y that violate 4NF4NF
1.1. Decompose X Decompose X Y into XY and X Y into XY and X (R-Y) (R-Y)
2.2. Produce the relations by projecting R onto XY and Produce the relations by projecting R onto XY and X X (R-Y) (R-Y)
3.3. Reconstruct R from the new relations using natural joinReconstruct R from the new relations using natural join
e.g. Star_Star_In {name, street, city, title, year} and e.g. Star_Star_In {name, street, city, title, year} and
name name street city street city
Decompose Star_Star_In using name Decompose Star_Star_In using name street city into street city into {name, street, city} and {name, title, year}{name, street, city} and {name, title, year}
Given: Relation R, decomposed into schemes RGiven: Relation R, decomposed into schemes R11, R, R22, … , …
RRkk, and D is a set of dependencies., and D is a set of dependencies.
Definition: RDefinition: R11, R, R22, … R, … Rk k is a lossless-join (w.r.t. D) if for is a lossless-join (w.r.t. D) if for
every relation r for R satisfying D:every relation r for R satisfying D:
r = r = R1R1(r) (r) R2R2(r) (r) …RkRk(r) (r)
i.e. Every relation r for R is the natural join of i.e. Every relation r for R is the natural join of its projections onto the Rits projections onto the Rii’s.’s.
The lossless-join property is necessary if the decomposed The lossless-join property is necessary if the decomposed relation is to be recoverable from its relation is to be recoverable from its decomposition.decomposition.
However, joins are expensive. So, don’t over decompose!However, joins are expensive. So, don’t over decompose!
SQL-99 extends SQL2 with object-relational features and SQL-99 extends SQL2 with object-relational features and other new featuresother new features
Most DBMS vendors implements the core, and then add Most DBMS vendors implements the core, and then add bells and whistles and variationsbells and whistles and variations
Query capability is close to relational algebra, with lots of Query capability is close to relational algebra, with lots of extensions.extensions.
Union, Intersection, Difference of Queries (continued)Union, Intersection, Difference of Queries (continued)
Q1 and Q2 are queries that produce relationsQ1 and Q2 are queries that produce relations
R1 and R2, or results of Q1 and Q2 should have R1 and R2, or results of Q1 and Q2 should have the same list of attributes and attribute types. the same list of attributes and attribute types. Rename if necessary.Rename if necessary.
Duplicates are eliminated automaticallyDuplicates are eliminated automatically
Add the keyword ALL after UNION, Add the keyword ALL after UNION, INTERSECT, or EXCEPT to prevent duplicates INTERSECT, or EXCEPT to prevent duplicates eliminationelimination
3.3. Value withheldValue withheld Operations involving NULLOperations involving NULL
1.1. Arithmetic operation: result is NULLArithmetic operation: result is NULL
2.2. Comparison: result is UNKNOWNComparison: result is UNKNOWN NULL is NULL is notnot a constant, therefore NULL cannot be used a constant, therefore NULL cannot be used
explicitly as an operand.explicitly as an operand. IS NULL and IS NOT NULL checksIS NULL and IS NOT NULL checks Read "Pitfalls Regarding Nulls" pp. 250Read "Pitfalls Regarding Nulls" pp. 250
Table level SQL (ref. 6.6, pp. 292)Table level SQL (ref. 6.6, pp. 292)
Create table – to define the schema of a base table Create table – to define the schema of a base table (Ref. 6.6.1 for data types syntax)(Ref. 6.6.1 for data types syntax)
E.g. E.g. create tablecreate table EMP EMP (( empno empno int not null,int not null, lastName lastName varchar(varchar(3030) not null,) not null, firstName firstName varchar(varchar(3030) not null,) not null, num_of_children num_of_children int,int, constraintconstraint pk_EMP pk_EMP primary keyprimary key ((empnoempno))););
Drop table – to destroy a base tableDrop table – to destroy a base tablee.g. e.g. drop tabledrop table EMP; EMP;
Tuple Modification Statements (ref. 6.5, pp. 286)Tuple Modification Statements (ref. 6.5, pp. 286)
Insert – to add a rowInsert – to add a row
Syntax: Syntax: insert intoinsert into R(A R(A11..A..Ann) ) valuesvalues (v (v11…v…vnn))
– E.g. E.g. insert intoinsert into emp( emp(empnoempno,, lastName lastName,, firstName firstName, , num_of_children)num_of_children) valuesvalues (12345, ‘Doe’, ‘John’, 1) (12345, ‘Doe’, ‘John’, 1)
– Or Or insert intoinsert into emp emp valuesvalues (12345, ‘Doe’, ‘John’, 1) (12345, ‘Doe’, ‘John’, 1)
Delete – to remove a rowDelete – to remove a row
Syntax: Syntax: delete fromdelete from R R wherewhere <condition> <condition>
– E.g. E.g. delete fromdelete from emp emp wherewhere empno = 12345 empno = 12345 Update – to modify the contents of a rowUpdate – to modify the contents of a row
Syntax: Syntax: updateupdate R R setset A Aii = value = value wherewhere A Ajj = targetValue = targetValue
– E.g. E.g. updateupdate emp emp setset num_of_children = 2 num_of_children = 2 wherewhere empno = empno = 1234512345
An operator to augment the result of a join by the An operator to augment the result of a join by the dangling tuples, padded with null values.dangling tuples, padded with null values.
Full outerjoin of R1 and R2 is a join that includes all Full outerjoin of R1 and R2 is a join that includes all rows from R1 and R2 matched or not. Unmatched rows rows from R1 and R2 matched or not. Unmatched rows are padded with NULLs.are padded with NULLs.
LEFT outerjoin of R1 and R2 is a join that includes all LEFT outerjoin of R1 and R2 is a join that includes all rows from R1, matched or not, plus the matching values rows from R1, matched or not, plus the matching values from R2. Unmatched rows are padded with NULLs.from R2. Unmatched rows are padded with NULLs.
RIGHT outerjoin of R1 and R2 is a join that includes all RIGHT outerjoin of R1 and R2 is a join that includes all rows from R2, matched or not, plus the matching values rows from R2, matched or not, plus the matching values from R1. Unmatched rows are padded with NULLs.from R1. Unmatched rows are padded with NULLs.
The joining may be NATURAL or theta joinThe joining may be NATURAL or theta join
1.1. R1R1 { {FULL FULL | | LEFT LEFT | | RIGHT}RIGHT} OUTER JOIN OUTER JOIN R2 R2 ON conditional expressionON conditional expression;;
E.g. 1. Movie E.g. 1. Movie FULL OUTER JOINFULL OUTER JOIN StarsIn StarsIn ONON title = movieTitle title = movieTitle ANDAND year = movieYear; year = movieYear;
E.g. 2. MovieStar E.g. 2. MovieStar LEFT OUTER JOINLEFT OUTER JOIN StarsIn StarsIn ONON title = movieTitle title = movieTitle ANDAND year = movieYear; year = movieYear;
E.g. 3. MovieStar E.g. 3. MovieStar RIGHT OUTER JOINRIGHT OUTER JOIN StarsIn StarsIn ONON title = movieTitle title = movieTitle ANDAND year = movieYear; year = movieYear;