SQLite how to delete all connected rows from other tables? - sqlite

i have the following problem. I have three tables, TABLE_1, TABLE_2 and TABLE_3.
Between TABLE_1 and TABLE_2 is a m:n relationship so theres a connecting table
lets call it TABLE_1_2.
Between TABLE_2 and TABLE_3 is a m:n relationship so theres a connecting table
lets call it TABLE_2_3.
If i delete now a row from TABLE_1 i want that all rows from the other tables connecte also will be deleted.
How can i handle this? i read that sqlite doesnt supports joins for delete statements.

In MS SQL this can be made via altering the table1 and corresponding column by adding ON DELETE CASCADE CONSTRAINT on this column. But in SQLite there is no ALTER COLUMN and ALTER TABLE is also much less functional than in MS SQL. That's why you'll probably have to
Rename the table1 to a table3
Create a new table with ON DELETE CASCADE CONSTRAINT and all other constraints you've already used.
Copy the content of the old table to the new one.
Remove the old table
See also the similar topic.

Related

Create temporary table

I'm coming from SQL Server enviroment where you can declare a temp table with #table, but as I've read you can't do this in oracle.
I want get a value for 500.000 hardcoded id's from a table, but as the IN clause has a limit of 1000 I need to find another way. Is the best way to create a temporary table and insert the hardcoded values and then join the other table which contains the values I need ?
My client (toad) has autocommit set to off and I dont want to commit anything, I want it to be session-based so when I close the database client I want the temporary table do disappear. Is the code below the right way to do in oracle?
CREATE GLOBAL TEMPORARY TABLE Test(HardcodedId number(10))
ON COMMIT DELETE ROWS;
I've also tried to use inner join and in the join select the hardcoded values from dual, but this creates a column for each value and i'm not able to use a reference to join with. Is it possible to insert all values into a single column in dual?
You can use some thing like this (500 union all)
select * from (
select '1' from dual
union all
select '2' from dual
...) q
Then you can join this with other tables.
For your situation, I would use a GTT (global temporary table) - which you have already researched by the looks.
The advantage of a GTT is that it's a permanent object (so no need to constantly create and drop it) and the data "stored" in it is on a session basis.

Batch filling in lookups in MS Access

I'm copying quite a lot of data from Excel into Access. The trouble is, I have a lookup field and I must select a value from it for each new row. There are about 1000 rows and I wondered if I can somehow fill in those lookups automatically.
Create a linked table to the Excel. Let's call it ExcelLinked.
Create a query with ExcelLinked and your lookup table, let's call it tblLookupItems.
The query will be:
INSERT INTO TargetTable (SELECT ExcelLinked.*, tblLookupItems.ID FROM ExcelLinked INNER JOIN tblLookupItems ON ExcelLinked.LookupItem = tblLookupItems.LookupItem)
However, if there are values in the Excel file that do not exist in the lookup table, you will have to decide whether you are willing to forgo those rows or use a lookup ID of 0 in which cae you should use a LEFT JOIN in the SQL query.

Joining tables in SQLite - what and how?

I need to manipulate some data in SQLite, it should be simple but trying to figure it out how to do exactly this has frustrated me.
It's just a join, one table called "routes" has a column "stop_id". I need to take another table called "stops" which also has a "stop_id" column and everywhere that they match, add all the additional columns from "stops" to the "routes" table (added columns are "stop_name" "stop_lat" "stop_lon" and "master_station"). "stop_id" is the primary key in the stops table. I need to join the tables and not keep them relational because after I do that I will be changing the rows by hand with new information. I am using Firefox SQLite Manager if that matters.
A join can be done with JOIN:
SELECT * FROM routes JOIN stops USING (stop_id)
However, the result of a join cannot be changed directly; the UPDATE statement works only on actual tables.
To change values that come from the routes or stops tables, you have to update those tables by using their respective primary keys to look up the records.

Merge a oracle table from different database having join condition on primary key generated using sequence

I have a table that needs to merge from same table in another database.
Condition is one of the column in composite primary key is generated by sequence and may differ in another database.
How can we sync remaining columns in this situation?
Ex.
DB1:
Table: SRE_SERVICE_OPTION
Columns:(OPTION_SET_ID, OPTION_NAME, OPTION_VALUE, VALUE_ORDER)
DB2:
Table: SRE_SERVICE_OPTION
Columns:(OPTION_SET_ID, OPTION_NAME, OPTION_VALUE, VALUE_ORDER)
Primary key:
OPTION_SET_ID, OPTION_NAME, VALUE_ORDER
How should I write merge statement?
You've made the classic error in a distributed database. Simply put, by using two separate sequences you have an inconsistent primary key across the tables. You will only be able to use MERGE if your tables are both separately unique on OPTION_NAME, OPTION_VALUE and VALUE_ORDER.
Your only other hope is that your tables do not contain any duplicated information between them. If so you can simply insert from one into the other.
Otherwise, you are, I'm afraid out of luck. I would re-design your database so that it's impossible to send duplicated information into two different tables with separate, inconsistent, surrogate keys.

Bulk update of column values of entire table

We have an Oracle 11g database table with around 35 million rows. We are in a situation where we have to update all values of one column. This column is indexed.
I have a script that can generate the updated values and can populate it in a text file.
I'm looking for a good strategy to do a bulk update to this table. We can afford a downtime of around 10 hours.
Will it be a good idea to
Dump the entire table to a flat file
Update the values using any scripting language
Reload the entire table
Rebuild indexes
What are the pitfalls that one can encounter?
I'm not competent in PL/SQL. Is there a way to solve this in PL/SQL or any way "within" the database itself?
Thanks,
Prabhu
The fastest way will probably be to create an external table based on your flat file of update values and then:
create table new_table as
select o.col1, o.col2, o.col3, ..., x.value as colN
from old_table o
join extern_table x on ...;
(Make sure that join returns all the rows from old_table. The join may need to be an outer join.)
-- drop foreign key constraints that reference old_table
alter table child1 drop constraint fk_to_old_table1;
...
drop table old_table;
rename new_table to old_table;
-- Re-create indexes and constraints on old_table
alter table old_table add constraint oldpk primary key (col1);
...
-- Re-create the dropped foreign key constraints to old_table
alter table child1 add constraint fk_to_old_table1 ...;
...

Resources