Dbms Lab Writting Documnet Edited
Dbms Lab Writting Documnet Edited
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
o CREATE
o ALTER
o DROP
o TRUNCATE
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;
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:
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]
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;
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
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)
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.
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.
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
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.
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;
RESULT
Thus all the basic queries, join and nested queries were executed and the output was verified.
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.
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...);
SEQUENCE
SYNTAX
CREATE SEQUENCE sequence_name
MINVALUE value
MAXVALUE value
INCREMENT BY value
CACHE value;
SYNONYMS
CREATE SYNONYM offices
FOR hr.locations;
RESULT: Thus the Views, Sequence, Indexes, and Synonyms are executed successfully.
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.
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.
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;
RESULT:
Thus database programming for implicit and explicit cursors are executed successfully
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
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;
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 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.
TYPES OF TRIGGERS
The various types of triggers are as follows,
Before: It fires the trigger before executing the trigger statement.
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 triggername [before/after] {DML statements} on [tablename] [for each row/statement]
begin
-------------------------
-------------------------
-------------------------
exception end;
RESULT:
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
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;
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;
RESULT:
Thus concept of exception handling in SQL is executed successfully.
ER diagram:
Chen Notation
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.
Solution: Introduce a third Intersection relation and copy keys from original two relations.
Chen Notation
Note that this can also be shown in the ER diagram. Also, look for potential added attributes in the
intersection relation.
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
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
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.
RESULT:
Thus the concept of database connectivity with front end tools is executed successfully
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:-
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.
RESULT:
Thus the banking system application is executed successfully.