在MySQL中创建表之间的一对一和一对多关系

时间:2014-04-01 16:22:54

标签: mysql sql database-design foreign-key-relationship

我正在准备考试,我决定做一些我编写的示例任务,但我被困在一个特定的问题上。关系。我一直在SQL的墙上打了3-4个小时,但收效甚微。我有2个表,我想在其中创建一对一和一对多的关系。

CREATE TABLE article (
    price INT(30) NOT NULL,
    published_on DATE NOT NULL
);

CREATE TABLE tag(
    descption VARCHAR(30) NOT NULL,
    priority INT(30) NOT NULL
);

CREATE TABLE category(
    date_created_on DATE NOT NULL,
    name VARCHAR(30) NOT NULL
);

INSERT INTO article VALUES (10.0, "2001-01-01", 0);
INSERT INTO article VALUES (20.0, "1992-05-08", 0);

INSERT INTO tag VALUES ("wtf", 1, 1);
INSERT INTO tag VALUES ("is this", 2, 2);

ALTER TABLE article ADD article_id INT(30) NOT NULL;
ALTER TABLE article ADD PRIMARY KEY(article_id);
ALTER TABLE article MODIFY article_id AUTO_INCREMENT;

ALTER TABLE tag ADD tag_id INT(30) NOT NULL;
ALTER TABLE tag ADD PRIMARY KEY(tag_id);
ALTER TABLE tag MODIFY tag_id AUTO_INCREMENT;

ALTER TABLE tag ADD FOREIGN KEY (tag_id) REFERENCES (article_id);

我想这样做,以便一篇文章与文章一对一,然后使文章具有一对多的类别。我无法做到一对一甚至尝试一对一。你能否就如何实现它以及这种关系如何发挥作用给我一些见解?

以下是数据库内部的信息 -

mysql> select * from Tag;
+-------------+----------+--------+
| description | priority | tag_id |
+-------------+----------+--------+
| wtf         |        1 |      1 |
| is this     |        2 |      2 |
+-------------+----------+--------+

mysql> select * from Article;
+-------+--------------+------------+
| price | published_on | article_id |
+-------+--------------+------------+
|    10 | 2001-01-01   |          7 |
|    20 | 1992-05-08   |          8 |
+-------+--------------+------------+

mysql> describe Article;
+--------------+---------------+------+-----+---------+----------------+
| Field        | Type          | Null | Key | Default | Extra          |
+--------------+---------------+------+-----+---------+----------------+
| price        | decimal(30,0) | YES  |     | NULL    |                |
| published_on | date          | YES  |     | NULL    |                |
| article_id   | int(30)       | NO   | PRI | NULL    | auto_increment |
+--------------+---------------+------+-----+---------+----------------+

mysql> describe Tag;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| description | varchar(30) | NO   |     | NULL    |       |
| priority    | int(30)     | NO   |     | NULL    |       |
| tag_id      | int(30)     | NO   | PRI | NULL    |       |
+-------------+-------------+------+-----+---------+-------+    mysql> select * from Tag;
+-------------+----------+--------+
| description | priority | tag_id |
+-------------+----------+--------+
| wtf         |        1 |      1 |
| is this     |        2 |      2 |
+-------------+----------+--------+

mysql> select * from Article;
+-------+--------------+------------+
| price | published_on | article_id |
+-------+--------------+------------+
|    10 | 2001-01-01   |          7 |
|    20 | 1992-05-08   |          8 |
+-------+--------------+------------+

mysql> describe Article;
+--------------+---------------+------+-----+---------+----------------+
| Field        | Type          | Null | Key | Default | Extra          |
+--------------+---------------+------+-----+---------+----------------+
| price        | decimal(30,0) | YES  |     | NULL    |                |
| published_on | date          | YES  |     | NULL    |                |
| article_id   | int(30)       | NO   | PRI | NULL    | auto_increment |
+--------------+---------------+------+-----+---------+----------------+

mysql> describe Tag;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| description | varchar(30) | NO   |     | NULL    |       |
| priority    | int(30)     | NO   |     | NULL    |       |
| tag_id      | int(30)     | NO   | PRI | NULL    |       |
+-------------+-------------+------+-----+---------+-------+

这是我创建外键的错误:

mysql> ALTER TABLE Tag ADD FOREIGN KEY(tag_id) REFERENCES Article(article_id);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`exam_example`.`#sql-2836_1`,
CONSTRAINT `#sql-2836_1_ibfk_1` FOREIGN KEY (`tag_id`) REFERENCES `Article` (`article_id`))

1 个答案:

答案 0 :(得分:1)

CREATE TABLE article (
    price INT(30) NOT NULL,
    published_on DATE NOT NULL,
    article_id INT(30) NOT NULL,
    CONSTRAINT pk_article_id PRIMARY KEY(article_id)
);

CREATE TABLE tag(
    descption VARCHAR(30) NOT NULL,
    priority INT(30) NOT NULL,
    tag_id INT(30) NOT NULL,
    CONSTRAINT pk_tag_article PRIMARY KEY(tag_id), 
    CONSTRAINT fk_tag_article FOREIGN KEY(tag_id) REFERENCES article(article_id)
);

INSERT INTO article VALUES (10.0, "2001-01-01", 0);
INSERT INTO article VALUES (20.0, "1992-05-08", 1);

INSERT INTO tag VALUES ("wtf", 1, 1);
INSERT INTO tag VALUES ("is this", 2, 2);

这是一对一的关系。

我在这里说标签的主键与文章相同。