Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i am searching for a solution for a few hours now. I created a table in sql_ce with the follwoing query:
SQL
CREATE TABLE [Customer]
([_id] int NOT NULL  IDENTITY (1,1),
[firstName] nvarchar(4000) NULL,
[lastName] nvarchar(4000) NULL,
[sex] nvarchar(4000) NULL,
[orderAddress_fk] int NULL,
[deliveryAddress_fk] int NULL,
[paymendMethod_fk] int NULL,
[emailAddress] nvarchar(4000) NULL,
[phone] nvarchar(4000) NULL,
[mobilePhone] nvarchar(4000) NULL,
[company] nvarchar(4000) NULL);

and added a foreignkeyconstraint. it was created succesfully, because i am not able to drop that table. i used the following query:
SQL
ALTER TABLE Customer
ADD CONSTRAINT orderAddress_fk
FOREIGN KEY (orderAddress_fk) REFERENCES Address(_id)

so i want to drop the constraint and tried the follwing commands:

SQL
ALTER TABLE [Customer]
ALTER COLUMN orderAddress_fk
DROP DEFAULT

SQL
ALTER TABLE [Customer]
DROP CONSTRAINT orderAddress_fk

the query executes with succes but after executing, i am still not able to drop the table, and the UI shows that the constraint still exists...

i dont know what to try else... :(
Posted
Updated 22-Dec-12 10:04am
v3
Comments
Zoltán Zörgő 22-Dec-12 16:33pm    
And what happens if you delete the constraint from the UI - supposing SQL Management Studio?
ChrisTopherus 22-Dec-12 18:22pm    
I am using SQLSM Express 2008 and there is no option in the propertys window, allowing to define any constrains exapt of the primary key. But i know that the pk exists. there is an FS mark in the UI description of the columns and i cant delete it.
Zoltán Zörgő 23-Dec-12 3:20am    
You are wrong, there is. I am using the same. Start to "Design" the table. In the "Table Designer" menu you will find a "Relationships" menu item. Look here: http://dotnetslackers.com/Community/blogs/bmdayal/archive/2010/06/05/creating-a-table-relationship-using-sql-server-2008-management-studio.aspx, that stands for Express too.
ChrisTopherus 23-Dec-12 8:36am    
okay there is. thank you for your reply. but my problem still exists. i have to delete the foreign key via query...
Zoltán Zörgő 23-Dec-12 12:29pm    
I understand, but can you delete it in the UI? If you can't even from UI than it is something not right. Check if you have exclusive access to the file when issuing ddl statement - be sure, that you have only one connection open. Check if you have not opened it as read-only.

1 solution

I found the solution:

if you want to delete a foreignkey via query, make sure that you have named your constraint and delete it by this name. so i changed my create foreign key query to:
SQL
ALTER TABLE [Customer]
ADD CONSTRAINT FK_deliveryAddress FOREIGN KEY (deliveryAddress_fk)
REFERENCES [Address](_id)
GO

And now i can delete it with this query:
SQL
ALTER TABLE[Customer]
DROP CONSTRAINT FK_deliveryAddress

Thanks to Zoltán Zörgő for his help...
 
Share this answer
 
Comments
tesnep 30-Sep-14 23:14pm    
And this is in Sql Server CE?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900