设计电子商务数据库和mysql错误

Posted

技术标签:

【中文标题】设计电子商务数据库和mysql错误【英文标题】:Designing E-commerce database and mysql error 【发布时间】:2014-09-03 11:53:56 【问题描述】:

我正在尝试从头开始制作一个电子商务网站。目前我正在尝试制作数据库。

这些是数据库将拥有的核心表:

客户:将拥有电子邮件、用户名、密码......

Customers_Session:在哈希中存储有关客户会话的信息

组:基本上告诉客户将拥有哪些权限

类别:产品的类别类型

产品:有关产品的信息,例如名称和描述...

Product_Price:产品的价格信息。这将存储在不同时间为每种产品设置的不同价格。

Product_Variation:关于产品图像的信息,以及产品的各种颜色或样式。

Customer_Orders:客户订购了哪些产品。

Customer_Reviews:客户对产品的评论。

Reviewed Products:此表是根据 Product 表和 Review 表之间的多对多关系创建的。

Ordered products:此表是根据 Product 和 Orders 表之间的多对多关系创建的。

基于以上,我想出了下面的sql代码:

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT ,
  `username` varchar(30) NOT NULL,
  `password` varchar(64) NOT NULL,
  `first_name` varchar(50) NOT NULL,
  `last_name` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `address` varchar(320) NOT NULL,
  `zip` mediumint(5) NOT NULL,
  `salt` varchar(40) NOT NULL,
  `joined` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `group` tinyint(1) NOT NULL,
  `dob` date NOT NULL,
  `pic_url` varchar(225) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

--


--
-- Table structure for table `groups`
--

CREATE TABLE IF NOT EXISTS `groups` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `permissions` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


--
-- This is for category table
--
CREATE TABLE IF NOT EXISTS `category`(
  `category_id` tinyint(3) NOT NULL AUTO_INCREMENT,
  `category_type` varchar(100) NOT NULL,
  `category_description` varchar(160) NOT NULL,
  PRIMARY KEY (`category_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--


--
-- This is for product table
--
CREATE TABLE IF NOT EXISTS `products`(
  `product_id` int(11) NOT NULL AUTO_INCREMENT,
  `product_name` varchar(100) NOT NULL UNIQUE,
  `product_description` varchar(160) NOT NULL,
  `quantity` int(5) NOT NULL,
  `product_code` varchar(4) NOT NULL,
  `keywords` varchar(70) NOT NULL,
  `category_id` tinyint(3),
  PRIMARY KEY (`product_id`),
  INDEX (`category_id`),
  CONSTRAINT FOREIGN KEY (`category_id`) REFERENCES category(`category_id`) ON DELETE SET NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--


--


--
-- Table structure for table `bookings`
--

CREATE TABLE IF NOT EXISTS `bookings` (
  `bookings_id` int(11) NOT NULL AUTO_INCREMENT,
  `party_type` varchar(50) NOT NULL,
  `location` varchar(100) NOT NULL,
  `day` varchar(10) NOT NULL,
  `time` smallint(6) NOT NULL,
  `people_count` smallint(6) NOT NULL,
  `booking_name` varchar(50) NOT NULL,
  `booking_email` varchar(50) NOT NULL,
  PRIMARY KEY (`bookings_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='This is for the bookings' AUTO_INCREMENT=1 ;

--
-- Dumping data for table `groups`
--

INSERT INTO `groups` (`id`, `name`, `permissions`) VALUES
(1, 'Administrator', '"admin":1'),
(2, 'Users', '"users":2');

-- 
-- Table Structure for Reviews
--
CREATE TABLE IF NOT EXISTS `customer_reviews`(
  `reviews_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11),
  `rating` int(5) NOT NULL,
  `comment` varchar(160) NOT NULL,
  PRIMARY KEY(`reviews_id`),
  INDEX (`user_id`),
  CONSTRAINT FOREIGN KEY(`user_id`) REFERENCES users(`user_id`) ON DELETE SET NULL
)ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

--

--
-- Table structure for reviewed products This is a many relationshi btw reviews table and product
--

CREATE TABLE IF NOT EXISTS `reviewed_products`(
  `product_id` int(11),
  `reviews_id`int(11),
  PRIMARY KEY(`product_id`,`reviews_id`),
  CONSTRAINT FOREIGN KEY(`product_id`) REFERENCES products(`product_id`) ON DELETE SET NULL,
  CONSTRAINT FOREIGN KEY(`reviews_id`) REFERENCES customer_reviews(`reviews_id`) ON DELETE SET NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='This is for many to many cardinality btw reviews and products' AUTO_INCREMENT=1 ;

--


--
-- Table structure for table `orders`
--

CREATE TABLE IF NOT EXISTS `customer_orders` (
  `order_id` int(11) NOT NULL AUTO_INCREMENT,
  `order_time` int(11) NOT NULL,
  `amount` int(5),
  `confirmation_number` int(,
  `user_id` int(11),
  `product_id` int(11),
  PRIMARY KEY (`order_id`),
  INDEX (`user_id`),
  CONSTRAINT FOREIGN KEY (`user_id`) REFERENCES users(`user_id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

/* We need a new table since the customer_order and product is in a many to many relationship */
--


--
-- Table structure for table `curtomer_order_product`
--
CREATE TABLE IF NOT EXISTS `ordered_product`(
  `product_id` int(11),
  `order_id` int(11),
  `quantity` smallint(5),
  PRIMARY KEY(`product_id`,`order_id`),
  INDEX (`product_id`,`order_id`),
  CONSTRAINT FOREIGN KEY (`order_id`) REFERENCES customer_orders(`order_id`) ON DELETE RESTRICT,
  CONSTRAINT FOREIGN KEY (`product_id`) REFERENCES products(`product_id`) ON DELETE RESTRICT
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--
-- This is for price table
--
CREATE TABLE IF NOT EXISTS `product_price`(
  `price_id` int(11) NOT NULL AUTO_INCREMENT,
  `price` decimal(6,2) NOT NULL,
  `product_id` int(11) NOT NULL,
  PRIMARY KEY (`price_id`),
  INDEX (`product_id`),
  CONSTRAINT FOREIGN KEY (`product_id`) REFERENCES products(`product_id`) 
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--


--
-- Table structure for table `product_variations`
--
CREATE TABLE IF NOT EXISTS `product_variations`(
  `variations_id` int(11) NOT NULL AUTO_INCREMENT,
  `color_name` varchar(10) NOT NULL,
  `color_value` char(6) NOT NULL,
  `product_id` int(11),
  `picture1` varchar(100) NOT NULL,
  `picture2` varchar(100) NOT NULL,
  `picture3` varchar(100) NOT NULL,
  PRIMARY KEY (`variations_id`),
  INDEX (`product_id`),
  CONSTRAINT FOREIGN KEY (`product_id`) REFERENCES products(`product_id`) ON DELETE RESTRICT
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;


--


--
-- Table structure for table `users_session`
--

CREATE TABLE IF NOT EXISTS `users_session` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `hash` varchar(64) NOT NULL,
  PRIMARY KEY (`id`),
  INDEX (`user_id`),
  CONSTRAINT FOREIGN KEY(`user_id`) REFERENCES users(`user_id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

问题是当我尝试在 phpmyadmin 中运行上述代码时,我收到一条错误消息,指出“无法添加外键约束”。这在创建 customer_Reviews 表和任何其他需要外键的后续表时开始发生。

我的问题是: 1. 你会推荐这样设计数据库吗? 2. 为什么会出现“无法添加外键约束”的错误?

谢谢。

【问题讨论】:

至少有一个表引用了user.user_id,但您的users 表只有一个id 列。 专业提示:即使电话号码和邮政编码(有时!)完全是数字,也不要将它们存储为 int 类型。痛苦和痛苦是可能的。经验法则是,除非对其进行数学运算是有效的(你能将两个电话号码相乘吗?加上两个邮政编码?),否则它应该存储为字符串/char/varchar。 【参考方案1】:

要对您的数据库设计进行全面质量审查,您可以尝试Code Review 姊妹网站。乍一看,我没有发现您的数据库设计或 SQL 有任何明显的问题。

外键约束失败,因为您错误地命名了相关列。

 CONSTRAINT FOREIGN KEY(`user_id`) REFERENCES users(`user_id`)

应该是:

 CONSTRAINT FOREIGN KEY(`user_id`) REFERENCES users(`id`)

【讨论】:

感谢两位炼金术士的回复。主要在我这边。我修改了它,它一直有效,直到我尝试添加“reviewed_products”表。它给了我同样的不能添加外键约束。从你的权力来看,我在这方面做错了什么吗?

以上是关于设计电子商务数据库和mysql错误的主要内容,如果未能解决你的问题,请参考以下文章

检查用户名/电子邮件可用性设计问题

MySQL的Docker容器化大规模实践

电子商务网站一般架构都有哪些

电子商务网站一般架构都有哪些

基于php737物流电子商务平台

打怪升级