ORA-00906: missing left parenthesis - LIES? - sql

I have stared at this until my eyeballs bleed, where am I missing a parenthesis? It does also say
Error starting at line: 1 in command-".
The cause and action section of the error report is blank.
CREATE TABLE EVENTREQUEST(
EVENTNO VARCHAR2(8) CONSTRAINT EVENTNO_NOTNULL NOT NULL,
DATEHELD DATE CONSTRAINT DATEHELD_NOTNULL NOT NULL,
DATEREQ DATE CONSTRAINT DATEREQ_NOTNULL NOT NULL,
CUSTNO VARCHAR2(8) CONSTRAINT CUSTNO_NOTNULL2 NOT NULL,
FACNO VARCHAR2(8) CONSTRAINT CUSTNO_NOTNULL2 NOT NULL,
DATEAUTH DATE,
STATUS VARCHAR2(15) CHECK (STATUS IN ('Pending', 'Denied', 'Approved')) CONSTRAINT STATUS_NOTNULL NOT NULL,
ESTCOAST VARCHAR2(30) CONSTRAINT ESTCOAST_NOTNULL NOT NULL,
ESTAUDIENCE VARCHAR2(30) CHECK(ESTAUDIENCE > 0) CONSTRAINT ESTAUDIENCE_NOTNULL NOT NULL,
BUDNO VARCHAR2(8),
CONSTRAINT PK_EVENTNO PRIMARY KEY,
CONSTRAINT FK_CUSTONO FOREIGN KEY (CUSTNO) REFERENCES CUSTOMER(CUSTNO),
CONSTRAINT FK_FACNO FOREIGN KEY (FACNO) REFERENCES FACILITY(FACNO)
);

This just because you missed column name while declaring primary key. There is another problem: constraint name for both fourth and fifth columns are same. I have changed that too.
And there is no need to declare CONSTRAINT EVENTNO_NOTNULL NOT NULL since you are declaring it as primary key.
CREATE TABLE EVENTREQUEST(
EVENTNO VARCHAR2(8) CONSTRAINT EVENTNO_NOTNULL NOT NULL,
DATEHELD DATE CONSTRAINT DATEHELD_NOTNULL NOT NULL,
DATEREQ DATE CONSTRAINT DATEREQ_NOTNULL NOT NULL,
CUSTNO VARCHAR2(8) CONSTRAINT CUSTNO_NOTNULL2 NOT NULL,
FACNO VARCHAR2(8) CONSTRAINT FACNO_NOTNULL2 NOT NULL,
DATEAUTH DATE,
STATUS VARCHAR2(15) CHECK (STATUS IN ('Pending', 'Denied', 'Approved')) CONSTRAINT STATUS_NOTNULL NOT NULL,
ESTCOAST VARCHAR2(30) CONSTRAINT ESTCOAST_NOTNULL NOT NULL,
ESTAUDIENCE VARCHAR2(30) CHECK(ESTAUDIENCE > 0) CONSTRAINT ESTAUDIENCE_NOTNULL NOT NULL,
BUDNO VARCHAR2(8),
CONSTRAINT PK_EVENTNO PRIMARY KEY (EVENTNO),
CONSTRAINT FK_CUSTONO FOREIGN KEY (CUSTNO) REFERENCES CUSTOMER(CUSTNO),
CONSTRAINT FK_FACNO FOREIGN KEY (FACNO) REFERENCES FACILITY(FACNO));

Related

Simple database with 3 tables and "no matching unique or primary key for this column"

I have three tables, two are created independently, and the third one is created to include some inputs from the first two. First two tables have no problems, however, when I try to create the third one, I get an error:
Error report -
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
The thing is, I created the third table by copy/pasting the column names/definitions right from the first two tables, yet I still get this ridiculous error message. Now I wonder if the order of the columns and the especially the order of constrains is important.
The tables:
comm_Customers
CREATE TABLE comm_Customers (
custID NUMBER(6) NOT NULL,
FirstName VARCHAR2(10) NOT NULL,
LastName VARCHAR2(15) NOT NULL,
HomeCountry VARCHAR2(2) NOT NULL,
HomeState_Prov VARCHAR2(2) NOT NULL,
HomeCity VARCHAR2(20) NOT NULL,
HomeAddress VARCHAR2(25) NOT NULL,
Phone NUMBER(10) NOT NULL,
Email VARCHAR2(15) NOT NULL,
ShippCountry VARCHAR2(2) NOT NULL,
ShippState_Prov VARCHAR2(2) NOT NULL,
ShippCity VARCHAR2(10) NOT NULL,
ShippAddress VARCHAR2(15) NOT NULL,
CONSTRAINT comm_customers_custid_pk PRIMARY KEY (custID)
);
comm_Items
CREATE TABLE comm_Items (
itemID NUMBER(4) NOT NULL,
ItemCat VARCHAR2(3) NOT NULL,
ItemQty NUMBER(4) NOT NULL,
SalePrice NUMBER(6,2) NOT NULL,
CostPrice NUMBER(6,2) NOT NULL,
ItemDesc VARCHAR2(15),
CONSTRAINT comm_items_itemid_pk PRIMARY KEY (itemID)
);
comm_Orders which gives the error
CREATE TABLE comm_Orders (
orderID NUMBER(10) NOT NULL,
OrderQty NUMBER(4) NOT NULL,
OrderDate DATE NOT NULL,
Shipped VARCHAR2(1),
ShippedDate DATE,
custID NUMBER(6) NOT NULL,
Phone NUMBER(10) NOT NULL,
Email VARCHAR2(15) NOT NULL,
ShippCountry VARCHAR2(2) NOT NULL,
ShippState_Prov VARCHAR2(2) NOT NULL,
ShippCity VARCHAR2(10) NOT NULL,
ShippAddress VARCHAR2(15) NOT NULL,
itemID NUMBER(4) NOT NULL,
SalePrice NUMBER(6,2) NOT NULL,
CONSTRAINT comm_order_orderid_pk PRIMARY KEY (orderID),
CONSTRAINT comm_order_custid_fk FOREIGN KEY (custID)
REFERENCES comm_Customers(custID),
CONSTRAINT comm_order_phone_fk FOREIGN KEY (Phone)
REFERENCES comm_Customers(Phone),
CONSTRAINT comm_order_email_fk FOREIGN KEY (Email)
REFERENCES comm_Customers(Email),
CONSTRAINT comm_order_shippcountry_fk FOREIGN KEY (ShippCountry)
REFERENCES comm_Customers(ShippCountry),
CONSTRAINT comm_order_shippstate_prov_fk FOREIGN KEY (ShippState_Prov)
REFERENCES comm_Customers(ShippState_Prov),
CONSTRAINT comm_order_shippcity_fk FOREIGN KEY (ShippCity)
REFERENCES comm_Customers(ShippCity),
CONSTRAINT comm_order_shippaddress_fk FOREIGN KEY (ShippAddress)
REFERENCES comm_Customers(ShippAddress),
CONSTRAINT comm_order_itemid_fk FOREIGN KEY (itemID)
REFERENCES comm_Items(itemID),
CONSTRAINT comm_order_saleprice_fk FOREIGN KEY (SalePrice)
REFERENCES comm_Items(SalePrice)
ON DELETE CASCADE,
CONSTRAINT comm_order_shipped_chk CHECK (Shipped IN ('Y','N'))
);
The references to non-primary key columns of customers and items do raise the error.
Bottom-line, you should not be duplicating the information from the referential tables. A single foreign key is sufficient.
So:
CREATE TABLE comm_Orders (
orderID NUMBER(10) NOT NULL,
OrderQty NUMBER(4) NOT NULL,
OrderDate DATE NOT NULL,
Shipped VARCHAR2(1),
ShippedDate DATE,
custID NUMBER(6) NOT NULL,
itemID NUMBER(4) NOT NULL,
CONSTRAINT comm_order_orderid_pk PRIMARY KEY (orderID),
CONSTRAINT comm_order_custid_fk FOREIGN KEY (custID) REFERENCES comm_Customers(custID),
CONSTRAINT comm_order_itemid_fk FOREIGN KEY (itemID) REFERENCES comm_Items(itemID),
CONSTRAINT comm_order_shipped_chk CHECK (Shipped IN ('Y','N'))
);
Then, whenever you need to recover an information from a referential table, you join it using the foreign key. Say you want the phone of a customer:
select o.*, c.phone
from comm_orders o
inner join comm_customers c on c.custid = o.custid

Table with foreign key won't compile (no matching unique or primary key for this column-list)

I've create two tables with some columns and all that stuff. And I get the error "no matching unique or primary key for this column-list", but I've no clue what I am missing...
It's a basic primary and foreign keys tables and it won't compile. I have check on internet for hours and found out nothing.
create table TP2_ITEM_FAVORI (
NOM_UTILISATEUR varchar2(30) not null,
NO_ITEM number(6) not null,
constraint PK_ITEM_FAV primary key(NOM_UTILISATEUR, NO_ITEM));
create table TP2_UTILISATEUR (
NOM_UTILISATEUR varchar2(30) not null,
NO_ENCAN number(6) not null,
MOT_DE_PASSE_UTI varchar(30) not null,
NOM_UTI varchar2(20) not null,
PRENOM_UTI varchar2(20) not null,
TEL_UTI char(13) not null,
COURRIEL_UTI varchar2(25) not null,
TYPE_UTI varchar(20) not null,
NOM_UTILISATEUR_PARENT varchar2(30) not null,
constraint PK_UTILISATEUR primary key(NOM_UTILISATEUR),
constraint AK_NOM_PRENOM_TEL_UTI unique(NOM_UTI, PRENOM_UTI, TEL_UTI),
constraint AK_COURR_UTI unique(COURRIEL_UTI),
constraint FK_NOM_UTILISATEUR_UTI foreign key(NOM_UTILISATEUR) references
TP2_ITEM_FAVORI(NOM_UTILISATEUR));
A foreign key needs to be a primary key or unique column of another table: https://www.techonthenet.com/oracle/errors/ora02270.php.
In TP2_UTILISATEUR, you references TP2_ITEM_FAVORI(NOM_UTILISATEUR) as a foreign key, but NOM_UTILISATEUR is not the primary key of TP2_ITEM_FAVORI. TP2_ITEM_FAVORI's primary key is (NOM_UTILISATEUR, NO_ITEM).
Resolution is to change TP2_ITEM_FAVORI's primary key to NOM_UTILISATEUR instead of both columns.

Error report - ORA-02253: constraint specification not allowed here

I want to create this below table but I'mm getting the error I don't understand why, could someone explain?
CREATE TABLE STUD (
RNO NUMBER(3) CONSTRAINT PK_RNO_STUD PRIMAY KEY,
SNAME VARCHAR2(15) CONSTRAINT NN_SNAME_STUD NOT NULL,
COURSE VARCHAR2(10) CONSTRAINT NN_COURSE_STUD NOT NULL,
FEE NUMBER(5) CONSTRAINT NN_FEE_STUD NOT NULL,
MOBILE NUMBER(10) CONSTRAINT UK_MOBIE_STUD UNIQUE,
CONSTRAINT CK_RNO_STUD CHECK(RNO BETWEEN 1 AND 60),
CONSTRAINT CK_COURSE_STUD CHECK(COURSE IN('ORACLE','SQL SERVER','UNIX')),
CONSTRAINT CK_FEE_STUD CHECK(FEE BETWEEN 5000 AND 10000)
);
Error report - ORA-02253: constraint specification not allowed here
02253. 00000 - "constraint specification not allowed here"
*Cause: Constraint specification is not allowed here in the statement.
*Action: Remove the constraint specification from the statement.
You have misspelled PRIMARY KEY:
CREATE TABLE STUD (
RNO NUMBER(3) CONSTRAINT PK_RNO_STUD PRIMARY KEY,
SNAME VARCHAR2(15) CONSTRAINT NN_SNAME_STUD NOT NULL,
COURSE VARCHAR2(10) CONSTRAINT NN_COURSE_STUD NOT NULL,
FEE NUMBER(5) CONSTRAINT NN_FEE_STUD NOT NULL,
MOBILE NUMBER(10) CONSTRAINT UK_MOBIE_STUD UNIQUE,
CONSTRAINT CK_RNO_STUD CHECK(RNO BETWEEN 1 AND 60),
CONSTRAINT CK_COURSE_STUD CHECK(COURSE IN('ORACLE','SQL SERVER','UNIX')),
CONSTRAINT CK_FEE_STUD CHECK(FEE BETWEEN 5000 AND 10000)
);

ORA-02253: constraint specification not allowed here - foreign key

I'm new to this sql and I wanted to know what the problem is in this case.
"constraint specification not allowed here"
Cause: Constraint specification is not allowed here in the statement.
CREATE TABLE CLIENTE(
RUT VARCHAR2(10) CONSTRAINT CLIENTE_PK PRIMARY KEY,
NOMBRE VARCHAR2(20) CONSTRAINT NOMBRE_CLIENTE NOT NULL,
APELLIDOP VARCHAR2(30) CONSTRAINT APELLIDOP_CLIENTE NOT NULL,
APELLIDOM VARCHAR2(30) CONSTRAINT APPELIDOM_CLIENTE NOT NULL,
DIRECCION VARCHAR2(100) CONSTRAINT DIRECCION_CLIENTE NOT NULL,
TELEFONO NUMBER(8) CONSTRAINT TELEFONO_CLIENTE NOT NULL,
EMAIL VARCHAR2(255) CONSTRAINT EMAIL_CLIENTE NOT NULL,
FECHA_NACIMIENTO DATE CONSTRAINT FECHA_NACIMIENTO_CLIENTE NOT NULL,
SEXO CHAR(1) CONSTRAINT SEXO_CLIENTE NOT NULL,
COD_CIUDAD NUMBER(3) CONSTRAINT CLIENTE_CIUDAD_FK FOREIGN KEY (COD_CIUDAD)
REFERENCES CIUDAD (COD_CIUDAD)
);
In the last line of code, you define column cod_ciudad and you add a foreign key constraint.
There are two categories of constraints: IN-LINE constraints, defined right after the column name and data type (they can only affect "that" column) and OUT-OF-LINE constraints, which come at the bottom of the table, after all the column definitions. Constraints that involve more than one column can only be out-of-line constraints.
The constraint you define in your code is on one column only, and moreover, it is on cod_ciudad. You did NOT add a comma between the column definition (name and data type); that is the correct syntax for IN-LINE constraints. But an IN-LINE foreign key constraint does not need the keywords FOREIGN KEY, and they don't need to repeat the column name in parentheses. The constraint, being IN-LINE, can only refer to the "current" column. So: One way to fix your code is to REMOVE FOREIGN KEY (COD_CIUDAD) from the code.
Another way to fix it is to make the constraint into an OUT-OF-LINE constraint. Then the syntax is correct, but you must add a comma (,) before the keyword CONSTRAINT.
Try This:
CREATE TABLE CLIENTE(
RUT VARCHAR2(10) NOT NULL,
NOMBRE VARCHAR2(20) NOT NULL,
APELLIDOP VARCHAR2(30) NOT NULL,
APELLIDOM VARCHAR2(30) NOT NULL,
DIRECCION VARCHAR2(100) NOT NULL,
TELEFONO NUMBER(8) NOT NULL,
EMAIL VARCHAR2(255) NOT NULL,
FECHA_NACIMIENTO DATE NOT NULL,
SEXO CHAR(1) NOT NULL,
COD_CIUDAD NUMBER(3)
);
ALTER TABLE CLIENTE
ADD CONSTRAINT CLIENTE_PK PRIMARY KEY (RUT)
USING INDEX;
ALTER TABLE CLIENTE
ADD CONSTRAINT CLIENTE_CIUDAD_FK FOREIGN KEY (COD_CIUDAD)
REFERENCES [FOREIGN KEY TABLE NAME] ([PRIMARY KEY COLUMN OF FOREIGN KEY TABLE]);
you must seperate the constraint from the column declaration
Just use the following command
CREATE TABLE CLIENTE(
RUT VARCHAR2(10) CONSTRAINT CLIENTE_PK PRIMARY KEY,
NOMBRE VARCHAR2(20) CONSTRAINT NOMBRE_CLIENTE NOT NULL,
APELLIDOP VARCHAR2(30) CONSTRAINT APELLIDOP_CLIENTE NOT NULL,
APELLIDOM VARCHAR2(30) CONSTRAINT APPELIDOM_CLIENTE NOT NULL,
DIRECCION VARCHAR2(100) CONSTRAINT DIRECCION_CLIENTE NOT NULL,
TELEFONO NUMBER(8) CONSTRAINT TELEFONO_CLIENTE NOT NULL,
EMAIL VARCHAR2(255) CONSTRAINT EMAIL_CLIENTE NOT NULL,
FECHA_NACIMIENTO DATE CONSTRAINT
FECHA_NACIMIENTO_CLIENTE NOT NULL,
SEXO CHAR(1) CONSTRAINT SEXO_CLIENTE NOT NULL,
COD_CIUDAD NUMBER(3),
CONSTRAINT CLIENTE_CIUDAD_FK FOREIGN KEY (COD_CIUDAD)
REFERENCES CIUDAD (COD_CIUDAD)
);

Why this SQL code showing error of "missing right parenthesis"?

CREATE TABLE PoolActivity_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
ActivityID NUMBER(11,0) NOT NULL,
CONSTRAINT PoolActivity_PK PRIMARY KEY (PoolID,ServiceDate,ActivityID)
CONSTRAINT PoolActivity_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID),
CONSTRAINT PoolActivity_FK FOREIGN KEY (ActivityID)
REFERENCES Activity_T (ActivityID)
);
Here is my code and I can not figure out why comes the error.
CREATE TABLE Customer_T
(CustomerID NUMBER(11,0) NOT NULL,
CustomerFname VARCHAR2(25),
CustomerMname VARCHAR2(25),
CustomerLname VARCHAR2(25),
CustomerAddress1 VARCHAR2(50),
CustomerAddress2 VARCHAR2(50),
CustomerCity VARCHAR2(25),
CustomerState CHAR(2),
CustomerZipCode NUMBER(5,0),
CustomerPhoneNumber NUMBER(11,0),
CONSTRAINT Customer_PK PRIMARY KEY (CustomerID)
);
CREATE TABLE Pool_T
(PoolID NUMBER(11,0) NOT NULL,
PoolAddress1 VARCHAR2(25),
PoolAddress2 VARCHAR2(25),
PoolCity VARCHAR2(25),
PoolState VARCHAR2(25),
PoolZipCode NUMBER(5,0),
TypeSurface VARCHAR2(9),
Gallon INTEGER,
Filters VARCHAR2(25),
VisitPerMonth INTEGER,
Dates DATE DEFAULT SYSDATE,
Fee NUMBER(11,0),
CONSTRAINT Pool_PK PRIMARY KEY (PoolID)
);
CREATE TABLE PoolVisit_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
LengthStay INTEGER,
CONSTRAINT Poolvisit_PK PRIMARY KEY (PoolID,ServiceDate)
CONSTRAINT PoolVisit_FK FOREIGN KEY (PoolID)
REFERENCES Pool_T (PoolID)
);
CREATE TABLE PoolActivity_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
ActivityID NUMBER(11,0) NOT NULL,
CONSTRAINT PoolActivity_PK PRIMARY KEY (PoolID,ServiceDate,ActivityID)
CONSTRAINT PoolActivity_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID),
CONSTRAINT PoolActivity_FK FOREIGN KEY (ActivityID)
REFERENCES Activity_T (ActivityID)
);
CREATE TABLE PoolChemical_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
ChemicalName VARCHAR2(25) NOT NULL,
Quantity INTEGER,
Cost NUMBER(11,2),
CONSTRAINT PoolChemical_PK PRIMARY KEY (PoolID,ServiceDate,ChemicalName)
CONSTRAINT PoolChemical_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID)
CONSTRAINT PoolChemical_FK FOREIGN KEY (ChemicalName)
REFERENCES Chemical_T(ChemicalName)
);
CREATE TABLE Activity_T
(ActivityID NUMBER(11,0) NOT NULL,
ActivityDesc VARCHAR2(500),
CONSTRAINT Activity_PK PRIMARY KEY (ActivityID));
CREATE TABLE Chemical_T
(ChemicalName VARCHAR2(25) NOT NULL,
CONSTRAINT Chemical_PK PRIMARY KEY (ChemicalName));
The error isn't just in the table you highlighted, it's also in the one before, PoolVisit_T. In both, you're just missing a comma after the primary key constraint:
CREATE TABLE PoolVisit_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
LengthStay INTEGER,
CONSTRAINT Poolvisit_PK PRIMARY KEY (PoolID,ServiceDate), -- was missing comma
CONSTRAINT PoolVisit_FK FOREIGN KEY (PoolID)
REFERENCES Pool_T (PoolID)
);
CREATE TABLE PoolActivity_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
ActivityID NUMBER(11,0) NOT NULL,
CONSTRAINT PoolActivity_PK PRIMARY KEY (PoolID,ServiceDate,ActivityID), -- was missing
CONSTRAINT PoolActivity_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID),
CONSTRAINT PoolActivity_FK FOREIGN KEY (ActivityID)
REFERENCES Activity_T (ActivityID)
);
And you're missing two in the one after that:
CREATE TABLE PoolChemical_T
(PoolID NUMBER(11,0) NOT NULL,
ServiceDate DATE DEFAULT SYSDATE NOT NULL,
ChemicalName VARCHAR2(25) NOT NULL,
Quantity INTEGER,
Cost NUMBER(11,2),
CONSTRAINT PoolChemical_PK PRIMARY KEY (PoolID,ServiceDate,ChemicalName), -- was missing
CONSTRAINT PoolChemical_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID), -- was missing
CONSTRAINT PoolChemical_FK FOREIGN KEY (ChemicalName)
REFERENCES Chemical_T(ChemicalName)
);
If you use an IDE like SQL Developer it will highlight syntax problems like this as you go.
But you still have issues; your PoolActivity_T foreign key:
CONSTRAINT PoolActivity_FK FOREIGN KEY (PoolID)
REFERENCES PoolVisit_T(PoolID),
is referring only to the PoolID, but the primary key on PoolVisit_T and the primary key has two columns, so that needs to be:
CONSTRAINT PoolActivity_FK FOREIGN KEY (PoolID,ServiceDate)
REFERENCES PoolVisit_T(PoolID,ServiceDate),
And you've used the same FK name for two constraints, so the next bit is:
CONSTRAINT PoolActivity_FK2 FOREIGN KEY (ActivityID)
REFERENCES Activity_T (ActivityID)
(but pick more descriptive FK names, perhaps), and you have to create the Activity_T table before this one.
And similar issues with PoolChemical_T:
CONSTRAINT PoolChemical_PK PRIMARY KEY (PoolID,ServiceDate,ChemicalName),
CONSTRAINT PoolChemical_FK FOREIGN KEY (PoolID,ServiceDate)
REFERENCES PoolVisit_T(PoolID,ServiceDate),
CONSTRAINT PoolChemical_FK2 FOREIGN KEY (ChemicalName)
REFERENCES Chemical_T(ChemicalName)
and create Chemical_T first.
SQL Fiddle with all tables created.