473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Referential Integrety constraint

Hi All,

I am PHP programmer, I am using XAMPP software for programming. In MySQL the concept of referential integrety is not working. This is the following link which explains you my requirement. Pl go thru the following code in the link, it is not working..

http://www.informit.com/articles/art...seqNum=12&rl=1

Thank you.

Pavankumar P
Oct 12 '07 #1
3 1345
pbmods
5,821 Expert 4TB
Heya, Pavankumar.

What do you want your code to do? Give an example.
What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.
Oct 12 '07 #2
Heya, Pavankumar.

What do you want your code to do? Give an example.
What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.

Hi,


This is what i am doing in the code.

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE parent
  2. (
  3.   par_id   INT NOT NULL,
  4.   PRIMARY KEY (par_id)
  5. ) ENGINE = INNODB;
  6.  
  7. CREATE TABLE child
  8. (
  9.   par_id   INT NOT NULL,
  10.   child_id  INT NOT NULL,
  11.   PRIMARY KEY (par_id, child_id),
  12.   FOREIGN KEY (par_id) REFERENCES parent (par_id)
  13.     ON DELETE CASCADE
  14.     ON UPDATE CASCADE
  15. ) ENGINE = INNODB;
  16.  
  17.  
  18. INSERT INTO parent (par_id) VALUES(1),(2),(3);
  19. INSERT INTO child (par_id,child_id) VALUES(1,1),(1,2);
  20. INSERT INTO child (par_id,child_id) VALUES(2,1),(2,2),(2,3);
  21. INSERT INTO child (par_id,child_id) VALUES(3,1);

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM parent;
Expand|Select|Wrap|Line Numbers
  1. +--------+
  2. | par_id |
  3. +--------+
  4. |      1 |
  5. |      2 |
  6. |      3 |
  7. +--------+

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM child;
Expand|Select|Wrap|Line Numbers
  1. +--------+----------+
  2. | par_id | child_id |
  3. +--------+----------+
  4. |      1 |        1 |
  5. |      1 |        2 |
  6. |      2 |        1 |
  7. |      2 |        2 |
  8. |      2 |        3 |
  9. |      3 |        1 |
  10. +--------+----------+
2) This is what I am doing and want the result to be.

Expand|Select|Wrap|Line Numbers
  1. DELETE FROM parent WHERE par_id = 1;
  2.  
  3. SELECT * FROM parent;
Expand|Select|Wrap|Line Numbers
  1. +--------+
  2. | par_id |
  3. +--------+
  4. |      2 |
  5. |      3 |
  6. +--------+
Expand|Select|Wrap|Line Numbers
  1. UPDATE parent SET par_id = 100 WHERE par_id =2;
  2.  
  3. SELECT * FROM parent;
Expand|Select|Wrap|Line Numbers
  1. +--------+
  2. | par_id |
  3. +--------+
  4. |      3 |
  5. |    100 |
  6. +--------+
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM child;
Expand|Select|Wrap|Line Numbers
  1. +--------+----------+
  2. | par_id | child_id |
  3. +--------+----------+
  4. |      3 |        1 |
  5. |    100 |        1 |
  6. |    100 |        2 |
  7. |    100 |        3 |
  8. +--------+----------+
This is not happening with my Databse. Even though i do the same process i am getting, when i select the child table.

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM child;
Expand|Select|Wrap|Line Numbers
  1. +--------+----------+
  2. | par_id | child_id|
  3. +--------+----------+
  4. |      1 |        1 |
  5. |      1 |        2 |
  6. |      2 |        1 |
  7. |      2 |        2 |
  8. |      2 |        3 |
  9. |      3 |        1 |
  10. +--------+----------+
Pl give me the right code to get the same result.
Nov 1 '07 #3
amitpatel66
2,367 Expert 2GB
Hi,


This is what i am doing in the code.

CREATE TABLE parent
(
par_id INT NOT NULL,
PRIMARY KEY (par_id)
) ENGINE = INNODB;

CREATE TABLE child
(
par_id INT NOT NULL,
child_id INT NOT NULL,
PRIMARY KEY (par_id, child_id),
FOREIGN KEY (par_id) REFERENCES parent (par_id)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE = INNODB;


INSERT INTO parent (par_id) VALUES(1),(2),(3);
INSERT INTO child (par_id,child_id) VALUES(1,1),(1,2);
INSERT INTO child (par_id,child_id) VALUES(2,1),(2,2),(2,3);
INSERT INTO child (par_id,child_id) VALUES(3,1);


SELECT * FROM parent;
+--------+
| par_id |
+--------+
| 1 |
| 2 |
| 3 |
+--------+


SELECT * FROM child;
+--------+----------+
| par_id | child_id |
+--------+----------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
| 3 | 1 |
+--------+----------+

2) This is what I am doing and want the result to be.

DELETE FROM parent WHERE par_id = 1;

SELECT * FROM parent;
+--------+
| par_id |
+--------+
| 2 |
| 3 |
+--------+

UPDATE parent SET par_id = 100 WHERE par_id =2;

SELECT * FROM parent;
+--------+
| par_id |
+--------+
| 3 |
| 100 |
+--------+

SELECT * FROM child;
+--------+----------+
| par_id | child_id |
+--------+----------+
| 3 | 1 |
| 100 | 1 |
| 100 | 2 |
| 100 | 3 |
+--------+----------+

This is not happening with my Databse. Even though i do the same process i am getting, when i select the child table.

SELECT * FROM child;
+--------+----------+
| par_id | child_id|
+--------+----------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
| 3 | 1 |
+--------+----------+



Pl give me the right code to get the same result.

Are you able to update value 2 to 100 in parent table?
This will not be allowed becuase you have dependent records in child table
Nov 1 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Darius | last post by:
I was having an issue inserting data into a table in an I-Series DB2 database. The Insert statement itself is very simple: Insert into Table1 select * from Table2 These two tables have...
1
by: Grant McLean | last post by:
Hi First a simple question ... I have a table "access_log" that has foreign keys "app_id" and "app_user_id" that reference the "application_type" and "app_user" tables. When I insert into...
6
by: Greg Stark | last post by:
There's another poster complaining about referential integrity checks causing deadlocks. Unfortunately I've deleted the message so this response (and the archives aren't responding) isn't going to...
7
by: Jimmie H. Apsey | last post by:
Referential Integrity on one of our production tables seems to have been lost. I am running Postgres 7.1.3 embedded within Red Hat kernel-2.4.9-e.49. Within that I have a table with referential...
5
by: Geisler, Jim | last post by:
So, as far as I know, PostgreSQL does not have any way of verifying the loss of referential integrity. Are there any recommended methods or utilities for checking referential integrity in a...
6
by: heyvinay | last post by:
I have transaction table where the rows entered into the transaction can come a result of changes that take place if four different tables. So the situation is as follows: Transaction Table...
6
by: Jeff North | last post by:
I'm using Microsoft SQL Server Management Studio Express 9.00.2047.00 and expriencing problems with setting referential integrity on a link table. The tables' schema is as follows:...
0
by: Paul Daly | last post by:
I have the following xml file: http://www.dalyconsulting.com/AutomaticUpdates/AutomaticUpdates.xml and the following xsd file: http://www.dalyconsulting.com/AutomaticUpdates/AutomaticUpdates.xsd...
4
by: ApexData | last post by:
I have a 15 LookUp Tables tied to 15 ComboBoxes. Some of the Tables contain 3 to 10 Recs, and a couple contain up to 50 Recs. 1- Is it a CommonPractice to link the field that gets the ComboBox...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.