[英]Understanding user-defined data types in SQL
i am trying to understand the architecture of PUBS Database sample by Microsoft In there, I am looking at au_id
Column, who has user-defined datatype id:varchar(11) . 我试图了解Microsoft提供的PUBS数据库示例的体系结构。在那里,我正在查看au_id
列,该列具有用户定义的数据类型id:varchar(11) 。
So, if I understand clearly, varchar(11) means it allows to enter 11 characters in the cell. 因此,据我所知,varchar(11)表示它允许在单元格中输入11个字符。 But if I enter 但是如果我进入
Why does this happen ? 为什么会这样? Do they have some method to validate this entry. 他们是否有某种方法可以验证此条目。 I can only find a user-defined datatype called id
in the User-Defined Data Type Folder
我只能在“ User-Defined Data Type Folder
找到一个名为id
的User-Defined Data Type Folder
Thank you in advance. 先感谢您。
Okay, just found the script that will create the pubs database. 好的,刚刚找到了将创建pubs数据库的脚本。
The au_id
column on authors is defined as: 关于作者的au_id
列定义为:
CREATE TABLE authors
(
au_id id
CHECK (au_id like '[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]')
CONSTRAINT UPKCL_auidind PRIMARY KEY CLUSTERED,
/* More columns */
It's the CHECK constraint that's rejecting your invalid values, rather than anything connected with the user-defined type. 是CHECK约束拒绝了无效值,而不是与用户定义类型有关的任何值。 If you examine the error messages, it probably mentions that it's a CHECK
constraint that's failing. 如果检查错误消息,它可能会提到这是CHECK
约束失败了。
(BTW - I'd assumed that this was SSN format, not telephone numbers - anyone confirm?) (顺便说一句-我以为这是SSN格式,而不是电话号码-有人确认吗?)
User defined types in SQL Server (other than table types) don't offer much value - all they really do is associated a shorthand name for a built-in type with all scale/precision/length options fixed. SQL Server中的用户定义类型(表类型除外)提供的价值不高-它们实际上所做的只是将内置类型的速记名称与固定的所有比例/精度/长度选项相关联。
They would be tremendously useful if the system would let you set up strict types - such that two values of the same underlying type, but with different type names, are not comparable/assignable - you'd get far better warnings/errors rather than queries proceding with mis-aligned joins, for example. 如果系统允许您设置严格的类型(例如,具有相同基础类型但具有不同类型名称的两个值不可比较/可分配),那么它们将非常有用。您将得到更好的警告/错误而不是查询例如,使用未对齐的连接进行处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.