【问题标题】:The way foreign keys work when there are 3 tables dependent on each other当有 3 个相互依赖的表时外键的工作方式
【发布时间】:2013-10-26 08:05:03
【问题描述】:

我有桌子

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(三星),无论我选择产品 1 还是 2(三星 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`)

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

【问题讨论】:

  • 我不明白你的意思:(这是已经发生的事情,还是我必须做的事情才能解决这个问题?
  • 发布模式,这就是我的意思:D 如果你使用 PHPMyAdmin 去导出转储模式视图。

标签: mysql sql


【解决方案1】:

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

【讨论】:

  • 再次更新。请注意, FOREIGN KEY 必须显式指向另一个表和指定的列。我认为创建的每个密钥都会遇到同样的问题。你可能想看看这个dev.mysql.com/doc/refman/5.1/en/…
  • 这是因为 RESTRICT 指令 ON DELETE。例如,您可以执行“更新”。第一个 SQL 必须按如下方式运行:ALTER TABLE items ADD FOREIGN KEY... 但首先您必须取消声明您在 generalProductId 上的外键
  • 使用正确的外键添加了整个架构。请注意,我省略了“BoughtFromUser”和这样的键,因为它们似乎无法引用某些内容,您可能会在稍后创建用户时添加它们,但实际上没有可引用的列。
  • 不,我不同意这个答案。应该有来自items 的直接外键引用brands。只有(间接)一个到products
  • 如果真的很难,通过 PhpMyAdmin 修改所有约束并推迟“RESTRICT”,然后一张一张地删除表。否则,删除数据库;创建数据库 *****; :D
【解决方案2】:

如果您想将brandID 保存在items 表中,则外键约束必须是一个复合的约束(并且要在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 ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 2011-04-26
    相关资源
    最近更新 更多