DBMS Lab File
DBMS Lab File
LAB FILE
ACADEMIC SESSION 2023-24
S. No. Experiments
1 Installing Oracle
4 Normalization
Creating cursor
5
Creating procedure and functions
6
TheoryConcept:Toinstallthesoftware,youmustusetheUniversalinstaller.
Implementation:
1. For this installation, you need either the DVDs or a downloaded version of the DVDs. In
thistutorial, you install from the downloaded version. From the directory where the DVD files
wereunzipped,openWindowsExploreranddouble-clickonsetup.exefromthe\db\Disk1directory.
2. TheproductyouwanttoinstallisDatabase11g.Makesuretheproductisselectedandclick
Next.
3. Youwillperformabasicinstallationwithastarterdatabase.EnterorclfortheGlobalDatabaseName
and for Database Password and Confirm Password.Then, clickNext
4. Configuration Manager allows you to associate your configuration information with
yourMetalinkaccount.Youcanchoosetoenableitonthiswindow.Then,clickNext.
5. Review the Summary window to verify what is to be installed.Then, clickInstall.
8. Yourdatabaseisnowbeingcreated.
9. When the database has been created, you can unlock the users you want to use. Click OK.
10. ClickExit.ClickYestoconfirmexit.
ExperimentNo:2
Steps:
In the diagram canvas, you can add entities by clicking on the "Entity" button in the toolbar and
then clicking on the canvas to place the entity.
Double-click on the entity to give it a name.
To add attributes to an entity, right-click on the entity and select "Add Attribute."
To define relationships between entities, select the "Relationship" tool from the toolbar.
Click on one entity and then click on the related entity to establish a relationship.
Specify the cardinality and other properties of the relationship.
Step 6: Save Your ERD
It's important to save your work. Click on "File" and then "Save" to save the model.
MySQL Workbench allows you to generate SQL scripts from your ERD. You can do this by
clicking on "Database" and then "Forward Engineer..." to create a database schema based on your
ERD.
Step 8: Review and Export (Optional)
You can review your ERD, make any necessary changes, and then export it in different formats,
such as PNG or PDF.
Output Examples:
Experiment No: - 3
-- Inner Join
SELECT orders.order_id, customers.customer_name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id;
-- Left Join
SELECT employees.first_name, departments.department_name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.department_id;
-- Deleting a record
DELETE FROM employees
WHERE employee_id = 102;
-- Dropping a table
DROP TABLE products;
Experiment No: - 4
ProgramName: Normalization
TheoryConcept:
We'll normalize the data by creating two separate tables: "Customers" and "Orders." The"Customers"
table will store customer information, and the "Orders" table will store order information.
Now that we have normalized our data, we can query the "Customers" and "Orders" tables to
retrieve information:
Output:
These queries demonstrate the result of normalizing the data. The "Customers" table contains
unique customer information, and the "Orders" table stores order details with a reference to the
customer. The last query retrieves the total order amount for each customer, demonstrating the power of
relational databases and normalization.
Experiment No-5
Implementation:
1. Declare Cursor
A cursor is a select statement, defined in the declaration section in MySQL.
Syntax
1. DECLARE cursor_name CURSOR FOR
2. Select statement;
Parameter:
cursor_name: name of the cursor
select_statement: select query associated with the cursor
2.Open Cursor
After declaring the cursor the next step is to open the cursor using open statement.
Syntax
Open cursor_name;
Parameter:
cursor_name: name of the cursor which is already declared.
3. Fetch Cursor
After declaring and opening the cursor, the next step is to fetch the cursor. It is used to fetch the row or the
column.
Syntax
FETCH [ NEXT [ FROM ] ] cursor_name INTO variable_list;
Parameter:
cursor_name: name of the cursor
variable_list: variables, comma separated, etc. is stored in a cursor for the result set
4. Close Cursor
The final step is to close the cursor.
Syntax
Close cursor_name;
Parameter:
Cursor_name: name of the cursor
Experiment No -6
ProgramName:Creating procedure and functions in MYSQL
TheoryConcept:
Let us understand how to create a procedure in MySQL through example. First, we need to select a
database that will store the newly created procedure. We can select the database using the below
statement:
Suppose this database has a table named student_info that contains the following data:
Implementation:
1. DELIMITER &&
2. CREATE PROCEDURE get_merit_student ()
3. BEGIN
4. SELECT * FROM student_info WHERE marks > 70;
5. SELECT COUNT(stud_code) AS Total_Student FROM student_info;
6. END &&
7. DELIMITER ;
Create Function
Just as you can create functions in other languages, you can create your own functions in
MySQL. Let's take a closer look.
Syntax
The syntax to create a function in MySQL is:
function_name
The name to assign to this function in MySQL.
parameter
One or more parameters passed into the function. When creating a function, all parameters
are considered to be IN parameters (not OUT or INOUT parameters) where the parameters
can be referenced by the function but can not be overwritten by the function.
return_datatype
The data type of the function's return value.
declaration_section
The place in the function where you declare local variables.
executable_section
The place in the function where you enter the code for the function.
Drop Function
Once you have created your function in MySQL, you might find that you need to remove it from the
database.
Syntax
The syntax to a drop a function in MySQL is:
DROP FUNCTION [ IF EXISTS ]function_name;
function_name
The name of the function that you wish to drop.
Example
Let's look at an example of how to drop a function in MySQL.
For example:
Implementation:
Parameter Explanation
The create trigger syntax contains the following parameters:
trigger_name: It is the name of the trigger that we want to create. It must be written after the
CREATE TRIGGER statement. It is to make sure that the trigger name should be unique within the
schema.
trigger_time: It is the trigger action time, which should be either BEFORE or AFTER. It is the required
parameter while defining a trigger. It indicates that the trigger will be invoked before or after each row
modification occurs on the table.
trigger_event: It is the type of operation name that activates the trigger. It can be
either INSERT, UPDATE, or DELETE operation. The trigger can invoke only one event at one time. If
we want to define a trigger which is invoked by multiple events, it is required to define multiple triggers,
and one for each event.
table_name: It is the name of the table to which the trigger is associated. It must be written after the ON
keyword. If we did not specify the table name, a trigger would not exist.
BEGIN END Block: Finally, we will specify the statement for execution when the trigger is activated.
If we want to execute multiple statements, we will use the BEGIN END block that contains a set of
queries to define the logic for the trigger.
The trigger body can access the column's values, which are affected by the DML statement.
The NEW and OLD modifiers are used to distinguish the column values BEFORE and AFTER the
execution of the DML statement. We can use the column name with NEW and OLD modifiers
as OLD.col_name and NEW.col_name. The OLD.column_name indicates the column of an existing
row before the updation or deletion occurs. NEW.col_name indicates the column of a new row that will
be inserted or an existing row after it is updated.
For example, suppose we want to update the column name message_info using the trigger. In the
trigger body, we can access the column value before the update as OLD.message_info and the new
value NEW.message_info.
We can understand the availability of OLD and NEW modifiers with the below table:
Creating
Packages:
Package Specification:
The specification is the interface to the package. It just DECLARES the types, variables, constants,
exceptions, cursors, and subprograms that can be referenced from outside the package. In other words, it
contains all information about the content of the package, but excludes the code for the subprograms.
All objects placed in the specification are called public objects. Any subprogram not in the package
specification but coded in the package body is called a private object.
The following program provides a more complete package. We will use the CUSTOMERS table stored
in our database with the following records –
When the above code is executed at the SQL prompt, it creates the above package and displays the
following result −Package created
The above example makes use of the nested table. We will discuss the concept of nested
table in the next chapter.
When the above code is executed at the SQL prompt, it produces the following result −
TheoryConcept:
1. Requirements Gathering:
Identify the specific requirements of your payroll system, including the types of employees, salary
structures, deductions, and reporting needs.
2. Database Design:
Create a MySQL database to store employee and payroll-related data. You can use tools like MySQL
Workbench for this task.
Set up relationships between tables using foreign keys to ensure data integrity.
Develop a user interface for data input, which allows HR personnel to add, edit, and delete employee
records.
Implement data validation to ensure that the entered data is accurate and consistent.
5. Salary Calculation:
Write SQL queries or create stored procedures to calculate employee salaries based on the salary structure
and allowances.
6. Deduction Management:
Create functions or stored procedures to manage deductions, including tax deductions, insurance, and
other deductions.
7. Payroll Processing:
Implement a payroll processing module that generates payroll records for each employee based on the pay
period.
8. Reporting:
9. Security:
Implement user authentication and authorization to restrict access to sensitive payroll data.
10. Testing:
Thoroughly test the system to ensure accuracy in salary calculations, data input, and report generation.
Implementation:
To implement a payroll processing system in MySQL, you'll need to create various tables to store employee
information, salary details, deductions, payroll records, and more. Below is an example of MySQL code to
create some of the essential tables for a basic payroll system. You can expand upon this schema to meet
your specific requirements:
MYSQL Code:
TheoryConcept:ThefollowingexamplewouldillustratetheconceptofCURSORS.Wewillbeusingthe
CLIENT_MASTER table and display records.
Implementation:
DECLARE
CURSOR
client_curisSELECTid,name
,address
FROM client_master;
client_recclient_cur%rowt
ype;BEGIN
OPENclient_cur;
LOOP
FETCH client_cur into
client_rec;EXITWHENclient_cur
%notfound;
DBMS_OUTPUT.put_line(client_rec.id||''||client_rec.name);
END LOOP;
END;
/
Output:Whenthe above codeis executed atSQLprompt,itproducesthefollowing result:
1 Ramesh
2 Khilan
3 kaushik
4 Chaitali
5 Hardik
6 Komal
ProgramName:CreatingEntity-RelationshipDiagramusingcasetools.
Theory Concept: Entity relationship diagram (ERD) is a kind of diagram for presenting visually
thestructure of relational database. In this experiment we will make use of ERD to model the
databasestructure of a simple bus route management system.
Implementation:
1. StartVisualParadigm.Selectanewworkspacefolderforthistutorial.
2. SelectProject>Newfromthetoolbartocreateaproject.NametheprojectasBusRouteMa
nagement and confirm.
3. TocreateanERD,selectDiagram>Newfromthetoolbar.IntheNewDiagramwindow,selectEntityRel
ationship Diagram and clickNext. Enter Bus Route Management as diagram name and click
OK.
4. Let's start by creating the first entity Route. Select Entity in diagram toolbar and click on the
diagram tocreatean entity. Name theentityRouteand press Enterto confirm.
5. CreatecolumnsinRoute.Let'sstartwithaprimarykey.RightclickonentityRouteandselectNewCol
umnfrom popup menu.
6. Enter+id:varchar(10)andpressEnter.Notethatthe+signmeansthatthecolumnisaprimarykey.Varc
haris the columntype and 10 isthe length.
7. Enterfare:floatandpressEnter, thenEsctocreateanothercolumn.
8. CreateentityStop.Abusroutehasmanybusstops,whileastopcanbesharedbymanyroutes.Therefore,there
is a many-to-many relationship between Route and Stop. Place the mouse pointer over
theRouteentity.DragouttheResourceCatalogiconattopright.
9. Release the mouse button and select Many-to-Many Relationship -> Entity from Resource Catalog.
NamethenewentityStop,YoucanseethatalinkedentityRoute_Stopisautomaticallycreatedinbetwee
n Route and Stop, with foreign key added.
K Nam Type
e e
y
P id int(10)
K
name varchar(2
55)
termi blob
nus
11. 11.
The diagram should now become:
12. A route has multiple bus schedules. Create an entity Schedule from Route with a one-to-
manyrelationship.MovethemousepointertoRoute.PressanddragouttheResourceCatalogicon.
Select One-to-Many Relationship -> Entity to create entity Schedule.
PK id int(10)
departure Date
arrive Date
14. A schedule is handled by a bus. Create an entity Bus from Schedule, with an one-to-one
relationship.Create the following columns inBus:
PK vehicle_id int(10)
fleet_id varchar(10)
last_main Date
15. The diagram should become:
16. A bus is driven by a bus driver. Create entity Driver from Bus with a one-to-one relationship. Add
thefollowing columns to Driver:
PK Id int(10)
Name varchar(255)
employ_date Date
ProgramName: PL/SQLprogramming
a. WriteaPL/SQLblockcodetoprintthesquaresofnumbersupto 99.
b. WriteaPL/SQLblockcodetoinsertdataintotableCUSTOMER
TheoryConcept:
Theprogramwouldprintthesquaresofnumbersupto99usingforloopanddataintotableCUSTOMERin
pl/sql.
Implementation:
Ans
(a):DECLARE
BEGIN
forxin1..99loop
dbms_output.put_l
ine(x*x);endloop;
end;
Output:
Implementation:
Ans:(b)
Null? Type
desc customer;Name
VARCHAR2(10)
C_IDVARCHAR2(2)CNAME
SALARY NUMBER
select*fromcustomer;
Output:
C_CNAME SALARY
c1jkjkjkj 7888