在Oracle中实现关系模型的错误

发布于 2025-01-24 06:15:55 字数 709 浏览 5 评论 0原文

mer

我需要在Oracle中创建此MER的帮助。特别是在FOTOS表中,因为我有此代码:

CREATE TABLE "FOTOS"
(  
    "ID_FOTO" INT NOT NULL ENABLE,
    "ID_USU" INT NOT NULL ENABLE,
    "FECHA" DATE NOT NULL ENABLE,

    CONSTRAINT "FOTOS_PK" 
        PRIMARY KEY ("ID_FOTO") ENABLE,
    CONSTRAINT "FOTOS_FK" 
        FOREIGN KEY ("ID_USU") REFERENCES "USUARIOS" ("ID_USU") ENABLE
)

但是我一直遇到此错误:

ORA-02270:此列列表

无匹配的唯一或主键

ID_USU无匹配的唯一或主键是USUARIOS中的两个主要键之一 usuarios table

MER

I need help creating this MER in Oracle. Specifically in FOTOS table as I have this code:

CREATE TABLE "FOTOS"
(  
    "ID_FOTO" INT NOT NULL ENABLE,
    "ID_USU" INT NOT NULL ENABLE,
    "FECHA" DATE NOT NULL ENABLE,

    CONSTRAINT "FOTOS_PK" 
        PRIMARY KEY ("ID_FOTO") ENABLE,
    CONSTRAINT "FOTOS_FK" 
        FOREIGN KEY ("ID_USU") REFERENCES "USUARIOS" ("ID_USU") ENABLE
)

However I keep getting this error:

ORA-02270: no matching unique or primary key for this column-list

ID_USU is one of two primary keys in USUARIOS
USUARIOS TABLE

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情泪▽动烟 2025-01-31 06:15:55

id_usu是usuarios中的两个主要键之一

一个表只有一个主键。它可以具有多个唯一的钥匙,但是您只能使其中一个主要键。

您错误地做的是创建一个复合主键。您希望ID是唯一的,并且名称是唯一的,但是您将ID和名称的组合做成了唯一的组合,因此可以在表中使用重复的ID和重复名称。

您拥有的:

create table usuarios
(
  id_usu  number        not null,
  nomusu  varchar2(50)  not null,
  ...
  constraint pk_usuarios primary key (id_usu, nomusu)
);

您想要什么:

create table usuarios
(
  id_usu  number        not null,
  nomusu  varchar2(50)  not null,
  ...
  constraint pk_usuarios primary key (id_usu),
  constraint uq_usuarios_nomusu unique (nomusu)
);

ID_USU is one of two primary keys in USUARIOS

No. A table can have just one primary key. It can have more than one unique key, but you can only make one of them primary.

What you mistakenly did instead is create a composite primary key. You want the ID to be unique and the name to be unique, but instead you made the combination of ID and name unique, thus allowing duplicate IDs and duplicate names in the table.

What you have:

create table usuarios
(
  id_usu  number        not null,
  nomusu  varchar2(50)  not null,
  ...
  constraint pk_usuarios primary key (id_usu, nomusu)
);

What you want instead:

create table usuarios
(
  id_usu  number        not null,
  nomusu  varchar2(50)  not null,
  ...
  constraint pk_usuarios primary key (id_usu),
  constraint uq_usuarios_nomusu unique (nomusu)
);
心不设防 2025-01-31 06:15:55

外键必须匹配主/唯一键,它们为列引用列。由于“ usuarios”的主要键是列“ id_usu”和“ nomusu”,因此您需要两个列才能将fk从“ fotos”添加到“ usuarios”中,所以只需将“ nomusu”列添加到“ fotos”并写入:

    CREATE TABLE "FOTOS"
    (  
    "ID_FOTO" INT NOT NULL ENABLE,
    "ID_USU" INT NOT NULL ENABLE,
    "NOMUSU" VARCHAR2(50) NOT NULL ENABLE,
    "FECHA" DATE NOT NULL ENABLE,

    CONSTRAINT "FOTOS_PK" 
        PRIMARY KEY ("ID_FOTO") ENABLE,
    CONSTRAINT "FOTOS_FK" 
        FOREIGN KEY ("ID_USU","NOMUSU") REFERENCES "USUARIOS" ("ID_USU","NOMUSU") ENABLE);
      

Foreign keys have to match the primary/unique key they reference column for column. Since the primary key of "USUARIOS" are columns "ID_USU" and "NOMUSU" you need two columns to add FK from "FOTOS" to "USUARIOS", so just add "NOMUSU" column to "FOTOS" and write:

    CREATE TABLE "FOTOS"
    (  
    "ID_FOTO" INT NOT NULL ENABLE,
    "ID_USU" INT NOT NULL ENABLE,
    "NOMUSU" VARCHAR2(50) NOT NULL ENABLE,
    "FECHA" DATE NOT NULL ENABLE,

    CONSTRAINT "FOTOS_PK" 
        PRIMARY KEY ("ID_FOTO") ENABLE,
    CONSTRAINT "FOTOS_FK" 
        FOREIGN KEY ("ID_USU","NOMUSU") REFERENCES "USUARIOS" ("ID_USU","NOMUSU") ENABLE);
      
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文