Re: How to store variable data in a field?

From: Bartosz Dmytrak <bdmytrak(at)eranet(dot)pl>
To: lopes80andre(at)gmail(dot)com
Cc: pgsql-general(at)postgresql(dot)org, spam_eater(at)gmx(dot)net
Subject: Re: How to store variable data in a field?
Date: 2012-02-21 20:33:51
Message-ID: CAD8_UcafnveJjVhmRt=UZLnUqbb=_x79uqLYjmtuA5iOxybtpQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,
I am going to start with quotation: "*PostgreSQL is a powerful, open
source object-relational
database system.*"

So let's use objects (TYPES):
First You have to create proper types:

CREATE TYPE "Facebook" AS
(account_name text,
fb_special_hash text,
fb_security_hash text,
fb_extended_hash text);

CREATE TYPE "Twitter" AS
(account_name text,
hash1 bigint, --assume this is bigint not text
megahash text);

Next You have to modify Your table to handle types:
ALTER TABLE public.account ADD COLUMN "TweeterInfo" "Twitter";
ALTER TABLE public.account ADD COLUMN "FacebookInfo" "Facebook";

there is nothing special, You simply use Your types as column types.

Next few examples of using types:
insert into table
INSERT INTO public.account(
id_account, id_account_type, n_account, account_details,
comment,
"TweeterInfo", "FacebookInfo")
VALUES (1, 2, 'ABc', 'test', 'test comment',
ROW('xpto', 3432454355, 'dfcf786fds987fds897'), ROW('xpto', 'dsdsad4535',
'dsadsad454355', 'sdasfe5r4536556fsgg'));

And SELECT examples:
SELECT *
FROM public.account
WHERE (account."TweeterInfo").account_name = 'xpto';

SELECT (account."TweeterInfo").megahash
FROM public.account
WHERE (account."TweeterInfo").account_name = 'xpto';

You can find more in doc:
http://www.postgresql.org/docs/9.1/static/rowtypes.html
http://www.postgresql.org/docs/9.1/static/sql-createtype.html

To be noticed:
using hstore is good idea too - this type is dedicated to be used with
key-value pairs with existing set of build-in functions.

Custom type gives You possibility to create table of this type or function
returning this type as row or set of rows.

More SQL oriented solution is to create one/two new tables to store these
values, or extend table account with few new columns (if all of them are
required).

Regards,
Bartek

2012/2/21 Thomas Kellerer <spam_eater(at)gmx(dot)net>

> Andre Lopes, 21.02.2012 16:11:
>
> Hi all,
>>
>> I need to create a table to store Twitter accounts information,
>> Facebook and other social networks details for API login. I need to
>> know if it is possible to store the account details("account_details"
>> field) in a field that contains something like an array. I need to
>> store this data in an array like field because the details for each
>> social network accounts are different. What is my best choice for the
>> field "account_details"?
>>
>> CREATE TABLE "account" (
>> "id_account" int4 NOT NULL,
>> "id_account_type" int4 NOT NULL,
>> "n_account" varchar(50) NOT NULL,
>> "account_details" varchar NOT NULL,
>> "comment" varchar(2000),
>> PRIMARY KEY("id_account","id_account_**type")
>> );
>>
>> I will need to store something like this:
>>
>> Twitter: array("account_name" => "xpto", "hash1" => "3432454355",
>> "megahash" => "dfcf786fds987fds897")
>> Facebook: array("account_name" => "xpto", "fb_special_hash" =>
>> "dsdsad4535", "fb_security_hash" => "dsadsad454355",
>> "fb_extended_hash" => "sdasfe5r4536556fsgg")
>>
>> It is possible to put something like that in a field? If yes, what
>> datatype should I choose? Pros and cons of doing this?
>>
>
> Use the hstore datatype. That's exactly what you are looking for
>
> http://www.postgresql.org/**docs/current/static/hstore.**html<http://www.postgresql.org/docs/current/static/hstore.html>
>
>
>
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/**mailpref/pgsql-general<http://www.postgresql.org/mailpref/pgsql-general>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jeff Herman 2012-02-21 21:53:21 Re: Combining data from Temp Tables
Previous Message Steve Crawford 2012-02-21 19:49:27 Re: How to split up phone numbers?