如何从 userb.table 添加外键到 usera.table?
How to add foreign key to a usera.table from a userb.table?
我是 Oracle 新手,我尝试将外键 "SupplyMGR"."SUPPLIES"
添加到 "OrderMGR"."USEDIN"
。
但我收到以下错误
ALTER TABLE "SupplyMGR"."SUPPLIES" ADD CONSTRAINT OrderMGR_fk FOREIGN KEY (ORDERNO) REFERENCES "OrderMGR"."USEDIN"(ORDERNO)
Error report
ORA-00922: missing or invalid option
00922. 00000 - "missing or invalid option"
*Cause:
*Action:
代码:
ALTER TABLE "SupplyMGR"."SUPPLIES"
ADD CONSTRAINT OrderMGR_fk
FOREIGN KEY (ORDERNO) REFERENCES "OrderMGR"."USEDIN"(ORDERNO);
已创建所有表。
"SupplyMGR"."SUPPLIES"Table
+-----+--------------+---------------+-------------------+-----+-----+
| SID | SDESCRIPTION | AMOUNTINSTOCK | SEPTUSAGE |STYPE|ORDERNO
+-----+--------------+---------------+-------------------+-----+-----+
| 1 | desc2 | 3 | 20 | 200 |
| 2 | desc1 | 300 | 10 | 30 |
+-----+--------------+---------------+-------------------+-----+-----+
"OrderMGR"."USEDIN"Table
+---------+--------------+----------+--------------+-------+---------+
| ORDERNO | SDESCRIPTION | SUPPLIES | QUANTITYUSED | STYPE | BIGJOBS |
+---------+--------------+----------+--------------+-------+---------+
| 9001 | desc2 | 20 | 10 | type1 | Bigjob1 |
| 9002 | desc1 | 30 | 20 | type2 | Bigjob2 |
+---------+--------------+----------+--------------+-------+---------+
请帮忙
好吧,有效。我没有你的用户,也没有 tables,所以我使用了我自己的用户(虽然经过了简化)。
第一个用户:
SQL> show user
USER is "MIKE"
SQL>
SQL> create table usedin (orderno number primary key);
Table created.
SQL> grant references on usedin to scott;
Grant succeeded.
SQL>
第二个用户(引用 table):
SQL> connect scott/tiger
Connected.
SQL>
SQL> create table supplies
2 (sid number primary key,
3 orderno number);
Table created.
SQL> alter table supplies add constraint ordermgr_fk foreign key (orderno)
2 references mike.usedin (orderno);
Table altered.
SQL>
我是 Oracle 新手,我尝试将外键 "SupplyMGR"."SUPPLIES"
添加到 "OrderMGR"."USEDIN"
。
但我收到以下错误
ALTER TABLE "SupplyMGR"."SUPPLIES" ADD CONSTRAINT OrderMGR_fk FOREIGN KEY (ORDERNO) REFERENCES "OrderMGR"."USEDIN"(ORDERNO)
Error report
ORA-00922: missing or invalid option
00922. 00000 - "missing or invalid option"
*Cause:
*Action:
代码:
ALTER TABLE "SupplyMGR"."SUPPLIES"
ADD CONSTRAINT OrderMGR_fk
FOREIGN KEY (ORDERNO) REFERENCES "OrderMGR"."USEDIN"(ORDERNO);
已创建所有表。 "SupplyMGR"."SUPPLIES"Table
+-----+--------------+---------------+-------------------+-----+-----+
| SID | SDESCRIPTION | AMOUNTINSTOCK | SEPTUSAGE |STYPE|ORDERNO
+-----+--------------+---------------+-------------------+-----+-----+
| 1 | desc2 | 3 | 20 | 200 |
| 2 | desc1 | 300 | 10 | 30 |
+-----+--------------+---------------+-------------------+-----+-----+
"OrderMGR"."USEDIN"Table
+---------+--------------+----------+--------------+-------+---------+
| ORDERNO | SDESCRIPTION | SUPPLIES | QUANTITYUSED | STYPE | BIGJOBS |
+---------+--------------+----------+--------------+-------+---------+
| 9001 | desc2 | 20 | 10 | type1 | Bigjob1 |
| 9002 | desc1 | 30 | 20 | type2 | Bigjob2 |
+---------+--------------+----------+--------------+-------+---------+
请帮忙
好吧,有效。我没有你的用户,也没有 tables,所以我使用了我自己的用户(虽然经过了简化)。
第一个用户:
SQL> show user
USER is "MIKE"
SQL>
SQL> create table usedin (orderno number primary key);
Table created.
SQL> grant references on usedin to scott;
Grant succeeded.
SQL>
第二个用户(引用 table):
SQL> connect scott/tiger
Connected.
SQL>
SQL> create table supplies
2 (sid number primary key,
3 orderno number);
Table created.
SQL> alter table supplies add constraint ordermgr_fk foreign key (orderno)
2 references mike.usedin (orderno);
Table altered.
SQL>