0

I came across this query in a book i'm learning from.

  CREATE TABLE `articles` ( 
      `title` varchar(1000), 
      `content` text, 
      `links` text, 
      `image` varchar(500), 
      `url` varchar(500), 
      UNIQUE KEY `url` (`url`) 
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 

I want to understand the last line of this query, in particular "DEFAULT CHARSET=utf8"

I looked up the documentation on table options

[DEFAULT] CHARACTER SET

Specifies a default character set for the table. CHARSET is a synonym for CHARACTER SET. If the character set name is DEFAULT, the database character set is used.

From the first line of this explanation, "[DEFAULT] CHARACTER SET" it looks to me as if this actually setting "CHARACTER SET" to default for all subsequent table creations, as well as this current one?

in the case of the code from the book, database character set is different to utf8, which would be used? If it's utf8 still, why have they put DEFAULT there anyway?

Is the last line of the documentation above talking about this?

CREATE TABLE `articles` ( 
      `title` varchar(1000), 
      `content` text, 
      `links` text, 
      `image` varchar(500), 
      `url` varchar(500), 
      UNIQUE KEY `url` (`url`) 
    ) ENGINE=InnoDB DEFAULT CHARSET=DEFAULT 

thanks.

1

You can remove Default, it is not mandatory. If you look att the grammar it says:

| [DEFAULT] CHARACTER Set =

Meaning that the table gets this CHARACTER SET. It does not affect other tables.

If no CHARACTER Set is declared it is "inherited" from the database definition. This is the same as:

CHARACTER Set = DEFAULT

I dont know if it is part of the confusion, but there are two different DEFAULT keywords here, with different meaning. The first says what the default CHARACTER Set for this table is, the second one that the default character set for the database should be used.

| improve this answer | |
  • hi @Lennart . Are you saying that the first DEFAULT is optional, but it is always there, even implicitly to catch the case that we do not explicitly use the CHARACTER SET = expression in the table creation statement? – Bucephalus Aug 23 at 13:26
  • 1
    DEFAULT in DEFAULT CHARACTER SET = xxxx Is the same as CHARACTER SET = xxxx. The second DEFAULT explicitly says to use the database CHARACTER SET. I.e. it is the same as not specifying the CHARACTER Set for the table. – Lennart Aug 23 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.