MySql常用命令

2021-09-12 04:27:29 字數 4486 閱讀 5564

0. 遠端連線資料庫

mysql -u devtest -h 10.248.0.51 -p 3308 -p
1. 使用show 語句找出在伺服器上當前存在什麼資料庫: 

mysql> show databases;
2. 建立資料庫sql語句:   

mysql> create database dbname default charset=utf8;

3. 選擇你所建立的資料庫:

mysql> use testdb;

4. 檢視資料庫的現有表: 

mysql> show tables;

5. 建表:

mysql> create table news(id int not null auto_increment primary key,title varchar(70),summary text,content text);

6. 顯示表的結構:

mysql > desc tablename;
檢視表結構包括注釋:

show full fields from table_name;
7. 檢視建表語句:

mysql> show create table news;
8. 用文字方式將資料匯入資料庫表中(例如d:/mysql.txt)

mysql>  load data local infile 「d:/mysql.txt」 into table tablename;
9. 匯入資料庫檔案:

mysql>use italk;

mysql>source d:/data_log.sql;

10. 刪除表

mysql>drop tabel tablename;
11. 清空表

mysql>delete from table;
12. 更新表中資料

mysql>update table set ***="f" where name='hyq';
13. 刪除表中字段:

mysql> alter table oper_log drop column opt_type;
14. 建索引:

create index index_user_username on user(username)
15. 刪除索引:

drop index index_user_userid on user;
17. 表增加字段:

alter table oper_log add module_id bigint(20);
alter table *** add yn tinyint(1) not null default '1'
18. 在表指定位置增加字段:

alter table oper_log add module_id bigint(20) after id;
// 在前面用first

19. 重新命名字段:

alter table oper_log change module_id m_id bigint(20);
20. 修改字段型別:

mysql> alter table data_log modify opt_time datetime not null;
21. 增加索引

alter table oper_log add index oper_index(id);
alter table *** add index idx_yn(yn)
22. 增加主鍵:

alter table oper_log add primary key(id);
24. 修改表名:

mysql> alter table oper_log rename to operate_log;
25. 匯出資料庫的結構

mysqldump -u root -p -d --add-drop-table italk>d:italk.sql
-d 沒有資料 --add-drop-table 在每個create語句之前增加乙個drop table. 

26. 匯出資料庫

mysqldump -u root -p italk>d:italk.sql
27. 匯出資料表

mysqldump -u root -p italk data_log>d:data_log.sql
28. 增加外來鍵

增加外來鍵之前,必須在要引用的兩個表的兩個列上建索引

alter table customer

add constraint fk_userinfo foreign key (user_id)

references userinfo (id) on update cascade on delete restrict;

on delete/on update,用於定義delete,update操作.以下是update,delete操作的各種約束型別:

cascade:

外來鍵表中外鍵欄位值會被更新,或所在的列會被刪除.

restrict:

restrict也相當於no action,即不進行任何操作.即,拒絕父表update外來鍵關聯列,delete記錄.

set null:

被父面的外來鍵關聯欄位被update ,delete時,子表的外來鍵列被設定為null.

而對於insert,子表的外來鍵列輸入的值,只能是父表外來鍵關聯列已有的值.否則出錯.

用法:

1.若不宣告on update/delete,則預設是採用restrict方式.

2.對於外來鍵約束,最好是採用: on update cascade on delete restrict 的方式.

29.刪除外來鍵:

alter table drop foreign key '外鍵名'

30.給表的列的字段增加注釋:

mysql> alter table news_category modify number varchar(10)  comment '1:company;2

:industry;3:technical';

31.檢視表的所有域

mysql> show full fields from news_category;

32.檢視表的索引

mysql> show index from jobs;

33. 修改root使用者的密碼

mysql -u root

mysql> set password for 'root'@'localhost' = password('newpass');

34. 增加新使用者

語法 : 

create user 'username'@'localhost

' identified by 'password';

create user 'dev'@'localhost' identified by 'test1234';
給新使用者賦權:

grant all privileges on exam.* to dev@localhost;
把exam資料庫的操作權賦予了localhost的dev使用者。

給使用者部分許可權:

grant select,update on wuba.* to dev@localhost identified by '1234';

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...