Reputation: 5864
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.
Upvotes: 17
Views: 7843
Reputation: 1
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.
Upvotes: 0
Reputation: 2557
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")
And then:
Hope this is not completely out of place for the question, and helps someone looking for programmatically determining the sign.
Upvotes: 0
Reputation: 30567
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
Upvotes: 9
Reputation: 453
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.
Upvotes: 3
Reputation: 2008
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.
Upvotes: 8