- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add a column to a MySQL table which is the result of concatenation of text and value from another auto increment column?
For this, you can use LAST_INSERT_ID(). Let us first create a table −
mysql> create table DemoTable ( UserId int(6) unsigned zerofill NOT NULL AUTO_INCREMENT, UserAutoIncrement char(100) default null, PRIMARY KEY(UserId) ); Query OK, 0 rows affected (0.72 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+--------+-------------------+ | UserId | UserAutoIncrement | +--------+-------------------+ | 000001 | NULL | +--------+-------------------+ 1 row in set (0.00 sec)
Following is the query to add a column to a MySQL table which is the result of concatenation −
mysql> update DemoTable set UserAutoIncrement=CONCAT('USER-', UserId) WHERE UserId = LAST_INSERT_ID(); Query OK, 1 row affected (0.20 sec) Rows matched: 1 Changed: 1 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
Output
+--------+-------------------+ | UserId | UserAutoIncrement | +--------+-------------------+ | 000001 | USER-000001 | +--------+-------------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to set my auto increment column ( id ) to zero or reset the value of auto increment field?
- Can we add a column to a table from another table in MySQL?
- How to create a table with auto-increment column in MySQL using JDBC?
- How to add auto-increment to column in MySQL database using PhpMyAdmin?
- MySQL DATE_ADD() to increment a date based on the value in another column?
- Increment column value ‘ADD’ with MySQL SET clause
- How to handle fragmentation of auto increment ID column in MySQL?
- Truncate a MySQL table and then set a custom value to auto increment
- Is it impossible to add a column in MySQL specifically before another column?
- How to set auto-increment to an existing column in a table using JDBC API?
- How do I INSERT INTO from one MySQL table into another table and set the value of one column?
- Fetch the first letter of a column value and insert it in another column with MySQL
- Update only a single column in a MySQL table and increment on the basis of a condition
- Set MySQL int column to auto increment by 1 beginning at 10000?
- How to auto increment with 1 after deleting data from a MySQL table?

Advertisements