I'm brand new to using Oracle SQLDeveloper and I'm working on a college project right now. I keep trying to add foreign key constraints to my tables(which already hold the foreign key as an attribute) so Im using ALTER like this:
alter table applies
add constraint e_number foreign key (e_number)
references student (e_number);
where e_number is the primary key in a table called student. The student table's e_number has the primary key constraint and also has an index that was auto-generated where it says UNIQUE under the UNIQUENESS column in the indexes tab. Whenever I try and create a foreign key for any of my tables I'm getting this same error everytime:
Error starting at line : 1 in command -
alter table applies
add constraint e_number foreign key (e_number)
references student (e_number)
Error report -
SQL Error: ORA-02264: name already used by an existing constraint
02264. 00000 - "name already used by an existing constraint"
*Cause: The specified constraint name has to be unique.
*Action: Specify a unique constraint name for the constraint.
I'm a bit confused and have been reading about unique on several sites but still don't get it. When I call an ALTER I can either specify a FOREIGN key or specify a UNIQUE key. Am I supposed to ALTER unique and then ALTER foreign? What am I doing wrong?
It's because you already have a key named e_number. Try:
alter table applies
add constraint applies_student_e_number foreign key (e_number)
references student (e_number);
Related
I need to add a Foreign key to my Date_Reserved attribute in the Additional_extra Table, (Maybe because the Date type can't be unique?
I keep getting the next error:
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
For The next Code:
ALTER TABLE Additional_Extra
Add(
CONSTRAINT test_date
FOREIGN KEY(Date_Reserved)
REFERENCES Reservation(Date_Reserved)
);
This is the part of the task that I need to create. So it is given that Date_Reserved must be FK:
Any Suggestions on what is wrong?
I tried to add FK when creating the Table, however when I find out that just the Date_Reserved FK line is wrong I created without that, but the error is still the same.
I tried from the GUI to add a Foreign key but same error
date_reserved is part of a composite primary key consisting of many columns; there is no unique or primary key that is solely on the date_reserved column.
Your constraint:
ALTER TABLE Additional_Extra
Add(
CONSTRAINT test_date
FOREIGN KEY(Date_Reserved)
REFERENCES Reservation(Date_Reserved)
);
Is trying to refer to a unique constraint that is solely on the date_reserved column and that does not exist so the SQL engine (correctly) raises the exception that such a constraint does not exist.
What you need to do is refer to the entire composite key:
ALTER TABLE Additional_Extra
Add(
CONSTRAINT test_date
FOREIGN KEY(Booking_Number, Room_Number, Date_Reserved)
REFERENCES Reservation(Booking_Number, Room_Number, Date_Reserved)
);
fiddle
I need to create a table called LFM_Enroll in SQL that has a composite primary key of Student_ID and Section_Number. Student_ID is also a foreign key, it references Student_ID in the LFM_Student table and Section_Number is also a foreign key, it references Section_Number in the LFM_Section table. How do I write the constraints and foreign keys? I've attached an image of the tables and below is what I have done so far. After the LFM_Enroll table is created I need to update one row. I tried doing so but kept getting the below error.
: Error starting at line : 173 in command -
UPDATE LFM_Enroll
SET Student_ID = 1234567,
Section_Number = 01234
WHERE Student_ID = 900000 AND Section_Number = 4138
Error report -
ORA-02291: integrity constraint (SYSTEM.FK_LFM_ENROLL_SECTION_NUMBER) violated - parent key not found.
Tables Thanks in advance for all your help.
CREATE TABLE LFM_Enroll (
Student_ID char(7),
Section_Number char(4),
constraint PK_LFM_Enroll Primary Key (Student_ID,Section_Number),
constraint FK_LFM_Enroll_Student_ID
Foreign Key (Student_ID,Section_Number) references LFM_Student (Student_ID),
constraint FK_LFM_Enroll_Section_Number
Foreign Key (Student_ID,Section_Number) references LFM_Section (Section_Number)
);
Your foreign key constraints are not right. You are trying to map two columns {Student_ID,Section_Number} to a single column LFM_Student.Student_ID.
The number of columns in the principal key must match the number of columns in the foreign key. In other words, the key LFM_Student is one column (Student_ID), so the foreign key also needs to be a single matching column - in this case LFM_Enroll.Student_ID. Correct DDL would be:
constraint FK_LFM_Enroll_Student_ID
Foreign Key (Student_ID) references LFM_Student (Student_ID),
constraint FK_LFM_Enroll_Section_Number
Foreign Key (Section_Number) references LFM_Section (Section_Number)
I'm not quite sure why your RDBMS is allowing what you have, but it may be using the first column and simply ignoring the second. In which case FK_LFM_Enroll_Section_Number is creating a foreign key LFM_Enroll.Student_ID => LFM_Section.Section_Number.
The error indicates that the values with which you are trying to update the two columns may not exist in Student and / or Sections tables i.e. 1234567 doesn't exists in the student table and / or 01234 doesn't exist in your section table . You should try inserting new rows or updating existing ones with the new values you are trying to update your foreign keys with.
[Edit: For defining constraints refer lc.'s post]
I've been looking over the following SQL code for awhile and just can't seem to find the problem. I'm relatively new to SQL, so I'm sure it's just something I'm overlooking. The error message I get is: ORA-01735: Invalid ALTER TABLE option.
Code:
ALTER TABLE PATIENT
(
ADD CONSTRAINT PProfileForeignKey
FOREIGN KEY (pProfileID) REFERENCES PATIENT_PROFILE(Profile_ID),
ADD CONSTRAINT InsForeignKey
FOREIGN KEY (pInsID) REFERENCES INSURANCE(Insurance_ID)
ON DELETE SET NULL
);
I have triple checked to make sure the foreign key column names and the referenced column names are correct.
seems The parentheses are in wrong place
ALTER TABLE PATIENT
ADD (CONSTRAINT PProfileForeignKey
FOREIGN KEY (pProfileID) REFERENCES PATIENT_PROFILE(Profile_ID),
CONSTRAINT InsForeignKey
FOREIGN KEY (pInsID) REFERENCES INSURANCE(Insurance_ID)
ON DELETE SET NULL);
I am trying to add a foreign key to my table but i am getting this error,
ERROR at line 3: ORA-00904: "DEDUCID": invalid identifier
ALTER TABLE pr_cust
ADD CONSTRAINT deduc_fk
FOREIGN KEY (deducid)
REFERENCES pr_deduc;
I have this other table named pr_deduc that has a column named deducid, that is a char with one value as my primary key. I have it spelled corrected, unless i am missing something.
The deducid you mention has to be a column on pr_cust, and you are not referencing the column in the other table. The propper syntax is:
ALTER TABLE pr_cust
ADD CONSTRAINT deduc_fk
FOREIGN KEY (deducid)
REFERENCES pr_deduc(deducid);
ALTER TABLE pr_cust
ADD CONSTRAINT deduc_fk
FOREIGN KEY (deducid)
REFERENCES pr_deduc(deducid);
I'm trying to add a primary and foreign key to a nested table, struggling to know how.
This is what I have;
create or replace type profile as object
(
id VARCHAR2(10), --- Suppose to be Primary Key
userID VARCHAR2(10) --- Suppose to be Foreign Key for user table
);
create or replace type profile_nest as table of profile;
CREATE OR REPLACE TYPE user_t UNDER group_T
(profile profile_nest_ty,);
CREATE TABLE user OF user_t
(id NOT NULL,
PRIMARY KEY (id),
nested table profile store as profile_storage_tbl;
Now the problem is this part, trying to do a foreign key -
alter table profile_storage_tbl add CONSTRAINT fk_userID FOREIGN KEY (userID)
REFERENCES user(id);
Gives this error -
*Error starting at line 3 in command:
alter table profile_storage_tbl add CONSTRAINT fk_userID FOREIGN KEY (userID)
REFERENCES user(id)
Error report:
SQL Error: ORA-30730: referential constraint not allowed on nested table column
30730. 00000 - "referential constraint not allowed on nested table column"
*Cause: An attempt was made to define a referential constraint on a nested
table column.
Action: Do not specify referential constraints on nested table columns.
Either you create 2 separate tables profile_storage_tbl and user with a foreign key between them or you create profile_storage_tbl as a nested table within the user table. It doesn't make sense to try to do both. (In fact nested tables make little sense to me, period - but that's another matter!)
It is just as the exception text says, creating a foreign key constraint on nested table columns is not allowed (Oracle 11).
There is sort of a workaround described here: http://ksun-oracle.blogspot.com/2011/05/foreign-key-on-nested-table.html. But there is no guarantee, that this would work on the next oracle release.
Behind the scene oracle will create two tables profile_storage_tbl and user whereas profile_storage_tbl has a foreign key on user.
You can do that on your own, with the advatage to have better control over the releations (also to other tables).