简体   繁体   English

MySQL-检查数据类型是否为smallint

[英]MySQL - check if datatype is smallint

I'm working on an existing database implementation (SQL and MySQL). 我正在研究现有的数据库实现(SQL和MySQL)。 Right now I need to change the datatype of one column from smallint to mediumint . 现在,我需要将一列的数据类型从smallint更改为mediumint While this wouldn't be a problem, the problem is the way updates are handled in the implementation. 虽然这不是问题,但问题在于实现中处理更新的方式。

On each start the program executes a simple MySQL statement (like SELECT xyz FROM 'tablename' ) to test if an update is needed. 在每次启动时,程序都会执行一个简单的MySQL语句(例如SELECT xyz FROM 'tablename' )来测试是否需要更新。 If the statement throws an error (like on the example when 'xyz' doesn't exist) the update itself is executed (in the example, 'xyz' would be created). 如果该语句引发错误(例如在不存在“ xyz”的示例中),则更新本身将被执行(在示例中,将创建“ xyz”)。 So what I need is a statement that throws an error if the datatype of the column is smallint . 所以我需要的是一个语句,如果列的数据类型为smallint则会引发错误。 I can only thing of writing an integer bigger than 65535 into the database but this isn't an option of course. 我只能将大于65535的整数写入数据库,但这当然不是一种选择。

If there is no such possibility and I actual need to change the way updates are handled, is there an easy way to check if the datatype of column 'xyz' is smallint ? 如果没有这种可能性,并且我实际上需要更改处理更新的方式,是否有一种简单的方法来检查列“ xyz”的数据类型是否为smallint

Thanks. 谢谢。

Does an empty result set count as an error ? 空结果集是否算作错误 Then you could use 那你可以用

SHOW COLUMNS FROM tablename WHERE Field = "xyz" AND Type = "mediumint(9)"

Handling flow control (logic) using exceptions is bad design: Exceptions are for "the exceptional". 使用异常处理流控制(逻辑)是不好的设计:异常是针对“特殊情况”的。

Consider changing your design to not require exceptions. 考虑更改设计以不要求例外。 For example, query the catalog to find out what you need: 例如,查询目录以查找所需内容:

select * from information_schema.columns
where tablename = 'foo'
and columnname = 'bar'

It seems that everything you need to know can be found in the information_schema tables. 您似乎需要了解的所有内容似乎都可以在information_schema表中找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM