logo
Published on

CH2. ER, Schema and Tables Through COMPANY

Authors
  • avatar
    Name
    seren-wib
    Twitter
Contents

ER, Schema and Tables Through One COMPANY

The same COMPANY drawn as three layers. ① ER (ch2) → ② the converted schema (ch3 3.4) → ③ tables filled with data (ch3 3.1). The tables below only point to where each concept is in the three figures. The exam's "draw the RDB" = look at ① and draw ②

1. ER diagram

COMPANY ER: 4 entities, 6 relationships, with (min,max)
SymbolMeaning
Rectangle / double rectangleentity type / weak entity type
Oval / underline / dashed underlineattribute / key / partial key
Double oval / dashed oval / branching ovalsmultivalued / derived / composite
Diamond / double diamondrelationship / identifying relationship. A double diamond is the relationship that ties a weak entity (double rectangle) to its owner entity. Without it you can't tell whose weak entity it is. In COMPANY there is one: DEPENDENTS_OF (employee–dependent) (section 6)
Single line / double linepartial / total participation
1, N, Mcardinality ratio (at most how many on the other side)
Purple (min,max)How many times, at least and at most, one entity of this type takes part in the relationship. min 0 = single line, min ≥ 1 = double line

2. Relational schema

COMPANY relational schema: 6 tables, PKs underlined, FK arrows
  • 6 tables = 4 rectangles + 1 M:N diamond (WORKS_ON) + 1 double oval (DEPT_LOCATIONS)
  • Every arrow is FK → PK. 7 arrows = 2 1:N diamonds (Dno, Dnum) + 1 1:1 diamond (Mgr_ssn) + 1 recursive (Super_ssn) + 2 from M:N (Essn, Pno) + 1 double diamond (DEPENDENT.Essn) + 1 double oval (Dnumber)
  • The double diamond (DEPENDENTS_OF) gets no table of its own; it just adds the owner's PK (Essn) to the weak entity DEPENDENT. That Essn is an FK and also part of DEPENDENT's PK
  • Watch the renamed columns: Number → Dnumber/Pnumber, Name → Dname/Pname, EMPLOYEE.Ssn taken elsewhere becomes Essn, Mgr_ssn, Super_ssn

3. Tables (relation state)

The 6 COMPANY tables filled with tuples
  • Header row = schema, the rows under it = state. Adding or deleting rows leaves the header unchanged
  • A value in an FK cell must be a value that exists in the other table's PK, or NULL. The five lines at the bottom right of the figure judge insert and delete violations

4. Entities and attributes

ConceptMeaning① ER② Schema③ Table
Entity typeThe kind of "thing" you want to keep as data. Like employee, department, project: the template for entities with the same attributes. One rectangle in ER, one table once converted
EntityOne real thing that fits the template. The employee John Smith. Not drawn in ER; one row in a tableNot drawn—
Entity setAll entities of the same template. Every employee. One rectangle stands for all of them; in a table, all the rows—
AttributeOne value an entity has. An employee's salary. In ER an oval hung off the rectangle by a line; in a table, one column
Key attributeAn attribute whose value differs for every entity, so knowing it pins down which one. Know the Ssn, get one employee. Underlined oval. A rectangle can have more than one (DEPARTMENT's Name, Number); in a table one of them becomes the PK
Composite attributeAn attribute that splits into smaller attributes. Name = Fname + Minit + Lname. Ovals branching off an oval. Only the leaf branches become columns; there's no Name column3 columns Fname, Minit, Lname
Multivalued attributeAn attribute with several values for one entity. One department in two places, Bellaire and Sugarland. Double oval. A table cell holds only one value, so it gets its own table
Derived attributeAn attribute that isn't stored but computed from elsewhere. A department's employee count comes from counting EMPLOYEE. Dashed oval. No column in the table

5. Relationships

ConceptMeaning① ER② Schema③ Table
Relationship typeHow two entity types are tied together. An employee works for a department = WORKS_FOR. A diamond between rectangles. Becomes an FK column or a new table
Relationship instanceOne actual fact of that relationship. Smith works for department 5. Not drawn in ER. One FK cell value in a tableNot drawn—
DegreeHow many entity types take part in one relationship. Employee–department is 2 (binary); supplier–project–part tied together at once is 3 (ternary). Count the rectangles connected to the diamond3 FKs per row
Cardinality 1:NOne entity on one side is tied to many on the other, and one on the other side is tied to only one on this side. One department has many employees, an employee has one department. 1 and N on the lines. The FK goes on the N side
Cardinality 1:1On both sides one entity is tied to only one on the other. One department has one manager, and that employee manages only one department. 1 and 1 on the lines. The FK goes on the double-line side
Cardinality M:NOn both sides one is tied to many. One employee on many projects, one project with many employees. M and N on the lines. One FK cell can't hold it, so a new table
Total participationEvery entity on that side, without exception, is in the relationship. No employee without a department. Double line between rectangle and diamond. That FK cell is never empty
Partial participationSome entities may not be in the relationship. Some employees aren't managers. Single line. That cell can be NULL
(min, max)At least and at most how many times one entity takes part in the relationship. An employee manages a department 0 or 1 times = (0,1); a department has exactly 1 manager = (1,1). min 0 means a single line, min 1 or more a double line, and whether max is 1 or N is the ratio abovemin ≥ 1 → NOT NULL / max 1 → one FK column, max N → new table—
Relationship attributeA value attached to the relationship, not an entity. Hours belong to neither the employee nor the project but to "the employee on that project". An oval hung off the diamond. In tables it follows the FK into the table it went to
Recursive relationshipA relationship an entity type has with itself. An employee supervises an employee. Both lines of the diamond go into the same rectangle. In a table, an FK pointing at its own PK
Role nameThe name of the role each side plays in a relationship. The supervising side is supervisor, the other supervisee. Text beside the line. In a recursive relationship both rectangles are the same, so without it you can't tell which line is whichStays as the FK column name (Super_ssn)—

6. Weak entities

ConceptMeaning① ER② Schema③ Table
Weak entity typeAn entity type whose own attributes can't pin down which entity it is. There are several dependents named "Alice", so you need to know whose daughter. Double rectangle. Once converted, the owner's PK column is added
OwnerThe entity type a weak entity depends on. Alice's employee. The rectangle across the double diamond. Its PK goes into the weak entity's tableThe EMPLOYEE that Essn points to—
Identifying relationshipThe relationship connecting a weak entity to its owner. Double diamond; the weak side's line is always double (no dependent without an owner). No table of its own; the FK column plays that role—
Partial keyAn attribute that tells entities apart only under the same owner. One employee's dependents don't share names. Oval with a dashed underline. Table PK = owner's PK + this

7. Relational model, keys and integrity

ConceptMeaning① ER② Schema③ Table
RelationOne table. One ER rectangle (double rectangles included), one M:N diamond and one double oval each become one table. 1:1 and 1:N diamonds, double diamonds and recursive diamonds make no table; they end up as FK columns. Sounds like relationship (diamond) but is a different word
Relation schemaThe table's template. Name + column names. EMPLOYEE(Fname, …, Dno). In ER, a rectangle plus the ovals hanging off it; this is the schema figure; in the table figure, the header row
Relation stateThe rows currently in that template. Changes as rows are inserted and deleted, but the schema stays. In ER, the entity set the rectangle stands for; everything below the header in the table figure—
TupleOne row. One employee = one entity, or one WORKS_ON row = one relationship factNot drawn (one entity / one relationship instance)—
DomainThe range of values allowed in that column. Salary is a positive integer. Values outside the range can't go in. In ER, the attribute's value setAs the type in DDL
NULLNo value in the cell. Unknown, or not applicable. Not 0 or an empty string. Borg's Super_ssn, who has no supervisor. In ER, the side with min 0 (single line) is where NULLs come from—
DegreeIn the relational model, the number of columns in a table. Count the header boxes. In ER, leaf ovals + FKs brought in. Same name as ER's degree (rectangles attached to a diamond) but a different thing
SuperkeyA set of columns where no two rows can have the same values. {Dnumber} works, and so does {Dnumber, Dname} with extras addedNot drawn—
KeyA superkey that stops being a superkey if you remove even one column. {Dname} and {Dnumber} each. If there are several, all are candidates. In ER, each underlined oval is a key—
Primary keyThe one candidate picked to represent the table. Dnumber. Underlined. A column with no duplicates and no NULLs in the tableNot drawn (ER only underlines; one of them is picked in the schema)
Foreign keyA column holding another table's (or its own table's) PK values to point at that row. EMPLOYEE.Dno = 5 is row 5 of DEPARTMENT. An arrow in the schema. In ER, 1:N and 1:1 diamonds, double diamonds, double ovals and recursive diamonds all become FKs
Both PK and FKA column brought in from someone else's PK that is also part of its own PK. WORKS_ON's Essn, Pno come from the M:N diamond; DEPENDENT's Essn comes from the double diamond. Half yellow, half blue
Key constraintNo two rows with the same PK value. You can't insert two employees with Ssn 123456789. In ER, a key attribute (underlined oval) differs for every entityUnderline
Entity integrityPK cells can't be NULL. An employee with an empty Ssn can't be identified. In ER, an entity without its key attribute can't be singled outUnderlined column
Referential integrityAn FK cell's value must actually exist in the other table's PK, or be NULL. Dno = 7 with no department 7 is a violation. In ER, the entity across the diamond must actually existThat value must exist at the arrow's end
Violation checksInsert checks all three: Key, Entity and Referential. Delete only checks whether other rows point at the deleted row through an FK. Treat update as delete + insertNot drawn—Bottom right of figure ③