Skip to content

Modifying an existing table structure in a MySQL database, particularly adding new constraints, is a fundamental task for maintaining data integrity and defining relationships between tables. Properly implemented constraints ensure a robust and error-free database environment. In this guide, we’ll walk through the steps to add different types of constraints using the ALTER TABLE statement in MySQL.

What are constraints in MySQL?

Constraints are rules applied to table columns to ensure data integrity and validity. They play a critical role in maintaining the accuracy and reliability of your database. Key constraints include:

  • PRIMARY KEY: Makes each row in a table uniquely identifiable.
  • FOREIGN KEY: Maintains referential integrity between two related tables.
  • UNIQUE: Ensures unique values in a column.
  • CHECK: Verifies that column values meet a specified condition.
  • NOT NULL: Prevents columns from having NULL values.

Adding a primary key

You can add a primary key constraint to ensure that each row in a table has a unique identifier. This operation requires the column to not contain NULL values.

ALTER TABLE your_table_name
ADD PRIMARY KEY (column_name);

Adding a foreign key

When adding a foreign key, you define the column(s) in one table that will link to the primary key in another table, ensuring data consistency across relationships.

ALTER TABLE child_table
ADD FOREIGN KEY (child_column_name)
REFERENCES parent_table(parent_column_name);

Adding a unique constraint

Implement a unique constraint when you need to guarantee that all values in a specific column or set of columns are distinct from one another.

ALTER TABLE your_table_name
ADD UNIQUE (column_name);

Adding a check constraint

Use a check constraint to ensure that all values in a column satisfy a predetermined condition, enhancing data validation.

ALTER TABLE your_table_name
ADD CONSTRAINT constraint_name
CHECK (column_name condition);

Adding a NOT NULL constraint

To ensure that a column never accepts NULL values, modify the column to enforce a NOT NULL constraint. This change is crucial for maintaining data completeness.

ALTER TABLE your_table_name
MODIFY column_name datatype NOT NULL;

Summary

By adding constraints to your MySQL tables, you actively enforce data rules, enhancing the integrity and consistency of your database. Always ensure to back up your database before making any structural modifications to prevent accidental data loss.

Written by

Robert Cooper avatar

Robert Cooper

Senior Engineer at Basedash

Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.

View full author profile →

Looking for an AI-native BI tool?

Basedash lets you build charts, dashboards, and reports in seconds using all your data.