我必须实现一个数据库,其中有分类和泛化。存在可以分类为个人,政府或公司的客户实体。此外,存在重叠概括,其中客户可以是电子商务或步入式或两者兼而有之,并且存在明显的概括,其中客户可以是雇员或普通客户。
我已经按如下方式实现了表格:
客户
CREATE TABLE Customer
(
Customer_id CHAR(4),
First_order_date DATE,
PRIMARY KEY(Customer_id)
);
人
CREATE TABLE Person
(
Customer_id CHAR(4),
PRIMARY KEY(Customer_id)
);
公司
CREATE TABLE Company
(
Customer_id CHAR(4),
PRIMARY KEY(Customer_id)
);
电子政务
CREATE TABLE Government
(
Customer_id CHAR(4),
PRIMARY KEY(Customer_id)
);
员工
CREATE TABLE Employee
(
Customer_id CHAR(4),
Employee _cutomer_name CHAR(15),
Employee_discount_rate FLOAT,
PRIMARY KEY(Customer_id) REFERENCES Customer(Customer_id)
);
正
CREATE TABLE Regular
(
Customer_id CHAR(4),
Regular_cutomer_name CHAR(15),
Regular_cutomer_address CHAR(80),
PRIMARY KEY(Customer_id) REFERENCES Customer(Customer_id)
);
走着
CREATE TABLE WalkIn
(
Customer_id CHAR(4),
Walkin_cutomer_name CHAR(15),
Walkin_cutomer_address CHAR(80),
PRIMARY KEY(Customer_id) REFERENCES Customer(Customer_id)
);
电子商务
CREATE TABLE ECommerce
(
Customer_id CHAR(4),
Ecommerce_cutomer_name CHAR(15),
Ecommerce_cutomer_email CHAR(80),
PRIMARY KEY(Customer_id) REFERENCES Customer(Customer_id)
);
如何确保在我的数据库中满足泛化和分类约束?
答案 0 :(得分:1)
我无法完全理解你的问题。如果我必须在我的工作中设计这种数据库。我将使用常规Customer
类进行设置,其中2列区分客户类型。
CREATE TABLE customer
(
customer_id CHAR(4),
type1 ENUM("Person", "Company", "Government"),
type2 ENUM("Employee", "Regular", "WalkIn", "ECommerce"),
first_order_date DATE,
name CHAR(15),
address CHAR(80),
email CHAR(80),
discount_rate FLOAT
);
所有客户都有type1,type2,frst_order_date,名称,地址。但只有ECommerce用户才有电子邮件,只有员工有discount_rate。对于其他客户类型,此值将为NULL。如果这不好,那么option
表可以帮助你。
CREATE TABLE customer
(
customer_id CHAR(4),
type1 ENUM("Person", "Company", "Government"),
type2 ENUM("Employee", "Regular", "WalkIn", "ECommerce"),
first_order_date DATE,
name CHAR(15),
address CHAR(80)
);
CREATE TABLE option_tbl
(
customer_id CHAR(4),
option_name CHAR(10),
option_value VARCHAR(80),
FOREING KEY (customer_id) REFERENCES customer(customer_id)
);
此设计也有所折衷。
ENUM
并不好。参考http://komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/ FLOAT
类型不好,NUMERIC
更好。 NUMERIC
是固定点