Delete TWO TABLES entries WITH SINGLE Query - iphone

I am trying to delete TWO TABLES entries WITH SINGLE Query in my sqlite database on my iPhone app, but am getting a weird error.
DELETE Sec1Opr_Equipment.*, Sec2Opr_Equipment.* FROM
Sec1Opr_Equipment INNER JOIN Sec2Opr_Equipment ON Sec1Opr_Equipment.ID = Sec2Opr_Equipment.ID
WHERE Sec1Opr_Equipment.ID='1'
And also Try
DELETE Sec1Opr_Equipment,Sec2Opr_Equipment
FROM Sec1Opr_Equipment
LEFT JOIN Sec2Opr_Equipment
ON Sec1Opr_Equipment.ID = Sec2Opr_Equipment.ID
WHERE Sec2Opr_Equipment.ID='1'
Plz any one help me..
thanks in advance

This is not possible, in SQLite the delete statement can only delete records from a single table without any joins. You can use subqueries in your WHERE clause though:
DELETE FROM tbl1 WHERE id IN (SELECT id FROM tbl2 WHERE ...)

Related

Select on delete actions for foreign keys [duplicate]

Almost all the information I had needed about a database, I could find in information_schema
This time I needed to read details of all foreign keys in a database through single query I found every thing in information_schema.key_Column_usage but could not find the constraints like on delete, on update
I could do show create table for all individual tables. But is there any way to get these details through some select query like this?
SELECT CONSTRAINT_NAME, TABLE_NAME,COLUMN_NAME, REFERENCED_TABLE_NAME,
REFERENCED_COLUMN_NAME FROM information_schema.`KEY_COLUMN_USAGE` WHERE
table_schema = 'mydbname' AND referenced_column_name IS NOT NULL
It is doing the job well but just missing constraints like on delete, on update How can I get those values as well so that I can get all info about foreign keys in a single query?
UPDATE_RULE and DELETE_RULE is the thing you asked for
it's a little bit too late but it could help someone else, here the solution :
SELECT tb1.CONSTRAINT_NAME, tb1.TABLE_NAME, tb1.COLUMN_NAME,
tb1.REFERENCED_TABLE_NAME, tb1.REFERENCED_COLUMN_NAME, tb2.MATCH_OPTION,
tb2.UPDATE_RULE, tb2.DELETE_RULE
FROM information_schema.`KEY_COLUMN_USAGE` AS tb1
INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS AS tb2 ON
tb1.CONSTRAINT_NAME = tb2.CONSTRAINT_NAME
WHERE table_schema = 'sfa' AND referenced_column_name IS NOT NULL
Update From information_schema.REFERENTIAL_CONSTRAINTS table add in mysql 5.1 mysql-5.1 we can get information about all constraints**. Accepted answer gives the solution as query.
Earlier
Before mysql 5.1 like mysql-5.0, we could not get this information, we could only use show create table for individual table.
If you are looking for (primary|foreign|unique) keys :
http://dev.mysql.com/doc/refman/5.5/en/table-constraints-table.html
Now, you can find foreign key constraint details in table INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
http://dev.mysql.com/doc/refman/5.5/en/referential-constraints-table.html

How to insert new rows only on tables without Primary or Foreign Keys?

Scenario: I have two tables. Table A and Table B, both have the same exact columns. My task is to create a master table. I need to ensure no duplicates are in the master table unless it is a new record.
problem: Whoever built the tables did not assign a Primary Key to the table.
Attempts: I attempted running an INSERT INTO WHERE NOT EXISTS query (below as an example not the actual query I ran)
Question: the portion of the query below WHERE t2.id = t1.id confuses me, my table has a multitude of columns, there is no id column like I said it has no PRIMARY key to anchor the match, so, in a scenario where all I have are values without primary keys, how can I append only new records? Also, perhaps I am going about this the wrong way but are there any other functions or options through TSQL worth considering? Maybe not an INSERT INTO statement or perhaps something else? My SQL skills aren't yet that advance so I am not asking for a solution but perhaps ideas or other methods worth considering? Any ideas are welcome.
INSERT INTO TABLE_2
(id, name)
SELECT t1.id,
t1.name
FROM TABLE_1 t1
WHERE NOT EXISTS(SELECT id
FROM TABLE_2 t2
WHERE t2.id = t1.id)
If I understand your question correctly, you would need to amend the SQL sample you posted by changing the condition t2.id = t1.id to whatever columns you do have.
Say your 2 tables have name and brand columns and you don't want duplicates, just change the sample to:
WHERE t2.name = t1.name
AND t2.brand = t1.brand
This will ensure you don't insert and rows in table 2 from table 1 which are duplicates. You would have to make sure the where condition contains all columns (you said the table schemas are identical).
Also, the above code sample copies everything into table 2 - but you said you want a master table - so you'd have to change it to insert into the master table, not table 2.

RedShift: Delete statement does not allow table alias?

Got the the following code to try to blow away duplicate entries in the table:
DELETE FROM events a
WHERE a.ctid <> (SELECT min(b.ctid)
FROM events b
WHERE a.eventid = b.eventid and
left(b.eventtimestamp,10)='2016-01-15');
Tried this with my query tool and with psql, and I get:
Error : ERROR: syntax error at or near "a"
It is objecting to the alias. I've tried different names, I've tried inserting the "AS" key word, all to no avail. From what I can tell, Redshift supports table aliases. So what am I doing wrong?
Redshift doesn't know about ctid because you didn't alias min(b.ctid).
AFAICT, this delete is possible but not in the way you structured it. Your subquery is correlated with the deletion target. I (personally) would not feel confident about the right rows being deleted with that query structure, even it it would run.
Since Redshift does not support joins in DELETE I reworked it a bit to use an IN list. Innermost to outer it:
finds the MIN() ctid per eventid for the given date
joins to events again where ctid does not match
concatenates eventid with ctid
DELETE from events using an IN list and the same concat
The advantage here is that you can confirm exactly which combinations of eventid and ctid will be deleted from the table. It's not clear to me whether you would also need to limit the events to a given date in the outer subquery.
DELETE FROM events
WHERE eventid||ctid IN (SELECT a.eventid||a.ctid
FROM events AS a
JOIN (SELECT eventid, MIN(b.ctid) ctid
FROM events
WHERE LEFT(b.eventtimestamp,10)='2016-01-15'
GROUP BY eventid) AS b
ON a.eventid = b.eventid
AND a.ctid <> b.ctid
;
Looks like the answer is that an alias is not supported there- finally tracked down the latest doc and it does not show a table alias as an option for the delete.
Also, /BTW, when I dropped the alias, RS said it didn't know about ctid- which is in Postgres 8.0 and 8.2. So this probably won't work for RS anyway.

What is the syntax for performing a parameterised delete using joins in SSIS 2008?

I'm trying to use an OLE DB Command to perform a delete using data from each row of my input file. The actual query works fine when running manually in sql server (given tableB.otherID is compared to an int), but I'm having issues parameterising it.
delete tableA from tableA
where tableA.ID = ?
The above query runs, and allows me to assign one of my input columns to tableA.ID. This is what I would expect.
Trying
delete tableA from tableA
INNER JOIN tableB ON tableB.ID = tableA.ID
where tableB.OtherID = ?
Throws up an error however ("The multi-part identifier tableB.OtherID could not be bound"). Hardcoding a value in place of the '?' stops this error from appearing.
It seems like this would be the correct syntax, is there anything wrong with the above?
This seems to be a bug/limitation with SSIS, I've found myself unable to perform similar parameterised update statements using a join.
Solution I ended up using was creating a temporary stored procedure with the delete statement I wanted, and passing the parameter to it directly.
I think the TSQL syntax you want is:
DELETE FROM tableA
FROM tableA INNER JOIN tableB ON tableB.ID = tableA.ID
WHERE tableB.OtherID = ?
DELETE FROM tableA
FROM tableA INNER JOIN tableB ON tableB.ID = tableA.ID
WHERE tableB.OtherID = #OrderId
Where #OrderID should be your variable in SSIS.
Depending on how many rows you need to delete using an Execute Sql task becomes slow quite quickly.
If that happens the solution that worked for me is putting the keys of the rows that need to be deleteed into a staging table, then when they're all in there issue one statement that deletes all those rows in a single statement and purges the staging table. Much quicker that way, added beneift is that you don;t have to use the quirky ? syntax. I never liked that, much too easy to mix stuff up when the sql becomes a little more complicated.
Regards Gert-Jan

Getting a list of tables that a view/table depends on in PostgreSQL

In PostgreSQL, is there a way to get all of the tables that a view/table depends on based on its use of foreign keys and access to a given table?
Basically, I want to be able to copy the structure of a view/table using a script and want to be able to automatically get the list of tables that I would also need to copy in order for everything to still work right.
This response appears to be headed in the right direction, but doesn't give me the results that I expect/need. Any suggestions?
Using the info from Andy Lester, I was able to come up with the following queries to retrieve the information that I needed.
Get Tables that Foreign Keys refer to:
SELECT cl2.relname AS ref_table
FROM pg_constraint as co
JOIN pg_class AS cl1 ON co.conrelid=cl1.oid
JOIN pg_class AS cl2 ON co.confrelid=cl2.oid
WHERE co.contype='f' AND cl1.relname='TABLENAME'
ORDER BY cl2.relname;
Get Tables that a View or Rules from a Table refer to:
SELECT cl_d.relname AS ref_table
FROM pg_rewrite AS r
JOIN pg_class AS cl_r ON r.ev_class=cl_r.oid
JOIN pg_depend AS d ON r.oid=d.objid
JOIN pg_class AS cl_d ON d.refobjid=cl_d.oid
WHERE cl_d.relkind IN ('r','v') AND cl_r.relname='TABLENAME'
GROUP BY cl_d.relname
ORDER BY cl_d.relname;
Assuming you have your foreign keys set up correctly, use pg_dump to dump the table definitions.
pg_dump -s -t TABLENAME
I think it is a quite bad idea. Just copy the whole database, I think that the application wants to have all data, not only data from one table.
What's more, there are also triggers, that could depend on some tables, but to know that you'd have to make not so easy code analysis.
In psql, adding + to the usual \d gives you a "Referenced by" list along with the table definition.
\d+ tablename