DBMS Module3
DBMS Module3
Step 7: For each n-ary relationship type R, where n>2, create a new relation S to represent R. Include the
primary keys of the relations participating in R as foreign keys in S. Simple attributes of R map to attributes of S.
The primary key of S is a combination of all the foreign keys that reference the participants that have cardinality
constraint > 1. For a recursive relationship, we will need a new relation.
SQL
The CREATE TABLE command is used to specify a new relation by giving it a name and specifying its
attributes and initial constraints.
The attributes are specified first, and each attribute is given a name, a data type to specify its domain of
values, and possibly attribute constraints, such as NOT NULL.
Sumarani H & Vanitha H N, Asst. Professor, Dept. of CSE,CBIT, Kolar 25
The key, entity integrity, and referential integrity constraints can be specified within the CREATE
TABLE statement after the attributes are declared, or they can be added later using the ALTER TABLE
command.
Eg: CREATE TABLE EMPLOYEE
Query 0
Retrieve the birthdate and address of the employee(s) whose name is ‘John B Smith’
SELECT BDATE, ADDRESS
FROM EMPLOYEE
WHERE FNAME = ‘John’ AND MINIT = ‘B’ AND LNAME = ‘Smith’;
Query 1
Retrieve the name and address of all employees who work for the ‘Research’ department
SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME = ‘Research’ AND DNUMBER = DNO;
Sumarani H & Vanitha H N, Asst. Professor, Dept. of CSE,CBIT, Kolar 29
Query 1A
Ambiguous attribute names
SELECT FNAME, EMPLOYEE.NAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
WHERE DEPARTMENT.NAME = ‘Research’ AND
DEPARTMENT.DNUMBER = EMPLOYEE.DNUMBER;
Query 1B
Aliasing
SELECT E.FNAME, E.NAME, E.ADDRESS
FROM EMPLOYEE E, DEPARTMENT D
WHERE D.NAME = ‘Research’ AND D.DNUMBER = E.DNUMBER;
Query 1C
Retrieve all the attribute values of EMPLOYEE tuples who work in department number 5
SELECT *
FROM EMPLOYEE
WHERE DNO = 5;
Query 1D
Retrieve all the attributes of an EMPLOYEE and the attributes of the DEPARTMENT he or she works in for
every employee of the ‘Research’ department
SELECT *
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME = ‘Research’ AND DNO = DNUMBER;
Query 2
For every project located in ‘Stafford’, list the project number, the controlling department number
and the department manager’s last name, address and birthdate
SELECT PNUMBER, DNUM, LNAME, ADDRESS, BDATE
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM = DNUMBER AND MGRSSN = SSN AND PLOCATION = ‘Stafford’;
Query 8
For each employee, retrieve the employee’s first and last name, and the first and last name of his or her
immediate supervisor.
SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME
FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.SUPERSSN = S.SSN;
Query 8A
Reformulation of query 8 to retrieve the last name of each employee and his or her supervisor, while
renaming the resulting attribute names as EMPLOYEE_NAME and SUPERVISOR_NAME
SELECT E.LNAME AS EMPLOYEE_NAME, S.LNAME AS SUPERVISOR_NAME
FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.SUPERSSN = S.SSN;
Sumarani H & Vanitha H N, Asst. Professor, Dept. of CSE,CBIT, Kolar 30
Query 9
Select all EMPLOYEE SSNs in the database
SELECT SSN
FROM EMPLOYEE;
Query 10
Select all combination of EMPLOYEE SSN and DEPARTMENT DNAME in the database
SELECT SSN, DNAME
FROM EMPLOYEE, DEPARTMENT;
Query 10A
Select the CROSS PRODUCT of the EMPLOYEE and DEPARTMENT relations
SELECT *
FROM EMPLOYEE, DEPARTMENT;
Query 11
Retrieve the salary of every employee
SELECT ALL SALARY
FROM EMPLOYEE;
Query 11A
Retrieve all distinct salary values
SELECT DISTINCT SALARY
FROM EMPLOYEE;
Query 12
Retrieve all employees whose address is in Houston, Texas
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE ADDRESS LIKE ‘%Houston,TX%’;
Query 12A
Find all employees who were born during the 1950s
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE BDATE LIKE ‘_ _ _ _ _ _ _ 70’;
Query 13
Show the resulting salaries if every employee working on the ‘ProductX’ project is given a 10 percent raise
SELECT FNAME, LNAME, 1.1*SALARY
FROM EMPLOYEE, WORKS_ON, PROJECT
WHERE SSN = ESSN AND PNO = PNUMBER AND PNAME = ‘ProductX’;
Query 14
Retrieve all employees in department 5 whose salary is between £30,000 and £40,000
Sumarani H & Vanitha H N, Asst. Professor, Dept. of CSE,CBIT, Kolar 31
SELECT *
FROM EMPLOYEE
WHERE (SALARY BETWEEN 30000 AND 40000) AND DNO = 5;
Query 15
Retrieve a list of employees and the projects they are working on, ordered by department and, within
each department, ordered alphabetically by last name, first name
SELECT DNAME, LNAME, FNAME, PNAME
FROM DEPARTMENT, EMPLOYEE,WORKS_ON, PROJECT
WHERE DNUMBER = DNO AND SSN = ESSN AND PNO = PNUMBER
ORDER BY DNAME DESC, LNAME ASC, FNAME ASC;