-1

I use mysql in my database I want to create multiple tables with delete and update relationship

as an example I want to link the profiles table to the users table Associate the mm_id_user column from the users table with the mm_id_profile column from the profiles table

What I actually tried to do is this example and it didn't work for me
https://stackoverflow.com/a/260453/10206991
https://stackoverflow.com/a/9796950/10206991

The code used to create the users table and it succeeds in creating the table

CREATE TABLE users (
    mm_email VARCHAR(255) NOT NULL,
    mm_id_user int NOT NULL AUTO_INCREMENT,
    mm_name VARCHAR(25) NOT NULL,
    mm_password VARCHAR(255) NOT NULL,
    mm_code_reset_pass VARCHAR(100) NOT NULL,
    mm_code_check_email VARCHAR(100) NOT NULL,
    mm_status CHAR(1) NOT NULL DEFAULT '0',
    mm_date_create_account TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    mm_date_last_login TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    mm_is_login CHAR(1) NOT NULL DEFAULT '0',

    PRIMARY KEY (mm_id_user, mm_email)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 DEFAULT COLLATE utf8_unicode_ci;

The code used to create the profiles table. He failed to create the table

CREATE TABLE profiles (
    mm_id_profile VARCHAR(255) NOT NULL,
    mm_image_user TEXT NOT NULL,
    mm_num_stores_all VARCHAR(4) NOT NULL,
    mm_num_stores_exist VARCHAR(4) NOT NULL,
    mm_name_public VARCHAR(25) NOT NULL,
    mm_email_public VARCHAR(255) NOT NULL,
    mm_phone_public VARCHAR(20) NOT NULL,
    mm_location_public TEXT NOT NULL,
    mm_update_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

    INDEX index_profiles(mm_name_public, mm_email_public),
    PRIMARY KEY (mm_id_profile),
    FOREIGN KEY (mm_id_profile) REFERENCES users(mm_email) ON DELETE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=utf8 DEFAULT COLLATE utf8_unicode_ci;

But if you remove the following line from the profiles table, it will be created But there is definitely no relationship between the two tables

FOREIGN KEY (mm_id_profile) REFERENCES users(mm_email) ON DELETE CASCADE

This is the error message that appears when creating a profiles table

#1005 - Can't create table `mustforu_test`.`profiles` (errno: 150 "Foreign key constraint is incorrectly formed") (Details…)
Mustafa Max
  • 59
  • 2
  • 9

1 Answers1

1

There's no reason to put both mm_id_user and mm_email in the primary key of users. If you want emails to be unique, it should have its own unique index. Your current primary key would allow duplicate emails with different IDs.

CREATE TABLE users (
    mm_email VARCHAR(255) NOT NULL,
    mm_id_user int NOT NULL AUTO_INCREMENT,
    mm_name VARCHAR(25) NOT NULL,
    mm_password VARCHAR(255) NOT NULL,
    mm_code_reset_pass VARCHAR(100) NOT NULL,
    mm_code_check_email VARCHAR(100) NOT NULL,
    mm_status CHAR(1) NOT NULL DEFAULT '0',
    mm_date_create_account TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    mm_date_last_login TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    mm_is_login CHAR(1) NOT NULL DEFAULT '0',

    PRIMARY KEY (mm_id_user),
    UNIQUE KEY (mm_email)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 DEFAULT COLLATE utf8_unicode_ci;

Giving mm_email its own index will allow the foreign key in profiles to work.

Barmar
  • 741,623
  • 53
  • 500
  • 612