You are on page 1of 5

Warsinee Apirakdacharchai ID 5080048 Assignment #2 Due Oct 17, 2011

1. Consider the instance of the Students relation shown in Figure 3.1.


Sid 50000 53666 53688 53650 53831 53832 Dave Jones Smith Smith Madayan Guldu Name Login dave@cs jones@cs smith@ee smith@math madayan@music guldu@music 19 18 18 19 11 12 Age 3.3 3.4 3.2 3.8 1.8 2.0 gpa

1.1 Give an example of an attribute (or set of attributes) that you can deduce is not a candidate key, based on this instance being legal. - Name and Age. 1.2 Is there any example of an attribute (or set of attributes) that you can deduce is a candidate key, based on this instance being legal? - A key of a relation cannot be determined by only one instance of the relation. But in this case the candidate key could be sid and login. 2. What is the difference between a candidate key and the primary key for a given relation? What is a superkey? -Candidate key is the filed that is unique, one of these will be chosen to be a primary key. Superkey is a filed that when combine with the primary key, it will be unique. For example,unique(Login), primary key(sid), superkey(sid, name) 3. What is a foreign key constraint? Why are such constraints important? What is referential integrity? -Foreign key is a set of fields in one relation that is used to 'refer' to a tuple in another relation. It must link to the primary key of a second relation. -The foreign key constraint is used to prevent actions that would destroy links between tables. -The foreign key constraint also prevents that invalid data form being inserted into the foreign key column, because it has to be one of the values contained in the table it points to. 4. Consider the conceptual schema of Students, Faculty, Courses, Rooms, Enrolled, Teaches, and Meets In below: Students(sid:string, name: string, login: string, age: integer, gpa: real) Faculty(_d: string, fname: string, sal: real) Courses(cid: string, cname: string, credits: integer) Rooms(rno: integer, address: string, capacity: integer) Enrolled(sid: string, cid: string, grade: string)

Teaches(_d: string, cid: string) Meets In(cid: string, rno: integer, time: string)

4.1 List all the foreign key constraints among these relations. - FOREIGN KEY (sid ) REFERENCE Enrolled - FOREIGN KEY ( cid) REFERENCE Courses - FOREIGN KEY (cid ) REFERENCE Meets In - FOREIGN KEY ( rno) REFERENCE Rooms - FOREIGN KEY (_d ) REFERENCE Faculty 4.2 Give an example of a (plausible) constraint involving one or more of these relations that is not a primary key or foreign key constraint. - cid reference Teaches 5. Answer each of the following questions briefly. The questions are based on the following relational schema: Emp(eid: integer, ename: string, age: integer, salary: real) Works(eid: integer, did: integer, pct time: integer) Dept(did: integer, dname: string, budget: real, managerid: integer) 5.1 Give an example of a foreign key constraint that involves the Dept relation. What are the options for enforcing this constraint when a user attempts to delete a Dept tuple? - FOREIGN KEY (did ) REFERENCE Dept - ON DELETE NO ACTION 5.2 Write the SQL statements required to create the above relations, including appropriate versions of all primary and foreign key integrity constraints. -CREATE TABLE Works (eid integer, did integer, pct time integer, PRIMARY KEY (eid,did) FOREIGN KEY (did) REFERENCE Dept, FOREIGN KEY (eid) REFERENCE Emps, ON DELETE NO ACTION ON UPDATE CASCADE) -CREATE TABLE Emp (eid integer, ename string, age integer, salary real PRIMARY KEY (eid)) -CREATE TABLE Dept ( did integer, dname string, budget real, managerid integer, PRIMARY KEY (did) FOREIGN KEY (managerid) REFERENCES Emp ON DELETE SET DEFAULT 5.3 Define the Dept relation in SQL so that every department is guaranteed to have a manager.

-CREATE TABLE Dept ( did integer, dname string, budget real, managerid integer, PRIMARY KEY (did) FOREIGN KEY (managerid) REFERENCES Emp ON DELETE SET DEFAULT 5.4 Write an SQL statement to add `John Doe' as an employee with eid = 101, age = 32 and salary = 15; 000. - INSERT INTO Emps (eid: integer, ename: string, age: integer, salary: real) VALUES (101, John, 32, 15000) 5.5 Write an SQL statement to give every employee a 10% raise. - UPDATE Emp E SET E.salary = E.salary * 1.10; 5.6 Write an SQL statement to delete the `Toy' department. Given the referential integrity constraints you chose for this schema, explain what happens when this statement is executed. -DELETE FROM Dept D WHERE D.dname = Toy When this statement is executed the Toy department is delete; additionally any tuple in Works that referenced the deleted tuple has its did value set to NULL.
6. Consider the SQL query whose answer is shown below:

Sid 53831 53832

Name Madayan Guldu

Login madayan@musi c guldu@music

Age 11 12 1.8 2.0

gpa

6.1 Modify this query so that only the login column is included in the answer. - SELECT login FROM Students S WHERE S.age < 13 6.2 If the clause WHERE S.gpa >= 2 is added to the original query, what is the set of tuples in the answer? - (53832, Guldu, guldu@music, 12, 2.0)

7. Consider the following scenario: A university database contains information about professors (identified by social security number, or SSN) and courses (identified by courseid). Professors teach courses; each of the following situations concerns the Teaches relationship set. For each situation, draw an ER diagram that describes it (assuming that no further constraints hold). 1. Professors can teach the same course in several semesters, and each offering must be recorded.

2. Professors can teach the same course in several semesters, and only the most recent such offering needs to be recorded. (Assume this condition applies in all subsequent questions.) 3. Every professor must teach some course. 4. Every professor teaches exactly one course (no more, no less). 5. Every professor teaches exactly one course (no more, no less), and every course must be taught by some professor. 6. Now suppose that certain courses can be taught by a team of professors jointly, but it is possible that no one professor in a team can teach the course. Model this situation, introducing additional entity sets and relationship sets if necessary. 7.1 Design an ER diagram for a university database. 7.2 Write SQL statements to create the corresponding relations and capture as many of the constraints as possible. If you cannot capture some constraints, explain why.

CREATE TABLE Professor( ssn CHAR(20), cid CHAR(10) NOT NULL, PRIMARY KEY (ssn) FOREIGN KEY (cid) REFERENCE Course) CREATE TABLE Course( cid CHAR(10), cname SRTING NOT NULL, PRIMARY KEY (cid) ON DELETE CASCADE)

CREATE TABLE Semester( sid CHAR(10), cid CHAR(10), PRIMARY KEY (sid), FORIEGN KEY (cid) REFERENCE Course, ON DELETE CASCADE) CREATE TABLE Individual( pname STRING, ssn CHAR(20), PRIMARY KEY (pname), FOREIGN KEY (ssn) REFERENCE Professor) CREATE TABLE Group( gid CHAR(10), gname STRING, PRIMARY KEY (gid))