一、背景
二、目的
三、什么是“进销存”,什么是超市进销存管理系统?
四、什么是openGauss数据库?
五、应用与实践(模拟超市进销存系统)
1、 超市进销存数据库表设计
1、 用户管理表 ( User_info )
2、 供应商信息表(supplier_info)
3、 订货单信息表 (order _info )
4、 进货信息表(Purchase_goods_info)
5、 仓库信息表 (warehouse_ info )
6、 商品信息表 (goods_info)
7、 出库信息表 (Outwarehouse_info)
8、 销售 清单信息表(sell_list_info)
9、 销售 信息表 (sell_info)
10、 客户管理表 ( customer_info)
2、创建数据库表
su - omm
gs_om -t start
gsql -d postgres -p 26000 -r
CREATE DATABASE retail_db ENCODING 'UTF8' template = template0;
\connect retail_db
CREATE SCHEMA retail_db;
SET search_path TO retail_db;
DROP TABLE IF EXISTS user_info;
CREATE TABLE user_info(u_id int PRIMARY KEY,u_name char(20) not null,u_mail char(20) unique not null,u_id_card char(20) not null,u_phone char(20) not null,u_password char(20) not null,u_role int not null);
DROP TABLE IF EXISTS supplier_info;
CREATE TABLE supplier_info(supplier_id int PRIMARY KEY,supplier_name char(20) not null,supplier_address varchar(100) not null,supplier_phone char(20) not null,supplier_type int not null,remark varchar(100));
DROP TABLE IF EXISTS order_info;
CREATE TABLE order_info(order_id int PRIMARY KEY,order_date date not null,supplier_id int not null,goods_id char(20) not null,goods_name char(20) not null,goods_home varchar(100) not null,goods_number int not null,goods_amount int not null,goods_sum_amount int not null,remark varchar(100));
DROP TABLE IF EXISTS Purchase_goods_info;
CREATE TABLE Purchase_goods_info(Purchase_goods_id int PRIMARY KEY,goods_id char(30) not null,Purchase_goods_number int not null,Purchase_goods_amount int not null,Purchase_goods_date date not null,supplier_id int not null,operator int not null,remark varchar(100));
DROP TABLE IF EXISTS warehouse_info;
CREATE TABLE warehouse_info(warehouse_id int PRIMARY KEY,warehouse_address varchar(100) not null,warehouse_name varchar(100) not null,warehouse_operator int not null,remark varchar(100));
DROP TABLE IF EXISTS goods_info;
CREATE TABLE goods_info(goods_id char(20) PRIMARY KEY,goods_code varchar(50) not null,goods_name char(20) not null,goods_home varchar(100) not null,goods_number int not null,Purchase_goods_amount int not null,sell_goods_amount int not null,goods_type char(20) not null,supplier_id int not null,warehouse_id int not null,remark varchar(100));
DROP TABLE IF EXISTS outwarehouse_info;
CREATE TABLE outwarehouse_info(outwarehouse_id int PRIMARY KEY,goods_id char(20) not null,Outwarehouse_number int not null,Outwarehouse_date date not null,operator int not null,remark varchar(100));
DROP TABLE IF EXISTS sell_list_info;
CREATE TABLE sell_list_info(sell_id int PRIMARY KEY,sell_date date not null,goods_id char(20) not null,goods_name char(20) not null,goods_number int not null,goods_measurement_unit char(10) not null,sell_goods_amount int not null,remark varchar(100));
DROP TABLE IF EXISTS sell_info;
CREATE TABLE sell_info(sell_id int PRIMARY KEY,sell_goods_sum_amount int not null,customer_phone char(20) not null,remark varchar(100));
DROP TABLE IF EXISTS customer_info;
CREATE TABLE customer_info(customer_id int PRIMARY KEY,customer_name char(20) not null,customer_phone char(20) not null,customer_point int not null,remark varchar(100));
3、手工插入数据
INSERT INTO user_info (u_id,u_name,u_mail,u_id_card,u_phone,u_password,u_role) VALUES (001,'张一','zhangyi@openGauss.com','2023001','12345678901','openGauss_001',1); INSERT INTO user_info (u_id,u_name,u_mail,u_id_card,u_phone,u_password,u_role) VALUES (002,'张二','zhanger@openGauss.com','2023002','12345678902','openGauss_002',2); INSERT INTO user_info (u_id,u_name,u_mail,u_id_card,u_phone,u_password,u_role) VALUES (003,'张三','zhangsan@openGauss.com','2023003','12345678903','openGauss_003',3); INSERT INTO user_info (u_id,u_name,u_mail,u_id_card,u_phone,u_password,u_role) VALUES (004,'张四','zhangsi@openGauss.com','2023004','12345678904','openGauss_004',4); INSERT INTO user_info (u_id,u_name,u_mail,u_id_card,u_phone,u_password,u_role) VALUES (005,'张五','zhangwu@openGauss.com','2023005','12345678905','openGauss_005',5);
INSERT INTO supplier_info(supplier_id,supplier_name,supplier_address,supplier_phone,supplier_type,remark) VALUES(001,'xxx水果批发商','西安市高陵区xxx','12345678901',1,''); INSERT INTO supplier_info(supplier_id,supplier_name,supplier_address,supplier_phone,supplier_type,remark) VALUES(002,'xxx日用百货批发商','西安市雁塔区xxx','12345678902',2,''); INSERT INTO supplier_info(supplier_id,supplier_name,supplier_address,supplier_phone,supplier_type,remark) VALUES(003,'xxx烟酒饮料批发商','西安市未央区xxx','12345678903',3,''); INSERT INTO supplier_info(supplier_id,supplier_name,supplier_address,supplier_phone,supplier_type,remark) VALUES(004,'xxx零食批发商','西安市碑林区xxx','12345678904',4,''); INSERT INTO supplier_info(supplier_id,supplier_name,supplier_address,supplier_phone,supplier_type,remark) VALUES(005,'xxx柴米油盐酱醋批发商','西安市新城区xxx','12345678905',5,'');
INSERT INTO warehouse_info (warehouse_id,warehouse_address,warehouse_name,warehouse_operator,remark) VALUES(001,'园区100-1','1号仓库',4,''); INSERT INTO warehouse_info (warehouse_id,warehouse_address,warehouse_name,warehouse_operator,remark) VALUES(002,'园区100-2','2号仓库',4,''); INSERT INTO warehouse_info (warehouse_id,warehouse_address,warehouse_name,warehouse_operator,remark) VALUES(003,'园区100-3','3号仓库',4,'');
INSERT INTO sell_list_info(sell_id,sell_date,goods_id,goods_name,goods_number, goods_measurement_unit,sell_goods_amount,remark) VALUES(2023001,'2023-01-16 10:01:00','CS001','食用油',1,'桶',98,''); INSERT INTO sell_list_info(sell_id,sell_date,goods_id,goods_name,goods_number, goods_measurement_unit,sell_goods_amount,remark) VALUES(2023001,'2023-01-16 10:01:00','CS002','猪肉',10,'斤',160,''); INSERT INTO sell_list_info(sell_id,sell_date,goods_id,goods_name,goods_number, goods_measurement_unit,sell_goods_amount,remark) VALUES(2023002,'2023-01-16 11:01:00','CS001','食用油',1,'桶',98,''); INSERT INTO sell_list_info(sell_id,sell_date,goods_id,goods_name,goods_number, goods_measurement_unit,sell_goods_amount,remark) VALUES(2023002,'2023-01-16 11:01:00','CS002','猪肉',10,'斤',160,''); INSERT INTO sell_list_info(sell_id,sell_date,goods_id,goods_name,goods_number, goods_measurement_unit,sell_goods_amount,remark) VALUES(2023003,'2023-01-16 12:01:00','CS001','食用油',1,'桶',98,''); INSERT INTO sell_list_info(sell_id,sell_date,goods_id,goods_name,goods_number, goods_measurement_unit,sell_goods_amount,remark) VALUES(2023003,'2023-01-16 12:01:00','CS002','猪肉',10,'斤',160,'');……
4、 添加约束
ALTER table sell_list_info ADD CONSTRAINT c_sell_goods_amount CHECK (sell_goods_amount >=0);
INSERT INTO sell_list_info(sell_id,sell_date,goods_id,goods_name,goods_number,measurement_unit,sell_goods_amount,remark) VALUE(2023003,'2023-01-16 12:01:00','CS002','猪肉',10,'斤',-160,''); 执行失败,失败原因:new row for relation " sell_list_info" violates check constraint "c_sell_goods_amount "。
5 、 创建视图
create view v_goods_sell_sum as select t1.goods_id,t1.goods_name ,t1.sum_num ,t1.sum_amount ,t2.supplier_id, t2.warehouse_id from (SELECT goods_id ,goods_name ,sum(goods_number) as sum_num ,sum(sell_goods_amount) as sum_amount from sell_list_info where SUBSTR(sell_date,1,10) ='2023-01-16' group by goods_id ,goods_name )t1 left join goods_info t2 ont1.goods_id =t2.goods_id order by sum_num desc
select * from v_goods_sell_sum;
6、创建存储过程
CREATE TABLE sell_list_info_tmp1 ( sell_id int PRIMARY KEY ,sell_date date not null ,goods_id char(20) not null ,goods_name char(20) not null ,goods_number int not null ,goods_ measurement_unit char(10) not null ,sell_goods_amount int not null);……
CREATE TABLE sell_list_info(sell_id int PRIMARY KEY ,sell_date date not null,goods_id char(20) not null ,goods_name char(20) not null ,goods_number int not null ,goods_ measurement_unit char(10) not null ,sell_goods_amount int not null);
CREATE PROCEDURE insert_data() IS BEGININSERT INTO sell_list_info select * from sell_list_info_tmp1; INSERT INTO sell_list_info select * from sell_list_info_tmp2; INSERT INTO sell_list_info select * from sell_list_info_tmp3; INSERT INTO sell_list_info select * from sell_list_info_tmp4; INSERT INTO sell_list_info select * from sell_list_info_tmp5; END;/
CALL insert_data();
7、新建用户并授权访问
CREATE USER user002 IDENTIFIED BY 'openGauss@123';
GRANT SELECT,INSERT ON order_info TO user002;
postgres=#\q
gsql -d retail_db -U user002 -p 26000 –r
select * from order_info;
六、总结
超市进销存管理系统是一个庞大复杂的系统,进销存软件涉及的模块也是非常之多,主要包括前端的交互(销售过程管理)、后端的管理(客户管理、用户管理、货品采购管理、货品入库/出库管理、财务总账管理、维护管理、优化改进管理等等),甚至可以上升到后台整个企业的管理(ERP)。由此可以延伸到整个物理架构设计、逻辑架构设计、数据管理(治理)方案、数据存储策略等。
本文设计仅从最基本的核心表设计出发,使用openGauss数据库,完成最基本的超市进销存管理任务,其他更多细节,欢迎交流。