foreign key Oracle - sql

I'm working with Oracle Database and I have this script :
create table Guardians (
noCage number(3),
nomE varchar2(20),
CONSTRAINT nc_ne1 PRIMARY KEY(noCage, nomE)
);
create table Animals(
nomA varchar2(20) PRIMARY KEY,
type varchar2(15) NOT NULL,
country varchar2(20) NOT NULL,
noCage number(3),
CONSTRAINT fk_anim_cage0 FOREIGN KEY(noCage) REFERENCES Guardians(noCage)
);
Upon executing the script, the table guardians is created, an error is prompted and the table Animals is not created. I did some manipulations and i think that it's got to do with the
CONSTRAINT nc_ne1 PRIMARY KEY(noCage, nomE)

Since Guardians Table has composite primary key you need to include both the columns in foreign key.
CONSTRAINT fk_anim_cage0 FOREIGN KEY(noCage,nomA) REFERENCES Guardians(noCage, nomE)
Your Animals table would be like this
create table Animals(
nomA varchar2(20) PRIMARY KEY,
type varchar2(15) NOT NULL,
country varchar2(20) NOT NULL,
noCage number(3),
CONSTRAINT fk_anim_cage0 FOREIGN KEY(noCage,nomA)
REFERENCES Guardians(noCage, nomE)
);

Related

Is it possible to create to tables that reference each other without using ALTER? [duplicate]

This question already has an answer here:
Is there any other way to create constraints during SQL table creation?
(1 answer)
Closed 2 years ago.
I am new to databases in general and would like to have an answer to a problem that I am facing.
I would like to know if there is away two create two tables that reference each other without creating one and Altering it later and adding the reference.
CREATE TABLE Cats(
name VARCHAR2(15) CONSTRAINT cat_name_nn NOT NULL,
gender VARCHAR2(1) CONSTRAINT cat_gd CHECK(gender IN('W', 'M')),
nickname VARCHAR2(15) CONSTRAINT cat_pk PRIMARY KEY,
function VARCHAR2(10) CONSTRAINT cat_fn REFERENCES Functions(function),
chief VARCHAR2(15) CONSTRAINT cat_chf REFERENCES Cats(nickname),
in_herd_sinnce DATE DEFAULT SYSDATE,
mice_ration NUMBER(3),
mice_extra NUMBER(3),
band_no NUMBER(2) CONSTRAINT cat_bno REFERENCES Bands(band_no))
CREATE TABLE Bands(
band_no NUMBER(2) CONSTRAINT bd_pk PRIMARY KEY,
name VARCHAR2(20) CONSTRAINT bd_name_nn NOT NULL,
site VARCHAR2(15) CONSTRAINT bd_site_un UNIQUE,
band_chief VARCHAR2(15) CONSTRAINT bd_chf_un UNIQUE
CONSTRAINT bd_chf_nm REFERENCES Cats(nickname)
);
As far as I remember I can not do it; I am right?
No, it cannot be done. Whichever table you CREATE first cannot have a foreign key to the other table as that other table does not exist yet; instead you need to create the tables and then use ALTER TABLE ADD CONSTRAINT to create the constraint from the first table to the second.
For example:
CREATE TABLE Functions ( function VARCHAR2(10) CONSTRAINT fn_fn PRIMARY KEY );
CREATE TABLE Cats(
name VARCHAR2(15) CONSTRAINT cat_name_nn NOT NULL,
gender VARCHAR2(1) CONSTRAINT cat_gd CHECK(gender IN('W', 'M')),
nickname VARCHAR2(15) CONSTRAINT cat_pk PRIMARY KEY,
function VARCHAR2(10) CONSTRAINT cat_fn REFERENCES Functions(function),
chief VARCHAR2(15) CONSTRAINT cat_chf REFERENCES Cats(nickname),
in_herd_sinnce DATE DEFAULT SYSDATE,
mice_ration NUMBER(3),
mice_extra NUMBER(3),
band_no NUMBER(2)
);
CREATE TABLE Bands(
band_no NUMBER(2) CONSTRAINT bd_pk PRIMARY KEY,
name VARCHAR2(20) CONSTRAINT bd_name_nn NOT NULL,
site VARCHAR2(15) CONSTRAINT bd_site_un UNIQUE,
band_chief VARCHAR2(15) CONSTRAINT bd_chf_un UNIQUE
CONSTRAINT bd_chf_nm REFERENCES Cats(nickname)
);
ALTER TABLE CATS ADD CONSTRAINT cat_bno FOREIGN KEY ( band_no )
REFERENCES Bands(band_no);
db<>fiddle here

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#)
);

query not working for foreign key as a composite primary key of another table oracle SQL

I am trying to add two foreign keys for an associative entity in SQL Oracle. The primary key that I am referring from another table is a composite primary key. When I try to enter the SQL it says
no matching unique or primary key for this column-list
The code that I used to create the tbl_customer
CREATE TABLE tbl_Customer(
customer_id NUMBER(4)
CONSTRAINT pk_customer PRIMARY KEY,
CustomerName VARCHAR2(50) NOT NULL,
Telephone VARCHAR2(10),
CusEmail VARCHAR2(20),
CONSTRAINT cus_email UNIQUE(CusEmail),
location_id NUMBER(4)
CONSTRAINT fk_location_id references tbl_Location(location_id));
SQL to create tbl_Vehicle
CREATE TABLE tbl_Vehicle(
vehicle_id NUMBER(4),
PlateNo VARCHAR2(10),
CONSTRAINT pk_v PRIMARY KEY(vehicle_id,PlateNo),
Brand VARCHAR2(20),
Model VARCHAR2(10),
TotalNoSeats NUMBER(2),
Class VARCHAR2(4) NOT NULL,
CONSTRAINT no_seats CHECK (TotalNoSeats<100),
driver_id NUMBER(4)
CONSTRAINT fk_driveridV references tbl_Driver(driver_id),
location_id NUMBER(4)
CONSTRAINT fk_locationV references tbl_Location(location_id));
The associative table is
CREATE TABLE tbl_Customer_Vehicle(
customer_id NUMBER(4)
CONSTRAINT fk_customer_id references tbl_Customer(customer_id),
vehicle_id NUMBER(4)
CONSTRAINT fk_vehicle_id references tbl_Vehicle(vehicle_id)
);
where the error is in this line
CONSTRAINT fk_vehicle_id references tbl_Vehicle(vehicle_id)
*
Is this error because vehicle_id is a composite primary key?
Please help!!
You need to add PlateNo column in tbl_Customer_Vehicle table and define foreign key on it.
CREATE TABLE tbl_Customer_Vehicle
(
customer_id NUMBER(4)
CONSTRAINT fk_customer_id references tbl_Customer(customer_id),
vehicle_id NUMBER(4),
PlateNo VARCHAR2(10),
CONSTRAINT fk_vehicle_id_PlateNo FOREIGN KEY(vehicle_id,PlateNo)
references tbl_Vehicle(vehicle_id,PlateNo)
);

Oracle DB can not create Table Showing "ORA-02270: no matching unique or primary key for this column-list"

I am trying to create tables in ORACLE with Foreign Key and Primary key but It is showing me error.
"
FOREIGN KEY(Branch_ID) REFERENCES Bank_Branchs(Branch_ID),
*
ERROR at line 10:
ORA-02270: no matching unique or primary key for this column-list
FOREIGN KEY(Branch_ID) REFERENCES Bank_Branchs(Branch_ID)
*
ERROR at line 10:
ORA-02270: no matching unique or primary key for this column-list
"
I don't know what is the reason of the error. Please take a look on my sql code.
drop table Employees;
drop table Bank_Branchs;
drop table Departments;
drop table Job_Titles;
drop table Accounts;
CREATE TABLE Bank_Branchs(
Branch_ID NUMBER(15) NOT NULL,
Branch_Name VARCHAR2(15),
Country VARCHAR2(35),
City VARCHAR2(35),
Phone VARCHAR2(15),
Manager_ID NUMBER(7) NOT NULL,
PRIMARY KEY (Branch_ID,Manager_ID)
);
CREATE TABLE Departments(
Dept_ID CHAR(3) NOT NULL,
Dept_Name VARCHAR2(25),
Head_of_Dept NUMBER(7),
PRIMARY KEY(Dept_ID)
);
CREATE TABLE Job_Titles(
Title_ID CHAR(3)NOT NULL,
Title_Name VARCHAR2(25),
Title_Desc VARCHAR2(250),
PRIMARY KEY(Title_ID)
);
CREATE TABLE Employees
(Emp_ID NUMBER(7) NOT NULL,
Branch_ID NUMBER(15) NOT NULL,
Title_ID CHAR(3),
Department_ID CHAR(3),
Manager_ID NUMBER(7),
Salary NUMBER(9),
Hourly_Rate NUMBER(9),
PRIMARY KEY(Emp_ID),
FOREIGN KEY(Branch_ID) REFERENCES Bank_Branchs(Branch_ID),
FOREIGN KEY(Title_ID) REFERENCES Job_Titles (Title_ID),
FOREIGN KEY(Department_ID) REFERENCES Departments (Dept_ID),
FOREIGN KEY(Manager_ID) REFERENCES Bank_Branchs (Manager_ID)
);
CREATE TABLE Accounts(
Account_ID NUMBER(7) NOT NULL,
Branch_ID NUMBER(15) NOT NULL,
Customer_ID NUMBER(7),
Acc_Type char(2),
Balance NUMBER(38),
Rate NUMBER(9),
Status VARCHAR(15),
PRIMARY KEY(Account_ID),
FOREIGN KEY(Branch_ID) REFERENCES Bank_Branchs(Branch_ID)
);
I tried to run this using start I:/SQLNAME.sql and the SQL command line is showing me the error.
Tested... And this will work, but please read below...
CREATE TABLE Bank_Branchs(
Branch_ID NUMBER(15) NOT NULL,
Branch_Name VARCHAR2(15),
Country VARCHAR2(35),
City VARCHAR2(35),
Phone VARCHAR2(15),
Manager_ID NUMBER(7) NOT NULL,
PRIMARY KEY (Branch_ID,Manager_ID)
);
CREATE TABLE Departments(
Dept_ID CHAR(3) NOT NULL,
Dept_Name VARCHAR2(25),
Head_of_Dept NUMBER(7),
PRIMARY KEY(Dept_ID)
);
CREATE TABLE Job_Titles(
Title_ID CHAR(3)NOT NULL,
Title_Name VARCHAR2(25),
Title_Desc VARCHAR2(250),
PRIMARY KEY(Title_ID)
);
CREATE TABLE Employees
(Emp_ID NUMBER(7) NOT NULL,
Branch_ID NUMBER(15) NOT NULL,
Title_ID CHAR(3),
Department_ID CHAR(3),
Manager_ID NUMBER(7),
Salary NUMBER(9),
Hourly_Rate NUMBER(9),
PRIMARY KEY(Emp_ID),
FOREIGN KEY(Branch_ID, Manager_ID) REFERENCES Bank_Branchs(Branch_ID, Manager_ID),
FOREIGN KEY(Title_ID) REFERENCES Job_Titles (Title_ID),
FOREIGN KEY(Department_ID) REFERENCES Departments (Dept_ID)
);
It seems like a manager_id would correspond to a employee_id...
It also seems like their may be more than one manager for an individual bank branch..
Bascically, i think manager_id should be removed from bank_branches and this should be created instead:
create table bank_branch_managers(
branch_id number(14),
manager_id number(7),
effective_date date,
activity_date date,
PRIMARY KEY (branch_id, manager_id),
FOREIGN KEY (branch_id) REFERENCES Bank_Branches(Brand_id),
FOREIGN KEY (manager_id) REFERENCED Employees( Emp_id)
);
But you'd need to adjust the setup of "Employees", probably adding a table for managers as well..
A FOREIGN KEY constraint have to be linked only to a PRIMARY KEY constraint in another table; or it can also be defined to reference the columns of a UNIQUE constraint in another table.
in your bank_branchs table, you have created a composite primary key ie a primary key consisting of more than one column.
PRIMARY KEY (Branch_ID,Manager_ID)
this statement does not make Brach_ID and Manager_ID as the primary keys, rather it makes their combination as a primary key. and since a foreign key can only refer to either a primary key or a column with UNIQUE constraint, it throws an error.
try adding UNIQUE constraint on both of these columns.
CREATE TABLE Bank_Branchs(
Branch_ID NUMBER(15) UNIQUE NOT NULL,
Branch_Name VARCHAR2(15),
Country VARCHAR2(35),
City VARCHAR2(35),
Phone VARCHAR2(15),
Manager_ID NUMBER(7) UNIQUE NOT NULL,
PRIMARY KEY (Branch_ID,Manager_ID)
);
this might solve your problem.

SQL: ORA-00904: invalid identifier

I am new to SQL and I get this error in Navicat for Oracle.
ORA-00904: : invalid identifier
DROP TABLE Series CASCADE CONSTRAINTS;
DROP TABLE User1 CASCADE CONSTRAINTS;
DROP TABLE Following CASCADE CONSTRAINTS;
DROP TABLE Episode CASCADE CONSTRAINTS;
--DROP TABLE BUNGALOW CASCADE CONSTRAINTS;
--DROP TABLE Watched CASCADE CONSTRAINTS;
CREATE TABLE Series
( SeriesID NUMBER(5) primary key,
Name VARCHAR2(100) NOT NULL,
Status VARCHAR2(15),
Genre VARCHAR2(100),
Country VARCHAR2(100),
Network VARCHAR2(100),
Runtime VARCHAR2(25)
);
CREATE TABLE User1(
UserID NUMBER(5) primary key,
Username VARCHAR2(100) NOT NULL,
Country1 varchar2(100),
Gender VARCHAR2(10) NOT NULL,
Birthday VARCHAR2(100),
Timezone VARCHAR2(100),
About_me VARCHAR2(300),
Password VARCHAR2(100) NOT NULL
);
CREATE TABLE Following (
UserID NUMBER(5) NOT NULL,
SeriesID NUMBER(5) NOT NULL,
Ignore1 CHAR(1) NOT NULL,
constraint Following_pk primary key(UserID, SeriesID),
constraint Following_fk1 foreign key(UserID) REFERENCES User1(UserID),
constraint Following_fk2 foreign key(SeriesID) REFERENCES Series(SeriesID)
);
CREATE TABLE Episode(
SeriesID NUMBER(5) NOT NULL,
Season NUMBER(2) NOT NULL,
Episode NUMBER(2) NOT NULL,
Name varchar2(100) NOT NULL,
Airdate DATE,
Summary varchar2(500),
constraint episode_pk primary key(SeriesID, Season, Episode),
constraint episode_fk foreign key(SeriesID) REFERENCES Series(SeriesID)
);
CREATE TABLE Watched(
UserID NUMBER(5) NOT NULL,
SeriesID NUMBER(5) NOT NULL,
Season NUMBER(2) NOT NULL,
Episode NUMBER(2) NOT NULL,
constraint watched_pk primary key(UserID, SeriesID, Season, Episode),
constraint watched_fk1 foreign key(UserID) REFERENCES User1(UserID),
constraint watched_fk2 foreign key(SeriesID) REFERENCES Episode(SeriesID),
constraint watched_fk3 foreign key(Season) REFERENCES Episode(Season),
constraint watched_fk4 foreign key(Episode) REFERENCES Episode(Episode),
);
The error comes up when creating the Watched table. I guess it has something to do with the multiple foreign keys from the Episode-table but i can't seem to figure it out.
You have a one comma to many, at the last constraint definition, try this:
CREATE TABLE Watched(
UserID NUMBER(5) NOT NULL,
SeriesID NUMBER(5) NOT NULL,
Season NUMBER(2) NOT NULL,
Episode NUMBER(2) NOT NULL,
constraint watched_pk primary key(UserID, SeriesID, Season, Episode),
constraint watched_fk1 foreign key(UserID) REFERENCES User1(UserID),
constraint watched_fk4 foreign key(Episode, season, seriesid) REFERENCES Episode(SeriesID, Season, Episode)
);
Also, you had too many foreign keys - instead of 3 foreign keys to reference the EPISODE table, you should have only one with 3 columns.