How do I set two or more foreign keys for a data table? (For more information, see the link)

Source: Internet
Author: User
The following questions have been updated: the link address is being designed for the mall data table. Currently, there is a commodity table goods, a commodity Attribute Table attributes, and a commodity and attribute Association Table goods_and_attributes :, the item table has been associated with the item attribute Association table. Now I...

The link address for subsequent issues has been updated.

I am working on the design of the mall data table. Currently, there is a commodity table.goods, Item Attribute Tableattributes, Item and attribute Association Tablegoods_and_attributes:

The item table has been associated with the item attribute Association table. Now I wantgoods_and_attributesSet foreign keyattribute_idAnd item Attribute TableattributesWhen connected, the database reports an error:

I cleared the data and re-created the slq statement:

DROP TABLE IF EXISTS `goods_and_attribute`;CREATE TABLE `goods_and_attribute` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `goods_id` int(11) DEFAULT NULL,  `value` varchar(50) NOT NULL,  `stock` int(11) NOT NULL,  `attribute_id` int(11) NOT NULL,  PRIMARY KEY (`id`),  KEY `IDX_72C0A1A2B7683595` (`goods_id`),  KEY `IDX_20160603` (`attribute_id`) USING BTREE,  CONSTRAINT `FK_good_id_20160519` FOREIGN KEY (`goods_id`) REFERENCES `goods` (`id`)  CONSTRAINT `FK_attribute_id_20160603` FOREIGN KEY (`attribute_id`) REFERENCES `attributes` (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

The error message is as follows:

Error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CONSTRAINT `FK_attribute_id_20160519` FOREIGN KEY (`attribute_id`) REFERENCES `a' at line 1

If you can give me some suggestions, it will be of great help to me!

Reply content:

The link address for subsequent issues has been updated.

I am working on the design of the mall data table. Currently, there is a commodity table.goods, Item Attribute Tableattributes, Item and attribute Association Tablegoods_and_attributes:

The item table has been associated with the item attribute Association table. Now I wantgoods_and_attributesSet foreign keyattribute_idAnd item Attribute TableattributesWhen connected, the database reports an error:

I cleared the data and re-created the slq statement:

DROP TABLE IF EXISTS `goods_and_attribute`;CREATE TABLE `goods_and_attribute` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `goods_id` int(11) DEFAULT NULL,  `value` varchar(50) NOT NULL,  `stock` int(11) NOT NULL,  `attribute_id` int(11) NOT NULL,  PRIMARY KEY (`id`),  KEY `IDX_72C0A1A2B7683595` (`goods_id`),  KEY `IDX_20160603` (`attribute_id`) USING BTREE,  CONSTRAINT `FK_good_id_20160519` FOREIGN KEY (`goods_id`) REFERENCES `goods` (`id`)  CONSTRAINT `FK_attribute_id_20160603` FOREIGN KEY (`attribute_id`) REFERENCES `attributes` (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

The error message is as follows:

Error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CONSTRAINT `FK_attribute_id_20160519` FOREIGN KEY (`attribute_id`) REFERENCES `a' at line 1

If you can give me some suggestions, it will be of great help to me!

Do not use physical Foreign keys of databases. Use programs to implement and maintain logical Foreign keys, especially in highly concurrent Internet environments.

Upstairs positive solution. It is unwise to make a physical foreign key.

I have never done any foreign keys in my projects. It makes no sense. There are still many unexpected effects. Generally, you just need to index a table. Not all situations are involved, in most cases, foreign keys are over-designed and unnecessary. Think about the design of the goods_and_attributes table. Of course, you have designed other tables as well ..., there are no creators and dates, and no status or deteled. It's too easy for you, like toys,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.