mysql> update teacher set name='aaa'; Query OK, 3 rows affected (0.09 sec) Rows matched: 3 Changed: 3 Warnings: 0 mysql> update teacher set name='bbb' where id =10002; Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from teacher; +-------+------+---------+ | id | name | dept_id | +-------+------+---------+ | 10002 | bbb | 1 | | 10003 | aaa | 4 | | 10004 | aaa | 4 | +-------+------+---------+ 3 rows in set (0.00 sec) mysql> update teacher set name='ccc' order by id desc limit 2; Query OK, 2 rows affected (0.06 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> select * from teacher; +-------+------+---------+ | id | name | dept_id | +-------+------+---------+ | 10002 | bbb | 1 | | 10003 | ccc | 4 | | 10004 | ccc | 4 | +-------+------+---------+ 3 rows in set (0.00 sec)
mysql> select * from students; Empty set (0.01 sec) mysql> desc students; +---------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+----------------+ | sid | int(11) | NO | PRI | NULL | auto_increment | | sname | varchar(64) | YES | | NULL | | | gender | varchar(12) | YES | | NULL | | | dept_id | int(11) | NO | MUL | NULL | | +---------+-------------+------+-----+---------+----------------+ 4 rows in set (0.01 sec) mysql> insert into students values(1,'a',1,1); Query OK, 1 row affected (0.10 sec) mysql> insert into students values(4,'b',1,1); Query OK, 1 row affected (0.12 sec) mysql> create table students2 as select * from students; ERROR 1050 (42S01): Table 'students2' already exists mysql> select * from students2; +-----+-------+------+ | sid | sname | sex | +-----+-------+------+ | 1 | bbb | 1 | +-----+-------+------+ 1 row in set (0.00 sec) mysql> insert into students2 values(3,'cc',1); Query OK, 1 row affected (0.04 sec) mysql> drop table students2; Query OK, 0 rows affected (0.19 sec) mysql> create table students2 as select * from students; Query OK, 2 rows affected (0.15 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from students2; +-----+-------+--------+---------+ | sid | sname | gender | dept_id | +-----+-------+--------+---------+ | 1 | a | 1 | 1 | | 4 | b | 1 | 1 | +-----+-------+--------+---------+ 2 rows in set (0.00 sec) mysql> update students2 set sid=3,sname='bb' where sid=4; Query OK, 1 row affected (0.05 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> update students2 set sname='aa' where sid=1; Query OK, 1 row affected (0.07 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from students2; +-----+-------+--------+---------+ | sid | sname | gender | dept_id | +-----+-------+--------+---------+ | 1 | aa | 1 | 1 | | 3 | bb | 1 | 1 | +-----+-------+--------+---------+ 2 rows in set (0.00 sec) mysql> select * from students; +-----+-------+--------+---------+ | sid | sname | gender | dept_id | +-----+-------+--------+---------+ | 1 | a | 1 | 1 | | 4 | b | 1 | 1 | +-----+-------+--------+---------+ 2 rows in set (0.01 sec) mysql> insert into students2 values(3,'cc',1,1); Query OK, 1 row affected (0.04 sec) mysql> select * from students; +-----+-------+--------+---------+ | sid | sname | gender | dept_id | +-----+-------+--------+---------+ | 1 | a | 1 | 1 | | 4 | b | 1 | 1 | +-----+-------+--------+---------+ 2 rows in set (0.00 sec) mysql> select * from students2; +-----+-------+--------+---------+ | sid | sname | gender | dept_id | +-----+-------+--------+---------+ | 1 | aa | 1 | 1 | | 3 | bb | 1 | 1 | | 3 | cc | 1 | 1 | +-----+-------+--------+---------+ 3 rows in set (0.00 sec) mysql> update students2 set sid=4 where sid=3 and sname='cc'; Query OK, 1 row affected (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from students; +-----+-------+--------+---------+ | sid | sname | gender | dept_id | +-----+-------+--------+---------+ | 1 | a | 1 | 1 | | 4 | b | 1 | 1 | +-----+-------+--------+---------+ 2 rows in set (0.00 sec) mysql> select * from students2; +-----+-------+--------+---------+ | sid | sname | gender | dept_id | +-----+-------+--------+---------+ | 1 | aa | 1 | 1 | | 3 | bb | 1 | 1 | | 4 | cc | 1 | 1 | +-----+-------+--------+---------+ 3 rows in set (0.00 sec) mysql> update students,students2 -> set students.sname=students2.sname -> where students.sid=students2.sid; Query OK, 2 rows affected (0.06 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> select * from students; +-----+-------+--------+---------+ | sid | sname | gender | dept_id | +-----+-------+--------+---------+ | 1 | aa | 1 | 1 | | 4 | cc | 1 | 1 | +-----+-------+--------+---------+ 2 rows in set (0.00 sec) mysql> select * from students2; +-----+-------+--------+---------+ | sid | sname | gender | dept_id | +-----+-------+--------+---------+ | 1 | aa | 1 | 1 | | 3 | bb | 1 | 1 | | 4 | cc | 1 | 1 | +-----+-------+--------+---------+ 3 rows in set (0.00 sec)
• 單表修改是指修改指定單個表中的已經存在數據的一個或多個列的數值;set短語后面跟要修改的列和值;
• where子句表示限定要修改表中的哪些行數據,如果沒有where子句則表示所有行都要修改;order by子句表示update數據按照指定的順序進行;limit子句表示限定修改數據的行數
• 多表修改是指修改table_references指定的多個表中滿足條件的行數據,多表修改不允許使用order by和limit子句
• 執行update語句需要修改表的權限
• Low_priority關鍵詞表示修改語句需要等待其他鏈接的讀此表操作結束后再執行,只作用在MyISAM, MEMORY, and MERGE存儲引擎
• Ignore關鍵詞表示當修改語句碰到違反唯一性約束條件等情況是,語句不會報錯回退而是報警告信息
mysql> show variables like '%commit%'; +-----------------------------------------+-------------------+ | Variable_name | Value | +-----------------------------------------+-------------------+ | autocommit | ON | | binlog_group_commit_sync_delay | 0 | | binlog_group_commit_sync_no_delay_count | 0 | | binlog_order_commits | ON | | innodb_api_bk_commit_interval | 5 | | innodb_commit_concurrency | 0 | | innodb_flush_log_at_trx_commit | 1 | | original_commit_timestamp | 36028797018963968 | | slave_preserve_commit_order | OFF | +-----------------------------------------+-------------------+ 9 rows in set (0.02 sec) mysql> set autocommit=off; Query OK, 0 rows affected (0.01 sec) mysql> select * from students; +-----+-------+--------+---------+ | sid | sname | gender | dept_id | +-----+-------+--------+---------+ | 1 | aa | 1 | 1 | | 4 | cc | 1 | 1 | +-----+-------+--------+---------+ 2 rows in set (0.00 sec) mysql> update students set sname='bb'; Query OK, 2 rows affected (0.00 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> select * from students; +-----+-------+--------+---------+ | sid | sname | gender | dept_id | +-----+-------+--------+---------+ | 1 | bb | 1 | 1 | | 4 | bb | 1 | 1 | +-----+-------+--------+---------+ 2 rows in set (0.00 sec) mysql> rollback; Query OK, 0 rows affected (0.10 sec) mysql> select * from students; +-----+-------+--------+---------+ | sid | sname | gender | dept_id | +-----+-------+--------+---------+ | 1 | aa | 1 | 1 | | 4 | cc | 1 | 1 | +-----+-------+--------+---------+ 2 rows in set (0.00 sec) mysql> update students set sname='bb'; Query OK, 2 rows affected (0.00 sec) Rows matched: 2 Changed: 2 Warnings: 0mysql> update students set gender=2; Query OK, 2 rows affected (0.00 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> select * from students; +-----+-------+--------+---------+ | sid | sname | gender | dept_id | +-----+-------+--------+---------+ | 1 | bb | 2 | 1 | | 4 | bb | 2 | 1 | +-----+-------+--------+---------+ 2 rows in set (0.00 sec) mysql> rollback; Query OK, 0 rows affected (0.03 sec) mysql> select * from students; +-----+-------+--------+---------+ | sid | sname | gender | dept_id | +-----+-------+--------+---------+ | 1 | aa | 1 | 1 | | 4 | cc | 1 | 1 | +-----+-------+--------+---------+ 2 rows in set (0.00 sec) mysql> update students set gender=3; Query OK, 2 rows affected (0.00 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> commit; Query OK, 0 rows affected (0.03 sec)