ORA-00907: missing right parenthesis but I cant see it? - sql

So this is my code for a homework
CREATE TABLE workorders
(
wo# NUMBER(5) PRIMARY KEY,
proj# VARCHAR(10) NOT NULL FOREIGN KEY REFERENCES project(proj#),
wo_desc VARCHAR(30) NOT NULL UNIQUE,
wo_assigned VARCHAR(30),
wo_hours NUMBER(5) NOT NULL CHECK(wo_hours>0),
wo_start DATE,
wo_due DATE,
wo_complete CHAR(1),
CONSTRAINT workorders_wo_complete_chk CHECK(wo_complete in('Y','N'))
);
I could not figure out why oracle apex won't let me create this table, it says
ORA-00907: missing right parenthesis
But I double-checked so many times and I think I do have all the parenthesis? What did I do wrong here?
Thanks in advance
I just want to create this table under these constreaints but I could find any errors that I know of.

Oracle's error messages are often not particularly helpful, and the best thing to do is to go through your code, line by line, commenting out each line until you isolate the problem.
In this case the offending line is
proj# VARCHAR(10) NOT NULL FOREIGN KEY REFERENCES project(proj#),
and the problem is that you don't use the words FOREIGN KEY to define a foreign key - you just need to specify what it references. So this should be
proj# VARCHAR(10) NOT NULL REFERENCES project(proj#),
and then you'll be fine.
db<>fiddle here

When defining an inline constraint, the FOREIGN KEY terms are not used:
proj# VARCHAR2(10) NOT NULL
REFERENCES project(proj#)
Note: Oracle uses VARCHAR2 and VARCHAR is an alias to VARCHAR2 and it is considered best-practice to use VARCHAR2 throughout.
However, those keywords are required when you define an out-of-line foreign key (but then you would be missing a comma and the column identifier):
proj# NOT NULL,
FOREIGN KEY (proj#) REFERENCES project (proj#)
Note: If you are using a foreign key constraint then you do not need to define the data type and Oracle will implicitly use the data type of the column being referenced.
The complete code using (named) inline constraints throughout would be:
CREATE TABLE workorders
(
wo# NUMBER(5)
CONSTRAINT workorders__wo#__pk PRIMARY KEY,
proj# -- Note: do not need the data type as the FK will define it.
NOT NULL
CONSTRAINT workorders__proj#__fk REFERENCES project(proj#),
wo_desc VARCHAR2(30)
NOT NULL
CONSTRAINT workorders__wo_desc__u UNIQUE,
wo_assigned VARCHAR2(30),
wo_hours NUMBER(5)
NOT NULL
CONSTRAINT workorders__wo_hours__chk CHECK(wo_hours>0),
wo_start DATE,
wo_due DATE,
wo_complete CHAR(1)
CONSTRAINT workorders_wo_complete_chk CHECK(wo_complete in('Y','N'))
);
and using (named) out-of-line constraints throughout:
CREATE TABLE workorders
(
wo# NUMBER(5),
proj# -- Note: do not need the data type as the FK will define it.
NOT NULL,
wo_desc VARCHAR2(30)
NOT NULL,
wo_assigned VARCHAR2(30),
wo_hours NUMBER(5)
NOT NULL,
wo_start DATE,
wo_due DATE,
wo_complete CHAR(1),
CONSTRAINT workorders__wo#__pk PRIMARY KEY(wo#),
CONSTRAINT workorders__proj#__fk FOREIGN KEY (proj#) REFERENCES project(proj#),
CONSTRAINT workorders__wo_desc__u UNIQUE(wo_desc),
CONSTRAINT workorders_wo_complete_chk CHECK(wo_complete in('Y','N')),
CONSTRAINT workorders__wo_hours__chk CHECK(wo_hours>0)
);
fiddle

Related

I'm getting an error on Oracle Apex ORA-00907: missing right parenthesis

I'm trying to run this snipped of SQL in Oracle Apex and keep receiving errors about right parenthesis. I have tried to remove the constraints or alter the tables later, but I keep coming up with the same sort of errors. I cannot seem to figure out what it is that's wrong with the table structure, and I can't find anything online about it that makes sense. Any help would be much appreciated, thanks. Code below...
DROP TABLE Employee_T
CASCADE CONSTRAINTS;
DROP TABLE TaxDepartment_T
CASCADE CONSTRAINTS;
DROP TABLE Location_T
CASCADE CONSTRAINTS;
CREATE TABLE Employee_T
(
EmployeeID NUMBER(11) NOT NULL,
EmployeeName VARCHAR2(25) NOT NULL,
EmployeeAddress VARCHAR2(30) ,
EmployeeCity VARCHAR2(20) ,
EmployeeState CHAR(2) ,
EmployeePostalCode VARCHAR2(10) ,
CONSTRAINT Employee_PK PRIMARY KEY(EmployeeID),
CONSTRAINT Employee_FK1 FOREIGN KEY(DepartmentID) REFERENCES (TaxDepartment_T),
CONSTRAINT Employee_FK2 FOREIGN KEY(BranchID) REFERENCES (Location_T)
);
CREATE TABLE TaxDepartment_T
(
DepartmentID INTEGER(11) NOT NULL,
BranchID INTEGER(11) NOT NULL,
CPAID INTEGER(11) NOT NULL,
EmployeeID INTEGER(11) NOT NULL,
BranchName VARCHAR2(50) NOT NULL,
CONSTRAINT TaxDepartment_PK PRIMARY KEY(DepartmentID, BranchID, CPAID),
CONSTRAINT TaxDepartment_FK1 FOREIGN KEY(BranchID) REFERENCES (Location_T),
CONSTRAINT TaxDepartment_FK2 FOREIGN KEY(EmployeeID) REFERENCES (Employee_T)
);
CREATE TABLE Location_T
(
BranchID INTEGER(11) NOT NULL,
BranchName VARCHAR2(50) NOT NULL,
ManagerName VARCHAR2(50) NOT NULL,
EmployeeID INTEGER(11) NOT NULL,
CONSTRAINT Location_PK PRIMARY KEY(BranchID),
CONSTRAINT Location_FK1 FOREIGN KEY(EmployeeID) REFERENCES (Employee_T)
);
Your foreign key constraint syntax is off.
What it should look like:
REFERENCES SCHEMA.TABLE (COLUMN)
and you just have:
REFERENCES (COLUMN)
If you look at this code in SQL Developer, the parser catches your issue right away, and even gives you a simple click to get to the Docs with syntax diagram for defining FK constraints.
This is your FIRST problem.
The fun with bugs is killing one only exposes the next one. You can't create FK constraints for tables you haven't created yet. So either you need to create the base tables first, OR you need to remove the FK constraints from your CREATE TABLE calls, and add them back later as
alter table TABLE_NAME add constraint CONSTRAINT_NAME foreign key(COLUMN_NAME) references TABLE_NAME2(COLUMN_NAME)
Place all of these ALTER TABLE ADD CONSTRAINT calls at the end of your script, once all the tables have already been created.
Someone else has also noticed that you're using INTEGER.
Which I do, ALL THE TIME...because I'm too lazy to type 'NUMBER(38,0)'
That's fine. But what you can't do is say INTEGER(9). That makes no sense in Oracle.
You should use NUMBER instead of INTEGER in TaxDepartment_T and Location_T tables
There are a few syntax issues.
integer is OK but not integer(11). Use number(11). (Also, while char is a valid type, you should stick to the standard varchar2 for strings to avoid unexpected behaviour.)
Foreign key constraints are written constraint fk references tablename, or optionally you can specify the referenced column in brackets: constraint fk references tablename (columnname). (Also if you write them inline as part of the column definition, you can let the datatype inherit from the parent.)
Employee FK1 and FK2 refer to DepartmentID and BranchID columns that the table doesn't have.
You need to put the parent before the child if you want to run it as a script.
I would write it like this:
drop table employee_t cascade constraints;
drop table taxdepartment_t cascade constraints;
drop table location_t cascade constraints;
create table Location_T
( BranchID number(11) not null constraint Location_PK primary key
, BranchName varchar2(50) not null
, ManagerName varchar2(50) not null );
create table TaxDepartment_T
( DepartmentID number(11) not null
, BranchID constraint TaxDepartment_Location_FK references location_t not null
, CPAID number(11) not null
, BranchName varchar2(50) not null
, constraint TaxDepartment_PK primary key(DepartmentID, BranchID, CPAID) );
create table Employee_T
( EmployeeID number(11) not null constraint Employee_PK primary key
, EmployeeName varchar2(25) not null
, EmployeeAddress varchar2(30)
, EmployeeCity varchar2(20)
, EmployeeState varchar2(2)
, EmployeePostalCode varchar2(10)
, DepartmentID constraint Employee_Department_FK references location_t
, BranchID constraint Employee_Branch_FK references Location_T );
I don't think Location or Tax Department should have EmployeeId columns so I removed them - say if you think that's wrong.
Personally I wouldn't put _T on the end of my table names, and I would avoid camelCase naming because the data dictionary doesn't retain it, and so describing a table gives for example:
SQL> #desc location_t
Name Null? Type
----------------------------------------- -------- ----------------------------
BRANCHID NOT NULL NUMBER(11)
BRANCHNAME NOT NULL VARCHAR2(50)
MANAGERNAME NOT NULL VARCHAR2(50)

Troubles creating tables

I'm trying to make two tables execute but I keep getting errors on them. Transaction table says it can't execute without the other being created and then I try to create the broker table and it says missing right parenthesis
SQL Error: ORA-00907: missing right parenthesis
but I checked my code and it looks well. Would any of you know what is the problem? thanks to all who comment!
Create table Broker
(
Broker_Number Number(7,0)
Constraint PK_Broker_Broker_Number Primary Key
Constraint NN_Broker_Broker_Number Not Null,
First_Name Varchar2(25)
Constraint NN_Broker_First_Name Not Null,
Last_Name Varchar2(3O)
Constraint NN_Broker_Last_Name Not Null,
Area_Code Number(3,0)
Default '780'
Constraint NL_Broker_Area_Code Null,
Phone_Number Number(7,0)
Constraint NL_Broker_Phone_Number Null,
Email_Address Varchar2(50)
Constraint CK_Broker_Email_Address Check (REGEXP_like(Email_Address,'%#%.%'))
Constraint NN_Broker_Email_Address Not Null
);
Create table Transaction
(
Portfolio_Number Number(7,0) Not Null,
Constraint FK_Trans_Portfolio_Number
Foreign Key (Portfolio_Number)
References Portfolio(Portfolio_Number),
Stock_Code Varchar2(10) Not Null,
Constraint FK_Transaction_Stock_Code
Foreign Key (Stock_Code)
References Stock(Stock_Code),
Transaction_Date Date
Constraint PK_Trans_Transaction_Date Not Null,
Exchange_Code Varchar2(4) Not Null,
Constraint FK_Transaction_Exchange_Code
Foreign Key (Exchange_Code)
References Exchange(Exchange_Code),
Broker_Number Number(7,0) Not Null,
Constraint FK_Transaction_Broker_Number
Foreign Key (Broker_Number)
References Broker(Broker_Number),
Buy_Sell Char(1)
Constraint CK_Transaction_Buy_Sell Check (Buy_Sell in ('B','S') )
Constraint NN_Transaction_Buy_Sell Not Null,
Quantity Number(7,0)
Constraint NN_Transaction_Quantity Not Null,
Price_Per_Share Number(6,2)
Constraint NN_Transaction_Price_Per_Share Not Null
)
It's this line
Last_Name Varchar2(3O)
That's not 30, it is 3O, as in the letter O after the number three.
One way to debug such error statements is to remove one column at a time, run the statement, and repeat the process until you don't encounter the error any more. In this case, I deleted this specific line and it worked fine. That's when I noticed the O instead of 0 in the editor.

foreign keys: number of columns not equal to referenced columns

I'm getting an error from oracle that says "number of referencing columns must match referenced columns."
I want my column recorded_on in the table measurement to reference recorded_on in the table called sample
The column Recorded on in the Sample table must be part of a composite key together with Scientist_Num
The error is coming from
FOREIGN KEY (Recorded_On) REFERENCES Sample(Scientist, Recorded_On, Site_ID)
CREATE TABLE Sample (
Scientist_Num varchar2(5) not null,
Recorded_On date not null,
Site_ID varchar2(4) not null,
Comments clob,
Primary key (Scientist_Num, Recorded_On),
FOREIGN KEY (Scientist_Num) REFERENCES Scientist(Scientist_Num),
FOREIGN KEY (Site_ID) REFERENCES Site(Site_ID)
);
CREATE TABLE Measurement (
Site_ID varchar2(4) not null,
Recorded_On date not null,
Name varchar2(10) not null,
Value varchar2(10),
Outlier_Indicator varchar2(10),
Primary key (Site_ID, Recorded_On, Name),
FOREIGN KEY (Site_ID) REFERENCES Sample(Site_ID),
FOREIGN KEY (Recorded_On) REFERENCES Sample(Scientist, Recorded_On, Site_ID)
);
The Scientist_Num and Recorded_On columns must be in a composite key together.
The answer to my problem and an explanation of what went wrong would be greatly appreciated.
You can create virtual column in sample table:
Recorded_virtual varchar2(5) [GENERATED ALWAYS] AS
(Scientist||Recorded_On||Site_ID) [VIRTUAL]
And create reference to this column:
CONSTRAINT fk_column
FOREIGN KEY (Recorded_On)
REFERENCES Sample(Recorded_virtual )
Foreign key references need to match the primary keys in number and type. So I think you intend:
CREATE TABLE Measurement (
Site_ID varchar2(4) not null,
Scientist_Num varchar2(5) not null,
----^ added for foreign key reference
Recorded_On date not null,
Name varchar2(10) not null,
Value varchar2(10),
Outlier_Indicator varchar2(10),
Primary key (Site_ID, Recorded_On, Name),
FOREIGN KEY (Site_ID) REFERENCES Site(Site_ID),
-------------------------------------^ Presumably you intend the site table
FOREIGN KEY (Scientist_Num, Recorded_On) REFERENCES
Sample(Scientist_Num, Recorded_On)
-----------------^ two columns, both need to already be defined
);
I suspect there are other issues with your data model, but this should fix the syntax error. If you want further help, then ask another question.

Error creating database with multiple primary key columns and referencing foreign key

I've been having an issue when creating a database. Each table has a primary key with many foreign keys used also. The issue I have is that I keep getting the error
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
I don't know what is causing this and is flagging the PROJECT_RECORDS table as the issue. I have used the same method in the PROJECT_TABLES table.
SQL/Oracle
CREATE TABLE PROJECT_DB
(DB_ID number (3) NOT NULL primary key,
DB_NAME varchar2 (25) NOT NULL,
DB_DESCRIPTION varchar2 (75) NOT NULL,
DB_DATE date NOT NULL);
CREATE TABLE PROJECT_DATATYPE
(DATATYPE_NAME varchar2 (20) NOT NULL PRIMARY KEY,
DATATYPE_DATATYPE varchar2(50) NOT NULL);
CREATE TABLE PROJECT_TABLES (
PROJECT_ID number(3) not null references PROJECT_DB(DB_ID) on delete cascade,
PROJECT_FIELDNAME varchar2(25) not null,
PROJECT_DATATYPE varchar2(50) not null references PROJECT_DATATYPE(DATATYPE_NAME),
PROJECT_LENGTH number(3),
PROJECT_REQUIRED varchar2(8),
PROJECT_LISTCOLUMNID number (3) not null,
primary key(PROJECT_ID, PROJECT_LISTCOLUMNID));
CREATE TABLE PROJECT_RECORDS (
RECORDS_ROWID number(3) not null,
RECORDS_LISTCOLUMNID number (3) not null references PROJECT_TABLES(PROJECT_LISTCOLUMNID)on delete cascade,
RECORDS_LISTID number (3) not null,
RECORDS_RECORDVALUE varchar2 (25),
primary key(RECORDS_ROWID, RECORDS_LISTCOLUMNID));
commit;
The reasoning for adding multiple primary keys to the PROJECT_TABLES table is that the listcolumnid isn't unique.
If listcolumnid is not unique, you cannot put a foreign key constraint on it. A foreign key always refrences exatcly one parent row. So you probably should use both columns in your foreign key (assuming there is really a 1:n relation):
CREATE TABLE PROJECT_RECORDS (
RECORDS_ROWID number(3) not null,
RECORDS_ID number(3) not null,
RECORDS_LISTCOLUMNID number (3) not null,
RECORDS_LISTID number (3) not null,
RECORDS_RECORDVALUE varchar2 (25),
primary key(RECORDS_ROWID, RECORDS_LISTCOLUMNID),
foreign key fk_project_projectrecords (RECORDS_ID, RECORDS_LISTCOLUMNID) references PROJECT_TABLES(PROJECT_ID, PROJECT_LISTCOLUMNID)on delete cascade,
);
(The example is using your prefix naming convention, I would change RECORDS_ID to PROJECT_ID, same for RECORDS_LISTCOLUMNID)
The problem is with your syntax. You don't do this:
, fieldname datatype references (something)
You do this:
, primary key(somefield)
, foreign key (somefield) references sometable(somefield)

Stuck on Oracle SQL constraint

CREATE TABLE STUDENT (
ST_ID NUMBER(2) PRIMARY KEY NOT NULL,
S_ID NUMBER(2) NOT NULL,
ST_FULL_NAME CHAR(25),
ST_GENDER CHAR(1) NOT NULL,
ST_AGE NUMBER(2),
ST_EMAIL VARCHAR(20) CHECK(ST_EMAIL LIKE ‘%#%’),
ST_GDATE DATE,
FOREIGN KEY (S_ID) REFERENCES STAFF,
CONSTRAINT STUDENT_CK_ST_GENDER CHECK (ST_GENDER IN ('M','F')),
CONSTRAINT STUDENT_CK_ST_GDATE CHECK (ST_GDATE > 01-JAN-2014)); --<-- constraint
I am stuck on the last constraint. Every time I get a message saying "JAN" invalid identifier.
Can anybody please identify the mistake and correct it for me?
CONSTRAINT STUDENT_CK_ST_GDATE CHECK (ST_GDATE > TO_DATE('20140101','YYYYMMDD'))