0

I am getting an issue and I feel like I fixed everything. MySQL workbench is still giving me this error. If you can please explain what is wrong I would be greatly appreciate it. I did check for key constraints in the Account table. It is giving the error in the account table.

CREATE TABLE Account(
    AcctNum int AUTO_INCREMENT,
    MemberID int,
    Balance double,
    PIN int,
    creationDate date,
    InitialBalance double,
    CreatedByEmployee int,
    type VARCHAR(20),
    PRIMARY KEY(AcctNum),
    FOREIGN KEY(MemberID) REFERENCES Member(MemNum),
    FOREIGN KEY(CreatedByEmployee) REFERENCES Employee(EmpId)
);

CREATE TABLE Member( 
    MemNum int AUTO_INCREMENT,  
    DOB date, 
    CreditScore int, 
    AcctOpened date, 
    SSN VARCHAR(11),  
    Address VARCHAR(255),
    PRIMARY KEY(MemNum)
);

CREATE TABLE Employee( 
    EmpId int AUTO_INCREMENT,  
    DOB date, 
    SSN VARCHAR(11), 
    HireDate date, 
    Salary double, 
    EmpLevel VARCHAR(50),
    PRIMARY KEY(EmpId)
);
1
  • 1
    The Member table and Employee table has to exist before reference. i.e., Create both of these tables before creating Account table. Commented May 4, 2014 at 18:21

4 Answers 4

1

You need to create the referencing table first before creating the table which is referring other tables with FOREIGN key.

The order of table creation should be as

CREATE TABLE Member( 
    MemNum int AUTO_INCREMENT,  
    DOB date, 
    CreditScore int, 
    AcctOpened date, 
    SSN VARCHAR(11),  
    Address VARCHAR(255),
    PRIMARY KEY(MemNum)
);

CREATE TABLE Employee( 
    EmpId int AUTO_INCREMENT,  
    DOB date, 
    SSN VARCHAR(11), 
    HireDate date, 
    Salary double, 
    EmpLevel VARCHAR(50),
    PRIMARY KEY(EmpId)
);

CREATE TABLE Account(
    AcctNum int AUTO_INCREMENT,
    MemberID int,
    Balance double,
    PIN int,
    creationDate date,
    InitialBalance double,
    CreatedByEmployee int,
    type VARCHAR(20),
    PRIMARY KEY(AcctNum),
    FOREIGN KEY(MemberID) REFERENCES Member(MemNum),
    FOREIGN KEY(CreatedByEmployee) REFERENCES Employee(EmpId)
);
Sign up to request clarification or add additional context in comments.

Comments

1

You are creating the Foreign key for Member before creating the table so it does not exist yet. Change the order you are creating the tables.

CREATE TABLE Member( 
    MemNum int AUTO_INCREMENT,  
    DOB date, 
    CreditScore int, 
    AcctOpened date, 
    SSN VARCHAR(11),  
    Address VARCHAR(255),
    PRIMARY KEY(MemNum)
);

CREATE TABLE Employee( 
    EmpId int AUTO_INCREMENT,  
    DOB date, 
    SSN VARCHAR(11), 
    HireDate date, 
    Salary double, 
    EmpLevel VARCHAR(50),
    PRIMARY KEY(EmpId)
);
CREATE TABLE Account(
    AcctNum int AUTO_INCREMENT,
    MemberID int,
    Balance double,
    PIN int,
    creationDate date,
    InitialBalance double,
    CreatedByEmployee int,
    type VARCHAR(20),
    PRIMARY KEY(AcctNum),
    FOREIGN KEY(MemberID) REFERENCES Member(MemNum),
    FOREIGN KEY(CreatedByEmployee) REFERENCES Employee(EmpId)
);

Comments

1

Ofcourse the order of table creation is important here. Create the Member and Employee tables first, followed by the Account. It should work fine.

Comments

1

you have wrong order of creating tables

try this

1- Create member table first

2- employer table second

3- account table in last

http://www.sqlfiddle.com/#!2/47d58

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.