MySQL資料庫裡查詢表注釋 表字段注釋資訊

2022-01-29 20:54:59 字數 2438 閱讀 3285

一、查詢資料庫和資料表資訊

information_schema:是mysql自帶資料庫,看作資訊資料庫,並且唯讀。

information_schema.tables:提供了關於資料庫中的表的資訊,屬於那個資料庫例項(table_schema)。

information_schema.columns:提供了表中的列資訊。詳細表述了某張表的所有列以及每個列的資訊。

1、table_schema:資料庫例項名稱;

2、table_name:資料存放物件表名稱;

3、table_type:資料存放物件型別(表或檢視);

4、table_rows:資料量;

5、column_name:欄位名稱

6、column_comment:字段注釋

7、column_type:字段型別

日常使用場景示例:

檢視某個資料庫中所有表注釋資訊:

select table_name,table_comment from information_schema.tables where table_schema='demo';

檢視資料庫下某張表所有字段注釋資訊:

select column_name,column_comment from information_schema.`columns` where table_name='table_demo' and table_schema='demo'

一次性查詢資料庫下所有表注釋和表下字段注釋:

select t.table_name,t.table_comment,c.column_name,c.column_type,c.column_comment from information_schema.tables t,information_schema.columns c where c.table_name=t.table_name and t.`table_schema`='demo'

查詢資料庫下所有表注釋:

select table_schema 資料庫名稱,table_name 表名稱,column_name 表字段,column_comment 表字段注釋,column_type 表字段型別

from information_schema.`columns` where table_schema='demo'

查詢某個資料下某張表的所有欄位的注釋:

select table_schema,table_name,column_name,column_type,column_comment from information_schema.`columns` where table_schema='資料庫名稱' and table_name='表名稱'

檢視資料的資料條數和資料庫儲存資料多少mb?

select concat(sum(tt.data_size)," mb") data_size,sum(tt.table_rows) table_rowsl from (select table_name, concat(truncate(data_length/1024/1024,2)) as data_size,table_rows from information_schema.tables where table_schema = 'demo-sl' ) tt ;

二、函式

if函式使用:

if(expr1,expr2,expr3);expr1成立則結果為expr2,否則為expr3

ifnull(expr1,expr2);expr1為null,則結果為expr2

-- 清空資料中的所有表

select concat('truncate table ',table_schema,'.',table_name, ';') from information_schema.tables where table_schema = '資料庫1';

-- 刪除資料庫中所有表

select concat('drop table if exists ', table_name, ';') from information_schema.tables where table_schema = 'industry_brain';

報錯 :cannot delete or update a parent row: a foreign key constraint fails,

解決方法:

set foreign_key_checks = 0; // 先設定外來鍵約束檢查關閉

drop table table1; // 刪除表,如果要刪除檢視,也是如此

set foreign_key_checks = 1; // 開啟外來鍵約束檢查,以保持表結構完整性

-- mysql的mysqldump備份語句

/usr/local/mysql/bin/mysqldump -hip -pport -uuser -ppassword -a -b -f | gzip > /usr/local/data/all-2020-11-27.sql.gz

SQL查詢資料庫裡表大小的命令

create procedure get tableinfo as create table tablespaceinfo 建立結果儲存表 nameinfo varchar 50 rowsinfo int reserved varchar 20 datainfo varchar 20 index s...

sql查詢資料庫注釋(表及表注釋,欄位及字段注釋)

1.要查詢資料庫下所有表名以及表注釋 查詢資料庫 mammothcode 所有表注釋 select table name,table comment from information schema.tables where table schema mammothcode 2.要查詢表字段的注釋 查...

SQL語句,查詢資料庫裡是否存在某個表

今天在搞乙個資料庫語句,因為老大要求,每個月自動生成乙個表,但是,我要做判斷,如果資料庫已經有這個表了,就不用建立了,但是我不知道怎麼查,在朋友的幫助下,找到這個兩個語句,和大家分享一下.select count 1 from sys.objects where name 表名 select obj...