Updating Data¶
Let us see how we can update data in the table.
- Typical syntax
UPDATE <table_name>
SET
col1 = val1,
col2 = val2
WHERE <condition>
- If
WHERE
condition is not specified all rows in the table will be updated. - For now we will see basic examples for update. One need to have good knowledge about
WHERE
clause to take care of complex conditions. UsingWHERE
will be covered extensively as part of filtering the data at a later point in time.
- Set user role for user_id 1 as ‘A’
In [9]:
%load_ext sql
%env DATABASE_URL= postgresql://itversity_sms_user:itversity@pg.itversity.com/itversity_sms_db
The sql extension is already loaded. To reload it, use: %reload_ext sql env: DATABASE_URL=postgresql://itversity_sms_user:itversity@pg.itversity.com/itversity_sms_db
In [10]:
%sql SELECT * FROM users
6 rows affected.
Out[10]:
user_id | user_first_name | user_last_name | user_email_id | user_email_validated | user_password | user_role | is_active | create_ts | last_updated_ts |
---|---|---|---|---|---|---|---|---|---|
1 | Scott | Tiger | scott@tiger.com | False | None | U | False | 2022-02-17 09:14:03.139186 | 2022-02-17 09:14:03.139186 |
2 | Donald | Duck | donald@duck.com | False | None | U | False | 2022-02-17 09:14:07.905932 | 2022-02-17 09:14:07.905932 |
3 | Mickey | Mouse | mickey@mouse.com | False | None | U | True | 2022-02-17 09:14:12.355915 | 2022-02-17 09:14:12.355915 |
4 | Gordan | Bradock | gbradock0@barnesandnoble.com | False | h9LAz7p7ub | U | True | 2022-02-17 09:14:16.774064 | 2022-02-17 09:14:16.774064 |
5 | Tobe | Lyness | tlyness1@paginegialle.it | False | oEofndp | U | True | 2022-02-17 09:14:16.774064 | 2022-02-17 09:14:16.774064 |
6 | Addie | Mesias | amesias2@twitpic.com | False | ih7Y69u56 | U | True | 2022-02-17 09:14:16.774064 | 2022-02-17 09:14:16.774064 |
In [11]:
%%sql
UPDATE users
SET user_role = 'A'
WHERE user_id = 1
* postgresql://itversity_sms_user:***@pg.itversity.com/itversity_sms_db 1 rows affected.
Out[11]:
[]
In [12]:
%sql SELECT * FROM users
* postgresql://itversity_sms_user:***@pg.itversity.com/itversity_sms_db 6 rows affected.
Out[12]:
user_id | user_first_name | user_last_name | user_email_id | user_email_validated | user_password | user_role | is_active | create_ts | last_updated_ts |
---|---|---|---|---|---|---|---|---|---|
2 | Donald | Duck | donald@duck.com | False | None | U | False | 2022-02-17 09:14:07.905932 | 2022-02-17 09:14:07.905932 |
3 | Mickey | Mouse | mickey@mouse.com | False | None | U | True | 2022-02-17 09:14:12.355915 | 2022-02-17 09:14:12.355915 |
4 | Gordan | Bradock | gbradock0@barnesandnoble.com | False | h9LAz7p7ub | U | True | 2022-02-17 09:14:16.774064 | 2022-02-17 09:14:16.774064 |
5 | Tobe | Lyness | tlyness1@paginegialle.it | False | oEofndp | U | True | 2022-02-17 09:14:16.774064 | 2022-02-17 09:14:16.774064 |
6 | Addie | Mesias | amesias2@twitpic.com | False | ih7Y69u56 | U | True | 2022-02-17 09:14:16.774064 | 2022-02-17 09:14:16.774064 |
1 | Scott | Tiger | scott@tiger.com | False | None | A | False | 2022-02-17 09:14:03.139186 | 2022-02-17 09:14:03.139186 |
- Set user_email_validated as well as is_active to true for all users
In [13]:
%sql SELECT user_id, user_email_validated, is_active FROM users
* postgresql://itversity_sms_user:***@pg.itversity.com/itversity_sms_db 6 rows affected.
Out[13]:
user_id | user_email_validated | is_active |
---|---|---|
2 | False | False |
3 | False | True |
4 | False | True |
5 | False | True |
6 | False | True |
1 | False | False |
In [14]:
%%sql
UPDATE users
SET
user_email_validated = true,
is_active = true
* postgresql://itversity_sms_user:***@pg.itversity.com/itversity_sms_db 6 rows affected.
Out[14]:
[]
In [15]:
%sql SELECT user_id, user_email_validated, is_active FROM users
* postgresql://itversity_sms_user:***@pg.itversity.com/itversity_sms_db 6 rows affected.
Out[15]:
user_id | user_email_validated | is_active |
---|---|---|
2 | True | True |
3 | True | True |
4 | True | True |
5 | True | True |
6 | True | True |
1 | True | True |
- Convert case of user_email_id to upper for all the records
In [16]:
%sql SELECT user_id, user_email_id FROM users
* postgresql://itversity_sms_user:***@pg.itversity.com/itversity_sms_db 6 rows affected.
Out[16]:
user_id | user_email_id |
---|---|
2 | donald@duck.com |
3 | mickey@mouse.com |
4 | gbradock0@barnesandnoble.com |
5 | tlyness1@paginegialle.it |
6 | amesias2@twitpic.com |
1 | scott@tiger.com |
In [17]:
%%sql
UPDATE users
SET
user_email_id = upper(user_email_id)
* postgresql://itversity_sms_user:***@pg.itversity.com/itversity_sms_db 6 rows affected.
Out[17]:
[]
In [18]:
%sql SELECT user_id, user_email_id FROM users
* postgresql://itversity_sms_user:***@pg.itversity.com/itversity_sms_db 6 rows affected.
Out[18]:
user_id | user_email_id |
---|---|
2 | DONALD@DUCK.COM |
3 | MICKEY@MOUSE.COM |
4 | GBRADOCK0@BARNESANDNOBLE.COM |
5 | TLYNESS1@PAGINEGIALLE.IT |
6 | AMESIAS2@TWITPIC.COM |
1 | SCOTT@TIGER.COM |
- Add new column by name user_full_name and update it by concatenating user_first_name and user_last_name.
In [19]:
%%sql
ALTER TABLE users ADD COLUMN user_full_name VARCHAR(50)
* postgresql://itversity_sms_user:***@pg.itversity.com/itversity_sms_db Done.
Out[19]:
[]
In [20]:
%sql SELECT user_id, user_first_name, user_last_name, user_full_name FROM users
* postgresql://itversity_sms_user:***@pg.itversity.com/itversity_sms_db 6 rows affected.
Out[20]:
user_id | user_first_name | user_last_name | user_full_name |
---|---|---|---|
2 | Donald | Duck | None |
3 | Mickey | Mouse | None |
4 | Gordan | Bradock | None |
5 | Tobe | Lyness | None |
6 | Addie | Mesias | None |
1 | Scott | Tiger | None |
In [21]:
%sql SELECT concat(user_first_name, ' ', user_last_name) FROM users
* postgresql://itversity_sms_user:***@pg.itversity.com/itversity_sms_db 6 rows affected.
Out[21]:
concat |
---|
Donald Duck |
Mickey Mouse |
Gordan Bradock |
Tobe Lyness |
Addie Mesias |
Scott Tiger |
In [22]:
%%sql
UPDATE users
SET user_full_name = upper(concat(user_first_name, ' ', user_last_name))
* postgresql://itversity_sms_user:***@pg.itversity.com/itversity_sms_db 6 rows affected.
Out[22]:
[]
In [23]:
%sql SELECT user_id, user_first_name, user_last_name, user_full_name FROM users
* postgresql://itversity_sms_user:***@pg.itversity.com/itversity_sms_db 6 rows affected.
Out[23]:
user_id | user_first_name | user_last_name | user_full_name |
---|---|---|---|
2 | Donald | Duck | DONALD DUCK |
3 | Mickey | Mouse | MICKEY MOUSE |
4 | Gordan | Bradock | GORDAN BRADOCK |
5 | Tobe | Lyness | TOBE LYNESS |
6 | Addie | Mesias | ADDIE MESIAS |
1 | Scott | Tiger | SCOTT TIGER |