I have created this table and I inserted the following values:
CREATE TABLE Product
(
ID INTEGER IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
Product_No AS RIGHT ('PDT0000' + CAST(ID AS VARCHAR(10)),10) PERSISTED,
Product_Name VARCHAR(50) NOT NULL
)
INSERT INTO Product(Product_Name)
VALUES('Fish'), ('Shrimp'), ('Crab')
Then by using
SELECT *
FROM Product
I get the following results:
ID Product_No Product_Name
1 PDT00001 Fish
2 PDT00002 Shrimp
3 PDT00003 Crab
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
SELECT *
FROM Product (with Product_No as the Primary Key):
Product_No Product_Name
-------------------------
PDT00001 Fish
PDT00002 Shrimp
PDT00003 Crab
So this is what I have tried:
CREATE TABLE Product
(
Product_No INTEGER IDENTITY(1,1) PRIMARY KEY ('PDT0000' + Product_No AS VARCHAR(10)),10),
Product_Name VARCHAR(50) NOT NULL
)
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.