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.
-- 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:
INT - Stores integer values.
TEXT - Stores text which has no constraint over its size.
VARCHAR - Stores text which has no constraint over its size.
SERIAL - Integer type, automatically add 1 to the specified column.
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.
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.
SELECT * FROM "user"
Fetches all the rows from the “user” table.
SELECT name, email FROM "user"
Fetches particular column data of all the rows.
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.
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.
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.
About the author
👋Hi there! I'm a B-Tech student specializing in Artificial Intelligence and machine learning. I'm passionate about Tech and web development, currently exploring front-end technologies. I enjoy creating projects and always ready to enhance my skills in the tech field.
Comments
Join the discussionNo comments yet. Be the first to comment.



