Results 1 to 9 of 9

Thread: cleaning Wrong PK FK

  1. #1
    Join Date
    Apr 2006
    Posts
    178

    cleaning Wrong PK FK

    Hello

    my 2 tables in MS SQL 2000

    Report :
    Report_id (PK)
    name

    Product :
    Product_id (PK)
    Report_id (FK)
    name


    the Foreign Key and Primary Key have been added later (when the tables were allready full)
    product has a few millions of lines and report a few 10.thousand

    now I want to clean the 2 tables and remove all the lines which are not conected by PK > FK or FK > PK


    i am trying :

    DELETE FROM Product WHERE (Product.Report_id NOT IN (SELECT Report.Report_id FROM Report))

    DELETE FROM Report WHERE (Report.Report_id NOT IN (SELECT Product.Report_id FROM Product))


    but the database crash : time overflow !

    how can I do it ?

    thank you

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    I'll run select statements first to see how many rows need to be deleted in each of tables.

  3. #3
    Join Date
    Apr 2006
    Posts
    178
    100 to 1000 only thats not much for that big database, but i need to clean it

  4. #4
    Join Date
    Sep 2002
    Posts
    5,938
    Did you delete rows from smaller table first?

  5. #5
    Join Date
    Apr 2006
    Posts
    178
    yes

    i am getting that code too :

    delete
    from Product
    where not exists (select * from Report where Report.id = Product.Reportid)

  6. #6
    Join Date
    Sep 2002
    Posts
    5,938
    Did you get any error message? Which sql2k service pack does the server have?

  7. #7
    Join Date
    Apr 2006
    Posts
    178
    with the last code it works , but it is still very long

  8. #8
    Join Date
    Sep 2005
    Posts
    168
    DELETE Product
    FROM Product a
    LEFT JOIN Report b ON b.Report_id = a.Report_id
    WHERE b.Report_id IS NULL

    DELETE Report
    FROM Report a
    LEFT JOIN Product b ON b.Report_id = a.Report_id
    WHERE b.Report_id IS NULL

    --HTH--

  9. #9
    Join Date
    Apr 2006
    Posts
    178
    thank you mikros

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •