0

please could you please tell me what I am doing wrong? I have MySQl script but finally I got this

Error 1215: Cannot add foreign key constraints

Here is code:

   CREATE TABLE IF NOT EXISTS profile_public_data
(
    idProfile INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    login VARCHAR(64),
    name VARCHAR(45),
    lastName VARCHAR(45),
    location VARCHAR(45),
    is_active BOOLEAN DEFAULT false,
    age TINYINT,
    photo VARCHAR(45),
    PRIMARY KEY(idProfile)
);

CREATE TABLE IF NOT EXISTS profile_private_data
(
    idProfile INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    CONSTRAINT fk_idProfile 
            FOREIGN KEY(idProfile) 
            REFERENCES `profile_public_data`(idProfile)
                ON DELETE SET NULL 
                ON UPDATE SET NULL,
    email VARCHAR(45),
    password VARCHAR(45),
    PRIMARY KEY(idProfile)
);
1
  • I think you are trying to set NULL on DELETE and UPDATE but the column is NOT NULL Commented Jul 29, 2018 at 9:09

1 Answer 1

1

You idProfile column in the dependent table is set as auto increment, this is not correct. It should be just INT(11).

Its value must be specified and exists in the source table when when inserting a new record, instead of auto incremented.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.