
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- How to increase varchar size of an existing column in a database without breaking existing data in MySQL?
- How to update data in a MySQL database without removing the old data?
- How to delete data in a MySQL database with Java?
- How to update data in a MySQL database with Java?
- How to insert data into a MySQL database with Java?
- How to insert raw data in MySQL database in Laravel?
- How to structure some data in a MySQL database for easier retrieval?
- Best data type for storing currency values in a MySQL database?
- How to add a new column at the front of an existing R data frame?
- Inserting data into a new column of an already existing table in MySQL?
- How can I modify an existing MySQL column’s data type?
- How to write PHP script to delete data from an existing MySQL table?
- Add a new value to a column of data type enum in MySQL?
- How to add +1 to existing MySQL values?
- Which PHP function is used to insert data into an existing MySQL table?

Advertisements