Skip to content
Advertisement

How to change column charset, set as not null and add a default value?

The column contains some rules such as not nullable and character_set_name is latin1 and I should write a query to update only the character_set_name.

field: name

type: varchar(255)

null: NO

default: JACK

character_set_name: latin1

Is there any way to do this in one query?

ALTER TABLE table MODIFY name VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE table MODIFY name VARCHAR(255) NOT NULL DEFAULT 'JACK';

if I run only the first query, ‘name’ will accept null values and default=null.

Advertisement

Answer

yes, simpy write them together

ALTER TABLE table1 MODIFY name VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'JACK';
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement