Skip to content
Advertisement

How to create a custom auto generated ID number for a primary key column?

I have created this table and I inserted the following values:

Then by using

I get the following results:

As you can see that ID is the primary key column .. not Product_No … I want Product_No to be the primary key column here without requiring to create ID column. Therefore, this is the output I want to get with

So this is what I have tried:

This is the following error I got:

Incorrect syntax near ‘PDT0000’.

So, how do I fix this ?

It would be really helpful if the appropriate syntax solution is provided.

Advertisement

Answer

If you want to make Product_No the primary key – just use this SQL syntax:

Now your column Product_No is the primary key for this table. You cannot combine the IDENTITY value column with the computed column specification into a single column – that’s just not possible.

HOWEVER: even with this, and your sample data, this query:

still returns this result:

But what is wrong with that output?? I don’t quite understand why this is a problem in your case?

If you don’t want (or create for) the ID column, just run this query:

to get this result:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement