sql-delete

Deleting orphans from a table

你说的曾经没有我的故事 提交于 2019-12-03 16:44:24
I am trying to clean up a table where there are quite a few orphaned items. I am approaching this by checking to see if there is a relationship to another table by looking for null values. DELETE FROM table1 LEFT JOIN table2 ON table1.ID = table2.ID WHERE table2.ID IS NULL I get an error that the left outer join is not valid. I am looking for suggestions on other ways that I can delete these orphans from this broken relationship try this: DELETE FROM table1 WHERE NOT EXISTS (SELECT NULL FROM table2 WHERE table1.ID = table2.ID) If you want to use the same syntax, here is how it could have been:

How can I insert the return of DELETE into INSERT in postgresql?

一笑奈何 提交于 2019-12-03 15:28:21
问题 I am trying to delete a row from one table and insert it with some additional data into another. I know this can be done in two separate commands, one to delete and another to insert into the new table. However I am trying to combine them and it is not working, this is my query so far: insert into b (one,two,num) values delete from a where id = 1 returning one, two, 5; When running that I get the following error: ERROR: syntax error at or near "delete" Can anyone point out how to accomplish

Delete, Truncate or Drop to clean out a table in MySQL

北战南征 提交于 2019-12-03 15:13:14
问题 I am attempting to clean out a table but not get rid of the actual structure of the table. I have an id column that is auto-incrementing; I don't need to keep the ID number, but I do need it to keep its auto-incrementing characteristic. I've found delete and truncate but I'm worried one of these will completely drop the entire table rendering future insert commands useless. How do I remove all of the records from the table so that I can insert new data? 回答1: TRUNCATE will reset your auto

Delete large amount of data in sql server

对着背影说爱祢 提交于 2019-12-03 13:04:01
Suppose that I have a table with 10000000 record. What is difference between this two solution? delete data like : DELETE FROM MyTable delete all of data with a application row by row : DELETE FROM MyTable WHERE ID = @SelectedID Is the first solution has best performance? what is the impact on log and performance? Nils Schmidt If you have that many records in your table and you want to delete them all, you should consider truncate <table> instead of delete from <table> . It will be much faster, but be aware that it cannot activate a trigger. See for more details (this case sql server 2000):

Counting the number of deleted rows in a SQL Server stored procedure

感情迁移 提交于 2019-12-03 09:18:52
In SQL Server 2005, is there a way of deleting rows and being told how many were actually deleted? I could do a select count(*) with the same conditions, but I need this to be utterly trustworthy. My first guess was to use the @@ROWCOUNT variables - but that isn't set, e.g. delete from mytable where datefield = '5-Oct-2008' select @@ROWCOUNT always returns a 0. MSDN suggests the OUTPUT construction, e.g. delete from mytable where datefield = '5-Oct-2008' output datefield into #doomed select count(*) from #doomed this actually fails with a syntax error. Any ideas? wcm Have you tried SET NOCOUNT

Deleting duplicate record from table - SQL query

邮差的信 提交于 2019-12-03 08:45:53
问题 I need to delete duplicate rows only from the table, like I have 3 duplicate rows in the table, my query will delete 2 rows from 3 duplicated rows. How can I get this? Please help me. 回答1: Please try the below query, it will definitely meet your objective SET ROWCOUNT 1 DELETE test FROM test a WHERE (SELECT COUNT(*) FROM test b WHERE b.name = a.name) > 1 WHILE @@rowcount > 0 DELETE test FROM test a WHERE (SELECT COUNT(*) FROM test b WHERE b.name = a.name) > 1 SET ROWCOUNT 0 where test is your

Using return value from DELETE for UPDATE in Postgres

ぐ巨炮叔叔 提交于 2019-12-03 06:07:34
I need to update a table using a value deleted from another table. The situation is a comment vote scorekeeper similar to the one on SO. I'm using python to work the postgres, but that shouldn't make a difference. query=""" UPDATE comment SET score=score-(DELETE FROM history WHERE commentId=%(commentId)s AND userIdentity=%(userIdentity)s RETURNING vote) WHERE commentId=%(commentId)s; """ cursor.execute(query, data) The error arises at (DELETE FROM ; a syntax error arises. I can replace the DELETE statement with a SELECT statement and it will work, is there something I am missing here? I want

How can I insert the return of DELETE into INSERT in postgresql?

ⅰ亾dé卋堺 提交于 2019-12-03 05:08:54
I am trying to delete a row from one table and insert it with some additional data into another. I know this can be done in two separate commands, one to delete and another to insert into the new table. However I am trying to combine them and it is not working, this is my query so far: insert into b (one,two,num) values delete from a where id = 1 returning one, two, 5; When running that I get the following error: ERROR: syntax error at or near "delete" Can anyone point out how to accomplish this, or is there a better way? or is it not possible? Before PostgreSQL 9.1 you can create a volatile

Delete, Truncate or Drop to clean out a table in MySQL

空扰寡人 提交于 2019-12-03 04:52:41
I am attempting to clean out a table but not get rid of the actual structure of the table. I have an id column that is auto-incrementing; I don't need to keep the ID number, but I do need it to keep its auto-incrementing characteristic. I've found delete and truncate but I'm worried one of these will completely drop the entire table rendering future insert commands useless. How do I remove all of the records from the table so that I can insert new data? TRUNCATE will reset your auto-increment seed (on InnoDB tables, at least), although you could note its value before truncating and re-set

hibernate and delete all

旧街凉风 提交于 2019-12-03 04:44:32
问题 What is the best way to delete all rows in a table in Hibernate ? If I iterate over a collection and call session.delete() it's not performing to my knowledge. If I use another option session.createQuery("delete ...") it doesn't affect persistence context. When I should use these methods if there is no better variant? 回答1: if you don't have anything to cascade, use the HQL delete DELETE FROM enityName if you have cascades, iterate the collection and delete each one individually. The problem