Dbms Aicte Lab
Dbms Aicte Lab
Dbms Aicte Lab
Lab Manual
INTRODUCTION TO SQL
DATA TYPES
Numeric: NUMBER, NUMBER(s,p), INTEGER, INT, FLOAT, DECIMAL
Consider following databases and draw ER diagram and convert entities and relationships
to relation table for a given scenario.
1. COLLEGE DATABASE:
STUDENT (USN, SName, Address, Phone, Gender)
SEMSEC (SSID, Sem, Sec)
CLASS (USN, SSID)
SUBJECT (Subcode, Title, Sem, Credits)
IAMARKS (USN, Subcode, SSID, Test1, Test2, Test3, FinalIA)
2. COMPANY DATABASE:
EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo)
DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)
DLOCATION (DNo,DLoc)
PROJECT (PNo, PName, PLocation, DNo)
WORKS_ON (SSN, PNo, Hours)
SOLUTION:
Mapping
entities and
relationships
to relation
table
(Schema
Diagram)
COMPANY DATABASE:
E-R Diagram
Schema Diagram
Experiment 2
Operation Purpose
Select(σ) The SELECT operation is used for selecting a subset of the tuples
according to a given selection condition
Projection(π) The projection eliminates all attributes of the input relation but those
mentioned in the projection list.
Set Difference(-) - Symbol denotes it. The result of A - B, is a relation which includes
all tuples that are in A but not in B.
Intersection(∩) Intersection defines a relation consisting of a set of all tuple that are
in both A and B.
Inner Join Inner join, includes only those tuples that satisfy the
matching criteria.
Theta Join(θ) The general case of JOIN operation is called a Theta join. It is
denoted by symbol θ.
EQUI Join When a theta join uses only equivalence condition, it becomes a equi
join.
Outer Join In an outer join, along with tuples that satisfy the matching criteria.
Left Outer Join( In the left outer join, operation allows keeping all tuple in the left
) relation.
Right Outer join( In the right outer join, operation allows keeping all tuple in the right
) relation.
Full Outer Join( ) In a full outer join, all tuples from both relations are included in the
result irrespective of the matching condition.
SOLUTION:
1. Creating a Database
CREATE DATABASE Company;
NOTE: Once DEPARTMENT and EMPLOYEE tables are created we must alter department
table to add foreign constraint MGRSSN using sql command
Update
COMMIT;
On execution of this command all changes to the database made by you are made
permanent and cannot be undone.
A COMMIT is automatically executed when you exit normally from
SQL*Plus. However, it does no harm to occasionally issue a COMMIT
command.
A COMMIT does not apply to any SELECT commands as there is nothing
to commit.
A COMMIT does not apply to any DDL commands (eg CREATE TABLE,
CREATE INDEX, etc). These are automatically committed and cannot be
rolled back.
If you wished to rollback (ie undo) any changes made to the database since
the last commit, you can issue the command:
ROLLBACK;
A group of related SQL commands that all have to complete successfully or otherwise be
rolled back, is called a transaction. Part of your research for Outcome 3 includes
investigating transaction processing and the implications of rollback and commit.
Experiment 4
SOLUTION:
Create Table
2. Add a new column PINCODE with not null constraints to the existing table DEPT
Table altered.
SQL> desc
dept
Name Null? Type
Table altered.
SQL> DESC DEPARTMENT;
Name Null? Type
6. Delete table
SQL> DROP TABLE DEPARTMENT;
Table dropped.
Experiment 5A
Consider Employee table
E101 45000
E102 70000
E103 120000
E105 67000
E106 145000
3. Retrieve average salary of all employee
SQL> select avg(salary) from employee;
AVG(SALARY)
89400
COUNT(*)
6. Retrieve total salary of employee group by employee name and count similar names
Mahesh 145000 1
Sunita 187000 2
Amit 115000 2
EMP_NAME SUM(SALARY)
mahesh 145000
sunita 187000
sunita
sunita
mahesh
Amit
Amit
9. Display details of employee whose name is AMIT and salary greater than 50000;
Table altered.
Note: update entries of employee table to fill missing fields SUPERSSN and DNO
UPDATE EMPLOYEE SET SUPERSSN=NULL, DNO=‘3‘ WHERE
SSN=‘RNSECE01‘;
UPDATE EMPLOYEE SET SUPERSSN=‘RNSCSE02‘, DNO=‘5‘ WHERE
SSN=‘RNSCSE01‘;
UPDATE EMPLOYEE SET SUPERSSN=‘RNSCSE03‘, DNO=‘5‘ WHERE
SSN=‘RNSCSE02‘;
UPDATE EMPLOYEE SET SUPERSSN=‘RNSCSE04‘, DNO=‘5‘ WHERE
SSN=‘RNSCSE03‘;
UPDATE EMPLOYEE SET DNO=‘5‘, SUPERSSN=‘RNSCSE05‘ WHERE
SSN=‘RNSCSE04‘; UPDATE EMPLOYEE SET DNO=‘5‘, SUPERSSN=‘RNSCSE06‘
WHERE SSN=‘RNSCSE05‘;
UPDATE EMPLOYEE SET DNO=‘5‘, SUPERSSN=NULL WHERE
SSN=‘RNSCSE06‘;
UPDATE EMPLOYEE SET DNO=‘1‘, SUPERSSN=‘RNSACC02‘ WHERE
SSN=‘RNSACC01‘;
UPDATE EMPLOYEE SET DNO=‘1‘, SUPERSSN=NULL WHERE
SSN=‘RNSACC02‘;
UPDATE EMPLOYEE SET DNO=‘4‘, SUPERSSN=NULL WHERE
SSN=‘RNSISE01‘;
UPDATE EMPLOYEE SET DNO=‘2‘, SUPERSSN=NULL WHERE
SSN=‘RNSIT01‘;
2. Find the sum of the salaries of all employees of the ‘Accounts’ department, as
well as the maximum salary, the minimum salary, and the average salary in this
department
SQL> SELECT SUM(E.SALARY),MAX(E.SALARY),MIN(E.SALARY),
AVG(E.SALARY)FROM EMPLOYEE1 E,DEPARTMENT D WHERE
E.DNO=D.DNUMBER AND D.DNAME='RESEARCH';
2 FROM EMPLOYEE1 E
3 WHERE EXISTS(SELECT DNO FROM EMPLOYEE1 WHERE E.DNO=5);
6. Retrieve the name of employees and their dept name (using JOIN)
Perform the String Functions, Date functions and Mathematical functions supported
by Oracle
ASCII('T')
116
ASCII('A')
97
ASCII('A')
65
ASCII('Z')
90
ASCII('Z')
122
SQL> SELECT UPPER('bldea sb arts and kcp science college') from dual;
UPPER('BLDEASBARTSANDKCPSCIENCECOLLEG
LOWER('WELCOMETODBM
LOWER('WELCOMETODB
welcome to dbmslab
REPLA
KELLO
REPLACE(
KOMPUTER
REPLA
HEAAO
TRIM('
--
NACOND
LTRIM('
NACONDA
LTR
---
NIL
RTRI
ANIT
RTRIM('
ANACOND
RTRIM('ANAC
ANACONDA
Date Functions
SQL> SELECT CURRENT_DATE FROM DUAL;
CURRENT_D
14-AUG-19
EXTRACT(YEARFROMSYSDATE)
2019
EXTRACT(DAYFROMSYSDATE)
14
EXTRACT(MONTHFROMSYSDATE)
SYSDATE
14-AUG-19
Mathematical Functions
SQL> select ABS(-100) from
dual; ABS(-100)
100
SQL> select ABS(-6) from dual;
ABS(-6)
6
SQL> select FLOOR(2345.78) FROM
DUAL; FLOOR(2345.78)
2345
SQL> SELECT GREATEST(23,67,90,123,78,50) FROM DUAL;
GREATEST(23,67,90,123,78,50)
123
LEAST(34,21,67,11,89,9)
10
9.94987437
SQL> SELECT POWER(2,4) FROM DUAL;
POWER(2,4)
16
1024
1024
DUAL; ROUND(5.86)
1002
1001
.893996664
.525321989
-6.4053312
1.33869021
-1
4.60517019
2.30258509
.5
DUAL; MOD(4,3)
1
SQL> SELECT MOD(4,2) FROM DUAL;
MOD(4,2)
0
SQL> SELECT EXP(2) FROM DUAL;
EXP(2)
7.3890561
EXP(-2)
.135335283
EXP(0)
1
Experiment 6
For a given EMPLOYEE tables
SOLUTION:
Creating View
The query that defines the sales_staffview references only rows in department 5.
Furthermore, the CHECK OPTION creates the view with the constraint (named
sales_staff_cnst) that INSERT and UPDATE statements issued against the view cannot
result in rows that the view cannot select.
3. Drop View
SOLUTION:
PL/SQL Block
SET SERVEROUTPUT ON SIZE 1000000;
DECLARE
n_times NUMBER := 10;
BEGIN
FOR n_i IN 1..n_times LOOP
DBMS_OUTPUT.PUT_LINE(n_i);
END LOOP;
END;
Output Table
Experiment 8
Given the table EMPLOYEE (EmpNo, Name, Salary, Designation, DeptID) write a cursor to
select the five highest paid employees from the table.
SOLUTION:
get e:/p8.sql;
1 declare
2 i number:=0;
3 cursor ec is select empno,name,salary from employee order by gross_salary
desc; 4 r ec%rowtype;
5 begin
6 open ec;
7 loop
8 exit when i=5;
9 fetch ec into r;
10 dbms_output.put_line(r.emp_no||' '||r.employee_name||' '||r.salary);
11 i:=i+1;
12 end loop;
13 close ec;
14* end;
15 .
SQL> /
1 rajesh 31000
2 paramesh 15000
3 pushpa 14000
4 vijaya 14000
5 keerthi 13000
Given an integer i, write a PL/SQL procedure to insert the tuple (i, 'xxx') into a given relation.
SOLUTION:
CREATE TABLE T2 (
a INTEGER,
b CHAR(10));
1. What is SQL?
Structured Query Language
2. What is database?
A database is a logically coherent collection of data with some inherent meaning,
representing some aspect of real world and which is designed, built and populated with
data for a specific purpose.
3. What is DBMS?
It is a collection of programs that enables user to create and maintain a database. In other
words it is general-purpose software that provides the users with the processes of
defining, constructing and manipulating the database for various applications.
4. What is a Database system?
The database and DBMS software together is called as Database system.
5. Advantages of DBMS?
Redundancy is controlled.
Unauthorized access is restricted.
Providing multiple user interfaces.
Enforcing integrity constraints.
Providing backup and recovery.
Extension - It is the number of tuples present in a table at any instance. This is time dependent.
Intension -It is a constant value that gives the name, structure of table and the constraints laid on
it.
10. What is Data Independence?
Data independence means that ―the application is independent of the storage structure and
access strategy of data‖. In other words, The ability to modify the schema definition in one level
should not affect the schema definition in the next higher level.
Two types of Data Independence:
Physical Data Independence: Modification in physical level should not affect the logical level.
Logical Data Independence: Modification in logical level should affect the view level.
A view may be thought of as a virtual table, that is, a table that does not really exist in its own
right but is instead derived from one or more underlying base table. In other words, there is no
stored file that direct represents the view instead a definition of view is stored in data dictionary.
Growth and restructuring of base tables is not reflected in views. Thus the view can insulate
users from the effects of restructuring and growth in the database. Hence accounts for logical
data independence.
12. What is Data Model?
A collection of conceptual tools for describing data, data relationships data semantics and
constraints.
13. What is E-R model?
This data model is based on real world that consists of basic objects called entities and of
relationship among these objects. Entities are described in a database by a set of attributes.
14. What is Object Oriented model?
This model is based on collection of objects. An object contains values stored in instance
variables within the object. An object also contains bodies of code that operate on the object.
These bodies of code are called methods. Objects that contain same types of values and the same
methods are grouped together into classes.
15. What is an Entity?
The collections of entities of a particular entity type are grouped together into an entity set.
19. What is an attribute?
A relation Schema denoted by R(A1, A2, …, An) is made up of the relation name R and the list
of attributes Ai that it contains. A relation is defined as a set of tuples. Let r be the relation
which contains set tuples (t1, t2, t3, ...,tn). Each tuple is an ordered list of n-values t=(v1,v2, ...,
vn).
21. What is degree of a Relation?
Relationship type defines a set of associations or a relationship set among a given set of entity
types.
25. What is degree of Relationship type?
It is the number of entity type participating.
26. What is DDL (Data Definition Language)?
A data base schema is specified by a set of definitions expressed by a special language called
DDL.
27. What is VDL (View Definition Language)?
This language is to specify the internal schema. This language may specify the mapping between
two schemas.
29. What is Data Storage - Definition Language?
The storage structures and access methods used by database system are specified by a set of
definition in a special type of DDL called data storage- definition language.
30. What is DML (Data Manipulation Language)?
This language that enable user to access or manipulate data as organized by appropriate data
model.
Procedural DML or Low level: DML requires a user to specify what data are needed and how
to get those data.
Non-Procedural DML or High level: DML requires a user to specify what data are needed
without specifying how to get those data.
31. What is DML Compiler?
It translates DML statements in a query language into low-level instruction that the query
evaluation engine can understand.
32. What is Relational Algebra?
It is a procedural query language. It consists of a set of operations that take one or two relations
as input and produce a new relation.
33. What is Relational Calculus?
It is an applied predicate calculus specifically tailored for relational databases proposed by E.F.
Codd. E.g. of languages based on it are DSL, ALPHA, QUEL.
34. What is normalization?
It is a process of analyzing the given relation schemas based on their Functional Dependencies
(FDs) and primary key to achieve the properties
Minimizing redundancy
Minimizing insertion, deletion and update anomalies.
35. What is Functional Dependency?
A Functional dependency is denoted by X Y between two sets of attributes X and Y that are
subsets of R specifies a constraint on the possible tuple that can form a relation state r of R.
The constraint is for any two tuples t1 and t2 in r if t1[X] = t2[X] then they have t1[Y] = t2[Y].
This means the value of X component of a tuple uniquely determines the value of component
Y.
36. When is a functional dependency F said to be minimal?
Every dependency in F has a single attribute for its right hand side.
We cannot replace any dependency X A in F with a dependency Y A where Y is a proper
subset of X and still have a set of dependency that is equivalent to F.
We cannot remove any dependency from F and still have set of dependency that is equivalent
to F.
37. What is Multivalued dependency?
It guarantees that the spurious tuple generation does not occur with respect to relation schemas
after decomposition.
39. What is 1 NF (Normal Form)?
The domain of attribute must include only atomic (simple, indivisible) values.
40. What is Fully Functional dependency?
A relation schema R is in 3NF if it is in 2NF and for every FD X A either of the following is true
X is a Super-key of R.
A is a prime attribute of R.
In other words, if every non prime attribute is non-transitively dependent on primary key.
43. What is BCNF (Boyce-Codd Normal Form)?
A relation schema R is in BCNF if it is in 3NF and satisfies additional constraints that for every
FD X A, X must be a candidate key.
44. What is 4NF?
A relation schema R is said to be in 4NF if for every Multivalued dependency X Y that holds
over R, one of following is true
X is subset or equal to (or) XY = R.
X is a super key.
45. What is 5NF?
A Relation schema R is said to be 5NF if for every join dependency {R1, R2, ...,Rn} that holds
R, one the following is true
Ri = R for some i.
The join dependency is implied by the set of FD, over R in which the left side is key of R.