The use of multiple product fields is a disaster waiting to
happen. Your searching problem is just the tip of the
iceberg that will eventually sink your database. Instead,
you should follow the rules of database normalization and
have a separate table with one product field (which make the
search problem disappear).
Hi, the OP here. Thanks for you advice on normalization. The thing is,
I have a business requirement that each entity for which the record
relates must have exactly 10 products, no more, no less (including
zero). With my existing design, this is simple to enforce i.e. 10 NOT
NULL (Required = Yes) columns. With your proposed design, I don't
think this can be enforced in the data layer (while it could in theory
be enforced using a table-level CHECK constraint, in practice I would
need to DROP it -- and remember to recreate it -- every time I need to
add a new set of ten rows to the table, making it impractical).
So should I fully normalize and leave myself wide open to data
integrity issues? Or stick with my existing design and perhaps have a
normalized helper VIEW (non-parameterized stored query that returns a
resultset) to make querying easier e.g.
CREATE VIEW Helper
AS
SELECT entity_ID, Product1 AS Product
FROM MyTable
UNION ALL
SELECT entity_ID, Product2
FROM MyTable
UNION ALL
SELECT entity_ID, Product3
FROM MyTable
UNION ALL
....
UNION ALL
SELECT entity_ID, Product10
FROM MyTable;
PS I'm not really the OP, I'm just trying to engender a meaningful
context for discussion.
Jamie.
--