The syntax you used is from a SQL Server example, not from MySQL. It would be good to check the MySQL documentation about ALTER TABLE syntax.
In MySQL, the ALTER COLUMN subclause can only be used for setting or dropping the default value of the column (SET DEFAULT literal or DROP DEFAULT).
You need to use either CHANGE COLUMN (note that column name is doubled as you could use this to change its name):
ALTER TABLE MyTable
CHANGE COLUMN comment comment BIGINT NOT NULL;
or MODIFY COLUMN:
ALTER TABLE MyTable
MODIFY COLUMN comment BIGINT NOT NULL;
Now, there are 2 more problems, beside the syntax:
Seeing the
CREATE TABLE, converting a "comment" column fromTEXTtoBIGINTdoes not make much sense. I'll assume that you want to keep itTEXTand only make itNOT NULL.There might be
NULLvalues already in the table. If that's the case, the statement will fail with errors. So, you have to firstUPDATEthose values to some non-null value (say the empty string) and then modify the column:
So our statements become:
UPDATE glpi_ticketsatisfactions
SET comment = ''
WHERE Comment IS NULL ;
ALTER TABLE glpi_ticketsatisfactions
MODIFY COLUMN comment TEXT COLLATE utf8_unicode_ci NOT NULL ;
Answer from ypercubeᵀᴹ on Stack ExchangeYes, by using COALESCE.
SELECT COALESCE(null_column, 0) AS null_column FROM whatever;
COALESCE goes through the list of values you give it, and returns the first non-null value.
I am adding this answer because no one mentioned IFNULL function
You can use IFNULL
SELECT IFNULL(column_name, 0) FROM table_name;
IFNULL will return column's value (if it has something other than NULL) otherwise second parameter passed (in this case 0).
You want the following:
ALTER TABLE mytable MODIFY mycolumn VARCHAR(255);
Columns are nullable by default. As long as the column is not declared UNIQUE or NOT NULL, there shouldn't be any problems.
Your syntax error is caused by a missing "table" in the query
ALTER TABLE mytable MODIFY mycolumn varchar(255) null;
Just use an ALTER TABLE... MODIFY... query and add NOT NULL into your existing column definition. For example:
ALTER TABLE Person MODIFY P_Id INT(11) NOT NULL;
A word of caution: you need to specify the full column definition again when using a MODIFY query. If your column has, for example, a DEFAULT value, or a column comment, you need to specify it in the MODIFY statement along with the data type and the NOT NULL, or it will be lost. The safest practice to guard against such mishaps is to copy the column definition from the output of a SHOW CREATE TABLE YourTable query, modify it to include the NOT NULL constraint, and paste it into your ALTER TABLE... MODIFY... query.
Try this, you will know the difference between change and modify,
ALTER TABLE table_name CHANGE curr_column_name new_column_name new_column_datatype [constraints]
ALTER TABLE table_name MODIFY column_name new_column_datatype [constraints]
- You can change name and datatype of the particular column using
CHANGE. - You can modify the particular column datatype using
MODIFY. You cannot change the name of the column using this statement.
Hope, I explained well in detail.
If you really must output every values including the NULL ones:
select IFNULL(prereq,"") from test
SELECT COALESCE(prereq, '') FROM test
Coalesce will return the first non-null argument passed to it from left to right. If all arguemnts are null, it'll return null, but we're forcing an empty string there, so no null values will be returned.
Also note that the COALESCE operator is supported in standard SQL. This is not the case of IFNULL. So it is a good practice to get use the former. Additionally, bear in mind that COALESCE supports more than 2 parameters and it will iterate over them until a non-null coincidence is found.