PostgreSQL hstoreIn this section, we are going to understand the working of the PostgreSQL hstore data type, examples of the hstore data type, and we also see the example of the hstore with WHERE clause, which helps us to handle the hstore data values more resourcefully. We will understand how we can add, update, deleting the key-values pairs in the PostgreSQL hstore data type. We will discuss the following operators, such as -> Operator (to select the data for a specific value), ? Operator (to check for an explicit key in an hstore column), @> operator (to check the key-value pair), ?& and ?|Operator ( to select the rows whose hstore column is having various keys). And also see the usage of avals(), svals(), akeys() skeys(), EACH(), hstore_to_json() functions. What is PostgreSQL hstore Data Type?In PostgreSQL, the next data type that we are going to understand is the hstore, which is used to store the key-value pair in the unit value. It is always the right approach for various cases, like semi-structured data or rows, with serval attributes, which are uncommonly enquired. Note: Basically, the keys and values are the text strings.Syntax of PostgreSQL hstore data typeThe syntax of the PostgreSQL hstore data type is as follows: Before we start learning the hstore data type, we must first allow the hstore extension, which loads the contrib module for our PostgreSQL illustration. Enable PostgreSQL hstore extensionTo enable the PostgreSQL hstore extension for our PostgreSQL design, we can use the CREATE EXTENSION command in the Organization database, as we can see in the following statement: Output After executing the above command, we will get the below message: the hstore extension has been installed successfully with the Create Extension command. Example of PostgreSQL hstore data typeLet us see one sample example to understand how the PostgreSQL hstore data type works. We are creating one new table as Movie and hstore columns with the CREATE command's help and inserting some values using the INSERT command. The Movie table contains the various columns such as Movie_id, Movie_name, Movie_attr and that have the following properties:
For the Movie_attr column, we have used the hstore data type. To create a Movie table into a similar database which is Organization, where we enable the hstore extension, we use the CREATE command as we can see in the following command: Output On executing the above command, we will get the following message, which displays that the Movie table has been created successfully. When the Movie table is created successfully, we will insert some values into it with the INSERT command's help. Inserting a value into PostgreSQL hstore columnTo insert the value into the hstore column, we are using the INSERT command as shown in the below statement: Output After implementing the above command, we will get the following message window, which displays that the specified values have been inserted successfully into the Movie table. Note: As we can see in the above insert command, the values we inserted into the hstore column are a list of comma-separated key => value pairs. And both keys and values are quoted using double quotes (").After creating and inserting the values in the Movie table, we will use the SELECT command to retrieve all data from the Movie table: Selecting values from an hstore columnSelecting the values from an hstore column is equal to selecting a value from a column with native data type with the help of the SELECT command, as shown below: Output After successfully implementing the above command, we will get the below output, which displays all the data present in the Movie table: Selecting data for a specific keyPostgreSQL hstore data type allows us to use the -> operator to select a particular key's data values from an hstore column (Movie_Attr). In the below example, we are using the -> operator to identify the running_time of all available movies in the Movie table, as shown in the below command: Output We will get the following output after implementing the above command, which displays each movie's running_time from the Movie table. Using hstore data value in the WHERE clauseTo filter the rows whose values of the hstore column match the input value. So, for this case, we can use the -> operator in the WHERE clause. Let us see one sample example for our better understanding: In the below example, we are trying to get Movie_name and Movie_genres of the movie, which has Relases_year value matches 2019: Output After implementing the above command, we will get the value of the hstore column using the WHERE clause in the output, as shown below: Retrieve all values from an hstore columnTo retrieve all the data values from the hstore column, we can use the avals() function in arrays. In the following example, we are getting all the values from an hstore column in the Movie table with the help of the avals() function: Output After executing the above command, we will get the following output, which shows all the Moive_attr values available in the Movie table: OR If we want to retrieve the output as a set, we can use the svals() function. In the following command, we are using the svals() function instead of avals() to get the output as a set: Output On implementing the above command, we will get the following output as a set: Retrieve all keys from an hstore columnLike we can get all the values from the hstore column using avals() and svals() function, we can use the akeys() function for retrieving all keys from an hstore column: Output On implementing the above command, we will get the following output, which shows all the key values of the Movie_attr column available in the Movie table: OR If we want that the PostgreSQL retrieve the output as a set, we can use the skey() function. In the following command, we are using the skey() function instead of akeys() to get the output as a set: Output After executing the above command, we will get the following output as a set: Modify hstore data to setsWe can use the EACH() function to modify the hstore data to sets as we can see in the following command: Output We will get the following output after successfully executing the above command, which displays the modified hstore data into sets: Modifying the hstore data to JSONTo change the hstore data to JSON, PostgreSQL offers the hstore_to_json() function. In the below command, we are using the hstore_to_json() function for converting the hstore data value into json: Output We will get the following result after executing the above command, which displays that we have successfully modified the hstore data to the json: Add key-value pairs to current rowsWe can add a new key-value pair to current rows with the hstore column. For example, we can add the Country key to the Movie_attr column of the Movie table as we can see in the following command: Output After executing the above command, we will get the below result, which shows that the three columns have been updated successfully. Now, if we want to see that the "Country"=>"United States" value has been updated successfully. Output We will get the following result on implementing the above command, which displays that the Country Movie_attr value United States has been updated for the specified values. Update current key-value pairWe can update the current key-value pair with the help of the UPDATE command. For example: In the below command, we will update the value of the "Country" key to "USA". Output After implementing the above command, we will get the below message window, which displays that the values have been updated successfully. Deleting the current key-value pairPostgreSQL provides us with deleting the current key-value pair from an hstore column. Let us see one sample example for our better understanding: In the below command, we are deleting the "Country"=>"USA" key-value pair in the Movie_ attr column. Output After implementing the above command, we will get the below message window, which says that specified values have been updated successfully. Check for an explicit key in the hstore columnThe ? operator is used to check for an explicit key in an hstore column within the WHERE clause. In the following example, the below command retrieves all rows with Movie_attr, which contains key Movie_genres. Output We will get the following result on implementing the above command, which shows the specified key in the hstore column. Check for a key-value pairWe can use the @> operator to SELECT the hstore key-value pair. Let us see one example to see the usage of the @> operator: The below command is used to return all the rows containing a key-value pair from the Movie_attr column, and also matches "Language"=>"English". Output After successfully executing the above command, we will get the below result, representing the hstore key-value pair. Selecting rows which having various specified keysThe ?& operator is used to select the rows whose hstore column having various keys. In the following example, we can get Movie where Movie_attr column contains both running_time and rating keys: Output After executing the above command, we will get the below output: Note: We can also use ?| operator in the ?& operator's place to check if a row whose hstore column contains any key from a list of keys.In the following command, we are using ?| operator instead of ?& to get those rows whose hstore column contains any key from a list of keys: Output After implementing the above command, we will get a similar result as compared to the output of ?& operator: OverviewIn the PostgreSQL hstore data type section to the most useful operations, we can perform against the hstore data type. We have learned the following topics:
Next TopicPostgreSQL Array |