-1

for example in my table i have domain and email columns, and data like this email column has something@Gmail.com and domain column has www.something .com i want to change domain column name to email name name if is this possible or not

sada
  • 27
  • 1
  • 2
  • 7

2 Answers2

3
ALTER TABLE your_table RENAME COLUMN email TO email_temp;
ALTER TABLE your_table RENAME COLUMN domain TO email;
ALTER TABLE your_table RENAME COLUMN email_temp TO domain;
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • SQL> create table hello (domain number(9),email number(8)); Table created. SQL> alter table hello rename column domain to email; alter table hello rename column domain to email * ERROR at line 1: ORA-00957: duplicate column name' – sada Jul 24 '16 at 10:14
  • Ya - That is why I first execute to change `email` into `email_temp` and then the rest. – Gilad Green Jul 24 '16 at 10:16
  • if there is any single query? – sada Jul 24 '16 at 10:29
  • Sort answer no. Longer - [see this](http://stackoverflow.com/questions/23274679/renaming-multiple-columns-in-one-statement-with-postgresql) – Gilad Green Jul 24 '16 at 10:31
  • 1
    @sada - why do you need a single "query"? What is wrong with running those three ALTER TABLE (not "queries", really) in succession? Why does the number of them matter? –  Jul 25 '16 at 02:34
  • That is worked but if there is any possibility to change in single short to get the update – sada Aug 06 '16 at 16:13
  • @sada - I don't understand what you are asking – Gilad Green Aug 06 '16 at 16:15
  • You was given for me some "querys" to interchange the column name in table now I edited my question can please see the question again you can understand, and my final intention is if we can change the columns name in single query or not (is this possible say yes or no) if yes can please explain me – sada Aug 06 '16 at 16:26
1

Simple... you'll get the same effect simply by swapping the data:

UPDATE your_table SET email = domain, domain = email;

Plus, it will be done in one transaction.

Jeffrey Kemp
  • 59,135
  • 14
  • 106
  • 158
  • i am not asking the data interchange . – sada Aug 06 '16 at 10:16
  • @sada - this is logically equivalent - in other words, you'll get the same **logical outcome** by swapping the *data* over instead of the *column names*. – Jeffrey Kemp Aug 06 '16 at 12:24
  • -suppose email column have something@gmail.com data and domain column www.sometimes.com what is the logical outcome there no sense to change swapping – sada Aug 06 '16 at 13:00
  • @sada, your question didn't convey that sense at all. So you want to only swap *a portion* of the column values? Please edit your question to make it clearer what your data looks like, and how you'd like it to look after the change. – Jeffrey Kemp Aug 06 '16 at 13:08
  • Thanks for suggestion, I will change that(you know my intention to the columns interchange and about my data) could you please help me to find the query – sada Aug 06 '16 at 16:04