Re: Query to split one table into 2

From: David Portas (REMOVE_BEFORE_REPLYING_dportas_at_acm.org)
Date: 08/11/04


Date: Wed, 11 Aug 2004 14:25:51 +0100


> I am not asking design
> advice on the tables.

Unfortunately it's a design problem. Lack of keys makes many things in SQL
impossible or at least very difficult. That's why correct design is so
important.

Taking just the subset of columns you posted earlier, try this:

INSERT INTO Products (photographerid, sort, programname, packagename)
 SELECT DISTINCT customer, sort, program, package
  FROM OldData

INSERT INTO ProductPriceCodes (productid, gpcode1, gpcode2, gpcode3)
 SELECT DISTINCT P.photographerid, O.code1, O.code2, O.code3
  FROM OldData AS O
  JOIN Products AS P
   ON O.customer = P.photographerid
    AND O.sort = P.sort
    AND O.program = P.programname
    AND O.package = P.packagename

There are problems with this. Maybe it happens that the same combination of
(photographerid, sort, programme, packagename) occurs for multiple products?
You can't resolve that in your design. Then again, maybe it never happens.

Also, because you don't have a natural key in the target table you can't
easily handle NULLs in the data - rows with NULLs just won't get inserted. I
guess you could try to work around this in the join of the above SELECT but
better to declare a proper key to start with (did I mention how important
keys are...?) :o)

-- 
David Portas
SQL Server MVP
--


Relevant Pages

  • Re: SQL
    ... > If you design right, you can *shift* much behavior to being data and ... > SQL is close to being Turing Complete. ... It is a bad habit of yours. ... If you're seriously suggesting that CRUD applications are equal ...
    (comp.object)
  • Re: UP -- patent restricted?
    ... some poor unsuspecting schmuck. ... Maybe there are capabilities in SQL that I'm totally ignorant of. ... After doing that I'd like to "ride the B-tree index" to visit and check all the entries for this client. ... When you need so many keys, i.e. you have so many functions, so much functionality, you don't have a lot of freedom to conform with what others have done. ...
    (comp.databases.pick)
  • Re: Object-oriented thinking in SQL context?
    ... away from arrays and other non-OO data structures associated ... SQL is constrained to 'trivial' arrays. ... Design: Logical Design", 4th edition. ... Something analogous happens with database design. ...
    (comp.databases.theory)
  • Re: Why does 1:3 relationsihp require another table?
    ... Pro SQL Server 2000 Database Design ... > there can be a relation with NULLs in its tuples (hence 1NF models with ... > can only conclude that you do not believe Louis and Lewis are correctly ...
    (microsoft.public.sqlserver.programming)
  • Re: design question
    ... Are you sure you don't mean "primary keys made up by composing several ... The second stage was logical database design, ... SQL modeling and relational modeling that I've since seen in this newsgroup. ...
    (comp.databases.theory)