Solved

Referencial integrity

Posted on 1998-07-13
9
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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 brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month4 days, 11 hours left to enroll

635 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