数据库约束查找的约束

Constraints exist as a way to enforce or document rules within the database. How do they affect performance, and what are the benefits of using them?

约束是在数据库中强制执行或记录规则的一种方式。 它们如何影响性能?使用它们有什么好处?

介绍 (Introduction)

There are a variety of constraints that can be implemented in a SQL Server database that can provide a variety of new functionality. Some are so familiar that we forget their presence, such as NULL constraints, while others may not be used as often, such as check constraints or default constraints.

在SQL Server数据库中可以实现各种约束,这些约束可以提供各种新功能。 有些非常熟悉,以至于我们忘记了它们的存在,例如NULL约束,而有些则可能不那么常用,例如检查约束或默认约束。

This is an opportunity to dive into each type of constraint, and after introducing their purpose and usage, dive into their storage, performance, and notes about their optimal use, as well as pitfalls. Please note that all demos were created and tested in SQL Server 2016 RC1.

这是深入研究每种约束类型的机会,并在介绍了它们的用途和用途之后,深入研究了它们的存储,性能以及关于它们的最佳使用以及陷阱的注意事项。 请注意,所有演示均已在SQL Server 2016 RC1中创建和测试。

目的 (Purpose)

Constraints may be used for a variety of purposes. The following is a short list of what I consider the most common reasons that constraints are created:

约束可以用于多种目的。 以下是我认为创建约束的最常见原因的简短列表:

  • Prevent bad data from being entered into tables of interest. 防止将不良数据输入到感兴趣的表中。
  • Enforce business logic at the database-level. 在数据库级别实施业务逻辑。
  • Documentation of important database rules. 重要数据库规则的文档。
  • Enforce relational integrity between any number of tables. 增强任意数量表之间的关系完整性。
  • Improve database performance. 提高数据库性能。
  • Enforce uniqueness. 增强唯一性。

There are others, of course, but the reasons for using constraints are quite varied, and can be a helpful tool for database administrators and developers alike. Typically, it is recommended to use constraints over more complex, procedural logic-enforcement, such as triggers, rules, stored procedures, or jobs.

当然,还有其他原因,但是使用约束的原因千差万别,对于数据库管理员和开发人员而言,它都是有用的工具。 通常,建议对更复杂的过程逻辑执行使用约束,例如触发器,规则,存储过程或作业。

Let’s review one-at-a-time each type of constraint and some examples of their usage.

让我们一次查看每种约束类型及其用法的一些示例。

非空约束 (NOT NULL constraints)

This is so common that we may forget that a column that does not allow NULLs is technically in possession of a NOT NULL constraint. Its function is straightforward: a column with this constraint will not allow a NULL to be entered, and will throw an error when anyone attempts to enter a NULL into a column that doesn’t allow NULLs.

这是如此常见,以至于我们可能忘记了不允许NULL的列在技术上拥有NOT NULL约束。 它的功能很简单:具有此约束的列将不允许输入NULL,并且当任何人试图在不允许NULL的列中输入NULL时都会抛出错误。

NULL may be defined at the time a table is defined, such as like this:

可以在定义表时定义NULL,例如:


CREATE TABLE dbo.NullDemo
(   Null_Id INT NOT NULL,Null_Data VARCHAR(50) NULL,Not_Null_Data VARCHAR(50) NOT NULL);

The syntax is simple, and in this table, Null_Id and Not_Null_Data will not allow any NULLs to be entered into either. Null_Data, on the other hand, will allow NULL, in addition to standard string values. When creating a table, you can choose to not specify NULL or NOT NULL, and if you do, the column will default to being NULLable. Since NOT NULL is an explicit constraint, the default setting for any column is to allow NULLs.

语法很简单,在此表中, Null_IdNot_Null_Data不允许将任何NULL输入其中一个。 另一方面, Null_Data除了标准字符串值外,还将允许NULL。 创建表时,可以选择不指定NULL或NOT NULL,如果这样做,则该列将默认为NULLable。 由于NOT NULL是显式约束,因此任何列的默认设置是允许NULL。

Using this table, let’s insert a few rows into it:

使用此表,让我们在其中插入几行:


INSERT INTO dbo.NullDemo(Null_Id, Null_Data, Not_Null_Data)
SELECT1, 'Stegosaurus', 'Spiky';
INSERT INTO dbo.NullDemo(Null_Id, Null_Data, Not_Null_Data)
SELECT2, 'Tyrannosaurus', 'Huge';

Both of these inserts succeed without incident, adding two dinosaurs into our demo table.

这两个插入都成功了,没有发生意外,将两个恐龙添加到了我们的演示表中。


INSERT INTO dbo.NullDemo(Null_Id, Null_Data, Not_Null_Data)
SELECT3, NULL, 'Long Neck';

This also works without a problem. Inserting a NULL is OK since we explicitly defined Null_Data to allow NULLs.

这也没有问题。 插入NULL是可以的,因为我们明确定义了Null_Data以允许NULL。


INSERT INTO dbo.NullDemo(Null_Id, Null_Data, Not_Null_Data)
SELECT4, 'Pterodactylus ', NULL;

Our poor dino-flyer never makes it into the database as we tried to insert a NULL into the last column, Not_Null_Data, which we applied the NOT NULL constraint to. The result is the expected error:

当我们试图在最后一列Not_Null_Data中插入NULL时,我们可怜的dino-flyer从未进入数据库,因为我们将NOT NULL约束应用于了该列。 结果是预期的错误:

Msg 515, Level 16, State 2, Line 27
Cannot insert the value NULL into column ‘Not_Null_Data’, table ‘AdventureWorks2014.dbo.NullDemo’; column does not allow nulls. INSERT fails.
The statement has been terminated.

信息515,第16级,州2,第27行
无法将值NULL插入表'AdventureWorks2014.dbo.NullDemo'的'Not_Null_Data'列中; 列不允许为空。 插入失败。
该语句已终止。

NOT NULL is exceptionally useful when we have a table with columns that are critical to the definition of that data structure. For example, an account without a name would be potentially meaningless, whereas an employee with no start date would cause us to scratch our heads and ask why no one entered it in the first place.

当我们有一个表的列对于该数据结构的定义至关重要时,NOT NULL异常有用。 例如,没有名称的帐户可能毫无意义,而没有开始日期的员工会导致我们挠头,问为什么没有人首先输入。

NOT NULL corrects mistakes by the user as well as mistakes by a software or database developer. If a column is required, then we want to ensure that it is always entered, no matter what. The application should tell a user that they need to enter a required field, returning a friendly message if they forget. In the event that the application somehow allows a NULL where one does not belong, then an error would be returned that the developers could easily troubleshoot and fix. This scenario is preferable to allowing data to be created with critical missing elements.

NOT NULL纠正用户的错误以及软件或数据库开发人员的错误。 如果需要一列,那么我们要确保始终输入该列,无论如何。 应用程序应告知用户他们需要输入必填字段,如果忘记了,则会返回友好的消息。 如果应用程序以某种方式允许一个不属于其中的NULL,则将返回错误,开发人员可以轻松地进行故障排除和修复。 此方案比允许使用严重缺失的元素创建数据更好。

There are potentially dangerous or inappropriate uses for NOT NULL. While we tend to prefer not having NULLs to deal with in our code, forcing arbitrary values into our data can be dangerous. A NOT NULL string that defaults to a blank is relatively harmless, but what about a NOT NULL date or a NOT NULL integer? How many times have we run into a date column that was peppered with “1/1/1900” within its data? Or the integer that uses “-1” to fill in the blanks? This data can become confusing as the columns appear to have valid values, when in fact they contain dummy data. Special application or database code needs to check for these values and act appropriately when they exist. In addition, what happen if a user deliberately enters a value that matches a dummy value? Will the application disallow this? Will the database consider their entry irrelevant and the same as taking some default instead? What if your great grandfather was actually born on 1/1/1900? As of the writing of this article, there are people older than 116 years old and have birthdays that fall around this oft-used dummy date. Improbable — sure, but if I was 116 years old and got an error while signing up for Snapchat, I’d be pretty pissed off

数据库约束查找的约束_数据库约束的好处,成本和文档相关推荐

  1. mysql 查找相似数据_数据库存储引擎大揭秘,不看不知道这里面的骚操作可真多!...

    吊打各种树这篇文章 带大家学习一遍数据结构中的各种树,对数据结构还不够熟悉的同学,那篇文章可以作为基础入门,我画了很多图理解起来不困难,建议回头先学习下那篇文章,更容易理解本文要讲的内容. 文章里有提 ...

  2. 数据库与python的关系_数据库 引用关系

    <深入解析sas:数据处理.分析优化与商业应用>一2.4 访问关系型数据库系统中的数据 本节书摘来自华章出版社<深入解析sas:数据处理.分析优化与商业应用>一书中的第2章,第 ...

  3. 数据库表的软硬关联_数据库软删除和硬删除

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

  4. mysql 数据库连表查询语句_数据库连表查询sql语句

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

  5. java访问数据库的中间件有哪些_数据库中间件是什么?

    面向数据库的中间件是促进与数据库通信的中间件,无论是来自应用程序还是数据库之间. 开发人员通常使用面向数据库的中间件作为从本地或远程数据库提取信息的机制. 例如,为了从Oracle数据库提取信息,开发 ...

  6. 连接mysql数据库的三个接口_数据库的三种接口

    数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后,数据管理不再仅仅是存储和管理数据,而转变成用户所需要 ...

  7. 数据库实训心得体会_数据库实习个人总结

      数据库实训心得体会篇一一个月的数据库实训就转眼间就上完了,期间讲解了一个学生管理系统,最后还做了一个小的数据库链接作业.现在就说说关于vb链接的数据库的一些方法. 首先说数据库,简单的说就是建表格 ...

  8. 数据库新增幂等操作_数据库幂等性

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

  9. access 导入 txt sql语句_[内附完整源码和文档] 基于C#和Access的智能聊天机器人

    一.软件说明 1.1 功能说明 一个可以自动回复的聊天机器人. 1.2 解决什么样的实际问题 用于娱乐,解闷. 1.3 性能说明 软件还存在一定的BUG,有待改进. 1.4 程序类型说明 娱乐性应用程 ...

最新文章

  1. SpringBoot集成Swagger-Bootstrap-UI,页面更清爽!
  2. C++代码片段(一)萃取函数返回值类型,参数类型,参数个数
  3. ASP.NET三层架构之不确定查询参数个数的查询
  4. 下一代微服务(service Mesh)
  5. Java 14:查看更新的switch语句
  6. MS Script Control的 COM
  7. 中秋节PSD分层模板|电商营销借势促销,快快收藏!
  8. 源码装置vsftpd
  9. 《隋唐演义》二:竞争对手的实力在不断增强
  10. java 使用的钩子_Java 钩子程序
  11. std::binary_serach, std::upper_bound以及std::lower_bound
  12. c++ cv转化灰度图_OpenCV C++如何使RGB图像变为灰度图像
  13. VisionPro 工具
  14. 怎么样防止服务器被入侵
  15. 分布式全局唯一ID生成算法(改进的雪花算法——解决时钟回拨问题)
  16. 在c语言中while与do-while,C语言中while /do while语句用法
  17. 人工智能工程师学习路线/自然语言处理算法工程师
  18. mysql快速导出数据(带列名)
  19. 2022育婴员(五级)试题及答案
  20. matlab 离散控制系统仿真,实验二-基于Matlab的离散控制系统仿真.doc

热门文章

  1. C语言字符排版,论坛编程大赛:字符行排版
  2. python学习中符号报错的一点总结
  3. Azkaban任务调度工具简述
  4. tensorflow入门教程(二十二)使用slim对图像识别与检测(下)
  5. 【Android】获取APP里面的图片素材
  6. PHP一维数组和字符串相互转换
  7. 服务是一种产品,是一种符号,更是一种责任和爱
  8. 字符串---美丽的图形
  9. 最小字典序列--选靓号
  10. 云职教课堂计算机文化基础,智慧职教mooc学院计算机文化基础答案