Creating table in Oracle 11g with multiple foreign keys with - ORA-00922: missing or invalid option - oracle

I followed the previous instruction of placing commas after each of the CONSTRAINTS. However, on this table, it's giving me the following error message:
ORA-02264: name already used by an existing constraint
All the foreign key tables that are associated with this table are created successfully. What is missing here?
CREATE TABLE FIELD (
ENCT_ID VARCHAR2(25) NOT NULL,
FLD_NUM NUMBER NOT NULL,
FLD_DESC VARCHAR2(50) NOT NULL,
SYMPT_CODE VARCHAR2(25),
DIAG_CODE VARCHAR2(25),
TEST_ID VARCHAR2(25),
RM_ID VARCHAR2(10) NOT NULL,
AX_CODE VARCHAR2(25) NOT NULL,
PROV_ID VARCHAR2(25) NOT NULL,
MED_NDC VARCHAR2(25),
PRIMARY KEY (ENCT_ID, FLD_NUM),
CONSTRAINT FK_ENCOUNTER FOREIGN KEY (ENCT_ID) REFERENCES ENCOUNTER(ENCT_ID),
CONSTRAINT FK_SYMPTOM FOREIGN KEY (SYMPT_CODE) REFERENCES SYMPTOM(SYMPT_CODE),
CONSTRAINT FK_DIAGNOSIS FOREIGN KEY (DIAG_CODE) REFERENCES DIAGNOSIS(DIAG_CODE),
CONSTRAINT FK_TEST FOREIGN KEY (TEST_ID) REFERENCES TEST(TEST_ID),
CONSTRAINT FK_ROOM FOREIGN KEY (RM_ID) REFERENCES ROOM(RM_ID),
CONSTRAINT FK_ASSESSMENT FOREIGN KEY (AX_CODE) REFERENCES ASSESSMENT(AX_CODE),
CONSTRAINT FK_PROVIDER FOREIGN KEY (PROV_ID) REFERENCES PROVIDER(PROV_ID),
CONSTRAINT FK_MEDICATION FOREIGN KEY (MED_NDC) REFERENCES MEDICATION(MED_NDC));

CREATE TABLE FIELD (
ENCT_ID VARCHAR2(25) PRIMARY KEY,
FLD_NUM NUMBER PRIMARY KEY,
FLD_DESC VARCHAR2(50) NOT NULL,...
Remove any one primary key. It will be the index for the table. Like a School text books have only one index, same way, every table will have only one primary key.

Related

ORA-02256: number of referencing columns must match referenced [duplicate]

This question already has an answer here:
ORA-02256: referencing foreign key
(1 answer)
Closed last year.
CREATE TABLE Goft_ForeEver_cus(
customer_Id VARCHAR(10),
first_Name VARCHAR2(20) CONSTRAINT sys_cus_fName_nn NOT NULL,
last_Name VARCHAR2(20) CONSTRAINT sys_cus_lName_nn NOT NULL,
girt_Card_Amount NUMBER(5,2) CONSTRAINT sys_cus_gca_nn NOT NULL,
email VARCHAR2(30) CONSTRAINT sys_cus_email_ck CHECK (email ='%[^a-z,0-9,#,.,_,-]%' ) CONSTRAINT sys_cus_email_nn NOT NULL,
CONSTRAINT sys_cus_pk PRIMARY KEY(customer_Id)
);
ALTER TABLE Goft_ForeEver_cus
ADD CONSTRAINT sys_cus_email_UK UNIQUE (email);
CREATE TABLE Goft_ForeEver_Course_Info(
course_Name VARCHAR2(20),
city VARCHAR2(30) CONSTRAINT sys_courtInf_city_nn NOT NULL,
prov VARCHAR2(20) CONSTRAINT sys_courtInf_prov_nn NOT NULL,
postal_Code VARCHAR2(10) CONSTRAINT sys_courtInf_posC_nn NOT NULL,
star_rating NUMBER(1,1),
discript VARCHAR2(200) CONSTRAINT sys_courtInf_descript_uk UNIQUE,
year_Build DATE CONSTRAINT sys_courtInf_date_nn NOT NULL,
court_length Number(5,2)
CONSTRAINT sys_courtInf_courtL_ck CHECK( court_length = 'YARD')
CONSTRAINT sys_courtInf_courtL_nn NOT NULL,
CONSTRAINT sys_courtInf_pk PRIMARY KEY (course_Name, star_rating )
);
CREATE TABLE Goft_ForeEver_favorite_courses(
course_Name VARCHAR2(20),
customer_Id VARCHAR2(10),
CONSTRAINT sys_favCourt_PK PRIMARY KEY(course_Name,customer_Id),
CONSTRAINT sys_favCourt_FK1 FOREIGN KEY (customer_Id) REFERENCES Goft_ForeEver_cus(customer_Id),
CONSTRAINT sys_favCourt_FK2 FOREIGN KEY (course_Name) REFERENCES Goft_ForeEver_Course_Info(course_Name)
);
);
Error
Error report - ORA-02256: number of referencing columns must match referenced columns 02256. 00000 - "number of referencing columns must match referenced columns" *Cause: The number of columns in the foreign-key referencing list is not equal to the number of columns in the referenced list. *Action: Make sure that the referencing columns match the referenced columns.
It is because Goft_ForeEver_Course_Info has primary key as
CONSTRAINT sys_courtInf_pk PRIMARY KEY (course_Name, star_rating )
while Goft_ForeEver_favorite_courses that references it has foreign key as
CONSTRAINT sys_favCourt_FK2 FOREIGN KEY (course_Name) REFERENCES Goft_ForeEver_Course_Info(course_Name)
See? You're referencing COURSE_NAME, STAR_RATING composite key by COURSE_NAME only - that won't work.
Therefore, either remove STAR_RATING from the primary key in Goft_ForeEver_Course_Info, add STAR_RATING to Goft_ForeEver_favorite_courses, or redesign everything alltogether.
Illustration (SQL*Plus, which points to the error):
SQL> CREATE TABLE Goft_ForeEver_favorite_courses(
2 course_Name VARCHAR2(20),
3 customer_Id VARCHAR2(10),
4 CONSTRAINT sys_favCourt_PK PRIMARY KEY(course_Name,customer_Id),
5 CONSTRAINT sys_favCourt_FK1 FOREIGN KEY (customer_Id) REFERENCES Goft_ForeEver_cus(customer_Id),
6 CONSTRAINT sys_favCourt_FK2 FOREIGN KEY (course_Name) REFERENCES Goft_ForeEver_Course_Info(course_Name)
7 );
CONSTRAINT sys_favCourt_FK2 FOREIGN KEY (course_Name) REFERENCES Goft_ForeEver_Course_Info(course_Name)
*
ERROR at line 6:
ORA-02270: no matching unique or primary key for this column-list
SQL>
The problem here is you have declared the "course_Name" along with "star_rating" column as composite primary key in Goft_ForeEver_Course_Info table whereas you are referencing only one column "course_Name" in sys_favCourt_FK2 foreign key declaration in Goft_ForeEver_favorite_courses table.

Multiple foreign key referencing same column

I am using Oracle and I'm trying to create foreign key constraints.
Say I have a user table like this:
CREATE TABLE USER (
USER_ID INTEGER GENERATED BY DEFAULT AS IDENTITY,
USERNAME VARCHAR2(30) NOT NULL,
CONSTRAINT USER_PK PRIMARY KEY (USER_ID)
Then I am trying to create table that contains lead_user, delivery_user, and support_user.
I tried both but were not working
1.
CREATE TABLE EXAMPLE (
EXAMPLE_ID INTEGER GENERATED BY DEFAULT AS IDENTITY,
LEAD_USER INTEGER NOT NULL,
DELIVERY_USER INTEGER NOT NULL,
SUPPORT_USER INTEGER NOT NULL,
CONSTRAINT USER_PK PRIMARY KEY (EXAMPLE_ID)
CONSTRAINT FK_USER FOREIGN KEY (LEAD_USER, DELIVERY_USER, SUPPORT_USER) REFERENCES USER(USER_ID)
CREATE TABLE EXAMPLE (
EXAMPLE_ID INTEGER GENERATED BY DEFAULT AS IDENTITY,
LEAD_USER INTEGER NOT NULL,
DELIVERY_USER INTEGER NOT NULL,
SUPPORT_USER INTEGER NOT NULL,
CONSTRAINT USER_PK PRIMARY KEY (EXAMPLE_ID)
CONSTRAINT FK_USER FOREIGN KEY (LEAD_USER, DELIVERY_USER, SUPPORT_USER) REFERENCES USER(USER_ID, USER_ID, USER_ID)
I saw from here
SQLite - Multiple Foreign Keys Referencing the Same Column
that for SQLite, it cannot be done the above way and have to create separate foreign keys such as
CONSTRAINT FK_LEAD_USER FOREIGN KEY (LEAD_USER) REFERENCES USER(USER_ID)
CONSTRAINT FK_DELIVERY_USER FOREIGN KEY (DELIVERY_USER) REFERENCES USER(USER_ID)
CONSTRAINT FK_SUPPORT_USER FOREIGN KEY (SUPPORT_USER) REFERENCES USER(USER_ID)
Does the same applies to Oracle as well?

I get this error executing table three "The specified constraint name has to be unique. *Action: Specify a unique constraint name for the constraint

Table One:
CREATE TABLE Customer
(CustNo VARCHAR2(8) CONSTRAINT CustNoNotNull NOT NULL,
CustName VARCHAR2(30) CONSTRAINT CustNameNotNull NOT NULL,
Address VARCHAR2(50) CONSTRAINT AddressNotNull NOT NULL,
Internal CHAR(1) CONSTRAINT InternalNotNull NOT NULL,
Contact VARCHAR2(35) CONSTRAINT ContractNotNull NOT NULL,
Phone VARCHAR2(11) CONSTRAINT CPhoneNotNull NOT NULL,
City VARCHAR2(30) CONSTRAINT CityNotNull NOT NULL,
State VARCHAR2(2) CONSTRAINT StateNotNull NOT NULL,
Zip VARCHAR2(10) CONSTRAINT zipNotNull NOT NULL,
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CustNo) ) ;
Table Two:
CREATE TABLE Facility
(FacNo VARCHAR2(8) CONSTRAINT FacNoNotNull NOT NULL,
FacName VARCHAR2(30) CONSTRAINT FacNameNotNull NOT NULL,
CONSTRAINT PK_FACILITY PRIMARY KEY (FacNo)
CONSTRAINT Unique_FacName UNIQUE(FacName) );
Table Three:
CREATE TABLE EVENTREQUEST
( EVENTNO VARCHAR2(8) CONSTRAINT EVENTNONOTNULL NOT NULL,
DATEHELD DATE CONSTRAINT DATEHELDNOTNULL NOT NULL,
DATEREQ DATE CONSTRAINT DATEREQNOTNULL NOT NULL,
CUSTNO VARCHAR2(8) CONSTRAINT CUSTNONOTNULL NOT NULL ,
FACNO VARCHAR2(8) CONSTRAINT FACNONOTNULL NOT NULL,
DATEAUTH DATE CONSTRAINT DATEAUTHNULL NULL,
STATUS VARCHAR2(10) CONSTRAINT STATUSNOTNULL NOT NULL,
ESTCOST VARCHAR2(25) CONSTRAINT ESTCOSTNOTNULL NOT NULL,
ESTAUDIENCE VARCHAR2(10) CONSTRAINT ESTAUDIENCENOTNULL NOT NULL,
BUDNO VARCHAR2(8) CONSTRAINT BUDNONULL NULL,
CONSTRAINT PK_EVENTREQUEST PRIMARY KEY (EVENTNO),
CONSTRAINT FK_CUSTNO FOREIGN KEY (CUSTNO) REFERENCES CUSTOMER (CUSTNO),
CONSTRAINT FK_FACNO FOREIGN KEY (FACNO) REFERENCES FACILITY (FACNO),
CONSTRAINT CHECK_EVENTREQUEST_STATUS CHECK(STATUS IN('PENDING','DENIED','APPROVED')));
I get this error executing Table Three:
"The specified constraint name has to be unique. *Action: Specify a unique constraint name for the constraint
How can I prevent this error from occurring?
In Oracle, constraints are a type of object, and they have an identifier (a name by which they are distinguished from other objects). All constraints, on all tables within a schema, share the same name space. Which means you can't have two constraints with the same name in the same schema, even if they are on different tables.
Moreover, identifiers by default are case insensitive. On the second table you defined a constraint FacNoNotNull, and on the third table you are trying to define a constraint FACNONOTNULL. Since identifiers are case insensitive, this is the same name - so you get an exception.
A completely wrong approach (which would work, unfortunately - so many people may be inclined to do it even though it's wrong) is to enclose names in double-quotes, which makes them case sensitive. Don't do that!
Rather, one has to ask - why do you need to name your NOT NULL constraints in the first place? Just add the key words NOT NULL after the column definition; it is very hard to see when or where you would need to know the name of each such constraint.

I want to have only 3 columns (departure_City_Id, arrival_City_Id, Emp_number). However, when I come to insert value , it says that I have 4 columns

For the Epms table, I want to have only 3 columns (departure_City_Id, arrival_City_Id, Emp_number). However, when I come to insert value, it says that I have 4 columns. The extra one for City_Id. All I want is to avoid having City_Id column in this table. I declare it because I need it just as FK.
CREATE TABLE City (
City_Id char(3),
state varchar(30),
Primary key (City_Id)
);
create table Emps (
Emp_number varchar(30) primary key,
City_Id char(3),
departure_City_Id char(3),
arrival_City_Id char(3),
FOREIGN KEY (City_Id)
REFERENCES City(City_Id),
FOREIGN KEY (City_Id)
REFERENCES City(City_Id)
);
This is pretty straightforward, just specify e.g. , FOREIGN KEY (arrival_city_id) REFERENCES city(city_id) and you'll get an index.
CREATE TABLE emps (
emp_number varchar(30) NOT NULL,
city_id char(3) NOT NULL,
departure_city_id char(3) NOT NULL,
arrival_city_id char(3) NOT NULL,
PRIMARY KEY (emp_number),
KEY city_id (city_id),
KEY departure_city_id (departure_city_id),
KEY arrival_city_id (arrival_city_id),
CONSTRAINT emps_ibfk_1 FOREIGN KEY (city_id) REFERENCES city (city_id),
CONSTRAINT emps_ibfk_2 FOREIGN KEY (departure_city_id) REFERENCES city (city_id),
CONSTRAINT emps_ibfk_3 FOREIGN KEY (arrival_city_id) REFERENCES city (city_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
" I want is to avoid having City_Id column in this table. I declare it because I need it just as FK."
But you don't need it. The foreign key columns are the arrival and departure columns, and those are the ones you must reference in the constraint declarations:
create table Emps (
Emp_number varchar(30) primary key,
departure_City_Id char(3),
arrival_City_Id char(3),
FOREIGN KEY (departure_City_Id)
REFERENCES City(City_Id),
FOREIGN KEY (City_Id)
REFERENCES City(arrival_City_Id)
);
Naming constraints is optional but you may find it helpful when debugging foreign key failures, especially when a table has multiple constraints referencing the same parent key.

data base oracle foreign key error ORA-02270: no matching unique or primary key for this column-list

while making foreign key in player table it shows following error
ORA-02270: no matching unique or primary key for this column-list
create table person
(
per_ssn number(10) not null,
per_name varchar2(30) not null,
CONSTRAINT pk_PersonID PRIMARY KEY (per_ssn,per_name)
);
create table Player
(
player_ssn number(10) not null,
player_name varchar2(30) not null,
football_club_name varchar2(30) not null,
p_age number(2) not null,
p_weight number(3) not null,
p_height number(10) not null,
country varchar2(20) not null,
p_starting_date date not null,
p_ending_date date not null
);
alter table Player
add constraint player_ssn
FOREIGN KEY (player_ssn)
REFERENCING person (per_ssn)on delete cascade
I want to make two primary keys in person table and then want to refer these
primary keys in player table.
If I make one primary key and then refer it in player table, then it does not show error but I want to make two primary keys.
You should be referencing per_ssn,per_name because that is your PK on person.
Anyway, think about making per_ssn your PK in person table
alter table Player
add constraint player_ssn
FOREIGN KEY (player_ssn,player_name)
REFERENCING person (per_ssn,per_name)on delete cascade

Resources