How does a data table set two or more foreign keys? (The following questions have been updated, see links)

Source: Internet
Author: User
Keywords Php mysql

Current follow-up issues, have been updated, link addresses

I am doing the design of the mall data table, currently has a commodity table goods , commodity attribute table attributes , commodity and Attribute Association table goods_and_attributes :

, the commodity table has been linked with the Commodity Attribute Association table, now I want to set the foreign key in the Commodity Attribute Association table goods_and_attributes attribute_id and the commodity attribute table attributes , the database will error:

I emptied the data and recreated 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 advice, it is a great help to me!

Reply content:

Current follow-up issues, have been updated, link addresses

I am doing the design of the mall data table, currently has a commodity table goods , commodity attribute table attributes , commodity and Attribute Association table goods_and_attributes :

, the commodity table has been linked with the Commodity Attribute Association table, now I want to set the foreign key in the Commodity Attribute Association table goods_and_attributes attribute_id and the commodity attribute table attributes , the database will error:

I emptied the data and recreated 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 advice, it is a great help to me!

Do not use the physical foreign keys of the database to implement and maintain logical foreign keys, especially in high-concurrency Internet environments.

Upstairs. Being a physical foreign key is a very unwise choice.

I have done the project has never done foreign key, this thing no meaning ah, there are many unexpected effects, the general table do point index on the line, not all the situation has, foreign key in most cases, the basic is over-design, there is no need. Think about it. Goods_and_attributes the design of this watch is more realistic, of course, your other table design also ... , creator and date, and status or deteled, you're too simple, like a toy,

  • 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.