There are two tables in my MySQL database which have a many-to-many relationship. There is a third table which handles it, with the foreign keys of the first two.
I need to update the relationship. I may have to add a row with a new relation and delete a row that represents a relation that does not exist any more. To take track of the changes, I created a new table that contains all the relations that are valid, and does not contain the old ones that are meant to be deleted.
There is a lot of content on this MERGE statement for SQL, which would solve my problem:
https://www.sqlshack.com/sql-server-merge-statement-overview-and-examples/
https://codingsight.com/merge-updating-source-and-target-tables-located-on-separate-servers/
https://www.sqlservertutorial.net/sql-server-basics/sql-server-merge/
https://www.educba.com/mysql-merge/
The problem is that for some unclear reason MERGE does not exist in MySQL. It kinda has an alternative, called INSERT ... ON DUPLICATE KEY UPDATE, but it is not the same and does not cover what I am aiming here. I don't want to delete all the relations on the table and re-insert the new ones.
I would like to know if there is any other alternative to MERGEin MySQL, or any way to "add" it to my database.
Related
I have created a database and created 10 tables. The master table have Id column common in all the other tables. I have connected these tables with one to many relationship and vice versa. Now I need to delete a single record from the master table by giving the ID. Does it automatically delete the records in other tables or I have to specify functions for deleting the records in other tables associated with the ID.Please clarify.All these process are done using Java hibernate concept. Thanks in advance.
Does it automatically delete the records in other tables
It depends on how you've specified the foreign keys to MySQL. You get several choices. Which one did you make? If you made the choice that allows deletions, it deletes. If you made the choice that prevents it, the deletion doesn't happen at all, not even from the master.
I have a MySql database containing data about users of an application. This application is in production already, however improvements are added every day. The last improvement I've made changed the way data is collected and inserted into the database.
Just to be clearer, my database is composed of 5 tables containing user data and 1 table to relate all the tables, through foreign keys. These 5 foreign keys, together, form my Unique Index for this "Main Table" I have.
The issue is that one of these tables containing user data changed its format, and I want to remove all the data older than the modification I made on my application (just from this table, the other ones I need to keep untouched). However, this dataset has foreign keys in the main table, and I can't just drop these lines on the main table because the other informations I have are important. I tried to change the value of the foreign key for this table, in specific, but then, obviously, I have a problem related to duplicated indexes.
Reading on internet, I've found a solution to my problem using "Insert ... On duplicate key update ...", but i'm not inserting data, just updating it. I have an Idea about how to make a program on PHP to update my database, but is there another easier solution? Is it possible to avoid these problems using just MySql syntax?
might be worth looking at the below link
http://www.kavoir.com/2009/05/mysql-insert-if-doesnt-exist-otherwise-update-the-existing-row.html
I am using a MySQL database. In my relational data model, I've got two entities that relate 1:1 to each other. In my schema, a 1:1 relation is set up by putting a FK field in one of the two tables, that relates to the PK of the other table. Both tables have PKs and they are both auto increment BIGINTs.
I am wondering whether it would be possible to have an ON DELETE CASCADE behaviour on them that works both ways.
i.e. A 1:1 B, means that [ deleting A also deletes B ] as well as [ deleting B also deletes A ].
I realise that this may not be absolutely necessary in terms of proper application design, but I am just wondering whether it is actually possible. As far as I recall, you can't put an FK constraint on a PK.
It'd be impossible to insert such records if you have a 2-way relationship enforced. Chicken-and-egg. Record in table #1 can't be inserted because there's no matching record in table #2, and table #2 cannot be inserted into because there's nothing in table #1 to hook to.
You can disable FK constraints temporarily (set foreign_key_checks = 0), but this should never be done in a "real" system. It's intended more for loading dumps where the table load order cannot be guaranteed.
I have some information that I am downloading via API into a SQL database. I am setting up cron to do this in the middle of the night. Sometimes new products are added or old ones are edited. I need to add new ones and update old ones if they exist. I am pretty sure it looks something like:
If (id exists){
update product
}else{
insert product
Is this the best way? What about just dropping then re-constructing it?
I would update the existing product, especially if the ID is an auto-number. But even if you have a surrogate key other than this ID, I'd still update existing products. In the future, your database may grow more complex and your products may get a couple of child tables. You don't want to reconstruct all of them.
Just update them.
You are looking for INSERT ... ON DUPLICATE UPDATE, i guess. See here.
This is a tough question to word...so bear with me.
I have two tables in my database, [Item] and [AssignedProperty]. [AssignedProperty] is the child in a parent-child relationship with [Item]. So it basically just has an ItemID field and a foreign key relationship utilizing it. [AssignedProperty] also has an identical relationship with another table named [Property], and its own primary key, AssignedPropertyID. This creates a nice many-to-many relationship. I have a constraint however, that dictates that one [AssignedProperty] cannot have duplicate occurrences of ItemID and PropertyID in the same record. Therefore an [Item] can only have one of each [Property].
This works nicely, but with LINQ-To-SQL, theres an issue in what I think is the order of execution that conflicts with this constraint.
To produce this error, I load up an [Item] in my application using LINQ-To-SQL. I then select an [AssignedProperty] object, from the item's [AssignedProperties] collection and delete it. I then create a new [AssignedProperty] object with the exact same ItemID and PropertyID as the one I just deleted, and add it back to the collection. When I call [SubmitChanges] on the DataContext, it will flag me on the constraint. If the old [AssignedProperty] record was deleted first, then there is no reason why the new one should produce that error. It looks like it's attempting to insert the new [AssignedProperty] before deleting the old one.
How should I fix this, and in the database or my app? TIA
The 94% is bugging me, and the answer is actually in the comments of the original post.
So, this is just so I can actually select an answer.