![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have a table and each row is a company - the name of the company, the website, etc. What I would like is one of the entries to be a list of products that the company sells - and here's the important part - I want the list of items to be set up so that each item can have its own description in another table. If I just make the list of products a comma separated list, that won't link to anything properly. Do I need to do something like create a table for every company, so each table can then contain a list of items? If I did that, how would I link each table to the "products entry" of the corresponding company? There's probably a better way to do this than anything I've mentioned, so any suggestions are welcome. (I'm using openoffice base 2.2) |
#3
| |||
| |||
|
|
I have a table and each row is a company - the name of the company, the website, etc. What I would like is one of the entries to be a list of products that the company sells - and here's the important part - I want the list of items to be set up so that each item can have its own description in another table. If I just make the list of products a comma separated list, that won't link to anything properly. Do I need to do something like create a table for every company, so each table can then contain a list of items? If I did that, how would I link each table to the "products entry" of the corresponding company? |
#4
| |||
| |||
|
|
A list of "company products" within the company table violates that; you would need a key column to further identify the "product". |
#5
| |||
| |||
|
|
"Lew" <lew (AT) lewscanon (DOT) nospam> wrote in message news:voudnXF1iKfxl-fbnZ2dnUVZ_vqpnZ2d (AT) comcast (DOT) com... A list of "company products" within the company table violates that; you would need a key column to further identify the "product". Just to be pedantic, it does not violate that (if by "that" you mean first normal form). A list is a perfectly acceptable data type in principle. It is just that if you elect to have an attribute which is a list the DBMS is obliged only to treat as a kind of black-box, with no discernable internal structure--just as it would if an attribute were a JPEG or an MP3 or any other more complex type. An attribute which is a list would probably be a nuisance, and most SQL implementations don't support it, but relational theory has nothing to say against lists as values or types. |
#6
| |||
| |||
|
|
OTOH, your early mention of these curlicues is useful to let folks know at least two things up front - that what I presented is far, far from the final word on the matter, and not to be too dogmatic about rules of thumb gleaned at introductory study levels. |
#7
| |||||
| |||||
|
|
"Lew" <l... (AT) lewscanon (DOT) nospam> wrote in message news:voudnXF1iKfxl-fbnZ2dnUVZ_vqpnZ2d (AT) comcast (DOT) com... A list of "company products" within the company table violates that; you would need a key column to further identify the "product". Just to be pedantic, it does not violate that (if by "that" you mean first normal form). A list is a perfectly acceptable data type in principle. It is just that if you elect to have an attribute which is a list the DBMS is obliged only to treat as a kind of black-box, |
|
with no discernable internal structure--just as it would if an attribute were a JPEG or an MP3 or any other more complex type. |
|
An attribute which is a list would probably be a nuisance, |
|
and most SQL implementations don't support it, but relational theory has nothing to say against lists as values or types. |
|
Roy |
#8
| |||
| |||
|
|
Rick Giuly <rgiuly.gr... (AT) yahoo (DOT) com> wrote: I have a table and each row is a company - the name of the company, the website, etc. What I would like is one of the entries to be a list of products that the company sells - and here's the important part - I want the list of items to be set up so that each item can have its own description in another table. If I just make the list of products a comma separated list, that won't link to anything properly. Do I need to do something like create a table for every company, so each table can then contain a list of items? If I did that, how would I link each table to the "products entry" of the corresponding company? Google for "normalizing" a database. Are you familiar with the concept of related tables? Foreign keys? The rows in a table must depend on the key that identifies the row, the whole key, and nothing but the key. (First, second and third normal forms, respectively, for the googler in you.) A list of "company products" within the company table violates that; you would need a key column to further identify the "product". (Google for "repeating groups" in a database model.) Therefore you need a separate "companyproducts" table. That other table will have the company key column(s), repeated in order to allow a "JOIN" between the two tables. The join column(s) represent the relation between the two tables - the rows in the "child" table that have a certain (set of) value(s) in common with a "parent" table row are the child rows that relate to the corresponding parent row. A child row will also have an additional column or columns to further identify which product, belonging to that company, is described in the row. Example (Please view the data types as chosen for example purposes): CREATE TABLE companies ( company VARCHAR (120) PRIMARY KEY, info CLOB ); CREATE TABLE companyproducts ( company VARCHAR (120) NOT NULL, product VARCHAR (120) NOT NULL, info CLOB, PRIMARY KEY ( company, product ), FOREIGN KEY ( company ) REFERENCES companies ( company ) ); There are still problems with this approach. What if two different companies carry the same product? As you might see in that case, the "info" column does not depend on "the whole key" but only part of it, the "product" column. That tells you that you need another table, for "products" independently of what company has them. "companyproducts" is a relation that shows which "company" rows relate to which "products" rows. Example: CREATE TABLE companies ( company VARCHAR (120) PRIMARY KEY, info CLOB ); CREATE TABLE companyproducts ( company VARCHAR (120) NOT NULL, product VARCHAR (120) NOT NULL, PRIMARY KEY ( company, product ), FOREIGN KEY ( company ) REFERENCES companies ( company ), FOREIGN KEY ( product ) REFERENCES products ( product ) ); CREATE TABLE products ( product VARCHAR (120) PRIMARY KEY, info CLOB, ); Now you can query for all the products for a certain company using a three-way table JOIN. Example: SELECT x.company, c.info AS company_info, x.product, p.info AS product_info FROM company c INNER JOIN companyproducts x ON c.company = x.company INNER JOIN products p ON x.product = p.product WHERE x.company = ? ORDER BY x.product; This is a canonical example. -- Lew |
![]() |
| Thread Tools | |
| Display Modes | |
| |