0

What is the best way to create columns in existing table i data-base....

So now ... I have 3 different tables in database as the below tables:

àll_sites

|---------------------|------------------|
|        Vendor       |     ops_num      |
|---------------------|------------------|
|         a           |         1        |
|---------------------|------------------|
|         b           |         2        |
|---------------------|------------------|
|         a           |         3        |
|---------------------|------------------|
|         b           |         4        |
|---------------------|------------------|
|         b           |         5        |
|---------------------|------------------|
|         a           |         6        |
|---------------------|------------------|
|         a           |         7        |
|---------------------|------------------|

umts_carrier_mnm

|---------------------|------------------|
|        ops_num      |     cell_name    |
|---------------------|------------------|
|         1           |         11       |
|---------------------|------------------|
|         1           |         12       |
|---------------------|------------------|
|         1           |         13       |
|---------------------|------------------|
|         2           |         21       |
|---------------------|------------------|
|         2           |         22       |
|---------------------|------------------|
|         2           |         23       |
|---------------------|------------------|
|         3           |         31       |
|---------------------|------------------|

So Now I need to add more 2 columns vendor and 'ops_num' in the below table:

umts_df_relation

|---------------------|
|        cell_name    |
|---------------------|
|         11          |
|---------------------|
|         12          |
|---------------------|
|         13          |
|---------------------|
|         21          |
|---------------------|
|         22          |
|---------------------|
|         23          |
|---------------------|
|         31          |
|---------------------|

So I want to add 2 columns ops_num and 'vendor' in the previous table as the result to be like this as the below table:

umts_df_relation

|---------------------|------------------|------------------|
|        cell_name    |     ops_num      |     vendor       |
|---------------------|------------------|------------------|
|         11          |         1        |         a        |
|---------------------|------------------|------------------|
|         12          |         1        |         a        |
|---------------------|------------------|------------------|
|         13          |         1        |         a        |
|---------------------|------------------|------------------|
|         21          |         2        |         b        |
|---------------------|------------------|------------------|
|         22          |         2        |         b        |
|---------------------|------------------|------------------|
|         23          |         2        |         b        |
|---------------------|------------------|------------------|
|         31          |         3        |         a        |
|---------------------|------------------|------------------|

So this the query I want to be like as the below query: alter table [myDB].[dbo].[umts_df_relation] add vendor varchar , ops_num INT

Now I need to fill the new columns with the va;ues related to other tables

So is there's any way to ALTER table using query or pandas?, and what is better way to solve this, and how?

Thanks for support

Mahmoud Al-Haroon
  • 2,239
  • 7
  • 36
  • 72
  • `ALTER` is a type of query. What about the `ALTER` statement don't you understand, and why isn't it working? – Thom A Feb 03 '20 at 09:44
  • @Larnu I need to use `ALTER` query or do this function by pandas... I don't know how to do it – Mahmoud Al-Haroon Feb 03 '20 at 09:45
  • Ok, so "What about the `ALTER` statement don't you understand"? What in the documentation confused you? – Thom A Feb 03 '20 at 09:46
  • @Larnu I don't know what's the right sequence to make a query do this – Mahmoud Al-Haroon Feb 03 '20 at 09:47
  • Ok, but you've still not told me what you don't understand. What in the documentation is confusing you? There are examples as well in the documentation, and plenty on Stack Overflow, so what about those didn't show you was the correct syntax? – Thom A Feb 03 '20 at 09:55
  • @Larnu if you have docs for this case please send me or help me to do this funtion... thank you for your support :) I understand the ALTER Query, but I don't know how to create those 2 columns related to the other tables :)... Thank you for you help!!! – Mahmoud Al-Haroon Feb 03 '20 at 09:56
  • 1
    I've marked this as a duplicate of another question, that shows you how to use `ALTER`. if you don't understand, then I suggest posting a new question, showing us why what you've tried doesn't work. Thanks. – Thom A Feb 03 '20 at 09:56
  • *"if you have docs for this case please send me or help me to do this funtion"* Your favourite search engine will find these for you *very* quickly; SQL Server's documentation is very extensive. That should always be your first port of call. – Thom A Feb 03 '20 at 09:57
  • @Larnu okay I'll try to use `ALTER` query then I'll back to you :) – Mahmoud Al-Haroon Feb 03 '20 at 10:07
  • @Larnu Now I created the 2 columns I need with this query `alter table [myDB].[dbo].[umts_df_relation] add vendor varchar , ops_num INT` So now how can I fill those columns with values related to others tables? please check edit – Mahmoud Al-Haroon Feb 03 '20 at 10:15
  • That's a completely different question, and should be asked as one. I, however, suggest looking up the `UPDATE` statement. – Thom A Feb 03 '20 at 10:23
  • @Larnu yes actually I am using something like this query `UPDATE [myDB].[dbo].[umts_df_relation] SET [myDB].[dbo].[umts_df_relation].[ops_num] = [myDB].[dbo].[all_sites].[ops_num] WHERE Condition` but I stucked now On condition that's for one column only :)... I am trying to figure it :) – Mahmoud Al-Haroon Feb 03 '20 at 10:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/207095/discussion-between-mahmoud-al-haroon-and-larnu). – Mahmoud Al-Haroon Feb 03 '20 at 10:27

0 Answers0