Download as pdf or txt
Download as pdf or txt
You are on page 1of 103

DEPARTMENT OF COLLEGIATE AND TECHNICAL EDUCATION

KARNATKA

LABORATORY MANUAL

ON

Database System Concepts and PL/SQL


20IS34P & 20CS34P

By

Smt. SAVITHA R ME
Head of Department of Information Science and Engineering

GOVERNMENT POLYTECHNIC-109
Kalaburagi
CONTENT
SL.NO EXPERIMENTS PAGE NO
A. Install and setup DBMS software (MYSQL and Oracle 21c Express
1
1 Edition)
B. Learn the interface and explore the features of installed DBMS 3
Identify ER Model elements and Draw ER diagram for the given
2 5
specification using tools (MYSQL)
3 Map ER Model to relational model. 10

4 Normalize the above design 15

5 Validate the above design against integrity constraint 17

A. Data Definition Command CREATE 24


6
B. Data Definition Commands ALTER and DROP 27
Data Manipulation Commands –INSERT, UPDATE, DELETE and
7 23
SELECT (works on instances)
8 Filtering Data -Conditional retrieval - WHERE clause 38

A. Queries that uses Set Operators: UNION, INTERSECT, MINUS 62

B. Aggregate functions in SQL (Count, Sum, Max, Min, Avg) 65


9
C. Grouping the result of query - GROUP BY clause and HAVING
67
clause
10 Write Sub Queries To Retrieve Information from the Created Database 70

A. Create View and Query 80


11
B. Create User and Assign Privileges for DB Operations 83

A. Create and Execute Stored Procedure 87


12
B. Create and Execute Functions 89

A. Create and Execute Transaction 93


13 B. Call Previously Created Stored Procedure or Functions in
97
Transaction
Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 1A: Install and setup DBMS software (MYSQL and Oracle
21c Express Edition)

1. Phypmyadmin

Pypmyadmin is a free and open source administration tool for MYSQL and MariaDB. As a
portable web application written primarily in PHP, it has become one of the most popular
MYSQL administration tools, especially for web hosting services.

XAMPP contains one of the most popular Relational data base management systems in the world
in combination with the web server apache and scripting languages PHP/Perl.

XAMPP is the title used for a compilation of free software. The name is an acronym of

X Operating systems Linux, windows and Mac OS x

A Apache web server

M MYSQL/ MariaDB Relational Database

P Perl Scripting language

P PHP scripting language

1.1 Installing XAMPP

Step 1: Download XAMPP from Apache friends website

Step 2: Run.exe file

Step 3: Deactivate any antivirus software

Step 4: Deactivate UAC (User Account Control) during installation process( because it affects
installation of XAMPP)

Step 5: start the setup wizard

Step 6: choose software components

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 1


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Step 7: Choose the installation directions

Step 8: Start the installation process

Step 9: Complete installation

2. Installing Oracle 21c Express Edition

Download the Oracle Database 21c Express Edition installer. It can be found
at https://www.oracle.com/database/technologies/oracle21c-windows-downloads.html you will
need to select the Windows download and accept the license agreement before the download will
commence.

1. Run the installer.


2. The only required input is the SYSTEM account password, which you should write down
or make sure you remember.
3. When the installer finishes, click Finish.
4. Click windows search button and type SQL plus to open SQL command prompt,
5. Now enter the default user name as system and password which you have given while
installation to connect as shown below

SQL> connect system/gpt109


.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 2


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 1b: Learn the interface and explore the features of installed
DBMS

1.1. The XAMPP Control Panel :

Controls for the individual components of your test server can be reached through the XAMPP
control panel. The clear user interface logs are all actions and allow you to start and stop
individual module with a single window.

The administration section of your MYSQL database can be opened by

localhost/phpmyadmin/

The MYSQL database are managed by the user in phpmyadmin.

1.2. Login to MySQL command promt with Username and


Password

Open the XAMPP control panel and start the Apache and MYSQL server.
Open Command Prompt and navigate to the bin location of MySQL Server.

MySQL Server x.0\bin contains mysql.exe. The executable can accept username and the mention
of password as optional arguments.

Run the following command, in the command prompt with ‘root’ as default administrator
username

C:\XAMPP\MySQL\bin> mysql -u root -p

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 3


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Now, you have to enter the password. The password is not echoed back to the prompt, but stars.
This is for security and protecting your password from social engineering.

Press enter key after typing your password. In this case password is null so just press enter key

If your login is successful, mysql command line is opened.

If your login and password do not match with the users in the MySQL server, following message
is displayed in the Command Prompt.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 4


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 2: Identify ER Model elements and Draw ER diagram for


the given specification using tools (MYSQL)

2.1. What is an ER diagram?

An Entity Relationship Diagram (ERD) is a visual representation of different entities within a


system and how they relate to each other.

2.2. Entity Relationship Diagram (ERD) Symbols and Notations

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 5


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

2.4. ER diagram for Library Database with the following Specification

1. Library consists of many branches and each branch has many borrower which maintains borrower
ID,branch_id, Borrower_name, address, phone etc
2. Publishers publish books. And many copy of the book is maintained in each library branch.
library maintains the no_of_copies available of each book
3. Borrower borrows books from the library branch which records date_borrowed and due_date to
return the book for each borrower.
4. Publisher details such as publisher_id, publisher_name, address and phone are stored
5. Book details such as book_id, Title, author , publisher and price are maintained
6. Library brach details such as branch_id, name and address are maintained.

Steps to create an ER diagram for a library management system.

Step 1: The first step is to identify the entity sets. These are things that have more than one
instance of being existent. The entity sets for a library management system are as follows:

 Library_branch
 Book
 Publisher
 Borrower

Step 2: The second step is to associate attributes to the entity sets. This is important because
entity sets are recurring, such as several books, members, etc. The attributes are needed to
differentiate between entities. The respective attributes are as follows:

 Library_branch: Branch_ID, Branch_Name, Address, phone


 Book: Book_ID,Title, Author, Publisher, Price
 Publisher: Publisher_ID, Publisher_Name, Address, phone
 Borrower: Library_card_no, branch_id, Borrower_Name, Address, Phone

Step 3: The third step is to find the key attributes of each entity. The key attribute is unique to
every single entity component. The key attributes are as follows:

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 6


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

 Library_Branch – Branch_ID
 Book – Book_ID
 Publisher –Publisher_ID
 Borrower – Library_card_no

Step 4: The fourth step is to identify the relationship between the different entities present. This
is necessary to analyze the database clearly and make a better library management system.

A single member can borrow multiple books if they want. And at the same time same copy of the
book is borrowed by multiple members. This kind of relationship is known as a many-to-many
relationship.

In this relation, a single library branch can have multiple books present over there. And the same
time a single copy of the book is maintained over multiple branch Making this many-to-many
relationship as well.

The ONE LIBRARY BRANCH HAS MANY BORROWERS. There is only one BRANCH and
multiple BORROWES; thus, this is a one-to-many relationship.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 7


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

A single publisher can publish several books and supply them to the library. This is another one-
to-many relationship.

Step 5: The fifth step is to compile all these steps. Join the entities that go together and form an
ER diagram.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 8


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

2.3. Steps to create ER diagram using MYSQL phpmyadmin

Step 1: Go to phpmyadmin setup using http://loacalhost/phpmyadmin in your local machine

Step 2: Select the database for which you want to generate ER diagram (Create a Library
database with tables using GUI)

Step 3: Select the diagram from last menu item of more section

Step 4: You will land on page with different options to create ER diagram

Step 5: Final Library ER Diagram looks as below

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 9


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 3A. Map ER Model to relational model.

EXAMPLE 1: Consider the Library ER Diagram.

3.1. Rule 1: Convert each entities in to a Relation with its simple attributes as
relation’s fields

Library ER Diagram consists of 4 entities. Hence each entity can be converted into a relation as
follows

Relation 1: Book(book_id, title,author,publisher,price)

Relation 2: Library_branch (Branch_id, Branch_name, address, phone)

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 10


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Relation 3: publisher( publisher_id, publisher_name, address, phone)

Relation 4: Borrower (library_card_no, name, address, phone)

3.2 Rule 2: Multivalued attributes of an entity

If you have a multi-valued attribute, take the attribute and turn it into a new entity or table of its
own. Then make a 1:N relationship between the new entity and the existing one. In simple
words. 1. Create a table for the attribute. 2. Add the primary (id) column of the parent entity as a
foreign key within the new table.

In the above Library database we do not have any multivalued attributes.

3.3 Rule 3: Derived attribute of an entity

We can ignore derived attributes because it can be derived from stored attributes

3.2 Rule 3: Convert Each Relationships in to a table

1:1 Relationship

In 1:1 relationship 2 tables must be created and the relationship attributes are added
either in the left side entity/table of the relationship or in the right side entity/table.
In the above ER diagram we donot have any 1:1 relationship.

1:N Relationship

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 11


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

In 1:N relationship 2 tables must be created and the relationship attributes (if any)
are added in to the N side entity of the relationship along with the primary key of
the 1 side entity.

In the above 1:N relation N side entity is book hence primary key of publisher that is
publisher_id must be included in book relation/table. The final book relation becomes

Modified Relation 1 : book(book_id, title, author, publisher,price)

In the above 1:N relation many side entity is borrower hence primary key of library_branch that
is library_branch_id must be included in borrowr relation/table, so the final borrower relation
becomes

Modified Relation 4: Borrower (library_card_no, library_branch_id, name, address, phone)

M:M Relationship

For M:N relationship 3 tables must be created (entity1 to table 1, entity 2 to table 2 and relation1
as table 3) as follows

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 12


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

1. Borrower (library_card_no,name, address, phone)

2. Book (book_id, title, author, publisher,price) and

3. Borrow  the third table consists of the primary key of table 1 and 2 along with relationship
attributes date_of_issue and due_date. Hence final Borrow relation will become as follows

Relation 5: Borrow (library_card_no, Book_id, Date_of_issue, Due_date)

1. Book (book_id, title, author, publisher,price)

2. Library_branch (Branch_id, Branch_name, address, phone) and

3. Copies  the third table consists of the primary key of table 1 and 2 along with relationship
attribute no_of_copies. Hence final Copies relation will become as follows

Relation 6: Copies (Book_id, Branch_id, no_of_copies).

3.3 Final relations of a Library Relational Model are as follows

Relation 1: Book(book_id, title, author, publisher,price)

Relation 2: Library_branch (Branch_id, Branch_name, address, phone)

Relation 3: Publisher (publisher_id, publisher_name, address, phone)

Relation 4: Borrower (library_card_no,library_branch_id, name, address, phone)

Relation 5: Borrow (library_card_no, Book_id, Date_of_issue, Due_date)

Relation 6: Copies (Book_id, Branch_id, no_of_copies).

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 13


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

EXAMPLE 2: Consider the company database ER diagram

The relational schema for the above ER Diagram would be as shown below:

Relation 1: Company( CompanyID , name , address )


Relation 2: Staff( StaffID , dob , address , WifeID)
Relation 3: Child( ChildID , name , StaffID )
Relation 4: Wife ( WifeID , name )
Relation 5: Phone(PhoneID , phoneNumber , StaffID)
Relation 6: Task ( TaskID , description)
Relation 7: Work(WorkID , CompanyID , StaffID , since )
Relation 8: Perform(PerformID , StaffID , TaskID )

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 14


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 3B. Identify Various Constraint

Following are some of the most commonly used constraints available in SQL.

NOT NULL Constraint − Ensures that a column cannot have NULL value.

DEFAULT Constraint − Provides a default value for a column when none is specified.

UNIQUE Constraint − Ensures that all values in a column are different. A table can have many
numbers of unique keys

PRIMARY Key − uniquely identifies each row/record in a database table.A table can have only
one primary key

FOREIGN Key – It references the primary key value before accepting.

CHECK Constraint − The CHECK constraint ensures that all the values in a column satisfies
certain conditions.

INDEX − Used to create and retrieve data from the database very quickly.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 15


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

In the above library database following are the constraints identified

Constarint Attributes
Primary Key 1.Book(Book_id)

2.Library_branch(Branch _id)

3.Publisher(publisher_id)

4.Borrower(library_card_no)
Not Null Amost all the attributes
Default Address may have default value ‘Kalaburgi’
Unique Publisher name in publisher may be unique since we donot
want the names to be repeated
Reference key 1. Book_id in all other tables except book table

2. Branch_id in all other tables except Library_branch table

3. Publisher_id in all other tables except publisher table

4. library_card_no in all other tables except borrower table


check Not required
index Not required

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 16


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 4. Normalize the above design

4.1. Functional Dependency:

If XY then X determines Y. In other words X is used to identify Y uniquely.

4.2 Identify the Functional dependencies in each table of Library database

Usually primary key is used to identify the attribute values hence the library database has
following functional dependencies in relation 1,2,3 and 4. Whereas Relation 5 and 6 does not
have primary key but the combination of 2 reference keys can be used to identify each attribute
value uniquely.

Relation 1: Book(book_id, title, author, publisher, price)

FD1: Book_id{title, author, publisher, price}

Relation 2: Library_branch (Branch_id, Branch_name, address, phone)

FD2: Branch_id{Branch_name, address, phone}

Relation 3: Publisher( publisher_id, publisher_name, address, phone)

FD3: publisher_id{publisher_name, address, phone}

Relation 4: Borrower (library_card_no, library_branch_id, name, address, phone)

FD4:Library_card_no { library_branch_id, name, address, phone}

Relation 5: Borrow (library_card_no, Book_id, Date_of_issue, Due_date)

FD5: (Library_card_no, book_id){ date_of_issue, due_date}

Relation 6: Copies (Book_id, Branch_id, no_of_copies).

FD6: (Book_id ,branch_id )no_of_copies

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 17


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

4.2 Normalization

Normalization is a set of rule we should follow when designing the database to reduce the
redundancy (duplication) and to improve data integrity (correctness)

4.2.1 Normalization rule:

1. Eliminate redundant data and Null values


2. Reduce Modification anamalies(Problems/errors)
 Insertion anamaly:Inability to add data to a table due to the absense of
other data
 Deltion anamoly: unintended loss of data due to the deletion of other data
results in data inconsistency
 Updation anamaly: same data in more than one column cause
inconsistency when partial update accur
3. Ensures dependencies are logical

4.2.2 Types of Normalization

First Normal Form: A given relation is called in First Normal Form (1NF) if each cell of the
table contains only an atomic value.

Example: Consider the below table of order(sales), here a single order can contain multiple
itemsIDs, which is a multivalued attribute.

To remove the multivalued attribute we can create the above table as follows

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 18


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

But then the data of the table may contain more null values as shown below.

In order to remove storage of null values (to utilize memory) we have to decompose this table to
satisfy first normal form as shown below

Second Normal Form: A given relation is called in Second Normal Form (2NF) if and only if-

1.Relation already exists in 1NF.


2.All of the non-key columns are dependent on the tables’ primary key (No partial
dependency exists in the relation).

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 19


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Example: Consider the below table order (sale) where we have included firstname, lastname and
address of the customer.

In this case the function dependencies would be as follows

OrderID{CustomerID, orderDate,OrderTotal}

CustomerID{ Firstname,Lastname,Address}

But according to 2NF non-key attributes (columns) must depends on the primary key only.

Hence to satisfy the 2NF we have to decompose this table as shown below

Now the functional dependencies of each relation (table) will become as follows
Relation1: Customer
FD: Customer {Firstname, Lastname, Address}

Relation2: Order
FD: OrderId {CustomerID, OrderDate, OrderTotal}

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 20


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Relation3: OrderItems
FD: OrderItemId {OrderID, ItemID, Quantity}

Now in all the three relations, all the non-key attributes depends on the primary key only. This
makes the relations to satisfy 2NF.

Third Normal Form: A given relation is called in Third Normal Form (3NF) if and only if-

1. Relation already exists in 2NF.


2. No transitive dependency exists for non-prime attributes.

Example: Consider the below table Customer

The functiuonal dependencies here are as follows


FD1: CustomerID FirstName, LastName, Address, City}
FD2: City {state, Country}
FD3: State {Country}

Here there exist transitive decencies as follows


CustomerID City {state, Country} then we can say
CustomerID {state, Country} which should be removed for a relation to satisfy 3NF. So we
need to decompose the above relation as follows.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 21


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Now these relations eliminate transitive dependencies and hence satisfy 3NF.
The final relations for this Shopping database would be as flows which satisfies 1NF, 2NF and
3NF.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 22


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

4.2.3 Normalization of library Relational Model

According to 1 NF each attribute must have atomic values. We are not having any multivalued
attributes here hence all the relation satisfies 1NF

According to 2 NF there should not be any partial dependencies on primary key. There is no such
partial dependencies in the above relations hence all the relation satisfies 2NF

According to 3NF there should not be any transitive dependencies. There is no transitive
dependencies in the above relations hence all the relation satisfies 3NF.

Since each relation is having only one functional dependency and all the attributes are dependent
on the primary key only we can clearly say each relation satisfies 1NF, 2NF AND 3NF.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 23


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 5. Validate the above design against integrity constraint

In the Library Database the following data types and Constraints are identified validated.

Relation 1: book (book_id, title, author, publisher,price)

Attributes Data Constraint Description


Type
Book_id Varchar Primary Book_id is used to identify the books in a book table
Key hence it is a primary key and book_id can store
alphanumeric characters such as B11, B22 ets hence the
data type is Varchar
title varchar Not null Title of the book cannot be null hence we should use
Not null constraint
author varchar Not null author of the book cannot be null hence we should use
Not null constraint
publisher varchar Reference It references the publisher relation hence it is a reference
key key of publisher table’s primary key
price number Not null price is a Number we are using number data type and it
cannot be null

Relation 2: Library_branch (Branch_id, Branch_name, address, phone)

Attributes Data Constraint Description


Type
Branch_id varchar Primary Branch_id is used to identify the branch in a
Key Library_branch table hence it is a primary key
Branch_name varchar Not null Branch_name cannot be null hence we should use Not
null constraint
address varchar Not null address cannot be null hence we should use Not null
constraint
phone number Not using any constraint

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 24


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Relation 3: publisher (publisher_id, publisher_name, address, phone)

Attributes Data Constraint Description


Type
publisher_id Varchar Primary Key publisher_id is used to identify the publisher in a
publisher table hence it is a primary key
publisher_name varchar Unique publisher_name cannot be repeated hence we
should use unique constraint
address varchar Default if the address not known then default value
kalaburagi can be used.
(‘Kalaburagi’)
phone number phone can be null hence we are not using Not null
constraint

Relation 4: Borrower (library_card_no, library_branch_id, name, address, phone)

Attributes Data Constraint Description


Type
Library_card_no Varchar Primary Key Library_card_no is used to identify the
borrower in a borrower table hence it is a
primary key
Library_branch_id varchar Reference key It references the Library_branch relation hence
it is a reference key of library_branch table’s
primary key
name varchar Not null name cannot be null hence we should use Not
null constraint
address varchar Default if the address not known then default value
kalaburagi can be used.
(‘Kalaburagi’)
phone number - phone can be null hence we are not using Not
null constraint

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 25


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Relation 5: Borrow (library_card_no, Book_id, Date_of_issue, Due_date)

Attributes Data Constraint Description


Type
Library_card_no Varchar Reference It references the Library_branch relation hence it
key is a reference key of library_branch table’s
primary key
book_id varchar Reference It references the book relation hence it is a
key reference key of book table’s primary key
Date_of_issue date Not null Date of issue cannot be null hence we should use
Not null constraint
Due_date date Not null Due date cannot be null hence we should use Not
null constraint

Relation 6: Copies (Book_id, Branch_id, no_of_copies).

Attributes Data Constraint Description


Type
book_id varchar Reference It references the book relation hence it is a
key reference key of book table’s primary key
Brach_id varchar Reference It references the Library_branch relation hence it
key is a reference key of library_branch table’s
primary key
No_of_copies number Not null No_of_copies cannot be null hence we should
use Not null constraint

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 26


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 6A: Data Definition Command CREATE


NOTE: We are using mysql here (some minor syntax varies for mysql and oracle).

CREATE: Create command is used to create DB objects or structures such as database, tables,
views, user, function, procedure, packages and triggers
1. CREATING TABLES FOR LIBRARY DATABASE

TABLE 1
CREATE TABLE PUBLISHER(
PUBLISHER_ID VARCHAR(10)PRIMARY KEY,
PUBLISHER_NAME VARCHAR(30) UNIQUE,
ADDRESS VARCHAR(30) DEFAULT ‘KALABURAGI’,
PHONE BIGINT(10));

TABLE 1
CREATE TABLE BOOK(
BOOK_ID VARCHAR(10)PRIMARY KEY,
TITLE VARCHAR(20) NOT NULL,
AUTHOR VARCHAR(20) NOT NULL,
PUBLISHER VARCHAR(10)REFERENCES PUBLISHER(ID) ,
PRICE BIGINT(10) NOT NULL);

TABLE 3
CREATE TABLE LIBRARY_BRANCH(
BRANCH_ID VARCHAR(10)PRIMARY KEY,
BRANCH_NAME VARCHAR(20) NOT NULL,
ADDRESS VARCHAR(30)NOT NULL,
PHONE BIGINT(10));

TABLE 4
CREATE TABLE BOOK_COPIES(

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 27


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

BOOK_ID VARCHAR(10)REFERENCES book(BOOK_ID),


BRANCH_ID VARCHAR(10)REFERENCES library_branch(BRANCH_ID),
NO_OF_COPIES INTEGER(10)NOT NULL);

TABLE 5
CREATE TABLE BORROWER(
LIBRARY_CARD_NO VARCHAR(10)PRIMARY KEY,
BRANCH_ID VARCHAR(10) REFERENCES library_branch(BRANCH_ID),
NAME VARCHAR(20) NOT NULL,
ADDRESS VARCHAR(30) DEFAULT ‘KALABURAGI’,
PHONE BIGINT(10));

TABLE 6
CREATE TABLE BOOK_BORROWED(
BOOK_ID VARCHAR(10)REFERENCES book(BOOK_ID),
LIBRARY_CARD_NO VARCHAR(10)REFERENCES borrower(LIBRARY_CARD_NO),
DATE_BORROWED DATE,
DUE_DATE DATE);
LIBRARY ER DIAGRAM

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 28


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

2. CREATING TABLES FOR GRADEBOOK DATABASE

TABLE 1
CREATE TABLE STUDENT (
NAME VARCHAR(20),
STUDENT_NUMBER VARCHAR(10)PRIMARY KEY,
CLASS VARCHAR(10),
MAJOR VARCHAR(10));

TABLE 2
CREATE TABLE COURSE(
COURSE_NAME VARCHAR(20),
COURSE_NUMBER VARCHAR(10)PRIMARY KEY,
CREDIT_HOURS INTEGER(2),
DEPARTMENT VARCHAR(10));

TABLE 3
CREATE TABLE SECTION(
SECTION_ID VARCHAR(10)PRIMARY KEY,
COURSE_NUMBER VARCHAR(10)REFERENCES COURSE(COURSE_NUMBER),
SEMESTER INTEGER(1),
ACADEMIC_YEAR YEAR(4),
INSTRUCTOR VARCHAR(20));

TABLE 4
CREATE TABLE GRADE_REPORT(
STUDENT_NUMBER VARCHAR(10)REFERENCES STUDENT(STUDENT_NUMBER),
SECTION_ID VARCHAR(10)REFERENCES SECTION(SECTION_ID),
GRADE VARCHAR(1));

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 29


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

TABLE 5
CREATE TABLE PREREQUISITE(
COURSE_NO VARCHAR(10) REFERENCES COURSE(COURSE_NUMBER),
PREREQUISITE_NUMBER VARCHAR(20) REFERENCES COURSE(COURSE_NUMBER));

GRADEBOOK ER DIAGRAM

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 30


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

3. CREATING TABLES FOR COMPANY DATABASE

TABLE 1
CREATE TABLE DEPARTMENT(
DNAME VARCHAR(20),
DNUMBER VARCHAR(10)PRIMARY KEY,
MGR_START_DATE DATE);

TABLE 2
CREATE TABLE DEPT_LOCATION(
DNUMBER VARCHAR(10)REFERENCES DEPARTMENT(DNUMBER),
DLOCATION VARCHAR(20));

TABLE 3
CREATE TABLE EMPLOYEE(
FNAME VARCHAR(20),
MINIT VARCHAR(20),
LNAME VARCHAR(20),
SSN VARCHAR(10)PRIMARY KEY,
BDATE DATE,
ADDRESS VARCHAR(20),
SEX VARCHAR(6),
SALARY INTEGER(10),
SUPER_SSN VARCHAR(10)REFERENCES EMPLOYEE(SSN),
DNO VARCHAR(10)REFERENCES DEPARTMENT(DNUMBER));

ADDING MGR ATTRIBUTE TO DEPARMENT AFTER CREATIN EMPLOYEE TABLE


ALTER TABLE DEPARTMENT ADD COLUMN
MGR_SSN VARCHAR(10)REFERENCES EMPLOYEE(SSN);

TABLE 4

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 31


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

CREATE TABLE PROJECT(


PNAME VARCHAR(20),
PNUMBER VARCHAR(10)PRIMARY KEY,
PLOCATION VARCHAR(20),
DNUM VARCHAR(10)REFERENCES DEPARTMENT(DNUMBER));

TABLE 5
CREATE TABLE WORKS_ON(
ESSN VARCHAR(10)REFERENCES EMPLOYEE(SSN),
PNO VARCHAR(10)REFERENCES PROJECT(PNUMBER),
HOURS INTEGER(2));
TABLE 6
CREATE TABLE DEPENDENT(
ESSN VARCHAR(10)REFERENCES EMPLOYEE(SSN),
DEPENDENT_NAME VARCHAR(20),
SEX VARCHAR(6),
BDATE DATE,
RELATIONSHIP VARCHAR(10));
COMPANY ER DIAGRAM

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 32


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 6B: Data Definition Commands ALTER and DROP


NOTE: We are using oracle 21c from now onwards (some minor syntax varies for mysql and oracle).

ALTER: Alter command is used to alter the existing schema. Such as


1. Adding new attribute (column) or constraint
2. Modifying existing attribute’s datatype, size and constraints
3. Deleting the existing attribure or constraint

DESC/DESCRIBE: desc command is used to display the schema

SQL> desc emp;


Name Null? Type
----------------------------------------- -------- ----------------------------
ENO NOT NULL VARCHAR2(3)
DNO NUMBER(1)
ENAME VARCHAR2(20)
SAL NUMBER(10)

1. Adding new attribute (column) address to ‘emp’ table

SQL> alter table emp add address varchar(20);

Table altered.

SQL> desc emp;


Name Null? Type
----------------------------------------- -------- ----------------------------
ENO NOT NULL VARCHAR2(3)
DNO NUMBER(1)
ENAME VARCHAR2(20)
SAL NUMBER(10)

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 33


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

ADDRESS VARCHAR2(20)

SQL> SELECT * FROM EMP;

ENO DNO ENAME SAL ADDRESS


--- ---------- -------------------- ---------- --------------------
e1 1 RAMESH 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 1 raju 25000
e5 1 fayaz khan 30000

2. Modifying size of existing attribute ‘address’ from 20 to 30 of ‘emp’ table

SQL> alter table emp modify address varchar(30);

Table altered.

SQL> desc emp;


Name Null? Type
----------------------------------------- -------- ----------------------------
ENO NOT NULL VARCHAR2(3)
DNO NUMBER(1)
ENAME VARCHAR2(20)
SAL NUMBER(10)
ADDRESS VARCHAR2(30)

3. Deleting the attribute ‘address’ from the table ‘emp’.

SQL> alter table emp drop column address;

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 34


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Table altered.

SQL> desc emp;


Name Null? Type
----------------------------------------- -------- ----------------------------
ENO NOT NULL VARCHAR2(3)
DNO NUMBER(1)
ENAME VARCHAR2(20)
SAL NUMBER(10)

SQL> SELECT * FROM EMP;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 RAMESH 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 1 raju 25000
e5 1 fayaz khan 30000

4. Adding constraint primary key to ‘dno’ of the existing table ‘dept’

SQL> create table dept(dno number(1),dname varchar(10));

Table created.

SQL> desc dept;


Name Null? Type
----------------------------------------- -------- ----------------------------
DNO NUMBER(1)
DNAME VARCHAR2(10)

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 35


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

SQL> alter table dept add constraint c1 primary key(dno);

Table altered.

SQL> desc dept;


Name Null? Type
----------------------------------------- -------- ----------------------------
DNO NOT NULL NUMBER(1)
DNAME VARCHAR2(10)

5. Deleting existing constriant ‘c1’ from the ‘dept’ schema

SQL> alter table dept drop constraint c1;

Table altered.

SQL> desc dept;


Name Null? Type
----------------------------------------- -------- ----------------------------
DNO NUMBER(1)
DNAME VARCHAR2(10)

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 36


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

DROP : drop is used to delete any schema (table/view/user/function/procedure/packages)


from the database

SQL> select * from emp;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 jhon 10000
e2 1 smith 20000
e3 2 suraj 30000
e4 2 pushkar 35000
e5 2 bhavesh 55000

SQL> drop table emp;

Table dropped.

SQL> desc emp;


ERROR:
ORA-04043: object emp does not exist

SQL> select * from emp;


select * from emp
*
ERROR at line 1:
ORA-00942: table or view does not exist

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 37


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 7: Data Manipulation Commands –INSERT, UPDATE,


DELETE and SELECT (works on instances)

7.1 INSERT: Insert is used for inserting record values (instances) into a table

SQL> create table dept(dno number(1)primary key,dname varchar(10),dlocation varchar(10));

Table created.

SQL> create table emp(eno varchar(3)primary key,dno number(1)references dept(dno),ename


varchar(20),sal number(10));

Table created.

SQL> insert into dept values (1,'research','bangalore');

1 row created.

SQL> insert into dept values (2,'testing','gulbarga');

1 row created.

SQL> select * from dept;

DNO DNAME DLOCATION


---------- ---------- ----------
1 research bangalore
2 testing gulbarga

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 38


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

SQL> insert into emp values('&eno',&dno,'&ename',&sal);


Enter value for eno: e1
Enter value for dno: 1
Enter value for ename: smith
Enter value for sal: 10000
old 1: insert into emp values('&eno',&dno,'&ename',&sal)
new 1: insert into emp values('e1',1,'smith',10000)

1 row created.

SQL> /
Enter value for eno: e2
Enter value for dno: 1
Enter value for ename: jhon
Enter value for sal: 15000
old 1: insert into emp values('&eno',&dno,'&ename',&sal)
new 1: insert into emp values('e2',1,'jhon',15000)

1 row created.

SQL> /
Enter value for eno: e3
Enter value for dno: 1
Enter value for ename: santhosh
Enter value for sal: 20000
old 1: insert into emp values('&eno',&dno,'&ename',&sal)
new 1: insert into emp values('e3',1,'santhosh',20000)

1 row created.

SQL> /

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 39


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Enter value for eno: e4


Enter value for dno: 2
Enter value for ename: raju
Enter value for sal: 25000
old 1: insert into emp values('&eno',&dno,'&ename',&sal)
new 1: insert into emp values('e4',2,'raju',25000)

1 row created.

SQL> /
Enter value for eno: e5
Enter value for dno: 2
Enter value for ename: fayaz khan
Enter value for sal: 30000
old 1: insert into emp values('&eno',&dno,'&ename',&sal)
new 1: insert into emp values('e5',2,'fayaz khan',30000)

1 row created.

SQL> commit;
Commit complete.

SQL> select * from emp;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 2 raju 25000
e5 2 fayaz khan 30000

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 40


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

7.2 UPDATE: Update is used to modify the existing record (instances) value in a table

SQL> SELECT * FROM EMP;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 2 raju 25000
e5 2 fayaz khan 30000
e6 1 kartik 15000

6 rows selected.

7.2.1 Below query modifies the name of eno ‘e1’ to ‘RAMESH’

SQL> UPDATE EMP SET ENAME=‘RAMESH’ WHERE ENO='e1';

1 row updated.

SQL> SELECT * FROM EMP;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 RAMESH 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 2 raju 25000
e5 2 fayaz khan 30000
e6 1 kartik 15000

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 41


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

6 rows selected.

7.2.2. Below query modifies dno of all the employees to 1

SQL> UPDATE EMP SET DNO=1;

6 rows updated.

SQL> SELECT * FROM EMP;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 RAMESH 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 1 raju 25000
e5 1 fayaz khan 30000
e6 1 kartik 15000

7.3 DELETE: delete is used to delete the existing record (instances) from the table

SQL> select * from emp;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 RAMESH 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 1 raju 25000
e5 1 fayaz khan 30000

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 42


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

7.3.1 Below query deletes the records of employee whose salary is 10000

SQL> delete emp where sal=10000;

1 row deleted.

7.3.1 Below query deletes the records of employee whose eno is ‘e5’

SQL> delete emp where eno='e5';

1 row deleted.

SQL> select * from emp;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e2 1 jhon 15000
e3 1 santhosh 20000
e4 1 raju 25000
7.3.1 Below query deletes all the records of emp table

SQL> delete emp;

4 rows deleted.

SQL> select * from emp;

no rows selected

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 43


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

7.4 SELECT : select is used to retrieve the records (instances) from the database tables

7.4.1 Below query retrieves all the attributes and instances from the table ‘emp’( * is used to
retrieve all the attributes)

SQL> select * from emp;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 2 raju 25000
e5 2 fayaz khan 30000

7.4.1 Below query retrives only ‘eno’ attribute and all the instances from the table ‘emp’

SQL> select eno from emp;

ENO
---
e1
e2
e3
e4
e5
7.4.2 Below query retrives only ‘ename’ attribute and all the instances from the table ‘emp’

SQL> select ename from emp;

ENAME

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 44


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

--------------------
smith
jhon
santhosh
raju
fayaz khan

7.5 Using Aliases (AS) with Select command

Alias are used to display specific given names for the existing attributes. Below query display
ename as Employee_Name and sal as Salary from the emp table

SQL> select ename AS Employee_Name, sal AS Salary from emp;

Employee_Name Salary
-------------------- ----------
smith 10000
jhon 15000
santhosh 20000
raju 25000
fayaz khan 30000

7.6 ORDER BY clause: it is used to display the instances in a sorted order of the given
attribute. Asc is used for descending order. Desc is used for descending order. The default order
is Asc.

SQL> select * from emp;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 45


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

e1 1 smith 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 2 raju 25000
e5 2 fayaz khan 30000

7.6.1 Below query displays emp table instances (data/records) in the default asceding order of
their names

SQL> select * from emp order by ename;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e5 2 fayaz khan 30000
e2 1 jhon 15000
e6 1 kartik
e4 2 raju 25000
e3 1 santhosh 20000
e1 1 smith 10000

6 rows selected.

7.6.2. Below query displays emp table instances (data/records) in the asceding order of their
names

SQL> select * from emp order by ename asc;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e5 2 fayaz khan 30000
e2 1 jhon 15000

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 46


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

e6 1 kartik
e4 2 raju 25000
e3 1 santhosh 20000
e1 1 smith 10000
6 rows selected.

7.6.3 Below query displays emp table instances (data/records) in the descending order of their
names

SQL> select * from emp order by ename desc;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e3 1 santhosh 20000
e4 2 raju 25000
e6 1 kartik
e2 1 jhon 15000
e5 2 fayaz khan 30000

6 rows selected.

7.6.4 Below query displays emp table instances (data/records) in the default ascedning order of
their dno first and if two records has same dno then ename is used to sort.

SQL> select * from emp order by dno,ename;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e2 1 jhon 15000
e6 1 kartik

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 47


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

e3 1 santhosh 20000
e1 1 smith 10000
e5 2 fayaz khan 30000
e4 2 raju 25000

6 rows selected.
7.6.4 Below query displays emp table instances (data/records) in the default ascedning order of
their ename first and if two records has same ename then dno is used to sort.

SQL> select * from emp order by ename,dno;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e5 2 fayaz khan 30000
e2 1 jhon 15000
e6 1 kartik
e4 2 raju 25000
e3 1 santhosh 20000
e1 1 smith 10000

6 rows selected.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 48


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 8. Filtering Data -Conditional retrieval - WHERE clause

SQL> select * from emp;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 2 raju 25000
e5 2 fayaz khan 30000

8.1 WHERE CLAUSE USING RELATIONAL OPERATOR (<, <=, >, >=,< >,
= , !=):

8.1.1 Below query retrieves all the records of emp whose salary is greater than 20000

SQL> select * from emp where sal > 20000;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e4 2 raju 25000
e5 2 fayaz khan 30000

8.1.2 Below query retrieves all the records of emp whose salary is greater than or equal to
20000

SQL> select * from emp where sal >= 20000;

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 49


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e3 1 santhosh 20000
e4 2 raju 25000
e5 2 fayaz khan 30000

8.1.3 Below query retrieves all the records of emp whose salary is less than 20000

SQL> select * from emp where sal < 20000;

ENO DNO ENAME SAL


--- ---------- -------------------- ---------
e1 1 smith 10000
e2 1 jhon 15000

8.1.4 Below query retrieves all the records of emp whose salary is less than or equal to
20000

SQL> select * from emp where sal <= 20000;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e2 1 jhon 15000
e3 1 santhosh 20000

8.1.5 Below query retrieves all the records of emp whose salary not equal to 20000 using <>

SQL> select * from emp where sal < > 20000;

ENO DNO ENAME SAL

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 50


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

--- ---------- -------------------- ----------


e1 1 smith 10000
e2 1 jhon 15000
e4 2 raju 25000
e5 2 fayaz khan 30000

8.1.6 Below query retrieves all the records of emp whose salary not equal to 20000 using !=

SQL> select * from emp where sal != 20000;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e2 1 jhon 15000
e4 2 raju 25000
e5 2 fayaz khan 30000

8.1.7 Below query retrieves all the records of emp whose salary is equal to 20000 using =

SQL> select * from emp where dno = 1;


ENO DNO ENAME SAL
--- ---------- -------------------- ----------
e1 1 smith 10000
e2 1 jhon 15000
e3 1 santhosh 20000
no rows selected

8.1.8 Below query retrieves all the records of emp whose name is smith

SQL> select * from emp where ename='smith';


ENO DNO ENAME SAL

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 51


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

--- ---------- -------------------- ----------


e1 1 smith 10000

8.2 WHERE CLAUSE USING IS NULL OPERATOR:

SQL> insert into emp (eno,dno,ename) values('e6',1,'kartik');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from emp;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 2 raju 25000
e5 2 fayaz khan 30000
e6 1 kartik

6 rows selected.
8.2.1 Below query retrieves all the records of emp whose salary is null

SQL> select * from emp where sal IS NULL;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 52


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

e6 1 kartik

8.2.2 Below query retrieves all the records of emp whose salary is not null

SQL> select * from emp where sal IS NOT NULL;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 2 raju 25000
e5 2 fayaz khan 30000

8.3 SQL Special operators (And, Or, Like, Between, In)

SQL> select * from emp;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 2 raju 25000
e5 2 fayaz khan 30000

8.3.1 Below query retrieves all the records of emp whose working for deptno 1 and alos having
salary greater than 10000

SQL> select * from emp where dno=1 AND sal >10000;

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 53


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e2 1 jhon 15000
e3 1 santhosh 20000

8.3.2 Below query retrieves all the records of emp whose working for deptno 1 or whose salary
is greater than 10000

SQL> select * from emp where dno=1 OR sal > 10000;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 2 raju 25000
e5 2 fayaz khan 30000

8.3.3 Below query retrieves all the records of emp whose working for deptno 1 using IN operator

SQL> select * from emp where dno IN 1;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e2 1 jhon 15000
e3 1 santhosh 20000

8.3.4 Below query retrieves all the records of emp whose working for deptno 1 or 2 using IN
operator

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 54


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

SQL> select * from emp where dno IN (1,2);

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 2 raju 25000
e5 2 fayaz khan 30000

8.3.5 Below query retrieves all the records of emp who is not working for deptno 1 or 2 using
NOT IN operator

SQL> select * from emp where dno NOT IN (1,2);

no rows selected

8.3.6 Below query retrieves all the records of emp who is not working for deptno 1 using NOT
IN operator

SQL> select * from emp where dno NOT IN (1);

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e4 2 raju 25000
e5 2 fayaz khan 30000

8.3.7 Below query retrieves all the records of emp whose salary lies between 15000 and 25000

SQL> select * from emp where sal BETWEEN 15000 AND 25000;
ENO DNO ENAME SAL

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 55


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

--- ---------- -------------------- ----------


e2 1 jhon 15000
e3 1 santhosh 20000
e4 2 raju 25000

8.3.8 Below query retrieves all the records of emp whose name is smith

SQL> select * from emp where ename LIKE 'smith';

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000

8.3.9 Below query retrieves all the records of emp whose name starts with s

SQL> select * from emp where ename LIKE ' s % ';

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e3 1 santhosh 20000

8.3.10 Below query retrieves all the records of emp whose name ends with h

SQL> select * from emp where ename LIKE ' % h ';

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e3 1 santhosh 20000

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 56


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

8.3.11 Below query retrieves all the records of emp whose name contains ‘h’

SQL> select * from emp where ename LIKE ' % h % ';

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e5 2 fayaz khan 30000

8.3.12 Below query retrieves all the records of emp whose name’s second letter is a

SQL> select * from emp where ename LIKE ' _ a % ';

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e3 1 santhosh 20000
e4 2 raju 25000
e5 2 fayaz khan 30000

8.3.13 Below query retrieves all the records of emp whose name’s fourth letter is a

SQL> select * from emp where ename LIKE '_ _ _a % ' ;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e5 2 fayaz khan 30000

8.3.14 Below query retrieves all the records of emp whose name’s second letter from the last is s
SQL> select * from emp where ename LIKE ' % s _ ‘ ;

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 57


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e3 1 santhosh 20000
8.3.15 Below query retrieves all the records of emp whose name’s second letter from the last is t

SQL> select * from emp where ename LIKE ' % t _' ;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000

8.3.16 Below query retrieves all the records of emp whose name contains ‘a’

SQL> select * from emp where ename LIKE ' % a % ';

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e3 1 santhosh 20000
e4 2 raju 25000
e5 2 fayaz khan 30000

8.4 Query multiple tables using JOIN operation.

SQL> connect system/gpt109;


Connected.

SQL> create table dept (dno number(2),dname varchar(10),location varchar(10));

Table created.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 58


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

SQL> create table emp(eno varchar(2),ename varchar(10),salary number(10,2),dno number(2));

Table created.

SQL> insert into dept values(1,'admin','banglore');

1 row created.

SQL> insert into dept values(4,'testing','gulbarga');

1 row created.

SQL> insert into emp values('e1','smith',10000.50,1);

1 row created.

SQL> insert into emp values('e2','john',16000.50,1);

1 row created.

SQL> insert into emp values('e3','john',5000.50,3);

1 row created.

SQL> commit;

Commit complete.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 59


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

SQL> select * from dept;

DNO DNAME LOCATION


---------- ---------- ----------
1 admin banglore
4 testing gulbarga

SQL> select * from emp;

EN ENAME SALARY DNO


-- ---------- ---------- ----------
e1 smith 10000.5 1
e2 john 16000.5 1
e3 john 5000.5 3

1. CARTESION JOIN

SQL> select * from emp,dept;

EN ENAME SALARY DNO DNO DNAME LOCATION


-- ---------- ---------- ---------- ---------- ---------- ----------
e1 smith 10000.5 1 1 admin banglore
e2 john 16000.5 1 1 admin banglore
e3 john 5000.5 3 1 admin banglore
e1 smith 10000.5 1 4 testing gulbarga
e2 john 16000.5 1 4 testing gulbarga
e3 john 5000.5 3 4 testing gulbarga

6 rows selected.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 60


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

2. EQUI JOIN (INNER JOIN)

SQL> select * from emp , dept where emp.dno=deptt.dno;

EN ENAME SALARY DNO DNO DNAME LOCATION


-- ---------- ---------- ---------- ---------- ---------- ----------
e1 smith 10000.5 1 1 admin banglore
e2 john 16000.5 1 1 admin banglore

3. RIGHT OUTER JOIN

SQL> select * from emp , dept where emp.dno=dept.dno(+);

EN ENAME SALARY DNO DNO DNAME LOCATION


-- ---------- ---------- ---------- ---------- ---------- ----------
e2 john 16000.5 1 1 admin banglore
e1 smith 10000.5 1 1 admin banglore
e3 john 5000.5 3

4. LEFT OUTER JOIN

SQL> select * from emp , dept where emp.dno(+)=dept.dno;

EN ENAME SALARY DNO DNO DNAME LOCATION


-- ---------- ---------- ---------- ---------- ---------- ----------
e1 smith 10000.5 1 1 admin banglore
e2 john 16000.5 1 1 admin banglore
4 testing gulbarga

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 61


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 9A. Queries that uses Set Operators: UNION, INTERSECT,


MINUS

SQL> create table A(id number(10), name varchar(20));

Table created.

SQL> create table B(id number(10), name varchar(20));

Table created.

SQL> insert into A values(1,'smith');

1 row created.

SQL> insert into A values(2,'john');

1 row created.

SQL> insert into B values(2,'john');

1 row created.

SQL> insert into B values(3,'wilson');

1 row created.

SQL> select * from A;

ID NAME
---------- --------------------

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 62


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

1 smith
2 john

SQL> select * from B;

ID NAME
---------- --------------------
2 john
3 wilson

UNION: Used to retrieve all the records from subquery1 and subqury2;
Synatx: Subquery-1 UNION Subqury-2

SQL> select * from A UNION select * from B;

ID NAME
---------- --------------------
1 smith
2 john
3 wilson

INTERSECT: Used to retrieve all the records which are common in both subquery1 and
subqury2;
Synatx: Subquery-1 INTERSECT Subqury-2

SQL> select * from A INTERSECT select * from B;

ID NAME
---------- --------------------
2 john

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 63


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

MINUS: Used to retrieve all the records which EXISTS IN subquery1 and NOT EXISTS IN
subqury2;
Synatx: Subquery-1 MINUS Subqury-2

SQL> select * from A MINUS select * from B;

ID NAME
---------- --------------------
1 smith

SQL> select * from B MINUS select * from A;

ID NAME
---------- --------------------
3 wilson

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 64


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 9B: Aggregate functions in SQL (Count, Sum, Max, Min,


Avg)
SQL> select * from emp;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 2 raju 25000
e5 2 fayaz khan 30000
e6 1 kartik

9b.1 Query to retrieve total number of employees in emp table(instead of * we can also use key
attribute, in this case Eno)

SQL> select count(*) from emp;

COUNT(*)
----------
6
SQL> select count(*) AS no_of_employees from emp;

NO_OF_EMPLOYEES
---------------
6
9b.2 Query to retrieve total number of employees getting salary in emp table

SQL> select count(sal) from emp;


COUNT(SAL)

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 65


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

----------
5
9b.3 Query to retrieve sum of salary of all the employees in emp table
SQL> select sum(sal) AS total_salary_paid from emp;
TOTAL_SALARY_PAID
-----------------
100000

9b.4 Query to retrieve maximum salary of the employees in emp table

SQL> select max(sal) AS highest_salary from emp;

HIGHEST_SALARY
--------------
30000

9b.5 Query to retrieve minimum salary of the employees in emp table

SQL> select min(sal) AS lowest_salary from emp;

LOWEST_SALARY
-------------
10000

9b.6 Query to retrieve average of salary of the employees in emp table

SQL> select avg(sal) from emp;

AVG(SAL)
----------
20000

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 66


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 9C. Grouping the result of query - GROUP BY clause and


HAVING clause

SQL> select * from emp;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 smith 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 2 raju 25000
e5 2 fayaz khan 30000
e6 1 kartik

SQL> select dno from emp group by dno;

DNO
----------
1
2

SQL> select dno,count(eno) as no_of_employee from emp group by dno;

DNO NO_OF_EMPLOYEE
---------- --------------
1 4
2 2

SQL> select dno,count(eno) from emp group by dno;

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 67


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

DNO COUNT(ENO)
---------- ----------
1 4
2 2

SQL> select dno,count(eno) from emp group by dno having dno=1;

DNO COUNT(ENO)
---------- ----------
1 4

SQL> select dno,count(eno) from emp group by dno having count(eno)<2;

no rows selected

SQL> select dno,count(eno) from emp group by dno having count(eno)<=2;

DNO COUNT(ENO)
---------- ----------
2 2

SQL> select dno,sum(sal) from emp group by dno;

DNO SUM(SAL)
---------- ----------
1 45000
2 55000

SQL> select dno,sum(sal) from emp group by dno having sum(sal)>50000;

DNO SUM(SAL)

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 68


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

---------- ----------
2 55000

SQL> select dno,count(*),sum(sal),avg(sal),max(sal),min(sal) from emp group by dno;

DNO COUNT(*) SUM(SAL) AVG(SAL) MAX(SAL) MIN(SAL)


---------- ---------- ---------- ---------- ---------- ----------
1 4 45000 15000 20000 10000
2 2 55000 27500 30000 25000

SQL> select dno,count(*),sum(sal),avg(sal),max(sal),min(sal) from emp group by dno having


sum(sal)>50000;

DNO COUNT(*) SUM(SAL) AVG(SAL) MAX(SAL) MIN(SAL)


---------- ---------- ---------- ---------- ---------- ----------
2 2 55000 27500 30000 25000

SQL> select dno,max(sal) from emp group by dno;

DNO MAX(SAL)
---------- ----------
1 20000
2 30000

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 69


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 10: Write Sub Queries To Retrieve Information from the


Created Database

Consider the Company Database with following tables and records

SQL> select * from employee;

ENAME SSN SALARY DN S SUPERSSN


-------------------- ---------- ---------- -- - ----------
RAJA 1234 30000 1 M 9998
FRANKLIN 3334 40000 1 M 9998
AHMED 9998 25000 1 M 6668
JAMES 9876 43000 2 M 8886
ANAND 6668 55000 1 M 0
RAMESH 8886 25000 2 M 6668

6 rows selected.

SQL> select * from department;

DNAME DNUMBER MGRSSN


-------------------- ------- ----------
HEAD OFFICE 1 6668
ADMIN 2 8886

SQL> select * from project;

PNO PNAME DN PLOCATION


---------- ---------- -- ----------
11 PX 1 BANG

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 70


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

12 PY 1 BANG
13 BENEFITS 1 BANG
14 PZ 2 BIJAPUR
15 COMPUTER 2 GULBARGA

SQL> select * from works_on;

ESSN PNO HOURS


---------- ---------- ----------
1234 11 10
3334 11 32
6668 12 5
9998 12 40
9998 13 2
9876 13 42
9876 14 42
8886 15 35

8 rows selected.

SQL> select * from dependent;

ESSN DEP_NAME SEX RELATION


---------- ---------- --- ----------
1234 KIRAN M SON
1234 USHA F WIFE
9998 NOORIE F WIFE
6668 ANU F DAUGHTER
6668 ROOPA F WIFE

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 71


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

QUERY 1: WRITE A NESTED QUERY TO SELECT THE SSN OF ALL EMPLOYEE WHO
WORKS ON THE SAME PROJECT THAT THE EMPLOYEE 'AHMED' WORKS ON

SQL> select distinct essn from works_on where pno in(select pno from works_on where essn
in(select ssn from employee where ename='AHMED'));

ESSN
----------
9876
9998
6668

QUERY 2: WRITE A NESTED QUERY TO SELECT THE PROJECT NUMBER IN WHICH


'ANAND' INVOLVED AS A MANAGER.

SQL> select distinct pno from project where pno in(select pno from project, department,
employee where project.dno=department.dnumber and mgrssn=ssn and ename='ANAND');

PNO
----------
11
13
12

QUERY 3:WRITE A NESTED QUERY TO SELECT THE PROJECT NUMBER IN WHICH


'ANAND' INVOLVED AS WORKER.

SQL> select distinct pno from project where pno in (select pno from works_on,employee where
essn=ssn and ename='ANAND');

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 72


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

PNO
----------
12

QUERY 4: WRITE A NESTED QUERY TO SELECT THE PROJECT NUMBER IN WHICH


'ANAND' INVOLVED AS A MANAGER AND ALSO AS A WORKER

SQL> select distinct pno from project where pno in(select pno from project , department,
employee where project.dno=department.dnumber and mgrssn=ssn and ename='ANAND') or
pno in(select pno from works_on,employee where essn=ssn and ename='ANAND');

PNO
----------
11
13
12

QUERY 5: WRITE A QUERY TO SELECT THE EMPLOYEE RECORDS OF


DEPARTMENT NUMBER 1 WHOSE SALARY LIES BETWEEN 20000 AND 40000

SQL> select * from employee where salary between 20000 and 40000 and dno=1;

ENAME SSN SALARY DN S SUPERSSN


-------------------- ---------- ---------- -- - ----------
RAJA 1234 30000 1 M 9998
FRANKLIN 3334 40000 1 M 9998
AHMED 9998 25000 1 M 6668

QUERY 6: WRITE A QUERY TO SELECT THE EMPLOYEE NAME WHO HAS ATLEAT
ONE DEPENDENT.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 73


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

SQL> select distinct ename from employee,dependent where ssn=essn;

ENAME
--------------------
AHMED
ANAND
RAJA

QUERY 7: WRITE A QUERY TO SELECT THE MANAGER WHO HAS ATLEAT ONE
DEPENDENT.

SQL> select distinct ename from employee, dependent,department where ssn=essn and
ssn=mgrssn;

ENAME
--------------------
ANAND

(OR)

SQL> select ename from employee where ssn in(select essn from dependent where essn in(select
mgrssn from department));

ENAME
--------------------
ANAND
QUERY 8: WRITE A QUERY TO SELECT THE EMPLOYEE NAME WHO HAS
DEPENDENT WITH THE SAME SEX.

SQL> select distinct ename from employee,dependent where ssn=essn and employee.sex =
dependent.sex;

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 74


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

ENAME
--------------------
RAJA

QUERY 9: WRITE A QUERY TO SELECT THE EMPLOYEE NAME AND ALSO THE
DEPENDENT NAME WHO HAS DEPENDENT WITH THE SAME SEX.

SQL> select distinct ename,dep_name from employee,dependent where ssn=essn and


employee.sex = dependent.sex;

ENAME DEP_NAME
-------------------- ----------
RAJA KIRAN

QUERY 10: WRITE A QUERY TO SELECT THE EMPLOYEE NAME WHO DOES NOT
HAVE DEPENDENT.

SQL> select ename from employee where ssn not in (select ssn from dependent where ssn =
essn);

ENAME
--------------------
FRANKLIN
JAMES
RAMESH

QUERY 11: WRITE A QUERY TO SELECT THE EMPLOYEE NAME AND HIS
DEPARTMENT NAME AND ALSO THE PROJECT NAMES ON WHICH THE EMPLOYEE
IS WORKING ON.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 75


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

SQL> select dname,ename,pname from department1,employee,works_on,project where dnumber


= project.dno and ssn=essn and project.pno=works_on.pno;

DNAME ENAME PNAME


-------------------- -------------------- ----------
HEAD OFFICE RAJA PX
HEAD OFFICE FRANKLIN PX
HEAD OFFICE AHMED BENEFITS
HEAD OFFICE AHMED PY
ADMIN JAMES PZ
HEAD OFFICE JAMES BENEFITS
HEAD OFFICE ANAND PY
ADMIN RAMESH COMPUTER

8 rows selected.

QUERY 12: WRITE A QUERY TO SELECT THE EMPLOYEE NAME WHOSE SALARY IS
GREATER THAN THE EMPLOYEE'S MAXIMUM SALARY OF DEPARTMENT NUMBER
2.

SQL> select ename from employee where salary>(select max(salary) from employee where
dno=2);

ENAME
--------------------
ANAND

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 76


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

10.1 ANY, ALL, SOME, EXIST and NOT EXIST in nested queries

SQL> SELECT * FROM CUSTOMER;

NAME AGE SALARY

---------- ---------- ----------

A 25 2000

B 33 4000

C 23 8500

D 27 6000

E 24 4150

ANY: Any is used when 1 or more records match the sub condition and the result is true for
those records.

SQL> SELECT * FROM customer WHERE Age > ANY( SELECT Age FROM customer
WHERE Salary > 5000);

NAME AGE SALARY

---------- ---------- ----------

A 25 2000

B 33 4000

D 27 6000

E 24 4150

ALL: All is used when all the records match the sub condition then the output to be true else
false.

SQL> SELECT * FROM customer WHERE Age > ALL( SELECT Age FROM customer
WHERE Salary > 5000);

NAME AGE SALARY

---------- ---------- ----------

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 77


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

B 33 4000

SOME: It is used to must match at least one row in the subquery and must be preceded by
comparison operators. Suppose using greater than ( >) with SOME means greater than at least
one value.

SQL> SELECT * FROM customer WHERE Age > SOME( SELECT Age FROM customer
WHERE Salary > 5000);

NAME AGE SALARY

---------- ---------- ----------

A 25 2000

B 33 4000

D 27 6000

E 24 4150

EXISTS: It returns true if the sub query returns any rows.

Below sub query returns true, because in customer table we are having records with salary<5000.
When the sub query returns true main query returns its records

SQL> SELECT * FROM customer WHERE exists( SELECT Age FROM customer WHERE
Salary < 5000);

NAME AGE SALARY

---------- ---------- ----------

A 25 2000

B 33 4000

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 78


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

C 23 8500

D 27 6000

E 24 4150

NOT EXISTS: It returns true if the sub query returns no rows.

Below sub query returns false, because in customer we are having records with salary<5000.
When the sub query returns false main query returns no records

SQL> SELECT * FROM customer WHERE NOT exists( SELECT Age FROM customer
WHERE Salary < 5000);

no rows selected

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 79


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 11A: Create View and Query

SQL> select * from emp;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 jhon 10000
e2 1 smith 20000
e3 2 suraj 30000
e4 2 pushkar 35000
e5 2 bhavesh 55000

SQL> create view dept_1_emp as select * from emp where dno=1;

View created.

SQL> desc dept_1_emp;


Name Null? Type
----------------------------------------- -------- ----------------------------
ENO NOT NULL VARCHAR2(3)
DNO NUMBER(1)
ENAME VARCHAR2(20)
SAL NUMBER(10)

SQL> select * from dept_1_emp;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e1 1 jhon 10000
e2 1 smith 20000

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 80


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

SQL> create view dept_2_emp as select * from emp where dno=2;

View created.

SQL> desc dept_2_emp;


Name Null? Type
----------------------------------------- -------- ----------------------------
ENO NOT NULL VARCHAR2(3)
DNO NUMBER(1)
ENAME VARCHAR2(20)
SAL NUMBER(10)

SQL> select * from dept_2_emp;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e3 2 suraj 30000
e4 2 pushkar 35000
e5 2 bhavesh 55000

SQL> UPDATE dept_2_emp SET ENAME='SURAJ PATIL' WHERE ENO='e3';

1 row updated.

SQL> select * from dept_2_emp;

ENO DNO ENAME SAL


--- ---------- -------------------- ----------
e3 2 SURAJ PATIL 30000
e4 2 pushkar 35000

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 81


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

e5 2 bhavesh 55000

SQL> drop view dept_1_emp;

View dropped.

SQL> select * from dept_1_emp;


select * from dept_1_emp
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL> drop view dept_2_emp;

View dropped.

SQL> desc dept_2_emp;


ERROR:
ORA-04043: object dept_2_emp does not exist

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 82


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 11B: Create User and Assign Privileges for DB Operations

SQL>Connect system/gpt109

SQL>alter session set "_ORACLE_SCRIPT"=true;

SQL> create user u3 identified by mypwd;

User created.

SQL> grant create session to u3;

Grant succeeded.

SQL> disconnect;

Disconnected from Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production

Version 21.3.0.0.0

SQL> connect u3/mypwd;

Connected.

SQL> create table a(id number(10));

create table a(id number(10))

ERROR at line 1:

ORA-01031: insufficient privileges

SQL> disconnect;

Disconnected from Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 83


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Version 21.3.0.0.0

SQL> connect system/gpt109;

Connected.

SQL> grant create table to u3;

Grant succeeded.

SQL> connect u3/mypwd;

Connected.

SQL> create table a(id number(10));

Table created.

SQL> insert into a values(1);

insert into a values(1)

ERROR at line 1:

ORA-01950: no privileges on tablespace 'USERS'

SQL> disconnect;

Disconnected from Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production

Version 21.3.0.0.0

SQL> connect system/gpt109;

Connected.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 84


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

SQL> GRANT UNLIMITED TABLESPACE TO u3;

Grant succeeded.

SQL> disconnect;

Disconnected from Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production

Version 21.3.0.0.0

SQL> connect u3/mypwd;

Connected.

SQL> insert into a values(1);

1 row created.

SQL> select * from a;

ID
----------
1

SQL> commit;

Commit complete.

SQL> disconnect;

Disconnected from Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production

Version 21.3.0.0.0

SQL> disconnect;

Disconnected from Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 85


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Version 21.3.0.0.0

SQL> connect system/gpt109;

Connected.

SQL> revoke create table from u3;

Revoke succeeded.

SQL> connect u3/mypwd;

Connected.

SQL> create table b(id number(10));

create table b(id number(10))

ERROR at line 1:

ORA-01031: insufficient privileges

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 86


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 12A: Create and Execute Stored Procedure

SQL> CREATE TABLE TEACHER (ID NUMBER (10) PRIMARY KEY, NAME VARCHAR (20),
ADDRESS VARCHAR (30));

Table created.

SQL> CREATE OR REPLACE PROCEDURE INSERTTEACHER (ID IN NUMBER, NAME IN


VARCHAR, ADDRESS IN VARCHAR)

2 IS

3 BEGIN

4 INSERT INTO TEACHER VALUES(ID,NAME,ADDRESS);

5 END;

6 /

Procedure created.

SQL> BEGIN

2 INSERTTEACHER(1,'SAVITHA','GULBARGA');

3 DBMS_OUTPUT.PUT_LINE('TEACHER RECORD INSERTED');

4 END;

5 /

TEACHER RECORD INSERTED

PL/SQL procedure successfully completed.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 87


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

SQL> SELECT * FROM TEACHER;

ID NAME ADDRESS

---------- -------------------- ------------------------------

1 SAVITHA GULBARGA

SQL> EXEC INSERTTEACHER (2,'SHANTHA',' GULBARGA ');

PL/SQL procedure successfully completed.

SQL> SELECT * FROM TEACHER;

ID NAME ADDRESS

---------- -------------------- ------------------------------

1 SAVITHA GULBARGA

2 SHANTHA GULBARGA

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 88


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 12B: Create and Execute Functions

SQL> CREATE OR REPLACE FUNCTION ADDITION (N1 IN NUMBER, N2 IN NUMBER)

2 RETURN NUMBER

3 IS

4 N3 NUMBER(10);

5 BEGIN

6 N3:=N1+N2;

7 RETURN N3;

8 END;

9 /

Function created.

CALL-1 TO FUNCTION ADDITION

SQL> DECLARE

2 N3 NUMBER(10);

3 BEGIN

4 N3:=ADDITION(11,22);

5 DBMS_OUTPUT.PUT_LINE('ADDITION RESULT IS '||N3);

6 END;

7 /

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 89


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

ADDITION RESULT IS 33

PL/SQL procedure successfully completed.

CALL-2 TO FUNCTION ADDITION

SQL> DECLARE

2 A NUMBER;

3 B NUMBER;

4 C NUMBER;

5 BEGIN

6 A:=&FIRSTNUMBER;

7 B:=&SECONDNUMBER;

8 DBMS_OUTPUT.PUT_LINE('ADDITION RESULT IS:'|| ADDITION(A,B));

9 END;

10 /

Enter value for firstnumber: 50

old 6: A:=&FIRSTNUMBER;

new 6: A:=50;

Enter value for secondnumber: 60

old 7: B:=&SECONDNUMBER;

new 7: B:=60;

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 90


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

ADDITION RESULT IS:110

PL/SQL procedure successfully completed.

CALL-3 TO FUNCTION ADDITION

SQL> EXEC DBMS_OUTPUT.PUT_LINE ('RESULT IS'||ADDITION(10,20));

RESULT IS30

PL/SQL procedure successfully completed.

PL/SQL function example using table

SQL> CREATE OR REPLACE FUNCTION TOTALSTUDENTS

2 RETURN NUMBER IS

3 TOTNUM NUMBER(2):=0;

4 BEGIN

5 SELECT COUNT(*) INTO TOTNUM FROM STUDENT;

6 RETURN TOTNUM;

7 END;

8 /

Function created.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 91


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

CALL-1 TO FUNCTION TOTALSTUDETNS

SQL> DECLARE

2 TOTALSTUDENT NUMBER(2);

3 BEGIN

4 S:=TOTALSTUDENTS();

5 DBMS_OUTPUT.PUT_LINE('TOTAL NUMBER OF STUDENTS:'||S);

6 END;

7 /

TOTAL NUMBER OF STUDENTS:4

PL/SQL procedure successfully completed.

SQL> SELECT * FROM STUDENT;

REGNO SNAME CO

---------- -------------------- --

109IS20001 AKASH IS

109IS20002 AYAN IS

109IS20003 NAGESH IS

109CS20001 RAGHAV CS

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 92


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 13A: Create and Execute Transaction

SQL>CREATE TABLE STUDENT (REGNO VARCHAR (10), SNAME VARCHAR (20),


COURSE VARCHAR (10));

SQL> set serverout on;

SQL> insert into student values ('109IS20001','AKASH','IS');

1 row created.

SQL> insert into student values ('109IS20002','AYAN','IS');

1 row created.

SQL> COMMIT;

Commit complete.

SQL> SELECT * FROM STUDENT;

REGNO SNAME COURSE

---------- -------------------- --

109IS20001 AKASH IS

109IS20002 AYAN IS

SQL> get p13a.sql;

1 DECLARE

2 rollno student.regno%type;

3 snm student.sname%type;

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 93


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

4 cr student.course%type;

5 BEGIN

6 rollno := '&RegisterNumer';

7 snm := '&StudentName';

8 cr := '&Course';

9 INSERT into student values(rollno,snm,cr);

10 dbms_output.put_line('One record inserted');

11 COMMIT;

12 -- adding savepoint

13 SAVEPOINT mysavepoint;

14 -- second time asking user for input

15 rollno := '&RegisterNumer';

16 snm := '&StudentName';

17 cr := '&Course';

18 INSERT into student values(rollno,snm,cr);

19 dbms_output.put_line('Second record inserted');

20 ROLLBACK TO mysavepoint;

21* END;

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 94


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

SQL> /

Enter value for registernumer: 109IS20003

old 6: rollno := '&RegisterNumer';

new 6: rollno := '109IS20003';

Enter value for studentname: NAGESH

old 7: snm := '&StudentName';

new 7: snm := 'NAGESH';

Enter value for course: IS

old 8: cr := '&Course';

new 8: cr := 'IS';

Enter value for registernumer: 109IS20004

old 15: rollno := '&RegisterNumer';

new 15: rollno := '109IS20004';

Enter value for studentname: SADIQ

old 16: snm := '&StudentName';

new 16: snm := 'SADIQ';

Enter value for course: IS

old 17: cr := '&Course';

new 17: cr := 'IS';

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 95


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

One record inserted

Second record inserted

PL/SQL procedure successfully completed.

SQL> SELECT * FROM STUDENT;

REGNO SNAME CO

---------- -------------------- --

109IS20001 AKASH IS

109IS20002 AYAN IS

109IS20003 NAGESH IS

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 96


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

Experiment 13B: Call Previously Created Stored Procedure or


Functions in Transaction

SQL> GET P13BPROCEDURE.SQL;

1 CREATE OR REPLACE PROCEDURE INSERTDATA(rollno student.regno%type,

2 snm student.sname%type, cr student.course%type)

3 IS

4 BEGIN

5 INSERT into student values(rollno,snm,cr);

6 DBMS_OUTPUT.PUT_LINE('RECORD INSERTED');

7* END;

8 /

Procedure created.

SQL> GET P13B.SQL;

1 DECLARE

2 rollno student.regno%type;

3 snm student.sname%type;

4 cr student.course%type;

5 BEGIN

6 rollno := '&RegisterNumer';

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 97


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

7 snm := '&StudentName';

8 cr := '&Course';

9 INSERTDATA(rollno,snm,cr);

10 COMMIT;

11 -- adding savepoint

12 SAVEPOINT mysavepoint;

13 -- second time asking user for input

14 rollno := '&RegisterNumer';

15 snm := '&StudentName';

16 cr := '&Course';

17 INSERTDATA(rollno,snm,cr);

18 ROLLBACK TO mysavepoint;

19* END;

20 /

Enter value for registernumer: 109CS20001

old 6: rollno := '&RegisterNumer';

new 6: rollno := '109CS20001';

Enter value for studentname: RAGHAV

old 7: snm := '&StudentName';

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 98


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

new 7: snm := 'RAGHAV';

Enter value for course: CS

old 8: cr := '&Course';

new 8: cr := 'CS';

Enter value for registernumer: 109CS20002

old 14: rollno := '&RegisterNumer';

new 14: rollno := '109CS20002';

Enter value for studentname: PRANAV

old 15: snm := '&StudentName';

new 15: snm := 'PRANAV';

Enter value for course: CS

old 16: cr := '&Course';

new 16: cr := 'CS';

RECORD INSERTED

RECORD INSERTED

PL/SQL procedure successfully completed.

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 99


Database System Concepts and PL/SQL-20IS34P & 20CS34P 2021-22

SQL> SELECT * FROM STUDENT;

REGNO SNAME COURSE

---------- -------------------- --

109IS20001 AKASH IS

109IS20002 AYAN IS

109IS20003 NAGESH IS

109CS20001 RAGHAV CS

Dept. of CS/IS, Govt. Polytechnic (109), Kalaburagi Page 100

You might also like