Professional Documents
Culture Documents
ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
VISION
To conceive our department as Centre of Academic Excellence by catering quality
education with ethical standards
MISSION
To create a conducive atmosphere to achieve active professionalism by fortifying
academic proficiency with ethical standards
To enhance the confidence level to develop sustainable solution to upgrade the
society forever
To empower the students with prerequisite professional skills for enhancing
employability and entrepreneurship, continuing education and research
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities
with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to
the professional engineering practice.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader
in diverse teams, and in multidisciplinary settings.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage
in independent and life-long learning in the broadest context of technological change.
IT1402 DATABASE MANAGEMENT SYSTEMS LABORATORY LTPC 0042
OBJECTIVES:
Experiments:
1. Data Definition Commands, Data Manipulation Commands for inserting, deleting, updating
and retrieving Tables and Transaction Control statements
2. Database Querying – Simple queries, Nested queries, Sub queries and Joins
3. Views, Sequences, Synonyms
4. Database Programming: Implicit and Explicit Cursors
5. Procedures and Functions
6. Triggers
7. Exception Handling
8. Database Design using ER modeling, normalization and Implementation for any application
9. Create Document, column and graph based data using NOSQL database tools.
10. Develop a simple GUI based database application
OUTCOMES:
COURSE OUTCOMES:
IT1402.3 Implement simple applications that use Views, Sequences, Synonyms, Cursors and
critically analyze the use of Tables, Functions, Procedures, Triggers and
Exception Handling using PL/SQL
IT1402.4 Create and manipulate data using NOSQL database.
IT1402.5 Design database using ER modeling, Normalize and Implement a GUI application that require
a Front-end and Back end Tool
IT1402.1 3 2 2 2 1 2 2
IT1402.2 3 2 2 2 1 2 2
IT1402.3 3 2 2 2 1 2 2 2
IT1402.4 3 3 3 3 2 1 2 2
IT1402.5 3 3 3 1 2 1 2 2
INDEX
Mapped Page
S.No. Date Name of the Experiment
CO No.
6. Triggers IT1402.3
9.
Create Document, column and graph based data using
NOSQL database tools IT1402.4
All 10 experiments are completed as per syllabus and 10th experiment is implemented as a Mini
Project.
AIM: To create a database and to write SQL queries to retrieve information from the database
and executing transaction control statements in RDBMS.
Create Table:
This command is used to create database tables in RDBMS.
Syntax
CREATE TABLE table_name (colname1 datatype, colname2 datatype,
colname3 datatype, …..…colnamen datatype);
Alter Table:
This command is used to add or drop or modify the attributes from the existing table.
Syntax
Adding an attribute:
ALTER TABLE <table_name> ADD column_name datatype;
Dropping an attribute:
ALTER TABLE <table_name> DROP COLUMN column_name;
Modifying an attribute:
ALTER TABLE <table_name> MODIFY column_name newdatatype;
Drop Table:
This command is used to remove a relation from an SQL database. This command deletes not
only the records of the table but also the entire schema of the database.
Syntax:
DROP TABLE <table_name>;
DML COMMANDS:
Insert
Select
Update
Delete
INSERT:
Insert command is used to insert the values into the table. There are three ways to insert a record
into the database.
Syntax
INSERT INTO table_name(col1,col2,…….,coln)
VALUES(val1,val2,……,valn);
2. Inserting Values:
Syntax
INSERT INTO table_name VALUES(val1,val2,……...,valn);
Syntax
INSERT INTO tablename VALUES(&col1,’&col2’,’&col3’,…….,&coln);
SELECT:
The select statement is used to query a database. This statement is used to retrieve the
information from the database. The SELECT statement can be used in many ways. They are:
Syntax
SELECT column name FROM table_name;
Syntax
SELECT * FROM table_name;
UPDATE:
Syntax:
UPDATE table_name
SET column name =new value
WHERE column name=some value;
DELETE:
Syntax:
SAVE POINT
A SAVEPOINT is a marker within a transaction that allows for a partial rollback. As changes are made in
a transaction, we can create SAVEPOINTs to mark different points within the transaction. If we
encounter an error, we can rollback to a SAVEPOINT or all the way back to the beginning of the
transaction.
Create a Save point:
Syntax
Partial Rollback:
Syntax
NAME TYPE
DNO NUMBER(4)
DNAME VARCHAR2(20)
NAME TYPE
EID NUMBER(4)
NAME VARCHAR2(30)
DOB DATE
ADDR VARCHAR2(50)
PIN NUMBER(10)
SEX CHARACTER
DESIG VARCHAR2(25)
DEPTNO NUMBER(4)
MARISTA CHARACTER
SALARY NUMBER(8,2)
Table created.
SQL> desc department;
Name Null? Type
----------------------------------------- -------- -----------------------
DNO NOT NULL NUMBER(4)
DNAME VARCHAR2(20)
Table created.
Table altered.
Table altered.
Table dropped
CREATION OF TABLE:
Table created.
SQL> /
Enter value for STUD_ID: 002
Enter value for STUD_NAME: ARUN
Enter value for STUD_DEPT: ECE
old 1: INSERT INTO STUDENT VALUES(&STUD_ID,'&STUD_NAME','&STUD_DEPT')
new 1: INSERT INTO STUDENT VALUES(002,'ARUN','ECE')
1 row created.
SQL> /
Enter value for STUD_ID: 003
Enter value for STUD_NAME: ARNAV
Enter value for STUD_DEPT: EEE
old 1: INSERT INTO STUDENT VALUES(&STUD_ID,'&STUD_NAME','&STUD_DEPT')
new 1: INSERT INTO STUDENT VALUES(003,'ARNAV','EEE')
1 row created.
SQL> /
Enter value for STUD_ID: 004
Enter value for STUD_NAME: ABHINAV
Enter value for STUD_DEPT: MECH
old 1: INSERT INTO STUDENT VALUES(&STUD_ID,'&STUD_NAME','&STUD_DEPT')
new 1: INSERT INTO STUDENT VALUES(004,'ABHINAV','MECH')
1 row created.
SQL> /
Enter value for STUD_ID: 005
Enter value for STUD_NAME: JAY
Enter value for STUD_DEPT: IT
old 1: INSERT INTO STUDENT VALUES(&STUD_ID,'&STUD_NAME','&STUD_DEPT')
new 1: INSERT INTO STUDENT VALUES(005,'JAY','IT')
1 row created.
1 row updated.
SAVE POINT:
ID SNAME SDEPT
---------- -------------------- --------------------
2 ARUN ECE
3 ARNAV EEE
4 ABHINAV MECH
5 JAY IT
SQL> insert into depmt values(6,'ABHI','MCA');
1 row created.
SQL>savepoint t1;
Savepoint created.
ID SNAME SDEPT
--------------------------------------------------------------
6 ABHI MCA
2 ARUN ECE
3 ARNAV EEE
8 ABHINAV MECH
5 JAY IT
RESULT:
Thus the database is created and the information is well retrieved using SQL in RDBMS.
EXP. NO: 2
DATE:
Database Querying – Simple queries, Nested queries, Sub queries and Joins
AIM: To query a database using simple SQL queries, Nested queries, Sub queries and Joins in
RDBMS.
The language used to manipulate data is called DML Manipulation of data includes the insertion
of data into the database, modification of data stored in the database, deletion of data from the
database and retrieval of data for further process. The commands used are
Insert
Update
Delete
QUERY:
Query is a statement in the DML that request the retrieval of data from database. The portion of
the DML used in a Query is called Query language. The SELECT statement is used to query a
database.
INSERT:
Insert command is used to insert the values into the table. There are three ways to insert a record
into the database.
Syntax:
INSERT INTO table_name(col1,col2,…….,coln)
VALUES(val1,val2,……,valn);
2. Inserting Values:
Syntax:
INSERT INTO table_name VALUES(val1,val2,……...,valn);
UPDATE:
Syntax:
UPDATE table_name
SET column name =new value
WHERE column name=some value;
DELETE:
Syntax:
SELECT:
The select statement is used to query a database. This statement is used to retrieve the
information from the database. The SELECT statement can be used in many ways. They are:
Syntax:
SELECT column name FROM table_name;
To select all columns from the table * is used instead of column names.
Syntax:
The DISTINCT keyword is used to return only different values (i.e. ) this command does
not select the duplicate values from the table .
Syntax:
SELECT DISTINCT column name(s) FROM table_name;
If you want to get the rows, which contain certain values, the best way to do it is to use
the IN conditional expression.
Syntax:
SELECT column name(s) FROM table_name WHERE
Column name IN(value1,value2,……,value n);
BETWEEN can be used to get those items that fall within a range.
Syntax:
SELECT column name FROM table_name WHERE
Column name BETWEEN value1 AND value2;
The select statement can be used to rename either a column or the entire table.
7. Sorting:
The select statement with the order by Clause is used to sort the contents of the table
either in ascending or descending order.
Syntax:
SELECT column name FROM table_name WHERE
Condition ORDER BY column name ASC/DESC;
The select statement along with like clause I is used to match strings. The like condition
is used to specify a search pattern in a column.
Syntax:
We can use the SELECT statement to select the ‘null’ values also. For retrieving
rows where some of the columns have been defined as NULLs there is a special
comparison operator of the form IS[NOT]NULL.
Syntax:
SELECT column name FROM table_name WHERE
Column name IS NULL;
We can combine one or more conditions in a SELECT statement using the logical
operators AND,OR,NOT.
Syntax:
SELECT column name FROM table_name WHERE
Condition1 LOGICAL OPERATOR condition2;
JOIN
This command is used for joining two or more relations into a single relation. It is of two
types namely,
Inner join
Full Outer join
*Left Outer Join
*Right Outer Join
1) INNER JOIN
Only pairs of tuples that match the join condition are retrieved from the two different
relations.
Syntax:
SELECT <attribute names> from ( <table name1> INNER JOIN <tablename2> on
<condition>);
2) FULL OUTER JOIN
Every tuple in the left table and right table must appear in the result; if it does not have a
matching tuple, it is padded with NULL values.
Syntax:
SELECT <attribute name> from ( <table name1> FULL OUTER JOIN <tablename2> on
<condition>);
Every tuple in the left table must appear in the result; if it does not have a matching tuple,
it is padded with NULL values for the attributes of the right table
Syntax:
SELECT <attribute name> from ( <table name1> LEFT OUTER JOIN <tablename2> on
<condition>);
Every tuple in the right table must appear in the result; if it does not have a matching
tuple, it is padded with NULL values for the attributes of the left table
Syntax:
SELECT <attribute name> from ( <table name1> RIGHT OUTER JOIN <tablename2> on
<condition>);
EXERCISES:
CREATION OF TABLE:
Table created.
SQL> /
Enter value for STUD_ID: 002
Enter value for STUD_NAME: ARUN
Enter value for STUD_DEPT: ECE
old 1: INSERT INTO STUDENT VALUES(&STUD_ID,'&STUD_NAME','&STUD_DEPT')
new 1: INSERT INTO STUDENT VALUES(002,'ARUN','ECE')
1 row created.
SQL> /
Enter value for STUD_ID: 003
Enter value for STUD_NAME: ARNAV
Enter value for STUD_DEPT: EEE
old 1: INSERT INTO STUDENT VALUES(&STUD_ID,'&STUD_NAME','&STUD_DEPT')
new 1: INSERT INTO STUDENT VALUES(003,'ARNAV','EEE')
1 row created.
SQL> /
Enter value for STUD_ID: 004
Enter value for STUD_NAME: ABHINAV
Enter value for STUD_DEPT: MECH
old 1: INSERT INTO STUDENT VALUES(&STUD_ID,'&STUD_NAME','&STUD_DEPT')
new 1: INSERT INTO STUDENT VALUES(004,'ABHINAV','MECH')
1 row created.
SQL> /
Enter value for STUD_ID: 005
Enter value for STUD_NAME: JAY
Enter value for STUD_DEPT: IT
old 1: INSERT INTO STUDENT VALUES(&STUD_ID,'&STUD_NAME','&STUD_DEPT')
new 1: INSERT INTO STUDENT VALUES(005,'JAY','IT')
1 row created.
SELECTING A TABLE:
1 row updated.
JOINS
1. Create the following tables with the given specifications and insert few records in both the
relations.
2. Write a SQL query to execute INNER JOIN on both the relations and record the output.
3. Write a SQL query to execute LEFT OUTER JOIN on both the relations and record the
output.
4. Write a SQL query to execute RIGHT OUTER JOIN on both the relations and record the
output.
5. Write a SQL query to execute FULL OUTER JOIN on both the relations and record the
output.
OUTPUT:
Table created.
1 row created.
SQL> /
Enter value for prod_id: 20
Enter value for p_name: eraser
Enter value for quantity: 30
old 1: insert into product_details values(&prod_id,'&p_name',&quantity)
new 1: insert into product_details values(20,'eraser',30)
1 row created.
SQL> /
Enter value for prod_id: 30
Enter value for p_name: pen
Enter value for quantity: 35
old 1: insert into product_details values(&prod_id,'&p_name',&quantity)
new 1: insert into product_details values(30,'pen',35)
1 row created.
SQL> /
Enter value for prod_id: 40
Enter value for p_name: compass
Enter value for quantity: 45
old 1: insert into product_details values(&prod_id,'&p_name',&quantity)
new 1: insert into product_details values(40,'compass',45)
1 row created
Table created.
1 row created.
SQL> /
Enter value for cust_id: 222
Enter value for cust_name: arun
Enter value for p_id: 30
old 1: insert into cust_order values(&cust_id,'&cust_name',&p_id)
new 1: insert into cust_order values(222,'arun',30)
1 row created.
SQL> /
Enter value for cust_id: 333
Enter value for cust_name: joy
Enter value for p_id: 20
old 1: insert into cust_order values(&cust_id,'&cust_name',&p_id)
new 1: insert into cust_order values(333,'joy',20)
1 row created.
MERITS:
More readable, as the JOIN criteria is separate from the WHERE clause
The main advantage of a join is that it executes faster and provides more information of a
particular tuple.
DEMERITS:
A disadvantage of using joins is that they are not as easy to read as sub queries.
Another disadvantage is that it can be confusing as to which relation the appropriate type
of join to use to yield the correct desired result set.
URL:
http://www.ehow.com/info_8700587_advantages-disadvantages-join-subquery.html
http://stackoverflow.com/questions/2241991/in-mysql-queries-why-use-join-instead-of-
where
http://ask.sqlservercentral.com/questions/91760/inner-join-vs-a-select-with-a-bunch-of-
tables-spec.html
http://www.techbubbles.com/sql-server/sql-server-joins-or-subqueries/
http://www.databasejournal.com/features/mssql/article.php/3623916/MS-SQL-Joins-
Part-2.htm
VIVA QUESTIONS:
1. What is a join?
2. Differentiate inner join from outer join.
3. What is the need for joining relations?
RESULT:
Thus simple SQL queries, Nested queries, Sub queries and Joins are successfully
executed in RDBMS.
EXP NO: 3
DATE:
VIEW
Views are used to provide security for the data. They provide a virtual image for a database.
#Create View:
This command is used to create database view in RDBMS.
Syntax
CREATE VIEW view_name as select <columnname> from <tablename>;
#Update View:
This command is used to change a value of a record in the database.
Syntax
UPDATE view_name SET column name =new value WHERE column name=some value;
#Drop View:
Drop command is used to drop the view.
Syntax
SYNONYM
Description
A synonym is an alternative name for objects such as tables, views, sequences, stored procedures, and
other database objects.
Syntax
table
view
sequence
stored procedure
function
synonym
Drop Synonym:
Once a synonym has been created in Oracle, you might at some point need to drop the synonym.
Syntax
SEQUENCES (AUTONUMBER)
Learn how to create and drop sequences in Oracle with syntax and examples.
Description
In Oracle, you can create an auto number field by using sequences. A sequence is an object in Oracle that
is used to generate a number sequence. This can be useful when you need to create a unique number to act
as a primary key.
Create Sequence:
You may wish to create a sequence in Oracle to handle an auto number field.
Syntax
Using Sequences:
<sequence name>.NEXTVAL;
Drop Sequence:
Once you have created your sequence in Oracle, you might find that you need to remove it from the
database.
Syntax
CREATE VIEW:
SQL> create view depview as select id,sname from depmt;
View created.
ID SNAME
---------- -----------------------------
6 ABHI
2 ARUN
3 ARNAV
4 ABHINAV
5 JAY
UPDATE VIEW:
SQL> update depview set id=8 where id=4;
1 row updated.
ID SNAME
---------- ----------------------------
6 ABHI
2 ARUN
3 ARNAV
8 ABHINAV
5 JAY
DROP VIEW:
SQL> drop view depview;
View dropped.
SQL> select * from depview;
ERROR at line 1:
ORA-00942: table or view does not exist
SYNONYM:
SQL> create synonym d for depmt;
Synonym created.
DROP SYNONYM:
SQL> DROP synonym d;
Synonym dropped.
SQL> select * from tab;
SEQUENCES:
CREATE SEQUENCE:
SQL> create sequence seq_stud minvalue 100 maxvalue 999 start with 100 increment
by 2 cache 20;
Sequence created.
SQL> /
Enter value for sname: arun
old 1: insert into stu values(seq_stud.nextval,'&sname')
new 1: insert into stu values(seq_stud.nextval,'arun')
1 row created.
SQL> select * from stu;
SID SNAME
---------- --------------------
102 abhi
104 arun
DROP SEQUENCE:
SQL> drop sequence seq_stud;
Sequence dropped.
RESULT:
Thus Views, Sequences and Synonyms have been created and updated in
RDBMS.
Ex. No: 4
Date:
AIM:
To implement implicit and explicit cursors using PL/SQL programs.
Hardware Requirements:
Pentium IV Processor
2 GB RAM
Software Requirements:
Windows 7 or above
Oracle
Visual Basic 6.0
1. PL/SQL:
PL/SQL stands for procedural structural language/SQL. PL/SQL
extends SQL by adding control structures found in other structural languages. PL/SQL
can be used in the Oracle relational database in the Oracle server and in the client side
application development tools.
SYNTAX:
A PL/SQL block can be divided into three parts namely, a
declarative part, an executable part and an exception handling part. The order is shown
below:
DECLARE
Declarations
BEGIN
Executable Statements
EXCEPTION
Handlers
END;
Objects can be declared in the declarative part, which can be used, in the
executable part for further manipulations. All procedural statements are included between
the BEGIN and END statements. Errors that occur during the execution are handled by
the exception handlers.
2. ATTRIBUTES:
Attributes allows us to refer data types and objects from
database. PL/SQL constants and variables can have attributes. The following are the
types of attributes supported by the PL/SQL.
%type
%row type
%type:
%type is used when declaring variables that refer to database columns.
SYNTAX:
%rowtype:
%rowtype attributes provides a record type that represents a row in the table. The
record can store an entire row of data selected from the table or fetched by a cursor.
SYNTAX:
3. CURSORS:
Oracle allocates a memory known as the context area for the processing
of the SQL statements. A cursor is a pointer or handle to the context area. Through the
cursor, a PL/SQL program can control the context area and what happens to it as the
statement is processed.
Static cursors
Dynamic cursors
REF cursors
Static cursors are the ones whose select statements are known at the compile time. These
are further classified into
Explicit cursors
Implicit cursors
4. EXPLICIT CURSORS:
The set of rows returned by a query can contain zero or multiple
rows depending on the query defined. These rows are called the active set. The cursor
will point to the current row in the active set.
After declaring a cursor, we can use the following commands to control the
cursor.
Open
Fetch
Close
The ‘open’ statement executes the query, identifies the active set and positions the
cursor before the first row. The syntax is given below:
Open <cursor_name>;
The ‘fetch’ statement retrieves the current row and advances the cursor to the next
row to fetch the remaining rows. The syntax is given below:
After processing the last row in the active set, the cursor is disabled with the help
of the ‘close’ command. The syntax is as follows:
Close <cursor_name>;
5. CURSOR ATTRIBUTES:
Explicit cursor attributes when appended to the cursor name
allows us to access useful information from the retrieved rows.
They are:
%notfound
%found
%rowcount
%isopen
%Notfound:
after opening the cursor , a fetch is used to fetch the rows from the active set, one
at a time. The attribute %notfound indicates whether fetch statements returns row from
the active set. If the last ‘fetch’ fails to return a row, then %notfound evaluates to true,
else, it evaluates to false.
%found:
It evaluates to true if the last ‘fetch’ statement succeeds in returning a row. It
would be evaluated to false if the last ‘fetch’ statement fails to return a row because no
rows were available.
%rowcount:
The %rowcount is used to return the number of rows fetched. Before the first
fetch, %rowcount is zero.
%isopen:
if the cursor is already open, then the attribute %isopen evaluates to true, else it
evaluates to false.
SQL>declare
cursor emp is select * from employee where marista='m';
dno employee.deptno%type;
a employee%rowtype;
begin
dno:=&dno;
open emp;
loop
fetch emp into a;
update employee set salary=salary+500 where deptno=dno;
exit when emp%notfound;
end loop;
end;
/
OUTPUT:
RESULT:
Thus implicit and explicit cursors using PL/SQL were executed successfully.
Ex. No: 5
Date:
HARDWARE REQUIREMENTS:
Processor Type : Pentium IV
RAM : 1 GB
Hard Disk : 80GB
Frequency : 1.60 GHZ
SOFTWARE REQUIREMENTS:
Front End : VB/VC ++/JAVA or equivalent
Back End : Oracle /SQL /MySQL/PostGress /DB2 or equivalent
Platform : Windows/XP, Windows 7 or its higher versions
PROBLEM DEFINITION:
The main concept is to understand the how procedures are created and implemented in
PL/SQL .
BASIC TERMINOLOGIES:
PL/SQL
PL/SQL stands for Procedural Language extension of SQL.PL/SQL is a combination
of SQL along with the procedural features of programming languages. It was developed by
Oracle Corporation in the early 90’s to enhance the capabilities of SQL.
PL/SQL Engine
Oracle uses a PL/SQL engine to processes the PL/SQL statements. A PL/SQL code can be
stored in the client system (client-side) or in the database (server-side).
Each PL/SQL program consists of SQL and PL/SQL statements which from a PL/SQL block.
PROCEDURES
<local declarations>;
begin
(executable statements)
[exception] (exception handlers)
end;
A procedure has two parts, namely, specification and body. The procedure specification
begins with the keyword procedure and ends with the procedure name or parameter list. The
procedure body begins with the keyword is and ends with the keyword end. It can also include
declarative, executable and exceptional parts within the keywords are and end.
FUNCTIONS
A function is a subprogram that computes a value.The syntax for creating a function is given
below.
Similar to a procedure, a function also has two parts, namely, the function specification
and the function body. The function specification begins with the keyword function and ends
with the return clause. The function body begins with the keyword is and ends with the keyword
end. A PL/SQL block can also be included in a function body.
EXERCISES:
1) To create a procedure to display the salary of the specified employee from employee
table
Procedure created.
OUTPUT
2) To create a PL/SQL program which calls a procedure to print 0 if salary is less than
15000 else print 1.
SQL> declare
2 A number;
3 B number;
4 begin
5 A:=&A;
6 proc2(A,B);
7 Dbms_output.put_line('B value is:'||B);
8 end;
9 /
OUTPUT:
Enter value for a: 22
old 5: A:=&A;
new 5: A:=22;
B value is:1
SQL> /
Enter value for a: 33
old 5: A:=&A;
new 5: A:=33;
B value is:0
Function created.
OUTPUT
Function created.
OUTPUT
F1(5)
----------
120
MERITS:
PROCEDURES
Stored procedures are compiled once and stored in executable form, so procedure calls
are quick and efficient.
Executable code is automatically cached and shared among users.
By grouping SQL statements, a stored procedure allows them to be executed with a
single call.
FUNCTIONS
The ability to store functions within the Oracle database allows you to reach great heights
in modularity, maintainability, and performance improvement. Using functions to
accomplish specific tasks improves the reliability of other modules and reduces
development time.
DEMERITS:
URLs:
http://www.dbforums.com/db2/972602-advanteges-disadvanteges-check-
constraints-triggers-stored-procedures.html
http://sqlperformance.com/2012/11/t-sql-queries/benefits-indexing-foreign-
keys
http://docs.oracle.com/cd/E18283_01/server.112/e16508/datainte.htm
http:/www.javas.com/oracle/catalog/index.html
VIVA QUESTIONS:
1. What is a PL/SQL?
2. Give the structure of Procedure?
3. Give the merits and demerits of using the procedure.
RESULT:
Thus the procedures and functions using control structures were studied and executed in
PL/SQL.
Ex. No: 06
Date:
TRIGGERS
OBJECTIVE:
To study and execute Triggers in RDBMS.
HARDWARE REQUIREMENTS:
Processor Type : Pentium IV
RAM : 2 GB
Hard Disk : 500 GB
Frequency : 1.60 GHZ
SOFTWARE REQUIREMENTS:
Front End : VB/VC ++/JAVA or equivalent
Back End : Oracle /SQL /MySQL/PostGress /DB2 or equivalent
Platform : Windows/XP, Windows 7 or its higher versions
PROBLEM DEFINITION:
The main concept is to understand the concept of triggers and functions in PL/SQL and
implement them in simple programs
BASIC TERMINOLOGIES:
PL/SQL
PL/SQL stands for Procedural Language extension of SQL.PL/SQL is a combination
of SQL along with the procedural features of programming languages. It was developed by
Oracle Corporation in the early 90’s to enhance the capabilities of 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 (CREATES, 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.
PARTS OF A TRIGGER
A database trigger has three parts, namely, a trigger statement, a trigger body and a
trigger restriction.
Trigger Statement: -
A trigger statement specifies the DML statements like update, delete and insert and it
fires the trigger body. It also specifies the table to which the trigger is associated.
Trigger Body: -
Trigger body is a PL/SQL block that is executed when a triggering statement is issued.
Trigger Restriction: -
Restrictions on a triggers can be achieved using the WHEN clause as shown in the syntax
for creating triggers. They can be included in the definition of a row trigger, where in, the
condition in the WHEN clause is evaluated for each row that is affected by the trigger.
1) PL/SQL program to create a trigger before the user inserts the data into the table.
Trigger created.
OUTPUT
2) Write a PL/SQL program to create a trigger before the user deletes the data from the
table.
Trigger created.
OUTPUT
SQL> create trigger upd1 before update on emp for each row
2 begin
3 if:new.salary<20000 then
4 raise_application_error(-20001,'salary cant be lower than this');
5 end if;
6 end;
7 /
Trigger created.
OUTPUT
MERITS:
TRIGGERS
DEMERITS:
TRIGGERS
Trigger can provide extended validation and it cannot be replaced with all the validations.
Simple validations can be done at the application level itself.
Triggers executes invisibly from client-application which connects to the database server.
So it is difficult to figure out what happens at the database layer.
Triggers run on every update made to the table therefore it adds more load to the database
and cause the system to run slow.
FUNCTIONS
Limited coding functionality
Testing
Portability
URLs:
http://www.dbforums.com/db2/972602-advanteges-disadvanteges-check-
constraints-triggers-stored-procedures.html
http://sqlperformance.com/2012/11/t-sql-queries/benefits-indexing-foreign-
keys
http://docs.oracle.com/cd/E18283_01/server.112/e16508/datainte.htm
http:/www.javas.com/oracle/catalog/index.html
VIVA QUESTIONS:
RESULT:
OBJECTIVE:
To write a PL/SQL block to handle all types of exceptions.
HARDWARE REQUIREMENTS:
Processor Type : Pentium IV
RAM : 1 GB
Hard Disk : 80GB
Frequency : 1.60 GHZ
SOFTWARE REQUIREMENTS:
Front End : VB/VC ++/JAVA or equivalent
Back End : Oracle /SQL /MySQL/PostGress /DB2 or equivalent
Platform : Windows/XP, Windows 7 or its higher versions
PROBLEM DEFINITION:
The main concept is to understand the basic concepts of Exceptions in PL/SQL and to
write some simple programs using it.
BASIC TERMINOLOGIES:
Exception Handling
PL/SQL provides a feature to handle the Exceptions which occur in a PL/SQL Block
known as exception Handling. Using Exception Handling we can test the code and avoid it from
exiting abruptly.
When an exception occurs messages which explains its cause is received.
PL/SQL Exception message consists of three parts.
1) Type of Exception
2) An Error Code
3) A message
By Handling the exceptions we can ensure a PL/SQL block does not exit abruptly.
General Syntax for coding the exception section
DECLARE
Declaration section
BEGIN
Exception section
EXCEPTION
WHEN ex_name1 THEN
-Error handling statements
WHEN ex_name2 THEN
-Error handling statements
WHEN Others THEN
-Error handling statements
END;
When an exception is raised, Oracle searches for an appropriate exception handler in the
exception section. For example in the above example, if the error raised is 'ex_name1 ', then the
error is handled according to the statements under it. Since, it is not possible to determine all the
possible runtime errors during testing fo the code, the 'WHEN Others' exception is used to
manage the exceptions that are not explicitly handled. Only one exception can be raised in a
Block and the control does not return to the Execution Section after the error is handled.
DELCARE
Declaration section
BEGIN
DECLARE
Declaration section
BEGIN
Execution section
EXCEPTION
Exception section
END;
EXCEPTION
Exception section
END;
In the above case, if the exception is raised in the inner block it should be handled in the
exception block of the inner PL/SQL block else the control moves to the Exception block of the
next upper PL/SQL Block. If none of the blocks handle the exception the program ends abruptly
with an error.
Types of Exception
For example: NO_DATA_FOUND and ZERO_DIVIDE are called Named System exceptions.
The general syntax to declare unnamed system exception using EXCEPTION_INIT is:
DECLARE
exception_name EXCEPTION;
PRAGMA
EXCEPTION_INIT (exception_name, Err_code);
BEGIN
Execution section
EXCEPTION
WHEN exception_name THEN
handle the exception
END;
c) User-defined Exceptions
Apart from sytem exceptions we can explicity define exceptions based on business rules.
These are known as user-defined exceptions.
RAISE_APPLICATION_ERROR ( )
PRE-DEFINED EXCEPTIONS
PL/SQL provides many pre-defined exceptions, which are executed when any database
rule is violated by a program. For example, the predefined exception NO_DATA_FOUND is
raised when a SELECT INTO statement returns no rows. The following table lists few of the
important pre-defined exceptions:
Oracle
Exception SQLCODE Description
Error
SQL>
SQL> DECLARE
2 num_a NUMBER := 6;
3 num_b NUMBER;
4 BEGIN
5 num_b := 0;
6 num_a := num_a / num_b;
7 num_b := 7;
8 dbms_output.put_line(' Value of num_b ' || num_b);
9 EXCEPTION
10 WHEN ZERO_DIVIDE
11 THEN
12 dbms_output.put_line('Trying to divide by zero');
13 dbms_output.put_line(' Value of num_a ' || num_a);
14 dbms_output.put_line(' Value of num_b ' || num_b);
15 END;
16 /
OUTPUT
Trying to divide by zero
Value of num_a 6
Value of num_b 0
OUTPUT
SQL> DECLARE
2 myNumber number default 'MY NUMBER';
3 BEGIN
4 NULL;
5 EXCEPTION
6 WHEN OTHERS THEN
7 dbms_output.put_line('Exception caught');
8 raise;
9 END;
10 /
OUTPUT
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 2
11 arun mumbai
22 harish delhi
33 deepak kolkata
SQL> declare
2 cid customer.c_id%type;
3 cname customer.c_name%type;
4 caddr customer.c_addr%type;
5 begin
6 cid:=&cid;
8 from customer
9 where c_id=cid;
10 dbms_output.put_line('Name:'||cname);
11 dbms_output.put_line('Address:'||caddr);
12 exception
13 when no_data_found then
16 dbms_output.put_line('Error!');
17 end;
18 /
OUTPUT
old 6: cid:=&cid;
new 6: cid:=22;
Name:harish
Address:delhi
SQL> /
old 6: cid:=&cid;
new 6: cid:=44;
Using exceptions for error handling has several advantages. With exceptions, you can
reliably handle potential errors from many statements with a single exception handler
Instead of checking for an error at every point where it might occur, add an exception
handler to your PL/SQL block. If the exception is ever raised in that block (including
inside a sub-block), it will be handled.
Isolating error-handling routines makes the rest of the program easier to read and
understand.
DEMERITS:
URLs:
http://www.dbforums.com/db2/972602-advanteges-disadvanteges-check-
constraints-triggers-stored-procedures.html
http://sqlperformance.com/2012/11/t-sql-queries/benefits-indexing-foreign-
keys
http://docs.oracle.com/cd/E18283_01/server.112/e16508/datainte.htm
http:/www.javas.com/oracle/catalog/index.html
VIVA QUESTIONS:
1. What is an Exception?
2. What is the function of Exception Handler?
3. Mention the different types of Exception.
RESULT:
Thus the PL/SQL block is created to handle all types of exceptions and are well executed.
Ex. No: 8
Date:
Database Design using ER modeling, normalization and
Implementation for any application
Aim:
To design database using normalization and ER-Diagram.
Hardware Requirements:
Pentium IV Processor or higher end processor
2 GB RAM
Software Requirements:
Windows 7 or above
Oracle
Visual Basic 6.0
Normalization
minimizing redundancy
A normal form is a state of a table that results from applying simple rules regarding
functional dependencies to that table.The various types of normal form are:
First Normal Form(1NF): The multi-valued should be removed i.e elimination
of redundant groups.
A relation in the 1NF will be in the 2NF if one of the following conditions is
satisfied:
The primary key consist of only one attribute.
No non-key attribute exist in relation i.e. all the attributes in the relation are components
of the primary key.
Every non-key attribute is FD on full set of primary key attributes.
The formal definition of BCNF differs from the definition of 3NF.A relatio
schema R is in BCNF if whenever a non-trivial functional dependency XA holds in
R,then X is a superkey of R.
Procedure:
Type created.
Table created.
1 row created.
SQL> /
Enter value for eno: 002
Enter value for enmae: balu
Enter value for sno: 13
Enter value for sname: car street
Enter value for city: madurai
Enter value for state: tamilnadu
Enter value for sal: 10000
old 1: insert into employees
values(&eno,'&enmae',address(&sno,'&sname','&city','&state'),&sal)
new 1: insert into employees values(002,'balu',address(13,'car
street','madurai','tamilnadu'),10000)
1 row created.
SQL> /
Enter value for eno: 003
Enter value for enmae: chiru
Enter value for sno: 10
Enter value for sname: 9th street
Enter value for city: hyderabad
Enter value for state: andhra pradesh
Enter value for sal: 15000
old 1: insert into employees
values(&eno,'&enmae',address(&sno,'&sname','&city','&state'),&sal)
new 1: insert into employees values(003,'chiru',address(10,'9th
street','hyderabad','andhra pradesh')
1 row created.
SQL> create table en1 as select eno, ename ,sal from employees;
Table created.
Table altered.
Table created.
Table altered.
Normalizing to 1NF:
employee
Eno Ename Eadd Sal
1NF
Emp1 emp2
Ename Sal Eno eadd
Eno
SECOND NORMAL FORM:
Table created.
1 row created.
SQL> /
Enter value for eno: 102
Enter value for ename: ragu
Enter value for pno: 13
Enter value for pname: atm
Enter value for hours: 24
old 1: insert into empproject values(&eno,'&ename',&pno,'&pname',&hours)
new 1: insert into empproject values(102,'ragu',13,'atm',24)
1 row created.
SQL> /
Enter value for eno: 103
Enter value for ename: sunil
Enter value for pno: 14
Enter value for pname: robotics
Enter value for hours: 15
old 1: insert into empproject values(&eno,'&ename',&pno,'&pname',&hours)
new 1: insert into empproject values(103,'sunil',14,'robotics',15)
1 row created.
Table created.
Table altered.
Table created.
Table altered.
SQL> desc ep2
Name Null? Type
----------------------------------------- -------- ----------------------------
PNO NOT NULL NUMBER(3)
PNAME VARCHAR2(20)
Table created.
Table altered.
Table altered.
Normalizing to 2NF:
2NF
Ep1
eno Ename
Ep2
pno Pname
Ep3
eno pno Hours
THIRD NORMAL FORM:
Table created.
1 row created.
SQL> /
Enter value for eno: 102
Enter value for ename: ragu
Enter value for sal: 20000
Enter value for dno: 2
Enter value for dname: eee
old 1: insert into empdept values(&eno,'&ename',&sal,&dno,'&dname')
new 1: insert into empdept values(102,'ragu',20000,2,'eee')
1 row created.
SQL> select * FROM EMPDEPT;
Table created.
Table altered.
Table altered.
Normalizing to 3NF:
Empdept
Eno ename Sal dno dname
3NF
Ed1
eno ename sal dno
Ed2
Dno Dname
Result:
Thus the database was designed using E-R diagram and Normalization.
EXP. NO : 9
To Create Document, column and graph based data using NOSQL database tools.
Example:
{
"_id": 1,
"first_name": "Tom",
"email": "tom@example.com",
"cell": "765-555-5555",
"likes": [
"fashion",
"spas",
"shopping"
],
"businesses": [
{
"name": "Entertainment 1080",
"partner": "Jean",
"status": "Bankrupt",
"date_founded": {
"$date": "2012-05-19T04:00:00Z"
}
},
{
"name": "Swag for Tweens",
"date_founded": {
"$date": "2012-11-01T04:00:00Z"
}
}
]
}
"_id": 2,
"first_name": "Donna",
"email": "donna@example.com",
"spouse": "Joe",
"likes": [
"spas",
"shopping",
"live tweeting"
],
"businesses": [
{
"name": "Castle Realty",
"status": "Thriving",
"date_founded": {
"$date": "2013-11-21T04:00:00Z"
}
}
]
}
Users
Likes
ID user_id like
10 1 fashion
11 1 spas
12 1 shopping
Businesses
RESULT:
Thus the Document, column and graph based data using NOSQL database tools is
successfully completed.