So, the other guy at work created a table with a column called:
Max(`abs_spg_20090430`.`ID`)
this is giving me an error now that I am trying to run a dump of the database on a different server.
I am trying to rename it, but
ALTER TABLE abs_spgID_20090504 CHANGE Max(`abs_spg_20090430`.`ID`) id bigint default null;
as well as
ALTER TABLE abs_spgID_20090504 CHANGE `Max(`abs_spg_20090430`.`ID`)` id bigint default null;
give me an error. Does any of you friendly people have a hint? Many thanks!
you need to quote your quotes and the column too, e.g:
ALTER TABLE abs_spgID_20090504 CHANGE `Max(``abs_spg_20090430``.``ID``)` id BIGINT DEFAULT NULL;
Related
Hello stackoverflows friends.
My problem in sql is this sintaxis:
ALTER TABLE user MODIFY COLUMN (role INT(10)) DEFAULT 2 NOT NULL;
I want to modify a column in this case role but my default should be 2.
Thanks.
This syntax should work:
alter table user modify column role int default 2;
Here is a SQL Fiddle.
Try this - For SQL
ALTER TABLE USER ALTER COLUMN ROLE SET DEFAULT(2);
For MySql :
ALTER TABLE USER ALTER ROLE SET DEFAULT(2);
I already have a table, simplified example:
CREATE TABLE t (c INT NOT NULL);
And I need to change column default value to NULL, so I tried:
ALTER TABLE t ALTER COLUMN c SET DEFAULT NULL;
but I got the error "Error Code: 1067. Invalid default value for 'c'".
It looks really strange, because query conforms with official docs.
I even tried to:
ALTER TABLE t ALTER COLUMN c DROP DEFAULT;
and after it to make a 'SET DEFAULT NULL' query, but the same error occurred.
It's interesting, that query like:
ALTER TABLE t ALTER COLUMN c SET DEFAULT 1;
executed without errors.
I know, that it is possible to change column default value to NULL in my case using:
ALTER TABLE t MODIFY COLUMN c INT NULL;
but this query is really slow on big tables (it is much slower, than queries like 'SET DEFAULT 1')
So, how to just change default value to NULL?
I mean, without any overhead caused by 'MODIFY COLUMN' command.
Details: MySQL x64 version 5.7.10, Win8. Tested using MySQL Workbench.
By creating column as NOT NULL you have created a CONSTRAINT - declaring that values entered into that column may never be NULL.
A default value of NULL (set to null is value not present during INSERT) would create invalid data.
As sadly nullability constraint is part of the datatype in mysql the only way to make the column nullable will be
ALTER TABLE t MODIFY COLUMN c INT NULL;
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.
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;
I'm using PHPMyAdmin and I try to add the NOT NULL constraint to a column of my table.
PHPMyAdmin accepts my following query :
ALTER TABLE `wall` MODIFY `token_message` varchar(40) NOT NULL;
But I can still insert empty strings (=NULL), I don't understand why.
PS : If you're going to give me some other queries to add this constraint, note I've have tried these 3 which don't work in my PHPMyAdmin (kind of error : #1064 - You have an error in your SQL syntax; check the manual) :
ALTER TABLE `wall` ALTER COLUMN `token_message` SET NOT NULL;
ALTER TABLE `wall` ALTER COLUMN `token_message` varchar(40) NOT NULL;
ALTER TABLE `wall` MODIFY `token_message` CONSTRAINTS token_message_not_null NOT NULL;
You wrote, "I can still insert empty strings (=NULL)," which sounds like a misunderstanding. In SQL, an empty string does not evaluate to NULL, or vice versa. Try inserting an empty string and doing SELECT from wall where token_message is NULL. You should get zero rows back. Then try doing an insert where you specify NULL (unquoted) as the value for your column, and you should get the expected error message.
If those tests work as expected, then everything is fine, and your problem is actually that you want to prevent blank strings from being inserted. Check out this question for suggestions, or just check for blank strings during validation, before the query.
MySQL's column alter syntax requires you to completely re-specify the column. You can't just change one attribute of a column, you have to re-define it completely:
ALTER TABLE wall MODIFY token_message varchar(40) NOT NULL default ''
The only 'SET' version allowed is to change the default value.
ref: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
I think this is a matter of scrubbing your inputs. As octern mentioned, an empty string ('') is not a NULL value in sql. The best way to handle this is to only allow updates through a store procedure which strips out empty strings, even space characters:
CREATE PROC InsertIntoMyDb (#MyVarChar VARCHAR(2000)) AS
SET #MyVarChar = NULLIF(RTRIM(LTRIM(#MyVarChar)), '')
INSERT INTO [TBL] (MyVarChar)
VALUES #MyVarChar
This will truncate any number of spaces to an empty string, turn an empty string into a NULL, and then it will not allow the NULL value to be inserted based on the constraint you already have in place.
Try to use this query
Alter table table_name
change column_name column_name datatype(length) definition
ie,
Alter table wall
change tocken_message tocken_message varchar(40) NOT NULL DEFAULT