6.1.3 Excel VBA数据库的索引

6.1.3 Excel VBA数据库的索引,第1张

数据库管理系统中.索引提供了一种无需扫描整张表就能实现对数据的快速查询的途径,使用索引可以优化查询。这就像为了找到某本书中一个句子可以逐页搜索,也可以通过使用该书的月录.快速定位到要搜索的主题然后找到需要的句子。显然后一种方法 *** 作起来更快。在数据库中,表的索引与附在一本书后面的索引非常相似.因此为数据衰建立索引是加快查询速度的有效手段.用户可以根据应用环境的需要,在基本表上建立一个或者多个索引,以提供多种存取路径,加快查找速度。索引一般由数据库管理员或者表的拥有者(创建该表的用户)进行创建。系统在存储数据时会自动选择合适的索引作为存取路径,用户无法手工选择索引。索引也存储数据.只不过索引中的数据反映的不是实际仿息,而是方便搜索用的路径信息。 .索引有两种类型.分别是聚簇索引和非聚簇索引。聚簇索引和目录表非常相似,记录的索引顺序与物理顺序相同。在非聚簇索引中,记录的物理顺序与逻辑顺序没有必然的联系。通常情况下使用的是聚簇索引,但是每个表只能有一个聚簇索引。

我在2003版上试了一下,不存在你说的错误呀,当然得到的值与其他单元格的属性有些不一样,比如合并后单元格rng行数结果是合并前第一区域的行数而不是总行数。

各单元格的索引用序号引用试试,如rng(5)

Sub CreateRelationX()

Dim dbsNorthwind As Database

Dim tdfEmployees As TableDef

Dim tdfNew As TableDef

Dim idxNew As Index

Dim relNew As Relation

Dim idxLoop As Index

Set dbsNorthwind = OpenDatabase("Northwind.mdb")

With dbsNorthwind

-- Add new field to Employees table.

Set tdfEmployees = .TableDefs!Employees

tdfEmployees.Fields.Append _

tdfEmployees.CreateField("DeptID", dbInteger, 2)

-- Create new Departments table.

Set tdfNew = .CreateTableDef("Departments")

With tdfNew

-- Create and append Field objects to Fields

-- collection of the new TableDef object.

.Fields.Append .CreateField("DeptID", dbInteger, 2)

.Fields.Append .CreateField("DeptName", dbText, 20)

-- Create Index object for Departments table.

Set idxNew = .CreateIndex("DeptIDIndex")

-- Create and append Field object to Fields

-- collection of the new Index object.

idxNew.Fields.Append idxNew.CreateField("DeptID")

-- The index in the primary table must be Unique in

-- order to be part of a Relation.

idxNew.Unique = True

.Indexes.Append idxNew

End With

.TableDefs.Append tdfNew

-- Create EmployeesDepartments Relation object, using

-- the names of the two tables in the relation.

Set relNew = .CreateRelation("EmployeesDepartments", _

tdfNew.Name, tdfEmployees.Name, _

dbRelationUpdateCascade)

-- Create Field object for the Fields collection of the

-- new Relation object. Set the Name and ForeignName

-- properties based on the fields to be used for the

-- relation.

relNew.Fields.Append relNew.CreateField("DeptID")

relNew.Fields!DeptID.ForeignName = "DeptID"

.Relations.Append relNew

-- Print report.

Debug.Print "Properties of " &relNew.Name &_

" Relation"

Debug.Print " Table = " &relNew.Table

Debug.Print " ForeignTable = " &_

relNew.ForeignTable

Debug.Print "Fields of " &relNew.Name &" Relation"

With relNew.Fields!DeptID

Debug.Print " " &.Name

Debug.Print "Name = " &.Name

Debug.Print "ForeignName = " &.ForeignName

End With

Debug.Print "Indexes in " &tdfEmployees.Name &_

" TableDef"

For Each idxLoop In tdfEmployees.Indexes

Debug.Print " " &idxLoop.Name &_

", Foreign = " &idxLoop.Foreign

Next idxLoop

-- Delete new objects because this is a demonstration.

.Relations.Delete relNew.Name

.TableDefs.Delete tdfNew.Name

tdfEmployees.Fields.Delete "DeptID"

.Close

End With

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11582243.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存