应该从后端代码外部创建MySQL数据库和表吗?

huangapple go评论55阅读模式
英文:

Should we create MySQL DATABASES/TABLES from outside of our backend code?

问题

我是一个新的MySQL学习者,喜欢在NodeJS应用程序中使用它。根据我以前对MongoDB的了解,所有我们需要的表格都会被MongoDB自动创建。我们只需要连接到MongoDB,并使用"create"或"save"命令,它就能找到表格并放入内容,或者如果不存在的话就创建表格。

但是从我迄今为止看到的MySQL教程中,它们通过在MySQL shell/bash中直接执行SQL代码或使用诸如"MySQL Workbench"之类的应用程序来手动创建数据库。

所以我想确保哪种方式是正确的?我应该在运行Node应用程序之前创建我的表格,还是有一种方式,根据用户使用应用程序的情况在代码中动态创建表格也是一种好方法?如果第二种方式更好,我该如何做呢?有没有创建数据库的代码示例可以在Node应用程序内部使用?

此外,对于数据库也是同样的问题。我们是否可以或者说从我们的Node应用程序内部创建它们是一个好的方式吗?

英文:

I am a new MySQL learner and like to use it within a NodeJS application. From my previous knowledge with MongoDB all the tables we need will be created by MongoDB automatically. We only needed to connect to the MongoDB and with create or save commands it could find the table and put something inside or create the table if it was not exist.

But from MySQL tutorials I have seen so far they create databases manually by executing SQL codes directly inside a MySQL shell/bash or by using applications like MySQL Workbench.

So I want to make sure which way is correct? Should I create my tables before running my Node app or there is also a way, and it is a good way to create tables dynamically within the code when it's needed regarding the app usage by the user? If the second way is a better way, how can I do that? Is there a code sample for creating databases from inside a Node app?

Moreover the same question for databases. Can we or is it a good way creating them from inside our Node app?

答案1

得分: 2

Both are possible. You can create a piece of code that will check the schema and update it if it is not (there are lots of tools and for all languages). In my experience, in most cases we tend to create the tables, indexes, views, etc. by hand first and then run the application. For larger companies / projects, they often use tools. I personally have seen a lot of Flyway in the Java world. In any case, the most important thing is that your creation script is idempotent. For this, you can use the "if not exists" statements: MySQL documentation

英文:

Both are possible.

You can create a piece of code that will check the schema and update it if it is not (there are lots of tools and for all languages).

In my experience, in most cases we tend to create the tables, indexes, views, etc. by hand first and then run the application. For larger companies / projects, they often use tools. I personally have seen a lot of flyway in the Java world.

In any case, the most important thing is that your creation script is idempotent. For this, you can use the if not exists statements : https://dev.mysql.com/doc/refman/8.0/en/replication-features-create-if-not-exists.html

答案2

得分: 2

在基于 SQL 的应用程序中,标准表在安装应用程序时创建,给定了固定的结构。表结构只在程序更新时更改,因为结构更改可能是一项非常昂贵的操作。初始表的创建和后续更改可以通过 SQL 脚本或应用程序代码来完成 - 这两种做法都是可接受的。但主要观点是:永久表不会即时创建。

一些复杂的脚本可能需要即时创建和销毁的临时表,但其中大多数将隐式完成(例如,数据库引擎可能将子查询的结果集生成为临时表),而不是显式完成(应用程序代码执行“create temporary table”语句)。

理由:关系型数据库管理系统(RDBMS)具有严格、不灵活的数据结构,其优化重点是数据存储和检索速度。引入灵活性的任何解决方案都与关系理论不符,并伴随着其缺点(请参阅Bill Karwin在此主题上的出色答案,特别是最后两段)。

与此不同,NoSQL 解决方案更侧重于灵活性,而不是数据存储。例如,MongoDB甚至没有行和列的表。它具有由文档组成的集合,集合中的每个文档都可以具有不同的属性。这就是为什么可以即时创建这些集合和文档的原因。

英文:

In sql-based applications standard tables are created when you install the application giving it a fix structure. Table structure is only changed during program updates as structure changes can be a very expensive operation. The initial table creation, subsequent changes can be done via sql scripts or by application code - both are acceptable practices. But the main point stands: permanent tables are not created on the fly.

Some complex scripts may require temporary tables that are created and destroyed on the fly, but most of these will be done implicitly (e.g. database engine may materialise the resultset of a subquery as a temporary table), rather than explicitly (application code executing create temporary table statement).

Rationale: RDBMSs have a rigid, inflexible data structure that is optimised towards data storage and retrieval speed. Any solution to introduce flexibility is at odds with relational theory and will come with its drawbacks (see Bill Karwin's excellent answer on this subject, particularly the last two pragraphs).

NoSQL solutions, however, more focus on flexibility, than data storage. Mongodb for example, does not even have tables with rows and columns. It has collections that are made up of documents and every document in a collection may have different properties. This is why you can create these collections and documents on the fly.

huangapple
  • 本文由 发表于 2023年1月6日 04:42:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75024135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定