选择列MySQL的默认值 [英] Select default value of column MySQL
问题描述
我不会详细介绍. 我有Java程序正在数据库中创建表.问题是创建表时,其中一个字段会获得默认值.因此,当我向该字段添加元素时,我想选择默认值.现在,加载所有元素的ID为:
I will not go deep in details. I have Java program which is creating tables in Database. The thing is that when table is created one of the fields get default value. So when I am adding elements to this field I want to select default value. Now loading of all element's ID is:
SELECT distinct(codeKind) FROM [table_name];
我想成为这样的人
SELECT [default value of column codeKind] from [table_name];
我检查了许多其他类似问题的答案,但都没有问题.
I checked many of answer of other similiar questions but none of them is OK.
谢谢.
推荐答案
使用coalesce
select coalesce(codeKind,<def_value>)
或者您可以为表的创建DDL
设置一个固定值,例如
Alternatively you can set a fixed value for table's creation DDL
such as
create table my_table ( my_column default 0, col2 int .... )
,如果在插入语句中将列值保留为空白,则该值将自动填充为零.
create table my_table ( my_column default 0, col2 int .... )
, if you leave the column value as blank in the insert statement, then the value is automatically populated as zero.
这篇关于选择列MySQL的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!