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?

4
  • possible duplicate of How to rename a table column in MySQL Commented Aug 23, 2014 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' Commented Aug 23, 2014 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) Commented Aug 23, 2014 at 17:34
  • In that case it's even simpler... Commented Aug 23, 2014 at 17:45

3 Answers 3

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';
Sign up to request clarification or add additional context in comments.

1 Comment

ok thanks, it worked (lol dunno how i missed this command, well need to learn mysql more!:D).
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.

1 Comment

As I said to alfasin, I'm pretty sure that the OP was actually just trying to update a cell, not rename a column.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.