Domanda

I am preparing for an exam and I decided to do some example tasks I made up but I am stuck on one particular. Relationships. I have been hitting my head against the wall of SQL for 3-4 hours with little progress. I have 2 tables in which I want to create a one to one and a one to many relationships.

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);

I want to make it so that one article has a one to one with article, then to make it so article has a one to many with category. I can't do the one to one to even try the one to many. Can you please give me some insight as to how to make it happen and how the relationships will work?

Here is the information from inside the database -

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    |       |
+-------------+-------------+------+-----+---------+-------+

And this is the error I get for creating the foreign key:

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`))
È stato utile?

Soluzione

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);

This is a relationship one to one.

I'm saying here that the primary key of the tag is the same from article.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top