Using a simples "CREATE TABLE" on SQL does not seem to work, more info - oracle

"CREATE TABLE bilhete(
id_bilhete int,
estado_bilhete varchar2(12),
id_viagem int,
id_cliente int,
PRIMARY KEY (id_bilhete),
FOREIGN KEY (id_viagem)
FOREIGN KEY (id_cliente)
REFERENCES viagem (id_viagem), cliente (id_cliente));
DESCRIBE bilhete;"
Error report -
ORA-00905: missing keyword
00905. 00000 - "missing keyword"
*Cause:
*Action:
ERROR:
ORA-04043: object bilhete does not exist

The syntax is:
[CONSTRAINT constraint_name] FOREIGN KEY (column_names) REFERENCES table_name ( referenced_column_names)`
So you need a REFERENCES clause for each FOREIGN KEY:
CREATE TABLE bilhete(
id_bilhete int,
estado_bilhete varchar2(12),
id_viagem int,
id_cliente int,
PRIMARY KEY (id_bilhete),
FOREIGN KEY (id_viagem) REFERENCES viagem (id_viagem),
FOREIGN KEY (id_cliente) REFERENCES cliente (id_cliente)
);
db<>fiddle

Related

ORA-02270 although there's primary key

Given this schema:
create table Review2 (
RID varchar(3),
YID varchar(3),
BID varchar(3),
-- other columns
primary key(RID,YID,BID)
);
create table textualContent2(
TID varchar(3),
RID varchar(3) NOT NULL,
-- other columns
primary key (TID),
foreign key (RID) references Review2 on delete cascade
);
I get this error:
ORA-02256: number of referencing columns must match referenced columns
Why is that and how do I fix it?
Review2 has PK:
primary key(RID,YID,BID),
but your FK definition
foreign key (RID) references Review2
uses the shorthand version, which implies referenced columns as being the PK columns (RID,YID,BID), so it's the same as coding:
foreign key (RID) references Review2(RID,YID,BID)
As suggested by the error message:
number of referencing columns must match referenced columns
the number of columns referenced (RID,YID,BID) disagrees those referencing (RID).
To fix the problem, either change your FK to:
foreign key (RID) references Review2(RID)
and define a unique constraint on RID in Review2, or add columns YID and BID to textualContent2 and add them to the FK:
foreign key (RID,YID,BID) references Review2(RID,YID,BID)

Tried to have recursive relation

I use oracle and I try to have a recursive relation
CREATE TABLE "EVENT"
(
"EVENT_ID" NUMBER(18) NOT NULL, //primary key
"NAME" VARCHAR(20) NULL,
"RELATED_EVENT_ID" NUMBER(18) NULL //foreign key
);
Event 1 parent is Event 2....
When I try to create this table, I get this error.
ALTER TABLE "EVENT"
ADD CONSTRAINT "FK_RELATED_EVENT_ID"
FOREIGN KEY ("RELATED_EVENT_ID") REFERENCES "EVENT" ("RELATED_EVENT_ID")
Error report -
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
You have two problems:
There is no primary key constraint on this table.
The foreign key constraint you defined has RELATED_EVENT_ID referencing RELATED_EVENT_ID. I suspect that was just a typo.
Change your table definition to:
CREATE TABLE EVENT
(EVENT_ID NUMBER
NOT NULL
CONSTRAINT PK_EVENT
PRIMARY KEY
USING INDEX,
NAME VARCHAR2(20),
RELATED_EVENT_ID NUMBER);
Then add the foreign key constraint as
ALTER TABLE EVENT
ADD CONSTRAINT EVENT_FK1
FOREIGN KEY (RELATED_EVENT_ID) REFERENCES EVENT(EVENT_ID);
db<>fiddle here
EDIT
Note that the better way to handle this is to use a junction table, such as:
CREATE TABLE EVENT_EVENT
(EVENT_ID1 NUMBER
CONSTRAINT EVENT_EVENT_FK1
REFERENCES EVENT(EVENT_ID),
EVENT_ID2 NUMBER
CONSTRAINT EVENT_EVENT_FK2
REFERENCES EVENT(EVENT_ID),
CONSTRAINT PK_EVENT_EVENT
PRIMARY KEY (EVENT_ID1, EVENT_ID2)
USING INDEX);
Then you can drop the RELATED_EVENT_ID column from EVENT as you no longer need it.
According to oracle document :
Foreign key specifies that the values in the column must correspond to values in a
referenced primary key or unique key column or that they are NULL.
In your case, create primary key on column (EVENT_ID) and use it in reference clause as following:
ALTER TABLE "EVENT"
ADD CONSTRAINT "FK_RELATED_EVENT_ID"
FOREIGN KEY ("RELATED_EVENT_ID")
REFERENCES "EVENT" ("EVENT_ID") -- this
Now, use EVENT2's EVENT_ID as RELATED_EVENT_ID in EVENT1 record to make EVENT2 as parent of EVENT1.
Cheers!!

Oracle SQL Check Error

Why will my command not work when i use the check constraint? The table can be added when the check is not included.
create table Car (
CarID number(32,0) NOT NULL ,
PurchaseDate date,
Colour varchar2(10) NOT NULL CHECK (Colour IN ("Red", "Blue", "Green")),
CONSTRAINT CAR_PK PRIMARY KEY (CarID),
FOREIGN KEY (CarID) REFERENCES Vehicle(ID)
);
Error report -
SQL 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 constriant.
create table Car (
CarID number(32,0) NOT NULL ,
PurchaseDate date,
Colour varchar2(10) NOT NULL CHECK (Colour IN ('Red', 'Blue', 'Green')),
CONSTRAINT CAR_PK PRIMARY KEY (CarID),
FOREIGN KEY (CarID) REFERENCES Vehicle(ID)
);
No double quotes are allowed in oracle SQL

ORA-02256: referencing foreign key

I'm a SQL newbie and having some troubles with referencing a foreign KEY
My Event Table:
create table Event
(
Bookid number(5),
edate date,
FacID int,
GuestID int,
CONSTRAINT pk1edate PRIMARY KEY (edate,Bookid),
CONSTRAINT fk1Bookid FOREIGN KEY (Bookid) references BOOK (Bookid),
CONSTRAINT fk2FacID FOREIGN KEY (FacID) references CUSTOMER (SponsorID),
CONSTRAINT fk3GuestID FOREIGN KEY (GuestID) references CUSTOMER (SponsorID)
);
and my event_register table
create table EVENT_REGISTER
(
CID number(6),
Bookid number(5),
edate date,
CONSTRAINT pk1Edate PRIMARY KEY (edate,Bookid),
CONSTRAINT fk2Bookid FOREIGN KEY (Bookid) references BOOK,
CONSTRAINT fk3edate FOREIGN KEY (edate) references Event (edate,Bookid)
);
I get this error when I try to run the Event_Register:
ERROR at line 9:
ORA-02256: number of referencing columns must match referenced columns
You need to specify all the columns in both parent and child tables while referring.
create table EVENT_REGISTER
(
CID number(6),
Bookid number(5),
edate date,
CONSTRAINT pk1Edate PRIMARY KEY (edate,Bookid),
CONSTRAINT fk2Bookid FOREIGN KEY (Bookid) references BOOK(Bookid),
CONSTRAINT fk3edate FOREIGN KEY (edate,Bookid) references Event (edate,Bookid)
);

mysql 1064 error on execution

I´ve got this table:
mysql> CREATE TABLE favorite food
-> (person_id SMALLINT UNSIGNED,
->food VARCHAR(20),
->CONSTRAINT pk_favorite_food PRIMARY KEY (person_id, food),
->CONSTRAINT fk_fav_food_person_id FOREIGN KEY (person_id)
->REFERENCES person (person_id)
->);
After execution i get the error 1064. Anybod any ideas what could be wrong?
MySQL Server 6.0
Table name should not have a whitespace in it. Make it something like favorite_food
By searching some possible response on Google, I found this thread on SO... The original table (which is the same as your, but with a nicer formatting) is:
CREATE TABLE favorite_food(
person_id SMALLINT UNSIGNED,
food VARCHAR(20),
CONSTRAINT pk_favorite_food PRIMARY KEY(person_id,food),
CONSTRAINT fk_fav_food_person_id FOREIGN KEY (person_id) REFERENCES person(person_id)
);
You probably don't have the person table, so the foreign key can't be created. (see the last line)
You have remove the _ in the table name (favorite food instead of favorite_food) which is not allowed.

Resources