oracle identifier too long - oracle

New to oracle
Im trying to assign composite key
CREATE TABLE rep_contracts
( Store_id NUMBER(8),
Name NUMBER(5)
Quarter CHAR(3),
Rep_id NUMBER(5),
CONSTRAINT rep_contracts_rep_idstore_id_pk PRIMARY KEY (rep_id, store_id),
CONSTRAINT rep_contracts_rep_id_fk FOREIGN KEY (rep_id),
CONSTRAINT rep_contracts_store_id_fk FOREIGN KEY (store_id) );
and im receiving
ORA-00972: identifier is too long
Im aware that composit key identifier longer than 30 char
but if I cut identifier few character shorter ex:
rep_contrac_rep_idstore_id_pk
then I receive
ORA-00905: missing keyword
I couldn't find any workaround

I cleaned the script and incorporated the comments:
added a comma after number(5)
changed all names to uppercase to save pain later
changed NAME which is an Oracle reserved key work to S_NAME
changed the data type from CHAR to VARCHAR2
added the references keyword to point to the table where the foreign key is
renamed the constraint so it is not more than 30 characters
like so:
CREATE TABLE rep_contracts
( STORE_ID NUMBER(8),
S_NAME NUMBER(5),
QUARTER VARCHAR2(3),
REP_ID NUMBER(5),
CONSTRAINT rep_cont_rep_idstore_id_pk PRIMARY KEY (REP_ID, STORE_ID),
CONSTRAINT rep_contracts_rep_id_fk FOREIGN KEY REFERENCES your_table(REP_ID),
CONSTRAINT rep_contracts_store_id_fk FOREIGN KEY REFERENCES your_table(STORE_ID) );

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.

want to link crew Assignment to above tables given I'm getting an error How to solve this error?

CREATE TABLE Route(
RouteNo VARCHAR(10),
Origin VARCHAR(30),
Destination VARCHAR(30),
DepartureTime VARCHAR(15),
SerialNo VARCHAR(5),
ArrivalTime VARCHAR(15),
PRIMARY KEY(RouteNo) );
CREATE TABLE Employee(
EmployeeID VARCHAR(5) NOT NULL,
Name VARCHAR(30),
Phone NUMBER,
JobTitle VARCHAR(30),
PRIMARY KEY(EmployeeID) );
CREATE TABLE Flight(
SerialNo VARCHAR(5),
RouteNo VARCHAR(5),
FlightDate DATE,
ActualTD VARCHAR(10),
ActualTA VARCHAR(10),
PRIMARY KEY(SerialNo, RouteNo, FlightDate),
FOREIGN KEY(RouteNo) REFERENCES Route(RouteNo),
FOREIGN KEY(SerialNo) REFERENCES Airplane(SerialNo) ); -- does Airplane table exists ?
CREATE TABLE CrewAssigment(
EmployeeID VARCHAR(5),
RouteNo VARCHAR(5),
FlightDate DATE,
Role VARCHAR(45),
Hours INT,
PRIMARY KEY(EmployeeID, RouteNo, FlightDate),
FOREIGN KEY(EmployeeID) REFERENCES Employee(EmployeeID),
FOREIGN KEY(RouteNo) REFERENCES Route(RouteNo),
FOREIGN KEY(FlightDate) REFERENCES Flight(FlightDate) );
Select * from CrewAssignment
This is my code where I'm getting an error in the CrewAssignment table and above are the tables where the foreign key is referenced from.
Error report -
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
A few objections.
This is clearly an Oracle question, not MySQL. How do I know? ORA-02270 is an Oracle database error code; pay attention to tags you use.
You should use VARCHAR2 datatype instead of VARCHAR. Why? Oracle recommends so.
create table flight fails first as it references the airplane table, and it doesn't exist yet (at least, not in code you posted)
error you're complaining about is due to create table crewassignment. One of its foreign keys references the flight table:
FOREIGN KEY(flightdate) REFERENCES flight(flightdate)
but flight's primary key is composite, made up of 3 columns:
PRIMARY KEY(serialno,
routeno,
flightdate)
which means that you can't create that foreign key.
So, what to do? No idea, I don't know rules responsible for such a data model. Either modify primary key of the flight table, or modify foreign key constraint of the crewassingment table.
Perhaps you could add a new column to flight table (made up of a sequence (or identity column, if your database version supports it) and then let the crewassignment table reference that primary key. Columns you currently use as a primary key (serialno, routeno, flightdate) would then switch to unique key.

missing right parenthesis SQL

I am new to SQL.
I am trying to create a table:
CREATE TABLE account
(AccountNo NUMBER(2) PRIMARY KEY,
AccountType VARCHAR(1) FOREIGN KEY REFERENCES account_type(TypeCode),
CustomerRef NUMBER(2) FOREIGN KEY REFERENCES bank_customer(CustomerRef),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2));
However it comes up with: ORA-00907: missing right parenthesis
I know for a fact that you can have to foreign keys, so that's not the problem. Could someone give me a had with the solution of creating the table?
A foreign key actually has to refer to something. In your case you have to tell Oracle what AccountType and CustomerRef refer to. Usually it looks as follows:
AccountType VARCHAR(1) FOREIGN KEY REFERENCES TABLE_NAME(COLUMN_NAME)
Of course, you have to replace TABLE_NAME and COLUMN_NAME with the name of the table and the name of the column you're referring to.
There are two notations you can use when declaring constraints:
1) inline (a constraint):
CREATE TABLE account
(AccountNo NUMBER(2) PRIMARY KEY,
AccountType VARCHAR(1) REFERENCES account_type(TypeCode),
CustomerRef NUMBER(2) REFERENCES bank_customer(CustomerRef),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2));
2) out-of-line
CREATE TABLE account
(
AccountNo NUMBER(2) PRIMARY KEY,
AccountType VARCHAR(1),
CustomerRef NUMBER(2),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2),
FOREIGN KEY(AccountType) REFERENCES account_type(TypeCode),
FOREIGN KEY(CustomerRef) REFERENCES account_type(TypeCode)
);
In both cases you can prepend the constraint declaration with CONSTRAINT <name> to give your name to a constraint, otherwise Oracle assigns its own name.
inline notation is applied to a column where the constraint is declared, out-of-line is applied to the table. There slight differences in syntax + some restrictions, for example you cannot declare out-of-line NULL constraint.
You can use inline and out-of-line syntax in CREATE TABLE and ALTER TABLE. Refer to Oracle documentation for more information
You need to define the foreign keys with REFERENCES. In the below snippet, replace AccountTypeTable and CustomerRefTable with the correct table names and replace typeColumn and refColumn with the correct column names in those tables these match to.
UPDATE
I added in the correct values from your comment on another answer
CREATE TABLE account (
AccountNo NUMBER(2) PRIMARY KEY,
AccountType VARCHAR(1),
CustomerRef NUMBER(2),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2),
CONSTRAINT account_fk1 FOREIGN KEY (AccountType) REFERENCES Account_Type(TypeCode),
CONSTRAINT account_fk2 FOREIGN KEY (CustomerRef) REFERENCES Bank_Customer(CustomerRef));
you are missing foreign key syntax...
CREATE TABLE account
(AccountNo NUMBER(2) PRIMARY KEY (P_ID),
AccountType VARCHAR(1) FOREIGN KEY (F1_ID) REFERENCES <table_name>(field_name>),
CustomerRef NUMBER(2) FOREIGN KEY (F2_ID) REFERENCES <table_name>(field_name>),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2));

missing right parenthesis problems

i didn't know what the problem is. First this one works fine in sql
create table Department
(Department_Id number(8) PRIMARY KEY ,
Dept_Name varchar(20),
Location varchar(20));
but the second one says missing right parenthesis ora-00907
create table Instructor(Instructor_ID number(8) PRIMARY KEY ,
Department_Id number(8) FOREIGN KEY REFERENCES Department(Department_Id) ,
Ins_name varchar2(20) ,
Position varchar(20) ,
email_Id varchar (40),
Contact_No number(10),
Date_Of_Joining date);
Leave out the FOREIGN KEY part:
Department_Id number(8) REFERENCES Department(Department_Id) ,
See Oracle FAQs for an example. The FOREIGN KEY keywords are for out-of-line constraints, which go after the column definitions.

Can two or more tables which share same foreign keys share a constraint on that foreign key?

What is the problem with this code??
It gives error "name already used by another constraint". Also if I can't define same constraint in different tables then is there any way I can reuse the previously defined constraint?
Any insight??
CREATE TABLE tbl_formats
(
format_id NUMBER(5),
format_name VARCHAR2(50),
format_desc VARCHAR2(100),
valid_from DATE,
valid_to DATE,
format_type VARCHAR2(50),
CONSTRAINT pk_format_id PRIMARY KEY(format_id)
);
CREATE TABLE tbl_format_detail
(
id NUMBER(10),
format_id NUMBER(5),
src_field VARCHAR2(200),
target_field VARCHAR2(100),
business_rule VARCHAR2(4000),
expression VARCHAR2(4000),
target_segment VARCHAR2(4),
CONSTRAINT pk_id PRIMARY KEY(id),
CONSTRAINT fk_format_id FOREIGN KEY(format_id) REFERENCES tbl_formats(format_id)
);
CREATE TABLE tbl_client_formats
(
client_format_id NUMBER(10),
format_id NUMBER(5),
client_id NUMBER(5),
CONSTRAINT pk_client_format_id PRIMARY KEY(client_format_id),
CONSTRAINT fk_format_id FOREIGN KEY(format_id) REFERENCES tbl_formats(format_id),
CONSTRAINT fk_client_id FOREIGN KEY(client_id) REFERENCES tbl_clients(client_id)
);
It seem like the foreign key constraint 'fk_format_id' defined in the table 'tbl_client_formats' conflicts with the same constraint already defined in the table 'tbl_format_detail'.
I am new to oracle so explain even the obvious things please.
The problem is that you're trying to use the same constraint name twice.
Just use a different name for your second constraint (e.g. fk_client_formats_format_id), and you should be fine.
Generally, I'd recommend using the table name as part of the constraint name, to avoid name clashes (if the constraint name gets too long, you'll have to use some kind of abbreviation scheme).
Foreign key are stored in a database range, not a table range. You cannot have two FK with the same name on the same database, even if they are not in the same table. You could name your FK that way:
FK_PARENT_CHILD_FIELD
ex:
FK_FORMATDETAILS_FORMATS_ID,
FK_CLIENTFORMATS_FORMATS_ID,
FK_CLIENTFORMATS_CLIENT_ID

Resources