mysql常用命令

來源:互聯網
上載者:User

標籤:mysql

1、資料庫的登入與退出

mysql -u root -p -- 使用root身份登陸當前的資料庫

mysql -u root -p -h server0 -- 使用root身份登陸到server0的資料庫

quit --退出資料庫

2、資料庫

2.1、建立

create databses db1; -- 建立一個資料庫

create databases db1 character set utf8; -- 建立並定義字元集

2.2、選中資料庫 use db1; -- 選中一個庫

2.3、查看

show databases; -- 顯示資料庫

show create database db1 -- 顯式資料庫建立語句

2.4、刪除

drop database db1; -- 刪除資料庫

2.5、資料使用權限設定

grant privileges on db1.tablename to [email protected] identified by "password"; -- 


按資料庫授所有許可權

grant select,insert,update,delete,create temporary tables,execute,show view on 


`db1`.* to ‘user1‘@‘10.168.0.8‘; -- 按資料庫授特定許可權

revoke select,insert,update,delete,create temporary tables,execute,show view on 


`db1`.* from ‘user1‘@‘10.168.0.8‘; -- 按資料庫取消特定許可權

drop user ‘user1‘@‘10.168.0.8‘; -- 刪除特定的使用者

fluse privileges; -- 重新整理許可權表

2.6、資料庫的備份與還原

  mysqldump -u username -p database > /backup/file.dump -- 備份資料庫

mysqldump -u username -p <./file.dump -- 還原資料庫

3、表

3.1、建立

3.1.1、建立1

create tabales tb1 (

  id int not null,

  name varchar(10)

); -- 建立一張表

3.1.2、建立2

create tabales tb1 (

  id int not null,

  name varchar(10)

)engine=MYISAM default charset=utf8; -- 建立一張表

3.2、查看

show tables; -- 顯示當前庫中的表

show create table tb1; --顯式建立表的語句

describe tb1; -- 查看錶結構

show create table tb1; -- 查看見表命令

show index from tb2; -- 查看錶包含的索引

show table status like ‘%tb3%‘; -- 查看當前表的狀態

3.3、刪除

drop table tb1

drop table if exists tab1

3.4、修改

alter table tb1 engine=myisam;

4、表的列操作

4.1、增加欄位

alter table Persons add column `City` varchar(255) DEFAULT NULL;

4.2、修改欄位

alter table Persons modify column `City` varchar(255) DEFAULT NULL comment ‘城市‘;

4.3、刪除欄位

alter table `Persons` drop column `City`;

5、表的行操作

5.1、查詢

select email from user group by email having count(email) > 1; -- 查詢重複的列

select * from user where email in (

  select email from user group by email having count(u_email) > 1

  ); -- 查詢重複資料列的行

5.2、插入

5.2.1、單行插入

insert into tablename set column1 = value1 , coumn2 = values2...;

5.2.2、多行插入

insert into tablename (column1,column2...) values (value1,value2....);

insert into tablename (column1,column2...) values (value1,value2....),


(value1,value2....)...;

5.2.3、簡寫

insert into tablename values (value1,value2....);

註:當滿足表中所有列都插入資料時才可簡寫

5.2.4、插入查詢結果

insert into tablename (column1,column2...) select (column1,column2...) from 


tablename2 [where condition];

簡寫

insert into tablename select (column1,column2...) from tablename2 [where 


condition];

5.3、更新

update tablename set column1 = valuesl,column2 = values2... [where condition];

註:不寫where會修改整張表資料(危險操作)

5.4、刪除

delete from tablename [where condition];註:不寫where會清空整張表資料(危險操作)

6、事務

begin; -- 開啟事務(必選)

-- 這裡做引起資料變更的操作(可選)

rollback; -- 復原事務(可選)

commit; -- 提交事務(必選)

註:資料InnoDB引擎支援表事務性操作

show variables like ‘%commit‘; -- 查詢事務自動認可設定

set autocommit=0; -- 自動認可關閉(需要手動鍵入commit,執行前所有操作當一個交易處理)

set autocommit=1; -- 自動認可開啟(每個操作一個事務)

7、其他查詢或命令

show warnings; -- 查詢警告資訊

show variables; -- 查詢變數環境

show variables like ‘%query_cache%‘ -- 查詢基於特定關鍵字環境變數

show processlist; -- 查詢進程列表

kill processID -- 結束查詢的進程

8、QEP

explan select user,host from user\G


本文出自 “天藍藍家園” 部落格,謝絕轉載!

mysql常用命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.