Add column into an existing table and add values

  • I just created a backup table from an existing table (The existing table contain some values in 'Marital Status' column like single or married etc.). I just dropped the 'Marital status' column from the backup table and now I am adding back that column into the backup table again (Marital Status) alter table DatFileBak add MaritalStatus varchar(10)  What I am trying to learn here is I want to be able to add the column into a table and add data in a way that it matches customer key. I am not sure if this question is confusing but let me know if it is.

  • add data in a way that it matches customer key

    Please expand on the above.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Phil Parkin - Tuesday, February 6, 2018 11:24 AM

    add data in a way that it matches customer key

    Please expand on the above.

    What i believe is that he already has another table with the column and that data and the idea is to insert into the new table and column the marital status of the other table containing the data of course, matching the ID on both tables, please correct me if im wrong.

  • Phil Parkin - Tuesday, February 6, 2018 11:24 AM

    add data in a way that it matches customer key

    Please expand on the above.

    Giving you the real scenario. I have table which contains customer information such as name, address, email address etc. What I need to do is I need to add 2 more columns (education and race). We pull the data the website and now I need to make sure that the education and race data is getting stored properly. For example There is a customer Name John Travolta and his education is Master in CIS and he is white. How do I store those values for let's say 1.5 customers? So when we pull the data for a customer, we have all the information. Does it make sense?

  • NewBornDBA2017 - Tuesday, February 6, 2018 12:37 PM

    Phil Parkin - Tuesday, February 6, 2018 11:24 AM

    add data in a way that it matches customer key

    Please expand on the above.

    Giving you the real scenario. I have table which contains customer information such as name, address, email address etc. What I need to do is I need to add 2 more columns (education and race). We pull the data the website and now I need to make sure that the education and race data is getting stored properly. For example There is a customer Name John Travolta and his education is Master in CIS and he is white. How do I store those values for let's say 1.5 customers? So when we pull the data for a customer, we have all the information. Does it make sense?

    Sure it makes sense. But which bit do you require assistance with?
    1) How to add columns to tables?
    2) How to populate columns from data held in other tables?
    3) How to transfer data from a web form to a SQL Server database table?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Phil Parkin - Tuesday, February 6, 2018 12:43 PM

    NewBornDBA2017 - Tuesday, February 6, 2018 12:37 PM

    Phil Parkin - Tuesday, February 6, 2018 11:24 AM

    add data in a way that it matches customer key

    Please expand on the above.

    Giving you the real scenario. I have table which contains customer information such as name, address, email address etc. What I need to do is I need to add 2 more columns (education and race). We pull the data the website and now I need to make sure that the education and race data is getting stored properly. For example There is a customer Name John Travolta and his education is Master in CIS and he is white. How do I store those values for let's say 1.5 customers? So when we pull the data for a customer, we have all the information. Does it make sense?

    Sure it makes sense. But which bit do you require assistance with?
    1) How to add columns to tables?
    2) How to populate columns from data held in other tables?
    3) How to transfer data from a web form to a SQL Server database table?

    So the 1st part is done, but I need help with the second part. How to populate the race and education data based on the customer key?
    Or is it better to have a separate column for race and education but I am not sure if that's going to make any sense. It will only make sense if someone has multiple race or even multiple education. But if are only concerned about 1 record of education (highest degree and not bachelor's or master's or PhD etc.), then adding 2 columns into an existing table should work.

  • NewBornDBA2017 - Tuesday, February 6, 2018 1:09 PM

    Phil Parkin - Tuesday, February 6, 2018 12:43 PM

    NewBornDBA2017 - Tuesday, February 6, 2018 12:37 PM

    Phil Parkin - Tuesday, February 6, 2018 11:24 AM

    add data in a way that it matches customer key

    Please expand on the above.

    Giving you the real scenario. I have table which contains customer information such as name, address, email address etc. What I need to do is I need to add 2 more columns (education and race). We pull the data the website and now I need to make sure that the education and race data is getting stored properly. For example There is a customer Name John Travolta and his education is Master in CIS and he is white. How do I store those values for let's say 1.5 customers? So when we pull the data for a customer, we have all the information. Does it make sense?

    Sure it makes sense. But which bit do you require assistance with?
    1) How to add columns to tables?
    2) How to populate columns from data held in other tables?
    3) How to transfer data from a web form to a SQL Server database table?

    So the 1st part is done, but I need help with the second part. How to populate the race and education data based on the customer key?
    Or is it better to have a separate column for race and education but I am not sure if that's going to make any sense. It will only make sense if someone has multiple race or even multiple education. But if are only concerned about 1 record of education (highest degree and not bachelor's or master's or PhD etc.), then adding 2 columns into an existing table should work.

    Regarding adding multiple columns for race and education, that's a design choice for you to make. But if you find yourself creating more than a couple of either, you're rapidly heading into a bad place & should seriously consider normalising your design.
    Regarding the query: without a full description of table or column names, the general structure is as follows:
    UPDATE target
    SET MaritalStatus = source.MaritalStatus
    FROM DatFileBak target
    JOIN Tab2 source on target.CustomerKey = source.CustomerKey

    The above query assumes a 1-1 link between the source and target tables on the match key ... this is very important.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

Viewing 7 posts - 1 through 6 (of 6 total)

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