Simple Ways to Choose a Primary Key in a Database: 4 Steps (2024)

  • Categories
  • Computers and Electronics
  • Software
  • Office
  • Spreadsheets

Download Article

Explore this Article

Steps

Steps

Other Sections

Related Articles

References

Article Summary

Written byNicole Levine, MFA

Last Updated: October 25, 2023

Download Article

A primary key is a column in a relational database that's used to uniquely identify each record in a table. When selecting a primary key, you'll need to choose a column that contains unique values for all rows. Every single row must have a value in the primary key column, and the values must not change. If you can't find a natural primary key that fits all of the rules, you could create a surrogate key. This wikiHow teaches you how to select a primary key for your database.

Steps

Download Article

  1. 1

    Ensure all records in the primary key are unique. This means you'll want to choose a column that contains some sort of unique identifier that never repeats in other rows. If a column contains any values that are identical for more than one row, it should not be your primary key.[1]

    • For example, if you are building a database of employees and each employee has a unique employee number, you could use the column that contains the employee ID number as your primary key—however, you should only do this if there's no chance the same ID will be re-used in the future.[2]
    • You may want to consider using a composite key, which is a primary key that uses multiple columns. Combining more than one column into your key (e.g., combining DateofBirth, CountryofOrigin, and EmployeeID) reduces the chance of duplicate entries.
  2. 2

    Choose a value that will not change. Once you assign a column as the primary key, you can't change any of the values in that column. Choose something static—something you know you will never need to update.[3]

    • For example, in our employee ID example, you'd only want to use the employee ID column as the primary ID if there's no chance the employee will ever be assigned a different employee ID.
    • Other pieces of information that can change are peoples' names, addresses, phone numbers, and addresses. Avoid these when selecting a primary key.

    Advertisem*nt

  3. 3

    There can't be any null values in the primary key. Every row must have an identifier—there can be no null values the primary key column for any row.

    • For example, let's say you're creating a database containing patient information. Because social security numbers are unique and don't change, such a column might seem like a good candidate for a private key. However, you'd need a social security number entry for all patients—if a patient hasn't provided one, you won't be able to add them to the table if that column is your primary key.
  4. 4

    Consider using a surrogate key. A natural key is a key that contains real data, such as a social security number or employee ID number—all of our previous examples are natural keys. It can be hard to find something that meets all of the aforementioned qualifiers! If you can't identify a column that would work as a (natural) primary key, try a surrogate key:

    • A surrogate key contains unique values that are generated when new records are entered. To use a surrogate key, you'd need to create a new column that doesn't reflect any real data—for example, if you have a list of customers, you might create a new column called CustomerID that you'll use exclusively as a unique database identifier for each customer.
    • In the CustomerID example, each time you enter a new customer into your database, you'd assign them a new CustomerID that would serve as their permanent unique identifier. You could use a number generator, or just add 1 to the previous value to make a unique new customer ID.
  5. Advertisem*nt

Expert Q&A

Ask a Question

200 characters left

Include your email address to get a message when this question is answered.

Submit

    Advertisem*nt

    Tips

    Submit a Tip

    All tip submissions are carefully reviewed before being published

    Name

    Please provide your name and last initial

    Submit

    Thanks for submitting a tip for review!

    You Might Also Like

    4 Easy Ways to Add the Time and Date Automatically in SheetsHow to Make a Google Spreadsheet: Beginners Guide
    The 3 Easiest Ways to Sort by Number in Google SheetsHow to Use Google Sheets: The Ultimate Guide for Getting StartedHow toAdd Rows to a Pivot Table3 Easy Ways to Get Rid of Dotted Lines in Microsoft ExcelHow to Add Custom Calculated Fields to Pivot Tables in ExcelHow toAdd a Row to a Table in ExcelHow toGraph Multiple Lines in Excel3 Simple Ways to Create a Shared SpreadsheetHow to Change the X Axis Scale in an Excel ChartHow toAdd Two Cells Containing the Sum of Other Formulas in Excel

    Advertisem*nt

    About This Article

    Simple Ways to Choose a Primary Key in a Database: 4 Steps (22)

    Written by:

    Nicole Levine, MFA

    wikiHow Technology Writer

    This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions. This article has been viewed 25,443 times.

    How helpful is this?

    Co-authors: 5

    Updated: October 25, 2023

    Views:25,443

    Categories: Spreadsheets

    Article SummaryX

    1.Each primary key record must be unique.
    2.The values in the primary key can never change.
    3.No null values can exist in the primary key.
    4.If you can't select a primary key that meets this criteria, create a surrogate key.

    Did this summary help you?

    • Print
    • Send fan mail to authors

    Thanks to all authors for creating a page that has been read 25,443 times.

    Is this article up to date?

    Advertisem*nt

    Simple Ways to Choose a Primary Key in a Database: 4 Steps (2024)
    Top Articles
    Are Hot Tubs Good for Inflammation? - Hydropool Mississauga
    JEPQ vs. VOO — ETF comparison tool
    What Is Single Sign-on (SSO)? Meaning and How It Works? | Fortinet
    St Thomas Usvi Craigslist
    Whas Golf Card
    Tattoo Shops Lansing Il
    Methstreams Boxing Stream
    Jonathon Kinchen Net Worth
    Federal Fusion 308 165 Grain Ballistics Chart
    The Realcaca Girl Leaked
    Pickswise the Free Sports Handicapping Service 2023
    Midway Antique Mall Consignor Access
    Amateur Lesbian Spanking
    Craigslist Chautauqua Ny
    Beau John Maloney Houston Tx
    104 Whiley Road Lancaster Ohio
    Becu Turbotax Discount Code
    iOS 18 Hadir, Tapi Mana Fitur AI Apple?
    Quest Beyondtrustcloud.com
    24 Best Things To Do in Great Yarmouth Norfolk
    Finger Lakes Ny Craigslist
    97226 Zip Code
    Dover Nh Power Outage
    Two Babies One Fox Full Comic Pdf
    Wisconsin Volleyball Team Boobs Uncensored
    Mythical Escapee Of Crete
    Обзор Joxi: Что это такое? Отзывы, аналоги, сайт и инструкции | APS
    FAQ's - KidCheck
    Top 20 scariest Roblox games
    Ou Football Brainiacs
    Tactical Masters Price Guide
    Stickley Furniture
    Turns As A Jetliner Crossword Clue
    HP PARTSURFER - spare part search portal
    My Reading Manga Gay
    Craftsman Yt3000 Oil Capacity
    Otis Offender Michigan
    Lehpiht Shop
    Agematch Com Member Login
    Domino's Delivery Pizza
    Yogu Cheshire
    Ursula Creed Datasheet
    The Attleboro Sun Chronicle Obituaries
    Pink Runtz Strain, The Ultimate Guide
    What Is The Optavia Diet—And How Does It Work?
    Congruent Triangles Coloring Activity Dinosaur Answer Key
    Abigail Cordova Murder
    Ihop Deliver
    Uno Grade Scale
    Morbid Ash And Annie Drew
    Round Yellow Adderall
    Tamilblasters.wu
    Latest Posts
    Article information

    Author: Domingo Moore

    Last Updated:

    Views: 6238

    Rating: 4.2 / 5 (73 voted)

    Reviews: 88% of readers found this page helpful

    Author information

    Name: Domingo Moore

    Birthday: 1997-05-20

    Address: 6485 Kohler Route, Antonioton, VT 77375-0299

    Phone: +3213869077934

    Job: Sales Analyst

    Hobby: Kayaking, Roller skating, Cabaret, Rugby, Homebrewing, Creative writing, amateur radio

    Introduction: My name is Domingo Moore, I am a attractive, gorgeous, funny, jolly, spotless, nice, fantastic person who loves writing and wants to share my knowledge and understanding with you.