sql-delete

I got error “The DELETE statement conflicted with the REFERENCE constraint”

无人久伴 提交于 2019-11-27 05:23:32
I tried to truncate a table with foreign keys and got the message: " Cannot truncate table because it is being referenced by a FOREIGN KEY constraint ". I read a lot of literature about the problem and thought that I found the solution by using delete DELETE FROM table_name DBCC CHECKIDENT (table_name, RESEED, 0) But I still got an error message: " The DELETE statement conflicted with the REFERENCE constraint ". When I try to delete with Microsoft Management Studio and execute the previous query DELETE FROM table_name DBCC CHECKIDENT (table_name, RESEED, 0) it doesn't give an error and works

Delete all rows with timestamp older than x days

偶尔善良 提交于 2019-11-27 05:19:08
问题 I want to delete all the rows with timestamp older than 180 days from a specific table in my database. I've tried the this: DELETE FROM on_search WHERE search_date < DATE_SUB(NOW(), INTERVAL 180 DAY); But that deleted all the rows and not only the rows older than 6 months. I have a column in on_search table called search_date and contains the time when that row was created. search_id search_term search_date 660779 car games 1390052553 回答1: DELETE FROM on_search WHERE search_date < UNIX

java.lang.IllegalArgumentException: Removing a detached instance com.test.User#5

╄→尐↘猪︶ㄣ 提交于 2019-11-27 05:12:30
问题 I have a java EE project using JPA (transaction-type="JTA"), hibernate as provider. I write my beans to handle the CRUD things. The program running in JBOSS 7 AS. I have an EntityManagerDAO : @Stateful public class EntityManagerDao implements Serializable { @PersistenceContext(unitName = "dtdJpa") private EntityManager entityManager; @TransactionAttribute(TransactionAttributeType.REQUIRED) public Object updateObject(Object object) { object = entityManager.merge(object); return object; }

Faster way to delete matching rows?

本小妞迷上赌 提交于 2019-11-27 05:11:55
问题 I'm a relative novice when it comes to databases. We are using MySQL and I'm currently trying to speed up a SQL statement that seems to take a while to run. I looked around on SO for a similar question but didn't find one. The goal is to remove all the rows in table A that have a matching id in table B. I'm currently doing the following: DELETE FROM a WHERE EXISTS (SELECT b.id FROM b WHERE b.id = a.id); There are approximately 100K rows in table a and about 22K rows in table b. The column 'id

How do you enable LIMIT for DELETE in SQLite?

自作多情 提交于 2019-11-27 04:54:19
Using PHP, I have a simple database that may store multiple items with the same content. I want to delete the first occurrence of an instance when I use DELETE. How do you enable LIMIT for DELETE in SQLite using PHP? You cannot enable these options from within PHP, you need to compile SQLite yourself in order to enable these options. Importantly, you need to download the full version, not the amalgamation source release from SQLite download page . If you're on Unix, get the sqlite-3.6.20.tar.gz tarball and download it. Then: tar xzf sqlite-3.6.20.tar.gz cd sqlite-3.6.20 export CFLAGS='-DSQLITE

delete rows from multiple tables

眉间皱痕 提交于 2019-11-27 04:54:02
I'm trying to use SQL to delete multiple rows from multiple tables that are joined together. Table A is joined to Table B Table B is joined to Table C I want to delete all rows in table B & C that correspond to a row in Table A CREATE TABLE `boards` ( `boardid` int(2) NOT NULL AUTO_INCREMENT, `boardname` varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (`boardid`) ); -- -------------------------------------------------------- -- -- Table structure for table `messages` -- CREATE TABLE `messages` ( `messageid` int(6) NOT NULL AUTO_INCREMENT, `boardid` int(2) NOT NULL DEFAULT '0', `topicid` int(4)

Delete all but one duplicate record

纵然是瞬间 提交于 2019-11-27 04:18:29
问题 I have a table that is supposed to keep a trace of visitors to a given profile (user id to user id pair). It turns out my SQL query was a bit off and is producing multiple pairs instead of single ones as intended. With hindsight I should have enforced a unique constraint on each id+id pair. Now, how could I go about cleaning up the table? What I want to do is delete all duplicate pairs and leave just one. So for example change this: 23515 -> 52525 date_visited 23515 -> 52525 date_visited

How to delete in MS Access when using JOIN's?

元气小坏坏 提交于 2019-11-27 04:04:36
I am attempting to use the DELETE clause in MS Access and have an issue when also using the JOIN clause. I have notice this can be accomplished by using the DISTINCTROW key word. For example, the following SQL statement does not allow for deletion: DELETE Table1.* FROM Table1 INNER JOIN Table2 ON Table1.Name=Table2.Name; However, this statement does: DELETE DISTINCTROW Table1.* FROM Table1 INNER JOIN Table2 ON Table1.Name=Table2.Name; Why does the DELETE work when using the DISTINCTROW key word? More specifically, what is happening in the JET engine to require this? Delete Table1.* From Table1

Delete from one table with join

半世苍凉 提交于 2019-11-27 04:04:00
问题 I'm trying to delete records from one database based on a selection criteria of another. We have two tables, emailNotification which stores a list of jobs and emails. Then we have jobs. I want to clear out emailNotifications for jobs that have been closed. I found some earlier examples on Stackoverflow that lead me to this type of syntax (I was previously trying to do the join before the where). DELETE FROM emailNotification WHERE notificationId IN ( SELECT notificationId FROM

SQL DELETE with JOIN another table for WHERE condition

人盡茶涼 提交于 2019-11-27 03:57:17
问题 I have to delete rows from guide_category that have no relation with guide table (dead relations). Here is what I want to do, but it of course does not work. DELETE FROM guide_category AS pgc WHERE pgc.id_guide_category IN (SELECT id_guide_category FROM guide_category AS gc LEFT JOIN guide AS g ON g.id_guide = gc.id_guide WHERE g.title IS NULL) Error: You can't specify target table 'guide_category' for update in FROM clause 回答1: Due to the locking implementation issues, MySQL does not allow