Click here to Skip to main content
15,893,923 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Write a query for primary key constraint with identity key word?
It was asked in one of my interviews..plz let me know the answer
Posted
Updated 15-May-20 6:12am
v2
Comments
RDBurmon 30-May-12 10:50am    
Thanks Everyone to give answer on this post

Hello OP , Hope you have got answer for your solution, but look the thread is still open.Please see the below answer for other CP members and accept those if worked for you. Or else If you have resolved it by yourself then add that as a solution to this thread and close the topic.

Thanks
RDBurmon Sr.Software Engineer

SQL
CREATE TABLE [dbo].[Tbl1]
(
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
     CONSTRAINT [PK_Tbl1] PRIMARY KEY CLUSTERED
     (
       [Id] ASC
     )WITH (PAD_INDEX  = OFF,
            STATISTICS_NORECOMPUTE  = OFF,
            IGNORE_DUP_KEY = OFF,
            ALLOW_ROW_LOCKS  = ON,
            ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
 
Share this answer
 
Comments
Maciej Los 30-May-12 7:53am    
Good answer, my 5!
Here:
SQL
CREATE TABLE [dbo].[TestTable]
(
   ID int IDENTITY(1,1)   NOT NULL,
   FirstName varchar(100) NULL,
   CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED([ID] ASC)
)

OR
SQL
CREATE TABLE CUSTOMERS(
       ID  INT IDENTITY(1,1) NOT NULL,
       NAME VARCHAR (20)     NOT NULL,
       AGE  INT              NOT NULL,
       ADDRESS  CHAR (25) ,
       SALARY   DECIMAL (18, 2),       
       PRIMARY KEY (ID)
);


Refer:
SQL - Primary Key[^]
MSDN: CREATE TABLE (Transact-SQL)[^]
 
Share this answer
 
Comments
Maciej Los 30-May-12 7:53am    
Good answer, my 5!
Sandeep Mewara 30-May-12 14:35pm    
Thanks losmac.
Try this:
SQL
CREATE TABLE Demo( DemoId int identity (1,1) Primary key, UserID int, Name nvarchar(100))


Also refer:
sql - create[^]
 
Share this answer
 
Comments
Maciej Los 30-May-12 7:53am    
Good answer, my 5!
Prasad_Kulkarni 30-May-12 8:15am    
Thank you Isomac!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900