Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,194,672 members, 7,955,463 topics. Date: Sunday, 22 September 2024 at 06:36 AM |
Nairaland Forum / Science/Technology / Programming / Please I Don't Really Understand Foreign Keys In MySQL (620 Views)
Combining Many Tables In Mysql / Complete Autocad Shortcuts 3D And 2D Commands Keys In PDF / Learn How To Save Img In Mysql Database. Many Web Developers Find It Difficult.. (2) (3) (4)
Please I Don't Really Understand Foreign Keys In MySQL by CenturyCoder: 2:25pm On Jul 09, 2020 |
Please help me out. So I just started a project and I would be using PHP and MySQL and Python for this project and then I came across the need of foreign keys which I don't really understand much. Please can someone explain foreign keys and show me a way I can use them. Thanks. |
Re: Please I Don't Really Understand Foreign Keys In MySQL by Karleb(m): 2:53pm On Jul 09, 2020 |
Foreign are the primary key of related tables. Let's say a Users table and Posts tables has a one to many relationship i.e. A user can have one or more posts. In the post table, you'll have these columns. Posts id - primary key user_id //this refers to the id on the user. text pictures In the users table, you'll have. Users id - primary key name The work of foreign key is to reference that a particular user is the owner of certain post(s). When you insert values into the posts, it'll be in this format: Posts id - 1 //the ID of the post also the primary //in posts table user_id - 1 //this is the primary key on users // table which refers to the user of //id 1, but in posts table here it is // called the foreign key. text - "SQL is SQL" pictures - "/pictures/sql.jpg" So now, a user has a post. There's a bit more to this though. 1 Like |
Re: Please I Don't Really Understand Foreign Keys In MySQL by CenturyCoder: 3:03pm On Jul 09, 2020 |
Karleb: Thanks a lot for your help 1 Like |
Re: Please I Don't Really Understand Foreign Keys In MySQL by CenturyCoder: 3:05pm On Jul 09, 2020 |
Karleb: Is there an website where I can visit to understand more or maybe a YouTube video |
Re: Please I Don't Really Understand Foreign Keys In MySQL by nwele2017: 3:38pm On Jul 09, 2020 |
Karleb:this your explanation is quite confusing. A primary key is unique key while a foreign key is not a unique key. A foreign key of one table is used to reference a primary of another table when there is reletionship between this table. This kind of relationship is called one to many relationship. for example, taking from Data analysis perspective, if we have two tables, customers table and sales table. The customers id column in the customers table shows the unique customers that make purchase everyday, this column has a unique entries (no entry appears twice) and it's called the primary of the costumers table. now in the sales table we might have a column called customer id or any other thing ,not necessarily costumer id, with entries that are not unique, which can be used to match up the customer id column of the customers table, this column is called the foreign key. The foreign key is not unique. Its entries appear many times in a column. For you to better understand it well, just imagine that we have 3 customers in our online store database, and we give them customer id of 1,2,3. This customer id will be unique in the customers table but in the sales table of the customers id this numbers will appaer many times since a customer makes transaction many times, customer 3 might make 4 transactions in a day and in the costumers Id column it appear like this 3,3,3,3. so are 1 Like |
Re: Please I Don't Really Understand Foreign Keys In MySQL by Karleb(m): 6:06pm On Jul 09, 2020 |
CenturyCoder: Personally, I prefer www.w3schools.com because their explanations is always to the point, but then you can always Google search any question you don't understand. But I believe the best way to learn is to practice. Model Model relationships such a has one, has many, belongs To Many will demystify everything. |
Re: Please I Don't Really Understand Foreign Keys In MySQL by offdavidsnr(m): 1:40pm On Jul 11, 2020 |
Its Matching one Model to another Model thats the easiest explanation i build Django apps with MySQL |
Re: Please I Don't Really Understand Foreign Keys In MySQL by Ayemileto(m): 10:18pm On Jul 11, 2020 |
It's very simple. Take English Premier League for example Currently we have 20 clubs in the league, and each club plays a certain number of games. Now, if you are to represent this in an Application, you will have at least 2 tables as follows: 1. Fixtures 2. Results Now, ordinarily, you will want to attach each club details to their fixtures and results. But this will lead to a lot of data repetition across the database. So,to eliminate this data repetition,we can create a third table named club_details Now we can put all the club data in this Club details table, and give each club a Unique numeric ID. This ID is normally called the Primary Key, and it's usually Auto Increment. So, our club_details table can have the following fields: Id club_name stadium So, in the Fixtures and Results table now, we don't need to insert the club details again. All we need to do is just use the ID from club_details table. So, our fixtures table have the following fields game_Id (primary key) home_team_id (refers to the Id of the home team from club details) away_team_id (refers to the Id of the away team from club details) Since both home_team_id and away_team_id refer to the Id in club_details table, both of them can be set as Foreign Keys. So, this is just a basic example of Foreign Keys. But, in the real sense, you don't really need foreign keys for your beginner project. The only major advantage of Foreign Keys is that it keeps your database consistent, by performing checks at the database level before inserting, updating or deleting data. So, by setting Foreign Keys, before inserting a new data in the Fixtures table, the database will check if the ID exists in the club_details table. If the ID does not exist, the insert fails. Meanwhile, if you don't use foreign keys, it will just perform the insert without checking, even if the ID does not exist. This also applies to Updating and Deleting data. Personally, I don't use Foreign Keys in my projects except I'm working with a framework, as most frameworks (like Laravel and Django) also use it for Data relationships. |
Re: Please I Don't Really Understand Foreign Keys In MySQL by CenturyCoder: 4:02pm On Jul 12, 2020 |
I just want to say thank you to all who contributed to this page, thanks a lot |
Re: Please I Don't Really Understand Foreign Keys In MySQL by devbits: 10:58am On Jul 16, 2020 |
. |
(1) (Reply)
Please Help! What Do I Learn To Build Something Like Paystack? / What Type Of Letter Should I Write For Business Proposals / Discussing Facebook Outage
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 32 |