2011-12-08 11 views
1

2つの多対多表の間にあるOracleでリレーション/表を作成しようとしているため、この表の主キーは複合キーですが、両方のキーは外部キーです。複合外部キー - Oracleでは可能ですか?

CREATE TABLE employee_licence_certificate(
    emp_id NUMBER(4) REFERENCES employee(emp_id) 
    , licence_cert_code VARCHAR2(6) REFERENCES licence_certificate(licence_cert_code) 
    , date_earned DATE NOT NULL 
    ) 
PRIMARY KEY (emp_id, licence_cert_code)) 

私は、組成キーの方法を使用して試してみましたが、私は不思議が、このことも可能である私を作るために開始され、次のエラーを、得るように見えますか?

Error starting at line 1 in command: 
CREATE TABLE employee_licence_certificate(emp_id NUMBER(4) REFERENCES employee(emp_id) 
, licence_cert_code VARCHAR2(6) REFERENCES licence_certificate(licence_cert_code) 
, date_earned DATE NOT NULL) PRIMARY KEY (emp_id, licence_cert_code)) 
Error at Command Line:3 Column:29 
Error report: 
SQL Error: ORA-00922: missing or invalid option 
00922. 00000 - "missing or invalid option" 
*Cause:  
*Action: 
+0

[SQLエラー:ORA-00922:複合キーを作成するオプションがありません](http://stackoverflow.com/questions/8426047/sql-error -ora-00922-missing-or-invalid-option-creating-composite-key) –

+0

はいごめんなさい昨日の夜/午前に私は徹夜で働いて忘れていたことを忘れていました。私を許して。 @JustinCave –

答えて

4

これを試してみてください:

CREATE TABLE employee_licence_certificate(
    emp_id NUMBER(4) REFERENCES employee(emp_id) 
    , licence_cert_code VARCHAR2(6) REFERENCES licence_certificate(licence_cert_code) 
    , date_earned DATE NOT NULL 
    , 
PRIMARY KEY (emp_id, licence_cert_code)) 
+0

はい、うまくいきましたが、私はそれが私のものとまったく同じであると誓っています。私は何かを見落としていると思います。これを指摘してください、私は将来同じ誤りを犯さないでくれてありがとう! @chance –

+0

右カッコの間違った位置にカンマがありません。 – chance

+0

日付の列と主キーの間に閉じ括弧 ')'がありましたが、@chanceはカンマ '、'を使用しています。 – Sodved

2

それは確か可能です。あなたは、単にあなたの文を修正する必要があります。

CREATE TABLE employee_licence_certificate(emp_id NUMBER(4) REFERENCES employee(emp_id) 
, licence_cert_code VARCHAR2(6) REFERENCES licence_certificate(licence_cert_code) 
, date_earned DATE NOT NULL, PRIMARY KEY (emp_id, licence_cert_code)) 

ところで、あなたが適切にあなたの文をフォーマットするとき、このようなエラーを発見する方がはるかに簡単です:

create table employee_licence_certificate 
(
    emp_id number(15) references employee(emp_id), 
    licence_cert_code VARCHAR2(6) REFERENCES licence_certificate(licence_cert_code), 
    date_earned date not null, 
    primary key (emp_id, licence_cert_code) 
) 
4

私は別の構文を使用します。私は明示的に私の外来のキー制約に名前をつけて、その違反がより意味/追跡可能な場合にエラーメッセージが出るようにしたい。だから私はこのような何かをするでしょう:

CREATE TABLE employee_licence_certificate 
( emp_id    NUMBER(4) NOT NULL 
, licence_cert_code VARCHAR2(6) NOT NULL 
, date_earned   DATE  NOT NULL 
, CONSTRAINT elc_pk PRIMARY KEY (emp_id, licence_cert_code) 
, CONSTRAINT elc_emp_fk FOREIGN KEY (emp_id) 
     REFERENCES employee(emp_id) 
, CONSTRAINT elc_lct_fk FOREIGN KEY (licence_cert_code) 
     REFERENCES licence_certificate(licence_cert_code) 
) 
+1

あなた、私はちょうど同じ答えを投稿しようとしていた:) – jFrenetic

関連する問題