如何创建和更新 SQL 脚本?

发布于 2024-11-18 14:41:53 字数 625 浏览 5 评论 0原文

我想知道创建和维护数据库映射和 SQL 脚本的最佳方法是什么。我知道我更喜欢在文本编辑器上手动编写 SQL 脚本(加上设计一个地图放在我旁边的一张纸上),而其他人更喜欢使用数据库地图软件(MySQL Workbench、Visual Studio ...这篇文章中列出了一些)以自动生成脚本。

两种解决方案都有优点和缺点。我看到了这些优点:

  • 手动编写 SQL 脚本
    1. 完全知道您所写的内容。
    2. 您能够保持干净且易于阅读的 SQL 代码。
    3. 通过编写代码,您可以更好地了解数据库详细信息。
    4. 编写代码可以让您练习 SQL 知识。
  • 从设计的地图自动生成脚本
    1. 节省一些时间。
    2. 即使不了解 SQL 语言,您也可以生成脚本(即使我将其列为手动 SQL 脚本的相反优势)。
    3. 防止拼写错误。

您怎么看,您将采取什么方式进行?

I'm wondering what is the best way to create and maintain databases maps and SQL scripts. I know I'm more a fan of writing the SQL script by hand on a text editor (plus designing a map to have next to me on a piece of paper), while others prefers to use database maps softwares (MySQL Workbench, Visual Studio... some listed in this post) to automatically generate the script.

Both solutions have advantages and drawbacks. I see these advantages:

  • SQL script by hand:
    1. You know exactly what you write.
    2. You are able to keep a clean and easily readable SQL code.
    3. Writing the code, you keep a better view of your database specifics.
    4. Writing the code enables you to to practive your SQL knowledge.
  • Automatically generated script from designed map:
    1. Saves some time.
    2. Even without knowing SQL language, you can generate a script (even if I listed this one as an contrary advantage for SQL script by hand).
    3. Prevents typos.

What do you think, what way do you proceed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是你 2024-11-25 14:41:53

如果自动化某件事可以节省时间并减少人为错误,您为什么不这样做呢?如果满足以下条件,我会尝试自动生成 SQL:

  1. 我已经填充了一个测试数据库,并通过编写和执行我没有跟踪的临时 SQL 非查询来以有机方式构建了模式;如果
  2. 数据库相当简单并且标准化程度较低

另一种罕见的情况可能是,如果您有大量数据集列表,需要以 SQL 的形式提取模式。

手工编写的好处是您了解设计的每个部分,并且能够更好地强制数据完整性(例如通过使用外键或限制数据类型)。简而言之,我同意你的专业人士名单。

对于升级脚本,我强烈喜欢手动编写这些脚本,因为它们通常相当短,而且对于最终运行它们的人来说更容易阅读是件好事。

我对升级脚本的一个技巧是确保每个脚本在表中添加一行,说明升级版本号,该表专门用于确保数据库的版本正确。像这样的东西:

INSERT INTO DB_VERSION (upgrade_time, version_from, version_to, comment)
    VALUES ('2011-07-04T120320', '2.2.4', '2.3', 'add column x to table y.')

If automating something saves time and human error why wouldn't you do it? I would try auto-generating SQL if:

  1. I had already populated a test database and built up the schema in an organic fashion by writing and executing ad hoc SQL non-queries which I hadn't kept track of; and
  2. If the database was reasonably simple and with a low amount of normalisation

Another rare case might be if you had a massive list of data sets for which the schema needed to be extracted in the form of SQL.

The benefits of writing by hand are that you are aware of every part of the design and better able to enforce data integrity (for instance through use of foreign keys or limiting data types). In short, I would agree with your list of pros.

For upgrade scripts I strongly prefer writing these by hand because they're usually fairly short and it's nice for them to be more readable for the people who end up running them.

One tip I'd have for upgrade scripts is to make sure each script adds a row stating the upgrade version number in a table which is exclusively there to ensure the database is versioned correctly. Something like:

INSERT INTO DB_VERSION (upgrade_time, version_from, version_to, comment)
    VALUES ('2011-07-04T120320', '2.2.4', '2.3', 'add column x to table y.')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文