Is it possible to set a default value for new records only in a mysql column?
I want all rows before today to be null if nothing has been entered, but any new rows added after today to be a specific string if nothing has be entered for that column.
Is this possible?
Search in all rows in the table and check if the specific column is empty, if yes set it to null.
UPDATE tablename SET column = CASE column WHEN '' THEN NULL ELSE column END
For future entries you can add to your table a Default Constraint like this:
ALTER TABLE tablename ALTER column SET DEFAULT 'specific string'
This is an old post but I do think this answer might help other folks.
ALTER TABLE object_table_name ADD `value` INT NULL;
ALTER TABLE object_table_name MODIFY COLUMN `value` INT DEFAULT 0;
Doing it this way will only impact newly inserted records, this code did not work for me
ALTER TABLE object_table_name ADD `value` INT DEFAULT 0
I'm using MySQL 5.6
Related
Basically I want to add a new updatedTime column so that it auto set to current_timestamp on updating and creating. Also I want to set the updatedTime column to null for existing rows.
So I try something like below:
Add new column and make it default to null
Set the new column default to current_timestamp afterward
alter table A
add column updatedTime timestamp null
default null on update current_timestamp;
alter table A
alter column updatedTime set default current_timestamp;
The reason is because my table is huge around 7 million rows and it's on live database as well so I try to optimise my query. By doing it this way, I just need to loop through the whole table once for the first query. The second query will only change the table config file.
So I am able to run the first query but the second one got syntax error.
EDIT:
My database is mysql server 5.7
I am having a table in sql with 20 rows inserted.In future it may need to add a another column.
In case of adding column values for the particular column will starts from the 21 line,previous rows values in particular column will be null.
I don't want to have null values on those rows.How can i alter the table for that.
You need to do two steps
1 Alter the table
Alter table table_name add column new_col datatype
2 Update that column into some default value
Update table_name set new_col='some value'
Do it in this way:
alter table tbl_sample add COLUMN sampleadd_column VARCHAR(50) default 'just a sample';
this should work for you;
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;
The code below doesn't seem to work even though the collumn and table does exist, any ideas?
ALTER TABLE `table` CHANGE 'collumn_1' 'collumn_1' TIMESTAMP DEFAULT 'CURRENT_TIMESTAMP' NOT NULL
I'm just trying to make the collumn available so it can store the current date and time when any data is added to this table.
ALTER TABLE `table` MODIFY collumn_1 TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;
i want to add a new column noOfTry in a table which already have some columns. the column data will be integer type and default value will be 0. i know i need to use alter query, just asking for the correct format of the query for this case
The simplest option would be
ALTER TABLE MyTable ADD COLUMN noOfTry INT NOT NULL DEFAULT 0;
alter table `database`.`table`
add column `noOfTry` int(11) DEFAULT '0' NULL after `column`;
where column is the column before the on you want to add if you want to add in the middle of the table but this is optional.