sql判斷表 字段是否存在

2021-08-11 08:52:35 字數 1766 閱讀 9475

mysql

1、判斷乙個表是否存在

語法:select table_name from information_schema.tables where table_name ='表名';

sql例子:

select table_name from information_schema.tables where table_name ='t_iov_vehicle_owner_info'
存在:

不存在:

備註,sql也可以這樣寫:

select count(*) from information_schema.tables where table_name='t_iov_vehicle_owner_info'
2、判斷表中乙個字段是否存在語法:select count(*) from information_schema.columns where table_name = '表名' and column_name = '欄位名'

sql例子:

select count(*) from information_schema.columns where table_name = 't_iov_vehicle_owner_info' and column_name = 'id'
存在:

不存在:

知識點延伸:

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

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

SQL判斷是否存在

判斷資料庫是否存在 if exists select from master.sysdatabases where name n 庫名 print exists else print not exists 判斷要建立的表名是否存在 if exists select from dbo.sysobjec...

SQL判斷是否存在

1 判斷資料庫是否存在 2 ifexists select frommaster.sysdatabaseswherename n 庫名 3print exists 4 else 5print notexists 67 8 判斷要建立的表名是否存在 9 ifexists select fromdbo....

Sql記錄 判斷表或字段是否存在

首先我們需要了解 information schema 這個庫 information schema 中儲存著關於mysql伺服器所維護的所有其他資料庫的資訊。如資料庫名,資料庫的表,表欄的資料型別與訪問許可權等。其內有數個唯讀表,它們實際上是檢視,而不是基本表,因此,你將無法看到與之相關的任何檔案...