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

Dbms Lab Writting Documnet Edited

Uploaded by

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

Dbms Lab Writting Documnet Edited

Uploaded by

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

K.

Ramakrishnan College of Engineering, Trichy

EX.NO-1
DATE:
DATA DEFINITION COMMANDS, DATA MANIPULATION COMMANDS FOR INSERTING,
DELETING, UPDATING AND RETRIEVING TABLES AND TRANSACTION CONTROL
STATEMENTS
AIM:

To Study And Execute Data Definition Commands, Data Manipulation Commands For
Inserting, Deleting, Updating And Retrieving Tables And Transaction Control Statements
SQL commands

o SQL commands are instructions. It is used to communicate with the database. It is also used to perform
specific tasks, functions, and queries of data.
o SQL can perform various tasks like create a table, add data to tables, drop the table, modify the table, set
permission for users.

Types

Data definition language (DDL)


DDL changes the structure of the table like creating a table, deleting a table, altering a table, etc. All the
command of DDL are auto-committed that means it permanently save all the changes in the database.

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

o CREATE
o ALTER
o DROP
o TRUNCATE

a. CREATE :It is used to create a new table in the database.

Syntax:

CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);  

b. DROP: It is used to delete both the structure and record stored in the table.

Syntax

DROP TABLE TABLE_NAME  ;  

c. ALTER: It is used to alter the structure of the database. This change could be either to modify the characteristics
of an existing attribute or probably to add a new attribute.

Syntax:

ALTER TABLE table_name ADD (COLUMN_NAME DATATYPES[,....]); 

ALTER TABLE table_name DROP (COLUMN_NAME); 

ALTER TABLE table_name MODIFY (COLUMN_NAME DATATYPES[,....]); 

d.TRUNCATE: It is used to delete all the rows from the table and free the space containing the table.

Syntax:
1. TRUNCATE TABLE table_name;  

Data Manipulation Language

INSERT
o UPDATE
o DELETE

a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the row of a table.

Syntax:

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

INSERT INTO TABLE_NAME  VALUES (value1, value2, value3, .... valueN); 
INSERT INTO TABLE_NAME   VALUES (&integervalue1, ‘&varcharvalue2’, value3, .... valueN); 

b. UPDATE: This command is used to update or modify the value of a column in the table.

Syntax:

UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE CONDITION]   

c. DELETE: It is used to remove one or more row from a table.

Syntax:

DELETE FROM table_name [WHERE condition];  
Transaction Control Language
o COMMIT
o ROLLBACK
o SAVEPOINT
a. Commit: Commit command is used to save all the transactions to the database.
Syntax:
COMMIT;  
Rollback: Rollback command is used to undo transactions that have not already been saved to the database.
Syntax:
ROLLBACK;  
SAVEPOINT: It is used to roll the transaction back to a certain point without rolling back the entire transaction.

Syntax:

SAVEPOINT SAVEPOINT_NAME;  

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

RESULT

Thus the Data Definition Commands, Data Manipulation Commands For Inserting, Deleting, Updating And
Retrieving Tables And Transaction Control Statements were implemented and the output was verified.

EX.NO-2
DATE:
DATABASE QUERYING – SIMPLE QUERIES, NESTED QUERIES, SUB QUERIES AND JOINS

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

AIM
To implement Database Querying – Simple queries, Nested queries, Sub queries and Joins.

DESCRIPTION
THE ORACLE TABLE – DUAL

Dual is a small oracle table which consists of only one row and one column and contains the value X in
that column.

SELECT
This command is used to display the contents of the table or those of a particular column.

RENAME
This command renames the name of the table.

ARITHMETIC OPERATIONS
Various operations such as addition, multiplication, subtraction and division can be performed using the
numbers available in the table.
DISTINCT
This keyword is used along with select keyword to display unique values from the specified column. It
avoids duplicates during display.

ORDER BY CLAUSE
The order by clause arranges the contents of the table in ascending order (by default) or in descending
order (if specified explicitly) according to the specified column.

CONCATENATION OPERATOR
This combines information from two or more columns in a sentence according to the format specified.

LOGICAL OPERATORS
AND : The oracle engine will process all rows in a table and displays the result only when all of the
conditions specified using the AND operator are specified.
OR : The oracle engine will process all rows in a table and displays the result only when any of the
conditions specified using the OR operators are satisfied.

NOT : The oracle engine will process all rows in a table and displays the result only when none
of the conditions specified using the NOT operator are specified.

BETWEEN : In order to select data that is within a range of values, the between operator is used.
(AND should be included)

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

PATTERN MATCH
 LIKE PREDICATE : The use of like predicate is that it allows the comparison of one string value
with another string value, which is not identical.
 IN AND NOT IN PREDICATE : The arithmetic operator = compares a single value to another
single value. In case a value needs to be compared to a list of values then the in predicate is
used.The not in predicate is the opposite of the in predicate. This will select all the rows whose
values do not match all of the values in the list.

NUMERIC FUNCTIONS
ABS: It returns the absolute value of „n.
POWER: It returns m raised to nth power. n must be an integer else an error is returned.
ROUND: It returns n rounded to m places right of the decimal point. SQRT: It returns square root of n. n
should be greater than zero.

STRING FUNCTIONS
LOWER:
It returns char with letters in lower case.
INITCAP: It returns char with the first letter in upper case.
UPPER: It returns char with all letters forced to upper case.
SUBSTR: It returns a portion of char beginning at character m, exceeding up to n characters. If n is
st
omitted result is written up to the end character. The 1 position of char is one.
LENGTH: It returns the length of char
st
LTRIM: It removes characters from the left of char with initial characters removed up to the 1
character not in set.
RTRIM: It returns char with final characters removed after the last character not in the set. Set is
optional. It defaults to spaces.
LPAD: It returns char1, left padded to length n with the sequence of characters in char2. char2
defaults to blanks.
RPAD: It returns char1, right padded to length n with the characters in char2, replicated as
many times as necessary. If char2 is omitted, it is padded with blanks.

AGGREGATE FUNCTIONS
AVG (N): It returns average value of n ignoring null values.

MIN (EXPR): It returns minimum value of the expression.

COUNT (EXPR): It returns the number of rows where expression is not null.

COUNT (*): It returns the number of rows in the table including the duplicates and those with null
values.

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

MAX (EXPR): It returns maximum value of the expression.

SUM(N): It returns sum of values of n.

CONVERSION FUCTIONS

TO_NUMBER(CHAR): It converts the char value containing a number to a value of number data type.

TO_CHAR(N,FMT): It converts a value of number data type to a value of char data type, using the
optional format string. It accepts a number n and a numeric format fmt in which the number has to
appear. If fmt is omitted, n is converted to a char value exactly long enough to hold significant digits.

TO_CHAR(DATE, FMT): It converts a value of data type to char value. It accepts a date as well as
the format in which the date has to appear. Fmt must be a date format. If fmt is omitted, date is the
default date format.
DATE FUNCTIONS
SYSDATE : The sysdate is a pseudo column that contains the current date and time. It requires no
arguments when selected from the table dual and returns the current date. ADD_MONTHS(D,N): It
returns date after adding the number of months specified with the function.

LAST_DAY(D): It returns the last date of the month specified with the function

MONTHS_BETWEEN(D1,D2): It returns number of months between D1 and D2.

NEXT_DAY(DATE, CHAR): It returns the date of the first week day named by char
. char must be a day of the week.

GROUP BY CLAUSE
The group by clause is another section of the select statement. This optional class tells oracle to group
rows based on distinct values that exists for specified columns.

HAVING CLAUSE
The having clause can be used in conjunction with the group by clause. Having imposes a condition on
the group by clause, which further filters the groups created by the group by clause.

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

SET OPERATIONS
UNION CLAUSE: Multiple queries can be put together and their output combined using the union
clause. The union clause merges the output of two or more queries into a single set of rows and columns.
INTERSECT CLAUSE: Multiple queries can be put together and their output can be combined using the
intersect clause. The intersect clause outputs only rows produced by both the queries intersected. The
output in an intersect clause will include only those rows that are retrieved by both the queries.

JOIN OPERATIONS
INNER JOIN/ NATURAL JOIN/ JOIN: It is a binary operation that allows us to combine certain
selections and a Cartesian product into one operation.
OUTER JOIN: It is an extension of join operation to deal with missing information.
Left Outer Join: It takes tuples in the left relation that did not match with any tuple in the right relation,
pads the tuples with null values for all other attributes from the right relation and adds them to the result of the
natural join.
Right Outer Join: It takes tuples in the right relation that did not match with any tuple in the left relation,
pads the tuples with null values for all other attributes from the left relation and adds them to the result of the
natural join.
Full Outer Join: It combines tuples from both the left and the right relation and pads the tuples with
null values for the missing attributes and them to the result of the natural join.

Syntax:
select table1.col1,table1.col2, table2.col1 from table1 join on table 2 where table1.col1=table2.coml2;

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

RESULT

Thus all the basic queries, join and nested queries were executed and the output was verified.

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

EX.NO-3
DATE:
VIEWS, SEQUENCES, SYNONYMS

AIM:
To create Views, Sequence, Indexes, Save point and Synonyms in SQL.

DESCRIPTION:
VIEWS
In SQL, a view is a virtual table based on the result-set of an SQL statement. A view contains rows and
columns, just like a real table. The fields in a view are fields from one or more real tables in the database.

SQL create view


CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition

SQL updating a view


Update view name set field name=value where condition

SQL create or replace view


CREATE OR REPLACE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE
condition.

SQL drop view


DROP VIEW view-name;

INDEXES
• 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
Syntax to create Index
CREATE INDEX index_name ON table_name (column_name1,column_name2...);
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.
Department of Computer Science and Engineering Page No.
K. Ramakrishnan College of Engineering, Trichy

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


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;

SEQUENCE
SYNTAX
CREATE SEQUENCE sequence_name
MINVALUE value
MAXVALUE value

START WITH value

INCREMENT BY value

CACHE value;

Drop Sequence: Syntax: DROP SEQUENCE sequence_name

SYNONYMS
CREATE SYNONYM offices

FOR hr.locations;

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

RESULT: Thus the Views, Sequence, Indexes, and Synonyms are executed successfully.

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

DATABASE PROGRAMMING: IMPLICIT AND EXPLICIT CURSORS


Ex.No.4

AIM:
To implement database programming for implicit and explicit cursors in sql.

CURSOR:

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.

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.

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.

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

syntax:
cursor cursor_name is select statement
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
syntax:
open cursor_name;
Fetching the Cursor
Fetching the cursor involves accessing one row at a time. For example, we will fetch rows from the above-opened
cursor as follows −
syntax:
fetch cursor_name into attributes;
Closing the Cursor
Closing the cursor means releasing the allocated memory. For example, we will close the above-opened cursor as
follows −
syntax:
close cursor_name;

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

RESULT:
Thus database programming for implicit and explicit cursors are executed successfully

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

PROCEDURES AND FUNCTIONS


Ex.No.5

AIM:
To implement PL/SQL programs that execute the concept of functions and procedures
DEFINITION
A procedure or function is a logically grouped set of SQL and PL/SQL statements that perform a specific
task. They are essentially sub-programs. Procedures and functions are made up of,
Declarative part
Executable part

Optional exception handling part

These procedures and functions do not show the errors.

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. Paranthesis 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 to use
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 82 environment after execution.

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

PROCEDURES – SYNTAX
create or replace procedure <procedure name> (argument {in,out,inout} datatype ) {is,as} variable declaration;

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

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;

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

RESULT:

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

Thus PL/SQL programs for concept of functions and procedures are executed successfully

TRIGGERS
Ex.No.6
AIM
To study and implement the concept of triggers.

DEFINITION
A trigger is a statement that is executed automatically by the system as a sideeffect of a modification to the
database. The parts of a trigger are,
Trigger statement: Specifies the DML statements and fires the trigger body. It also specifies the table to which the
trigger is associated.

Trigger body or trigger action: It is a PL/SQL block that is executed when the triggering statement is used.

Trigger restriction: Restrictions on the trigger can be achieved

The different uses of triggers are as follows,


To generate data automatically
To enforce complex integrity constraints
To customize complex securing authorizations
To maintain the replicate table
To audit data modifications

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

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

SYNTAX
create or replace trigger triggername [before/after] {DML statements} on [tablename] [for each row/statement]
begin
-------------------------
-------------------------
-------------------------
exception end;

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

RESULT:

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

Thus concept of triggers in SQL is executed successfully.

EXCEPTION HANDLING
Ex.No.7
AIM
To study and implement the concept of exception handling

PL/SQL supports programmers to catch such conditions using EXCEPTION block in the program and an
appropriate action is taken against the error condition. There are two types of exceptions −

 System-defined exceptions
 User-defined exceptions

Syntax for Exception Handling


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-statements
........
WHEN others THEN
exception3-handling-statements
END;

Raising Exceptions
Exceptions are raised by the database server automatically whenever there is any internal database error, but
exceptions can be raised explicitly by the programmer by using the command RAISE. Following is the simple
syntax for raising an exception −
DECLARE
exception_name EXCEPTION;
BEGIN
IF condition THEN
RAISE exception_name;
END IF;

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

EXCEPTION
WHEN exception_name THEN
statement;
END;

User-defined Exceptions
A user-defined exception must be declared and then raised explicitly, using either a RAISE statement or the
procedure DBMS_STANDARD.RAISE_APPLICATION_ERROR.
The syntax for declaring an exception is −
DECLARE
my-exception EXCEPTION;

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

RESULT:
Thus concept of exception handling in SQL is executed successfully.

Database design using E-R model and Normalization


EX NO:8
Aim:
To create Database Design using ER modelling, normalization and Implement the design for any
application.

ER diagram:
Chen Notation

 ORDER (OrderNum (key), OrderDate, SalesPerson)


ORDERITEMS (OrderNum (key)(fk) , ItemNum (key), PartNum, Quantity, Cost)
 In the above example, in the ORDERITEMS Relation: OrderNum is the Foreign
Key and OrderNum plus ItemNum is the Composite Key.
Chen Notation

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

In the ORDER Relation: OrderNum is the Key.

Representing Relationships

 1:1 Relationships. The key of one relation is stored in the second relation. Look at example
queries to determine which key is queried most often.
 1:N Relationships.

Parent - Relation on the "1" side. Child - Relation on the


"Many" side.

 Represent each Entity as a relation.


Copy the key of the parent into the child relation.

 CUSTOMER (CustomerID (key), Name, Address, ...)


ORDER (OrderNum (key), OrderDate, SalesPerson, CustomerID (fk))
 M:N Relationships. Many to Many relationships can not be directly implemented in relations.

 Solution: Introduce a third Intersection relation and copy keys from original two relations.

Chen Notation

 SUPPLIER (SupplierID (key), FirmName, Address, ...)


COMPONENT (CompID (key), Description, ...) SUPPLIER_COMPONENT
(SupplierID (key), CompID (key))

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

 Note that this can also be shown in the ER diagram. Also, look for potential added attributes in the
intersection relation.

DATABASE CONNECTIVITY WITH FRONT END TOOLS

EX NO:9
To create Database Design and Implement Database Connectivity With Front End Tools for Pay Roll Processing.

STEPS:

1. Create a database for payroll processing which request the using SQL

2. Establish ODBC connection

3. In the administrator tools open data source ODBC

4. Click add button and select oracle in ORA home 90, click finish

5. A window will appear given the data source home as oracle and select TNS source name as lion and give
the used id as SWTT

6. ADODC CONTROL FOR SALARY FORM:-

7. The above procedure must be follow except the table , A select the table as salary

8. Write appropriate Program in form each from created in VB from each from created in VB form project.

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

RESULT:
Thus the concept of database connectivity with front end tools is executed successfully

CASE STUDY USING REAL LIFE DATABASE APPLICATIONS

EX NO:10
STEPS:
1.Create the DB for banking system source request the using SQL
2.Establishing ODBC connection
3.Click add button and select oracle in ORA home 90 click finished
4.A window will appear give the data source name as oracle and give the user id as scott
5.Now click the test connection a window will appear with server and user name give user as scott and
password tiger Click ok
6.VISUAL BASIC APPLICATION:-

 Create standard exe project in to and design ms from in request format


 To add ADODC project select component and check ms ADO data control click ok
 Now the control is added in the tool book
 Create standard exe project in to and design ms from in request format

7.ADODC CONTEOL FOR ACCOUNT FROM:- Click customs and property window and window will appear and
select ODBC data source name as oracle and click apply as the some window.

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

Department of Computer Science and Engineering Page No.


K. Ramakrishnan College of Engineering, Trichy

RESULT:
Thus the banking system application is executed successfully.

Department of Computer Science and Engineering Page No.

You might also like