English 中文(简体)
利用外国钥匙删除多个表格中的条目
原标题:Using foreign keys to delete entries in multiple tables
  • 时间:2012-01-12 16:13:28
  •  标签:
  • mysql

The following setup will:

  • Allow me to delete existing entries
  • Prevent me from creating new entries in products

这可能是因为条目是在<编码>产品之前在<编码>上设定的。 但是,我不能在<条码>类别中编造条目。 在此之前,我知道该产品。

What would you do?

ALTER TABLE products
ADD CONSTRAINT FK_products
FOREIGN KEY (id) REFERENCES categories_products(product_id)
ON UPDATE CASCADE
ON DELETE CASCADE;

<>可见<>

categories:
id, name

categories_products:
category_id, product_id

products:
id, name

www.un.org/Depts/DGACM/index_spanish.htm 每个入境点的顺序为。

  1. Create an entry in categories
  2. Create an entry in product
  3. Using the IDs from 1. and 2. to create a relationship between categories and products
问题回答

我认为,你的制约因素落后。 该外国钥匙应列入<编码>类别——产品表格,并注明<编码>产品表,而不是以下表格:

ALTER TABLE categories_products
ADD CONSTRAINT FK_products
FOREIGN KEY (product_id) REFERENCES products(id)
ON DELETE CASCADE;

页: 1

  • Each product can have many categories.
  • Each category can have many products.

因此,你应当增加两个支持这种关系的关键。 这些外国人必须列入产品类别表:

ALTER TABLE categories_products
  ADD CONSTRAINT fk_products FOREIGN KEY (product_id)
    REFERENCES products(id),
  ADD CONSTRAINT fk_categories FOREIGN KEY (category_id)
    REFERENCES categories(id);

然后,删除类别和相关产品时,采用同样的问题:

SET FOREIGN_KEY_CHECKS = 0;

DELETE c, cp, p
  FROM category c
    JOIN categories_products cp
      ON cp.category_id = c.id
    JOIN products p
      ON p.id = cp.product_id
WHERE
  c.id = 100;

SET FOREIGN_KEY_CHECKS = 1;

您也不妨加上“儿童成长”条款,这将有助于你从母系中删除行文,并自动删除儿童表中的相应行文。 例如,下个查询将删除<编码>分类-产品上的自动相关记录。 表:

DELETE FROM category id = 100;




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签