简体   繁体   English

MySQL数据完整性?

[英]MySQL data integrity?

In various PostgreSQL vs. MySQL comparisons I've seen many mentions of problems with data integrity in MySQL. 在各种PostgreSQL与MySQL的比较中,我已经看到很多关于MySQL数据完整性问题的提及。 Is this currently still an issue? 目前这仍然是一个问题吗?

Particularly for web applications using MySQL, are there tricks to making sure data integrity is maintained? 特别是对于使用MySQL的Web应用程序,是否有技巧来确保保持数据完整性? Or with new versions is this true 'out of the box' with no additional configuration required? 还是在新版本中,这是真正的“开箱即用”,而无需其他配置?

MySQL has a feature to support multiple storage backends (storage engine). MySQL具有支持多个存储后端(存储引擎)的功能。 The most prominent ones are MyISAM and InnoDB. 最突出的是MyISAM和InnoDB。 MyISAM is the default, in many situations quite fast but provides no data integrity. MyISAM是默认设置,在许多情况下速度非常快,但不提供数据完整性。 InnoDB supports full data integrity. InnoDB支持完整的数据完整性。 When creating a table you can set the type. 创建表时,可以设置类型。

CREATE TABLE foo (...) ENGINE=innodb;

See also http://dev.mysql.com/doc/refman/5.1/en/storage-engines.html for more details. 另请参见http://dev.mysql.com/doc/refman/5.1/en/storage-engines.html

By default MySQL created myisam engine tables, which does not support transactions or foreign keys, you need to explicitly force transactional innodb engine while creating a table. 默认情况下,MySQL创建了不支持事务或外键的myisam引擎表,您需要在创建表时显式强制使用事务型innodb引擎。

By default MySQL accepts invalid data, like date '2010-02-30', silently truncates too long textual data, too big numbers etc. But you can change it for INNODB tables using SET sql_mode = 'STRICT_TRANS_TABLES'; 默认情况下,MySQL接受无效数据,例如日期'2010-02-30',会默默地截断太长的文本数据,太大的数字等。但是您可以使用SET sql_mode = 'STRICT_TRANS_TABLES';来为INNODB表更改它SET sql_mode = 'STRICT_TRANS_TABLES'; on mysql 5.0.2 and up - see "Constraints on Invalid Data" . 在mysql 5.0.2及更高版本上-请参见“对无效数据的约束”

MySQL does not support check constraints at all, so you can not for example: MySQL根本不支持检查约束,因此您不能例如:

  • force integer data to be at some range; 强制整数数据在某个范围内;
  • force text data to be at some format (for example valid e-mail address or valid url); 强制文本数据采用某种格式(例如,有效的电子邮件地址或有效的url);
  • limit text data to valid characters (only ASCII, only ISO-8859-1, only numbers and minus etc.); 将文本数据限制为有效字符(仅ASCII,仅ISO-8859-1,仅数字和减号等);
  • disallow spaces, newlines, double-spaces, spaces at the end etc. or empty text data. 禁止使用空格,换行符,双倍空格,末尾的空格等,或文本数据为空。

So all data validation has to be done in client application. 因此,所有数据验证都必须在客户端应用程序中完成。 Which is harder to do and more error-prone. 这更难做,更容易出错。

All of this is not a problem in PostgreSQL. 所有这些在PostgreSQL中都不是问题。

No storage engine for MySQL supports CHECK constraints. MySQL的任何存储引擎都不支持CHECK约束。 Nuff said. 纳夫说。

"For other storage engines, the clauses are parsed but ignored. The CHECK clause is parsed but ignored by all storage engines." “对于其他存储引擎,该子句将被分析但会被忽略。CHECK子句将被所有存储引擎解析但会被忽略。” "The reason for accepting but ignoring syntax clauses is for compatibility, to make it easier to port code from other SQL servers, and to run applications that create tables with references" - http://dev.mysql.com/doc/refman/5.1/en/alter-table.html No enhancements for this in MySQL 5.4 or 5.5 according to the manual. “接受但忽略语法子句的原因是兼容性,它使从其他SQL Server移植代码更加容易,并易于运行使用引用创建表的应用程序” -http://dev.mysql.com/doc/refman/ 5.1 / en / alter-table.html根据手册,在MySQL 5.4或5.5中对此功能没有增强。

Please note that InnoDB does support FOREIGN KEYs. 请注意,InnoDB不支持FOREIGN KEY。

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

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