sql-delete

How to programmatically check if row is deletable?

…衆ロ難τιáo~ 提交于 2019-12-01 19:02:45
Say we have a PostgreSQL table like so: CREATE TABLE master ( id INT PRIMARY KEY, ... ); and many other tables referencing it with foreign keys: CREATE TABLE other ( id INT PRIMARY KEY, id_master INT NOT NULL, ... CONSTRAINT other_id_master_fkey FOREIGN KEY (id_master) REFERENCES master (id) ON DELETE RESTRICT ); Is there a way to check (from within trigger function) if a master row is deletable without actually trying to delete it? The obvious way is to do a SELECT on all referencing tables one by one, but I would like to know if there is an easier way. The reason I need this is that I have a

Use a CTE to UPDATE or DELETE in MySQL

空扰寡人 提交于 2019-12-01 17:24:45
问题 The new version of MySQL, 8.0, now supports Common Table Expressions. According to the manual: A WITH clause is permitted at the beginning of SELECT, UPDATE, and DELETE statements: WITH ... SELECT ... WITH ... UPDATE ... WITH ... DELETE ... So, I thought, given the following table: ID lastName firstName ---------------------- 1 Smith Pat 2 Smith Pat 3 Smith Bob I can use the following query: ;WITH ToDelete AS ( SELECT ID, ROW_NUMBER() OVER (PARTITION BY lastName, firstName ORDER BY ID) AS rn

SELECT then immediately DELETE mysql record

孤街浪徒 提交于 2019-12-01 16:43:49
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 delay but it's not working very well. I tried using a transaction, but I don't think it applies here.

Python MySQL - SELECTs work but not DELETEs?

孤街浪徒 提交于 2019-12-01 16:00:20
I'm new to Python and Python's MySQL adapter. I'm not sure if I'm missing something obvious here: db = MySQLdb.connect(# db details omitted) cursor = self.db.cursor() # WORKS cursor.execute("SELECT site_id FROM users WHERE username=%s", (username)) record = cursor.fetchone() # DOES NOT SEEM TO WORK cursor.execute("DELETE FROM users WHERE username=%s", (username)) Any ideas? Bill Karwin I'd guess that you are using a storage engine that supports transactions (e.g. InnoDB) but you don't call db.commit() after the DELETE. The effect of the DELETE is discarded if you don't commit. See http://mysql

In SQL syntax, is 'from' in 'delete from' optional if you plan to use 'where'?

倾然丶 夕夏残阳落幕 提交于 2019-12-01 15:45:43
I am new to SQL. We have some code that should work on SQL Server 2005/2008, Oracle 10 as well as Sybase. I was writing a script to try to figure out which tables a given stored procedure modifies (but does not drop), e.g insert , update and delete . The delete one turned out being puzzling - sometimes I see statements like: delete phone_book where ... as opposed to: delete from phone_book where ... So ... is the from keyword truly optional in this case? Does this cause any problems? Is it just a bad style, or does it not matter? I have not found a reference to T-SQL that would make from

Python MySQL - SELECTs work but not DELETEs?

强颜欢笑 提交于 2019-12-01 14:57:24
问题 I'm new to Python and Python's MySQL adapter. I'm not sure if I'm missing something obvious here: db = MySQLdb.connect(# db details omitted) cursor = self.db.cursor() # WORKS cursor.execute("SELECT site_id FROM users WHERE username=%s", (username)) record = cursor.fetchone() # DOES NOT SEEM TO WORK cursor.execute("DELETE FROM users WHERE username=%s", (username)) Any ideas? 回答1: I'd guess that you are using a storage engine that supports transactions (e.g. InnoDB) but you don't call db.commit

In SQL syntax, is 'from' in 'delete from' optional if you plan to use 'where'?

北慕城南 提交于 2019-12-01 14:38:40
问题 I am new to SQL. We have some code that should work on SQL Server 2005/2008, Oracle 10 as well as Sybase. I was writing a script to try to figure out which tables a given stored procedure modifies (but does not drop), e.g insert , update and delete . The delete one turned out being puzzling - sometimes I see statements like: delete phone_book where ... as opposed to: delete from phone_book where ... So ... is the from keyword truly optional in this case? Does this cause any problems? Is it

PHP Multiple CheckBox Delete

大憨熊 提交于 2019-12-01 12:33:22
I am having a very hard time solving my problem with the multiple checkbox delete. Can someone direct me to the solution? What is supposed to happen here is that the user can tick the boxes and click a delete button to delete the ticked ones. Unfortunately, my code doesn't seem to work; can you point me in the right direction? <div id="container" class="page"> <img id="disclaimer" class="page" src="images/DISCLAIMER.png" alt="" /> <img id="logo" class="page" src="images/MI-LOGO.png" alt="" /> <div id="videoContainer" class="page"> <video id="video" controls> <source src="video/animationTest

PHP Multiple CheckBox Delete

久未见 提交于 2019-12-01 10:35:38
问题 I am having a very hard time solving my problem with the multiple checkbox delete. Can someone direct me to the solution? What is supposed to happen here is that the user can tick the boxes and click a delete button to delete the ticked ones. Unfortunately, my code doesn't seem to work; can you point me in the right direction? <div id="container" class="page"> <img id="disclaimer" class="page" src="images/DISCLAIMER.png" alt="" /> <img id="logo" class="page" src="images/MI-LOGO.png" alt="" />

SQL Truncate, Delete, Drop advise

非 Y 不嫁゛ 提交于 2019-12-01 10:23:35
问题 I have a table in a SQL db that I want to remove the data from? I want to keep the columns though. e.g. my table has 3 columns, Name, Age, Date. I don't want to remove these, i just want to remove the data. Should I should Truncate, Delete or Drop? 回答1: Don't drop - it will delete the data and the definition. If you delete - the data is gone and auto-increment values go on from the last value. If you truncate - then it is like you just did create the table. No data and all counters resetted