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

时间:2014-07-13 10:32:07

标签: mysql database database-design phpmyadmin

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

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

客户:将有电子邮件,用户名和密码......

Customers_Session:以哈希

存储有关客户会话的信息

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

类别:产品的类别类型

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

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

Product_Variation:有关产品图片,产品的各种颜色或款式的信息。

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

Customer_Reviews:客户对产品的评论。

已审核的产品:此表是根据Product表和Review Table之间的多对多关系创建的。

有序产品:此表是根据产品和订单表之间的多对多关系创建的。

基于以上所述,我提出了以下的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表和任何其他需要外键的后续表时,就会发生这种情况。

我的问题是: 你会建议用这种方式设计数据库吗? 2.为什么我得到"无法添加外键约束"错误?

感谢。

1 个答案:

答案 0 :(得分:1)

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

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

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

应该是:

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