Ask HN: 外键约束还有用吗?
1 分•作者: doganugurlu•9 个月前
曾经,我们把大量的业务逻辑卸载到数据库服务器上。嗯,当时它几乎是唯一“在线”的服务器,所以感觉这么做也没什么问题。
于是,我们在数据库服务器上堆积了大量的业务逻辑。触发器、存储过程,你能想到的,我们都用上了。然后我们才意识到,存在于数据库服务器中的代码极其难以维护——或者微软决定放弃“SQL Server是所有东西的归宿”的策略。不太确定,有点模糊了。
但是,我们仍然会以约束的形式将一些逻辑卸载到数据库中,最常见的就是外键约束。
在 15 年以上的专业编程生涯中,我不记得数据库约束保护过我参与的任何一个系统。几乎 99% 的情况下,数据库约束都是在我更改本地环境时出现的,而剩下的 1% 是在紧急情况下,我不得不对生产数据库进行紧急更改的时候。我从未见过任何日志行表明数据库约束是一种有用的保护措施。
然后是 NoSQL 世界,显然,没有这些约束,生活照样继续,而且没有人试图将外键约束移植到 NoSQL 中。
我觉得数据库约束是早已逝去的时代的遗物,而需要数据库约束的需求早已消失。
你同意吗?还是我疯了?
查看原文
There was a time we offloaded a lot of business logic to the database server. Well, it was the only server "up" so it felt like it was ok.<p>So we piled on a ton of business logic on the database server. Triggers, stored procedures, you name it, we did it. And then it dawned on us that the code that lives in the database server extremely is unmaintainable - or MS decided to stop pursuing the "SQL server is where everything lives" strategy. Not sure, bit of a blur.<p>But, we still offload some logic to the database in the form of constraints, most popular one being the foreign key constraint.<p>In 15+ years of professional programming, I can't remember a time DB constraints protected a system I was working on. Almost 99% of the time, DB constraints pop up when I am making changes to my local environment, and the 1% was when there was a fire, and I had to make changes to a prod database in an urgent fashion. I never saw a log line that indicated DB constraints to be a useful protection.<p>And then there is the NoSQL world where obviously life goes on without these constraints and no one is trying to port foreign key constraints to NoSQL.<p>I feel like DB constraints are relic from a bygone era and the requirements that necessitated DB constrains are long gone.<p>Do you agree or am I going crazy?