Solved

MySQL Database Unique Primary Keys?

Posted on 2008-06-19
2
455 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:Kdo
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

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…
I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
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 …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

777 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