使用两个外键作为主键 - MySQL

时间:2014-05-06 07:15:27

标签: php mysql sql database

我是MySQL的新手(必须为uni学习它)。 我必须为作业创建数据库和Web界面。

在其中一个表上,我有两列,两列都是外键,我需要将它们用作主键。

这是到目前为止的代码:

drop database if exists testJoke;
create database testJoke;
use testJoke;

CREATE TABLE Author
(
  id           int(11)   NOT NULL ,
  name         varchar(255) NULL ,
  cust_email   varchar(255) NULL,
  password char(32) null,

  PRIMARY KEY (id)


);


**CREATE TABLE AuthorRole
(
  authorid  int(11) NOT NULL ,
  roleid varchar(255) NOT NULL,
  PRIMARY KEY (authorid, roleid),
  FOREIGN KEY(authorid) REFERENCES Author(id),
  FOREIGN KEY(roleid) REFERENCES Role(id)

);**



CREATE TABLE Category
(
  id  int(11)      NOT NULL ,
  name varchar(255) NULL,
  PRIMARY KEY (id)
);


CREATE TABLE Joke
(
  id    int(11)      NOT NULL ,
  joketext   text    NULL ,
  jokedate    date   NOT NULL ,
  authorid int(11)   NULL,
  PRIMARY KEY (id),
  FOREIGN KEY(authorid) REFERENCES Author(id)

);


CREATE TABLE JokeCategory
(
  jokeid    int(11)      NOT NULL ,
  categoryid    int(11)  NOT NULL ,
  PRIMARY KEY (jokeid, categoryid),
  FOREIGN KEY(jokeid) REFERENCES Joke(id),
  FOREIGN KEY(categoryid) REFERENCES Category(id)**


);

CREATE TABLE Role
(
  id    varchar(255)      NOT NULL ,
  description  varchar(255)  NULL ,
  PRIMARY KEY (id)
);

所有表格语法都与提供的数据字典一致。

当我在mysql命令行中运行它时,我在上面以粗体突出显示的部分出现错误(表" AuthorRole"),说它"无法添加外键约束&#34 ;

我试过调试它,它似乎是:

FOREIGN KEY(roleid) REFERENCES Role(id)

导致问题的外键(如果我将其删除,一切正常,如果我将其删除并删除其他外键,则会出错)。

如果有人可以解释我哪里出错了,我将非常感激。

我尝试使用Google搜索,但无法找到任何内容(可能是因为我使用了错误的关键字)。

谢谢

干杯 科里

3 个答案:

答案 0 :(得分:3)

这只是因为您指的是在引用它时未创建的表的主键,请尝试以下sql脚本:

drop database if exists testJoke;
create database testJoke;
use testJoke;

CREATE TABLE Author
(
   id           int(11)   NOT NULL ,
   name         varchar(255) NULL ,
   cust_email   varchar(255) NULL,
   password char(32) null,

   PRIMARY KEY (id)


);

CREATE TABLE Role
(
   id    varchar(255)      NOT NULL ,
   description  varchar(255)  NULL ,
   PRIMARY KEY (id)
);


CREATE TABLE AuthorRole
(
   authorid  int(11) NOT NULL ,
   roleid varchar(255) NOT NULL,
   PRIMARY KEY (authorid, roleid),
   FOREIGN KEY(authorid) REFERENCES Author(id),
   FOREIGN KEY(roleid) REFERENCES Role(id)

);



CREATE TABLE Category
(
  id  int(11)      NOT NULL ,
  name varchar(255) NULL,
  PRIMARY KEY (id)
);


CREATE TABLE Joke
(
  id    int(11)      NOT NULL ,
  joketext   text    NULL ,
  jokedate    date   NOT NULL ,
  authorid int(11)   NULL,
  PRIMARY KEY (id),
  FOREIGN KEY(authorid) REFERENCES Author(id)

);


CREATE TABLE JokeCategory
(
  jokeid    int(11)      NOT NULL ,
  categoryid    int(11)  NOT NULL ,
  PRIMARY KEY (jokeid, categoryid),
  FOREIGN KEY(jokeid) REFERENCES Joke(id),
  FOREIGN KEY(categoryid) REFERENCES Category(id)


);

首先创建表格角色,然后创建作者角色。

答案 1 :(得分:2)

首先创建表“Role”,然后创建表“AuthorRole”,它就可以了

CREATE TABLE Role
(
  id    varchar(255)      NOT NULL ,
  description  varchar(255)  NULL ,
  PRIMARY KEY (id)
);

CREATE TABLE AuthorRole
(
  authorid  int(11) NOT NULL ,
  roleid varchar(255) NOT NULL,
  PRIMARY KEY (authorid, roleid),
  FOREIGN KEY(authorid) REFERENCES Author(id),
  FOREIGN KEY(roleid) REFERENCES Role(id)
);

创建主键时,最好使用id INT(11) NOT NULL AUTO_INCREMENT

答案 2 :(得分:0)

尝试更改

  roleid varchar(255) NOT NULL,

  roleid int(11) NOT NULL,

因为字段和关键字段属于不同类型