简体   繁体   English

了解SQL中的用户定义数据类型

[英]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 但是如果我进入

  • 11 alphanumeric characters, it gives error . 11个字母数字字符, 错误
  • 11 numeric characters, it gives error 11个数字字符,显示错误
  • But if I enter the characters in a US Telephone number format ie 123-54-2345, it Works 但是,如果我以美国电话号码格式(即123-54-2345)输入字符, 则可以正常工作
  • Again, if I enter the dashes(hyphens) in some other order ie 1234-5-4544, it again shows error 同样,如果我以其他顺序输入破折号(连字符),即1234-5-4544, 则它再次显示错误

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找到一个名为idUser-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.

相关问题 Windows Azure SQL数据库中用户定义的CLR数据类型 - User-defined CLR data types in Windows Azure SQL Database 从SQL Server删除所有用户定义的类型 - Drop All User-Defined Types from SQL Server 使用用户定义的表类型 - 操作数类型冲突从 excel 到 SQL 的 MVC 300K 行数据表 - MVC 300K row data table from excel into SQL using User-defined Table Types - Operand type clash 用户定义的SQL排序 - user-defined ordering in SQL 是否可以更改 SQL 用户定义的数据类型? - Is it possible to change SQL user-defined data type? 标准 SQL (BigQuery) 中的用户定义数据类型? - User defined data types in standard SQL (BigQuery)? SQL Server创建用户定义的表类型与架构不能正常工作 - SQL Server create User-Defined Table Types with schema not working properly 我们可以为 ORM 使用用户定义的(非标量)SQL 类型吗? - Can we use user-defined (non-scalar) SQL-types for ORM? 在存储过程中向SQL Server添加用户定义表类型的集合 - Adding a collection of User-Defined Table Types in a stored procedure to SQL Server SQL 服务器中用户定义的 function 出现问题 - Problem with user-defined function in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM