Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6029539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:54:05+00:00 2026-05-23T04:54:05+00:00

I came across the following SQL in a book: CREATE TABLE ‘categories'( id SMALLINT

  • 0

I came across the following SQL in a book:

CREATE TABLE 'categories'(
id SMALLINT NOT NULL AUTO INCREMENT,
category VARCHAR(30) NOT NULL,
PRIMARY KEY('id'),
UNIQUE KEY 'category'('category')
)ENGINE=MyISAM DEFAULT CHARSET = utf8;

I was wondering is there a reason why I would need a PRIMARY and UNIQUE KEY in the same table? I guess, underlying that question is, what is the difference between PRIMARY and UNIQUE keys?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:54:06+00:00Added an answer on May 23, 2026 at 4:54 am

    The relational model says there’s no essential difference between one key and another. That is, when a relation has more than one candidate key, there are no theoretical reasons for declaring that this key is more important than that key. Essentially, that means there’s no theoretical reason for identifying one key as a primary key, and all the others as secondary keys. (There might be practical reasons, though.)

    Many relations have more than one candidate key. For example, a relation of US states might have data like this.

    State      Abbr      Postal Code
    --
    Alabama    Ala.      AL
    Alaska     Alaska    AK
    Arizona    Ariz.     AZ
    ...
    Wyoming    Wyo.      WY
    

    It’s clear that values in each of those three columns are unique–there are three candidate keys.

    If you were going to build a table in SQL to store those values, you might do it like this.

    CREATE TABLE states (
      state varchar(15) primary key,
      abbr varchar(10) not null unique,
      postal_code char(2) not null unique
    );
    

    And you’d do something like that because SQL doesn’t have any other way to say “My table has three separate candidate keys.”

    I didn’t have any particular reason for choosing “state” as the primary key. I could have just as easily chosen “abbr” or “postal_code”. Any of those three columns can be used as the target for a foreign key reference, too.

    And as far as that goes, I could have built the table like this, too.

    CREATE TABLE states (
      state varchar(15) not null unique,
      abbr varchar(10) not null unique,
      postal_code char(2) not null unique
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to optimize SQL queries in Akonadi and came across the following problem
Recently I came across with the following quiz. Imagine we have this table +--------+
I came across a rather strange problem with linq-to-sql. In the following example, var
I came across the following markup in a JSP file in a legacy app
I came across the following expression in someone else's code. I think it's terrible
I came across the following regular expression below on one of my code, anyone
I came across the following code that compiles fine (using Visual Studio 2005): SomeObject
I came across the following weird chunk of code.Imagine you have the following typedef:
I came across the following code in a code base I am working on:
I came across the following http://ckeditor.com/demo , and was wondering if anyone had a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.