Share via


Erstellen, Ändern und Entfernen von Indizes

In der SQL Server Management Objects-Hierarchie (SMO) werden Indizes durch das Index-Objekt dargestellt. Die indizierten Spalten werden durch eine Auflistung von IndexedColumn-Objekten dargestellt, die von der IndexedColumns-Eigenschaft dargestellt werden.

Sie können einen Index für eine XML-Spalte erstellen, indem Sie die IsXmlIndex-Eigenschaft des Index-Objekts angeben.

Beispiele

Um die bereitgestellten Codebeispiele verwenden zu können, müssen Sie die Programmierumgebung, die Programmiervorlage und die Programmiersprache wählen, in der die Anwendung erstellt werden soll. Weitere Informationen finden Sie unter Vorgehensweise: Erstellen eines Visual Basic-SMO-Projekts in Visual Studio .NET oder Vorgehensweise: Erstellen eines Visual C#-SMO-Projekts in Visual Studio .NET.

Erstellen eines nicht gruppierten, zusammengesetzten Indexes in Visual Basic

Dieses Codebeispiel zeigt, wie ein zusammengesetzter, nicht gruppierter Index erstellt wird. Bei einem zusammengesetzten Index müssen Sie dem Index mindestens zwei Spalten hinzufügen. Legen Sie die IsClustered-Eigenschaft für einen nicht gruppierten Index auf False fest.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks database.
Dim db As Database
db = srv.Databases("AdventureWorks")
'Declare a Table object and reference the HumanResources table.
Dim tb As Table
tb = db.Tables("Employee", "HumanResources")
'Define an Index object variable by providing the parent table and index name in the constructor.
Dim idx As Index
idx = New Index(tb, "TestIndex")
'Add indexed columns to the index.
Dim icol1 As IndexedColumn
icol1 = New IndexedColumn(idx, "EmployeeID", True)
idx.IndexedColumns.Add(icol1)
Dim icol2 As IndexedColumn
icol2 = New IndexedColumn(idx, "HireDate", True)
idx.IndexedColumns.Add(icol2)
'Set the index properties.
idx.IndexKeyType = IndexKeyType.DriUniqueKey
idx.IsClustered = False
idx.FillFactor = 50
'Create the index on the instance of SQL Server.
idx.Create()
'Modify the page locks property.
idx.DisallowPageLocks = True
'Run the Alter method to make the change on the instance of SQL Server.
idx.Alter()
'Remove the index from the table.
idx.Drop()

Erstellen eines nicht gruppierten, zusammengesetzten Index in Visual C#

Dieses Codebeispiel zeigt, wie ein zusammengesetzter, nicht gruppierter Index erstellt wird. Bei einem zusammengesetzten Index müssen Sie dem Index mindestens zwei Spalten hinzufügen. Legen Sie die IsClustered-Eigenschaft für einen nicht gruppierten Index auf False fest.

{ 
//Connect to the local, default instance of SQL Server. 
Server srv; 
srv = new Server(); 
//Reference the AdventureWorks database. 
Database db; 
db = srv.Databases("AdventureWorks"); 
//Declare a Table object and reference the HumanResources table. 
Table tb; 
tb = db.Tables("Employee", "HumanResources"); 
//Define an Index object variable by providing the parent table and index name in the constructor. 
Index idx; 
idx = new Index(tb, "TestIndex"); 
//Add indexed columns to the index. 
IndexedColumn icol1; 
icol1 = new IndexedColumn(idx, "EmployeeID", true); 
idx.IndexedColumns.Add(icol1); 
IndexedColumn icol2; 
icol2 = new IndexedColumn(idx, "HireDate", true); 
idx.IndexedColumns.Add(icol2); 
//Set the index properties. 
idx.IndexKeyType = IndexKeyType.DriUniqueKey; 
idx.IsClustered = false; 
idx.FillFactor = 50; 
//Create the index on the instance of SQL Server. 
idx.Create(); 
//Modify the page locks property. 
idx.DisallowPageLocks = true; 
//Run the Alter method to make the change on the instance of SQL Server. 
idx.Alter(); 
//Remove the index from the table. 
idx.Drop();
}

Erstellen eines XML-Index in Visual Basic

Dieses Codebeispiel zeigt, wie für einen XML-Datentyp ein XML-Index erstellt wird. Der XML-Datentyp ist eine XML-Schemaauflistung mit dem Namen MySampleCollection, die in Verwenden von XML-Schemas erstellt wird. XML-Indizes weisen einige Einschränkungen auf. Eine dieser Einschränkungen ist, dass der Index auf Basis einer Tabelle erstellt werden muss, die bereits über einen gruppierten Primärschlüssel verfügt.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server()
'Reference the AdventureWorks database.
Dim db As Database
db = srv.Databases("AdventureWorks")
'Define a Table object variable and add an XML type column. 
Dim tb As Table
tb = New Table(db, "XmlTable")
Dim col1 As Column
'This sample requires that an XML schema type called MySampleCollection exists on the database.
col1 = New Column(tb, "XMLValue", DataType.Xml("MySampleCollection"))
'Add another integer column that can be made into a unique, primary key.
tb.Columns.Add(col1)
Dim col2 As Column
col2 = New Column(tb, "Number", DataType.Int)
col2.Nullable = False
tb.Columns.Add(col2)
'Create the table of the instance of SQL Server.
tb.Create()
'Create a unique, clustered, primary key index on the integer column. This is required for an XML index.
Dim cp As Index
cp = New Index(tb, "clusprimindex")
cp.IsClustered = True
cp.IndexKeyType = IndexKeyType.DriPrimaryKey
Dim cpcol As IndexedColumn
cpcol = New IndexedColumn(cp, "Number", True)
cp.IndexedColumns.Add(cpcol)
cp.Create()
'Define and XML Index object variable by supplying the parent table and the XML index name arguments in the constructor.
Dim i As Index
i = New Index(tb, "xmlindex")
Dim ic As IndexedColumn
ic = New IndexedColumn(i, "XMLValue", True)
i.IndexedColumns.Add(ic)
'Create the XML index on the instance of SQL Server. 
i.Create()

Erstellen eines XML-Index in Visual C#

Dieses Codebeispiel zeigt, wie für einen XML-Datentyp ein XML-Index erstellt wird. Der XML-Datentyp ist eine XML-Schemaauflistung mit dem Namen MySampleCollection, die in Verwenden von XML-Schemas erstellt wird. XML-Indizes weisen einige Einschränkungen auf. Eine dieser Einschränkungen ist, dass der Index auf Basis einer Tabelle erstellt werden muss, die bereits über einen gruppierten Primärschlüssel verfügt.

{
//Connect to the local, default instance of SQL Server. 
Server srv; 
srv = new Server(); 
//Reference the AdventureWorks database. 
Database db; 
db = srv.Databases("AdventureWorks"); 
//Define a Table object variable and add an XML type column. 
Table tb; 
tb = new Table(db, "XmlTable"); 
Column col1; 
//This sample requires that an XML schema type called MySampleCollection exists on the database. 
col1 = new Column(tb, "XMLValue", DataType.Xml("MySampleCollection")); 
//Add another integer column that can be made into a unique, primary key. 
tb.Columns.Add(col1); 
Column col2; 
col2 = new Column(tb, "Number", DataType.Int); 
col2.Nullable = false; 
tb.Columns.Add(col2); 
//Create the table of the instance of SQL Server. 
tb.Create(); 
//Create a unique, clustered, primary key index on the integer column. This is required for an XML index. 
Index cp; 
cp = new Index(tb, "clusprimindex"); 
cp.IsClustered = true; 
cp.IndexKeyType = IndexKeyType.DriPrimaryKey; 
IndexedColumn cpcol; 
cpcol = new IndexedColumn(cp, "Number", true); 
cp.IndexedColumns.Add(cpcol); 
cp.Create(); 
//Define and XML Index object variable by supplying the parent table and the XML index name arguments in the constructor. 
Index i; 
i = new Index(tb, "xmlindex"); 
IndexedColumn ic; 
ic = new IndexedColumn(i, "XMLValue", true); 
i.IndexedColumns.Add(ic); 
//Create the XML index on the instance of SQL Server. 
i.Create(); 
}

Siehe auch

Verweis