Dbms Soft
Dbms Soft
SYSTEM LABORATORY
Course Objectives:
1. Analyze the problem and identify the Entities and Relationships, keys for given
database.
2. Design, develop and query a database.
3. Able to construct queries and maintain a simple database using MySQL.
4. Normalization of data present in database tables.
5. Develop triggers programs using PL/SQL.
List of Experiments:
1. Designing the Database through Identifying Entities, Relationship Attributes.
MySQL
1. Queries to facilitate acquaintance of Built-In Functions, String Functions, Numeric
Functions,
2. Queries to facilitate acquaintance of Date Functions and Conversion Functions.
3. Queries for Creating, Dropping, and Altering Tables
4. Queries using operators in SQL
5. Queries to Retrieve and Change Data: Select, Insert, Delete, and Update
6. Queries using Group By, Order By, and Having Clauses
7. Queries on Controlling Data: Commit, Rollback, and Save point
8. Queries for creating Views, and Constraints
9. Queries on Joins ( Outer and Inner joins)
10. Queries on Correlated Sub-Queries
PL/SQL
1. Write a PL/SQL Code using Basic Variable, Anchored Declarations, and Usage of
Assignment Operation
2. Write a PL/SQL block using SQL and Control Structures in PL/SQL
3. Write a PL/SQL Code using Cursors, Exceptions and Composite Data Types
4. Write a PL/SQL Code using Procedures, Functions, and Packages FORMS
Course Outcomes:
After completing this course the student must demonstrate the knowledge and ability to:
ENTITY ATTRIBUTES
Login Loginid,roleid,username,password
User Userid,email,username,mobile,address
Customer cust_id,cust_name,cust_pass,cus_mobile,hotel_id
Booking Book_id,Book_type,Book_desc,hotel_id
Hotel Hotel_id,Hotel_name,Hotel_rent,Hotel_type
Payments pay_date,pay_amt,pay_id,pay_mode,cus_id
Roles role_id,role_name,role_desc
role_ID
username
password
user_ID
Login_ID
Login
role_ID
username
User Has Roles
role_name
email
mobile
role_desc
address
Manage
Cus_name
Hotel_type pay_id
Cus_ID
pay_amt
Hotel_name Payments
Customer Hotel
pay_date
Cus_email
Hotel_id Hotel_rent
Cus_pass
pay_mode
cus_id
hotel_id Has
Booking
hotel_id
Book_ID
Book_desc
Book_type
LOGIN
ROLES
USER
\
]CUSTOMER
HOTEL
BOOKING
PAYMENTS
ATTRIBUTES ENTITIES
Bank Name,bcode,address
Branch branch_id,name,address,bcode
Account acc_no,acc_type,balance,customer_id
Customer customer_id,name,phoneno,address,bcode
Loan loan_id,loanamt,loantype,branchid
Name bcode Branchid
bcode Name
Bank has Branch
Adress
Address
AccountType
Loantype Loan Account
branchid Balance
Amount
Availed by Hold by
customer_id
f
Customer
bcode
Address
CustomerId
PhoneNo
Name
BANK
BRANCH
LOAN
CUSTOMER
ACCOUNT
ENTITIES ATTRIBUTES
College Name,locatio,established_year
Buildings reg_id,floors,model
Rooms Model,roomno,size
Labs Labname,eqipment,location
Library Bookslist,inoutbook,registerbook
Classroom Year,cno,regid
c_id b_id
established_year c_id
roomno
College b_id
Contains Buildings Contains Rooms
roomsize
cname
location Floors
fname
f_id
Faculty
subject
fmobile
Used for
labname
rack_no
Roomno
branch
fid bid
bid
College
Buildings
Rooms
Faculty
Labs
Library
Classrooms
ENTITIES ATTRIBUTES
Customer customer_id,customer_name,customer_phno
Product Productid,customer_id,product_name,product_rating
Customer_id Customer_ph Product_id Customer_id
Product_name
Customer_name
Product_rating
Customer
Product
ENTITIES ATTRIBUTES
Records ad_date,dis_date,recordid,doctorid
Patient Pid,pname,gender,phno
Doctor Doctorid,dname,speciality
Nurse Nursename,gender
Room room_id,room_no,type
Medicine med_id,price,quantity,name
Bill Billid,medical_charges,pid
Payment Paymentid,status,type,name,paydate
doctorid
ad_date
dis_date recordid
doctor_id Records p_id
has has gender
speciality
Patient
Doctor treats p_name
phno
dname
members Given
medid
type
roomno roomid
billid
Bill issued
type
pay_date
RECORDS
DOCTOR
PATIENT
BILL
ROOM
PAYMENTS
AIM:To implementing queries for creating ,dropping and altering a table using DDL
language
1.Creating a table:
The MySQL CREATE TABLE command is used to create a new table into the database.
A table creation command requires three things:
i.Name of the table
ii.Names of fields
iii.Definitions for each field
Example:
1.create table cus_tbl( cus_id integer not null auto_increment, cus_firstname varchar(100)
not null, cus_surname varchar(100) not null, primary key(cus_id) );
-->describe cus_tbl;
output:
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| cus_id | int | NO | PRI | NULL | auto_increment |
| cus_firstname | varchar(100) | NO | | NULL | |
| cus_surname | varchar(100) | NO | | NULL | |
+---------------+--------------+------+-----+---------+---------------
2.Alter commands:
MySQL ALTER statement is used when you want to change the name of your table or
any table field. It is also used to add or delete an existing column in a table.
The ALTER statement is always used with "ADD", "DROP" ,”RENAME”and "MODIFY"
commands according to the situation.
i.Add column:
Parameters:
table_name: It specifies the name of the table that you want to modify.
new_column_name: It specifies the name of the new column that you want to add to the
table.
column_definition: It specifies the data type and definition of the column (NULL or NOT
NULL, etc).
FIRST | AFTER column_name: It is optional. It tells MySQL where in the table to create the
column. If this parameter is not specified, the new column will be added to the end of the
table.
Example:
mysql>alter table cus_tbl add s_no integer first;
-->describe cus_tbl;
Output:
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| s_no | int | YES | | NULL | |
| cus_id | int | NO | PRI | NULL | auto_increment |
| cus_firstname | varchar(100) | NO | | NULL | |
| cus_surname | varchar(100) | NO | | NULL | |
+------------------------+----------------------------------------------
Output:
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| s_no | int | YES | | NULL | |
| cus_id | int | NO | PRI | NULL | auto_increment |
| cus_phno | varchar(20) | YES | | NULL | |
| cus_firstname | varchar(100) | NO | | NULL | |
| cus_Address | varchar(20) | YES | | NULL | |
| cus_surname | varchar(100) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
SYNTAX:
1. ALTER TABLE table_name ADD new_column_name column_definition
2. [ FIRST | AFTER column_name ],
3. ADD new_column_name column_definition [ FIRST | AFTER column_name ],
4. ...
5. ;
Example:
mysql> alter table cus_tbl modify cus_firstname char(20)null;
Output:
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| s_no | int | YES | | NULL | |
| cus_id | int | NO | PRI | NULL | auto_increment |
| cus_phno | varchar(20) | YES | | NULL | |
| cus_firstname | char(20) | YES | | NULL | |
| cus_Address | varchar(20) | YES | | NULL | |
| cus_surname | varchar(100) | NO | | NULL | |
+---------------------------------------+--------------------------------
iii.drop---deleting a column
Syntax:
Example:
mysql> alter table cus_tbl drop column cus_address;
4.truncate
truncate—deleting all the records not the structure
The TRUNCATE TABLE statement is used when you want to delete the complete data from
a table without removing the table structure.
Example:
mysql> truncate table customer_tbl;
mysql> describe customer_tbl;
Output:
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| s_no | int | YES | | NULL | |
| cus_id | int | NO | PRI | NULL | auto_increment |
| cus_phno | varchar(20) | YES | | NULL | |
| cus_firstname| char(20) |YES | | NULL | |
| cus_name | varchar(100) | NO | | NULL | |
---------------------------------------------------------------------------
5.drop:
MYSQL DROP table statement removes the complete data with structure.
Syntax: DROP TABLE table_name;
Example:
mysql> drop customer_table;
mysql> show tables;
Output:
Empty set (0.00 sec)
PROGRAM NO-2
2.DML LANGUAGE COMMANDS
AIM:To implementing the queries for inserting,updating,deleting and retrieving the data
from a table using DML language.
DESCRIPTION:
DML is means Data Manipulation Language which deals with data manipulation, and
includes most common SQL statements such SELECT, INSERT, UPDATE, DELETE etc,
and it is used to store, modify, retrieve,delete and update data in database.
1)INSERT – insert data into a table.
2)UPDATE – updates existing data within a table.
3)DELETE – Delete all records from a database table.
4)SELECT – retrieve data from the database.
1.insert:
MySQL INSERT statement is used to insert data in MySQL table within the database. We
can insert single or multiple records using a single query in MySQL.
Syntax:
i.The SQL INSERT INTO command is used to insert data in MySQL table.
Following is a generic syntax:
1. INSERT INTO table_name ( field1, field2,...fieldN )
2. VALUES ( value1, value2,...valueN );
Field name is optional. If you want to specify partial values, field name is mandatory.
ii.Syntax for all fields:
1. INSERT INTO table_name VALUES ( value1, value2,...valueN );
Example:
//i.insert command for all fileds
mysql> insert into student values(1,'deepika',89,'6300416456');
//ii.insert command for partial fields
mysql> insert into student(sno,sname)values(2,'sai');
//iii.insert command for multiple records
mysql> insert into student values(3,'ganesh',90,'7658962173'),(4,'vasantha',78,'7567269562'),
(5,'gnapika',98,'7387897432');
mysql> select * from student;
Output:
+------+----------+-------+------------+
| sno | sname | marks | s_phno |
+------+----------+-------+------------+
| 1 | deepika | 89 | 6300416456 |
| 2 | sai | NULL | NULL |
| 3 | ganesh | 90 | 7658962173 |
| 4 | vasantha | 78 | 7567269562 |
| 5 | gnapika | 98 | 7387897432 |
+------+----------+-------+------------
2.UPDATE:
MySQL UPDATE statement is used to update data of the MySQL table within the database.
It is used when you need to modify the table.
Example:
mysql> update student set marks=94,s_phno='9789482978' where sno=2;
mysql> select * from student;
Output:
+------+----------+-------+------------+
| sno | sname | marks | s_phno |
+------+----------+-------+------------+
| 1 | deepika | 89 | 6300416456 |
| 2 | sai | 94 | 9789482978 |
| 3 | ganesh | 90 | 7658962173 |
| 4 | vasantha | 78 | 7567269562 |
| 5 | gnapika | 98 | 7387897432 |
+------+----------+-------+------------
3.DELETE:
MySQL DELETE statement is used to delete data from the MySQL table within the
database. By using delete statement, we can delete records on the basis of conditions.
4.SELECT:
The MySQL SELECT statement is used to fetch data from the one or more tables in
MySQL. We can retrieverecords of all fields or specified fields.
Example:
mysql> select sname,marks from student where marks>85;
Output:
+---------+-------+
| sname | marks |
+---------+-------+
| deepika | 89 |
| sai | 94 |
| ganesh | 90 |
+---------+-------
PROGRAM NO-3
3.TCL LANGUAGE COMMANDS
AIM:To implementing the queries for commit,savepoint and rollback the data of a table
using TCL language.
1.commit:
It commits the current transaction means making changes permanently.
Syntax:commit;
Example:
mysql> select * from student;
+------+----------+-------+------------+
| sno | sname | marks | s_phno |
+------+----------+-------+------------+
| 1 | deepika | 89 | 6300416456 |
| 2 | sai | 94 | 9789482978 |
| 3 | ganesh | 90 | 7658962173 |
| 4 | vasantha | 78 | 7567269562 |
+------+----------+-------+------------+
mysql> commit;
2.savepoint:
It is used to store data temporarily.It is an restoring point which lies in between the
commit and rollback.
Syntax:savepoint savepoint_name;
Example:
mysql> start transaction;
mysql> savepoint 2rows;
mysql> insert into student values(5,'gnapika',56,'7789347893');
mysql> select * from student;
Output:
+------+----------+-------+------------+
| sno | sname | marks | s_phno |
+------+----------+-------+------------+
| 1 | deepika | 89 | 6300416456 |
| 2 | sai | 94 | 9789482978 |
| 3 | ganesh | 90 | 7658962173 |
| 4 | vasantha | 78 | 7567269562 |
| 5 | gnapika | 56 | 7789347893 |
+------+----------+-------+------------+
mysql> rollback to 2rows;
mysql> select * from student;
Output:
+------+----------+-------+------------+
| sno | sname | marks | s_phno |
+------+----------+-------+------------+
| 1 | deepika | 89 | 6300416456 |
| 2 | sai | 94 | 9789482978 |
| 3 | ganesh | 90 | 7658962173 |
| 4 | vasantha | 78 | 7567269562 |
+------+----------+-------+------------
3.rollback:
The rollback statement restores the database changes to the starting of transaction on i.e
previous commit.
Syntax:rollback;
Example:
mysql> start transaction;
mysql> select * from student;
+------+----------+-------+------------+
| sno | sname | marks | s_phno |
+------+----------+-------+------------+
| 1 | deepika | 89 | 6300416456 |
| 2 | sai | 94 | 9789482978 |
| 3 | ganesh | 90 | 7658962173 |
| 4 | vasantha | 78 | 7567269562 |
--------------------------------------------
DESCRIPTION:DCL full form Data Control Language.It is used to provide control to the
data in a database.
There are two types in DCL commands.
1.grant
2.revoke
1.grant:
It is used to provide access or privilages or permissions on the database objects
to the user.
Example:
mysql> grant insert,select on deepika.student to deepu@localhost;
//performing insert,select....
Database changed
mysql> insert into student values(4,'gnapika',76,'9378326432');
mysql> select * from student;
Output:
+------+----------+-------+------------+
| sno | sname | marks | s_phno |
+------+----------+-------+------------+
| 1 | deepika | 89 | 6300416456 |
| 2 | sai | 94 | 9789482978 |
| 3 | ganesh | 90 | 7658962173 |
| 4 | vasantha | 78 | 7567269562 |
| 4 | gnapika | 76 | 9378326432 |
+------+----------+-------+------------
2.Revoke:
To take back privileges from a specific user, use the REVOKE command. It works similar to
the GRANT command.
Output:
ERROR 1142 (42000): INSERT command denied to user 'deepu'@'localhost' for table
'student'
-------------------------------------------------------------------------------------------------------
PROGRAM NO-5
5.OPERATORS
DESCRIPTION:
To retrieve data from the table based on some rules, then use conditional operators.
Conditional operators return true or false esteem based on the instance of the variables.
1.comparison operators--- =,<,>,<=,>=,!=
2.arithmetic operators---+,-,*,/,%
3.logical operators---AND,OR,NOT
4.special operators—between,isnull,is notnull,like,in
5.relational set operators—union,unionall,intersection,minus
EXAMPLE:
i.mysql> select *from employee where empsal=78000;
Output:
+-------+---------+--------+--------+
| empid | empname | empsal | deptno |
+-------+---------+--------+--------+
| 4 | vasuu | 78000 | 2|
+-------+---------+--------+--------
ii.mysql> select *from employee where empsal<60000;
+-------+---------+--------+--------
| empid | empname | empsal | deptno |
+-------+---------+--------+--------+
| 1 | deepika | 27899 | 1|
| 2 | sai | 56576 | 2|
| 5 | gnapika | 54565 | 3|
+-------+---------+--------+--------
Examples:
mysql> select empsal+1000 from employee;
+-------------+
| empsal+1000 |
+-------------+
| 28899 |
| 57576 |
| 61000 |
| 79000 |
| 55565 |
+-------------+
+----------+
| empsal%2 |
+----------+
| 1|
| 0|
| 0|
| 0|
| 1|
+----------
3.LOGICAL OPERATORS{AND,OR,NOT}
The logical operators are AND, OR, NOT, are used in between two or more conditional
expression.
i.AND
To specify multiple conditions in the column of MySQL table use Logical AND operator. i.e.
MySQL Logical And operator compares two results and returns true if both of the results are
true.
Example:
mysql> select * from employee where deptno=1 or deptno=2 or deptno=3;
Output:
+-------+---------+--------+--------+
| empid | empname | empsal | deptno |
+-------+---------+--------+--------+
| 1 | deepika | 27899 | 1|
| 2 | sai | 56576 | 2|
| 3 | ganesh | 60000 | 3|
| 4 | vasuu | 78000 | 2|
| 5 | gnapika | 54565 | 3|
+-------+---------+--------+--------
iii.NOT EQUAL
MySQL NOT EQUAL operator is used to return a set of rows from the table, after making
sure that two expressions placed on either sides of the table are NOT NULL.
4.special operators(BETWEEN,ISNULL,ISNOTNULL,LIKE,IN,ALL,ANY)
i.between
MySQL BETWEEN AND operator checks whether a value is present between minimum and
maximum range of an expression.
ii.isnull
MySQL IS NULL operator will review whether a esteem is NULL.
iii.isnotnull
MYSQL IS NOT NULL operator will review whether the esteem IS Not Null or not.
iv.like
The Like operation is used with wildcard characters [%, -]
To find out the patterns within the string attributes % it means any and all following or
preceding characters.
_ (Under score) it means any single characters
Example:
mysql> select * from employee where empname like '___p%';
Output:
+-------+---------+--------+--------+
| empid | empname | empsal | deptno |
+-------+---------+--------+--------+
| 1 | deepika | 27899 | 1|
| 5 | gnapika | 54565 | 3|
+-------+---------+--------+--------
v.in
This operator is used to check whether all attribute values match any value with in a value
list. The values in the list are all of same data types
Syntax:select * from table_name where column_name in(value1,value2,....);
Example:
mysql> select * from employee where empsal in(60000,78000);
Output:
+-------+---------+--------+--------+
| empid | empname | empsal | deptno |
+-------+---------+--------+--------+
| 3 | ganesh | 60000 | 3|
| 4 | vasuu | 78000 | 2|
----------------------------------------------
vi.all
The ALL operator:
• returns a boolean value as a result
• returns TRUE if ALL of the subquery values meet the condition
• is used with SELECT, WHERE and HAVING statements
ALL means that the condition will be true only if the operation is true for all values in the range.
OUTPUT:
+-------+---------+--------+--------+
| empno | empname | empsal | deptno |
+-------+---------+--------+--------+
| 1 | deepika | 27899 | 1|
| 2 | sai | 56576 | 2|
| 3 | ganesh | 60000 | 3|
| 4 | vasuu | 78000 | 2|
| 5 | gnapika | 54565 | 3|
| 6 | dinesh | NULL | 4|
+-------+---------+--------+--------+
6 rows in set (0.00 sec)
2)mysql> select * from employee where 5<all(select deptno from employee);//false
OUTPUT:
Empty set (0.00 sec)
V.ANY
The ANY operator:
SQL SET Operators are behaving same like mathematical sets, these sql set operators are
classified into four types, which is given below;
❖Union
❖Union all
❖Minus
❖Intersect
DESCRIPTION:
Clause is defined as a set of rules, that makes to understand the concepts of MySQL
command in Database.
MySQL Clauses are very similar to SQL clause, except some functional operations.
Types of clauses are
1.Distinct clause.
2.Where clause.
3.Group By clause.
4.Having clause.
5.Order by clause .
1.Distinct clause:
Used to eliminate the duplicate values in a specific column
Example:
mysql> select * from employee;
+-------+---------+--------+--------+
| empid | empname | empsal | deptno |
+-------+---------+--------+--------+
| 1 | deepika | 27899 | 1|
| 2 | sai | 56576 | 2|
| 3 | ganesh | 60000 | 3|
| 4 | vasuu | 78000 | 2|
| 5 | gnapika | 54565 | 3|
| 6 | dinesh | NULL | 3|
+-------+---------+--------+--------
Output:
mysql> select distinct deptno from employee;
+--------+
| deptno |
+--------+
| 1|
| 2|
| 3|
+--------
2.where clause:
Where clause will specify the condition for the result set in the table.
Syntax:select * from table_name where column_name =’value’;
Example:
mysql> select empname from employee where empsal>55000;
Output:
+---------+
| empname |
+---------+
| sai |
| ganesh |
| vasuu |
+---------
3.Groupby clause:
To divide the data of a table into groups based on single column use Group By clause.
Syntax:select columnname,condition from table_name group by column_name;
Example:
mysql> select deptno,max(empsal) from employee group by deptno;
Output:
+--------+-------------+
| deptno | max(empsal) |
+--------+-------------+
| 1 | 27899 |
| 2 | 78000 |
| 3 | 60000 |
+--------+-------------
4.Having clause:
To specify a condition with group by clause use Having clause.
Syntax:select columnname,condition from table_name group by column_name having
condition;
Example:
mysql> select deptno,max(empsal) from employee group by deptno having
max(empsal)>55000;
Output:
+--------+-------------+
| deptno | max(empsal) |
+--------+-------------+
| 2 | 78000 |
| 3 | 60000 |
+--------+-------------
5.order by clause:
To arrange data of a table either in ascending order or descending order based on single
column use Orderby clause i.e., Select * from the table name will show all the rows from the
table and order by clause is used to arrange the specific column in ascending or descending
order.
Syntax:select * from table_name order by column_name;
Example:
mysql> select * from employee order by empsal;
Output:
+-------+---------+--------+--------+
| empid | empname | empsal | deptno |
+-------+---------+--------+--------+
| 6 | dinesh | NULL | 3|
| 1 | deepika | 27899 | 1|
| 5 | gnapika | 54565 | 3|
| 2 | sai | 56576 | 2|
| 3 | ganesh | 60000 | 3|
| 4 | vasuu | 78000 | 2|
+-------+---------+--------+--------+
//descending order
mysql> select * from employee order by empsal desc;
output:
+-------+---------+--------+--------+
| empid | empname | empsal | deptno |
+-------+---------+--------+--------+
| 4 | vasuu | 78000 | 2|
| 3 | ganesh | 60000 | 3|
| 2 | sai | 56576 | 2|
| 5 | gnapika | 54565 | 3|
| 1 | deepika | 27899 | 1|
| 6 | dinesh | NULL | 3|
+-------+---------+--------+--------
PROGRAM NO-7
7.JOINS
DESCRIPTION:
Join is used to retrieve the data from more than one table in a single query
There are following joins are available in SQL:
1.Equi join.
2.Cartesian join[cross join].
3.Non-equi join.
4.Outer join.
5.Self join.
1.equi join:
✓ It is used to retrieve the data from more than 1 table based on “equality” condition.
✓ Tables must have common column between them to apply this join.
✓ If N tables are joined N-1 conditions are applied.
Syntax:select * from table1_name,table2_name where condition;
Example:
Table1:
mysql> select * from dept;
+--------+-----------+
| deptno | deptname |
+--------+-----------+
| 1 | software |
| 2 | hardware |
| 3 | developer |
| 4 | tester |
+--------+-----------
Table2:
mysql> select * from employee;
+-------+---------+--------+--------+
| empid | empname | empsal | deptno |
+-------+---------+--------+--------+
| 1 | deepika | 27899 | 1|
| 2 | sai | 56576 | 2|
| 3 | ganesh | 60000 | 3|
| 4 | vasuu | 78000 | 2|
| 5 | gnapika | 54565 | 3|
| 6 | dinesh | NULL | 3|
+-------+---------+--------+--------+
Example:
mysql> select empname,deptname from employee,dept where
employee.deptno=dept.deptno;
Output:
+---------+-----------+
| empname | deptname |
+---------+-----------+
| deepika | software |
| sai | hardware |
| ganesh | developer |
| vasuu | hardware |
| gnapika | developer |
| dinesh | developer |
------------------------
✓ It is used to retrieve the data from the multiple tables without any conditions.
✓ It is used to retrieve data analysis report.
✓ No need to have common column between tables to apply this type of join
3.Non Equi-join:
✓ It is used to retrieve the data from multiple tables based on other condition but not equal.
✓ Non Equi Join uses all comparison operators except the equal (=) operator like !=, >=, <=,
<, >.
4.Outer join:
This join returns all the rows from one table and only those rows from second table which
meets the condition.
3types:
a)left outer join
b)right outer join
c)full outer join
✓ Returns all the rows from right table(ie, second table) and rows that meet the condition
from first table.
✓ All rows from right table and only matching rows from the left table.
5)self join:
DESCRIPTION:
➔ All SQL functions are inbuilt functions.
➔ These are classified as two types:
1. Single row function
2. Multiple row function
i.conversion function:
ii.string functions:
d)substr():This function is used to return a porttion of string given start point to end point.
Syntax:select substr(string,startpoint);
Example:
mysql> select substr("deepika",5);
Output:
+---------------------+
| substr("deepika",5) |
+---------------------+
| ika |
-------------------
e)instr():This function is used to return a numeric position of a character (or) string.
Syntax:select instr(string,character);
Example:
mysql> select instr("rgukt","u");
Output:
+--------------------+
| instr("rgukt","u") |
+--------------------+
| 3|
---------------------
f)lpad():This function is used to insert the symbol with the actual length of the string from
left to right.
Syntax:select lpad(string,length,symbol);
Example:
mysql> select lpad("rgukt",10,"*");
Output:
+----------------------+
| lpad("rgukt",10,"*") |
+----------------------+
| *****rgukt |
+----------------------
g)rpad():This function is used to insert the symbol with the actual length of the string from
right to left.
Syntax:select rpad(string,length,symbol);
Example:
mysql> select rpad("rgukt",10,"*");
Output:
+----------------------+
| rpad("rgukt",10,"*") |
+----------------------+
| rgukt***** |
--------------------------
h)ltrim():This function is used to remove leading spaces in a given string from left side.
Syntax:select ltrim(“ string” );
Example:
mysql> select ltrim("world ");
Output:
+-----------------------+
| ltrim("world ") |
+-----------------------+
| world |
+-----------------------
iii.numeric functions
1.Truncate():Remove decimal part.
Syntax:select trunc(number having decimalpart);
Example:
mysql> select round (27.6);
Output:
+--------------+
| round (27.6) |
+--------------+
| 28 |
+--------------+
Example:
mysql> select count(*) from employee;
Output:
+----------+
| count(*) |
+----------+
| 7|
+----------
d)min():This function is used to get the minimum value from a column.
Syntax:select min(column_name)from table_name;
Example:
mysql> select min(empsal) from employee;
Output:
+-------------+
| min(empsal) |
+-------------+
| 27899 |
+-------------+
Output:
+---------+
| empname |
+---------+
| deepika |
+---------+
| 2|
+----------
g)last():This function is used to get the lastvalue of a selected column.
Example:
mysql> select empname from employee order by empname desc limit 1;
Output:
+---------+
| empname |
+---------+
| vasuu |
+---------
PROGRAM NO-9
9.VIEWS
AIM:To implement Queries of creating,altering,inserting and deleting the views.
DESCRIPTION:
MySQL Views are the database objects that can be created on a table to improve the
performance of MySQL Server. A view has unique Constraints look on data from one or
more tables. It can organize data in unique order, focus or hide some data.
MySQL Views comprises of a stored query accessible as a fundamental table composed of
the outcome set.
Beside standard tables a perspective does not shaped a part of the physical schema. It is a
virtual table, dynamic in the database.
View is a stored query, and it can be attributed like a table.
1)create a view:
Syntax for creat view:
create view view_name as select column 1 , column2,...... from table name where condition;
Example:
mysql> create view deptview as select * from employee where deptno=3;
mysql> select * from deptview;
Output:
+-------+---------+--------+--------+
| empid | empname | empsal | deptno |
+-------+---------+--------+--------+
| 3 | ganesh | 60000 | 3|
| 5 | gnapika | 54565 | 3|
| 6 | dinesh | NULL | 3|
+-------+---------+--------+--------
Insert rows in a view:
Syntax :
Insert into view_name(column 1,column 2) values value1(name),value2(age);
Example:
mysql>insert into deptview(empid,empname,empsal,deptno) values (7,”sruthi”,89000,3);
Output:
+-------+---------+--------+--------+
| empid | empname | empsal | deptno |
+-------+---------+--------+--------+
| 3 | ganesh | 60000 | 3|
| 5 | gnapika | 54565 | 3|
| 6 | dinesh | NULL | 3|
| 7| sruthi | 89000 | 3|
+-------+---------+--------+--------
6)truncate view:
Syntax: truncate view view_name;
Example:
mysql>truncate view deptview;
mysql>select * from deptview;
Output:
+-------+---------+--------+--------+
| empid | empname | empsal | deptno |
+-------+---------+--------+--------+
| - | - | - | -|
| -| - | - | -|
+-------+---------+--------+--------
7)drop a view:
Syntax:drop view view_name;
Example:
mysql> drop view deptview;
mysql> select * from deptview;
Output:
ERROR 1146 (42S02): Table 'deepika.deptview' doesn't exist
PROGRAM NO-10
10.SUB QUERIES
DESCRIPTION:
A sub query is a query inside another query. A sub query or inner query is used to generate
the required information used as input for outer query. A sub query has characteristics. ·
A sub query is normally expressed inside parenthesis · The inner query or sub query is
executed first.
Example:
mysql> select empname,empsal from employee where empsal>(select avg(empsal)from
employee);
Output:
+---------+--------+
| empname | empsal |
+---------+--------+
| sai | 56576 |
| ganesh | 60000 |
| vasuu | 78000 |
| gnapika | 54565 |
+---------+--------
2)In sub queries:
When you want to compare single attributewith the list of value we can use in sub queries
inside.
Example:
mysql> select empname,empsal from employee where empsal in (select max(empsal)from
employee group by deptno);
Output:
+---------+--------+
| empname | empsal |
+---------+--------+
| deepika | 27899 |
| ganesh | 60000 |
| vasuu | 78000 |
| ramesh | 45000 |
+---------+--------
3)Having subqueries:
An sub queries used in the where clause, we can use a sub queries with a havingclause,
Having clause is used to restrict the rows by using the condition in the group by query.
Example:
mysql> select deptno from employee group by deptno having avg(empsal)>(select
avg(empsal) from employee);
Output:
+--------+
| deptno |
+--------+
| 2|
| 3|
+--------
4)From subqueries:
As you already know, in the from clause, this specifies table name from which the
data can be drawn; because the output of the select statement is another table.
Example:
mysql> select empname,empsal from (select *from employee)alias;
Output:
+---------+--------+
| empname | empsal |
+---------+--------+
| deepika | 27899 |
| sai | 56576 |
| ganesh | 60000 |
| vasuu | 78000 |
| gnapika | 54565 |
| dinesh | NULL |
| ramesh | 45000 |
+---------+--------
***************************THE END*******************************
DATABASES
AIM:To create a database of Hotel management system with 7 entities
DESCRIPTION:
Creating a database with related tables in it using SQL commands and joining
all the tables using join operations.
CREATING DATABASE:
CREATION OF TABLES:
Login Table:
Roles Table:
Customer Table:
hotel Table:
Booking Table:
Login Table:
Roles Table:
Customer Table:
Booking Table:
Payments Table:
Hotel_name book_type
Vihara Direct
Novatel Indirect
Vasista Direct
Prakruthi Indirect
Anand Direct
RESULT:
Creation of Hotel management database created successfully.
AIM: To Create database of Bank Management System with 10 Related
Tables.
Program:
location customername
VIJAYWADA Pavan
CHENNAI laxman
ONGOLE ganesh
BANGLORE ravi
BANGLORE Bharath
MUMBAI Arjum
KOLKATTA srinu
TANGUTUR vasu
PUNJAB NULL
HYD sai
HYD Suresh
VIZAG NULL
RESULT:
Bank Management database is created successfully.
AIM:To create data base for College with 6 Relation Tables.
CREATING DATABASE:
sql>create database College;
sql>use College;
CREATING TABLES:
COLLEGE TABLE:
sql>create table college(
c_id integer,primary key(c_id),cname char(30),location char(30),
estd_year integer);
BUILDINGS TABLE:
sql>create table buildings(
b_id integer,primary key(b_id),c_id integer,foreign key(c_id) references
college(c_id),floors integer);
ROOMS TABLE:
sql>create table rooms(
roomno integer,b_id integer,primary key(b_id),roomsize varchar(10));
FACULTY TABLE:
sql>create table faculty(
f_id integer,primary key(f_id),fname varchar(30),fmobile bigint,
subject char(30));
LABS TABLE:
sql>create table labs(
labname varchar(30),f_id integer,foreign key(f_id)references
faculty(f_id),branch char(10));
LIBRARY TABLE:
sql>create table library(
rack_no integer primary key,branch char(10),no_of_books integer,
b_id integer,foreign key(b_id) references buildings(b_id));
CLASSROOMS TABLE:
sql>create table classrooms(
room_no varchar(10)primary key,branch varchar(10),section_no varchar(5),
b_id integer,foreign key(b_id)references buildings(b_id));
sql>show tables;
Tables_in_College
College
buildings
rooms
faculty
labs
library
classrooms
SQL> select faculty.fname as Facultyname,labs.labname Lab from faculty inner join lab
on faculty.f_id=labs.f_id;
Facultyname Lab
02 physicslab
03 Chemlab
06 javalab
07 c++lab
CREATING DATABASE:
CREATING TABLES:
HOSPITAL TABLE:
sql>create table Hospital(
Hospital_ID int primary key,Hospital_name char(30),City
char(20));
DOCTOR TABLE:
sql>create table Doctor(
Doctor_ID int ,D_name char(30),Hospital_ID int,
Qualification char(20),Salary int,
foreign key(Hospital_ID) references
Hospital(Hospital_ID));
PATIENT TABLE:
sql>create table Patient(
Patient_ID int primary key,Patient_name char(20),Age int,
Hospital_ID int,foreign key(Hospital_ID) references
Hospital(Hospital_ID));
PHARMACY TABLE:
sql>create table Pharmacy(
P_ID int primary key,P_name char(30),Hospital_ID int,foreign
key(Hospital_ID) references Hospital(Hospital_ID));
REPORTS TABLE:
sql>create table Reports(
Report_ID int primary key,Problem char(20),
Date_of_admission varchar(10),Patient_ID int,foreign
key(Patient_ID) references Patient(Patient_ID));
sql>show tables;
Tables_in_Hospital
Doctor
Hospital
Patient
Pharmacy
Reports
sql>select
Hospital.Hospital_name,Doctor.Hospital_ID,Doctor.Qualification,
Patient.Patient_name,Pharmacy.P_name,Reports.Problem from Hospital left join Doctor on
Hospital.Hospital_ID=Doctor.Hospital_ID left join Patient on
Patient.Hospital_ID=Hospital.Hospital_ID left join Pharmacy on
Pharmacy.Hospital_ID=Patient.Hospital_ID left join Reports on
Reports.Hospital_ID=Hospital.Hospital_ID;
CREATING DATABASE:
sql>create database Sports_management;
sql>use Sports_management;
CREATING TABLES:
PLAYER TABLE:
sql>create table player(
player_id int,p_name char(20),age integer,team_id int,primary
key(player_id),foreign key(team_id)references team(team_id);
MATCHES TABLE:
sql>create table matches(
match_id int,time varchar(10),date varchar(20),winner char(20),
primary key(match_id));
TEAM TABLE:
sql>create table team(
team_id int,t_name varchar(20),match_id integer,
foreign key(match_id)references matches(match_id),
primary key(team_id));
COMPETITION TABLE:
sql>creae table compitition(
team_id inr,sports_name char(20),type char(10));
SPORTS_CLUB TABLE:
sql>create table sports_club(
clubname varchar(10),location char(20),contact_no int);
STADIUM TABLE:
sql>create table stadium(
capacity varchar(10),city char(20),name char(20));
COACH TABLE:
sql> create table coach(
cid integer,name char(20),email varchar(20),nationality
char(10),player_id integer));
DISPLAYING TABLES IN DATABASE:
sql>show tables;
Tables_in_Hospital
Player
matches
team
compitition
sports_club
stadium
RESULT:
The sports management database is successfully created.
PL/SQL
PL/SQL
NOTE: For some of the programs which are based on tables, you may need to create related tables before
execution of the respective programs.
Introduction(Basic Program):
OUTPUT:
HAI WELCOME
PL/SQL PROGRAMS
SQL>
DECLARE A
NUMBER; B
NUMBER; C
NUMBER; BEGIN
A:=100; B:=200;
C:=A+B;
DBMS_OUTPUT.PUT_LINE('THE SUM OF TWO INTEGERS IS: '||C); END;
OUTPUT:
SQL>
DECLARE EMPNO NUMBER;
ENAME VARCHAR2(20); SAL
NUMBER(7,2); ANU_SAL
NUMBER(10,2); BONUS NUMBER(10,2);
BEGIN
EMPNO:=1234; ENAME:='Ravi';
SAL:=18000; ANU_SAL:=SAL*12;
BONUS:=ANU_SAL*20/100;
DBMS_OUTPUT.PUT_LINE('EMPNO: '||EMPNO);
DBMS_OUTPUT.PUT_LINE('ENAME: '||ENAME);
DBMS_OUTPUT.PUT_LINE('SAL: '||SAL);
DBMS_OUTPUT.PUT_LINE('BONUS: '||BONUS); END;
OUTPUT:
EMPNO: 1234
ENAME: ravi SAL:
18000
BONUS: 43200
Statement processed.
SQL>
DECLARE PRODNO NUMBER;
PNAME VARCHAR2(20); QUAN
NUMBER(3); PRICE NUMBER(7,2);
TOTAL NUMBER(7,2); DISCOUNT
NUMBER(7,2); NET NUMBER(7,2);
BEGIN
PRODNO:=1234;
PNAME:='Chocolates'; QUAN:=10;
PRICE:=100; TOTAL:=QUAN*PRICE;
DISCOUNT:=TOTAL*20/100;
NET:=TOTAL-DISCOUNT; DBMS_OUTPUT.PUT_LINE('PRODNO: '||
PRODNO); DBMS_OUTPUT.PUT_LINE('PNAME: '||PNAME);
DBMS_OUTPUT.PUT_LINE('QUANTITY: '||QUAN);
DBMS_OUTPUT.PUT_LINE('PRICE: '||PRICE);
DBMS_OUTPUT.PUT_LINE('TOTAL: '||TOTAL);
DBMS_OUTPUT.PUT_LINE('DISCOUNT: '||DISCOUNT);
DBMS_OUTPUT.PUT_LINE('NET BALANCE: '||NET); END;
OUTPUT:
PRODNO: 1234
SQL>
OUTPUT:
EMPNO: 1234
SAL: 8000
ANU_SAL: 96000
BONUS: 14400
Statement processed.
SQL>
DECLARE I NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('THE NUMBERS ARE'); FOR I IN
REVERSE 1..10 LOOP DBMS_OUTPUT.PUT_LINE(I);
END LOOP; END;
OUTPUT:
THE NUMBERS ARE 10
9
8
7
6
5
4
3
2
1
OUTPUT:
Note: Create a table with name DEPT and columns DEPTNO,DNAME,LOC with data inserted into
it before running the below program and write ouptut according to the data inserted.
DECLARE
CURSOR EC IS SELECT * FROM DEPT; BEGIN
FOR V_EC IN EC LOOP
DBMS_OUTPUT.PUT_LINE('DEPTNO='||V_EC.DEPTNO); DBMS_OUTPUT.PUT_LINE('DNAME='||
V_EC.DNAME); DBMS_OUTPUT.PUT_LINE('LOC='||V_EC.LOC);
END LOOP; END;
Output:
DEPTNO=101
DNAME=Ongole
LOC=Designing
DEPTNO=103
DNAME=Guntur LOC=Sales
DEPTNO=102
DNAME=Kakinada
LOC=Development
3. Write a PL/SQL Code using Cursors, Exceptions and Composite Data Types
a)Write a Program to calc bonus for all emps insert into bonus table
SQL>
DECLARE
CURSOR EC IS SELECT EMPNO,ESAL FROM EMP; V_EC EC
%ROWTYPE;
ANN_SAL NUMBER(10,2); B
BONUS1%ROWTYPE; BEGIN
OPEN EC; LOOP
FETCH EC INTO V_EC;
EXIT WHEN EC%NOTFOUND; ANN_SAL
:=V_EC.ESAL*12; B.BONUS_AMT := ANN_SAL*0.2;
INSERT INTO BONUS1(EMPNO,BONUS_AMT,ADD_AMT,ISS_DATE)
VALUES(V_EC.EMPNO,B.BONUS_AMT,1000,SYSDATE);
END LOOP; CLOSE EC;
END;
OUTPUT:
EMPNO BONUS_AMT
7698 6840
ADD_AMT ISS_DATE 1000 02/07/2023
7654 3300 1000 02/07/2023
7499 4224 1000 02/07/2023
7782 5880 1000 02/07/2023
7839 12000 1000 02/07/2023
7566 7140 1000 02/07/2023
4. Write a PL/SQL Code using Procedures, Functions, and Packages FORMS
NOTE: Run both sql1,sql2 commands in each section one after another,not at a time.
SQL1>
CREATE OR REPLACE PROCEDURE min(x IN number, y IN number, z OUT number)
IS BEGIN
IF x<y THEN
z:=x; ELSE
z:=y; END IF;
END;
SQL2>
DECLARE
a number;
b number;
c number;
BEGIN
a:=125;
b:=40;
min(a,b,c);
dbms_output.put_line(c);
END;
OUTPUT:40
Statement processed.
b)Write a program with Procedure to print Square of a number in PL/SQL.
SQL1>
SQL2>
DECLARE
a number;
BEGIN a:=10;
square(a);
dbms_output.put_line(‘Square of 10 is: ’||a);
END;
OUTPUT:
Square of 10 is: 100 Statement processed.