一、MySQL默认值相关查询1、列出MySQL数据库中的表默认值selecttable_schemaasdatabase_name,table_name,column_name,column_defaultfrominformation_schema.columnswherecolumn_defaultisnotnullandtable_schemanotin('information_schema','sys','performance_schema','mysql')--andtable_schema='你的数据库名'orderbytable_schema,table_name,ordinal_position;说明:database_name-数据库的名称(mode)table_name-表的名称column_name-列的名称column_default-定义该列的默认值SQL表达式2.MySQL数据库默认值汇总selectcolumn_default,count(distinctconcat(col.table_schema,'.',col.table_name))astables,count(column_name)ascolumnsfrominformation_schema.columnscoljoininformation_schema.tablestaboncol.table_schema=tab.table_schema和col.table_name=tab.table_name其中col.table_schema不在('sys','information_schema','mysql','performance_schema')andtab.table_type='BASETABLE'groupbycolumn_defaultorderbytablesdesc,columnsdesc;说明:column_default——为没有默认值的列定义默认约束(公式)和NULLtables——有这个约束的表数(或者有NumberoftableswithcolumnsthathavenoconstraintsonNULLrows)columns——有这个的列数特定约束(或没有约束的NULL行)2.约束1.列出数据库(模式)选择选项卡中的所有不可空列。table_schema作为database_name,tab.table_name,col.ordinal_position作为column_id,col.column_name,col.data_type,当col.numeric_precision不为null时,col.numeric_precision否则col.character_maximum_length以max_length结束,当col.datenotnull时,col.data_type.datetime_precisionwhencol.numeric_scaleisnotnullthencol.numeric_scaleelse0endas'precision'frominformation_schema.tablesastabjoininformation_schema.columnsascoloncol.table_schema=tab.table_schemaandcol.table_name=tab.table_nameandcol。is_nullable='no'wheretab.table_schemanotin('information_schema','sys','mysql','performance_schema')andtab.table_type='BASETABLE'--andtab.table_schema='databasename'orderbytab.table_schema,tab.table_name,col.ordinal_position;注意:如果您需要特定数据库(架构)的信息,请取消注释where子句中的条件并提供您的数据库名称。说明:database_name-数据库的名称(模式)table_name-表的名称column_id-列在表中的位置column_name-列的名称data_type-列的数据类型max_length-数据的最大长度typeprecision-数据类型的精度2.检查MySQL数据库中的列是否可以为空selectc.table_schemaasdatabase_name,c.table_name,c.column_name,casec.is_nullablewhen'NO'then'notnullable'当'YES'时,'isnullable'以nullablefrominformation_schema结束。columnscjoininformation_schema.tablestonc.table_schema=t.table_schemaandc.table_name=t.table_namewherec.table_schemanotin('mysql','sys','information_schema','performance_schema')andt.table_type='基表E'--andt.table_schema='database_name'--把你的数据库名放在这里按t.table_schema,t.table_name,c.column_name排序;说明:database_name-数据库名(模式)table_name-表名column_name-列名nullable-列的可空性属性:isnullable-可以为nullable,notnullable-不能为nullable
