Solved

Referencial integrity

Posted on 1998-07-13
9
157 Views
Last Modified: 2010-04-04
I know how to create indexes (primary key, secondary...)at runtime. We can use "addindex" command.
I need now to create referential inegrity at runtime.
Anybody can help me?
0
Comment
Question by:fabianos
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 5

Expert Comment

by:mayhew
ID: 1357785
fabianos,

Referential integrity is more of a design-time issue dealing largely with foreign keys between two (or more) tables in a (usually) one to many relationship.  I'll give you an example.

Say you have two tables: one that tracks parent (mother) information and one that tracks child information.  

Your first table might be:

mother_id     mother_name     mother_address   etc....
---------------     ---------------------     --------------------------  
1                  Mary                   123 Main
2                  Linda                  456 Oak St.
3                  Kathy                  789 Maple

Your second table might be

kid_id     mother_id     kid_name
---------     ----------------     ---------------
1            1                   Bob
2            1                   Louise
3            2                   Jill
4            3                   Jack
5            3                   Nick

Analysis of this table shows that Mary has two children (Bob, Louise), Linda has Jill and Kathy has Jack and Nick.

What referential integrity does is to make sure that you can't delete Kathy's record in the mother table without making sure that Jack and Nick have also been deleted in the kid table first.  It also ensures that you can't add rows to the kid table unless there is a corresponding row already in the mother table.

This can be handled programatically.  But it is usually dealt with by setting up a foreign key on the mother_id column of the kid table that references the mother_id row of the mother table.

Hope that helps!
0
 
LVL 3

Expert Comment

by:rickpet
ID: 1357786
If I had to do this at Runtime...I would use SQL to do it

Look at Create Unique Index or Create Table

Rick
0
 

Author Comment

by:fabianos
ID: 1357787
Thanks everybody!
What I really want is make an utility program to my applications that create tables, indexes, integrities...
I will use SQL like rickpet said.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 3

Expert Comment

by:rickpet
ID: 1357788
Also you'll need to look at alter table to setup constraints...

Rick
0
 
LVL 3

Accepted Solution

by:
rickpet earned 50 total points
ID: 1357789
So does that mean your happy with answer???

Rick
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1357790
Use DbiDoRestructure.

This C example is from the BDE help file:

Example 3: Add validity checks and referential integrity to a table.

This example uses the following input:      fDbiDoRestructure3(hDb, "CUSTOMER");

DBIResult fDbiDoRestructure3(hDBIDb hDb, char *TblName)
{
   DBIResult   rslt;
   CRTblDesc   TblDesc;
   RINTDesc    pRintDesc[] = {{1, "Order No", rintDEPENDENT, "orders.db",
                                rintCASCADE, rintRESTRICT, 1, {2}, {1}}};
   VCHKDesc    pVchkDesc[] = {{1, TRUE, TRUE, FALSE, FALSE, 1000.00,
                               NULL, NULL, NULL, lkupNONE, NULL},
              // Setting the first field required with minimum value 1000.00
                              {2, TRUE, TRUE, FALSE, FALSE, NULL, NULL,

                                NULL, NULL, lkupNONE, NULL}};
              // Setting second field required.
   memset((void *) &TblDesc, 0, sizeof(CRTblDesc));
   lstrcpy(TblDesc.szTblName, TblName);
   lstrcpy(TblDesc.szTblType, szPARADOX);
   TblDesc.pvchkDesc = pVchkDesc;
   TblDesc.printDesc = pRintDesc;
   rslt = Chk(DbiDoRestructure(hDb, 1, &TblDesc, NULL, NULL,
                               NULL, FALSE));
   return rslt;
}
0
 
LVL 5

Expert Comment

by:mayhew
ID: 1357791
Actually you can set up constraints in your create table statement.

The syntax is:
CONSTRAINT constraint_name FOREIGN KEY (col_name)      REFERENCES ref_table (ref_col)

For example,
CREATE TABLE grades (
gradesid integer constraint fk_student foreign key (gradesid) references students(studentsid),
grade1 char(1),
etc. )

Where students is a table with studentsid having a primary key or unique constraint.

The above statement will provide the referential integrity you are looking for.
0
 
LVL 3

Expert Comment

by:rickpet
ID: 1357792
Yes I agree...that's why I said look at Create Table originally...but if you want to do it after the fact you should look at alter table...

Rick
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1357793
The problem is that if you are using Local SQL, say for Paradox, you cannot use some reserved words such as REFERENCES and FOREIGN KEY.
See Local SQL help file on SQL-92 language elements that are not used in local SQL.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question