PDO deleting records from multiple tables using joins - php

i want to delete a table row with a photo table. there is also a table called photo_translate where i store my alt text in different languages. is this possible using joins?
$query=$db->prepare("DELETE FROM photo,photo_tranlate INNER JOIN photo_translate on photo.id=photo_translate.rec_id WHERE photo.rec_id=? and photo.page=?" );
$query->bindvalue(1,$rec_id);
$query->bindvalue(2,$page_id);
$query->execute();

You should set a foreign key with "on delete cascade" on the field photo_translate.rec_id
This way when you delte the record in the "photo" table, the corrisponding record in the "photo_translate" table will be deleted automatically.
This is the correct way to handle this situation

Related

Laravel: How to delete rows from multiple table with same id with only 1 query?

I have this code to delete data from multiple tables in one go:
DB::table('tb_stikes_register_school')->where('register_id', $_POST['id'])->delete();
DB::table('tb_stikes_register_guardian')->where('register_id', $_POST['id'])->delete();
DB::table('tb_stikes_register_student')->where('register_id', $_POST['id'])->delete();
I'm trying to shorten this into 1 query only, register_id from guardian and school tables is the foreign key of student table. I've been trying to use join but only student table record is deleted. Is there any workaround this?
Something like this maybe - haven't tested
DB::table(DB::raw('FROM tb_stikes_register_school, tb_stikes_register_guardian, tb_stikes_register_student'))
->join(ENTER JOIN INFO) // wasn't clear how your tables were related
->where('register_id', $_POST['id'])
->delete();
Or you could use a fully raw query:
DB::query('SQL statement here');
Basically recreating something similar to this: delete rows from multiple tables

Use one table to update another SQL

I have having problems updating one table from another. I want SQL to update the rows in Employees from the data in CompanyEmployees where the two EmployeeNum fields are the same. Also if an EmployeeNum exists inside of CompanyEmployees that doesn't match one in Employees then I need a new row created in Employees
I so far have tried a join for the two tables.
SELECT Employees.PhoneNum, Employees.Data,
CompanyEmployees.PhoneNum, CompanyEmployees.SystemData
FROM CompanyEmployees
INNER JOIN Employees
ON CompanyEmployees.Employees=Techs.EmployeeNum
I get the right column data in both tables but i doesnt update Employees. Do I need an INSERT or UPDATE somewhere?
How can I insert the whole row of data from CompanyEmployees into Employees where CompanyEmployees.EmployeeNum doesn't exist in Employees?
I need to do this because CompanyEmployees is only a phone directory and Employees has phone numbers and more information. But CompanyEmployees has new hires inside it that are not inside Employees.
Remember RDBMS has the R for relational. They are built for relationships.
To update you Employees table with the CompanyEmployees table you could use something like the below:
INSERT INTO Employees (columns) VALUES (SELECT columns FROM CompanyEmployees) ON DUPLICATE KEY UPDATE
You probably do not even need 'Employees' as it is a superset of 'CompanyEmployees'. I would suggest looking to merge the two tables or at least move the data into a single table.

Set column to automatically pull data from referenced table

I have one column called 'speeding' in a table containing many other columns. This column contains an integer that is foreign key to an entry on a table named Speeding. This table has columns 1-25 and an id that is referenced by the 'speeding' column from the first table.
Besides using join, is there any setting I can set on 'speeding' to make it automatically pull the associated data from the table Speeding?
You could create a view, a view is basically a SQL statement that is stored on the MySQL server and acts like a table
CREATE VIEW ViewName AS
SELECT tbl1.data, tbl2.speeding
FROM tbl1
INNER JOIN tbl2 ON tbl2.key = tbl1.key;
http://dev.mysql.com/doc/refman/5.0/en/create-view.html
You then use the view as you would use any table
SELECT data, speeding
FROM ViewName

How do I delete a row from one table, when no more rows exist in another

I would like to know the best way to check one table of data, and when no more rows exist matching the WHERE clause, delete a row from another table.
I have tried myself but it has become too cumbersome with 6 queries and nested if/else, and it doesn't work to top it off.
I have never used SQL join's before, so examples will help me to understand responses.
I have a table of devices, there is a master table with a device and a password.
There is a second table containing the multiple rows of the device in the above table, and a series of serial numbers.
When the second table no longer contains any of the serial numbers listed in the master table, I want the row containing the device and password from the master table.
If you mean like when you have a table customer and a table order, delete the customers if they have no orders? Then you can use subselect:
delete from customer where customerid not in (select customerid from order)
You coult make a DELETE statement like
DELETE FROM masterTable WHERE ID NOT IN (SELECT masterTableID FROM secondaryTable)
This would delete all the rows from the master-table which don't have any references in the second table. That also means it would not delete only one row, but all of the matching ones. The only necessary thing you need is that every row in the second table references to the master table.
DELETE table_devices
FROM table_devices
left JOIN serial ON table_devices.id= serial.device_id
WHERE serial.device_id is null

Can this mySQL query be optimized better? Not a mySQL guru but I think I got it right

Basically, I have two tables that have a 1 to 1 relation.
When I delete a row from subset_panel_options I also want to delete the related row in subset_panel_options_list
Here is some of structure for the two tables. There is no need to show the full table.
[subset_panel_options]
->id
->subset_panel_id
[subset_panel_options_list]
->id
->subset_panel_options_id
DELETE subset_panel_options, subset_panel_options_list
FROM subset_panel_options
JOIN subset_panel_options_list
WHERE subset_panel_options.id = subset_panel_options_list.subset_panel_options_id
AND subset_panel_id = $subsetPanelId
Yes, it can:
DELETE FROM subset_panel_options
LEFT JOIN subset_panel_options_list
ON (subset_panel_options.id = subset_panel_options_list.subset_panel_options_id)
WHERE subset_panel_options.subset_panel_id = $subsetPanelId
Using LEFT JOIN you ensure you are deleting from "subset_panel_options" even if there is no corresponding match in the subset_panel_options_list table.
You may also want use referential integrity features available in InnoDB engine. In this case, you need to define subset_panel_options_id as a FK (foreign key) in the subset_panel_options_list table, and an "ON DELETE CASCADE" constraint on it, meaning that when rows at subset_panel_options are deleted, the "orphan" rows in subset_panel_options_list should be immediately deleted too.

Categories