当前位置:操作系统 > Unix/Linux >>

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

常用数据库查询判断表和字段是否存在
 
数据库postgresql表是否存在字段是否存在oracle
 
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';

 

 
    总结:以上结果为1表示存在,为0表示不存在。
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,