# Know the Basic PostgreSQL

## What is PostgreSQL?

PostgreSQL is a relational database which uses `SQL`, SQL is structured query language that allows us to communicate with the database and make changes using series of keywords.

Relational database means, data is stored in the form of rows and columns whereas non-relational database stores data in unstructured format. Generally, rows are called as entities and columns as attributes. Each column represents some information about entities.

## Basic queries in PostgreSQL

**Note:** In PostgreSQL, all keywords are conventionally written in capital letters. Although PostgreSQL is case-insensitive to keywords, using uppercase improves readability and helps distinguish them from other identifiers.

### Comments :

Comments are ignorable part for the execution, it improves readability, helps others (and your future self) understand the purpose of the code.

```bash
-- something in here --
```

### Data Types :

Data types specify, which type of data we are storing in our database. This is one of the benefits of using PostgreSQL, as it allows us to define data types explicitly, preventing the storage of invalid or incompatible data.

Common data types:

1. INT - Stores integer values.
    
2. TEXT - Stores text which has no constraint over its size.
    
3. VARCHAR - Stores text which has no constraint over its size.
    
4. SERIAL - Integer type, automatically add 1 to the specified column.
    

1. ### CREATE a table
    

```bash
CREATE TABLE "user"(
	id SERIAL PRIMARY KEY,
	name VARCHAR(255),
	email VARCHAR(255),
	password TEXT,
	age INT
)
```

This is the syntax for creating a table. “user” is the name of our table and our column names are written in small letters and keywords are in capital letters.

2. ### INSERT values
    

```bash
INSERT INTO "user"(name, email, password, age)
VALUES(
    'John', 
    'john12345@gmail.com', 
    'jhfiifiueowiew00223', 
    20
);
```

The syntax for inserting values into a database specifies the table name, its attributes, and the corresponding values to be stored.

3. ### Fetching data
    

```bash
SELECT * FROM "user"
```

Fetches all the rows from the “user” table.

```bash
SELECT name, email FROM "user"
```

Fetches particular column data of all the rows.

```bash
SELECT * FROM "user" WHERE id = 1
SELECT * FROM "user" WHERE age > 20
```

Fetches specified row data, here conditions have been applied it displays the data from database whose `id` is 1. And next line fetches the data of people who are more than 20 years old.

4. ### UPDATE
    

```bash
UPDATE "user" SET age = 19 WHERE id = 1;
```

If we want to update the data after being stored, we can achieve this using the above query.

5. ### DELETE data
    

```bash
DELETE FROM "user" WHERE id = 4;
DELETE FROM "user";
```

This query deletes the row of id 4. If we execute the second line, all the data (rows) will get deleted. So, make sure you apply condition.

## Conclusion

In conclusion, these basic CRUD operations in PostgreSQL form the foundation for interacting with a database. With this knowledge, we can perform a wide range of database operations effectively.
