Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top