foreign keys: number of columns not equal to referenced columns - sql

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.

Related

Foreign Key Reference

I addressed in class that the 2 foreign keys in the FlightData table are Depart_Code and Ariv_Code that there isn't any table to make references to them being a primary key in, in the relational schema we were given.
In class I was told that they reference Airport_Code in the Airport table. I was wondering I would go about doing that? I feel like I am missing something obvious. I appreciate any help offered I am still new to database in general and I am currently on Oracle 11g.
Airport table
CREATE TABLE Airport
(
Airport_Code VARCHAR2(7) CONSTRAINT pk_Airport Primary Key,
City_Code VARCHAR2(3),
CONSTRAINT fk_Airport_City_Code
FOREIGN KEY(City_Code) REFERENCES City,
Airport_Name VARCHAR2(30)
);
FlightData table:
CREATE TABLE FlightData
(
Flt_Nbr VARCHAR2(3) CONSTRAINT pk_FlightData Primary Key,
Depart_Code VARCHAR2(30),
Ariv_Code VARCHAR2(30)
);
To make sure Depart_Code and Ariv_Code always reference an airport in the Airport table you need to:
Make these columns NOT NULL.
Ensure they have the same data type as the key in Airport. Make them have a length of 7.
Add two foreign key constraints, each one based on each column.
For example, the second table could look like:
CREATE TABLE FlightData (
Flt_Nbr VARCHAR2(3) CONSTRAINT pk_FlightData Primary Key,
Depart_Code VARCHAR2(7) not null,
constraint fk1 foreign key (Depart_Code) references Airport (Airport_Code),
Ariv_Code VARCHAR2(7) not null,
constraint fk2 foreign key (Ariv_Code) references Airport (Airport_Code)
);

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.

Oracle SQL: Receiving 'no matching unique or primary key' error and don't know why

I'm receiving this error when trying to create a table and I don't know why:
[2016-07-05 14:08:02] [42000][2270] ORA-02270: no matching unique or primary key for this column-list
This question seems different (to me) from a similar question, because in that question the OP is referencing a table with a composite PK, while I am not.
And while this other question has the same error code, it is because the OP is incorrectly references the primary key, which I don't think I did.
May someone more experienced in SQL educate me?
(A couple of things to note: 1) I know the table/column names have small errors/deviations from convention, but this is for homework, and the teacher requires I have the tables and rows declared exactly his way, even if it's non-conventional. 2) Yes, that's silly; but no, I can't change it or I get marked down.)
CREATE TABLE Student_Course
(
Stu_ID NUMBER(5) NOT NULL,
Course_ID VARCHAR2(8) NOT NULL,
Section# NUMBER(3),
CONSTRAINT pk_stu_crse PRIMARY KEY (Stu_ID, Course_ID),
CONSTRAINT fk_course_id FOREIGN KEY (Course_ID) REFERENCES course(Course_ID),
CONSTRAINT fk_stu_id FOREIGN KEY (Stu_ID) REFERENCES student(Stu_ID),
CONSTRAINT fk_section FOREIGN KEY (Section#) REFERENCES course(Section#)
)
There are only two, small, referenced tables, which are:
CREATE TABLE student
(
Stu_ID NUMBER(5) PRIMARY KEY ,
Lname VARCHAR2(20),
Fname VARCHAR2(20),
Mi CHAR(1),
Sex CHAR(1),
Major VARCHAR2(15),
Home_State CHAR(2)
);
CREATE TABLE course
(
Course_ID VARCHAR2(8) PRIMARY KEY ,
Section# NUMBER(3),
C_Name VARCHAR2(30),
C_Description VARCHAR2(30)
);
A foreign key is a reference to a primary key in another table.
The last constraint CONSTRAINT fk_section FOREIGN KEY (Section#) REFERENCES course(Section#) won't work - Section# isn't a primary key in that table
Section# Must be at least UNIQUE in course table.
If you want to use Section# as a reference for a foreign key, it must be a UNIQUE or a PRIMARY KEY
More information about FOREIGN KEY and constraints
Thanks to good answers, I'm posting my code which I corrected based on help here. Hope my corrections help others in the future.
CREATE TABLE student
(
Stu_ID NUMBER(5) PRIMARY KEY ,
Lname VARCHAR2(20),
Fname VARCHAR2(20),
Mi CHAR(1),
Sex CHAR(1),
Major VARCHAR2(15),
Home_State CHAR(2)
);
CREATE TABLE course
(
Course_ID VARCHAR2(8) ,
Section# NUMBER(3) ,
C_Name VARCHAR2(30),
C_Description VARCHAR2(30),
CONSTRAINT pk_course PRIMARY KEY (Course_ID, Section#)
);
CREATE TABLE Student_Course
(
Stu_ID NUMBER(5) ,
Course_ID VARCHAR2(8) ,
Section# NUMBER(3) ,
CONSTRAINT pk_stu_crse PRIMARY KEY (Stu_ID, Course_ID, Section#),
CONSTRAINT fk_stu FOREIGN KEY (Stu_ID) REFERENCES student(Stu_ID),
CONSTRAINT fk_course_id FOREIGN KEY (Course_ID, Section#) REFERENCES course(Course_ID, Section#)
);

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)

Invalid FK and PK reference

Unable to create table as oracle shows ' no matching unique or primary key for this column-list' when I did label the primary key reference for the required table.
First table created successfully:
CREATE TABLE TEST
(
TESTno VARCHAR2(6) NOT NULL,
ExamNo VARCHAR2(6) NOT NULL,
TEST_Date DATE NOT NULL,
ACTUAL DATE,
PREDICTED Date,
CONSTRAINT TESTPKs PRIMARY KEY (TEST_Date, TESTno, ExamNo),
CONSTRAINT TTESTNO_Fk FOREIGN KEY (TESTno) REFERENCES TESTPAPER (Flightno)
CONSTRAINT TEXAMNo_FK FOREIGN KEY (ExamNo) REFERENCES Exam (ExamNo)
);
Here's the table i want to create and gives me error:
CREATE TABLE Assignment
(
TEST_Date DATE NOT NULL,
ExamNo VARCHAR2(6) NOT NULL,
TestNo VARCHAR2(6) NOT NULL,
Type VARCHAR2(20),
Hours_Spent Decimal(4,2),
CONSTRAINT ASSIGNPKS PRIMARY KEY (TEST_Date, TestNo , ExamNo),
CONSTRAINT ASSIGNTESTDATE_FK FOREIGN KEY (TEST_Date) REFERENCES TEST(TEST_Date) ON
DELETE CASCADE,
CONSTRAINT ASSIGNTESTNO_FK FOREIGN KEY (TESTno) REFERENCES TESTPAPER (Flightno)
CONSTRAINT TEXAMNo_FK FOREIGN KEY (ExamNo) REFERENCES Exam (ExamNo)
);
May i know where's the issue that it keeps giving me no matching unique primary keys? I already tried to recreate and labelled the 'test_Date' as my primary key. But oracle can't seems to find.
Thanks
The PK you refer to is PRIMARY KEY (TEST_Date, TESTno, ExamNo) — hence the foreign key should be FOREIGN KEY (TEST_Date, TESTno, ExamNo) as well. The error you're getting is due to your attempt to refer to a part of TEST's PK.
See also http://download.oracle.com/docs/cd/B10500_01/server.920/a96524/c22integ.htm
Check the tables you are referencing in your foreign keys. Those columns must be the primary key or otherwise unique on the foreign table.