sql-delete

Deleting from multiple tables with foreign constraints

老子叫甜甜 提交于 2019-12-04 15:52:28
问题 I am trying to delete from multiple tables. Here's what my tables look like A_has_B ---- B ---- C_has_B (many to many) (many to many) I am trying to delete all rows from A_has_B, B and C_has_B given the ID of a record in B. I am using MySQL with the innodb storage engine with foreign keys defined for A_has_B and C_has_B referencing the IDs in B. I am trying to perform my delete like so: DELETE A_has_B.*, C_has_B.*, B.* FROM A join B on (B.B_id = A.B_id) join C on (C.B_id = B.B_id) where B.B

Delete rows with Laravel query builder and LEFT JOIN

孤街醉人 提交于 2019-12-04 13:10:08
How to delete rows from multiple tables in one query (with left join). The query: DELETE `deadline`, `job` FROM `deadline` LEFT JOIN `job` .... So, I try it like this: DB::table('deadline', 'job') ->leftJoin('job', 'deadline.id', '=', 'job.deadline_id') ->where('deadline.id', $id) ->delete(); Seems that Laravel doesn't support delete from multiple tables with left join. Is there a supported way or workaround? It seems that my way is not possible. So, I did it like this. $q = 'DELETE deadline, job FROM deadline LEFT JOIN job ...where deadline.id = ?'; $status = \DB::delete($q, array($id));

sql server delete slowed drastically by indexes

核能气质少年 提交于 2019-12-04 12:50:40
问题 I am running an archive script which deletes rows from a large (~50m record DB) based on the date they were entered. The date field is the clustered index on the table, and thus what I'm applying my conditional statement to. I am running this delete in a while loop, trying anything from 1000 to 100,000 records in a batch. Regardless of batch size, it is surprisingly slow; something like 10,000 records getting deleted a minute. Looking at the execution plan, there is a lot of time spent on

Postgresql delete multiple rows from multiple tables

女生的网名这么多〃 提交于 2019-12-04 07:11:39
Consider 2 or more tables: users (id, firstname, lastname) orders (orderid, userid, orderdate, total) I wish to delete all users and their orders that match first name ' Sam '. In mysql, I usually do left join. In this example userid is unknown to us. What is the correct format of the query? Juan Carlos Oropeza http://www.postgresql.org/docs/current/static/sql-delete.html DELETE FROM orders o USING users u WHERE o.userid = u.id and u.firstname = 'Sam'; DELETE FROM users u WHERE u.firstname = 'Sam'; You can also create the table with ON delete cascade http://www.postgresql.org/docs/current

MySQL Before Delete trigger to avoid deleting multiple rows

两盒软妹~` 提交于 2019-12-04 04:59:26
问题 I am trying to avoid deletion of more than 1 row at a time in MySQL by using a BEFORE DELETE trigger. The sample table and trigger are as below. Table test: DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `a` int(11) NOT NULL, `b` int(11) NOT NULL, PRIMARY KEY (`id`)); INSERT INTO `test` (`id`, `a`, `b`) VALUES (1, 1, 2); INSERT INTO `test` (`id`, `a`, `b`) VALUES (2, 3, 4); Trigger: DELIMITER // DROP TRIGGER IF EXISTS prevent_multiple_deletion; CREATE

SQL DELETE - Maximum number of rows

北战南征 提交于 2019-12-04 04:52:54
What limit should be placed on the number of rows to delete in a SQL statement? We need to delete from 1 to several hundred thousand rows and need to apply some sort of best practise limit in order to not absolutely kill the SQL server or fill up the logs every time we empty a waste-basket. This question is not specific to any type of database. That's a very very broad question that basically boils down to "it depends". The factors that influence it include: What is your level of concurrency? A delete statement places an exclusive lock on affected rows. Depending on the databse engine, deleted

SELECT then immediately DELETE mysql record

旧城冷巷雨未停 提交于 2019-12-04 02:59:21
问题 I have a PHP script that runs a SELECT query then immediately deletes the record. There are multiple machines that are pinging the same php file and fetching data from the same table. Each remote machine is running on a cron job. My problem is that sometimes it is unable to delete fast enough since some of the machines ping at the exact same time. My question is, how can I SELECT a record from a database and have it deleted before the next machine grabs it. For right now I just added a short

DELETE all where MySQL foreign key constraint does not fail

余生长醉 提交于 2019-12-04 02:41:23
I am trying to delete a few records but am getting the following error: Cannot delete or update a parent row: a foreign key constraint fails The thing is, the foreign key constraint is failing for only 1 or 2 of my 100 records I wish to delete. I wish to write a query which deletes these 98-99 records, skipping the 1 or 2 which failed , which I can later manually inspect and delete/modify. Not stopping because of some single problematic record, but continuing with the others, ignoring that. Is there a neat way to do this ? You have to LEFT JOIN the referencing table and add a condition saying

Add Delete Button to PHP results table

戏子无情 提交于 2019-12-03 23:05:23
I have outputted the results of a MySQL table to an HTML table. In the last column, I want to add a delete option which calls another form and deletes the user. I can't seem to get it to work though. This is my code for the results page: <?php $contacts = mysql_query(" SELECT * FROM contacts ORDER BY ID ASC") or die( mysql_error() ); // If results if( mysql_num_rows( $contacts ) > 0 ) ?> <table id="contact-list"> <thead> <tr> <th>Name</th> <th>Email</th> <th>Telephone</th> <th>Address</th> <th>Delete</th> </tr> </thead> <tbody> <?php while( $contact = mysql_fetch_array( $contacts ) ) : ?> <tr>

Delete multiple rows in MYSQL with info from python list

纵然是瞬间 提交于 2019-12-03 20:40:33
If list LL: LL = ['foo', bar', 'noo', 'boo',] is in a MySQL table, test in column ID with other ID's. I could use the following to delete all rows with ID's in LL: csr.execute("""DELETE FROM test.test WHERE ID = "Foo"; """) csr.execute("""DELETE FROM test.test WHERE ID = "bar"; """) csr.execute("""DELETE FROM test.test WHERE ID = "noo"; """) csr.execute("""DELETE FROM test.test WHERE ID = "boo"; """) How could I do it programatically? You can do it with a single query: id_list = ['abc', 'def', 'ghi'] query_string = "delete from test where id in (%s)" % ','.join(['?'] * len(id_list)) cursor