Is it possible to have a foreign key (InnoDB) reference two possible tables?
If not, is there a workaround for this?
There are two different subjects that contain the same field (interface_id). A third table references this field.
An example being:
physical_interface ( id, name, etc )
virtual_interface ( id, name, etc )
usage ( interface_id, etc )
I had an idea of using a view, but came across this related to SQL server: Can I have a foreign key referencing a column in a view in SQL Server? so it seems you cannot use views in foreign keys.
The alternative, I suppose, would be storing all intefaces in one table but I feel that would be less organized.
Well, the answer is interesting. MySQL 5.6.14, my version, won't stop you from creating a foreign key relationship with 2 tables but that is a bad idea. Let me show you why.
Create 2 master tables: test1 and test3
create table test1 (field1 int, primary key (field1));
create table test3 (field1 int, primary key (field1));
Create a dependent table that will have FK relationship with field1 of both tables.
create table test2 (
field0 int,
field1 int,
constraint fk_test2_test1
foreign key (field1)
references test1 (field1),
constraint fk_test2_test3
foreign key (field1)
references test3 (field1)
);
Great. Now let's add some data and see the issue:
insert into test1 values (1);
insert into test3 values (3);
Master tables are done. Let's add data to dependent table.
insert into test2 values (100, 1);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`test2`, CONSTRAINT `fk_test2_test3` FOREIGN KEY (`field1`) REFERENCES `test3` (`field1`))
insert into test2 values (100, 3);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`test2`, CONSTRAINT `fk_test2_test1` FOREIGN KEY (`field1`) REFERENCES `test1` (`field1`))
insert into test2 values (100, 2);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`test2`, CONSTRAINT `fk_test2_test1` FOREIGN KEY (`field1`) REFERENCES `test1` (`field1`))
So...don't do that.
Proposal
create table interface_type (
id int auto_increment,
type_name varchar(30),
primary key (id)
);
create table interface (
id int auto_increment,
interface_name varchar(30),
interface_type int,
primary key (id),
constraint fk_interface_type
foreign key (interface_type)
references interface_type (id)
);
create table `usage` (
id int auto_increment,
interface_id int,
primary key (id),
constraint fk_usage_interface
foreign key (interface_id)
references interface (id)
);
Now you can add as many types of interfaces in the interface_type table such as:
insert into interface_type (type_name) values ('physical'), ('virtual');
Then, add as many interfaces as you'd like in the interface table, and reference them in the usage table as needed.
Related
I am doing on these two tables, which is accountinfo and userrecipeinfo
accountinfo
create table accountinfo
(
id int NOT NULL AUTO_INCREMENT,
username varchar(80),
password varchar(80),
name varchar(80),
primary key (id)
);
userrecipeinfo
create table userrecipeinfo
(
recipeid int NOT NULL AUTO_INCREMENT,
recipename varchar(80),
reciperating int,
recipephoto LONGTEXT,
primary key (recipeid),
foreign key (recipeid) references accountinfo(id)
);
However, when I try to insert one of the values
insert into userrecipeinfo (recipename, reciperating, recipephoto)
values ('tom yum', 4, 'https://www.cbronline.com/wp-content/uploads/2016/07/Trolling.jpg');
I get an error:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (userinfo.userrecipeinfo, CONSTRAINT userrecipeinfo_ibfk_1 FOREIGN KEY (recipeid) REFERENCES accountinfo (id))
I would like to know what caused this error. Thanks!
You are violating the foreign key constraint. When a foreign key is added it must match a existing value in the reference table.
In your case userrecipeinfo.recipeid refers to accountinfo.id, so when you insert a row into userrecipeinfo, there must be a corresponding row in accountinfo which matches accountinfo.id = userrecipeinfo.recipeid.
You can try select * from accountinfo where id=4; to see whether there is a row whose id is 4.
Below is the simplified picture of the relationships I have in my DB:
create table attribute (id int auto_increment, primary key (id));
create table state_sample (id int auto_increment, primary key(id));
create table state_sample_attribute (
state_sample_id int,
attribute_id int,
primary key(state_sample_id, attribute_id),
foreign key (state_sample_id) references state_sample(id) on update cascade,
foreign key (attribute_id) references attribute(id) on update cascade
);
create table note (
id int auto_increment,
state_sample_id int,
attribute_id int,
primary key(id),
foreign key (state_sample_id) references state_sample(id) on update cascade,
foreign key (state_sample_id, attribute_id)
references state_sample_attribute(state_sample_id, attribute_id) on update cascade
);
insert into attribute values (1);
insert into state_sample values (1);
insert into state_sample_attribute values (1, 1);
insert into note values (1, 1, 1);
Whenever I try to update the ss table, it fails:
update state_sample set id = 2;
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`demotemplate`.`note`, CONSTRAINT `note_ibfk_1` FOREIGN KEY (`ss_id`) REFERENCES `ss` (`id`) ON UPDATE CASCADE)
As far as I understand, this is what happens:
It tries to set state_sample.id = 2.
It sees the cascade to note and tries to update note.state_sample_id.
However, note.state_sample_id is also involved in the foreign key to to state_sample_attribute(state_sample_id, attribute_id), so it goes to check whether that's still valid.
As state_sample_attribute.state_sample_id has not yet been updated, the constraint fails.
Is my assumption correct? And if so, is there a way to work this around?
Give the ssa table its own id primary key, and use that in the foreign key in notes, rather than referencing the ss_id and a_id columns.
create table ssa (
id int auto_increment,
ss_id int,
a_id int,
primary key (id),
unique key (ss_id, a_id),
foreign key (ss_id) references ss(id) on update cascade,
foreign key (a_id) references a(id) on update cascade);
create table note (
id int auto_increment,
ss_id int,
ssa_id int,
primary key(id),
foreign key (ss_id) references ss(id) on update cascade,
foreign key (ssa_id) references ssa(id) on update cascade);
Now you don't have the redundant dependency.
It's also not clear that note needs ss_id at all, since it's redundant with the related ssa row.
Try
DISABLE KEYS
or
SET FOREIGN_KEY_CHECKS=0;
make sure to turn it on
SET FOREIGN_KEY_CHECKS=1;
after.
What solved it in the end is dropping the extra FK:
alter table note drop foreign key (state_sample_id) references state_sample(id);
I am getting this error:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (university.registration, CONSTRAINT registration_ibfk_2 FOREIGN KEY (section_id) REFERENCES Section (section_id))
This is my code
INSERT INTO Registration VALUES (24766, 1102, 'B', 'B');
CREATE TABLE Registration (
student_id INT,
section_id INT,
midterm_grade VARCHAR(5),
final_grade VARCHAR(5),
PRIMARY KEY (student_id, section_id),
FOREIGN KEY (student_id)
REFERENCES Student (student_id),
FOREIGN KEY (section_id)
REFERENCES Section (section_id)
);
Any help would be appreciated on fixing this problem.
This is a common error in MySQL, most likey caused by either that student_id 24766 does not exist in the Student table, or section_id 1102 does not exist in the Section table.
The fix is to simply make sure that your foreign keys in the Registration table point to actual primary keys of records in the other two tables. So, you may need to insert some data to resolve this error.
I have created the following tables:
A3STUDENT
CREATE TABLE A3STUNDENT(
STD_ID INTEGER NOT NULL,
STD_NAME VARCHAR(30),
STD_MAJOR CHAR(4),
STD_RANK CHAR(2),
CONSTRAINT PK_A3STUDENT PRIMARY KEY (STD_ID)
);
CREATE TABLE A3COURSE(
CRS_TIME VARCHAR(10),
CRS_ROOM CHAR(5),
CRS_ID CHAR(7) NOT NULL,
CONSTRAINT PK_A3COURSE PRIMARY KEY (CRS_ID)
);
CREATE TABLE A3ENROLL(
ENR_GRADE CHAR(1),
STD_ID INTEGER NOT NULL,
CRS_ID CHAR(7) NOT NULL,
CONSTRAINT PK_A3ENROLL PRIMARY KEY (STD_ID, CRS_ID),
CONSTRAINT FK_STD_ENR FOREIGN KEY (STD_ID) REFERENCES A3STUDENT(STD_ID),
CONSTRAINT FK_CRS_ENR FOREIGN KEY (CRS_ID) REFERENCES A3COURSE(CRS_ID)
);
When I go to insert values such as this:
INSERT INTO A3ENROLL VALUES ('A', 100, 'MGMT445');
I receive this error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (Hess.A3ENROLL, CONSTRAINT FK_CRS_ENR FOREIGN KEY
(CRS_ID) REFERENCES A3COURSE (CRS_ID))
I have can't seem to understand why my data won't insert. What am I overlooking?
This usually happens because have not data in the A3STUNDENT andA3COURSE tables.
The foreign key relationships means that a primary table that contains the central data values and a child table with identical values pointing to the parent, any INSERT or UPDATE operation that attempts to create a foreign key value in a child table is rejected if there is no matching candidate key value in the parent table.
The table A3ENROLL is taking as reference100 and MGMT445 that does not exist in the tables mentioned above.
Before inserting data in the A3ENROLL table, you must be sure that there are data in the other tables A3STUNDENT and A3COURSE, because the tableA3ENROLL has a foreign key of A3STUNDENT andA3COURSE, this means that you must have these data in those tables, for example:
Select * from A3STUNDENT;
STD_ID | STD_NAME | STD_MAJOR | STD_RANK
100 | 'Zack' | ... | ....
Select * from A3COURSE;
CRS_ID | CRS_ROOM | CRS_TIME
MGMT445 | ... | ....
You can try to insert data in the previous tables and then insert in the table A3ENROLL.
Here you have more info: https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
I have 2 tables here for example table A and table B, how can i insert data when both of the tables consist of foreign key of each other? like table A got an attribute is foreign key references of table B, and table B got an attribute is foreign key references of table A
create table abc
(ID varchar(10),
subID varchar(10),
primary key (ID),
foreign key (subID) references def(SubID)
)
create table def
(SubID varchar(10),
ID varchar(10),
primary key (SubID),
foreign key (ID) references abc(ID)
)
somehow like this(i skipped other various informations)
I don't think it will be possible in your current design.
If you really have the need to do insert with cross dependency over the two tables, remove the one foreign key. Then you can do an insert on table ABC and then DEF.
I also think your DB design is incorrect.