Skip to main content

Create a Primary Key

Last updated 19/03/2024

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.

Primary key field
Figure 1 - Primary key field

1. Add your table and the fields for your tables.

2. Click the Indices tab.


Indices tab
Figure 2 - Indices tab

3. Click the Add Indices button.


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.

Add a primary index
Figure 4 - Add a primary index

6. Click the Index Fields tab.


Index Fields tab
Figure 5 - Index Fields tab

7. Click the Add Index Fields button.


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.


Add an index field
Figure 7 - Add an index field

9. Click all three Save buttons in the stacked form app bars.


Save buttons
Figure 8 - Save buttons

10. Click the Save button in the Table Upgrade window.


Save the table upgrade
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.


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.


Add a table
Figure 11 - Add a table

5. Click the Fields tab.


Fields tab
Figure 12 - Fields tab

6. Click the Add Fields button.


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.


Add a field
Figure 14 - Add a field

11. Click the Save button in the form app bar.


Save button
Figure 15 - Save button

12. Click the Add Fields button.


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.


Add a field
Figure 17 - Add a field

17. Click the Save button in the form app bar.


Save button
Figure 18 - Save button

18. Click the Indices tab


Indices tab
Figure 19 - Indices tab

19. Click the Add Indices button.


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.


Add a primary index
Figure 21 - Add a primary index

22. Click the Index Fields tab.


Index Fields tab
Figure 22 - Index Fields tab

23. Click the Add Index Fields button.


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.


Add an index field
Figure 24 - Add an index field

25. Click the Save button in the form app bar.


Save button
Figure 25 - Save button

26. Click the Add Index Fields button.


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.


Add an index field
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.


Save button
Figure 28 - Save button

29. Click the Save button in the Table Upgrade window.


Save the table upgrade
Figure 29 - Save the table upgrade