I have two separate tables already created and filled with info. The first table has a unique field called hostname. Each host name may have more than one record in the second table. I already created them and the hostname is inserted in both tables. Now, how can I connect them?
I’m using MySQL workbench. I have to create foriegn key in table2, and this key should be the hostname. Now if I want to insert in table2 before inserting into table1. Is this possible? i.e, my program run in a way that it inserts the information of table2 before table1. Is there any change in the insert statement? Do I have to specify anything about table1 when inserting in table2?
If you have a foreign key, it must always reference a valid record or be NULL. If you need to insert into
table2beforetable1, you must first insert thetable2record with the foreign-key column NULL, then insert thetable1record, and finally update thetable2record with the reference totable1.It’s an odd analogy, but think of it like building a house. If you build it on-site before laying the foundation, you have a problem. But, you can build it somewhere, then build the foundation, then move the house onto it.
Now, on a side note, I do have to question why you are inserting
table2beforetable1…