postgresql
  1. postgresql-syntax

Syntax - (PostgreSQL Tutorial)

PostgreSQL is a powerful, open-source database system that is widely used for developing complex and high-performance applications. In this tutorial, we'll discuss the syntax used in PostgreSQL.

Syntax

Creating a Table

CREATE TABLE table_name (column1 datatype1, column2 datatype2,...);

Inserting Data into a Table

INSERT INTO table_name (column1, column2,...) VALUES (value1, value2,...);

Selecting Data from a Table

SELECT column1, column2,... FROM table_name WHERE condition;

Updating Data in a Table

UPDATE table_name SET column1 = value1, column2 = value2,... WHERE condition;

Deleting Data from a Table

DELETE FROM table_name WHERE condition;

Joining Tables

SELECT column_name(s) FROM table1 JOIN table2 ON table1.column_name = table2.column_name;

Creating Views

CREATE VIEW view_name AS SELECT column1, column2,... FROM table_name WHERE condition;

Using Aggregate Functions

SELECT COUNT(column_name) FROM table_name WHERE condition;

Example

Let's create a table called users with columns id, name, age and insert some data into it.

CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  age INTEGER
);

INSERT INTO users (id, name, age) VALUES (1, 'John Doe', 30);
INSERT INTO users (id, name, age) VALUES (2, 'Jane Doe', 28);

We can select the data from the users table using the following query:

SELECT * FROM users;

The output of this query will be:

 id |   name    | age
----+-----------+-----
  1 | John Doe  |  30
  2 | Jane Doe  |  28

Explanation

In the above example, we created a table called users with columns id, name, and age. We then inserted some data into the table using the INSERT INTO statement. We then selected data from the table using the SELECT statement.

Use

PostgreSQL is a powerful relational database management system that is widely used for developing complex and high-performance applications. The syntax used in PostgreSQL is essential for creating tables, inserting, updating, and deleting data, creating views, and using aggregate functions.

Important Points

  • Table and column names in PostgreSQL are case-insensitive unless they are enclosed in quotes.
  • The semicolon (;) at the end of a statement is required in PostgreSQL to end the statement.
  • To specify a value as a string in PostgreSQL, enclose it in single quotes ('').
  • PostgreSQL has strict data type checking, so make sure to use the correct data types for each column.

Summary

In this tutorial, we discussed the syntax used in PostgreSQL for creating tables, inserting, updating, and deleting data, creating views, joining tables, and using aggregate functions. We also provided an example demonstrating the usage of these statements. With this knowledge, you can start creating tables and manipulating data in PostgreSQL more effectively.

Published on: