欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

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

程序员文章站 2024-01-19 19:33:04
...

参数table_name和column_name的均为大写: 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 表:sel

参数table_name和column_name的值均为大写:

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';