# Naming index based on index properties

**URL:** <https://forums.toadworld.com/t/naming-index-based-on-index-properties/41040>\
**Category:** Toad Data Modeler\
**Created:** [June 11, 2018, 8:10am UTC](https://forums.toadworld.com/t/naming-index-based-on-index-properties/41040 "2018-06-11T08:10:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dm.satya](https://avatars.discourse-cdn.com/v4/letter/d/ecd19e/32.png) [@Dm.satya](https://forums.toadworld.com/u/Dm.satya)\
**Post date:** [June 11, 2018, 8:10am UTC](https://forums.toadworld.com/t/naming-index-based-on-index-properties/41040/1 "2018-06-11T08:10:54Z")

</div>

Toad data modeler version 5.5  
Database: MS Sql Server 2014  
I am trying to auto generate indexes for PK and also name indexes based on their properties(clustered, non clustered, columnstore etc). I would like to prefix the index based on the index type  
Is there an option to do that in Toad data modeler to auto generate based on type of index or at least auto generate index for primary key similar to what we have for FK index generation.

---

<div class="post-metadata">

**Author:** ![Daril](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/daril/32/31_2.png) [@Daril](https://forums.toadworld.com/u/Daril)\
**Post date:** [June 12, 2018, 5:11am UTC](https://forums.toadworld.com/t/naming-index-based-on-index-properties/41040/2 "2018-06-12T05:11:23Z")

</div>

Hello,

for this operation is the best way to create some macro or script. Macros are very powerfull tools in TDM. TDM doesn’t generate indexes for PK only for foreign key, but I think SQL Server generate index for each PK automatically.

Daril

---

<div class="post-metadata">

**Author:** ![Dm.satya](https://avatars.discourse-cdn.com/v4/letter/d/ecd19e/32.png) [@Dm.satya](https://forums.toadworld.com/u/Dm.satya)\
**Post date:** [June 12, 2018, 6:26am UTC](https://forums.toadworld.com/t/naming-index-based-on-index-properties/41040/3 "2018-06-12T06:26:40Z")

</div>

Yes Sql Server generate index automatically. But we have some naming conventions to follow for other indexes that we create based on index properties.  
I am new to TDM scripting and I found a sample/working script to create macro for renaming indexes. But I am not able to modify that to apply for creating new indexes.  
Is there any existing sample script for this type in Toad or a Macro that can help me?

---

<div class="post-metadata">

**Author:** ![Daril](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/daril/32/31_2.png) [@Daril](https://forums.toadworld.com/u/Daril)\
**Post date:** [June 12, 2018, 6:43am UTC](https://forums.toadworld.com/t/naming-index-based-on-index-properties/41040/4 "2018-06-12T06:43:38Z")

</div>

At first you need to enabled Expert Mode in TDM. Is is in Main Menu - Settings - Options - General - Expert Mode. Now you can open Scripting Window in which you are able create small scripts and quickly try if it is OK (code for macro are very similar).  
In scripting Window choose one of opened model and by arrow button shift it to right (this model will be available for script). Bellow is Example script:

function main(){  
var Entity = Model.Entities.GetObject(0); //Get First Entity from model  
var Index = Entity.CreateNewObject(2012); //2012 is Object Type for Index, you can find more object types in Reference Guide  
Index.Name = “AAA”;

var IndexItem = Index.CreateNewObject(2013); //2013 is Object Type for Index Item  
var Attr = Entity.Attributes.GetObject(0); //Get First Attribute of Entity  
IndexItem.Attribute = Attr;  
Attr.LinkObject(IndexItem); //Create Back Link. Attribute must know in which indexes it is

Model.RefreshModel(); //Refreshed information in GUI  
}

A lot of information is in Reference Guide. There are list of properties of each object. Objects for Physical Model has Prefix PER. Object of MS SQL Database has postfix MS.

Daril
