mode 5 SQL III Flashcards
What is multiplicity?
Multiplicity describes the relationship between tables/entities.
Types of multiplicity?
> MANY-TO-MANY
>owners to cars
>customers to stores
>fridges to healthinspectors
> MANY-TO-ONE
>workers to managers
>carModels to carMake
>fridges to restaurant
> ONE-TO-MANY
>managers to workers
>carMake to carModels
>restaurant to fridges
> ONE-TO-ONE
>currentGovener to state
>person to drivers license
>person to ssn
What is referential integrity?
The enforcing of foreign keys referencing valid and existing primary keys
Essentially, referential integrity protects us from having orphan records
What is an orphan record?
Because a record w/ a FK references a PK record, the record w/ a FK is DEPENDENT upon the PK record…
meaning the PK record is a parent record to the FK record (aka the child record)
So…if you delete the Parent record then the child record (FK) will no longer have a parent, it will be referred to as an orphan record. REFERENTIAL INTEGRITY protects us from accidentally creating any orphan records.
To delete the parent record, you must first delete the children records (also, you can look into “cascade on delete” OR you could set the child’s FK field to null temporarily)
What is a join?
It’s a way to combine two or more tables into a single result set based on some attributes (like a FK)
Types of Joins?
> Inner Join
Outer Join
>Left
>Right
>Full
Cartesean Join / Cross Join
(example: shirt size table cartesean join shirt graphics table)
Self Join: (when a table joins with itself)
(example: employee and managers that are also employees. so a reports_to attribute)
(example: person join person ON PPK=spouse_id)
Equijoin: (equijoins are when we use equalities “=”)
(example employee joins otherTable ON EPK = reports_to)
Thetajoin: (theta joins are when we use inequalities like >, Natural Join
N-way join
etc
What are set operators
set operators combine result sets into a single NEW result set
Types of Set Operators
Union: takes all the data entries in both result sets and puts them into a NEW result set, no duplicates
Union All: takes all the data entries in both result sets and puts them into a NEW result set, yes duplicates
Intersect: takes all the data entries that happen to appear in BOTH result sets and puts them into a NEW result set
Minus: takes all the data entries from the left result set and subtracts any data entries that happen to appear in the right table
Sample syntax for set operators
EXAMPLE SYNTAX
SELECT * FROM tableA WHRE attr1=’stuff’ AND attr3=10;
UNION
SELECT * FROM tableB WHRE attr2=5;
What are the requirements for set operators?
Requirements for the set operators:
- each result set must have the same number of columns AND
- each column must have the same datatypes [and order of the datatypes].
- The names of the columns don’t matter, they don’t have to match
What is normalization?
Normalization is the process of organizing your DB schema to reduce data redundancy and DB inconsistencies
Types of Normalization
1st Normal Form (1st NF)
- Each record must have a PK, relevant to the table
- Attributes should be atomic/granular
- –break down attributes into their smallest possible units
- –no “name” column, you’d do “firstname” and “lastname” columns
- Attributes must be singular
- –no “cust1”, “cust2”, “cust3”…they should be in their own table, then have a multiplicity relationship
> > > > > > > 2nd Normal Form (2nd NF)
- Must be in 1st NF
- All attributes MUST be dependant on the PK
- –to put it another way, attributes in the table should be properties of THIS record
- No parital dependencies
- –this deals with composite keys
- –example (Author, BookTitle) “Author Age”
> > > > > > > > > 3rd Normal Form (3rd NF)
- Must be in 2nd NF
- No transitive dependencies
What is a lookup table?
A lookup table is normally a table that acts as a “master list” for something and you use it to look up a business key value (like “Make”) in exachange for it’s identifier (like the id column) for use in some other table’s foreign key column.
What is enum?
Enumeration
What is a View?
A view is essentially a projection of 1 or more ACTUAL tables.
The view itself is just a virtual table, it doesn’t store data, it just references data from other tables.
In other words, a view stores a query so you can access massive queries easier