0% found this document useful (0 votes)
17 views

Dbms Procedure

The document discusses implementing SQL commands for creating tables, inserting and manipulating data, applying integrity constraints, and using queries. It covers creating and altering tables, inserting data in different ways, maintaining referential integrity, and using simple, nested, subqueries and joins.

Uploaded by

dharanikam7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Dbms Procedure

The document discusses implementing SQL commands for creating tables, inserting and manipulating data, applying integrity constraints, and using queries. It covers creating and altering tables, inserting data in different ways, maintaining referential integrity, and using simple, nested, subqueries and joins.

Uploaded by

dharanikam7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

K.

Ramakrishnan College of Engineering (Autonomous), Trichy

EX.No.:1(a) MANIPULATE A DATABASE BY CREATING, INSERTING, DELETING,

UPDATING AND RETRIEVING TABLES

AIM:

To execute SQL commands for creating tables, retrieving the values, inserting, updating and
deleting values from the table.

PROCEDURE:

1. CreatingaDatabase

Create is a DDL SQL command used to create a table or a database in relational


database management system.
TocreateadatabaseinRDBMS,create commandisused.

Syntax:

CREATEDATABASE<DB_NAME>;

Example:

CREATEDATABASETest;

The above command will create a database named Test, which will be an empty
schema without any table.

2. CreatingaTable

Create command can also be used to create tables. Now when we create a table, we
have to specify the details of the columns of the tables too. We can specify the names and
data types of various columns in the create command itself.
Syntax:

CREATE TABLE <TABLE_NAME> (column_name1 datatype1, column_name2


datatype2, column_name3 datatype3, column_name4 datatype4);

Example:

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

CREATE TABLE Employee


(
EmployeeNochar(4),
EmployeeNamevarchar2(30),
EmployeeSalnumber(10,2),
EmployeeCityvarchar2(30),
EmployeeDob date
);
The above command will create a table named emp.
3.INSERT SQL command

Data Manipulation Language (DML) statements are used for managing data in
database. DML commands are not auto-committed. It means changes made by DML
command are not permanent to database, it can be rolled back.
Syntax:

INSERT INTO table_nameVALUES(data1, data2,...)

Example:
INSERT INTO Employee(EmployeeNo, EmployeeName, EmployeeSal, EmployeeCity,
EmployeeDob) Values(('1', 'Arvind', 5000, 'Mumbai','23-DEC-1992');

Other Options to insert records, using this technique all the table's columns are required.

INSERT INTO Employee values('2', 'Santosh', 5000, 'Delhi','23-DEC-1994');


4.SelectCommand

The SQL SELECT statement is used to fetch the data from a database table which
returns this data in the form of a result table. These result tables are called result-sets.
Syntax :

Thebasicsyntax oftheSELECTstatementisasfollows −

SELECTcolumn1,column2,columnNFROMtable_name;

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

Here,column1,column2...arethefieldsofatablewhosevaluesyouwanttofetch.Ifyou
wantto fetchall the fieldsavailable in the
field,thenyoucanusethefollowingsyntax.

SELECT*FROMtable_name;
Example:

select * from Employee

selectEmployeeNo, EmployeeName, EmployeeSal,EmployeeCity,EmployeeDob from


Employee
5.UPDATE Command

UPDATEcommand is usedto updateanyrecord of data inatable.

Syntax:

UPDATE table_name SET column_name = new_value WHERE some_condition;

WHERE is used to add acondition to anySQLquery.

Example:

UPDATE Employee SET EmployeeName='KASHISH' WHERE EmployeeNo=1


5.DELETE Command

DELETE command is used to delete data from a table.

Syntax:

DELETE FROM table_name; Example

DELETE FROM EMPLOYEE WHERE employeeNo=1

RESULT:
Thus, the SQL commands for creating tables, retrieving the values, inserting, updating and
deleting values from the table is executed successfully.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

EX.No.:1(b) IMPLEMENTATION OF DDL COMMANDS TO CREATE

,ALTER AND DROP TABLE


AIM:
To execute SQL commands for creating tables, altering and dropping the table from a
database.
PROCEDURE:

1. ALTERcommand

altercommandisusedforalteringthetablestructure,suchas,

1. to add a column to existing table


2. to rename any existing column
3. to change datatype of any column or to modify its size.
4. to drop a column from the table.

ALTERCommand:AddanewColumn

UsingALTERcommand,wecanadd a column toanyexistingtable.

Syntax:

ALTERTABLEtable_nameADD(column_namedatatype);

Example:

ALTER TABLE

ALTERCommand:AddmultiplenewColumns

UsingALTERcommandwecanevenaddmultiplenewcolumnstoanyexistingtable.

Syntax:

ALTERTABLEtable_nameADD(column_name1datatype1,column-name2datatype2, );

ALTERCommand:AddColumnwithdefaultvalue

ALTER command can add a new column to an existing table with a default value too.
The default value is used when no value is inserted in the column.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

Syntax:

ALTERTABLEtable_nameADD(column-name1datatype1DEFAULTsome_value);

ALTERCommand:ModifyanexistingColumn

ALTER command can also be used to modify data type of any existing column.
Syntax:

ALTERTABLEtable_namemodify(column_namedatatype);

ALTERCommand:RenameaColumn

Using ALTER command you can rename an existing column.

Syntax:

ALTER TABLE table_name RENAMEold_column_name TO new_column_name;

ALTERCommand: Drop a Column

ALTERcommandcanalsobeusedtodroporremovecolumns.

Syntax:

ALTERTABLEtable_nameDROP(column_name);

2..TRUNCATEcommand

TRUNCATE command removes all the records from a table. But this command will
not destroy the table's structure. When we use TRUNCATE command on a table its
(autoincrement) primary key is also initialized.
Syntax:

TRUNCATETABLEtable_name;

Example:

TRUNCATE TABLE EMPLOYEE;

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

3.DROPcommand

DROP command completely removes a table from the database. This command will
also destroy the table structure and the data stored in it.
Syntax:

DROP TABLEtable_name;

Example:

DROP TABLE EMPLOYEE;

RESULT:
Thus, the SQL commands for creating tables, altering and dropping the table from a database
was executed successfully.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

EX.No.:2 IMPLEMENTATION OF DML COMMANDS FOR DATA INSERTION USING


DIFFERENT WAYS, INTEGRITY CONSTRAINTS AND TRUNCATE

AIM:

To implement commands for data insertion using different ways, integrity constraints and
truncate commands

PROCEDURE:

1.DIFERENT WAYS TO INSERT A DATA INTO TABLE:

Method 1: The first way specifies both the column names and the values to be inserted.
Syntax:
INSERT INTO table-name (column-names) VALUES (values) ;

Method 2:Insert Data Only in Specified Columns.

Method 3:If you are adding the values for all the columns of the table, you do not need to
specify the column names in the SQL query. However, make sure the order of the values is in
the same order as the columns in the table.

2.INTEGRITY CONSTRAINTS:

• The Set of rules which is used to maintain the quality of information are known as
integrity constraints.

• Integrity constraints make sure about data intersection, update and so on.

• Integrity constraints can be understood as a guard against unintentional damage to the


database.

Domain Constraint
• The Definition of an applicable set of values is known as domain constraint.

• Strings, character, time, integer, currency, date etc. Are examples of the data type of
domain constraints

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

Entity Integer Constraint


• Entity Integrity Constraints states that the primary value key cannot be null because
the primary value key is used to find out individual rows in relation and if the value of
the primary key is null then it is not easy to identify those rows.

• There can be a null value in the table apart from the primary key field.

Referential Integrity Constraint


1. Referential Integrity Constraint is specific between two tables.
2. A foreign key in the 1st table refers to the primary key of the 2nd table, in this case
each value of the foreign key in the 1st table has to be null or present in the 2nd table.
Key Constraints
• The Entity within its entity set is identified uniquely by the key which is the entity set.

• There can be a number of keys in an entity set but only one will be the primary key
out of all keys. In a relational table a primary key can have a unique as well as a null
value.

3.TRUNCATE:

TRUNCATE command removes all the records from a table. But this command will
not destroy the table's structure. When we use TRUNCATE command on a table its
(autoincrement) primary key is also initialized.
Syntax:

TRUNCATETABLEtable_name;

Example:

TRUNCATE TABLE EMPLOYEE;

RESULT:
Thus the commands for data insertion using different ways, integrity constraints and truncate
has been implemented and executed successfully.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

EX.No.:3 MANIPULATE TABLES IN A DATABASE USING SIMPLE QUERIES,


NESTED QUERIES, SUB QUERIES AND JOINS

AIM:

To create simple queries,nestedqueries,sub


queries and joins using tables.

PROCEDURE:

STEP1:Starttheprogram.
STEP 2: Create two different tables with its
essential attributes.

STEP3:Insert attributevalues into the table.


STEP 4: Create the Nested query and join from the above created table.
STEP 5: Execute Command andextractinformation fromthe tables.
STEP 6:Stopthe program.

RESULT:

Thus the simple queries, nestedqueries ,sub queries and joins has been executed
successfully.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

EX.No.:4 IMPLENTATION OF AGGREGATION FUNCTIONS, GROUPING AND


ORDERING COMMANDS TO MANIPULATE TABLES IN A DATABASE

AIM:

To implement on aggregation functions, grouping and ordering commands to manipulate


tables in a database.

PROCEDURE:

An aggregate function allows you to perform a calculation on a set of values to return a


single scalar value. We often use aggregate functions with the
GROUP BY and HAVING clauses of the SELECT statement.

The following are the most commonly used SQL aggregate functions:

AVG – calculates the average of a set of values.

COUNT – counts rows in a specified table or view.

MIN – gets the minimum value in a set of values.

MAX – gets the maximum value in a set of values.

SUM – calculates the sum of values.

The SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange
identical data into groups. This GROUP BY clause follows the WHERE clause in a SELECT
statement and precedes the ORDER BY clause.

Syntax:

SELECT column1, column2

FROM table_name

WHERE [ conditions ]

GROUP BY column1, column2

ORDER BY column1, column2

The ORDER BY keyword is used to sort the result-set in ascending or descending order.

The ORDER BY keyword sorts the records in ascending order by default. To sort the
records in descending order, use the DESC keyword.

Syntax;

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

SELECT column1, column2, ...


FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

RESULT:

Thus the aggregation functions, grouping and ordering commands to manipulate tables in a
database has been implemented and executes successfully.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

EX.No.:5a) IMPLEMENT DCL COMMANDS TO SET AND REVOKE PRIVILEGES

AIM:

To implement DCL Commands to set and revoke privileges.


PROCEDURE:

Data control language (DCL) is used to access the stored data. It is mainly used for
revoke and to grant the user the required access to a database. In the database, this language
does not have the feature of rollback.
It is a part of the structured query language (SQL).
It helps in controlling access to information stored in a database. It complements the
data manipulation language (DML) and the data definition language (DDL).
It is the simplest among three commands.
It provides the administrators, to remove and set database permissions to desired users
as needed.
These commands are employed to grant, remove and deny permissions to users for
retrieving and manipulating a database.
GRANT Command
It is employed to grant a privilege to a user. GRANT command allows specified users
to perform specified tasks
Syntax
GRANT privilege_name on objectname to user; Here, privilege names
are SELECT,UPDATE,DELETE,INSERT,ALTER,ALL objectname is
table name
user is the name of the user to whom we grant privileges
REVOKE Command
It is employed to remove a privilege from a user. REVOKE helps the owner to cancel
previously granted permissions.
Syntax
REVOKE privilege_name on objectname from user; Here, privilege
names are SELECT,UPDATE,DELETE,INSERT,ALTER,ALL
object name is table name user is the name of the user whose privileges are removing
5(a).IMPLEMENT DCL COMMANDS TO SET AND REVOKE PRIVILEGES

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

RESULT:

Thus the DCL Commands to set and revoke privileges has been executed
successfully.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

EX.No.:5b) IMPLEMENTATION OF TCL COMMANDS SAVE-POINT, ROLL BACK


AND ROLL BACK TO COMMANDS

AIM:
To execute Transactional Control Commands such as Commit, Rollback and Savepoint.

ALGORITHM:

STEP 1: Start the DMBS.

STEP 2: Connect to the existing database (DB)

STEP 3: Create the table with its essential attributes.

STEP 4: Insert record values into the table or perform any kind of DML operation.

STEP 5: Create the SAVE POINTs for some set of statement on the transaction of database
object. STEP 6: Use the COMMIT command to save the effect of the previous command
operation except DDL command

STEP 7: Use the ROLLBACK TO SP_LABLE / ROLLBACK command for restore the
database status up to the save point STEP 8: Check the status of the database.

STEP 9: Stop the DBMS.

THEORY:

Transaction Control Language(TCL) commands are used to manage transactions in the


database.These are used to manage the changes made to the data in a table by DML
statements. It also allowsstatements to be grouped together into logical transactions.

COMMIT command

COMMIT command is used to permanently save any transaction into the database.

To avoid that, we use the COMMIT command to mark the changes as permanent.

Syntax:

COMMIT;

ROLLBACK command

This command restores the database to last commited state. It is also used with

SAVEPOINT command to jump to a savepoint in an ongoing transaction.

Syntax:
ROLLBACK TO savepoint_name;

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

SAVEPOINT command

SAVEPOINT command is used to temporarily save a transaction so that you can rollback to
that point when ever required.

Syntax:

SAVEPOINT savepoint_name;

RESULT:
Thus the Transactional Control Commands such as Commit, Rollback and Savepoint has
been executed successfully.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

EX.No.:6 IMPLEMENTATION OF PL/SQL USING CONDITIONAL


STATEMENTS

AIM:
To implement the conditional selection statement in PL/SQL block.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Create the PL/SQL Block with necessary blocks. STEP
3: Declare the necessary variable in the declaration section
STEP 4: write the main program logics in the begin block.
STEP 5: if you want to access the table use the SQL statement.
STEP 6: if you want to solve any exception, write the exception name with WHEN
statement
STEP 7: Execute the PL/SQL block.
STEP 8: Give the input values or validate the information from the tables.
STEP 9: Stop the program.

The PL/SQL stands for Procedural Language extensions to Structured Query Language.
Basically, SQL is used to perform basic operations of creating a database, storing data in the
database, updating data in the database, retrieving the stored data of database, etc, whereas
PL/SQL is a fully Structured Procedural language which enables the developer to combine
the powers of SQL with its procedural statements.

PL/SQL Block
In a PL/SQL program, code is written in blocks. Each PL/SQL block has 3 sections, which
are:

1. Declare section

2. Begin section

3. Exception section

Followed by END statement at the end

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

PL/SQL Block

PL/SQL block creates the structured logical blocks of code that describes the process to be
executed. Such a block consists of SQL statements and PL/SQL instructions that are then
passed to the oracle engine for execution. PL/SQL block consists of the following four sections:

• DECLARE Section:

PL/SQL code starts with a declaration section in which memory variables and other
oracle objects like cursor, triggers etc can be declared and if required can be initialized
as well. Once declared/initialised we can use them in SQL statements for data
manipulation. As it is not necessary that we would require variables etc in every
PL/SQL code, hence this section is an optional section.

• BEGIN Section:

This section contains the SQL and PL/SQL statements that are required to be executed
and contains the main logic. This section is responsible for handling the data retrieval
and manipulation, may be working with branching, can use looping and conditional
statements, etc.

• EXCEPTION Section:

This section is optional. It is mainly used to handle the errors that may occur between
BEGIN and EXCEPTION sections.

• END Section:

This section is the indication of the end of the PL/SQL block.

PL/SQL Conditional Statements


Decision making statements are those statements which are in charge of executing a statement
out of multiple given statements based on some condition. The condition will return either true
or false. Based on what the condition returns, the associated statement is executed.

For example, if someone says, If I get 40 marks, I will pass the exam, else I will fail. In this
case condition is getting 40 marks, if its true then the person will pass else he/she will fail.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

This can be logically implemented in PL/SQL block using decision making statements.

The decision making statements in PL/SQL are of two types:

1. If Else statements

2. Case statement

Let's see them all one by one with examples.

PL/SQL: if Statement

The if statement, or the if...then statement can be used when there is only a single condition to
be tested. If the result of the condition is TRUE then certain specified action will be performed
otherwise if it is FALSE then no action is taken and the control of program will just move out
of the if code block.

Syntax:

if<test_condition> then
body of action
end if;

PL/SQL: if...then...else statement

Using this statement group we can specify two statements or two set of statements, dependent
on a condition such that when the condition is true then one set of statements is executed and
if the condition is false then the other set of statements is executed.

Syntax: if<test_condition> then


statement 1/set of statements 1 else
statement 2/set of statements 2
end if;

PL/SQL: if...then...elsif...else statement

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

It is used to check multiple conditions. Sometimes it is required to test more than one condition
in that case if...then...else statement cannot be used. For this purpose, if...then...elsif...else
statement is suitable in which all the conditions are tested one by one and whichever condition
is found to be TRUE, that block of code is executed. And if all the conditions result in FALSE
then the else part is executed.

In the following syntax, it can be seen firstly condition1 is checked, if it is true, the statements
following it are executed and then control moves out of the complete if block but if the
condition is false then the control checks condition2 and repeats the same process. If all the
conditions fail then the else part is executed.

Syntax:

if<test_condition1> then
body of action
elsif<test_condition2>then
body of action
elsif<test_condition3>then
body of action
...
...
...
else
body of action
end if;

PL/SQL: Case Statement

If we try to describe the case statement in one line then, then we can say means "one out of
many". It is a decision making statement that selects only one option out of the multiple
available options.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

It uses a selector for this purpose. This selector can be a variable, function or procedure that
returns some value and on the basis of the result one of the case statements is executed. If all
the cases fail then the else case is executed.

Syntax:
CASE selector when value1 then
Statement1; when value2 then
Statement2;
...
...
else statement; end
CASE;

RESULT:
Thus the conditional selection statement in PL/SQL block has been verified and executed
successfully.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

EX.No.:7 IMPLEMENTATION OF IMPLICIT AND EXPLICIT


CURSOR TO MANIPULATE A TABLE IN PL/SQL

AIM:

To manipulate a table using Implicit and Explicit Cursors.

PROCEDURE:

PL/SQL - Cursors
A cursor is a pointer to this context area. PL/SQL controls the context area through a cursor.
A cursor holds the rows (one or more) returned by a SQL statement. The set of rows the cursor
holds is referred to as the active set.
There are two types of cursors.

• Implicit cursors
• Explicit cursors

Implicit Cursors
Implicit cursors are automatically created by Oracle whenever an SQL statement is executed,
when there is no explicit cursor for the statement. Programmers cannot control the implicit
cursors and the information in it.
Whenever a DML statement (INSERT, UPDATE and DELETE) is issued, an implicit cursor
is associated with this statement. For INSERT operations, the cursor holds the data that needs
to be inserted. For UPDATE and DELETE operations, the cursor identifies the rows that would
be affected.
In PL/SQL, you can refer to the most recent implicit cursor as the SQL cursor, which
always has attributes such as %FOUND, %ISOPEN, %NOTFOUND, and
%ROWCOUNT. The
SQL cursor has additional attributes, %BULK_ROWCOUNT and
%BULK_EXCEPTIONS, designed for use with the FORALL statement. The following
table provides the description of the most used attributes −
S.No Attribute & Description

1
%FOUND
Returns TRUE if an INSERT, UPDATE, or DELETE statement affected one or more rows or
a SELECT INTO statement returned one or more rows. Otherwise, it returns FALSE.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

%NOTFOUND

2 The logical opposite of %FOUND. It returns TRUE if an INSERT, UPDATE, or DELETE


statement affected no rows, or a SELECT INTO statement returned no rows. Otherwise, it
returns FALSE.

%ISOPEN
3 Always returns FALSE for implicit cursors, because Oracle closes the SQL cursor
automatically after executing its associated SQL statement.

%ROWCOUNT
4 Returns the number of rows affected by an INSERT, UPDATE, or DELETE statement, or
returned by a SELECT INTO statement.

Any SQL cursor attribute will be accessed as sql%attribute_name as shown below in the
example.
Explicit Cursors
Explicit cursors are programmer-defined cursors for gaining more control over the context
area. An explicit cursor should be defined in the declaration section of the PL/SQL Block. It
is created on a SELECT Statement which returns more than one row.
The syntax for creating an explicit cursor is −

CURSOR cursor_name IS select_statement;


Working with an explicit cursor includes the following steps −

• Declaring the cursor for initializing the memory


• Opening the cursor for allocating the memory
• Fetching the cursor for retrieving the data
• Closing the cursor to release the allocated memory
Declaring the Cursor
Declaring the cursor defines the cursor with a name and the associated SELECT statement.
For example −
CURSOR c_customers IS SELECT id, name, address FROM customers;
Opening the Cursor
Opening the cursor allocates the memory for the cursor and makes it ready for fetching the
rows returned by the SQL statement into it. For example, we will open the above defined
cursor as follows − OPEN c_customers;
Fetching the Cursor

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

Fetching the cursor involves accessing one row at a time. For example, we will fetch rows
from the above-opened cursor as follows − FETCH c_customers INTO c_id, c_name, c_addr;
Closing the Cursor
Closing the cursor means releasing the allocated memory. For example, we will close the
above-opened cursor as follows −
CLOSE c_customers;

RESULT:

Thus the manipulation of a table using Implicit and Explicit Cursors has been verified and
executed successfully.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

EX.No.:8 IMPLEMENTATION OF CREATING AND DROPING A


TRIGGER IN PL/SQL

AIM:

To create and drop triggers in PL/SQL

PROCEDURE:

TRIGGER :A Trigger is a stored procedure that defines an action that the database
automatically take when some database-related event such as Insert, Update or Delete occur.

TYPES OF TRIGGERS:The various types of triggers are as follows,

• Before: It fires the trigger before executing the trigger statement.


• After: It fires the trigger after executing the trigger statement.
• For each row: It specifies that the trigger fires once per row.
• For each statement: This is the default trigger that is invoked. It specifies that the
trigger fires once per statement.

VARIABLES USED IN TRIGGERS:

:new :old

These two variables retain the new and old values of the column updated in the database.

The values in these variables can be used in the database triggers for data manipulation

Syntax:

Create or replace trigger <trg_name>Before /After Insert/Update/Delete

[ofcolumn_name, column_name….] on<table_name>

[for each row]

[when

condition] begin

---statement end;

Create a trigger that insert current user into a username column of an existing table

Procedure for doing the experiment:

1. Create a table student4 with name and username as arguments

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

2. Create a trigger for each row that insert the current user as user name into a table 3.

Execute the trigger by inserting value into the table

RESULT:
Thus the creation and dropping of triggers was performed and executed successfully

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

EX.No.:9 IMPLEMENTATION OF PROCEDURE AND FUNCTION


MANIPULATE A DATABASE USING PL/SQL

AIM:

To develop procedures and function for various operations

PROCEDURE:

A procedure is a block that can take parameters (sometimes referred to as arguments) and be
invoked. Procedures promote reusability and maintainability. Once validated, they can be
used in number of applications. If the definition changes, only the procedure are affected, this
greatly simplifies maintenance. Modularized program development: · Group logically related
statements within blocks. · Nest sub-blocks inside larger blocks to build powerful programs.
· Break down a complex problem into a set of manageable well defined logical modules and
implement the modules with blocks.

KEYWORDS AND THEIR PURPOSES

REPLACE: It recreates the procedure if it already exists.

PROCEDURE: It is the name of the procedure to be created.

ARGUMENT: It is the name of the argument to the procedure. Parenthesis can be omitted
if no arguments are present.

IN: Specifies that a value for the argument must be specified when calling the procedure ie.,
used to pass values to a sub-program. This is the default parameter.

OUT: Specifies that the procedure passes a value for this argument back to it‟s calling
environment after execution ie. used to return values to a caller of the sub-program. INOUT:
Specifies that a value for the argument must be specified when calling the procedure and that
procedure passes a value for this argument back to it‟s calling environment after execution.

RETURN: It is the data type of the function‟s return value because every function must
return a value, this clause is required.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

PROCEDURES

Syntax : create or replace procedure <procedure name> (argument {in,out,inout}

datatype ) {is,as} variable declaration; constant declaration; begin

PL/SQL subprogram body; exception

exception PL/SQL block; end;

FUNCTIONS

Syntax: create or replace function <function name> (argument in datatype,……) return

datatype

{is,as}

variable declaration; constant

declaration; begin

PL/SQL subprogram body; exception

exception PL/SQL block; end;

RESULT:

Thus the procedures and function for various operations was developed and executed
successfully.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

EX.No.:10 IMPLEMENTATION OF HANDLING EXCEPTION IN QUERY

AIM:

To handle exceptions in PL/SQL Program

PROCEDURE:

An error occurs during the program execution is called Exception in PL/SQL.

PL/SQL facilitates programmers to catch such conditions using exception block in the
program and an appropriate action is taken against the error condition.

There are two type of exceptions:

o System-defined Exceptions o User-defined Exceptions

SYNTAX:

DECLARE

<declarations section>

BEGIN

<executable command(s)>

EXCEPTION

<exception handling goes here >

WHEN exception1 THEN

exception1-handling-statements

WHEN exception2 THEN

exception2-handling-statements

WHEN exception3 THEN

exception3-handling-

statement........

WHEN others THEN exception3-handling-statements

END;

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

RESULT:
Thus the exceptions are handled in PL/SQL Program verified and executed
successfully.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

EX.No.:11 DESIGNING A DATABASE USING ER MODELLING AND


NORMALIZATION

AIM:

To design a database using ER Modeling and Normalization

PROCEDURE:

NORMALIZATION

Normalization is the analysis of functional dependencies between attributes/data items of user


views. It reduces a complex user view to a set of small and stable subgroups of the fields and
relations.

This process helps to design a logical data model known as conceptual data model.

There are different normal forms

1. First Normal Form(1NF)

2. Second Normal Form(2NF)

3. Third Normal Form(3NF)

FIRST NORMAL FORM (1NF)

1NF states that the domain of an attribute must include only atomic values and that value of
any

attribute in a tuple must be a single value from the domain of that attribute. Hence 1NF
disallows multivalued attributes, composite attributes. It disallows “relations within
relations”.

SECOND NORMAL FORM (2NF)

A relation is said to be in 2NF if it is already in 1NF and it has no partial dependency. 2NF is
based on the concept of full functional dependency.

A functional dependency(FD) x→y is fully functional dependency is (x-(A))→y does not


hold dependency any more if A→x.

A functional dependency x→y is partial dependency if A can be removed which does not
affect the dependencyie (x-

(A))→y holds.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

A relation is in 2NF if it is in 1NF and every non-primary key attribute is fully and
functionally dependent on primary key.

A relation is in 1NF will be in the 2NF if one of the following conditions is satisfied:

1. The primary key consist of only one attribute.

2. No non-key attribute exist in relation ie all the attributes in the relation are components of
the primary key.

Every non-key attribute is functionally dependent on full set of primary key attributes.

THIRD NORMAL FORM (3NF)

A relation is said to be in 3NF if it is already in 2NF and it has no transitive dependency.

A FD x→y in a relation schema R is a transitive dependency if there is a set of attributes z


that is neither a candidate key nor a subset of any key of the relation and both x→z and z→y
hold.

Entity relationship diagram (ERD):

An entity relationship diagram (ERD) shows the relationships of entity sets stored in a
database. An

entity in this context is an object, a component of data. An entity set is a collection of


similar entities.

These entities can have attributes that define its properties. Notation

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

RESULT:

Thus the database has been designed using ER Modeling and Normalization successfully.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

EX.No.:12 DEVELOPING AN ENTERPRISE APPLICATION USING USER AND

AIM:

To design the payroll processing system in visual basic using ORACLE as backend

PROCEDURE :

1. Create table with following fields.

NAMENULL? TYPE

EID NUMBER(10)

ENAME VARCHAR2(1 0)

DES VARCHAR2(10)

BASICPAY NUMBER(10)

HRA NUMBER(10)

DA NUMBER(10) MA

NUMBER(10)

GROSSPAY NUMBER(10)

DEDUCTION NUMBER(10)

NETPAY NUMBER(10)

2. Insert all possible values into the table.

3. Enter commit work command.

4. Go to Start --> Settings --> Control Panel --> Administrative tools --> Data
Sources(ODBC) --> User DSN --> Add --> Select ORACLE database driver --> OK.

5. One new window will appear. In that window, type data source name as table name
created in ORACLE. Type user name as second csea.

Procedure for adodc in visual basic:

1. In visual basic create tables, command buttons and then text boxes.

2. In visual basic, go to start menu.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

3. Projects --> Components --> Microsoft ADO Data Control 6.0 for OLEDB --> OK.

4. Now ADODC Data Control available in tool box.

5. Drag and drop the ADODC Data Control in the form.

6. Right click in ADODC Data Control, then click ADODC properties.

7. One new window will appear.

8. Choose general tab, select ODBC Data Sources name as the table name created in
ORACLE

9. Choose authentication tab and select username password as secondcsea and


secondcsea

10. Choose record name-->select command type as adcmdTable.

11. Select table or store procedure name as table created in ORACLE.

12. Click Apply-->OK

13. Set properties of each text box.

14. Select the data source as ADODC1.

15. Select the Data field and set the required field name created in table

RESULT:

Thus the payroll processing system was designed in Visual Basic using
ORACLE as backend and the output was verified

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

EX.No.:13 IMPLEMENTATION OF CREATING INDEX

AIM:

To create and drop index in a table

PROCEDURE:

Indexes are special lookup tables that the database search engine can use to speed up data
retrieval. An index is a pointer to data in a table. An index in a database is very similar to an
index in the back of abook. An index helps speed up SELECT queries and WHERE clauses,
but it slows down data input, with UPDATE and INSERT statements. Indexes can be created
or dropped with no effect on the data. Index in sql is created on existing tables to retrieve the
rows quickly. When there are thousands of records in a table, retrieving information will take
a long time. When an index is created, it first sorts the data and then it assigns a ROWID for
each row.

An index can be created in a table to find data more quickly and efficiently.

The users cannot see the indexes, they are just used to speed up searches/queries

1.Syntax to create Index


CREATE INDEX index_name ON table_name (column_name1,column_name2...);

2.Syntax to create SQL unique index

CREATE UNIQUE INDEX index_name ON table_name


(column_name1, column_name2...);

•index_name is the name of the INDEX.

•table_name is the name of the table to which the indexed column belongs.

•column_name1, column_name2..is the list of columns which make up the INDEX.

3.The Drop Index Command

An index can be dropped using SQL DROP command. Care should be taken when dropping
an index because performance may be slowed or improved.

DROP INDEX index_name;

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

RESULT:

Thus the index has been created and dropped in a table has been implemented and executed
successfully.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

EX.No.:14 INTRODUCTION TO NOSQL DATABASES USING MONGODB

Aim:
The objective is to introduce some features of non-relational or NoSQL databases
using MongoDB. MongoDB stores data in JSON objects which it calls documents and uses a
custom language for queries.
Installation
Option 1:
1. Set up a free cluster on Mongo Atlas by following the instructions here:
https://docs.atlas.mongodb.com/tutorial/deploy-free-tier-cluster/
2. Install the mongo shell on your machine and connect to your cluster following the
instructions on the dashboard.

Option 2:
1. Download and setup MongoDB for your OS following these instructions
https://www.mongodb.com/download-center/community
2. Start the server. Then use mongo shell to connect your
server: https://docs.mongodb.com/manual/mongo/ Preparation The
instructions below assume you have MongoDB installed and started on your local machine.
For Atlas, create a database and a user by following the instructions on the dashboard.
1. Once you have installed and started the Mogodb you can log into
your server as root. mongo -u root
2. To create a new database do use mongolab 3. Now create a new
user to access the database.
db.createUser({user:"e14xxx", pwd:"abc123", roles:[{role: "dbOwner" , db:"mongolab"}]})
4. Log out of the mongo shell and log back in using the user you created. mongo
localhost/mongolab -u e14xxx

Data Validation:
Document databases are a flexible alternative to the predefined schemas of relational
databases. Each document in a collection can have a unique set of fields, and those fields
can be added or removed from documents once they are inserted. Since the data fields can
be changed for each document in a collection, data validation is extremely important to
ensure queries run predictability.

Department of Information Technology Page


K. Ramakrishnan College of Engineering (Autonomous), Trichy

To create the customer collection with a custom data validation function, enter the following

RESULT:

Thus the Data validation in NoSQL databases using MongoDB has been implemented and
executed successfully.

Department of Information Technology Page

You might also like