常用SQL語句分享

2021-10-01 07:10:14 字數 4500 閱讀 6662

前言:

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

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

1.show相關語句

2.檢視賬戶相關資訊

# 這裡先介紹下concat函式:在mysql中 concat()函式用於將多個字串連線成乙個字串,

利用此函式我們可以將原來一步無法得到的sql拼接出來,後面部分語句有用到該函式。

# 當拼接字串**現''時 需使用\轉義符

# 檢視所有使用者名稱:

select distinct

concat(

'user: \'',

user,

'\'@\'',

host,

'\';'

) as query

from

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 createdatabasequery

from

information_schema.schemata

where

schema_name not in (

'information_schema',

'performance_schema',

'mysql',

'sys'

);# 拼接建立使用者語句(排除系統使用者):

select

concat(

'create user \'',

user,

'\'@\'',

host,

'\''

' identified by password \'',

authentication_string,

'\';'

) as createuserquery

from

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.查詢無主鍵表
# 查詢某乙個庫無主鍵表:

select

table_schema,

table_name

from

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_name

from

information_schema.`tables` t1

left outer join information_schema.table_constraints t2 on t1.table_schema = t2.table_schema

and t1.table_name = t2.table_name

and t2.constraint_name in ('primary')

where

t2.table_name is null

and t1.table_schema not in (

'information_schema',

'performance_schema',

'mysql',

'sys'

) ;

語句拼接 常用SQL語句分享

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

sql常用sql語句

1 查詢某個庫中所有的表名字 select name from sysobjects where xtype u and name dtproperties order by name 2 得到資料庫中所有使用者檢視 select name from sysobjects where xtype v...

常用sql語句

t sql語句複製表的方法 我在sql server 2000中有現個資料庫datahr及demo,它們的結構是一樣,其它有乙個表名為 gbitem.現在我想將demo資料庫的表名 gbitem的全部內容複製到datahr資料庫的表名為 gbitem中。請問此t sql語句應該怎麼寫?謝謝高人指點!...