MySQL Removing Some Foreign keys

I have a table whose primary key is used in several other tables and has several foreign keys to other tables. CREATE TABLE location ( locationID INT NOT NULL AUTO_INCREMENT PRIMARY KEY … ) ENGINE = InnoDB; CREATE TABLE assignment ( assignmentID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, locationID INT NOT NULL, FOREIGN KEY locationIDX … Read more

How can I find which tables reference a given table in Oracle SQL Developer?

In Oracle SQL Developer, if I’m viewing the information on a table, I can view the constraints, which let me see the foreign keys (and thus which tables are referenced by this table), and I can view the dependencies to see what packages and such reference the table. But I’m not sure how to find … Read more

ERROR: there is no unique constraint matching given keys for referenced table “bar”

Trying to create this example table structure in Postgres 9.1: CREATE TABLE foo ( name VARCHAR(256) PRIMARY KEY ); CREATE TABLE bar ( pkey SERIAL PRIMARY KEY, foo_fk VARCHAR(256) NOT NULL REFERENCES foo(name), name VARCHAR(256) NOT NULL, UNIQUE (foo_fk,name) ); CREATE TABLE baz( pkey SERIAL PRIMARY KEY, bar_fk VARCHAR(256) NOT NULL REFERENCES bar(name), name VARCHAR(256) … Read more

Foreign key constraints: When to use ON UPDATE and ON DELETE

I’m designing my database schema using MySQL Workbench, which is pretty cool because you can do diagrams and it converts them 😛 Anyways, I’ve decided to use InnoDB because of it’s Foreign Key support. One thing I noticed though is that it allows you to set On Update and on Delete options for foreign keys. … Read more

Mysql error 1452 – Cannot add or update a child row: a foreign key constraint fails

I’m having a bit of a strange problem. I’m trying to add a foreign key to one table that references another, but it is failing for some reason. With my limited knowledge of MySQL, the only thing that could possibly be suspect is that there is a foreign key on a different table referencing the … Read more

Migration: Cannot add foreign key constraint

I’m trying to create foreign keys in Laravel however when I migrate my table using artisan i am thrown the following error: [Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL : alter table `priorities` add constraint priorities_user_id_foreign foreign key (`user_id`) references `users` (`id`)) My migration code is as so: priorities migration file … Read more

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint

Why does add a foreign key to the tblDomare table result in this error? The ALTER TABLE statement conflicted with the FOREIGN KEY constraint “FK__tblDomare__PersN__5F7E2DAC”. The conflict occurred in database “almu0004”, table “dbo.tblBana”, column ‘BanNR’. Code CREATE TABLE tblDomare (PersNR VARCHAR (15) NOT NULL, fNamn VARCHAR (15) NOT NULL, eNamn VARCHAR (20) NOT NULL, Erfarenhet … Read more

What’s wrong with foreign keys?

I remember hearing Joel Spolsky mention in podcast 014 that he’d barely ever used a foreign key (if I remember correctly). However, to me they seem pretty vital to avoid duplication and subsequent data integrity problems throughout your database. Do people have some solid reasons as to why (to avoid a discussion in lines with … Read more

INSERT statement conflicted with the FOREIGN KEY constraint – SQL Server

I am getting the following error. Could you please help me? Msg 547, Level 16, State 0, Line 1 The INSERT statement conflicted with the FOREIGN KEY constraint “FK_Sup_Item_Sup_Item_Cat”. The conflict occurred in database “dev_bo”, table “dbo.Sup_Item_Cat”. The statement has been terminated. Code: insert into sup_item (supplier_id, sup_item_id, name, sup_item_cat_id, status_code, last_modified_user_id, last_modified_timestamp, client_id) values … Read more