Professional Documents
Culture Documents
DBMS Fourth Chapter Part-1
DBMS Fourth Chapter Part-1
The relational
database Standard
Chapter4 (part-1): SQL-The relational database
Standard T1: Page 245-284
1. Numeric
2. Character
3. Bit-string
4. Boolean
5. Date
6. Time
7. Timestamp
8. Interval
• Attribute constraints
• Attributes defaults
• Key constraints
• Referential integrity constraints
• ON UPDATE CASCADE
• ON UPDATE SET DEFAULT
• ON UPDATE SET NULL
• The new attribute will have NULLs in all the tuples of the
relation right after the command is executed; hence, the
NOT NULL constraint is not allowed for such an attribute
Example:
• ALTER TABLE EMPLOYEE ADD JOB CHAR(12);
• In SQL, we can use the same name for two (or more)
attributes as long as the attributes are in different
relations
• A query that refers to two or more attributes with the
same name must qualify the attribute name with the
relation name by prefixing the relation name to the
attribute name
Example:
• EMPLOYEE.LNAME
• DEPARTMENT.DNAME
B.S.Shankar, Dept of MCA, VVIET 59
Ambiguous Attribute names, Aliasing, and
Tuple Variables
• Q1B
• SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE E, DEPARTMENT D
WHERE DNAME=’Research’ AND
E.DNUMBER = D.DNUMBER;
• SELECT SSN
FROM EMPLOYEE
Q1C: SELECT *
FROM EMPLOYEE
WHERE DNO=5;
Q1D: SELECT *
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME=’Research’ AND
DNO=DNUMBER;
B.S.Shankar, Dept of MCA, VVIET 68
Tables as Sets in SQL
• SELECT E.ENAME
FROM EMPLOYEE AS E
WHERE E.SSN IN
(SELECT ESSN
FROM DEPENDENT
WHERE E.GENDER = D.GENDER AND
E.NAME=DEP_NAME)
B.S.Shankar, Dept of MCA, VVIET 98
Single Block Query
• SELECT COUNT(*)
FROM EMPLOYEE;
• SELECT COUNT(*)
FROM EMPLOYEE, DEPARTMENT
WHERE DNO=DNUMBER AND DNAME=‘Research’;
• Query 28: For each department that has more than five
employees, retrieve the department number, and the
number of its employees who are earning more than 40000.
• SELECT DNUMBER, COUNT (*)
FROM EMPLOYEE, DEPARTMENT
WHERE DNUMBER = DNO AND SALARY>40000 AND DNO IN
(SELECT DNO
FROM EMPLOYEE
GROUP BY DNO
HAVING COUNT (*)>5)
GROUP BY DNUMBER;
U3A:
• CREATE TABLE DEPTS_INFO (DEPT_NAME VARCHAR(1O),
NO_OF_EMPS INTEGER, TOTAL_SAL INTEGER);
U3B:
• INSERT INTO DEPTSINFO (DEPT_NAME,
NO_OF_EMPS, TOTAL_SAL)
SELECT DNAME, COUNT (*), SUM (SALARY)
FROM DEPARTMENT, EMPLOYEE
WHERE DNUMBER=DNO
GROUP BY DNAME;
U4C:
• DELETE FROM EMPLOYEE
WHERE DNO IN
(SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME=’Research’)
U4D:
• DELETE FROM EMPLOYEE
CREATE ASSERTION
– components include: a constraint name, followed by
CHECK, followed by a condition
• SQL command:
• CREATE VIEW <view> <a possible list of attribute names >
< a query to specify the contents of the view>
• If attribute names are not specified, these will be same
as in the base relations)
• Select first name, last name of employee along with the
project name and number of hours on which they are
working on.
• UPDATE EMP_DEPT
SET DNO = 4
WHERE ENAME = ‘SMITH’;
• UPDATE WORKS_ON_NEW
SET PNAME = ‘ProductY’
WHERE LNAME = ‘Smith’
AND FNAME = ‘John’
AND PNAME = ‘ProductX’;
b) UPDATE PROJECT
SET PNAME = ‘ProductY’
WHERE PNAME = ‘ProductX’);
-------------not feasible
a) UPDATE WORKS_ON
SET PNO = (SELECT PNUMBER FROM PROJECT
WHERE PNAME = ‘ProductY’)
WHERE ESSN IN (SELECT SSN
FROM EMPLOYEE
WHERE LNAME = ‘Smith’
AND FNAME = ‘John’)
AND
PNO IN (SELECT PNUMBER FROM PROJECT WHERE
PNAME = ‘ProductX’);
B.S.Shankar, Dept of MCA, VVIET 164
End of Chapter 4
(part-1)