MySQL - How I can rename a column? - mysql

So far I am doing it this way:
"ALTER TABLE `database_name`.`table_name` CHANGE
`old_column_name` `new_column_name` column_type_now"
But this is problematic if the column had not NULL default values or was a key since those things are discarded when the column is renamed.
How I can deal with this issue?

try:
ALTER TABLE `database_name`.`table_name` CHANGE
`old_column_name` `NEW Column Name` varchar(255) DEFAULT NOT NULL
Replace Varchar with the type of column you're making.
You can also just make it
`DEFAULT NULL`
If you prefer it to be null.
Did that work?

try this:
ALTER TABLE "table_name" CHANGE "old_column_name" "new_column_name" DATATYPE;
FOR ORACLE:
ALTER TABLE "table_name" RENAME COLUMN "old_column_name" TO "new_column_name";

Related

How to add column using alter in mysql?

How to add the column using alter by inputting the values on it?
As the example, I want to add tempID's column which has value "3" on every row
Maybe it's something like this
ALTER TABLE NAMEYOURTABLE
ADD COLUMN last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
but the code above is only for the timestamp, not for integer values. Thanks in advance
You should check out this https://dev.mysql.com/doc/refman/5.7/en/alter-table.html
For your case it would be something like
ALTER TABLE NAMEYOURTABLE ADD COLUMN tempID int NULL DEFAULT 3;
Along with Jovana's answer, you can specify after which column you need the new column with AFTER .
ALTER TABLE NAMEYOURTABLE ADD tempID int NULL DEFAULT 3
AFTER YOUREXISTINGCOLUMNNAME;
Refer : https://www.w3schools.com/sql/sql_alter.asp
Example to add a column:
ALTER TABLE table_name
ADD column_name datatype;

converting mysql scripts to postgresql script

I have the following line in a .sql file from a mysql db:
ALTER TABLE lcr_gw ALTER COLUMN ip_addr TYPE VARCHAR(50) DEFAULT NULL;
I would like to convert it into syntax that postgresql would understand. In my personal tests, I was only able to get it to work by breaking it down into two separate statements, like so:
ALTER TABLE lcr_gw ALTER COLUMN ip_addr TYPE VARCHAR(50);
ALTER TABLE lcr_gw ALTER COLUMN ip_addr SET DEFAULT NULL;
Just wondering if there's a way to consolidate the two statements back into one, but one that postgresql will be happy with?
Thanks!
The statement you posted is not valid syntax at all:
SQL Fiddle
To change the type in MySQL, you would use CHANGE or MODIFY.
To change the default you would use DROP DEFAULT or SET DEFAULT NULL.
If the intention was to change the type and reset the column default:
Like in MySQL, you can pack multiple actions into a single ALTER TABLEstatement in Postgres .
ALTER TABLE lcr_gw ALTER COLUMN ip_addr SET DEFAULT NULL
,ALTER COLUMN ip_addr TYPE VARCHAR(50);
Per documentation:
The main reason for providing the option to specify multiple changes
in a single ALTER TABLE is that multiple table scans or rewrites can
thereby be combined into a single pass over the table.
But if there was a DEFAULT on the column that is incompatible with the new type, you have to run two separate statements:
ALTER TABLE lcr_gw ALTER COLUMN ip_addr SET DEFAULT NULL;
ALTER TABLE lcr_gw ALTER COLUMN ip_addr TYPE VARCHAR(50);
Doesn't matter in this case anyway.
As #Gordon Linoff states in the comments, postgreSQL by default sets a value to null unless a value is given or the default is changed to something else;
therefore, all you'll need is:
ALTER TABLE lcr_gw ALTER COLUMN ip_addr TYPE VARCHAR(50);
The PostgreSQL ALTER TABLE syntax diagram doesn't show any way to combine changing a data type and changing a default value in a single SQL statement. You can't simply omit set default null in the general case. For example,
create table test (
column_1 char(10) not null default 'a'
);
alter table test alter column column_1 type varchar(50);
insert into test values (default);
select * from test;
column_1
--
a
Instead, either rewrite as two independent statements (which you already know how to do), or as two statements in a single transaction.

MySQL - How to modify column default value?

How do I change my column's default value from None to something else? For example, I want my dates to have a default value of 0000-00-00 if I don't specify one when I create the row.
I understand this in phpMyAdmin, but I'm not sure how to do it via command prompt.
I also understand how to do this when adding a column. But all of my columns are made and have data in some of them.
ALTER TABLE table1 ADD COLUMN foo INT DEFAULT 0;
From searching, I found this line, but I'm not sure if that's what I want?
ALTER TABLE foobar_data MODIFY COLUMN col VARCHAR(255) NOT NULL DEFAULT '{}';
Use ALTER TABLE to CHANGE or MODIFY the DEFAULT value of column. Check this link ALTER TABLE SYNTAX
ALTER TABLE `tableName` CHANGE `columnName` `columnName` DATE DEFAULT '0000-00-00';
ALTER TABLE `tableName` MODIFY `columnName` DATE DEFAULT '0000-00-00';
try this
ALTER TABLE foobar_data CHANGE COLUMN col VARCHAR(255) NOT NULL DEFAULT '{}';
user CHANGE to alter a existing column
see Link
In my case, it worked
ALTER TABLE `table_name` CHANGE `col_name` `col_name_again` VARCHAR(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT 'anydefault_text';
Hope it helps you too.

`MODIFY COLUMN` vs `CHANGE COLUMN`

I know, we cannot rename a column using MODIFY COLUMN syntax, but we can using CHANGE COLUMN syntax.
My question is: what is the main usage of modify syntax?
For example:
ALATER TABLE tablename CHANGE col1 col1 INT(10) NOT NULL;
instead of
ALATER TABLE tablename MODIFY col1 INT(10) NOT NULL;
Edited (question replaced)
What is the main usage of MODIFY syntax?
Why we have to use CHANGE COLUMN instead of MODIFYCOLUMN?
CHANGE COLUMN
If you have already created your MySQL database, and decide after the fact that one of your columns is named incorrectly, you don't need to remove it and make a replacement, you can simply rename it using change column.
ALTER TABLE MyTable CHANGE COLUMN foo bar VARCHAR(32) NOT NULL FIRST;
MODIFY COLUMN
This command does everything CHANGE COLUMN can, but without renaming the column. You can use the MODIFY SQL command if you need to resize a column in MySQL. By doing this you can allow more or less characters than before. You can't rename a column using MODIFY and other.
ALTER TABLE MyTable MODIFY COLUMN foo VARCHAR(32) NOT NULL AFTER baz;
Note
ALTER TABLE is used for altering a table in order to change column name, size, drop column etc. CHANGE COLUMN and MODIFY COLUMN commands cannot be used without help of ALTER TABLE command.
The difference is whether you want to change the column name, column definition or both.
CHANGE
Can change a column name or definition, or both
ALTER TABLE t1 CHANGE a b BIGINT NOT NULL
MODIFY
Can change a column definition but not its name
ALTER TABLE t1 MODIFY b INT NOT NULL
RENAME COLUMN (from MySQL 8.0)
Can change a column name but not its definition
ALTER TABLE t1 RENAME COLUMN b TO a
Also, CHANGE and MODIFY can be followed by an optional COLUMN keyword.
For complete explanation:
MySQL 5.7 Docs- Renaming, Redefining, and Reordering Columns
MySQL 8.0 Docs- Renaming, Redefining, and Reordering Columns
I found one difference after more than an hour of effort in trying to make a non auto_increment column into auto_increment
statement:
alter table `doctor_experience` modify column `id` int(11) unsigned auto_increment
works, but statment:
alter table `doctor_experience` change column `id` `id` int(11) unsigned auto_increment
will report an error.
That is the same. It was done to support another syntax (Oracle ALTER TABLE as I know). You can use both of them.
Note: ALTER TABLE CHANGE old_col_name new_col_name syntax allows renaming column using one command.
Change Column : Used when we want to change the column name with its definition.
eg - alter table student CHANGE name full_name VARCHAR(32) NOT NULL;
Modify column : Used when column name is to be same but change in its definition.
eg - alter table student MODIFY full_name VARCHAR(64) NOT NULL;
Rename column : Used when we only need to change the column name (its definition will be same)
alter table student RENAME COLUMN full_name TO name;

MySQL - How to increase varchar size of an existing column in a database without breaking existing data?

I have a column that is currently varchar(100) and I want to make it 10000.
is it as simple as
alter table table_name set column col_name varchar (10000);
I am afraid to corrupt the exiting data. Will I be ok if I run this query? Or should I do I alter the column another way?
It's safe to increase the size of your varchar column. You won't corrupt your data.
If it helps your peace of mind, keep in mind, you can always run a database backup before altering your data structures.
By the way, correct syntax is:
ALTER TABLE table_name MODIFY col_name VARCHAR(10000)
Also, if the column previously allowed/did not allow nulls, you should add the appropriate syntax to the end of the alter table statement, after the column type.
I normally use this statement:
ALTER TABLE `table_name`
CHANGE COLUMN `col_name` `col_name` VARCHAR(10000);
But, I think SET will work too, never have tried it. :)
I'd like explain the different alter table syntaxes - See the MySQL documentation
For adding/removing defaults on a column:
ALTER TABLE table_name
ALTER COLUMN col_name {SET DEFAULT literal | DROP DEFAULT}
For renaming a column, changing it's data type and optionally changing the column order:
ALTER TABLE table_name
CHANGE [COLUMN] old_col_name new_col_name column_definition
[FIRST|AFTER col_name]
For changing a column's data type and optionally changing the column order:
ALTER TABLE table_name
MODIFY [COLUMN] col_name column_definition
[FIRST | AFTER col_name]
For me worked this one:
ALTER TABLE tablename MODIFY fieldname VARCHAR(128) NOT NULL;
ALTER TABLE emp MODIFY COLUMN name VARCHAR(100);
emp is your table name.
I am using mysql and below syntax worked well for me,
ALTER TABLE table_name MODIFY col_name VARCHAR(12);
For me this has worked-
ALTER TABLE table_name
ALTER COLUMN column_name VARCHAR(50)