Oracle midterm
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
516
On Slideshare
516
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
2
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. ILLINOIS INSTITUTE OF TECHNOLOGY TAKE-HOME MIDTERM ITM 421: INTRODUCTION TO DATABASES – DATA MODELING ROBERT HENDRY BY SEAN P. MCBRIDE CHICAGO, IL 11 MARCH 2011 AD MAIOREM DEI GLORIAM
  • 2. 1. Create two tables. Use a primary key constraint, foreign key constraint, and a check constraint. Make up the table and column names; just make sure that each of the two tables use VARCHAR2, NUMBER, and DATE data types. DROP TABLE PATRONAGE; DROP TABLE SAINT; DROP TABLE SPECIALIZATION; CREATE TABLE SAINT( SAINTID NUMBER, NAME VARCHAR2(50) NOT NULL, TITLE VARCHAR2(9) NOT NULL, FEASTDATE DATE, BIRTHDATE DATE, NATIONALITY VARCHAR2(25), DEATHDATE DATE, MARTYR VARCHAR2(1), CONSTRAINT PK_SAINT PRIMARY KEY(SAINTID), CONSTRAINT CHECK_TITLE CHECK (TITLE IN ('Venerable','Blessed','Saint')), CONSTRAINT CHECK_MARTYR CHECK (MARTYR IN ('Y','N')) ); CREATE TABLE SPECIALIZATION( SPECIALIZATIONID NUMBER, NAME VARCHAR2(25) NOT NULL, CONSTRAINT PK_SPECIALIZATION PRIMARY KEY(SPECIALIZATIONID) ); CREATE TABLE PATRONAGE( SAINTID NUMBER, SPECIALIZATIONID NUMBER, CONSTRAINT PK_SAINTID_SPECIALIZATIONID PRIMARY KEY(SAINTID,SPECIALIZATIONID), CONSTRAINT FK_SAINTID FOREIGN KEY (SAINTID) REFERENCES SAINT(SAINTID), CONSTRAINT FK_SPECIALIZATIONID FOREIGN KEY (SPECIALIZATIONID) REFERENCES SPECIALIZATION(SPECIALIZATIONID) );
  • 3. 2. Write SQL statements to INSERT, UPDATE, DELETE and SELECT for each of the tables you created in #1. INSERT INTO SAINT VALUES(1,'Jude Thaddeus','Saint',to_date('October 28', 'MONTH DD'),NULL,'Jewish',NULL,'Y'); INSERT INTO SPECIALIZATION VALUES(1,'Lost Causes'); INSERT INTO PATRONAGE VALUES(1,1); SELECT SAINT.NAME, SPECIALIZATION.NAME FROM SAINT, SPECIALIZATION, PATRONAGE WHERE SAINT.SAINTID=PATRONAGE.SAINTID AND SPECIALIZATION.SPECIALIZATIONID=PATRONAGE.SPECIALIZATIONID; INSERT INTO SAINT VALUES(2,'John Paul II','Venerable',NULL,to_date('October 16, 1978', 'MONTH DD, YYYY'),'Polish',to_date('April 2, 2005', 'MONTH DD, YYYY'),'N'); UPDATE SAINT SET TITLE = 'Blessed' WHERE SAINTID = 2; INSERT INTO SPECIALIZATION VALUES(2,'Poland'); INSERT INTO PATRONAGE VALUES(2,2); UPDATE SPECIALIZATION SET NAME = 'USSR' WHERE SPECIALIZATIONID = 2; INSERT INTO SPECIALIZATION VALUES(3,'Anti-Communism'); UPDATE PATRONAGE SET SPECIALIZATIONID = 3 WHERE SAINTID = 2; SELECT * FROM SAINT; SELECT * FROM SPECIALIZATION; SELECT * FROM PATRONAGE; DELETE FROM PATRONAGE WHERE SAINTID = 2; DELETE FROM SPECIALIZATION WHERE NAME = 'USSR'; DELETE FROM SAINT WHERE SAINTID = 2;
  • 4. 3. When creating tables and constraints, how do you implement referential integrity for a many-to-many relationship? Provide a short answer and a diagram. When one is implementing a many-to-many relationship between table A and table B, then one must relate tables A and B through a bridge/linker table (table C). Table C will have a foreign key relationship with the primary keys of both table A and table B. Often these two foreign keys also serve as a composite primary key for Table C. The Saints database used above is an example of this type of relationship. Saints can be the patrons of numerous things: illnesses, countries, occupations. Many saints are the patron saints of numerous things. E.G. Saint Martin of Tours is the patron saint of Soldiers, Beggars, and Alcoholics. Additionally, certain specializations have multiple patron saints. E.G. Soldiers can pray for intercession from Bessus, Martin of Tours, Adrian of Nicomedia, Sebastian, and Michael the Archangel (taken from the Wikipedia article on Patron Saints at http://en.wikipedia.org/wiki/Patron_saint). Because of these examples, saints and specializations form a many-to-many relationship. In order to facilitate these relationships, the database relates saints to specializations in the patronage table. The following diagram demonstrates this relationship: The Patronage table is actually a bridge/linker table that relates specific SaintIDs to specific SpecializationIDs through foreign key relationships with the Saint and Specialization tables. These two attributes also form a composite primary key for the Patronage table.
  • 5. 4. When writing a SQL SELECT from two different tables, do the tables in the SQL need to be joined by primary/foreign key(s)? Example: CREATE TABLE A( ID NUMBER, NAME VARCHAR2(10) PRIMARY KEY ); CREATE TABLE B( ID NUMBER, NAME VARCHAR2(10) PRIMARY KEY ); INSERT INTO A VALUES(1,'Book'); INSERT INTO B VALUES(1,'Person'); No, the tables do not need to be joined by primary/foreign keys in order to call a SQL SELECT statement on those two tables. First, a SELECT statement on two tables need not even demonstrate a relationship between those tables. A statement like (select * from A,B;) functions without showing any meaningful relationships between tables A and B. Second, each SQL SELECT statement creates ad-hoc relationships between tables by using the WHERE clause. A statement like (select a.name, b.name from A,B where a.id=b.id;) works fine without predefined foreign key relationships. In fact, predefined foreign key relationships do not impact SELECT functions. The main function of Foreign Key constraints is to enforce data integrity by constraining INSERT, UPDATE, and DELETE functions.
  • 6. 5. From the INVOICE table, write a SQL that will return a result set for all Amounts that are below average. This statement will return all rows with below-average amounts: SELECT CLIENTNAME, INVOICEDATE, AMOUNT, (SELECT AVG(AMOUNT) FROM INVOICE) AS AVERAGEAMOUNT FROM INVOICE WHERE AMOUNT < (SELECT AVG(AMOUNT) FROM INVOICE) ORDER BY AMOUNT; This statement will return all clients with a personal average amount less than the overall average amount. SELECT CLIENTNAME, AVG(AMOUNT) AS PERSONALAVERAGE, (SELECT AVG(AMOUNT) FROM INVOICE) AS OVERALLAVERAGE FROM INVOICE GROUP BY CLIENTNAME HAVING AVG(AMOUNT) < (SELECT AVG(AMOUNT) FROM INVOICE);