Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Add data to existing data in a MySQL Database?
You can use CONCAT() function for this. Let us first create a table −
mysql> create table DemoTable ( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserName varchar(100) ); Query OK, 0 rows affected (0.43 sec)
Insert records in the table using insert command −
mysql> insert into DemoTable(UserName) values('John');
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable(UserName) values('Chris');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable(UserName) values('Robert');
Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+----------+ | UserId | UserName | +--------+----------+ | 1 | John | | 2 | Chris | | 3 | Robert | +--------+----------+ 3 rows in set (0.00 sec)
Here is the query to add data to existing data in MySQL database. The UserId 1 is concatenated with the name “Smith” to the already existed name−
mysql> update DemoTable set UserName=concat(UserName,' Smith') where UserId=1; Query OK, 1 row affected (0.12 sec) Rows matched: 1 Changed: 1 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+--------+------------+ | UserId | UserName | +--------+------------+ | 1 | John Smith | | 2 | Chris | | 3 | Robert | +--------+------------+ 3 rows in set (0.00 sec)
Advertisements
