How to add primary key in existing table?
-
Hi all :laugh: Currently now I used MS SQL Server Management Studio 2008. I have an existing table with data already store on that table. Table Name : Application Column : Categories (nvarchar(1), null) Sequence (nvarchar(1), null) Detail (nvarchar(150), null) I would like to ask how to add primary key to column Categories and Sequence ? Do I need to use alter query? Thank you in advance !
-
Hi all :laugh: Currently now I used MS SQL Server Management Studio 2008. I have an existing table with data already store on that table. Table Name : Application Column : Categories (nvarchar(1), null) Sequence (nvarchar(1), null) Detail (nvarchar(150), null) I would like to ask how to add primary key to column Categories and Sequence ? Do I need to use alter query? Thank you in advance !
Yes.
ALTER TABLE application
ADD CONSTRAINT pk_application PRIMARY KEY (categories,sequence)Learn more: 1. https://technet.microsoft.com/en-us/library/ms190273(v=sql.105).aspx[^] 2. http://www.w3schools.com/sql/sql_primarykey.asp[^]
-
Yes.
ALTER TABLE application
ADD CONSTRAINT pk_application PRIMARY KEY (categories,sequence)Learn more: 1. https://technet.microsoft.com/en-us/library/ms190273(v=sql.105).aspx[^] 2. http://www.w3schools.com/sql/sql_primarykey.asp[^]
Alright ! i got it now. Thank you Peter :-D
-
Hi all :laugh: Currently now I used MS SQL Server Management Studio 2008. I have an existing table with data already store on that table. Table Name : Application Column : Categories (nvarchar(1), null) Sequence (nvarchar(1), null) Detail (nvarchar(150), null) I would like to ask how to add primary key to column Categories and Sequence ? Do I need to use alter query? Thank you in advance !
Hi, for creating primary key u must need to maintain that columns as not null, so first execute this query to make it as not null
ALTER TABLE D_D_ext
ALTER COLUMN Categories NVARCHAR(1) NOT NULL ;
ALTER TABLE D_D_ext
ALTER COLUMN Sequence NVARCHAR(1) NOT NULL ;Then execute this query for creating Primary key
ALTER TABLE D_D_ext
ADD CONSTRAINT pk_D_D_ext_Categories_Sequence PRIMARY KEY (Categories, Sequence)Thanks dhamu
-
Hi, for creating primary key u must need to maintain that columns as not null, so first execute this query to make it as not null
ALTER TABLE D_D_ext
ALTER COLUMN Categories NVARCHAR(1) NOT NULL ;
ALTER TABLE D_D_ext
ALTER COLUMN Sequence NVARCHAR(1) NOT NULL ;Then execute this query for creating Primary key
ALTER TABLE D_D_ext
ADD CONSTRAINT pk_D_D_ext_Categories_Sequence PRIMARY KEY (Categories, Sequence)Thanks dhamu
Hi dhamu :) alright ! i try to apply the suggestion that you give. thank you for your response :-D
-
Hi all :laugh: Currently now I used MS SQL Server Management Studio 2008. I have an existing table with data already store on that table. Table Name : Application Column : Categories (nvarchar(1), null) Sequence (nvarchar(1), null) Detail (nvarchar(150), null) I would like to ask how to add primary key to column Categories and Sequence ? Do I need to use alter query? Thank you in advance !
You can't define a primary key on a nullable column, so first you will need to ensure there are no null values in the Categories column and in the Sequence column. Then you can alter the table so these columns are not null:
ALTER TABLE [Application] ALTER COLUMN Categories NVARCHAR(1) NOT NULL
ALTER TABLE [Application] ALTER COLUMN Sequence NVARCHAR(1) NOT NULLThen you can create your primary key:
ALTER TABLE [Application] ADD CONSTRAINT PK_Application PRIMARY KEY (Categories, Sequence)