Solved

VB.net remove all primary keys from SQL Table

Posted on 2014-12-23
6
285 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:Murray Brown
[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
6 Comments
 
LVL 34

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:Scott Pletcher
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 34

Expert Comment

by:ste5an
ID: 40515262
hmm, need new glasses. I really read tables..(
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:Murray Brown
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:Murray Brown
ID: 40521894
thanks
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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

623 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