Lab 5
Lab 5
Based on the following (simplified) requirements of the COMPANY Database. We can draw an
Entity Relationship Diagram (ERD) and design a database schema.
Description of Company Database:
2. A constraint NOT NULL is specified when NULL is not permitted for a particular attribute.
*/
# Create 'EMPLOYEE' table
CREATE TABLE EMPLOYEE (
fname varchar(15) NOT NULL,
minit varchar(1),
lname varchar(15) NOT NULL,
ssn char(9),
bdate date,
address varchar(50),
sex char,
salary decimal(10,2),
superssn char(9),
dno int,
CONSTRAINT pk_employee PRIMARY KEY (ssn),
CONSTRAINT fk_employee_department FOREIGN KEY (dno) REFERENCES
DEPARTMENT(dnumber)
);
/* Notes:
Here, there are some foreign keys that may cause errors:
1. The foreign key Super_ssn in the EMPLOYEE table is a circular reference because it refers
to the table itself.
To deal with this problem, this constraint will be left out of the initial CREATE TABLE
statement, and then added later (after inserting all the records) using the ALTER TABLE.
ALTER TABLE EMPLOYEE
ADD CONSTRAINT fk_employee_employee FOREIGN KEY (superssn) REFERENCES
EMPLOYEE(ssn);
2. The foreign key Dno in the EMPLOYEE table refers to the DEPARTMENT table.
To deal with this point, the table EMPLOYEE table created after creating the
DEPARTMENT table to avoid referencing a table that has not yet been created. */
# Create 'DEPENDENT' table
CREATE TABLE DEPENDENT (
essn char(9),
dependent_name varchar(15),
sex char,
bdate date,
relationship varchar(8),
CONSTRAINT pk_dependent PRIMARY KEY (essn,dependent_name),
CONSTRAINT fk_dependent_employee FOREIGN KEY (essn) REFERENCES EMPLOYEE(ssn)
);