I'm trying to add a constraint to my two tables, but I can't remember what the correct syntax is. Below are the two tables,
Is it.........
ALTER TABLE dispatch ADD CONSTRAINT fk_productlines FOREIGN KEY
(productlines_fkid) REFERENCES productlines(fkid)
Is that right?
When I try
ALTER TABLE dispatch ADD CONSTRAINT fk_productlines FOREIGN KEY
(fkid) REFERENCES productlines(fkid)
I get the following message:
#1072 - Key column 'fkid' doesn't exist in table
Is productlines.fkid a primary key ?
To make it a foreign key in dispatch it must be a primary key of productlines
To add a foreign key in dispatch :
ALTER TABLE dispatch
ADD CONSTRAINT fk_productlines
FOREIGN KEY (id) REFERENCES productlines(fkid)
ALTER TABLE product_lines ADD CONSTRAINT fk_productlines FOREIGN KEY
(fkid) REFERENCES dispatch(ID)
That should work.. Try it yourself!
Related
ALTER TABLE `tbl_acc_payable` ADD CONSTRAINT `FK_APAY_SGS` FOREIGN KEY (`shiftid`, `groupid`, `sectionid`) REFERENCES `tbl_academic`(`id`, `id`, `id`) ON DELETE SET NULL ON UPDATE CASCADE
why I cannot apply multiple columns references another table multiple column in MySQL database?
return Error Message:
#1005 - Can't create table fastpay.tbl_acc_payable (errno: 150 "Foreign key constraint is incorrectly formed") (Details…)
A composite foreign key should reference a composite primary key. As you can't create a primary key on triple ID column, you should create 3 foreign keys, each of them referencing the same primary key (on the tbl_academic.id column).
FK references used the whole expression value or its prefix. The value - not the column name.
You need to reference to the values in 3 different rows which cannot be provided by composite index. So create 3 separate foreign keys referred to the same index tbl_academic(id):
ALTER TABLE tbl_acc_payable
ADD CONSTRAINT FK_APAY_SGS_1
FOREIGN KEY (shiftid)
REFERENCES tbl_academic(id)
ON DELETE SET NULL ON UPDATE CASCADE,
ADD CONSTRAINT FK_APAY_SGS_2
FOREIGN KEY (groupid)
REFERENCES tbl_academic(id)
ON DELETE SET NULL ON UPDATE CASCADE,
ADD CONSTRAINT FK_APAY_SGS_3
FOREIGN KEY (sectionid)
REFERENCES tbl_academic(id)
ON DELETE SET NULL ON UPDATE CASCADE;
I am trying to add a foreign key constraint in my table. My table structure are:
table requisition
My next table where i want to add foreign key of requisition table
requisition_approval
When i try to add a foreign key constraint with following sql query:
ALTER TABLE `requisition_approval` ADD CONSTRAINT `requisition_id` FOREIGN KEY (`requisition_id`) REFERENCES `requisition`(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
It gives following error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`proprompt`.`#sql-34e8_3d7`, CONSTRAINT `requisition_id` FOREIGN KEY (`requisition_id`) REFERENCES `requisition` (`id`))
What is the problem here and how can i solve it?
Error is in the requisition_approval table more specificly in the requisition_id column. You inserted id 0 which does not references any id in requisition table. Change the value to 1 and this will work :)
So real quick what I did:
Realisted didn't need unique key but still foreign key
First needed to drop the foreign key
ALTER TABLE groups DROP FOREIGN KEY group_information_id
Then dropped the unique key with
ALTER TABLE groups DROP INDEX group_information_id
Now I want to add the foreign key again with
ALTER TABLE groups ADD CONSTRAINT group_information_id FOREIGN KEY (group_id) REFERENCES group_information.group_id
Get the error (this is tanslated sorry if i'm not accurate, tell me if you don't understand):
Can not add foreign key
Why is this happening, and how can I fix this so I can add a foreign key?
Engine innodb used
I have 4 sql tables.
create table general(regno int NOT NULL primary key);
create table company_information(cregno int NOT NULL primary key);
create table company_jobs (jcode int NOT NULL primary key, cregno int , foreign key(cregno) references company_information(cregno));
create table applied(cregno int ,jcode int, regno int, foreign key(regno) references general(regno), foreign key(jcode) references company_jobs(jcode));
All i need to do is delete from table company_jobs when table applied has some value. Actually, all the tables must have some value for table applied to have some value as you can see from the structure of tables.
I used these commands to add ON DELETE CASCADE Constraint:
alter table company_jobs add constraint fk_cregno13 foreign key(cregno) references company_information (cregno) on delete cascade;
alter table applied add constraint fk_jcode16 foreign key(jcode) references company_jobs(jcode) on delete cascade;
alter table applied add constraint fk_regno14 foreign key(regno) references general(regno) on delete cascade;
But it is not working unfortunately and i am getting this error when i am giving the following command.
mysql> delete from company_jobs;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign
key constrai nt fails (test.applied, CONSTRAINT applied_ibfk_2
FOREIGN KEY (jcode) RE FERENCES company_jobs (jcode))
Help me out please if anyone can. Thank you
When you created the applied table you created 2 foreign keys.
After that, you have added 2 other foreign keys to this table.
As you can see, the error references a foreign key named applied_ibfk_2 that's not the foreign key you added after the creation.
Thus, ad the moment you have 4 foreign key constraint on that table.
So, you have to drop the 2 foreign keys created on table creation (that have a predefined name) and everything will work
The 1st foreign key pointing from table applied to company_job doesn't have any cascade rule, so it simple blocks the delete from company_job;
See mysql dump bellow
ALTER TABLE `applied`
ADD CONSTRAINT `applied_ibfk_1` FOREIGN KEY (`regno`) REFERENCES `general` (`regno`),
ADD CONSTRAINT `applied_ibfk_2` FOREIGN KEY (`jcode`) REFERENCES `company_jobs` (`jcode`),
ADD CONSTRAINT `fk_jcode16` FOREIGN KEY (`jcode`) REFERENCES `company_jobs` (`jcode`) ON DELETE CASCADE,
ADD CONSTRAINT `fk_regno14` FOREIGN KEY (`regno`) REFERENCES `general` (`regno`) ON DELETE CASCADE;
You need to drop the foreign key first, or recreate the table without the 1st foreign key
ALTER TABLE 'applied' DROP CONTRAINT 'applied_ibfk_2';
I have the following two foreign keys:
CONSTRAINT `FK_rel_object-user_users` FOREIGN KEY (`User`) REFERENCES `_users` (`ID`),
CONSTRAINT `FK__rel_object-user__map_usernames` FOREIGN KEY (`User`) REFERENCES `_map_usernames` (`ID`)
How can I define, that the data should exist in _users OR _map_usernames instead of AND
No.
That is, you cannot create a foreign key constraint this way. You can however, use a foreign key without a foreign key constraint.
All a foreign key is, is the value of another table's (or another record in the same table) primary key, which can be used in joins. In fact, you could reference fields other than the primary key, if all you need is to use the value for joins.