ORACLE - Missing keyword when creating table help - oracle

I am trying to complete a project but i keep getting "missing keyword" error when i try to create a table:
CREATE TABLE SKILLS (
SkillsID CHAR(4) NOT NULL,
ConsultantID CHAR(4) NOT NULL,
ExpertiseID CHAR(4) NOT NULL,
MonthsExperience INT NOT NULL,
CONSTRAINT SkillsPK PRIMARY KEY (SkillsID),
CONSTRAINT SkillsConsultFK FOREIGN KEY(ConsultantID)
REFERENCES CONSULTANTS(ConsultantID)
ON UPDATE NO ACTION
ON DELETE CASCADE,
CONSTRAINT SkillsExpertiseFK FOREIGN KEY(ExpertiseID)
REFERENCES EXPERTISE(ExpertiseID)
ON UPDATE NO ACTION
ON DELETE CASCADE
)
Please! all help is greatly appreciated

Remove the ON UPDATE NO ACTION. Since there is no ON UPDATE CASCADE in Oracle so there's no need to specify NO ACTION.

Related

want to link crew Assignment to above tables given I'm getting an error How to solve this error?

CREATE TABLE Route(
RouteNo VARCHAR(10),
Origin VARCHAR(30),
Destination VARCHAR(30),
DepartureTime VARCHAR(15),
SerialNo VARCHAR(5),
ArrivalTime VARCHAR(15),
PRIMARY KEY(RouteNo) );
CREATE TABLE Employee(
EmployeeID VARCHAR(5) NOT NULL,
Name VARCHAR(30),
Phone NUMBER,
JobTitle VARCHAR(30),
PRIMARY KEY(EmployeeID) );
CREATE TABLE Flight(
SerialNo VARCHAR(5),
RouteNo VARCHAR(5),
FlightDate DATE,
ActualTD VARCHAR(10),
ActualTA VARCHAR(10),
PRIMARY KEY(SerialNo, RouteNo, FlightDate),
FOREIGN KEY(RouteNo) REFERENCES Route(RouteNo),
FOREIGN KEY(SerialNo) REFERENCES Airplane(SerialNo) ); -- does Airplane table exists ?
CREATE TABLE CrewAssigment(
EmployeeID VARCHAR(5),
RouteNo VARCHAR(5),
FlightDate DATE,
Role VARCHAR(45),
Hours INT,
PRIMARY KEY(EmployeeID, RouteNo, FlightDate),
FOREIGN KEY(EmployeeID) REFERENCES Employee(EmployeeID),
FOREIGN KEY(RouteNo) REFERENCES Route(RouteNo),
FOREIGN KEY(FlightDate) REFERENCES Flight(FlightDate) );
Select * from CrewAssignment
This is my code where I'm getting an error in the CrewAssignment table and above are the tables where the foreign key is referenced from.
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
A few objections.
This is clearly an Oracle question, not MySQL. How do I know? ORA-02270 is an Oracle database error code; pay attention to tags you use.
You should use VARCHAR2 datatype instead of VARCHAR. Why? Oracle recommends so.
create table flight fails first as it references the airplane table, and it doesn't exist yet (at least, not in code you posted)
error you're complaining about is due to create table crewassignment. One of its foreign keys references the flight table:
FOREIGN KEY(flightdate) REFERENCES flight(flightdate)
but flight's primary key is composite, made up of 3 columns:
PRIMARY KEY(serialno,
routeno,
flightdate)
which means that you can't create that foreign key.
So, what to do? No idea, I don't know rules responsible for such a data model. Either modify primary key of the flight table, or modify foreign key constraint of the crewassingment table.
Perhaps you could add a new column to flight table (made up of a sequence (or identity column, if your database version supports it) and then let the crewassignment table reference that primary key. Columns you currently use as a primary key (serialno, routeno, flightdate) would then switch to unique key.

Name already existing error in SQL Developer

I got some help last night on this code but now I am getting a different error. My professor is STILL not answering me so I am coming to you guys. Here is the code:
--Create Volunteer Supervisor
CREATE TABLE Volunteer_Supervisor
(
PH_Person_ID Number(10) NOT NULL,
EM_Person_ID Number(10) NOT NULL,
VO_Person_ID Number(10) NOT NULL,
End_Date Date NOT NULL,
Begin_Date Date NOT NULL,
Hours_Worked Number(4) NULL,
PWork_Unit_ID Number(4) NULL,
PRIMARY KEY (PWork_Unit_ID),
CONSTRAINT CCPHPersonID_FK FOREIGN KEY (PH_Person_ID) References Physician (PH_Person_ID),
CONSTRAINT CCEMPersonID_FK FOREIGN KEY (EM_Person_ID) References Employee (EM_Person_ID),
CONSTRAINT CCVOPersonID_FK FOREIGN KEY (VO_Person_ID) References Volunteer (VO_Person_ID),
CONSTRAINT CCPWorkUnitID_PK FOREIGN KEY (PWork_Unit_ID) References Work_Unit (PWork_Unit_ID)
);
Now I have changed the names but still getting this error:
Error report -
ORA-00955: name is already used by an existing object
00000 - "name is already used by an existing object"
*Cause:
*Action:
What am I missing?
If you are sure that no such table Volunteer_Supervisor exists, You may try below code -
CREATE TABLE Volunteer_Supervisor
(
PH_Person_ID Number(10) NOT NULL,
EM_Person_ID Number(10) NOT NULL,
VO_Person_ID Number(10) NOT NULL,
End_Date Date NOT NULL,
Begin_Date Date NOT NULL,
Hours_Worked Number(4) NULL,
PWork_Unit_ID Number(4) NULL,
PRIMARY KEY (PWork_Unit_ID),
FOREIGN KEY (PH_Person_ID) References Physician (PH_Person_ID),
FOREIGN KEY (EM_Person_ID) References Employee (EM_Person_ID),
FOREIGN KEY (VO_Person_ID) References Volunteer (VO_Person_ID),
FOREIGN KEY (PWork_Unit_ID) References Work_Unit (PWork_Unit_ID)
);

On DELETE CASCADE not working in snowflake

I have two tables:
CREATE TABLE category(
id INTEGER NOT NULL DEFAULT CATEGORY_SEQUENCE.NEXTVAL,
name VARCHAR(50),
PRIMARY KEY(id)
);
CREATE TABLE product(
id INTEGER NOT NULL DEFAULT PRODUCT_SEQUENCE.NEXTVAL,
name VARCHAR(50),
id_category INTEGER REFERENCES category(id) ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY(id)
);
When I DELETE FROM category, the products do not get deleted.
What am I doing wrong?
Thing is Snowflake doesn't really service constraints other than not null. It will have them (as in store somewhere) but not really enforce - which means your cascade won't work.
reference:
https://docs.snowflake.com/en/sql-reference/constraints-overview.html

Oracle Create TABLE

I am trying to create a table with foreign key using oracle. My syntax is as follows
CREATE TABLE product (
product_id INT(7) NOT NULL,
supplier_id INT(7) NOT NULL,
product_name VARCHAR2(30),
product_price DOUBLE(4),
product_category VARCHAR2(30),
product_brand VARCHAR2(20),
product_expire DATE,
PRIMARY KEY (product_id),
FOREIGN KEY (supplier_id)
)
I got a error, saying
Error at Command Line:2 Column:14 Error report: SQL Error: ORA-00907:
missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Please help!
Your foreign key should refference another column on another table.
Here is the documentation you need to fix your issue (how to write the query with the correct syntax for foreign key)
Also, change your data type for column product_price from DOULBE(4) to NUMBER(12,4).
You should not use limit for int type...oracle will take default length for int type .
Instead of int you can use Number type to make it run. And DOUBLE PRECISION is a data type in oracle but Double is not there. Also , syntax for foreign key is wrong.
so this query will work for sure :
CREATE TABLE product(
product_id number(7) NOT NULL,
supplier_id number(7) NOT NULL,
product_name VARCHAR2(30),
product_price DOUBLE PRECISION,
product_category VARCHAR2(30),
product_brand VARCHAR2(20),
product_expire DATE,
PRIMARY KEY (product_id),
FOREIGN KEY (supplier_id)
REFERENCES parent_table (supplier_id)
);
You create like foreign key references parent table that is the proper syntax for creating the foreign key
CREATE TABLE product(
product_id number(7) NOT NULL,
supplier_id number(7) NOT NULL,
product_name VARCHAR(30),
product_price DOUBLE PRECISION,
product_category VARCHAR(30),
product_brand VARCHAR(20),
product_expire DATE,
PRIMARY KEY (product_id),
FOREIGN KEY (supplier_id)
REFERENCES parent_table (supplier_id)
);

Drop a column from composite primarykey

i want to drop a column from composite primary key(not to delete from table). if my table is like this.
create table "scott"."xyz"(
"column1" not null,
"column2" not null,
"column3" not null,
"column4" not null,
"column5" not null,
"column6",
CONSTRAINT PRIMARY KEY ("column1","column2","column3","column4")
);
i want to alter this primary key to first three column without dropping it. beacuse i don't know CONSTRAINT name.
You don't need the constraint name:
ALTER TABLE "scott"."xyz" DROP PRIMARY KEY;
ALTER TABLE "scott"."xyz" ADD PRIMARY KEY ("column1","column2","column3");
But it is probably a good idea to give your PK a name in the future:
ALTER TABLE "scott"."xyz"
ADD CONSTRAINT pk_xyz PRIMARY KEY ("column1","column2","column3");
And I would not recommend to use quoted identifiers. You will have problems with various tools in the long run.

Resources