简体   繁体   English

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

[英]Using two foreign keys as a primary key - MySQL

I am quite new to MySQL (have had to learn it for uni). 我是MySQL的新手(必须为uni学习它)。 I have to create a database and web interface for an assignment. 我必须为作业创建数据库和Web界面。

On one of the tables I have two columns, both of which are foreign keys, and i need to use them both as the primary key. 在其中一个表上,我有两列,这两列都是外键,我需要将它们用作主键。

This is the code so far: 这是到目前为止的代码:

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)
);

All of the table syntax is in line with a data dictionary provided. 所有表语法都与提供的数据字典一致。

When i run this in the mysql command line, i get an error on the section highlighted in bold above (table "AuthorRole"), saying that it "cannot add foreign key constraint". 当我在mysql命令行中运行它时,我在上面以粗体突出显示的部分(表“AuthorRole”)上出现错误,说它“无法添加外键约束”。

I have had a try at debugging it, and it seems to be the: 我试过调试它,它似乎是:

FOREIGN KEY(roleid) REFERENCES Role(id)

Foreign key that is causing the problem (if i remove it, all works well, and if i leave it in and remove the other foreign key, it gives an error). 导致问题的外键(如果我删除它,一切正常,如果我留下并删除其他外键,则会出错)。

If someone could please explain where i am going wrong, i would be very grateful. 如果有人可以解释我哪里出错了,我将非常感激。

I have tried googling this, but was unable to find anything (probably because i was using the wrong keywords). 我试过谷歌搜索,但无法找到任何东西(可能是因为我使用了错误的关键字)。

Thanks 谢谢

Cheers Corey 干杯科里

At first create the table "Role", then the table "AuthorRole" and it'll be ok 首先创建表“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)
);

And when creating primary keys it's better to use id INT(11) NOT NULL AUTO_INCREMENT 在创建主键时,最好使用id INT(11) NOT NULL AUTO_INCREMENT

It is simply because you are referring to a primary key of the table which is not created at the time you refer to it, try the following sql script: 这只是因为您指的是表引用时未创建的表的主键,请尝试以下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)


);

Create table Role first and then AuthorRole. 首先创建表Role,然后创建AuthorRole。

try changeing 尝试改变

  roleid varchar(255) NOT NULL,

to

  roleid int(11) NOT NULL,

as the field and your key field are of different types 因为字段和关键字段属于不同类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM