[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Changing a Data Type from INT to BIGINT

Posted on 2013-02-04
3
Medium Priority
?
776 Views
Last Modified: 2013-03-27
I have a PK column which was created as a INT IDENTITY(1,1) NOT FOR REPLICATION NOT NULL - I need to change this columns DataType to BIGINT

Can I just drop the primary key constraint and then

--ALTER TABLE [TableName]
--ALTER COLUMN  ColumnName  BIGINT NOT NULL

If I do this will it keep the same structure with the IDENTITY field and the not for replication not null or do I need to do something different

Thanks In Advance
0
Comment
Question by:VitaminD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 10

Accepted Solution

by:
deviprasadg earned 2000 total points
ID: 38853868
It should keep the same structure with the IDENTITY field and the not for replication not null.
Your plan looks good.

Below is the demo script:
DROP TABLE TESTPK
GO

CREATE TABLE TESTPK(
ID INT IDENTITY(1,1) NOT FOR REPLICATION NOT NULL ,
A VARCHAR(100))
GO

ALTER TABLE TESTPK ADD CONSTRAINT PKTESTPK PRIMARY KEY (ID)
GO

INSERT INTO TESTPK(A)
SELECT 'A'
GO 10

SELECT * FROM TESTPK
GO

ALTER TABLE TESTPK
DROP CONSTRAINT PKTESTPK

GO

ALTER TABLE TESTPK
ALTER COLUMN ID BIGINT

GO

ALTER TABLE TESTPK ADD CONSTRAINT PKTESTPK PRIMARY KEY (ID)

GO

INSERT INTO TESTPK(A)
SELECT 'A'
GO 10

SELECT * FROM TESTPK
GO

Open in new window

0
 
LVL 4

Expert Comment

by:krtyknmsql
ID: 38853926
Yes, You can drop the Primary Key constraint then alter the column to change the data type and recreate the primary key constraint as Deviprasad mentioned with three steps.

ALTER TABLE Tbl DROP CONSTRAINT PK_Tbl
GO

ALTER TABLE Tbl ALTER COLUMN ID BIGINT 
GO

ALTER TABLE Tbl ADD CONSTRAINT PK_Tbl PRIMARY KEY (ID)
GO

Open in new window

0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38854257
which version/edition of sql server?
how big is the table rows/columns?
is the column involved in any other parent/child relations ...? (ie is it used as a foreign key in any other tables?)

is the table actually being published (or is it the destination of a publication) as part of a replication strategy?

...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question