Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a column is unsigned?

I'm currently trying to list all columns of a specific table and determine if each column is unsigned or not.

Here, an example of my test fixture:

CREATE TABLE ttypes
(
    cbiginteger BIGINT UNSIGNED,
    cinteger INT UNSIGNED,
    csmallinteger SMALLINT UNSIGNED
) ENGINE = InnoDB;

In order to list all columns of a specific table, I have found two possibilities:

SHOW FULL COLUMNS
FROM ttypes;

According to the documentation, this query returns these fields: Field, Type, Null, Default, Extra & Comment. None of them allows me to determine if a column is unsigned or not.

After that, I look at the information_schema.columns which is the base table used by the SHOW COLUMNS query.

SELECT ...
FROM information_schema.columns
WHERE table_name = 'ttypes';

Unfortunately, none of the result fields allows me to determine if a column is unsigned or not.

like image 682
egeloen Avatar asked May 19 '12 11:05

egeloen


People also ask

What is unsigned column?

The “unsigned” in MySQL is a data type. Whenever we write an unsigned to any column that means you cannot insert negative numbers. Suppose, for a very large number you can use unsigned type. The maximum range with unsigned int is 4294967295.

Is Bigint signed or unsigned?

A large integer. The signed range is -9223372036854775808 to 9223372036854775807 . The unsigned range is 0 to 18446744073709551615 . SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE .

What is difference between signed and unsigned in MySQL?

Difference between Signed and Unsigned in MySQLSigned value - variables having signed numerals are capable of storing 0, positive, and negative numbers. Unsigned value - variables having unsigned numerals are capable of storing only 0 and positive numbers.

Can a primary key be unsigned?

TL/DR: Yes, but it almost doesn't matter. Auto-increment always increases, so it will never use negative values. You might as well make it unsigned, and you get twice the range of values.


2 Answers

As far as I can tell, the only place those attributes are stored is in COLUMN_TYPE in INFORMATION_SCHEMA.COLUMNS.

That should be included in the output from SHOW COLUMNS (within Type):

mysql> show columns from ttypes;
+---------------+----------------------+------+-----+---------+-------+
| Field         | Type                 | Null | Key | Default | Extra |
+---------------+----------------------+------+-----+---------+-------+
| cbiginteger   | bigint(20) unsigned  | YES  |     | NULL    |       |
| cinteger      | int(10) unsigned     | YES  |     | NULL    |       |
| csmallinteger | smallint(5) unsigned | YES  |     | NULL    |       |
+---------------+----------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

Unfortunately you'd have to parse out the contents of Type and find unsigned, or not unsigned in there - it doesn't put anything in for signed columns.

like image 126
Simon Avatar answered Oct 25 '22 16:10

Simon


Try this magic:

select COLUMN_NAME,
       COLUMN_TYPE, 
       IS_NULLABLE, 
       IF(COLUMN_TYPE LIKE '%unsigned', 'YES', 'NO') as IS_UNSIGNED 
       from information_schema.COLUMNS where TABLE_NAME='record1'

Output

COLUMN_NAME  COLUMN_TYPE       IS_NULLABLE  IS_UNSIGNED
-----------  ----------------  -----------  -----------
id           int(10) unsigned  NO           YES
recordID     varchar(255)      YES          NO
like image 41
malhal Avatar answered Oct 25 '22 14:10

malhal