Dbms Lab File
Dbms Lab File
DBMS LAB
FILE
Prepared by
Manish Kumar
1213704419
Submitted to
Mr. Ashish Nayyar
Subject Faculty
Practical No :- 1
Oracle Introduction
1.1 Introduction:
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.
Steps:-
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.
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
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 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
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,
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.
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
Syntax:
The following example shows how to use PRIMARY KEY constraint at column level.
Syntax:
The following example shows how to define composite primary key using PRIMARY KEY
constraint at the table level.
3. ALTER
Syntax:
ALTER TABLE (ADD Column)- To add a column in a table, use the following syntax:
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):
4. DROP
Syntax:
Basic syntax of DROP TABLE statement is as follows:
5.TRUNCATE
Syntax:
The basic syntax of TRUNCATE TABLE is as follows:
6.RENAME
Syntax:
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.
1. INSERT
4. UPDATE
5. DELETE
7. SELECT USING OPERATORS (=, <,>,<>, <=, >=, AND , OR , NOT , BETWEEN,
IN ,LIKE)
6. Find details of students having all subject marks between 75 and 90.
Practical No :- 5
Create the following tables populate the tables and answer the queries given below: (Apply
all integrity constraints applicable).
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
Practical No :- 7
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:-
6. Find the names of sailors who have reserved a ‘black’ or ‘brown’ boat.
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
18. Find the names of sailors who are younger than the oldest sailor with a rating of 10.
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
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.
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
Practical No :- 13
Triggers:-