MySQL判断库、表、列是否存在

1.判断数据库是否存在

select 1 from information_schema.schemata  where schema_name='数据库名';

指定数据库名查询是否存在,若存在则返回1,若不存在测返回空。

2.判断表是否存在

select 1 from information_schema.tables where table_schema='数据库名' and table_name ='表名';

指定表名查询是否存在,若存在则返回1,若不存在测返回空。(需指定在哪个数据库查询,若不指定则在整个服务上查询)

3.判断列是否存在 

select 1 from information_schema.columns  where table_schema='数据库名' and table_name='表名' and column_name ='列名';

指定列名查询是否存在,若存在则返回1,若不存在测返回空。(需指定在哪个数据库查询,若不指定则在整个服务上查询)

posted @ 2022-03-28 19:47  钟小嘿  阅读(3049)  评论(0编辑  收藏  举报