Sei sulla pagina 1di 40

Tutorial

2: Retail Chain Management System (RCMS)


Database Management Systems
SWAYAM: NPTEL-NOC MOOCs Jan-Apr, 2018

Partha Pratim Das
SrijoniMajumdar
Gurunath Reddy M
Himadri B G S Bhuyan

Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
ppd@cse.iitkgp.ernet.in

28-Mar-2018

In this tutorial, an end to end implementation of a schema for a specific system is shown along with
illustrations of querying using the schema. A system is described by specifying the functional and
non-functional requirements. Based on this description, the major entities are identified and
modeled. Further the relationships are modeled to form the initial schema. The schema is further
refined by removing redundancies through normalization. Also based on the query requirements,
the schema is remodeled to facilitate querying. Finally, an illustration of various queries to extract
required information from the system is shown using MYSQL.

The five major workflows are identified as

1. System Specifications
2. Design of Initial Schema
3. Schema refinement using functional dependencies and normalization
4. Schema refinement using query requirements
5. Illustration of querying the system using MYSQL






1. SYSTEM SPECIFICATIONS

Description and functionalities of the system

A retail chain has multiple stores across the city of Mumbai. It specializes in garment items. Every
item is characterized by an item number, name, gender and segment. The segment is required to
differentiate between the materials of the garment. The gender is required to understand whether
the garment is designed for male, female or kids.

The sales of every garment are tracked by its item number and the store from which it is bought.
Hence every sale is characterized by the item number, item name, quantity, date of purchase, the
store details like the store name, store location, store manager name and address.

The inventory is stocked in various warehouses across the city. Every item is stocked in various
warehouses. A certain item can be stocked in more than one warehouse. The quantity stored per
warehouse is maintained along with the item number and warehouse number, location, manager id
and address.

There are various types of vendors.

Type A vendors provides a minimum of 10 types of items with a discount of 10 %

Type B vendors provides a minimum of 5 types of items with a discount of 5 %

Type C vendors provides a minimum of 5 types of items with a discount of 4 %

Each of these vendors can also supply the same item in parallel. Also there is no upper limit on the
types of item which can be supplied by these vendors. Type A vendors are most preferred.

Every purchase is tracked through the item number, the vendor details (like id, name, type, and
address), the date of purchase and the date of delivery.

Likewise, a delivery for an item is tracked through the item number, vendor details, the warehouse
number and location (to which the item will be delivered), the actual date of delivery and delay if
applicable.

Employees of the retail chain either work at the store or at the warehouse. For every store worker,
the worker id, name, address is stored along with the store details where the worker is employed. A
worker can work only at one store.

Similarly, for every warehouse worker, the worker id, name, address is stored along with the
warehouse details where the worker is employed. A worker can work only at one warehouse.

A worker cannot be employed at the warehouse and the store simultaneously. He can either work at
the warehouse or store.

RCMS should support the following operations / query:


1. Add / Remove /Update sales record

2. Add / Remove / Update stock record.

3. Add / Remove / Update worker information

4. Find the item with highest sales in the year 2009 for store ‘A’

5. Find the store recording the highest sales of item ‘A’ in the year 2010

6. Find the item with the highest quantity in store in warehouse ‘B’ in the month of July in
2015.

7. List the type A vendors

8. Check and list all the vendors who have delivered after the initial date of delivery and
the reasons for delay

9. Check for the workers residing in address ‘C’ and list their names and id.

10. and so on

2. DESIGN OF THE INITIAL SCHEMA


In this section, the major entities will be identified along with the possible attributes. Further the
relationships within these entities will be modeled.

Entities and Relationships


Entity: item

A retail chain has several sections to manage its operation. It has multiple stores across the city of
Mumbai. It specializes in garment items. Every item is characterized by an item number, name,
gender and segment. The segment is required to differentiate between the materials of the
garment. The gender is required to understand whether the garment is designed for male, female or
kids.

Entity

• item

Attributes:

• item_no

• item_name

• item_segment

• gender

Key: item_no


Entity: sales

The sales of every garment are tracked by its item number and the store from which it is bought.
Hence every sale is characterized by the item number, item name, quantity, date of purchase, the
store details like the store name, store location, store manager name and address.

Entity

• sales

Attributes:

• item_no

• item_name

• item_segment

• quantity

• store_id

• date

• store_name

• store_location

• store_manager_name is composite

• store_manager_id

• store_manager_address

Key: item_no, store_id

Entity: stock
The inventory is stocked in various warehouses across the city. Every item is stocked in various
warehouses. A certain item can be stocked in more than one warehouse. The quantity stored per
warehouse is maintained along with the item number and warehouse number, location, manager id
and address.

Entity Set:

• stock

Attributes:

• item_no

• quantity_in_store

• month

• year

• warehouse_number

• warehouse_location

• warehouse_manager_name is composite

• warehouse_manager_id

• warehouse_manager_address

Key: item_no, warehouse_number,month, year

Entity: store workers


Employees of the retail chain either work at the store or at the warehouse. For every store worker,
the worker id, name, address is stored along with the store details where the worker is employed. A
worker can work only at one store.

A worker cannot be employed at the warehouse and the store simultaneously. He can either work at
the warehouse or store.

Entity Set:

• store workers

Attributes:

• worker_id

• worker_name

• worker_address

• store_id

• store_manager_id

Key: worker_id

Entity: warehouse workers

Similarly, for every warehouse worker, the worker id, name, address is stored along with the
warehouse details where the worker is employed. A worker can work only at one warehouse.

A worker cannot be employed at the warehouse and the store simultaneously. He can either work at
the warehouse or store.

Entity Set:

• warehouse workers

Attributes:

• worker_id

• worker_name

• worker_address

• warehouse_num

• warehouse_manager_id

Key: worker_id


Entity: vendors

There are various types of vendors.

Type A vendors provides a minimum of 10 types of items with a discount of 10 %

Type B vendors provides a minimum of 5 types of items with a discount of 5 %

Type C vendors provides a minimum of 5 types of items with a discount of 4 %

Each of these vendors can also supply the same item in parallel. Also there is no upper limit on the
types of item which can be supplied by these vendors. Type A vendors are most preferred

Entity Set:

• vendor

Attributes:

• vendor_id

• vendor_name

• vendor_address

• vendor_type

Key: vendor_id


Relationship: buy

Every purchase is tracked through the item number, the vendor details (like id, name, type, and
address), the date of purchase and the date of delivery.

Relationship:

• buy

Entities Involved:

Item, Vendor

Attributes of Item and Vendor:

• item_no

• vendor_id

• vendor_name

• vendor_address

• vendor_type

Relationship Attributes:

• date_purchase

• date_delivery_intial

Key: item_no, vendor_id,date_purchase


Relationship: deliver

Likewise, a delivery for an item is tracked through the item number, vendor details, the warehouse
number and location (to which the item will be delivered), the actual date of delivery and delay if
applicable.

Relationship:

• deliver

Entities Involved:

Item, Vendor

Attributes of Item and Vendor:

• item_no

• vendor_id

• vendor_name is composite

• warehouse_number

• warehouse_location

• date_delivery_intial

• date_delivery_final

• reason_if_delay

Key: item_no, vendor_id,date_delivery_initial


The initial schema

Ø item(item_no, item_name, item_segment, gender)

Ø sales(item_no, item_name, item_segment, quantity, store_id, date, store_name,


store_location, store_manager_name, store_manager_id, store_manager_address )

Ø stock(item_no, quantity_in_store, warehouse_number, warehouse_location,


warehouse_manager_name, warehouse_manager_id, warehouse_manager_address,
month, year)

Ø store_workers(worker_id, worker_name, worker_address, store_id, store_manager_id)

Ø warehouse_workers(worker_id, worker_name, worker_address, warehouse_number,


warehouse_manager_id)

Ø vendor(vendor_id, vendor_name, vendor_address, vendor_type)

Ø buy(item_no, vendor_id, vendor_name, vendor_address, vendor_type, date_purchase,


date_delivery_intial)

Ø delivery(item_no, vendor_id, vendor_name, vendor_type, warehouse_number,


warehouse_location, date_delivery_intial, date_delivery_final, reason_if_delay)


3. SCHEMA REFINEMENT USING DEPENDENCIES AND NORMALISATION
In this section, initial schema will be refined by analyzing functional dependencies and normalizing
the schemas to remove the redundancies.

Ø item(item_no, item_name, item_segment, gender)

Functional Dependencies

• item_no à item_name, item_segment, gender - 1NF, 2NF, 3NF, BCNF


Ø sales(item_no, item_name, item_segment, quantity, store_id, date, store_name,
store_location, store_manager_name, store_manager_id, store_manager_address )

Functional Dependencies

• item_no à item_name, item_segment - violates 2NF


• store_id à store_name, store_location, store_manager_name, store_manager_id - violates
2NF
• store_manager_idàstore_manager_name, store_manager_address - violates 3NF
• item_no, store_id à quantity, date

RESULTS IN

Entity Set: store

Ø store(store_id, store_name, store_location, store_manager_id ) - 1NF, 2NF, 3NF,


BCNF
Ø store_id is unique
Ø Store Manager_id can be mapped to store_workers_id

Relationship: sales

Ø sales(item_no, store_id, quantity, date) - 1NF, 2NF, 3NF, BCNF


Ø Involved entities: item (attribute: item_no); store(store_id)
Ø Attributes: quantity, date


Ø stock(item_no, quantity_in_store, warehouse_number, warehouse_location,
warehouse_manager_name, warehouse_manager_id, warehouse_manager_address,
month, year)

Functional Dependencies

• warehouse_number à warehouse_location, warehouse_manager_name,


warehouse_manager_id, warehouse_manager_address - violates 2NF

• warehouse_manager_idàwarehouse_manager_name,
warehouse_manager_address violates 3NF

• item_no, warehouse_number,month, year à quantity_in_store

RESULTS IN

Entity: warehouse

Ø warehouse(warehouse_number, warehouse_location, warehouse_manager_id) -


1NF, 2NF, 3NF, BCNF

Ø warehouse_number is unique
Ø warehouse_manager_id details can be merged with warehouse_worker table

Relationship: stock

Ø stock(item_no, warehouse_number, quantity_in_store, month, year) - 1NF, 2NF,


3NF, BCNF
Ø Involved entities: item (attribute: item_no); warehouse(warehouse_number)
Ø Attributes: quantity_in_store, month, year


Ø store_workers(worker_id, worker_name, worker_address, store_id, store_manager_id)

Functional Dependencies

• worker_idà worker_name, worker_address, store_id, store_manager_id - 1NF,


2NF, 3NF, BCNF


Ø warehouse_workers(worker_id, worker_name, worker_address, warehouse_number,
warehouse_manager_id)

Functional Dependencies

• worker_idàworker_name, worker_address, warehouse_number,


warehouse_manager_id - 1NF, 2NF, 3NF, BCNF


Ø vendor(vendor_id, vendor_name, vendor_address, vendor_type)

Functional Dependencies

• vendor_idà vendor_name, vendor_address, vendor_type - 1NF, 2NF, 3NF, BCNF


Ø buy(item_no, vendor_id, vendor_name, vendor_address, vendor_type, date_purchase,
date_delivery_intial)

Functional Dependencies

• vendor_id à vendor_name, vendor_address, vendor_type – violates 2NF

• item_no, vendor_id, date_purchase à date_delivery_intial

RESULTS IN

Relationship: buy

Ø buy(item_no, vendor_id, date_purchase, date_delivery_intial)- 1NF, 2NF, 3NF, BCNF


Ø Involved entities: item (attribute: item_no); vendor(attribute:vendor_id)
Ø Attributes: date_purchase, date_delivery_intial


Ø delivery(item_no, vendor_id, warehouse_number, date_delivery_intial, date_delivery_final,
reason_if_delay)

Functional Dependencies

• item_no, vendor_id, date_delivery_intial à date_delivery_final, reason_if_delay,


warehouse_number - 1NF, 2NF, 3NF, BCNF


Refined Schema

1. sales(item_no, store_id, quantity, date)

2. item(item_no, item_name, item_segment, gender)

3. store(store_id, store_name, store_location, store_manager_id)

4. stock(item_no, warehouse_number, quantity_in_store, month, year)

5. warehouse(warehouse_number, warehouse_location, warehouse_manager_id)

6. store_workers(worker_id, worker_name, worker_address, store_id, store_manager_id)

7. warehouse_workers(worker_id, worker_name, worker_address, warehouse_number,

warehouse_manager_id)

8. buy(item_no, vendor_id, date_purchase, date_delivery_intial)

9. vendor(vendor_id, vendor_name, vendor_address, vendor_type)

10. delivery(item_no, vendor_id, date_delivery_intial, warehouse_number, date_delivery_final,

reason_if_delay)


4. SCHEMA REFINEMENT USING QUERY REQUIREMENTS OF THE SYSTEM
Based on query requirements, the entities will be remodeled in this section.

Analyzing query requirements and schema refinement


Query: Check for the workers residing in address ‘C’ and list their names and id.

Will require querying from two tables store_workers and warehouse_workers.

If these tables could be merged and a ‘type’ column could be introduced.

As the format for Store_id’s and Warehouse numbers are different, we can keep two separate
columns

Entity: workers

Entity Set:

• workers

Attributes:

• worker_id

• worker_name

• worker_address

• type - can be ‘S’ for store, ‘W’ for warehouse

• warehouse_num will be null if type = ‘S’

• store_id will be null if type=’W’

• manager_id may be null for managers

Key: worker_id


Refined Schema
Ø sales(item_no, store_id, quantity, date) FK: item_no, store_id
Ø item(item_no, item_name, item_segment, gender)
Ø store(store_id, store_name, store_location, store_manager_id)
Ø stock(item_no, warehouse_number, month, year, quantity_in_store) FK: item_no,
warehouse_number
Ø warehouse(warehouse_number, warehouse_location, warehouse_manager_id)
Ø workers(worker_id, worker_name, worker_address, type, warehouse_number, store_id,
manager_id)
Ø buy(item_no, vendor_id, date_purchase, date_delivery_intial) FK: item_no, vendor_id
Ø vendor(vendor_id, vendor_name, vendor_address, vendor_type)
Ø delivery(item_no, vendor_id, date_delivery_intial, warehouse_number,
date_delivery_final, reason_if_delay) FK: item_no, vendor_id, warehouse_number


The underlined attributes are the primary keys of the relation. The foreign key are
mentioned alongside the relation as FK.


5. ILLUSTRATION OF QUERYING THE SYSTEM USING MYSQL
In this section, various instances of querying the system using MYSQL will be shown. The results are
also shown.

(Note: When you will be trying the queries from this section, please don’t copy the queries from this
document and paste it directly in the mysql prompt, as it might not run, due to insertion of
unwanted hidden special characters while copying. Please refer the queries, but type them in the
prompt)

MySQL

Install SQL in Ubuntu machine by issuing the below command in terminal ($ is shell prompt)

$ sudo apt-get install mysql-server

You could able to see the following prompts

Note:

If prompts for a password, give your own password

Once MySQL is installed, access the MySQL shell by

$ mysql -u root -p

You should be able to see the following on the terminal


To check the available databases issue ($ will turn to mysql> prompt)

mysql> SHOW DATABASES;

After the above command, your screen will looks like this

You can see that our previous HMSystem along with few other databases are present in the MySQL
server.

To create a new database use

CREATE DATABASE database_name;

For our Retail Chain Management System (RCMS), we create our database by

mysql>CREATE DATABASE RCMS;

Now,

mysql>SHOW DATABASES;

From the above screen, we can see that our new database RCMS has been added into the system
databases.

Accessing our newly created database system

To create the tables in the newly created database RCMS, we need to open it.

mysql> USE RCMS;

Your command prompt should say Database changed

To check the tables in RCMSuse

mysql> SHOW TABLES;

Your command prompt should say Empty set (0.00 sec) because we have not yet added any tables.

Now, lets add tables intoRCMS:

We can add the item table as follows

mysql>CREATE TABLE item(

item_no NUMERIC(4, 0),

item_name VARCHAR(50),

item_segment VARCHAR(1) CHECK (item_segment IN ('C', 'V', 'R')),

gender VARCHAR(1) CHECK (gender IN ('M', 'W', 'K')),

PRIMARY KEY (item_no) );

After adding the table, we can check for the added fields in the table as

mysql> DESCRIBE item;


Similarly, we can create other tables as follows

To create tabel store use

CREATE TABLE store(

store_id NUMERIC(2, 0) NOT NULL,

store_name VARCHAR(50),

store_location VARCHAR(100),

manager_id NUMERIC(6, 0),

PRIMARY KEY (store_id) );

To create table warehouse use

CREATE TABLE warehouse(

warehouse_number NUMERIC(2, 0) NOT NULL,

warehouse_location VARCHAR(100),

warehouse_manager_id NUMERIC(6, 0),

PRIMARY KEY (warehouse_number) );


To create tableworkersuse

CREATE TABLE workers(

worker_id NUMERIC(6, 0) NOT NULL,

worker_name VARCHAR(50),

worker_address VARCHAR(100),

type VARCHAR(1) check (type in ('S', 'W')),

warehouse_number NUMERIC(2, 0),

store_id NUMERIC(2, 0),

manager_id NUMERIC (6, 0),

PRIMARY KEY (worker_id) );

To create tablevendoruse

CREATE TABLE vendor(

vendor_id NUMERIC(6, 0) NOT NULL,

vendor_name VARCHAR(50),

vender_address VARCHAR(100),

vendor_type VARCHAR(1) check (type in ('A', 'B', 'C')),

PRIMARY KEY (vendor_id));


To create tablesalesuse

CREATE TABLE sales(

item_no NUMERIC(4, 0) NOT NULL,

store_id NUMERIC(2, 0) NOT NULL,

quantity INTEGER CHECK (quantity > 0),

date DATE,

PRIMARY KEY (item_no, store_id),

FOREIGN KEY (item_no) REFERENCES item (item_no),

FOREIGN KEY (store_id) REFERENCES store(store_id) );

To create tablebuyuse

CREATE TABLE buy(

item_no NUMERIC(4, 0) NOT NULL,

vendor_id NUMERIC(6, 0) NOT NULL,

date_purchase DATE,

date_delivery_initial DATE,

PRIMARY KEY (item_no, vendor_id, date_purchase),

FOREIGN KEY (item_no) REFERENCES item (item_no),

FOREIGN KEY (vendor_id) REFERENCES vendor(vendor_id) );


To create tabledeliveryuse

CREATE TABLE delivery(

item_no NUMERIC(4, 0) NOT NULL,

vendor_id NUMERIC(6, 0) NOT NULL,

date_delivery_initial DATE NOT NULL,

warehouse_number NUMERIC(2,0),

date_delivery_final DATE,

reason_if_delay VARCHAR(200),

PRIMARY KEY (item_no, vendor_id, date_delivery_initial),

FOREIGN KEY (item_no) REFERENCES item (item_no),

FOREIGN KEY (vendor_id) REFERENCES vendor(vendor_id),

FOREIGN KEY (warehouse_number) REFERENCES warehouse(warehouse_number) );

Finally, SHOW TABLES should list all the tables created in RCMS


Adding Tuples into item Relation

mysql>INSERT INTO item (item_no, item_name, item_segment, gender) VALUES (1234, "Dress",
"C", "W");

mysql> INSERT INTO item(item_no, item_name, item_segment, gender) VALUES (1256, "Shirt",
"C", "M");

mysql> INSERT INTO item(item_no, item_name, item_segment, gender)VALUES (1252, "Trousers",


"V", "M");

mysql> INSERT INTO item(item_no, item_name, item_segment, gender) VALUES (1254, "Trousers",
"C", "M");

mysql>INSERT INTO item(item_no, item_name, item_segment, gender) VALUES (1215, "Sarees",


"C", "W");

mysql> INSERT INTO item(item_no, item_name, item_segment, gender) VALUES (1200, "Kurtas",
"C", "W");

mysql> INSERT INTO item(item_no, item_name, item_segment, gender) VALUES(1202, "Kurtas",


"R", "W");

mysql> INSERT INTO item(item_no, item_name, item_segment, gender) VALUES (1222, "Ties", "R",
"M");

We can view the tuples in item using SELECT statement shown below


Similarly, we can add the tuples to other tables as shown below

For store table

INSERT INTO store(store_id, store_name, store_location, manager_id) VALUES (11, "High Street",
"Circus Road", 451234);

INSERT INTO store(store_id, store_name, store_location, manager_id) VALUES (12, "Quick Buy",
"Central Park", 451212);

INSERT INTO store(store_id, store_name, store_location, manager_id) VALUES (13, "Refresh",


"Central Avenue", 451244);

For warehouse table

INSERT INTO warehouse(warehouse_number, warehouse_location, warehouse_manager_id)


VALUES (11, "Circus End",
461789);

INSERT INTO warehouse(warehouse_number, warehouse_location, warehouse_manager_id)


VALUES (12, "Central End",
411134);

INSERT INTO warehouse(warehouse_number, warehouse_location, warehouse_manager_id)


VALUES (13, "Ring End",
451244);

For workers table:

INSERT INTO workers(worker_id, worker_name, worker_address, type, warehouse_number,


store_id, manager_id) VALUES (451234, "Ram Roy", "Park Circus Road 1", "S", NULL, 11, NULL);

INSERT INTO workers(worker_id, worker_name, worker_address, type, warehouse_number,


store_id, manager_id) VALUES (451212, "Rahul Sen", "Park Circus Road 1", "S", NULL, 12, NULL);

INSERT INTO workers(worker_id, worker_name, worker_address, type, warehouse_number,


store_id, manager_id) VALUES (451244, "Sourya Das", "Park Circus Road 2", "S", NULL, 13, NULL);

INSERT INTO workers(worker_id, worker_name, worker_address, type, warehouse_number,


store_id, manager_id) VALUES (461789, "Sai Ajay", "Park Circus Road 2", "W", 21, NULL, NULL);

INSERT INTO workers(worker_id, worker_name, worker_address, type, warehouse_number,


store_id, manager_id) VALUES (411134, "Rohan Singh", "Park Circus Road 3", "W", 22, NULL, NULL);

INSERT INTO workers(worker_id, worker_name, worker_address, type, warehouse_number,


store_id, manager_id) VALUES (422234, "Rahul Mukherjee", "Park Circus Road 3", "W", 23, NULL,
NULL);

INSERT INTO workers(worker_id, worker_name, worker_address, type, warehouse_number,


store_id, manager_id) VALUES (433234, "Sharmat Roy", "Park Circus Road 1", "W", 21, NULL,
461789);

INSERT INTO workers(worker_id, worker_name, worker_address, type, warehouse_number,


store_id, manager_id) VALUES (451111, "Roshan Sen", "Park Circus Road 6", "W", 22, NULL,
411134);

INSERT INTO workers(worker_id, worker_name, worker_address, type, warehouse_number,


store_id, manager_id) VALUES (451415, "Ritur Banerjee", "Park Circus Road 7", "W", 22, NULL,
411134);

INSERT INTO workers(worker_id, worker_name, worker_address, type, warehouse_number,


store_id, manager_id) VALUES (451876, "Rom Roy", "Park Circus Road 7", "W", 23, NULL, 422234);
INSERT INTO workers(worker_id, worker_name, worker_address, type, warehouse_number,
store_id, manager_id) VALUES (451989, "Dev Narayan", "Park Circus Road 8", "S", NULL, 11,
451234);

INSERT INTO workers(worker_id, worker_name, worker_address, type, warehouse_number,


store_id, manager_id) VALUES (451888, "Rohit Kumar", "Park Circus Road 8", "S", NULL, 11, 451234);

INSERT INTO workers(worker_id, worker_name, worker_address, type, warehouse_number,


store_id, manager_id) VALUES (451877, "Surinder Singh", "Park Circus Road 8", "S", NULL, 12,
451212);

INSERT INTO workers(worker_id, worker_name, worker_address, type, warehouse_number,


store_id, manager_id) VALUES (451889, "Raj Singh", "Park Circus Road 8", "S", NULL, 13, 451244);

SELECT query:

To select names and addresses of all the workers

mysql>SELECT worker_name, worker_address

FROM workers;

Using
WHE
RE
clause

To display the names of all workes who works in warehouse

To display the attributes of all workers who stays in Park Circus Road 8 with names in ascending
order

Using AND clause

To select the cotton collections of women

ALTER TABLE command

To add new column gender to the workers use

mysql> ALTER TABLE workers ADD gender char(1);

This will add a gender column to the workers table with all entries initialized to null initially.

To drop the gender column from workers and to check if the gender column is dropped from
workers

mysql >ALTER TABLE workers DROP gender;

mysql >SELECT * FROM workers;

Updating tuple using UPDATE clause

First add the salary column to the workers and the update the salary to each worker

mysql> ALTER TABLE workers ADD salary INTEGER;


To update the salary of the worker with worker_id with Rs 10000,

mysql>UPDATE workers

SET salary=10000

WHERE worker_id=411134;

Similarly update the salary of other workers with the worker_id as unique tuple identifier.

GROUP BY clause

To know the total amount of salary of each worker


Aggregate functions

To find the number of employees

To find the average salary of the workers

Min and Max Functions

To find the maximum and minimum salary given to the workers


BETWEEN clause

To display all workers whose salary is between 10000 and 40000

EXISTS clause

To select all employee names and salary whose salary >= 10000 using EXISTS clause

Using CURSORS

This is an example to use the cursor to fetch the worker_name if the warehouse_number matches
the id passed by the user through procedure curserdemo().
mysql> delimiter $$

mysql> create procedure curserdemo(id numeric(2, 0))

begin

declare name varchar(50);

declare cur1 cursor for select worker_name from workers where warehouse_number=id;

open cur1;

fetch cur1 into name;

select name;

close cur1;

end $$

mysql> delimiter ;

mysql> call curserdemo(22);

will results in the following output

Potrebbero piacerti anche