Solved

MySQL Database Unique Primary Keys?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now