Jump to content

[SOLVED] MySQL FAQ?


Trium918

Recommended Posts

Although technically even Data definition language (DDL) is a "query" that's only issued when the table is created.  In selects and updates you would never use unsigned, as the concept of data typing of columns is not usually relevant once the table has been created.

 

MySQL is similar to SQL Server in that it has byte length oriented numeric datatypes.  The types match the number of bytes used to store the value, so in your example, the int type is a 4 bytes (32 bit value).  Simple binary will reveal to you the largest value that can be stored with 32 bits. 

 

By default if you you don't specify at create time, that you want an UNSIGNED int, you will get a signed int, where one of the 32 bits is reserved to indicate whether the value is + or -.  Effectively what this does is cut the maximum value range in half.

 

I your example, you are creating the primary key of a table.  More often than not, people will also define that column to be Auto Increment.  With auto_increment you would never have a value that is minus, so it is a waste of storage to not used UNSIGNED as you lose 1/2 the potential maximum rows.  This means that you would run out of keys at 2,147,483,647 rows rather than 4,294,967,295.

 

Very few databases require that number of rows, so this is largely academic for most people, although the same principle works for smaller data types like the smallint (64k) or tinyint(256) values.  One of the most important decisions you make in mysql design is correct data typing.  The smaller and more efficient the size of an individual row, the more efficient the database will be, all other things being equal.

 

For example, if I have a lookup table that is going to store types of fruit, it's worth asking the question of what is the largest number of fruit I expect to ever have in that table.  In all probability, I will not be able to think of more than 256 different types of fruit, so an UNSIGNED TINYINT is a good type for the primary key of that table.

 

As I relate that table to other tables in my database (think for example of a recipe database where I might indicate fruit and a quantity) that I may have thousands of rows of recipes that involve fruit, however in each one it only costs me 1 byte of storage to indicate the fruit as a foreign key.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.