Could not create constraint in sql server 2005 - sql-server-2008

I am giving following error while adding the object into database.
Foreign key 'fk4150' references object 'TESTPOLY2' which is not a user
table. Could not create constraint. See previous errors. in SQL:
ALTER TABLE [WORKLOG2SETUP] ADD CONSTRAINT fk4150 FOREIGN KEY
(related_id) REFERENCES TESTPOLY2
Please let me know if anyone knows solution.
Cheers,
Pravin

Related

How to add foreign key after creating table

Since this is my first day of learning SQL, I am trying to add a foreign key to a column in a table referring to another column in another table. I am getting an error like "Error Code 1215: Cannot add Foreign Key Constraint". I have read the documentation on many websites, I couldn't get any solutions. I am not familiar with technical language or knowledge in SQL, so help me with simple answers to proceed further. Thanks in advance.
Here is the snippet I used to add a foreign key to an existing table.
"ALTER TABLE location_details ADD CONSTRAINT FK_location FOREIGN KEY (Zip) REFERENCES student_details(Zip);"

mysqldbcopy combining foreign key constraints?

So I currently have a table that has the following constraints according to SHOW CREATE TABLE:
CONSTRAINT `FK_BA62400997790DEE` FOREIGN KEY (`some_id`) REFERENCES `Other_Table` (`id`),
CONSTRAINT `FK_BA624009C8554709` FOREIGN KEY (`someOther_id`) REFERENCES `Yet_Another_Table` (`id`)
However, when using mysqldbcopy to copy the db over, I get the following error:
ALTER TABLE old_database.OriginalTable add CONSTRAINT `FK_BA62400997790DEE`
FOREIGN KEY (`some_id`,`someOther_id`)
REFERENCES `old_database`.`Other_Table`
(`id`,`id`)
ON UPDATE RESTRICT
ON DELETE RESTRICT
. Error: Query failed. 1005 (HY000): Can't create table 'old_database.#sql-602_4b5' (errno: 150)
Something is clearly going wrong here, but not sure what it is. Clearly it's somehow trying to combine the keys?
I am running version 1.3.5.
The 1.3.6 release fixed the following bug which may be related
BUG#17474810: constraint error copying the employees with mysqldbcopy
Try upgrading to something higher, currently up to version 1.6 - https://launchpad.net/mysql-utilities
I am using MySQL Utilities mysqldbcopy version 1.6.5, it still shows me something like this:
ERROR: Unable to execute constraint query
ALTER TABLE node5.books add CONSTRAINT `books_ibfk_1`
FOREIGN KEY (`publisher_id`)
REFERENCES `node5`.`publishers`
(`id`)
ON UPDATE RESTRICT
ON DELETE RESTRICT
. Error: Query failed. 1215 (HY000): Cannot add foreign key constraint
And then I found setting storage engine to InnoDB can solve this issue.
https://github.com/maghead/maghead/commit/b406abf277b525f665966f8e8ec421a229bb13c7

MySQL error 1452

Here's the script
ALTER TABLE `candycorn`.`bb_users`
ADD CONSTRAINT `pf_minecraftusername`
FOREIGN KEY (`pf_minecraftusername`)
REFERENCES `candycorn`.`bb_profile_fields_data` (`pf_minecraftusername`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
and the error description
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails
(`candycorn`.<result 2 when explaining filename '#sql-4e4_1785d'>, CONSTRAINT
`pf_minecraftusername` FOREIGN KEY (`pf_minecraftusername`) REFERENCES
`bb_profile_fields_data` (`pf_minecraftusern)
if somebody could, would you explain what I'm missing out here?
The constraint that you are trying to add isn't satisfied by some data already in the tables. This can be because a value in bb_users table isn't found in the corresponding column (pf_minecraftusername) in bb_profile_fields_data.
If you have to add the constraint to tables with data already in them, you have to clean up the tables by hand first. Alternatively, you can empty the tables (with truncate, or by making the tables afresh after doing a "drop database"), then add the constraint, and then run whatever scripts you have to put data in the tables.
Of course, if this is a production system, you'll need to do more complex data fixing before you can successfully add the constraint.
Finally, I suggest that you make a dummy copy of the database with no data at all and add the constraint there, just to check that the constraint is properly specified. You don't want to be barking up the wrong tree.
ALTER TABLE tablename with NOCHECK
ADD CONSTRAINT [FK_1] FOREIGN KEY ([Column name])
REFERENCES restaurants([column name])
It seems data is already present in table.. So
You need to modify table using above query.. No need to recreate table nd data

SQL Server: drop a FK constraint, which is not foreign key

I am using SQL Server 2008 and its Management Studio. I am doing a web project, which has a tool to automate the tables/relationships creation.
My web project reveals this error:
Unsuccessful: alter table Tester add constraint FK_c6c4bf4s2rvp56a32nnruww2b foreign key (game) references Game
Column 'Game.id' is not the same data type as referencing column 'Tester.game' in foreign key 'FK_c6c4bf4s2rvp56a32nnruww2b'
However, when I ran the following in the management studio:
ALTER TABLE dbo.Tester DROP CONSTRAINT FK_c6c4bf4s2rvp56a32nnruww2b
I get the following:
Msg 3728, Level 16, State 1, Line 1
'FK_c6c4bf4s2rvp56a32nnruww2b' is not a constraint.
Msg 3727, Level 16, State 0, Line 1
Could not drop constraint. See previous errors.
I am confused. What type of constraint is FK_c6c4bf4s2rvp56a32nnruww2b?
How can I remove it?
Thanks and regards.
You can try
sp_help [table_name]
to get all the foreign key constraints.
When you get the foreign key constraints on the table. Drop them by writing something like this:-
ALTER TABLE [dbo].[table_name] DROP CONSTRAINT [Foreign_FK]
There is no such constraint!
The first error clearly says that it could NOT create that constraint since the datatypes of those two columns involved (Game.Id and Tester.Game) do not match.
What you should do is check how you're creating your FK constraint that leads to that first error - and explicitly give that FK constraint a meaningful name!

Foreign Key Constraint is incorrectly formed

There are several other questions about this topic that I have gone through, but I can't seem to figure out how their solutions apply to my tables. Check out the sqlfiddle. You can see it builds the schema just fine.
Basically, one table is a table of contacts/people. The second table is a table of countries. I am attempting to create a foreign key reference between contacts.country_id and countries.id.
Now, add the following to the panel on the left side:
ALTER TABLE `ultra_contacts`
ADD INDEX `fk_test` (`country_id`),
ADD CONSTRAINT `fk_test` FOREIGN KEY (`country_id`) REFERENCES `ultra_countries` (`id`) ON UPDATE CASCADE ON DELETE CASCADE`
The alter table code is not working for some reason. Any help would be appreciated.
The error is: Schema Creation Failed: Can't create table 'db_e342e.#sql-7711_1a4d2' (errno: 150): Using a 3rd party program (HeidiSQL) the error is a bit more detailed:
Foreign key constraint is incorrectly formed
You're trying to use foreign keys on a MyISAM table, which is not allowed (they only work with InnoDB). Take a look here: http://sqlfiddle.com/#!2/64951 All I've changed from your original is the table type (from MyISAM to InnoDB) and then I added the constraint. Worked fine.
Full disclosure - I'm the author of SQL Fiddle :)