sql-delete

MySQL: Truncate Table vs Delete From Table

大憨熊 提交于 2019-11-30 21:09:43
When do we use the DELETE command versus the TRUNCATE command? I am trying to find on the Internet but both commands delete the data; I can't tell the difference. DELETE FROM TABLE 1. DELETE is a DML Command. 2. DELETE statement is executed using a row lock, each row in the table is locked for deletion. 3. We can specify filters in where clause 4. It deletes specified data if where condition exists. 5. Delete activates a trigger because the operation are logged individually. 6. Slower than truncate because, it keeps logs. 7. Rollback is possible. 8. LIMIT clause can also be used to set a limit

DELETE all duplicate topics with few conditions

主宰稳场 提交于 2019-11-30 17:23:46
问题 I'm trying to make sql who will delete all duplicate titles BUT must delete duplicates with these conditions: must delete only duplicates with same object_id must keep only the newest record (biggest topic_id ) (topic_id is the unique id for every topic AI) So far I've done that (testing with select...) SELECT topic_id,object_id,title,url,date FROM topics GROUP BY title HAVING ( COUNT(title) > 1) ORDER BY topic_id DESC But doesn't meet the conditions. I'm using mysql. 回答1: In MySQL , you

Oracle sql merge to insert and delete but not update

▼魔方 西西 提交于 2019-11-30 12:19:09
Is there a way to use oracle merge to insert and delete but not update? I have a table representing a set of values related to a single row in another table. I could change the set of values by deleting them all and adding back the new set, or by selectively deleting some and adding others, but I am interested in making it a single statement if possible. Here is a working example with update. In order to make this work, I had to add dummy so that a column was available to update that was not in the on condition. Is there some way to only delete and insert without a dummy column to update? No

Delete first X lines of a database

耗尽温柔 提交于 2019-11-30 12:17:40
问题 is there an SQL command to delete the first X lines of a database table? I have a database table containing some information but no id or auto-incrementing value and a program that processes the first X lines of this table. Afterwards these X lines need to be deleted. So the standard query is: DELETE FROM table WHERE something = value; So, is there a way to build a query like: DELETE FROM table WHERE rownumber <= X; I have tried this command, but nothing happens to the database.. Do you have

SQL Delete Records within a specific Range [duplicate]

妖精的绣舞 提交于 2019-11-30 10:44:56
问题 This question already has answers here : How to delete multiple rows in SQL where id = (x to y) (5 answers) Closed 2 years ago . This is probably a very simple question for somebody with experience, but I just wanted to know the safest way to delete a couple of hundred records in an SQL table that fall between a specific range. For example I need to delete rows with an ID between 79 & 296: My worry is if I say delete everything with an ID (>79 AND < 296) then it may literally wipe the whole

MySQL: how to drop multiple tables using single query?

十年热恋 提交于 2019-11-30 08:55:08
I want to drop multiple tables with ease without actually listing the table names in the drop query and the tables to be deleted have prefix say 'wp_' I've used a query very similar to Angelin's. In case you have more than a few tables, one has to increase the max length of group_concat . Otherwise the query will barf on the truncated string that group_concat returns. This is my 10 cents: -- Increase memory to avoid truncating string, adjust according to your needs SET group_concat_max_len = 1024 * 1024 * 10; -- Generate drop command and assign to variable SELECT CONCAT('DROP TABLE ',GROUP

Disable DELETE on table in PostgreSQL?

て烟熏妆下的殇ゞ 提交于 2019-11-30 06:22:23
For a security sensitive design, I'd like to disable DELETEs on certain tables. The DELETE should merely set a deleted flag on a row (which would be then visible on a view, which would be used by the application layer). As I understand a rule would generate additional queries - so a rule could not suppress the original query. As illustration a toy example with a trigger (not yet tested): -- data in this table should be 'undeletable' CREATE table article ( id serial, content text not null, deleted boolean default false ) -- some view that would only show articles, that are NOT deleted ... --

MySQL: Truncate Table vs Delete From Table

二次信任 提交于 2019-11-30 05:39:58
问题 When do we use the DELETE command versus the TRUNCATE command? I am trying to find on the Internet but both commands delete the data; I can't tell the difference. 回答1: DELETE FROM TABLE 1. DELETE is a DML Command. 2. DELETE statement is executed using a row lock, each row in the table is locked for deletion. 3. We can specify filters in where clause 4. It deletes specified data if where condition exists. 5. Delete activates a trigger because the operation are logged individually. 6. Slower

Can I do a mysql Select, Update and Delete in one query?

自古美人都是妖i 提交于 2019-11-30 05:39:44
问题 Can I say that one of many ways to optimize mysql is to reduce the number of queries? If that so, can I do this: - Select "data" => $A from table X - Update $A from table Y - Delete $A from table X in one query? 回答1: You can't reduce the number of queries - they all do different things - but you could reduce the number of round trips to the database and the number of parses by wrapping it all as a PLSQL function. However you can't select the data after you've deleted it.....but consider:

How can I delete expired data from a huge table without having the log file grow out of control?

情到浓时终转凉″ 提交于 2019-11-30 04:28:55
问题 I have a huge table (3 billion rows), which unfortunately contains mostly expired data. I want to simply delete all of these expired rows, and keep the rest. I can execute a statement like this: delete from giganticTable where exp_date < getDate() The execution plan somehow estimates that about 400 million rows will be deleted. When executed, not only does this not finish after an hour, but the database transaction log file is also growing from 6 GB to 90 GB. Note that the database was in