0% found this document useful (0 votes)
15 views

Exp 5 and 9

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Exp 5 and 9

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Experiment:

5.Normalization

Prod_Id Sup_Id Price Prod_name Sup_Name Cust_Id Cust_name Date

Sales
Primary key: ( Prod_Id,Sup_Id)

The above table is in 1NF

But the above table is not in 2NF because there is a partial dependency

If you know Pro_Id, we can write Prod_name that is

Prod_Id->Prod_name ( Partial dependency)

If you know Sup_Id, we can write Sup_Name that is

Sup_Id->Sup_Name (Partial Dependency)

Hence the table is not in 2N

Decompose the table to convert into 2NF

Sales:

Prod_Id Sup_Id Price Cust_Id Cust_name Date

Primary key: ( Prod_Id,Sup_Id)

Product:

Prod_Id Prod_name

Primary key: Prod_Id


Supplier:

Sup_Id Sup_Name

Primary key: Sup_Name

Product and Supplier tables are in 3NF but Sales table is not in 3NF because there is a transitive
dependency

Sales:

Cust_nam
Prod_Id Sup_Id Price Cust_Id Date
e

If you know the Cust_Id we can write Cust_name,

Cust_Id->Cust_name

that is a non key attribute determines an attribute value that non key attribute Cust_nmae
transitively dependent on primary key, this is not allowed in 3NF.

1) (Prod_Id,Sup_Id ) -> Cust_Id


2) Cust_Id->Cust_name

From 1 and 2 : (Prod_Id,Sup_Id )->Cust_name

To convert into 3NF decompose the table

Sales:

Prod_Id Sup_Id Price Date

Primary key: ( Prod_Id,Sup_Id)

Customer:

Cust_Id Cust_name

Primary key: Cust_Id

Product:

Prod_Id Prod_name

Primary key: Prod_Id

Supplier:
Sup_Id Sup_Name

Primary key: Sup_Name

Now the tables Sales,Customer,Product and supplier are in 3NF

Experiment:

9.Procedures
Procedure (Stored Procedure)

A procedure (often called a stored procedure) is a collection of pre-compiled SQL


statements stored inside the database. It is a subroutine or a subprogram in the regular
computing language. A procedure always contains a name, parameter lists, and SQL
statements.

How to create a procedure?


The following syntax is used for creating a stored procedure in MySQL. It can return one or
more value through parameters or sometimes may not return at all.
Syntax:

DELIMITER &&

CREATE PROCEDURE procedure_name [[IN | OUT | INOUT] parameter_name datatype [, parameter


datatype]) ]

BEGIN

Declaration_section

Executable_section

END &&

DELIMITER ;

Parameter Explanations
The procedure syntax has the following parameters:

Parameter Name Descriptions

procedure_name It represents the name of the stored procedure.


It represents the number of parameters. It can be one or more than
parameter
one.

Declaration_section It represents the declarations of all variables.

Executable_section It represents the code for the function execution.

MySQL procedure parameter has one of three modes:


IN parameter
It is the default mode. It takes a parameter as input, such as an attribute. When we define it, the
calling program has to pass an argument to the stored procedure. This parameter's value is
always protected.
OUT parameters
It is used to pass a parameter as output. Its value can be changed inside the stored procedure,
and the changed (new) value is passed back to the calling program. It is noted that a procedure
cannot access the OUT parameter's initial value when it starts.

INOUT parameters
It is a combination of IN and OUT parameters. It means the calling program can pass the
argument, and the procedure can modify the INOUT parameter, and then passes the new value
back to the calling program.

How to call a stored procedure?


We can use the CALL statement to call a stored procedure. This statement returns the values
to its caller through its parameters (IN, OUT, or INOUT). The following syntax is used to call
the stored procedure in MySQL:

Syntax:

CALL procedure_name ( parameter(s))

Example on procedures:
create the following table and insert data

table name: student_info

Program on Procedure without Parameter


1) create a procedure to display all records of this table whose marks are greater than 70 and count
all the table rows.

Code:

DELIMITER &&

CREATE PROCEDURE get_merit_student ()

BEGIN

SELECT * FROM student_info WHERE marks > 70;

SELECT COUNT(stud_code) AS Total_Student FROM student_info;

END &&

DELIMITER ;

Execution:

CALL get_merit_student();

Output:
Program on Procedures with IN Parameter
2. Create a procedure to to display n number of records

In this procedure, we have used the IN parameter as 'var1' of integer type to accept a number from
users.

Its body part fetches the records from the table using a SELECT statement and returns only those
rows that will be supplied by the user.

It also returns the total number of rows of the specified table. See the procedure code:

DELIMITER &&

CREATE PROCEDURE get_student (IN var1 INT)

BEGIN

SELECT * FROM student_info LIMIT var1;

SELECT COUNT(stud_code) AS Total_Student FROM student_info;

END &&

DELIMITER ;

Execution:

CALL get_student(4);

output:
Program on Procedures with OUT Parameter
3.Create a procedure to find the highest marks

In this procedure, we have used the OUT parameter as the 'highestmark' of integer type. Its body
part fetches the maximum marks from the table using a MAX() function. See the procedure code:

DELIMITER &&

CREATE PROCEDURE display_max_mark (OUT highestmark INT)

BEGIN

SELECT MAX(marks) INTO highestmark FROM student_info;

END &&

DELIMITER ;

Execution:

CALL display_max_mark(@M);

SELECT @M;

output:

You might also like