Track which columns were modified by an Update - sql

How do I track the list of updated columns in a table
Table1
Name Place Email Id
John US john#gmail.com 1
Sam US sam#gmail.com 2
Now if I update Name,place columns for John and place,email for Sam like
Name Place Email Id
Johnny UK john#gmail.com 1
Sam UK sammy#gmail.com 2
I want to store the list of updated attributes in another table as following
Id ListOfModifiedAttributes Status othercolumns
1 name,place success
2 place,email success
How to achieve the above in plsql.

Related

Oracle SQL Query to order by column value in different table

I'm rather new to sql and am struggling with a statement. For simplicity, let's say I have two tables -
Table - JourneyOrder, and Table - ItemsToBuy.
JourneyOrder has list of cities, index of a particular city in a journey and who is taking the journey.
So:
City Index Name
London 1 John
Sydney 2 John
Paris 3 John
Berlin 1 Jack
Paris 2 Jack
The other table has a list of items to buy, containing location (as in City), who needs to buy an item, and different characteristics of an item.
Item Location Weight Price Responsible
Pencil London 1 2 John
Glue London 2 5 John
Car Paris 1000 12000 John
Wallet Paris 3 20 Jack
I want to select all items a particular person needs to buy and order them by location. So if John's first route is London, I want items you can buy in London displayed first.
At the moment, I can use the following:
SELECT itb.item, itb.location, itb.price
FROM ItemsToBuy itb,
WHERE itb.responsible = 'John'
ORDER BY CASE
WHEN itb.location = 'London' THEN '1'
WHEN itb.location = 'Sydney' THEN '2'
WHEN itb.location = 'Paris' THEN '3'
Now this is obviously terrible, as I need to 'hardcode' the order by part according to the journey each responsible is taking, how can I make it so it matches location of items table to city from journey table and uses the index from the journey table when these match.
I think you just want a join:
SELECT itb.item, itb.location, itb.price
FROM ItemsToBuy itb JOIN
JourneyOrder jo
ON itb.name = jo.name AND itb.location = jo.city
WHERE itb.responsible = 'John'
ORDER BY jo.index;

Find last match with latest inserted name in table

I have a table PersonalInfo having columns such as Id, Name, Address.
Suppose I have following data
1 John US
2 Mark UK
3 John UK
4 David US
Now when I insert following new record
5 John China
I want to update the last record having same name as the new one for example as shown here the record 3 John UK will be updated to 3 John China.
When I insert the 5th record the table should be
1 John US
2 Mark UK
3 John China
4 David US
5 John China
what sql query should I use?
Assuming the Id is auto-incremented, it would be the highest value for that Name and Address combination.
UPDATE PersonalInfo
SET Address = 'US'
WHERE ID = (SELECT MAX(Id)
FROM PersonalInfo
WHERE Name = 'John'
AND Address = 'UK'
)

Categories datatype

I want to check what hobbies a user has and I want to insert it to a database.
I was thinking about making a column Hobbies and to insert the data in a string type in that form : {"Hobby1", "Hobby2", "Hobby3"}
But then the code to filter out the hobbies would be big, is there a more efficient way?
Create a Hobbies table and insert hobbies into that such as Golf, Photography etc.
Then create a UserHobby table to associate a User with a Hobby such that a User can have multiple hobbies.
EG
UserID FirstName
1 Bob
2 John
HobbyID HobbyName
1 Golf
2 Photography
UserHobbyID UserID HobbyID
1 1 1
2 1 2
3 2 2

Updating multiple fields in a column simultaneously Microsoft Access

Say I have the following table:
App owner owner_id
______________________
1 John NULL
2 Jack 000123
3 John NULL
4 April 000124
5 John NULL
6 April 000124
7 John 000123
8 Ash NULL
9 Ash NULL
10 Ash NULL
If I need to update John and Ash's owner_ids, is there a way to update each owner's owner_id for every occurrence in the table by only entering the information once?
I have to do this for about 1000 owners, but with the amount of duplicates I am looking at around 10000 blank owner_ids.
I do not have the proper rights to restructure or split the table.
If I didn't misunderstand your question, it's just:
update owners_table set owner_id = '000001' where owner = 'john'
update owners_table set owner_id = '000002' where owner = 'ash'
This will set the owner_id to 000001 in every row where the owner is John, and to 000002 in every row where the owner is Ash.
It's just more copy & paste when you have a lot of owners, because you have to create one query for each owner.
Another way would be to create a temporary table with just the owner and his new id:
owner newid
--------------
John 000001
Ash 000002
You can enter hundreds or thousands of owners and their new owner_ids into the table.
Then, you can update them in your main table all at once with the following query:
UPDATE owners_table
INNER JOIN ownertmp ON owners_table .owner = ownertmp.owner
SET owners_table.owner_id = [ownertmp].[newid];
After that, you can delete the temporary table again - it's just needed to run the query.

SQL Query that leaves duplicate fields blank?

What I would like is if my data is this:
ID Name
0 Jim
1 Dave
1 Bob
1 John
2 Ann
My query returns this:
ID Name
0 Jim
1 Dave
Bob
John
2 Ann
Is there a simple way to do this?
Edit:
The query which returns the original 2 columns of data would be something like this:
SELECT ID, NAME
FROM TestTable
ORDER BY ID