Somebody wanted to know if you could add a NOT NULL
column constraint in MySQL. That’s a great question and the answer is yes. The following example shows you how to do it.
- Create a sample table without a
NOT NULL
constraint on a column that should have one. After creating this table, describe it and you’ll see that the testing_text
column is
CREATE TABLE testing
( testing_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY
, testing_text VARCHAR(10)); |
CREATE TABLE testing
( testing_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY
, testing_text VARCHAR(10));
- Change the column definition from null allowed to not null for the
TESTING_TEXT
column. The only problem with this syntax is that it only works when there are no null values in the table or there are no rows in the table.
ALTER TABLE testing
CHANGE testing_text testing_text VARCHAR(10) NOT NULL; |
ALTER TABLE testing
CHANGE testing_text testing_text VARCHAR(10) NOT NULL;
- Change the column definition from not null constrained to null allowed for the
TESTING_TEXT
column.
ALTER TABLE testing
CHANGE testing_text testing_text VARCHAR(10); |
ALTER TABLE testing
CHANGE testing_text testing_text VARCHAR(10);
As always, I hope this helps.