Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Since Primary Keys are UNIQUE by default, this makes this relation One to One.</p> </blockquote> <p>No, that makes the relation "one to zero or one". Is that what you actually need?</p> <p>If <strong>yes</strong>, then then your "second solution" is better:</p> <ul> <li>it's simpler,</li> <li>takes less storage<sup>1</sup> (and therefore makes cache "larger")</li> <li>hes less indexes to maintain<sup>2</sup>, which benefits data manipulation,</li> <li>and (since you are using InnoDB) naturally <a href="http://use-the-index-luke.com/sql/clustering/index-organized-clustered-index" rel="noreferrer">clusters</a> the data, so users that are close together will have their accounts stored close together as well, which may benefit cache locality and certain kinds of range scans.</li> </ul> <p>BTW, you'll need to make <code>accounts.id</code> an ordinary integer (not auto-increment) for this to work.</p> <p>If <strong>no</strong>, see below...</p> <blockquote> <p>What is the best way to create One to One relation in MySQL?</p> </blockquote> <p>Well, "best" is an overloaded word, but the "standard" solution would be the same as in any other database: put both entities (user and account in your case) in the same physical table.</p> <blockquote> <p>Are there any other solutions other than these two?</p> </blockquote> <p>Theoretically, you could make circular FKs between the two PKs, but that would require <em>deferred</em> constraints to resolve the chicken-and-egg problem, which are unfortunately not supported under MySQL.</p> <blockquote> <p>And if I import any of these solutions into MySQL Workbench EER diagram, it recognizes relations as One to Many :S Thats also confusing.</p> </blockquote> <p>I don't have much practical experience with that particular modeling tool, but I'm guessing that's because it is "one to many" where "many" side was capped at 1 by making it unique. Please remember that "many" doesn't mean "1 or many", it means "0 or many", so the "capped" version really means "0 or 1".</p> <hr> <p><em><sup>1</sup> Not just in the storage expense for the additional field, but for the secondary index as well. And since you are using InnoDB which <a href="http://www.ovaistariq.net/521/understanding-innodb-clustered-indexes/" rel="noreferrer">always clusters tables</a>, beware that secondary indexes are even more expensive in clustered tables than they are in heap-based tables.</em></p> <p><em><sup>2</sup> InnoDB <a href="http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html" rel="noreferrer">requires indexes</a> on foreign keys.</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload