PL-SQL
Oracle PL/SQL LOOP with Example
What are Loops? Loops allows a certain part of the code in a program to get executed for the...
In PostgreSQL, we can define a column as an array of valid data types. The data type can be built-in, user-defined, or enumerated type. Other than this, arrays play an important role in PostgreSQL.
Every corresponding PostgreSQL data type comes with a relevant array type. For example, the integer data type has the integer[] array type, the character data type has the character[] array type, etc.
In this PostgreSQL tutorial, you will learn:
In the following example, we will create a table named Employees with the contact column defined as a text array:
CREATE TABLE Employees ( id int PRIMARY KEY, name VARCHAR (100), contact TEXT [] );
The Command should run successfully.
Let us now insert values into the above table:
INSERT INTO Employees
VALUES
(
1,
'Alice John',
ARRAY [ '(408)-743-9045',
'(408)-567-7834' ]
);
The insertion should run successfully.
The values of the third column, that is, contact, have been inserted as an array. This has been achieved by use of the ARRAY constructor.
In this example, we have enclosed them within square brackets []. We have two contacts for the employee Alice John.
It is still possible for us to use curly braces {} as shown below:
INSERT INTO Employees
VALUES
(
2,
'Kate Joel',
'{"(408)-783-5731"}'
),
(
3,
'James Bush',
'{"(408)-745-8965","(408)-567-78234"}'
);
The command should run successfully.
The above statements will insert two rows into the Employees table. When using curly braces, the array is wrapped inside single quotes (') while the text array items are wrapped within double quotes (").
To query the elements of an array, we use the SELECT statement.
To see the contents of our Employees table, we run the following command:
SELECT * FROM Employees;
This returns the following:
The elements of the array column, that is, contact, have been enclosed within curly braces {}.
To access the array elements themselves, we add a subscript within square brackets []. The first element in an array is at position 1.
For example, we need to get the names of employees and only their first contact for those employees with more than one contact. We can access this as contact[1].
Let us see this:
SELECT name, contact[1] FROM Employees;
This will return the following:
We can use the SELECT statement together with the WHERE clause to filter rows based on the array column.
For example, to see the employee with (408)-567-78234 as the second contact, we can run the following command:
SELECT name FROM Employees WHERE contact [ 2 ] = '(408)-567-78234';
This will return the following:
You can update all or a single element of an array.
Here are the contents of the Employees table:
Let us update the second phone number of the employee James Bush, whose id is 3:
Run the following command:
UPDATE Employees SET contact [ 2 ] = '(408)-589-89347' WHERE id = 3;
The command should run successfully:
Let us query the table to check whether the change was successful:
The change was successful.
Currently, our Employees table is as follows:
Suppose we need to know who owns the contact (408)-783-5731 regardless of the position within the contact array, we can use the ANY() function as shown below:
SELECT name, contact FROM Employees WHERE '(408)-783-5731' = ANY (contact);
This will return the following:
We can split the values of an array into rows. This process is known as array expansion.
In the example of the Employees table, there are some employees with two contacts in the contact array. We can split these into separate rows.
PostgreSQL provides the unnest() function that can be used for this.
For example:
SELECT name, unnest(contact) FROM Employees;
This will return the following:
The employees Alice John and James Bush, have two contacts. We can split into separate rows.
To accomplish the same through pgAdmin, do this:
Step 1) Login to your pgAdmin account.
Step 2)
Step 3) Type the query in the query editor to create the Employees table:
CREATE TABLE Employees ( id int PRIMARY KEY, name VARCHAR (100), contact TEXT [] );
Step 4) Click the Execute button.
Step 1) Type the following query in the query editor:
INSERT INTO Employees
VALUES
(
1,
'Alice John',
ARRAY [ '(408)-743-9045',
'(408)-567-7834' ]
);
Step 2) Click the Execute button:
Step 3)
To use curly braces in the query
Step 1) Type the following query in the query editor:
INSERT INTO Employees
VALUES
(
2,
'Kate Joel',
'{"(408)-783-5731"}'
),
(
3,
'James Bush',
'{"(408)-745-8965","(408)-567-78234"}'
);
Step 2) Click the Execute button:
Step 1) To see the contents of the Employees table, type the following query on the query editor:
SELECT * FROM Employees;
Step 2) Click the Execute button:
It should return the following:
Step 3) To see the first contacts of employees:
SELECT name, contact[1] FROM Employees;
It should return the following:
Step 4) To combine the SELECT statement with the WHERE clause:
SELECT name FROM Employees WHERE contact [ 2 ] = '(408)-567-78234';
It should return the following:
Step 1) To update the second contact of user with id of 3, run the following command:
UPDATE Employees SET contact [ 2 ] = '(408)-589-89347' WHERE id = 3;
Step 2) Click the Execute button.
Step 3)
1.Type the following command on the query editor to check whether the change was successful:
SELECT * FROM Employees;
2.Click the Execute button.
It should return the following:
Step 1) Type the following query in the query editor:
SELECT name, contact FROM Employees WHERE '(408)-783-5731' = ANY (contact);
Step 2) Click the Execute button.
It should return the following:
Step 1) Type the following query in the query editor:
SELECT name, unnest(contact) FROM Employees;
Step 2) Click the Execute button.
It should return the following:
Download the Database used in this Tutorial
What are Loops? Loops allows a certain part of the code in a program to get executed for the...
Data types in SQLite are different compared to other database management system. In SQLite, you...
What is Trigger in PL/SQL? TRIGGERS are stored programs that are fired by Oracle engine...
Sorting Results Using the SELECT command, results were returned in the same order the records were...
What are functions? MySQL can do much more than just store and retrieve data . We can also perform...
WHAT IS THE ALTER COMMAND? As the saying goes Change is the only constant With time business...