How to alter column with primary key and identity [duplicate] - sql-server

This question already has answers here:
SQL Server add auto increment primary key to existing table
(16 answers)
Closed 8 years ago.
How to alter column in the existing table for the primary key and identity. I tried below query but showing Incorrect syntax near the keyword 'identity'.
alter table IAM_Software_Licence_Master
alter column SoftwareLicId int identity(1,1) primary key
How can I achieve this..

Its difficult to say what the issues are with changing this on your end as I do not know the table structure or whether or not this is used as a foreign Key. But the below link should give you everything you need:
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/04d69ee6-d4f5-4f8f-a115-d89f7bcbc032/how-to-alter-column-to-identity11

Related

Using alphanumeric instead of INT in my primary key table. The table ID is going to be including A1, A2 or B1,C1 [duplicate]

This question already has answers here:
How to generate unique incremental employee code(pattern: E0001, E0002, E0003, ... ) in the INSERT stored procedure in SQL Server?
(4 answers)
Closed 7 months ago.
I am building this project with SQL Server.
I need to create a Shop table, this shop (numbers) will include alphanumeric as the shop's identification number.
How do I create a table with alphanumeric primary key instead of using INT as data type? And will this table still increment like when using INT for the primary key?
As for the data type, you can use "nvarchar" to be able to deal with letters and numbers, for increment you have to do it manually (either in code or in a function in the database)
You can use guid and below is how you define your primary column.
CREATE TABLE table_name
(
id UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY
);
Read more on guid here

ALTER TABLE statement conflicted with the FOREIGN KEY SAME TABLE constraint [duplicate]

This question already has an answer here:
FOREIGN KEY SAME TABLE error - but foreign key doesn't exist
(1 answer)
Closed 6 years ago.
I like to know if it's possible to create a self refering constraint using an ALTER TABLE statement. (tsql SQL Server 2012)
expected I have a table with just a Primary Key called ID and column called parent_id
I want to do something like this:
ALTER TABLE myTable
ADD CONSTRAINT FK_myTablemyTable
FOREIGN KEY (parent_Id)
REFERENCES myTable(Id)
but I just get the error:
The ALTER TABLE statement conflicted with the FOREIGN KEY SAME TABLE constraint
Is there an alternative way to get the statement working, or do you have to recreate the whole table in this case ?
Hope somebody is able to help
Cheers
Problem solved
I am creating some kind of table migration tool and have not realized that the data was inserted already ...
Problem was that the root element was Inserted with parent_id = 0 and not null...
So there is no mistake in the SQL statement.
now it's working

The INSERT statement conflicted with the FOREIGN KEY constraint Please Verify [duplicate]

This question already has answers here:
The INSERT statement conflicted with the FOREIGN KEY constraint
(3 answers)
Closed 6 years ago.
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__faredestin__svno__503BEA1C". The conflict occurred in database
"travelproject", table "dbo.bus", column 'svno'. The statement has
been terminated.
svno column is used as FOREIGN KEY in table.you have to consider FOREIGN KEY hierarchy while insert/delete operation.And FOREIGN KEY must be unique value and record must be in main table for given FOREIGN KEY.
while Inserting in to table check what value goes in svno column and check whether their is record for given svno in table where svno is used as a PRIMERY KEY.

SQL Server 2008 : ALTER IDENTITY TYPE [duplicate]

This question already has answers here:
How to Alter a table for Identity Specification is identity SQL Server
(3 answers)
Closed 8 years ago.
I have an existing table MCheckTypes and it has a primary key column ID. What I want to do is to alter the table to add identity type on the primary key. I don't want to drop a table and recreate a new one. I already google it but no luck. Below is my script
ALTER TABLE MCheckTypes
ALTER COLUMN [ID] INT IDENTITY(1, 1) NOT NULL
below is the error message
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'IDENTITY'.
You cannot "convert" an existing column into an IDENTITY column - you have to create a new column as INT IDENTITY:
ALTER TABLE MCheckTypes
ADD NewID INT IDENTITY (1, 1) NOT NULL;

Remove auto increment By Script SQL Server [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
SQL Server how to drop identity from a column
How I alter a Table by script to remove autoindenty of a column?
Add new NON-autoincrementing column of the same type with default constraint to your identity column
drop default constraint
drop identity column
rename new column to old one with sp_rename

Resources