如何确定列是否未签名?

时间:2012-05-19 11:49:03

标签: mysql database-metadata

我目前正在尝试列出特定表的所有列,并确定每列是否未签名。

这里是我的测试夹具的一个例子:

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

为了列出特定表的所有列,我发现了两种可能性:

SHOW FULL COLUMNS
FROM ttypes;

根据documentation,此查询返回以下字段:Field,Type,Null,Default,Extra&评论。它们都不允许我确定列是否未签名。

之后,我查看information_schema.columns这是SHOW COLUMNS查询使用的基表。

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

不幸的是,没有一个结果字段允许我确定列是否未签名。

4 个答案:

答案 0 :(得分:8)

据我所知,这些属性存储的唯一位置是COLUMN_TYPE中的INFORMATION_SCHEMA.COLUMNS

这应该包含在SHOW COLUMNS的输出中(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)

很遗憾,您必须解析Type的内容并在其中找到unsigned或不unsigned - 它不会为签名列添加任何内容。

答案 1 :(得分:4)

要确定表中所有变量的类型,您可以运行如下查询:

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

之后,您可以使用如下查询轻松确定特定变量(例如cinterger)的类型:

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

上面的代码只有在未签名的情况下才会返回搜索变量的名称。

最后你可以使用mysql循环,程序或你最喜欢的脚本语言来使用这个结果和/或继续搜索其他变量。

答案 2 :(得分:4)

试试这个魔法:

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'

输出

COLUMN_NAME  COLUMN_TYPE       IS_NULLABLE  IS_UNSIGNED
-----------  ----------------  -----------  -----------
id           int(10) unsigned  NO           YES
recordID     varchar(255)      YES          NO

答案 3 :(得分:0)

以防万一有人像我一样使用GetSchema()偶然发现.net中的MySQL驱动程序,这是未签名信息的可用方式。

_connection.GetSchema("Columns")

enter image description here

然后:

enter image description here

希望此问题并非完全不合时宜,并且可以帮助寻求编程的人确定路标。