MySQL Forums :: InnoDB :: Denormalization to allow use UNQIUE index?


Advanced Search

Re: Denormalization to allow use UNQIUE index?
Posted by: Peter Brawley ()
Date: November 12, 2016 01:25PM

It's not a denormalisation issue.

You say the relation of post to comment is 1:many. Then your comment FK is misformed, referencing the comment table instead of the post table.

You need ...

CREATE TABLE post ( 
  id INT UNSIGNED NOT NULL AUTO_INCREMENT, 
  body TEXT NOT NULL, 
  issue_id INT UNSIGNED NOT NULL, 
  PRIMARY KEY (id) 
); 

CREATE TABLE comment ( 
  id int unsigned NOT NULL AUTO_INCREMENT, 
  post_id int unsigned NOT NULL, 
  body text COLLATE utf8_bin NOT NULL, 
  PRIMARY KEY (id), 
  FOREIGN KEY (post_id) REFERENCES post(id) 
);

insert into post set body='abc',issue_id=1;
insert into comment values(1,1,'def'),(2,1,'ghi');

select p.id,p.body as post,p.issue_id,c.id,c.post_id,c.body as comment 
from post p 
join comment c on p.id=c.post_id;
+----+------+----------+----+---------+---------+
| id | post | issue_id | id | post_id | comment |
+----+------+----------+----+---------+---------+
|  1 | abc  |        1 |  1 |       1 | def     |
|  1 | abc  |        1 |  2 |       1 | ghi     |
+----+------+----------+----+---------+---------+

Options: ReplyQuote


Subject Views Written By Posted
Denormalization to allow use UNQIUE index? 493 Victor Porton 11/11/2016 09:34AM
Re: Denormalization to allow use UNQIUE index? 249 Peter Brawley 11/11/2016 11:49AM
Re: Denormalization to allow use UNQIUE index? 242 Victor Porton 11/11/2016 01:02PM
Re: Denormalization to allow use UNQIUE index? 242 Peter Brawley 11/12/2016 01:25PM
Re: Denormalization to allow use UNQIUE index? 327 Victor Porton 11/12/2016 02:14PM
Re: Denormalization to allow use UNQIUE index? 235 Rick James 11/12/2016 05:38PM
Re: Denormalization to allow use UNQIUE index? 223 Peter Brawley 11/12/2016 06:30PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.