Solved

MySQL Database Unique Primary Keys?

Posted on 2008-06-19
2
458 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
[X]
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
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
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 …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 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