我想知道是否可以仅选择空列的默认值? 我的表绝对是空的,我只想选择列的默认值之一-这对我正在填充表的JAVA应用程序很重要。
谢谢。
答案 0 :(得分:2)
您可以从INFORMATION_SCHEMA.COLUMNS中获取默认值
select COLUMN_DEFAULT
from INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA='your_db' and TABLE_NAME='your_table' and COLUMN_NAME='your_column'
答案 1 :(得分:0)
我只能想到两种方式:
...可能全部都在交易中,所以没有其他人看到它。
describe
(explain
) describe
命令(又名explain
)描述了系统中的对象,包括表。因此,如果您执行explain YourTable
,则会获得有关该表的信息,包括其默认值。
这是该链接文档中的一个示例:
mysql> DESCRIBE City; +−−−−−−−−−−−−+−−−−−−−−−−+−−−−−−+−−−−−+−−−−−−−−−+−−−−−−−−−−−−−−−−+ | Field | Type | Null | Key | Default | Extra | +−−−−−−−−−−−−+−−−−−−−−−−+−−−−−−+−−−−−+−−−−−−−−−+−−−−−−−−−−−−−−−−+ | Id | int(11) | NO | PRI | NULL | auto_increment | | Name | char(35) | NO | | | | | Country | char(3) | NO | UNI | | | | District | char(20) | YES | MUL | | | | Population | int(11) | NO | | 0 | | +−−−−−−−−−−−−+−−−−−−−−−−+−−−−−−+−−−−−+−−−−−−−−−+−−−−−−−−−−−−−−−−+
因此,您可以从返回的行中的Default
列中提取默认值。
当然,还有第三种方法,请参见slaakso's answer。
答案 2 :(得分:0)
如果只希望MySQL自动插入表,则可以在创建表时为列定义默认值:
create table my_table (i INT DEFAULT 1);
但是,如果您要获取存储在数据库字典中的默认值,则可以通过以下查询获取它:
SELECT Column_Default
FROM Information_Schema.Columns
WHERE Table_Schema = 'yourSchema'
AND Table_Name = 'yourTableName'
AND Column_Name = 'yourColumnName'