Home  >  Article  >  Database  >  How to update table data in MySQL database

How to update table data in MySQL database

autoload
autoloadOriginal
2021-03-22 15:57:139358browse

This article mainly takes you to understand and use the MySQL Update statement to update existing records in the database table.

Basic syntax of the Update statement:

UPDATE <表名> SET 字段 1=值 1 [,字段 2=值 2… ] [WHERE 子句 ]
[ORDER BY 子句] [LIMIT 子句]
  • 722e3d59fd24604761db25f00f9b264f: used to specify the name of the table to be updated.

  • SET clause: used to specify the column name and column value to be modified in the table. Among them, each specified column value can be an expression or the default value corresponding to the column. If a default value is specified, the column value can be represented by the keyword DEFAULT.

  • WHERE Clause: Optional. Used to limit the rows in the table to be modified. If not specified, all rows in the table will be modified.

  • ORDER BY clause: optional. Used to limit the order in which rows in a table are modified.

  • LIMIT clause: optional. Used to limit the number of rows that are modified.

First, create a new database:

CREATE TABLE tasks (
    id INT NOT NULL,
    subject VARCHAR(45) NULL,
    start_date DATE NULL,
    end_date DATE NULL
)charset utf8;

Secondly, insert data:

insert into tasks values(1,&#39;math&#39;,2029-6-1,2060-6-1)

Finally, update the data:

UPDATE `tasks` SET `start_date`=&#39;2029-6-1&#39;, `end_date`=&#39;2060-6-1&#39; WHERE (`id`=&#39;1&#39;) AND (`subject`=&#39;math&#39;) AND (`start_date`=&#39;0000-00-00&#39;) AND (`end_date`=&#39;0000-00-00&#39;) LIMIT 1

Note: Ensure that Update ends with a WHERE clause, passed # The ##WHERE clause specifies the conditions that the updated records need to meet. If the WHERE clause is ignored, MySQL will update all rows in the table.

Recommended:

mysql tutorial

The above is the detailed content of How to update table data in MySQL database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn