Primary Key Constraint Implementation In Notes

To build a Primary Key constraint in Notes -- sounds great, isn't it. This is a very basic and at times very essential requirement in the applications we develop. Let me demonstrate how writing simple script can achieve this.

Assume we have a form called "Department Master Form", which has two Text, Editable fields -- Dept_Code and Dept_name. Now the requirement is, Dept_Code should be Unique and Not Null for each record (document) we create, thus Dept_Code is the Primary Key field.

In any RDBMS, this implementation is quite straightforward by just declaring the Dept_Code column as Primary Key when creating the "Department Master" table.

To implement in Notes

1. Create a view (e.g. Department Master View) with first sorted column as the Primary Key column, in this case Dept_Code is the first sorted column for this view.

2. In the form (Department Master Form) QuerySave event, write script to do the following things:
2a. Check that the Key field value (i.e. Dept_Code) in the Current Document is Not Null;
2b. If Key value (Dept_Code) of Current Document (Document you are currently Editing / Creating) is the same as Key value of an Existing Document (Document saved earlier in the database), compare their Universal ID's. If Universal ID's match (You are Editing an Existing Document), allow saving the Current Document. If UniversalID's for these two documents is different, raise Primary Key Voilation message.

The below code in QuerySave event of "Department Master Form" achieves point 2 above.

'' Code in Code field below Extension 1:

The same code can be extended to cater for Primary Key with multiple field values (Combination Primary Key).

Lets say, Dept_Code and Section_Code fields in the form "Department Master Form" make the Combination Primary Key. Modify the view to have multiple sorted columns - Sort by Dept_Code then by Section_Code, and change the QuerySave script to include Not Null validation for both fields. Also, change the line Set existingdoc = view.GetDocumentByKey( Key, True ) to... Set existingdoc = view.GetDocumentByKey( KeyArray, True ) where KeyArray stores all fields in Primary Key in that order.

In the present example, KeyArray will have 2 values as Dim KeyArray(0 to 1) As Variant KeyArray(0) = Cstr(currentdoc.Dept_Code(0)) KeyArray(1) = Cstr(currentdoc.Section_Code(0))

Extension 2: (Case Insensitive Keys) You can even extend the script to take care of the Case of the key fields values. Values like "D0001" and "d0001" for Dept_Code field can always be saved and compared as "D0001".

Thanks and Happy Reading :)

This was first published in November 2000

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.