BR 3
BR 3
1. How do the relations (tables) in SQL differ from the relations defined formally? Discuss the other
differences in terminology. Why does SQL allow duplicate tuples in a table or in a query result?
2. List the data types that are allowed for SQL attributes.
3. How does SQL allow implementation of the entity integrity and referential integrity constraints? What
about referential triggered actions?
4. Describe the four clauses in the syntax of a simple SQL retrieval query. Show what type of constructs can
be specified in each of the clauses. Which are required and which are optional?
5. Discuss how NULLs are treated in comparison operators in SQL. How are NULLs treated when
aggregate functions are applied in an SQL query? How are NULLs treated if they exist in grouping
attributes?
Exercise
6. Consider the database schema is shown below. Make suitable assumptions about primary key. What are the
referential integrity constraints that should hold on the schema? Write appropriate SQL DDL statements to
define the database.
8. Write appropriate SQL DDL statements for declaring the LIBRARY relational database schema of shown
above. Specify the keys and referential triggered actions.
9. Specify the following queries in SQL on the COMPANY relational database schema.
a. Retrieve the names of all employees in department 5 who work more than 10 hours per week on the
ProductX project.
b. List the names of all employees who have a dependent with the same first name as themselves.
c. Find the names of all employees who are directly supervised by ‘Franklin Wong’.
a) Insert <‘Robert’, ‘F’, ‘Scott’, ‘943775543’, ‘1972-06-21’, ‘2365 Newcastle Rd, Bellaire, TX’, M, 58000,
‘888665555’, 1> into EMPLOYEE.
b) Insert <‘ProductA’, 4, ‘Bellaire’, 2> into PROJECT.
c) Insert <‘Production’, 4, ‘943775543’, ‘2007-10-01’> into DEPARTMENT.
d) Insert <‘677678989’, NULL, ‘40.0’> into WORKS_ON.
e) Insert <‘453453453’, ‘John’, ‘M’, ‘1990-12-12’, ‘spouse’> into DEPENDENT.
f) Delete the WORKS_ON tuples with Essn = ‘333445555’.
g) Delete the EMPLOYEE tuple with Ssn = ‘987654321’.
h) Delete the PROJECT tuple with Pname = ‘ProductX’.
i) Modify the Mgr_ssn and Mgr_start_date of the DEPARTMENT tuple with Dnumber = 5 to ‘123456789’
and ‘2007-10-01’, respectively.
j) Modify the Super_ssn attribute of the EMPLOYEE tuple with Ssn = ‘999887777’ to ‘943775543’.
k) Modify the Hours attribute of the WORKS_ON tuple with Essn = ‘999887777’ and Pno = 10 to ‘5.0’.
11. Specify the following queries in SQL on the database schema of question 6.
a. Retrieve the names of all senior students majoring in ‘CS’ (computer science).
b. Retrieve the names of all courses taught by Professor King in 2007 and 2008.
c. For each section taught by Professor King, retrieve the course number, semester, year, and number of
students who took the section.
d. Retrieve the name and transcript of each senior student (Class = 4) majoring in CS. A transcript includes
course name, course number, credit hours, semester, year, and grade for each course completed by the
student.
12. Write SQL update statements to do the following on the database schema of question 6.
a. Insert a new student, <‘Johnson’, 25, 1,‘Math’>, in the database.
b. Change the class of student ‘Smith’ to 2.
c. Insert a new course, <‘Knowledge Engineering’, ‘CS4390’, 3, ‘CS’>.
d. Delete the record for the student whose name is ‘Smith’ and whose student number is 17.
13. Specify the following views in SQL on the COMPANY database schema.
a. A view that has the department name, manager name, and manager salary for every department.
b. A view that has the employee name, supervisor name, and employee salary for each employee who works
in the ‘Research’ department.
c. A view that has the project name, controlling department name, number of employees, and total hours
worked per week on the project for each project.
d. A view that has the project name, controlling department name, number of employees, and total hours
worked per week on the project for each project with more than one employee working on it.