Getting an error that I'm missing a right paren when I am not [duplicate] - sql

This question already has answers here:
Missing right Paranthesis on Create Table command SQL
(2 answers)
Closed 3 years ago.
I'm getting an error when creating two tables that I'm missing a right paren on the second table although I am not.
I've tried different oracle variations of this code and I'm still getting the error.
(ORACLE LIVE SQL)
CREATE TABLE PET_OWNER
(OwnerID Integer Primary Key,
OwnerLastName Char(25) Not Null,
OwnerFirstName Char(25) Not Null,
OwnerPhone Char(25) Null,
OwnerEmail Char(50) Not Null);
CREATE TABLE PET_DATA
(PetID Integer Not Null,
PetName Char(50) Not Null,
PetType Char(25) Not Null,
PetBreed Char(50) Not Null,
PetDOB Varchar(50) Not Null,
Primary Key (PetID)
Constraint FK_PetOwner Foreign Key (OwnerID)
References Owner(OwnerID));
I expect the tables to be created but only the first table is being created successfully. The second table has a foreign key.

It looks like you are missing a comma after the primary-key definition on the second table.
The Oracle parser often complains about missing closing parentheses when the real issue is some other syntax error.

I would recommend:
CREATE TABLE PET_OWNER (
OwnerID Integer Primary Key,
OwnerLastName varchar2(25) Not Null,
OwnerFirstName varchar2(25) Not Null,
OwnerPhone varchar2(25) Null,
OwnerEmail varchar2(50) Not Null
);
CREATE TABLE PET_DATA (
PetID Integer Not Null,
OwnerID Integer,
PetName varchar2(50) Not Null,
PetType varchar2(25) Not Null,
PetBreed varchar2(50) Not Null,
PetDOB varchar2(50) Not Null,
Primary Key (PetID),
Constraint FK_PetOwner Foreign Key (OwnerID) References Pet_Owner(OwnerID)
);
This fixes small problems (missing OwnerId column in the second table, wrong table name). It also uses varchar2() for variable length strings rather than char() -- which are padded with spaces to the specified length.

Related

ORA-00907 (missing right parenthesis)

I'm trying to run this in my database but for some reason, I keep getting missing the right parenthesis. Thoughts?
CREATE TABLE PET_OWNER
(
OwnerID Int NOT NULL IDENTITY(1, 1),
OwnerLastName Char(25) NOT NULL,
OwnerFirstName Char(25) NOT NULL,
OwnerPhone Char(12) NULL,
OwnerEmail VarChar(100) NULL
CONSTRAINT OWNER_PK PRIMARY KEY(OwnerID)
);
EShirvana's answer directly answers the question you asked. However, I would suggest:
CREATE TABLE PET_OWNER (
OwnerID Int generated always as identity primary key,
OwnerLastName varchar2(25) NOT NULL,
OwnerFirstName varchar2(25) NOT NULL,
OwnerPhone varchar2(12),
OwnerEmail varchar2(100)
);
The differences:
The primary key constraint can be inlined. To be honest, I don't generally see much use for naming the primary key as a separate constraint (no harm).
Declaring a primary key column as NOT NULL is redundant. That is part of being a primary key.
Do you know what the char() data type does? It pads strings with spaces so they match the length. Use variable length strings -- and Oracle recommends varchar2() for this purpose.
By default, columns are NULLable. I actually find that explicitly declaring NULL is harder to read because I have to distinguish between NOT NULL and NULL which requires more work than just seeing NOT NULL.
2 issues:
identity and missing comma before constraint:
CREATE TABLE PET_OWNER(
OwnerID Int GENERATED ALWAYS AS IDENTITY NOT NULL,
OwnerLastName Char(25) NOT NULL,
OwnerFirstName Char(25) NOT NULL,
OwnerPhone Char(12) NULL,
OwnerEmail VarChar(100) NULL,
CONSTRAINT OWNER_PK PRIMARY KEY(OwnerID)
);

Why am I getting invalid identifier error?

I have already created two tables without issue. They are:
CREATE TABLE region
(
regionid CHAR NOT NULL,
regionname VARCHAR(25) NOT NULL,
PRIMARY KEY (regionid)
);
CREATE TABLE store
(
storeid VARCHAR(3) NOT NULL,
storezip CHAR(5) NOT NULL,
regionid CHAR NOT NULL,
PRIMARY KEY (storeid),
FOREIGN KEY (regionid) REFERENCES region(regionid)
);
However, when I try to enter the following table I get an error:
CREATE TABLE employee
(
employeeid VARCHAR(3) NOT NULL,
firstN CHAR(25) NOT NULL,
lastN CHAR(25) NOT NULL,
PRIMARY KEY (employeeid),
FOREIGN KEY (storeid) REFERENCES store(storeid)
);
The error I am getting is:
ORA-00904: "storeid": invalid identifier
Why am I getting this error? Thank you
You have:
FOREIGN KEY (storeid) REFERENCES store(storeid)
But you have not declared storeid. You need to declare it before you can specify that it is a foreign key.
Other notes:
Don't use char without a length. If it is one character, declare it as such.
I think id columns should be numeric rather than strings.
Don't use char() when you should be using varchar2(). char() pads the value out with spaces, and that is usually undesirable.

beginner sql missing keyword and invalid identifier

CREATE table Book
(
book_title varchar (100) not null ,
book_genre char(60) not null,
Date_of_publish date not null,
user_code char(7) not null ,
book_id char (7) primary key not null ,
constraint writer__id_fk foreign key (writer_id),
constraint publisher__id_fk foreign key (publisher_id)
);
I'm getting
[ORA-00905: missing keyword]
in publisher table
CREATE table publisher
(
publisher_id char (7) primary key not null,
publisher_name char(20) not null,
publisher_number char(10) not null,
publisher_email varchar2(60) not null,
publisher_address varchar2(60) not null,
);
I'm getting
[ORA-00904: : invalid identifier]
The following SQL creates a FOREIGN KEY on the "PersonID" column when the "Orders" table is created:
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
Refer this link for more details
https://www.w3schools.com/sql/sql_foreignkey.asp
Hope this helps.
Welcome to the wonderful world of SQL! :-)
General remark:
Please tell us what kind of DBMS you're using. MySQL? SQL Server? Oracle? SQlite? Different systems use different kinds of syntaxes.
First statement:
The problem seems to be in the FOREIGN KEY-portion.
Usually, you'll state something like:
CONSTRAINT [constraint_name] FOREIGN KEY([column_in_this_table]) REFERENCES OTHER_TABLE([column_in_other_table])
edit (added):
The [column_in_this_table] has to exist in your DDL (CREATE TABLE-statement), like so:
CREATE TABLE Book ( book_title ... etc., publisher_id INT, CONSTRAINT FK_publ_id FOREIGN KEY(publisher_id) REFERENCES publisher(publisher_id));
Here, you'll have a 'original' column called 'publisher_id', in the 'publisher'-table. You refer to it from within the 'Book'-table, by first having a 'publisher_id' column in the 'Book'-table (which should have the same DDL as the original column by the way). Next, you'll add a FOREIGN KEY to the 'Book'-table, that is imposed on the Book(publisher_id) column. Note, that you could also name the column in your 'Book'-table differently -- like, say, 'Spongebob' or 'Patrick'. But for future use, you'd like naming conventions that tell what you might expect to find in a column. So you'd name columns for what they contain.
Second statement:
The problem is with the last portion of your statement, where there's a comma after the NOT NULL portion for column publisher_address.
(Part of) your statement:
publisher_address varchar2(60) not null, );
Try replacing that with:
publisher_address VARCHAR2(60) NOT NULL);
edit (note to self):
VARCHAR2 turns out to be a valid datatype in Oracle databases (see:
Oracle documentation)
For your first table, the Foreign Keys do not reference any table. For your second table, I would imagine that comma after your last column isn't helping anything.
So this is the answer.
CREATE table Book
(
book_title varchar (100) not null ,
book_genre char(60) not null,
Date_of_publish date not null,
user_code char(7) not null ,
publisher_id char (7) not null,
writer_id char(7) not null,
book_id char (7) primary key not null ,
CONSTRAINT book_writer_id_fk FOREIGN KEY(writer_id) REFERENCES writer(writer_id),
CONSTRAINT book_publisher_id_fk FOREIGN KEY(publisher_id) REFERENCES publisher(publisher_id)
);
CREATE table publisher
(
publisher_id char (7) primary key not null,
publisher_name char(20) not null,
publisher_number char(10) not null,
publisher_email varchar2(60) not null,
publisher_address varchar2(60) not null
);

Error creating table in oracle

I am trying to create a table in oracle but I am getting this error: unknown command ")" - rest of line ignored. I can't figure out what is causing this error. Below is my SQL for the table:
CREATE TABLE PAYMENT
(PayNum INT NOT NULL PRIMARY KEY,
CType VARCHAR(1) NOT NULL,
CCNum VARCHAR(16) NOT NULL,
BankName VARCHAR(75) NOT NULL,
AccNum INT NOT NULL,
PDate DATE NOT NULL,
Amt DECIMAL(11,2) NOT NULL,
CONSTRAINT fk_BANKACC_PAYMENT FOREIGN KEY (BankName, AccNum)
REFERENCES BANKACC(BankName, AccNum),
CONSTRAINT fk_CRCARD_PAYMENT FOREIGN KEY (CType, CCNum)
REFERENCES CRCARD(CType, CCNum)
);
Your code is correct. Make sure you are referencing primary keys (all 4).
Check this: http://sqlfiddle.com/#!2/7be70/1/0
If you do not follow that, you may get this error: There are no primary or candidate keys in the referenced table 'BANKACC' that match the referencing column list in the foreign key 'fk_BANKACC_PAYMENT'.
Code in the fiddle above:
CREATE TABLE BANKACC
(BankName VARCHAR(75) NOT NULL,
AccNum INT NOT NULL,
PRIMARY KEY(BankName, AccNum));
CREATE TABLE CRCARD
(CType VARCHAR(1) NOT NULL,
CCNum VARCHAR(16) NOT NULL,
PRIMARY KEY(CType, CCNum));
CREATE TABLE PAYMENT
(PayNum INT NOT NULL PRIMARY KEY,
CType VARCHAR(1) NOT NULL,
CCNum VARCHAR(16) NOT NULL,
BankName VARCHAR(75) NOT NULL,
AccNum INT NOT NULL,
PDate DATE NOT NULL,
Amt DECIMAL(11,2) NOT NULL,
CONSTRAINT fk_BANKACC_PAYMENT FOREIGN KEY (BankName, AccNum)
REFERENCES BANKACC(BankName, AccNum),
CONSTRAINT fk_CRCARD_PAYMENT FOREIGN KEY (CType, CCNum)
REFERENCES CRCARD(CType, CCNum)
);
Also you should read this for better understanding on how to implement foreign key constraint: http://docs.oracle.com/cd/E17952_01/refman-5.5-en/create-table-foreign-keys.html
If you're running this in SQL*Plus, get rid of the blank line between REFERENCES and ):
REFERENCES CRCARD(CType, CCNum)
);
Or set sqlblanklines on to change the behaviour.
By default it interprets a blank line as the end of the statement, but doesn't run it. So your entire CREATE TABLE command is essentially ignored, and the ); is treated as a stanalone command. Hence the error message you get, since it doesn't mean anything on its own.
Please use NUMBER for numeric columns.
http://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm#CNCPT313
Also, in Oracle, use VARCHAR2 for strings.
but of it your syntax should be correct.

Creating Table (Oracle)

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)
);
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.
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)