Solved

VB.net remove all primary keys from SQL Table

Posted on 2014-12-23
6
250 Views
Last Modified: 2014-12-29
Hi

What VB.net code would I use to remove all the primary keys from a SQL table?

Thanks
0
Comment
Question by:murbro
6 Comments
 
LVL 32

Expert Comment

by:ste5an
ID: 40515167
You would include SQL Server Management Objects in your references and use the IndexCollection to find all indices which are primary keys. Then you may be able to delete them as long as no foreign key relationship exists.

Something like
foreach (Table table in database.Tables)
{
    Index primaryKey = table.Indexes
		.FirstOrDefault(index => index.IndexKeyType == IndexKeyType.DriPrimaryKey);
    if (primaryKey != null)
    {
        primaryKey.Drop();
        table.Alter();
    }
}

Open in new window

.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40515221
A given table could have only one primary key, so you don't need to worry about any kind of looping or multiple entries.
0
 
LVL 32

Expert Comment

by:ste5an
ID: 40515262
hmm, need new glasses. I really read tables..(
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:murbro
ID: 40516300
I use the following code to add the keys. What similar code would I use to remove them
          Dim oSQL As String
            oSQL = "ALTER TABLE " & oTable
            oSQL = oSQL & " ADD CONSTRAINT pk_" & oTable & " PRIMARY KEY ([" & oColumn1 & "], [" & oColumn2 & "])"
            'oSQL = oSQL & " GO"
            Dim cn As New SqlConnection(Globals.ThisAddIn.oRIGHT.lblConnectionString.Text)
            Dim cmd As New SqlCommand(oSQL, cn)
            cn.Open()
            cmd.ExecuteNonQuery()
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 40521004
I would change:
 oSQL = oSQL & " ADD CONSTRAINT pk_" & oTable & " PRIMARY KEY ([" & oColumn1 & "], [" & oColumn2 & "])"
To:
oSQL = oSQL & " DROP CONSTRAINT pk_" & oTable
0
 

Author Closing Comment

by:murbro
ID: 40521894
thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now