Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9214921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:04:53+00:00 2026-06-18T02:04:53+00:00

two tables, created using these scripts: create table user_auth ( username varchar(255) NOT NULL,

  • 0

two tables, created using these scripts:

create table user_auth
(
    username varchar(255) NOT NULL,
    password varchar(255) NOT NULL,
    user_id varchar(36) NOT NULL,
    PRIMARY KEY(username, password)
)engine=innodb;

create table user
(
    user_id varchar(36) NOT NULL,
    PRIMARY KEY(user_id),
    FOREIGN KEY(user_id) REFERENCES user_auth(user_id) ON DELETE CASCADE
)engine=innodb;

I get the error:
Can’t create table ‘xxx.user’ (errno: 150)

A foreign key error.

IF instead I do this (remove foreign key constraint but still reference user_auth):

create table user_auth
(
    username varchar(255) NOT NULL,
    password varchar(255) NOT NULL,
    user_id varchar(36) NOT NULL,
    PRIMARY KEY(username, password)
)engine=innodb;

create table user
(
    user_id varchar(36) NOT NULL REFERENCES user_auth(user_id) ON DELETE CASCADE,
    PRIMARY KEY(user_id),
)engine=innodb;

Everything is just peachy, BUT I can insert a user_id into the user table without having a corresponding key in user_auth, which puts a hole in my referential integrity.

Now for kicks say I do this (remove composite key and make user_id a primary key in user_auth):

create table user_auth
(
    user_id varchar(36) NOT NULL,
    PRIMARY KEY(username, password)
)engine=innodb;

create table user
(
    user_id varchar(36) NOT NULL,
    PRIMARY KEY(user_id),
    FOREIGN KEY(user_id) REFERENCES user_auth(user_id) ON DELETE CASCADE
)engine=innodb;

This also works, though I do not have the username/password composite to ensure uniqueness.

I have a feeling I’m missing something pretty key in how MySQL works. Please enlighten.

Thanks for your time!

UPDATE

In the article ypercube linked in their answer, point three mentions the order of the PK and corresponding FK must be the same. If I add user_id as a PK in user_auth in the following order, the script works:

create table user_auth
(
    username varchar(255) NOT NULL,
    password varchar(255) NOT NULL,
    user_id varchar(36) NOT NULL,
    PRIMARY KEY(user_id, username, password)
)engine=innodb;

create table user
(
    user_id varchar(36) NOT NULL,
    PRIMARY KEY(user_id),
    FOREIGN KEY(user_id) REFERENCES user_auth(user_id) ON DELETE CASCADE
)engine=innodb;

So I can still SELECT and such, but now I can’t make duplicate username/password combos and thus each account will be unique since a username/password/user_id record must exist before I can insert user data.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:04:55+00:00Added an answer on June 18, 2026 at 2:04 am

    Point 3 in http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html says everything. So if I add user_id as a PK in user_auth (I thought I mentioned I tried this, but it seems I missed the script example), then it still might not work:

    create table user_auth
    (
        username varchar(255) NOT NULL,
        password varchar(255) NOT NULL,
        user_id varchar(36) NOT NULL,
        PRIMARY KEY(username, password, user_id) -- <== DOES NOT WORK
    )engine=innodb;
    
    create table user
    (
        user_id varchar(36) NOT NULL,
        PRIMARY KEY(user_id),
        FOREIGN KEY(user_id) REFERENCES user_auth(user_id) ON DELETE CASCADE
    )engine=innodb;
    

    But, if I change the order around…

    create table user_auth
    (
        username varchar(255) NOT NULL,
        password varchar(255) NOT NULL,
        user_id varchar(36) NOT NULL,
        PRIMARY KEY(user_id, username, password) -- <== WORKS!
    )engine=innodb;
    
    create table user
    (
        user_id varchar(36) NOT NULL,
        PRIMARY KEY(user_id),
        FOREIGN KEY(user_id) REFERENCES user_auth(user_id) ON DELETE CASCADE
    )engine=innodb;
    

    Because in order to user the PK of user_auth as an FK, they must be in the same order as indices, (comment if I am misunderstanding something).

    But in terms of table design we still have an issue, the composite key means I could have duplicate user_ids in user_auth, so a smarter design would be:

    create table user_auth
    (
        username varchar(255) NOT NULL,
        password varchar(255) NOT NULL,
        user_id varchar(36) NOT NULL,
        PRIMARY KEY(user_id),
        UNIQUE KEY(username) -- <== I should allow duplicate passwords, so just usernames should be               unique
    )engine=innodb;
    
    create table user
    (
        user_id varchar(36) NOT NULL,
        PRIMARY KEY(user_id),
        FOREIGN KEY(user_id) REFERENCES user_auth(user_id) ON DELETE CASCADE
    )engine=innodb;
    

    Much better

    The main take-away is that the order of your composite keys matters!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are two tables: CREATE TABLE STORE ( ID INTEGER NOT NULL, MEDICINE_ID INTEGER
I am using luasql. I have two tables of this type: IPINFO CREATE TABLE
First.. here are the two tables I've created (sans irrelevant columns).. CREATE TABLE users_history1
How to create a relation between two tables using PHPMyAdmin?
I have created two tables & inserted values as shown below . Table 1
Create two tables with some ids overlapping. create table outer_table ( id integer, overlap_in
I'm trying to create a map using Google Fusion Tables with two layers, one
Two tables (Teachers and Students) are created while the Id in Students is referenced
I have two tables one called fs_note the other called dumy_fs_note I created after
I have two auditing tables: Trip_aud and Event_aud. They were created in Envers, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.