entity-relationship diagram rules entitylyndiahayden.yolasite.com/resources/317/cis 317...
TRANSCRIPT
Page 1
Entity-Relationship Diagram Rules 1. Entity – object; nouns; independent existence (physical or conceptual)
a. Physical – staff, part, customer, branch b. Conceptual – inspection, sale
2. Relationship – verbs; meaningful association to entities
a. Recursive relationship – entity in more than 1 role (i.e. manager, sponsor); two tables
3. Attributes – adjectives; property of an attribute; a. Simple – single value: SS#, phone#, names by itself (fname, lname) b. Composite - address, name (fname and lname joined); indented in UML c. Derived – value of attribute(s) not necessarily in same entity (i.e. /total staff) d. Multi-value attributes cause normalization issues (i.e. address, phone number) e. Weak attribute – no primary key; dependent on other entity; round corners f. Week entity – no primary key
Degrees of relationships – number of participating entities 1. Binary (2)
a. one-to-one (1:1)
b. one-to-many (1:*)
c. many-to-many (*:*)
2. Ternary (3) 3. Quaternary (4) Using more tables is better for normalization, but it slows database performance because each table must be accessed to retrieve the data
Student doesn’t have to take a class
Student can take many classes
At least 2 students must take a class
Faculty may be on sabbatical leave
Faculty can teach many classes
Faculty can only teach 1 class at a specific time
Page 2
Cardinality is on the right; it describes maximum number of possible relationship occurrences for an entity participating in a given relationship type Participation is on the left; it determines whether all or only some entity occurrences participate in a relationship Multiplicity is the term for what happens between cardinality and participation; a constraint; it is the number (or range) of possible occurrences of an entity type that may relate to a single occurrence of an associated entity type through a particular relationship
Candidate Key - Minimal set of attributes that uniquely identifies each occurrence of an entity type
Primary Key - Candidate key selected to uniquely identify each occurrence of an entity type
Foreign Key – Candidate key that relates to a primary key in another entity
Composite Key - Candidate key that consists of two or more attributes
Rows in a table (relation) are called records or tuples
Columns in a table are called fields or attributes
Page 3
Enhanced ER Diagrams improves performance because tables can be merged and not all attributes are shared. Since blanks occupy space, separate tables are created – IS relationship (1:1), a super sub class
Generalization – superclass, common attributes; process of minimizing differences between entities by identifying their common characteristics
Specialization – subclass; process of maximizing differences between members of an entity by identifying their distinguishing characteristics
Constraint Scenarios - Optional or Mandatory, AND, OR
Relational Models are tables based on requirements and ER diagram Parent-Child relationship; add attribute of parent as Foreign Key Take entity – m:m; composite Primary Key
Student
Take
Class
S1
S1 C1
C1
S2
S1 C2
C2
S3
S1 C3
C3
S2 C1
C4
S2 C3
Each column is unique All values in a column are values of the same attribute Each row contains unique values
Page 4
Super Class - An entity type that includes one or more distinct subgroupings of its occurrences; may
contain overlapping or distinct subclasses; not all members of a superclass need be a member of a
subclass; 1:1 relationship with sub class
Sub Class - A distinct subgrouping of occurrences of an entity type; 1:1 relationship with superclass Super Subclass –IS relationship (always 1:1) – see below diagram
Page 5
CONSTRAINT SCENARIOS
X Y
A
MANDATORY, AND A(a1(PK), a2, x1, x2, y1, y2) MANDATORY, OR AX(a1, a2, a3, x1, x2) AY(a1, a2, ae, y1, y2) OPTIONAL, AND A(a1(PK), a2, a3) XY(x1, x2, y1, y2, a1(FK)) OPTIONAL, OR A(a1, a2, a3) X(a1(FK), x1, x2) Y(a1(FK), y1, y2)
Page 6
Connection Traps
Fan Trap - Where a model represents a relationship between entity types, but pathway between
certain entity occurrences is ambiguous.
At which branch office does staff number SG37 work?
Example of Fan Trap
Correction of Fan Trap
Page 7
Chasm Trap - Where a model suggests the existence of a relationship between entity types, but pathway
does not exist between certain entity occurrences.
At which branch office is property PA14 available?
Example of Chasm Trap
Correction of Chasm Trap
Page 8
Functional Dependency (FD) – Relationship that may/may not depend on columns; guards against wanted and unwanted data. Restrictions that depend only on the equality or inequality of values. These constraints do not depend upon what values a given tuple has in its components, but only on whether two tuples agree in certain components.
Given a relation R, a set of attributes X in R is said to functionally determine another attribute Y, also in R, (written X → Y) if and only if each X value is associated with precisely one Y value. Customarily we call X the determinant set and Y the dependent attribute. Thus, given a tuple and the values of the attributes in X, one can determine the corresponding value of the Y attribute. For the purposes of simplicity, given that X and Y are sets of attributes in R, X → Y denotes that X functionally determines each of the members of Y - in this case Y is known as the dependent set.
Example College students visiting one or more lectures in each of which they are assigned a teaching assistant (TA). Let's further assume that every student is in some semester and is identified by a unique integer ID.
StudentID Semester Lecture TA
1234 6 Numerical Methods John
1200 4 Numerical Methods Peter
1234 6 Visual Computing Thomas
1201 4 Numerical Methods Peter
1201 4 Physics II Simone
We notice that whenever two rows in this table feature the same StudentID, they also necessarily have the same Semester values. This basic fact can be expressed by a functional dependency:
StudentID → Semester.
Other nontrivial functional dependencies can be identified, for example:
{StudentID, Lecture} → TA {StudentID, Lecture} → {TA, Semester}
The latter expresses the fact that the set {StudentID, Lecture} is a superkey of the relation.
Primary Key is an entity type may have one or more possible candidate keys, one of which is selected to be the primary key
Candidate key is a minimal set of attributes that functionally determines all of the attributes in a relation, such that if you remove an attribute, the remaining attributes no longer functionally determine the whole relation; minimal superkey (does not contain another superkey); uniquely identifies individual occurrences of an entity type
Super key is a set of attributes that functionally determine all attributes in a relation
Page 9
Composite key is a candidate key that consists of two or more attributes Normalization – process of analyzing anomalies and separating the data for efficiency
Redundancy – unnecessary repetition
Update anomaly – inconsistent change in tables
Insertion anomaly – incomplete record
Deletion anomaly – lose data from related deletion Armstrong’s Axiom Theorums
1. Trivial – the right is a subset of the left; it cannot break the left-hand side; X → Y AB -> C A -> C B -> C
2. Transitivity – stops a relation from being in 3NF; If X → Y and Y → Z, then X → Z A –> B B -> C A -> C A -> B -> C
3. Augmentation – add on each side; no subtraction; If X → Y, then XZ → YZ AC -> BC
4. Union/Additivity If X → Y and X → Z, then X → YZ AB -> Z [B -> X] AX -> Z
5. Psuedotransitivity If X → Y and WY → Z, then XW → Z XW -> Z AB -> C [A -> W] [A->B] XA -> Z AA -> C is the same as A -> C
6. Decomposition If X → YZ, then X → Y and X → Z
7. Subset Property/Reflexivity If Y is a subset of X, then X → Y
Page 10
Minimum Cover – There are 3 steps that are performed to get to 3NF, which is the normalization level that is needed to guarantee no anomalies for insertion, deletion and data redundancy Step 1 – operate on the right-hand side ONLY (Theorum #4) A -> BC A -> B A -> C Step 2 – operate on the left-hand side ONLY (Theorum #5); you can reduce on the left without breaking AB -> C [A-> B] AA -> C A-> C Step 3 –3 parts: remove duplicates, transitivity and minimum number of tables to find candidate key First Normal Form (1NF) sets the very basic rules for an organized database.
Eliminate duplicative columns from the same table.
Create separate tables for each group of related data and identify each row with a unique column or set of columns (the primary key).
Second Normal Form (2NF) further addresses the concept of removing duplicative data. Break the nonkey attributes into two groups, those that are fully functionally dependent on the key and those
that are dependent on only part of the key. Take the attributes that are dependent on a part of the
key and put them into a separate relation along with the part of the key they depend on.
Meet all the requirements of the first normal form.
Remove subsets of data that apply to multiple rows of a table and place them in separate tables.
Create relationships between these new tables and their predecessors through the use of foreign keys.
Third Normal Form (3NF) goes one large step further of decomposition. There must be no
transitive dependencies.
Meet all the requirements of the second normal form.
Remove columns that are not dependent upon the primary key.
Page 11
SQL – Structured Query Language (not case sensitive; format free)
DDL – Data Definition Language (create tables and relationships, drop, alter)
DML – Data Manipulation Language (select (query), insert, delete, update)
Every query has the below prefixes. A query must match names exactly (i.e. column, table)
Select *
From [table]
OPTIONAL
Where [condition] – can be multiple conditions; conditions must not be separated with
commas – use AND and OR; Sum() and count() do not appear in where clause
Group By Field(s)
Order By Field(s)
Database Creation
Database Creation
Data Types
Database Integrity
Dropping a Table
Loading a Table with Data
The INSERT Command
The INSERT Command with Nulls
To create a database table, you will need to use Create Table command.
Syntax of Create command:
Create Table Table _name (
Field_name_1 Data Type,
Field_name_2 Data Type,
….
Field_name_N Data Type)
Page 12
Examples of constraints are primary key, foreign key, not null, null, unique and check. If you
want to define constraints during the creation of a table, you can use the following syntax:
Create Table Table _name (
Field_name_1 Data Type Constraint_Specifications ,
Field_name_2 Data Type Constraint_Specifications,
….
Field_name_N Data Type Constraint_Specifications)
SQL Data Types
CHAR(n)
DATE
DECIMAL(p,q)
INTEGER
SMALLINT (-32,768 – 32,767)
ORACLE SQL Data Types
CHAR(n), VARCHAR2(n),
LONG, AND NCHAR(n)
DATE
DECIMAL(p,q), NUMBER(p,q),
NUMBER(p), NUMBER(n)
INTEGER, SMALLINT
Large Object Data types:
BLOB, CLOB, BFILE, NCLOB
Using CHAR(n) is faster than using VARCHAR2(n) because the system does not have to
process as much. Oracle user VARCHAR2(n) to save space and use only the amount of
characters that are necessary.
CHAR can be used for numbers if a column is not using number in a math equation.
DECIMAL (7,2) would be a number like 2300.05
DECIMAL (3,2) would be a number like .05
Best Practice: type commands on notepad/word format first and then paste into Oracle
Page 13
Dropping a Table
To delete a database Table, use Drop Command:
Syntax: Drop Table table_name
Example: Drop Table Customer;
Loading a Table with Data
To insert a record in a database Table, use Insert command:
Syntax: INSERT INTO Table_name
VALUES (value_of_field1, value_of_field2, ……, value_of_fieldN);
Example: INSERT INTO PART
VALUES ('AX12','Iron',104,'HW','3',24.95);
You can use the INSERT Command to insert for values for a specific fields :
Syntax: INSERT INTO Table_name ( Field_name1, Field_name2,…, Field_nameN)
VALUES (value_of_field1, value_of_field2, ……, value_of_fieldN);
Example: INSERT INTO PART (PNUM, PART_DESCRIPTION, ITEM_CLASS)
VALUES (‘CA14’,’ Griddle’, ’HW’);
You can use the INSERT Command to insert a date value:
Using Default Format:
INSERT INTO ORDERS
VALUES ('12489','2-SEP-98','124');
Using User Selected Format:
INSERT INTO ORDERS
VALUES ('12489',TO_DATE(‘9/2/1998’,’mm/dd/yyyy’) ,'124');
Describing a Table
To display the structure of a specific table, use Describe command:
Syntax: Describe Table_Name
Example: Describe Customer;
Page 14
You can place constraints to limit the type of data that can go into a table. Such constraints can
be specified when the table when the table is first created via the CREATE TABLE statement,
or after the table is already created via the ALTER TABLE statement.
Common types of constraints include the following:
NOT NULL Constraint: Ensures that a column cannot have NULL value.
DEFAULT Constraint: Provides a default value for a column when none is specified.
UNIQUE Constraint: Ensures that all values in a column are different. A column that is
specified as a primary key must also be unique. At the same time, a column that's unique
may or may not be a primary key. In addition, multiple UNIQUE constraints can be
defined on a table.
CHECK Constraint: Makes sure that all values in a column satisfy certain criteria.
Primary Key Constraint: Used to uniquely identify a row in the table. When multiple
fields are used as a primary key, they are called a composite key.
Foreign Key Constraint: Used to ensure referential integrity of the data.
Example of Default Constraint:
CREATE TABLE Student (Student_ID integer Unique, Last_Name varchar (30), First_Name varchar (30), Score DEFAULT 80);
Example of Unique Constraint:
CREATE TABLE Customer (SID integer Unique, Last_Name varchar (30), First_Name varchar(30));
Example of Check Constraint:
CREATE TABLE Customer (SID integer CHECK (SID > 0), Last_Name varchar (30), First_Name varchar(30));