Oracle (ORA-02270) : no matching unique or primary key for that column list - sql

I have two tables:
CREATE TABLE Trasy_srodki_transportu(
ID_Trasy Integer NOT NULL,
ID_pojazdu Integer NOT NULL
)
/
CREATE TABLE Trasy(
ID_Trasy Integer NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL
)
Now i want to add foreign key to Trasy_srodki_transportu referencing to Trasy table:
ALTER TABLE Trasy_srodki_transportu ADD CONSTRAINT Trasa_jest_wykorzystywana FOREIGN KEY (ID_Trasy) REFERENCES Trasy (ID_Trasy)
/
and this throws Oracle (ORA-02270) : no matching unique or primary key for this column-list error. Any suggestions how to fix this?Data modeler view

A foreign key needs to reference a key on the related table, but it's not the case in your example. Change the definition of the second table by adding a PRIMARY KEY constraint in it, as in:
CREATE TABLE Trasy (
ID_Trasy Integer PRIMARY KEY NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL
)
Alternatively, you can create a unique constraint on it, that can also serve as a key. For example:
CREATE TABLE Trasy (
ID_Trasy Integer NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL,
CONSTRAINT uq_idtrasy UNIQUE (ID_Trasy)
)

Related

No primary or candidate keys in the referenced table?

I keep getting an error:
There are no primary or candidate keys in the referenced table 'TProducts' that match the referencing column list in the foreign key 'TProducts_TCategories_FK'."
When trying to establish a relationship between my products table and categories table. I have checked the spelling multiple times but can't seem to figure out why I keep getting this error. The rest of my tables are fine, and I used the same method.
CREATE TABLE TCategories
(
intCategoryID INTEGER NOT NULL,
strCategoryName VARCHAR(255) NOT NULL,
CONSTRAINT TCategories_PK PRIMARY KEY (intCategoryID)
)
CREATE TABLE TProducts
(
intProductID INTEGER NOT NULL,
intVendorID INTEGER NOT NULL,
intCategoryID INTEGER NOT NULL,
strProductName VARCHAR(255) NOT NULL,
monCostofProduct MONEY NOT NULL,
monRetailCost MONEY NOT NULL,
intInventory INTEGER NOT NULL,
CONSTRAINT TProducts_PK PRIMARY KEY (intProductID)
)
ALTER TABLE TProducts
ADD CONSTRAINT TProducts_TCategories_FK
FOREIGN KEY (intCategoryID) REFERENCES TProducts (intCategoryID)
You are referencing to TProducts table and not TCategories table:
ALTER TABLE TProducts ADD CONSTRAINT TProducts_TCategories_FK
FOREIGN KEY ( intCategoryID ) REFERENCES TCategories( intCategoryID )

Column exist but program don t see? [duplicate]

This question already has answers here:
SQLite Foreign Key
(5 answers)
Closed 1 year ago.
I created a 3 tables but in the last one shows errors
CREATE TABLE Student
(
St_Id char(7) PRIMARY KEY,
St_Fname varchar(15) NOT NULL,
St_Lname varchar(20) NOT NULL,
St_DOB date
)
CREATE TABLE Course
(
Course_code char(5) PRIMARY KEY,
Course_title varchar(30) NOT NULL,
Course_credit INTEGER NOT NULL
)
CREATE TABLE Registration
(
Reg_no INTEGER PRIMARY KEY AUTOINCREMENT ,
FOREIGN KEY (St_Id) REFERENCES Student(St_Id),
FOREIGN KEY (Course_code) REFERENCES Course(Course_code),
Mark_obtaines INTEGER
)
and error is
Execution finished with errors. Result: unknown column "St_Id" in
foreign key definition At line 1: CREATE TABLE Registration ( Reg_no
INTEGER PRIMARY KEY AUTOINCREMENT , FOREIGN KEY (St_Id) REFERENCES
Student(St_Id),
In order to define a foreign key, you need to define the column first -- and the types need to match the type in the referenced table:
CREATE TABLE Registration (
Reg_no INTEGER PRIMARY KEY AUTOINCREMENT,
St_Id CHAR(7),
Course_Code CHAR(5),
FOREIGN KEY (St_Id) REFERENCES Student(St_Id),
FOREIGN KEY (Course_code) REFERENCES Course(Course_code),
Mark_obtaines INTEGER
);
Then the FOREIGN KEY declaration provides more information about the column.

there is no unique constraint matching given keys for referenced table in SQL

CREATE TABLE TEST( course_number INTEGER NOT NULL,
semester INTEGER NOT NULL,
time INTEGER NOT NULL,
room INTEGER NOT NULL,
day INTEGER NOT NULL,
credit_points INTEGER NOT NULL,
UNIQUE(course_number,semester),
CHECK(course_number>0),
CHECK(credit_points>0),
CHECK(room>0));
CREATE TABLE STUDENT (student_id INTEGER NOT NULL,
student_name text NOT NULL,
faculity text NOT NULL,
credit_points INTEGER NOT NULL,
UNIQUE(student_id),
CHECK(student_id>0),
CHECK(credit_points>=0));
CREATE TABLE STUDENT_REG
(student_id INTEGER NOT NULL,
course_number INTEGER NOT NULL,
semester INTEGER NOT NULL,
FOREIGN KEY (student_id) REFERENCES STUDENT(student_id),
FOREIGN KEY (course_number) REFERENCES TEST(course_number),
FOREIGN KEY (semester) REFERENCES TEST(semester));
I have three tables:
Test
Student
Student registration, it's purpose is to a student to a course.
I get this error when I compile the third table:
ERROR: there is no unique constraint matching given keys for referenced table "test"
I have no idea why, any help will be highly appreciated.
You want a compound foreign key rather than two distinct keys:
CREATE TABLE STUDENT_REG (
student_id INTEGER NOT NULL,
course_number INTEGER NOT NULL,
semester INTEGER NOT NULL,
FOREIGN KEY (student_id) REFERENCES STUDENT(student_id),
FOREIGN KEY (course_number semester) REFERENCES TEST(course_number, semester)
);
Why you need that is because table TEST as has compound unique key on these two columns:
UNIQUE(course_number,semester)
So for table STUDENT_REG to unambigously refer to a row in TEST, you need the combination of both columns, which means a 2-columns foreign key rather than two distinct foreign keys.

Creating Table (Oracle)

I am creating two tables. The first table creates with no errors, but when I try to create the SUBHEAD table, I get an error: Line 2, Missing right parenthesis. I am not sure what is wrong with this line. Below is my SQL statements:
CREATE TABLE HEAD
(Code NUMERIC(4,0) NOT NULL PRIMARY KEY,
HeadName VARCHAR(50) NOT NULL UNIQUE,
HType VARCHAR(1) NOT NULL,
HDate DATE NOT NULL,
OpBal DECIMAL(11,2) NOT NULL
);
CREATE TABLE SUBHEAD
(HCode NUMERIC(4,0) NOT NULL FOREIGN KEY REFERENCES HEAD(Code),
SubCode NUMERIC(4,0) NOT NULL,
SubName VARCHAR(50) NOT NULL,
SDate DATE NOT NULL,
OpBal DECIMAL (11,2) NOT NULL,
CONSTRAINT pk_subheadID PRIMARY KEY (HCode, SubCode)
);
syntax: http://docs.oracle.com/cd/B28359_01/server.111/b28286/clauses002.htm#SQLRF52167
note that "in-line" constraint syntax is : "col col_type REFERENCES TAB(COL)" and not "FOREIGN KEY"
The data type for number is NUMBER not NUMERIC
So my memory did not fool me - you can have multiple in-line constraints (although the syntax graph doesn't show it):
SQL> create table tt1(a number primary key);
Table created.
SQL> create table tt2(a number references tt1(a) not null);
Table created.
CREATE TABLE SUBHEAD
(HCode NUMERIC(4,0) NOT NULL, FOREIGN KEY (Hcode) REFERENCES HEAD(Code),
SubCode NUMERIC(4,0) NOT NULL,
SubName VARCHAR(50) NOT NULL,
SDate DATE NOT NULL,
OpBal DECIMAL (11,2) NOT NULL,
CONSTRAINT pk_subheadID PRIMARY KEY (HCode, SubCode)
);
(note comma, and reference to the new table column)

Error: Could not create constraint SQL Server 2008

I am creating 6 tables. When creating the first 3 tables, everything works well. But then when I have to use a Foreign Key that I have used previously when creating another table, the system presents an error.
These are the 3 first tables:
CREATE TABLE Employee (
EmployeeID INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL,
Position TEXT NOT NULL,
Salary REAL NOT NULL,
Remarks TEXT
);
CREATE TABLE Planet (
PlanetID INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL,
Coordinates REAL NOT NULL
);
CREATE TABLE Has_Clearance (
Employee INTEGER NOT NULL
CONSTRAINT fk_Employee_EmployeeID REFERENCES Employee(EmployeeID),
Planet INTEGER NOT NULL
CONSTRAINT fk_Planet_PlanetID REFERENCES Planet(PlanetID),
Level INTEGER NOT NULL,
PRIMARY KEY(Employee, Planet)
);
Then I create the 4th table:
CREATE TABLE Shipment (
ShipmentID INTEGER PRIMARY KEY NOT NULL,
Date TEXT,
Manager INTEGER NOT NULL
CONSTRAINT fk_Employee_EmployeeID REFERENCES Employee(EmployeeID),
Planet INTEGER NOT NULL
CONSTRAINT fk_Planet_PlanetID REFERENCES Planet(PlanetID)
);
And I get the following error:
"There is already an object named 'fk_Employee_EmployeeID' in the database. Could not create constraint."
Please let me know how to create the FK in this 4th table.
The constraint "fk_Employee_EmployeeID" is first created on table "Has_Clearance" and then you try to create another constraint with the same name - which is not allowed.
Just rename the constraint in the 4-th table like this:
CREATE TABLE Shipment (
ShipmentID INTEGER PRIMARY KEY NOT NULL,
Date TEXT,
Manager INTEGER NOT NULL CONSTRAINT fk_Maneger_EmployeeID REFERENCES Employee(EmployeeID),
Planet INTEGER NOT NULL CONSTRAINT fk_Planet_PlanetID REFERENCES Planet(PlanetID)
);