简体   繁体   English

创建具有多个主键列的数据库并引用外键时出错

[英]Error creating database with multiple primary key columns and referencing foreign key

I've been having an issue when creating a database. 创建数据库时遇到了问题。 Each table has a primary key with many foreign keys used also. 每个表都有一个主键,并且还使用了许多外键。 The issue I have is that I keep getting the error 我的问题是我不断收到错误消息

SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 -  "no matching unique or primary key for this column-list"
*Cause:    A REFERENCES clause in a CREATE/ALTER TABLE statement
           gives a column-list for which there is no matching unique or primary
           key constraint in the referenced table.
*Action:   Find the correct column names using the ALL_CONS_COLUMNS
           catalog view

I don't know what is causing this and is flagging the PROJECT_RECORDS table as the issue. 我不知道是什么原因造成的,并将PROJECT_RECORDS表标记为问题。 I have used the same method in the PROJECT_TABLES table. 我在PROJECT_TABLES表中使用了相同的方法。

SQL/Oracle SQL / Oracle

CREATE TABLE PROJECT_DB
(DB_ID number (3) NOT NULL primary key,
DB_NAME varchar2 (25) NOT NULL,
DB_DESCRIPTION varchar2 (75) NOT NULL,
DB_DATE date NOT NULL);

CREATE TABLE PROJECT_DATATYPE
(DATATYPE_NAME varchar2 (20) NOT NULL PRIMARY KEY,
DATATYPE_DATATYPE varchar2(50) NOT NULL);

CREATE TABLE PROJECT_TABLES (
PROJECT_ID number(3) not null references PROJECT_DB(DB_ID) on delete cascade,
PROJECT_FIELDNAME varchar2(25) not null,
PROJECT_DATATYPE varchar2(50) not null references PROJECT_DATATYPE(DATATYPE_NAME),
PROJECT_LENGTH number(3),
PROJECT_REQUIRED varchar2(8),
PROJECT_LISTCOLUMNID number (3) not null,
primary key(PROJECT_ID, PROJECT_LISTCOLUMNID));


CREATE TABLE PROJECT_RECORDS (
RECORDS_ROWID number(3) not null,
RECORDS_LISTCOLUMNID number (3) not null references PROJECT_TABLES(PROJECT_LISTCOLUMNID)on delete cascade,
RECORDS_LISTID number (3) not null,
RECORDS_RECORDVALUE varchar2 (25),
primary key(RECORDS_ROWID, RECORDS_LISTCOLUMNID));

commit;

The reasoning for adding multiple primary keys to the PROJECT_TABLES table is that the listcolumnid isn't unique. 向PROJECT_TABLES表添加多个主键的原因是listcolumnid不是唯一的。

The problem is with your syntax. 问题在于您的语法。 You don't do this: 您不这样做:

, fieldname datatype references (something)

You do this: 你做这个:

, primary key(somefield)
, foreign key (somefield) references sometable(somefield)

If listcolumnid is not unique, you cannot put a foreign key constraint on it. 如果listcolumnid不是唯一的,则不能在其上施加外键约束。 A foreign key always refrences exatcly one parent row. 外键总是引用一个父行。 So you probably should use both columns in your foreign key (assuming there is really a 1:n relation): 因此,您可能应该在外键中使用这两列(假设确实存在1:n关系):

CREATE TABLE PROJECT_RECORDS (
RECORDS_ROWID number(3) not null,
RECORDS_ID number(3) not null,
RECORDS_LISTCOLUMNID number (3) not null,
RECORDS_LISTID number (3) not null,
RECORDS_RECORDVALUE varchar2 (25),
primary key(RECORDS_ROWID, RECORDS_LISTCOLUMNID),
foreign key fk_project_projectrecords (RECORDS_ID, RECORDS_LISTCOLUMNID) references PROJECT_TABLES(PROJECT_ID, PROJECT_LISTCOLUMNID)on delete cascade,
);

(The example is using your prefix naming convention, I would change RECORDS_ID to PROJECT_ID , same for RECORDS_LISTCOLUMNID ) (该示例使用您的前缀命名约定,我将RECORDS_ID更改为PROJECT_ID ,与RECORDS_LISTCOLUMNID相同)

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

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