1

How can I print the 'ID' column twice. I've already got it in the start of the table, and also want it to also print out as the last column again for easy viewing purposes.

ALTER TABLE EMPLOYEES
ADD ID int;

'ID already exists'

Also, when I update 'ID' I want both columns to be updated at once.

Thank you

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
ROOT
  • 57
  • 1
  • 3
  • 10
  • "Printing" should generally be handled by the client rather than in queries. Including the data twice increases the amount of data the database has to handle, the amount of data sent across the network, and the amount of data the client must unpack; in addition to all that, as GMB's answer notes, having multiple fields with the same name can often be problematic for database access libraries. – Uueerdo Nov 13 '19 at 23:42
  • I removed the inconsistent database tags. Please tag only with the database you are really using. – Gordon Linoff Nov 14 '19 at 01:08

2 Answers2

1

Don't add a new column to the table. Instead, you can fetch it twice when selecting from the table:

select
    id,
    -- other columns here
    id
from employees

Please note that, depending on your client, having two columns with the same name in your resultset could cause issues (for example, if you are fetching records as an associative array, only one of the two occurences of the column will appear in the array).

GMB
  • 216,147
  • 25
  • 84
  • 135
1

If you want to have another copy of a column, you can always use it in the SELECT statement for more than once

SELECT ID, ID
FROM EMPLOYEES

If you insist in add it to the table structure, you have to change the name

ALTER TABLE EMPLOYEES
ADD ID2 int;

To update them both, you have to specify that in the update statement

UPDATE EMPLOYEES
SET ID = /*Whatever*/
,ID2 = /*Whatever*/

OR

you can create a trigger to update ID2 every time ID is updated

asmgx
  • 7,328
  • 15
  • 82
  • 143