0

I have a table in mysql with just one column (no primary key), say column is like this->

Column Name
----------------
Data1
Data2
Data4

What i want to do is change "Data4" to "Data3" using java connectivity. But to change cell values, i know that command->

Alter Table <tablename> set <columnname>="something" where <someothercolumnname>="somethingelse";

but this needs atleast 2 column in table, i get the syntax error when there is just one column. So can anyone help me with correct command?

user3271166
  • 573
  • 1
  • 6
  • 17
  • possible duplicate of [How to rename a table column in MySQL](http://stackoverflow.com/questions/4002340/how-to-rename-a-table-column-in-mysql) – Nir Alfasi Aug 23 '14 at 17:04
  • 2
    `Alter` is different from `Update` alter is used to update table schema if you want to update table data then use update query `update tablename set columnname ='value' where columnname='somevalue'` – M Khalid Junaid Aug 23 '14 at 17:05
  • @alfasin I don't think so; if I'm reading this question right, "Column Name" is the column, and "Data1", "Data2", "Data4" is the data in the table under that column (note that OP said there's only one column, not three) – Dennis Meng Aug 23 '14 at 17:34
  • In that case it's even simpler... – Nir Alfasi Aug 23 '14 at 17:45

3 Answers3

4

Use update query instead of alter query. The alter will help you update the table description.

update <your table name> set columnname ='Data3' where columnname='Data4';
Loganathan Mohanraj
  • 1,736
  • 1
  • 13
  • 22
1

You could leave the table as it is and just write your query as:

select data1, data2, data4 as data3
from tablex;

Or, if you want to rename it in the database:

ALTER TABLE <tablename> CHANGE data4 data3 varchar(255);

You need to have a data type when you do this. Change the data type to the appropriate type for the column.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • As I said to alfasin, I'm pretty sure that the OP was actually just trying to update a cell, not rename a column. – Dennis Meng Aug 23 '14 at 18:14
0

Alter query is the actual answer. But there is a silly alternate, first delete it "Data4", and then insert "Data3"

Benoy Prakash
  • 436
  • 5
  • 17