Dbms Lab Manual
Dbms Lab Manual
RAMAPURAM
FACULTY OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF INFORMATION TECHNOLOGY
LAB MANUAL
Prepared By HOD/IT
[Dr. R. Mythili] [Dr. Rajeswari Mukesh]
Degree/Branch: B. Tech / IT
Practical
S.NO EXPERIMENT NAME
Hours
1 SQL Data Definition Language Commands 2
6 Nested Queries 2
7 Join Queries 2
10 PL/SQL Procedures 2
11 PL/SQL Functions 2
12 PL/SQL Cursors 2
AIM:
To write a program for SQL Data Definition Language Commands on sample exercise.
CREATE Command:
● Used to create objects in database.
Syntax:
ALTER Command:
The SQL ALTER TABLE command is used to add, delete or modify columns in an existing
table. We would also use ALTER TABLE command to add and drop various constraints on a
an existing table.
Syntax
The basic syntax of ALTER TABLE to add a new column in an existing table is as follows:
The basic syntax of ALTER TABLE to DROP COLUMN in an existing table is as follows:
ALTER TABLE table_name DROP COLUMN column_name;
The basic syntax of ALTER TABLE to change the DATA TYPE of a column in a table is as
follows:
DROP Command:
Syntax
TRUNCATECommand:
The TRUNCATE TABLE statement is used to delete the data inside a table, but not the table
itself.
Syntax
RENAME Command:
● Used to rename the table.
RENAME TABLE offers more flexibility. It allows renaming multiple tables in one
statement. This can be useful when replacing a table with a new pre-populated version:
Thus the query for SQL Data Definition Language Commands are written, executed
and the outputs are verified successfully.
Ex.No: 2 SQL DATA MANIPULATION LANGUAGE COMMANDS
Date:
AIM:
To write a program for SQL Data Manipulation Language Commands on sample exercise.
DML commands are the most frequently used SQL commands and is used to query and
manipulate the existing database objects.
Syntax :
INSERT: This is used to add one or more rows to a table. The values are separated by
commas andthe data types char and date are enclosed in apostrophes. The values must br
entered in the same order as they are defined.
insert into <tablename(coln names to which datas to b inserted)> values (list of values);
UPDATE - It is used to alter the column values in a table. A single column may be updated
or more than one column could be updated.
DELETE - After inserting row in a table we can also delete them if required. The delete
command consists of a from clause followed by an optional where clause.
AIM:
To write a program for SQL Data Control Language Commands and Transaction Control
Commands on sample exercise.
Syntax:
GRANT SELECT, UPDATE ON TABLE NAME TO SOME_USER, ANOTHER_USER;
Revoke:
It is used to take back permissions from the user.
Syntax:
REVOKE SELECT, UPDATE ON TABLE NAME FROM USER1, USER2;
Commit:
Commit command is used to save all the transactions to the database.
Syntax:
commit;
Rollback:
Rollback command is to rollback a transaction in case of any error occurs.
Syntax:
rollback;
Savepoint:
Savepoint command is to Set a savepoint within a transaction. If transaction happens
in big data, then for checking and rollup can't do it with all the data, to rollback the small part
of the data we use savepoint query.
Syntax:
SAVEPOINT savepoint_name
Set Transaction:
Set command is to Specify the characteristics of the transaction.
Syntax:
SET TRANSACTION Access NAME transaction_name
Result:
Thus the query for SQL Data Control Language Commands and Transaction Control
Commands on sample exercise was executed and verified successfully.
Ex:No:4 INBUILT FUNCTIONS IN SQL
Date:
AIM:
SQL Functions:
Functions in SQL Server are the database objects that contain a set of SQL
statements to perform a specific task. The main purpose of functions is to replicate the
common task easily. We can build functions one time and can use them in multiple locations
based on our needs. SQL Server does not allow to use of the functions for inserting, deleting,
or updating records in the database tables.
1. Aggregate Functions
2. Scalar Functions
Function Description
SUM() Used to return the sum of a group of values.
COUNT() Returns the number of rows either based on a condition, or without a condition.
AVG() Used to calculate the average value of a numeric column.
MIN() This function returns the minimum value of a column.
MAX() Returns a maximum value of a column.
FIRST() Used to return the first value of the column.
LAST() This function returns the last value of the column.
Function Description
LCASE() Used to convert string column values to lowercase
UCASE() This function is used to convert a string column values to Uppercase.
LEN() Returns the length of the text values in the column.
MID() Extracts substrings in SQL from column values having String data type.
ROUND() Rounds off a numeric value to the nearest integer.
NOW() This function is used to return the current system date and time.
FORMAT() Used to format how a field must be displayed.
CHARACTER/STRING FUNCTION:
NUMERIC FUNCTION
RESULT:
Thus the implementation of inbuilt functions using SQL was executed and verified
successfully.
Ex.No:5 CONSTRUCTING AN ER MODEL FOR THE APPLICATION
Date:
AIM:
COMPONENTS OF ER DIAGRAM:
An ER diagram has three main components:-
1. Entity:
An entity is an object or component of data. An entity is represented as a rectangle in an ER
diagram.
It has 2 types:
- Weak Entity (represented by a double rectangle)
- Strong Entity (represented by a single rectangle).
2. Attributes:
An attribute describes the property of an entity. An attribute is represented as Oval in an ER
diagram. There are four types of attributes:
- Key attribute
- Composite attribute
- Multivalued attribute
- Derived attribute
3. Relationships:
A relationship is represented by a diamond shape in ER diagram, it shows the relationship
among entities. There are four types of relationships:
- One to One
- One to Many
- Many to One
- Many to Many
KEY CONSTRAINTS:
PRIMARY KEY:
Primary key uniquely identifies each record in a table. A Primary Key must contain unique
value and it must not contain null value. Usually the primary Key is used to index the data
inside the table.
FOREIGN KEY:
Foreign Key is used to relate two tables. The relationship between the two tables matches the
Primary Key in one of the tables with a Foreign Key in the second table. This is also called a
referencing key.
RESULT:
Thus the Entity Relationship Diagram for (Application_Name) has been constructed
and verified successfully.
Ex.No: 6 Nested Queries
Date:
AIM:
To implement Nested Queries commands on sample exercise using SQL.
Sub Query can have more than one level of nesting in one single query. A SQL nested query
is a SELECT query that is nested inside a SELECT, UPDATE, INSERT, or DELETE SQL
query.
1. Select Command Is Used To Select Records From The Table.
2. Where Command Is Used To Identify Particular Elements.
3. Having Command Is Used To Identify Particular Elements.
4. Min (Sal) Command Is Used To Find Minimum Salary.
AIM:
To implement join query commands using SQL.
JOIN QUERIES:
● SQL joins are used to query data from two or more tables, based on a relationship
between certain columns in these tables.
1. Inner Join
2. Self Join
3. Outer Join
a) Left Outer Join
b) Right Outer Join
c) Full Outer Join
4. Cross Join
INNER JOIN:
Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
SELF JOIN:
A self join is a regular join, but the table is joined with itself.
Syntax:
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
Returns all records from the left table, and the matched records from the right table.
Syntax:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
RIGHT OUTER JOIN:
Returns all records from the right table, and the matched records from the left table
Syntax:
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Syntax:
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
CROSS JOIN:
Combines all of the possibilities of two or more tables and returns a result that includes every
row from all contributing tables. It's also known as CARTESIAN JOIN.
Syntax:
SELECT column_lists
FROM table1
CROSS JOIN table2;
RESULT:
Thus the implementation of join queries using SQL was executed and verified
successfully.
Ex.No:8 SET OPERATORS & VIEWS
AIM:
To implement Set Operators & Views using SQL
SET Operators:
SET operators are special type of operators which are used to combine the result of
two queries.
Operators covered under SET operators are:
- UNION
- UNION ALL
- INTERSECT
- MINUS(EXCEPT)
Union:
UNION will be used to combine the result of two select statements. Duplicate rows
will be eliminated from the results obtained after performing the UNION operation.
Syntax:
SELECT *FROM (table_name1) UNION SELECT *FROM (table_name2);
Union All:
This operator combines all the records from both the queries. Duplicate rows will be
not be eliminated from the results obtained after performing the UNION ALL operation.
Syntax:
SELECT *FROM (table_name1) UNION ALL SELECT *FROM (table_name2);
Intersect:
It is used to combine two SELECT statements, but it only returns the records which
are common from both SELECT statements.
Synatx:
SELECT *FROM (table_name1) INTERSECT SELECT *FROM (table_name2);
Minus(Except):
It displays the rows which are present in the first query but absent in the second query
with no
duplicates.
Synatx:
SELECT *FROM(table_name1) MINUS SELECT *FROM (table_name2);
VIEWS:
Syntax:
Syntax:
INSERT INTO (Table_Name) VALUES (value1, value2, value 3, .... Value N);
Syntax:
4. UPDATE command is used to change a value in a tuple without changing all values in the
tuple.
Syntax:
UPDATE (table_name) SET (column1 = value1, column2 = value2, ...) WHERE (co
ndition);
Syntax:
Date:
AIM:
PL/SQL supports the programming language features like conditional statements and iterative
statements.
PL/SQL If Statement:
Syntax:
(IF-THEN statement):
IF condition
THEN
END IF;
IF condition
THEN
ELSE
END IF;
It is used when a set of statements has to be executed as long as a condition is true, the While
loop is used. The condition is decided at the beginning of each iteration and continues until
the condition becomes false.
Syntax:
WHILE <condition>
LOOP statements;
END LOOP;
PL/SQL for loop:
It is used when we want to execute a set of statements for a predetermined number of times.
The loop is iterated between the start and end integer values. The counter is always
incremented by 1 and once the counter reaches the value of end integer, the loop ends.
LOOP statements;
END LOOP;
Result:
Thus a program on PL/SQL Conditional and Iterative Statements was executed and verified
successfully.
Ex.No: 10 PL/SQL PROCEDURES
Date:
AIM:
To write a program on PL/SQL Procedures on sample exercises.
PL/SQL PROCEDURES
The PL/SQL stored procedure is a PL/SQL block which performs one or more specific tasks.
It is just like procedures in other programming languages.
Header: The header contains the name of the procedure and the parameters or variables
passed to the procedure.
Body: The body contains a declaration section, execution section and exception section
similar to a general PL/SQL block.
IS
[declaration_section]
BEGIN
executable_section
[EXCEPTION
exception_section]
END [procedure_name];
Result:
Date:
AIM:
To write a program on PL/SQL Functions
PL/SQL FUNCTIONS:
RETURN return_datatype
{IS | AS}
BEGIN
END [function_name];
Result:
Date:
AIM:
PL/SQL Cursors:
A cursor is used to refer to a program to fetch and process the rows returned by the SQL
statement, one at a time.
● The implicit cursors are automatically generated by Oracle while an SQL statement is
executed, if we don't use an explicit cursor for the statement.
● These are created by default to process the statements when DML statements like
INSERT, UPDATE, DELETE etc. are executed.
● Some of them are: %FOUND, %NOTFOUND, %ROWCOUNT and %ISOPEN.
• The Explicit cursors are defined by the programmers to gain more control over the context
area.
• These cursors 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.
Date:
AIM:
• System-defined Exceptions.
• User-defined Exceptions.
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;
Result:
Thus a program on PL/SQL Exception Handling was verified and executed successfully.
Ex.No: 14 PL/SQL TRIGGER
Date:
AIM:
PL/SQL TRIGGER:
[OF col_name]
ON table_name
WHEN (condition)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;
Result:
Date:
AIM:
1. Draw ER Diagram
2. Write Schemas
3. Create tables
4. Create Views
Demonstration:
Demonstrate in ORACLE/MS-SQL.