简体   繁体   English

SQL关系数据库,用于跟踪全年的销售情况

[英]SQL relational database for tracking sales throughout a year

I am a noobie to relational databases and MySQL. 我是关系数据库和MySQL的入门者。 I was tasked with creating an application that can track sales throughout the year down to the day. 我的任务是创建一个应用程序,该应用程序可以跟踪全年的销售情况。 So as an employee makes a sale, the sale amount and day the sale was made are kept in a database. 因此,当员工进行销售时,销售金额和销售日期将保存在数据库中。

This database needs to keep the current sales for the day, the week, the month and the year. 该数据库需要保留当日,星期,月份和年份的当前销售额。 It also needs to track what employee those sales belong to and how many sales that employee made on a given day. 它还需要跟踪这些销售所属的员工以及该员工在给定的一天中完成了多少销售。

I have drawn a database diagram and have built the tables in MySQL. 我已经绘制了一个数据库图,并在MySQL中建立了表。 The issue I am having is I heard (through online tutorials and such) that a many to many or junction table is not supposed to have duplication. 我遇到的问题是(通过在线教程等)我听说多对多或联结表不应重复。

Well my junction table would be my sales table. 好吧,我的交汇表就是我的销售表。 It has a many to 1 with the employee that made the sale and a many to 1 with the sales made that day. 与进行销售的员工多对一,与当天进行的销售多对一。 So employee1 makes 3 sales on one day, my sales table would look like this: 因此,employee1在一天中进行了3次销售,我的销售表如下所示:

sale_id | sale_amount | day_id | employee_id
1         500           1        1
2         600           1        1
3         750           1        1

where the day_id is the current day and the employee_id is employee1. 其中day_id是当前日期,而employee_id是employee1。 Question 1, is this bad, and if so what would be a better solution? 问题1,这不好吗?如果是这样,哪种解决方案更好?

Question 2, what is the best way to determine the day/week/month of a year to keep track of all this data? 问题2,确定一年中的日期/星期/月份以跟踪所有这些数据的最佳方法是什么? Manually entering 7 day for each week, 52 weeks for each year, and 12 months for each year seems overly tedious. 每周手动输入7天,每年手动输入52周,每年手动输入12个月似乎过于繁琐。 I entered in 5 years, I then entered 12 months for the first year, 52 weeks for the first year, then stopped before adding days to come here and see if there was a better solution available. 我输入了5年的时间,然后第一年输入了12个月,第一年输入了52周,然后停了下来,然后再增加几天来这里,看看是否有更好的解决方案。

Any help on this would be greatly appreciated. 任何帮助,将不胜感激。 Also I would like to do this without any additional 3rd party software if possible. 另外,如果可能的话,我也希望没有任何其他第三方软件。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

Instead of having the table for days, weeks, months and years, store the date as a datetime type. 将日期存储为日期时间类型,而不是使用几天,几周,几个月和几年的表格。 You will only need two tables. 您只需要两个表。

Employee Table : 员工表

DROP TABLE IF EXISTS `employees`;

CREATE TABLE IF NOT EXISTS `tablename` (
    `emp_id`        INT(11) UNSIGNED        NOT NULL    AUTO_INCREMENT                  COMMENT 'Primary Key',
    `emp_name`      VARCHAR(200)            NOT NULL    DEFAULT ''                      COMMENT 'Name of employee',
    `emp_email`     VARCHAR(200)            NOT NULL    DEFAULT ''                      COMMENT 'Email address of employee',
    `emp_password`  VARCHAR(200)            NOT NULL    DEFAULT ''                      COMMENT 'Password encoded',
    PRIMARY KEY (`emp_id`),
    UNIQUE `idx_emp_name` (`emp_name`),
    UNIQUE `idx_emp_email` (`emp_email`)
) 
    ENGINE=MyISAM 
    AUTO_INCREMENT=1 
    DEFAULT CHARSET=utf8 
    COLLATE=utf8_unicode_ci
    COMMENT 'List of Employees';

Sales Table : 销售表

DROP TABLE IF EXISTS `sales`;

CREATE TABLE IF NOT EXISTS `tablename` (
    `sale_id`           INT(11) UNSIGNED        NOT NULL    AUTO_INCREMENT                  COMMENT 'Primary Key',
    `sale_amount`       DOUBLE                  NOT NULL    DEFAULT 0                       COMMENT 'Amount Of Sale',
    `sale_date`         DATETIME                NOT NULL    DEFAULT '0000-00-00'            COMMENT 'Date/Time of sale',
    `sale_emp_id`       INT(11)                 NOT NULL                                    COMMENT 'FK To employees',
    PRIMARY KEY (`sale_id`),
    KEY `idx_sale_date` (`sale_date`),
    KEY `idx_sale_emp_id` (`sale_emp_id`)
) 
    ENGINE=MyISAM 
    AUTO_INCREMENT=1 
    DEFAULT CHARSET=utf8 
    COLLATE=utf8_unicode_ci
    COMMENT 'List of Employees';

-- ==============================================================================================
-- TRIGGER: Update sale_date timestamp on sales
-- ==============================================================================================
DELIMITER |

DROP TRIGGER IF EXISTS `sales_CreatedTS`|

CREATE TRIGGER `sales_CreatedTS` BEFORE INSERT ON `sales`
FOR EACH ROW
BEGIN
    SET NEW.`sale_date` = CURRENT_TIMESTAMP;
END|

DELIMITER ;

From these tables you can produce any report by any period type you need. 从这些表中,您可以根据需要的任何期间类型生成任何报告。

For instance, you could produce a report by employee and week, with subtotals, like this: 例如,您可以按员工和星期生成一份报告,包括小计,如下所示:

SELECT
    a.`emp_name`,
    WEEK(b.`sale_date`) as `week_no`,
    SUM(b.`sale_amount`) as `total_sales_for_week`
FROM `employees` a
JOIN `sales` b
    ON a.`emp_id` = b.`sales_emp_id`
GROUP BY WEEK(b.`sale_date`),a.`emp_id` WITH ROLLUP;

For more info on the MySQL date functions look at this reference . 有关MySQL date函数的更多信息,请参见此参考资料 With those functions, you could produce reports by day of week, date, week, month, and year. 使用这些功能,您可以按星期,日期,星期,月份和年份生成报告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM