I have one scenario as below
Generted_I'd Correct_gen_I'd
223455. 13456
256782. 14678
Want to check for the correct_gen_I'd in a table A, if it exists in Table B then delete the corresponding generated_I'd i.e 223455.
If it doesn't exists in table A, then update the corresponding generated_I'd with the correct_id in table A i.e update 223455 to 13456.
Can anyone please provide the approach/query to achieve this
Thanks a lot!!
Related
How do I populate an empty column in my sql database?
I have a list that I want in the last column of my database. If I use executemany it just adds rows on the bottom of my database. Is there a way to fill it from the top? The column is currently populated with NULL values.
Just to be clear. I want the first item in the list to be put in the first row and item n on the list to be put into row n.
Thanks in advance
*Always take backup of your database, just incase you want to revert it back to original state.
Each row in your database table should have a unique primary key. As you mentioned you have a list to be uploaded in the last column, I assume you have some kind of relationship that distinguishes which list item goes to which row in the last column of the table.
You an update a single row by simple update syntax as below:
UPDATE table_name
SET last_column = somevalue_from_list
WHERE id = unique_row_id;
If you want to update all the rows with the same value then
UPDATE table_name
SET last_column = one_value_for_all_rows;
Follow the below suggestion only when you are using phpmyadmin to manage your tables:
If you want to update each row with the unique value from the list then my suggestion will be to export your table in csv format, open in excel like software and add your last column values in last column of each row and then import the changed table back to the database.
Hope this help.
Below I have a users table and I want it so that every time a row's avatar changes to a different value, the same row's downloadavatar is set to 1.
CREATE TABLE users (
id UNIQUE TEXT,
avatar TEXT,
downloadavatar INTEGER
)
CREATE TRIGGER update_users_downloadavatar AFTER UPDATE OF avatar ON users WHEN old.avatar!=new.avatar
BEGIN
UPDATE users SET downloadavatar=1 WHERE id=old.id;
END
The SQL above does just that. My question is will it result in searching the entire users table for the row that meets the WHERE condition despite the TRIGGER already knowing where it is? If yes, how can I rectify this inefficiency?
The trigger does not know where the row is; it just knows how it is identified.
Looking up the row by ID will use the index, and because of the previous lookup done by the UPDATE, the few pages needed are already in the cache.
To avoid going through the index, use the rowid instead of the id column.
I've seen enough answers to know you can't easily check for columns in SQLITE before adding. I'm trying to make a lazy person's node in Node-Red where you pass a message to SQLITE which is the query. Adding a table if it does not exist is easy.
msg.topic='create table IF NOT EXISTS fred (id PRIMARY KEY);'; node.send(msg);
it occurred to me that adding a table which had the names of the fields would be easy - and if the field name is not in the table.... then add the field. BUT you can't add multiple fields at once - so I can't do this...
msg.topic='create table IF NOT EXISTS fred (id PRIMARY KEY, myfields TEXT);'; node.send(msg);
The problem with THAT is that I can't add this in later, there's no way to check before adding a field it the table exists!
This is what I WANT
msg.topic='create table IF NOT EXISTS fred (id PRIMARY KEY, myfields TEXT);'; node.send(msg);
msg.topic='if not (select address from myfields) alter table fred add column address text';
I just cannot think of any way to do this - any ideas anyone (the idea is that the node-red node would input a table, field and value and if the table didn't exist it would be created, if the field didn't exist it would be created, all before trying to add in the value).
You won't be able to make the ALTER TABLE conditional in the SQL itself. You'll need to handle that from your calling script.
Alternately, simply attempt to add the column to the table and accept failure as an outcome. Assuming the table exists, the only failure reason you could encounter is that the column already exists.
If you'd like to do something more graceful, you can check if the column exists in advance, then conditionally run the SQL from your calling application.
I have an autoincrementing ID field. What I would like to do is retrieve the value that gets assigned to this ID field after I've done an INSERT command.
Can somebody please tell me how to do this?
Thanks
You want last_insert_rowid():
SELECT last_insert_rowid();
I have a situation like, I have two tables having same structure. I have a view which points to one of the table. In my store procedure I have to check first which table the view points to and accordingly update the other table and then finally update the view to point to the updated table.
How to check that which table the view is currently pointing at??
select referenced_name
from all_dependencies
where name = 'MYVIEW'
and owner = 'MYSCHEMA'
and referenced_type = 'TABLE';