I want to create a primary key(auto-increment)
which is start with A001000001
.
Here A001
would be constant and 000001
would be iterate.
I want rows like this:
How can I do it using SQL query?
I want to create a primary key(auto-increment)
which is start with A001000001
.
Here A001
would be constant and 000001
would be iterate.
I want rows like this:
How can I do it using SQL query?
its much easier than you think ;)
CREATE TABLE bob ( id MEDIUMINT NOT NULL AUTO_INCREMENT, foo CHAR(30) NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM;
For SQL Server (you didn't clearly specify which RDBMS you're using), my suggestion would be:
INT IDENTITY
column to your table - and quite frankly, I would make that column the primary keySomething like:
That way, if you insert a row, the
ID
gets set automatically, and theEmpID
will also be set automatically to values likeA0010000001
,A0010000002
, .... and so forth.If you want to put the primary key on the
EmpID
column, then you need to make sure to include theNOT NULL
specification when creating it:and then you can put the primary key constraint on that column:
You can create BEFORE INSERT trigger, check MAX
id
value, increment it, add prefix, and then insert.