Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Referencial integrity

Posted on 1998-07-13
9
Medium Priority
?
162 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 100 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
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

688 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