1

I am creating two tables. The first table creates with no errors, but when I try to create the SUBHEAD table, I get an error: Line 2, Missing right parenthesis. I am not sure what is wrong with this line. Below is my SQL statements:

CREATE TABLE HEAD
   (Code NUMERIC(4,0) NOT NULL PRIMARY KEY,
    HeadName VARCHAR(50) NOT NULL UNIQUE,
    HType VARCHAR(1) NOT NULL,
    HDate DATE NOT NULL,
    OpBal DECIMAL(11,2) NOT NULL
   );

CREATE TABLE SUBHEAD
   (HCode NUMERIC(4,0) NOT NULL FOREIGN KEY REFERENCES HEAD(Code),
    SubCode NUMERIC(4,0) NOT NULL,
    SubName VARCHAR(50) NOT NULL,
    SDate DATE NOT NULL,
    OpBal DECIMAL (11,2) NOT NULL,
    CONSTRAINT pk_subheadID PRIMARY KEY (HCode, SubCode)
   );
3
  • Put the foreign key declaration in the constraints clause. Commented Apr 6, 2014 at 19:53
  • How comes it doesn't work where I have it? I thought you could do it in both places. Commented Apr 6, 2014 at 19:56
  • 1
    I'm not big on theory but I can get things done. Commented Apr 6, 2014 at 20:04

2 Answers 2

2

syntax: http://docs.oracle.com/cd/B28359_01/server.111/b28286/clauses002.htm#SQLRF52167
note that "in-line" constraint syntax is : "col col_type REFERENCES TAB(COL)" and not "FOREIGN KEY"

The data type for number is NUMBER not NUMERIC

So my memory did not fool me - you can have multiple in-line constraints (although the syntax graph doesn't show it):

SQL> create table tt1(a number primary key);

Table created.

SQL> create table tt2(a number references tt1(a) not null);

Table created.
Sign up to request clarification or add additional context in comments.

Comments

2
CREATE TABLE SUBHEAD
   (HCode NUMERIC(4,0) NOT NULL, FOREIGN KEY (Hcode) REFERENCES HEAD(Code),
    SubCode NUMERIC(4,0) NOT NULL,
    SubName VARCHAR(50) NOT NULL,
    SDate DATE NOT NULL,
    OpBal DECIMAL (11,2) NOT NULL,
    CONSTRAINT pk_subheadID PRIMARY KEY (HCode, SubCode)
   );

(note comma, and reference to the new table column)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.