MySQL Primary Key: Basics, Design & Error Fixes

目次

1. Introduction

In database design, the “primary key (PRIMARY KEY)” is an essential concept. A primary key plays a crucial role in guaranteeing the uniqueness of each record and serves as the foundation for efficient data management and integrity. This article provides a detailed explanation of primary keys in MySQL, covering basics, how to set them, troubleshooting errors, and design tips. It aims to be useful for a wide range of users, from beginners to intermediate. By the time you finish this guide, you will be able to:
  • Understand the basic concept of primary keys.
  • Set primary keys and resolve related errors.
  • Choose the optimal primary key for database design.
Now, let’s start learning the basics of primary keys.

2. What is a Primary Key (PRIMARY KEY)?

In a database, a “primary key (PRIMARY KEY)” is a type of key used within a table to guarantee uniqueness. A primary key functions as an identifier for data, uniquely identifying each row.

Primary Key Features

  1. Ensures Uniqueness The values in columns set as a primary key cannot be duplicated within the table.
  2. Disallows NULL Values A primary key must always have a value and does not permit NULL.
  3. Only One Per Table Each table can have only one primary key; you cannot set multiple primary keys. However, a “composite primary key” that combines multiple columns into a single primary key is possible.

Why a Primary Key Is Needed

Setting a primary key provides the following benefits:
  • Ensures Data Integrity: Prevents duplicate entries of the same data.
  • Efficient Queries: Improves efficiency when searching, updating, or deleting data.
  • Manages Data Relationships: Serves as the reference point when using foreign keys.
For example, in a table that manages user information, setting “User ID” as the primary key allows each user to be uniquely identified.

3. How to Set Primary Keys in MySQL

In MySQL, you can easily set a primary key (PRIMARY KEY). Below, we explain how to set a primary key when creating a new table and how to add a primary key to an existing table, with concrete examples.

Setting a Primary Key When Creating a New Table

When creating a new table, you use the CREATE TABLE statement to set a primary key. Columns that define the primary key must be given the NOT NULL constraint.

Basic Example

In the following example, we create a table called users and set the id column as the primary key.
CREATE TABLE users (
    id INT NOT NULL AUTO_INCREMENT,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(100),
    PRIMARY KEY (id)
);
Explanation
  • The id column is set with AUTO_INCREMENT, which automatically generates sequential unique values.
  • PRIMARY KEY (id) designates the id column as the primary key.

Setting Multiple Columns as a Primary Key (Composite Primary Key)

By setting a composite primary key, you can guarantee uniqueness through a combination of multiple columns.

Example: Setting a Composite Primary Key

CREATE TABLE orders (
    order_id INT NOT NULL,
    product_id INT NOT NULL,
    quantity INT,
    PRIMARY KEY (order_id, product_id)
);
Explanation
  • The combination of order_id and product_id is designated as the primary key.
  • You can insert rows with the same order_id as long as they have different product_id values.

How to Add a Primary Key to an Existing Table

If you need to add a primary key to an existing table later, use the ALTER TABLE statement.

Example: Adding a Primary Key to a Single Column

The following is an example of adding the id column as a primary key to an existing users table.
ALTER TABLE users ADD PRIMARY KEY (id);

Example: Adding a Composite Primary Key

The same applies when setting a primary key by combining multiple columns.
ALTER TABLE orders ADD PRIMARY KEY (order_id, product_id);

How to Remove a Primary Key

To drop a primary key, use the ALTER TABLE statement as follows.
ALTER TABLE users DROP PRIMARY KEY;
Note
  • Dropping a primary key also removes the index created based on it, so proceed with caution.

Using AUTO_INCREMENT

A common feature used together with primary keys is AUTO_INCREMENT. When this is set, the value automatically increments each time a row is inserted.

Example: Primary Key Using AUTO_INCREMENT

CREATE TABLE customers (
    customer_id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(100),
    PRIMARY KEY (customer_id)
);

4. Using Composite Primary Keys

Composite primary keys combine multiple columns into a single primary key to guarantee data uniqueness. They are especially useful when a single column cannot ensure uniqueness or when data must be identified according to specific business rules.

Basic Concept of Composite Primary Keys

Using a composite primary key ensures that the combination of values from two or more columns is unique. For example, in a table that manages order data, combining the order ID (order_id) and product ID (product_id) uniquely identifies each product within an order.

How to Define a Composite Primary Key

Setting It When Creating a New Table

Below is an example of defining a composite primary key on the orders table.
CREATE TABLE orders (
    order_id INT NOT NULL,
    product_id INT NOT NULL,
    quantity INT,
    PRIMARY KEY (order_id, product_id)
);
Explanation
  • The primary key is defined by combining order_id and product_id.
  • Even with the same order_id, records with different product_id values can be inserted.

Adding a Composite Primary Key to an Existing Table

To add a composite primary key to an existing table, use an ALTER TABLE statement as shown below.
ALTER TABLE orders ADD PRIMARY KEY (order_id, product_id);

Benefits of Composite Primary Keys

  1. Guarantees Uniqueness
  • Combining multiple columns ensures uniqueness.
  1. Fits Business Rules
  • For instance, when managing order information, order_id alone may not guarantee uniqueness, but combining order_id with product_id maintains data integrity.
  1. Simplifies Data Management
  • Using a composite primary key prevents duplicate data and keeps the data structure simple.

Considerations for Composite Primary Keys

  1. Performance Impact
  • Composite primary keys require comparing multiple columns during searches and inserts, which can be more costly than a single-column primary key. This is especially important in large databases.
  1. Index Usage Limitations
  • When a composite primary key is defined, the automatically generated index covers multiple columns. If you need an index on an individual column, you must create a separate index.
  1. Increased Design Complexity
  • Combining multiple columns can make the schema more complex. Careful column selection is essential.

When to Use Composite Primary Keys

  • When uniqueness must be managed across multiple conditions Example: order ID and product ID, or project ID and task ID, etc.
  • When a single column cannot guarantee uniqueness Example: a customer purchases multiple products; combine customer ID with purchase date to manage uniqueness.

5. Differences Between Primary Keys and Indexes

Primary keys (PRIMARY KEY) and indexes (INDEX) are important concepts in MySQL, but their roles and purposes differ. This section explains the differences between primary keys and indexes and provides a detailed description of their characteristics and usage.

Features of Primary Keys (PRIMARY KEY)

  1. Guarantee of Uniqueness
  • A primary key uniquely identifies each row in a table.
  • It does not allow duplicates or NULL values.
  1. Ensuring Data Integrity
  • Setting a primary key maintains table consistency and data integrity.
  1. Automatic Index Creation
  • When a primary key is defined, an index is automatically created, improving the efficiency of searches and data operations.

Example of a Primary Key

The following is an example of setting the id column as a primary key.
CREATE TABLE users (
    id INT NOT NULL AUTO_INCREMENT,
    username VARCHAR(50),
    PRIMARY KEY (id)
);

Features of Indexes (INDEX)

  1. Improved Search Speed
  • Indexes speed up data retrieval and query execution.
  1. Does Not Guarantee Uniqueness
  • Regular indexes do not guarantee uniqueness (although unique indexes do).
  1. Can Be Applied to Any Column
  • Indexes can be created on columns other than the primary key to optimize specific queries.

Example of an Index

The following adds an index to the username column.
CREATE INDEX idx_username ON users (username);

Differences Between Primary Keys and Indexes

FeaturePrimary Key (PRIMARY KEY)Index (INDEX)
Whether it guarantees uniquenessYesNo (except for unique indexes)
Whether NULL values are allowedNot allowedAllowed
Whether it is generated automaticallyAn index is automatically created when a primary key is definedMust be created manually
Number of columns that can be setOne per tableMultiple allowed

Relationship Between Primary Keys and Indexes

  • Primary keys automatically have an index When a primary key is defined, a unique index is automatically created. Consequently, the primary key column can be searched quickly.
  • Indexes can also be set on non-primary-key columns Creating indexes on columns other than the primary key can improve the performance of specific queries.

Example: Using Both Primary Keys and Indexes

The following example defines a primary key and an index on another column.
CREATE TABLE products (
    product_id INT NOT NULL AUTO_INCREMENT,
    product_name VARCHAR(100),
    category_id INT,
    PRIMARY KEY (product_id),
    INDEX idx_category (category_id)
);
Explanation:
  • product_id is the primary key, guaranteeing uniqueness and disallowing NULLs.
  • category_id has an index, speeding up product searches by category.

Key Points for Choosing Between Primary Keys and Indexes

  1. Primary Key Selection
  • Set the column that uniquely identifies the data as the primary key.
  • Each table must have exactly one primary key.
  1. Utilizing Indexes
  • If a non-primary-key column is frequently used in search conditions, create an index on it.
  • Examples: columns like username or email that are often used in queries.
  1. Considerations
  • Setting too many indexes can degrade performance during data insertion and updates. It is recommended to create only the necessary indexes.

6. Primary Key Considerations

Primary key (PRIMARY KEY) plays a very important role in database design, but if it is not designed and managed correctly, performance and integrity issues can arise. This section explains the considerations and best practices for primary keys.

Considerations for Deleting or Modifying Primary Keys

How to Delete a Primary Key

To delete a primary key, use the ALTER TABLE statement.
ALTER TABLE users DROP PRIMARY KEY;
Note
  1. When a primary key is deleted, the indexes created based on it are also removed.
  2. If there are foreign key constraints that depend on the primary key, the delete operation will fail. You need to drop those foreign key constraints beforehand.

How to Modify a Primary Key

To change an existing primary key, follow these steps:
  1. Drop the current primary key.
  2. Add the new primary key.
Example:
ALTER TABLE users DROP PRIMARY KEY;
ALTER TABLE users ADD PRIMARY KEY (username);

Considerations for Choosing a Primary Key

  1. Choose an appropriate column
  • Select a column that can guarantee uniqueness for the primary key.
  • Common choices:
    • Numeric type with AUTO_INCREMENT (e.g., id)
    • Natural keys (e.g., employee numbers or product codes)
  1. Avoid columns that are frequently updated
  • Frequently updating the value of a column used as a primary key can negatively affect performance.
  1. Avoid long strings
  • Using long strings (e.g., a 200-character VARCHAR) as a primary key reduces the efficiency of searches and operations.
  1. Avoid columns that can contain NULL values
  • Since primary keys cannot contain NULL values, you must choose a column that always has a value.

Errors When Setting Primary Keys and How to Resolve Them

  1. Error: ERROR 1068 (42000): Multiple primary key defined
  • Cause
    • Occurs when attempting to define multiple primary keys.
  • Solution
    • Delete the duplicate primary key definition. sql ALTER TABLE users DROP PRIMARY KEY; ALTER TABLE users ADD PRIMARY KEY (id);
  1. Error: ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
  • Cause
    • Occurs when a foreign key constraint does not match the primary key.
  • Solution
    • Make the data types and constraints of the primary key and foreign key match.
  1. Error: ERROR 1265 (01000): Data truncated for column
  • Cause
    • Occurs when the data assigned to the primary key exceeds the expected type or length.
  • Solution
    • Adjust the data type of the primary key column appropriately, or correct the inserted data.

Best Practices for Setting Primary Keys

  1. Prefer a single column
  • Using a single-column primary key (e.g., a numeric type with AUTO_INCREMENT) enables a simple and efficient design.
  1. Appropriate use of composite primary keys
  • Combining multiple columns should be done to keep the number of columns to the minimum necessary.
  1. Optimize the data type of primary key columns
  • Choose numeric types (e.g., INT) or short strings (e.g., VARCHAR(50)) to improve performance.
  1. Maintain data consistency
  • Perform appropriate input data validation on primary key columns to maintain uniqueness and integrity.

7. Best Practices for Designing Primary Keys

In database design, proper design of the primary key (PRIMARY KEY) directly impacts data consistency and performance. This section explains best practices for designing primary keys effectively.

Natural Key vs. Surrogate Key: Which Should You Choose?

What Is a Natural Key?

A natural key uses existing business data as the primary key. Example: employee number, product code, email address, etc. Advantages:
  • The meaning of the data is intuitive.
  • It aligns easily with existing systems and business rules.
Disadvantages:
  • If it includes long strings or frequently changing values, performance can be affected.
  • There is a risk of duplicate data.

What Is a Surrogate Key?

A surrogate key is a unique identifier generated within the database. Example: a numeric ID using AUTO_INCREMENT. Advantages:
  • Uniqueness is always guaranteed.
  • Searches and updates are efficient.
  • There is no risk of the value changing.
Disadvantages:
  • Because the value has no meaning to humans, it does not help with data understanding.
Which Should You Choose?
  • Surrogate key recommended: In most cases, using a surrogate key (e.g., a numeric ID with AUTO_INCREMENT) is the best choice.
  • If you choose a natural key: Only when business rules require a natural key and data integrity is guaranteed.

Performance Optimization for Primary Keys

  1. Use numeric types
  • Using numeric types such as INT or BIGINT for primary keys speeds up search and comparison operations.
  • Example: sql CREATE TABLE customers ( customer_id INT NOT NULL AUTO_INCREMENT, name VARCHAR(100), PRIMARY KEY (customer_id) );
  1. Keep the size minimal
  • Keeping primary keys small reduces storage usage and improves index performance.
  • Example: INT (4 bytes) is common, while BIGINT (8 bytes) is reserved for very large datasets.
  1. Avoid updating primary keys
  • Updating a primary key also updates related indexes and foreign key constraints, which can cause significant performance degradation.

Designing Primary Keys and Foreign Keys

  • Primary keys are often referenced by foreign keys (FOREIGN KEY). Therefore, when designing foreign key constraints, the uniqueness and integrity of the primary key are crucial.

Example: Primary Key and Foreign Key Design

CREATE TABLE customers (
    customer_id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(100),
    PRIMARY KEY (customer_id)
);

CREATE TABLE orders (
    order_id INT NOT NULL AUTO_INCREMENT,
    customer_id INT NOT NULL,
    order_date DATE,
    PRIMARY KEY (order_id),
    FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
Explanation:
  • The customer_id column in the orders table references the customer_id column in the customers table.
  • If primary key integrity is not maintained, foreign key constraints can trigger errors.

Primary Key Design Checklist

  1. Is the primary key column guaranteed to be unique?
  2. Does the primary key avoid long strings or columns that change frequently?
  3. Have you considered whether a surrogate key is more appropriate than a natural key?
  4. Is the data type of the primary key column appropriate (e.g., INT, BIGINT)?
  5. If using a composite primary key, have you minimized the number of columns?
Designing primary keys properly improves database performance and helps maintain data integrity. In the next section, “8. FAQ (Frequently Asked Questions)”, we will answer common questions readers may have.

8. FAQ (Frequently Asked Questions)

Here we answer common questions about MySQL primary keys (PRIMARY KEY). We explain the points that beginners to intermediate users often wonder about in an easy-to-understand way.

What is the difference between a primary key and a unique key?

Primary Key (PRIMARY KEY):
  • Uniquely identifies each row.
  • Only one can be defined per table.
  • Does not allow NULL values.
Unique Key (UNIQUE KEY):
  • Ensures each row’s values are unique, and multiple unique keys can be defined per table.
  • Allows NULL values (though NULLs are treated as distinct).
Example:
CREATE TABLE example (
    id INT NOT NULL AUTO_INCREMENT,
    email VARCHAR(100) UNIQUE,
    username VARCHAR(50),
    PRIMARY KEY (id)
);
  • id column is the primary key.
  • email column is a unique key.

What happens if you don’t define a primary key?

If no primary key is defined:
  1. Data uniqueness cannot be guaranteed.
  2. Foreign key constraints cannot be set.
  3. Table data queries and operations may become inefficient.
Recommendation:
  • It is recommended to define a primary key for every table.

When should you use a composite primary key?

A composite primary key is used when you need to guarantee uniqueness by combining multiple columns. Example:
  • When managing order data, ensure uniqueness with the combination of order_id and product_id.
CREATE TABLE orders (
    order_id INT,
    product_id INT,
    quantity INT,
    PRIMARY KEY (order_id, product_id)
);
Note:
  • Composite primary keys can complicate design, so they should be considered carefully.

Is AUTO_INCREMENT required?

It is not required, but it is useful in situations such as:
  • When you want the column used as a primary key to be generated automatically.
  • When you need a numeric primary key that guarantees uniqueness.
Example:
CREATE TABLE users (
    user_id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(100),
    PRIMARY KEY (user_id)
);

How can you change a primary key value?

While you can change a primary key value directly, you need to be aware of the following:
  1. If foreign key constraints exist, related data may be affected.
  2. The update cost can be high, potentially impacting performance.
Example:
UPDATE users SET user_id = 10 WHERE user_id = 1;

What impact does a primary key have on performance?

Advantages:
  • Primary keys automatically receive an index, speeding up data retrieval and sorting.
Considerations:
  • If the data type or length of the primary key column is not appropriate, performance may degrade.

What is the optimal data type for a primary key?

  • Numeric types (INT or BIGINT) are most recommended.
  • Compact and fast for searching.
  • Can be used with AUTO_INCREMENT.
  • If using strings, VARCHAR is recommended, but keeping the length short is important.

Can you define multiple primary keys?

A table can have only one primary key. However, you can combine multiple columns to create a composite primary key.

How to handle errors when deleting or modifying a primary key?

Common error:
  • Cannot delete or modify the primary key due to foreign key constraints.
Solution:
  1. Drop the foreign key constraints.
  2. After changing the primary key, re-add the foreign key constraints.
Example:
ALTER TABLE orders DROP FOREIGN KEY fk_customer_id;
ALTER TABLE orders DROP PRIMARY KEY;
ALTER TABLE orders ADD PRIMARY KEY (new_id);

9. Summary

In this article, we provided a detailed explanation of the basics of primary keys (PRIMARY KEY) in MySQL, including how to set them, considerations, best practices, and FAQs. A primary key is an essential element in database design, and its proper configuration has a significant impact on data uniqueness and performance.

Revisiting the Importance of Primary Keys

  1. Ensures Data Uniqueness A primary key uniquely identifies each row in a table, preventing duplicate data.
  2. Improves Data Retrieval and Manipulation Efficiency Because a primary key automatically receives an index, searches and data operations are accelerated.
  3. Foundation for Foreign Key Constraints Setting a primary key enables the use of foreign key constraints to manage data integrity.

Key Points to Consider During Design

  • Choose Appropriate Columns for the Primary Key It is important to select columns that guarantee uniqueness and are not frequently changed as the primary key.
  • Use Composite Primary Keys Cautiously Only use composite primary keys when necessary, and avoid making the design overly complex.
  • Select Appropriate Data Types Using numeric types such as INT or BIGINT for primary keys helps achieve efficient performance.

Future Learning Topics

Once you have a solid understanding of primary keys, studying the following topics will enable even more effective database design.
  1. Setting Up Foreign Keys (FOREIGN KEY) Learn how to establish relationships between data and maintain integrity.
  2. Index Optimization Gain the skills to improve query performance by adding indexes to columns other than the primary key.
  3. Normalization and Denormalization Consider normalization, the foundation of database design, and denormalization for performance improvements.

Conclusion

Designing MySQL primary keys correctly is the first step toward improving the overall efficiency and reliability of your database. Use this article as a reference to hone your skills in setting and managing appropriate primary keys. When primary key design is solid, database construction and operation become smoother, leading to increased overall productivity. This concludes the article. Please refer to it and try applying effective primary key design in your actual projects.