Solved

Referencial integrity

Posted on 1998-07-13
9
155 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
 
LVL 3

Expert Comment

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

Rick
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now