Home > Database > Mysql Tutorial > Set similar values ​​for columns in MySQL table?

Set similar values ​​for columns in MySQL table?

PHPz
Release: 2023-09-02 09:17:08
forward
849 people have browsed it

为 MySQL 表中的列设置类似的值?

You can set values ​​for all records in a column with the help of the update command.

If you want to set NULL values ​​for all records in a column, the syntax is as follows -

update yourTableName set yourColumnName = NULL;
Copy after login

Or, if you want to use an empty string, the syntax is as follows -

update yourTableName set yourColumnName = ’’;
Copy after login

To understand the above concept, let us create a table. Query to create table.

mysql> create table StudentDemo
   −> (
   −> Studentid int,
   −> StudentName varchar(100),
   −> Age int
   −> );
Query OK, 0 rows affected (0.64 sec)
Copy after login

The following is the table into which records are inserted-

mysql> insert into StudentDemo values(1,'Johnson',23);
Query OK, 1 row affected (0.18 sec)

mysql> insert into StudentDemo values(2,'Carol',24);
Query OK, 1 row affected (0.16 sec)

mysql> insert into StudentDemo values(3,'David',20);
Query OK, 1 row affected (0.18 sec)

mysql> insert into StudentDemo values(4,'Bob',21);
Query OK, 1 row affected (0.19 sec)
Copy after login

Use the select statement to display all records in the table-

mysql> select *from StudentDemo;
Copy after login
Copy after login

The following is the output-

+-----------+-------------+------+
| Studentid | StudentName | Age |
+-----------+-------------+------+
|         1 | Johnson     | 23   |
|         2 | Carol       | 24   |
|         3 | David       | 20   |
|         4 | Bob         | 21   |
+-----------+-------------+------+
4 rows in set (0.00 sec)
Copy after login

The following The query sets the column value to NULL for all records in a specific column. The query is as follows -

mysql> update StudentDemo set Age=NULL;
Query OK, 4 rows affected (0.14 sec)
Rows matched: 4 Changed: 4 Warnings: 0
Copy after login

Now let us check it -

mysql> select *from StudentDemo;
Copy after login
Copy after login

The following output shows that we have successfully updated the "Age" column to NULL -

+-----------+-------------+------+
| Studentid | StudentName | Age |
+-----------+-------------+------+
|         1 | Johnson     | NULL |
|         2 | Carol       | NULL |
|         3 | David       | NULL |
|         4 | Bob         | NULL |
+-----------+-------------+------+
4 rows in set (0.00 sec)
Copy after login

The above is the detailed content of Set similar values ​​for columns in MySQL table?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template