认识 XML — 两种长相、一个数据类型、一张校验卡
认识 XML — 两种长相、一个数据类型、一张校验卡
这一章讲什么: 在 SQL Server 里用 XML 之前必须想清楚的三个决定—— 数据写成元素中心还是属性中心、这一列用 XML 数据类型还是普通大字符串、 要不要把一份 XML Schema 绑到列上当校验卡。三个决定一旦做错, 后面六章的查询、拆表、改值、建索引全都要跟着返工。 读完你会拿到全书反复使用的那套词汇和那条主线数据(AdventureWorks 的商品目录)。
1. 先看现象:同样三行数据,两种 XML 长相
打开任何一段 XML,你最先注意到的一定是它的标签——尖括号括起来的名字,比如 <Category>。标签要么成对出现(<Category>…</Category>,中间夹着内容),要么自闭合(<Product … />)。
XML 和网页用的 HTML 长得像,但有一条规则完全不同,书里把它列在第一条:XML 区分大小写,HTML 不区分;XML 里开了的标签必须关,HTML 允许只开不关1。SQL Server 本身默认不区分大小写,而 XML 区分——所以你写错大小写时,报错的是 XML 这一层,不是数据库这一层。
书里给的示范数据,是 AdventureWorks 样例库里的商品目录,关系表长这样(只列关键三行):
| CategoryName | SubcategoryName | Product Name | Number | Price |
|---|---|---|---|---|
| Accessories | Bike Racks | Hitch Rack - 4-Bike | RA-H123 | 120.0000 |
| Accessories | Bike Stands | All-Purpose Bike Stand | ST-1401 | 159.0000 |
| Components | Handlebars | LL Road Handlebars | HB-R504 | 44.5400 |
把这三行变成 XML,至少有两种摆法,这一章的核心就是讲清它们的差别。
2. 顶层全景:三个决定,一条主线例子
决定一:数据摆在哪? 决定二:列用什么类型? 决定三:要不要校验卡?
┌─ 元素中心(值在标签对里) ┌─ XML 数据类型(推荐) ┌─ 不挂 → 无类型(untyped)
└─ 属性中心(值在开标签上) └─ nvarchar(max) 等大字符串 └─ 挂上 XML Schema → 有类型(typed)
│
图说:三个决定彼此独立,但后面每一章的写法都由它们定。本章的主线例子就是上面那三行商品目录。它会在第 3 节走完整条路: 先变成元素中心的 XML,再变成属性中心的 XML,比较两者的体积和表达能力 → 然后用两种工具自动生成这份 XML 的「校验卡」(XML Schema)→ 把校验卡装进 SQL Server 的 XML Schema Collection 对象 → 绑到表列上 → 最后往这个「有类型」列里插一条不合规的数据,看它怎么被拒。
(主线例子里表名、列名、具体数值全部来自原书示例;中间为讲解补的步骤性描述会当场标明。)
3. 核心原理
3.1 两种长相:元素中心 vs 属性中心
解决什么问题: 同样一行数据,值可以摆在两个地方——摆在标签对中间,或摆在开标签里当一个「属性」。摆错地方,后面的生成、拆分、查询写法全变。
怎么做: 拿主线例子的第一行(Bike Racks / Hitch Rack - 4-Bike)看两种摆法。
元素中心(element-centric):每个值各占一对标签,标签可以一层套一层,能表达层级:
<Category>
<CategoryName>Accessories</CategoryName>
<Subcategory>
<SubcategoryName>Bike Racks</SubcategoryName>
<Product>
<Name>Hitch Rack - 4-Bike</Name>
<Number>RA-H123</Number>
<Price>120.0000</Price>
</Product>
</Subcategory>
</Category>属性中心(attribute-centric):值全部塞进开标签,名字写在等号左边、双引号里:
<Category CategoryName="Accessories">
<Product Name="Hitch Rack - 4-Bike" Number="RA-H123" Price="120.0000" />
</Category>为什么有效 / 差在哪: 书里点了两条硬差别。第一,元素中心更占地方——每个值都要付出一对开闭标签的字符,属性中心把值挂进开标签,整份文档明显更短2。第二,元素中心能表达层级——一个元素里可以套子元素、子子元素;属性只有「名字=值」一个平面,没法再往下长。此外还有一条本章埋的伏笔:数据库里的「没有值」(NULL)在元素中心格式里可以用一个特殊属性 xsi:nil="true" 表达,属性中心格式表达不了——这条到第 02 章讲生成 XML 时才展开3。
书里还给这两个词各造了一个岗位名,后面章节会一直用:元素中心的格式叫 element-centric,属性中心的叫 attribute-centric。另外,把 XML 反向拆回行和列的过程,书里叫 shredding(拆分)——「把 XML 转换成行列格式的一个过程」4。这个词是全书 Part I 的关键词,第 04 章整章都在讲它。
顺便交代书的立场:这两种格式没有绝对的好坏,但「数据库这一侧为每种格式准备了专门的功能」, 所以选哪一种要先想清楚数据怎么用。这是作者的实践经验之谈,不是规范里的死规定。
3.2 五个特殊字符:为什么 < 不能想写就写
解决什么问题: XML 用 <、> 定标签、用双引号包属性值。如果数据本身含这些字符,解析器会把数据误认成标签,整份文档就坏了。
怎么做: XML 规定了一套预定义实体(predefined entities):五个特殊字符各有一个替身写法,所有 XML 解析器都必须认。把字符换成替身写法的这个过程,书里叫 entitizing(实体化)5:
| 字符 | 替身写法 | 说明 |
|---|---|---|
" | " | 双引号 |
& | & | 和号(它自己也是替身的开头,所以必须替换) |
' | ' | 撇号 |
< | < | 小于号 |
> | > | 大于号 |
为什么有效: 替身写法本身由普通字符组成(&、l、t、;),不会和标签语法冲突;解析器读完再把替身还原成原字符。所以你在 SSMS 里点开一个 XML 结果,看到的总是还原后的正常字符——书里拿一个含实体的文件双击打开做演示,看到的就是这个效果6。
边界: 只有这五个是「所有解析器必须认」的预定义实体。XML 还有「自定义实体」的玩法(第 04 章的配方 4-1 会用到内部 ENTITY 声明),那是另一套机制,别把两者混为一谈。
3.3 XML 数据类型:从 2000 到 2005,以及它的一串限制
解决什么问题: 早期的 SQL Server 没有专门的 XML 类型。2000 版只能把 XML 塞进 TEXT 甚至 IMAGE 这种「大对象」类型,操作起来书里原话是「一场噩梦」,还得调用 COM 组件的存储过程7。
怎么做: 2005 版给出了真正的 XML 数据类型:XML 以原生格式存储,查询结果在 SSMS 里显示成一个可点击的超链接,点开就是完整的 XML 文档视图8。定义一列只需要在 CREATE TABLE 里写 列名 XML。
为什么有效: XML 类型不只是存字符串——它把文档解析成了一棵结构化的树存进数据库,这为后面所有事(按路径取值、拆分、专门索引)打下了地基。
但代价是一串限制,书里列得很全,这些限制到 2026 年的版本仍然成立9:
| 限制 | 实际含义 |
|---|---|
| 最大 2.1 GB | 单列存不下超大文档 |
| 不能当主键 | 也不能参与排序(ORDER BY)、比较(WHERE)、分组(GROUP BY) |
| 内建标量函数(一次只处理一个值、返回一个值的函数)大多不收它 | 只有 ISNULL、COALESCE、DATALENGTH、CAST、CONVERT 等少数几个例外 |
| 不能出现在链接服务器查询里 | 跨服务器查询碰到 XML 列会报错 |
| 只能建 XML 专用索引 | 普通聚集/非聚集索引想带上它,只能用 INCLUDE 附带 |
什么时候反而不该用 XML 类型: 书里给了一个很实际的理由——当你需要逐字节保留 XML 的原貌时。XML 类型存进去可能改动「不重要的空白字符」,而且不保证属性的排列顺序10。书里举的例子是 msdb 库里的 sysssispackages 表:SSIS 包本身就是 XML,但微软自己用 IMAGE 类型存它。作者的做法是把它先转成 VARBINARY(MAX) 再 CAST 成 XML 来查询11——大字符串类型(nvarchar(max)、varchar(max)、varbinary(max))适合「整包搬运、偶尔整包查询」的场景。注意老类型 IMAGE/NTEXT/TEXT 已被废弃,书里明确说「不要考虑用于任何新实现」12。
3.4 无类型与有类型:XML Schema 是干什么用的
解决什么问题: XML 列建好之后,进来的数据谁来把关?XML 类型自带一道最低门槛——格式良好(well-formed):标签成对、正确嵌套、没有非法字符。但这只保证「语法没坏」,不保证「结构对」。
怎么做: XML 类型有两种形态,书里定义得很干净13:
- 无类型(untyped,默认): 不挂任何校验卡。进来的数据只要格式良好,或是一个不带根元素的「片段(fragment)」,就收。
- 有类型(typed): 把一份 XML Schema 绑到列上。Schema 是一张机器可读的校验卡,写清了「允许哪些标签、按什么顺序、各是什么数据类型、可出现几次」。每一行插入的数据都要先过这张卡。
为什么有效: 校验发生在写入的那一刻——插入的 XML 不合 Schema,SQL Server 直接报错、插入失败,脏数据进不了库14。作者列的绑 Schema 的三个好处:按 Schema 校验数据;基于类型信息的存储和查询优化;查询编译时能更好地利用类型信息15。
边界(这是实践里最常踩的坑): 如果表里已经有数据,这时想把 Schema 绑上去,而存量数据不合 Schema,绑定操作本身就会失败16。所以「先存数据后加校验」这条路走不通——要么先清洗,要么建新列迁数据。
3.5 生成校验卡的两条自动路
解决什么问题: Schema 是机器读的,手写又繁琐又易错。书里作者的立场很鲜明:「手动创建 XML Schema 的时代结束了」——用微软自己的工具,几秒钟就能从一份现成的 XML 生成 Schema17。
怎么做,路线一(Visual Studio): 把 XML 存成 .xml 文件,用 Visual Studio(2008 或更新,或 SQL Server Data Tools)打开;菜单栏会多出一个 XML 菜单,点 Create Schema,生成的 Schema 出现在新标签页,另存为 .xsd 文件即可18。
怎么做,路线二(SSMS,纯 SQL): 在查询后面加一句 FOR XML AUTO, ELEMENTS, XMLSCHEMA('命名空间'), ROOT('Category')。XMLSCHEMA 关键字会让 SQL Server 在查询结果里内联一份 Schema;点开结果、复制 <xsd:schema> 到 </xsd:schema> 那一段,就拿到了19。
两条路的差别: 同一份数据,两条路生成的 Schema 内容不一样——SSMS 路线生成的 Schema 大得多(它会把 SQL Server 的数据类型信息、排序规则都写进去);Visual Studio 生成的更精简。但两条路出来的 Schema 都能用,选哪条看你顺手20。
主线例子走到这里: 我们的三行商品目录,无论走哪条路,生成的校验卡都会写明:Category 下面可以有一个 CategoryName、可以有多个 Subcategory,每个 Subcategory 下面可以有多个 Product,而 Product 的 Price 是数字。——「可出现几次」就是靠 Schema 里 minOccurs/maxOccurs 这样的标注表达的。
3.6 Schema Collection:校验卡在数据库里的「卡套」
解决什么问题: 生成的 Schema 只是一段文本。数据库里要有一个正式对象把它装起来、起个名,之后才能绑到列上。
怎么做: 这个对象叫 XML Schema Collection。创建语法一行:CREATE XML SCHEMA COLLECTION dbo.TypedXML_VisualStudio AS N'…Schema 内容…'21。建好之后,绑到列上的语法是把集合名写在 XML 类型后面的括号里:
CREATE TABLE dbo.TypedXML_VS ( … TypedXMLData XML (TypedXML_VisualStudio) … );
-- 已有表则用:ALTER TABLE … ALTER COLUMN … XML (集合名);
-- 解绑(变回无类型):ALTER COLUMN … XML; —— 注意括号没了为什么有效: 一个集合里可以装多份 Schema(比如同一列要收两种不同格式的报文);列引用集合名,校验规则就挂上了。
边界(三条,条条会咬人):
- 绑定后即锁死: 只要集合绑在任何一个列上,集合本身就不能改、不能删——想动它,先把所有列解绑22。
- 权限门槛高: 创建集合需要服务器级 CONTROL/ALTER ANY DATABASE,或数据库级 ALTER/CONTROL 等一组权限,不是随便谁都能建23。
- 解绑方向不可逆地「丢类型」:
ALTER COLUMN 列名 XML(没有括号)就解绑了,列退回无类型;再想绑回来,存量数据要重新过校验。
3.7 XML 压缩:SQL Server 2022 的新选项
解决什么问题: XML 是出了名的啰嗦——每个值都拖着一对标签,占的磁盘空间比纯数据大得多。
怎么做: SQL Server 2022 起,建表或改表时加一个表级选项:WITH (XML_COMPRESSION = ON)。一开,表里所有 XML 列一起压24。已有的表用 ALTER TABLE … REBUILD WITH (XML_COMPRESSION = ON) 重建一遍即可启用25。
为什么有效: 压缩在页级(page,SQL Server 读写磁盘的基本单位)自动进行,对查询和应用程序完全透明——不需要改任何已有语句26。好处书里列了四条:读盘量变少所以查询更快;存储占用下降;索引和统计信息也更紧凑、维护更省。
怎么查出哪些表开了压缩: 系统视图 sys.partitions 多了两个列:xml_compression(0/1)和 xml_compression_desc('OFF'/'ON'),WHERE xml_compression_desc = 'ON' 一过滤就是全部开启压缩的表27。
边界: 压缩率取决于数据本身的重复程度,书里没给可复现的压缩比数字;而且 2022 之前的版本没有这个功能——作者在正文里特别提醒「XML 索引的压缩到第 7 章才讲」,别把表压缩和索引压缩混为一谈28。
4. 作者的判断与证据
这一章是入门章,作者的判断多为「立场 + 经验」,证据强度分三档:
有书内直接演示支撑的: 元素中心更占字符、两种格式表达能力不同——用同一组数据两份 XML 并列展示2;无类型列会校验格式良好性、有类型列会拒掉不合 Schema 的插入——都有可直接运行的 SQL 演示14;sys.partitions 两个新列——给了查询语句本身27。
属于作者经验之谈、书里明说「我认为」的: 「手动建 Schema 的时代结束了,一律自动生成」17;「别学微软用 IMAGE 存 XML」12;「LOB 类型的最佳用途是保留 XML 原貌」10。这些在真实工程里普遍被认同,但属于判断,不是规范条文。
需要打问号的: XML 压缩的「显著收益」一段,语气来自功能宣传材料(那段文字本身是从 Azure 文档风格改写的「技术概述」),没有给出可核对的压缩比实测数字——读者在自己库上开压缩前,应该用 sp_estimate_data_compression_savings 一类的手段先估一遍(这条是补充,不在书里,来自通用知识)。
5. 边界与局限
- 这一章只教「存进去」,不教「取出来」: 怎么从 XML 里查值、怎么拆成行和列、怎么改、怎么建索引,是第 2–7 章的事。读完本章你还不会写任何一条 XML 查询——这是故意的。
- 元素中心 vs 属性中心只讲了差别,没给决策树: 「什么时候该选哪种」散落在后面各章的配方里(比如 NULL 的表达、索引的用法),本章只立了概念。
- Visual Studio / SSMS 的界面操作会过时: 点哪个菜单、哪一页对话框,随工具版本变化;概念(自动生成 Schema、XMLSCHEMA 关键字)不会过时。
- 样例数据库依赖: 全部示例基于 AdventureWorks(微软官方样例库),不装它跑不起代码——这是全书的通用前提29。
- 2.1 GB 上限与那串限制,是「这个数据库这样设计」的事实,不是 XML 本身的限制: 别的数据库对 XML 的限制完全不同,别把这一章的限制背成 XML 的「自然规律」。
6. 可带走的
本章主线例子,一行走完: 三行商品目录( Accessories / Bike Racks / Hitch Rack - 4-Bike …)→ 两种摆法(元素中心更长、能分层;属性中心更短、只有一层)→ 自动生成 Schema(VS 的 Create Schema 或 SQL 的 XMLSCHEMA)→ 装进 CREATE XML SCHEMA COLLECTION → 列名 XML (集合名) 绑成有类型列 → 插入不合规数据,报错被拒。
- 先选长相再动手: 元素中心(element-centric)值在标签对里、能分层、更占地方;属性中心(attribute-centric)值在开标签上、更紧凑、只有一层——这个选择决定了后面所有章节怎么写。
- XML 区分大小写,SQL Server 默认不区分——报错时先怀疑大小写,这条能省半天时间1。
- 五个字符必须替身化:
"&'<>,替身分别是" & ' < >5。 - 要 XML 的原貌(属性顺序、空白)就用
nvarchar(max)等大字符串,别用 XML 类型10。 - XML 列的硬限制背最疼的三条: 不能当主键、不能排序比较分组、只能建 XML 专用索引9。
- 无类型 = 只查格式良好;有类型 = 再查 Schema,插入即校验1314。
- 表里有存量数据时绑 Schema,存量不合规绑定直接失败——校验卡只能先挂后存16。
- Schema 用手动时代已结束: Visual Studio 点 Create Schema,或查询加
XMLSCHEMA关键字1719。 - Schema Collection 绑在列上就被锁死,想改先解绑所有列22。
- SQL Server 2022 的 XML 压缩是表级选项
WITH (XML_COMPRESSION = ON),对应用透明;用sys.partitions的新列查谁开了2427。
7. 原文地图
| 主题 | 原书章 | 原文位置 |
|---|---|---|
| XML 与 HTML 的差别、大小写 | Chapter 1: Introducing XML | text/05-ch01-chapter-1-introducing-xml.txt:15(搜「predefined elements」) · text/05-ch01-chapter-1-introducing-xml.txt:18(搜「key sensitive」) |
| 元素中心 / 属性中心定义与比较 | 同上 | text/05-ch01-chapter-1-introducing-xml.txt:48(搜「element-centric format」) · text/05-ch01-chapter-1-introducing-xml.txt:109(搜「bigger」) |
| shredding 的定义 | 同上 | text/05-ch01-chapter-1-introducing-xml.txt:48(搜「rows-columns format」) |
| 预定义实体与 entitizing | 同上 | text/05-ch01-chapter-1-introducing-xml.txt:122(搜「predefined entities」) · text/05-ch01-chapter-1-introducing-xml.txt:137(搜「entitizing」) |
| XML 类型的历史与 LOB 之争 | 同上 | text/05-ch01-chapter-1-introducing-xml.txt:153(搜「first introduced」) · text/05-ch01-chapter-1-introducing-xml.txt:176(搜「exact representation」) |
| XML 类型限制清单 | 同上 | text/05-ch01-chapter-1-introducing-xml.txt:197(搜「limitations」) · text/05-ch01-chapter-1-introducing-xml.txt:199(搜「2.1 GB」) |
| 无类型 / 有类型定义 | 同上 | text/05-ch01-chapter-1-introducing-xml.txt:217(搜「two forms」) |
| VS 生成 Schema | 同上 | text/05-ch01-chapter-1-introducing-xml.txt:370(搜「Create Schema」) · text/05-ch01-chapter-1-introducing-xml.txt:380(搜「manually is over」) |
| SSMS 的 XMLSCHEMA 路线 | 同上 | text/05-ch01-chapter-1-introducing-xml.txt:420(搜「XMLSCHEMA directive」) · text/05-ch01-chapter-1-introducing-xml.txt:512(搜「much larger XML schema」) |
| Schema Collection 的创建、权限、锁定 | 同上 | text/05-ch01-chapter-1-introducing-xml.txt:523(搜「schema collection needs to be created」) · text/05-ch01-chapter-1-introducing-xml.txt:556(搜「permissions is required」) · text/05-ch01-chapter-1-introducing-xml.txt:573(搜「bound to one or more columns」) |
| 有类型列的绑定、校验、存量数据 | 同上 | text/05-ch01-chapter-1-introducing-xml.txt:604(搜「straightforward」) · text/05-ch01-chapter-1-introducing-xml.txt:630(搜「raises an error」) · text/05-ch01-chapter-1-introducing-xml.txt:631(搜「already have XML」) |
| XML 压缩选项与版本 | 同上 | text/05-ch01-chapter-1-introducing-xml.txt:646(搜「XML_COMPRESSION = ON」) · text/05-ch01-chapter-1-introducing-xml.txt:665(搜「SQL Server 2022 and newer」) |
| sys.partitions 新列 | 同上 | text/05-ch01-chapter-1-introducing-xml.txt:709(搜「extended with two new columns」) |
| 本章小结 | 同上 | text/05-ch01-chapter-1-introducing-xml.txt:743(搜「first introduced XML functionality」) |
Footnotes
-
出处:「Chapter 1: Introducing XML」第 15 段(
text/05-ch01-chapter-1-introducing-xml.txt:15,搜「predefined elements」)与第 18 段(text/05-ch01-chapter-1-introducing-xml.txt:18,搜「key sensitive」)。原文:「HTML contains predefined elements (tags), while XML's elements and attributes are not predefined」;「XML is key sensitive, while HTML is not」(书中 key sensitive 即 case-sensitive,大小写敏感)。大小写警告见第 24 段(text/05-ch01-chapter-1-introducing-xml.txt:24,搜「case-sensitive」):SQL Server 默认不区分大小写,而 XML 及所有 XPath 函数、节点测试都区分,且一律小写。 ↩ ↩2 -
出处:「Chapter 1: Introducing XML」第 107–110 段(
text/05-ch01-chapter-1-introducing-xml.txt:109,搜「bigger」)。原文:「Element-centric XML is bigger (in number of characters) than attribute-centric XML」「Element-centric XML supports element hierarchy」。 ↩ ↩2 -
出处:「Chapter 1: Introducing XML」第 113 段(
text/05-ch01-chapter-1-introducing-xml.txt:113,搜「xsi:nil」)。原文:element-centric XML「can represent SQL NULLs with the xsi:nil attribute (xsi:nil will be covered in Chapter 2, Recipe 2-5)」。 ↩ -
出处:「Chapter 1: Introducing XML」第 48 段(
text/05-ch01-chapter-1-introducing-xml.txt:48,搜「rows-columns format」)。原文:「shredding is a process to convert XML into a rows-columns format」。 ↩ -
出处:「Chapter 1: Introducing XML」第 122 段(
text/05-ch01-chapter-1-introducing-xml.txt:122,搜「predefined entities」)与第 137 段(text/05-ch01-chapter-1-introducing-xml.txt:137,搜「entitizing」)。原文:预定义实体是「special character sequences, known as predefined entities, which all XML parsers must honor」;「The process when predefined entities are replaced with entity references is known as entitizing」。 ↩ ↩2 -
出处:「Chapter 1: Introducing XML」第 145–148 段(
text/05-ch01-chapter-1-introducing-xml.txt:146,搜「double-click on the file」)。原文演示:把含实体的 XML 存成文件后双击打开,「the entity references will display as normal characters」。 ↩ -
出处:「Chapter 1: Introducing XML」第 153 段(
text/05-ch01-chapter-1-introducing-xml.txt:153,搜「first introduced」)与第 157–160 段(text/05-ch01-chapter-1-introducing-xml.txt:158,搜「nightmare」)。原文:2000 版建议用 TEXT、IMAGE 存 XML,「Many found dealing with these data types to be a nightmare」。 ↩ -
出处:「Chapter 1: Introducing XML」第 161 段(
text/05-ch01-chapter-1-introducing-xml.txt:161,搜「release of SQL Server 2005」)。原文:2005 版提供了 XML data type,「the XML data can be stored in its native format」,结果在 SSMS 里返回可点击的超链接。 ↩ -
出处:「Chapter 1: Introducing XML」第 197–215 段(
text/05-ch01-chapter-1-introducing-xml.txt:197,搜「limitations」)。清单原文:「The storage is limited to 2.1 GB」「It cannot be used as a table primary key」「It cannot be sorted by the ORDER BY clause」「It cannot be compared in the WHERE clause」「It cannot be used in the GROUP BY clause」;只有 ISNULL、COALESCE、DATALENGTH、CAST、TRY_CAST、CONVERT、CHOOSE、IIF 等少数函数收 XML 参数;XML 列「can only be indexed via an XML index」。 ↩ ↩2 -
出处:「Chapter 1: Introducing XML」第 175–178 段(
text/05-ch01-chapter-1-introducing-xml.txt:176,搜「exact representation」)。原文:「The best argument for using a LOB data type to store XML is when you need to store the exact representation of the XML, since the XML data type will store the content you provide, but it might alter insignificant whitespace, and the order of attributes is not guaranteed」。 ↩ ↩2 ↩3 -
出处:「Chapter 1: Introducing XML」第 178–184 段(
text/05-ch01-chapter-1-introducing-xml.txt:178,搜「sysssispackages」)。SSIS 包代码本身就是 XML,却用 IMAGE 类型存;作者的查询先把 packagedata 转成 VARBINARY(MAX) 再 CAST 成 XML。 ↩ -
出处:「Chapter 1: Introducing XML」第 191–195 段(
text/05-ch01-chapter-1-introducing-xml.txt:193,搜「because they are deprecated」)。原文:IMAGE、NTEXT、TEXT「not be considered for any column implementation」,微软 2008 年已宣布废弃。 ↩ ↩2 -
出处:「Chapter 1: Introducing XML」第 217–221 段(
text/05-ch01-chapter-1-introducing-xml.txt:217,搜「two forms」)。原文:「Untyped XML (the default) is an XML data type instance that does not have an associated XML schema」「Typed XML is an XML data type instance with an XML schema bound to it」。 ↩ ↩2 -
出处:「Chapter 1: Introducing XML」第 317–325 段(
text/05-ch01-chapter-1-introducing-xml.txt:319,搜「the object created is untyped XML」)与第 628–630 段(text/05-ch01-chapter-1-introducing-xml.txt:629,搜「validates each insert」)。无类型列「the parser will verify incoming data to ensure that the XML data is well-formed」;有类型列「The XML schema validates each insert; when validation fails, SQL Server raises an error, and the insert fails」。 ↩ ↩2 ↩3 -
出处:「Chapter 1: Introducing XML」第 621–626 段(
text/05-ch01-chapter-1-introducing-xml.txt:621,搜「benefit」)。原文列出「Ability to validate XML data per the XML schema」「Take advantage of storage and query optimizations based on type information」「Better advantage of type information within a compilation of the queries」。 ↩ -
出处:「Chapter 1: Introducing XML」第 631–634 段(
text/05-ch01-chapter-1-introducing-xml.txt:631,搜「already have XML」)。原文:「If a table with the XML column has existing XML, then binding an XML schema to the table fails when the XML does not conform to the XML schema」。 ↩ ↩2 -
出处:「Chapter 1: Introducing XML」第 380 段(
text/05-ch01-chapter-1-introducing-xml.txt:380,搜「manually is over」)。原文:「The era of creating your XML schemas manually is over, from my point of view. There are many ways to generate an XML schema automatically」。 ↩ ↩2 ↩3 -
出处:「Chapter 1: Introducing XML」第 346–375 段(
text/05-ch01-chapter-1-introducing-xml.txt:370,搜「Create Schema」)。步骤:VS 打开 .xml 文件后菜单出现 XML 选项,点「Create Schema」,Schema 生成在新标签页,可另存为 .xsd。 ↩ -
出处:「Chapter 1: Introducing XML」第 419–428 段(
text/05-ch01-chapter-1-introducing-xml.txt:420,搜「XMLSCHEMA directive」)与第 446–458 段(text/05-ch01-chapter-1-introducing-xml.txt:456,搜「opening xsd:schema tag」)。原文:在 FOR XML 子句中加 XMLSCHEMA 关键字生成 inline XSD;提取时「Copy from the opening xsd:schema tag to the closing </xsd:schema> tag」。 ↩ ↩2 -
出处:「Chapter 1: Introducing XML」第 510–514 段(
text/05-ch01-chapter-1-introducing-xml.txt:512,搜「much larger XML schema」)。原文:「Creating an XML Schema via SQL Server tends to create a much larger XML schema. However, both variants deliver an XML schema that can be used with the XML Schema Collection」。 ↩ -
出处:「Chapter 1: Introducing XML」第 522–537 段(
text/05-ch01-chapter-1-introducing-xml.txt:523,搜「schema collection needs to be created」)。原文:「To make an XML schema eligible to be bound to a table's column, XML variable, or XML stored procedure's parameter, the XML schema collection needs to be created」;创建语法见第 528 段(text/05-ch01-chapter-1-introducing-xml.txt:528,搜「CREATE XML SCHEMA COLLECTION」)。 ↩ -
出处:「Chapter 1: Introducing XML」第 573–575 段(
text/05-ch01-chapter-1-introducing-xml.txt:573,搜「bound to one or more columns」)。原文:「When the XML schema collection is bound to one or more columns, no changes can be applied to the XML schema collection. To modify or drop the XML schema collection, you need to unbind it from the column(s) first」。 ↩ ↩2 -
出处:「Chapter 1: Introducing XML」第 556–566 段(
text/05-ch01-chapter-1-introducing-xml.txt:556,搜「permissions is required」)。创建 XML SCHEMA COLLECTION 需要 CONTROL(server)、ALTER ANY DATABASE(server)、ALTER(database)、CONTROL(database) 或 ALTER ANY SCHEMA + CREATE XML SCHEMA COLLECTION(database) 之一。 ↩ -
出处:「Chapter 1: Introducing XML」第 646–648 段(
text/05-ch01-chapter-1-introducing-xml.txt:646,搜「XML_COMPRESSION = ON」)与第 665 段(text/05-ch01-chapter-1-introducing-xml.txt:665,搜「SQL Server 2022 and newer」)。原文:「To enable XML compression, use the XML_COMPRESSION = ON option when creating or altering a table. This setting applies to all XML columns within the specified table」;版本注释:「SQL Server 2022 and newer versions support the XML compression option at the table level」。 ↩ ↩2 -
出处:「Chapter 1: Introducing XML」第 673–676 段(
text/05-ch01-chapter-1-introducing-xml.txt:674,搜「rebuilt using the ALTER TABLE」)。原文:已有表需「rebuilt using the ALTER TABLE statement with the XML_COMPRESSION = ON option」。 ↩ -
出处:「Chapter 1: Introducing XML」第 253–256 段(
text/05-ch01-chapter-1-introducing-xml.txt:253,搜「fully transparent」)。原文:「XML compression is fully transparent to users. Existing queries and applications require no changes, as the SQL engine automatically manages compression and decompression behind the scenes」。 ↩ -
出处:「Chapter 1: Introducing XML」第 707–722 段(
text/05-ch01-chapter-1-introducing-xml.txt:709,搜「extended with two new columns」)。原文:「the system view sys.partitions has been extended with two new columns」:xml_compression(bit)与 xml_compression_desc(varchar(3));过滤示例用「WHERE xml_compression_desc = 'ON'」。 ↩ ↩2 ↩3 -
出处:「Chapter 1: Introducing XML」第 753–755 段(
text/05-ch01-chapter-1-introducing-xml.txt:754,搜「XML index compression」)。本章小结:「SQL Server 2022 introduces support for XML compression, offering improved storage efficiency for XML data in tables and indexes (XML index compression will be covered later in Chapter 7)」。 ↩ -
出处:「Chapter 1: Introducing XML」第 32–36 段(
text/05-ch01-chapter-1-introducing-xml.txt:36,搜「AdventureWorks sample database」)。原文:「All code samples for Part I in this book utilize the SQL Server AdventureWorks sample database」。 ↩