I have old SQL sever table with 5000 rows. It has a column called OrderID which has the data type int. But this table doesn't have a primary key and OrderID is not on the sorted order. Can you please tell me how can I make this OrderID column the primary key and make it auto increment
You can't add identity to an existing column.
Your best option is to create a new table with the same structure and an identity column, set identity_insert on and then copy your records from the old one into the new one.
Check out this answer from the MS SQL Forum
You can't add identity to existing column. Create a new column "new_OderId" , copy data from "OderId" column paste in "new_OderId" column.
#add new column to Order_table
alter table Order_table add new_OderId int
#copy data from OrderId to new_OrderId
update Order_table set new_OrderId=OderId
#drop OderId column
alter table Order_table drop column OrderId
Related
GOAL
I would like to add a Redshift SUPER column to and existing redshift table.
I need this to store JSON data there
CODE
This is how Normally I would add a new column.
ALTER TABLE products
ADD COLUMN created_at NOT NULL;
1. Tried
CREATE TABLE temp_test_persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
ALTER TABLE temp_test_persons ADD COLUMN newSuperColumn SUPER NOT NULL;
Error running query: ALTER TABLE ADD COLUMN defined as NOT NULL must have a non-null default expression
Reviewed Solutions
Alter column data type in Amazon Redshift
AWS Redshift - Add IDENTITY column to existing table
Adding column to existing tables
Add dynamic column to existing MySQL table?
SQLAlchemy: How to add column to existing table?
adding columns to an existing table
add column to existing table postgres
How to add not null unique column to existing table
Adding columns to existing csv file using super-csv
UCanAccess: Add column to existing table
Adding columns to existing redshift table
How to add column to existing table in laravel?
Add column to existing table in rds
oracle add column to existing table
The solution is to set a default value for the column. Then you can define the column as NotNull.
Like this
ALTER TABLE temp_test_persons
ADD COLUMN newSuperColumn SUPER NOT NULL
DEFAULT '';
ALTER TABLE temp_test_persons
ADD COLUMN newSuperColumn SUPER;
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 would like to create a new column called PurchaseOrderID in an existing table using SSMS. It combines LineNumber and PONUMBER to create a surrogate key and then I would enter into table design mode and assign it a PK there.
Creating new column:
ALTER TABLE FactPurchaseOrders
ADD PurchaseOrderID VARCHAR(64);
Populating with values:
UPDATE FactPurchaseOrders
SET PurchaseOrderID = (CONVERT(VARCHAR(64), LineNumber) + CONVERT(VARCHAR(64), PONUMBER))
WHERE 1=1;
Currently with this I am unable to assign this column a PK and I believe because it is nullable.
I have also tried creating it in design mode first and the same problem occurs.
That is most certainly the case. After you have run your update, simply alter the columns you will use in your PK to NOT NULL. Since the columns will have values now it can be set as NOT NULL and then it will allow the assignment of PK. Also make sure there isn't already another PK on the table already. There can be only 1!
Update Records
Alter columns to not null
Create PK on fields
I'm new to SQL. I created a table and filled it with some data. Now I want to change the properties of my primary key, so it will auto increment every single time by itself when adding data.
I know how to do it when creating a table, but where do I add auto_increment now, when I already have a created table with filled data?
Update
Try
ALTER TABLE [TableName] DROP COLUMN ID
ALTER TABLE [TableName] ADD ID INT IDENTITY(1,1)
If you had entered data to your table review this link that have an answer to you question SQL Server, How to set auto increment after creating a table without data loss?
You can't change logical structure column when you have some filled data in column. That's impossible. You must deleting data from column and use ALTER TABLE to change properites column
I have an existing table where I use existing column (type INT) as PK and manually increment its value with each row inserted. I wanted to change it to IDENTITY with auto increment. I found a thread here (http://stackoverflow.com/questions/4862385/sql-server-add-auto-increment-primary-key-to-existing-table) that seems to achieve exactly what I want. But every time I run the ALTER statement, Mgmt Studio crashes.
I had also tried to achieve my above goal by changing the column properties manually (Identity specification/Is Identity:yes) as in this thread (http://stackoverflow.com/questions/3876785/sql-server-cant-insert-null-into-primary-key-field). But every time I close the table after changing properties, I get an error
'Pix' table
Unable to modify table.
Cannot insert the value NULL into column 'picID', table 'photo.dbo.Tmp_Pix'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Not sure what's going on.
You cannot change an existing column to become an IDENTITY column.
What you need to do is:
create a new column with INT IDENTITY
drop the primary key constraint
drop the old column
add the primary key constraint on the new column
The trouble might be - if you already have data in that table - that the new identity values don't necessarily match the old values in your manual ID column.
If you need to preserve those, then it gets even more involved:
create a new table with the proper structure, and make sure that the ID column is INT IDENTITY
turn on IDENTITY_INSERT for that table
insert all the rows from the old table into the new one (and in the process, insert the old ID values into the new ID IDENTITY column)
turn off IDENTITY_INSERT for that table
drop the old table
possibly rename the new table