Project Appendix A

Embed Size (px)

Citation preview

  • 8/17/2019 Project Appendix A

    1/16

    Project appendix a

    Appendix A----------------------------------------------------------------------------------------------------------------

    The Company Database SchemaThroughout the textbook you will study SQL statements that query and modify

    information stored in tables for the Company database. This is one of two databases that areused with this textbook the second being the Riverbend Hospital database described in!ppendix ".

    Database Schema

    #igure !.$ shows a diagram of the six tables that comprise the Company database. The tablenames are underlined in the figure for example employee and the column names are showninside the rectangles for example emp_ssn .#igure !.$

  • 8/17/2019 Project Appendix A

    2/16

    %elationships between the tables are shown with connecting lines. Consider the line connectingthe employee and dependent tables. #igure !.& shows you how to interpret the relationshipconnecting lines.

    #igure !.&

    %eading from the employee towards the dependent table the line has a circle and three'legged(crow)s foot* meaning this is an optional relationship +circle, such that each employee canoptionally +circle, ha-e no dependents. The three'legged crow)s foot means an employee canha-e one or more dependents. %eading from the dependent towards the employee table the lineis crossed by two short lines indicating that this relationship is mandatory in this direction suchthat each dependent can belong to one and only one employee a dependent cannot exist withoutan employee.#igure !.& shows two other symbol combinations. The mandatory relationship with one or moredependent table rows per employee row this would be the situation if the Company only hired

    employees who already had a dependent but this is not the case so this symbol was not used in#igure !.$. The other is the optional /ero or one employee per dependent and this is also not thecase so this symbol was not used in #igure !.$. 0owe-er you may find other relationships thatuse these symbol combinations.

    Table Definitions

    Department Table

  • 8/17/2019 Project Appendix A

    3/16

    The Company is organi/ed into separate departments. Tables !.$ and !.& describe the columnsin the department table and show the test data stored for each department. 1ach department isidentified by a unique department number + dpt_no , and has a department name. 1achdepartment has a manager and date on which the manager started managing the department. Themanager2s social security number + dpt_mgrssn , stored in the department table pro-ides a

    #3%1456 718 link to the specific employee that manages a department.Table !.$TABLE NAME: departmentColumn 6amedpt_nodpt9namedpt_mgrssndpt9mgr9start9date

    Table !.&TABLE DATA: department

    dpt_no:;$

    SQL 1xample !.$ gi-es the C%1!T1 T!"L1 command for the department table. Twoconstraints are named one for the department primary key and the other to ensure that eachdepartment row must ha-e a department name +63T 6 SQL 1xample !.$ >=C%1!T1 T!"L1 department + dpt9no 6

  • 8/17/2019 Project Appendix A

    4/16

    TABLE DATA: dept_locations

    SQL 1xample !.& shows the code to create dept9locations. !s you will learn from your study of Chapter & the constraint to create the primary key is a special form of the @%4?!%8 718clause that is used to create composite keys from two or more columns. There is also a#3%1456 718 clause to specify that dpt_no references the department column.

    => SQL 1xample !.& >=C%1!T1 T!"L1 dept9locations + dpt9no 6

  • 8/17/2019 Project Appendix A

    5/16

    GGGGGGFFFFFFEEEEEEHH::::&&&&&&$$$$$$;;;;;;HHHHHH

    emp_!ip

    G&B&F; E$

    G;$&$; EB; E$

    G;$&$GG&;E

    G&B&F

    1mployees work in departments. 1mployees also manage departments. Thus #igure !.$ showsthese two relationshipsI Manage-Dept and Works-Here . The Manage-Dept relationship linkseach row in the department table to the employee table row for the employee that manages thedepartment. The Works-Here relationship links the employee table to the department table forthe department to which each employee is assigned to work.8ou should interpret the Works-Here relationship as meaning that a department can ha-e /eroone or more than one assigned employees +crow)s foot, and that each employee is assigned toone and only one +mandatory, department. SQL 1xample !.; shows the C%1!T1 T!"L1command for the employee table. The Works-Here relationship is implemented by the

    #3%1456 718 constraint named fk_emp_dpt .The Manage-Dept relationship is interpreted as each department has one and only one managerand any gi-en employee may optionally manage a department. 3f course most employees willnot manage a department. 8ou will not find the #3%1456 718 constraint to enforce the

    Manage-Dept relationship in the C%1!T1 T!"L1 command for either the employee ordepartment tables. This is because a constraint cannot be created for a non'existent table and oneof the two tables has to be created firstJ 4nstead this constraint is added to the database after allof the tables are created.1mployees are super-ised by other employees. This is a unary or recursi-e relationship betweenrows within a single table and is shown as the Supervise relationship in #igure !.$. TheSupervise relationship is enforced by the #3%1456 718 constraint named fk_emp_superssn

    that links a gi-en employee to their super-isor thus the employee super-isor)s social securitynumber +emp_superssn , is stored as a column in the table. This relationship is co-ered in detailin Chapter G.

    => SQL 1xample !.; >=C%1!T1 T!"L1 employee + emp9ssn C0!%+ , C36ST%!46T pk9employee @%4?!%8 718

  • 8/17/2019 Project Appendix A

    6/16

    emp9last9name A!%C0!%&+&F, C36ST%!46T nn9emp9last9name 63T 6

  • 8/17/2019 Project Appendix A

    7/16

    ;B @ay "enefits ?arina ;

    SQL 1xample !.; gi-es the C%1!T1 T!"L1 command for the project table. The fk_pro_dept_number #3%1456 718 constraint implements the Controls-Proj relationship.=> SQL 1xample !.E >=

    C%1!T1 T!"L1 proMect + pro9number 6

  • 8/17/2019 Project Appendix A

    8/16

    SQL 1xample !.F gi-es the C%1!T1 T!"L1 command to create the assignment table. The@%4?!%8 718 clause implements the primary key in the same fashion as was done earlier forthe dept_locations table. Two #3%1456 718 constraints fk_"ork_emp and

    fk_"ork_pro_number implement the Works- n and Has-!mployee relationships by referencing back to the respecti-e employee and project tables. 8ou will learn about the 36 D1L1T1clauses when you study Chapter &.=> SQL 1xample !.F >=C%1!T1 T!"L1 assignment + work9emp9ssn C0!%+ , work9pro9number 6

  • 8/17/2019 Project Appendix A

    9/16

    dep_emp_ssnEEEEEEEEEEEEEEEEEEFFFFFF$$$$$$

    $$$$$$$$$$$$

    SQL 1xample !.G gi-es the C%1!T1 T!"L1 command for the dependent table. The@%4?!%8 718 constraint named pk_dependent enforces the composite primary key. The#3%1456 718 constraint named fk_dep_emp_ssn implements the 0as'Dependent relationship.=> SQL 1xample !.G >=C%1!T1 T!"L1 dependent + dep9emp9ssn C0!%+ , dep9name A!%C0!%&+FB, dep9gender C0!%+$,

    dep9date9of9birth D!T1 dep9relationship A!%C0!%&+$B,C36ST%!46T pk9dependent @%4?!%8 718 +dep9emp9ssn dep9name,C36ST%!46T fk9dep9emp9ssn

    #3%1456 718 +dep9emp9ssn, %1#1%16C1S employee 36 D1L1T1 C!SC!D1,

    E$"ipment Table

    1quipment is acquired from a local equipment rental company for proMects. Nhen proMects are

    completed the equipment is returned thus the Company has no need to maintain a stock ofequipment. ! proMect can ha-e /ero one or more items of equipment as reflected by the #ocated- $t relationship in #igure !.$. 1quipment items are identified by an equipment number +eqp9no,ha-e a description -alue and quantity on hand. The e%p_pro_number column in the table is a#3%1456 718 link to the project table for the proMect that is using the equipment. Table !.$;and !.$E describe the columns and data for the e%uipment table.work9pro9number 6

  • 8/17/2019 Project Appendix A

    10/16

    $&;EFG:H

    H:GE;&$

    => Create equipment table >=C%1!T1 T!"L1 equipment + eqp9no C0!%+E,C36ST%!46T pk9equipment @%4?!%8 718 eqp9description A!%C0!%&+$F, eqp9-alue 6

  • 8/17/2019 Project Appendix A

    11/16

    !LT1% T!"L1 department D%3@ C36ST%!46T fk9dept9empD%3@ T!"L1 dependentD%3@ T!"L1 assignment

    D%3@ T!"L1 employeeD%3@ table equipmentD%3@ T!"L1 proMectD%3@ table dept9locationsD%3@ table department

    %1? Create tables

    %1? Create department tableC%1!T1 T!"L1 department + dpt9no 6

  • 8/17/2019 Project Appendix A

    12/16

    emp9ssn C0!%+ , C36ST%!46T pk9employee @%4?!%8 718 emp9last9name A!%C0!%&+&F, C36ST%!46T nn9emp9last9name 63T 6

  • 8/17/2019 Project Appendix A

    13/16

    dep9gender C0!%+$, dep9date9of9birth D!T1 dep9relationship A!%C0!%&+$B,C36ST%!46T pk9dependent @%4?!%8 718 +dep9emp9ssn dep9name,C36ST%!46T fk9dep9emp9ssn

    #3%1456 718 +dep9emp9ssn, %1#1%16C1S employee 36 D1L1T1 C!SC!D1,

    %1? Create equipment tableC%1!T1 T!"L1 equipment + eqp9no C0!%+E,C36ST%!46T pk9equipment @%4?!%8 718 eqp9description A!%C0!%&+$F, eqp9-alue 6

  • 8/17/2019 Project Appendix A

    14/16

    46S1%T 46T3 proMect A!L

  • 8/17/2019 Project Appendix A

    15/16

    46S1%T 46T3 assignment A!L

  • 8/17/2019 Project Appendix A

    16/16