简体   繁体   中英

what datatype to use for MySQL unique key/index

Is it ok to use INT as the datatype for the index in MySQL. I'm not sure if this is the best way to go as I fully expect this table to get a lot of entries that will eventually 'overflow' the INT

I doubt something like Facebook uses INT for their index on something such as the wallpost table or something else that would be high traffic / get lots of entries.

Any thoughts?

使用INT( 11 ) UNSIGNED ZEROFILL NOT NULL

An INT can go up to over 2 billion (twice that many if unsigned). If you expect your table to have more rows than that, you can use a BIGINT (which unsigned goes up to 18,446,744,073,709,551,615—probably more than even Facebook needs).

Generally speaking, you have two choices:

A ' natural key ' that reflects the reality of the data you are working with. For example, a telephone number might be a valid primary key for a calling list, but it would not be appropriate at all for, say, a list of health care beneficiaries because many people might share the same phone number in a home. Note that a natural key might be made up of more than one field. For example, the combination of Make, Model, and Year might be a key for a list of automobile models.

A ' surrogate key ' on the other hand, is just an arbitrary value that you assign to a row. If you go that route, I'd recommend using a GUID (UUID in MySql). The best way that I know to represent those in MySQL is with a char(36) column. GUIDs are effectively unique forever and can be used infinitely.

If you insist on using a plain old number, then INT is probably fine, or you can use BIGINT to be really sure.

BIGINT UNSIGNED ZEROFILL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM