如何使用与主键具有不同列名的外键?

时间:2018-02-02 04:13:23

标签: mysql foreign-keys

我收到了解析错误。该错误由Image表生成。我正在尝试使用具有与主键不同的列名的外键。

DROP DATABASE IF EXISTS fastpic;


CREATE DATABASE IF NOT EXISTS fastpic;


USE fastpic;


CREATE TABLE user
  (
  email_address varchar(255),
  name varchar (255),
  user_id INT NOT NULL,

   PRIMARY KEY (user_id),
   created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
   modified_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
   );

CREATE TABLE image
   (
   filename varchar (255),
   image_id INT NOT NULL,
    PRIMARY KEY (image_id),
   uploaded_by_user_id INT NOT NULL,
   FOREIGN KEY (uploaded_by_user_id),
   REFERENCES user(user_id),
   ON DELETE CASCADE),
   created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
   modified_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
   );

CREATE TABLE commentcomment
   (
    image_id INT NOT NULL,
    FOREIGN KEY (image_id),
    REFERENCES image(image_id),
    ON DELETE CASCADE),
    user_id INT NOT NULL,
    FOREIGN KEY (user_id),
    REFERENCES user(user_id),
    comment_id INT NOT NULL,
    PRIMARY KEY (comment_id),
    TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    imageurl varchar (1000) NOT NULL,
    comment varchar(2500),
    created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    modified_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
    );

CREATE TABLE follow
     (
      user_id INT NOT NULL,
      FOREIGN KEY (user_id),
      REFERENCES user(user_id),
      ON DELETE CASCADE),
      following_id INT NOT NULL,
      PRIMARY KEY (following_id),
      timestamp_id INT NOT NULL,
      created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
      modified_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
      )

1 个答案:

答案 0 :(得分:0)

您的脚本中存在一些synthax错误。其中一些包括:

  • 每次引用之前的逗号并级联。
  • 删除后级联后的右括号。
  • TIMESTAMP表创建代码中的半列,就在最后一个右括号之前。
  • commentcomment表中的主键定义后,DROP DATABASE IF EXISTS fastpic; CREATE DATABASE IF NOT EXISTS fastpic; USE fastpic; CREATE TABLE user ( email_address varchar(255), name varchar (255), user_id INT NOT NULL, PRIMARY KEY (user_id), created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, modified_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE image ( filename varchar (255), image_id INT NOT NULL, PRIMARY KEY (image_id), uploaded_by_user_id INT NOT NULL, FOREIGN KEY (uploaded_by_user_id) REFERENCES user(user_id), created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, modified_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE commentcomment ( image_id INT NOT NULL, FOREIGN KEY (image_id) REFERENCES image(image_id) ON DELETE CASCADE, user_id INT NOT NULL, FOREIGN KEY (user_id) REFERENCES user(user_id), comment_id INT NOT NULL, PRIMARY KEY (comment_id), -- [No_Column name]TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, imageurl varchar (1000) NOT NULL, comment varchar(2500), created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, modified_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE follow ( user_id INT NOT NULL, FOREIGN KEY (user_id) REFERENCES user(user_id) ON DELETE CASCADE, following_id INT NOT NULL, PRIMARY KEY (following_id), timestamp_id INT NOT NULL, created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, modified_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); 没有列名。

有关使用MySQL作为DDL(数据定义语言)的更多信息,您可以看到MySQL tutorials。 这是一个SQL Fiddle Demo

{{1}}