题
我目前正在尝试列出特定表的所有列,并确定每列是否无符号。
这里,我的测试夹具的一个例子:
CREATE TABLE ttypes
(
cbiginteger BIGINT UNSIGNED,
cinteger INT UNSIGNED,
csmallinteger SMALLINT UNSIGNED
) ENGINE = InnoDB;
为了列出特定表的所有列,我发现了两种可能性:
SHOW FULL COLUMNS
FROM ttypes;
根据 文件, ,此查询返回这些字段:字段,类型,Null,默认值,额外和注释。它们都不允许我确定列是否无符号。
在那之后,我看着 information_schema.columns
这是由使用的基表 SHOW COLUMNS
查询。
SELECT ...
FROM information_schema.columns
WHERE table_name = 'ttypes';
不幸的是,没有一个结果字段允许我确定列是否无符号。
解决方案
据我所知,这些属性存储的唯一地方是在 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
在那里-它没有为签名列放置任何东西。
其他提示
试试这个魔法:
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
.
要确定表中所有变量的类型,您可以运行这样的查询:
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循环,过程或您喜欢的脚本语言来使用此结果和/或继续搜索其他变量。
不隶属于 StackOverflow