EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL String Array

By Sohel SayyadSohel Sayyad

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL String Array

PostgreSQL String Array

Introduction to PostgreSQL String Array

PostgreSQL provides different types of data types. String Array is one of the data types provided by PostgreSQL, array is a user-defined data type or built-in data type. PostgreSQL arrays play an important task in the database system, PostgreSQL provides a facility to define columns as an array with any valid data type, array should be integer [] type, character [] type. Sometimes we need to create our own data type. At that time, PostgreSQL created an equivalent array data type in the backend. We can perform different operations on strings using an array of data types.

Syntax

create table                            table_name                           (column 1 data type [], column 2 data type []………..,                                                  column n data type[]);

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Explanation

In the above syntax, we can declare a String Array data type at the time of table creation. Where table name is the specified table name that we need to create and column 1, column 2, and column n declared with the data type of array and it separated by using a comma.

How to create a String Array in PostgreSQL?

  1. We must install PostgreSql in your system.
  2. We required basic knowledge about PostgreSql.
  3. We must require tables to perform String Array operation.

Operations of String Array

We can perform different operations on String Array as follows.

To perform an Array operation we need a table, so first, we create a table using the following statement.

CREATE TABLE                        customer                    (cust_id serial PRIMARY KEY,
cust_name VARCHAR (100),                                             cust_phones TEXT []);

Explanation:

In the above statement, we create a table name as a customer with three columns such as cust_id with data type is an integer, cust_name with data type is varchar, and cust_phones with data type is text (string).

1. Insert String Array Operation

In this operation, we can insert a String Array into the table using the following syntax.

Syntax

Insert into                   table name                 (column name1, column name 2, ………….column nameN) values (value1 ARRAY[], value2 ARRAY[],…………valueN );

Explanation

In the above syntax where insert into is a keyword, column name1, column name2 is a specified column name in the table, and values mean actual string array values which we need to insert into the table.

Example

Now perform the insertion operation on the above table using the following statement.

Insert into                   customer                     ( cust_name,              cust_phones)
values                          ('John',             ARRAY [ '(206)-678-1233',      '(303)-567-9876' ]);

Explanation

In the above statement, we insert a record into the table. See the customer name is john and we insert two phone numbers for john customers. The phone is actually an integer value but in this example, we declared it as a string (Text).

We can perform insertion operation with String Array using curly braces using the following statement.

Insert               into                 customer                    (cust_name,               cust_phones)
Values             ('Jacson','{"(204)-123-3452"}'),       ('Paul','{"(222)-654-0979","(205)-756-13345"}');
select * from customer;

Explanation

In the above statement, the customer Jacson has a single phone number and the customer Paul has two phone numbers and we use curly braces to construct String Array. Illustrate the result of the above declaration by using the use of the following snapshot.

Popular Course in this category
Sale
PostgreSQL Course (2 Courses, 1 Project)2 Online Courses | 1 Hands-on Project | 7+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (6,848 ratings)
Course Price

View Course

Related Courses

PostgreSQL String Array output 1

2. Display String Array Operation

In this operation, we display the Array String using the following syntax.

Syntax

select               column name              from               table name ;

Explanation

In the above syntax, we use a select clause to retrieve the data from the table where the column name is specified column name in the table which we need to display and table name is specified table name from the database.

Example

  • Suppose users need to show a single phone number of a customer, so at that time we use the following statement.

select             cust_name,                cust_phones [ 1 ]        from   customer;

Explanation

We can access any single array by using subscript brackets []. But the default value of the subscript bracket is one in PostgreSQL. Illustrate the result of the above declaration by using the use of the following snapshot.

PostgreSQL String Array output 2

  • In some cases, users need a second phone number from the table so use the following statement.

select               cust_name,                 cust_phones [ 2 ]        from               customer;

Illustrate the result of the above declaration by using the use of the following snapshot.

PostgreSQL String Array output 3

3. Update String Array Operation

PostgreSQL also provides a facility to update String Array (single or whole array).

Syntax

update               tableName                set                               columnName[] =’value’
where             condition;

Explanation

In the above syntax, we use the update command where table name means specified table name, set is the keyword, the column name is the specified column name which we need to update and where is used to condition.

Example

Single String Array Update
update            customer         set       cust_phones [1] = '(707)-111-2222'
where cust_id = 1;
select * from customer;

Explanation

In the above example, we update the first phone number in String Array whose cust_id is 1. Illustrate the result of the above declaration by using the use of the following snapshot.

output 4

Similarly, we can update the second phone number of a customer using the following statement.

update            customer         set       cust_phones [2] =                  '(606)-555-7643'
where cust_id = 1;
select * from customer;

Illustrate the result of the above declaration by using the use of the following snapshot.

output 5

4. Whole String Array Update

In this operation, we update the whole String Array.

Example

Suppose we need to update the whole String in the table at that time we use the following statement to update the whole string in PostgreSQL.

update customer                    set                   cust_phones = '{"(909)-555-11111"}'
where                          cust_id = 1;
select * from customer;

Explanation

In the above example, we update the whole String Array. Illustrate the result of the above declaration by using the use of the following snapshot.

output 6

5. Expand String Array Operation

PostgreSQL provides the unnest array function to expand the whole String Array.

Syntax

select              column name1,                      column name2,                      unnest(String Array column) from table name;

Example

Suppose we need to expand all customers’ phone numbers at that time we use the following statement.

select              cust_id ,           cust_name,                 unnest             (cust_phones)             from               customer;

Illustrate the result of the above declaration by using the use of the following snapshot.

output 7

Conclusion

We hope from this article you have understood about PostgreSQL String Array statement. From the above article, we learn the basic syntax of String Array as well as different String Array operations with different examples. From this article, we learn how we can implement a String Array operation.

Recommended Articles

This is a guide to PostgreSQL String Array. Here we discuss How to create a String Array in PostgreSQL along with syntax, examples, and explanation. You may also have a look at the following articles to learn more –

  1. PostgreSQL Stored Procedures
  2. PostgreSQL Extensions
  3. PostgreSQL Clone Database
  4. PostgreSQL DDL

PostgreSQL Course (2 Courses, 1 Project)

2 Online Courses

1 Hands-on Project

7+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Independence Day Offer - PostgreSQL Course (2 Courses, 1 Project) Learn More