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.
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.
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 .
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With