How to change MySQL column definition? - mysql

I have a mySQL table called test:
create table test(
locationExpect varchar(120) NOT NULL;
);
I want to change the locationExpect column to:
create table test(
locationExpect varchar(120);
);
How can it be done quickly?

Do you mean altering the table after it has been created? If so you need to use alter table, in particular:
ALTER TABLE tablename MODIFY COLUMN new-column-definition
e.g.
ALTER TABLE test MODIFY COLUMN locationExpect VARCHAR(120);

Syntax to change column name in MySql:
alter table table_name change old_column_name new_column_name data_type(size);
Example:
alter table test change LowSal Low_Sal integer(4);

This should do it:
ALTER TABLE test MODIFY locationExpert VARCHAR(120)

Related

MySQL: having trouble with rename of auto_incriment column

On some versions of mysql (e.g. 5.0.77, for Win32 (ia32)) I'm having trouble with the following query
ALTER TABLE `table_name` CHANGE COLUMN `old` `new` integer auto_increment
I'm getting Incorrect table definition; there can be only one auto column and it must be defined as a key
How that can be ommited?
If the column is already an auto-increment primary key, just change its name and supply the data type. (MySQL will error if the data type isn't supplied here). The existing auto-increment and key definition will be retained.
ALTER TABLE `table_name` CHANGE COLUMN `old` `new` INT
If you are changing its name and making an auto-increment where there previously wasn't one, you need to specify the index or make it a primary key in the same statement:
ALTER TABLE `table_name` CHANGE COLUMN `old` `new` INT PRIMARY KEY AUTO_INCREMENT
Note however, that if you have another column on this table already serving as AUTO_INCREMENT, then the error as reported is correct and you will not be able to specify a second AUTO_INCREMENT column. You have to choose one or the other.
You need to first disable auto-increment, then make the changes, then turn it back auto_increment.
Try this (this will change it to INT):
ALTER TABLE `table_name` MODIFY COLUMN `old` INT;
ALTER TABLE `table_name` CHANGE COLUMN `old` `new` integer
ALTER TABLE `table_name` MODIFY COLUMN `new` INT AUTO_INCREMENT;
try this one:
ALTER TABLE `t1` CHANGE COLUMN `uid` `id` INT AUTO_INCREMENT;
Another one interesting answer:
ALTER TABLE table_name MODIFY COLUMN 'old_id','new_id' INT auto_increment;
click

Performance concerns regarding ALTER TABLE ADD COLUMN

I have a big MySQL InnoDB table having 5 million rows. I need to add a column to the table which will have a default int value.
What is the best way to do it? The normal alter table command appears to take a lot of time. Is there any better way to do it? Basically I want to know if there is any faster way or efficient way of doing it.
And if the table has foreign key references, is there any way other than alter table to do this?
Any help appreciated.
I would not say this is a better way, but ... You could create a separate table for the new data and set it up as foreign key relationship to the existing table. That would be "fast", but if the data really belongs in the main table and every (or most) existing records will have a value, then you should just alter the table and add it.
Suppose the table looked like this:
CREATE TABLE mytable
(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(25),
PRIMARY KEY (id),
KEY name (name)
);
and you want to add an age column with
ALTER TABLE mytable ADD COLUMN age INT NOT NULL DEFAULT 0;
You could perform the ALTER TABLE in stages as follows:
CREATE TABLE mytablenew LIKE mytable;
ALTER TABLE mytablenew ADD COLUMN age INT NOT NULL DEFAULT 0;
INSERT INTO mytablenew SELECT id,name FROM mytable;
ALTER TABLE mytable RENAME mytableold;
ALTER TABLE mytablenew RENAME mytable;
DROP TABLE mytableold;
If mytable uses the MyISAM Storage Engine and has nonunique indexes, add two more lines
CREATE TABLE mytablenew LIKE mytable;
ALTER TABLE mytablenew ADD COLUMN address VARCHAR(50);
ALTER TABLE mytablenew DISABLE KEYS;
INSERT INTO mytablenew SELECT id,name FROM mytable;
ALTER TABLE mytable RENAME mytableold;
ALTER TABLE mytablenew RENAME mytable;
DROP TABLE mytableold;
ALTER TABLE mytable ENABLE KEYS;
This will let you see how many seconds each stage takes. From here, you can decide whether or not a straightforward ALTER TABLE is better.
This technique gets a little gory if there are foreign key references.
Your steps would be
SET UNIQUE_CHECKS = 0;
SET FOREIGN_KEY_CHECKS = 0;
Drop the foreign key references in mytable.
Perform the ALTER TABLE in Stages
Create the foreign key references in mytable.
SET UNIQUE_CHECKS = 1;
SET FOREIGN_KEY_CHECKS = 1;
Give it a Try !!!

How to delete a column from a table in MySQL

Given the table created using:
CREATE TABLE tbl_Country
(
CountryId INT NOT NULL AUTO_INCREMENT,
IsDeleted bit,
PRIMARY KEY (CountryId)
)
How can I delete the column IsDeleted?
ALTER TABLE tbl_Country DROP COLUMN IsDeleted;
Here's a working example.
Note that the COLUMN keyword is optional, as MySQL will accept just DROP IsDeleted. Also, to drop multiple columns, you have to separate them by commas and include the DROP for each one.
ALTER TABLE tbl_Country
DROP COLUMN IsDeleted,
DROP COLUMN CountryName;
This allows you to DROP, ADD and ALTER multiple columns on the same table in the one statement. From the MySQL reference manual:
You can issue multiple ADD, ALTER, DROP, and CHANGE clauses in a single ALTER TABLE statement, separated by commas. This is a MySQL extension to standard SQL, which permits only one of each clause per ALTER TABLE statement.
Use ALTER TABLE with DROP COLUMN to drop a column from a table, and CHANGE or MODIFY to change a column.
ALTER TABLE tbl_Country DROP COLUMN IsDeleted;
ALTER TABLE tbl_Country MODIFY IsDeleted tinyint(1) NOT NULL;
ALTER TABLE tbl_Country CHANGE IsDeleted IsDeleted tinyint(1) NOT NULL;
To delete a single column:
ALTER TABLE `table1` DROP `column1`;
To delete multiple columns:
ALTER TABLE `table1`
DROP `column1`,
DROP `column2`,
DROP `column3`;
You can use
alter table <tblname> drop column <colname>
ALTER TABLE `tablename` DROP `columnname`;
Or,
ALTER TABLE `tablename` DROP COLUMN `columnname`;
If you are running MySQL 5.6 onwards, you can make this operation online, allowing other sessions to read and write to your table while the operation is been performed:
ALTER TABLE tbl_Country DROP COLUMN IsDeleted, ALGORITHM=INPLACE, LOCK=NONE;
Use ALTER:
ALTER TABLE `tbl_Country` DROP COLUMN `column_name`;
ALTER TABLE tbl_Country DROP columnName;
It is worth mentioning that MySQL 8.0.23 and above supports Invisible Columns
CREATE TABLE tbl_Country(
CountryId INT NOT NULL AUTO_INCREMENT,
IsDeleted bit,
PRIMARY KEY (CountryId)
);
INSERT INTO tbl_Country VALUES (1, 1), (2,0);
ALTER TABLE tbl_Country ALTER COLUMN IsDeleted SET INVISIBLE;
SELECT * FROM tbl_Country;
CountryId
1
2
ALTER TABLE tbl_Country DROP COLUMN IsDeleted;
db<>fiddle demo
It may be useful in scenarios when there is need to "hide" a column for a time being before it could be safely dropped(like reworking corresponding application/reports etc.).

Table Data Type Alter

I created a table named items and inserted name as int and provided the length as 255 type by mistake but now i wanted to alter the table structure and run the query as--
ALTER TABLE items
ALTER COLUMN name varchar(255); but it is not altering the table what i need to change in the table.help plz
there are two ways
ALTER TABLE t1 CHANGE <column_name> <column_name> <type>
note: you have to write column name twice
OR
ALTER TABLE t1 MODIFY <column_name> <type> ;
Reference
try:
alter table t1 modify column name varchar(255);
Try:
ALTER table items
MODIFY name varchar(255);

How do I Alter Table Column datatype on more than 1 column?

For example:
ALTER TABLE webstore.Store MODIFY COLUMN (
ShortName VARCHAR(100),
UrlShort VARCHAR(100)
);
The above however does not work. I am using MySql 5.x
ALTER TABLE can do multiple table alterations in one statement, but MODIFY COLUMN can only work on one column at a time, so you need to specify MODIFY COLUMN for each column you want to change:
ALTER TABLE webstore.Store
MODIFY COLUMN ShortName VARCHAR(100),
MODIFY COLUMN UrlShort VARCHAR(100);
Also, note this warning from the manual:
When you use CHANGE or MODIFY, column_definition must include the data type and all attributes that should apply to the new column, other than index attributes such as PRIMARY KEY or UNIQUE. Attributes present in the original definition but not specified for the new definition are not carried forward.
Use the following syntax:
ALTER TABLE your_table
MODIFY COLUMN column1 datatype,
MODIFY COLUMN column2 datatype,
... ... ... ... ...
... ... ... ... ...
Based on that, your ALTER command should be:
ALTER TABLE webstore.Store
MODIFY COLUMN ShortName VARCHAR(100),
MODIFY COLUMN UrlShort VARCHAR(100)
Note that:
There are no second brackets around the MODIFY statements.
I used two separate MODIFY statements for two separate columns.
This is the standard format of the MODIFY statement for an ALTER command on multiple columns in a MySQL table.
Take a look at the following: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html and Alter multiple columns in a single statement