开发者

In SQL / MySQL, are there reasons not to put one-to-one relationship in the same table?

开发者 https://www.devze.com 2022-12-28 10:44 出处:网络
One-to-one relationship could usually be stored in the same table.Are there reasons not to store them in the same t开发者_JS百科able?Number and type of columns. There is a limit on the size of the col

One-to-one relationship could usually be stored in the same table. Are there reasons not to store them in the same t开发者_JS百科able?


Number and type of columns. There is a limit on the size of the columns in a table. See here. There is a maximum of 8,060 bytes per row.

Very large tables can also affect performance and can be difficult to optimize and index well.

This is apart from keeping data the is conceptually different, apart from each other. For example, a country and currency have a 1 to 1 relationship (illustrative example, I know this is not always the case). I would still not keep them together.


You'll find some information about when it's useful to create one-to-one relations under http://onlamp.com/pub/a/onlamp/2001/03/20/aboutSQL.html

The most important thing is following:

The key indicator of a possible need for a one-to-one relationship is a table that contains fields that are only used for a certain subset of the records in that table.


I've done this to prevent locking/blocking, put the read heavy columns in one table the update heavy columns in another, worked like a charm. A lot of big fat update transactions were slowing down a lot of reads.


One - to zero-or-one relationships are common and linked from the optional to the mandatory - the example given in http://onlamp.com/pub/a/onlamp/2001/03/20/aboutSQL.html is of this kind, not one-to-one. Type/subtype relations can be implemented like this.

one-to-one relations occur when each represents a clear, meaningful entity, which in a different context may be in some different relationship and where a minor change to the requirements may change the cardinality of the relation. It is arbitrary which links to which so its best to choose one to be optional and convert to one to zero-or-one.

0

精彩评论

暂无评论...
验证码 换一张
取 消