Inserting Data¶
Let us see how to insert the data into the table.
- We need to use INSERT clause to insert the data. Here is the sample syntax.
INSERT INTO <table_name> (col1, col2, col3)
VALUES (val1, val2, val3)
- If we don’t pass columns after table name then we need to specify values for all the columns. It is not good practice to insert records with out specifying column names.
- If we do not specify value for
SERIAL
field, a sequence generated number will be used. - It is not mandatory to pass the values for those fields where
DEFAULT
is specified. Values specified inDEFAULT
clause will be used. - It is mandatory to specify columns and corresponding values for all columns where
NOT NULL
is specified.
In [5]:
%reload_ext sql
In [6]:
%env DATABASE_URL=postgresql://itversity_sms_user:itversity@pg.itversity.com:5432/itversity_sms_db
env: DATABASE_URL=postgresql://itversity_sms_user:itversity@pg.itversity.com:5432/itversity_sms_db
In [7]:
%sql TRUNCATE TABLE users
Done.
Out[7]:
[]
In [8]:
%%sql
INSERT INTO users (user_first_name, user_last_name, user_email_id)
VALUES ('Scott', 'Tiger', 'scott@tiger.com')
* postgresql://itversity_sms_user:***@pg.itversity.com:5432/itversity_sms_db 1 rows affected.
Out[8]:
[]
In [9]:
%sql SELECT * FROM users
* postgresql://itversity_sms_user:***@pg.itversity.com:5432/itversity_sms_db 1 rows affected.
Out[9]:
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-03-09 12:15:51.066866 | 2022-03-09 12:15:51.066866 |
In [10]:
%%sql
INSERT INTO users (user_first_name, user_last_name, user_email_id)
VALUES ('Donald', 'Duck', 'donald@duck.com')
* postgresql://itversity_sms_user:***@pg.itversity.com:5432/itversity_sms_db 1 rows affected.
Out[10]:
[]
In [11]:
%sql SELECT * FROM users
* postgresql://itversity_sms_user:***@pg.itversity.com:5432/itversity_sms_db 2 rows affected.
Out[11]:
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-03-09 12:15:51.066866 | 2022-03-09 12:15:51.066866 |
2 | Donald | Duck | donald@duck.com | False | None | U | False | 2022-03-09 12:15:55.692717 | 2022-03-09 12:15:55.692717 |
In [12]:
%%sql
INSERT INTO users (user_first_name, user_last_name, user_email_id, user_role, is_active)
VALUES ('Mickey', 'Mouse', 'mickey@mouse.com', 'U', true)
* postgresql://itversity_sms_user:***@pg.itversity.com:5432/itversity_sms_db 1 rows affected.
Out[12]:
[]
In [13]:
%sql SELECT * FROM users
* postgresql://itversity_sms_user:***@pg.itversity.com:5432/itversity_sms_db 3 rows affected.
Out[13]:
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-03-09 12:15:51.066866 | 2022-03-09 12:15:51.066866 |
2 | Donald | Duck | donald@duck.com | False | None | U | False | 2022-03-09 12:15:55.692717 | 2022-03-09 12:15:55.692717 |
3 | Mickey | Mouse | mickey@mouse.com | False | None | U | True | 2022-03-09 12:15:58.617705 | 2022-03-09 12:15:58.617705 |
In [14]:
%%sql
INSERT INTO users
(user_first_name, user_last_name, user_email_id, user_password, user_role, is_active)
VALUES
('Gordan', 'Bradock', 'gbradock0@barnesandnoble.com', 'h9LAz7p7ub', 'U', true),
('Tobe', 'Lyness', 'tlyness1@paginegialle.it', 'oEofndp', 'U', true),
('Addie', 'Mesias', 'amesias2@twitpic.com', 'ih7Y69u56', 'U', true)
* postgresql://itversity_sms_user:***@pg.itversity.com:5432/itversity_sms_db 3 rows affected.
Out[14]:
[]
In [15]:
%sql SELECT * FROM users
* postgresql://itversity_sms_user:***@pg.itversity.com:5432/itversity_sms_db 6 rows affected.
Out[15]:
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-03-09 12:15:51.066866 | 2022-03-09 12:15:51.066866 |
2 | Donald | Duck | donald@duck.com | False | None | U | False | 2022-03-09 12:15:55.692717 | 2022-03-09 12:15:55.692717 |
3 | Mickey | Mouse | mickey@mouse.com | False | None | U | True | 2022-03-09 12:15:58.617705 | 2022-03-09 12:15:58.617705 |
4 | Gordan | Bradock | gbradock0@barnesandnoble.com | False | h9LAz7p7ub | U | True | 2022-03-09 12:16:02.576284 | 2022-03-09 12:16:02.576284 |
5 | Tobe | Lyness | tlyness1@paginegialle.it | False | oEofndp | U | True | 2022-03-09 12:16:02.576284 | 2022-03-09 12:16:02.576284 |
6 | Addie | Mesias | amesias2@twitpic.com | False | ih7Y69u56 | U | True | 2022-03-09 12:16:02.576284 | 2022-03-09 12:16:02.576284 |