File notes/00_sum.md changed (mode: 100644) (index 32568de..d40590c) |
... |
... |
author: Clément Aubert |
9 |
9 |
institute: Augusta University |
institute: Augusta University |
10 |
10 |
dir: ltr |
dir: ltr |
11 |
11 |
lang: en |
lang: en |
|
12 |
|
numbersections: true |
12 |
13 |
keywords: |
keywords: |
13 |
14 |
- Computer Science |
- Computer Science |
14 |
15 |
- Database |
- Database |
|
... |
... |
Mathematical relations, set-theory, first-order predicate logic! |
188 |
189 |
|
|
189 |
190 |
# The Relational Data Model and Relational Database Constraints |
# The Relational Data Model and Relational Database Constraints |
190 |
191 |
|
|
191 |
|
|
|
192 |
|
**Plan:** |
|
193 |
|
|
|
194 |
|
The Relational Data Model and Relational Database Constraints |
|
195 |
|
|
|
196 |
|
1. Concepts |
|
197 |
|
1. Domains, Attributes, Tuples and Relations |
|
198 |
|
2. Characteristics of Relations |
|
199 |
|
3. Notation |
|
200 |
|
2. Constraints |
|
201 |
|
1. Types of Constraints |
|
202 |
|
2. Keys |
|
203 |
|
3. Foreign Keys |
|
204 |
|
3. Transactions & Operations |
|
205 |
|
1. Presentation |
|
206 |
|
2. Insert / Delete / Update |
|
207 |
|
3. Dealing with Violations |
|
208 |
|
|
|
209 |
|
|
|
210 |
|
(Next: SQL) |
|
211 |
|
|
|
212 |
|
|
|
213 |
192 |
**Ressources:** |
**Ressources:** |
214 |
193 |
|
|
215 |
194 |
- Textbook: Ch. 3 |
- Textbook: Ch. 3 |
|
... |
... |
Relational data model: |
266 |
245 |
**Last Time:** |
**Last Time:** |
267 |
246 |
Relational model, vocabulary (domain, attribute, tuple, relation, relation scheme, atomic value |
Relational model, vocabulary (domain, attribute, tuple, relation, relation scheme, atomic value |
268 |
247 |
|
|
|
248 |
|
## Constraints |
269 |
249 |
|
|
270 |
|
## Types of Constraints |
|
|
250 |
|
### Types of Constraints |
271 |
251 |
|
|
272 |
252 |
Those are constraints on the tuples (there are constraints on the scheme, for instance, "a relation can't have two attributes with the same name"). |
Those are constraints on the tuples (there are constraints on the scheme, for instance, "a relation can't have two attributes with the same name"). |
273 |
253 |
|
|
274 |
|
### Inherent model-based constraints (implicit) |
|
|
254 |
|
#### Inherent model-based constraints (implicit) |
275 |
255 |
|
|
276 |
256 |
Those are part of the model. |
Those are part of the model. |
277 |
257 |
|
|
278 |
258 |
- No duplicate tuple |
- No duplicate tuple |
279 |
259 |
- Arity must match |
- Arity must match |
280 |
260 |
|
|
281 |
|
### Schema-based constraints (explicit) |
|
|
261 |
|
#### Schema-based constraints (explicit) |
282 |
262 |
|
|
283 |
263 |
Those are parts of the schema |
Those are parts of the schema |
284 |
264 |
|
|
|
... |
... |
Those are parts of the schema |
286 |
266 |
- Entity integrity constraint (no primary key value can be `NULL`) |
- Entity integrity constraint (no primary key value can be `NULL`) |
287 |
267 |
- Referential integrity constraint (referred values must exists) |
- Referential integrity constraint (referred values must exists) |
288 |
268 |
|
|
289 |
|
### Application-based constraints (semantics) |
|
|
269 |
|
#### Application-based constraints (semantics) |
290 |
270 |
|
|
291 |
271 |
*Cannot* be expressed in the schema, and hence must be enforced by some other way. |
*Cannot* be expressed in the schema, and hence must be enforced by some other way. |
292 |
272 |
Example: the date of birth of an employee must be greater than xxx. |
Example: the date of birth of an employee must be greater than xxx. |
293 |
273 |
|
|
294 |
|
## Keys |
|
|
274 |
|
### Keys |
295 |
275 |
|
|
296 |
276 |
Tuples can't be equal, so a subset of values must distinguish them, we study the corresponding subset of attributes. |
Tuples can't be equal, so a subset of values must distinguish them, we study the corresponding subset of attributes. |
297 |
277 |
|
|
|
... |
... |
Constraints: |
336 |
316 |
- Entity Integrity Constraint and Referential Integrity Constraint. |
- Entity Integrity Constraint and Referential Integrity Constraint. |
337 |
317 |
|
|
338 |
318 |
|
|
339 |
|
|
|
340 |
319 |
| CAR(\underline{VIN}, Make, Model, Year) |
| CAR(\underline{VIN}, Make, Model, Year) |
341 |
320 |
| DRIVER(\underline{State}, \underline{Licence\_number}, Name, Address) |
| DRIVER(\underline{State}, \underline{Licence\_number}, Name, Address) |
342 |
321 |
| INSURANCE(\underline{Policy\_Number}, Insured\_Car, Insured\_Driver\_State, Insured\_Driver\_Num, Rate) |
| INSURANCE(\underline{Policy\_Number}, Insured\_Car, Insured\_Driver\_State, Insured\_Driver\_Num, Rate) |
343 |
322 |
| PRICE(\underline{Stock\_number}, Car\_Vin, Price, Margin) |
| PRICE(\underline{Stock\_number}, Car\_Vin, Price, Margin) |
344 |
323 |
|
|
345 |
|
## Presentation |
|
|
324 |
|
## Transactions And Operations |
346 |
325 |
|
|
347 |
326 |
Operations are of two kinds: retrievals and updates. |
Operations are of two kinds: retrievals and updates. |
348 |
327 |
|
|
|
... |
... |
They are two constraints for updates: |
356 |
335 |
|
|
357 |
336 |
Transaction = series of retrievals and updates performed by an application program, that leaves the DB in a consistent state. |
Transaction = series of retrievals and updates performed by an application program, that leaves the DB in a consistent state. |
358 |
337 |
|
|
359 |
|
## Insert / Delete / Update |
|
|
338 |
|
### Insert / Delete / Update |
360 |
339 |
|
|
361 |
340 |
a., b. and c. refers to the "remedies", on the next page. |
a., b. and c. refers to the "remedies", on the next page. |
362 |
341 |
|
|
363 |
|
### Insert: |
|
|
342 |
|
#### Insert: |
364 |
343 |
|
|
365 |
344 |
`Insert <109920, Honda, Accord, 2012> into CAR.` |
`Insert <109920, Honda, Accord, 2012> into CAR.` |
366 |
345 |
|
|
|
... |
... |
How things can go wrong: |
371 |
350 |
- Wrong number of arguments (a.) |
- Wrong number of arguments (a.) |
372 |
351 |
- Fail to reference to an existing value for the foreign key (a.) |
- Fail to reference to an existing value for the foreign key (a.) |
373 |
352 |
|
|
374 |
|
### Delete |
|
|
353 |
|
#### Delete |
375 |
354 |
|
|
376 |
355 |
`Delete the DRIVER tuple with State = GA and Licence_number = 123` |
`Delete the DRIVER tuple with State = GA and Licence_number = 123` |
377 |
356 |
|
|
|
... |
... |
How things can go wrong: |
380 |
359 |
- Deleting tuples inadvertently (meta) |
- Deleting tuples inadvertently (meta) |
381 |
360 |
- Delecing tuples that are referenced (a., b., c.) |
- Delecing tuples that are referenced (a., b., c.) |
382 |
361 |
|
|
383 |
|
### Update (a.k.a. modify) |
|
|
362 |
|
#### Update (a.k.a. modify) |
384 |
363 |
|
|
385 |
364 |
`Update Name of tuple in DRIVER where State = GA and Licence_number = 123 to Georges` |
`Update Name of tuple in DRIVER where State = GA and Licence_number = 123 to Georges` |
386 |
365 |
|
|
|
... |
... |
How things can go wrong: |
392 |
371 |
- Change foreign key to a non-existing value (a.) |
- Change foreign key to a non-existing value (a.) |
393 |
372 |
|
|
394 |
373 |
|
|
395 |
|
## Dealing with Violations |
|
|
374 |
|
### Dealing with Violations |
396 |
375 |
|
|
397 |
376 |
a. Reject (restrict) |
a. Reject (restrict) |
398 |
377 |
b. Cascade (propagate) |
b. Cascade (propagate) |
|
... |
... |
INSERT INTO PROF VALUES ( |
513 |
492 |
- Textbook: Ch. 4, 5 (but warning, describe `SQL`, not one of its implementation) |
- Textbook: Ch. 4, 5 (but warning, describe `SQL`, not one of its implementation) |
514 |
493 |
- On-line ressources: cf. homework + web-page |
- On-line ressources: cf. homework + web-page |
515 |
494 |
|
|
516 |
|
**Plan:** |
|
517 |
|
|
|
518 |
|
1. Overview of Constraints |
|
519 |
|
2. Foreign Keys |
|
520 |
|
3. Restrictions on Foreign Keys |
|
521 |
|
|
|
522 |
495 |
Note: Use `DESCRIBE <TableName>` and `SELECT * FROM <TableName>` intensively to see where you are. |
Note: Use `DESCRIBE <TableName>` and `SELECT * FROM <TableName>` intensively to see where you are. |
523 |
496 |
Use `DROP TABLE <TableName>` and `DROP SCHEMA <SchemaName>` for a "fresh start". |
Use `DROP TABLE <TableName>` and `DROP SCHEMA <SchemaName>` for a "fresh start". |
524 |
497 |
|
|
|
... |
... |
SELECT * FROM Table_set_null; |
773 |
746 |
|
|
774 |
747 |
Note: Use `DESCRIBE <Table>;`, `SHOW TABLES;` and `SELECT * FROM <Table>;` intensively to see where you are. |
Note: Use `DESCRIBE <Table>;`, `SHOW TABLES;` and `SELECT * FROM <Table>;` intensively to see where you are. |
775 |
748 |
|
|
776 |
|
**Plan:** |
|
777 |
|
|
|
778 |
|
1. Constructing and Populating a New Example |
|
779 |
|
2. A First Look at Conditions |
|
780 |
749 |
|
|
781 |
750 |
### Constructing and Populating a New Example |
### Constructing and Populating a New Example |
782 |
751 |
|
|
|
... |
... |
Take the time to introduce future topics + to give exam back. |
1218 |
1187 |
|
|
1219 |
1188 |
--- |
--- |
1220 |
1189 |
|
|
1221 |
|
## Plan: (for the future lectures) |
|
1222 |
|
|
|
1223 |
|
1. E.R. Models |
|
1224 |
|
|
|
1225 |
|
a. Enties and Attributes |
|
1226 |
|
b. Entity Types and Key Attributes |
|
1227 |
|
c. Relationships and Structural Constraints |
|
1228 |
|
1. Vocabulary |
|
1229 |
|
2. Recursive & Role Names |
|
1230 |
|
3. Constraints |
|
1231 |
|
4. Attributes |
|
1232 |
|
5. Relationships of Degree Higher Than 2 |
|
1233 |
|
d. Weak Entity Types |
|
1234 |
|
e. Alternative Notations |
|
1235 |
|
f. Enhanced (extended) Entity–Relationship (EER) Models |
|
1236 |
|
g. Reverse Engineering |
|
1237 |
|
|
|
1238 |
|
2. E.R.-to-Relational Models Mapping |
|
1239 |
|
|
|
1240 |
|
3. Guidelines and Normal Forms |
|
1241 |
|
|
|
1242 |
|
a. General Rules |
|
1243 |
|
b. Functional Dependencies |
|
1244 |
|
c. Normal Forms and Keys |
|
1245 |
|
|
|
1246 |
|
4. UML Diagram |
|
1247 |
|
|
|
1248 |
|
a. Overview |
|
1249 |
|
b. Types of Diagrams |
|
1250 |
|
|
|
1251 |
1190 |
**Textbook:** |
**Textbook:** |
1252 |
1191 |
|
|
1253 |
1192 |
- E.-R. models: Chapter 7 |
- E.-R. models: Chapter 7 |
|
... |
... |
Take the time to introduce future topics + to give exam back. |
1255 |
1194 |
- Normalization: 15 |
- Normalization: 15 |
1256 |
1195 |
- UML: not so much in the textbook, but you can look at 7.8 and 10.3 |
- UML: not so much in the textbook, but you can look at 7.8 and 10.3 |
1257 |
1196 |
|
|
1258 |
|
## E.R. Models |
|
|
1197 |
|
## Entity-Relationship (ER) model |
1259 |
1198 |
|
|
1260 |
1199 |
Data = entity, relationships, attributes |
Data = entity, relationships, attributes |
1261 |
1200 |
|
|
|
... |
... |
Naming convention: |
1337 |
1276 |
{ width=100% } |
{ width=100% } |
1338 |
1277 |
|
|
1339 |
1278 |
|
|
1340 |
|
#### Recursive |
|
|
1279 |
|
#### Recursive And Role Names |
1341 |
1280 |
|
|
1342 |
1281 |
Convenient, and sometimes mandatory, to give role names: |
Convenient, and sometimes mandatory, to give role names: |
1343 |
1282 |
|
|
|
... |
... |
Crow's foot notation: |
1435 |
1374 |
|
|
1436 |
1375 |
<https://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model#Crow%27s_foot_notation> |
<https://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model#Crow%27s_foot_notation> |
1437 |
1376 |
|
|
1438 |
|
## E.E.R. Models |
|
|
1377 |
|
## Enhanced Entity–Relationship (E.E.R) Model |
1439 |
1378 |
|
|
1440 |
1379 |
Extended (or Enhanced) E.R. Models have additionaly: |
Extended (or Enhanced) E.R. Models have additionaly: |
1441 |
1380 |
|
|
|
... |
... |
Closer to OO programming. |
1446 |
1385 |
|
|
1447 |
1386 |
## Reverse Engineering |
## Reverse Engineering |
1448 |
1387 |
|
|
1449 |
|
From Rel. Models to E.R. models (sometimes needed) |
|
|
1388 |
|
From relational models to E.R. models (sometimes needed) |
1450 |
1389 |
|
|
1451 |
1390 |
{ width=100% } |
{ width=100% } |
1452 |
1391 |
|
|
|
... |
... |
Multivalued attribute | Relation and foreign key |
1508 |
1447 |
Value set | Domain |
Value set | Domain |
1509 |
1448 |
Key attribute | Primary key |
Key attribute | Primary key |
1510 |
1449 |
|
|
1511 |
|
Need to work on a better example, includings n-ary relationship, and propagate options. |
|
1512 |
|
|
|
1513 |
|
--- |
|
1514 |
|
|
|
1515 |
|
+ Propagate option? Cascade, most of the time: weak entity type, lookup tables, etc. |
|
|
1450 |
|
**Worked on PB 2 and 3 of HW4, that needs to be adapted. Needs to be written more properly. Cf. Drawing in Lecture 16's notes.** |
1516 |
1451 |
|
|
|
1452 |
|
**Need to work on a better example, includings n-ary relationship, and propagate options.** |
1517 |
1453 |
|
|
1518 |
|
### Outro |
|
|
1454 |
|
**+ Propagate option? Cascade, most of the time: weak entity type, lookup tables, etc.** |
1519 |
1455 |
|
|
1520 |
|
E.R. Model | Relational Model |
|
1521 |
|
--- | --- |
|
1522 |
|
Entity type | Entity relation |
|
1523 |
|
1:1 or 1:N relationship type | Foreign key (or relationship relation) |
|
1524 |
|
M:N relationship type | Relationship relation and two foreign keys |
|
1525 |
|
n-ary relationship type | Relationship relation and n foreign keys |
|
1526 |
|
Simple attribute | Attribute |
|
1527 |
|
Composite attribute | Set of simple component attributes |
|
1528 |
|
Multivalued attribute | Relation and foreign key |
|
1529 |
|
Value set | Domain |
|
1530 |
|
Key attribute | Primary key |
|
1531 |
|
|
|
1532 |
|
**Worked on PB 2 and 3 of HW4, that needs to be adapted. Needs to be written more properly. Cf. Drawing in Lecture 16's notes.** |
|
1533 |
1456 |
|
|
1534 |
1457 |
## Guidelines and Normal Form |
## Guidelines and Normal Form |
1535 |
1458 |
|
|
|
... |
... |
BRAND(Name, Email) |
1615 |
1538 |
|
|
1616 |
1539 |
Brand, Name and Name being the ID. |
Brand, Name and Name being the ID. |
1617 |
1540 |
|
|
1618 |
|
## Functional Dependencies |
|
|
1541 |
|
### Functional Dependencies |
1619 |
1542 |
|
|
1620 |
1543 |
Formal tool to assess how "good" a database is, a property of the relation schema. |
Formal tool to assess how "good" a database is, a property of the relation schema. |
1621 |
1544 |
|
|
1622 |
|
### Using Semantics of Attributes |
|
|
1545 |
|
#### Using Semantics of Attributes |
1623 |
1546 |
|
|
1624 |
1547 |
"What *should* be." |
"What *should* be." |
1625 |
1548 |
|
|
|
... |
... |
Think about their dependencies, and list them: |
1637 |
1560 |
- `TEACHER.Office` → `TEACHER.Phone` |
- `TEACHER.Office` → `TEACHER.Phone` |
1638 |
1561 |
- `MAKER.Owner` and `MARKER.Color` → `MARKER.Brand` ? |
- `MAKER.Owner` and `MARKER.Color` → `MARKER.Brand` ? |
1639 |
1562 |
|
|
1640 |
|
### Using Relation States |
|
|
1563 |
|
#### Using Relation States |
1641 |
1564 |
|
|
1642 |
1565 |
"What *is*.", can disprove some of the assumptions made previously, but shouldn't add new dependencies based on it (they may be by chance!). |
"What *is*.", can disprove some of the assumptions made previously, but shouldn't add new dependencies based on it (they may be by chance!). |
1643 |
1566 |
|
|
|
... |
... |
Att. 3 → Att. 2 | Att. 3 → Att. 2 |
1662 |
1585 |
Att. 1 → Att. 3 | Att. 2 → Att. 1 |
Att. 1 → Att. 3 | Att. 2 → Att. 1 |
1663 |
1586 |
{Att. 1, Att. 2} → Att. 3 | {Att. 3, Att. 2} → Att. 1 |
{Att. 1, Att. 2} → Att. 3 | {Att. 3, Att. 2} → Att. 1 |
1664 |
1587 |
|
|
1665 |
|
### Notations |
|
|
1588 |
|
#### Notations |
1666 |
1589 |
|
|
1667 |
1590 |
Functional dependencies list the constraints between two sets of attributes from the database. |
Functional dependencies list the constraints between two sets of attributes from the database. |
1668 |
1591 |
X → Y reads "X fixes Y", and applier that values in Y are fixed by the value in X. |
X → Y reads "X fixes Y", and applier that values in Y are fixed by the value in X. |
|
... |
... |
Note that: |
1685 |
1608 |
- Transitivity: If X → Y and Y → Z, then X → Z |
- Transitivity: If X → Y and Y → Z, then X → Z |
1686 |
1609 |
|
|
1687 |
1610 |
|
|
1688 |
|
### Definitions |
|
|
1611 |
|
#### Definitions |
1689 |
1612 |
|
|
1690 |
1613 |
Remember superkey (not minimal key), key, candidate key, secondary key? |
Remember superkey (not minimal key), key, candidate key, secondary key? |
1691 |
1614 |
|
|
|
... |
... |
A FD : X → Y is a **transivive dependency** if there exist a set of attribute |
1714 |
1637 |
--- |
--- |
1715 |
1638 |
|
|
1716 |
1639 |
|
|
1717 |
|
## Normal Forms and Keys |
|
|
1640 |
|
### Normal Forms and Keys |
1718 |
1641 |
|
|
1719 |
1642 |
First, Second, Third, Fourth, Fifth normal form ("X"NF). |
First, Second, Third, Fourth, Fifth normal form ("X"NF). |
1720 |
1643 |
Stronger than the Third, there is the Boyce-Codd NF (BCNF) |
Stronger than the Third, there is the Boyce-Codd NF (BCNF) |
|
... |
... |
Stronger than the Third, there is the Boyce-Codd NF (BCNF) |
1722 |
1645 |
If you satisfy $N$, you satisfy $N-1$, $N-2$, etc. |
If you satisfy $N$, you satisfy $N-1$, $N-2$, etc. |
1723 |
1646 |
The *normal form* of a relation is the highest normal form condition that it meets. |
The *normal form* of a relation is the highest normal form condition that it meets. |
1724 |
1647 |
|
|
1725 |
|
### Fist Normal Form |
|
|
1648 |
|
#### Fist Normal Form |
1726 |
1649 |
|
|
1727 |
|
#### Definition |
|
|
1650 |
|
##### Definition |
1728 |
1651 |
|
|
1729 |
1652 |
The domain of all attributes must be atomic (simple, indivisible): exclude multi-valued and composite attributes. |
The domain of all attributes must be atomic (simple, indivisible): exclude multi-valued and composite attributes. |
1730 |
1653 |
|
|
1731 |
1654 |
Sometimes, additional requirement that every relation has a primary key. |
Sometimes, additional requirement that every relation has a primary key. |
1732 |
1655 |
We will take this requirement to be part of the definition of 1NF, but some authors take a relation to be in 1NF if it has at least candidate keys (i.e., multiple possible keys, but no primary key, which makes their definition more general, cf. [@Textbook7, 14.4.1]). |
We will take this requirement to be part of the definition of 1NF, but some authors take a relation to be in 1NF if it has at least candidate keys (i.e., multiple possible keys, but no primary key, which makes their definition more general, cf. [@Textbook7, 14.4.1]). |
1733 |
1656 |
|
|
1734 |
|
#### Normalization |
|
|
1657 |
|
##### Normalization |
1735 |
1658 |
|
|
1736 |
1659 |
**To be written** |
**To be written** |
1737 |
1660 |
|
|
1738 |
|
### Second Normal Form |
|
|
1661 |
|
#### Second Normal Form |
1739 |
1662 |
|
|
1740 |
|
#### Definition |
|
|
1663 |
|
##### Definition |
1741 |
1664 |
|
|
1742 |
1665 |
1NF + Every non-prime attribute is fully functionnaly dependent on the primary key. |
1NF + Every non-prime attribute is fully functionnaly dependent on the primary key. |
1743 |
1666 |
|
|
1744 |
|
#### Normalization |
|
|
1667 |
|
##### Normalization |
1745 |
1668 |
|
|
1746 |
1669 |
For each attribute $A$ of the relation whose primary key is $A_1, ..., A_n$: |
For each attribute $A$ of the relation whose primary key is $A_1, ..., A_n$: |
1747 |
1670 |
|
|
|
... |
... |
Refinment: note that if more than one attribute depends of the same subset $\{A' |
1762 |
1685 |
|
|
1763 |
1686 |
Note also that if our primary key is a singleton, then there is nothing to do, we are in 2NF as soon as we are in 1NF. |
Note also that if our primary key is a singleton, then there is nothing to do, we are in 2NF as soon as we are in 1NF. |
1764 |
1687 |
|
|
1765 |
|
### Third Normal Form |
|
|
1688 |
|
#### Third Normal Form |
1766 |
1689 |
|
|
1767 |
|
#### Definition |
|
|
1690 |
|
##### Definition |
1768 |
1691 |
|
|
1769 |
1692 |
2NF + no non-prime attribute is transitively dependent on the primary key. |
2NF + no non-prime attribute is transitively dependent on the primary key. |
1770 |
1693 |
|
|
1771 |
|
#### Normalization |
|
|
1694 |
|
##### Normalization |
1772 |
1695 |
|
|
1773 |
1696 |
For each attribute $A$ of the relation whose primary key is $A_1, ..., A_n$: |
For each attribute $A$ of the relation whose primary key is $A_1, ..., A_n$: |
1774 |
1697 |
|
|
|
... |
... |
For each attribute $A$ of the relation whose primary key is $A_1, ..., A_n$: |
1786 |
1709 |
**ADD EXAMPLES** |
**ADD EXAMPLES** |
1787 |
1710 |
|
|
1788 |
1711 |
|
|
1789 |
|
### Notes And Examples |
|
|
1712 |
|
#### Notes And Examples |
1790 |
1713 |
|
|
1791 |
1714 |
CCL: every FD X → Y s.t. X is a proper subset of the primary key, or a non-prime attribute, is problematic. |
CCL: every FD X → Y s.t. X is a proper subset of the primary key, or a non-prime attribute, is problematic. |
1792 |
1715 |
|
|