How to create a new table from an existing table?

后端 未结 3 735
南笙
南笙 2021-01-25 10:49
dbo.Profile_Updated
  (
     BusinessName VARCHAR,
     ContactName  VARCHAR,
     Address      VARCHAR,
     City         VARCHAR,
     State        VARCHAR,
     Posta         


        
相关标签:
3条回答
  • 2021-01-25 10:55

    I assume you just mean SELECT INTO:

    SELECT City, State, Postalcode
      INTO dbo.PostalDB
      FROM dbo.Profile_Updated;
    
    CREATE UNIQUE CLUSTERED INDEX p ON dbo.PostalDB(PostalCode);
    CREATE INDEX ... -- you'll need to fill in these details
    
    0 讨论(0)
  • 2021-01-25 11:05

    You are doing things in the wrong order. You should have the PostalDB table created and populated first with a name like PostalDBId as the primary key. The PostalDBId should be effectively meaningless. It should not be a zip code or anything like that.

    Then, your Profile_Updated needs a different primary key, something that's not related to the PostalDB table. You also want another field that is a foreign key to the PostalDb table.

    0 讨论(0)
  • 2021-01-25 11:12

    Perhaps this will work:

    SELECT DISTINCT City, State, Postalcode
    INTO dbo.PostalDB
    FROM dbo.Profile_Updated
    

    The INTO clause will create a new table using the attributes from the source. The A little web searching will reveal many other possible solutions.

    0 讨论(0)
提交回复
热议问题