MySQL:无法使用外键创建表

时间:2019-10-08 03:12:22

标签: mysql sql

到目前为止,它仅创建前3个表:employeeskillclient

CREATE SCHEMA IF NOT EXISTS my_db;

USE my_db;

CREATE TABLE IF NOT EXISTS employee(
    id INT UNSIGNED AUTO_INCREMENT,
    first_name VARCHAR(255) NOT NULL,
    last_name VARCHAR(255) NOT NULL,
    position VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL,
    PRIMARY KEY(id)
)ENGINE=INNODB;

CREATE TABLE IF NOT EXISTS skill(
    code VARCHAR(255),
    description VARCHAR(255),
    PRIMARY KEY(code)
)ENGINE=INNODB;

CREATE TABLE IF NOT EXISTS client(
    code VARCHAR(255),
    description VARCHAR(255),
    PRIMARY KEY(code)
)ENGINE=INNODB;

CREATE TABLE IF NOT EXISTS project(
    code VARCHAR(255),
    role VARCHAR(255) NOT NULL,
    start_date DATE NOT NULL,
    end_date DATE NOT NULL,
    description VARCHAR(255),
    client.code VARCHAR(255),
    PRIMARY KEY(code),
    FOREIGN KEY(client.code) REFERENCES client(code),
    ON DELETE CASCADE
    ON UPDATE CASCADE
)ENGINE=INNODB;

CREATE TABLE IF NOT EXISTS responsibility(
    id INT UNSIGNED AUTO_INCREMENT,
    description VARCHAR(255) NOT NULL,
    project.code VARCHAR(255),
    PRIMARY KEY(id),
    FOREIGN KEY(project.code) REFERENCES project(code),
    ON DELETE CASCADE
    ON UPDATE CASCADE
)ENGINE=INNODB;

/* Junction Table */
CREATE TABLE IF NOT EXISTS employee_skill(
    employee.id VARCHAR(255),
    skill.code VARCHAR(255),
    PRIMARY KEY(employee.id, skill.code),
    FOREIGN KEY(employee.id) REFERENCES employee(id),
    FOREIGN KEY(skill.code) REFERENCES skill(code)
    ON DELETE CASCADE
    ON UPDATE CASCADE
)ENGINE=INNODB;

/* Junction Table */
CREATE TABLE IF NOT EXISTS employee_client(
    employee.id VARCHAR(255),
    client.code VARCHAR(255),
    PRIMARY KEY(employee.id, client.code),
    FOREIGN KEY(employee.id) REFERENCES employee(id),
    FOREIGN KEY(client.code) REFERENCES client(code)
    ON DELETE CASCADE
    ON UPDATE CASCADE
)ENGINE=INNODB;

,当它用外键project击中第一个表时停止运行

然后引发此错误:

#1103 - Incorrect table name 'client'

2 个答案:

答案 0 :(得分:2)

项目表中的列名const导致中断,因为已经有一个名为a的表。

您必须在反引号中使用列名:

client.code

我建议使用下划线代替client,以完全避免句点。

答案 1 :(得分:2)

exports.updateUser = functions.firestore
.document('trashcan/{trashcanId}')
.onUpdate((change, context) => {
  'front';
  // Get an object representing the document
  // e.g. {'name': 'Marie', 'age': 66}
  const newValue = change.after.data();

  // ...or the previous value before this update
  const previousValue = change.before.data();

  // access a particular field as you would any JS property
  const front = newValue.front;

  // perform desired operations ...

删除逗号(,)并将CREATE TABLE IF NOT EXISTS project( code VARCHAR(255), role VARCHAR(255) NOT NULL, start_date DATE NOT NULL, end_date DATE NOT NULL, description VARCHAR(255), client_code VARCHAR(255), PRIMARY KEY(code), FOREIGN KEY(client_code) REFERENCES client(code) ON DELETE CASCADE ON UPDATE CASCADE )ENGINE=INNODB; 更改为client. 享受(',')