Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi, I have a table where I wan't to remove the Primary key. I run the sql command of "ALTER TABLE table_name DROP CONSTRAINT Primary_key"

 

The error I get is "CHECK constraint 'Primary_Key' does not exist.

 

When I added the Primary key, I used the sql command of "ALTER TABLE table_name ALTER COLUMN column_name varchar(30) PRIMARY KEY"

 

This added fine because I now cannot drop the column.

 

I am trying to find the constraint name so I can delete it. I have run code to try to find the constraint name. This is what I have run:

 

Dim cs As Constraint

For Each cs In dsBudget.Tables.Item(0).Constraints

Console.WriteLine(cs.ConstraintName)

Next cs

 

The problem I am facing is that this code cannot pickup any constraints. If I use code to find a primary key, the array returns -1 indicating that there is no primary key.

 

The table or column definitely has a primary key.

 

My question is, if I can't find out the name of the constraint, how can I remove it?

 

Thank you for your assistance

  • Administrators
Posted

The code you have posted will display any constraints on the DataSet, not on the database table. If you are using MS SQL then

SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE  =  'PRIMARY KEY'
AND TABLE_NAME = '</pre><table name here>'
</

will return the name of the primary key for a given table.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

I can see the constraint when I create a dataset & view the xml schema in design mode. It has a key next to afield.

 

I ran this command "SELECT CONSTRAINT_NAME FROM Account_Details.TABLE_CONSTRAINTS

WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_NAME = 'Account_Codes'"

 

and got this error "The Microsoft Jet database engine cannot find the input table or query 'TABLE_CONSTRAINTS'. Make sure it exists and that its name is spelled correctly."

 

I'm sorry, I failed to mention that I am using MS Jet database engine to access my Access 2000 database.

 

I changed "INFORMATION_SCHEMA" to the database name because an error said it couldn't find table INFORMATION_SCHEMA

 

Is there another thing I could try?

 

Thank you for your assistance

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...