简体   繁体   English

创建一个有 2 个主键和两个外键的表,引用 2 个具有相同约束名称的不同表

[英]Creating a table with 2 primary keys and two foreign keys referring to 2 different tables with same constraint name

For this particular schema:对于此特定架构:

这是架构

I have created the department student and subject tables now the problem right now arises in the creation of the mark table.我已经创建了系学生和主题表,现在问题出现在创建标记表中。

It is specified that there are 2 primary keys and 2 foreign keys , but as the title suggests creating 2 foreign keys referring to 2 different tables having the same constraint name seems impossible as per my understanding by surfing on the internet and reading on few threads here.指定有 2 个主键和 2 个外键,但正如标题所暗示的那样,通过在 Internet 上浏览并阅读这里的几个线程,根据我的理解,创建 2 个外键引用具有相同约束名称的 2 个不同表似乎是不可能的。 .

Is there any way to do this ?有没有办法做到这一点? I want to have both the foreign key's constraint name to be "fk".我想让外键的约束名称都为“fk”。

This code pops with a identifier error:此代码弹出标识符错误:

create table mark(
    value number,
    subject_id number,
    student_id number,
    constraint pk primary key(subject_id,student_id),
    constraint fk foreign key(subject_id,student_id) references subject(subject_id,student_id));

But even if i create 2 constraints with different name test cases fail.但即使我创建 2 个具有不同名称的约束,测试用例也会失败。 Is there a solution?有解决办法吗?

This is department table这是部门表

create table department(
    department_id number(2),
    department_name varchar(30),
    department_block_number number,
    constraint PK primary key(department_id));

This is student table这是学生桌

create table student(
        student_id number,
        student_name varchar(30),
        address varchar(40),
        city varchar(30),
        department_id number,
        constraint pk primary key(student_id),
        constraint fk foreign key(department_id) references department(department_id));

This is staff table这是员工桌

create table staff(
    staff_id number,
    staff_name varchar(30),
    department_id number,
    constraint pk primary key(staff_id),
    constraint fk foreign key(department_id) references department(department_id));

You have a composite primary key - on more than one column - which is fine;你有一个复合主键——在不止一列上——这很好; but you can't have a composite foreign key as the student table doesn't have a subject_id column - hence it giving you an invalid-identifier error.但是你不能有复合外键,因为学生表没有subject_id列 - 因此它会给你一个无效标识符错误。

You need two foreign keys, something like:您需要两个外键,例如:

create table mark(
    value number,
    subject_id number,
    student_id number,
    constraint mark_pk primary key(subject_id,student_id),
    constraint mark_fk_subject foreign key(subject_id) references subject(subject_id),
    constraint mark_fk_student foreign key(student_id) references student(student_id));

The constraint names have to be unique, both within and across tables in the same schema, and something more descriptive than 'pk' or 'fk' would be sensible anyway.约束名称必须是唯一的,无论是在同一架构中的表内还是跨表,并且比 'pk' 或 'fk' 更具描述性的东西无论如何都是明智的。

db<>fiddle 数据库<>小提琴

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

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