0% found this document useful (0 votes)
7 views

DBMS Lab-4

The document outlines the creation of a SQL database named 'COMPANY' with several tables including EMPLOYEE, DEPARTMENT, and PROJECT. It includes SQL commands for creating tables, inserting data, and performing queries to retrieve specific information about employees and projects. The document also provides examples of SQL SELECT queries to extract data based on certain conditions.

Uploaded by

neerajrahul2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

DBMS Lab-4

The document outlines the creation of a SQL database named 'COMPANY' with several tables including EMPLOYEE, DEPARTMENT, and PROJECT. It includes SQL commands for creating tables, inserting data, and performing queries to retrieve specific information about employees and projects. The document also provides examples of SQL SELECT queries to extract data based on certain conditions.

Uploaded by

neerajrahul2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

DBMS

Neeraj Rahul
23bcs085
1. Perform the following tasks using SQL
commands:

Aim: Create a database ‘COMPANY’ which will have a set of relations, namely,
EMPLOYEE, DEPARTMENT, DEPT_LOCATIONS, PROJECT, WORKS_ON,
DEPENDENT.

See the following figure to understand the conceptual view of relations and relationship
in the COMPANY database.

he underlined attributes represent primary keys. There is one more figure added in the
next page that gives you information that needs to be filled in each relation.
Exercises:
1. Write a SELECT query to get the birth date and address of the employee(s) whose
name is ‘John B. Smith’.
2. Write a SELECT query to retrieve the name and address of all employees who work
for the ‘Research’ department.
3. For every project located in ‘Stafford’, list the project number, the controlling
department number, and the department manager’s last name, address, and birth date.
4. For each employee, retrieve the employee’s first and last name.

ANSWERS
CREATE DATABASE COMPANY;
USE COMPANY;

CREATE TABLE EMPLOYEE (


Fname VARCHAR(20),
Minit CHAR(1),
Lname VARCHAR(20),
Ssn CHAR(9) PRIMARY KEY,
Bdate DATE,
Address VARCHAR(50),
Sex CHAR(1),
Salary DECIMAL(10, 2),
Super_ssn CHAR(9),
Dno INT
);

CREATE TABLE DEPARTMENT (


Dname VARCHAR(20),
Dnumber INT PRIMARY KEY,
Mgr_ssn CHAR(9),
Mgr_start_date DATE
);

CREATE TABLE DEPT_LOCATIONS (


Dnumber INT,
Dlocation VARCHAR(20),
PRIMARY KEY (Dnumber, Dlocation)
);

CREATE TABLE PROJECT (


Pname VARCHAR(20),
Pnumber INT PRIMARY KEY,
Plocation VARCHAR(20),
Dnum INT
);

CREATE TABLE WORKS_ON (


Essn CHAR(9),
Pno INT,
Hours DECIMAL(5, 2),
PRIMARY KEY (Essn, Pno)
);

CREATE TABLE DEPENDENT (


Essn CHAR(9),
Dependent_name VARCHAR(20),
Sex CHAR(1),
Bdate DATE,
Relationship VARCHAR(20)
);

INSERT INTO EMPLOYEE VALUES


('John', 'B', 'Smith', '123456789', '1965-01-09', '731 Fondren, Houston, TX', 'M', 30000,
'333445555', 5),
('Franklin', 'T', 'Wong', '333445555', '1965-12-08', '638 Voss, Houston, TX', 'M', 40000,
'888665555', 5),
('Alicia', 'J', 'Zelaya', '999887777', '1968-01-19', '3321 Castle, Spring, TX', 'F', 25000,
'987654321', 4),
('Jennifer', 'S', 'Wallace', '987654321', '1941-06-20', '291 Berry, Bellaire, TX', 'F', 43000,
'888665555', 4),
('Ramesh', 'K', 'Narayan', '666884444', '1962-09-15', '975 Fire Oak, Humble, TX', 'M',
38000, '333445555', 5),
('Joyce', 'A', 'English', '453453453', '1972-07-31', '5631 Rice, Houston, TX', 'F', 25000,
'333445555', 5),
('Ahmad', 'V', 'Jabbar', '987987987', '1969-03-29', '980 Dallas, Houston, TX', 'M', 25000,
'987654321', 4),
('James', 'E', 'Borg', '888665555', '1937-11-10', '450 Stone, Houston, TX', 'M', 55000,
NULL, 1);

INSERT INTO DEPARTMENT VALUES


('Research', 5, '333445555', '1988-05-22'),
('Administration', 4, '987654321', '1995-01-01'),
('Headquarters', 1, '888665555', '1981-06-19');

INSERT INTO DEPT_LOCATIONS VALUES


(1, 'Houston'),
(4, 'Stafford'),
(5, 'Bellaire'),
(5, 'Sugarland'),
(5, 'Houston');

INSERT INTO PROJECT VALUES


('ProductX', 1, 'Bellaire', 5),
('ProductY', 2, 'Sugarland', 5),
('ProductZ', 3, 'Houston', 5),
('Computerization', 10, 'Stafford', 4),
('Reorganization', 20, 'Houston', 1),
('Newbenefits', 30, 'Stafford', 4);

INSERT INTO WORKS_ON VALUES


('123456789', 1, 32.5),
('123456789', 2, 7.5),
('666884444', 3, 40.0),
('453453453', 10, 20.0),
('453453453', 30, 20.0),
('333445555', 10, 10.0),
('333445555', 30, 10.0),
('987654321', 30, 5.0),
('987987987', 30, 15.0),
('999887777', 10, 10.0);

INSERT INTO DEPENDENT VALUES


('333445555', 'Alice', 'F', '1986-04-05', 'Daughter'),
('333445555', 'Theodore', 'M', '1983-10-25', 'Son'),
('333445555', 'Joy', 'F', '1958-05-03', 'Spouse'),
('987654321', 'Abner', 'M', '1942-02-28', 'Spouse'),
('123456789', 'Elizabeth', 'F', '1967-05-05', 'Spouse');

SELECT Bdate, Address


FROM EMPLOYEE
WHERE Fname = 'John' AND Minit = 'B' AND Lname = 'Smith';
SELECT E.Fname, E.Minit, E.Lname, E.Address
FROM EMPLOYEE E
JOIN DEPARTMENT D ON E.Dno = D.Dnumber
WHERE D.Dname = 'Research';

SELECT P.Pnumber, P.Dnum, E.Lname, E.Address, E.Bdate


FROM PROJECT P
JOIN DEPARTMENT D ON P.Dnum = D.Dnumber
JOIN EMPLOYEE E ON D.Mgr_ssn = E.Ssn
WHERE P.Plocation = 'Stafford';

SELECT Fname, Lname


FROM EMPLOYEE;

You might also like