如何创建和更新 SQL 脚本?
我想知道创建和维护数据库映射和 SQL 脚本的最佳方法是什么。我知道我更喜欢在文本编辑器上手动编写 SQL 脚本(加上设计一个地图放在我旁边的一张纸上),而其他人更喜欢使用数据库地图软件(MySQL Workbench、Visual Studio ...这篇文章中列出了一些)以自动生成脚本。
两种解决方案都有优点和缺点。我看到了这些优点:
- 手动编写 SQL 脚本:
- 您完全知道您所写的内容。
- 您能够保持干净且易于阅读的 SQL 代码。
- 通过编写代码,您可以更好地了解数据库详细信息。
- 编写代码可以让您练习 SQL 知识。
- 从设计的地图自动生成脚本:
- 节省一些时间。
- 即使不了解 SQL 语言,您也可以生成脚本(即使我将其列为手动 SQL 脚本的相反优势)。
- 防止拼写错误。
您怎么看,您将采取什么方式进行?
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:
- You know exactly what you write.
- You are able to keep a clean and easily readable SQL code.
- Writing the code, you keep a better view of your database specifics.
- Writing the code enables you to to practive your SQL knowledge.
- Automatically generated script from designed map:
- Saves some time.
- 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).
- Prevents typos.
What do you think, what way do you proceed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果自动化某件事可以节省时间并减少人为错误,您为什么不这样做呢?如果满足以下条件,我会尝试自动生成 SQL:
另一种罕见的情况可能是,如果您有大量数据集列表,需要以 SQL 的形式提取模式。
手工编写的好处是您了解设计的每个部分,并且能够更好地强制数据完整性(例如通过使用外键或限制数据类型)。简而言之,我同意你的专业人士名单。
对于升级脚本,我强烈喜欢手动编写这些脚本,因为它们通常相当短,而且对于最终运行它们的人来说更容易阅读是件好事。
我对升级脚本的一个技巧是确保每个脚本在表中添加一行,说明升级版本号,该表专门用于确保数据库的版本正确。像这样的东西:
If automating something saves time and human error why wouldn't you do it? I would try auto-generating SQL if:
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: