You are on page 1of 7

CINS 370 - Homework 5 -

Ch5

5.1. Define the following terms as they apply to the relational model of data: domain, attribute, n-
tuple, relation schema, relation state, degree of a relation, relational database schema, and relational
database state.

Answer: Domain: The range of values of an attribute. It has a logical definition and a data type or a
format defined for it.

Attribute: In a relation table, each column is called by its column name or attribute name.

N-tuple - In mathematics, an ordered set of n elements called its components or coordinates.

Relation schema: Represented by R(A1, A2, .., An) where R is called the name of the relation and (A1,
A2, .., An) are the attributes of this schema,

Relation State: A state of relations between a set of n-tuples.

Degree of a relation: It is the number of attributes in a relation schema.

Relational Database State: Represented as DB = {r1, r2, .., rn} where r1, r2, .., rn are different relation
states in the database. Each ri satisfies the integrity constraints specified by the IC.

Relational database schema: Represented as S = {R1, R2, .., Rn} where R1, R2, .., Rn are the relation
schemas. It also consists of a set of Integrity Constraints(IC).

5.2. Why are tuples in a relation not ordered?

Answer: A relation is defined as a set of tuples. Mathematically, elements of a set have no order among
them; hence, tuples in a relation do not have any particular order.

5.3. Why are duplicate tuples not allowed in a relation?

Answer: Because it violates the specifications of the relational integrity constraints, particularly the key
constraint which states that no two tuples can have the same values for their attributes at any relation
state of a database.
5.4. What is the difference between a key and a superkey?

KEY SUPERKEY
A single or combination of multiple fields that A combination of attributes that can be uniquely
retrieve data rows from the table according to used to identify a database record.
the requirement.
Defined in tables to access or sequence the A table might have many superkeys. Candidate
stored data quickly and smoothly and are used to keys are a special subset of superkeys that do not
create links between different tables. have any extraneous information in them.

5.5. Why do we designate one of the candidate keys of a relation to be the primary key?

Answer: A particular candidate key is called as a primary key over the other candidate keys. This primary
key is generally a single attribute or a smaller number of attributes. It becomes easier to deal with a
database when we can have a single distinct key for a particular relation instead of having more than
one key.

(When there is more than one key in a relation schema of a database, all keys are referred to as
candidate keys.)

5.6. Discuss the characteristics of relations that make them different from ordinary tables and files.

Answer: Ordering of tuples in a Relation -The tuples are not considered to be ordered, even though they
appear to be in the tabular form.

Ordering of attributes in a relation schema R and of values within each tuples - We consider the
attributes in R(A1, A2, .., An) and the values in t=<v1, v2, .., vn> to be ordered.

Values in a tuple - All values are considered to be indivisible (atomic). A special null value is used to
represent values that are unknown or inapplicable to certain tuples.

5.7. Discuss the various reasons that lead to the occurrence of NULL values in relations.

Answer: - The value of an attribute for a particular tuple is not known or not defined.

- It is known but does not apply to the tuple specifically.

- Values can be not available presently.

- It is possible to devise different codes for different meanings of NULL values.

5.8. Discuss the entity integrity and referential integrity constraints. Why each is considered
important?

Answer: Entity integrity constraint -


No primary key value can be NULL because the primary key value is used to identify individual tuples in a
relation. Having NULL values for the primary key implies that we cannot identify some tuples.

Referential integrity constraint –

Specified between two relations and is used to maintain the consistency among the tuples in the two
relations.

- It states that a tuple in one relation that refers to another relation must refer to an existing tuple in
that relation.

5.9. Define foreign key. What is this concept used for?

Answer: A primary key of one table that appears as an attribute in another table and acts to provide a
logical relationship between the two tables.

Pertaining to Figure 5.6 (page 162 from out class textbook or see below), write the SQL queries that

produce answers to the following:

a. Given only the “Research” department name, retrieve/display and display all the last names of

the employees who work in the “Research” department.

- SELECT Employee.Lname FROM Employee INNER JOIN Department ON Employees.Dno =

Department.Dnumber AND Department.Dnumber = 5;

b. Retrieve/display all the employee’s first name whose supervisor’s salary is greater than 50,000.

- SELECT E1.Fname from Employee.E1, Employee.E2 where E1.SuperSSN = E2.SSN and

E2.Salary > 50000;

c. Retrieve/display the number of males (i.e. Sex = M) who work in Stafford (i.e. Dlocation =

Stafford).

- SELECT count(*) from Employee where Employee.sex = 'M' and Dno = (SELECT Dnumber from

Dept_Location where Dname = "Strafford");

Ch6

6.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?

Answer: Select: list of attribute names to be received by the query.

From: the tables that these attributes with be retrieved from.

Where: conditional Boolean expression to identify certain tuples to be retrieved (optional).


Order by: attribute list to order the result by (optional).

6.10. Specify the following queries in SQL on the COMPANY relational database schema shown in
Figure 5.5. Show the result of each query if it is applied to the COMPANY database in Figure 5.6.

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’.

Answer: In the relational algebra, as in other languages, it is possible to specify the same query in
multiple ways. We give one possible solution for each query. We use the symbol s for SELECT, P for
PROJECT, J for EQUIJOIN, * for NATURAL JOIN, and f for FUNCTION.

(a) EMP_W_X <-- ( s PNAME='ProductX' (PROJECT)) J (PNUMBER),(PNO)


(WORKS_ON)
EMP_WORK_10 <-- (EMPLOYEE) J (SSN),(ESSN) ( s HOURS>10 (EMP_W_X))
RESULT <-- P LNAME,FNAME ( s DNO=5 (EMP_WORK_10))

Result:
LNAME FNAME
Smith John
English Joyce

(b) E <-- (EMPLOYEE) J (SSN,FNAME),(ESSN,DEPENDENT_NAME) (DEPENDENT)


R <-- P LNAME,FNAME (E)

Result (empty):
LNAME FNAME

(c) WONG_SSN <-- P SSN ( s FNAME='Franklin' AND


LNAME='Wong' (EMPLOYEE))
WONG_EMPS <-- (EMPLOYEE) J (SUPERSSN),(SSN) (WONG_SSN)
RESULT <-- P LNAME,FNAME (WONG_EMPS)

Result:
LNAME FNAME
Smith John
Narayan Ramesh
English Joyce

6.13. Write SQL update statements to do the following on the database schema shown in Figure 1.2.
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.
Answer:
(a) Insert a new student <'Johnson', 25, 1, 'MATH'> in the database.
INSERT INTO STUDENT
VALUES ('Johnson', 25, 1, 'MATH')

(b) Change the class of student 'Smith' to 2.


UPDATE STUDENT
SET CLASS = 2
WHERE Name='Smith'

(c) Insert a new course <'Knowledge Engineering','COSC4390', 3,'COSC'>.


INSERT INTO COURSE
VALUES ('Knowledge Engineering','COSC4390', 3,'COSC')

(d) Delete the record for the student whose name is 'Smith' and student number is 17.
DELETE FROM STUDENT
WHERE Name='Smith' AND StudentNumber=17
Ch7

7.1. Describe the six clauses in the syntax of an SQL retrieval query. Show what type of constructs can
be specified in each of the six clauses. Which of the six clauses are required and which are optional?
Answer:
1. SELECT - column name
2. FROM - table or views
3. WHERE - conditions or predicates are met (OPTIONAL)
4. GROUP BY - subsets of rows (OPTIONAL)
5. HAVING - a common condition as a group (OPTIONAL)
6. ORDER BY - a sorting method (OPTIONAL)

7.3. 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?
Answer:
-Each individual NULL value considered to be different from every other NULL value

-The NULL values are discarded when aggregate functions are applied to a particular column
Aggregate function example:
FIRST() - Returns the first value
LAST() - Returns the last value
MAX() - Returns the largest value
MIN() - Returns the smallest value

-They are separated into a group created for all tuples with a NULL value in grouping attribute.

7.4. Discuss how each of the following constructs is used in SQL, and discuss the various options for
each construct. Specify what each construct is useful for.
a. Nested queries
b. Joined tables and outer joins
c. Aggregate functions and grouping
d. Triggers
e. Assertions and how they differ from triggers
f. The SQL WITH clause
g. SQL CASE construct
h. Views and their updatability
i. Schema change commands
Answer:
Nested queries: A query (SELECT statement) inside a query that can appear as part of a condition in the
WHERE or HAVING clauses as well as in the FROM clause.

Joined tables and outer joins: Permits users to specify a table resulting from a join operation in the
FROM clause of a query
Inner join
- Default type of join in a joined table
- Tuple is included in the result only if a matching tuple exists in the other relation
Outer join
- Every tuple in right/left table must appear in result

Aggregate functions and grouping


Aggregate functions - Used to summarize information from multiple tuples into a single-tuple summary
Grouping - Create subgroups of tuples before summarizing

Triggers: Triggers are used to specify certain actions that will be triggered when certain events occur
(such as before an insert or update). This creates "active database" behavior.

Assertions and how they differ from triggers: Conditions declared via an insertion and a check query.
This query must be satisfied for all database entries for the assertion to pass. This varies from triggers
because it is not explicitly tied to an event

The SQL WITH clause: A clause that can be added before a SELECT statement, to define aliases for
complicated expressions that are referenced multiple times within the body of the SELECT. Similar
to CREATE VIEW, except that the table and column names defined in the WITH clause do not persist
after the query finishes, and do not conflict with names used in actual tables or views. Also known
as "subquery factoring”.

SQL CASE construct: CASE is used to provide if-then-else type of logic to SQL. There are two formats:
The first is a Simple CASE expression, where we compare an expression to static values. The second is a
Searched CASE expression, where we compare an expression to one or more logical conditions.

Views and their updatability: A view is a single table that is derived from other tables in the database or
previously defined views. Because a views is a virtual table, its ability to be updated is limited.

Schema change commands: Used to alter a schema by adding or dropping tables or adding or deleting
columns in a table. Checks need to be done when altering schemas to ensure it does not become
inconsistent.

You might also like