Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7403083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:57:53+00:00 2026-05-29T04:57:53+00:00

I am student of RDBMS. I have very basic question let say I have

  • 0

I am student of RDBMS.

I have very basic question let say I have one existing Table in SQL server. What will be script to alter table.

  • Drop Column ‘RowId’ if exist.
  • Drop contraint if exist.
  • Add one new column ‘RowId’ into table.
  • Make this column as primary key.
  • Autoincrement type int.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:57:55+00:00Added an answer on May 29, 2026 at 4:57 am

    In SQL Server 2005 or newer, you could use this script:

    -- drop PK constraint if it exists
    IF EXISTS (SELECT * FROM sys.key_constraints WHERE type = 'PK' AND parent_object_id = OBJECT_ID('dbo.YourTable') AND Name = 'PK_YourTable')
       ALTER TABLE dbo.YourTable
       DROP CONSTRAINT PK_YourTable
    GO
    
    -- drop column if it already exists
    IF EXISTS (SELECT * FROM sys.columns WHERE Name = 'RowId' AND object_id = OBJECT_ID('dbo.YourTable'))
        ALTER TABLE dbo.YourTable DROP COLUMN RowId
    GO
    
    -- add new "RowId" column, make it IDENTITY (= auto-incrementing)
    ALTER TABLE dbo.YourTable 
    ADD RowId INT IDENTITY(1,1)
    GO
    
    -- add new primary key constraint on new column   
    ALTER TABLE dbo.YourTable 
    ADD CONSTRAINT PK_YourTable
    PRIMARY KEY CLUSTERED (RowId)
    GO
    

    Of course, this script may still fail, if other tables are referencing this dbo.YourTable using foreign key constraints onto the pre-existing RowId column…

    Update: and of course, anywhere I use dbo.YourTable or PK_YourTable, you have to replace those placeholder with the actual table / constraint names from your own database (you didn’t mention what they were, in your question…..)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table student ( id, name ). Then I have one textbox,
Say I have a Student table, it's got an int ID. I have a
I have a STUDENT table which contains data about students from a faculty: STUDENT(ID,
I have a table student like this id | name | zip 1 |
A student in my class brought this to my attention, and I didn't have
I have Class and Student objects. Both have collection of another as property. Which
I am a student at statistics department and I have a thesis about the
I have a Student and a Teacher model in my app. I want a
A student of mine submitted some C++ code similar to the following one. The
I am using MATLAB Student Version R2011a. I am creating a GUI-table to compare

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.