How to store variable data in a field?

Started by Andre Lopesabout 14 years ago3 messagesgeneral
Jump to latest
#1Andre Lopes
lopes80andre@gmail.com

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?

Best Regards,

#2Thomas Kellerer
spam_eater@gmx.net
In reply to: Andre Lopes (#1)
Re: How to store variable data in a field?

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

#3Bartosz Dmytrak
bdmytrak@eranet.pl
In reply to: Thomas Kellerer (#2)
Re: How to store variable data in a field?

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@gmx.net>

Show quoted text

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&lt;http://www.postgresql.org/docs/current/static/hstore.html&gt;

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/**mailpref/pgsql-general&lt;http://www.postgresql.org/mailpref/pgsql-general&gt;