Hi there, we’re Harisystems

"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".

For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.

1
1

MySQL Types of Keys

Introduction

In MySQL, keys play a vital role in organizing and indexing data within a table. They ensure data integrity, enforce relationships between tables, and improve query performance. In this article, we will explore the different types of keys in MySQL and provide examples to illustrate their usage.

Primary Key

A primary key uniquely identifies each record in a table. It must have a unique value for each row and cannot contain null (empty) values. Here's an example of defining a primary key:

CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(50),
  age INT
);

Foreign Key

A foreign key establishes a relationship between two tables by referencing the primary key of another table. It ensures referential integrity and maintains the relationship between related tables. Here's an example:

CREATE TABLE orders (
  id INT PRIMARY KEY,
  product_id INT,
  customer_id INT,
  FOREIGN KEY (product_id) REFERENCES products(id),
  FOREIGN KEY (customer_id) REFERENCES customers(id)
);

Unique Key

A unique key ensures that each value in a column or a group of columns is unique. Unlike the primary key, it can contain null (empty) values, but the non-null values must be unique. Here's an example:

CREATE TABLE products (
  id INT PRIMARY KEY,
  code VARCHAR(10) UNIQUE,
  name VARCHAR(50)
);

Composite Key

A composite key consists of multiple columns that together uniquely identify each record in a table. This is useful when a single column does not provide a unique identifier. Here's an example:

CREATE TABLE order_items (
  order_id INT,
  product_id INT,
  item_number INT,
  PRIMARY KEY (order_id, item_number),
  FOREIGN KEY (order_id) REFERENCES orders(id),
  FOREIGN KEY (product_id) REFERENCES products(id)
);

Conclusion

Understanding the different types of keys in MySQL is essential for designing effective database schemas and ensuring data integrity. In this article, we explored the primary key, foreign key, unique key, and composite key in MySQL, providing examples to illustrate their usage. By using the appropriate key types, you can establish relationships between tables, enforce data uniqueness, and optimize query performance in your MySQL databases.

4.5L

Learners

20+

Instructors

50+

Courses

6.0L

Course enrollments

4.5/5.0 5(Based on 4265 ratings)

Future Trending Courses

When selecting, a course, Here are a few areas that are expected to be in demand in the future:.

Beginner

The Python Course: Absolute Beginners for strong Fundamentals

By: Sekhar Metla
4.5 (13,245)
Intermediate

JavaScript Masterclass for Beginner to Expert: Bootcamp

By: Sekhar Metla
4.5 (9,300)
Intermediate

Python Coding Intermediate: OOPs, Classes, and Methods

By: Sekhar Metla
(11,145)
Intermediate

Microsoft: SQL Server Bootcamp 2023: Go from Zero to Hero

By: Sekhar Metla
4.5 (7,700)
Excel course

Future Learning for all

If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.

View Courses

Most Popular Course topics

These are the most popular course topics among Software Courses for learners