Missing keyword in Oracle SQL - sql

What's wrong with this query?
I am getting following error:
ORA-00905: missing keyword
00905. 00000 - "missing keyword"
My SQL:
CREATE Table ORDERDET
(
ORDERID NUMBER,
CUSTID NUMBER,
PRODID NUMBER,
ORDPRIORITY VARCHAR2(15),
ORDDISCOUNT NUMBER(3,2),
ORDSHIPMODE VARCHAR2(15),
ORDDATE DATE,
ORDSHIPDATE DATE,
ORDSHIPCOST NUMBER(5,2),
ORDQTY NUMBER,
ORDSALES NUMBER(7,2),
CONSTRAINT ch_ORDPRIORITY
CHECK (ORDPRIORITY IN ('Low', 'Medium', 'High', 'Critical', 'Not Specified')),
CONSTRAINT ch_ORDSHIPMODE
CHECK (ORDSHIPMODE IN ('Regular Air','Delivery Truck','Express Air')),
CONSTRAINT pk_ORDERDET
PRIMARY KEY (ORDERID, CUSTID, PRODID),
CONSTRAINT fk_ORDERD
FOREIGN KEY (ORDERID) REFERENCES ORDERS (ORDERID) on DELETE RESTRICT,
CONSTRAINT fk_CUSTOMERORDER
FOREIGN KEY (CUSTID) REFERENCES CUSTOMERS (CUSTID) on DELETE RESTRICT,
CONSTRAINT fk_PRODUCTORDER
FOREIGN KEY (PRODID) REFERENCES PRODUCTS (PRODID) on DELETE RESTRICT
);

According to the documentation: CREATE TABLE
there is no on DELETE RESTRICT option, only CASCADE or SET NULL are allowed, please see attached syntax diagram below:
I guess you want to prevent from deletion of a row in the parent table in a case when there are rows in the child table that references this parent row - if yes, then skip ON DELETE clause completely, because this is default behaviour of the foreign key constraint.
----------
EDIT
----------
Error report - ORA-02264: name already used by an existing constraint
02264. 00000 - "name already used by an existing constraint" *Cause: The specified constraint name has to be unique. *Action: Specify a
unique constraint name for the constraint.
The error message says, that one of a constraint name you have used in the create table has already been creaded (used) and you cannot use it again.
I think you haven't showed us the whole error message, because Oracle should print this duplicate name.
Anyway, you can find which name is duplicate using this query:
select *
from user_objects
where object_name in (
'CH_ORDPRIORITY' ,
'CH_ORDSHIPMODE' ,
'PK_ORDERDET' ,
'FK_ORDERD' ,
'FK_CUSTOMERORDER' ,
'FK_PRODUCTORDER'
)
If one (or a few) of these names are already used, then use a different name, say FK_PRODUCTORDER_11 instead of FK_PRODUCTORDER

Related

Why getting error: No matching unique or primary key for this column-list, if I have the same columns?

I need to add a Foreign key to my Date_Reserved attribute in the Additional_extra Table, (Maybe because the Date type can't be unique?
I keep getting the next 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
For The next Code:
ALTER TABLE Additional_Extra
Add(
CONSTRAINT test_date
FOREIGN KEY(Date_Reserved)
REFERENCES Reservation(Date_Reserved)
);
This is the part of the task that I need to create. So it is given that Date_Reserved must be FK:
Any Suggestions on what is wrong?
I tried to add FK when creating the Table, however when I find out that just the Date_Reserved FK line is wrong I created without that, but the error is still the same.
I tried from the GUI to add a Foreign key but same error
date_reserved is part of a composite primary key consisting of many columns; there is no unique or primary key that is solely on the date_reserved column.
Your constraint:
ALTER TABLE Additional_Extra
Add(
CONSTRAINT test_date
FOREIGN KEY(Date_Reserved)
REFERENCES Reservation(Date_Reserved)
);
Is trying to refer to a unique constraint that is solely on the date_reserved column and that does not exist so the SQL engine (correctly) raises the exception that such a constraint does not exist.
What you need to do is refer to the entire composite key:
ALTER TABLE Additional_Extra
Add(
CONSTRAINT test_date
FOREIGN KEY(Booking_Number, Room_Number, Date_Reserved)
REFERENCES Reservation(Booking_Number, Room_Number, Date_Reserved)
);
fiddle

Oracle 12c - Column check constraint cannot reference other column

I have to modify my table and I try do it as following:
ALTER TABLE projects
MODIFY (
id_proj NUMBER(4) CONSTRAINT pk_proj PRIMARY KEY,
desc VARCHAR2(40) NOT NULL CONSTRAINT uk_proj UNIQUE,
end_date CONSTRAINT chk_date CHECK(end_date > start_date),
fund CHECK (fund > 0)
);
And when I try execute this query I get an error:
ORA-02438: Column check constraint cannot reference other columns
02438. 00000 - "Column check constraint cannot reference other columns"
*Cause: attempted to define a column check constraint that references
another column.
*Action: define it as a table check constraint.
Additionally I want the column to accept values greater than 0 or NULL values.
This is just a syntax quirk. Do this:
ALTER TABLE projects
MODIFY (
id_proj NUMBER(4) CONSTRAINT pk_proj PRIMARY KEY,
desc VARCHAR2(40) NOT NULL CONSTRAINT uk_proj UNIQUE,
end_date DATE, -- I'm guessing this is the type
CONSTRAINT chk_date CHECK (end_date > start_date),
fund CHECK (fund > 0)
);
As the error suggests, you cannot have an in-line check constraint that references other columns. You can still have a check constraint; it just needs to be declared by itself as a constraint.
While we are commenting:
desc is a really bad name for a column, because it is a SQL key word. Use descr or spell the whole thing out, description.
CONSTRAINT uk_proj UNIQUE seems verbose to me. It can be replaced with a simple UNIQUE. Admittedly, this doesn't allow you to name unique constraints. Is that important for your database?
The same is true of the primary key constraint. (If you have a reason to name them, then leave it as it is; I just don't often find such a reason.)

I keep getting errors on my code SQL Oracle

So I've been working on this code for a few days now and I can't figure out why I'm getting errors. These are the errors I'm getting:
SQL Error: ORA-00904: "PATIENTID_FK": invalid identifier
00904. 00000 - "%s: invalid identifier"
and
SQL Error: ORA-02264: name already used by an existing constraint
02264. 00000 - "name already used by an existing constraint"
I've included the code that I wrote. The "..." symbolizes not important information from the table I was creating, such as "DoctorFirstName". Can someone help me figure out where I'm messing up? Thank you!
DROP TABLE HealthRecord;
DROP TABLE Patient;
DROP TABLE Insurance;
DROP TABLE Doctor;
DROP TABLE Hospital;
DROP TABLE Prescription;
CREATE TABLE Insurance (
InsuranceID number,
...
CONSTRAINT InsuranceID_pk
PRIMARY KEY (InsuranceID));
CREATE TABLE Prescription (
PrescriptionID number,
...
CONSTRAINT PrescriptionID_pk
PRIMARY KEY(PrescriptionID));
CREATE TABLE Hospital (
HospitalID number,
...
CONSTRAINT HospitalID_pk
PRIMARY KEY(HospitalID));
CREATE TABLE Doctor (
DoctorID number,
...
HospitalID number,
CONSTRAINT DoctorID_pk
PRIMARY KEY(DoctorID),
CONSTRAINT HospitalID_fk
FOREIGN KEY (HospitalID)
REFERENCES Hospital (HospitalID));
CREATE TABLE Patient (
PatientID number,
...
InsuranceID number,
CONSTRAINT PatientID_pk
PRIMARY KEY (PatientID),
CONSTRAINT InsuranceID_fk
FOREIGN KEY (InsuranceID)
REFERENCES Insurance(InsuranceID));
CREATE TABLE HealthRecord(
RecordID number,
...
DoctorID number,
PrescriptionID number,
PatientID number,
CONSTRAINT RecordID_pk
PRIMARY KEY(RecordID),
CONSTRAINT DoctorID_fk
FOREIGN KEY (DoctorID)
REFERENCES Doctor (DoctorID),
CONSTRAINT PrescriptionID_fk
FOREIGN KEY (PrescriptionID)
REFERENCES Prescription (PrescriptionID),
CONSTRAINT PatientID_fk
FOREIGN KEY (PatientID_fk)
REFERENCES Patient(PatientID));
Just dropping the table doesn't remove the constraints. So you constraints still exist. You should drop the constraints and indexes. Check this link out:
https://www.1keydata.com/sql/alter-table-drop-constraint.html
Reference the link below for dropping tables and the constraint in one statement (CASCADE CONSTRAINTS)
http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9003.htm

What is the notation of recursive relation in oracle 11g?

I cannot find the solution on the web. I am wondering how I write the recursive relation in oracle. At the moment this is what I got:
create table medewerkers
(medewerker_ID varchar(15) primary key,
naam varchar(50) not null,
adres varchar(50) not null,
telefoon_nummer varchar(10) not null,
salaris number(4) not null,
functie varchar(50) not null,
manager varchar(15) constraint FK_Manager references medewerkers (medewerker_ID) on delete cascade,
werknemer_winkel_nummer number(15) constraint FK_W_winkel references winkel (winkel_nummer) on delete cascade,
constraint check_salaris check (salaris < 3000)
);
at the moment I created a manager column as FK for this recursive relation. I did not create an extra table because I am told that if they are 1-to-many with employee then I could place the FK within the table.
Now I am inserting a value like this one:
insert into MEDEWERKERS (medewerker_id, naam, adres, telefoon_nummer, salaris, functie, werknemer_winkel_nummer, manager)
values(11159112, 'Joost', 'Eindhoven Langloopstraat 1', 0678765478, 1500, 'baliemedewerker', 10, 'nee');
Oracle db gives an error back:
SQL Error: ORA-02291: integrity constraint (MAXIME.FK_MANAGER) violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
*Cause: A foreign key value has no matching primary key value.
*Action: Delete the foreign key or add a matching primary key.
How am I else supposed to get values into the manager column?
I hope my question is not too vague.
You need to make sure the manager is there before you add their underlings.
The CEO/Manager/Owner can be added with a NULL manager:
INSERT INTO MEDEWERKERS (medewerker_id, naam, adres, telefoon_nummer, salaris, functie, werknemer_winkel_nummer, manager)
VALUES ( 'nee', 'The Boss Man', 'Home Office', '0000000001', 9999, 'Owner', 10, NULL );
The employees can then be added with the correct foreign key references (as per the OP).
Also - if you are entering telephone numbers with a leading 0 then you probably want to wrap the data in quotes '' otherwise you may find that the conversion from number to varchar will lose it.
[As an aside: do you really want ON DELETE CASCADE on the foreign keys? If you delete a manager then all their employees will be deleted as well.]
Your question was perfectly formed.
Have you considered temporarily disabling the FK_Manager constraint so you can add the top-level of management? Then enable the constraint while you add the next level down of employees?
Just a thought.

SQL Incorrect syntax for definition of the 'TABLE' constraint

I keep getting this error everytime i try to exceute the query . I checked for any syntax errors multiple time, but i cant find any.
Msg 142, Level 15, State 2, Line 0
Incorrect syntax for definition of the 'TABLE' constraint.
CREATE TABLE "hold" (
timePutOnHold TIME(7),
customer_id VARCHAR(13),REFERENCES "Customer",
isbn VARCHAR(13) REFERENCES "Item_Details",
PRIMARY KEY (customer_id, isbn, timePutOnHold)
)
SOLVED! I had to remove a comma after customer_id VARCHAR(13)
customer_id VARCHAR(13),REFERENCES "Customer",
The comma between the data type and the REFERENCES keyword might be causing the syntax error.
In general, it is a good practice to give meaningful names to constraints so that they can be dropped easily later on.
CREATE TABLE hold
(
timeputonhold TIME(7),
customer_id VARCHAR(13),
isbn VARCHAR(13),
CONSTRAINT pk_hold PRIMARY KEY (customer_id, isbn, timeputonhold),
CONSTRAINT fk_hold_customer FOREIGN KEY (customer_id) REFERENCES customer(customer_id),
CONSTRAINT fk_hold_isbn FOREIGN KEY (isbn) REFERENCES item_details(isbn)
)
Please note that DEFAULT constraints are different with regards to syntax in a CREATE TABLE statement.
Without a comma separator, the CONSTRAINT references the preceding column.
-- correct syntax for adding a default constraint to a CREATE TABLE statement
IsAvailable BIT NOT NULL CONSTRAINT d_IsAvailable DEFAULT(1)
However, using a comma separate and the keys words DEFAULT FOR will produce the same error message.
-- incorrect syntax for adding a default constraint to a CREATE TABLE statement
IsAvailable BIT NOT NULL
, CONSTRAINT d_IsAvailable DEFAULT(1) FOR IsAvailable
This nuance can be tricky since CONSTRAINT constraint_name DEFAULT(constraint) FOR column_name is correct for ALTER TABLE statements.
Further Reading:
Declaring a default constraint when creating a table
Specify Default Values for Columns