SQL Newbie in Need of Help!!!

  • I am in the process of creating a DB in SQL Server 2008 R2 and am having a couple of problems. I have the following:

    8 Total Tables as outlined below:

  • World (WorldID Primary Key, Description)
  • Hub (HubID Primary Key,WorldID Foriegn Key, Description)
  • Sub (SubID Primary Key,HubID Foriegn Key, Description)
  • Tag (TagID Primary Key,SubID Foriegn Key, Description)
  • Keyword (KeywordID Primary Key, Keyword, Searches, Results)
  • Keyword Tag Relationship(KeywordTagID Primary Key,TagID Foriegn Key, KeywordID Foriegn Key)
  • Universal Word (UniversalWordID Primary Key, Description)
  • SEOStagingTable (SEOStagingID Primary Key, World, Hub, Sub, Tag, Keyword, Searches, Results)
  • This issue is that I want to import from an excel document to the SEOStagingTable, and then run a saved process that will automatically add the data in the correct locations to the correct tables. Here is what I tried:

    USE [SEO Keyword]

    GO

    ----INSERT INTO KeywordTable using Select

    INSERT INTO Keyword (Keyword, Searches, Results)

    SELECT Keyword, Searches, Results

    FROM SEOStagingTable

    Go

    ----INSERT INTO WorldTable using Select

    INSERT INTO World (Description)

    SELECT World

    FROM SEOStagingTable

    Go

    ----INSERT INTO HubTable using Select

    INSERT INTO Hub (Description)

    SELECT Hub

    FROM SEOStagingTable

    Go

    ----INSERT INTO SubTable using Select

    INSERT INTO Sub (Description)

    SELECT Sub

    FROM SEOStagingTable

    Go

    ----INSERT INTO TagTable using Select

    INSERT INTO Tag (Description)

    SELECT Tag

    FROM SEOStagingTable

    Go

    The problem is that I need to make sure that I do not create more than one of the same type of World, Hub, Sub, and Tag. Yet there can be multiple "Descriptions" for Tag, as there may be more than one Tag in relation to the Sub. Here is how it would be organized logically:

    Hub -> Sub1 -> Tag1

    Hub -> Sub1 -> Tag2

    Hub -> Sub1 -> Tag3

    Hub -> Sub2 -> Tag1

    Hub -> Sub2 -> Tag4

    Hub -> Sub2 -> Tag5

    Can someone please help me with the Query code needed to update all tables from the one table as described above while keeping the following constraints.

    World Table can only have one of each type of Description - there are duplicates in the SEOStagingTable

    Hub Table can only have one of each type of Description per WorldID - there are duplicates in the SEOStagingTable

    SubTable can only have one of each type of Description per HubID - there are duplicates in the SEOStagingTable

    TagTable can only have one of each type of Description per SubID - there are duplicates in the SEOStagingTable

    KeywordTable cannot have any duplicates in Keyword.

    Thank you for your help on this issue.

    Sincerely,

    Aaron

  • Aaron you want to change it a little so your lookup tables get populated by a SELECT featuring the DISTINCT clause:

    ----INSERT INTO WorldTable using Select

    INSERT INTO World (Description)

    SELECT DISTINCT World

    FROM SEOStagingTable

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • That worked out perfectly. Thank you for getting back to me on that one.

    Sincerely,

    Aaron

  • Viewing 3 posts - 1 through 3 (of 3 total)

    You must be logged in to reply to this topic. Login to reply