Search code examples
mysqldatabaserdbms

Specify character_set_client as default charset for Table in MySQL


I have the following query to create a table:

  CREATE TABLE qrtz_simprop_triggers
            (          
            SCHED_NAME VARCHAR(120) NOT NULL,
            TRIGGER_NAME VARCHAR(200) NOT NULL,
            TRIGGER_GROUP VARCHAR(200) NOT NULL,
            STR_PROP_1 VARCHAR(512) NULL,
            STR_PROP_2 VARCHAR(512) NULL,
            STR_PROP_3 VARCHAR(512) NULL,
            INT_PROP_1 INT NULL,
            INT_PROP_2 INT NULL,
            LONG_PROP_1 BIGINT NULL,
            LONG_PROP_2 BIGINT NULL,
            DEC_PROP_1 NUMERIC(13,4) NULL,
            DEC_PROP_2 NUMERIC(13,4) NULL,
            BOOL_PROP_1 BOOL NULL,
            BOOL_PROP_2 BOOL NULL,
            PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)) ENGINE=InnoDB 
default charset=<value of character_set_client>;

That is, if the value of character_set_client is UTF8, then default charset should be utf8 and if character_set_client is latin1, then default charset should be latin1.

This is very important for me and I am not able to crack it even after spending hours. Any help would be highly appreciated.


Solution

  • You'd have to do this with dynamic SQL. You can't use variables directly in a CREATE TABLE statement.

    SET @sql = CONCAT('CREATE TABLE ... DEFAULT CHARSET=', @@character_set_client);
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    However, I wonder why you're doing this. I would normally allow the table charset to default to the charset for the schema. It seems odd to use the client charset.