几种常见数据库查询判断表和字段是否存在sql

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'

注意,表名和字段名都要大写,oracle已经测过,其他请自测

你可能感兴趣的:(几种常见数据库查询判断表和字段是否存在sql)