Could you tell me some advise Oracle Error? - oracle

Sorry, I don't well English.
CREATE TABLE Reservation(
tno NUMBER,
sno NUMBER,
cno NUMBER,
seat_no NUMBER,
Res_Date DATE,
CONSTRAINT FK_RES_TNO FOREIGN KEY (tno) REFERENCES Theater(tno),
CONSTRAINT FK_RES_SNO FOREIGN KEY (sno) REFERENCES Screen(sno),
CONSTRAINT FK_RES_CNO FOREIGN KEY (cno) REFERENCES T_Customer(cno),
CONSTRAINT PK_RESERVATION PRIMARY KEY (tno,sno,cno)
--CONSTRAINT RES_UNIQUE UNIQUE (cno)
);
I Write Oracle Table.
But throw me Error "ORA-02270"
I don't know come to me this message.
Could you tell me some advise?

There would be multiple reasons for having this error,
You may have same primary key already, which might be disabled
Composite primary key in the table may not provide you the unique (this error many happen only when the table is ALTERED, since its new table, this logical check can be ignored)

Related

Still Getting Errors After Applying All I've Read About the Error 02270

I'm creating a couple of Tables for an assignment.
So I created a Gardener Table and an Offering Table, with all the appropriate data types and NULL statuses, as well as the Primary Key constraint for each. In the Gardener table I've included offeringID, and vice versa.
When I try to add Foreign Key constraint offeringID to the Gardener Table I get an error.
After checking online, I realized I had forgotten to make offeringID and gardenerID in each other's tables UNIQUE, hence I altered table to add uniqueness.
Tried adding Foreign Key constraint and I get the same error. I reckon I may be understanding something wrongly, but I can't seem to put my finger on it.
Create Table Gardener
(gardenerID NUMBER(10) NOT NULL,
offeringID NUMBER(10) NOT NULL,
CONSTRAINT gardener_pk PRIMARY KEY(gardenerID)
);
Create Table Offering
(offeringID NUMBER(10) NOT NULL,
gardenerID NUMBER(10) NOT NULL,
CONSTRAINT offering_pk PRIMARY KEY(offeringID)
);
Alter Table Gardener
add CONSTRAINT offering_fk FOREIGN KEY(offeringID)
REFERENCES Offering(offeringID);
Alter Table Gardener
add Unique(offeringID);
Alter Table Offering
add Unique(gardenerID);
This is the 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.
Like, I still don't get it. Isn't offeringID a Primary Key hence pointing to it from Gardener shouldn't be an issue still?
Since you're trying to add a a foreign key constraint for offering.offeringID column within the Gardener table, whereas that column has no unique/primary key key when you try to add a foreign key. i.e. operation stops at the 3rd command.
So, just exchange the order of commands as :
Alter Table Gardener
add Unique(offeringID); -- should be prior to the below command
Alter Table Gardener
add CONSTRAINT offering_fk FOREIGN KEY(offeringID)
REFERENCES Offering(offeringID);
Demo

How work PK in oracle

I got a task to review and change a PK field in a oracle table (I'm Postgresql and Sql Server guy) I check on TOAD constraints and found this:
PK_POI Primary Key Disabled Not Deferrable Immediate Not Validated 1 MI_PRINX
SYS_C0010910 Check Disabled Not Deferrable Immediate Not Validated 1 "MI_PRINX" IS NOT NULL
For what I can see two constraint where created, but by my understanding a PK is already a NOT NULL field so why need two constraint?
Also I notice both constraint said are Disable but in my test I can insert null values but not the same id twice
So not sure if Disable constraint mean what I thought should mean.
Disable means what you think it means.
Regarding NOT NULL and PRIMARY KEY, they are two constraints. A primary key implies NOT NULL, but if you drop the primary key constraint, the check constraint is still there. See here for details.
Regarding "I can insert null values...": this is because both constraints are disabled.
Regarding "...but not the same id twice": a primary key constraint and the unique index that is used to enforce it are two different things. Can you check if there is an unique index on this column?

Check constraints on two tables in Oracle

I have two tables with a one-to-one relationship (and relationship is mandatory from one side only). As follows:
create table PRS (
id number(18) not null,
common_code varchar2(10),
constraint pk_prs primary key (id));
create table RLP {
id number(18),
spec_code varchar2(20),
constraint pk_rlp primary key (id),
constraint fk_rlp_prs foreign key (id) references prs(id) on delete cascade);
So the problem is when inserting a record in RLP at least one of common_code or spec_code must have value.
Is it possible to enforce this constraint using a constraint or the only solution is having a trigger?
It seems there is no way to create a constraint on two tables, and the only solution is to create a trigger to throw an exception on desired situation.

FK on a single column referencing a column from composite PK

Not able to create /find the logic to apply FK on a column in child table referencing a column from composite PK of parent table.
create table product(prod_id number,
prod_name varchar2(20),
price number,
constraint PK12 primary key(prod_id,prod_name));
Table created.
create table purchase(prod_id number,
purchase_price number,
constraint FK12 foreign key(prod_id) references product(prod_id));
create table purchase(prod_id number,
purchase_price number,
constraint FK12 foreign key(prod_id) references product(prod_id))
ERROR at line 1:
ORA-02270: no matching unique or primary key for this column-list
Kinldy suggest how i can incorporate this logic.
Thanks.
You can't.
As the error says there's no matching primary key for that column list; you must have one. You have three options:
Remove PROD_NAME from the primary key of PRODUCT. On the face of it this seems like the logical solution, if this is not required in order to make the primary key unique.
Add PROD_NAME to the PURCHASE table.
Create a unique index on PURCHASE.PROD_ID. This seems excessive if it would be a primary key candidate anyway.
I suspect that this is not unique to Oracle. Considering you have a composite primary key in the referenced table, that implies that only one of the columns comprising the composite key is not enough to uniquely identify the record in that table. Therefore, it's impossible to reference only a single column of the primary key in a foreign key relationship that's one-to-many (e.g. one record in the referenced table can have many records in the referencing table--the one with the FK). However, if the relationship to be established is many-to-many, this may be possible.
HTH.

Oracle Foreign or Primary keys

I'm a bit confused over when I should use a primary or foreign key. I have two tables, and in both of them, some of the columns reference columns that are primary keys in other tables.
Here they are:
CREATE TABLE roles (
movie_id NUMBER(10,0) NOT NULL REFERENCES movies(movie_id),
actor_id NUMBER(10,0) NOT NULL REFERENCES actors(actor_id),
movie_description VARCHAR2(50),
salary NUMBER(10),
CONSTRAINT pk_roles PRIMARY KEY (movie_id, actor_id)
);
CREATE TABLE profits (
movie_id NUMBER(10,0) NOT NULL,
gross_profit NUMBER(9) NOT NULL,
net_profit NUMBER(9) NOT NULL,
CONSTRAINT fk_profits FOREIGN KEY (movie_id) REFERENCES movies(movie_id) ON DELETE CASCADE
);
In the first table I have made a composite primary key from teh two columns that reference columns in other tables. Those columns happen to be primary keys in their respective tables.
In the second table, I've made a foreign key again referencing a primary key in anther table. But what is best practice? Should the key in the first table also be a foreign key since it references primary keys in other tables?
Primary key constraints and unique constraints prevent duplicate rows. Duplicate rows not only waste space, they make it harder to get meaningful answers from your database.
Foreign key constraints restrict values to those that exist in another table. The target of a foreign key constraint is commonly a primary key, but it could be any column(s) that have a unique constraint.
Every table should have a primary key constraint. If the column(s) that make up the primary key also require a foreign key constraint, add the foreign key constraint as well.
Your table "roles" is fine, as far as implementing primary key constraints and foreign key constraints. But "profits" needs a primary key.
To your Question "Should the key in the first table also be a foreign key since it references primary keys in other tables?"
There is no simple answer, as it heavily depends on type of planned usage of data and database being used. If you need a simple answer, yes it is a good idea. Below is the longer version.
Pros :
It helps keep your data clean.
Based on how database is planned to be used and which database you are using, some databases, tend to optimize joins better, if Foreign keys are defined for joins upfront.
Cons :
If you plan to bulk load into your tables frequently, then FK constraints tend to slow down your loads, if that is the case, some databases allow you to define soft constraints, which are only used for query optimization purposes, but are not verified during loads.

Resources