100% found this document useful (1 vote)
97 views

Dbms Lab File

Oracle is a relational database management system that can reliably manage large amounts of data in a multi-user environment. It consists of tools that allow for easy and flexible application creation. SQL is the programming language used to store and manage data in relational database management systems like Oracle. SQL has three main components - DDL for data definition, DML for data manipulation, and DCL for data control.

Uploaded by

Manish Hooda
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
97 views

Dbms Lab File

Oracle is a relational database management system that can reliably manage large amounts of data in a multi-user environment. It consists of tools that allow for easy and flexible application creation. SQL is the programming language used to store and manage data in relational database management systems like Oracle. SQL has three main components - DDL for data definition, DML for data manipulation, and DCL for data control.

Uploaded by

Manish Hooda
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 53

Institute of Information Technology and Management

Affiliated to Guru Govind Singh Indraprastha University, New


Delhi

DBMS LAB
FILE

Prepared by
Manish Kumar
1213704419

Submitted to
Mr. Ashish Nayyar
Subject Faculty

Practical No :- 1
Oracle Introduction

1.1 Introduction:

ORACLE is a fourth generation relational database management system. In general, a database


management system (DBMS) must be able to reliably manage a large amount of data in a multi-
user environment so that many users can concurrently access the same data. All this must be
accomplished while delivering high performance to the users of the database. A DBMS must
also be secure from unauthorized access and provide efficient solutions for failure recovery. The
ORACLE Server provides efficient and effective solutions for the major database features.

ORACLE consists of many tools that allow you to create an application with ease and flexibility.
You must determine how to implement your requirements using the features available in
ORACLE, along with its tools. The features and tools that you choose to use to implement your
application can significantly affect the performance of your application.

Several of the more useful features available to ORACLE application developers are integrity
constraints, stored procedures and packages, database triggers, cost-based optimizer, shared
SQL, locking and sequences.

1.2 Versions:

● Oracle v2 : 2.3
● Oracle v3 : 3.1.3
● Oracle v4 : 4.1.4.0-4.1.4.4
● Oracle v5 : 5.0.22, 5.1.17, 5.1.22
● Oracle v6 : 6.0.17-6.0.36 (no OPS code), 6.0.37 (with OPS)
● Oracle7: 7.0.12–7.3.4
● Oracle8 Database: 8.0.3–8.0.6
● Oracle8i Database Release 1: 8.1.5.0–8.1.5.1
● Oracle8i Database Release 2: 8.1.6.0–8.1.6.3
● Oracle8i Database Release 3: 8.1.7.0–8.1.7.4
● Oracle9i Database Release 1: 9.0.1.0–9.0.1.5 ( patchset as of December 2003)
● Oracle9i Database Release 2: 9.2.0.1–9.2.0.8 (patchset as of April 2007)
● Oracle Database 10g Release 1: 10.1.0.2–10.1.0.5 (patchset as of February 2006)
● Oracle Database 10g Release 2: 10.2.0.1–10.2.0.5 (patchset as of April 2010)
● Oracle Database 11g Release 1: 11.1.0.6–11.1.0.7 (patchset as of September 2008)
● Oracle Database 11g Release 2: 11.2.0.1–11.2.0.4 (patchset as of August 2013)
● Oracle Database 12c Release 1: 12.1.0.1 (patchset as of June 2013)
● Oracle Database 12c Release 1: 12.1.0.2 (patchset as of July 2014)
● Oracle Database 12c Release 2: 12.2.0.1 (patchset as of March 2016)
1.3 Current version:
Oracle 12c database: - The latest version of Oracle’s database will be released in either
December 2012 or January 2013. CEO of Oracle Larry Ellison said in an onstage interview.In
Oracle 12c, ‘c’ stands for Cloud, Oracle in putting more effort on Cloud systems these days.
Oracle already released Oracle Enterprise Manager 12c earlier this year. Now Oracle has the
latest database a Oracle 11g release 2, but sure that it will be more ‘cloud’ based than Oracle
11g.

1.4 Login Procedure:

Steps:-

1. OPEN SQL PLUS


2. ENTER USERNAME, PASSWORD AND HOST STRING
3. SQL PLUS WINDOW WILL APPEAR.

Click on
icon

Click on
Login
button
Select SQL

Type query
here

Practical No :- 2
SQL Introduction

SQL, which is an abbreviation for Structured Query Language, is a language to request data from
a database, to add, update, or remove data within a database, or to manipulate the metadata of the
database.

Structure Query Language (SQL) is a programming language used for storing and managing data
in RDBMS. SQL was the first commercial language introduced for E.F Codd's Relational model.
Today almost all RDBMS (MySql, Oracle, Infomix, Sybase, MS Access) use SQL as the
standard database language. SQL is used to perform all types of data operations in RDBMS.

SQL is a declarative language in which the expected result or operation is given without the
specific details about how to accomplish the task. The steps required to execute SQL statements
are handled transparently by the SQL database. Sometimes SQL is characterized as non-
procedural because procedural languages generally require the details of the operations to be
specified, such as opening and closing tables, loading and searching indexes, or flushing buffers
and writing data to file systems. Therefore, SQL is considered to be designed at a higher
conceptual level of operation than procedural languages because the lower level logical and
physical operations aren't specified and are determined by the SQL engine or server process that
executes it.

2.1 SQL Components


SQL consists of three components:
1. Data Definition Language (DDL)
2. Data Manipulation Language (DML)
3. Data Control Language (DCL)

The Data Definition Language (DDL). This component of the SQL language is used to create
and modify tables and other objects in the database. For tables there are three main commands:
● CREATE – To create objects in the database
● ALTER – Alters the structure of the database
● DROP – Delete objects from the database
● TRUNCATE – Remove all records from a table, also free the space occupied by those
records

The Data Manipulation Language (DML) component of the SQL language is used to
manipulate data within a table. There are four main commands:
● SELECT to select rows of data from a table
● INSERT to insert rows of data into a table
● UPDATE to change rows of data in a table
● DELETE to remove rows of data from a table

The Data Control Language (DCL) This component of the SQL language is used to create
privileges to allow users access to, and manipulation of, the database. There are two main
commands:
● GRANT to grant a privilege to a user
● REVOKE to revoke (remove) a privilege from a user

2.2 SQL Tables and Columns

A relational database system contains one or more objects called tables. The data or information
for the database is stored in these tables. Tables are uniquely identified by their names and
consist of columns and rows. Columns contain the column name, data type, and any other
attributes for the column. Rows contain the records or data for the columns. Here is a sample
table called "weather".

City, state, high, and low are the columns. The rows contain the data for this table:

Weather
city state high low
Phoenix Arizona 105 90
Tucson Arizona 101 92
Flagstaff Arizona 88 69
San Diego California 77 60
New
Albuquerque 80 72
Mexico

SQL CREATE TABLE Example


The following example creates a table called "Persons" that contains five columns: PersonID,
LastName, FirstName, Address, and City:
Example

CREATE TABLE Persons (


     PersonID int,
     LastName varchar(255),
     FirstName varchar(255),
Address varchar(255),
     City varchar(255)
);
2.3 SQL Data types:

SQL data type is an attribute that specifies type of data of any object. Each column, variable and
expression has related data type in SQL.

You would use these data types while creating your tables. You would choose a particular data
type for a table column based on your requirement.

SQL Server offers six categories of data types for your use −
Exact Numeric Data Types
DATA TYPE FROM TO
Bigint -9,223,372,036,854,775,808 9,223,372,036,854,775,807
Int -2,147,483,648 2,147,483,647
Smallint -32,768 32,767
Tinyint 0 255
Bit 0 1
Decimal -10^38 +1 10^38 -1
Numeric -10^38 +1 10^38 -1
Money -922,337,203,685,477.5808 +922,337,203,685,477.5807
smallmoney -214,748.3648 +214,748.3647

Approximate Numeric Data Types


DATA TYPE FROM TO
Float -1.79E + 308 1.79E + 308
Real -3.40E + 38 3.40E + 38

Date and Time Data Types


DATA TYPE FROM TO
Datetime Jan 1, 1753 Dec 31, 9999
smalldatetime Jan 1, 1900 Jun 6, 2079
Stores a date like June 30,
Date
1991
Stores a time of day like 12:30
Time
P.M.
Note − Here, datetime has 3.33 milliseconds accuracy where as
smalldatetime has 1 minute accuracy.

Character Strings Data Types


DATA TYPE Description
Maximum length of 8,000 characters.( Fixed
Char
length non-Unicode characters)
Maximum of 8,000 characters.(Variable-length
Varchar
non-Unicode data).
Maximum length of 231characters, Variable-
varchar(max) length non-Unicode data (SQL Server 2005
only).
Variable-length non-Unicode data with a
Text
maximum length of 2,147,483,647 characters.

Unicode Character Strings Data Types


DATA TYPE Description
Maximum length of 4,000 characters.( Fixed
Nchar
length Unicode)
Maximum length of 4,000 characters.(Variable
Nvarchar
length Unicode)
Maximum length of 231characters (SQL Server
nvarchar(max)
2005 only).( Variable length Unicode)
Maximum length of 1,073,741,823 characters.
Ntext
( Variable length Unicode )

Binary Data Types


DATA TYPE Description
Maximum length of 8,000 bytes(Fixed-length
Binary
binary data )
Maximum length of 8,000 bytes.(Variable
Varbinary
length binary data)
Maximum length of 231 bytes (SQL Server
varbinary(max)
2005 only). ( Variable length Binary data)
Maximum length of 2,147,483,647 bytes.
Image
( Variable length Binary Data)
Misc Data Types
DATA TYPE Description
Stores values of various SQL Server-supported
sql_variant
data types, except text, ntext, and timestamp.
Stores a database-wide unique number that gets
Timestamp
updated every time a row gets updated
uniqueidentifier Stores a globally unique identifier (GUID)
Stores XML data. You can store xml instances
Xml in a column or a variable (SQL Server 2005
only).
Cursor Reference to a cursor object
Table Stores a result set for later processing

2.4 SQL Constraints

SQL Constraints are rules used to limit the type of data that can go into a table, to maintain the
accuracy and integrity of the data inside table. Constraints can be divided into following two
types,

● Column level constraints : limits only column data

● Table level constraints : limits whole table data

Constraints are used to make sure that the integrity of data is maintained in the database.
Following are the most used constraints that can be applied to a table.

● NOT NULL
● UNIQUE
● PRIMARY KEY
● FOREIGN KEY
● CHECK

● DEFAULT
NOT NULL Constraint

NOT NULL constraint restricts a column from having a NULL value. Once NOT NULL
constraint is applied to a column, you cannot pass a null value to that column. It enforces a
column to contain a proper value. One important point to note about NOT NULL constraint is
that it cannot be defined at table level.
UNIQUE Constraint

UNIQUE constraint ensures that a field or column will only have unique values. A UNIQUE
constraint field will not have duplicate data. UNIQUE constraint can be applied at column level
or table level.
Primary Key Constraint

Primary key constraint uniquely identifies each record in a database. A Primary Key must
contain unique value and it must not contain null value. Usually Primary Key is used to index the
data inside the table.
Foreign Key Constraint

FOREIGN KEY is used to relate two tables. FOREIGN KEY constraint is also used to restrict
actions that would destroy links between tables. To understand FOREIGN KEY, let's see it using
two table.

*Behavior of Foreign Key Column on Delete

There are two ways to maintain the integrity of data in Child table, when a particular record is
deleted in main table. When two tables are connected with Foreign key, and certain data in the
main table is deleted, for which record exit in child table too, then we must have some
mechanism to save the integrity of data in child table.

● On Delete Cascade : This will remove the record from child table, if that value of
foriegn key is deleted from the main table.
● On Delete Null : This will set all the values in that record of child table as NULL, for
which the value of foriegn key is deleted from the main table.

● If we don't use any of the above, then we cannot delete data from the main table for
which data in child table exists. We will get an error if we try to do so.

CHECK Constraint

CHECK constraint is used to restrict the value of a column between ranges. It performs check on
the values, before storing them into the database. It’s like condition checking before saving data
into a column.

Practical No :- 3

SQL DDL COMMANDS

3.1 SQL DDL COMMANDS SYNATX

1 CREATE command syntax with column level constraints

Syntax:
The following example shows how to use PRIMARY KEY constraint at column level.

CREATE TABLE TEST


(ID NUMBER CONSTRAINT TEST_PK PRIMARY KEY,
... );

2. CREATE command syntax with table level constraints

Syntax:
The following example shows how to define composite primary key using PRIMARY KEY
constraint at the table level.

CREATE TABLE TEST


( ...,
CONSTRAINT TEST_PK PRIMARY KEY (ID)
);

3. ALTER
Syntax:
ALTER TABLE (ADD Column)- To add a column in a table, use the following syntax:

ALTER TABLE table_name


ADD column_name datatype;

ALTER TABLE ( DROP COLUMN): -To delete a column in a table, use the following syntax
(notice that some database systems don't allow deleting a column):

ALTER TABLE table_name


DROP COLUMN column_name;
ALTER TABLE –(ALTER/MODIFY COLUMN):-To change the data type of a column in a
table, use the following syntax:
SQL Server / MS Access:

ALTER TABLE table_name


ALTER COLUMN column_name datatype;

4. DROP

Syntax:
Basic syntax of DROP TABLE statement is as follows:

DROP TABLE table_name;

5.TRUNCATE

Syntax:
The basic syntax of TRUNCATE TABLE is as follows:

TRUNCATE TABLE table_name;

6.RENAME
Syntax:

The basic syntax of TRUNCATE TABLE is as follows:

TRUNCATE TABLE table_name;


SQL DDL EXERCISE 1:-

1. Create a table student containing a unique roll no., address, date of birth where
year should be greater than 1992, phone no., course which should only be either
MCA or MBA.

2. Create a table marks containing roll no. as foreign key from student table, exam
date, exam name and marks of 3 subjects.

3. Alter student table to add two new column city & hobby with city name from four
fixed.
4. Alter marks database table to add new percent column.

5. Drop column hobby from student table.

6. Rename student table to stud table.


Practical No :- 4

4.1 SQL DML COMMANDS SYNATX

1. INSERT

Syntax: INSERT INTO table_nameVALUES (col1_val, col2_val,…,coln_val);

2. INSERT with &

Syntax: INSERT INTO table_nameVALUES(&colname1,&colname2,..,&colnamen);

3. INSERT with NULL values

Syntax: INSERT INTO table_nameVALUES(col1_val,col2_val,...,NULL,coln_val);

4. UPDATE

Syntax: UPDATE tablename SET colname=<value> where search_condition;

5. DELETE

Syntax: DELETE tablename where search_condition;

6. SELECT & SELECT WITH WHERE


Syntax: Select * FROM tablename;

Select col_name1,col_name2,.. FROM tablename;

Select * FROM tablename WHERE search_condition;

Select col_name1,col_name2,.. FROM tablename WHERE serach_condition;

7. SELECT USING OPERATORS (=, <,>,<>, <=, >=, AND , OR , NOT , BETWEEN,
IN ,LIKE)

Syntax: Select * FROM tablename WHERE colname1=value AND colname2<value;

Select * FROM tablename WHERE colname1>value AND colname2<>value;

Select * FROM tablename WHERE colname1>=value OR


colname2<=value; Select * FROM tablename WHERE colname BETWEEN
value1 AND value2;

Select * FROM tablename WHERE colname LIKE value%1 or val_ue

4.2 SQL DML EXERCISE:-

1. Find details of all students of 'MCA'

2. Find details of all students between 1990 to 2014


3. Find name and roll no of all students with middle name 'Singh’

4. Find student scoring marks below 70 in any one subject.

5. Find details of student have name starting with 'S'.

6. Find details of students having all subject marks between 75 and 90.

7. Find details of students having marks above 60 in all subjects.


8. Find the name, dob, age of all students having age>=20.

9. Delete all students record without dob.

10. Update City value in student table.

11. Find the name of the students living outside Delhi.


12. Update percentage in marks table (as (s1+s2+s3)/3).

Practical No :- 5

Create the following tables populate the tables and answer the queries given below: (Apply
all integrity constraints applicable).

Sailors (Sid: integer, Sname:string, Rating:integer, Age:integer, Income:real)

Boats (Bid: integer, Bname:string, Color:string)

Reserves (Sid:integer,Bid:integer,dob:date, dot:date)

Execute the following DDL commands:

1. Create table Sailors & Boats.


2. Alter table sailors.

3. Add check constraints.

4. Insert data into table Sailors & Boats.


5. Create duplicate table for Sailor.

6. Use of Drop table command.

7. Create table Reserves with foreign key constraint.


8. Alter table Reserve - add composite primary key.

9. Insert data into Reserve tables.

10. Delete data from sailor or boat table.


Solve the following queries:

1. Find the names of all sailors with a rating above 8.

2. Find the Id of sailors who have reserved boat number 104.

3. Find the names of all sailors with income above 5000.

4. Find the names of all sailors with income between 5000 to10000.
5. Find the names of all ‘brown’ color boats.

6. Find the names of all sailors having either rating above 7 or income above 6000.

7. Find the details of all boats reserved from 1st Jan 2011 to 10th Jan 2011.

8. Find the names of sailors who have reserved a ‘black’ or ‘brown’ boat.
9. Find the details of sailors having names starting from ‘R’.

10. Find the details of sailors having names middle name as ‘Kr’ and rating between 5- 6.

11. Find the names of sailors whose names begins and ends with ‘S’ and has atleast three
characters.

12. Find the ids of all ‘brown’ color boats in ascending order.

Practical No :- 6

Queries on string and math functions

1. Show the details of sailors in Lower order


2. Show the details of boats in Upper order

3. Show the details of boats in initcap order.

4. Find concatenation between two words using ||


5. Find concatenation between two words using concat

6. Find the position of letters in a word.

7. Find first five letters in a word


8. Perform ltrim and rtrim on a string

9. Find ASCII code in a word


10. Find length of any string.

11. Perform lpad & rpad on a string

12. Perform round, truncate of 50.789.


13. Perform ceil of 50.789.

14. Perform abs of 50.789, 50.90000000.

15. Perform power function.

16. Find square root of 36.

17. Find 50 mod 100.


18. Find the date 3 months from the today

19. Find last day of month.

20. Find next day of month.

21. Find the month between of year.

22. Find the date 15 days from the current date.


23. Convert the date in character to date format.

24. Print the date in dd-mm-yy format.

Practical No :- 7

7.1 General syntax and notes on types of Join:-


A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.

1. Equi join: 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.

You may also perform EQUI JOIN by using JOIN keyword followed by ON keyword and then
specifying names of the columns along with their associated tables to check equality.

2. Non-equi join: The SQL NON EQUI JOIN uses comparison operator instead of the equal
sign like >, <, >=, <= along with conditions.

3. Natural join: We have already learned that an EQUI JOIN performs a JOIN against equality
or matching column(s) values of the associated tables and an equal sign (=) is used as
comparison operator in the where clause to refer equality.

The SQL NATURAL JOIN is a type of EQUI JOIN and is structured in such a way that,
columns with the same name of associated tables will appear once only.

4. Cross join: The SQL CROSS JOIN produces a result set which is the number of rows in the
first table multiplied by the number of rows in the second table if no WHERE clause is used
along with CROSS JOIN. This kind of result is called as Cartesian Product.

If WHERE clause is used with CROSS JOIN, it functions like an INNER JOIN.

An alternative way of achieving the same result is to use column names separated by commas
after SELECT and mentioning the table names involved, after a FROM clause.

5. Outer join: The SQL OUTER JOIN returns all rows from both the participating tables which
satisfy the join condition along with rows which do not satisfy the join condition. The SQL
OUTER JOIN operator (+) is used only on one side of the join condition only.

1. Left outer: The LEFT OUTER JOIN returns a resultset table with the matched
data from the two tables and then the remaining rows of the left table and null
from the right table's columns.
2. Right outer: The RIGHT OUTER JOIN returns a resultset table with the matched
data from the two tables being joined, then the remaining rows of the right table
and null for the remaining left table's columns.
3. Full outer: The full outer join returns a resultset table with the matched data of
two table then remaining rows of both left table and then the right table.

6. Inner join: The INNER JOIN selects all rows from both participating tables as long as there is
a match between the columns. An SQL INNER JOIN is same as JOIN clause, combining rows
from two or more tables.

7. Using clause: Use the USING clause to specify the columns for the equijoin where several
columns have the same names but not same data types. Use the USING clause to match only one
column when more than one column matches. The NATURAL JOIN and USING clauses are
mutually exclusive.

8. On clause: The join condition for the natural join is basically an equijoin of identical column
names. ON clause can be used to join columns that have different names. Use the ON clause to
specify conditions or specify columns to join.The join condition are separated from other search
conditions. This is the most easiest and widely used form of the join clauses.
7.2 Queries on JOINS:-

1. Perform the equijoin on sailor and reserve table

2. Perform the inner join on boat and reserve table

3. Perform the outer join on sailor and reserve table

4. Perform the equijoin on sailor, boat and reserve table


5. Find the details of sailors who have reserved boat number not as 104.

6. Find the names of sailors who have reserved a ‘black’ or ‘brown’ boat.

7. Find the color of boats reserved by ‘Sam’.

8. Find the names of sailors who have reserved atleast one boat.
9. Find the sids of sailors who have a rating 5 or have reserved boat number 101.

10. Find the names of sailors who have not reserved a black boat.

11. Find the names and sids of sailors whose rating is better than some sailor called Manish

12. Find the sailors with highest rating.


13. Find the names and sids of sailors who have reserved all boats.

7.3 Group by, having clause, group value functions:-

14. Find the average age of all the sailors.

15. Find the average age of sailors with a rating of 10.

16. Find the name and age of the oldest sailor.


17. Count the number of sailors.

18. Find the names of sailors who are younger than the oldest sailor with a rating of 10.

19. Count the number of different sailor names.

20. Find the age of youngest sailor for each rating level.
21. Find the age of youngest sailor who is eligible to vote for each rating level with atleast

two such sailors.

22. Find the average age of sailors for each rating level that has atleast two sailors.

23. Find those ratings for which the average age of sailors is the minimum of overall ratings.

24. Find the names of sailors who have reserved a ‘black’ or ‘brown’ boat.
Practical No :- 8

1. Write a simple PL/SQL block to show the use of begin and end section.
2. Write a simple PL/SQL block of code to demonstrate the use of declare section.

3. Write a program to demonstrate the use of if-end if control

4. Write a PL/SQL block of code to demonstrate use of IF-THEN-ELSE-ENDIF control.


5. Write a PL/SQL block of code to demonstrate use of IF-THEN-ELSEIF-ELSE-ENDIF
control.

Practical No :- 9
Iterative Controls:-

6. Create a simple loop which displays the calculated values in a table format.
7. Write PL/SQL code to print the table of a number.

8. Write a PL/SQL block of code for inverting a given number using for loop.

9. Write a PL/SQL block of code to check whether a number entered by the user is
Armstrong or not.
Practical No :- 10

Cursors:-

10. Write a PL/SQL block of code for demonstrating the use of implicit cursors.
11. Write a PL/SQL block of code for demonstrating the use of explicit cursors.
12. Write a PL/SQL block of code for demonstrating the use of cursors using while loop.

13. Write a PL/SQL block of code to retrieve all employee names and their salary from the
table emp_rec using cursor for loop
Practical No :- 11

Exception Handling:-

14. Write a PL/SQL block of code to raise an exception for divide by zero.
15. Write a PL/SQL block of code for demonstrating the use of NO_DATA_FOUND
exception handler

16. Write a PL/SQL block of code for demonstrating the use of DUP_VAL_ON_INDEX
exception handler

17.Write a PL/SQL block of code to raise an application error using numbered exception
18. Write a PL/SQL block of code to raise an exception from different blocks.

Practical No :- 12

Procedures and Functions:-


19. Write a PL/SQL block of code demonstrating the use of procedures

20. Write a PL/SQL block of code demonstrating the use of functions

Practical No :- 13

Triggers:-

21. Write a PL/SQL block of code demonstrating the use of triggers(after)


22. Write a PL/SQL block of code demonstrating the use of triggers(before)

You might also like