Mark Unwin
Mark Unwin

Reputation: 1623

MySQL adding (num) to smallint column

When I create a table in MySQL specifying smallint as a column, but then use show create table or even mysqldump, MySQL has added (5) after the smallint definition, as below.

I'm guessing it doesn't really matter as far as the data is concerned, but can anyone explain why and if/how I can stop it doing this?

As an aside, I am attempting to change an existing database table to exactly match that of a new sql script. I could always alter the new sql script, but I'd prefer to alter the existing table if possible (think software install versus software upgrade).

DROP TABLE IF EXISTS `test`;

CREATE TABLE `test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `status` varchar(100) NOT NULL DEFAULT '',
  `port` smallint unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

SHOW CREATE TABLE test;

CREATE TABLE `test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `status` varchar(100) NOT NULL DEFAULT '',
  `port` smallint(5) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Upvotes: 1

Views: 4617

Answers (3)

Madona wambua
Madona wambua

Reputation: 1428

Try this start adding values in your table.

<mysql> CREATE TABLE test(
->    ID SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
->    Name VARCHAR(100) NOT NULL
-> );>

Upvotes: 0

spencer7593
spencer7593

Reputation: 108370

No, you can't stop the SHOW CREATE TABLE from including the display width attribute for integer types.

If a value for the display width is not included in the column declaration of an integer type, MySQL supplies a default value for it. A value of 5 is the default value for SMALLINT UNSIGNED.

The display width doesn't have any affect on the values that can be stored or retrieved. Client applications can make use of the value for formatting a resultset.

Reference: http://dev.mysql.com/doc/refman/5.6/en/numeric-type-attributes.html

Upvotes: 2

Pepi
Pepi

Reputation: 171

tMySQL is simply setting the (displayed) length of the column to match he data type (max value 65535, five digits). To change this, you can write:

port smallint (3) unsigned NOT NULL DEFAULT '0',

if you like.

Upvotes: 0

Related Questions