Relate Field By coding

Hi, I want to create related field functionality through code . can you explain us steps to achieve this functionality.

May I suggest learning this technique

https://pgorod.github.io/Audit-File-Accesses/

and doing it from Studio, then learning how Studio does it, so you can do it from your code.