Using meta-data for foreign key?

Поиск
Список
Период
Сортировка
Искать

Using meta-data for foreign key?

От:
Mike Blackwell <maiku41@sbcglobal.net>
Дата:
I have an existing table in an app, along the lines of:

CREATE TABLE foo (
  name text,
  address text,
  some_numeric_info integer,
  
);

I now need to be able to associate additional information (e.g. printing
order) with each field.  Is it a bad idea to use the (fully qualified)
field name as a foreign key to link back to the table meta-data?  Is
there a better way?

Re: Using meta-data for foreign key?

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Mike Blackwell  writes:
> My question, then, is if it considered acceptable practice to, instead
> of having field_name in the secondary table, have a foreign key
> reference back to the field definition in the meta-data.

If you mean a foreign key reference into the system catalogs, we don't
support that :-(

			regards, tom lane

Re: Using meta-data for foreign key?

От:
"Roberts, Jon" <Jon.Roberts@asurion.com>
Дата:
> I have an existing table in an app, along the lines of:
> >>
> >> CREATE TABLE foo (
> >>  name text,
> >>  address text,
> >>  some_numeric_info integer,
> >>  
> >> );
> >>
> 
> 
> I essentially need another table
> 
> CREATE TABLE foo_printing_options (
>   field_name text,
>   print_order int not null,
>   suppress boolean not null,
>   
> );
> 
> 
> The values of field_name in foo_printing_options would be the
individual
> field names in foo (i.e. name, address, etc).   foo_printing_options
> would be available to the user via the app to allow them to specify
> report layout.
> 
> My question, then, is if it considered acceptable practice to, instead
> of having field_name in the secondary table, have a foreign key
> reference back to the field definition in the meta-data.  This would
> allow the app to be less sensitive to schema changes (fairly common).
> 

Yes, of course you want to normalize your data in an operational
application and when doing so, you will want to use primary and foreign
keys.

CREATE TABLE foo (
id integer primary key not null,
name text,
address text,
some_numeric_info integer,
);


CREATE TABLE foo_printing_options (
id integer primary key not null,
foo_id integer not null,
field_name text,
print_order int not null,
suppress boolean not null,

> );

alter table foo_printing_options 
add foreign key (foo_id) references foo (id);


I can also see you adding a field_name table, a unique key on
printing_options.id and printing_options.print_order.  Name and address
in foo makes me think you should probably have a customer table (or is
foo the customer)?  What if your customer can have multiple addresses?
You'll need just an address table in that case.  A State table is likely
needed with maybe zip codes or cities.  

> Is linking to the metadata from the app tables directly bad?  Unsafe?
Ok?
> 

Using the term "metadata" is misleading in your example.  Metadata is
data about data and in your example, you just have data.  It is fine to
join tables in an RDBMS.



Jon

Re: Using meta-data for foreign key?

От:
"David Wilson" <david.t.wilson@gmail.com>
Дата:
On Thu, Apr 17, 2008 at 1:55 PM, Roberts, Jon  wrote:
>  Using the term "metadata" is misleading in your example.  Metadata is
>  data about data and in your example, you just have data.  It is fine to
>  join tables in an RDBMS.
>

I believe you missed the OP's actual goal. He's not interested in
linking printing options to specific rows of the foo table; he wants
each column of the foo table to have a single entry in the printing
options table; that is, he wants a foreign key reference to the system
catalog giving the columns of entity foo. He does, in fact, appear to
be interested in a foreign key reference to a table's metadata.

-- 
- David T. Wilson
david.t.wilson@gmail.com
FAQ