2

I have one table like this

enter image description here

and I want to convert it into table like this

enter image description here

Basically, I want to assign sequential value to column id. Is there any query for that? Also what are the equivalents for rownum in mysql?

2

3 Answers 3

2

You need to use AUTO_INCREMENT

CREATE TABLE animals (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (id)
) ENGINE=MyISAM;
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it in one way. Make the id column as PRIMARY KEY AUTO INCREMENT and then change the data values of all the 1s with null and then try inserting.

CREATE TABLE `people` (
     `id` INT NOT NULL AUTO_INCREMENT,
     `name` CHAR(30) NOT NULL,
     `profession` CHAR(30) NOT NULL,
     PRIMARY KEY (id)
) ENGINE=InnoDB;

And then the SQL like:

INSERT INTO `people` VALUES
(null, 'John', 'Engineer'),
(null, 'Ronny', 'Doctor'),
(null, 'Monty', 'Engineer');

3 Comments

is it possible to do it in existing table?
can you please tell me how to do that?
You just need to generate the SQL and replace the same values using a text editor. As simple as that. But do you have any code with you?
1

You would do this by creating a PRIMARY KEY and giving it an AUTO_INCREMENT, like this

id int NOT NULL AUTO_INCREMENT,

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.