MySQL事務和隔離等級

來源:互聯網
上載者:User

標籤:mysql   事務   隔離等級   

廣義查詢:

delete:

insert into:

update:



insert into  TB_NAME  (COL1, COL2,...)values (VAL1,VAL2,...)[,(VAL1,VAL2,...),...]

 字元型:單引號

 數值型:不需要引號

 日期時間型:不需要引號

 空值:NULL,需要單引號


mysql> help insert 


INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]

    [INTO] tbl_name [(col_name,...)]

    {VALUES | VALUE} ({expr | DEFAULT},...),(...),...

    [ ON DUPLICATE KEY UPDATE

      col_name=expr

        [, col_name=expr] ... ]


Or:


INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]

    [INTO] tbl_name

    SET col_name={expr | DEFAULT}, ...

    [ ON DUPLICATE KEY UPDATE

      col_name=expr

        [, col_name=expr] ... ]


Or:


INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]

    [INTO] tbl_name [(col_name,...)]

    SELECT ...

    [ ON DUPLICATE KEY UPDATE

      col_name=expr

        [, col_name=expr] ... ]


insert into tutors (tname,gender,age) value (‘tom‘,‘f‘,30);

insert into tutors set tname=‘tom‘, gender=‘f‘,age=30;

insert into tutors (tname.gender,age) select name,gender,age from students where age > 20;



查看錶裡的最後一個資料,先降序排序,然後只顯示第一個

select * from tutors order by TID desc limit 1;  


select last_insert_id(); 此函數為表的計數函數,即把表清空後再插入也是從之前的行數+1開始插入



replace 插入的時候判斷資料是否存在,如果存在則不插入

mysql> help replace 


REPLACE [LOW_PRIORITY | DELAYED]

    [INTO] tbl_name [(col_name,...)]

    {VALUES | VALUE} ({expr | DEFAULT},...),(...),...


Or:


REPLACE [LOW_PRIORITY | DELAYED]

    [INTO] tbl_name

    SET col_name={expr | DEFAULT}, ...


Or:


REPLACE [LOW_PRIORITY | DELAYED]

    [INTO] tbl_name [(col_name,...)]

    SELECT ...




delete: mysql> help delete


DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name

    [WHERE where_condition]

    [ORDER BY ...]

    [LIMIT row_count]


truncate TB_NAME 清楚表裡的資料並重設計數器



update:

mysql> help update


UPDATE [LOW_PRIORITY] [IGNORE] table_reference

    SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...

    [WHERE where_condition]

    [ORDER BY ...]

    [LIMIT row_count]


Multiple-table syntax:


UPDATE [LOW_PRIORITY] [IGNORE] table_references

    SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...

    [WHERE where_condition]


update TB_NAME set COL1=...,COL2=... where


連線管理員:

  接受請求

  建立線程

  認證使用者

  建立安全連線

並發控制:

   多版本並發控制:MVCC。通過快照和時間點來管理


鎖:

   讀鎖:共用鎖定

   寫鎖:獨佔鎖

mysql> help lock

Name: ‘LOCK‘

LOCK TABLES

    tbl_name [[AS] alias] lock_type

    [, tbl_name [[AS] alias] lock_type] ...


lock_type:

    READ [LOCAL]

  | [LOW_PRIORITY] WRITE


UNLOCK TABLES


  lock tables TBNAME {read|write};

  unlock tables 解鎖鎖


鎖粒度:從大到小,mysql伺服器僅支援表級鎖,行鎖需要由儲存引擎完成:

   表鎖:

   行鎖:

   頁鎖:



事物:

 RDBMS:ACID(原子性,一致性,隔離性,持久性)

 MyISAM

 Innodb



隔離性:

  隔離等級:

    READ UNCOMMITTED 讀未提交

    READ COMMITTED 讀提交

    REPATABLEREAD 可重讀

    SERIABLIZABLE 可串列


mysql> show global variables like ‘%iso%‘;  預設的隔離等級

+---------------+-----------------+

| Variable_name | Value           |

+---------------+-----------------+

| tx_isolation  | REPEATABLE-READ |

+---------------+-----------------+

1 row in set (0.00 sec)

修改: set {session|global} var_name=‘‘;


mysql> set tx_isolation =‘read-uncommitted‘;


mysql> show  variables like ‘%iso%‘;

+---------------+------------------+

| Variable_name | Value            |

+---------------+------------------+

| tx_isolation  | READ-UNCOMMITTED |

+---------------+------------------+



本文出自 “營運成長路” 部落格,謝絕轉載!

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.