所以我有一个由不同PC组件,客户表和销售表组成的数据库。我正在努力研究如何实现它,所以当我添加供应商表时,可以根据最便宜的价格为销售商品选择不同的价格
CREATE TABLE IF NOT EXISTS `mydb`.`customer` (
`customer_id` INT(11) NOT NULL,
`customer_first_name` VARCHAR(45) NULL DEFAULT NULL,
`customer_last_name` VARCHAR(45) NULL DEFAULT NULL,
PRIMARY KEY (`customer_id`))
CREATE TABLE IF NOT EXISTS `mydb`.`sale` (
`sale_id` INT NOT NULL,
`sale_items` INT(3) NOT NULL,
`sale_paid` TINYINT(1) NULL DEFAULT NULL,
`customer_customer_id` INT(11) NOT NULL,
PRIMARY KEY (`sale_id`, `customer_customer_id`),
INDEX `fk_sale_customer1_idx` (`customer_customer_id` ASC),
CONSTRAINT `fk_sale_customer1`
FOREIGN KEY (`customer_customer_id`)
REFERENCES `mydb`.`customer` (`customer_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
我猜我需要供应商表vendor_id
和vendor_name
,但我会在哪里为每个6个组件表的供应商定价cpu
,{ {1}}等等,我如何制作它,以便在销售时,它从最便宜的供应商那里拉出来,并显示它是哪一个?
我在这里有点新手,所以这就是我要求这么多帮助的原因
答案 0 :(得分:1)
我认为,有6个单独的表可以解决有关不同实体的基本相同的信息,这是您问题的主要原因。
许多年前,我研究过一个记录工业过程样本数据的系统,我认为基本原理是一样的。 程序逻辑 无需区分一种类型组件的特性和另一种类型的特性,或同一类型的不同特性之间的特性,因此您可以将整个组件表集抽象为单个"规范"实体。
这个问题的经典解决方案是使用以下内容:
components (
component_id primary key
type references component_types
description
etc.)
vendors (
vendor_id primary key,
name
address
etc.)
vendor_catalog (
vendor_id references vendors
component_id references components
vendor_catalog_no
price
etc.
pk(vendor_id, component_id))
characteristics (
specification_id primary key
spec_name
type references component_types
description
units
lower_limit
upper_limit
etc.
specifications (
specification_id references characteristics
component_id references components
vendor_id references vendors
value
pk(component_id, vendor_id, specification_id)
index(component_id, specification_id) not unique
以上必然是不完整的;我留给你填写数据类型,其他字段,& c。以及component_types
查找表。
对于某些外键,存在同等有效的备用选择,specifications
上的额外非唯一索引是早期优化,这可能是不必要的。也。整个图式是我在当下激起的东西,不应该被视为福音。