Professional Documents
Culture Documents
RECORD NOTEBOOK
KPR INSTITUTE OF ENGINEERING AND TECHNOLGY
COIMBATORE – 641 407
§
LABORATORY RECORD
Name : ……………………………………………………………….
Department : ……………………………………………………………….
Register Number………………………………………………………
Place:
Date:
He/She has submitted the record for the University Practical Examination held on
……………………
PAGE
S.NO Date NAME OF THE EXERCISE Marks Sign
NO.
7 Creating Database.
9 PL/SQL Procedures.
10 PL/SQL Functions.
11 Triggers
1
1. DDL COMMANDS
AIM:
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
Example:
INPUT::
SQL> CREATE TABLE DEPARTMENT ( DEPTID VARCHAR(5) CONSTRAINT PKEY PRIMARY KEY,
SQL> CREATE TABLE EMPLOYE ( EMPID VARCHAR(5) CONSTRAINT PKEY PRIMARY KEY,
SALARY NUMBER(7,2));
2
SQL>desc Employe;
EMPNAME VARCHAR2(20)
HIREDATE DATE
DEPTID VARCHAR2(5)
SALARY NUMBER(7,2)
INPUT::
INPUT::
RESULT:
Thus the table was successfully created, altered, truncated, and dropped.
3
2. DML COMMANDS
AIM:
To insert, select, update and delete records in a table using DML commands.
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
Listing the selected records which satisfies the given condition from a table
Single-row insert
Inserting one row, with values to the column in the given order
OTHER EXAMPLES:
INPUT::
4
INPUT::
OUTPUT:
1 row created.
SQL - UPDATE
INPUT::
1 row updated.
INPUT::
RESULT:
Thus the table was successfully inserted, updated, select, and deleted.
5
3. TCL COMMANDS
AIM:
TCL COMMANDS:
COMMANDS OVERVIEW
SAVEPOINT
Syntax: savepoint username;
INPUT::
SQL> savepoint emp;
Output:
savepoint created;
COMMIT
Syntax: commit;
INPUT::
SQL> commit;
Output:
Commit complete.
ROLLBACK
Syntax:
rollback to savepoint_text_identifier;
INPUT::
SQL> rollback to emp;
Output:
Rollback complete.
RESULT:
6
4. WRITING AND PRACTICE OF SIMPLE QUERIES
AIM:
PROBLEM STATEMENTS:
OUTPUT:
Name Null? Type
-------------------------------- ----------------------- -------------------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(3)
AGE NUMBER(3)
ESAL NUMBER(10)
OUTPUT:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO AGE ESAL
-------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------- -----------------
7369 SMITH CLERK 7902 17-DEC-10 8000 0 20 25 0
7499 ALLEN SALESMAN 7698 20-FEB-11 16000 300 30 25 0
7521 WARD SALESMAN 7698 22-FEB-11 12500 500 30 25 0
7566 JONES MANAGER 7839 02-APR-11 29705 500 20 25 0
7698 BLAKE MANAGER 7839 01-MAY-12 28500 1400 30 25 0
3. 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
----------
ALLEN
JONES
BLAKE
CLARK
4 rows selected.
7
4. List all employee names and their and their manager whose manager is 7902 or 7566 0r 7789.
OUTPUT:
ENAME
-------
SCOTT
FORD
INPUT: SQL> SELECT ENAME FROM EMP WHERE ENAME LIKE ‘J%’ OR ENAME LIKE ‘T%’;
OUTPUT:
ENAME
---------
JONES
TURNER
JAMES
6. 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
---------- ---------
JONES MANAGER
BLAKE MANAGER
CLARK MANAGER
KING PRESIDENT
OUTPUT:
JOB
---------
ANALYST
CLERK
MANAGER
PRESIDENT
SALESMAN
assistant
clerk
7 rows selected.
8
OUTPUT:
ENAME
----------
SMITH
JONES
CLARK
SCOTT
KING
ADAMS
FORD
MILLER
8 rows selected.
RESULT:
ENAME SAL SAL+0.15*SAL
---------- ---------- ------------
SMITH 800 920
ALLEN 1600 1840
WARD 1250 1437.5
JONES 2975 3421.25
MARTIN 1250 1437.5
BLAKE 2850 3277.5
CLARK 2450 2817.5
7 rows selected.
OUTPUT:
11. Find how many job titles are available in employee table.
OUTPUT:
COUNT(DISTINCTJOB)
------------------
7
12. What is the difference between maximum and minimum salaries of employees in the organization?
OUTPUT:
MAX(SAL)-MIN(SAL)
9
-----------------
4997
13. 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 >
OUTPUT:
ENAME SAL
---------- ----------
JONES 2975
BLAKE 2850
CLARK 2450
14. Find how much amount the company is spending towards salaries.
OUTPUT:
SUM(SAL)
---------
32928
OUTPUT :
ENAME
----------
SMITH
JONES
SCOTT
ADAMS
OUTPUT:
ENAME
------------
CLARK
SCOTT
KING
ADAMS
JAMES
FORD
6 rows selected.
10
17. Find no.of dept in employee table.
OUTPUT:
COUNT(DISTINCT ENAME)
---------------------------------------
17
18. 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.
19. Calculate the experience in years of each programmer and display along with their name in
descending order.
RESULT:
Thus the given problem statements were solved using simple queries.
11
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.
PROBLEM STATEMENTS:
Programmer(pname, deptname,doj,job,gender,salary,mgrid)
Study(pname, institute,city)
Software(title,dname,dlan,dcost,scost)
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
OUTPUT:
SPLACE PNAME
------------ -------------
SAHBHARI MARY
2. Find the salary and institute of a person who developed the highest selling package.
INPUT: SQL> SELECT STUDY.PNAME, SAL, SPLACE FROM STUDY, PROGRAMMER WHERE
12
SCOST = (SELECT MAX (SCOST) FROM SOFTWARE));
OUTPUT:
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);
INPUT: SQL> SELECT P.PNAME, P.SAL FROM PROGRAMMER P, PROGRAMMER T WHERE P.PNAME <>
INPUT: SQL>SELECT PNAME,SAL FROM PROGRAMMER T WHERE PNAME<>T.PNAME AND SAL= T.SAL;
13
PRESIDENT 5000
SALESMAN 5600
assistant 2200
clerk 2003
7 rows selected.
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
OUTPUT:
PNAME SUM(SCOST)
-------------------- ----------
john 12000
kamala 12000
raju 12333
3 rows selected.
OUTPUT:
PNAME COUNT(TITLE)
-------------------- ------------
14
john 1
kamala 1
raju 1
ramana 1
rani 1
5 rows selected.
12. Display the number of packages in each language for which the development cost is less than
thousand.
INPUT: SQL>SELECT DEVIN, COUNT(TITLE) FROM SOFTWARE WHERE DCOST < 1000 GROUP BY DEVIN;
OUTPUT:
DEVIN COUNT(TITLE)
---------- ------------
cobol 1
13. 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);
OUTPUT:
SOLD
---------
11
INPUT: SQL>SELECT TITLE FROM SOFTWARE WHERE DEVIN = ‘PASCAL’ AND DCOST = (SELECT
MAX(DCOST) FROM SOFTWARE WHERE DEVIN = ‘PASCAL’);
OUTPUT:
no rows selected
15
INPUT: SQL>SELECT DEVIN, COUNT (*) FROM SOFTWARE GROUP BY DEVIN HAVING COUNT(*)
OUTPUT:
DEVIN COUNT(*)
---------- ----------
jsp 2
16.Who are the male programmers earning below the average salary of female programmers?
INPUT: SQL>SELECT PNAME FROM PROGRAMMER WHERE SAL < (SELECT AVG(SAL) FROM PROGRAMMER
WHERE SEX = ‘F’) AND SEX = ‘M’;
OUTPUT:
PNAME
--------------------
vijay
17. Display the details of software developed by the male programmers earning more than 3000/-.
INPUT: SQL> SELECT PROGRAMMER.PNAME, TITLE, DEVIN FROM PROGRAMMER, SOFTWARE WHERE SAL
> 3000 AND SEX = ‘M’ AND PROGRAMMER.PNAME = SOFTWARE.PNAME;
OUTPUT:
no rows selected
18. Display the details of software developed in c language by female programmers of Pragathi.
INPUT: SQL>SELECT SOFTWARE.PNAME, TITLE, DEVIN, SCOST, DCOST, SOLD FROM PROGRAMMER,
SOFTWARE, STUDY WHERE DEVIN = ‘C’ AND SEX =’F’ AND SPLACE = ‘PRAGATHI’ AND
PROGRAMMER.PNAME = SOFTWARE.PNAME AND SOFTWARE.PNAME = STUDY.PNAME;
16
RESULT:
Thus the given problem statements were solved using nested queries and group by & other
clauses.
AIM:
To solve the problem statements based on views, synonym, sequence and Indexes.
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. Its 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:
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.
17
SYNTAX:
CREATE INDEX <index_name> ON <table_name(attribute)>
PROBLEM STATEMENTS:
1. Create a view from single table containing all columns from the base table.
SQL>CREATE VIEW XYZ AS SELECT * FROM PROGRAMMER FULL NATURAL JOIN SOFTWARE;
SQL> CREATE VIEW LMN AS (SELECT PROGRAMMER, PNAME, TITLE, DEVIN FROM PROGRAMMER,
SOFTWARE WHERE SAL < 3000 AND PROGRAMMER.PNAME = SOFTWARE.PNAME);
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.
18
1 row deleted.
View dropped.
1 row created.
1 row Updated.
4. Create a sequence to generate unique value for empid field in the employee table while inserting.
INCREMENT BY 1
NOCACHE
NOCYCLE;
OUTPUT:
Sequence Created.
1 row created;
OUTPUT:
Index Created.
19
RESULT:
Thus the given problem statements based on views, synonyms, sequences and indexes were
solved.
7. CREATING DATABASE
AIM:
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
};
PROBLEM STATEMENT:
20
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:
PL/SQL BLOCK:
PL/SQL is a block-structured language, meaning that PL/SQL programs are divided and written in
logical blocks of code. Each block consists of three sub-parts:
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.
SYNTAX:
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;
21
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:
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;
22
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;
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;
23
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 KALIS 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;
END;
OUTPUT:
SQL>/
Enter value for eno1: 105
24
old 11: ENO1:=&ENO1;
new 11: ENO1:=105;
Enter value for basic1: 8000
old 14: BASIC1:=&BASIC1;
new 14: BASIC1:=8000;
3. Create a PL/SQL block for showing the new netpay after getting new basic salary for a particular
employee. Display the new netpay along the old netpay without updating in the table. Also If no
customer found with the given customer id then show appropriate error message.
INPUT:
DECLARE
ENO employe.emp_id%type;
BASICNW employe.basic%type;
HRANW employe.HRA%type;
DANW employe.DA%type;
PFNW employe.pf%type;
NETPAY employe.net%type;
NETPAYNW employe.net%type;
BEGIN
ENO:=&ENO;
BASICNW:=&BASICNW;
HRANW:=(BASICNW*50)/100;
DANW:=(BASICNW*20)/100;
PFNW:=(BASICNW*7)/100;
NETPAY1:=BASICNW+HRA1+DA1-PF1;
SELECT NET INTO NETPAY FROM EMPLOYE WHERE EMP_ID=ENO;
DBMS_OUTPUT.PUT_LINE(EN0|| ‘ ‘ || NETPAY || ‘ ‘ || NETPAY1);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('NO SUCH EMPLOYEE.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('ERROR');
END;
25
RESULT:
Thus PL/SQL statements were written for inserting & updating the rows in the EMPLOYE table.
9. PROCEDURES
AIM:
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 behaviour: 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 very simple to swap out one database and swap in another one.
SYNTAX:
BEGIN
<executable_section>
EXCEPTION
26
<exception_section>
END;
PROBLEM STATEMENTS:
REFERRED TABLES:
1. Write a procedure to insert a record in borrower relation. Before inserting check whether the book
is available or not.
IS
CNT NUMBER(5);
BEGIN
IF(CNT=0)
THEN
ELSE
END IF;
END;
OUTPUT:
SQL> @ e:\proc.sql;
Procedure Created.
ACC_NOROLLNO DOI
27
---------- ------------ ------------
2. Write a procedure to insert a record in borrower relation with the above constraints and also
ensure that the member has not borrowed more than three books.
IS
CNT NUMBER(5);
BEGIN
IF(CNT=0)
THEN
IF(CNT1<4)
THEN
END IF;
ELSE
END IF;
END;
OUTPUT:
SQL> @ e:\proc.sql;
Procedure Created.
28
Result:
Thus PL/SQL Procedures were created to solve the given problem statements.
10.FUNCTIONS
AIM:
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
END;
PROBLEM STATEMENT:
REFERRED TABLES:
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.
RETURN NUMBER
29
IS
BALANCE NUMBER;
BEGIN
RETURN BALANCE;
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);
END;
OUTPUT:
SQL> @E:\SQL\FUNCPLSQL.SQL;
30
RESULT 26000
RESULT:
Thus PL/SQL functions were created to solve the given problem statements.
11.TRIGGERS
AIM:
To solve the given problem statements using triggers.
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.
Benefits of Triggers
Generating some derived column values automatically
Enforcing referential integrity
Event logging and storing information on table access
Auditing
Synchronous replication of tables
Imposing security authorizations
Preventing invalid transactions.
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]
31
WHEN < condition >
DECLARE
< Declaration-statements >
BEGIN
< Executable-statements >
EXCEPTION
< Exception-handling-statements >
END;
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.
DECLARE
BEGIN
END;
OUTPUT:
SQL> @e:/plsql/accnt_trig.sql
Trigger Created.
1 Row inserted.
32
AC010 27-AUG-2013 12,000
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.
DECLARE
BEGIN
IF(:NEW.BALANCE<0)
THEN
END IF;
END;
OUTPUT:
SQL> @e:/plsql/loan_trig.sql
Trigger Created.
1 Row inserted.
33
Result:
Thus the triggers were created accordingly to solve the given problem statements.
12.MINI PROJECT
PAY ROLL PROCESSING
AIM
To create a database for payroll processing system using SQL and implement it using VB
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 orcl1 and give the user id and password.
Create necessary table to store the data
Write appropriate Program VB to connect it with database.
Report Generation
In a new Project select the Add Data Environment designer from the Project menu.
In project explorer select Data Environment1 connection1 will appear.
Select connection1 and press Right Click and select Properties.
Select Microsoft OLEDB provider for Oracle and press Next.
Enter Server name, User name and Password and choose allow saving password.
Click Test Connection to check whether the connection is established with Oracle and click
Ok button.
Select connection and press Right Click and Choose Add Command.
In Properties of command1 set the database object as table and the object name as the
desired table which is used for reports and click Ok.
Select project menu and click Add Reports.
In the Data Report property change Data Source as Data Environment1 and Data Member
as Command1
34
Drag command1 and drop it in the detail section of Data Report.
Select Project Properties and change the start from object to Data Report.
Run the application to View the Report.
SAMPLE CODING
//FORM 1
Private Sub emp_Click()
Form2.Show
End Sub
Private Sub exit_Click()
Unload Me
End Sub
Private Sub salary_Click()
Form3.Show
End Sub
//FORM 2
Private Sub add_Click()
Adodc1.Recordset.AddNew
MsgBox "Record added"
End Sub
Private Sub clear_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
End Sub
Private Sub delte_Click()
Adodc1.Recordset.Delete
MsgBox "Record Deleted"
If Adodc1.Recordset.EOF = True Then
Adodc1.Recordset.MovePrevious
End If
End Sub
Private Sub exit_Click()
Unload Me
End Sub
Private Sub main_Click()
Form1.Show
End Sub
Private Sub modify_Click()
Adodc1.Recordset.Update
35
End Sub
//FORM 3
Private Sub add_Click()
Adodc1.Recordset.AddNew
MsgBox "Record added"
End Sub
Private Sub clear_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
End Sub
Private Sub delte_Click()
Adodc1.Recordset.Delete
MsgBox "Record Deleted"
If Adodc1.Recordset.EOF = True Then
Adodc1.Recordset.MovePrevious
End If
End Sub
Private Sub exit_Click()
Unload Me
End Sub
Private Sub main_Click()
Form1.Show
End Sub
Private Sub modify_Click()
Adodc1.Recordset.Update
End Sub
SAMPLE OUTPUT
36
37
38
39
RESULT
Thus the design and implementation of payroll processing system using SQL, VB was
successfully done
BANKING SYSTEM
AIM
To develop a mini project for banking system.
PROCEDURE:
Create the DB for banking system source 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 orcl1 and give the user id and password.
Create necessary table to store the data
Add ADODC project select component and check ms ADO data control click ok
Click customs and property window and window will appear and select ODBC data source
name as oracle and click apply as the some window
SAMPLE CODING
//FORM 1
Private Sub ACCOUNT_Click()
Form2.Show
End Sub
Private Sub EXIT_Click()
Unload Me
End Sub
40
Private Sub TRANSACTION_Click()
Form3.Show
End Sub
//FORM 2
Private Sub CLEAR_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
Private Sub DELETE_Click()
Adodc1.Recordset.DELETE
MsgBox "record deleted"
Adodc1.Recordset.MoveNext
If Adodc1.Recordset.EOF = True Then
Adodc1.Recordset.MovePrevious
End If
End Sub
Private Sub EXIT_Click()
Unload Me
End Sub
Private Sub HOME_Click()
Form1.Show
End Sub
Private Sub INSERT_Click()
Adodc1.Recordset.AddNew
End Sub
Private Sub TRANSACTION_Click()
Form3.Show
End Sub
Private Sub UPDATE_Click()
Adodc1.Recordset.UPDATE
MsgBox "record updated successfully"
End Sub
//FORM 3
Private Sub ACCOUNT_Click()
Form2.Show
End Sub
Private Sub CLEAR_Click()
Text1.Text = ""
Text2.Text = ""
End Sub
Private Sub DEPOSIT_Click()
Dim s As String
s = InputBox("enter the amount to be deposited")
Text2.Text = Val(Text2.Text) + Val(s)
A = Text2.Text
MsgBox "CURRENT BALANCE IS Rs" + Str(A)
Adodc1.Recordset.Save
Adodc1.Recordset.UPDATE
End Sub
41
Private Sub EXIT_Click()
Unload Me
End Sub
Private Sub HOME_Click()
Form1.Show
End Sub
Private Sub WITHDRAW_Click()
Dim s As String
s = InputBox("enter the amount to be deleted")
Text2.Text = Val(Text2.Text) - Val(s)
A = Text2.Text
MsgBox "current balance is Rs" + Str(A)
Adodc1.Recordset.Save
Adodc1.Recordset.UPDATE
End
OUTPUT
42
43
44
RESULT
Thus the mini project for banking system by using VB,SQL was done.
45
LIBRARY MANAGEMENT SYSTEM
AIM
To develop an application software for Library Management System.
PROCEDURE
Create the DB for library management system source 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 orcl1 and give the user id and password.
Create necessary table to store the data
Add ADODC project select component and check ms ADO data control click ok
Click customs and property window and window will appear and select ODBC data source
name as oracle and click apply as the some window
SAMPLE CODING
//FORM 1
Private Sub Command1_Click()
Me.Hide
Load Form5
Form5.Visible = True
End Sub
Private Sub Command2_Click()
Me.Hide
Load Form3
Form3.Visible = True
End Sub
Private Sub Command3_Click()
Me.Hide
Load Form4
Form4.Visible = True
End Sub
Private Sub Command4_Click()
End
End Sub
//FORM 2:
Private Sub Command1_Click()
Data1.Recordset.AddNew
Data1.Recordset.Fields("roll_no") = Val(Text1.Text)
Data1.Recordset.Fields("name") = Text2.Text
Data1.Recordset.Fields("dep") = Text3.Text
Data1.Recordset.Fields("year") = Val(Text4.Text)
Data1.Recordset.Update
End Sub
46
Private Sub Command2_Click()
b = MsgBox("Are you sure u want to delete it...", vbOKCancel + vbExclamation)
If b = 1 Then
Data1.Recordset.Delete
Data1.Recordset.MoveNext
MsgBox "Record is deleted"
Else
End If
End Sub
Private Sub Command3_Click()
a = InputBox("Enter the student roll number", roll_no)
Data1.Recordset.MoveFirst
On Error GoTo jvm
While Not Data1.Recordset.Fields("roll_no") = Val(a)
Data1.Recordset.MoveNext
End
End Sub
Sub Command5_Click()
End
End Sub
Private Sub Command6_Click()
Unload Me
Load Form1: Form1.Visible = True
End Sub
FORM 3:
Private Sub Command1_Click()
Data1.Recordset.AddNew
Data1.Recordset.Fields("book_id") = Val(Text1.Text)
Data1.Recordset.Fields("name") = Text2.Text
Data1.Recordset.Fields("author") = Text3.Text
Data1.Recordset.Fields("copies") = Val(Text4.Text)
Data1.Recordset.Update
End Sub
Private Sub Command2_Click()
b = MsgBox("Are you sure u want to delete it...", vbOKCancel + vbExclamation)
If b = 1 Then
Data1.Recordset.Delete
Data1.Recordset.MoveNext
MsgBox "Record is deleted"
Else
End If
End Sub
Private Sub Command3_Click()
Dim a As String
a = InputBox("Enter the book name", book_name)
Data1.Recordset.MoveFirst
On Error GoTo jvm
Do Until Data1.Recordset.EOF
If Data1.Recordset("book_name") = a Then
Text1 = Data1.Recordset.Fields("book_id")
47
MsgBox "The book id is " + Data1.Recordset.Fields("book_id") + " It has " +
Data1.Recordset.Fields("copies")
End If
Data1.Recordset.MoveNext
Loop
jvm:
End Sub
Private Sub Command5_Click()
End
End Sub
Private Sub Command6_Click()
Unload Me
Load Form1: Form1.Visible = True
End Sub
Private Sub Text6_Change()
If Text6.Text = 0 Then
MsgBox "No copies Available"
End If
End Sub
FORM 4:
Dim x As Date
Private Sub Command1_Click()
Data1.Recordset.AddNew
Data1.Recordset.Fields("roll_no") = Val(Text1.Text)
Data1.Recordset.Fields("name") = Text2.Text
Data1.Recordset.Fields("book_id") = Val(Text3.Text)
Data1.Recordset.Fields("book_name") = Text4.Text
Data1.Recordset.Fields("curr_date") = Val(Text5.Text)
Data1.Recordset.Fields("date_of_return") = Val(Text6.Text)
Data1.Recordset.Update
End Sub
Private Sub Command2_Click()
b = MsgBox("Are you sure u want to delete it...", vbOKCancel + vbExclamation)
If b = 1 Then
Data1.Recordset.Delete
Data1.Recordset.MoveNext
MsgBox "Record is deleted"
Else
End If
End Sub
Private Sub Command3_Click()
a = InputBox("Enter the student roll number", roll_no)
Data1.Recordset.MoveFirst
On Error GoTo jvm
While Not Data1.Recordset.Fields("roll_no") = Val(a)
Data1.Recordset.MoveNext
End
End Sub
Private Sub Command4_Click()
Text6.Text = DateValue(Text5) + 15
48
End Sub
Private Sub Command5_Click()
End
End Sub
Private Sub Command6_Click()
Unload Me
Load Form1: Form1.Visible = True
End Sub
Private Sub Command7_Click()
Dim n As Double
n = (DateValue(Text7) - DateValue(Text5))
If n > 15 Then
n = n - 15
n=n/2
Text8 = n
End If
End Sub
Private Sub Text5_Click()
If Val(Text4.Text) = 0 Then
MsgBox "No copies Available"
End If
End Sub
FORM 5:
Private Sub Command1_Click()
Form2.Show
End Sub
Private Sub Command2_Click()
Form6.Show
End Sub
Private Sub Command3_Click()
Form1.Show
End Sub
FORM 6:
Private Sub Command1_Click()
Form1.Show
End Sub
SAMPLE OUTPUT
49
50
51
52
53
RESULT
Thus the mini project for library management system by using VB, oracle was done.
54