PostgreSQL - TABLE Operations

Search for a command to run...

No comments yet. Be the first to comment.
In order to become familiar with SQL and SQL-based platforms, one have to gain basic knowledge about some of the basic concepts. Here I will list what seems to be to be important to know before jumping to learn any SQL-based platform. These concepts ...

Install PostgreSQL on your system. you can find the installers for various platforms in this link. Difference between PSQL and SQL commands First of all, lets differentiate between PostgreSQL and SQL commands: Any command starting with backslash \ i...

Recently I finished a specialization named "Data Visualization with Tableau Specialization" in Coursera, and I believe it was a very comprehensive review of what a person needs to know to start to work with Tableau and also design principles a person...

In order to use SQL-based databases, one needs to become familiar with SQL clauses, keywords and queries. In this post we are going through some of the most basic SQL clauses and keywords using simple examples. SQL Comments: You can write comments in...

Lets review all of the Table-related functions in one place.
Top create a table in your database use the CREATE TABLE command. Here, we define the schema of a table. We create users table which has two attributes/fields/columns, name and email. Each of these attributes are defined as character strings with max of 128 characters
people=> CREATE TABLE users(
people(> name VARCHAR(128),
people(> email VARCHAR(128)
people(> );
CREATE TABLE
people=>
To list tables/relations in the database use to he following command:
people=> \dt
Did not find any relations.
if you haven't created any tables in your database yet, you will get "Did not find any relations." message. Otherwise, you will get list of existing tables like what follows:
people=> \dt
List of relations
Schema | Name | Type | Owner
--------+-------+-------+-------
public | users | table | pg4e
(1 row)
people=>
Lets suppose we have forgotten what was the schema (the definition of the table and its attributes/columns), and we want to review the schema one more time. In this case we can use the following command:
people=> \d+ users
Table "public.users"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
--------+------------------------+-----------+----------+---------+----------+-------------+--------------+-------------
name | character varying(128) | | | | extended | | |
email | character varying(128) | | | | extended | | |
Access method: heap
people=>
Suppose we wan to insert several rows/objects into the table. We have to use the following command:
people=> INSERT INTO users (name, email) VALUES ('Peter', 'peter@example.edu');
INSERT 0 1
people=> INSERT INTO users (name, email) VALUES ('Terry', 'terry@example.edu');
INSERT 0 1
people=> INSERT INTO users (name, email) VALUES ('Eliza', 'eliza@example.edu');
INSERT 0 1
people=> INSERT INTO users (name, email) VALUES ('John', 'john@example.edu');
INSERT 0 1
In order to read the rows in a table we should use SELECT command:
people=> SELECT * FROM users;
name | email
-------+-------------------
Peter | peter@example.edu
Terry | terry@example.edu
Eliza | eliza@example.edu
John | john@example.edu
(4 rows)
We can include some criteria in the SELECT command as well:
people=> SELECT * FROM users WHERE email='terry@example.edu';
name | email
-------+-------------------
Terry | terry@example.edu
(1 row)
In order to update one/more piece(s) of information in one/more row(s), we use UPDATE command like what follows:
people-> UPDATE users SET name='Johnny' WHERE email='john@example.edu';
UPDATE 1
Hint: this command will update any number of rows that meet its criteria.
Lets see how this command changed the last entry of users table:
people=> SELECT * FROM users;
name | email
--------+-------------------
Peter | peter@example.edu
Terry | terry@example.edu
Eliza | eliza@example.edu
Johnny | john@example.edu
(4 rows)
To remove one/more row(s) from a table, use the DELETE command like what follows:
people=> DELETE FROM users WHERE email='terry@example.edu';
DELETE 1
Hint: this command will delete any number of rows that meet its criteria.
Lets see how this command removed an entry of users table:
people=> SELECT * FROM users;
name | email
--------+-------------------
Peter | peter@example.edu
Eliza | eliza@example.edu
Johnny | john@example.edu
(3 rows)
What happens if you missed something in the time of creation of a table in database? Is it over? ABSOLUTELY NOT! SQL lets us change the table schema in the times needed.
When you use ALTER TABLE and change the definition of one field, a SQL-based database applies that change to both the table's schema and to all of the existing data in that table, all while the system is running and functional. So there are a couple of actions like add, change, drop a column that you can do using ALTER TABLE key words. Lets suppose that we have created the following table and we want to make changes to this table.
CREATE TABLE users(
name VARCHAR(128),
email VARCHAR(128)
);
First, lets take a look at the table's schema:
people=# \d+ users
Table "public.users"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
--------+------------------------+-----------+----------+---------+----------+-------------+--------------+-------------
name | character varying(128) | | | | extended | | |
email | character varying(128) | | | | extended | | |
Access method: heap
In order to add a column with " address VARCHAR(128) " definition to the users table, we have to use the following code. You can see the changes into the table's schema
people=# ALTER TABLE users ADD COLUMN address VARCHAR(128);
ALTER TABLE
people=# \d+ users
Table "public.users"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
---------+------------------------+-----------+----------+---------+----------+-------------+--------------+-------------
name | character varying(128) | | | | extended | | |
email | character varying(128) | | | | extended | | |
address | character varying(128) | | | | extended | | |
Access method: heap
To change type of address column from VARCHAR(128) to TEXT, we have to use to following code:
people=# ALTER TABLE users ALTER COLUMN address TYPE TEXT;
ALTER TABLE
people=# \d+ users
Table "public.users"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
---------+------------------------+-----------+----------+---------+----------+-------------+--------------+-------------
name | character varying(128) | | | | extended | | |
email | character varying(128) | | | | extended | | |
address | text | | | | extended | | |
Access method: heap
In order to drop the address column of users table, we have to use the following code:
people=# ALTER TABLE users DROP address;
ALTER TABLE
people=# \d+ users
Table "public.users"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
--------+------------------------+-----------+----------+---------+----------+-------------+--------------+-------------
name | character varying(128) | | | | extended | | |
email | character varying(128) | | | | extended | | |
Access method: heap
What happens if we wanted to update content of one/several row(s)? In this case we can you UPDATE key word.
First Lets take a look at the current contents of the users table:
people=# SELECT * FROM users;
name | email
--------+-------------------
Peter | peter@example.edu
Eliza | eliza@example.edu
Johnny | john@example.edu
Terry | terry@example.edu
(4 rows)
Now, lets change the email address of the user named Johnny, and watch the results:
people=# UPDATE users SET email='johnny@example.edu' WHERE name='johnny';
UPDATE 1
people=# SELECT * FROM users;
name | email
--------+--------------------
Peter | peter@example.edu
Eliza | eliza@example.edu
Terry | terry@example.edu
Johnny | johnny@example.edu
(4 rows)