Page 127 - CodePilot V5.0 C8
P. 127

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_ID  First_Name      Last_Name      Date of Birth            Email              Course_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).


                  RAPID RECALL                             Tick ( ) if you know this.


                     1.  Data backup in databases means making a copy of the database files.

                     2.  A record is a collection of related data stored in different fields.









                                                                                                                  125
                                                                                          MySQL: My First Database
   122   123   124   125   126   127   128   129   130   131   132