DEV Community

KENNEDY NDUNGU WANJIRU
KENNEDY NDUNGU WANJIRU

Posted on

Postgres commands

These commands can be classified into group based on their nature
1.Data Definition Language - (DDL)
The commands include
CREATE,ALTER,DROP
2.Data Manipulation Language -(DML)
The commands include
INSERT,UPDATE,DELETE
3.Data Control Language - (DCL)
The commands include
GRANT,GRANT
4.Dta Query Language - (DQL)
The commands include
SELECT
We shall learn these commands through practice,first its important to create a Schema where most of our tables will reside.
schemapostgres

Go ahead and set the path to schema.
setting schema path
Create a table with the following syntax.

create table syntax

create the following 2 tables.

exam results

subects table

INSERT INTO command is used to insert data into tables.When inserting data into a table its important to specify the columns
in chronological order.Make sure the data being inserted is presented in the correct way.Date,texts and phone_numbers should be in single quotes.Integers,decimals and percentages should not be in single quotes.
Inserting info into the database

To confirm if the tables have data run the following commands.

confirmation commands
It's evident that we have 3 tables exams_result,students and subjects table.

In the comment section(/* ...*/) of the data definition language
,there is a syntax showing how to write such a query.
Data definition language like alter table can be used to rename
a table,rename a column,change dat type,remove a column and add a column.
data deinition language

I have also outlined the syntax for the where clause in the comment section.The where clause can be used in a filtering context and combining logic.On combining logic we use different operators such as between,and,or,greater than,less than and eaqual to.
where clause

Count function can be used in different way.First to count the number of rows in the dataset with the following syntax SELECT COUNT(*) FROM table_name;.Second to count the number of values in a certain column with the following syntax SELECT COUNT(column_name) FROM table_name WHERE condition;
count

The CASE expression is used to define different results based on specified conditions in an SQL statement.
The CASE expression goes through the conditions and stops at the first match (like an if-then-else statement). So, once a condition is true, it will stop processing and return the result. If no conditions are true, it returns the value in the ELSE clause. If there is no ELSE clause and no conditions are true, it returns NULL.
case when

Top comments (1)

Collapse
 
ken_ndungu profile image
KENNEDY NDUNGU WANJIRU •

Hello everyone could you help critique my work.