Jump to content

Primary And Foreign KEy Help


john666

Recommended Posts

Hi Guys..

 

Im new in PHP and Mysql

1 have Two table 1.students 2nd classes

i want to Set Primary key and Foreign Key in student Table and want to use Join Query on both

Help me in that

student Fields

 

 

1..(sn)seriel number

2..std_name

3.std_father_name

3..std_roll_num

4..std_class

5..std_cell

6..std_address

7..std_dob

8..std_address

9..std_fee

 

table 2 classes fields

 

1.class_id

2.class_name

3.date_created

 

what will be Primary key and foreign key here and join query to retrieve record   using phpmyadmin need step by step guide thanks

 

Link to comment
Share on other sites

+--------------+       +-------------------+
| class        |       | student           |
+--------------+       +-------------------+
| class_id (PK)|--+    | serial number (PK)|
| class_name   |  |    | std_name          |
| date_created |  |    | std_father_name   |
+--------------+  |    | std_roll_num      |
                  +---<| std_class     (FK)|
                       | std_cell          |
                       | std_address       |
                       | std_dob           |
                       | std_fee           |
                       +-------------------+

                       

The primary keys are the unique identifiers of each record.

Assuming std_class contains the id of the student's class then that would be a foreign key.

 

To query the table using a JOIN you would have a query like this

 

SELECT c.class_name, s.std_name, s.std_father_name
FROM student as s
    INNER JOIN class as c ON s.std_class = c.class_id
ORDER BY c.class_name

Note that you need InnoDB tables to define foreign key constraints.

 

You do not have to formally define a foreign key to use a field in a join.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.