Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

MySQL Database Unique Primary Keys?

Posted on 2008-06-19
2
457 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 45

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 50 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

789 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