search
HomeDatabaseMysql TutorialSQL Server2005的XML数据类型之基础篇

一、 引言 如今,在SQL Server 2005中,XML成为第一流的 数据 类型 。借助于基于XML模式的强 类型 化支持和基于服务器端的XML 数据 校验功能,现在,开发者可以对存储的XML文档进行轻松地远程修改。 作为 数据 库开发者,许多人都必须大量地涉及XML。 如今,

  一、 引言

  如今,在SQL Server 2005中,XML成为第一流的数据类型。借助于基于XML模式的强类型化支持和基于服务器端的XML数据校验功能,现在,开发者可以对存储的XML文档进行轻松地远程修改。

  作为数据库开发者,许多人都必须大量地涉及XML。

  如今,在SQL Server 2005中,你能以一种新的数据类型的形式把XML存储在数据库中。

  事实上,在SQL Server 2000中就已经包括了一些XML特征。其中,最关键的特征是使用FOR XML语句以XML形式返回结果。SQL Server 2005的功能则明显不同。在SQL Server 2005中,XML是一种真正的数据类型;这意味着,你可以使用XML作为表和视图中的列,XML可以用于T-SQL语句中或作为存储过程的参数。现在,你可以直接在数据库中存储、查询和管理XML文件。

  更重要的是,现在你还能规定你的XML必须遵从的模式。

  在SQL Server 2005中,除了提供机制以校验你的数据库中的XML类型之外,它还允许你描述要被存储的复杂数据类型并且提供一个引擎来强制施加这些规则。

  二、 使用XML数据类型

  其实,XML数据类型与SQL Server中的其它数据类型并不存在根本的区别。你可以把它用在使用任何普通SQL数据类型的地方。例如,下列语句创建一个XML变量并用一个XML填充它:

DECLARE @doc xml
SELECT @doc = '<Team name="Braves" />'

  另外,你还可以使用一个查询和SQL Server的FOR XML语法来填充一个XML变量:

SELECT @doc =
(SELECT * FROM Person.Contact FOR XML AUTO)

  XML数据类型不仅可以作为变量使用,也可以应用于表列中。你还能分配缺省值并且支持NOT NULL约束:

CREATE TABLE Team
(
TeamID int identity not null,
TeamDoc xml DEFAULT '<Team />' NOT NULL
)

  注意:SQL Server 2005的XML功能与SQL Server 2000中具有明显的不同。

  把XML数据插入到表格中只需要用字符串形式的XML指定即可。

  下列示例插入一组记录:

INSERT INTO Team (TeamDoc)
VALUES ('
<Team name="Braves">
<Players>
<Pitcher name="John Smoltz"
role="Closer"/>
</Players>
</Team>');
INSERT INTO Team (TeamDoc)
VALUES ('
<Team name="Red Sox">
<Players>
<Pitcher name="Petro Martinez"
role="Starter"/>
</Players>
</Team>');

  当在SQL Server 2005中创建XML的实例时,唯一的转换是从一个字符串转换成一个XML类型。同样,沿着相反的方向,你只可以把XML类型转换成一个字符串类型。在text和ntext类型之间转换是不允许的。

  三、 XML数据类型的限制

  尽管在SQL Server 2005中XML数据类型就象许多其它数据类型一样对待,但是还存在一些如何使用它的具体限制。这些限制是:

  ? XML类型不能转换成text或ntext数据类型

  ? 除了string类型,没有其它数据类型能够转换成XML。

  ? XML列不能应用于GROUP BY语句中。

  ? 分布式局部(partitioned)视图不能包含XML数据类型

  ? sql_variant实例的使用不能把XML作为一种子类型

  ? XML列不能成为主键或外键的一部分。

  ? XML列不能指定为唯一的。

  ? COLLATE子句不能被使用在XML列上。

  ? XML列不能加入到规则中。

  ? 唯一可应用于XML列的内置标量函数是ISNULL和COALESCE。没有任何其它内置标量函数支持使用XML类型

  ? 表中最多只能拥有32个XML列。

  ? 具有XML列的表不能有一个超过15列的主键。

  ? 具有XML列的表不能有一个timestamp数据类型作为它们的主键的一部分。

  ? 存储在数据库中的XML仅支持128级的层次。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL String Types: Storage, Performance, and Best PracticesMySQL String Types: Storage, Performance, and Best PracticesMay 10, 2025 am 12:02 AM

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

Understanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreUnderstanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreMay 10, 2025 am 12:02 AM

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

What are the String Data Types in MySQL?What are the String Data Types in MySQL?May 10, 2025 am 12:01 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft