Here is my table and Id is unique constraint...Now I want to add Identity Column in ID .Can anyone help me how to do it with TSQL.
This is existing table.
You can only have 1 Identity Column per Table. So if you don't have one already, Just alter the table and add the Column. Like this
ALTER TABLE YourTableName
ADD IdCol INT IDENTITY(1,1)
You can rename your previous ID column to keep those values if you need them in your application
EXEC sp_rename 'TableName.id', 'oldid', 'COLUMN';
Then add new ID column with Unique constraint as follows
Alter table TableName Add id int identity(1,1) unique not null
Related
I have a table which is having a column name ID field of integer type. It was declared IDENTITY. And it has data according to IDENTITY. But recently I removed IDENTITY column from that table. Now I want to change that to IDENTITY again. But this query says incorrect syntax
Alter table FuleConsumptions alter column TransactionID INT IDENTITY(1,1);
But I can perform the same task using SQL server designer in properties of the table.
What am I doing wrong here?
I think The identity column will hold the sequence of number thats why it thrown error
better you drop column then create it again and set IDENTITY
Alter Table FuleConsumptions Drop Column TransactionID
Go
ALTER TABLE FuleConsumptions
ADD TransactionID int;
go
Alter table FuleConsumptions alter column TransactionID INT IDENTITY(1,1);
I am trying to add primary key to newly added column in existing table name Product_Details.
New Column added: Product_Detail_ID (int and not null)
I am trying add primary key to Product_Detail_ID (please note: there are no other primary or foreign key assigned to this table)
I am trying with this query but getting error.
ALTER TABLE Product_Details
ADD CONSTRAINT pk_Product_Detils_Product_Detail_ID PRIMARY KEY(Product_Detail_ID)
GO
Error:
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.Product\_Details' and the index name 'pk\_Product\_Detils'. The duplicate key value is (0).
Am I missing something here? I am using SQL Server 2008 R2. I would appreciate any help.
If you want SQL Server to automatically provide values for the new column, make it an identity.
ALTER TABLE Product_Details DROP COLUMN Product_Detail_ID
GO
ALTER TABLE Product_Details ADD Product_Detail_ID int identity(1,1) not null
GO
ALTER TABLE Product_Details
add CONSTRAINT pk_Product_Detils_Product_Detail_ID primary key(Product_Detail_ID)
GO
In mysql, I was able to achieve with following query
ALTER TABLE table_name ADD new_column int NOT NULL AUTO_INCREMENT primary key
Add Primary Key to First Position
ALTER TABLE table_name
ADD column_name INT PRIMARY KEY AUTO_INCREMENT FIRST;
Reference: Stack Overflow | Tech On The Net
You are getting the error because you have existing data that does not fullfill the constraint.
There are 2 ways to fix it:
clean up the existing data before adding the constraint
add the constraint with the "WITH NOCHECK" option, this will stop sql server checking existing data, only new data will be checked
ALTER TABLE Jaya
ADD CONSTRAINT no primary key(No);
here Jaya is table name,
no is column name,
ADD CONSTRAINT is we giving the primary key keyword
If you want to add a new column say deptId to the existing table say department then you can do it using the below code.
ALTER TABLE department ADD COLUMN deptID INT;
it will create your new column named deptID.
now if you want to add constraint also along with new column while creating it then you can do it using the below code. In the below code I am adding primary key as a constraint. you can add another constraint also instead of primary key like foreign key, default etc.
ALTER TABLE department ADD COLUMN deptID INT NOT NULL ADD CONSTRAINT PRIMARY KEY(deptID);
k. friend
command:
sql> alter table tablename add primary key(col_name);
ex: alter table pk_Product_Detils add primary key(Product_Detail_ID);
I am facing one problem. I have a table already create in DB2.
CREATE TABLE "DDL12"
(
"D4_1" decimal(10,0),
"D4_2" decimal(10,0),
);
I am trying to create a PK on this table as :-
ALTER TABLE "DDL12" ADD CONSTRAINT "Key4" PRIMARY KEY ("D4_1");
But while running the command, I am getting the error saying D4_1 is NULLABLE.
Now, how can I create a PK on this table?
Thanks
Yes, this is due the fact, that your database "could have" rows having NULL value in that non PK column right now.
So first set the column to NOT NULL (+ make sure having a unique value in all rows) and then set the primary key with the command above.
You can change a column to not NULL like this:
ALTER TABLE "DDL12"
MODIFY "D4_1" decimal(10,0) NOT NULL;
I have a table set up that currently has no primary key. All I need to do is add a primary key, no null, auto_increment.
I'm working with a Microsoft SQL Server database. I understand that it can't be done in a single command but every command I try keeps returning syntax errors.
edit ---------------
I have created the primary key and even set it as not null. However, I can't set up the auto_increment.
I've tried:
ALTER TABLE tableName MODIFY id NVARCHAR(20) auto_increment
ALTER TABLE tableName ALTER COLUMN id NVARCHAR(20) auto_increment
ALTER TABLE tableName MODIFY id NVARCHAR(20) auto_increment
ALTER TABLE tableName ALTER COLUMN id NVARCHAR(20) auto_increment
I'm using NVARCHAR because it wouldn't let me set NOT NULL under int
It can be done in a single command. You need to set the IDENTITY property for "auto number":
ALTER TABLE MyTable ADD mytableID int NOT NULL IDENTITY (1,1) PRIMARY KEY
More precisely, to set a named table level constraint:
ALTER TABLE MyTable
ADD MytableID int NOT NULL IDENTITY (1,1),
CONSTRAINT PK_MyTable PRIMARY KEY CLUSTERED (MyTableID)
See ALTER TABLE and IDENTITY on MSDN
If the table already contains data and you want to change one of the columns to identity:
First create a new table that has the same columns and specify the primary key-kolumn:
create table TempTable
(
Id int not null identity(1, 1) primary key
--, Other columns...
)
Then copy all rows from the original table to the new table using a standard insert-statement.
Then drop the original table.
And finally rename TempTable to whatever you want using sp_rename:
http://msdn.microsoft.com/en-us/library/ms188351.aspx
You can also perform this action via SQL Server Management Studio.
Right click on your selected table -> Modify
Right click on the field you want to set as PK --> Set Primary Key
Under Column Properties set "Identity Specification" to Yes, then specify the starting value and increment value.
Then in the future if you want to be able to just script this kind of thing out you can right click on the table you just modified and select
"SCRIPT TABLE AS" --> CREATE TO
so that you can see for yourself the correct syntax to perform this action.
If you have the column it's very easy.
Using the designer, you could set the column as an identity (1,1): right click on the table → design → in part left (right click) → properties → in identity columns, select #column.
Properties:
Identity column:
In SQL Server 2008:
Right click on table
Go to design
Select numeric datatype
Add Name to the new column
Make Identity Specification to 'YES'
you can try this...
ALTER TABLE Your_Table
ADD table_ID int NOT NULL PRIMARY KEY auto_increment;
I'm creating a table that needs to have 2 columns. The first column can't be repeated. The thing is, I will insert the value of the first column. How do I create this column?
SQLServer 2005
Make the first column the primary key of the table.
Set the column as a primary key. I doesn't have to be an identity column to has a PK.
Create it the same way you would any other column: create table sometable (column1 varchar(10), column2 varchar(20)) or whatever.
Do you mean: How can you get the database to force it to be unique? Either declare it to be the primary key, or create a unique index on the column.
Perhaps you're thinking that a primary key must be auto-generated? There's no such rule. Whether you invent the value yourself or use an autonumber feature has nothing to do with whether a field can be a primary key.
Why not just put a unique constraint on it:
ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name>
UNIQUE(<column_name>)