我从同事那里继承了一系列SQL脚本,我每年运行几次,现在已经两年了。这次,我遇到了一个从未见过的错误。代码如下:
/* First drop and then create table scbcrse_xref */
DROP TABLE ddef_stag.SCBCRSE_XREF;
CREATE TABLE ddef_stag.SCBCRSE_XREF AS SELECT
ddef_stag.SSBSECT.ssbsect_term_code,
ddef_stag.SSBSECT.ssbsect_crn,
ddef_stag.SSBSECT.ssbsect_subj_code,
ddef_stag.SSBSECT.ssbsect_crse_numb,
MAX(ddef_stag.SCBCRSE.scbcrse_eff_term) "SCBCRSE_MAX_EFF_TERM"
FROM ddef_stag.SCBCRSE, ddef_stag.SSBSECT
WHERE ddef_stag.SCBCRSE.scbcrse_subj_code = ddef_stag.SSBSECT.ssbsect_subj_code AND
ddef_stag.SCBCRSE.scbcrse_crse_numb = ddef_stag.SSBSECT.ssbsect_crse_numb AND
ddef_stag.SCBCRSE.scbcrse_eff_term <= ddef_stag.SSBSECT.ssbsect_term_code
GROUP BY ddef_stag.SSBSECT.ssbsect_term_code, ddef_stag.SSBSECT.ssbsect_crn,
ddef_stag.SSBSECT.ssbsect_subj_code, ddef_stag.SSBSECT.ssbsect_crse_numb;
-----------------------------------------------------------------------------
/* Add foreign key constraint on scbcrse_xref */
ALTER TABLE ddef_stag.SCBCRSE_XREF ADD
CONSTRAINT SCBCRSE_XREF_SSBSECT_FK
FOREIGN KEY (ssbsect_term_code, ssbsect_crn)
REFERENCES SSBSECT(ssbsect_term_code, ssbsect_crn);
-----------------------------------------------------------------------------
/* Add foreign key constraint on scbcrse_xref */
ALTER TABLE ddef_stag.SCBCRSE_XREF ADD
CONSTRAINT SCBCRSE_XREF_SCBCRSE_FK
FOREIGN KEY (ssbsect_subj_code, ssbsect_crse_numb, scbcrse_max_eff_term)
REFERENCES SCBCRSE (scbcrse_subj_code, scbcrse_crse_numb, scbcrse_eff_term);
两个外键约束都给出错误: ORA-02270:此列列表没有匹配的唯一键或主键
查看SCBCRSE和SSBSECT的表信息,它们看起来好像已启用了正确的主键。我已经看过SCBCRSE和SSBSECT,并且在这两个表之间的联接中没有明显的问题。怎么了?我还能检查什么?
编辑:我被要求发布有关表的describe():
describe scbcrse
TABLE scbcrse
Name Null? Type
----------------------------------------- -------- ----------------------------
SCBCRSE_SUBJ_CODE NOT NULL VARCHAR2(16)
SCBCRSE_CRSE_NUMB NOT NULL VARCHAR2(20)
SCBCRSE_EFF_TERM NOT NULL VARCHAR2(24)
SCBCRSE_COLL_CODE VARCHAR2(8)
SCBCRSE_DIVS_CODE VARCHAR2(16)
SCBCRSE_DEPT_CODE VARCHAR2(16)
SCBCRSE_CSTA_CODE VARCHAR2(4)
SCBCRSE_TITLE VARCHAR2(120)
SCBCRSE_CIPC_CODE VARCHAR2(24)
SCBCRSE_CREDIT_HR_IND VARCHAR2(8)
SCBCRSE_CREDIT_HR_LOW NUMBER(7,3)
SCBCRSE_CREDIT_HR_HIGH NUMBER(7,3)
SCBCRSE_LEC_HR_IND VARCHAR2(8)
SCBCRSE_LEC_HR_LOW NUMBER(7,3)
SCBCRSE_LEC_HR_HIGH NUMBER(7,3)
SCBCRSE_LAB_HR_IND VARCHAR2(8)
SCBCRSE_LAB_HR_LOW NUMBER(7,3)
SCBCRSE_LAB_HR_HIGH NUMBER(7,3)
SCBCRSE_OTH_HR_IND VARCHAR2(8)
SCBCRSE_OTH_HR_LOW NUMBER(7,3)
SCBCRSE_OTH_HR_HIGH NUMBER(7,3)
SCBCRSE_BILL_HR_IND VARCHAR2(8)
SCBCRSE_BILL_HR_LOW NUMBER(7,3)
SCBCRSE_BILL_HR_HIGH NUMBER(7,3)
SCBCRSE_APRV_CODE VARCHAR2(4)
SCBCRSE_REPEAT_LIMIT NUMBER(2)
SCBCRSE_PWAV_CODE VARCHAR2(4)
SCBCRSE_TUIW_IND VARCHAR2(4)
SCBCRSE_ADD_FEES_IND VARCHAR2(4)
SCBCRSE_ACTIVITY_DATE DATE
SCBCRSE_CONT_HR_LOW NUMBER(7,3)
SCBCRSE_CONT_HR_IND VARCHAR2(8)
SCBCRSE_CONT_HR_HIGH NUMBER(7,3)
SCBCRSE_CEU_IND VARCHAR2(4)
SCBCRSE_REPS_CODE VARCHAR2(8)
SCBCRSE_MAX_RPT_UNITS NUMBER(9,3)
SCBCRSE_CAPP_PREREQ_TEST_IND NOT NULL VARCHAR2(4)
SCBCRSE_DUNT_CODE VARCHAR2(16)
SCBCRSE_NUMBER_OF_UNITS NUMBER(7,2)
SCBCRSE_DATA_ORIGIN VARCHAR2(120)
SCBCRSE_USER_ID VARCHAR2(120)
SCBCRSE_PREREQ_CHK_METHOD_CDE NOT NULL VARCHAR2(4)
SCBCRSE_SURROGATE_ID NUMBER(19)
SCBCRSE_VERSION NUMBER(19)
SCBCRSE_VPDI_CODE VARCHAR2(24)
describe ssbsect
TABLE ssbsect
Name Null? Type
----------------------------------------- -------- ----------------------------
SSBSECT_TERM_CODE NOT NULL VARCHAR2(24)
SSBSECT_CRN NOT NULL VARCHAR2(20)
SSBSECT_PTRM_CODE VARCHAR2(12)
SSBSECT_SUBJ_CODE NOT NULL VARCHAR2(16)
SSBSECT_CRSE_NUMB NOT NULL VARCHAR2(20)
SSBSECT_SEQ_NUMB NOT NULL VARCHAR2(12)
SSBSECT_SSTS_CODE NOT NULL VARCHAR2(4)
SSBSECT_SCHD_CODE NOT NULL VARCHAR2(12)
SSBSECT_CAMP_CODE NOT NULL VARCHAR2(12)
SSBSECT_CRSE_TITLE VARCHAR2(120)
SSBSECT_CREDIT_HRS NUMBER(7,3)
SSBSECT_BILL_HRS NUMBER(7,3)
SSBSECT_GMOD_CODE VARCHAR2(4)
SSBSECT_SAPR_CODE VARCHAR2(8)
SSBSECT_SESS_CODE VARCHAR2(4)
SSBSECT_LINK_IDENT VARCHAR2(8)
SSBSECT_PRNT_IND VARCHAR2(4)
SSBSECT_GRADABLE_IND VARCHAR2(4)
SSBSECT_TUIW_IND VARCHAR2(4)
SSBSECT_REG_ONEUP NOT NULL NUMBER(4)
SSBSECT_PRIOR_ENRL NOT NULL NUMBER(4)
SSBSECT_PROJ_ENRL NOT NULL NUMBER(4)
SSBSECT_MAX_ENRL NOT NULL NUMBER(4)
SSBSECT_ENRL NOT NULL NUMBER(4)
SSBSECT_SEATS_AVAIL NOT NULL NUMBER(4)
SSBSECT_TOT_CREDIT_HRS NUMBER(9,3)
SSBSECT_CENSUS_ENRL NUMBER(4)
SSBSECT_CENSUS_ENRL_DATE NOT NULL DATE
SSBSECT_ACTIVITY_DATE NOT NULL DATE
SSBSECT_PTRM_START_DATE DATE
SSBSECT_PTRM_END_DATE DATE
SSBSECT_PTRM_WEEKS NUMBER(3)
SSBSECT_RESERVED_IND VARCHAR2(4)
SSBSECT_WAIT_CAPACITY NUMBER(4)
SSBSECT_WAIT_COUNT NUMBER(4)
SSBSECT_WAIT_AVAIL NUMBER(4)
SSBSECT_LEC_HR NUMBER(9,3)
SSBSECT_LAB_HR NUMBER(9,3)
SSBSECT_OTH_HR NUMBER(9,3)
SSBSECT_CONT_HR NUMBER(9,3)
SSBSECT_ACCT_CODE VARCHAR2(8)
SSBSECT_ACCL_CODE VARCHAR2(8)
SSBSECT_CENSUS_2_DATE DATE
SSBSECT_ENRL_CUT_OFF_DATE DATE
SSBSECT_ACAD_CUT_OFF_DATE DATE
SSBSECT_DROP_CUT_OFF_DATE DATE
SSBSECT_CENSUS_2_ENRL NUMBER(4)
SSBSECT_VOICE_AVAIL NOT NULL VARCHAR2(4)
SSBSECT_CAPP_PREREQ_TEST_IND NOT NULL VARCHAR2(4)
SSBSECT_GSCH_NAME VARCHAR2(40)
SSBSECT_BEST_OF_COMP NUMBER(3)
SSBSECT_SUBSET_OF_COMP NUMBER(3)
SSBSECT_INSM_CODE VARCHAR2(20)
SSBSECT_REG_FROM_DATE DATE
SSBSECT_REG_TO_DATE DATE
SSBSECT_LEARNER_REGSTART_FDATE DATE
SSBSECT_LEARNER_REGSTART_TDATE DATE
SSBSECT_DUNT_CODE VARCHAR2(16)
SSBSECT_NUMBER_OF_UNITS NUMBER(7,2)
SSBSECT_NUMBER_OF_EXTENSIONS NOT NULL NUMBER(3)
SSBSECT_DATA_ORIGIN VARCHAR2(120)
SSBSECT_USER_ID VARCHAR2(120)
SSBSECT_INTG_CDE VARCHAR2(20)
SSBSECT_PREREQ_CHK_METHOD_CDE VARCHAR2(4)
SSBSECT_SURROGATE_ID NUMBER(19)
SSBSECT_VERSION NUMBER(19)
SSBSECT_VPDI_CODE VARCHAR2(24)
SSBSECT_KEYWORD_INDEX_ID VARCHAR2(120)
describe SCBCRSE_XREF
TABLE SCBCRSE_XREF
Name Null? Type
----------------------------------------- -------- ----------------------------
SSBSECT_TERM_CODE NOT NULL VARCHAR2(24)
SSBSECT_CRN NOT NULL VARCHAR2(20)
SSBSECT_SUBJ_CODE NOT NULL VARCHAR2(16)
SSBSECT_CRSE_NUMB NOT NULL VARCHAR2(20)
SCBCRSE_MAX_EFF_TERM VARCHAR2(24)
答案 0 :(得分:1)
我与我们的DBA进行了交谈,发现没有正确启用SSBSECT和SCBCRSE的约束。启用它们可以解决问题。