Updating a MySQL column that contains dot (.) in its name?

MySQLMySQLi Database

If the MySQL column contains dot (.) in its name, then you need to use backticks around the column name. To understand the above concept, let us create a table. The query to create a table is as follows

mysql> create table UpdateDemo
   -> (
   -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> `User.FirstName.LastName` varchar(60)
   -> );
Query OK, 0 rows affected (0.54 sec)

Insert some records in the table using insert command.

The query is as follows

mysql> insert into UpdateDemo(`User.FirstName.LastName`) values('John Smith');
Query OK, 1 row affected (0.14 sec)
mysql> insert into UpdateDemo(`User.FirstName.LastName`) values('Adam Smith');
Query OK, 1 row affected (0.12 sec)
mysql> insert into UpdateDemo(`User.FirstName.LastName`) values('Carol Taylor');
Query OK, 1 row affected (0.25 sec)
mysql> insert into UpdateDemo(`User.FirstName.LastName`) values('Mitchell Johnson');
Query OK, 1 row affected (0.19 sec)
mysql> insert into UpdateDemo(`User.FirstName.LastName`) values('David Brown');
Query OK, 1 row affected (0.16 sec)
mysql> insert into UpdateDemo(`User.FirstName.LastName`) values('Larry Miller');
Query OK, 1 row affected (0.22 sec)

Display all records from the table using select statement.

The query is as follows

mysql> select *from UpdateDemo;

The following is the output

+--------+-------------------------+
| UserId | User.FirstName.LastName |
+--------+-------------------------+
| 1      | John Smith              |
| 2      | Adam Smith              | 
| 3      | Carol Taylor            |
| 4      | Mitchell Johnson        |
| 5      | David Brown             |
| 6      | Larry Miller            |
+--------+-------------------------+
6 rows in set (0.00 sec)

Use backticks around the column name User.FirstName.LastName which contains (.).

The query is as follows

mysql> update UpdateDemo set `User.FirstName.LastName`='David Miller' where UserId=5;
Query OK, 1 row affected (0.19 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Let us check the table record once again. The row with UserId=5 is successfully updated.

The query is as follows

mysql> select *from UpdateDemo;

The following is the output

+--------+-------------------------+
| UserId | User.FirstName.LastName |
+--------+-------------------------+
| 1      | John Smith              |
| 2      | Adam Smith              |
| 3      | Carol Taylor            |
| 4      | Mitchell Johnson        |
| 5      | David Miller            |
| 6      | Larry Miller            |
+--------+-------------------------+
6 rows in set (0.00 sec)
raja
Published on 29-Mar-2019 11:06:40
Advertisements