English 中文(简体)
问题理解 * E/CN.6/2009/1。
原标题:Problems understanding FOREIGN KEY / CASCADE constraints

我需要一些帮助了解外国钥匙和连锁店是如何运作的。 我理解这一理论,但我有麻烦,难以把这些理论应用到真正的世界范例。

请允许我假设以下表格(以及可参考表格<代码>的编号/代码>的任意数目):

CREATE TABLE tags (
    id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(255) UNIQUE
) Engine=InnoDB;

CREATE TABLE news (
    id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    title VARCHAR(63),
    content TEXT,
    INDEX (title)
) Engine=InnoDB;

因此,我再造一个表格,以提供<条码>、和<条码>之间的许多对手关系:

CREATE TABLE news_tags (
    news_id INT UNSIGNED,
    tags_id INT UNSIGNED,
    FOREIGN KEY (news_id) REFERENCES news (id) ON DELETE ...,
    FOREIGN KEY (tags_id) REFERENCES tags (id) ON DELETE ...
) Engine=InnoDB;

我对学院的要求:

  • If I delete a news, all corresponding entries in news_tags should be removed as well.
  • Same applies for table x that may be added later with x_tags-table.
  • If I delete a tag, all corresponding entries in news_tags and in every further table x_tags should be removed as well.

我恐怕我可能不得不为此目的重新讨论我的桌子结构,但那是荒谬的,因为我只想说什么是徒劳的。

赞赏与良好教学、地理结构或JPA-examples的任何联系!

最佳回答

你似乎提出这样的建议,这对我来说是合理的:

CREATE TABLE tags 
(
 id INTEGER NOT NULL, 
 name VARCHAR(20) NOT NULL, 
 UNIQUE (id), 
 UNIQUE (name)
);

CREATE TABLE news 
(
 id INTEGER NOT NULL, 
 title VARCHAR(30) NOT NULL,
 content VARCHAR(200) NOT NULL,
 UNIQUE (id)
);

CREATE TABLE news_tags
(
 news_id INTEGER NOT NULL, 
 tags_id INTEGER NOT NULL, 
 UNIQUE (tags_id, news_id), 
 FOREIGN KEY (news_id) 
    REFERENCES news (id)
    ON DELETE CASCADE
    ON UPDATE CASCADE, 
 FOREIGN KEY (tags_id) 
    REFERENCES tags (id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
);
问题回答

暂无回答




相关问题
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 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签