Sample test1

advertisement
Name:
TEST 2
CSCE 520– Fall 2004
Name:
Major
SSN (last 4 digits):
Answer the following questions. Be brief and precise, please. You have 1 hour 15 minutes to
finish the exam. Undergraduate students: answer only question for a total of 85! (That is, you
can drop any questions for a total of 15 points.)
1. 15 points
(5) What are referential integrity constraints?
Assert that a values appearing in one context also appears in another, related context. For
example, by design of the database, we expect that the value of an attribute A1 of a relation R1
also appear as the value of the attribute A2 in R2. Relationship between R1 and R2 “makes
sense.”
(5) Describe an example of referential integrity.
Given two relations: Owner(name, SSN, phone) and Dog(name, breed, owner), the “owner”
attribute of a dog is expected to be the same value as the “name” attribute of the Owner relation.
That is, the attribute “owner” refers to the name of the owner of the dog.
(5) Give a simple SQL “CREATE TABLE” statement with referential integrity.
CREATE TABLE Owner (
name CHAR (20) PRIMARY KEY,
SSN INT,
phone CHAR (20));
CREATE TABLE Dog (
name CHAR(20) PRIMARY KEY ,
breed CHAR(10),
owner CHAR(20) REFERECES Owner(name) );
2.
3. 15 points
(10) What is the difference between the SELECT operator () in relational algebra and the
SELECT keyword in SQL?
Both projects the selected attributes. The main difference is the  works on sets, and the output is
a set, while SQL SELECT is a bag. That is if the answer has more than one occurrences of the
same tuple,  will return only one of them, while SQL SELECT will return all. We can eliminate
the duplicates of the SQL SELECT by using the “SELECT DISTINCT” command.
(5) Assume that R(A,B) and S(B,C) are relations. Convert the following relational algebra
expression to a corresponding SQL statement: AB (C=3(R |X| S) (|X| is the natural join)
SELECT A, R.B
FROM R, S
WHERE R.B = S.B AND C= 3 ;
1
Name:
4. 30 points
Consider the following 4 tables:
CREATE TABLE Branch (
branchname CHAR(20),
assets REAL,
branchcity CHAR(20) );
CREATE TABLE Customer (
customername VARCHAR(20) PRIMARY KEY,
street CHAR(20),
customercity CHAR(20) );
CREATE TABLE Deposit (
branchname CHAR(20),
accountnumber REAL,
customername CHAR(20),
balance REAL,
PRIMARY KEY (branchname, accountnumber) );
CREATE TABLE Borrow (
branchname CHAR(20),
loannumber REAL,
customername CHAR(20),
amount REAL,
PRIMARY KEY (branchname, loannumber) );
Write the following SQL queries: Note, accounts are in Deposit table, loans are in Borrow table.
(10) Find all customers who have an account at some branch at which customer “Black” also has
an account.
select distinct d1.customername
from deposit d1, deposit d2
where d2.customername = 'Black' and d1.branchname = d2. branchname
and d1.customername != 'Black' ;
(10) Find the average balance of all depositors who live in “Spartanburg” and has at least 3
accounts.
SELECT D.customername, AVG(balance)
FROM Deposit D, Customer C
WHERE D.customername = C.customername AND
customercity = 'Spartanburg'
GROUP BY D.customername
having count(accountnumber) > 2 ;
2
Name:
(10) Find all customers of the branch “Best-Bank” of “Columbia” such that the customer has an
account at this branch but no loan at this branch.
SELECT C.customername
FROM Customer C
WHERE EXISTS
(SELECT *
FROM Deposit D1, Branch B1
WHERE D1.customername = C.customername AND
D1.branchname = B1.branchname AND
D1.branchname = 'Best-Bank' AND B1.branchcity = 'Columbia')
AND
NOT EXISTS
(SELECT *
FROM borrow w1, Branch B2
WHERE w1.customername = C.customername
AND w1.branchname = B2.branchname AND
w1.branchname = 'Best-Bank' AND
B2.branchcity = 'Columbia') ;
4. (15 points)
(5) What is a trigger?
Event-condition-action (ECA) rules
Event: typically a type of database modification
Condition: and SQL boolean-valued expression
Action: any SQL statement
Allows the user to specify when the check occurs.
General purpose conditions and sequence of SQL database modifications
(10) Consider the following syntax specification of the trigger statement. Explain its
components:
create [or replace] trigger trigger-name
Creates a new trigger with the name “trigger-name” or replaces the definition of anexisting
trigger with the name “trigger-name”
{before | after}
Defines whether the action should be executer before or after the triggering event
{delete | insert | update [of column [, column] …]}
[or
{delete | insert | update [of column [, column] …]}
]…
triggering event can be delete, insert, update, or their combination
ON table-name
Name of the table on which the trigger is defined
[ [referencing {old [as] <old> [new [as] <new>]
| new [as] < new > [old [as] < old >] } ]
Define names used to refer to the old and new values of the rows
for each row
Whether to execute the trigger only once (by omitting the “for each row”) or for every row that is
effected by the triggering event
[when (condition)] ]
Condition must be satisfied for the trigger action to be executed
PL/SQL Block
The action to be executed
3
Name:
6. (10 points)
(7) SQL allows attributes to have a special value NULL. However, it is desirable not to have
NULL values in a relation. Why? Give two examples of the interpretation of the null values.
Null values cannot be interpreted unambiguously. Several possible meanings may materialize in
Null values.
Missing value: there is a value but it is unknown, e.g., age of a dog
Inapplicable: the value is meaningless for that tuple, e.g., rank information of a nonshow dog.
Because neither the value of the attribute nor the reason for the null value is known, Null values
may created incorrect query results, insertions, or updates.
(3) Let x have the value NULL. What is the value of the following expressions?
x*10 NULL
x=15 unknown
x>y+4 unknown
4
Name:
----------------------------------------------------------------------------------------------------------(BONUS QUESTION)
5 points
Consider the following tables:
S
R
A
B
C
A
F
G
3
7
9
5
8
1
8
6
5
8
2
6
Show the semantics and the output of the following query:
SELECT *
FROM S, R
WHERE S.A=R.A AND S.B=R.G;
Create the Cartesian product of the 2 tables, leading to the schema S.A, B, C, R.A, F, G
Assign a tuple variable t to the Cartesian product table.
The tuple variable ranges over the tuples and evaluates the selection condition S.A=R.A AND
S.B=R.G
If the selection condition is TRUE, than the tuple is in the answer. Otherwise it is not.
5
Download