Sei sulla pagina 1di 3

CSC 540

Midterm Exam (3/23/17)

1. Tables are related to one another through


A. A foreign key
B. A projection
C. A primary key
D. All of the above

2. When would it be appropriate to define the primary key as a


combination of two fields?
A. When neither field is unique
B. When you expect to sort on both fields for one or more queries
C. When the first column of the table isnt a number field.
D. Never

3. Create a new schema in your PostgreSQL database called exam1.


Create the following tables in the PostgreSQL database. (Save the SQL
Commands used to create the tables or get it from PgAdmin if using the
wizard, that part will be graded)
Table: product

Column Domain
prod_id Integer (PK)
prod_desc VarChar(200)
cost Float
weight Float

Table: inventory

Column Domain
prod_id Integer (PK)
quantity Integer
location VarChar(10)

Table: orders

Column Domain
order_id Integer(PK)
prod_id Integer(PK)
quantity Integer
Table: order_contents

Column Domain
order_id Integer(PK)
cust_name VarChar(30)
cust_id Integer
cust_address VarChar(100)

4. After your tables have been created. Explain what the appropriate
foreign keys are and why. (You dont have to create them just explain
them)

5. Run the following scrip to insert rows into the product table. (The
deletes to clear the tables have been included in case you need to run
multiple times to fix errors)
delete from exam1.product;
INSERT INTO exam1.product (prod_id,prod_desc,cost,weight) VALUES
(1,'Baseball',4.99,0.5);
INSERT INTO exam1.product (prod_id,prod_desc,cost,weight) VALUES
(2,'Basketball',24.99,3);
INSERT INTO exam1.product (prod_id,prod_desc,cost,weight) VALUES
(3,'Softball',5.99,0.6);
INSERT INTO exam1.product (prod_id,prod_desc,cost,weight) VALUES (4,'Hockey
Puck',5.99,0.7);
INSERT INTO exam1.product (prod_id,prod_desc,cost,weight) VALUES (5,'Hockey
stick',99.99,3);
INSERT INTO exam1.product (prod_id,prod_desc,cost,weight) VALUES (6,'Soccer
Ball',29.99,2);
INSERT INTO exam1.product (prod_id,prod_desc,cost,weight) VALUES (7,'Tennis
Racquet',79.99,3);

delete from exam1.inventory;


INSERT INTO exam1.inventory values (1,100,'Bin A');
INSERT INTO exam1.inventory values (2,25,'Bin B');
INSERT INTO exam1.inventory values (3,20,'Bin A');
INSERT INTO exam1.inventory values (4,75,'Bin C');
INSERT INTO exam1.inventory values (5,20,'Window Shelf');
INSERT INTO exam1.inventory values (6,45,'Window Shelf');

delete from exam1.orders;


INSERT INTO exam1.orders values (100,'John Smith',1000,'123 Fake St.');
INSERT INTO exam1.orders values (101,'Rick Flair',1001,'1 Whitney St.');
INSERT INTO exam1.orders values (102,'Caitlyn Jenner',1002,'450 Hollywood Blvd.');
INSERT INTO exam1.orders values (103,'Donald Trump',1003,'1 Trump Ln.');
delete from exam1.order_contents;
INSERT INTO exam1.order_contents values (100,5,2);
INSERT INTO exam1.order_contents values (100,3,10);
INSERT INTO exam1.order_contents values (101,5,2);
INSERT INTO exam1.order_contents values (101,3,10);
INSERT INTO exam1.order_contents values (102,7,1);
INSERT INTO exam1.order_contents values (103,3,1);
INSERT INTO exam1.order_contents values (103,1,13);
INSERT INTO exam1.order_contents values (103,2,7);

6. Using SQL, find the total number of all items in the store. (Save the
SQL statement executed)

7. Write a query that returns the names of all items along with their
quantity including any items out of stock. An out of stock item could be
indicated by a row in the inventory table OR BY NO ROW FOR THAT ITEM IN
THE INVENTORY TABLE (Hint MySQL supports left and right joins)

8. Find the descriptions of all projects ordered by Donald Trump.

9. Find the total cost of all items purchased by Donald Trump. (Hint
Arithmetic operations can be performed on columns returned in the select
statement) Rounding is not necessary.

10. Write a SQL query that returns unfillable orders (by order_id) due to
stocking issues (no inventory to fill the order).

11. Create a safe statement to raise prices on products costing under 50


by 5%.

12. Explain how a transaction could be used safely insert an order and
update the level of inventory and why one should be used.

13. What is the difference between DDL and DML.

Potrebbero piacerti anche