Académique Documents
Professionnel Documents
Culture Documents
EX: NO: 1
Data Definition, Table Creation, Constraints
AIM:
To execute and verify the Data Definition Language commands and constraints
PROCEDURE:
STEP 1: Start
STEP 2: Create the table with its essential attributes.
STEP 3: Execute different Commands and extract information from the table.
STEP 4: Stop
QUERY: 01-Write a query to create a table employee with empno, ename, designation & salary.
Syntax for creating a table:
J.K.K.Nattraja College of Engineering and Infromation Technology
Technology
Page 1
Database Management Systems Lab IT-IV-SEM
QUERY: 02-Write a query to display the column name and datatype of the table employee.
Syntax for describe the table:
SQL: DESC <TABLE NAME>;
SQL> DESC EMP;
Name Null? Type
----------------------------------------- -------- -------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUERY: 03-Write a query for create a from an existing table with all the fields
Syntax For Create A from An Existing Table With All Fields
SQL> CREATE TABLE <TRAGET TABLE NAME> SELECT * FROM
<SOURCE TABLE NAME>;
SQL> CREATE TABLE EMP1 AS SELECT * FROM EMP;
Table created.
SQL> DESC EMP1
Name Null? Type
----------------------------------------- -------- ------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUERY: 04-Write a query for create a from an existing table with selected fields
J.K.K.Nattraja College of Engineering and Infromation Technology
Technology
Page 2
Database Management Systems Lab IT-IV-SEM
QUERY: 05 Write a query for create a new table from an existing table without any record.
Syntax for create a new table from an existing table without any record:
SQL> CREATE TABLE <TRAGET TABLE NAME> AS SELECT * FROM
<SOURCE TABLE NAME> WHERE <FALSE CONDITION>;
SQL> CREATE TABLE EMP3 AS SELECT * FROM EMP WHERE 1>2;
Table created.
SQL> DESC EMP3;
Name Null? Type
----------------------------------------- -------- ----------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2);
EMPNO NUMBER(6)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUERY: 07-Write a Query to Alter the table employee with multiple columns (EMPNO,
ENAME.)
Syntax for alter table with multiple column:
SQL > ALTER <TABLE NAME> MODIFY <COLUMN NAME1> <DATATYPE>
(SIZE), MODIFY <COLUMN NAME2> <DATATYPE> (SIZE) ………………….;
SQL>ALTER TABLE EMP MODIFY (EMPNO NUMBER (7), ENAME VARCHAR2(12));
Table altered.
Name Null? Type
----------------------------------------- -------- ----------------------
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2);
REMOVE / DROP
Unique
Not null
Null
Default
CONSTRAINTS CAN BE CREATED IN THREE WAYS:
Child Table:
Syntax for Column level constraints Using foreign key:
SQL:>CREATE <OBJ.TYPE> <OBJ.NAME> (COLUMN NAME.1 <DATATYPE>
(SIZE), COLUMN NAME2 <DATATYPE> (SIZE) REFERENCES <TABLE NAME>
(COLUMN NAME> ……………………………);
SQL>CREATE TABLE DEPT(DEPTNO NUMBER(2) PRIMARY KEY, DNAME
VARCHAR2(20), LOCATION VARCHAR2(15));
SQL>CREATE TABLE EMP4 (EMPNO NUMBER(3), DEPTNO NUMBER(2)
REFERENCES DEPT(DEPTNO), DESIGN VARCHAR2(10));
KEY(DEPTNO)REFERENCES DEPT(DEPTNO);
Check constraint
Column Level Check Constraint
QUERY:21 Write a query to create Check constraints with column level
Syntax for clumn level constraints using Check:
SQL:>CREATE <OBJ.TYPE> <OBJ.NAME> (COLUMN NAME.1 <DATATYPE>
(SIZE) CONSTRAINT <CONSTRAINTS NAME> <TYPE OF CONSTRAINTS>
(CONSTRAITNS CRITERIA) , COLUMN NAME2 <DATATYPE> (SIZE));
SQL>CREATE TABLE EMP7(EMPNO NUMBER(3), ENAME VARCHAR2(20),
Unique Constraint
Column Level Constraint
QUERY:24 Write a query to create unique constraints with column level
Syntax for Column level constraints with Unique:
SQL :> CREATE <OBJ.TYPE> <OBJ.NAME> (<COLUMN NAME.1> <DATATYPE>
(SIZE) CONSTRAINT <NAME OF CONSTRAINTS> <CONSTRAINT TYPE>, (COLUMN
NAME2 <DATATYPE> (SIZE)) ;
SQL>CREATE TABLE EMP10(EMPNO NUMBER(3), ENAME VARCHAR2(20), DESGIN
VARCHAR2(15)CONSTRAINT EMP10_DESIGN_UK UNIQUE, SAL NUMBER(5));
Constraint Enable
SQL>ALTER TABLE EMP13 DISABLE CONSTRAINT EMP13_ENAME_NN NULL;
QUERY:30 Write a query to enable the constraints
Syntax for disabling a single constraint in a table:
SQL>ALTER TABLE <TABLE-NAME> DISABLE CONSTRAINT <CONSTRAINTNAME>
SQL>ALTER TABLE EMP13 ENABLE CONSTRAINT EMP13_ENAME_NN NULL;
Result
Thus the Data Definition Language commands and constraints has been executed and
verified successfully.
EX: NO: 2
Insert, Select Commands, Update & Delete Commands
AIM:
To execute and verify the DML and TCL Language commands
DML (DATA MANIPULATION LANGUAGE)
SELECT
INSERT
DELETE
UPDATE
TCL (TRANSACTION CONTROL LANGUAGE)
COMMIT
ROLL BACK
SAVE POINT
PROCEDURE
STEP 1: Start
STEP 2: Create the table with its essential attributes.
STEP 3: Insert the record into table
STEP 4: Update the existing records into the table
STEP 5: Delete the records in to the table
STEP 6: Use save point if any changes occur in any portion of the record to undo its original
state.
STEP 7: use rollback for completely undo the records
STEP 6: use commit for permanently save the records.
SQL COMMANDS
1. COMMAND NAME: INSERT
INSERT command is used to Insert objects in the database.
2. COMMAND NAME: SELECT
SELECT command is used to SELECT the object from the database.
3. COMMAND NAME: UPDATE
UPDATE command is used to UPDATE the records from the table
4. COMMAND NAME: DELETE
DELETE command is used to DELETE the Records form the table
5. COMMAND NAME: COMMIT
COMMIT command is used to save the Records.
6. COMMAND NAME: ROLLBACK
ROLL BACK command is used to undo the Records.
7. COMMAND NAME: SAVE POINT
SAVE POINT command is used to undo the Records in a particular transaction.
INSERT
SQL> /
Enter value for empno: 103
Enter value for ename: PANNERSELVAM
Enter value for designatin: ASST. PROF
Enter value for salary: 20000
old 1: INSERT INTO EMP VALUES(&EMPNO,'&ENAME','&DESIGNATIN','&SALARY')
new 1: INSERT INTO EMP VALUES(103,'PANNERSELVAM','ASST. PROF','20000')
1 row created.
SQL> /
Enter value for empno: 104
J.K.K.Nattraja College of Engineering and Infromation Technology
Technology Page
16
Database Management Systems Lab IT-IV-SEM
UPDATE
QUERY: 04 Write a query to update the records from employee.
Syntax for update Records from the table:
SQL> UPDATE <<TABLE NAME> SET <COLUMNANE>=<VALUE> WHERE <COLUMN
NAME=<VALUE>;
SQL> UPDATE EMP SET SALARY=16000 WHERE EMPNO=101;
1 row updated.
DELETE
QUERY: 06 Write a query to delete records from employee.
Syntax for delete Records from the table:
SQL> DELETE <TABLE NAME> WHERE <COLUMN NAME>=<VALUE>;
SQL> DELETE EMP WHERE EMPNO=103;
1 row deleted.
ROLL BACK
QUERY: 08 Write a query to implement the Rollback.
Syntax for RollBack:
SQL> ROLL BACK <SAVE POINT NAME>;
SQL> ROLL BACK S1;
Rollback complete.
SQL> SELECT * FROM EMP;
EMPNO ENAME DESIGNATIN SALARY
---------- ------------ ---------- ----------
101 NAGARAJAN LECTURER 16000
102 SARAVANAN ASST. PROF 16000
103 PANNERSELVAM ASST. PROF 20000
104 CHINNI HOD, PROF 45000
COMMIT
QUERY: 09 write a query to implement the Commit.
Syntax for commit:
SQL> COMMIT;
SQL> COMMIT;
Commit complete.
DCL (DATA CONTROL LANGUAGE)
CREATING A USER
SQL>CONNECT SYSTEM/MANAGER;
SQL>CREATE USER "USERNAME" IDENTIFIED BY "PASSWORD"
SQL>GRANT DBA TO "USERNAME"
J.K.K.Nattraja College of Engineering and Infromation Technology
Technology Page
20
Database Management Systems Lab IT-IV-SEM
SQL>CONNECT "USERNAME"/"PASSWORD";
EXAMPLE
CREATING A USER
SQL>CONNECT SYSTEM/MANAGER;
SQL>CREATE USER CSE2 IDENTIFIED BY CSECSE;
SQL>GRANT DBA TO CSE2;
SQL>CONNECT CSE2/CSECSE;
SQL>REVOKE DBA FROM CSE2;
4 rows selected.
SQL> select empno,ename,sal from emp where sal in (800,5000);
Result:
Thus the DML and TCL Language commands has been executed and verified
successfully.
Table -1
SQL> CREATE TABLE EMP2(EMPNO NUMBER(5), ENAME VARCHAR2(20), JOB
VARCHAR2(20), SAL NUMBER(6), MGRNO NUMBER(4), DEPTNO NUMBER(3));
SQL> INSERT INTO EMP2 VALUES(1001,'MAHESH','PROGRAMMER',15000,1560,200);
1 ROW CREATED.
SQL> INSERT INTO EMP2 VALUES(1002,'MANOJ','TESTER',12000,1560,200);
1 ROW CREATED.
SQL> INSERT INTO EMP2 VALUES(1003,'KARTHIK','PROGRAMMER',13000,1400,201);
1 ROW CREATED.
SQL> INSERT INTO EMP2 VALUES(1004,'NARESH','CLERK',1400,1400,201);
1 ROW CREATED.
SQL> INSERT INTO EMP2 VALUES(1005,'MANI','TESTER',13000,1400,200);
1 ROW CREATED.
SQL> INSERT INTO EMP2 VALUES(1006,'VIKI','DESIGNER',12500,1560,201);
1 ROW CREATED.
SQL> INSERT INTO EMP2 VALUES(1007,'MOHAN','DESIGNER',14000,1560,201);
1 ROW CREATED.
SQL> INSERT INTO EMP2 VALUES(1008,'NAVEEN','CREATION',20000,1400,201);
1 ROW CREATED.
SQL> INSERT INTO EMP2 VALUES(1009,'PRASAD','DIR',20000,1560,202);
1 ROW CREATED.
SQL> INSERT INTO EMP2 VALUES(1010,'AGNESH','DIR',15000,1400,200);
1 ROW CREATED.
SQL> SELECT *FROM EMP2;
EMPNO ENAME JOB SAL MGRNO DPTNO
---------- ---------- ---------- ---------- ---------- ----------
1001 MAHESH PROGRAMMER 15000 1560 200
1002 MANOJ TESTER 12000 1560 200
1003 KARTHIK PROGRAMMER 13000 1400 201
1004 NARESH CLERK 1400 1400 201
1005 MANI TESTER 13000 1400 200
1006 VIKI DESIGNER 12500 1560 201
1007 MOHAN DESIGNER 14000 1560 201
1008 NAVEEN CREATION 20000 1400 201
1009 PRASAD DIR 20000 1560 202
1010 AGNESH DIR 15000 1400 200
TABLE- 2
SQL> CREATE TABLE DEPT2(DEPTNO NUMBER(3), DEPTNAME VARCHAR2(10),
LOCATION VARCHAR2(15));
Table created.
J.K.K.Nattraja College of Engineering and Infromation Technology
Technology Page
25
Database Management Systems Lab IT-IV-SEM
SQL COMMANDS
1. COMMAND NAME: INNER JOIN
The INNER JOIN keyword return rows when there is at least one match in both tables.
2. COMMAND NAME LEFT JOIN
The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are
no matches in the right table (table_name2).
3. COMMAND NAME : RIGHT JOIN
The RIGHT JOIN keyword Return all rows from the right table (table_name2), even if there
are no matches in the left table (table_name1).
4. COMMAND NAME : FULL JOIN
The FULL JOIN keyword return rows when there is a match in one of the tables.
TABLE SECTION:
SQL> SELECT * FROM orders;
O_ID ORDERNO P_ID
-------- ---------- ----------
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15
INNER JOIN
INNTER JOIN SYNTAX
J.K.K.Nattraja College of Engineering and Infromation Technology
Technology Page
31
Database Management Systems Lab IT-IV-SEM
Result:
Thus the SQL command for Nested Queries and Join Queries has been executed and
verified successfully.
EX: NO: 4
VIEWS
AIM
To execute and verify the SQL commands for Views.
OBJECTIVE:
Views Helps to encapsulate complex query and make it reusable.
Provides user security on each view - it depends on your data policy security.
Using view to convert units - if you have a financial data in US currency, you can create view to
convert them into Euro for viewing in Euro currency.
PROCEDURE
STEP 1: Start
STEP 2: Create the table with its essential attributes.
STEP 3: Insert attribute values into the table.
STEP 4: Create the view from the above created table.
STEP 5: Execute different Commands and extract information from the View.
STEP 6: Stop
SQL COMMANDS
COMMAND NAME: CREATE VIEW
CREATE VIEW command is used to define a view.
COMMAND NAME: INSERT IN VIEW
INSERT command is used to insert a new row into the view.
COMMAND NAME: DELETE IN VIEW
DELETE command is used to delete a row from the view.
COMMAND NAME: UPDATE OF VIEW
UPDATE command is used to change a value in a tuple without changing all values in
the tuple.
COMMAND NAME: DROP OF VIEW
DROP command is used to drop the view table
COMMANDS EXECUTION
CREATION OF TABLE
--------------------------------
SQL> CREATE TABLE EMPLOYEE (EMPLOYEE_NAMEVARCHAR2(10),
EMPLOYEE_NONUMBER(8), DEPT_NAME VARCHAR2(10), DEPT_NO NUMBER
(5),DATE_OF_JOIN DATE);
Table created.
SQL> DESC EMPLOYEE;
NAME NULL? TYPE
------------------------------- -------- ------------------------
EMPLOYEE_NAME VARCHAR2(10)
EMPLOYEE_NO NUMBER(8)
DEPT_NAME VARCHAR2(10)
DEPT_NO NUMBER(5)
DATE_OF_JOIN DATE
SYNTAX FOR CREATION OF VIEW
--------------------------------------------------
SQL> CREATE <VIEW> <VIEW NAME> AS SELECT <COLUMN_NAME_1>,
<COLUMN_NAME_2> FROM <TABLE NAME>;
CREATION OF VIEW
------------------------------
SQL> CREATE VIEW EMPVIEW AS SELECT EMPLOYEE_NAME, EMPLOYEE_NO,
DEPT_NAME,DEPT_NO,DATE_OF_JOIN FROM EMPLOYEE;
VIEW CREATED.
DESCRIPTION OF VIEW
--------------------------------
SQL> DESC EMPVIEW;
NAME NULL? TYPE
----------------------------------------- -------- ----------------------------
EMPLOYEE_NAME VARCHAR2(10)
EMPLOYEE_NO NUMBER(8)
DEPT_NAME VARCHAR2(10)
DEPT_NO NUMBER(5)
DISPLAY VIEW:
----------------------
SQL> SELECT * FROM EMPVIEW;
EMPLOYEE_N EMPLOYEE_NO DEPT_NAME DEPT_NO
-------------------- ---------------------- ----------------- --------------
RAVI 124 ECE 89
VIJAY 345 CSE 21
RAJ 98 IT 22
GIRI 100 CSE 67
INSERTION INTO VIEW
----------------------------------
INSERT STATEMENT:
SYNTAX:
SQL> INSERT INTO <VIEW_NAME> (COLUMN NAME1,…) VALUES(VALUE1,….);
SQL> INSERT INTO EMPVIEW VALUES ('SRI', 120,'CSE', 67,'16-NOV-1981');
1 ROW CREATED.
SQL> SELECT * FROM EMPVIEW;
EMPLOYEE_N EMPLOYEE_NO DEPT_NAME DEPT_NO
------------------- ---------------------- ------------------ --------------
RAVI 124 ECE 89
VIJAY 345 CSE 21
RAJ 98 IT 22
GIRI 100 CSE 67
SRI 120 CSE 67
SQL> SELECT * FROM EMPLOYEE;
EMPLOYEE_N EMPLOYEE_NO DEPT_NAME DEPT_NO DATE_OF_J
--------------------- ---------------------- ------------------ -------------- ---------
RAVI 124 ECE 89 15-JUN-05
VIJAY 345 CSE 21 21-JUN-06
RAJ 98 IT 22 30-SEP-06
GIRI 100 CSE 67 14-NOV-81
SRI 120 CSE 67 16-NOV-81
DELETION OF VIEW:
DELETE STATEMENT:
SYNTAX:
SQL> DELETE <VIEW_NMAE>WHERE <COLUMN NMAE> =’VALUE’;
SQL> DELETE FROM EMPVIEW WHERE EMPLOYEE_NAME='SRI';
1 ROW DELETED.
SQL> SELECT * FROM EMPVIEW;
EMPLOYEE_N EMPLOYEE_NO DEPT_NAME DEPT_NO
--------------------- ----------------------- ------------------ ----------
RAVI 124 ECE 89
VIJAY 345 CSE 21
RAJ 98 IT 22
GIRI 100 CSE 67
UPDATE STATEMENT:
SYNTAX:
SQL>UPDATE <VIEW_NAME> SET< COLUMN NAME> = <COLUMN NAME> +
<VIEW> WHERE <COLUMNNAME>=VALUE;
SQL> UPDATE EMPKAVIVIEW SET EMPLOYEE_NAME='KAVI' WHERE
EMPLOYEE_NAME='RAVI';
1 ROW UPDATED.
SQL> SELECT * FROM EMPKAVIVIEW;
EMPLOYEE_N EMPLOYEE_NO DEPT_NAME DEPT_NO
-------------------- ---------------------- ------------------ ----------
KAVI 124 ECE 89
VIJAY 345 CSE 21
RAJ 98 IT 22
GIRI 100 CSE 67
DROP A VIEW:
SYNTAX:
SQL> DROP VIEW <VIEW_NAME>
EXAMPLE
SQL>DROP VIEW EMPVIEW;
J.K.K.Nattraja College of Engineering and Infromation Technology
Technology Page
39
Database Management Systems Lab IT-IV-SEM
VIEW DROPED
CREATE A VIEW WITH SELECTED FIELDS:
SYNTAX:
SQL>CREATE [OR REPLACE] VIEW <VIEW NAME>AS SELECT <COLUMN NAME1>
…..FROM <TABLE ANME>;
EXAMPLE-2:
SQL> CREATE OR REPLACE VIEW EMPL_VIEW1 AS SELECT EMPNO, ENAME,
SALARY FROM EMPL;
SQL> SELECT * FROM EMPL_VIEW1;
EXAMPLE-3:
SQL> CREATE OR REPLACE VIEW EMPL_VIEW2 AS SELECT * FROM EMPL WHERE
DEPTNO=10;
SQL> SELECT * FROM EMPL_VIEW2;
Note:
Replace is the keyboard to avoid the error “ora_0095: name is already used by an existing
object”.
CHANGING THE COLUMN(S) NAME M THE VIEW DURING AS SELECT
STATEMENT:
TYPE-1:
SQL> CREATE OR REPLACE VIEW EMP_TOTSAL (EID, NAME, SAL) AS SELECT
EMPNO, ENAME, SALARY FROM EMPL;
View created.
EMPNO ENAME SALARY
---------- -------------------- ---------- ----------
7369 SMITH 1000
7499 MARK 1050
7565 WILL 1500
7678 JOHN 1800
7578 TOM 1500
7548 TURNER 1500
6 rows selected.
J.K.K.Nattraja College of Engineering and Infromation Technology
Technology Page
40
Database Management Systems Lab IT-IV-SEM
View created.
EMPNO ENAME SALARY MGRNO DEPTNO
RESULT:
Thus the SQL commands for View has been verified and executed successfully.
EX: NO: 5
A CONTROL STRCTURE
AIM
To write a PL/SQL block using different control (if, if else, for loop, while loop,…)
statements.
OBJECTIVE:
PL/SQL Control Structure provides conditional tests, loops, flow control and branches that let to
produce well-structured programs.
Addition of Two Numbers:
1. Write a PL/SQL Program for Addition of Two Numbers
PROCEDURE
STEP 1: Start
STEP 2: Initialize the necessary variables.
STEP 3: Develop the set of statements with the essential operational parameters.
STEP 4: Specify the Individual operation to be carried out.
STEP 5: Execute the statements.
STEP 6: Stop.
PL/ SQL General Syntax
SQL> DECLARE
<VARIABLE DECLARATION>;
BEGIN
<EXECUTABLE STATEMENT >;
END;
PL/SQL CODING FOR ADDITION OF TWO NUMBERS
SQL> declare
a number;
b number;
c number;
begin
a:=&a;
b:=&b;
c:=a+b;
dbms_output.put_line('sum of'||a||'and'||b||'is'||c);
end;
/
INPUT:
Enter value for a: 23
old 6: a:=&a;
new 6: a:=23;
Enter value for b: 12
old 7: b:=&b;
new 7: b:=12;
OUTPUT:
sum of23and12is35
PL/SQL procedure successfully completed.
PL/ SQL Program for IF Condition:
2. Write a PL/SQL Program using if condition
PROCEDURE
STEP 1: Start
STEP 2: Initialize the necessary variables.
STEP 3: invoke the if condition.
STEP 4: Execute the statements.
STEP 5: Stop.
PL/ SQL GENERAL SYNTAX FOR IF CONDITION:
SQL> DECLARE
<VARIABLE DECLARATION>;
BEGIN
IF(CONDITION)THEN
begin
dbms_output. put_line('enter a number');
n:=&number;
if n<5 then
dbms_output.put_line('entered number is less than 5');
else
dbms_output.put_line('entered number is greater than 5');
end if;
end;
/
Input
Enter value for number: 2
old 5: n:=&number;
new 5: n:=2;
Output:
entered number is less than 5
PL/SQL procedure successfully completed.
PL/ SQL GENERAL SYNTAX FOR NESTED IF:
SQL> DECLARE
<VARIABLE DECLARATION>;
BEGIN
IF (TEST CONDITION) THEN
<STATEMENTS>;
ELSEIF (TEST CONDITION) THEN
<STATEMENTS>;
ELSE
<STATEMENTS>;
ENDIF;
END;
********** GREATEST OF THREE NUMBERS USING IF ELSEIF************
SQL> declare
a number;
b number;
c number;
d number;
begin
a:=&a;
b:=&b;
c:=&b;
if(a>b)and(a>c) then
dbms_output.put_line('A is maximum');
elsif(b>a)and(b>c)then
dbms_output.put_line('B is maximum');
else
dbms_output.put_line('C is maximum');
end if;
end;
/
INPUT:
Enter value for a: 21
old 7: a:=&a;
new 7: a:=21;
Enter value for b: 12
old 8: b:=&b;
new 8: b:=12;
Enter value for b: 45
old 9: c:=&b;
new 9: c:=45;
OUTPUT:
C is maximum
n number;
sum1 number default 0;
endvalue number;
begin
endvalue:=&endvalue;
n:=1;
for n in 1..endvalue
loop
if mod(n,2)=1
then
sum1:=sum1+n;
end if;
end loop;
dbms_output.put_line('sum ='||sum1);
end;
/
INPUT:
Enter value for endvalue: 4
old 6: endvalue:=&endvalue;
new 6: endvalue:=4;
OUTPUT:
sum =4
PL/SQL procedure successfully completed.
PL/ SQL GENERAL SYNTAX FOR LOOPING STATEMENT:
SQL> DECLARE
<VARIABLE DECLARATION>;
BEGIN
WHILE <condition>
LOOP
<STATEMENT>;
END LOOP;
<EXECUTAVLE STATEMENT>;
END;
*********SUMMATION OF ODD NUMBERS USING WHILE LOOP**********
SQL> declare
n number;
sum1 number default 0;
endvalue number;
begin
endvalue:=&endvalue;
n:=1;
while(n<endvalue)
loop
sum1:=sum1+n;
n:=n+2;
end loop;
dbms_output.put_line('sum of odd no. bt 1 and' ||endvalue||'is'||sum1);
end;
INPUT:
Enter value for endvalue: 4
old 6: endvalue:=&endvalue;
new 6: endvalue:=4;
OUTPUT:
sum of odd no. bt 1 and4is4
PL/SQL procedure successfully completed.
RESULT:
Thus the PL/SQL block for different controls are verified and executed.
EX: NO: 5B
PROCEDURES
AIM
To write a PL/SQL block to display the student name, marks whose average mark is
above 60%.
ALGORITHM
STEP1:Start
STEP2:Create a table with table name stud_exam
STEP3:Insert the values into the table and Calculate total and average of each student
STEP4: Execute the procedure function the student who get above 60%.
STEP5: Display the total and average of student
STEP6: End
EXECUTION
SETTING SERVEROUTPUT ON:
SQL> SET SERVEROUTPUT ON
I) PROGRAM:
PROCEDURE USING POSITIONAL PARAMETERS:
SQL> SET SERVEROUTPUT ON
SQL> CREATE OR REPLACE PROCEDURE PROC1 AS
2 BEGIN
3 DBMS_OUTPUT.PUT_LINE('Hello from procedure...');
4 END;
5/
Output:
Procedure created.
SQL> EXECUTE PROC1
Hello from procedure...
PL/SQL procedure successfully completed.
II) PROGRAM:
PROCEDURE USING NOTATIONAL PARAMETERS:
SQL> CREATE OR REPLACE PROCEDURE PROC2
2 (N1 IN NUMBER,N2 IN NUMBER,TOT OUT NUMBER) IS
3 BEGIN
4 TOT := N1 + N2;
5 END;
6/
Output:
Procedure created.
SQL> VARIABLE T NUMBER
SQL> EXEC PROC2(33,66,:T)
PL/SQL procedure successfully completed.
SQL> PRINT T
T
----------
99
PROCEDURE FOR GCD NUMBERS
III) PROGRAM:
SQL> create or replace procedure pro
is
a number(3);
b number(3);
c number(3);
d number(3);
begin
a:=&a;
b:=&b;
if(a>b) then
c:=mod(a,b);
if(c=0) then
J.K.K.Nattraja College of Engineering and Infromation Technology
Technology Page
56
Database Management Systems Lab IT-IV-SEM
dbms_output.put_line('GCD is');
dbms_output.put_line(b);
else
dbms_output.put_line('GCD is');
dbms_output.put_line(c);
end if;
else
d:=mod(b,a);
if(d=0) then
dbms_output.put_line('GCD is');
dbms_output.put_line(a);
else
dbms_output.put_line('GCD is');
dbms_output.put_line(d);
end if;
end if;
end;
/
Output:
Enter value for a: 8
old 8: a:=&a;
new 8: a:=8;
Enter value for b: 16
old 9: b:=&b;
new 9: b:=16;
Procedure created.
SQL> set serveroutput on;
SQL> execute pro;
GCD is
8
TOT:=STUDENT.MARK1+STUDENT.MARK2+STUDENT.MARK3+STUDENT.MARK4+S
TUDENT.MARK5;
AVE:=TOT/5;
DBMS_OUTPUT.PUT_LINE(STUDENT.REGNO||RPAD(STUDENT.NAME,15)
||RPAD(STUDENT.MARK1,6)||RPAD(STUDENT.MARK2,6)||RPAD(STUDENT.MARK3,6)
||RPAD(STUDENT.MARK4,6)||RPAD(STUDENT.MARK5,6)||RPAD(TOT,8)||RPAD(AVE,5))
;
END LOOP;
END;
/
SAMPLE OUTPUT
REGNO NAME M1 M2 M3 M4 M5 TOTAL AVG
------------ ---------------- ------ ------ ------- ---- ------- ---------- -------
101 PRIYA 78 88 77 60 89 393 79
102 SURYA 99 77 69 81 99 425 85
103 SURYAPRIYA 100 90 97 89 91 467 93
PL/SQL PROCEDURE SUCCESSFULLY COMPLETED.
EXPLICIT CURSORS AND EXPLICIT CURSORS IMPLEMENTATION
CREATING A TABLE EMP IN ORACLE
V) PROGRAM
SQL> select * from EMP;
EMPNO ENAME JOB MGR HIREDATE SAL COMM D_NO
---------- ---------- --------- ---------- --------- ---------- ---------- ---------
7369 SMITH CLERK 7 902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
J.K.K.Nattraja College of Engineering and Infromation Technology
Technology Page
60
Database Management Systems Lab IT-IV-SEM
RESULT:
Thus the PL/SQL block to display the student name,marks,average is verified and
executed.
EX: NO: 5C
FUNCTIONS
AIM
To write a Functional procedure to search an address from the given database.
PROCEDURE
STEP 1: Start
STEP 2: Create the table with essential attributes.
STEP 3: Initialize the Function to carryout the searching procedure..
STEP 4: Frame the searching procedure for both positive and negative searching.
STEP 5: Execute the Function for both positive and negative result .
STEP 6: Stop
EXECUTION
SETTING SERVEROUTPUT ON:
SQL> SET SERVEROUTPUT ON
IMPLEMENTATION OF FACTORIAL USING FUNCTION
I) PROGRAM:
SQL>CREATE FUNCTION FNFACT(N NUMBER)
RETURN NUMBER IS
B NUMBER;
BEGIN
B:=1;
FOR I IN 1..N
LOOP
B:=B*I;
END LOOP;
RETURN B;
END;
/
SQL>DECLARE
N NUMBER:=&N;
Y NUMBER;
BEGIN
Y:=FNFACT(N);
DBMS_OUTPUT.PUT_LINE(Y);
END;
/
Function created.
Enter value for n: 5
old 2: n number:=&n;
new 2: n number:=5;
120
PL/SQL procedure successfully completed.
II) PROGRAM
SQL> CREATE TABLE PHONEBOOK (PHONE_NO NUMBER (6) PRIMARY
KEY,USERNAME VARCHAR2(30),DOORNO VARCHAR2(10), STREET
VARCHAR2(30),PLACE VARCHAR2(30),PINCODE CHAR(6));
TABLE CREATED.
SQL> INSERT INTO PHONEBOOK VALUES(20312,'VIJAY','120/5D','BHARATHI
STREET','NGO COLONY','629002');
1 ROW CREATED.
SQL> INSERT INTO PHONEBOOK VALUES(29467,'VASANTH','39D4','RK
BHAVAN','SARAKKAL VILAI','629002');
1 ROW CREATED.
SQL> SELECT * FROM PHONEBOOK;
PHONE_NO USERNAME DOORNO STREET PLACE PINCODE
----------------- -------------- ------------- ---------------- ----------- ---------
20312 VIJAY 120/5D BHARA NGO 629002
THI ST COLONY
29467 VASANTH 39D4 RK SARAKKAL 629002
BHAVAN VILAI
OUTPUT2:
Address not found
PL/SQL procedure successfully completed.
RESULT:
Thus the Function for searching process has been executed successfully.
EX:NO:6
FRONT END TOOLS
AIM
To design a form using different tools in Visual Basic.
PROCEDURE
STEP 1: Start
STEP 2: Create the form with essential controls in tool box.
STEP 3: Write the code for doing the appropriate functions.
STEP 4: Save the forms and project.
STEP 5: Execute the form .
STEP 6: Stop
CODING:
Private Sub Calendar1_Click()
Text3.Text = Calendar1.Value
End Sub
Private Sub Combo1_Change()
Combo1.AddItem "BSC"
Combo1.AddItem "MSC"
Combo1.AddItem "BE"
Combo1.AddItem "ME"
End Sub
Private Sub Command1_Click()
List1.AddItem Text1.Text
List1.AddItem Text2.Text
If Option1.Value = True Then
gender = "male"
End If
If Option2.Value = True Then
gender = "female"
End If
List1.AddItem gender
List1.AddItem Text3.Text
If Check1.Value = 1 And Check2.Value = 1 Then
area = "software Engineering & Networks"
End If
If Check1.Value = 0 And Check2.Value = 1 Then
area = " Networks"
End If
List1.AddItem area
List1.AddItem Text4.Text
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Command3_Click()
If List1.ListIndex <> 0 Then
List1.RemoveItem (0)
End If
End Sub
Private Sub Form_Load()
Label10.Caption = Date$
MsgBox "Welcome to Registration"
End Sub
Private Sub Option1_Click()
If (Option1.Value = True) Then
MsgBox ("You have selected Male")
ElseIf (Option2.Value = True) Then
MsgBox ("You have selected Female")
End If
End Sub
REGISTRATION FORM:
RESULT:
EX: NO:7
FORM DESIGN
AIM
To design a Single Document Interface and Multiple Document Interface forms using
Visual Basic.
PROCEDURE
STEP 1: Start
STEP 2: Create the form with essential controls in tool box.
STEP 3: Write the code for doing the appropriate functions.
STEP 4: Save the forms and project.
STEP 5: Execute the form.
STEP 6: Stop
EXECUTION
Code for Dialog Menu:
Private Sub OKButton_Click()
If (Option1.Value = True) Then
SDI.Show
Unload Me
Else
MDIForm1.Show
Unload Me
End If
End Sub
Code for MDI Menu:
Private Sub ADD_Click()
MDIADD.Show
End Sub
Private Sub DIV_Click()
MDIDIV.Show
End Sub
a = Val(Text1.Text) * Val(Text2.Text)
MsgBox ("Addition of Two numbers is" + Str(a))
End Sub
Private Sub Command5_Click()
MDIForm1.Show
End Sub
Code for MDI SUB:
Private Sub Command1_Click()
Dim a As Integer
a = Val(Text1.Text) - Val(Text2.Text)
MsgBox ("Addition of Two numbers is" + Str(a))
End Sub
Private Sub Command5_Click()
MDIForm1.Show
End Sub
Code for SDI MENU:
Private Sub Command1_Click()
SDIADD.Show
End Sub
Private Sub Command2_Click()
SDIMUL.Show
End Sub
Private Sub Command3_Click()
SDIDIV.Show
End Sub
Private Sub Command4_Click()
SDISUB.Show
End Sub
Private Sub Command5_Click()
Dialog.Show
Unload Me
End Sub
Code for SDI ADD:
Private Sub Command1_Click()
Dim a As Integer
a = Val(Text1.Text) + Val(Text2.Text)
MsgBox ("Addition of Two numbers is" + Str(a))
Unload Me
End Sub
Private Sub Command5_Click()
SDI.Show
Unload Me
End Sub
Code for SDI DIV:
Private Sub Command2_Click()
a = Val(Text1.Text) / Val(Text2.Text)
MsgBox ("Addition of Two numbers is" + Str(a))
Unload Me
End Sub
Private Sub Command5_Click()
SDI.Show
Unload Me
End Sub
Code for SDI MUL:
Private Sub Command2_Click()
a = Val(Text1.Text) * Val(Text2.Text)
MsgBox ("Addition of Two numbers is" + Str(a))
Unload Me
End Sub
Private Sub Command5_Click()
SDI.Show
Unload Me
End Sub
Code for SDI SUB:
Private Sub Command2_Click()
a = Val(Text1.Text) - Val(Text2.Text)
MsgBox ("Addition of Two numbers is" + Str(a))
Unload Me
End Sub
Private Sub Command5_Click()
SDI.Show
Unload Me
End Sub
Output:
SDI MDI MENU:
SDI MENU:
SDI ADDITION:
SDI DIVISION:
SDI MULTIPLICATION:
SDI SUBRACTION:
MDI MENU:
MDI ADD:
MDI MUL:
MDI DIV:
MDI SUB:
RESULT:
EX: NO: 8
TRIGGER
AIM
To develop and execute a Trigger for Before and After update, Delete, Insert operations
on a
table.
PROCEDURE
STEP 1: Start
STEP 2: Initialize the trigger with specific table id.
STEP 3:Specify the operations (update, delete, insert) for which the trigger has to be
executed.
STEP 4: Execute the Trigger procedure for both Before and After sequences
STEP 5: Carryout the operation on the table to check for Trigger execution.
STEP 6: Stop
EXECUTION
1. CREATE A TRIGGER TO POP-UP THE DML OPERATIONS
SQL> CREATE TABLE EMPA(ID NUMBER(3),NAME VARCHAR2(10),INCOME
NUMBER(4),EXPENCE
NUMBER(3),SAVINGS NUMBER(3));
TABLE CREATED.
SQL> INSERT INTO EMPA VALUES(2,'KUMAR',2500,150,650);
1 ROW CREATED.
SQL> INSERT INTO EMPA VALUES(3,'VENKY',5000,900,950);
1 ROW CREATED.
SQL> INSERT INTO EMPA VALUES(4,'ANISH',9999,999,999);
1 ROW CREATED.
SQL> SELECT * FROM EMPA;
ID NAME INCOME EXPENCE SAVINGS
------- ------------- ---------- ----------------- --------------------------------
2 KUMAR 2500 150 650
3 VENKY 5000 900 950
2. Create a Trigger to check the age valid or not Using Message Alert
SQL> CREATE TABLE TRIG(NAME CHAR(10),AGE NUMBER(3));
SQL> DESC TRIG;
Table created.
PROGRAM:
SQL> SET SERVEROUTPUT ON;
SQL> CREATE TRIGGER TRIGNEW
AFTER INSERT OR UPDATE OF AGE ON TRIG
FOR EACH ROW
BEGIN
IF(:NEW.AGE<0) THEN
DBMS_OUTPUT.PUT_LINE('INVALID AGE');
ELSE
DBMS_OUTPUT.PUT_LINE('VALID AGE');
END IF;
END;
/
TRIGGER CREATED.
3. Create a Trigger to check the age valid and Raise appropriate error code and
error message.
SQL> create table data(name char(10),age number(3));
Table created.
SQL> desc data;
Name Null? Type
----------------------------------------- -------- ------------------------
NAME CHAR(10)
AGE NUMBER(3)
SQL> CREATE TRIGGER DATACHECK
AFTER INSERT OR UPDATE OF AGE ON DATA
FOR EACH ROW
BEGIN
IF(:NEW.AGE<0) THEN
RAISE_APPLICATION_ERROR(-20000,'NO NEGATIVE AGE ALLOWED');
END IF;
END;
/
Trigger created.
4. Create a Trigger for EMP table it will update another table SALARY while
inserting values.
SQL> CREATE TABLE SRM_EMP2(INAME VARCHAR2(10),
IID NUMBER(5),
SALARY NUMBER(10));
Table created.
1 row created.
SQL> SELECT * FROM SRM_SAL2;
INAME TOTALEMP TOTALSAL
1 row created.
RESULT:
Thus the Trigger procedure has been executed successfully for both before and after
sequences.
J.K.K.Nattraja College of Engineering and Infromation Technology
Technology Page
96
Database Management Systems Lab IT-IV-SEM
EX:NO:9
MENU DESIGN
AIM
To design a Note Pad Application menu using Visual Basic.
PROCEDURE
STEP 1: Start
STEP 2: Create the form with essential controls and insert the menu using menu editor.
STEP 3: Write the code for doing the appropriate functions.
STEP 4: Save the forms and project.
STEP 5: Execute the form.
STEP 6: Stop
EXECUTION
Coding:
Private Sub ab_Click()
RichTextBox1.SelFontName = "Arial Black"
End Sub
Private Sub al_Click()
End Sub
Private Sub bold_Click()
RichTextBox1.SelBold = True
End Sub
Private Sub cb_Click()
RichTextBox1.SelColor = vbblue
End Sub
Private Sub cl_Click()
RichTextBox1.SelColor = vbred
End Sub
Private Sub copy_Click()
'Clipboard.SetText "richtextbox1.seltext", 1
'MsgBox Clipboard.GetText
Clipboard.SetText RichTextBox1.SelText, 1
RichTextBox1.SelText = Clipboard.GetText
MsgBox Clipboard.GetText
End Sub
Private Sub eighteen_Click()
RichTextBox1.SelFontSize = 18
End Sub
Private Sub exit_Click()
End
End Sub
Private Sub fcg_Click()
RichTextBox1.SelColor = vbgreen
End Sub
Private Sub fourteen_Click()
RichTextBox1.SelFontSize = 14
End Sub
Private Sub helpp_Click()
ans = MsgBox("visual basic sample notepad .....!", vbYes + vbinforamtion, "Help")
If ans = vbYes Then
Unload Me
End If
End Sub
Private Sub italic_Click()
RichTextBox1.SelItalic = True
End Sub
Private Sub MC_Click()
RichTextBox1.SelFontName = "Monotype Corsiva"
End Sub
Private Sub new_Click()
RichTextBox1 = ""
End Sub
RichTextBox1.SelColor = vbgreen
End Sub
RESULT:
Thus the program has been loaded and executed successfully.
EX: NO: 10
REPORT DESIGN
AIM
To design a report design using Visual Basic.
PROCEDURE
STEP 1: Start
STEP 2: Create the form with essential controls and insert the menu using menu editor.
STEP 3: Write the code for doing the appropriate functions.
STEP 4: Save the forms and project.
STEP 5: Execute the form and generate report
STEP 6: Stop
EXECUTION
Code for progress bar:
Private Sub Form_KeyPress(KeyAscii As Integer)
Unload Me
End Sub
Private Sub Frame1_Click()
Unload Me
frmLogin.Show
End Sub
Private Sub Timer1_Timer()
On Error Resume Next
ProgressBar1.Value = ProgressBar1.Value + 1
If ProgressBar1.Value = 100 Then
login.Show
Unload Me
End If
End Sub
Private Sub Timer2_Timer()
On Error Resume Next
ProgressBar2.Value = ProgressBar2.Value + 1
If ProgressBar2.Value = 100 Then
MsgBox ("welcome")
login.Show
Unload Me
End If
End Sub
Code for login form:
Private Sub Command1_Click()
If (LCase(Text1.Text)) = "nagraaj" And (LCase(Text2.Text)) = "nagraaj" Then
Unload Me
Stock.Show
Else
MsgBox "Please Enter Correct Username and Password"
End If
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
If (LCase(Text1.Text)) = "nagraaj" And (LCase(Text2.Text)) = "nagraaj" Then
frmDataEnv.Show
Unload Me
End If
End Sub
Stock Form:
Private Sub cmadd_Click(Index As Integer)
Adodc1.Recordset.AddNew
a = InputBox("ENTER THE PRODUCT CODE")
Text1.Text = a
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Text7.Text = ""
Text8.Text = ""
Text9.Text = ""
End Sub
Private Sub cmdmovl_Click(Index As Integer)
Adodc1.Recordset.MoveNext
End Sub
Private Sub cmdmovn_Click(Index As Integer)
'dodc1.Recordset.MoveNext
If (Adodc1.Recordset.EOF) = True Then
Adodc1.Recordset.MoveNext
Else
Adodc1.Recordset.MovePrevious
MsgBox ("THIS IS YOUR LAST RECORD")
End If
End Sub
Private Sub cmdmovp_Click(Index As Integer)
'dodc1.Recordset.MovePrevious
If Adodc1.Recordset.BOF = True Then
Adodc1.Recordset.MoveFirst
Else '
Adodc1.Recordset.MoveNext
MsgBox ("THISIS YOUR FIRST RECORD")
End If
End Sub
Private Sub cmdsearch_Click(Index As Integer)
Dim a As String
'Adodc1.Recordset ("select * from t1 where [ITEM_CODE] = " & Text1.Text(0) & "")
DataReport1.Show
If Adodc1.Recordset.EOF Then
MsgBox ("INVALID RECORD")
End If
End Sub
Private Sub Command2_Click()
Adodc1.Recordset.Update
MsgBox ("UPDATED SUCCESSFULY")
End Sub
Private Sub Command3_Click()
Dim s As String
a = InputBox("Enter The student name")
a = Trim(a)
s = "STUDENTNAME='" & a '" "
Adodc1.Recordset.Delete
MsgBox ("deleted")
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Text7.Text = ""
Text8.Text = ""
Text9.Text = ""
End Sub
Private Sub Command5_Click()
DataReport1.Show
End Sub
Login
Stock Form:
Report Design:
RESULT:
Thus the program has been loaded and executed successfully.