A brief understanding of the three uses of standard SQL update statements

A brief understanding of the three uses of standard SQL update statements

1. Environment:

MySQL-5.0.41-win32

Windows XP Professional

2. Establish a test environment:

DROP TABLE IF EXISTS t_test; 
CREATE TABLE t_test ( 
bs bigint(20) NOT NULL auto_increment, 
username varchar(20) NOT NULL, 
password varchar(20) default NULL, 
remark varchar(200) default NULL, 
PRIMARY KEY (bs) 
)ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=gbk; 
INSERT INTO t_test VALUES (1,'lavasoft','123456',NULL); 
INSERT INTO t_test VALUES (2,'hello',NULL,NULL); 
INSERT INTO t_test VALUES (3,'haha',zz,tt);

3. Testing

1. Set a field

In the table t_test, set the password of the second record (bs=2) to '***'.

update t_test t
set t.password = '***'
where t.bs = 2;

2. Set multiple fields

In the table t_test, set the password of the first record (bs is 1) to '*' and remark to '*'.

update t_test t
set t.password = '*', t.remark = '*'
where t.bs = 1;

3. Set null value

In the table t_test, set the password and remark of the third record (bs is 3) to null and null respectively.

update t_test t
set t.password = null, t.remark = null
where t.bs = 3;

Conclusion

This is written according to the standard syntax. In different database systems, there are more ways to write update, but the standard syntax is supported. In order to illustrate the situation, the above three examples update one row each time. In practice, the number of updated rows can be controlled through where statement constraints.

You may also be interested in:
  • Will MySQL execute the update statement again if it has the same data as the original one?
  • SQL UPDATE update statement usage (single column and multiple columns)
  • Detailed explanation of MySQL database insert and update statements
  • Tutorial on using UPDATE and DELETE statements in MySQL
  • Example tutorial on using UPDATE statement in MySQL
  • Correct use of MySQL update statement

<<:  An example of how Vue implements four-level navigation and verification code

>>:  How to use Nginx to proxy multiple application sites in Docker

Recommend

CocosCreator Getting Started Tutorial: Making Your First Game with TS

Table of contents premise TypeScript vs JavaScrip...

Eight ways to implement communication in Vue

Table of contents 1. Component Communication 1. P...

MySQL data archiving tool mysql_archiver detailed explanation

Table of contents I. Overview 2. pt-archiver main...

Detailed steps to build a file server in Windows Server 2012

The file server is one of the most commonly used ...

Vue state management: using Pinia instead of Vuex

Table of contents 1. What is Pinia? 2. Pinia is e...

HTML optimization speeds up web pages

Obvious HTML, hidden "public script" Th...

Introduction to Vue3 Composition API

Table of contents Overview Example Why is it need...

How to generate mysql primary key id (self-increment, unique and irregular)

Table of contents 1. Use the uuid function to gen...

Simple understanding and examples of MySQL index pushdown (ICP)

Preface Index Condition Pushdown (ICP) is a new f...

Summary of commonly used tool functions in Vue projects

Table of contents Preface 1. Custom focus command...

Three ways to avoid duplicate insertion of data in MySql

Preface In the case of primary key conflict or un...

Some conclusions on the design of portal website focus pictures

Focus images are a way of presenting content that ...

JavaScript to add and delete messages on the message board

This article shares a small example of adding and...

SQL ROW_NUMBER() and OVER() method case study

Syntax format: row_number() over(partition by gro...