SQL basic syntax statement -update

1 update statement presentation

update statement is used to modify the contents of the data table

Single-table syntax:

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
    SET assignment_list
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

Multiple-table syntax:

UPDATE [LOW_PRIORITY] [IGNORE] table_references
    SET assignment_list
    [WHERE where_condition]

General usage update statement

update students set sname='abcd',gender='1' where sid=1;
update student,students set students.sname=student2.sname,students.gender=students2.gender where students.sid=students2.sid;
  • Modifying means to modify a single table or a plurality of columns of data to specify a single table of data already exists; setlater with the phrase and to modify the values of the column
  • whereClause indicates which row is defined to modify the data in the table, if there is no means all rows where clause to be modified; order byclause indicates that the updatedata in the specified order; limitclauses defining the number of rows of modified data
  • Multi-table modifying means to modify table_referencesrows in multiple tables in the specified conditions are satisfied, the multi-table is not allowed to modify order byand limitclause
  • Execute updatestatements need to modify the permissions on the table
  • low_priorityKey words represent modification statements need to wait for other links to read this table before the end of the operation performed in the role MylSAM, MEMORY, MERGEthe storage engine
  • ignoreKey words modify the statement indicates that the current constraints encountered violations of the unique conditions, the statement not being given back-off is reported warnings

Example 2 update statement using

2.1 Single table modification

##修改全表中的数据,慎用
mysql> update student2 set sname='aaa';

##加上where限制条件,修改指定行的数据
mysql> update student2 set sname='aaa' where sid=1006;

##sid字段比原值增加1,哪一行先执行时随机的
mysql> update student2 set sid=1+sid;

##将sid字段设置成primary key,会出先以下错误,原因是,哪一行数据的sid先+1是随机的
mysql> update student2 set sid=1+sid;
ERROR 1062 (23000): Duplicate entry '1005' for key 'PRIMARY'

##所以在使用update时,一般会结合order by子句对数据先进行排序
mysql> update student2 set sid=sid+1 order by sid desc;  ##默认是升序,desc表示降序
Query OK, 2 rows affected (0.06 sec)
Rows matched: 2  Changed: 2  Warnings: 0

##使用limit限定行数,一般和order by配合使用
mysql> update student2 set sname='sss' order by id limit 2;

More than 2.2 modify table

##多表修改(表之间通过where条件进行join操作)
mysql> update items,month set items.price=month.price where items.id=month.id;

Case presentation:

##案例需求:使用一条命令将students表中a,b改成students2中的aa,bb
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> mysql> update students,students2 set students.sname=students2.sname where students.sid=students2.sid;

3 simple rollback mechanism set up

MySQL in autocommitparameter controls whether you can roll back, in fact, turned on by default, turn on automatic rollback on behalf of, the use of set autocommit=offopen manually rollback feature, use the rollbackcommand, you can do a rollback operation.

rollbackRollback mechanism: When the execution commitand rollbackcommand, indicates that the current transaction ends, the next transaction begins:

  • commit: Terminate the current things, after the command is executed, rollbackyou can not return to the initial state of the transaction
  • rollback: Roll back the current operation to the initial state after the transaction

4 Exercises

  • All students learn less number of lines instead of 100education
mysql> update students set students.dept_id=(select id from dept where dept_name='education');

mysql> update students,dept set students.dept_id=dept.id where sid<=100 and dept_name='education';
  • The name is ruthand idchange in teacher between 100 and 200 namescarey
mysql> update teacher set teacher.name='carey' where teacher.name='ruth' and id>100 and id<200;

Guess you like

Origin www.cnblogs.com/dabric/p/12343857.html