为什么?我用过
CONSTRAINT FK_r_p FOREIGN KEY (id_p) REFERENCES Perguntas (id_p)
以前,我不知道这是否有什么不同,但。。。这个怎么了?
CREATE TABLE Pergunta
(
id_p INT(4) NOT NULL PRIMARY KEY AUTO_INCREMENT,
nome_aluno VARCHAR2(60) NOT NULL,
titulo VARCHAR2(50) NOT NULL,
nome_disciplina VARCHAR2(50),
descricao VARCHAR2(4000) NOT NULL
);
CREATE TABLE Resposta
(
id_r INT(4) NOT NULL PRIMARY KEY AUTO_INCREMENT,
titulo VARCHAR2(50) NOT NULL,
descricao VARCHAR2(4000) NOT NULL,
FOREIGN KEY (id_p) REFERENCES Pergunta (id_p)
);
多个问题:
- Oracle中的Identity列是使用
generated always as identity
创建的 - Int数据类型不需要长度
- 对另一个表的引用是基于列的,您需要将一列链接到另一个表格中的一列,因此当您在Pergunta中创建fk到idp时,您需要在表中为其定义一列
我的意思是:
CREATE TABLE Pergunta(
id_p INT GENERATED ALWAYS AS IDENTITY NOT NULL,
nome_aluno VARCHAR2(60) NOT NULL,
titulo VARCHAR2(50) NOT NULL,
nome_disciplina VARCHAR2(50),
descricao VARCHAR2(4000) NOT NULL,
primary key(id_p)
);
CREATE TABLE Resposta(
id_r INT GENERATED ALWAYS AS IDENTITY NOT NULL,
titulo VARCHAR2(50) NOT NULL,
descricao VARCHAR2(4000) NOT NULL,
id_p int ,
FOREIGN KEY (id_p) REFERENCES Pergunta (id_p),
PRIMARY KEY(id_r)
);