How to determine if a column is unsigned?
Asked Answered
D

5

16

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.

Driedup answered 19/5, 2012 at 11:49 Comment(1)
What version of MySQL are you using?Hawkshaw
H
8

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.

Hawkshaw answered 19/5, 2012 at 13:24 Comment(0)
Z
9

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
Zeralda answered 20/2, 2015 at 12:45 Comment(0)
H
8

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.

Hawkshaw answered 19/5, 2012 at 13:24 Comment(0)
I
3

To determine type for all variables in a table you can run a query like this:

select COLUMN_NAME,COLUMN_TYPE from information_schema.COLUMNS where TABLE_NAME='ttypes' and COLUMN_TYPE LIKE '%unsigned%' 

After that your can easily determine type for a specific variable (for example the cinterger) with a query like this:

select COLUMN_NAME from information_schema.COLUMNS where TABLE_NAME='ttypes' and COLUMN_TYPE LIKE '%unsigned%' and COLUMN_NAME LIKE 'cinteger'

The above code will return the name of the variable searched only if it is unsigned.

At last your can using a mysql loop, procedure or your favourite scripting language to use this result and/or continue searching other variables.

Inkstand answered 19/5, 2012 at 14:59 Comment(0)
C
0

Just in case somebody stumbles upon this for the MySQL driver in .net, using GetSchema() like me, here is how the unsigned info is available.

_connection.GetSchema("Columns")

enter image description here

And then:

enter image description here

Hope this is not completely out of place for the question, and helps someone looking for programmatically determining the sign.

Condonation answered 27/8, 2018 at 16:28 Comment(0)
D
0

I was using SQLColumns to get the type name through ODBC. This worked with versions of the Connector/ODBC before 8.0.29. Starting with 8.0.29, it no longer includes "unsigned" and I can no longer get the type that way.

Dioptometer answered 11/4, 2023 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.