gpt4 book ai didi

mysql - 在 PhpMyAdmin 中添加 CONSTRAINT FOREIGN KEY 时出现问题

转载 作者:行者123 更新时间:2023-11-29 23:45:03 25 4
gpt4 key购买 nike

我正在尝试创建 5 个表(应该不会太难),但我在分配外键时遇到问题。 PhpMyAdmin 给我这个错误:

Can't create table 'databasexx.gave' (errno: 150)

基本上所有没有外键的表都会被创建。

DROP TABLE IF EXISTS oppbygging;
CREATE TABLE oppbygging (
gnr INT,
dnr INT,
ant INT,
CONSTRAINT dnr_grn_pk PRIMARY KEY (gnr, dnr)
) ENGINE=InnoDB;

DROP TABLE IF EXISTS onske;
CREATE TABLE onske (
onr INT,
pnr INT,
gnr INT,
prioriet INT,
ferdig INT,
CONSTRAINT pnr_gnr_pk PRIMARY KEY (pnr, gnr)
) ENGINE=InnoDB;

DROP TABLE IF EXISTS person;
CREATE TABLE person (
pnr INT,
fornavn VARCHAR(64),
etternavn VARCHAR(64),
fdato DATE,
CONSTRAINT pnr_pk PRIMARY KEY (pnr),
CONSTRAINT person_pnr_fk FOREIGN KEY (pnr) REFERENCES onske(pnr)
) ENGINE=InnoDB;

DROP TABLE IF EXISTS gave;
CREATE TABLE gave
(
gnr int,
navn varchar (255) UNIQUE,
prod_tid int NOT NULL,
CONSTRAINT gnr_pk PRIMARY KEY (gnr),
CONSTRAINT gave_gnr_fk FOREIGN KEY (gnr) REFERENCES oppbygging(gnr),
CONSTRAINT gave_gnr_fk FOREIGN KEY (gnr) REFERENCES onske(gnr)

) ENGINE=InnoDB;

DROP TABLE IF EXISTS del;
CREATE TABLE del (
dnr INT,
navn VARCHAR(64),
lager_ant INT NOT NULL,
CONSTRAINT dnr_pk PRIMARY KEY (dnr),
CONSTRAINT del_dnr_fk FOREIGN KEY (dnr) REFERENCES oppbygging(dnr)

) ENGINE=InnoDB;

我确信我犯了某种明显的错误,但我就是无法弄清楚。任何帮助将非常感激。

这是关系 View ,忽略 Eiendom 表:

enter image description here

最佳答案

使用外键创建另一个表,即外键指定的必须是至少一个表的主键例如,已经创建了

使用主键创建表 1:

CREATE TABLE PERSONS(
PERSON_ID INT(10),
FNAME VARCHAR(20)NOT NULL,
LNAME VARCHAR(20),
DOB DATETIME,
AMOUNTPAID DECIMAL(15,2),
PRIMARY KEY(PERSON_ID)
);

然后使用外键创建表 2:

CREATE TABLE COMMENTS(
COMMENTID INT AUTO_INCREMENT,
COMMENTS VARCHAR(500),
PERSON_ID INT(10),
PRIMARY KEY(COMMENTID),
FOREIGN KEY (PERSON_ID) REFERENCES PERSONS(PERSON_ID)
);

关于mysql - 在 PhpMyAdmin 中添加 CONSTRAINT FOREIGN KEY 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25992888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com