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

Mysql 当有3个表相互依赖时外键的工作方式,mysql,sql,Mysql,Sql,我有桌子 items products brands 其内容: products: - samsung galaxy s2 - iphone 5 brands - samsung - apple 项目和产品之间的区别如下: 产品就是iPhone。 商品是特定用户的特定iPhone,具有自己的属性,如颜色和购买价格。 iPhone产品的品牌/制造商为苹果 插入新项目时,我希望数据库从项目所属的产品中获取品牌,因此我的外键设置如下: 'db_name`.'products'.`productB

我有桌子

items
products
brands
其内容:

products:
- samsung galaxy s2
- iphone 5

brands
- samsung
- apple
项目和产品之间的区别如下:

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

iPhone产品的品牌/制造商为苹果

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

'db_name`.'products'.`productBrand`
我有两个ATM品牌——三星和苹果

当我尝试通过phpMyAdmin的界面插入一个新项目,并进入itemBrand列时,下拉字段只允许我选择一个选项-1(三星),无论我在itemGenericProduct列选择了产品1或2(三星Galaxy或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);
编辑: 产品表中的以下几行似乎很奇怪

 KEY `productBrand` (`productBrand`,`productFirstAddedFrom`),
  KEY `productFirstAddedFrom` (`productFirstAddedFrom`)
因为在视觉布局中,它们看起来是这样的:

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

如果您希望将brandID存储在
items
表中,则外键常量必须是复合的(并且为
产品
添加一个唯一的索引以使其工作):

品牌

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 ...');
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);
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 ;
产品

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 ...');
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);
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 ;
项目

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

我不知道你的意思:(这是已经发生的事情,还是为了解决这个问题我必须做的事情?发布架构,这就是我的意思:D如果使用PHPMyAdmin go to export来转储架构视图。再次更新。请注意,外键必须显式指向其他表和指定列。我想您会遇到同样的问题h每个键都已创建。您可能需要查看此项,因为删除时有限制指令。您可以执行“更新”例如,第一个SQL必须按如下方式运行:更改表项添加外键…但首先必须在GeneralProductID上取消外键声明,并使用正确的外键添加整个架构。请注意,我省略了“BoughtFromUser”还有像这样的键,因为它们似乎不能引用某些东西,您可能会在稍后创建用户时添加它们,但实际上没有可引用的列。不,我不同意答案。不应该有来自
项目的直接外键
引用
品牌
。只有(间接的)一到
产品
。如果确实变得困难,请通过PhpMyAdmin修改所有约束并推迟“限制”,然后逐个删除表。否则,删除数据库;创建数据库*****;:D