php - Cannot set FK constrains in MySQL

983

I have a problem that i cant solve right now and i have read similar question but i didnt help me!

I have i Users table and a Books table and i want to set a ONE to ONE relationship between them.

Im trying to set users_id - PK in Users to FK in Books table but it dosent work!

What am i doing wrong?

Please help!

I'll add images for the steps i go through in the same order:

Users table enter image description here

Books table, with users_id as index (Non unique) enter image description here

Setting upp the constrain for FK enter image description here

And at last the ERROR s

716

Answer

Solution:

It means that you have abooks row that does not have a matchinguser_id in theusers table.

You need to add an user or update the books table first.

If you add a foreign key, you cannot havenull or0 values (forAUTO_INCREMENT keys) in your books.user_id anymore.

People are also looking for solutions to the problem: php - Multidimensional Array to html MultiDimensional checkbox

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.