开发者

How to set a foreign key and retrieve or delete data from multiple tables?

I am new to php and mysql. I created a database named 'students' which contain two tables as 'student_details' which have fields like 'ID, Name, Age, Tel#, Address' and another table as 'fee_details' wh开发者_如何学JAVAich have fields like 'ID(student_details table ID), Inst Id, Date, Receipt No'.

I want to set a foreign key and retrieve data from both tables when a student paid their fees and if a student passed out or discontinued I want a delete option to delete his all records from my tables. So please help me to solve this by PHP code and displays it in HTML while using a search form.


Enforcing referential integrity at the database level is the way to go. I believe when you said you wanted the delete "to delete his all records from my tables" you meant deleting the row and all its child records. You can do that by using foreign keys and ON DELETE CASCADE.

CREATE TABLE students 
(   
    student_id INT NOT NULL,
    name VARCHAR(30) NOT NULL,
    PRIMARY KEY (student_id)
) ENGINE=INNODB;


CREATE TABLE fee_details
(
    id INT, 
    date TIMESTAMP,
    student_id INT,

    FOREIGN KEY (student_id) REFERENCES students(student_id)
        ON DELETE CASCADE
) ENGINE=INNODB;

With this, when a student is deleted from the students table, all its associated records will be deleted from fee_details.


you can try mysql_query() and mysql_assoc_array()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜