I'm trying to create a primary key from 2 columns, but it doesn't work well - sql

I'm learning Oracle by myself.
Here's my code:
create table Schedule
(
Schedule_SN number(10) primary key,
ScreeningDate date not null,
Price number(6) not null
);
create table Seat
(
Schedule_SN number(10) REFERENCES Schedule(Schedule_SN),
Seat_SN varchar2(4) not null
);
create table Reservation
(
Reservation_SN number(15) primary key,
DCtype number(2) not null,
DCamount number(7),
PaymentMethod number(1) not null,
TotalPrice number(7) not null,
ReservationDate date not null
);
create table Reservation_details ** I need help here **
(
Reservation_SN number(15) REFERENCES Reservation(Reservation_SN),
Schedule_SN number(10) REFERENCES Schedule(Schedule_SN),
Seat_SN varchar2(10) REFERENCES Seat(Seat_SN),
CONSTRAINT Reservation_detailesPK primary key (Reservation_SN, Schedule_SN)
);
Error messages:
Errors - 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
How can I make my 2 columns (Reservation_SN, Schedule_SN) into a primary key?

The problem is with seat_sn. You want child column in reservation_details to reference parent column in seat, but the parent column is not a primary or unique key. Actually, seat has no primary key; just make seat_sn the primay key of this table (if this fits your use case), and the rest should run fine:
create table seat (
schedule_sn nmber(10) references schedule(schedule_sn),
seat_sn varchar3(4) primary key
)
Demo on DB Fiddle

Related

ORA-02270 Foreign key, Can't find fault

I am having a problem connecting my Sample_Measure_FK in the Sample table to the Measurement Table.
There sections of code are:
create table Sample
(
Site_ID varchar2(5) not null,
Recorded_On timestamp not null,
Scientist_Num varchar2(7) not null,
Comments varchar2(4000), -- or CLOB
constraint Sample_PK primary key (Site_ID, Recorded_On),
constraint Sample_Site_FK foreign key (Site_ID) references Site,
constraint Sample_Scientist_FK foreign key (Scientist_Num) references Scientist(Scientist_Num),
-- the following is the problem:
constraint Sample_Measure_FK foreign key (Recorded_On) references Measurement(Recorded_On)
);
create table Measurement
(
Site_ID varchar2(5) not null,
Recorded_On timestamp not null,
Name varchar2(50),
Value numeric(10,8),
Outlier_Indicator varchar2(50),
constraint Measurement_PK primary key(Site_ID, Recorded_On),
);
The error message I receive is:
Error starting at line : 65 in command -
create table Sample
(
Site_ID varchar2(5) not null,
Recorded_On timestamp not null,
Scientist_Num varchar2(7) not null,
Comments varchar2(4000), -- or CLOB
constraint Sample_PK primary key (Site_ID, Recorded_On),
constraint Sample_Site_FK foreign key (Site_ID) references Site,
constraint Sample_Scientist_FK foreign key (Scientist_Num) references Scientist(Scientist_Num),
constraint Sample_Measure_FK foreign key (Recorded_On) references Measurement(Recorded_On)
)
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
The Other foreign keys work, but the one in bold does not.
Perhaps it is the naming, but I would expect a single sample to have multiple measurements, suggesting that the foreign key relationship is on the wrong table:
create table Sample (
Site_ID varchar2(5) not null,
Recorded_On timestamp not null,
Scientist_Num varchar2(7) not null,
Comments varchar2(4000), -- or CLOB
constraint Sample_PK primary key (Site_ID, Recorded_On)
constraint Sample_Site_FK foreign key (Site_ID) references Site,
constraint Sample_Scientist_FK foreign key (Scientist_Num) references Scientist(Scientist_Num)
);
create table Measurement (
Site_ID varchar2(5) not null,
Recorded_On timestamp not null,
Name varchar2(50),
Value numeric(10, 8),
Outlier_Indicator varchar2(50),
constraint Measurement_Sample_FK foreign key (Site_ID, Recorded_On) references Sample(Site_ID, Recorded_On),
constraint Measurement_PK primary key (Site_ID, Recorded_On, Name)
);
This does work.
The Measurement table needs to be created before the Sample table
The foreign key is validated, so the referenced table must already exist.
A foreign key needs to point to a primary key of another table
constraint Sample_Measure_FK foreign key (Recorded_On) references Measurement(Recorded_On)
Well, Recorded_on is not the primary key on Measurement. Alternatively it could be a unique constraint also, but it's not either.
You usually point foreign keys to primary keys of other tables.

foreign keys: number of columns not equal to referenced columns

I'm getting an error from oracle that says "number of referencing columns must match referenced columns."
I want my column recorded_on in the table measurement to reference recorded_on in the table called sample
The column Recorded on in the Sample table must be part of a composite key together with Scientist_Num
The error is coming from
FOREIGN KEY (Recorded_On) REFERENCES Sample(Scientist, Recorded_On, Site_ID)
CREATE TABLE Sample (
Scientist_Num varchar2(5) not null,
Recorded_On date not null,
Site_ID varchar2(4) not null,
Comments clob,
Primary key (Scientist_Num, Recorded_On),
FOREIGN KEY (Scientist_Num) REFERENCES Scientist(Scientist_Num),
FOREIGN KEY (Site_ID) REFERENCES Site(Site_ID)
);
CREATE TABLE Measurement (
Site_ID varchar2(4) not null,
Recorded_On date not null,
Name varchar2(10) not null,
Value varchar2(10),
Outlier_Indicator varchar2(10),
Primary key (Site_ID, Recorded_On, Name),
FOREIGN KEY (Site_ID) REFERENCES Sample(Site_ID),
FOREIGN KEY (Recorded_On) REFERENCES Sample(Scientist, Recorded_On, Site_ID)
);
The Scientist_Num and Recorded_On columns must be in a composite key together.
The answer to my problem and an explanation of what went wrong would be greatly appreciated.
You can create virtual column in sample table:
Recorded_virtual varchar2(5) [GENERATED ALWAYS] AS
(Scientist||Recorded_On||Site_ID) [VIRTUAL]
And create reference to this column:
CONSTRAINT fk_column
FOREIGN KEY (Recorded_On)
REFERENCES Sample(Recorded_virtual )
Foreign key references need to match the primary keys in number and type. So I think you intend:
CREATE TABLE Measurement (
Site_ID varchar2(4) not null,
Scientist_Num varchar2(5) not null,
----^ added for foreign key reference
Recorded_On date not null,
Name varchar2(10) not null,
Value varchar2(10),
Outlier_Indicator varchar2(10),
Primary key (Site_ID, Recorded_On, Name),
FOREIGN KEY (Site_ID) REFERENCES Site(Site_ID),
-------------------------------------^ Presumably you intend the site table
FOREIGN KEY (Scientist_Num, Recorded_On) REFERENCES
Sample(Scientist_Num, Recorded_On)
-----------------^ two columns, both need to already be defined
);
I suspect there are other issues with your data model, but this should fix the syntax error. If you want further help, then ask another question.

"no matching unique or primary key for this column-list". The primary key does exist though

So i'm practicing some sql coding for a test and I can't get a foreign key to reference a primary key.
Here's the table that doesn't work:
CREATE TABLE ASSIGNMENT(
ASSIGN_ID NUMBER(2) NOT NULL,
START_DATE DATE,
END_DATE DATE,
BUDGET NUMBER (10,2),
MANAGER_ID NUMBER(2),
PRIMARY KEY (ASSIGN_ID,MANAGER_ID),
FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEE(EMP_ID)
);
Here's the table it is referencing:
CREATE TABLE EMPLOYEE(
EMP_ID NUMBER(2) NOT NULL,
NAME VARCHAR(40),
OFFICE VARCHAR(20),
EXPERT_ID NUMBER(2),
PRIMARY KEY (EMP_ID,EXPERT_ID),
FOREIGN KEY (EXPERT_ID) REFERENCES EXPERTISE(EXPERT_ID)
);
Whenever I try to run the script it always comes back with:
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
I've looked around but can't seem to find the problem. Any help would be appreciated.
Here's the full code (haven't tested the last table yet):
CREATE TABLE EXPERTISE(
EXPERT_ID NUMBER(2) NOT NULL,
DESCRIPTION VARCHAR(50),
HOURLY_RATE NUMBER(3,2),
CHARGE_RATE NUMBER(3,2),
PRIMARY KEY(EXPERT_ID)
);
CREATE TABLE EMPLOYEE(
EMP_ID NUMBER(2) NOT NULL,
NAME VARCHAR(40),
OFFICE VARCHAR(20),
EXPERT_ID NUMBER(2),
PRIMARY KEY (EMP_ID,EXPERT_ID),
FOREIGN KEY (EXPERT_ID) REFERENCES EXPERTISE(EXPERT_ID)
);
CREATE TABLE ASSIGNMENT(
ASSIGN_ID NUMBER(2) NOT NULL,
START_DATE DATE,
END_DATE DATE,
BUDGET NUMBER (10,2),
MANAGER_ID NUMBER(2),
PRIMARY KEY (ASSIGN_ID,MANAGER_ID),
FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEE(EMP_ID)
);
CREATE TABLE ALLOCATION(
EMP_ID NUMBER(3) NOT NULL,
ASSIGN_ID NUMBER(3) NOT NULL,
DAYS_WORKED_ON DATE,
HOURS_WORKED_ON DATE,
PRIMARY KEY(EMP_ID,ASSIGN_ID),
FOREIGN KEY(EMP_ID) REFERENCES EMPLOYEE(EMP_ID),
FOREIGN KEY(ASSIGN_ID) REFERENCES ASSIGNMENT(ASSIGN_ID)
);
I'm using Oracle SQL Developer to make it
*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.
The problem is that EMP_ID (by itself) isn't a primary or unique key of table Employees, instead, you have a compound primary key (EMP_ID, EXPERT_ID).
To fix the issue either make EMP_ID the primary key of the Employees table (which seems intuitive as each employee ought to have a unique id) or add a separate unique constraint on EMP_ID.
As pointed out in the comments, if you make EMP_ID the primary key, then (EMP_ID, EXPERT_ID) will also be unique by extension.
As the error suggest, the column you've referenced a foreign key doesn't match a unique constraint/pk on the parent table. Specifically for the primary key EMP_ID, EXPERT_ID you reference only EMP_ID.

Oracle SQL Database error: "no matching unique or primary key for this column-list"

I'm trying to set up a database in Oracle sql developer, I've got these 3 tables.
I need the table "GuyAddress" to have 3 primary keys, which are all foreign keys as well. This is where I'm running into an error which I can't get my head around.
CREATE TABLE Guy
(
id NUMBER(10) PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE Address
(
zipcode VARCHAR(6),
"number" NUMBER(10),
CONSTRAINT PK_Address PRIMARY KEY(zipcode, "number")
);
CREATE TABLE GuyAddress
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address_zipcode FOREIGN KEY(Address_zipcode) REFERENCES Address(zipcode),
CONSTRAINT FK_GuyAddress_Address_number FOREIGN KEY(Address_number) REFERENCES Address("number"),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
);
This is the error, hopefully someone can spot the mistake I made because I can't...
Error starting at line : 18 in command -
CREATE TABLE "GuyAddress"
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address_zipcode FOREIGN KEY(Address_zipcode) REFERENCES Address(zipcode),
CONSTRAINT FK_GuyAddress_Address_number FOREIGN KEY(Address_number) REFERENCES Address("number"),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
)
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
Thanks!
You don't need separate foreign keys for each column in the referenced table's primary key - you can have multiple columns in a foreign key, e.g.:
CREATE TABLE Guy
(
id NUMBER(10) PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE Address
(
zipcode VARCHAR(6),
address_number NUMBER(10),
CONSTRAINT PK_Address PRIMARY KEY(zipcode, address_number)
);
CREATE TABLE GuyAddress
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address FOREIGN KEY(Address_zipcode, Address_number) REFERENCES Address(zipcode,address_number),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
);
Note that I've updated your address.number column to be address.address_number, as it's not recommended that you use a column based on a keyword. Using doublequotes to get around this (and also enforce case sensitivity) is not recommended either; you'll have to remember to use them every time you reference that column!
As an aside, I assume that your address table has other columns? Because as things stand, the address table is pointless and could be skipped!

Error creating database with multiple primary key columns and referencing foreign key

I've been having an issue when creating a database. Each table has a primary key with many foreign keys used also. The issue I have is that I keep getting the error
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
I don't know what is causing this and is flagging the PROJECT_RECORDS table as the issue. I have used the same method in the PROJECT_TABLES table.
SQL/Oracle
CREATE TABLE PROJECT_DB
(DB_ID number (3) NOT NULL primary key,
DB_NAME varchar2 (25) NOT NULL,
DB_DESCRIPTION varchar2 (75) NOT NULL,
DB_DATE date NOT NULL);
CREATE TABLE PROJECT_DATATYPE
(DATATYPE_NAME varchar2 (20) NOT NULL PRIMARY KEY,
DATATYPE_DATATYPE varchar2(50) NOT NULL);
CREATE TABLE PROJECT_TABLES (
PROJECT_ID number(3) not null references PROJECT_DB(DB_ID) on delete cascade,
PROJECT_FIELDNAME varchar2(25) not null,
PROJECT_DATATYPE varchar2(50) not null references PROJECT_DATATYPE(DATATYPE_NAME),
PROJECT_LENGTH number(3),
PROJECT_REQUIRED varchar2(8),
PROJECT_LISTCOLUMNID number (3) not null,
primary key(PROJECT_ID, PROJECT_LISTCOLUMNID));
CREATE TABLE PROJECT_RECORDS (
RECORDS_ROWID number(3) not null,
RECORDS_LISTCOLUMNID number (3) not null references PROJECT_TABLES(PROJECT_LISTCOLUMNID)on delete cascade,
RECORDS_LISTID number (3) not null,
RECORDS_RECORDVALUE varchar2 (25),
primary key(RECORDS_ROWID, RECORDS_LISTCOLUMNID));
commit;
The reasoning for adding multiple primary keys to the PROJECT_TABLES table is that the listcolumnid isn't unique.
If listcolumnid is not unique, you cannot put a foreign key constraint on it. A foreign key always refrences exatcly one parent row. So you probably should use both columns in your foreign key (assuming there is really a 1:n relation):
CREATE TABLE PROJECT_RECORDS (
RECORDS_ROWID number(3) not null,
RECORDS_ID number(3) not null,
RECORDS_LISTCOLUMNID number (3) not null,
RECORDS_LISTID number (3) not null,
RECORDS_RECORDVALUE varchar2 (25),
primary key(RECORDS_ROWID, RECORDS_LISTCOLUMNID),
foreign key fk_project_projectrecords (RECORDS_ID, RECORDS_LISTCOLUMNID) references PROJECT_TABLES(PROJECT_ID, PROJECT_LISTCOLUMNID)on delete cascade,
);
(The example is using your prefix naming convention, I would change RECORDS_ID to PROJECT_ID, same for RECORDS_LISTCOLUMNID)
The problem is with your syntax. You don't do this:
, fieldname datatype references (something)
You do this:
, primary key(somefield)
, foreign key (somefield) references sometable(somefield)