简体   繁体   English

关于create table ddl格式的mysql查询

[英]mysql query about create table ddl format

I am a mysql newbie. 我是一个mysql新手。 I have a question about the right thing to do for create table ddl. 我有一个关于为create table ddl做正确的事情的问题。 Up until now I have just been writing create table ddl like this... 到目前为止,我刚刚写了这样的create table ddl ...

CREATE TABLE file (
    file_id mediumint(10) unsigned NOT NULL AUTO_INCREMENT,
    filename varchar(100) NOT NULL,
    file_notes varchar(100) DEFAULT NULL,
    file_size mediumint(10) DEFAULT NULL,
    file_type varchar(40) DEFAULT NULL,
    file longblob DEFAULT NULL,
  CONSTRAINT pk_file PRIMARY KEY (file_id)
);

But I often see people doing their create table ddl like this... 但我经常看到人们正在做他们的创建表ddl这样...

CREATE TABLE IF NOT EXISTS `etags` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `item_code` varchar(100) NOT NULL,
  `item_description` varchar(500) NOT NULL,
  `btn_type` enum('primary','important','success','default','warning') NOT NULL DEFAULT 'default',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

A few questions... 几个问题...

  1. What difference do the quotes around the table name and column names make? 表名和列名的引号有什么区别?

  2. Is it good practice to explicitly declare the engine and character set? 显式声明引擎和字符集是一种好习惯吗? What engine and character sets are used by default? 默认使用哪些引擎和字符集?

thanks 谢谢

  1. There's no difference. 没有区别。 Identifiers (table names, column names, et al.) must be enclosed in the backticks if they contain special characters or are reserved words. 标识符(表名,列名等)必须包含在反引号中,如果它们包含特殊字符或保留字。 Otherwise, the backticks are optional. 否则,反引号是可选的。

  2. Yes, it's good practice, for portability to other systems. 是的,这是一种很好的做法,可以移植到其他系统。 If you re-create the table, having the storage engine and character set specified explicitly in the CREATE TABLE statement means that your statement won't be dependent on the settings of the default_character_set and default-storage-engine variables (these may get changed, or be set differently on another database.) 如果重新创建表,则在CREATE TABLE语句中显式指定存储引擎和字符集意味着您的语句将不依赖于default_character_setdefault-storage-engine变量的设置(这些变量可能会更改,或者在另一个数据库上设置不同。)


You can get your table DDL definition in that same format using the SHOW CREATE TABLE statement, eg 您可以使用SHOW CREATE TABLE语句以相同的格式获取表DDL定义,例如

SHOW CREATE TABLE `file`

The CREATE TABLE DDL syntax you are seeing posted by other users is typically in the format produced as output of this statement. 您看到的其他用户发布的CREATE TABLE DDL语法通常采用生成此语句输出的格式。 Note that MySQL doesn't bother with checking whether an identifier contains special characters or reserved words (to see if backticks are required or not), it just goes ahead and wraps all of the identifiers in backticks. 请注意,MySQL不会检查标识符是否包含特殊字符或保留字(以查看是否需要反引号),它只是继续并将所有标识符包装在反引号中。

  1. With backticks, reserved words and some special characters can be used in names. 使用反引号,可以在名称中使用保留字和一些特殊字符。
    It's simply a safety measure and many tools automatically add these. 这只是一种安全措施,许多工具会自动添加这些措施。

  2. The default engine and charset can be set in the servers configuration. 可以在服务器配置中设置默认引擎和字符集。
    They are often (but not always) set to MyISAM and latin1 . 它们通常(但不总是)设置为MyISAMlatin1

Personally, I would consider it good practice to define engine and charset, just so you can be certain what you end up with. 就个人而言,我认为定义引擎和字符集是一种很好的做法,这样您就可以确定最终的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM