Set up multiple picture table Good_pic (the Product ID in) is a foreign key. Refer to the ID in the Goods commodity table when deleting the item. Delete multiple pictures together.

Source: Internet
Author: User
#商品id primary Table

CREATE TABLE ' goods ' (
' ID ' INT (one) UNSIGNED auto_increment not NULL,
' Data ' VARCHAR not NULL DEFAULT ',
PRIMARY KEY (' id ')
) Type=innodb;

#商品多图片从表

CREATE TABLE ' Good_pic ' (
' ID ' INT (one) UNSIGNED auto_increment not NULL,
' Goodid ' INT (one) UNSIGNED not NULL DEFAULT ' 0 ',
' Pic ' VARCHAR not NULL DEFAULT ',
PRIMARY KEY (' id '),
INDEX (' Goodid '),
FOREIGN KEY (' goodid ') REFERENCES ' goods ' (' ID ') on DELETE CASCADE
) Type=innodb;

INSERT into ' goods '
(' id ', ' data ')
VALUES
(' 1 ', ' Audi '),
(' 2 ', ' Changan '),
(' 3 ', ' Land Rover ');

INSERT into ' good_pic '
(' id ', ' goodid ', ' pic ')
VALUES
(' 1 ', ' 1 ', ' ehjhg.jpg '),
(' 2 ', ' 1 ', ' wqer.jpg '),
(' 3 ', ' 1 ', ' 1wer.jpg '),
(' 4 ', ' 2 ', ' qwer.jpg '),
(' 5 ', ' 2 ', ' fgd.jpg '),
(' 6 ', ' 2 ', ' asdf.jpg '),
(' 7 ', ' 3 ', ' ed.jpg '),
(' 8 ', ' 3 ', ' sdfew.jpg '),
(' 9 ', ' 3 ', ' ewe.jpg ');

Delete from goods where id=2;


The Product ID cannot be modified because the picture table is set to a foreign key (Specify a field of its own, with the same type). Refer to the ID in the Product goods table, when deleted
FOREIGN KEY (' goodid ') REFERENCES ' goods ' (' ID ') on DELETE CASCADE
So also can not directly delete the commodity table, you may first delete pic picture table foreign key. or delete the entire pic picture table, so there is no restriction to the commodity table
DROP TABLE Goods


Set the multiple picture table Good_pic (good_id in) as a foreign key. Refer to the ID in the Goods commodity table when deleting the item. Delete multiple pictures together.


01 Product and picture table engine must be set to InnoDB to function
02 Picture table. Commodity ID field type. Copy the type of commodity ID (must be the same)
03 Picture table must be indexed index (' Goodid '),
FOREIGN KEY (' goodid ') REFERENCES ' goods ' (' ID ') on DELETE CASCADE

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.