我對MySQL非常陌生(必須爲uni學習它)。 我必須爲作業創建數據庫和Web界面。使用兩個外鍵作爲主鍵 - MySQL
在其中一個表上我有兩列,它們都是外鍵,我需要將它們用作主鍵。
這是迄今爲止的代碼:
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」)得到一個錯誤,說它「無法添加外鍵約束」。
我已經在調試它一個嘗試,它似乎是:
FOREIGN KEY(roleid) REFERENCES Role(id)
導致該問題(如果我刪除它,一切運作良好,如果我留在國外關鍵並刪除其他外鍵,它會給出錯誤)。
如果有人能解釋我要去哪裏錯,我會非常感激。
我試過使用Google搜索這個,但無法找到任何東西(可能是因爲我使用了錯誤的關鍵字)。
感謝
乾杯 科瑞
嗨,這工作!謝謝你的幫助。我會嘗試你說的主鍵。再次感謝。乾杯,科裏 – Fishingfon