MySQL學習系列一

2021-07-30 14:31:28 字數 2797 閱讀 2629

看了《mysql核心技術手冊》,在這裡整理一下知識點。

安全、使用者語句和函式

使用者的訪問許可權可以是全域性層級(伺服器上的所有資料庫),也可以是資料庫層級、表層級和列層級。

使用者的許可權儲存在授權表中,位於伺服器的mysql資料庫中:

user 全域性級許可權

db 資料庫層級許可權

tables_priv 表層級許可權

column_priv 列層級許可權

create user:在伺服器上建立新使用者賬戶。@後跟主機ip位址或主機名,identified by後跟密碼,但create user無法指定使用者許可權,需要再使用grant語句設定許可權。

如:

create

user

'xiaohui'@'localhost' identified by

'123456';

drop user :刪除mysql伺服器的使用者賬戶,使用該語句,需要擁有包含使用者賬戶資訊和許可權的mysql資料庫的create user 和delete許可權。

drop

user

'xiaohui'@'localhost';

flush:可以清除並過載mysql臨時快取。必須擁有reload許可權。

flush privileges; //重新載入使用者許可權
grant:建立新增使用者,並授予使用者許可權。

//授予所有基本許可權 with grant

option:令使用者具備grant許可權

grant

allprivileges

on *.* to

'xiaohui'@'localhost' identified by

'123456'

with

grant

option

//授予oa資料庫的所有表的select和update許可權

grant

select,update

on oa.* to

'xiaohui'@'localhost' identified by

'123456'

rename user:修改使用者名稱或主機

rename user

'xiaohui'@'localhost'

to'xiaohui2'@'localhost'

set password:修改密碼

set password for

'xiaohui2'@'localhosst' = password('12345678')

相關函式

current_user():當前的使用者名稱和主機

資料庫和表模式語句

新增新列

//加到表的末尾

alter

table

`user`

addcolumn name varchar(10)

//加到表的開頭

alter

table

`user`

addcolumn name varchar(10) first

//加到id列後

alter

table

`user`

addcolumn name varchar(10) after id

新增索引

//基於name列前五個字元和id列的索引name_id

alter

table

`user`

add index name_id (name(5),id)

新增外來鍵

alter

table

`user`

addforeign

key user_role (role_id) references

`role` (id) on

delete

setnull;

修改列名和型別

alter

table

`user` change column name varchar(10) newname varchar(20);

//modify不可修改列名

alter

table

`user` modify column name varchar(20);

刪除列

alter

table

`user`

drop

column name

刪除索引、主鍵和外來鍵

alter

table

`user`

drop index name_id;

//若主鍵自增 先去掉自增 在刪除

alter

table

`user` change id id int ,drop

primary

key id;

alter

table

`user`

drop

foreign

key role_id;

修改表名

alter

table

`user`rename to newuser;

MySQL必學必會 系列學習一

一 基本概念 1 資料庫 database 資料庫是乙個以某種有組織的方式儲存的資料集合。儲存有組織的資料的容器,一般是乙個檔案或者一組檔案 2 表 table 某種特定型別資料的結構化清單。3 模式 schema 關於資料庫和表的布局及特性的資訊。4 列 column 表中的乙個字段。所有表都是由...

MySql學習系列(四)

專案十 行程和使用者 難度 困難 trips 表中存所有計程車的行程資訊。每段行程有唯一鍵 id,client id 和 driver id 是 users 表中 users id 的外來鍵。status 是列舉型別,列舉成員為 completed cancelled by driver cance...

MySQL學習系列二 MySQL函式

mysql提供了一些操作字串和日期等的內建函式,可以大大簡化我們的開發,這裡整理一下常用的函式。字串函式 bin number 返回給定整數值對應的二進位制字串,輸入null則返回null。select bin 10 1010cast experssion as type 將一種資料型別轉為另一種資...