[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MySQL Database Unique Primary Keys?

Posted on 2008-06-19
2
Medium Priority
?
463 Views
Last Modified: 2012-05-05
Hi, just wanted to know if a primary key can have duplicate values?

I have one table (courses) which has an 'id' field.

Then i have another table (bookings) where i want to store various bookings for each course, but need to store the same 'id' so they can be linked.

Can this be done?
Thanks
0
Comment
Question by:zollgut
2 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 21827427
A primary key is, by definition, a unique key.

Kent
0
 
LVL 16

Accepted Solution

by:
DrNikon224 earned 200 total points
ID: 21827897
You're looking for a "foreign key". A primary key stores unique identifiers for data stored in table1, while a foreign key stores unique identifiers linking each record in table1 to data stored in table2. You've got all of your data in the courses table, along with their unique IDs. And you've got another table called bookings. There should be a field in the bookings table, called something courseID, which will store the ID of the course being booked in each record. The bookings will have their own primary key, unrelated to the id of the course, and the courseID is a separate field. You would then retrieve the data from the tables by joining the tables in a query:

SELECT courses.field1, courses.field2, bookings.field1, bookings.field2 FROM courses, bookings WHERE courses.id = bookings.courseID

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month20 days, 11 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question