Cannot INSERT because of Foreign key constraints - mysql

commandantp

So I don't understand why I cannot insert datas in my table that have foreign constraint keys or even modify anything in it....

Here is an example of the tables that are created. I am trying to insert datas in the addresses table:

///////////////////////ADDRESSES TABLE ////////////////////////
CREATE TABLE IF NOT EXISTS addresses (
                id INT NOT NULL AUTO_INCREMENT,
                addressline1 VARCHAR(255) NOT NULL,
                addressline2 VARCHAR(255) NOT NULL,
                postcode VARCHAR(255) NOT NULL,
                phonenumber INT(13) NOT NULL,

                country_id INT NOT NULL,

                PRIMARY KEY (id),

                FOREIGN KEY (country_id) REFERENCES countries(id)

                  ON UPDATE CASCADE
                  ON DELETE RESTRICT

                ) ENGINE=InnoDB ";

///////////////////////COUNTRIES TABLE ////////////////////////
CREATE TABLE IF NOT EXISTS countries (
                id INT NOT NULL AUTO_INCREMENT,
                countryname VARCHAR(255) NOT NULL,

                PRIMARY KEY (id)

                )

Any idea what I am doing wrong??

Thanks i advance!!!

The Velcromancer

The issue here is that you are trying to insert into a referencing table (addresses) when the referenced entry (the country you reference) does not exist. That's what's triggering the FOREIGN KEY CONSTRAINT exception.

Try first inserting some countries into the countries table, then inserting some addresses where you reference those countries you entered in the first step.

As for your second question, that's a choice for you to make. I would probably choose to have the User have an Address (address field in the User table), but some of that depends on how the data is being used/updated.

Have a quick look through this resource if you're new to relational database design. It covers (in brief) topics like relationship types, key constraints, and normal forms.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't insert to database because of foreign key constraints

From Dev

Foreign Key constraints in MySQL

From Dev

Cannot add foreign key constraints

From Dev

Generate insert statements with foreign key constraints

From Dev

Cannot delete a row because of foreign key constraint

From Dev

Backup MySQL schema with foreign key table constraints

From Dev

Problem with foreign key constraints and construction of Mysql DB

From Dev

What is wrong with my mysql foreign key constraints?

From Dev

MySQL DB is ignoring foreign key constraints

From Dev

Insert with execute many, skipping rows that fail foreign key constraints

From Dev

Insert with execute many, skipping rows that fail foreign key constraints

From Dev

cannot insert a record with a foreign key (android sqlite)

From Dev

Laravel 5.2 Entrust migrate error, cannot add foreign key constraints

From Dev

SQLite Foreign Key Constraints cannot be enabled or disabled while there are transactions in progress

From Dev

Foreign key with additional constraints?

From Dev

"polymorphism" for FOREIGN KEY constraints

From Dev

Foreign Key Constraints Hell

From Dev

Magento Foreign Key Constraints

From Java

Cannot truncate table because it is being referenced by a FOREIGN KEY constraint?

From Dev

MySQL on foreign key duplication insert ignore

From Dev

Insert values into a table with a foreign key using mysql

From Dev

insert value to update foreign key in mysql

From Dev

Doctrine not generating cross-database foreign key constraints in MySQL

From Dev

MySQL - Can't create table (errno: 150) - Foreign key constraints

From Dev

Cannot insert duplicate key when directly assigning Foreign Key with EF

From Dev

cannot update foreign key in different session mysql

From Dev

Cannot add foreign key constraint in MySQL

From Java

MySQL Cannot Add Foreign Key Constraint

From Dev

MySql - Sequalize - Cannot add foreign key constraint

Related Related

  1. 1

    Can't insert to database because of foreign key constraints

  2. 2

    Foreign Key constraints in MySQL

  3. 3

    Cannot add foreign key constraints

  4. 4

    Generate insert statements with foreign key constraints

  5. 5

    Cannot delete a row because of foreign key constraint

  6. 6

    Backup MySQL schema with foreign key table constraints

  7. 7

    Problem with foreign key constraints and construction of Mysql DB

  8. 8

    What is wrong with my mysql foreign key constraints?

  9. 9

    MySQL DB is ignoring foreign key constraints

  10. 10

    Insert with execute many, skipping rows that fail foreign key constraints

  11. 11

    Insert with execute many, skipping rows that fail foreign key constraints

  12. 12

    cannot insert a record with a foreign key (android sqlite)

  13. 13

    Laravel 5.2 Entrust migrate error, cannot add foreign key constraints

  14. 14

    SQLite Foreign Key Constraints cannot be enabled or disabled while there are transactions in progress

  15. 15

    Foreign key with additional constraints?

  16. 16

    "polymorphism" for FOREIGN KEY constraints

  17. 17

    Foreign Key Constraints Hell

  18. 18

    Magento Foreign Key Constraints

  19. 19

    Cannot truncate table because it is being referenced by a FOREIGN KEY constraint?

  20. 20

    MySQL on foreign key duplication insert ignore

  21. 21

    Insert values into a table with a foreign key using mysql

  22. 22

    insert value to update foreign key in mysql

  23. 23

    Doctrine not generating cross-database foreign key constraints in MySQL

  24. 24

    MySQL - Can't create table (errno: 150) - Foreign key constraints

  25. 25

    Cannot insert duplicate key when directly assigning Foreign Key with EF

  26. 26

    cannot update foreign key in different session mysql

  27. 27

    Cannot add foreign key constraint in MySQL

  28. 28

    MySQL Cannot Add Foreign Key Constraint

  29. 29

    MySql - Sequalize - Cannot add foreign key constraint

HotTag

Archive