Can't modify a table to add a foreign key - sql

I have 2 tables
Songs (SONGID, SONGNAME, ALBUM, ARRANGER, VOCALISTS, LYRCISTS)
Source_songs (Source_Song_Name, ID_ACTUALSONGNAME)
and I can't modify Songs to add a foreign key
ALTER TABLE SONGS
ADD CONSTRAINT Original_Song_Name
ADD FOREIGN KEY (Source_song_id)
REFERENCES Source_SongName (SOURCE_SONGNAME);
And I get this error. I know I'm spelling the column name right idk what to do.
Error:
SQL Error: ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"

Alter table Songs ADD CONSTRAINT FK_ID FOREIGN KEY(source_song_id) references ...
Use ADD keyword only once

What is so hard to understand? The syntax is references <table_name> ( <column_name> ). Your table name is not Source_SongName, it is Source_songs, and it doesn't have any column named SOURCE_SONGNAME. You are missing the second underscore _ in Source_Song_Name.
Besides, it is strange that you would have a foreign key on a column (Source_song_id) that doesn't exist in your table.
Or is all the information you provided made up?

Related

How to add a foreign key that is a reserved word in oracle

I am trying to alter a table in oracle SQL to add a foreign key to it which name is a reserved word ("Date" to be specific).
I tried using double quotations, single quotations, no quotations, and squared brackets but nothing seems to work.
My code goes as follows:
ALTER TABLE GAME ADD FOREIGN KEY ("Date") REFERENCES SCHEDULE("Date");
The error it shows with double quotations is: ORA-00904: "Date": invalid identifier
Any help is appreciated.
You almost certainly don't want to:
Name columns after reserved words.
Have to always use quoted identifiers (and the correct case) for those columns.
Only have unique dates in your schedule table.
However, if you really do then assuming you have the tables:
CREATE TABLE schedule ("Date" DATE);
CREATE TABLE game ("Date" DATE);
Then you can create a UNIQUE constraint on the schedule table which will be used as the "target" of the referential constraint.
ALTER TABLE SCHEDULE ADD CONSTRAINT schedule__date__u UNIQUE ("Date");
Then create the foreign key constraint on the game table:
ALTER TABLE GAME ADD CONSTRAINT game__date__fk FOREIGN KEY ("Date") REFERENCES SCHEDULE("Date");
Or, if you want the system name to name the constraint then your code works:
ALTER TABLE GAME ADD FOREIGN KEY ("Date") REFERENCES SCHEDULE("Date");
db<>fiddle here

While alter table adding foreign key getting error in SQL Server

I am trying to add a foreign key in SQL Server to an existing table, but I'm getting an error. Could any one please help me?
Note: objid is present in both table 1 & table 2
ALTER TABLE table1
ADD CONSTRAINT FK_41_PRICE_INST2PRICE_QTY
FOREIGN KEY (Table1 PRICE_INST2PRICE_QTY) REFERENCES table2(objid);
Error:
FK_40_PRICE_INST2PRICE_QTY. The conflict occurred in database "test", table "dbo.table2", column 'objid'.
Apart from the error in the syntax, I think there are some values in the objid column that doesn't exist in the PRICE_INST2PRICE_QTY table. You have to check the values between the two columns. This is why you are creating the foreign key to prevent such things.
The syntax should be something more like this:
ALTER TABLE table1
ADD CONSTRAINT FK_41_PRICE_INST2PRICE_QTY
FOREIGN KEY (PRICE_INST2PRICE_QTY) REFERENCES table2(objid);
You don't need to qualify the column name.
Do a quick check on you column 'objid' and don't write
FOREIGN KEY (Table1 PRICE_INST2PRICE_QTY) REFERENCES table2(objid);
It should be more like:
FOREIGN KEY (PRICE_INST2PRICE_QTY) REFERENCES table2(objid);
without table1, because you're working in table1.

SQLite3 database

Hello everyone i got this error while trying to create a table in my database, have checked my query carefully but could not understand where the problem is:
sqlite> CREATE TABLE Subreddit (subreddit VARCHAR(15) PRIMARY KEY,name_name VARCHAR(15), FOREIGN KEY(name_name) REFERENCES Name(name), subreddit_id VARCHAR(15) );
Error: near "subreddit_id": syntax error
Table constraints must come after all column definitions. You are declaring a column subreddit_id after you specify a foreign key constraint, which is invalid syntax.
Simply swap the definition of subreddit_id and the FOREIGN KEY definition.

Alter table & Add UNIQUE key results in an error

I have a table called Animal. AnimalId is the primary key & I wanted to set the column AnimalType_id as UNIQUE (I have an AnimalType table and need to set a foreign key here)
ALTER TABLE Animal
ADD UNIQUE Animal.AnimalType_id int
There already is data in both tables, because of that I can't drop the table.
This however results in an error:
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '.'.
See the documentation for how to add a table constraint.
ALTER TABLE Animal ADD CONSTRAINT UQ_Animal_AnimalTypeId UNIQUE (AnimalType_id)
It sounds like AnimalType_id is a foreign key so I just wanted to check you understood that by making this column unique, you're making the relationship one-one - you'll only be able to have one animal of each type.
Since you're getting an error adding the unique constraint, I'm going to suggest that you actually want a foreign key instead of a unique constraint:
ALTER TABLE Animal
ADD CONSTRAINT FK_Animal_AnimalType
FOREIGN KEY
(
AnimalType_id
)
REFERENCES AnimalType
(
id
)
I've had to guess at the name of the AnimalType table name and it's primary key column name - please change these if they are incorrect.
If you get into the habit of giving names to all objects (even constraints) that you create, you will have easier time later when you need to disable, drop, or alter the constraint:
ALTER TABLE Animal ADD CONSTRAINT UQ_Animal_Type UNIQUE (AnimalType_id)
It is also possible to get a more flexible constraint-like effect from creating a unique index.
I think you are trying to do this:
ALTER TABLE Animal
ADD COLUMN AnimalType_id int;
It seems the data in the column is not unique.
when you create a unique constraint on a column, you can not have any duplicate entries (you can have at most one null)
try this
ALTER TABLE table_name ADD CONSTRAINT UNIQUE (column_name)

SQL Error: ORA-02298: cannot validate (SYSTEM.AEROPUERTO_FK) - parent keys not found

I'm getting the following error in on Oracle SQL Developer:
Error starting at line 1 in command:
ALTER TABLE AEROPUERTO ADD CONSTRAINT AEROPUERTO_FK FOREIGN KEY (CODIGO_CIUDAD) REFERENCES CIUDAD(CODIGO)
Error report:
SQL Error: ORA-02298: cannot validate (SYSTEM.AEROPUERTO_FK) - parent keys not found
02298. 00000 - "cannot validate (%s.%s) - parent keys not found"
*Cause: an alter table validating constraint failed because the table has
child records.
*Action: Obvious
Why?
There are records in AEROPUERTO that point to records that do not exist in CIUDAD.
To find out which records of AEROPUERTO have that kind of issue:
select * from AEROPUERTO where CODIGO_CIUDAD not in (select CODIGO from CIUDAD)
If the result set is not empty, you do have orphanaged records. You'll need to add the missing CIUDAD records in order to create the AEROPUERTO_FK foreign key, or update all the erroneous AEROPUERTO.CODIGO_CIUDAD to null (if this is a nullable field, but you will lose the city information for those airport records).
remove primary key constraint from the column in which you want to add the foreign key