[
Creating Database Table¶
Before getting into action with respect to CRUD operations, we need to prepare tables.
- We can either use
psql
or SQL Workbench to connect to the database and create table directly. - Let us drop and create the table
users
if it already exists. - I am using Jupyter based environment for demo – feel free to use what ever is relevant to your project or comfortable for you.
One need to have decent database and SQL skills to be comfortable with all types of application development. Feel free to Master SQL using Postgresql as target database using this course or playlist.
In [1]:
%load_ext sql
In [2]:
%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 [4]:
%%sql
SELECT * FROM information_schema.tables
WHERE table_catalog = 'itversity_sms_db' AND table_schema = 'public'
LIMIT 10
* postgresql://itversity_sms_user:***@pg.itversity.com:5432/itversity_sms_db 1 rows affected.
Out[4]:
table_catalog | table_schema | table_name | table_type | self_referencing_column_name | reference_generation | user_defined_type_catalog | user_defined_type_schema | user_defined_type_name | is_insertable_into | is_typed | commit_action |
---|---|---|---|---|---|---|---|---|---|---|---|
itversity_sms_db | public | users | BASE TABLE | None | None | None | None | None | YES | NO | None |
In [5]:
%%sql
DROP TABLE IF EXISTS users CASCADE;
* postgresql://itversity_sms_user:***@pg.itversity.com:5432/itversity_sms_db Done.
Out[5]:
[]
In [6]:
%%sql
SELECT * FROM information_schema.tables
WHERE table_catalog = 'itversity_sms_db' AND table_schema = 'public'
AND table_name = 'users'
LIMIT 10
* postgresql://itversity_sms_user:***@pg.itversity.com:5432/itversity_sms_db 0 rows affected.
Out[6]:
table_catalog | table_schema | table_name | table_type | self_referencing_column_name | reference_generation | user_defined_type_catalog | user_defined_type_schema | user_defined_type_name | is_insertable_into | is_typed | commit_action |
---|
In [7]:
%%sql
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
user_first_name VARCHAR(30) NOT NULL,
user_last_name VARCHAR(30) NOT NULL,
user_email_id VARCHAR(50) NOT NULL,
user_email_validated BOOLEAN DEFAULT FALSE,
user_password VARCHAR(200),
user_role VARCHAR(1) NOT NULL DEFAULT 'U', --U and A
is_active BOOLEAN DEFAULT FALSE,
create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_updated_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
* postgresql://itversity_sms_user:***@pg.itversity.com:5432/itversity_sms_db Done.
Out[7]:
[]
- Let us validate the objects that are created in the underlying database. We can either run query against information_schema or use Database Explorer in SQL Workbench or even
psql
.
In [8]:
%%sql
SELECT * FROM information_schema.tables
WHERE table_catalog = 'itversity_sms_db' AND table_schema = 'public'
AND table_name = 'users'
LIMIT 10
* postgresql://itversity_sms_user:***@pg.itversity.com:5432/itversity_sms_db 1 rows affected.
Out[8]:
table_catalog | table_schema | table_name | table_type | self_referencing_column_name | reference_generation | user_defined_type_catalog | user_defined_type_schema | user_defined_type_name | is_insertable_into | is_typed | commit_action |
---|---|---|---|---|---|---|---|---|---|---|---|
itversity_sms_db | public | users | BASE TABLE | None | None | None | None | None | YES | NO | None |
In [10]:
%%sql
SELECT * FROM information_schema.columns
WHERE table_name = 'users'
LIMIT 10
* postgresql://sms_user:***@pg.itversity.com:5432/sms_db 10 rows affected.
Out[10]:
table_catalog | table_schema | table_name | column_name | ordinal_position | column_default | is_nullable | data_type | character_maximum_length | character_octet_length | numeric_precision | numeric_precision_radix | numeric_scale | datetime_precision | interval_type | interval_precision | character_set_catalog | character_set_schema | character_set_name | collation_catalog | collation_schema | collation_name | domain_catalog | domain_schema | domain_name | udt_catalog | udt_schema | udt_name | scope_catalog | scope_schema | scope_name | maximum_cardinality | dtd_identifier | is_self_referencing | is_identity | identity_generation | identity_start | identity_increment | identity_maximum | identity_minimum | identity_cycle | is_generated | generation_expression | is_updatable |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sms_db | public | users | user_id | 1 | nextval(‘users_user_id_seq’::regclass) | NO | integer | None | None | 32 | 2 | 0 | None | None | None | None | None | None | None | None | None | None | None | None | sms_db | pg_catalog | int4 | None | None | None | None | 1 | NO | NO | None | None | None | None | None | NO | NEVER | None | YES |
sms_db | public | users | user_first_name | 2 | None | NO | character varying | 30 | 120 | None | None | None | None | None | None | None | None | None | None | None | None | None | None | None | sms_db | pg_catalog | varchar | None | None | None | None | 2 | NO | NO | None | None | None | None | None | NO | NEVER | None | YES |
sms_db | public | users | user_last_name | 3 | None | NO | character varying | 30 | 120 | None | None | None | None | None | None | None | None | None | None | None | None | None | None | None | sms_db | pg_catalog | varchar | None | None | None | None | 3 | NO | NO | None | None | None | None | None | NO | NEVER | None | YES |
sms_db | public | users | user_email_id | 4 | None | NO | character varying | 50 | 200 | None | None | None | None | None | None | None | None | None | None | None | None | None | None | None | sms_db | pg_catalog | varchar | None | None | None | None | 4 | NO | NO | None | None | None | None | None | NO | NEVER | None | YES |
sms_db | public | users | user_email_validated | 5 | false | YES | boolean | None | None | None | None | None | None | None | None | None | None | None | None | None | None | None | None | None | sms_db | pg_catalog | bool | None | None | None | None | 5 | NO | NO | None | None | None | None | None | NO | NEVER | None | YES |
sms_db | public | users | user_password | 6 | None | YES | character varying | 200 | 800 | None | None | None | None | None | None | None | None | None | None | None | None | None | None | None | sms_db | pg_catalog | varchar | None | None | None | None | 6 | NO | NO | None | None | None | None | None | NO | NEVER | None | YES |
sms_db | public | users | user_role | 7 | ‘U’::character varying | NO | character varying | 1 | 4 | None | None | None | None | None | None | None | None | None | None | None | None | None | None | None | sms_db | pg_catalog | varchar | None | None | None | None | 7 | NO | NO | None | None | None | None | None | NO | NEVER | None | YES |
sms_db | public | users | is_active | 8 | false | YES | boolean | None | None | None | None | None | None | None | None | None | None | None | None | None | None | None | None | None | sms_db | pg_catalog | bool | None | None | None | None | 8 | NO | NO | None | None | None | None | None | NO | NEVER | None | YES |
sms_db | public | users | create_ts | 9 | CURRENT_TIMESTAMP | YES | timestamp without time zone | None | None | None | None | None | 6 | None | None | None | None | None | None | None | None | None | None | None | sms_db | pg_catalog | timestamp | None | None | None | None | 9 | NO | NO | None | None | None | None | None | NO | NEVER | None | YES |
sms_db | public | users | last_updated_ts | 10 | CURRENT_TIMESTAMP | YES | timestamp without time zone | None | None | None | None | None | 6 | None | None | None | None | None | None | None | None | None | None | None | sms_db | pg_catalog | timestamp | None | None | None | None | 10 | NO | NO | None | None | None | None | None | NO | NEVER | None | YES |
In [9]:
%sql SELECT * FROM users
* postgresql://itversity_sms_user:***@pg.itversity.com:5432/itversity_sms_db 0 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 |
---|
]