MySQL 基礎命令

2021-09-16 22:16:32 字數 2032 閱讀 9221

作為前端雖然資料庫接觸的不多,但當使用時總記不住命令確實有點尷尬,遂做此筆記,以方便查閱。

mysql -h localhost -u root -p  //登入

quit // 登出

ps: 又一次儲存emoji表情時,出現了一次報錯,因為utf-8編碼有可能是兩個、三個、四個位元組,其中emoji表情是4個位元組,而mysql的utf8編碼最多3個位元組,所以導致了資料插不進去。

當時的解決方式是更改了某個表的字符集

alter table tab-name convert to character set utf8mb4 collate utf8mb4_unicode_ci;
注釋:utf8mb4的最低mysql版本支援版本為5.5.3+

檢視資料庫變數

資料庫操作

修改資料庫

檢視建立資料庫語句

刪除資料庫

使用資料庫

顯示當前選擇

資料表操作

檢視次資料庫中的資料表:

檢視次資料庫中的資料表:

檢視資料列:

插入資料:

更新資料:

外來鍵新增:

檢視資料:

修改資料表:

新增列:alter table tab-name add column data-type [first | after column-name](新增多列-add後跟小括號,但無位置關係)

清空資料表:

刪除資料表:

資料表均用到了的為sql語句查詢,語句參考

檢視賬戶許可權:

show grants; # 當前使用者許可權

show privileges; # mysql 所支援的許可權列表

show grants for user; # 檢視某使用者

grant 許可權名稱[字段列表] on [資料庫資源型別]資料庫資源 to mysql賬戶1,[mysql賬戶2] [with grant option]

*.* 表示所有資料庫,所有資料表

'user'@'%' user 賬號可以在任意的主機上進行登入。

grant all privileges on *.* to 'supperuser'@'%' identified by 'password' with grant option;
將建立乙個名字為supperuser的賬號,擁有所有的資料庫許可權,並且具有grant 許可權,可以建立其他擁有服務例項許可權的其他使用者。

建立資料庫例項賬號

grant all privileges on database_name.* to 'user'@'%' identified by 'userpass' with grant option;
將建立乙個名字為user的賬號。擁有database_name 資料庫的所有許可權,可以隨該庫中的表進行所有操作。

grant all privileges on table tab_name.test to 'user'@'%' identified by 'userpass' with grant option;
將建立乙個名字為user 的使用者,對tab_name資料庫中test擁有所有的許可權。

想要增加update, delete,alter 許可權可以如下操作:

grant update,delete,alter on database_name.* to 'user'@'%' identified by 'userpass' with grant option;
想要移除insert 許可權:

revoke insert on database_name.* from  'user'@'%';
刪除使用者:

drop user user_name
參考文章roverliang

mysql 基礎命令

1 連線mysql資料庫 mysql u使用者名稱 p密碼 port 埠號 2 建立資料庫 drop database if exists 資料庫名稱 create database 資料庫名稱 default character set utf8 use 資料庫名稱 3 建立表 create ta...

MySQL基礎命令

資料庫登入 mysql uroot proot檢視資料庫名 show databases使用資料庫 use 資料庫名檢視表的屬性 show columns from nc setting 插入表的列 alter table nc setting add column member id int 11...

MySQL基礎命令

退出命令 mysql exit mysql quit mysql q 修改mysql的提示符 musql u root p yourpassword 提示符 連線上客戶端 prompt 提示符 提示符引數 d 完整的日期 d 當前資料庫 h 伺服器名稱 u 當前使用者 mysql常用命令 顯示當前伺...