SQL表结构反馈
我对下面的 sql 表结构不太满意,原因有两个。
我不喜欢在 1020 字节 varchar 变量中列出订单 (orders.itemOrderIds) 中包含的所有商品。我觉得有一种更优雅的方式来保存一堆 ID 号。
我依赖于从项目表中的 itemDescription 变量中提取有关我的 java 代码中的项目(基本上是字段)的信息。
有人对处理这些情况的最佳方法有一些建议吗?我在这个网站上读到,让java代码生成表是可以的。例如,我的 Java 代码将创建一个名为 order_(orderId)_items 的表,该表将保存带有 orderId 的订单中包含的所有项目。这是解决这个问题的最佳方法吗?
CREATE TABLE IF NOT EXISTS customers
(
id INT AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
company VARCHAR(255) NOT NULL,
address1 VARCHAR(255) NOT NULL,
address2 VARCHAR(255),
city VARCHAR(255) NOT NULL,
province_state VARCHAR(255) NOT NULL,
country VARCHAR(255) NOT NULL,
zip VARCHAR(255),
telephone VARCHAR(255),
PRIMARY KEY(id)
) ENGINE = INNODB;
CREATE TABLE IF NOT EXISTS orders
(
id INT AUTO_INCREMENT,
customerId INT NOT NULL,
orderNum VARCHAR(255) NOT NULL,
itemOrderIds VARCHAR(1020) NOT NULL,
regName VARCHAR(255) NOT NULL,
regCompany VARCHAR(255) NOT NULL,
regEmail VARCHAR(255) NOT NULL,
orderTime DATETIME NOT NULL,
FOREIGN KEY(customerId) REFERENCES customers(id),
PRIMARY KEY(id)
) ENGINE = INNODB;
CREATE TABLE IF NOT EXISTS items
(
itemNum VARCHAR(255) UNIQUE,
itemName VARCHAR(255),
itemTypeId SMALLINT NOT NULL,
--itemDescription must hold all information needed to generate license keys in format
-- Field_Name1: Field_Value1, Field_Name2: Field_Value2, .....
-- Field names are platform, version, (choose one: port, voiceName)
itemDescription VARCHAR(1020),
FOREIGN KEY(itemTypeId) REFERENCES item_types(id) ON DELETE CASCADE,
PRIMARY KEY(id)
) ENGINE = INNODB;
CREATE TABLE IF NOT EXISTS item_types
(
id SMALLINT AUTO_INCREMENT,
itemType VARCHAR(255) UNIQUE NOT NULL,
PRIMARY KEY(id)
) ENGINE = INNODB;
CREATE TABLE IF NOT EXISTS item_orders
(
id INT AUTO_INCREMENT,
itemNum VARCHAR(255) NOT NULL,
licenseKey VARCHAR(255) NOT NULL,
FOREIGN KEY(itemNum) REFERENCES items(itemNum),
PRIMARY KEY(id)
) ENGINE = INNODB;
编辑:
CREATE TABLE IF NOT EXISTS Order_Items
(
orderId INT NOT NULL,
itemOrderId INT NOT NULL,
PRIMARY KEY(orderId, itemOrderId)
) ENGINE = INNODB;
I am not too happy with the sql table structure I have below for two reasons.
I do not like that I am listing all items included in an Order (orders.itemOrderIds) in a 1020 byte varchar variable. I feel like there is a more elegant way to hold a bunch of id numbers.
I am relying on extracting information about an item (basically fields) in my java code from the itemDescription variable in the item table.
Does anybody has some advice as to the best way to deal with these situations. I have read on this website that it is okay to have java code generate tables. So for instance my Java code would create a table called order_(orderId)_items that would hold all items included in the order with orderId. Is this the best way to go about this?
CREATE TABLE IF NOT EXISTS customers
(
id INT AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
company VARCHAR(255) NOT NULL,
address1 VARCHAR(255) NOT NULL,
address2 VARCHAR(255),
city VARCHAR(255) NOT NULL,
province_state VARCHAR(255) NOT NULL,
country VARCHAR(255) NOT NULL,
zip VARCHAR(255),
telephone VARCHAR(255),
PRIMARY KEY(id)
) ENGINE = INNODB;
CREATE TABLE IF NOT EXISTS orders
(
id INT AUTO_INCREMENT,
customerId INT NOT NULL,
orderNum VARCHAR(255) NOT NULL,
itemOrderIds VARCHAR(1020) NOT NULL,
regName VARCHAR(255) NOT NULL,
regCompany VARCHAR(255) NOT NULL,
regEmail VARCHAR(255) NOT NULL,
orderTime DATETIME NOT NULL,
FOREIGN KEY(customerId) REFERENCES customers(id),
PRIMARY KEY(id)
) ENGINE = INNODB;
CREATE TABLE IF NOT EXISTS items
(
itemNum VARCHAR(255) UNIQUE,
itemName VARCHAR(255),
itemTypeId SMALLINT NOT NULL,
--itemDescription must hold all information needed to generate license keys in format
-- Field_Name1: Field_Value1, Field_Name2: Field_Value2, .....
-- Field names are platform, version, (choose one: port, voiceName)
itemDescription VARCHAR(1020),
FOREIGN KEY(itemTypeId) REFERENCES item_types(id) ON DELETE CASCADE,
PRIMARY KEY(id)
) ENGINE = INNODB;
CREATE TABLE IF NOT EXISTS item_types
(
id SMALLINT AUTO_INCREMENT,
itemType VARCHAR(255) UNIQUE NOT NULL,
PRIMARY KEY(id)
) ENGINE = INNODB;
CREATE TABLE IF NOT EXISTS item_orders
(
id INT AUTO_INCREMENT,
itemNum VARCHAR(255) NOT NULL,
licenseKey VARCHAR(255) NOT NULL,
FOREIGN KEY(itemNum) REFERENCES items(itemNum),
PRIMARY KEY(id)
) ENGINE = INNODB;
Edit:
CREATE TABLE IF NOT EXISTS Order_Items
(
orderId INT NOT NULL,
itemOrderId INT NOT NULL,
PRIMARY KEY(orderId, itemOrderId)
) ENGINE = INNODB;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,您需要一个
OrderItem
交集表(我通常将父对象放在名称的前面),而不是将所有订单 ID 反规范化到 varchar 字段中。没有办法正确索引这样的字段,并且这使得更新非常困难。阅读数据库规范化,这些是您将找到的第一类示例。
Yes, you need an
OrderItem
intersection table (I usually put the parent object first in the name), rather than denormalizing all of the order IDs into a varchar field. There is no way to properly index a field like this, and it makes updates very difficult.Read up on database normalization, these are the first first types of examples you will find.
最好的方法就是将所有数据结构至少标准化为 3NF。
数据库规范化
目前 - 更好地将订单项目列表存储在单独的表中,该表还包含对订单表。
The best way to do with that - is to normalize all your data structure at least to 3NF.
Database Normalization
As for now - better store list of order items in separate table which also holds a reference to Orders table.
您需要一个订单详细信息表(而不是连接表)。为什么?因为订单信息是特定于特定日期的。因此,您必须按该日期的原样存储信息。因此,订单详细信息通常包括商品编号、商品名称、价格(包含价格至关重要,您需要的是订单出售时的价格,而不是 2 年后的价格。)以及选项的任何详细信息,例如如颜色、订购的商品数量等。在订单表中,您还需要包含有关发货地址的详细信息,以及客户的姓名,因为这些事情也会随着时间的推移而变化。
我还会打破客户表。客户通常可以有多个地址、电话号码和电子邮件,每个地址、电话号码和电子邮件都应该是一个单独的相关表。
You want an order detail table (not a joining table). Why? Because order information is specific to a particular date. So you must store the information as it was at that date. So the order details typically include the item number, the name of the item, the price (critical to include the price, you need the price at the time the order was sold not the price 2 years later.) and any details of options such as color, number of items ordered, etc. In the order table you will also want to include details about what address it was shipped to, and the customer's name as those thigns also change over time.
I woudl also break out the customer table. Customers typically can have multiple addressses, phone number and emails and each should be a separate related table.