Re: [SQL] Advice on key design
The reason is simple - as you need the artificial PK lpp_id, then everything else becomes an constraint Thanks, Anton On Jul 24, 2013, at 0:28, JORGE MALDONADO wrote: >> In your case it would be lpp_id as PK, and >> lpp_person_id,lpp_language_id as unique constraint >> >> Thanks, >> Anton Is there a reason to do it the way you suggest? Regards, Jorge Maldonado On Tue, Jul 23, 2013 at 5:02 PM, Anton Gavazuk wrote: > Hi Jorge, > > In your case it would be lpp_id as PK, and > lpp_person_id,lpp_language_id as unique constraint > > Thanks, > Anton > > On Jul 23, 2013, at 23:45, JORGE MALDONADO wrote: > > > I have 2 tables, a parent (tbl_persons) and a child > (tbl_languages_per_person) as follows (a language table is also involved): > > > > -- > > tbl_persons > > -- > > * per_id > > * per_name > > * per_address > > > > -- > > tbl_languages_per_person > > -- > > * lpp_person_id > > * lpp_language_id > > * lpp_id > > > > As you can see, there is an obvious key in the child table which is > "lpp_person_id + lpp_language_id", but I also need the field "lpp_id" as a > unique key which is a field that contains a consecutive number of type > serial. > > > > My question is: what should I configure as the primary key, > "lpp_person_id + lpp_language_id" or "lpp_id"? > > Is the role of a primary key different from that of a unique index? > > > > With respect, > > Jorge Maldonado > > > > > > > > > > > > >
Re: [SQL] Advice on key design
Agreed with Anton, as PK, lpp_id is easier to be managed than (lpp_person_id + lpp_language_id) One more suggestion, foreign key constraints could be setup on lpp_person_id and lpp_language_id to link with target tables. Thanks, Stan 2013/7/24 Anton Gavazuk > The reason is simple - as you need the artificial PK lpp_id, then > everything else becomes an constraint > > Thanks, > Anton > > On Jul 24, 2013, at 0:28, JORGE MALDONADO wrote: > > >> In your case it would be lpp_id as PK, and > >> lpp_person_id,lpp_language_id as unique constraint > >> > >> Thanks, > >> Anton > > Is there a reason to do it the way you suggest? > > Regards, > Jorge Maldonado > > > On Tue, Jul 23, 2013 at 5:02 PM, Anton Gavazuk wrote: > >> Hi Jorge, >> >> In your case it would be lpp_id as PK, and >> lpp_person_id,lpp_language_id as unique constraint >> >> Thanks, >> Anton >> >> On Jul 23, 2013, at 23:45, JORGE MALDONADO >> wrote: >> >> > I have 2 tables, a parent (tbl_persons) and a child >> (tbl_languages_per_person) as follows (a language table is also involved): >> > >> > -- >> > tbl_persons >> > -- >> > * per_id >> > * per_name >> > * per_address >> > >> > -- >> > tbl_languages_per_person >> > -- >> > * lpp_person_id >> > * lpp_language_id >> > * lpp_id >> > >> > As you can see, there is an obvious key in the child table which is >> "lpp_person_id + lpp_language_id", but I also need the field "lpp_id" as a >> unique key which is a field that contains a consecutive number of type >> serial. >> > >> > My question is: what should I configure as the primary key, >> "lpp_person_id + lpp_language_id" or "lpp_id"? >> > Is the role of a primary key different from that of a unique index? >> > >> > With respect, >> > Jorge Maldonado >> > >> > >> > >> > >> > >> > >> > >
Re: [SQL] Advice on key design
Hi, > I also need the field "lpp_id" as a unique key which is a field that contains a consecutive number of type serial. Are you sure you REALLY need this? It looks like a lot of trouble for nothing, as you now have one thing warranting the unicity of the record (your serial) plus a unique key warranting you do not couple twice the same language with the same person. Normally, you'd rather have something like (person_id) (language_id) (weight) where the primary key is (person_id, language_id) (hence a simple Cartesian product, both elements of the key being FKs to Language and People, or whatever you call your tables) and weight is outside the key, but used for sorting, so you have a preferred language. What would be the rationale behind the serial number? Cheers Bèrto On 23 July 2013 22:44, JORGE MALDONADO wrote: > I have 2 tables, a parent (tbl_persons) and a child > (tbl_languages_per_person) as follows (a language table is also involved): > > -- > tbl_persons > -- > * per_id > * per_name > * per_address > > -- > tbl_languages_per_person > -- > * lpp_person_id > * lpp_language_id > * lpp_id > > As you can see, there is an obvious key in the child table which is > "lpp_person_id + lpp_language_id", but I also need the field "lpp_id" as a > unique key which is a field that contains a consecutive number of type > serial. > > My question is: what should I configure as the primary key, "lpp_person_id > + lpp_language_id" or "lpp_id"? > Is the role of a primary key different from that of a unique index? > > With respect, > Jorge Maldonado > > > > > > > -- == If Pac-Man had affected us as kids, we'd all be running around in a darkened room munching pills and listening to repetitive music.
Re: [SQL] Advice on key design
On Wed, Jul 24, 2013 at 10:38 AM, Bèrto ëd Sèra wrote: > What would be the rationale behind the serial number? > The serial key, also named "surrogate key" is there for management purposes. Imagine one day you find out your database design is wrong and what was unique the day before is no more so, how can you find your records? The idea is to have a surrogate key to save you from real world troubles, and then constraints to implement the database design. I usually use this convention: - primary surrogate keys named pk and defined as primary keys - database design keys named _key and defined with a unique constraint. Luca -- Sent via pgsql-sql mailing list ([email protected]) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql
Re: [SQL] Advice on key design
Hi, It looks heavy, performance-wise. If this is not OLTP intensive you can probably survive, but I'd still really be interested to know ow you can end up having non unique records on a Cartesian product, where the PK is defined by crossing the two defining tables. Unless you take your PK down there is no way that can happen, and even if it does, a cartesian product defining how many languages a user speaks does not look like needing more than killing doubles. So what would be the rationale for investing process into this? Get me right, just trying to understand what you guys are doing. Bèrto On 24 July 2013 10:39, Luca Ferrari wrote: > On Wed, Jul 24, 2013 at 10:38 AM, Bèrto ëd Sèra > wrote: > > > What would be the rationale behind the serial number? > > > > The serial key, also named "surrogate key" is there for management > purposes. Imagine one day you find out your database design is wrong > and what was unique the day before is no more so, how can you find > your records? > The idea is to have a surrogate key to save you from real world > troubles, and then constraints to implement the database design. > > I usually use this convention: > - primary surrogate keys named pk and defined as primary keys > - database design keys named _key and defined with a unique constraint. > > Luca > -- == If Pac-Man had affected us as kids, we'd all be running around in a darkened room munching pills and listening to repetitive music.
Re: [SQL] Listing table definitions by only one command
On Wed, Jul 17, 2013 at 5:29 PM, Carla Goncalves wrote:
> Hi
> I would like to list the definition of all user tables by only one command.
> Is there a way to *not* show pg_catalog tables when using "\d ." in
> PostgreSQL 9.1.9?
>
What do you mean by "user tables"? The execution of \d without any
argument provides the definition of all reachable tables (by mean of
search_path) that are not belonging to the information schema or toast
space, that is:
SELECT n.nspname as "Schema",
c.relname as "Name",
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i'
THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f'
THEN 'foreign table' END as "Type",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','v','S','f','')
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
This kind of queries are hard-coded into the psql program, and
therefore cannot be altered on the fly as far as I know.
One trick could be to define a custom query as a psql variable, let's say:
\set my_d '* from pg_class left join ';
and then do something like
select :my_d;
It's shorter, but it is not the same as a builtin command.
Luca
--
Sent via pgsql-sql mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
Re: [SQL] Advice on key design
On Wed, Jul 24, 2013 at 11:47 AM, Bèrto ëd Sèra wrote: > Hi, > > It looks heavy, performance-wise. If this is not OLTP intensive you can > probably survive, but I'd still really be interested to know ow you can end > up having non unique records on a Cartesian product, where the PK is defined > by crossing the two defining tables. Unless you take your PK down there is > no way that can happen, and even if it does, a cartesian product defining > how many languages a user speaks does not look like needing more than > killing doubles. So what would be the rationale for investing process into > this? You are probably right: you are like to never refactor this kind of design, and this situation using a surrogate key is useless. But what happens if your language is no more uniquely identified by lpp_language_id? Suppose you need to track also the language version and therefore a language is identified by the couple (id, version). In this case you have to refactor two tables: the language one and the person-language join table. Having a surrogate key on both sides allows you to smoothly add such constraint without having to refactor the latter table and ensuring all previous joins still work. Ok, not a really smart example, but the only one that comes into my mind at the moment. Luca -- Sent via pgsql-sql mailing list ([email protected]) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql
Re: [SQL] Advice on key design
Hi, yeah, I am okay with design prudence, just used to be so paranoid about performance that just any possible "one more thing to do" gets me nervous :) Language versions do exist, say Dutch has different orthography depending on what convention is used, so you may well need to suddenly add a further level of definition. But you can pretty much do that by a sequence of alter tables, especially in a situation like this. Oh well, we are discussing principles as applied to a practical situation that we actually ignore, so... It's been a pleasure, but unless we get more detail... not much we can do apart from putting out personal preferences :) Bèrto On 24 July 2013 11:05, Luca Ferrari wrote: > On Wed, Jul 24, 2013 at 11:47 AM, Bèrto ëd Sèra > wrote: > > Hi, > > > > It looks heavy, performance-wise. If this is not OLTP intensive you can > > probably survive, but I'd still really be interested to know ow you can > end > > up having non unique records on a Cartesian product, where the PK is > defined > > by crossing the two defining tables. Unless you take your PK down there > is > > no way that can happen, and even if it does, a cartesian product defining > > how many languages a user speaks does not look like needing more than > > killing doubles. So what would be the rationale for investing process > into > > this? > > > You are probably right: you are like to never refactor this kind of > design, and this situation using a surrogate key is useless. But what > happens if your language is no more uniquely identified by > lpp_language_id? Suppose you need to track also the language version > and therefore a language is identified by the couple (id, version). In > this case you have to refactor two tables: the language one and the > person-language join table. > Having a surrogate key on both sides allows you to smoothly add such > constraint without having to refactor the latter table and ensuring > all previous joins still work. > Ok, not a really smart example, but the only one that comes into my > mind at the moment. > > Luca > -- == If Pac-Man had affected us as kids, we'd all be running around in a darkened room munching pills and listening to repetitive music.
Re: [SQL] monthly statistics
On Mon, Jul 8, 2013 at 2:18 PM, Andreas wrote: > How could I combine those 2 queries so that the date in query 1 would be > replaced dynamically with the result of the series? > Surely I'm missing something, but maybe this is something to work on: WITH RECURSIVE months(number) AS ( SELECT 1 UNION SELECT number + 1 FROM months WHERE number < 12 ) SELECT m.number, s.id, s.name, count( h.state_id ) FROM state s JOIN history h ON s.id = h.state_id JOIN months m ON m.number = date_part( 'month', h.ts ) Luca -- Sent via pgsql-sql mailing list ([email protected]) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql
