560 likes | 742 Views
CSS/416. Data Design and Retrieval Workshop 3. Database Management Systems. Terminology. Database management Systems. Relational model Based on 1970 paper by E.F. Codd User sees all data in tables Tables can be combined using “set” operations ( linked via columns )
E N D
CSS/416 Data Design and Retrieval Workshop 3
Database Management Systems Terminology CSS/416 Workshop 3
Database management Systems • Relational model • Based on 1970 paper by E.F. Codd • User sees all data in tables • Tables can be combined using “set” operations (linked via columns) • Metadata also stored in tables as above • Logical design separate from physical implementations CSS/416 Workshop 3
Database Management Systems • Functional Dependencies • A -> B; A determines B (A is Determinant) • Partial (PK = AB, B -> C) • CustID -> Customer Name CSS/416 Workshop 3
Database Management Systems • Keys • Derived from entity identifier • One or more attributes (more = composite key) • Uniquely determine a row • Functionally determine an entire row’s attributes CSS/416 Workshop 3
Database management systems Modification Anomalies (multiple themes) - Can’t insert fact that scuba diving cost $175 - Cant update swimming fee - Delete SID 100=>lose skiing data CSS/416 Workshop 3
Database Normalization • Entities should have common theme • Stages • First Normal form • Second • Third • Others (BCNF, 4th, 5th, DK) • Impact on referential integrity • Denormalize for performance (city, state, zip is classic example) CSS/416 Workshop 3
Database Normalization • 1st normal form – eliminate multi-valued attributes(repeating groups) • 2nd normal – no partial key functional dependencies • 3rd normal – eliminate transitive dependencies CSS/416 Workshop 3
Database Normal Forms Violates first normal – why? CSS/416 Workshop 3
Database Normalization 1st Normal satisfied Still violates 2nd normal– why? CSS/416 Workshop 3
Database Normalization 2nd OK but 3rd? CSS/416 Workshop 3
Database Normalization Satisfies 3rd normal form CSS/416 Workshop 3
Database Normalization • BCNF – Every determinant is a candidate key • Fourth – Multi-valued dependencies • Fifth – Can recombine relations CSS/416 Workshop 3
Database Normalization • Domain Key (DK/NF) • Provably free from anomalies • But no one way to generate this form • All constraints follow from domain definitions and keys CSS/416 Workshop 3
Database Normalization • Domain Key (DK/NF) • “The key, the whole key, and nothing but the key” CSS/416 Workshop 3
Database Design • Translating a model to the Database • Entities -> tables • Establish primary & foreign keys • Many-to-many relations ->Junction table • Business rules -> triggers, constraints, etc. • Surrogate keys • Security • Typically done with a “CASE” tool CSS/416 Workshop 3
Database Design One-to-one Auto # Employee Has Auto 1 1 Emp # CSS/416 Workshop 3
Database Design One-to-many N 1 Mfgr Makes Equipment Mfgr # CSS/416 Workshop 3
Database Design One-to-many (w/ ID Dependency PK = inv#, item# N 1 Invoice Has Line Item Inv # CSS/416 Workshop 3
Database Design Many-to-many Mfgr_Eqpt Mfg # Eqpt ID Equipment Mfgr M N CSS/416 Workshop 3
Database Design Recursive 1 Member Referred by 1 Member # CSS/416 Workshop 3
Database Design IS-A relationship (Subscriptions) Member Member# Print Online CSS/416 Workshop 3
Database Design • Learning teams to implement sample relations for above E-R relationships: • 1 to 1 • 1 to many • Many to many • 1 to many w/ ID Dependency • Recursive • IS-A CSS/416 Workshop 3
SQL • Two main language components • Data definition language (DDL) • Data manipulation language (DML) CSS/416 Workshop 3
SQL • Data definition language (DDL) • Create, alter, drop, etc. • Frequently implemented via various CASE tools: Visio, Embarcadero, ERWin, etc. • But – also very useful for database administration CSS/416 Workshop 3
SQL Create Statement Create table patient (name varchar2(35) not null, age smallint, gender varchar2(1), account_number integer not null, primary key account_number) <..additional constraints such as foreign keys, etc. ..> CSS/416 Workshop 3
SQL Select (Projection) Select plantname from equipment Select (Restriction) Select * from equipment where eqpt_name = ‘feed heater’ CSS/416 Workshop 3
SQL Select (Selection + projection) Select eqpt_name from equipment where plantname = “styrene” Sorting (Ordering) Select * from manufacturers order by eqpt_mfgr CSS/416 Workshop 3
SQL • There are many built in functions • Some are called aggregate functions • Count • Max • Avg • Etc. Example Select max(age) from dependents CSS/416 Workshop 3
SQL Joins (old syntax) Select m.mfgr_addr from manufacturers m, equipment e where m.eqpt_mfgr = e.eqpt_mfgr and e.plantname = ‘styrene’ CSS/416 Workshop 3
SQL Joins (new syntax) Select m.mfgr_addr from manufacturers m inner join equipment e On e.name = m.name where e.plantname = ‘styrene’ CSS/416 Workshop 3
SQL Subqueries Select m.mfg_addr from manufacturers m where eqpt_mfgr in (select e.eqpt_name from equipment e) CSS/416 Workshop 3
SQL Insert Insert into equipment values(‘propylene’, ‘feed heater’, ‘ABC Exchanger’) Update Update manufacturers Set address = ‘18 Front Road’ Where mfgr_name = ‘ABC Exchanger’ CSS/416 Workshop 3
SQL Delete Delete from equipment Where plant_name = ‘styrene’ And eqpt_name = ‘feed cooler’ How many rows could this delete? CSS/416 Workshop 3
SQL Exercise • Create the tables in the design exercise • Create a few sample SQL selects, etc. CSS/416 Workshop 3
Functions of a Database Application Page 238 Figure 10-1 © 2000 Prentice Hall
CRUD “the first function of a database application is to CRUD views” • Create • Read • Update • Delete Page 237 CSS/416 Workshop 3
Format or Materialize views “the second function of a database application; the appearance of the content” Page 238 CSS/416 Workshop 3
Other database functions • Enforce constraints • Provide for security and control • Execute business logic Page 238 CSS/416 Workshop 3
E-R Diagram Page 240 Figure 10-3b © 2000 Prentice Hall
Relational Design Page 240 Figure 10-3c © 2000 Prentice Hall
Relational Design(w/ Surrogate Keys) Page 240 Figure 10-3d © 2000 Prentice Hall
Relational Diagram Page 241 Figure 10-3e © 2000 Prentice Hall
View “A structured list of data items (attributes) from the entities or semantic objects defined in the data model” A view can be materialized or formatted as a form or report Page 242 CSS/416 Workshop 3
Recordset “the result of an SQL statement” Page 243 CSS/416 Workshop 3
CRUD actions on a view Read SELECT CUSTOMER.CustomerID, CUSTOMER.Name FROM CUSTOMER, WORK WHERE CUSTOMER.CustomerID = WORK.CustomerID Page 243 CSS/416 Workshop 3
CRUD actions on a view Create INSERT INTO CUSTOMER (CUSTOMER.Name, CUSTOMER.City) VALUES (NewCust.CUSTOMER.Name, NewCust.CUSTOMER.City) Page 244 CSS/416 Workshop 3
CRUD actions on a view Update INSERT INTO CUSTOMER (CUSTOMER.Name, CUSTOMER.City) VALUES (NewCust.CUSTOMER.Name, NewCust.CUSTOMER.City) Page 246 CSS/416 Workshop 3
CRUD actions on a view Delete Cascading deletions depends on relationship cardinality Page 247 Figure 10-6 © 2000 Prentice Hall
Form “a screen display used for data entry and edit” Forms should... • reflect the view structure • make data associations graphically clear • encourage appropriate action Page 248 CSS/416 Workshop 3