Page 61 - 2611_SmartGPT Pro V(5.0) C-8
P. 61

Primary Key
                 Primary key is a field which is used to uniquely identify records in a database. It is a unique field

                 and it cannot be left blank. There can be only one primary key in a table.
                 In the Student Table, Student_ID is the primary key, which is used to uniquely identify the records.


                 Foreign Key

                 A foreign key is a field in one table that refers to the primary key in another table. It helps to
                 establish a relationship between two tables. A foreign key makes sure that data in one table
                 matches data in another table.

                 Let’s understand the relationship among tables in a relational database with the help of an example.
                                                            Student Table

                     Student_      First_       Last_        Date of                  Email            Course_
                         ID        Name        Name           Birth                                       ID

                          1      Aarav        Sharma      15-Mar-2012     aarav.sharma@gmail.com          C101
                          2      Priya        Patel       20-May-2012     priya.patel@gmail.com           C102
                          3      Rahul        Verma       10-Mar-2012     rahul.verma@gmail.com           C103
                          4      Sneha        Iyer        25-Nov-2011     sneha.iyer@gmail.com            C101

                         5       Dev          Kapoor      12-Jul-2012     dev.kapoor@gmail.com            C102

                                                            Course Table



                            Course_ID                        Course Name                     Teacher_Name


                               C101                Mathematics                       Dr. Ramesh Kumar

                               C102                Science                           Ms. Priya Sethi
                               C103                Social Studies                    Mr. Rahul Sharma

                 In this example, the Student Table and Course Table share a common field named Course_ID.
                 The Course_ID in the Student Table is a foreign key that references the Course_ID (a primary key)
                 in the Course Table.

                 From this relationship, the following information gets extracted:

                   One Course (Course Table) is opted by many Students (Student Table).
                   Each student (Student Table) is linked to only one course via Course_ID (Foreign Key).




                      Tick ( ) if you know this.

                      ▶  Data backup in databases means making a copy of the database files.
                      ▶  A record is a collection of related data stored in different fields.





                                                                                          MySQL: My First Database  59
   56   57   58   59   60   61   62   63   64   65   66