Upload
others
View
8
Download
0
Embed Size (px)
Citation preview
DHANALAKSHMI COLLEGE OF ENGINEERING
Tambaram, Chennai – 601 301
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
CS6312 – DATABASE MANAGEMENT SYSTEM LABORATORY
III SEMESTER - R 2013
LABORATORY MANUAL
Name :
Register No. :
Section :
1 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
DHANALAKSHMI COLLEGE OF ENGINEERING
VISION
Dhanalakshmi College of Engineering is committed to provide highly disciplined, conscientious and
enterprising professionals conforming to global standards through value based quality education and training.
MISSION
● To provide competent technical manpower capable of meeting requirements of the industry
● To contribute to the promotion of Academic Excellence in pursuit of Technical Education at different levels
● To train the students to sell his brawn and brain to the highest bidder but to never put a price tag on heart
and soul
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
VISION
To strive for acquiring, applying and imparting knowledge in Computer Science and Engineering through
quality education and to provide enthusiastic professionals with commitment
MISSION
● To educate the students with the state-of-art technologies to meet the growing challenges of the electronics
industry
● To carry out research through continuous interaction with research institutes and industry, on advances in
communication systems
● To provide the students with strong ground rules to facilitate them for systematic learning, innovation and
ethical practices
2 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
PROGRAMME EDUCATIONAL OBJECTIVES (PEOs)
1. Fundamentals
To impart students with fundamental knowledge in Mathematics, Science and fundamentals of engineering
that will would them to be successful professionals
2. Core Competence
To provide students with sound knowledge in engineering and experimental skills to identify complex
software problems in industry and to develop practical solution for them
3. Breadth
To provide relevant training and experience to bridge the gap between theory and practice this enables to
find solutions for real time problem in industry and organization and to design products requiring interdisciplinary
skills
4. Professionalism skills
To bestow students with adequate training and provide opportunities to work as team that will build up their
communication skills, individual leadership and supportive qualities and to develop them to adapt and work in
ever changing technologies
5. Lifelong Learning
To develop the ability of students to establish themselves as professionals in Computer Science and
Engineering and to create awareness about the need for lifelong learning and pursuing advanced degrees
3 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
PROGRAMME OUTCOMES (POs)
a) To apply basic knowledge of Mathematics, Science and Engineering fundamentals in Computer Science and
Engineering field
b) To design and conduct experiments as well as to analyze and interpret and apply the same in the career
c) To design and develop innovative and creative software applications
d) To understand a complex real world problems and develop an efficient practical solutions
e) To create, select and apply appropriate technique, resources, modern engineering and IT tools
f) To understand their roles as professionals and give the best to the society
g) To develop a system that will meet expected need with realistic constraints such as economic, environmental, social,
political, ethical, safe and sustainable
h) To communicate effectively and make others understand exactly what they are trying to convey in both verbal and
written forms
i) To work in a team as team member or a leader and make unique contributions and work with coordination
j) To engage lifelong learning and exhibit their technical skills
k) To develop and manage projects in multidisciplinary environments
4 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
CS6312 – DATABASE MANAGEMENT SYSTEMS LABORATORY
SYLLABUS
COURSE OBJECTIVES
1. Learn to create and use a database.
2. Be familiarized with a query language.
3. Have hands on experience on DDL Commands.
4. Have a good understanding of DML Commands and DCL commands.
5. Familiarize advanced SQL queries.
6. Be exposed to different applications.
LIST OF EXPERIMENTS:
1. Creation of a database and writing SQL queries to retrieve information from the database.
2. Performing Insertion, Deletion, Modifying, Altering, Updating and Viewing records.
3. Creation of Views, Synonyms, Sequence, Indexes, save point.
4. Creating an Employee database to set various constraints.
5. Creating relationship between the databases.
6. Study of PL/SQL block.
7. Write a PL/SQL block to satisfy some conditions by accepting input from the user.
8. Write a PL/SQL block that handles all types of exceptions.
9. Creation of Procedures.
10. Creation of database triggers and functions.
11. Mini project (Application Development using Oracle/ MySQL)
a) Inventory Control System
b) Material Requirement Processing
c) Hospital Management System
d) Railway Reservation System
e) Personal Information System
f) Web Based User Identification System
g) Timetable Management System
h) Hotel Management System
5 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
COURSE OUTCOMES
1. Apply data definition language.
2. Create table for real world scenarios.
3. Apply integrity constraints.
4. Manipulate the database using DML commands.
5. Frame Nested and Join Queries.
6. Develop a PL/SQL programs like procedures and functions.
7. Write a trigger.
8. Design and Develop a database.
9. Design a front end including forms and menus.
10. Develop a report using VB.
11. Design and implement a database schema for a given problem-domain.
12. Populate and query a database.
13. Create and maintain tables using PL/SQL.
14. Prepare reports.
6 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
CS6312 - DATABASE MANAGEMENT SYSTEMS LABORATORY
CONTENTS
Sl. No. Name of the Experiment Page No.
CYCLE 1 – EXPERIMENTS
1 DDL- Data Definition, Table Creation, Constraints 7
2 DML- Insert, Select Commands, Update & Delete Commands 10
3 TCL –Rollback, Commit commands 13
4 Writing and Practice of Simple Queries 15
5 Nested Queries & Queries Using Group By And Other Clauses 20
6 Problems Based On Views, Synonym, Sequence & Index 25
CYCLE 2 – EXPERIMENTS
7 Creating Database 29
8 High Level Programming Language Extension (PL/SQL Blocks) 34
9 PL/SQL Procedures 38
10 PL/SQL Functions 40
11 Triggers 44
12 Mini project – Payroll Processing System 49
CONTENT BEYOND SYLLABUS
13
SQL Join 52
7 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Expt. No.: 1 DDL COMMANDS
Aim: To create, alter, truncate and drop a table using DDL commands
Software requirements: Front end: VB/VC ++/JAVA or Equivalent
Back end: Oracle / SQL / MySQL/ PostGress / DB2 or Equivalent
Hardware requirements:
Pentium Dual Core 1.9Ghz and 2GB RAM
DDL commands:
OVERVIEW:
CREATE - to create objects in the database
ALTER - alters the structure of the objects in the database.
DROP - delete objects from the database.
TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed.
COMMENT - add comments to the data dictionary.
GRANT - gives user's access privileges to database.
REVOKE - withdraw access privileges given with the GRANT command.
SQL - CREATE TABLE
Syntax: CREATE TABLE tablename (column_name data_ type constraints, …)
INPUT:
SQL> CREATE TABLE DEPARTMENT ( DEPTID VARCHAR(5) CONSTRAINT PKEY PRIMARY KEY,
DEPTNAME VARCHAR(15), DOS DATE CONSTRAINT NOTNULL );
OUTPUT: Table created.
SQL> CREATE TABLE EMPLOYE ( EMPID VARCHAR(5) CONSTRAINT PKEY PRIMARY KEY, EMPNAME
VARCHAR(15), JOB CHAR(10) CONSTRAINT UNIQ1 UNIQUE, MGRID VARCHAR(5)
CONSTRAINT FKEY1 REFERENCES EMP (EMPID), HIREDATE DATE, DEPTID VARCHAR(5) CONSTRAINT
FKEY2 REFERENCES DEPT(DEPTID), SALARY NUMBER(7,2));
OUTPUT: Table created.
8 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
SQL - DESC TABLE
SQL>desc Employee;
SQL - TRUNCATE TABLE
SQL>ALTER TABLE EMPLOYE ADD CONSTRAINT NTNLL NOT NULL (SALARY);
OUTPUT: Table Altered.
Similarly, ALTER TABLE EMPLOYE DROP CONSTRAINT UNIQ1;
SQL - TRUNCATE TABLE
TRUNCATE TRUNCATE TABLE <TABLE NAME>;
Result:
Thus the table was successfully created, altered, truncated and dropped successfully.
Outcome:
Thus the outcome of applying Data Definition Language has been attained.
9 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Viva-voce
1. What is database?
2. What is DBMS?
3. What is a database system?
4. What are the advantages of DBMS?
5. What is the disadvantage in File Processing System?
6. Describe the three levels of data abstraction?
7. What is data independence?
8. What is an entity?
9. What is a key?
10. What are the categories of SQL command?
11. Define DDL.
10 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Expt. No.: 2 DML COMMANDS
Aim:
To insert, select, update and delete records in a table using DML commands
Software requirements:
Front end: VB/VC ++/JAVA or Equivalent
Back end: Oracle / SQL / MySQL/ PostGress / DB2 or Equivalent
Hardware requirements:
Pentium Dual Core 1.9Ghz and 2GB RAM
DML Commands:
COMMANDS OVERVIEW:
SELECT - retrieve data from the a database INSERT - insert data into a table
UPDATE - updates existing data within a table
DELETE - deletes all records from a table, the space for the records remain
SQL – SELECT FROM
Selecting all records from a table SELECT * FROM EMPLOYE
Listing the selected records which satisfies the given condition from a table
SELECT * FROM EMPLOYE WHERE DEPTID=’DPT01’
SQL - INSERT INTO
Syntax:
INSERT INTO tablename VALUES (value list)
Single-row insert
INSERT INTO DEPARTMENT VALUES (‘DPT02’,’TESTING’,’10-JAN-2012’)
Inserting one row, with values to the column in the given order
INSERT INTO DEPARTMENT (DEPTNAME, DEPTID) VALUES (‘DESIGN’, ‘DPT02’);
Inserting one or more rows with values from other tables.
INSERT INTO DEPARTMENT (DEPTID, DEPTNAME)
SELECT EMPID, EMPNAME FROM EMP WHERE MGRID=EMPID; OTHER
11 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
EXAMPLES:
INPUT:
SQL>INSERT INTO EMPLOYE VALUES (‘Emp121’,’Gayle’,’Testing’,’Emp01’,’01-Feb-2013’, ’Dpt01’, 32000);
OUTPUT: 1 row created.
INPUT:
SQL>INSERT INTO DEPARTMENT VALUES (‘&deptid,’&deptname’); Enter value for deptid: Dpt01
Enter value for deptname: Development
OUTPUT:
old 1: Insert into department values(‘&deptid,’&deptname’) new 1: Insert into department values(‘Dpt01’,'
Development’) 1 row created.
SQL - UPDATE
Syntax: UPDATE tablename SET COLUMN_NAME =value [ WHERE CONDITION]
INPUT::
SQL> UPDATE DEPT SET DOS=’01-JAN-2010’ WHERE DEPTID=’DPT01’;
1 row updated.
SQL - DELETE FROM
Syntax: DELETE FROM tablename WHERE condition
INPUT::
SQL> DELETE FROM DEPARTMENT;
OUTPUT: 1 row deleted.
Result:
Thus the table was successfully inserted, updated, select, and deleted.
Outcome:
Thus the outcome of applying DDL Commands has been attained.
12 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Viva-voce
1. What is the role of DML compiler in the database architecture?
2. Is it possible to delete a column using delete command?
3. Can update command can be used to change the structure of the table.
4. How to add a new constraint to a table using update command?
5. What is DML (Data Manipulation Language)?
6. What is DML Compiler?
7. Define TCL
8. Define Cursor
9. What are the privileges that can be granted on a table by a user to others?
10. What command is used to get back the privileges offered by the GRANT command?
11. What command is used to create a table by copying the structure of another table?
13 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Expt. No.: 3 TCL COMMANDS
Aim:
To create save point, commit and rollback while using a transaction
Software requirements:
Front end: VB/VC ++/JAVA or Equivalent
Back end: Oracle / SQL / MySQL/ PostGress / DB2 or Equivalent
Hardware requirements:
Pentium Dual Core 1.9Ghz and 2GB RAM
TCL commands:
COMMANDS OVERVIEW
COMMIT - save work done & it is visible to other users
SAVEPOINT - identify a point in a transaction to which you can later roll back ROLLBACK - restore database to
original since the last COMMIT
SET TRANSACTION - Change transaction options like what rollback segment to use
SAVEPOINT
Syntax: savepoint username;
INPUT:
SQL> savepoint emp;
Output:
savepoint created;
ROLLBACK
Syntax:
rollback to savepoint_text_identifier;
INPUT:
SQL> rollback to emp;
Output:
Rollback complete.
14 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
COMMIT Syntax: commit; INPUT:
SQL> commit;
Output:
Commit completed.
Result:
Thus the table was successfully saved, committed and rollback.
Outcome:
Thus the outcome of applying TCL Commands has been attained.
Viva-voce
1. What is savepoint in Oracle?
2. Write the syntax for savepoint.
3. What are the TCL commands?
4. What is VDL (View Definition Language)?
5. What is SDL (Storage Definition Language)?
6. What is Data Storage?
7. What is Functional dependency?
8. What is DCL?
9. What is transaction control language?
10. What do you mean by oracle language?
15 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
ENO
7369
ENAME
SMITH
JOB
CLERK
MGR
7902
HIREDATE
17-DEC-10
SAL
8000
COMM
0
DEPTNO
20
AGE
25
7499 ALLEN SALESMAN 7698 20-FEB-11 16000 300 30 25
7521 WARD SALESMAN 7698 22-FEB-11 12500 500 30 25
7566 JONES MANAGER 7839 02-APR-11 29705 500 20 25
7698 BLAKE MANAGER 7839 01-MAY-12 28500 1400 30 25
Expt. No.: 4 WRITING AND PRACTICE OF SIMPLE QUERIES
Aim:
To write simple SQL queries and to practice them
Software Requirements:
Front end: VB/VC ++/JAVA or Equivalent
Back end: Oracle / SQL / MySQL/ PostGress / DB2 or Equivalent
Hardware Requirements:
Standalone desktops 30 Numbers
Problem Statements:
List all employee details SQL>SELECT * FROM EMP;
OUTPUT:
List all employee names and their salaries, whose salary lies between 1500/- and 3500/- both inclusive.
INPUT
SQL>SELECT ENAME FROM EMP WHERE SAL BETWEEN 1500 AND 3500;
OUTPUT: ENAME
CLARK
rows selected.
List all employee names and their and their manager whose manager is 7902 or 7566 0r 7789. INPUT:
SQL> SELECT ENAME FROM EMP WHERE MGR IN(7602,7566,7789);
OUTPUT:
ENAME ------- SCOTT
16 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
FORD
1. List all employees which starts with either J or T.
INPUT: SQL> SELECT ENAME FROM EMP WHERE ENAME LIKE ‘J%’ OR ENAME LIKE ‘T%’; OUTPUT:
ENAME
--------- JONES
TURNER
JAMES
2. List all employee names and jobs, whose job title includes M or P.
INPUT:
SQL> SELECT ENAME,JOB FROM EMP WHERE JOB LIKE ‘M%’ OR JOB LIKE ‘P%’;
OUTPUT:
ENAME JOB
---------- ---------
JONE MANAGER
BLAKE MANAGER
CLARK MANAGER
KING PRESIDENT
3. List all jobs available in employee table. INPUT:
SQL> SELECT DISTINCT JOB FROM EMP;
OUTPUT:
JOB
---------
ANALYST
CLERK
MANAGER
PRESIDENT
SALESMAN
ASSISTANT
CLERK
7 rows selected.
4. What is the difference between maximum and minimum salaries of employees in the organization? INPUT: SQL>
SELECT MAX(SAL)-MIN(SAL) FROM EMP;
17 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
OUTPUT:
MAX(SAL)-MIN(SAL)
-----------------
4997
5. Display all employee names and salary whose salary is greater than minimum salary of the company and job title
starts with ‘M’.
INPUT: SQL> SELECT ENAME,SAL FROM EMP WHERE JOB LIKE ‘M%’ AND SAL > (SELECT MIN (SAL)
FROM EMP);
OUTPUT:
ENAME SAL
---------- ----------
JONES 2975
BLAKE 2850
CLARK 2450
6. Find how much amount the company is spending towards salaries. INPUT: SQL> SELECT SUM (SAL) FROM
EMP;
OUTPUT: SUM(SAL)
---------
32928
7. Display name of the dept. with deptno 20.
INPUT: SQL>SELECT ENAME FROM EMP WHERE DEPTNO = 20;
OUTPUT :
ENAME
--------- SMITH JONES
SCOTT ADAMS
8. List ename whose commission is NULL.
INPUT: SQL> SELECT ENAME FROM EMP WHERE COMM IS NULL; OUTPUT:
ENAME
------------
CLARK SCOTT KING ADAMS JAMES FORD
6 rows selected.
9. Find no.of dept in employee table.
18 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
INPUT: SQL> SELECT COUNT (DISTINCT ENAME) FROM EMP;
OUTPUT:
COUNT(DISTINCT ENAME)
--------------------------------------- 17
10. List ename whose manager is not NULL.
INPUT:
SQL> SELECT ENAME FROM EMP WHERE MGR IS NOT NULL; OUTPUT: ENAME
---------- SMITH ALLEN WARD JONES MARTIN
5 rows selected.
11. Calculate the experience in years of each programmer and display along with their name in descending order.
INPUT: SQL> SELECT PNAME, ROUND (MONTHS_BETWEEN(SYSDATE, DOJ)/12, 2) "EXPERIENCE" FROM
PROGRAMMER ORDER BY MONTHS_BETWEEN (SYSDATE, DOJ) DESC;
12. In which month most number of programmers has joined.
INPUT: SQL>SELECT TO_CHAR (DOJ,’YY’) FROM PROGRAMMER GROUP BY TO_CHAR (DOJ,’YY’) HAVING
COUNT (*) = (SELECT MAX (COUNT(*)) FROM PROGRAMMER GROUP BY TO_CHAR (DOJ,’YY’);
Result:
Thus the given problem statements were solved using simple queries.
Outcome:
Thus the outcome for creating table for real world scenarios has been attained
19 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Viva-voce
1. Which command used to display all details from employee?
2. List all employee names and their salaries, whose salary lies between 1500/- and 3500/- both inclusive.
3. List all employee names and their and their manager whose manager is 7902 or 7566 0r 7789.
4. Which date function is used to find the difference between two dates?
5. What is the use of DESC in SQL?
6. What do you mean by where clause?
7. Which of the SQL command can be used to modify existing data in a database table?
8. What is E-R model?
9. What is a checkpoint and when does it occur?
10. List any two advantages of view.
20 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Expt. No.: 5 NESTED QUERIES & QUERIES USING GROUP BY
AND OTHER CLAUSES
Aim:
To write queries using Set operations, to write nested queries and also to write queries using clauses such as
GROUP BY, ORDER BY, etc. and retrieving information by joining tables
Software requirements:
Front end: VB/VC ++/JAVA or Equivalent
Back end: Oracle / SQL / MySQL/ PostGress / DB2 or Equivalent
Hardware requirements:
Pentium Dual Core 1.9Ghz and 2GB RAM
Set Operations & Other Clauses:
NESTED QUERY: A nested query makes use of another sub-query to compute or retrieve the information.
UNION - OR
INTERSECT - AND
EXCEPT - NOT
Order by: The order by clause is used to display the results in sorted order.
Group by: The attribute or attributes given in the clauses are used to form groups. Tuples with the same value on
all attributes in the group by clause are placed in one group.
Having: SQL applies predicates (conditions) in the having clause after groups have been formed, so aggregate
function be used.
Problem Statements:
1. Find the name of the institute in which the person studied and developed the costliest package. INPUT: SQL>
SELECT SPLACE, PNAME FROM STUDY WHERE PNAME = (SELECT PNAME FROM SOFTWARE
WHERE SCOST = (SELECT MAX (SCOST) FROM SOFTWARE);
OUTPUT:
SPLACE PNAME
----------- -------------
SAHBHARI MARY
2. Find the salary and institute of a person who developed the highest selling package. INPUT: SQL> SELECT
21 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
STUDY.PNAME, SAL, SPLACE FROM STUDY, PROGRAMMER WHERE
STUDY.PNAME = PROGRAMMER.PNAME AND STUDY.PNAME = (SELECT PNAME FROM SOFTWARE
WHERE SCOST = (SELECT MAX (SCOST) FROM SOFTWARE));
OUTPUT:
PNAME SAL SPLACE
----------- ------ ----
MARY 4500S BHARI
3. How many packages were developed by the person who developed the cheapest package.
INPUT: SQL> SELECT PNAME, COUNT (TITLE) FROM SOFTWARE WHERE DCOST = (SELECT MIN(DCOST)
FROM SOFTWARE) GROUP BY PNAME;
RESULT
PNAME COUNT(TITLE)
------------- ----------------------
VIJAY 1
4. Calculate the amount to be recovered for those packages whose development cost has not yet recovered.
INPUT: SQL>SELECT TITLE, (DCOST-SCOST) FROM SOFTWARE WHERE DCOST > SCOST;
5. Display the title, scost, dcost, difference of scost and dcost in the descending order of difference. INPUT: SQL>
SELECT TITLE, SCOST, DCOST, (SCOST - DCOST) FROM SOFTWARE DESCENDING ORDER
BY (SCOST-DCOST);
6. Display the details of those who draw the same salary.
INPUT: SQL> SELECT P.PNAME, P.SAL FROM PROGRAMMER P, PROGRAMMER T WHERE P.PNAME <>
T.PNAME AND P.SAL = T.SAL;(OR)
INPUT: SQL>SELECT PNAME,SAL FROM PROGRAMMER T WHERE PNAME<>T.PNAME AND SAL= T.SAL;
7. Display total salary spent for each job category.
INPUT: SQL>SELECT JOB,SUM (SAL) FROM EMP GROUP BY JOB; OUTPUT:
JOB SUM(SAL)
--------- ----------
CLERK 23050
MANAGER 8275
PRESIDENT 5000
SALESMAN 5600
7 rows selected.
22 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
8. Display lowest paid employee details under each manager.
INPUT: SQL>SELECT ENAME, SAL FROM EMP WHERE SAL IN (SELECT MIN(SAL) FROM EMP GROUP BY
MGR);
OUTPUT:
ENAME SAL
---------- ----------
CHAI 3
JAMES 950
MILLER 1000
ADAMS 1100
russel 2200
5 rows selected.
9. Display number of employees working in each department and their department name.
INPUT: SQL> SELECT DNAME, COUNT (ENAME) FROM EMP, DEPT WHERE EMP.DEPTNO=DEPT.DEPTNO
GROUP BY DNAME;
OUTPUT:
DNAME COUNT(ENAME)
-------------- ------------
ACCOUNTING 3
RESEARCH 5
SALES 9
10. Display the sales cost of package developed by each programmer.
INPUT: SQL> SELECT PNAME, SUM(SCOST) FROM SOFTWARE GROUP BY PNAME; OUTPUT:
PNAMESUM(SCOST)
-------------------- ----------
john 12000
kamala 12000
raju 12333
3 rows selected.
11. Display the number of packages sold by each programmer.
INPUT: SQL>SELECT PNAME, COUNT(TITLE) FROM SOFTWARE GROUP BY PNAME;
12. Display the number of packages in each language for which the development cost is less than thousand.
23 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
INPUT: SQL>SELECT DEVIN, COUNT(TITLE) FROM SOFTWARE WHERE DCOST < 1000 GROUP BY DEVIN;
13. Display each institute name with number of students.
INPUT: SQL> SELECT SPLACE, COUNT(PNAME) FROM STUDY GROUP BY SPLACE;
OUTPUT:
SPLACE COUNT(PNAME)
-------------------- ------------
BDPS 2
BITS 1
BNRILLIANI 1
COIT 1
HYD 1
5 rows selected.
14. How many copies of package have the least difference between development and selling cost, were sold?
INPUT: SQL>select SOLD FROM SOFTWARE WHERE SCOST – DCOST=(SELECT MIN(SCOST – DCOST)
FROM SOFTWARE);
999
OUTPUT: SOLD
--------- 11
15.Which is the costliest package developed in Pascal.
INPUT: SQL>SELECT TITLE FROM SOFTWARE WHERE DEVIN = ‘PASCAL’ AND DCOST = (SELECT
MAX(DCOST) FROM SOFTWARE WHERE DEVIN = ‘PASCAL’);
OUTPUT:
no rows selected
Result:
Thus the given problem statements were solved using nested queries and group by & other clause.
Outcome:
Thus the outcome of Framing and Joining Nested Queries has been attained.
24 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Viva-voce
1. What is the purpose of Having clause?
2. What is format of Date?
3. What is the purpose of ROWNUM?
4. Differentiate Nested Loop from Merge Join.
5. How are the OUTER JOIN operations different from JOIN operations?
6. How is the OUTER UNION operation different from UNION?
7. List the types of privileges available in SQL.
8. What are the commands available for modifying the database in SQL?
9. What is view in SQL? How is it defined?
10. List the SQL domain Types.
11. What are aggregate functions?
12. What is the use of group by clause?
13. What is the use of sub queries?
25 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Expt. No.: 6 PROBLEMS BASED ON VIEWS, SYNONYM,
SEQUENCE & INDEX
Aim:
To solve the problem statements based on views, synonym, sequence and Indexes
Software Requirements:
Front end: VB/VC ++/JAVA or Equivalent
Back end: Oracle / SQL / MySQL/ PostGress / DB2 or Equivalent
Hardware Requirements: Pentium Dual Core 1.9Ghz and 2GB RAM
Overview:
VIEW:
A view is simply the representation of a SQL statement that is stored in memory so that it can easily be re-used.
It’s also referred as logical table.
SYNTAX:
CREATE OR REPLACE VIEW <view name > AS < select statement >
SYNONYM:
A synonym is an alias or alternate name for a table, view, sequence, or other schema object. They are used
mainly to make it easy for users to access database objects owned by other users.
SYNTAX:
CREATE OR REPLACE SYNONYM <synonym_name> FOR <object_name>
SEQUENCE:
A sequence is a database object from which multiple users may generate unique integers. User can use
sequences to automatically generate primary key values.
INDEXES:
Database system uses indexes to avoid the need for large-table, full-table scans and disk sorts, which are
required when the SQL optimizer cannot find an efficient way to service the SQL query.
26 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Problem Statements:
1. Create a view from single table containing all columns from the base table. SQL>CREATE VIEW VIEW1 AS
(SELECT * FROM PROGRAMMER);
2. Create a view from single table with selected columns.
SQL>CREATE A VIEW VIEW2 AS (SELECT PNAME, DOB, DOJ, SEX, SAL FROM PROGRAMMER);
3. Create a view from two tables with all columns.
SQL>CREATE VIEW XYZ AS SELECT * FROM PROGRAMMER FULL NATURAL JOIN SOFTWARE;
4. Create a view from two tables with selected columns.
SQL> CREATE VIEW LMN AS (SELECT PROGRAMMER, PNAME, TITLE, DEVIN FROM PROGRAMMER,
SOFTWARE WHERE SAL < 3000 AND PROGRAMMER.PNAME = SOFTWARE.PNAME);
5. Check all DML commands with above 4 views.
INPUT: SQL> INSERT INTO VIEW1 VALUES (‘RAMU’,’12-SEP-03’,’28-JAN-85’ ,’F’, ’DBASE’,’ ORACLE’, 74000);
OUTPUT:
1 row created;
INPUT: SQL>UPDATE VIEW1 SET SALARY =50000 WHERE PNAME LIKE ‘SARVAN’; OUTPUT:
1 row updated.
Note: update command does not works for all queries on views. INPUT: SQL>DELETE FROM VIEW1 WHERE
PNAME LIKE ‘SMITH’;
OUTPUT:
1 row deleted.
6. Drop views which you generated. INPUT: SQL>DROP VIEW VIEW1;
OUTPUT: View dropped.
7. Create a synonym for a table created by other user.
SQL> CREATE SYNONYM EMP FOR SP.EMPLOYE;
8. Try to perform all DML operations through the created synonym.
SQL> INSERT INTO EMP VALUES (‘EMP10’,’LARA’,’MAINTENANCE’,’27-OCT-1998’);
OUTPUT:
1 row created.
SQL> UPDATE EMP SET EMPNAME=’BRAIN LARA’ WHERE EMPID=’EMP10’; OUTPUT:
1 row Updated.
9. Create a sequence to generate unique value for empid field in the employee table while inserting. SQL> CREATE
SEQUENCE EMP_SEQ START WITH 1000;
27 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
INCREMENT BY 1 NOCACHE NOCYCLE;
Sequence Created.
SQL> INSERT INTO EMP VALUES (EMP_SEQ.NEXTVAL,’SAHA’,’TESTING’,’27-DEC-1999’); OUTPUT:
1. Declarations: This section starts with the keyword DECLARE. It is an optional section and defines all variables,
cursors, subprograms, and other elements to be used in the program.
2. Executable Commands: This section is enclosed between the keywords BEGIN and END and it is a mandatory
section. It consists of the executable PL/SQL statements of the program. It should have at least one executable line
of code, which may be just a NULL command to indicate that nothing should be executed.
3. Exception Handling: This section starts with the keyword EXCEPTION. This section is again optional and
contains exception(s) that handle errors in the program.Every PL/SQL statement ends with a semicolon (;). PL/SQL
blocks can be nested within other PL/SQL blocks using BEGIN and END.
Result:
Thus the given problem statements based on views, synonyms, sequences and indexes were solved.
Outcome:
Thus the outcome for developing a PL/SQL programs based on views, synonyms, sequences and indexes has
been attained.
28 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Viva-voce
1. What is view?
2. List out the types of views.
3. How many types of indexes were available in Oracle?
4. What are the four broad categories of constraints?
5. What is meant by Cost Estimation?
6. List the SQL statements used for transaction control.
7. With an example explain referential integrity.
8. What are the advantages of referential integrity constraints?
9. What is sequence?
10. What is a synonym in Oracle?
31 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Expt. No.: 7 CREATING DATABASE
Aim:
To create database and also to create relationship among database
Software requirements:
Front end: VB/VC ++/JAVA or Equivalent
Back end: Oracle / SQL / MySQL/ PostGress / DB2 or Equivalent
Hardware requirements:
Pentium Dual Core 1.9Ghz and 2GB RAM
Command Overview:
SYNTAX:
CREATE DATABASE [ database
{ USER SYS IDENTIFIED BY password
| USER SYSTEM IDENTIFIED BY password
| CONTROLFILE REUSE
| MAXDATAFILES integer
| MAXINSTANCES integer
| CHARACTER SET charset
| NATIONAL CHARACTER SET charset
| SET DEFAULT
{ BIGFILE | SMALLFILE } TABLESPACE
| database_logging_clauses
| tablespace_clauses
| set_time_zone_clause
};
32 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Problem Statement:
1. Create a new database by the name student_db with required features.
SQL> CREATE DATABASE STUDENT_DB USER SYS IDENTIFIED BY ORACLE USER SYSTEM IDENTIFIED
BY ORACLE
DATAFILE ‘C:\oracle\oradata\ORA11\SYSTEM01.DBF’ SIZE 325M REUSE AUTOEXTEND ON NEXT 10240K
MAXSIZE UNLIMITED
SYSAUX DATAFILE ‘C:\oracle\oradata\ORA11\SYSAUX01.DAT’ SIZE 120M REUSE AUTOEXTEND ON NEXT
5M MAXSIZE 2048M
DEFAULT TABLESPACE USERS DATAFILE ‘C:\oracle\oradata\ORA11\USERS01.DBF’ SIZE 50M REUSE
AUTOEXTEND ON MAXSIZE UNLIMITED;
Result:
Thus the database was created with the specified parameters.
Outcome:
Thus the outcome for developing and designing a database has been attained.
33 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Viva-voce
1. What is Data?
2. How to know the current database we are working with?
3. How to create table space?
4. What are the types of storage devices?
5. Write the syntax for database creation.
6. What is the "integrity rules"?
7. What is extension and intension?
8. What is an Entity?
34 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Expt. No.: 8 HIGH LEVEL PROGRAMMING LANGUAGE
EXTENSION (PL/SQL BLOCKS)
Aim:
To solve the given problem statements using PL/SQL blocks
Software requirements:
Front end: VB/VC ++/JAVA or Equivalent
Back end: Oracle / SQL / MySQL/ PostGress / DB2 or Equivalent
Hardware requirements:
Pentium Dual Core 1.9Ghz and 2GB RAM
PL/SQL block:
1. Declarations: This section starts with the keyword DECLARE. It is an optional section and defines all variables,
cursors, subprograms, and other elements to be used in the program.
2. Executable Commands: This section is enclosed between the keywords BEGIN and END and it is a mandatory
section. It consists of the executable PL/SQL statements of the program. It should have at least one executable line
of code, which may be just a NULL command to indicate that nothing should be executed.
3. Exception Handling: This section starts with the keyword EXCEPTION. This section is again optional and
contains exception(s) that handle errors in the program.Every PL/SQL statement ends with a semicolon (;). PL/SQL
blocks can be nested within other PL/SQL blocks using BEGIN and END.
Problem Statements:
REFERED TABLES:
Employe (emp_id,emp_name,dept_id,basic,hra,da,pf,net);
1. Create a PL/SQL block for inserting values in the Employee table. Only emp_id, emp_name, department & basic
should be received as input while executing the block and for the rest of the fields the values need to be calculated
as given below.
Calculations: HRA=50% OF BASIC DA=20% OF BASIC PF=7% OF BASIC
NETPAY=BASIC+DA+HRA-PF
INPUT:
35 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
DECLARE
ENO1 employe.emp_id%type; ENAME1 employe.emp_name%type; DEPTNO1 employe.dept_id%type; BASIC1
employe.basic%type;
HRA1 employe.HRA%type; DA1 employe.DA%type; PF1 employe.pf%type; NETPAY1
employe.net%type;
BEGIN
ENO1:=&ENO1; ENAME1:='&ENAME1'; DEPTNO1:=&DEPTNO1; BASIC1:=&BASIC1; HRA1:=(BASIC1*50)/100;
DA1:=(BASIC1*20)/100; PF1:=(BASIC1*7)/100; NETPAY1:=BASIC1+HRA1+DA1-PF1;
INSERT INTO EMPLOYE VALUES (ENO1, ENAME1, DEPTNO1, BASIC1, HRA1, DA1, PF1,NETPAY1); END;
OUTPUT:
SQL> @BASIC
Enter value for eno1: 104 old 11: ENO1:=&ENO1; new
11: ENO1:=104;
Enter value for ename1: SRINIVAS REDDY old 12: ENAME1:='&ENAME1';
new 12: ENAME1:='SRINIVAS REDDY';
Enter value for deptno1: 10
old 13: DEPTNO1:=&DEPTNO1;
new 13: DEPTNO1:=10;
Enter value for basic1: 6000 old 14: BASIC1:=&BASIC1; new
14: BASIC1:=6000;
PL/SQL procedure successfully completed. SQL>/
Enter value for eno1: 105 old 11: ENO1:=&ENO1;
new 11: ENO1:=105;
Enter value for ename1: CIRAJ old 12: ENAME1:='&ENAME1'; new
12: ENAME1:='CIRAJ';
Enter value for deptno1: 10
old 13: DEPTNO1:=&DEPTNO1;
new 13: DEPTNO1:=10;
Enter value for basic1: 6000 old 14: BASIC1:=&BASIC1; new
14: BASIC1:=6000;
PL/SQL procedure successfully completed.
36 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
SQL> SELECT * FROM EMPDET;
OUTPUT:
EMP_ID
EMP_ NAME
DEPT_ID
BASIC
HRA
DA
PF
NETPAY
101 GAYLE 10 5000 2500 1000 350 8150
102 SARVAN 20 5000 2500 1000 350 8150
103 POINTING 20 5500 2750 1100 385 8965
104 SMITH 10 6000 3000 1200 420 9780
105 ALIS 10 6000 3000 1200 420 9780
2. Create a PL/SQL block for updating records in Employe table where the user should provide the emp_id and the
new basic salary and thus the HRA,DA, PF and NETPAY should get calculated and updated accordingly.
INPUT: DECLARE
ENO1 employe.emp_id%type;
BASIC1 employe.basic%type; HRA1 employe.HRA%type; DA1 employe.DA%type;
PF1 employe.pf%type; NETPAY1 employe.net%type;
BEGIN ENO1:=&ENO1; BASIC1:=&BASIC1;
HRA1:=(BASIC1*50)/100; DA1:=(BASIC1*20)/100; PF1:=(BASIC1*7)/100; NETPAY1:=BASIC1+HRA1+DA1-PF1;
UPDATE EMPLOYE SET BASIC=BASIC1, HRA=HRA1, DA=DA1, PF=PF1, NETPAY=NETPAY1
WHERE EMP_ID=ENO1;
END; OUTPUT: SQL>/
Enter value for eno1: 105 old 11: ENO1:=&ENO1;
new 11: ENO1:=105;
Enter value for basic1: 8000 old 14: BASIC1:=&BASIC1;
new 14: BASIC1:=8000;
PL/SQL procedure successfully completed.
Result:
Thus PL/SQL statements were written for inserting & updating the rows in the EMPLOYE table.
Outcome:
Thus the outcome for developing a PL / SQL programs has been attained.
37 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Viva-voce
1. What is PL SQL?
2. What are the uses of cursor?
3. What are the two types of exceptions?
4. What is Raise_application_error?
5. What is function in PL/SQL block?
6. Explain two virtual tables available at the time of database trigger execution.
7. What are the rules to be applied to NULLs whilst doing comparisons?
8. How is a process of PL /SQL compiled?
9. Define Implicit and Explicit Cursors
10. When is a declare statement required?
11. What is the importance of SQLCODE and SQLERRM?
38 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Expt. No.: 9 PROCEDURES
Aim:
To solve the given problem statements using procedures
Software requirements:
Front end: VB/VC ++/JAVA or Equivalent
Back end: Oracle / SQL / MySQL/ PostGress / DB2 or Equivalent
Hardware requirements:
Pentium Dual Core 1.9Ghz and 2GB RAM
PL/SQL procedures:
An Oracle stored procedure is a program stored in an Oracle database.
The following are the advantages of using procedures.
Better performance: Oracle stored procedures load once into the shared pool and remain there unless they
become paged out. Subsequent executions of the Oracle stored procedure are far faster than executions of
external code.
Coupling of data with behavior: DBAs can use naming conventions to couple relational tables with the behaviors
associated with a table by using Oracle stored procedures as "methods". If all behaviors associated with the
employee table are prefixed with the table name--employee. Hire, employee.give_raise, for example--the data
dictionary can be queries to list all behaviors associated with a table (select * from dba_objects where owner =
'EMPLOYEE'), and it's easy to identify and reuse code via stored procedures.
Isolation of code: Since all SQL is moved out of the external programs and into the Oracle stored procedures, the
application programs become nothing more than calls to Oracle stored procedures. As such, it becomes v e r y
simple to swap out one database and swap in another one.
A view is simply the representation of a SQL statement that is stored in memory so that it can easily be re-used.
Its also referred as logical table.
1. Declarations: This section starts with the keyword DECLARE. It is an optional section and defines all variables,
cursors, subprograms, and other elements to be used in the program.
3 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
2. Executable Commands: This section is enclosed between the keywords BEGIN and END and it is a mandatory
section. It consists of the executable PL/SQL statements of the program. It should have at least one executable
line of code, which may be just a NULL command to indicate that nothing should be executed.
3. Exception Handling: This section starts with the keyword EXCEPTION. This section is again optional and
contains exception(s) that handle errors in the program.Every PL/SQL statement ends with a semicolon (;).
PL/SQL blocks can be nested within other PL/SQL blocks using BEGIN and END.
SYNTAX:
CREATE OR REPLACE VIEW <view name > AS < select statement >
SYNONYM:
A synonym is an alias or alternate name for a table, view, sequence, or other schema object. They are
used mainly to make it easy for users to access database objects owned by other users.
SYNTAX:
CREATE OR REPLACE SYNONYM <synonym_name> FOR <object_name>
SEQUENCE:
A sequence is a database object from which multiple users may generate unique integers. User can
use sequences to automatically generate primary key values.
INDEXES:
Database system uses indexes to avoid the need for large-table, full-table scans and disk sorts, which
are required when the SQL optimizer cannot find an efficient way to service the SQL query.
SYNTAX:
CREATE [OR REPLACE] PROCEDURE procedure_name (parameters list) IS
<declaration_section>
BEGIN
<executable_section>
EXCEPTION
<exception_section
> END;
4 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Problem Statements:
REFERRED TABLES:
Borrow(acc_no , rollno, date_issue);
1.Write a procedure to insert a record in borrower relation. Before inserting check whether the book is available or
not.
CREATE OR REPLACE PROCEDURE PROC_BORROW(ACCNO NUMBER, ROLL VARCHAR, DOI DATE) IS
CNT NUMBER(5); BEGIN
SELECT COUNT(*) INTO CNT FROM BORROW WHERE ACC_NO=ACCNO;
IF(CNT=0) THEN
INSERT INTO BORROW VALUES (ACCNO,ROLL,DOI); ELSE
DBMS_OUTPUT.PUT_LINE(‘BOOK NOT AVAILABLE’); END IF;
END;
OUTPUT:
SQL> @ e:\proc.sql; Procedure Created.
SQL> exec pro1(‘123’,’CS01’,’27-OCT-2013’);
Procedure successfully completed. SQL> SELECT * FROM BORROW;
Result:
Thus PL/SQL Procedures were created to solve the given problem statements.
Outcome:
Thus the outcome for developing a PL / SQL program using Procedures has been attained.
5 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Viva-voce
1. What is a stored procedure?
2. What is a stand-alone procedure?
3. What are the modes of parameters that can be passed to a procedure?
4. What are advantages of Stored Procedures?
5. What operators deal with NULL?
6. Does SQL*Plus also have a PL/SQL Engine?
7. What packages are available to PL SQL developers?
8. Explain 3 basic parts of a trigger.
9. What are character functions?
10. Show the cursor attributes of PL/SQL.
41 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Expt. No.: 10 FUNCTIONS
Aim:
To solve the given problem statements using PL/SQL functions
Software requirements:
Front end: VB/VC ++/JAVA or Equivalent
Back end: Oracle / SQL / MySQL/ PostGress / DB2 or Equivalent
Hardware requirements:
Pentium Dual Core 1.9Ghz and 2GB RAM
PL/SQL function:
A PL/SQL function is same as a procedure except that it returns a value. A standalone function is created using
the CREATE FUNCTION statement.
SYNTAX:
CREATE [OR REPLACE] FUNCTION function_name (parameter_name [IN | OUT | IN OUT] type [, ...]) RETURN
return_datatype
{IS | AS} BEGIN
< function_body > END;
Problem Statement:
REFERRED TABLES:
Transaction (accno number(5), amount number(7,2), trans_type varchar(5),dot date);
1. Create a function to insert the records into the transaction table, after performing each transaction in the
transaction table show the net balance of the particular account.
CREATE OR REPLACE FUNCTION FUNC_TRANS (ACC_ID NUMBER,AMNT NUMBER,TYPE VARCHAR)
RETURN NUMBER
IS BALANCE NUMBER; BEGIN
INSERT INTO TRANSACTION VALUES(ACC_ID,AMNT,TYPE);
SELECT SUM(AMNT) INTO BALANCE FROM TRANSACTION WHERE ACC_NO=ACC_ID; RETURN
BALANCE;
42 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
END;
PL/SQL FUNCTION:
DECLARE
BALANCE_AMNT NUMBER(6); ACC_NO VARCHAR(5);
AMNT NUMBER(5); TYPE VARCHAR(2); RESULT NUMBER(5); BEGIN
ACC_NO:=’&ACC_NO’; AMNT:=’&AMNT’; TYPE:=’&TYPE’;
BALANCE_AMNT:=FUNC1(ACC_ID,AMNT,TYPE); DBMS_OUTPUT.PUT_LINE(‘TOTAL AMOUNT ‘||
TOTAL_AMNT);
END;
OUTPUT:
SQL> @E:\SQL\FUNCPLSQL.SQL;
Enter value for accno: 001 Enter value for amt: 25000 Enter value for type=’CRDT’ RESULT 26000
PL/SQL procedure successfully completed.
Result:
Thus PL/SQL functions were created to solve the given problem statements.
Outcome:
Thus the outcome for developing a PL / SQL programs using functions has been attained.
43 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Viva-voce
1. What is the difference between FUNCTION, PROCEDURE AND PACKAGE in PL/SQL?
2. Is the function return value?
3. What are the basic functions for master, msdb, model, tempdb databases?
4. Write the syntax for function.
5. How to display return value the PL/SQL function?
44 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Expt. No.: 11 TRIGGERS
Aim:
To solve the given problem statements using triggers
Software requirements:
Front end: VB/VC ++/JAVA or Equivalent
Back end: Oracle / SQL / MySQL/ PostGress / DB2 or Equivalent
Hardware requirements:
Pentium Dual Core 1.9Ghz and 2GB RAM
PL/SQL Triggers:
Triggers are stored programs, which are automatically executed or fired when some events occur. Triggers are, in
fact, written to be executed in response to any of the following events: A database manipulation (DML) statement
(DELETE, INSERT, or UPDATE).
A database definition (DDL) statement (CREATE, ALTER, or DROP).
A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN).
Triggers could be defined on the table, view, schema, or database with which the event is associated.
SYNTAX:
CREATE OR REPLACE TRIGGER < trigger_name >
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE} [OF col_name]
ON < table_name >
[REFERENCING OLD AS o NEW AS n] [FOR EACH ROW]
WHEN < condition > DECLARE
< Declaration-statements > BEGIN
< Executable-statements > EXCEPTION
< Exception-handling-statements > END;
45 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Problem Statement:
REFERRED TABLES:
Account ( accnt_no,cst_id,acnt_type,last_trans_date,balance ) Account_bckup(accnt_no,last_trans_date,balance)
Loan (ln_id,cst_id,ln_amount,ln_date);
1. Create a trigger for Account relation such that whenever a record is inserted in the Account table the same record
also gets inserted in the backup table.
CREATE OR REPLACE TRIGGER TRIG_ACNT_BCKUP AFTER INSERT ON ACCOUNT FOR EACH ROW
DECLAR BEGIN
INSERT INTO ACCOUNT_BCKUP VALUES (:NEW.ACCNT_NO, :NEW.LAST_TRANS_DATE,
NEW.BALANCE); END;
OUTPUT:
SQL> @e:/plsql/accnt_trig.sql Trigger Created.
SQL> INSERT INTO ACCOUNT VALUES (‘AC010’,’CST011’,’SVNG’,’27-AUG-2013’,12000);
1 Row inserted.
SQL> SELECT * FROM ACCOUNT_BCKUP;
2. Create a trigger for account relation such that whenever account record is inserted in account relation with
negative relation then that record should also be inserted in the loan relation with positive balance.
CREATE OR REPLACE TRIGGER TRIG_LOAN AFTER INSERT ON ACCOUNT
FOR EACH ROW DECLARE BEGIN
IF(:NEW.BALANCE<0)
THEN
INSERT INTO LOAN VALUES (:NEW.ACCNT_NO, :NEW.CST_ID, -(:NEW.BALANCE), SYSDATE); END IF;
END;
OUTPUT:
SQL> @e:/plsql/loan_trig.sql Trigger Created.
SQL> INSERT INTO ACCOUNT VALUES (‘AC011’,’CST011’,’SVNG’,’27-DEC-2013’,-8000);
1 Row inserted.
SQL> SELECT * FROM LOAN;
LN_ID CST_ID LN_AMOUNT LN_DATE
AC010 CST011 8000 30-DEC-2013
3. Create a trigger for Account relation such that whenever a record is inserted in the Account table the same record
also gets inserted in the backup table.
46 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
CREATE OR REPLACE TRIGGER TRIG_ACNT_BCKUP AFTER INSERT ON ACCOUNT FOR EACH ROW
DECLAR BEGIN
INSERT INTO ACCOUNT_BCKUP VALUES (:NEW.ACCNT_NO, :NEW.LAST_TRANS_DATE,
:NEW.BALANCE); END;
OUTPUT:
SQL> @e:/plsql/accnt_trig.sql Trigger Created.
SQL> INSERT INTO ACCOUNT VALUES (‘AC010’,’CST011’,’SVNG’,’27-AUG-2013’,12000);
1 Row inserted.
SQL> SELECT * FROM ACCOUNT_BCKUP;
4. Create a trigger for account relation such that whenever account record is inserted in account relation with
negative relation then that record should also be inserted in the loan relation with positive balance.
CREATE OR REPLACE TRIGGER TRIG_LOAN AFTER INSERT ON ACCOUNT
FOR EACH ROW DECLARE BEGIN
IF(:NEW.BALANCE<0)
THEN
INSERT INTO LOAN VALUES (:NEW.ACCNT_NO, :NEW.CST_ID, -(:NEW.BALANCE), SYSDATE); END IF;
END;
OUTPUT:
SQL> @e:/plsql/loan_trig.sql Trigger Created.
SQL> INSERT INTO ACCOUNT VALUES (‘AC011’,’CST011’,’SVNG’,’27-DEC-2013’,-8000);
1 Row inserted.
SQL> SELECT * FROM LOAN;
LN_ID CST_ID LN_AMOUNT LN_DATE
AC010 CST011 8000 30-DEC-2013
47 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Result:
Thus the triggers are created accordingly to solve the given problem statements.
Outcome:
Thus the outcome for developing a Trigger has been attained.
48 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Viva-voce
1. What is a Trigger?
2. What is a stored procedure?
3. What is the difference between Trigger and Stored Procedure?
4. What is a transaction? What are ACID properties?
5. What are indexes?
6. Compare trigger, procedure and cursors.
7. What are the various types of triggers?
8. What is a PL/SQL trigger?
9. Describe triggers features and limitations.
10. What are the variables used in triggers?
49 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Expt. No.: 12 Payroll Processing System
Aim: To develop a mini project using vb/oracle
Software requirements:
Front end: VB/VC ++/JAVA or Equivalent
Back end: Oracle / SQL / MySQL/ PostGress / DB2 or Equivalent
Hardware requirements:
Pentium Dual Core 1.9Ghz and 2GB RAM
Procedure:
• Create a database for payroll processing which request the using SQL
• Establish ODBC connection
• In the administrator tools open data source ODBC
• Click add button and select oracle in ORA home 90
• click finish
• A window will appear given the data source home as oracle and select TNS source name as lion and give the
used id as SWTT
• ADODC CONTROL FOR SALARY FORM:-
• The above procedure must be follow except the table ,A select the table as salary
• Write appropriate Program in form each from created in VB from each from created in VB form project.
SQL>create table emp(eno number primary key,enamr varchar(20),age number,addr varchar(20),DOB date,phno
number(10));
Table created.
SQL>create table salary(eno number,edesig varchar(10),basic number,da number,hra number,pf number,mc
number,met number,foreign key(eno) references emp);
Table created.
TRIGGER to calculate DA,HRA,PF,MC
SQL> create or replace trigger employ
50 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
2 after insert on salary
3 declare
4 cursor cur is select eno,basic from salary;
5 begin
6 for cur1 in cur loop 7
update salary set
8 hra=basic*0.1,da=basic*0.07,pf=basic*0.05,mc=basic*0.03 where hra=0;
9 end loop;
10 end;
11 /
Trigger created
Output
51 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Result:
Thus the design and implementation of payroll processing system using oracle, Vb was successfully done.
52 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
Exp. No. 13 SQL Join
Aim:
To solve the given problem statements using triggers
Software requirements:
Front end: VB/VC ++/JAVA or Equivalent
Back end: Oracle / SQL / MySQL/ PostGress / DB2 or Equivalent
Hardware requirements:
Standalone desktops 30 Numbers
SQL JOIN
An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.
Different SQL JOINs
• LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
• RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
• FULL JOIN: Return all rows when there is a match in ONE of the tables
SQL LEFT JOIN Keyword
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.
SQL LEFT JOIN Syntax
SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name=table2.column_name;
SQL LEFT JOIN Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
53 Format No.:DCE/Stud/LM/34/Issue:00/Revision:00
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
SQL RIGHT JOIN Keyword
The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.
SQL RIGHT JOIN Syntax
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
SQL RIGHT JOIN Example
SELECT Orders.OrderID, Employees.FirstName
FROM Orders
RIGHT JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID
ORDER BY Orders.OrderID;
SQL FULL OUTER JOIN Keyword
The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table (table2).
The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.
SQL FULL OUTER JOIN Syntax
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
SQL FULL OUTER JOIN Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
Result:
Thus the sqljoin are created and executed successfully.