简体   繁体   English

创建表时如何引用另一个模式表的外键?

[英]How to reference foreign key of another schema table while creating table?

I have two Schemas :- 我有两个模式:

  • GamersProfileDB - Contain UserCredential_tbl table GamersProfileDB-包含UserCredential_tbl
  • DeveloperDB - Contain PlayerAchievements_tbl table DeveloperDB-包含PlayerAchievements_tbl

Now while creating table called PlayerAchievements_tbl , i want to reference it from another schema table Usercredential_tbl . 现在,在创建名为PlayerAchievements_tbl表时,我想从另一个架构表Usercredential_tbl引用它。 Here is the following sql query :- 这是下面的SQL查询:

create table PlayerAchievements_tbl 
(
pid number(10) references gamersprofiledb.usercredential (id),
aid number(10) references achievements (id) 
);

But the above query gave me 'table or view does not exist' error 但是上面的查询给了我“表或视图不存在”的错误

This (possible duplicate) answer adding foreign_key in ALTER operation whereas i want it to add foreign_key on CREATE TABLE operation and Moreover it also didn't specify what grants or privileges may require to execute this query 这个 (可能重复)的答案是在ALTER操作中添加foreign_key,而我希望它在CREATE TABLE操作上添加foreign_key,而且它也没有指定执行此查询可能需要的授予或特权

So my exact question would be :- 所以我的确切问题是:

1) could it be possible to reference another schema table into existing schema table while creating new table ? 1)在创建新表时可以将另一个模式表引用到现有模式表中吗?

2) or do we need some privileges to execute this query ? 2)还是我们需要一些特权才能执行此查询?

Thanks in Advance. 提前致谢。

By default an (unprivileged) user has no permissions to see objects owned by another user. 默认情况下,(非特权)用户无权查看另一个用户拥有的对象。 You need to GRANT the privilege using: 您需要GRANT使用权限:

GRANT REFERENCES ON GamersProfileDB.UserCredential TO DeveloperDB;

or, if you also need to SELECT from the table then: 或者,如果您还需要从表中进行SELECT ,则:

GRANT SELECT ON GamersProfileDB.UserCredential TO DeveloperDB;

Once you have permissions to reference (or select) the table then you can use: 拥有引用(或选择)表的权限后,您可以使用:

CREATE TABLE PlayerAchievements_tbl                  -- Why add the "_tbl" suffix?
(
  pid number(10)
      CONSTRAINT PlayerAchievements__PID__FK         -- name the constraint
        REFERENCES GamersProfileDB.UserCredential (id),
  aid number(10)
      CONSTRAINT PlayerAchievements__AID__FK         -- name the constraint
        REFERENCES Achievements (id),
  CONSTRAINT PlayerAchievements__PID_AID__PK
    PRIMARY KEY ( pid, aid )
);

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

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