3

我有桌子

items
products
brands

他们的内容:

products:
- samsung galaxy s2
- iphone 5

brands
- samsung
- apple

物品和产品的区别如下:

产品可以说是 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`)

因为在视觉布局中它们看起来是这样的: 在此处输入图像描述

4

2 回答 2

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;
于 2013-10-26T08:10:44.813 回答
1

如果您想保留存储在items表中的品牌 ID,则外键约束必须是一个复合约束(并添加一个唯一索引以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 ;
于 2013-10-26T09:34:23.927 回答