mysql四:資料操作

來源:互聯網
上載者:User

標籤:time   timestamp   單表   body   gre   src   too   更新mysql   str   

閱讀目錄

  • 一 介紹
  • 二 插入資料INSERT
  • 三 更新資料UPDATE
  • 四 刪除資料DELETE
  • 五 查詢資料SELECT
  • 六 許可權管理
一 介紹

MySQL資料操作: DML

========================================================

在MySQL管理軟體中,可以通過SQL語句中的DML語言來實現資料的操作,包括

  1. 使用INSERT實現資料的插入
  2. UPDATE實現資料的更新
  3. 使用DELETE實現資料的刪除
  4. 使用SELECT查詢資料以及。

========================================================

本節內容包括:

插入資料
更新資料
刪除資料
查詢資料

二 插入資料INSERT
1. 插入完整資料(順序插入)    文法一:    INSERT INTO 表名(欄位1,欄位2,欄位3…欄位n) VALUES(值1,值2,值3…值n);    文法二:    INSERT INTO 表名 VALUES (值1,值2,值3…值n);2. 指定欄位插入資料    文法:    INSERT INTO 表名(欄位1,欄位2,欄位3…) VALUES (值1,值2,值3…);3. 插入多條記錄    文法:    INSERT INTO 表名 VALUES        (值1,值2,值3…值n),        (值1,值2,值3…值n),        (值1,值2,值3…值n);        4. 插入查詢結果    文法:    INSERT INTO 表名(欄位1,欄位2,欄位3…欄位n)                     SELECT (欄位1,欄位2,欄位3…欄位n) FROM 表2                    WHERE …;
三 更新資料UPDATE
文法:    UPDATE 表名 SET        欄位1=值1,        欄位2=值2,        WHERE CONDITION;樣本:    UPDATE mysql.user SET password=password(‘123’)         where user=’root’ and host=’localhost’;
四 刪除資料DELETE
文法:    DELETE FROM 表名         WHERE CONITION;樣本:    DELETE FROM mysql.user         WHERE password=’’;練習:    更新MySQL root使用者密碼為mysql123    刪除除從本地登入的root使用者以外的所有使用者
五 查詢資料SELECT

單表查詢:http://www.cnblogs.com/llhtjwq/p/8306743.html

多表查詢:http://www.cnblogs.com/llhtjwq/p/8306766.html

六 許可權管理

#授權表user #該表允許存取的許可權,針對:所有資料,所有庫下所有表,以及表下的所有欄位db #該表允許存取的許可權,針對:某一資料庫,該資料庫下的所有表,以及表下的所有欄位tables_priv #該表允許存取的許可權。針對:某一張表,以及該表下的所有欄位columns_priv #該表允許存取的許可權,針對:某一個欄位#按圖解釋:user:允許存取db1,db2及其包含的所有db:允許存取db1,及其db1包含的所有tables_priv:允許存取db1.table1,及其該表包含的所有columns_prive:允許存取db1.table1.column1,只允許存取該欄位
#建立使用者create user ‘egon‘@‘1.1.1.1‘ identified by ‘123‘;create user ‘egon‘@‘192.168.1.%‘ identified by ‘123‘;create user ‘egon‘@‘%‘ identified by ‘123‘;#授權:對檔案夾,對檔案,對檔案某一欄位的許可權查看協助:help grant常用許可權有:select,update,alter,deleteall可以代表除了grant之外的所有許可權#針對所有庫的授權:*.*grant select on *.* to ‘egon1‘@‘localhost‘ identified by ‘123‘; #只在user表中可以查到egon1使用者的select許可權被設定為Y#針對某一資料庫:db1.*grant select on db1.* to ‘egon2‘@‘%‘ identified by ‘123‘; #只在db表中可以查到egon2使用者的select許可權被設定為Y#針對某一個表:db1.t1grant select on db1.t1 to ‘egon3‘@‘%‘ identified by ‘123‘;  #只在tables_priv表中可以查到egon3使用者的select許可權#針對某一個欄位:mysql> select * from t3;+------+-------+------+| id   | name  | age  |+------+-------+------+|    1 | egon1 |   18 ||    2 | egon2 |   19 ||    3 | egon3 |   29 |+------+-------+------+grant select (id,name),update (age) on db1.t3 to ‘egon4‘@‘localhost‘ identified by ‘123‘; #可以在tables_priv和columns_priv中看到相應的許可權mysql> select * from tables_priv where user=‘egon4‘\G*************************** 1. row ***************************       Host: localhost         Db: db1       User: egon4 Table_name: t3    Grantor: [email protected]  Timestamp: 0000-00-00 00:00:00 Table_priv:Column_priv: Select,Updaterow in set (0.00 sec)mysql> select * from columns_priv where user=‘egon4‘\G*************************** 1. row ***************************       Host: localhost         Db: db1       User: egon4 Table_name: t3Column_name: id  Timestamp: 0000-00-00 00:00:00Column_priv: Select*************************** 2. row ***************************       Host: localhost         Db: db1       User: egon4 Table_name: t3Column_name: name  Timestamp: 0000-00-00 00:00:00Column_priv: Select*************************** 3. row ***************************       Host: localhost         Db: db1       User: egon4 Table_name: t3Column_name: age  Timestamp: 0000-00-00 00:00:00Column_priv: Updaterows in set (0.00 sec)#刪除許可權revoke select on db1.* to ‘alex‘@‘%‘;
許可權相關操作

 

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.