Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Prefix to auto-increment in mysql db

Tags:

php

mysql

I have my database with table test1. It has a primary id "Id" which is auto-increment. Now the id is in the format 1,2,3.. . .Is it possible to store the primary Id as PNR1,PNR2,PNR3 .. . . and so on(with auto-increment).

like image 363
hjaffer2001 Avatar asked May 13 '11 11:05

hjaffer2001


People also ask

Can we insert auto increment value in MySQL?

Syntax for MySQLMySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature. By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record. VALUES ('Lars','Monsen'); The SQL statement above would insert a new record into the "Persons" table.

How do I add a prefix to a column in MySQL?

CREATE OR REPLACE VIEW yourViewName AS SELECT *FROM yourTableName; To understand the above syntax, let us create a table. Insert some records in the table using insert command. Display all records from the table using select statement.

Does MySQL primary key auto increment?

One of the important tasks while creating a table is setting the Primary Key. The Auto Increment feature allows you to set the MySQL Auto Increment Primary Key field. This automatically generates a sequence of unique numbers whenever a new row of data is inserted into the table.

How do I automatically generate numbers in MySQL?

If you want to avoid writing sql, you can also do it in MySQL Workbench by right clicking on the table, choose "Alter Table ..." in the menu. When the table structure view opens, go to tab "Options" (on the lower bottom of the view), and set "Auto Increment" field to the value of the next autoincrement number.


2 Answers

No. Either add the prefix in the query, or use a view instead.

like image 154
Ignacio Vazquez-Abrams Avatar answered Oct 19 '22 08:10

Ignacio Vazquez-Abrams


Not really, but you can use another column (but a view) this is already covered here: MySQL Auto Increment Custom Values

like image 4
radosch Avatar answered Oct 19 '22 06:10

radosch