語句拼接 常用SQL語句分享

2021-10-16 03:20:44 字數 3623 閱讀 3825

前言:

日常工作或學習過程中,我們可能會經常用到某些sql,建議大家多多整理記錄下這些常用的sql,這樣後續用到會方便很多。筆者在工作及學習過程中也整理了下個人常用的sql,現在分享給你!可能有些sql你還不常用,但還是希望對你有所幫助,說不定某日有需求就可以用到。

注:下文分享的sql適用於mysql 5.7 版本,低版本可能稍許不同。有些sql可能執行需要較高許可權。

1.show相關語句

2.檢視賬戶相關資訊

# 這裡先介紹下concat函式:在mysql中 concat()函式用於將多個字串連線成乙個字串,利用此函式我們可以將原來一步無法得到的sql拼接出來,後面部分語句有用到該函式。# 當拼接字串**現''時 需使用轉義符# 檢視所有使用者名稱:select distinct concat( 'user: '', user, ''@'', host, '';' ) as queryfrom mysql.user;# 檢視使用者詳細資訊:select user, host, authentication_string, password_expired, password_lifetime, password_last_changed, account_locked from mysql.user;
3.kill資料庫鏈結

4.拼接建立資料庫或使用者語句

# 拼接建立資料庫語句(排除系統庫):select concat( 'create database ', '`', schema_name, '`', ' default character set ', default_character_set_name, ';' ) as createdatabasequeryfrom information_schema.schematawhere schema_name not in ( 'information_schema', 'performance_schema', 'mysql', 'sys' ); # 拼接建立使用者語句(排除系統使用者):select concat( 'create user '', user, ''@'', host, ''' ' identified by password '', authentication_string, '';' ) as createuserqueryfrom mysql.`user`where `user` not in ( 'root', 'mysql.session', 'mysql.sys' );# 有密碼字串哦 在其他例項執行 可直接建立出與本例項相同密碼的使用者。
5.檢視庫或表大小

# 檢視整個例項占用空間大小:select concat( round( sum( data_length / 1024 / 1024 ), 2 ), 'mb' ) as data_length_mb, concat( round( sum( index_length / 1024 / 1024 ), 2 ), 'mb' ) as index_length_mb from information_schema.`tables`; # 檢視各個庫占用大小:select table_schema, concat( truncate ( sum( data_length )/ 1024 / 1024, 2 ), ' mb' ) as data_size, concat( truncate ( sum( index_length )/ 1024 / 1024, 2 ), 'mb' ) as index_size from information_schema.`tables`group by table_schema; # 檢視單個庫占用空間大小:select concat( round( sum( data_length / 1024 / 1024 ), 2 ), 'mb' ) as data_length_mb, concat( round( sum( index_length / 1024 / 1024 ), 2 ), 'mb' ) as index_length_mb from information_schema.`tables`where table_schema = 'test_db'; # 檢視單個表占用空間大小:select concat( round( sum( data_length / 1024 / 1024 ), 2 ), 'mb' ) as data_length_mb, concat( round( sum( index_length / 1024 / 1024 ), 2 ), 'mb' ) as index_length_mb from information_schema.`tables`where table_schema = 'test_db'  and table_name = 'tbname';
6.檢視表碎片及收縮語句

# 檢視某個庫下所有表的碎片情況:select t.table_schema, t.table_name, t.table_rows, concat( round( t.data_length / 1024 / 1024, 2 ), 'm' ) as size, t.index_length, concat( round( t.data_free / 1024 / 1024, 2 ), 'm' ) as datafree from information_schema.`tables` t where t.table_schema = 'test_db' order by datafree desc; # 收縮表,減少碎片:alter table tb_name engine = innodb;optimize table tb_name;
7.查詢無主鍵表

# 查詢某乙個庫無主鍵表:selecttable_schema,table_namefrom information_schema.`tables`where table_schema = 'test_db'and table_name not in ( select table_name from information_schema.table_constraints t join information_schema.key_column_usage k using ( constraint_name, table_schema, table_name ) where t.constraint_type = 'primary key' and t.table_schema = 'test_db');# 查詢除系統庫外 無主鍵表:select t1.table_schema, t1.table_namefrom information_schema.`tables` t1left outer join information_schema.table_constraints t2 on t1.table_schema = t2.table_schemaand t1.table_name = t2.table_nameand t2.constraint_name in ('primary')where t2.table_name is nulland t1.table_schema not in ( 'information_schema', 'performance_schema', 'mysql', 'sys') ;
總結:希望這些sql語句能對你有所幫助,可以收藏一下,說不定某次就用到了呢!原創不易,感謝大家支援。

常用SQL語句分享

前言 日常工作或學習過程中,我們可能會經常用到某些sql,建議大家多多整理記錄下這些常用的sql,這樣後續用到會方便很多。筆者在工作及學習過程中也整理了下個人常用的sql,現在分享給你!可能有些sql你還不常用,但還是希望對你有所幫助,說不定某日有需求就可以用到。注 下文分享的sql適用於mysql...

拼接SQL語句 Oracle

因為專案需要,有一段select語句中的列,想實現可配置,因此就需要用for迴圈。但嘗試之後發現select語句中是不允許放for迴圈的。需求 select column1,column2,column3,column41,column42,column43,column44.from table1...

SQL查詢語句拼接

1.判斷引數是否為空,2.把引數中的空格去掉3.1 1永遠是真,加1 1的目的是為了接後面的條件的,否則 where and 條件 and 條件 是有語法錯誤的string sql select from 表名 where 1 1 if condition1 string.empty 再分享一下我老...