Solved

Referencial integrity

Posted on 1998-07-13
9
154 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

12 Experts available now in Live!

Get 1:1 Help Now