If I am using the following SQL command in SQL Server 2008 to update a table with a foreign key constraint:
ALTER TABLE Employees
ADD FOREIGN KEY (UserID)
REFERENCES ActiveDirectories(id)
UserID being my FK column in the Employees table. I'm trying to reference the UserID in my ActiveDirectories table. I receive this error:
Foreign key 'UserID' references invalid column 'UserID' in referencing
table 'Employees'.
Error indicates that there is no UserID column in your Employees table. Try adding the column first and then re-run the statement.
ALTER TABLE Employees
ADD CONSTRAINT FK_ActiveDirectories_UserID FOREIGN KEY (UserID)
REFERENCES ActiveDirectories(id);
Maybe you got your columns backwards??
ALTER TABLE Employees
ADD FOREIGN KEY (UserID) <-- this needs to be a column of the Employees table
REFERENCES ActiveDirectories(id) <-- this needs to be a column of the ActiveDirectories table
Could it be that the column is called ID in the Employees table, and UserID in the ActiveDirectories table?
Then your command should be:
ALTER TABLE Employees
ADD FOREIGN KEY (ID) <-- column in table "Employees"
REFERENCES ActiveDirectories(UserID) <-- column in table "ActiveDirectories"
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
In the future.
ALTER TABLE Employees
ADD UserID int;
ALTER TABLE Employees
ADD CONSTRAINT FK_ActiveDirectories_UserID FOREIGN KEY (UserID)
REFERENCES ActiveDirectories(id);
ALTER TABLE Faculty
WITH CHECK ADD CONSTRAINT FKFacultyBook
FOREIGN KEY FacId
REFERENCES Book Book_Id
ALTER TABLE Faculty
WITH CHECK ADD CONSTRAINT FKFacultyStudent
FOREIGN KEY FacId
REFERENCES Student StuId
way of foreign key creation correct for ActiveDirectories(id), i think the main mistake is you didn't mentioned primary key for id in ActiveDirectories table
If the table has already been created:
First do:
ALTER TABLE `table1_name` ADD UNIQUE( `column_name`);
Then:
ALTER TABLE `table1_name` ADD FOREIGN KEY (`column_name`) REFERENCES `table2_name`(`column_name`);
Related
So, I am applying foreign key constraint on a column in MySQL table.
What I noticed is that I am able to do that in two ways -
ALTER TABLE book ADD CONSTRAINT fk_code_id FOREIGN KEY(book_type) REFERENCES code(id);
and
ALTER TABLE book ADD FOREIGN KEY(book_type) REFERENCES code(id);
Why do we have two ways in place to achieve same thing?
Alter table with Constraint option for adding check constraints to MySQL database tables.
The add constraint function allows the user to add a constraint name and a constraint condition.
https://razorsql.com/features/mysql_add_constraint.html
And by this ALTER TABLE book ADD FOREIGN KEY(book_type) REFERENCES code(id) you just make fk between two tables.
create a FOREIGN KEY constraint on the "PersonID" column when the "Orders" table is already created, use the following SQL:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Orders
ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Orders
ADD CONSTRAINT FK_PersonOrder
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);
I have two tables, employee and tbl_feedback. What I wanted to do is to add id as a foreign key in tbl_feedback. I already have an id column on my tbl_feedback so I tried altering it using this:
ALTER TABLE tbl_feedback
ADD FOREIGN KEY (id) REFERENCES employee(id);
When I hit go, I only get "Cannot add foreign key constraint". Am I doing it wrong?
tbl_feedback
employee
Reference table must have a PK and that PK fields should not be NULL
So, first modify referenced table if required
For example,
ALTER TABLE TblReference
Alter column refid int NOT NULL
ALTER TABLE TblReference
ADD constraint PK_TblReference_RefId primary key (refid)
Then simply add the Foreign Key constraint
ALTER TABLE TblSource
ADD CONSTRAINT FK_TblSource_Id FOREIGN KEY (id)
REFERENCES TblReference (refid)
I haven't practised SQL in a while and I forgot how to add a foreign key to my table
mysql> alter table students
add foreign key fk_unit(unitid)
references unit(unitid)
on delete no action
on update cascade;
ERROR 1072 (42000): Key column 'unitid' doesn't exist in table
I'm wondering why this is the case? My unit table has a primary key called unitid, why does this keep happening?
Try this one it should work....
ALTER TABLE students
ADD CONSTRAINT FK_UnitId FOREIGN KEY (unitid)
REFERENCES unit(unitid);
Try this
ALTER TABLE Students
ADD FOREIGN KEY (unitid)
REFERENCES unit(unitid)
Your query is correct. Looks like field 'unitid' is missing from 'students' table or it has a different name.
I have a MySQL database with an existing (and not removable) cities table with a new reference to a table province by a field province_id.
When I run:
ALTER TABLE cities ADD province_id INTEGER NOT NULL;
ALTER TABLE cities ADD INDEX (province_id),
ADD FOREIGN KEY (province_id) REFERENCES provinces (id);
I got next error on the second query, adding the index and foreign key:
ALTER TABLE cities ADD INDEX (province_id), ADD FOREIGN KEY (province_id) REFERENCES provinces (id) Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`yanpytest`.`#sql-16d8_325`, CONSTRAINT `#sql-16d8_325_ibfk_3` FOREIGN KEY (`province_id`) REFERENCES `provinces` (`id`))
I donĀ“t understand why, and this is not working in my production environment, in development it worked (both MySQL).
This probably means that in the province_id column in the cities table you have some value(s) e.g. 123 which does not exist in the provinces table in the id column there. So the foreign key (FK) constraint cannot be enforced by MySQL and it fails on you.
You can find the violating records by running:
SELECT * FROM `cities` WHERE `province_id` NOT IN
(SELECT id FROM `provinces`)
I got two tables in my database: user and call. User exists of 3 fields: id, name, number and call : id, 'source', 'destination', 'referred', date.
I need to monitor calls in my app. The 3 ' ' fields above are actually userid numbers.
Now I'm wondering, can I make those 3 field foreign key elements of the id-field in table user?
Yes - you can ;-)
Just define all three foreign keys to refer to the id column in User.
Something alike should do the work:
ALTER TABLE call
ADD CONSTRAINT fk_call_source_user FOREIGN KEY (source)
REFERENCES user (id)
ALTER TABLE call
ADD CONSTRAINT fk_call_destination_user FOREIGN KEY (destination)
REFERENCES user (id)
ALTER TABLE call
ADD CONSTRAINT fk_call_referred_user FOREIGN KEY (referred)
REFERENCES user (id)
Alter Table call
ADD FOREIGN KEY (Sourceid) references Source(Id),
FOREIGN KEY (DesId) references Destination(Id)