27

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?

0

4 Answers 4

31

The following MySQL statement should modify your column to accept NULLs.

ALTER TABLE `MyTable`
ALTER COLUMN `Col3` varchar(20) DEFAULT NULL
Sign up to request clarification or add additional context in comments.

7 Comments

Yeah,but when I edit the column Col 3 (Date), it get the value '0000-00-00' automatically. But I want it to be equal to NULL.
UPDATE TABLE MyTable SET Col3 = null WHERE Col3='0000-00-00'
Wait... isn't Col3 a varchar(20), not Date ?
Empty strings and NULL values are different things, in MySQL query browser it is a little bit confusing this.
I do it without "DEFAULT" keyword.is it necessary?
|
30
ALTER TABLE MyTable MODIFY Col3 varchar(20) NULL;

7 Comments

When I edit the column (try to make it empty) in MySQL Query Browser, the following error message occurs: Data truncated for column 'Col 3' at row 1. It happens after your updates.
if i try ALTER TABLE spen_recipe MODIFY Null varchar(20) NULL; i get err
@SachinS: What's that first Null doing there?
have jus set the description as default to unsigned NOT NULL
@SachinS: It should be ALTER TABLE <table_name> MODIFY <column_name> <data_type> NULL;.
|
0

This works in PSQL, not sure if it also works in normal SQL.

ALTER TABLE tablename
ALTER COLUMN columnname DROP NOT NULL;

Comments

0
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

1 Comment

This answer was reviewed in the Low Quality Queue. Here are some guidelines for How do I write a good answer?. Code only answers are not considered good answers, and are likely to be downvoted and/or deleted because they are less useful to a community of learners. It's only obvious to you. Explain what it does, and how it's different / better than existing answers. From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.