XML スキーマ コレクションに対する権限の許可

適用対象:SQL ServerAzure SQL DatabaseAzure SQL Managed Instance

XML スキーマ コレクションを作成する権限、および XML スキーマ コレクション オブジェクトに対する権限を許可できます。

XML スキーマ コレクションを作成する権限の許可

XML スキーマ コレクションを作成するには、次の権限が必要です。

  • プリンシパルは、データベースレベルの CREATE XML SCHEMA COLLECTION 権限を必要とします。

  • XML スキーマ コレクションはリレーショナル スキーマ スコープなので、プリンシパルはリレーショナル スキーマに対する ALTER 権限も必要とします。

次の権限があると、プリンシパルはサーバー上のデータベース内のリレーショナル スキーマに XML スキーマ コレクションを作成できます。

  • サーバーに対する CONTROL 権限

  • サーバーに対する ALTER ANY DATABASE 権限

  • データベースに対する ALTER 権限

  • データベースに対する CONTROL 権限

  • データベースに対する ALTER ANY SCHEMA 権限と CREATE XML SCHEMA COLLECTION 権限

  • リレーショナル スキーマに対する ALTER 権限または CONTROL 権限と、データベースに対する CREATE XML SCHEMA COLLECTION 権限

上に示した最後の方法を次の例で使用します。

リレーショナル スキーマの所有者は、そのスキーマで作成される XML スキーマ コレクションの所有者になります。 その結果、この所有者には XML スキーマ コレクションに対するフル コントロール権限が許可されます。 したがって、この所有者は XML スキーマ コレクションの変更も、xml 列の型指定も、XML スキーマ コレクションの削除も行えます。

XML スキーマ コレクション オブジェクトの権限の許可

XML スキーマ コレクションに対して許可できる権限は次のとおりです。

  • ALTER 権限。ALTER XML SCHEMA COLLECTION ステートメントを使用して既存の XML スキーマ コレクションのコンテンツを変更する場合に必要です。

  • CONTROL 権限。許可されると、ユーザーは XML スキーマ コレクションに任意の操作を実行できるようになります。

  • TAKE OWNERSHIP 権限。XML スキーマ コレクションの所有者権を特定のプリンシパルから別のプリンシパルに転送するために必要です。

  • REFERENCES 権限。許可されたプリンシパルは、XML スキーマ コレクションを使用してテーブル、ビュー、およびパラメーター内の xml 型の列に対して型指定や制約を行えます。 また、REFERENCES 権限は、ある XML スキーマ コレクションで別の XML スキーマ コレクションを参照する場合にも必要です。

  • VIEW DEFINITION 権限。このコレクションに対する ALTER 権限、REFERENCES 権限、または CONTROL 権限のいずれかがあれば、XML_SCHEMA_NAMESPACE またはカタログ ビューのどちらかを使用して XML スキーマ コレクションの内容に対してクエリ実行できるようになります。

  • EXECUTE 権限。 xml 型の列、変数、およびパラメーターの型指定または制約を定義している XML スキーマ コレクションに対して、プリンシパルが挿入または更新した値を検証するために必要です。 また、これらの列や変数に格納されている XML に対してクエリを実行する場合にも必要な権限です。

次の例に示すシナリオでは、XML スキーマ権限のしくみを説明します。 各例では、必要なテスト データベース、リレーショナル スキーマ、およびログインを作成します。 それらのログインには、必要な XML スキーマ コレクション権限が許可されています。 最後に必要なクリーンアップを行います。

A。 XML スキーマ コレクションを作成する権限の許可

次の例では、権限を許可することにより、プリンシパルが XML スキーマ コレクションを作成できるようにする方法を説明しています。 この例では、サンプル データベースとテスト ユーザー TestLogin1を作成します。 TestLogin1 に、リレーショナル スキーマに対する ALTER 権限と、データベースに対する CREATE XML SCHEMA COLLECTION 権限を許可します。 これらの権限を使用して、 TestLogin1 はサンプルの XML スキーマ コレクションを正常に作成できます。

SETUSER;
GO
USE master;
GO
CREATE LOGIN TestLogin1 WITH password='SQLSvrPwd1';
GO
CREATE DATABASE SampleDBForSchemaPermissions;
GO
USE SampleDBForSchemaPermissions;
GO
CREATE USER TestLogin1;
GO
-- User must have ALTER permission on the relational schema in the database.
GRANT ALTER ON SCHEMA::dbo TO TestLogin1;
GO
-- User also must have permission to create XML schema collections in the database.
GRANT CREATE XML SCHEMA COLLECTION
TO TestLogin1;
GO
-- Execute CREATE XML SCHEMA COLLECTION.
SETUSER 'TestLogin1';
GO
CREATE XML SCHEMA COLLECTION myTestSchemaCollection AS '<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema targetNamespace="https://schemas.adventure-works.com/Additional/ContactInfo"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xsd:element name="AdditionalContactInfo" >
  <xsd:complexType mixed="true" >
    <xsd:sequence>
      <xsd:any processContents="strict"
               namespace="https://schemas.adventure-works.com/Contact/Record
                          https://schemas.adventure-works.com/AdditionalContactTypes"
               minOccurs="0" maxOccurs="unbounded" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>
<xsd:element name="root" type="xsd:byte"/>
</xsd:schema>';
GO
-- Final cleanup
SETUSER;
GO
USE master;
GO
DROP DATABASE SampleDBForSchemaPermissions;
GO
DROP LOGIN TestLogin1;
GO

B. 既存の XML スキーマ コレクションを使用する権限の許可

次の例では、XML スキーマ コレクションの権限モデルを詳しく説明しています。 XML スキーマ コレクションの作成と使用には、各種の権限が必要であることがわかります。

この例では、テスト データベースとログイン TestLogin1を作成します。 TestLogin1 で、データベースに XML スキーマ コレクションを作成します。 次に、テーブルを作成し、XML スキーマ コレクションを使用して、型指定された xml 列を作成します。 その後、データを挿入し、クエリを実行します。 次のコードに示すように、これらのどの手順でも正しいスキーマ権限を必要とします。

SETUSER
GO
USE master;
GO
CREATE LOGIN TestLogin1 WITH password='SQLSvrPwd1';
GO
CREATE DATABASE SampleDBForSchemaPermissions;
GO
USE SampleDBForSchemaPermissions;
GO
CREATE USER TestLogin1;
GO
-- Grant permission to the user.
SETUSER;
GO
-- User must have ALTER permission on the relational schema in the database.
GRANT ALTER ON SCHEMA::dbo TO TestLogin1;
GO
-- User also must have permission to create XML schema collections in the database.
GRANT CREATE XML SCHEMA COLLECTION
TO TestLogin1;
GO
-- Now user can execute the previous CREATE XML SCHEMA COLLECTION statement.
SETUSER 'TestLogin1';
GO
CREATE XML SCHEMA COLLECTION myTestSchemaCollection AS '<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema targetNamespace="https://schemas.adventure-works.com/Additional/ContactInfo"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xsd:element name="AdditionalContactInfo" >
  <xsd:complexType mixed="true" >
    <xsd:sequence>
      <xsd:any processContents="strict"
               namespace="https://schemas.adventure-works.com/Contact/Record
                          https://schemas.adventure-works.com/AdditionalContactTypes"
               minOccurs="0" maxOccurs="unbounded" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>
<xsd:element name="telephone" type="xsd:string" />
</xsd:schema>';
GO

-- Create a table by using the collection to type an XML column.
--TestLogin1 must have permission to create a table.
SETUSER;
GO
GRANT CREATE TABLE TO TestLogin1;
GO
-- The user also must have REFERENCES permission to use the XML schema collection
-- to create a typed XML column (REFERENCES permission on schema
-- collection is not needed).
GRANT REFERENCES ON XML SCHEMA COLLECTION::myTestSchemaCollection
TO TestLogin1;
GO
-- Now user can create a table and use the XML schema collection to create
-- a typed XML column.
SETUSER 'TestLogin1';
GO
CREATE TABLE MyTestTable (xmlCol xml (dbo.myTestSchemaCollection));
GO
-- To insert data in the table, the user needs EXECUTE permission on the XML schema collection.
-- GRANT EXECUTE permission to TestLogin2 on the xml schema collection.
SETUSER;
GO
GRANT EXECUTE ON XML SCHEMA COLLECTION::myTestSchemaCollection
TO TestLogin1;
GO
-- TestLogin1 does not own the dbo schema. This user must have INSERT permission.
GRANT INSERT TO TestLogin1;
GO
-- Now the user can insert data into the table.
SETUSER 'TestLogin1';
GO
INSERT INTO MyTestTable VALUES('
<telephone xmlns="http://schemas.adventure-works.com/Additional/ContactInfo">111-1111</telephone>
');
GO
-- To query the table, TestLogin1 must have permissions: SELECT on the table and EXECUTE on the XML schema collection.
SETUSER
GO
GRANT SELECT TO TestLogin1;
GO
-- TestLogin1 already has EXECUTE permission on the schema (granted before inserting a record in the table).
SELECT xmlCol.query('declare default element namespace "https://schemas.adventure-works.com/Additional/ContactInfo"; /telephone[1]')
FROM MyTestTable;
GO
-- To show that the user must have EXECUTE permission to query, revoke the
-- previously granted permission and return the query.
SETUSER;
GO
REVOKE EXECUTE ON XML SCHEMA COLLECTION::myTestSchemaCollection to TestLogin1;
GO
-- Now TestLogin1 cannot execute the query.
SETUSER 'TestLogin1';
GO
SELECT xmlCol.query('declare default element namespace "https://schemas.adventure-works.com/Additional/ContactInfo"; /telephone[1]')
FROM MyTestTable;
GO
-- Final cleanup
SETUSER;
GO
USE master;
GO
DROP DATABASE SampleDBForSchemaPermissions;
GO
DROP LOGIN TestLogin1;
GO

.C XML スキーマ コレクションに対する ALTER 権限の許可

データベース内の既存の XML スキーマ コレクションを変更するには、ユーザーに ALTER 権限が必要です。 次の例では、 ALTER 権限を許可する方法を示します。

SETUSER;
GO
USE master;
GO
CREATE LOGIN TestLogin1 WITH password='SQLSvrPwd1';
GO
CREATE DATABASE SampleDBForSchemaPermissions;
GO
USE SampleDBForSchemaPermissions;
GO
CREATE USER TestLogin1;
GO
-- Grant permission to the user.
SETUSER;
GO
-- User must have ALTER permission on the relational schema in the database.
GRANT ALTER ON SCHEMA::dbo TO TestLogin1;
GO
-- User also must have permission to create XML schema collections in the database.
GRANT CREATE XML SCHEMA COLLECTION
TO TestLogin1;
GO
-- Now user can execute the previous CREATE XML SCHEMA COLLECTION statement.
SETUSER 'TestLogin1';
GO
CREATE XML SCHEMA COLLECTION myTestSchemaCollection AS '<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema targetNamespace="https://schemas.adventure-works.com/Additional/ContactInfo"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xsd:element name="AdditionalContactInfo" >
  <xsd:complexType mixed="true" >
    <xsd:sequence>
      <xsd:any processContents="strict"
               namespace="https://schemas.adventure-works.com/Contact/Record
                          https://schemas.adventure-works.com/AdditionalContactTypes"
               minOccurs="0" maxOccurs="unbounded" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>
<xsd:element name="telephone" type="xsd:string" />
</xsd:schema>';
GO
-- Grant ALTER permission to TestLogin1.
SETUSER;
GO
GRANT ALTER ON XML SCHEMA COLLECTION::myTestSchemaCollection TO TestLogin1;
GO
-- TestLogin1 should be able to add components to the collection.
SETUSER 'TestLogin1';
GO
ALTER XML SCHEMA COLLECTION myTestSchemaCollection ADD '
<xsd:schema targetNamespace="https://schemas.adventure-works.com/Additional/ContactInfo"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://schemas.adventure-works.com/Additional/ContactInfo"
elementFormDefault="qualified">
<xsd:element name="pager" type="xsd:string"/>
</xsd:schema>';
GO
-- Final cleanup
SETUSER;
GO
USE master;
GO
DROP DATABASE SampleDBForSchemaPermissions;
GO
DROP LOGIN TestLogin1;
GO

D. XML スキーマ コレクションに対する TAKE OWNERSHIP 権限の許可

次の例では、XML スキーマの所有権を特定のユーザーから別のユーザーに移す方法を説明しています。 より興味深い例にするために、この例の 2 人のユーザーは異なる既定リレーショナル スキーマを使用します。

この例の内容は次のとおりです。

  • 2 つのリレーショナル スキーマ dbo および myOtherDBSchemaを備えた 1 つのデータベースを作成します。

  • TestLogin1TestLogin2という 2 人のユーザーを作成します。 TestLogin2 を、 myOtherDBSchema リレーショナル スキーマの所有者にします。

  • TestLogin1 は、 dbo リレーショナル スキーマに XML スキーマ コレクションを作成します。

  • TestLogin1 はその XML スキーマ コレクションに対する TAKE OWNERSHIP 権限を TestLogin2に許可します。

  • TestLogin2 は、 myOtherDBSchemaの XML スキーマ コレクションの所有者になります。このとき、XML スキーマ コレクションのリレーショナル スキーマは変更されません。

CREATE LOGIN TestLogin1 with password='SQLSvrPwd1';
GO
CREATE LOGIN TestLogin2 with password='SQLSvrPwd2';
GO
CREATE DATABASE SampleDBForSchemaPermissions;
GO
USE SampleDBForSchemaPermissions;
GO
-- Create another relational schema in the database.
CREATE SCHEMA myOtherDBSchema;
GO
-- Create users in the database. Note TestLogin2's default schema is
-- myOtherDBSchema.
CREATE USER TestLogin1;
GO
CREATE USER TestLogin2 WITH DEFAULT_SCHEMA=myOtherDBSchema;
GO
-- TestLogin2 will own myOtherDBSchema relational schema.
ALTER AUTHORIZATION ON SCHEMA::myOtherDBSchema TO TestLogin2;
GO

-- For TestLogin1 to create XML schema collection, the following
-- permission is required.
GRANT CREATE XML SCHEMA COLLECTION
TO TestLogin1;
GO
GRANT ALTER ON SCHEMA::dbo TO TestLogin1;
GO
-- Now TestLogin1 can create an XML schema collection.
SETUSER 'TestLogin1';
GO
CREATE XML SCHEMA COLLECTION myTestSchemaCollection AS '<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema targetNamespace="https://schemas.adventure-works.com/Additional/ContactInfo"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xsd:element name="AdditionalContactInfo" >
<xsd:complexType mixed="true" >
    <xsd:sequence>
      <xsd:any processContents="strict"
               namespace="https://schemas.adventure-works.com/Contact/Record
                          https://schemas.adventure-works.com/AdditionalContactTypes"
               minOccurs="0" maxOccurs="unbounded" />
    </xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="telephone" type="xsd:string" />
</xsd:schema>';
GO

-- Grant TAKE OWNERSHIP to TestLogin2.
SETUSER;
GO
GRANT TAKE OWNERSHIP ON XML SCHEMA COLLECTION::dbo.myTestSchemaCollection
TO TestLogin2;
GO
-- Verify the owner. Note the UserName and Principal_id is null.
SELECT user_name(sys.xml_schema_collections.principal_id) as UserName,
       sys.schemas.name as RelSchemaName,*
FROM   sys.xml_schema_collections
      JOIN sys.schemas
      ON sys.schemas.schema_id=sys.xml_schema_collections.schema_id;
GO
-- TestLogin2 can take ownership now.
SETUSER 'TestLogin2';
GO
ALTER AUTHORIZATION ON XML SCHEMA COLLECTION::dbo.myTestSchemaCollection
TO TestLogin2;
GO
-- Note that although TestLogin2 is the owner,the XML schema collection
-- is still in dbo.
SELECT user_name(sys.xml_schema_collections.principal_id) as UserName,
      sys.schemas.name as RelSchemaName,*
FROM sys.xml_schema_collections JOIN sys.schemas
     ON sys.schemas.schema_id=sys.xml_schema_collections.schema_id;
GO

-- TestLogin2 moves the collection from dbo to myOtherDBSchema relational schema.
-- TestLogin2 already has all necessary permissions.
-- 1) TestLogin2 owns the destination relational schema so he can alter it.
-- 2) TestLogin2 owns the XML schema collection (therefore, has CONTROL permission).
ALTER SCHEMA myOtherDBSchema
TRANSFER XML SCHEMA COLLECTION::dbo.myTestSchemaCollection;
GO

SELECT user_name(sys.xml_schema_collections.principal_id) as UserName,
       sys.schemas.name as RelSchemaName,*
FROM   sys.xml_schema_collections JOIN sys.schemas
       ON sys.schemas.schema_id=sys.xml_schema_collections.schema_id;
GO
-- Final cleanup
SETUSER;
GO
USE master;
GO
DROP DATABASE SampleDBForSchemaPermissions;
GO
DROP LOGIN TestLogin1;
DROP LOGIN TestLogin2;
GO

E. XML スキーマ コレクションに対する VIEW DEFINITION 権限の許可

次の例では、XML スキーマ コレクションに対する VIEW DEFINITION 権限を許可する方法を示します。

SETUSER;
GO
USE master;
GO
IF EXISTS( SELECT * FROM sysdatabases WHERE name='permissionsDB' )
   DROP DATABASE permissionsDB;
GO
IF EXISTS( SELECT * FROM sys.sql_logins WHERE name='schemaUser' )
   DROP LOGIN schemaUser;
GO
CREATE DATABASE permissionsDB;
GO
CREATE LOGIN schemaUser WITH PASSWORD='Pass#123',DEFAULT_DATABASE=permissionsDB;
GO
GRANT CONNECT SQL TO schemaUser;
GO
USE permissionsDB;
GO
CREATE USER schemaUser WITH DEFAULT_SCHEMA=dbo;
GO
CREATE XML SCHEMA COLLECTION MySC AS '
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ns"
xmlns:ns="http://ns">
   <simpleType name="ListOfIntegers">
      <list itemType="integer"/>
   </simpleType>
   <element name="root" type="ns:ListOfIntegers"/>
   <element name="gRoot" type="gMonth"/>
</schema>';
GO
-- schemaUser cannot see the contents of the collection.
SETUSER 'schemaUser';
GO
SELECT XML_SCHEMA_NAMESPACE(N'dbo',N'MySC');
GO

-- Grant schemaUser VIEW DEFINITION and REFERENCES permissions
-- on the XML schema collection.
SETUSER;
GO
GRANT VIEW DEFINITION ON XML SCHEMA COLLECTION::dbo.MySC TO schemaUser;
GO
GRANT REFERENCES ON XML SCHEMA COLLECTION::dbo.MySC TO schemaUser;
GO
-- Now schemaUser can see the content of the collection.
SETUSER 'schemaUser';
GO
SELECT XML_SCHEMA_NAMESPACE(N'dbo',N'MySC');
GO
-- Revoke schemaUser VIEW DEFINITION permissions
-- on the XML schema collection.
SETUSER;
GO
REVOKE VIEW DEFINITION ON XML SCHEMA COLLECTION::dbo.MySC FROM schemaUser;
GO
-- Now schemaUser cannot see the contents of
-- the collection.
SETUSER 'schemaUser';
GO
SELECT XML_SCHEMA_NAMESPACE(N'dbo',N'MySC');
GO

関連項目