PostgreSQL - Day 2
Learn PostgreSQL - Day 2
Keep learning PostgreSQL by Learn PostgreSQL Tutorial - Full Course for Beginners.
7. How to Create Database
- get help with
help
- list databases
\l
- create database
CREATE DATABASE test;
- tip: using UPPERCASE syntax for SQL commands can increase readablility
8. How to Connect to Database
- connection with psql
psql -h localhost -p 5432 -U <username> <database_name>
- in psql, call
\c
command to connect database
9. A Very Dangerous Command
DROP DATABASE [name];
This command will delete all the data in the database and itself.
10. Creating Tables Without Constraints
CREATE TABLE table_name (
<column name> <data type> [constrains]
)
-
check tables in database
\d
-
check description of table
\d <table name>
all data types supported by Postgres
https://www.postgresql.org/docs/current/datatype.html
11. Creating Tables with Constraints
Specify constraints to avoid inserting an incomplete entry.
NOT NULL
PRIMARY KEY
There is a person_id_seq
sequence
.
This sequence
is for BIGSERIAL
remembering the current value.
\dt
to show table only
12. How to insert records into tables
- use
INSERT INTO
command - specify the columns we want to insert.
- Set values by an array of values matching the columns
You don’t have to manage the value of auto-incremental columns.
- What does
0 1
means? It’s oid and count, spec- oid is a deprecate feature.