mysql - 3个表相互依赖时外键的工作方式

标签 mysql sql

我有 table

items
products
brands

它们的内容:

products:
- samsung galaxy s2
- iphone 5

brands
- samsung
- apple

item和product的区别如下:

产品可以说是 iPhone。
item 是特定用户的特定 iPhone,具有自己的属性,例如颜色和购买价格。

产品 iPhone 具有 Apple 的品牌/制造商。

当插入一个新商品时,我希望数据库从该商品所属的产品中获取品牌,所以我的外键是这样设置的:

'db_name`.'products'.`productBrand`

我有两个品牌的 ATM - 三星和苹果。

当我尝试通过 phpMyAdmin 的界面插入一个新项目并进入 itemBrand 列时,下拉字段只允许我一个选项 - 1 (Samsung),无论我选择的是产品 1 还是 2 (Samsung Galaxy或 iPhone5) 在 itemGenericProduct 列。

我做错了什么?

这里有一些更详细的信息:

CREATE TABLE IF NOT EXISTS `brands` (
  `brandId` int(11) NOT NULL AUTO_INCREMENT,
  `brandName` varchar(30) NOT NULL,
  `brandLogo` varchar(100) NOT NULL,
  `brandDescription` text NOT NULL,
  PRIMARY KEY (`brandId`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `brands`
--

INSERT INTO `brands` (`brandId`, `brandName`, `brandLogo`, `brandDescription`) VALUES
(1, 'Samsung', '', 'Manufacturer of ...'),
(2, 'Apple', '', 'American high-tech company ...');


--
-- Table structure for table `items`
--

CREATE TABLE IF NOT EXISTS `items` (
  `itemId` int(11) NOT NULL AUTO_INCREMENT,
  `generalProductId` int(11) NOT NULL,
  `itemPurchasedPrice` double NOT NULL,
  `itemDateAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `itemDescription` text,
  `itemBrand` int(11) NOT NULL,
  `itemBoughtFromPlace` int(11) NOT NULL,
  `itemBoughtFromUser` int(11) NOT NULL,
  `itemConditionNew` tinyint(1) NOT NULL DEFAULT '1',
  `itemBeingSold` tinyint(1) NOT NULL DEFAULT '1',
  `itemSellPrice` double DEFAULT NULL,
  PRIMARY KEY (`itemId`),
  KEY `generalProductId` (`generalProductId`),
  KEY `itemBrand` (`itemBrand`),
  KEY `itemBoughtFromPlace` (`itemBoughtFromPlace`),
  KEY `itemBoughtFromUser` (`itemBoughtFromUser`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

--
-- Table structure for table `products`
--

CREATE TABLE IF NOT EXISTS `products` (
  `productId` int(11) NOT NULL AUTO_INCREMENT,
  `productName` varchar(200) NOT NULL,
  `productTimeAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `productDescription` text,
  `productBrand` int(11) NOT NULL,
  `productFirstAddedFrom` int(11) NOT NULL,
  `productAvatar` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`productId`),
  KEY `productBrand` (`productBrand`,`productFirstAddedFrom`),
  KEY `productFirstAddedFrom` (`productFirstAddedFrom`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;

    --
    -- Dumping data for table `products`
    --

    INSERT INTO `products` (`productId`, `productName`, `productTimeAdded`, `productDescription`, `productBrand`, `productFirstAddedFrom`, `productAvatar`) VALUES
    (3, 'Samsung Galaxy SII', '2013-10-26 07:46:08', 'The Samsung Galaxy S II is a .....', 1, 1, NULL),
    (4, 'iPhone 5', '2013-10-26 07:46:08', 'The iPhone 5 is a ....', 1, 2, NULL);

编辑: PRODUCTS 表中的以下几行看起来很奇怪

 KEY `productBrand` (`productBrand`,`productFirstAddedFrom`),
  KEY `productFirstAddedFrom` (`productFirstAddedFrom`)

因为在视觉布局中它们看起来是这样的: enter image description here

最佳答案

外键必须指向另一个表的列(必须相同(例如:INT(11) - INT(11)))。 创建表后,您可以使用

添加外键
ALTER TABLE mytable ADD FOREIGN KEY (myfkey) REFERENCES myothertable(parentkey)

现在,如果我们将它应用到您的结构中:

DROP TABLE items; DROP TABLE brands; DROP TABLE products;
CREATE TABLE IF NOT EXISTS `brands` (
`brandId` int(11) NOT NULL AUTO_INCREMENT,
`brandName` varchar(30) NOT NULL,
 `brandLogo` varchar(100) NOT NULL,
 `brandDescription` text NOT NULL,
 PRIMARY KEY (`brandId`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

INSERT INTO `brands` (`brandId`, `brandName`, `brandLogo`, `brandDescription`) VALUES
(1, 'Samsung', '', 'Manufacturer of ...'),
(2, 'Apple', '', 'American high-tech company ...');

CREATE TABLE IF NOT EXISTS `items` (
`itemId` int(11) NOT NULL AUTO_INCREMENT,
`generalProductId` int(11) NOT NULL,
`itemPurchasedPrice` double NOT NULL,
`itemDateAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`itemDescription` text,
`itemBrand` int(11) NOT NULL,
`itemBoughtFromPlace` int(11) NOT NULL,
`itemBoughtFromUser` int(11) NOT NULL,
 `itemConditionNew` tinyint(1) NOT NULL DEFAULT '1',
 `itemBeingSold` tinyint(1) NOT NULL DEFAULT '1',
 `itemSellPrice` double DEFAULT NULL,
   PRIMARY KEY (`itemId`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

  CREATE TABLE IF NOT EXISTS `products` (
   `productId` int(11) NOT NULL AUTO_INCREMENT,
  `productName` varchar(200) NOT NULL,
   `productTimeAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `productDescription` text,
  `productBrand` int(11) NOT NULL,
   `productFirstAddedFrom` int(11) NOT NULL,
   `productAvatar` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`productId`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;

INSERT INTO `products` (`productId`, `productName`, `productTimeAdded`, `productDescription`, `productBrand`, `productFirstAddedFrom`, `productAvatar`) VALUES
(3, 'Samsung Galaxy SII', '2013-10-26 07:46:08', 'The Samsung Galaxy S II is a .....', 1, 1, NULL),
(4, 'iPhone 5', '2013-10-26 07:46:08', 'The iPhone 5 is a ....', 1, 2, NULL);

          ALTER TABLE items ADD FOREIGN KEY(generalProductId) REFERENCES products(productId) ON DELETE CASCADE      ON UPDATE CASCADE;
  ALTER TABLE products ADD FOREIGN KEY(productBrand) REFERENCES brands(brandId) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE items ADD FOREIGN KEY(itemBrand) REFERENCES product(productBrand) ON DELETE CASCADE ON UPDATE CASCADE;

关于mysql - 3个表相互依赖时外键的工作方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19604440/

相关文章:

mysql - 一个简单的选择返回一个空表

php - 在 php/mysql 中左加入一个带有数组的列

php - 显示包含 2 个输入字段条件的表格

sql - Oracle 中的 SELECT *, ROW_NUMBER() OVER

php - 两表多次删除

php - PDO 和 SSH2 隧道

sql - MySQL count() 问题

php - 如何对 MySQL 数据库中的行进行排序

SQL 命令.ExecuteReader vb.net

sql - 查询匹配字符串标签