mysql黑視窗常用命令 mysql 常用命令大全

2021-10-17 16:30:05 字數 2957 閱讀 2140

1 進入 mysql:mysql -u root -p

2 建立資料庫:create database 資料庫名;

3 使用資料庫:use 資料庫名;

4 刪除資料庫:drop database 資料庫名;

5 展示表:show tables;

6 建立表

create table 表名 (

列名 1 資料型別,

列名 2 資料型別,

列名 3 資料型別,

7 刪除表:drop table 表名

8 增加資料

insert into 表名 (列名 1,列名 2,列名 3) values (.......);

9 修改資料

update 表名 set ....where...;

10 新增一列:alter table 表名 add column 列名 資料型別;

11 刪除一列:alter table 表名 drop 列名;

12 增加主鍵約束:

alter table 表名 add constraint 主鍵名 primary key (約束描述);

13 增加外來鍵約束:

alter table 表名 2 add constraint 外鍵名 foreign key (約束描述)references 表名 2 (約束描述);

14 檢視主鍵:desc 表名

15 檢視外來鍵:show create table 表名;

16 刪除外來鍵約束:

alter table 表名 drop foreign key 外鍵名;(刪除外來鍵後,才可以修改表的資料)

17 刪除主鍵約束:

alter table 表名 drop primary key;

18 修改表名:alter table tablename rename table_newname;

19 修改表的列名:

例子:alter table emp change salary sal decimal (7,2);

20 檢視 mysql 版本號:status;

21 檢視資料庫字符集:show variables like 'character%';

22 刪除表中的一行資料:delete from 表名 where 條件

23 列出表中的字段:show full fields from 表名;

24 檢視資料庫字段編碼方式:show full columns from 表名;

25 檢視 mysql 支援的字符集:show character set;

26 檢視當前資料庫的校對規則:show variables like 'collation';

27 表結構詳細描述:describe tablename;

28 複製表結構:create table newtable like oldtable;

29 複製表資料:insert into newtable select * from oldtable;

30 顯示當前 mysql 版本和當前日期:select version(),current_date;

31 修改 root 密碼:

31.1 原始密碼為空

mysqladmin -u root password

new password:< 輸入新的密碼 >

confirm new password:< 再次輸入新密碼 >

31.2 原始密碼不為空

mysqladmin -u root password

enter password:< 輸入舊的密碼 >

new password:< 輸入新的密碼 >

confirm new password:< 再次輸入新密碼 >

32 備份和恢復

mysqldump -u root -p dbname > dbname.sql # 備份整個資料庫(包含表結構和資料)

mysqldump -u root -p -d dbname > dbname.sql # 備份資料庫表結構,不包含資料

mysqldump -u root -p dbname tablename > tablename.sql # 備份資料庫中的某張資料表(包含表結構和資料)

mysqldump -u root -p dbname tablename1 tablename2 > tables.sql # 備份資料庫中 2 張資料表

mysqldump -u root -p -d dbname tablename > tablename.sql # 備份資料庫中的某張資料表的表結構(不含資料)

mysqladmin -u root -p create dbname # 恢復資料庫步驟 1:建立資料庫

mysql -u root -p dbname < dbname.sql # 恢復資料庫步驟 2:恢復資料

33 使用者和許可權管理

33.1 給使用者 username 分配所有資料庫的所有許可權

grant all on . to 'username'@'localhost' identified by '123456';

33.2 用 revoke 刪除原來的許可權

revoke all on . from 'username'@'localhost';

33.3 重新授予僅在 dbname 資料庫上的許可權

grant all on dbname.* to 'username'@'localhost' identified by '123456';

33.4 僅授予 select、update 許可權,無法執行 insert、delete 等命令

grant select, update on dbname.* to 'username'@'localhost' identified by '123456';

33.5 每當調整許可權後,通常需要用這個命令重新整理許可權

flush privileges;

33.6 刪除使用者

drop user username@localhost;

mysql基本常用命令 MySQL常用命令(一)

cmd提示框中的mysql基礎命令 一 命令 連線mysql伺服器 mysql h localhost u root p 展示所有資料庫 show databases 選擇資料庫 use database 展示所選資料下所有表 show tables 設定資料庫編碼 set names gbk 用s...

mysql巡檢常用命令 mysql 常用命令

客戶端連線 進入命令列,windows cmd,連線 mysql u 使用者名稱 p密碼 h 伺服器ip位址 p 伺服器端mysql埠號 d 資料庫名 注意 1 伺服器端口標誌 p一定要大些以區別於使用者 p,如果直接連線資料庫標誌 d也要大寫 2 如果要直接輸入密碼 p後面不能留有空格如 pmyp...

mysql常用命令總結 mySql常用命令總結

總結一下自己常用的mysql資料庫的常用命令 mysql u root p 進入mysql bin目錄後執行,回車後輸入密碼連線。資料庫操作 1 create database dbname 建立資料庫,資料庫名為dbname 2 create database todo default chara...