Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 66

DAV INSTITUTE OF ENGINEERING & TECHNOLOGY, JALANDHAR

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

DATABASE MANAGEMENT SYSTEMS LAB FILE


(BTCS 505-18)

SUBMITTED TO: SUBMITTED BY:


Mrs. Shaveta Kalsi Karan Gupta
University Roll No.-1904018
Class Roll No. -166/19
CSE-5 th SEM

1
INDEX

S.NO. PRACTICAL PAGE NO.

1 Introduction to DBMS. 4-9

Introduction to oracle and its installation.


2 10-19

3 To CREATE, ALTER and DROP the Table. 20-22

To perform SELECT, UPDATE, INSERT and DELETE


4 operation on a table. 23-25

5 To make use of different clauses viz where, groupby, having, 26-31


order by, union, intersection, set difference.

6 To perform various Arithmetic and logical operations. 32-35

7 To perform various Constraints of SQL. 36-38

To perform aggregate and numeric Functions in Oracle.


8 39-44

9 To perform conversion and string Functions in Oracle. 45-50

10 To perform various join operations. 51-54

2
11 To understand the use and working of Sub-Queries. 55-57

To make use of transaction control statement viz Rollback,


12 58-60
Commit and savepoint.

13 To make Views of a table 61-63

14 To make indexes for a table. 64-65


Experiment No: 1

Aim: - Introduction to DBMS and its related languages

DBMS:
A Database Management System is a software program used to manage a database. These programs
enable users to access and modify database.

A DBMS is a complex set of software programs that controls the organization, storage, management, and
retrieval of data in a database.

A DBMS includes four main components, which are: Modeling Language, Data Structures, DB Query
Language and Report Writer, and Transaction Mechanism. Each of these components can be further broken
down into smaller and more specific pieces, but it is the sum of these parts which are combined to create the
management system around the particular database to be utilized.

A database management system, or DBMS, gives the user access to their data and helps them transform
the data into information.

Such database management systems include dBase, Paradox, IMS, and Oracle. These systems allow users
to create, update, and extract information from their databases. Compared to a manual filing system, the
biggest advantages to a computerized database system are speed, accuracy, and accessibility. A
database is a structured collection of data. Data refers to the characteristics of people, things, and events.

SQL is a database computer language designed for the retrieval and management of data in a relational
database. SQL stands for Structured Query Language.
SQL is a computer language for storing, manipulating and retrieving data stored in a relational database.
SQL is the standard language for Relational Database System. All the Relational Database Management
Systems (RDMS) like MySQL, MS Access, Oracle and SQL Server use SQL as their standard database
language.

Applications of SQL:

 Allows users to access data in the relational database management systems.


 Allows users to describe the data.
 Allows users to define the data in a database and manipulate that data.
 Allows users to create and drop databases and tables.
 Allows users to create view, stored procedure, functions in a database.
 Allows users to set permissions on tables, procedures and views.

SQL Commands:

The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT,
UPDATE, DELETE and DROP. These commands can be classified into the following groups based on
their nature
Languages of DBMS:

1. DDL-Data Definition Language:


DDL or Data Definition Language actually consists of the SQL commands that can be used to define the
database schema. It simply deals with descriptions of the database schema and is used to create and
modify the structure of database objects in the database. Examples of DDL commands:

1 CREATE
Creates a new table, a view of a table, or other object in the database.

ALTER
2
Modifies an existing database object, such as a table.

RENAME
3
rename an existing database object

DROP
4
Deletes an entire table, a view of a table or other objects in the database.

2. DML-Data Manipulation Language:


The SQL commands that deal with the manipulation of data present in the database belongs to DML or Data
Manipulation Language and this includes most of the SQL statements.

1 SELECT
Retrieves certain records from one or more tables.

INSERT
2
Creates a record.

UPDATE
3
Modifies records.

DELETE
4
Deletes records.

3. DCL-Data Control Language:


DCL includes commands such as GRANT and REVOKE which mainly deals with the rights, permissions
and other controls of the database system.
GRANT
Gives a privilege to user.

REVOKE
Takes back privileges granted from user.

3. TCL-transaction Control Language:


TCL commands deal with the transaction within the database.

1 COMMIT
Commits a Transaction.

ROLLBACK
2
Rollbacks a transaction in case of any error occurs.

SAVEPOINT
3
Sets a save point within a transaction..

Three schemas Architecture:

o The three schema architecture is also called three-level architecture.


o This framework is used to describe the structure of a specific database system.
o The three schema architecture is also used to separate the user applications and
physical database.
o The three schema architecture contains three-levels. It breaks the database down into
three different categories.
Internal Level:
o The internal level has an internal schema which describes the physical storage structure of the
database.
o The internal schema is also known as a physical schema.
o It uses the physical data model. It is used to define that how the data will be stored in a block.
o The physical level is used to describe complex low-level data structures in detail.

Conceptual Level:
o The conceptual schema describes the design of a database at the conceptual level.
Conceptual level is also known as logical level.
o The conceptual schema describes the structure of the whole database.
o The conceptual level describes what data are to be stored in the database and also describes what
relationship exists among those data.
o In the conceptual level, internal details such as an implementation of the data structure are hidden.
o Programmers and database administrators work at this level.

External Level:
o At the external level, a database contains several schemas that sometimes called as subschema. The
subschema is used to describe the different view of the database.
o An external schema is also known as view schema.
o Each view schema describes the database part that a particular user group is interested and hides
the remaining database from that user group.

# Advantages of DBMS:

o Controls database redundancy: It can control data redundancy because it stores all the data in
one single database file and that recorded data is placed in the database.

o Data sharing: In DBMS, the authorized users of an organization can share the data among
multiple users.

o Easily Maintenance: It can be easily maintainable due to the centralized nature of the database
system.

o Reduce time: It reduces development time and maintenance need.

o Backup: It provides backup and recovery subsystems which create automatic backup of
data from hardware and software failures and restores the data if required.

o Multiple user interface: It provides different types of user interfaces like graphical user
interfaces, application program interfaces
# Disadvantages of DBMS:

o Cost of Hardware and Software: It requires a high speed of data processor and large memory size
to run DBMS software.

o Size: It occupies a large space of disks and large memory to run them efficiently.

o Complexity: Database system creates additional complexity and requirements.

o Higher impact of failure: Failure is highly impacted the database because in most of the
organization, all the data stored in a single database and if the database is damaged due to electric
failure or database corruption then the data may be lost forever.
Experiment No: 2

Aim: - Introduction to Oracle and its Installation. Oracle:

Oracle Database (commonly referred to as Oracle DBMS or simply as Oracle) is a multi-model


database management system produced and marketed by Oracle Corporation.
It is a database commonly used for running online transaction processing, data warehousing and mixed
(OLTP & DW) database workloads. Oracle Database is available by several service providers on-prem, on-
cloud, or as hybrid cloud installation. Exclusively for Cloud customers Oracle offers Oracle Autonomous
Database providing fully automated operation procedures.

Oracle features:

 Logical data structure – Oracle uses the logical data structure to store data so that you can interact with
the database without knowing where the data is stored physically.

 Partitioning – is a high-performance feature that allows you to divide a large table into different pieces
and store each piece across storage devices.

 Memory caching – the memory caching architecture allows you to scale up a very large database that
still can perform at a high speed.

 Data Dictionary is a set of internal tables and views that support administer Oracle Database more
effectively.

 Backup and recovery – ensure the integrity of the data in case of system failure. Oracle includes a
powerful tool called Recovery Manager (RMAN) – allows DBA to perform cold, hot, and
incremental database backups and point-in-time recoveries.

 Clustering – Oracle Real Application Clusters (RAC) – Oracle enables high availability that enables the
system is up and running without interruption of services in case one or more server in a cluster fails.
#Oracle Built-in Data Types:
Following table summarizes Oracle built-in data types.

Types Description

VARCHAR2(size [BYTE | Variable-length character


CHAR]) string.

NVARCHAR2(size) Variable-length Unicode


character string having
maximum length size
characters.

NUMBER [ (p [, s]) ] Number having precision p


and scale s.
Range of p : From 1 to 38.
Ranges of s : From -84 to 127.
Both precision and scale are in
decimal digits.

FLOAT [(p)] A FLOAT value is represented


internally as NUMBER.
Range of p : From 1 to 126
binary digits.

LONG Character data of variable


length up to 2 gigabytes, used
for backward compatibility.

DATE Valid date range : From


January 1, 4712 BC, to
December 31, 9999 AD.
The default format is
determined explicitly by the
NLS_DATE_FORMAT
parameter or implicitly by the
NLS_TERRITORY
parameter.
BINARY_FLOAT 32-bit floating point number.

BINARY_DOUBLE 64-bit floating point number.

TIMESTAMP This data type contains the


[(fractional_seconds_precision)] datetime fields YEAR,
MONTH, DAY, HOUR,
MINUTE, and
SECOND. It contains
fractional
seconds but does not have a
time zone.

TIMESTAMP This data type contains the


[(fractional_seconds_precision)] datetime fields YEAR,
WITH TIME ZONE MONTH, DAY, HOUR,
MINUTE, SECOND,
TIMEZONE_HOUR, and
TIMEZONE_MINUTE. It
has fractional seconds and an
explicit time zone.
INTERVAL YEAR Stores a period of time in
[(year_precision)] TO MONTH years and months, where
year_precision is the number
of digits in the YEAR
datetime field.
Accepted values are 0 to 9.
The default is 2.

INTERVAL DAY Stores a period of time


[(day_precision)] TO SECOND in days, hours, minutes,
[(fractional_seconds_precision)] and seconds, where
day_precision is the maximum
number of digits in the DAY
datetime field.
Accepted values are 0 to 9.
The default is 2.

RAW(size) Raw binary data of length size


bytes.

LONG RAW Raw binary data of variable.

ROWID The unique address (base


64 string representing) of a
row in its table.

UROWID [(size)] The logical address of a row


(base 64 string representing)
of an index-organized table.

CHAR [(size [BYTE | CHAR])] Fixed-length character data


of length size bytes or
characters.
NCHAR[(size)] Fixed-length character data of
length size characters. The
number of bytes can be up to
two times size for
AL16UTF16 encoding and
three times size for UTF8
encoding.
CLOB A character large object
containing single-byte or
multibyte characters.

NCLOB A character large object


containing Unicode
characters.

BLOB A binary large object.

BFILE Contains a locator to a large


binary file stored outside the
database.

Following are the steps involved in installing the Oracle 18c Express Edition in Window Server:

Step: 1
Download the Oracle Database 18c Express Edition from the Oracle website. It will be in the Zip form, start
by extracting the file.
Step: 2
After Extraction we will have a number of files, we need to run the setup.exe file.
It will check the system before installation.

Step: 3
After system check we will get a window where we are required to click Next.
Step:4
After that we will have to accept the License agreement in order to proceed.
Step: 5
In this step we are required to set the destination address where Oracle will get installed.

Step:6
In this step we need to set the Database Password, After typing the password click Next.
Step: 7
We will have a review window to check the settings.

Step:8
After clicking Install, Oracle will start installing.

After validating files will start to copy.


Step:9
After the completion of the above process a window will open confirming the installation of Oracle.

Click Finish and you are done installing.


Experiment No: 3

Aim:- To CREATE, ALTER and DROP the Table.

 Create Table Statement:


The CREATE TABLE statement is used to create a new table in a database. Creating a basic table involves
naming the table and defining its columns and each column's data type.

Syntax:
CREATE TABLE
table_name( column1 datatype,
column2 datatype, column3
datatype,
.....
columnN datatype
);

Implementation:

 Alter Table Statement:

The ALTER TABLE command is used to add, delete or modify columns in an existing table. You should
also use the ALTER TABLE command to add and drop various constraints on an existing table
Syntax:
ALTER TABLE - ADD Column

To add a column in a table, use the following syntax: ALTER TABLE

table_name ADD column_name datatype;

Implementation:

ALTER TABLE - MODIFY COLUMN

To change the data type of a column in a table, use the following syntax:

ALTER TABLE table_name


MODIFY COLUMN column_name datatype;

Implementation:
 Drop Table Statement:

The DROP TABLE statement is used to drop an existing table in a database. This command not only deletes
all the records in the table, but also removes the definition of a table.

Syntax:
DROP TABLE table_name;

Implementation:
Experiment No: 4

Aim:- To perform SELECT, UPDATE, INSERT and DELETE operation on a table.

 SELECT Statement:
A SELECT statement is used as a data retrieval statement i.e. It retrieves information from the database.

Syntax:
SELECT * FROM table_name;

Implementation:

 INSERT INTO Statement:


The INSERT INTO statement is used to insert new records in a table.
Syntax:
INSERT INTO table_name(column1, column2, column3,... ) VALUES (value1,
value2, value3,... value N);

Implementation:
 UPDATE Statement:
The UPDATE statement is used to modify the existing records in a table. You can use the WHERE clause
with the UPDATE query to update the selected rows, otherwise all the rows would be affected.

Syntax:
UPDATE table_name SET column=value, column1=value ,…. WHERE condition;

Implementation:
 DELETE Statement:
The SQL DELETE Query is used to delete the existing records from a table. We can use WHERE clause.

Syntax:
DELETE FROM Table name WHERE condition;

Implementation:
Experiment No: 5

Aim: To make use of different clauses viz where, group by, having, order by, union, intersection,
set difference.
 WHERE CLAUSE:

The WHERE clause is used to specify a condition while fetching the data from a single table or by joining
with multiple tables.
Syntax:
SELECT * FROM Column name WHERE
Condition;
Operators in the WHERE Clause
The following operators can be used in the WHERE clause:
Operator Description
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal
BETWEEN Between a certain range LIKE
Search for a pattern
IN To specify multiple possible values for a column
Implementation:
 ORDER BY CLAUSE:

The ORDER BY clause is used to sort the data in ascending or descending order, based on one or more
columns. Some databases sort the query results in an ascending order by default.
Syntax:
SELECT * FROM table_name ORDER BY Condition ASC|DESC;

Implementation:

 GROUP BY CLAUSE:
The 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;

Implementation:

 HAVING CLAUSE:
The HAVING Clause enables you to specify conditions that filter which group results appear in the
results.

Syntax:
SELECT * FROM Column GROUP BY condition HAVING condition;
Implementation:

 UNION CLAUSE:
The SQL UNION clause/operator is used to combine the results of two or more SELECT statements without
returning any duplicate rows.

Syntax:
SELECT Column_name FROM table1 UNION SELECT Column_name FROM table2;

Implementation:
 INTERSECT CLAUSE:
The INTERSECT clause/operator is used to combine two SELECT statements, but returns rows only from
the first SELECT statement that are identical to a row in the second SELECT statement. This means
INTERSECT returns only common rows returned by the two SELECT statements.

Syntax:
SELECT Column_name FROM table1 INTERSECT SELECT Column_name FROM table2;

Implementation:
 MINUS CLAUSE:

The Minus Operator in SQL is used with two SELECT statements. The MINUS operator is used to subtract
the result set obtained by first SELECT query from the result set obtained by second SELECT query.

Syntax:
SELECT Column_name FROM table1 MINUS SELECT Column_name FROM table2;

Implementation:
Experiment No: 6

Aim: To perform various Arithmetic and logical operations.

Arithmetic Operations:

 SUBTRACTION OPERATION:
It is use to perform subtraction operation on the data items, items include either single column or multiple
columns.

Syntax:
SELECT Column, Column-Column FROM Table;

Implementation:
 ADDITION OPERATION:
It is used to perform addition operation on the data items, items include either single column or multiple
columns.

Syntax:
SELECT Column, Column+Column FROM Table;

Implementation:

 DIVISION OPERATION:
It is use to perform division of data items.

Syntax:
SELECT Column, Column/2 FROM Table;
Implementation:

 MULTIPLICATION OPERATION:
It is use to perform multiplication of data items.

Syntax:
SELECT Column, Column*Column FROM Table;

Implementation:

Logical Operations:

 AND:

This operator displays only those records where both the conditions condition1 and condition2 evaluates
to True.

Syntax:
SELECT * FROM Table WHERE Condition AND Condition;

Implementation:
 OR:
This operator displays the records where either one of the conditions condition1 and condition2 evaluates
to True. That is, either condition1 is True or condition2 True.
Syntax:
SELECT * FROM Table WHERE Condition OR Condition;

Implementation:

 NOT:
The NOT operator displays a record if the condition(s) is NOT TRUE.

Syntax:
SELECT * FROM Table WHERE NOT Condition;

Implementation:
Experiment No: 7

Aim: To perform various Constraints of SQL.

Constraints:

Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and
reliability of the data in the table. If there is any violation between the constraint and the data action, the
action is aborted.
Constraints can be column level or table level. Column level constraints apply to a column, and table level
constraints apply to the whole table.
Syntax:
CREATE TABLE table_name ( column1
datatype constraint, column2 datatype
constraint, column3 datatype constraint,
....
);
The following constraints are commonly used in SQL:

 NOT NULL:
By default, a column can hold NULL values. If you do not want a column to have a NULL value, then
you need to define such a constraint on this column specifying that NULL is now not allowed for that
column.

Syntax:
Create table table_name(column_name NOT NULL,…..);

IMPLEMENTATION:
 UNIQUE CONSTRAINT:
A unique key is a set of one or more than one fields/columns of a table that uniquely identify a record in a
database table.

Syntax:
Create table table_name(column_name UNIQUE,…);

#IMPLEMENTATION:

 PRIMARY KEY:
A primary key is a field in a table which uniquely identifies each row/record in a database table. Primary
keys must contain unique values. A primary key column cannot have NULL values.

Syntax:
Create table table_name(column name NOT NULL PRIMARY KEY,…..);

IMPLEMENTATION:
 FOREIGN KEY:
A foreign key is a key used to link two tables together. This is sometimes also called as a referencing key.
A Foreign Key is a column or a combination of columns whose values match a Primary Key in a different
table.
Syntax:
Create table Table1(column1 NOT NULL PRIMARY KEY, column2 varchar(10));

Create table Table2(column11 NOT NULL, column12 varchar(10), FOREIGN KEY(column11)


REFERENCES Table1(column1));

IMPLEMENTATION:
Experiment No: 8

Aim: To Study Aggregate and Numerical functions.

Aggregate Function:-
In database management an aggregate function is a function where the values of multiple rows are
grouped together as input on certain criteria to form a single value of more significant meaning.
Syntax −
SELECT FUNCTION_NAME(Column_Name) FROM Table_Name;

Various Aggregate Functions:-

 COUNT Function:
The COUNT function is used with SQL SELECT statement and it is very useful to count the number of
rows in a table having enormous data.

Syntax:
Select COUNT(Column) From Table;

IMPLEMENTATION:
 SUM Function:
SQL SUM function is used to find out the sum of a field in various records.

Syntax:
Select SUM(Column) From Table;

IMPLEMENTATION:

 AVG Function:
It is used to find the Average of a field.

Syntax:
Select AVG(Column) From Table;

IMPLEMENTATION:

 MIN Function:
It is used to find the minimum value of a field.
Syntax:
Select MIN(Column) From Table;

IMPLEMENTATION:

 MAX Function:
It is used to find the maximum value of a field.
Syntax:
Select MAX(Column) From Table;

IMPLEMENTATION:

 VARIANCE Function:

The VARIANCE Function is used to find variance of the column specified as argument.

Syntax:
SELECT VARIANCE(Column) FROM Table;

IMPLEMENTATION:
Numeric Functions:
Numeric Functions are used to perform operations on numbers and return numbers

Syntax :
SELECT FUNCTION NAME (Column_Name) FROM Table_Name; Following
are the numeric functions defined in SQL:

 ROUND:
The ROUND function rounds a number to a specified number of decimal places.

Syntax:
Select round(number, no of decimal places needed) from Table;

IMPLEMENTATION:

 TRUNC:
Truncates value of a number up to specified decimal places.

Syntax:
Select trunc(number, no of decimal places needed) from Table;

IMPLEMENTATION:
 MOD:
It is used to find Remainder of a number divided by another number.

Syntax:
Select MOD(number, number1) from Table;

IMPLEMENTATION:

 CEIL:
It is used to find a number greater than or equal to the given number.

Syntax:
Select CEIL(number) from Table;

IMPLEMENTATION:
 FLOOR:
It is used to find a number smaller than or equal to the given number.

Syntax:
Select FLOOR(number) from Table;

IMPLEMENTATION:

 ABS:
It is used to find the Absolute value of a number.

Syntax:
Select ABS(number) from Table;

IMPLEMENTATION:
Experiment-9

Aim: To perform conversion and string Functions in Oracle.

 TO_CHAR:
TO_CHAR function is used to typecast a numeric or date input to character type with a format model
(optional).

Syntax:
select TO_CHAR(Column name, '$999999') from table name;

IMPLEMENTATION:

 TO_DATE:
The function takes character values as input and returns formatted date equivalent of the same.
Syntax:
select TO_DATE('12 Dec 2020', 'DD MM YYYY') from dual;

IMPLEMENTATION:

 NVL:
NVL converts a null value to an actual value. Data types that can be used are date, character and number.
Data type must match with each other i.e. expr1 and expr2 must of same data type.

Syntax:
select NVL(Null value, “Against Null value”) from dual;

IMPLEMENTATION:

 UPPER:
The SQL UPPER function converts all the letters in a string into uppercase.

Syntax:
select UPPER(column name) from table name;

IMPLEMENTATION:
 LOWER:
The SQL LOWER function converts all the letters in a string into lowercase.

Syntax:
select LOWER(column name) from table name;

IMPLEMENTATION:

 INITCAP:
The Oracle INITCAP function sets the first letter of each word in uppercase.

Syntax:
select INITCAP(column name) from table name;

IMPLEMENTATION:

 CONCAT:
It is used to concatenate strings.

Syntax:
select CONCAT(column name1, column name2) from table name;

IMPLEMENTATION:
 LENGTH:
It is used to find the length of the string.

Syntax:
select LENGTH(column_name) from table_name;

IMPLEMENTATION:

 LAPD:

LPAD function is used to padding the left side of a string with a specific set of characters.

Syntax:
select LPAD(column name, length, “padding symbol”) from table name;

IMPLEMENTATION:
 RAPD:

RPAD function is used to padding the right side of a string with a specific set of characters.

Syntax:
select RPAD(column name, length, “padding symbol”) from table name;

IMPLEMENTATION:

 SUBSTR:

The SUBSTR function returns a substring of a character value.

Syntax:
select SUBSTR(column name, start position, End position) from table name;

IMPLEMENTATION:

 INSTR:

The INSTR functions search string for substring. The INSTR function returns a numeric value.
Syntax:
select INSTR(column name, “string/character”) from table name;

IMPLEMENTATION:
Experiment-10

Aim: To perform various join operations.

Oracle join is used to combine columns from two or more tables based on values of the related columns.

 Equi join:

SQL EQUI JOIN performs a JOIN against equality or matching column(s) values of the associated tables.
An equal sign (=) is used as comparison operator in the where clause to refer equality.

Syntax:

Select column1, column2 from table1, table2 where colum1=column2;

IMPLEMENTATION:
 INNER JOIN:

The INNER JOIN statement joins the left table to the right table using the values in the column.

Syntax:

Select column1, column2 from table1 INNER JOIN table2 on column1=column2;

IMPLEMENTATION:

 Cross/Cartesian join:

The CROSS JOIN is used to generate a paired combination of each row of the first table with each
row of the second table.

Syntax:

Select column1, column2 from table1, table2;


IMPLEMENTATION:

 RIGHT JOIN:

The right join or right outer join is a reversed version of the left join. The right join makes a result set that
contains all rows from the right table with the matching rows from the left table. If there is no match, the
left side will have nulls.

Syntax:

Select column1, column2 from table1 RIGHT JOIN table2 on column1=column2;

IMPLEMENTATION:
 LEFT JOIN:

The left join returns all rows from the left table with the matching rows if available from the right table. If
there is no matching row found from the right table, the left join will have null values for the columns of
the right table.

Syntax:

Select column1, column2 from table1 LEFT JOIN table2 on column1=column2;

IMPLEMENTATION:

 FULL OUTER JOIN:

Oracle full outer join or full join returns a result set that contains all rows from both left and right tables,
with the matching rows from both sides where available. If there is no match, the missing side will have
nulls.

Syntax:

Select column1, column2 from table1 FULL JOIN table2 on column1=column2;

IMPLEMENTATION:
Experiment-11

Aim: To understand the use and working of Sub-Queries.

THEORY:
A Sub query or Inner query or a Nested query is a query within another SQL query and embedded
within the WHERE clause.
A subquery is used to return data that will be used in the main query as a condition to further restrict the
data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the
operators like =, <, >, >=, <=, IN, BETWEEN, etc.

 Subqueries with the SELECT Statement:

Subqueries are most frequently used with the SELECT statement.

Syntax:

Select * from table where column in (select column from table where column condition);

IMPLEMENTATION:
 Subqueries with the INSERT Statement:

Subqueries also can be used with INSERT statements. The INSERT statement uses the data returned
from the subquery to insert into another table. The selected data in the subquery can be modified with any
of the character, date or number functions.

Syntax: Insert into table select * from table where column condition;

IMPLEMENTATION:

 Subqueries with the UPDATE Statement:


The subquery can be used in conjunction with the UPDATE statement. Either single or multiple
columns in a table can be updated when using a subquery with the UPDATE statement.

Syntax: Update table set column expression where column in (select column from table where
condition);

IMPLEMENTATION:

 Subqueries with the DELETE Statement:

The subquery can be used in conjunction with the DELETE statement like with any other statements
mentioned above.

Syntax: Delete from table where column in (select column from table where condition);

IMPLEMENTATION:
Experiment-12

Aim: To make use of transaction control statements viz ROLLBACK, COMMIT AND SAVEPOINT.

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 allows statements
to be grouped together into logical transactions.

 ROLLBACK:

The ROLLBACK command is the transactional command used to undo transactions that have not already
been saved to the database. The ROLLBACK command can only be used to undo transactions since the
last COMMIT or ROLLBACK command was issued.

Syntax:

ROLLBACK;

IMPLEMENTATION:
 COMMIT:

The COMMIT command is the transactional command used to save changes invoked by a transaction to
the database. The COMMIT command saves all transactions to the database since the last COMMIT or
ROLLBACK command.

Syntax:

COMMIT;

IMPLEMENTATION:
 SAVEPOINT:
The savepoint command is used to temporarily save a transaction so that you can rollback to that point
whenever required.
Syntax:
SAVEPOINT savepoint_name;
IMPLEMENTATION:
Practical-13

Aim: To make Views of a table and to perform Insert, Update, Select and Delete operations on View.

 SQL CREATE VIEW:


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. You can add SQL functions, WHERE, and JOIN statements to a view and
present the data as if the data were coming from one single table.

Syntax:
CREATE VIEW view_name AS SELECT column1, column2, ...FROM table_name WHERE condition;

IMPLEMENTATION:
 SQL Updating a View:
A view can be updated with the CREATE OR REPLACE VIEW command.

Syntax:
CREATE OR REPLACE VIEW view_name AS SELECT column1, column2, ... FROM table_name
WHERE condition;

IMPLEMENTATION:
 Insertion a Views:

It is used to Insert rows in views. We must specify a list of values for a particular row.

Syntax:

INSERT INTO view_name VALUES list_of_values;

IMPLEMENTATION:

 SQL Dropping a View:


A view is deleted with the DROP VIEW command.

Syntax:
DROP VIEW view_name;

IMPLEMENTATION:
Practical-14

Aim: To make indexes for a table.

THEORY:
 INDEXES:
An Index is a database object that is used by the server to find a row in a table Quickly. Indexing a table is an
access strategy, that is, a way to sort and search records in the table. Indexes are essential to improve the
speed with which the records can be located and retrieved from a table.
Indexes are of two types:
1. Simple Index.
2. Composite Index.

1. Simple Index: An Index created on a Single Column of a table is called a simple Index.

Syntax:
CREATE INDEX index_name ON table_name (column_name);

IMPLEMENTATION:

2. Composite Index: An Index Created on more than one column is called a Composite Index.

Syntax:
CREATE INDEX index_name on table_name (column1, column2);

IMPLEMENTATION:
3. Unique Indexes
Unique indexes are used not only for performance, but also for data integrity. A unique index does not
allow any duplicate values to be inserted into the table. The basic syntax is as follows.
Syntax:

CREATE UNIQUE INDEX index_name ON table_name(column_name);

IMPLEMENTATION:

You might also like