Create a Primary Key
Last updated 21/11/2023
To create a primary key, you must also create the primary key field to hold the GUID used for the index. After you have named your table and added the primary key field, perform the following steps to create the primary index. If you have not added a table in Five before, please refer to the chapter Add a Table.Add a Primary Key
tip
If your primary key field has a data type of GUID ensure you have the Generated switch set to true! This is shown in the image below.
Figure 1 - Primary key field
1. Add your table and the fields for your tables.
2. Click the Indices tab.
Figure 2 - Indices tab
3. Click the Add Indices button.
Figure 3 - Add Indices button
4. Type an ID in the Index ID field.
tip
It is good naming convention to name your primary key after your table! For example, if your table was named Customer table, call the primary index CustomerKey or CustomerID.
5. Click the lookup icon in the Key Type field and select Primary.
info
The Is Unique switch is defaulted to true as it is necessary for the index to have a unique value to identify the record. The Is Parent and Is Cascade Delete switches are removed because they are not relevant when creating a primary key.
Figure 4 - Add a primary index
6. Click the Index Fields tab.
Figure 5 - Index Fields tab
7. Click the Add Index Fields button.
Figure 6 - Add Index Fields button
8. Click the lookup icon in the Field field and select the field on the current table that will hold the GUID.
Figure 7 - Add an index field
9. Click all three Save buttons in the stacked form app bars.
Figure 8 - Save buttons
10. Click the Save button in the Table Upgrade window.
Figure 9 - Save the table upgrade
Add a Composite Primary Key
A primary key consisting of two or more columns is called a composite primary key. Creating a composite primary key can be a little bit different from a single field primary key, because you don't need to generate a GUID for the key. Instead, the values in the multiple fields combined will create the uniqueness. For this reason, the steps below will take you through creating an entire table with multiple fields for the primary key.1. Click the Add Item button.
Figure 10 - Add Item button
2. Click the lookup icon in the Database field and select your database if not already preselected.
3. Type an ID in the Data Source ID field.
4. Type a name in the Table Name field.
Figure 11 - Add a table
5. Click the Fields tab.
Figure 12 - Fields tab
6. Click the Add Fields button.
Figure 13 - Add Fields button
7. Type an ID in the Field ID field.
8. Click the lookup icon in the Data Type field and select a type.
caution
If you were making a single field primary key you would normally select a data type of GUID and have the Generated switch set to true for Five to generate a GUID. Because we are making a multiple field primary key, the fields being created for the primary key do not need to be a GUID and the Generated switch needs to be set to false for this scenario.
9. Click the Required switch.
10. Click the lookup icon in the Default Display Type field and select a type.
Figure 14 - Add a field
11. Click the Save button in the form app bar.
Figure 15 - Save button
12. Click the Add Fields button.
Figure 16 - Add Fields button
13. Type an ID in the Field ID field.
14. Click the lookup icon in the Data Type field and select a type.
15. Click the Required switch.
16. Click the lookup icon in the Default Display Type field and select a type.
Figure 17 - Add a field
17. Click the Save button in the form app bar.
Figure 18 - Save button
18. Click the Indices tab
Figure 19 - Indices tab
19. Click the Add Indices button.
Figure 20 - Add Indices button
20. Type an ID in the Index ID field.
21. Click the lookup icon in the Key Type field and select Primary.
Figure 21 - Add a primary index
22. Click the Index Fields tab.
Figure 22 - Index Fields tab
23. Click the Add Index Fields button.
Figure 23 - Add Index Fields button
24. Click the lookup icon in the Field field and select a field in the current table.
Figure 24 - Add an index field
25. Click the Save button in the form app bar.
Figure 25 - Save button
26. Click the Add Index Fields button.
Figure 26 - Add Index Fields button
27. Click the lookup icon in the Field field and select another field in the current table.
Figure 27 - Add an index field
info
You can continue to add as many fields as your primary key needs.
28. Click the Save button in the form app bar.
Figure 28 - Save button
29. Click the Save button in the Table Upgrade window.
Figure 29 - Save the table upgrade