Question

What are Surrogate Keys and Natural Keys? Why to use them

  • 14 March 2024
  • 3 replies
  • 37 views

I'm encountering the terms "surrogate key" and "natural key" while diving into Acumatica data management. Can someone explain the difference between these two key types and how they're used in Acumatica? Understanding their purposes would be much appreciated.

 

 

As you can see in documentation it has mentioned that for primary key use the suffix as ID and for natural key use the suffix as CD based on the requirement. But in some cases the natural keys and primary keys are two different columns. And in some cases both were in same column. Can anyone justify why so.


3 replies

Badge +11

This is fairly comprehensive.

Can you give me a detailed example on surrogate and natural keys

Badge +11

I’m not sure I can, however, I will share an excerpt from my personal Acumatica documentation:

 

The most important piece of the DAC is the KEY configuration.

A simple DAC should have an identity ID column which will auto-generate in SQL.

To make the key look nicer in the UI, specify a CD field which operates as the Substitute Key for the UI only.

Move the IsKey = true from the ID field to the CD field

The CD field will have a selector looking at itself

#region PlantID
[PXDBIdentity]
public virtual int? PlantID { get; set; }
public abstract class plantID : PX.Data.BQL.BqlInt.Field<plantID> { }
#endregion

#region PlantCD
[PXDBString(50, IsKey = true, IsUnicode = true, InputMask = ">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")]
[PXDefault]
[PXUIField(DisplayName = "Plant ID", Required = true)]
[PXSelector(typeof(Search<PlantsDAC.plantCD>),
typeof(PlantsDAC.plantCD))]
[AutoNumber(typeof(DSAPSetupExt.usrDSPositivePayNumberingSequenceID), typeof(AccessInfo.businessDate))]
public virtual string PlantCD { get; set; }
public abstract class plantCD : PX.Data.BQL.BqlString.Field<plantCD> { }
#endregion

NOTE: this also includes [AutoNumber] which may not be required in your situation.

 

When referencing the DAC from other tables (as a foreign key, use the ID field in the selector with the CD field specified as the Substitute Key:

#region ProductID
[PXDBInt(IsKey = true)]
[PXSelector(typeof(Search<ProductsDAC.productID>),
    typeof(ProductsDAC.productCD),
    SubstituteKey = typeof(ProductsDAC.productCD))]
[PXUIField(DisplayName = "Product ID")]
public virtual int? ProductID { get; set; }
public abstract class productID : PX.Data.BQL.BqlInt.Field<productID> { }
#endregion

 

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved