Tworzenie, zmienianie i usuwanie indeksów
W SQL Server Hierarchia obiektów (obiekty SMO) zarządzania, indeksy są reprezentowane przez Index obiekt. Kolumny indeksowane są reprezentowane przez kolekcja IndexedColumn reprezentowane przez obiekty IndexedColumns() Właściwość.
Tworzenie indeksu kolumna XML przez określenie IsXmlIndex() Właściwość Index obiekt.
Przykłady
Aby używać dostarczonych przykładów kodu źródłowego, należy wybrać środowisko, szablon oraz język programowania, które będą używane do tworzenia aplikacji.Aby uzyskać więcej informacji zobacz Jak Tworzenie obiektów SMO projektu Visual Basic w programie Visual Studio .NET lub Jak Tworzenie projektu programu Visual C# obiekty SMO w programie Visual Studio .NET.
Tworzenie non klastrowej, indeks złożony w języku Visual Basic
Ten przykładowy kod demonstruje sposób tworzony jest indeks złożony, bez klastrów.Projekt wstępny indeksu należy dodać więcej niż jedną kolumna do indeksu.zestaw IsClustered() Właściwość False dla indeks nieklastrowany.
Tworzenia indeksu nie klastrowej, projekt wstępny w środowisku Visual C#
Ten przykładowy kod demonstruje sposób tworzony jest indeks złożony, bez klastrów.Projekt wstępny indeksu należy dodać więcej niż jedną kolumna do indeksu.zestaw IsClustered() Właściwość False dla indeks nieklastrowany.
{
//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();
}
Tworzenie indeksu XML w programie Visual Basic
W tym przykładzie kodu pokazano, jak utworzyć indeks XML na podstawie typu danych XML.Typ danych XML jest kolekcja schematu XML o nazwie MySampleCollection, który jest tworzony w Używając schematów XML. Indeksy XML ma pewne ograniczenia, z których jeden to, że należy utworzyć w tabela, która już ma klastrowany klucz podstawowy.
Tworzenie indeksu XML w środowisku Visual C#
W tym przykładzie kodu pokazano, jak utworzyć indeks XML na podstawie typu danych XML.Typ danych XML jest kolekcja schematu XML o nazwie MySampleCollection, który jest tworzony w Używając schematów XML. Indeksy XML ma pewne ograniczenia, z których jeden to, że należy utworzyć w tabela, która już ma klastrowany klucz podstawowy.
{
//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();
}
See Also
Reference
Index