skip to Main Content

I keep getting this error when attempting to create a table with SQL.

I have these two tables:

I’m using PHPMyAdmin and it won’t allow me to use M_id as a foreign key which references Employee Table primary key E_id.

Anyone able to see what’s wrong with my code?

Thanks!

3

Answers


  1. Foreign key definitions have to exactly match the primary key columns to which they refer. In this case, you defined Department.M_id to a be a nullable integer column, while EMPLOYEE.E_id is integer not nullable. Try making M_id not nullable:

    CREATE TABLE Department (
        D_name VARCHAR(100) NOT NULL,
        D_id INT NOT NULL,
        M_id INT NOT NULL DEFAULT 0000,
        ...
        FOREIGN KEY (M_id) REFERENCES EMPLOYEE(E_id)
        ON DELETE SET DEFAULT   ON UPDATE CASCADE
    )
    
    Login or Signup to reply.
  2. Your code has multiple errors:

    • varchar() length is too long.
    • You have a forward reference for a foreign key constraint.
    • SET DEFAULT doesn’t really work.

    You want something like this:

    CREATE TABLE employees (
      employee_id int not null primary key,
      Job_type VARCHAR(100),
      Ssn INT NOT NULL,
      Salary DECIMAL NOT NULL,
      Address VARCHAR(500) NOT NULL,
      First_name VARCHAR(50) NOT NULL,
      M_initial CHAR(1),
      Last_name VARCHAR(50) NOT NULL,
      E_end_date DATE,
      E_start_date DATE NOT NULL,
      department_id INT NOT NULL,
      Super_id INT,
      FOREIGN KEY (Super_id) REFERENCES employees(employee_id) ON DELETE SET NULL ON UPDATE CASCADE,
      UNIQUE (Ssn)
    );
    
    CREATE TABLE departments (
      department_id int primary key,
      D_name VARCHAR(100) NOT NULL,
      D_id INT NOT NULL,
      M_id INT DEFAULT 0000,
      Manager_start_date DATE NOT NULL,
      Manager_end_date DATE,
      Report VARCHAR(8000),
      Num_of_employees INT NOT NULL,
      FOREIGN KEY (M_id) REFERENCES employees(employee_id) ON DELETE SET NULL ON UPDATE CASCADE,
      UNIQUE (D_name)
    );
    
    ALTER TABLE employees ADD CONSTRAINT FOREIGN KEY (department_id) REFERENCES departments(department_id)
      ON DELETE CASCADE ON UPDATE CASCADE;
    

    I also changed a few other things:

    • The table names are plural.
    • The primary keys are the singular form followed by “_id”.
    • Foreign keys and primary keys have the same name.
    • The primary key is the first column in the table.

    Here is a db<>fiddle showing that this works.

    Login or Signup to reply.
  3. I will not question your design, though it looks problematic.

    However – You cannot reference a table which doesn’t exist yet (REFERENCES Department(D_id)). You should either remove the FOREIGN KEY constraints from the CREATE statements and add them afterwards in ALTER TABLE statements.

    Example:

    CREATE TABLE EMPLOYEE (...);
    CREATE TABLE Department (...);
    
    ALTER TABLE EMPLOYEE 
      ADD FOREIGN KEY (D_id)
      REFERENCES Department(D_id)
      ON DELETE CASCADE
      ON UPDATE CASCADE
    ;
    

    Demo

    Or temporarily disable foreign key checks:

    SET FOREIGN_KEY_CHECKS = 0;
    
    CREATE TABLE EMPLOYEE (...);
    
    CREATE TABLE Department (...);
    
    SET FOREIGN_KEY_CHECKS = 1;
    

    Demo

    You can also not use ON DELETE SET DEFAULT. InnoDB doesn’t support it. You need to change it to ON DELETE SET NULL. If you want that behavior, you will need to implement it either in your application code or in a trigger.

    I would also use TEXT as data type instead of VARCHAR(30000).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search