How can i make a table field with default value "none" and null value "no"?
When i create a table with following code it always add default value NULL and null to yes
ALTER TABLE `table_name` ADD `test` INT(11) AFTER `test1`
ALTER TABLE `table_name` ADD `test` INT NOT NULL AFTER `test1`;
ALTER TABLE myTable
ALTER column SET DEFAULT 'none'.
For replacing NULL with NO you don't have to alter the table just do it in your query :
SELECT COALESCE(columnWhichisNull,'no'))
FROM myTable ;
COALESCE checks if a value from a column is null and replaces it with your desired character
Related
I'm trying to alter current id field in organization table to UUID_SHORT but failed?
ALTER TABLE `organization` CHANGE `id` `id` BIGINT(16) UNSIGNED
NOT NULL DEFAULT uuid_short();
I don't see any error message?!
I don't think you can set the default value for id like this
Rather you can create a trigger to do this:
CREATE TRIGGER before_insert_organization
BEFORE INSERT ON organization
FOR EACH ROW
SET new.id = uuid_short();
I've a table item that has some columns that are nullable.
To one of them type, I'd like to automatically insert a default value (instead of a NULL) whenever a new record in inserted in the table and do not specify a value for that column.
Can it be done without affecting the existing data?
The type column is a varchar.
I can update the current nulls.
You can try to ALTER column set a default value.
ALTER TABLE `T` MODIFY `type` varchar(50) DEFAULT 'default';
then insert by DEFAULT keyword:
INSERT INTO T (type) VALUES (DEFAULT);
Results:
This query will work for you.
For update table.
ALTER TABLE `column_name` CHANGE `tab` `my_id` INT(11) NOT NULL DEFAULT '0';
For insert table
CREATE TABLE `db_name`.`Tbale_name` ( `demo` INT NOT NULL DEFAULT '0');
An existing MySQL table has a DateTime field which is not null and having a Default Value set as '0001-00-00 00:00:00'. Is it possible to Alter this table to remove the default value for the DateTime field ?
Yes, you can drop the default using an ALTER TABLE statement like this:
alter table your_table
alter column your_column drop default;
To drop the default from multiple datetime columns in a table:
ALTER TABLE your_table
ALTER COLUMN columnname1 DROP DEFAULT,
ALTER COLUMN columnname2 DROP DEFAULT,
ALTER COLUMN columnname3 DROP DEFAULT,
....
You can simply change the column and exclude default clause using following query. Here T1 is table containing default value for column updated and subject. You can simply remove default value as given below:
ALTER TABLE `T1`
CHANGE `updated` `updated` DATETIME NOT NULL,
CHANGE `subject` `subject` VARCHAR(100)
I got the following error while trying to alter a column's data type and setting a new default value:
ALTER TABLE foobar_data ALTER COLUMN col VARCHAR(255) NOT NULL SET DEFAULT '{}';
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'VARCHAR(255) NOT NULL SET DEFAULT '{}'' at line 1
ALTER TABLE foobar_data MODIFY COLUMN col VARCHAR(255) NOT NULL DEFAULT '{}';
A second possibility which does the same (thanks to juergen_d):
ALTER TABLE foobar_data CHANGE COLUMN col col VARCHAR(255) NOT NULL DEFAULT '{}';
As a follow up, if you just want to set a default, pretty sure you can use the ALTER .. SET syntax. Just don't put all the other stuff in there. If you're gonna put the rest of the column definition in, use the MODIFY or CHANGE syntax as per the accepted answer.
Anyway, the ALTER syntax for setting a column default, (since that's what I was looking for when I came here):
ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT 'literal';
For which 'literal' could also be a number (e.g. ...SET DEFAULT 0). I haven't tried it with ...SET DEFAULT CURRENT_TIMESTAMP but why not eh?
If you want to add a default value for the already created column,
this works for me:
ALTER TABLE Persons
ALTER credit SET DEFAULT 0.0;
For DEFAULT CURRENT_TIMESTAMP:
ALTER TABLE tablename
CHANGE COLUMN columnname1 columname1 DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CHANGE COLUMN columnname2 columname2 DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP;
Please note double columnname declaration
Removing DEFAULT CURRENT_TIMESTAMP:
ALTER TABLE tablename
ALTER COLUMN columnname1 DROP DEFAULT,
ALTER COLUMN columnname2 DROPT DEFAULT;
In case the above does not work for you (i.e.: you are working with new SQL or Azure) try the following:
1) drop existing column constraint (if any):
ALTER TABLE [table_name] DROP CONSTRAINT DF_my_constraint
2) create a new one:
ALTER TABLE [table_name] ADD CONSTRAINT DF_my_constraint DEFAULT getdate() FOR column_name;
Accepted Answer works good.
In case of Invalid use of NULL value error, on NULL values, update all null values to default value in that column and then try to do the alter.
UPDATE foobar_data SET col = '{}' WHERE col IS NULL;
ALTER TABLE foobar_data MODIFY COLUMN col VARCHAR(255) NOT NULL DEFAULT '{}';
Try this
ALTER TABLE `table_name` CHANGE `column_name` `column_name` data_type NULL DEFAULT '';
like this
ALTER TABLE `drivers_meta` CHANGE `driving_license` `driving_license` VARCHAR(30) NULL DEFAULT '';
Use this approach:-
ALTER TABLE foobar_data
CHANGE COLUMN col VARCHAR(255) NOT NULL SET DEFAULT '{}';
Initially, the table "MyTable" has been defined in the following way:
CREATE TABLE IF NOT EXISTS `MyTable` (
`Col1` smallint(6) NOT NULL AUTO_INCREMENT,
`Col2` smallint(6) DEFAULT NULL,
`Col3` varchar(20) NOT NULL,
);
How to update it in such a way that the column "Col 3" would be allowed to be NULL?
The following MySQL statement should modify your column to accept NULLs.
ALTER TABLE `MyTable`
ALTER COLUMN `Col3` varchar(20) DEFAULT NULL
ALTER TABLE MyTable MODIFY Col3 varchar(20) NULL;
This works in PSQL, not sure if it also works in normal SQL.
ALTER TABLE tablename
ALTER COLUMN columnname DROP NOT NULL;
ALTER TABLE school MODIFY COLUMN school_van varchar(36) DEFAULT NULL;
"ALTER" keyword didn't work but "MODIFY" worked fine in MySQL 8.0.26