首页 >数据库 >mysql教程 >常用数据库查询判断表和字段是否存在的SQL

常用数据库查询判断表和字段是否存在的SQL

大家讲道理
大家讲道理原创
2016-11-12 09:34:163675浏览

常用数据库查询判断表和字段是否存在的SQL(如果结果为1表示存在,为0表示不存在)

1、MSSQL Server
表:

SELECT COUNT(*) FROM dbo.sysobjects 
WHERE name= 'table_name';

字段:

SELECT COUNT(*) FROM syscolumns 
WHERE id=object_id(‘table_name’) AND name= 'column_name';

 
2、My SQL
表:

SELECT COUNT(*) FROM information_schema.tables 
WHERE table_name ='table_name';

字段:

SELECT COUNT(*) FROM information_schema.columns 
WHERE table_name ='table_name' AND column_name ='column_name';

 
3、Oracle
表:

SELECT count(*) FROM USER_OBJECTS 
WHERE OBJECT_NAME = 'table_name';

字段:

SELECT COUNT(*) FROM USER_TAB_COLUMNS 
WHERE TABLE_NAME ='table_name' AND column_name ='column_name';

 
4、PostgreSql
表:

SELECT count(*) FROM information_schema.tables 
WHERE table_schema='table_schema' AND table_name ='table_name';

字段:

SELECT count(*) FROM information_schema.columns 
WHERE table_schema='table_schema' AND table_name ='table_name' AND column_name='column_name';
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn