不管我们喜欢与否,我们开发人员中的许多人(如果不是大多数的话)都经常使用数据库,或者有一天可能不得不使用数据库。考虑到大量的误用和滥用,以及每天出现的大量与数据库相关的问题,公平地说,有一些概念是开发人员应该知道的——即使他们今天不设计或使用数据库。
关于数据库,开发人员和其他软件专业人员应该知道的一个重要概念是什么?
不管我们喜欢与否,我们开发人员中的许多人(如果不是大多数的话)都经常使用数据库,或者有一天可能不得不使用数据库。考虑到大量的误用和滥用,以及每天出现的大量与数据库相关的问题,公平地说,有一些概念是开发人员应该知道的——即使他们今天不设计或使用数据库。
关于数据库,开发人员和其他软件专业人员应该知道的一个重要概念是什么?
当前回答
好问题。以下是一些想法,排名不分先后:
Normalization, to at least the second normal form, is essential. Referential integrity is also essential, with proper cascading delete and update considerations. Good and proper use of check constraints. Let the database do as much work as possible. Don't scatter business logic in both the database and middle tier code. Pick one or the other, preferably in middle tier code. Decide on a consistent approach for primary keys and clustered keys. Don't over index. Choose your indexes wisely. Consistent table and column naming. Pick a standard and stick to it. Limit the number of columns in the database that will accept null values. Don't get carried away with triggers. They have their use but can complicate things in a hurry. Be careful with UDFs. They are great but can cause performance problems when you're not aware how often they might get called in a query. Get Celko's book on database design. The man is arrogant but knows his stuff.
其他回答
好问题。以下是一些想法,排名不分先后:
Normalization, to at least the second normal form, is essential. Referential integrity is also essential, with proper cascading delete and update considerations. Good and proper use of check constraints. Let the database do as much work as possible. Don't scatter business logic in both the database and middle tier code. Pick one or the other, preferably in middle tier code. Decide on a consistent approach for primary keys and clustered keys. Don't over index. Choose your indexes wisely. Consistent table and column naming. Pick a standard and stick to it. Limit the number of columns in the database that will accept null values. Don't get carried away with triggers. They have their use but can complicate things in a hurry. Be careful with UDFs. They are great but can cause performance problems when you're not aware how often they might get called in a query. Get Celko's book on database design. The man is arrogant but knows his stuff.
非常好的问题。让我们看看,首先,没有完全理解连接的人不应该考虑查询数据库。这就像开车时不知道方向盘和刹车在哪里一样。您还需要了解数据类型以及如何选择最佳数据类型。
开发人员应该了解的另一件事是,在设计数据库时,你应该记住三件事:
Data integrity - if the data can't be relied on you essentially have no data - this means do not put required logic in the application as many other sources may touch the database. Constraints, foreign keys and sometimes triggers are necessary to data integrity. Don't fail to use them because you don't like them or don't want to be bothered to understand them. Performance - it is very hard to refactor a poorly performing database and performance should be considered from the start. There are many ways to do the same query and some are known to be faster almost always, it is short-sighted not to learn and use these ways. Read some books on performance tuning before designing queries or database structures. Security - this data is the life-blood of your company, it also frequently contains personal information that can be stolen. Learn to protect your data from SQL injection attacks and fraud and identity theft.
在查询数据库时,很容易得到错误的答案。确保完全理解数据模型。请记住,实际决策通常是基于查询返回的数据做出的。当它是错误的,就会做出错误的商业决策。你可能会因为糟糕的询问而杀死一家公司,或者失去一个大客户。数据是有意义的,但开发者往往忘记了这一点。
数据几乎永远不会消失,考虑的是随着时间的推移存储数据,而不是今天如何获取数据。数据库在拥有10万条记录时运行良好,十年后可能就不那么好了。应用程序很少能像数据一样持久。这就是为什么性能设计如此重要的原因之一。
您的数据库可能需要应用程序不需要看到的字段。比如用于复制的guid,插入的日期字段。等。您还可能需要存储更改的历史,以及谁在什么时候做了更改,并能够从这个存储库中恢复坏的更改。在向网站询问如何修复忘记在更新中添加where子句并更新整个表的问题之前,请考虑一下您打算如何做到这一点。
永远不要在比生产版本更新的数据库版本中进行开发。永远、永远、永远不要直接针对生产数据库进行开发。
如果没有数据库管理员,请确保有人正在进行备份,并且知道如何恢复备份,并且已经测试过如何恢复备份。
数据库代码就是代码,没有理由不把它像其他代码一样放在源代码控制中。
不要依赖于SQL查询返回的行顺序。
对于一个经常使用数据库(每天或几乎每天编写/维护查询)的中间派专业开发人员,我认为期望应该与任何其他领域相同:你在大学里写过一个。
每个c++极客在大学里都写过一个字符串类。每个图形狂人在大学里都写过一个光线追踪器。每个网络极客在大学里都写过交互式网站(通常在我们有“web框架”之前)。每个硬件书呆子(甚至软件书呆子)在大学里都做过CPU。大学里每个内科医生都解剖过一整具尸体,即使她今天只是给我量血压,告诉我胆固醇太高。为什么数据库会有所不同呢?
不幸的是,由于某种原因,他们今天看起来确实不一样了。人们希望。net程序员知道字符串在C语言中是如何工作的,但是RDBMS的内部结构不应该太关心你。
仅仅通过阅读,甚至从上到下都不可能达到同样的理解水平。但是,如果您从底层开始并理解每个部分,那么就相对容易找出数据库的细节。甚至是许多数据库极客似乎无法理解的事情,比如何时使用非关系数据库。
也许这有点严格,特别是如果你在大学里没有学习计算机科学。我会把它调低一些:你今天完全可以从头开始写一个。我不关心你是否知道PostgreSQL查询优化器的工作原理,但如果你知道足够多的知识自己编写一个,它可能不会与他们所做的有太大的不同。你知道,写一个基本的公式并不难。
我认为每个开发人员都应该理解数据库需要不同的范例。
在编写查询以获取数据时,需要一种基于集的方法。许多具有交互背景的人对此感到困惑。然而,当他们接受它时,他们可以获得更好的结果,即使解决方案可能不是第一次出现在他们以迭代为中心的思想中的解决方案。