当有3个表相互依赖时,外键的工作方式

时间:2013-10-26 08:05:03

标签: mysql sql

我有桌子

items
products
brands

他们的内容:

products:
- samsung galaxy s2
- iphone 5

brands
- samsung
- apple

项目与产品之间的差异如下:

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

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

插入新项目时,我希望数据库从项目所属的产品中获取品牌,因此我的外键设置如下:

'db_name`.'products'.`productBrand`

我有两个品牌ATM - 三星和Apple。

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

我做错了什么?

以下是一些更详细的信息:

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

2 个答案:

答案 0 :(得分:3)

外键必须指向另一个表的列(必须相同(例如: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;

答案 1 :(得分:1)

如果要将brandID保存在items表中,则外键constarint必须是复合的(并且将唯一索引添加到products以使其工作):

brands

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 ;

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

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`),
  FOREIGN KEY (productBrand)                                  -- FK added
    REFERENCES brands (brandId),
  UNIQUE (productBrand, productId)                   -- Unique index added
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 ;

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);

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`),
  FOREIGN KEY (itemBrand, generalProductId)            -- composite FK
    REFERENCES products (productBrand, productId)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;