简体   繁体   English

MySQL的数据库架构设计,有何建议?

[英]Database schema design for MySQL, suggestions?

So, have the chance to do my first "real" database, just thinking to myself what I should think about... any suggestions? 因此,有机会建立我的第一个“真实”数据库,只是对自己进行思考,我应该考虑...任何建议?

  • Select DB (using MySQL) 选择数据库(使用MySQL)
  • Select DB engine based on needs (using MySQL, know MyISAM vs InnoDB) 根据需要选择数据库引擎(使用MySQL,了解MyISAM与InnoDB)
  • CREATE TABLE documention for DB using 使用DB创建CREATE TABLE文档
  • Column definition (select right datatype) 列定义(选择正确的数据类型)
  • Model data to be normalized 模型数据要规范化
  • Referential Integrity: Primary Key (Natural or Surrogate); 参照完整性:主键(自然或替代); Composite Keys; 复合键; Foreign Key 外键

Which leads me to my main question, how do I know if the database is a success at fit enough? 这就引出了我的主要问题,我如何知道数据库是否足够成功?

As you are listing general questions that need to be answered in order to go from idea to working system I'll propose that it is very important to organize them into 当您列出从构思到工作系统需要回答的一般问题时,我建议将它们组织成

1) logical design (get a clean model that represents the problem space you are trying to model) 1)逻辑设计(获取代表您要建模的问题空间的干净模型)

2) physical design (which RDBMS and storage engine, exact data types, other practical and performance related decisions) 2)物理设计(哪些RDBMS和存储引擎,确切的数据类型,其他与性能和性能有关的决策)

You are doing too much of mixing between the two. 您在两者之间做得太多了。 When you get a clean logical model and know the relationships between the entities you are modelling then the physical modelling will not be hard. 当您获得干净的逻辑模型并知道要建模的实体之间的关系时,物理建模就不会困难了。

EDIT: There are many books that deal with the steps of logical data design, but normally you would try to: 编辑:有很多书籍涉及逻辑数据设计的步骤,但是通常您会尝试:

  1. define use cases and business requirements (things are pretty soft still, check the requirements for contradictions; this is done interviewing people who know business process well, which can degenerate to a discussion with yourself) 定义用例和业务需求(事情还很软,检查矛盾的需求;这是通过采访了解业务流程的人来完成的,这些人可能演变成与您自己进行讨论的人)
  2. get a list of all the attributes and entities used across the system and define them (data dictionary) 获取整个系统中使用的所有属性和实体的列表并进行定义(数据字典)
  3. determine the domain of the attributes (which, later at physical level can be accomplished as data type, check contraint or by referring to 'helper' table, but don't worry about this yet, just make sure that you define domains well) 确定属性的域(稍后可以在物理级别将其作为数据类型来完成,检查约束或通过引用“ helper”表,但不必担心,只需确保定义正确即可)
  4. draw ER/UML diagrams defining relationships - define tables in terms of primary keys, foreign keys and all other attributes (this time aim for a completeness); 绘制定义关系的ER / UML图-根据主键,外键和所有其他属性来定义表(这次旨在保持完整性); this step can be done using CAM and decent diagramming tools will spit out CREATE DATABASE scripts from diagrams 可以使用CAM完成此步骤,体面的图表工具将从图表中吐出CREATE DATABASE脚本
  5. examine the model in search for denormalized data (should be normalized already, however when translating problem space into logical model it is possible to make mistakes and discover that you have redundancy or other anomalies) 检查模型以查找非规范化数据(应该已经规范化,但是将问题空间转换为逻辑模型时,可能会犯错误并发现您有冗余或其他异常情况)

A few of these steps need to go back and forth as you consider different ways of accomplishing certain tasks. 当您考虑完成某些任务的不同方法时,需要反复执行其中一些步骤。 For example including new attributes might make you go and analyze a new use case. 例如,包含新属性可能使您去分析新的用例。 Or a discovery of contradicting requirement might lead to a discovery of a whole new entity. 或者发现矛盾的需求可能会导致发现一个全新的实体。 Or discovering a redundancy might lead you to a discovery of undocumented process that exists (and justifies, or rather, explains percieved redundancy by redefining a seemingly duplicate attribute). 否则,发现冗余可能会导致您发现存在的未记录过程(并通过重新定义看似重复的属性来证明或确切地解释了冗余)。 Etc... 等等...

  1. Model your data and normalise it before defining your columns. 在定义列之前,对数据建模并对其进行规范化。 This needs to be your first task even before selecting a database and table tiypes as it will allow you to get clarity about the task you are modeling. 即使在选择数据库和表类型之前,这也应该是您的第一项任务,因为这将使您对正在建模的任务更加清楚。

  2. Select DB engine based on needs (using MySQL, know MyISAM vs InnoDB) The trade off is "query performance" v "transactions". 根据需要选择数据库引擎(使用MySQL,了解MyISAM与InnoDB)。需要权衡的是“查询性能”与“事务”。 In most cases innoDB performance is good enough and the benefits of transactions outweigh any downside. 在大多数情况下,innoDB性能足够好,事务的好处胜过任何弊端。

  3. Create table documentation is available on the MySQL website. 创建表文档可在MySQL网站上找到。

As Unreason says "When you get a clean logical model and know the relationships between the entities you are modelling then the physical modelling will not be hard". 正如Unreason所说:“当您获得一个干净的逻辑模型并知道要建模的实体之间的关系时,物理建模就不会困难了”。

Success can be measured in various ways. 成功可以通过多种方式来衡量。 Money in your pocket, good performance on low priced hardware. 口袋里的钱,在低价硬件上的良好性能。 Lots of happy comments from users ... Like Stackoverflow:) 来自用户的许多快乐评论……例如Stackoverflow :)

1. Selecting a RDBMS is largely a matter of preference. 1.选择RDBMS主要是一个优先事项。 You seem to be leaning towards MySQL already. 您似乎已经倾向于MySQL。 That's okay, because MySQL is cheap and popular. 没关系,因为MySQL很便宜而且很流行。 However, you are left with not having an engine that can do transactions and full-text search at the same time (between MyISAM and InnoDB). 但是,您没有一个可以同时进行事务和全文本搜索的引擎(在MyISAM和InnoDB之间)。 Fulltext Search with InnoDB 使用InnoDB进行全文搜索

2. (and 4) MyISAM vs InnoDB and datatypes 2. (和4) MyISAM与InnoDB和数据类型

  • MyISAM for: full-text search and table level locking MyISAM用于:全文搜索和表级锁定
  • InnoDB for: transactions, FKs, and row level locking (but no full-text search) InnoDB用于:事务,FK和行级别锁定(但不提供全文搜索)
  • Also, InnoDB will probably perform better with large number of rows because of row level locking versus table level locking 另外,由于行级锁定与表级锁定相比,InnoDB在行数较多时可能会表现更好

3. CREATE TABLE ? 3. CREATE TABLE I prefer to use a database IDE, like Toad for MySQL 我更喜欢使用数据库IDE,例如MySQL的Toad

5. (and 6) Review of DB normalization/PKs/FKs (You'll need to use InnoDB for FKs.) 5. (和6) 查看数据库规范化/ PK / FK (您需要将InnoDB用于FK。)

7. You forgot indexes! 7. 您忘记了索引! Very important factor in a database. 数据库中非常重要的因素。

Yes MySQL is a good fit if you have the above requirements. 是的,如果您具有上述要求,那么MySQL非常适合。

However, as I said, with MySQL/MyISAM/InnoDB, you don't have an engine that can do full-text search AND transactions/FKs. 但是,就像我说的那样,对于MySQL / MyISAM / InnoDB,您没有能够执行全文本搜索事务/ FK的引擎。 A simple option is to have a 2nd copy (in MyISAM) of the InnoDB tables that need full-text search capability. 一个简单的选择是拥有需要全文搜索功能的InnoDB表的第二个副本(在MyISAM中)。 You can do this because you can mix the 2 engines in the same database. 您可以执行此操作,因为您可以将两个引擎混合在同一数据库中。 Or, maybe you don't even need full-text search because LIKE is sufficient for your application. 或者,也许您甚至不需要全文本搜索,因为LIKE足以满足您的应用需求。

On the other hand, with SQL Server, you can have all the features, including full-text, transactions, and FKs all in one engine. 另一方面,使用SQL Server,您可以在一个引擎中拥有所有功能,包括全文本,事务和FK。

Yet another option, is to use a separate technology for indexed full-text searches. 另一种选择是使用单独的技术进行索引全文搜索。 There's a plugin for MySQL: 有一个MySQL插件:

Example: 例:

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

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