Database Design: Using MySQL and PHP to Implement the Retail Business Management System

This project is to use MySQL and PHP to implement the RBMS application.

1. Preparation (5 points)

We will use the following tables:

Employees(eid, ename, city)
Customers(cid, cname, city, visits_made, last_visit_time)
Products(pid, pname, qoh, qoh_threshold, original_price, discnt_rate, sid)
Suppliers(sid, sname, city, telephone)
Purchases(pur, cid, eid, pid, qty, ptime, total_price) //我把pdate改成了ptime

In addition, the following table is also needed for this project

Logs(logid, who, time, table_name, operation, key_value)

Each tuple in the logs table describes who (the login name of a database user) has performed what operation (insert, delete, update) on which table (give the table name) and which tuple (as indicated by the value of the primary key of the tuple) at what time. Attribute logid is the primary key of table.

每条记录应该保存:何人,在何时,对何表的何元组执行了何操作.

who what operation which table which tuple what time
何人 何操作 何表 何元组 何时
the login name of a database user insert/delete/update give the table name primary key when

Use the following SQL DDL statements to create the seven tables required for this project.
Note that you need to use the exact statements as shown below to ensure that the instructor can test your programs using the instructor’s data later. Please also note that the tables are created in certain order such that by the time when a foreign key needs to be created, the corresponding primary key has already been created.

一定要使用下面的语句建表,而且要按顺序建表 (复制建表即可,有些错别字自己改改)

create table employees
(eid varchar(3) not null, //change all eid, cid, pid, pur, key_value to int types in the future
 ename varchar(15),
 city varchar(15),
 primary key(eid));
      
create table customers
(cid varchar(4) not null,
 cname varchar(15),
 city varchar(15),
 visits_made int(5),
 last_visit_time datetime,
 primary key(cid));

create table suppliers
(sid varchar(2) not null,
 sname varchar(15) not null,
 city varchar(15),
 telephone_no char(10),
 primary key(sid),
 unique(sname));

 create table products
(pid varchar(4) not null,
 pname varchar(15) not null,
 qoh int(5) not null,
 qoh_threshold int(5),
 original_price decimal(6,2),
 discnt_rate decimal(3,2),
 sid varchar(2),
 primary key(pid),
 foreign key (sid) references suppliers (sid));

 create table purchases
(pur varchar(4)not null,
 cid varchar(4) not null,
 eid varchar(3) not null,
 pid varchar(4) not null,
 qty int(5),
 ptime datetime,
 total_price decimal(7,2),
 primary key (pur),
 foreign key (cid) references customers(cid),
 foreign key (eid) references employees(eid),
 foreign key (pid) references products(pid));

 create table logs
(logid int(5) not null auto_incrment,
 who varchar(10) not null,
 time datetime not null,
 table_name varchar(20) not null,
 peration varchar(6) not null      key_value varchar(4),
 primary key (logid)); 

The meanings of most of the tables and their attributes are clear. If they are not clear to you, please let the instructor know.
表的属性已经很清楚了,要是有模糊的地儿,您得赶早找您的老师询问清楚。

For a given customer, visits_made indicates how many times the customer has purchased products from the business. For a given customer, last_visit_time is the time of the most recent visit made by the customer.

1.对于 Customers(cid, cname, city, visits_made, last_visit_time)中的 visits_made指的是购买次数,last_visit_time是最近访问时间。

Attribute qoh in the products table indicates quantity on hand and for each product, qoh_threshold is an integer such that when qoh becomes less than qoh_threshold, it is time to get new supplies of this product from the suppliers.

2.对于Products(pid, pname, qoh, qoh_threshold, original_price, discnt_rate, sid)的qoh指的是库存,qoh_threshold指的是库存阈值,库存小于阈值就该进货了。

Each tuple in the purchases table tells which customer (cid) has purchased what product (pid) with what quantity (qty) and the total price (total_price) from which employee (eid) at what time(ptime).
The total price is computed by multiplying the discount price of the product and the quantity purchased. The discount price of a product is computed based on its original price and its discount rate.

对于表Purchases(pur, cid, eid, pid, qty, ptime, total_price),每条记录的含义如下所示

pur cid eid pid qty ptime total_price
pur which customer which employee what product what quantity what time the total price
订单号 顾客id 售货员id 所购买的产品 购买产品数量 购买时间 购买的总价

You should populate the first five tables with appropriate tuples to test your program.
上面六个表中,日志表Logs是自动生成的,其他五个表您得自个儿输入数据,去测试您的程序。


2. MySQL Implementation (50 points)

You need to write SQL queries, stored procedures/functions, and triggers to implement this project. The following requirements and functionalities need to be implemented.
您需要自个儿编写SQL函数/存储过程与触发器,来实现下面的要求:


  1. (6 points) Write a stored procedure to show the tuples in each table. For example, you can implement a procedure, say show_products(), to display all products in the products table.

第一题:对每个表都写一个存储过程,显示表的记录。/或者写一个存储过程,显示指定表的的记录
eg:写个函数show_products(),输出指定的表的所有记录。


  1. (4 points) Write a procedure to report the monthly sale information for any given product. For example, you can use a procedure, say report_monthly_sale(prod_id), for this operation.
    For the given product id, you need to report the product name, the month(the first three letters of the month, e.g., FEB for February), year, the total quantity sold each month, the total dollar amount sold each month, and the average sale price (the total dollar amount divided by the total quantity) of each month. You need to list the information for only those months during which the given product has been purchased by some customers.

第二题:写个存储过程,输出指定产品的全部月度销售记录。
eg:report_monthly_sale(prod_id),其实参数可以自己设置,可以是id,也可以是模糊搜索。

要求:给定产品id,输出如下信息。其中,月份用前三个大写字母(eg:FEB)
如果某月的销售记录为空,则该月记录不输出。

product name month year the total quantity sold per month the total dollar amount sold per month the average sale price per month
产品名字 销售月份 销售年份 该月销售数量num 该月总销售额 amount amount / num

  1. (7 points) Write procedures to add tuples into the purchases table and the products table.
    As an example, you can use a procedure, say add_purchase(pur_no, c_id, e_id, p_id, pur_qty), to add a tuple in the purchases table, where pur_no, c_id, e_id, p_id and pur_qty are parameters of the procedure.
    Note that total_price should be computed based on the data in the database automatically and ptime should be the current time (use current_timestamp).

第三题:写两个存储过程分别往 Products,Purchases 里面添加记录

函数一:add_purchase(pur_no, c_id, e_id, p_id, pur_qty),//这个地方参数很奇怪pur_no不应该是自己生成的吗
记录中非参数的ptime应该利用时间戳自动生成total_price应该自动计算
//第六题中有讲应该同时修改库存,还有 visit_made
Purchases(pur, cid, eid, pid, qty, ptime, total_price)

函数二:add_product(...)
Products(pid, pname, qoh, qoh_threshold, original_price, discnt_rate, sid)


  1. (9 points) Add a tuple to the logs table automatically whenever any table is modified.
    To simplify, you are only required to consider the following modifications (events):
    (1) insert a tuple into the purchases table;
    (2) update the qoh attribute of the products table;
    (3) update the visits_made attribute of the customers table.
    When a tuple is added to the logs table due to the first event, the table_name should be “purchases”, the operation should be “insert” and the key_value should be the pur of the newly inserted purchase.
    When a tuple is added to the logs table due to the second event, the table_name should be “products”, the operation should be “update” and the key_value should be the pid of the affected product.
    When a tuple is added to the logs table due to the third event, the table_name should be “customers”, the operation should be “update” and the key_value should be the cid of the affected customer.
    Adding tuples to the logs table should be implemented using triggers. You need to implement three triggers for this task, one for each event.

Logs(logid, who, time, table_name, operation, key_value)

第四题:自动往日志表Logs中添加记录。
下面三个操作对应三个 触发器。当进行下面的操作的时候,使用 对应触发器 自动往日志表Logs中添加记录。

1.往表Purchases中添加记录的时候

logid who time table_name operation key_value
- - - purchases insert the pur of the newly inserted purchase(订单号)

2.往表Products更新库存 qoh 的时候

logid who time table_name operation key_value
- - - products update the pid of the affected product(产品编号)

3.往表Customers更新购买次数 visits_made的时候

logid who time table_name operation key_value
- - - customers update the cid of the affected customer(顾客编号)

  1. (4 points) Before a purchase is actually made (i.e., before a tuple is added into the purchases table), your program needs to make sure that, for the involved product, the quantity to be purchased is equal to or smaller than the quantity on hand (qoh). Otherwise, an appropriate message should be displayed (e.g., “Insufficient quantity in stock.”) and the purchase request should be rejected.

第五题:保证产品购买量小于等于库存,一旦购买量大于库存,就弹出提示信息“库存不足”,并拒绝本次购买行为。


  1. (16 points) After adding a tuple to the purchases table, the qoh column of the products table should be modified accordingly; that is, the qoh of the product involved in the purchase should be reduced by the quantity purchased. If the purchase causes the qoh of the product to be below qoh_threshold, your program should perform the following tasks:
    (a) print a message indicating the current qoh of the product,
    (b) increase qoh by making it 2 * old_qoh, where old_qoh represents the value of qoh before the corresponding purchase was made (other attribute values of the product will not be changed),
    (c) print another message indicating that the quantity on hand of the product has been increased by old_qoh + qty_sold, where qty_sold is the number of the product sold in the involved purchase.
    In addition, the insertion of the new tuple in the purchases table will cause the visits_made of the customer to be increased by one. Use triggers to implement the update of qoh, printing of the messages and the update of visits_made and last_visit_time.

第六题:
当购买产品时,要往表Purchases中添加记录,使用触发器实现 qoh、visits_made、 last_visit_time的更新,并且弹出提示信息,显示 visits_made、 last_visit_time

同时,当库存 qoh 小于库存阈值 qoh_threshold 时,要执行下面的操作
1.弹出信息,显示产品当前库存(购买后)
2.增加库存,使库存是购买前的两倍
3.弹出信息,表示新购进了 购买量+购买前的库存量

举个例子:
购买前库存量为5,阈值是4。现购买3件。
1.弹出信息,提示库存为 2件。
2.进货使库存变成 10件。
3.提示新进货 5+3, 即8件


  1. (4 points) You need to make your code user friendly by designing and displaying appropriate messages for all exceptions. For example, if someone wants to find the purchases of a customer but entered a non-existent customer id, your program should report the problem clearly.

第七题:对任何异常操作,都输出对应的提示信息。比如查询一个不存在的用户信息,提示用户不存在


3. Interface (35 points)

Implement a Web interactive interface using PHP. Your interface program should utilize as many of your MySQL stored procedures/functions as possible.

PHP与WEB交互的界面,接口多使用上述函数

4. Documentation (10 points)

Documentation consists of the following aspects:

  1. Each procedure and function and every other object you create for your project needs to be explained clearly regarding its objective and usage.
    提供每个函数包括参数的说明书
  2. Your code needs to be well documented with in-line comments.
    代码写注释
  3. Hand-ins, Demo and Grading
    当场演示打分
  4. You will also need to submit your source code along with your documentation to the Blackboard.
    提交文档和源码
  5. It is required to demonstrate your project to the instructor using tuples created by the instructor. More instructions on demo will be given before the demo.
    通过老师的测试
  6. The grading will be based on the quality of your code, the documentation and on how successful of your demo is.
    代码质量要高,演示要好,文档清晰

你可能感兴趣的:(Database Design: Using MySQL and PHP to Implement the Retail Business Management System)