将外键引用到其他类型

时间:2014-07-10 08:03:09

标签: sql oracle plsql

是否可以在PL / SQL中执行此操作:

CREATE TABLE ROOM_TYPE
(
   ROOM_TYPE_ID                     NUMBER NOT NULL PRIMARY KEY,
   REF_SERVICE_PROVIDER             NUMBER NOT NULL,
   ROOM_TYPE_NAME1                  VARCHAR2(100) NOT NULL,
   ROOM_TYPE_NAME2                  VARCHAR2(100),
   REF_SERVICE_ID                   NUMBER(15,3),
   ROOM_AC                          NUMBER(1),
   ROOM_BATH_ROOM                   NUMBER(1),
   ROOM_CHOICE_OF_MEAL              NUMBER(1),


   CONSTRAINT fk_ref_service_provider
   FOREIGN KEY (REF_SERVICE_PROVIDER)
   REFERENCES PROVIDER(PROVIDER_ID),

   CONSTRAINT fk_ref_service_id
   FOREIGN KEY (REF_SERVICE_ID)
   REFERENCES MEDICAL_SERVICE(SERVICE_ID)
);

REF_SERVICE_ID 类型的 number(15,3) 是此表中的外键,它指的是主键 {{1 SERVICE_ID 类型的

问题是,当外键是 number 类型时,我可以引用 number 类型的主键吗??

1 个答案:

答案 0 :(得分:1)

是的,你可以,但很有可能会出现以下错误:

ORA-01438: value larger than specified precision allowed for this column

例如:

CREATE TABLE A ( ID NUMBER NOT NULL PRIMARY KEY);
CREATE TABLE b
     (
          id  NUMBER(15,3),
          val CHAR(1),
          CONSTRAINT b_pk FOREIGN KEY (ID) REFERENCES a(id)
     );

     INSERT INTO A VALUES(456);
     INSERT INTO B VALUES( 456, 'X');  --inserts successfully

 INSERT INTO A VALUES(12345678901234567890);
 INSERT INTO B VALUES( 12345678901234567890, 'X'); --errors