COP4540 MidtermReview (2)

download COP4540 MidtermReview (2)

of 35

Transcript of COP4540 MidtermReview (2)

  • 8/8/2019 COP4540 MidtermReview (2)

    1/35

    COP4540 Database

    Management SystemMidterm Review

    Reviewed byRamakrishna

    Slides created by Fernando & edited by Ramakrishna

  • 8/8/2019 COP4540 MidtermReview (2)

    2/35

    AGENDA Ch1. Overview of DBMSs

    Ch2. Database Design Ch3. Relational Model

    Ch4. Relational Algebra

    Ch19. Normal Forms Ch5. SQL

  • 8/8/2019 COP4540 MidtermReview (2)

    3/35

    AGENDA Ch1. Overview of DBMSs

    Ch2. Database Design Ch3. Relational Model

    Ch4. Relational Algebra

    Ch19. Normal Forms Ch5. SQL

  • 8/8/2019 COP4540 MidtermReview (2)

    4/35

    CH1 EXERCISES 1.4. Explain the difference between external, internal, andconceptual schemas. How are these different schema layers

    related to the concepts oflogical and physical dataindependence?

    External schemas:

    Allow data access to be customized at the level of individual usersor groups of users using different VIEWSof the same conceptualschema.

    Views are not stored in DBMS but they generated on-demand.

    In the data model of DBMS.

    Conceptual (logical) schemas:

    Describes all the data in terms of the data model. In a relational

    DBMS, it describes all relations stored. While there are several views for a given database, there is exactly

    one conceptual schema to allusers.

    Internal (physical) schemas:

    Describes how the relations described in the conceptual schemaare actually stored on disk (or other physical media).

  • 8/8/2019 COP4540 MidtermReview (2)

    5/35

    CH1 EXERCISES Logical Data Independence

    Protection from changes in Logical Structure

    of Data (The Conceptual Schema) Provided by External Schema (Views)

    Physical Data Independence

    Protection from changes in Physical Structureof Data

    Provided by Conceptual Schema

  • 8/8/2019 COP4540 MidtermReview (2)

    6/35

    AGENDA Ch1. Overview of DBMSs

    Ch2. Database Design Ch3. Relational Model

    Ch4. Relational Algebra

    Ch19. Normal Forms Ch5. SQL

  • 8/8/2019 COP4540 MidtermReview (2)

    7/35

    CH2 EXERCISES 2.2. A university database contains information

    aboutprofessors (id. by SSN) and courses (id. by courseid). Professors

    teach courses; each of the following situations concerns the Teaches

    relationship set. For each situation, draw an ER diagram thatdescribes it (assuming no further constraints hold).

    1. Professors can teach the same course in several

    semesters, and each offering must be recorded.

  • 8/8/2019 COP4540 MidtermReview (2)

    8/35

    CH2 EXERCISES 2.2. CONT

    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 allsubsequent questions.)

  • 8/8/2019 COP4540 MidtermReview (2)

    9/35

    CH2 EXERCISES 2.2. CONT

    3. Every professor must teach some course.

  • 8/8/2019 COP4540 MidtermReview (2)

    10/35

    CH2 EXERCISES 2.2. CONT

    4. Every professor teaches exactly one course (no more, noless).

  • 8/8/2019 COP4540 MidtermReview (2)

    11/35

    CH2 EXERCISES 2.2. CONT

    5. Every professor teaches exactly one course (no more, no

    less), and every course must be taught by someprofessor.

  • 8/8/2019 COP4540 MidtermReview (2)

    12/35

    CH2 EXERCISES

    2.2. CONT

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

  • 8/8/2019 COP4540 MidtermReview (2)

    13/35

    CH2 EXERCISES 2.4

    A company database needs to store information about

    employees (identified byssn

    , withsalaryandpho

    neas attributes),departments (identified by dno, with dnameand budgetas attributes),

    and children of employees (with nameand ageas attributes).

    Employees workin departments; each department is managed byan

    employee; a child must be identified uniquely by namewhen the parent

    (who is an employee; assume that only one parent works for the

    company) is known. We are not interested in information about a childonce the parent leaves the company.

  • 8/8/2019 COP4540 MidtermReview (2)

    14/35

    CH2 EXERCISES

  • 8/8/2019 COP4540 MidtermReview (2)

    15/35

    AGENDA Ch1. Overview of DBMSs

    Ch2. Database Design

    Ch3. Relational Model & SQL

    Ch4. Relational Algebra

    Ch19. Normal Forms Ch5. SQL

  • 8/8/2019 COP4540 MidtermReview (2)

    16/35

    CH3 EXERCISES 3.2. How many distinct tuples are in a

    relation instance with cardinality 22?

    Since a relation is formally defined as a setof

    tuples, if the cardinality is 22 (i.e., there are 22

    tuples), there must be 22 distinct tuples.

  • 8/8/2019 COP4540 MidtermReview (2)

    17/35

    CH3 EXERCISES 3.4. What is the difference between a

    candidate key and the primary key for a

    given relation? What is a superkey?

    Theprimary keyis the key selected by the

    DBA from among the group ofcandidate

    keys, all of which uniquely identify a tuple. A

    superkeyis a set of attributes that contains a

    key.

  • 8/8/2019 COP4540 MidtermReview (2)

    18/35

    CH3 EXERCISES 3.8. Answer each of the following questions briefly. Thequestions are based on the following relational schema:

    Emp(eid: integer,ename: string,age: integer,salary: real)Works(eid: integer,did: integer,pcttime: integer)Dept(did: integer,dname: string,budget: real,managerid:

    integer)

    1. Give an example of a foreign key constraint that involves theDept relation. What are the options for enforcing this constraintwhen a user attempts to delete a Dept tuple?

    Consider the following example. It is natural to require that the didfield

    of Works should be a foreign key, and refer to Dept.CREATE TABLE Works ( eid INTEGER NOT NULL ,

    did INTEGER NOT NULL ,pcttime INTEGER,PRIMARY KEY (eid, did),FOREIGN KEY (did) REFERENCES Dept )

  • 8/8/2019 COP4540 MidtermReview (2)

    19/35

    CH3 EXERCISES OPTIONS for maintaining referential

    integrity

    ON DELETE {CASCADE, SET DEFAULT,

    SET NULL, NO ACTION}

    ON UPDATE {CASCADE, SET DEFAULT,SET NULL, NO ACTION}

  • 8/8/2019 COP4540 MidtermReview (2)

    20/35

    CH3 EXERCISES 3.8.

    2. Write the SQL statements required to create the preceding

    relations, including appropriate versions of allprimary and

    foreign key integrity constraints.CREATE TABLE Emp (

    eid INTEGER,

    ename CHAR(10),

    age INTEGER,

    salary REAL,

    PRIMARY KEY (eid)

    )

    CREATE TABLE Works (

    eid INTEGER,

    did INTEGER,

    pcttime INTEGER,

    PRIMARY KEY (eid, did),

    FOREIGN KEY (did)

    REFERENCES Dept,

    FOREIGN KEY (eid)

    REFERENCES Emp,

    ON DELETE CASCADE

    )

    CREATE TABLE Dept (

    did INTEGER,

    budget REAL,

    managerid INTEGER ,

    PRIMARY KEY (did),

    FOREIGN KEY (managerid)

    REFERENCES Emp,

    ON DELETE SET NULL

    )

  • 8/8/2019 COP4540 MidtermReview (2)

    21/35

    CH3 EXERCISES 3.8.

    3. Define the Dept relation in SQL so thatevery department is guaranteed to have a

    manager. CREATE TABLE Dept (

    did INTEGER,budget REAL,managerid INTEGER NOT NULL ,

    PRIMARY KEY (did),FOREIGN KEY (managerid) REFERENCES Emp)

  • 8/8/2019 COP4540 MidtermReview (2)

    22/35

    CH3 EXERCISES 3.8.

    4. Write an SQL statement to add John

    Doe as an employee with eid= 101,age = 32 and salary= 15, 000.

    INSERT INTO Emp (eid, ename, age, salary)

    VALUES (101, John Doe, 32, 15000)

  • 8/8/2019 COP4540 MidtermReview (2)

    23/35

    CH3 EXERCISES 3.8.

    5. Write an SQL statement to give every

    employee a 10 percent raise.

    UPDATE Emp E

    SET E.salary = E.salary * 1.10

  • 8/8/2019 COP4540 MidtermReview (2)

    24/35

    CH3 EXERCISES 3.8.

    6. Write an SQL statement to delete the Toy

    department. Given the referential integrityconstraints you chose for this schema,

    explain what happens when this statement is

    executed.

    DELETEFROM Dept D

    WHERE D.dname = Toy

  • 8/8/2019 COP4540 MidtermReview (2)

    25/35

    CH5 EXERCISES 5.2. Consider the following schema:Suppliers(sid: integer,sname: string,address: string)Parts(pid: integer,pname: string,color: string)Catalog(sid: integer,pid: integer,cost: real)

    The Catalog relation lists the prices charged forparts bySuppliers. Write the following queries in SQL:

    1. Find the pnames ofparts for which there is some supplier. SELECT DISTINCT P.pname

    FROM Parts P, Catalog CWHERE P.pid = C.pid

    5. Find the sids of suppliers who charge more for some part than

    the average cost of that part (averaged over all the supplierswho supply that part). SELECT DISTINCT C.sid

    FROM Catalog CWHERE C.cost >( SELECT AVG (C1.cost)

    FROM Catalog C1WHERE C1.pid = C.pid )

  • 8/8/2019 COP4540 MidtermReview (2)

    26/35

    CH5 EXERCISES8. Find the sids of Suppliers who supply a red parts and a greenpart

    SELECT DISTINCT C.sid

    FROM Catalog C, Parts P

    WHERE C.pid = P.pid AND P.color = Red

    INTERSECT

    SELECT DISTINCT C1.sid

    FROM Catalog C1, Parts P1

    WHERE C1.pid = P1.pid AND P1.color = Green

    9. Find the sids of Suppliers who supply a red parts or a green

    partSELECT DISTINCT C.sid FROM Catalog C, Parts P

    WHERE C.pid = P.pid AND P.color = Red

    UNION

    SELECT DISTINCT C1.sid FROM Catalog C1, Parts P1

    WHERE C1.pid = P1.pid AND P1.color = Green

  • 8/8/2019 COP4540 MidtermReview (2)

    27/35

    AGENDA Ch1. Overview of DBMSs

    Ch2. Database Design

    Ch3. Relational Model

    Ch4. RelationalAlgebra

    Ch19. Normal Forms Ch5. SQL

  • 8/8/2019 COP4540 MidtermReview (2)

    28/35

    CH4 EXERCISES 4.2. Given two relations R1 and R2, whereR1 contains N1

    tuples,R2 contains N2 tuples, and N2 >N1 >0, give the minand maxpossible sizes for the resulting relational algebraexpressions:(1)R1UR2, (2)R1R2, (3)R1R2, (4)R1R2, (5)a=5(R1), and (6)

    a(R1)

  • 8/8/2019 COP4540 MidtermReview (2)

    29/35

    CH4 EXERCISES 4.4. Consider the Supplier-Parts-Catalog schema from the

    previous question. State what the following queries compute:

    Find the Supplier names of the suppliers who supply a red part that costs less

    than 100 dollars.

    This Relational Algebra statement does not return anything

    Find the Supplier names of the suppliers who supply a red part that costs less

    than 100 dollars and a green part that costs less than 100 dollars.

  • 8/8/2019 COP4540 MidtermReview (2)

    30/35

    CH4 EXERCISES

    4.4. Consider the Supplier-Parts-Catalog schema from the

    previous question. State what the following queries compute:

    Find the Supplier ids of the suppliers who supply a red part that costs less than

    100 dollars and a green part that costs less than 100 dollars.

    Find the Supplier names of the suppliers who supply a red part that costs less

    than 100 dollars and a green part that costs less than 100 dollars.

  • 8/8/2019 COP4540 MidtermReview (2)

    31/35

    CH4 EXERCISES

    4.6. What is relational completeness? Ifa query language is relationally

    complete, can you write any desiredquery in that language?

    Relational completenessmeans that a querylanguage can express all the queries that can

    be expressed in relational algebra. It does notmean that the language can express anydesired query.

  • 8/8/2019 COP4540 MidtermReview (2)

    32/35

    AGENDA

    Ch1. Overview of DBMSs

    Ch2. Database Design

    Ch3. Relational Model

    Ch4. Relational Algebra

    Ch19. Normal Forms

    Ch5. SQL

  • 8/8/2019 COP4540 MidtermReview (2)

    33/35

    CH19EXERCISESA relation R is in third normal form if for

    every functional dependency of the form X

    A one of the following statements is true:

    A X that is, A is a trivial functionaldependency , or (1)

    X is a superkey, or (2)

    A is part of some key forR (3)

    A relation R is in BCNF if (1) or (2)

  • 8/8/2019 COP4540 MidtermReview (2)

    34/35

    CH19EXERCISES

    19.2. Consider a relation Rwith fiveattributesABCDE. You are given thefollowing dependencies:A B,BC E,

    and ED A.1. List all keys forR.

    CDE, ACD, BCD

    2. Is Rin 3NF?

    R is in 3NF because B, E and A are all parts of keys.3. Is Rin BCNF?

    R is not in BCNF because none of A, BC and EDcontain a key.

  • 8/8/2019 COP4540 MidtermReview (2)

    35/35

    GOOD LUCK!!

    RAMAKRISHNA

    [email protected]

    ECS 234