Home Homesss Linux HTML/CSS PHP PostgreSQL Node JS React VR Blog T-Shirt Shop

Postgresql Basic Queries







Quick Copy Boxes


PostgresqlBasic Queries

In this lession we will:

✶ Create a Table
✶ Insert Data into the Table
✶ Select Data from the Table
✶ Update Data from the Table




Quick Copy Box: Create Table

Create Table: user_table

Now we will create a table called 'user_table'. To access the psql command line type: psql database_name. My database is named candy so I will type:
psql candy




CREATE TABLE if not exists user_table (

user_id serial,

username VARCHAR (50),

password VARCHAR (100),

email VARCHAR (50),

PRIMARY KEY (user_id)

);
(Hover over bold to highlight the corresponding code)
CREATE TABLE - We are making a table.
if not exisits - If we already have a table by this name it will throw an error.
user_table - This is the name of the table we are creating.
() - We will list our columns inside ().
Column Names
serial - This is like 'auto-increment' in MySQL. It will automatically assign the appropriate integer.
Datatypes
Max length
PRIMARY KEY - user_id is our primary key. Each row of data must have a unique identifier, which is called the primary key.
; - Don't forget to end your statements with a semi-colon!





If you find my tutorials helpful, please consider donating to support my free web development resources.

The more money I raise, the more content I can produce.

Thank you,
Commander Candy

DONATE

Quick Copy Boxes



Insert Data

Next, we will insert a row of data into our user_table.

insert into user_table (username,email,password)
values ('candy_cane','codingcommanders@gmail.com','cupcake');

insert - We are going to insert data!
user_table - This is the name of the table we are putting data into.
coloumn names - Names of the columns that we are inserting values into.
values - Corresponding data values for the columns named.

I am going to insert 1 more row of data:

insert into user_table (username,email,password)
values ('bradley','brad@gmail.com','love');


Update Data

Bradley has decided 'love' is not a good password. He wants to change it to 'Hjjs&&*j$js!'

update user_table
set password = 'Hjjs&&*j$js!'
where
user_id = 2;

Select Data

Now I am going to select all the data in user_table:

SELECT * FROM user_table;

You should see something like this:


SELECT with WHERE clause

Now, I want to select the data row with a username of 'bradley'

SELECT * FROM user_table WHERE username='bradley';

You should see something like this:







Follow Coding Commanders on:

YouTube

GitHub

Facebook

Instagram

Twitter


Coding Commanders Newsletter



Featured Blog:

Previous Lesson | Continue to Project