- Primary key: A column (or set of columns) that uniquely identifies each row in a table. It ensures data integrity by preventing duplicate and null values.
- Foreign key: A column (or set of columns) in one table that refers to the primary key in another table. It establishes a relationship between two tables.
What is a primary key in DBMS?
A primary key is like a unique identifier for every row in a database table. It ensures that each record is distinct and helps maintain data integrity. Think of it as the ID card of your data.
What is a primary key?
A primary key is a column (or combination of columns) that uniquely identifies each row in a database table. Every table should have a primary key to keep the data organized.
Let’s look at an example of a simple Students table:
Students
StudentID (Primary Key) | Name | Age |
1 | Alice | 22 |
2 | Smith | 20 |
3 | Charlie | 23 |
In this table:
The
StudentIDcolumn is the primary key.Each student has a unique
StudentID, ensuring there are no duplicates.
Understanding the purpose of a primary key, but how do we implement one in a database? Let’s look at the syntax and examples of creating a primary key.
Syntax for creating a primary key
We can define a primary key when creating a table or add it to an existing table.
Example 1: Creating a table with a primary key
CREATE TABLE Students (StudentID INT PRIMARY KEY, -- Defines StudentID as the primary keyName VARCHAR(100), -- Column for storing student namesAge INT -- Column for storing student ages);
Here, the
StudentIDis declared as the primary key.This ensures no two students can have the same
StudentID.
If we insert data into the table:
CREATE TABLE Students (StudentID INT PRIMARY KEY,Name VARCHAR(50),Age INT);INSERT INTO Students (StudentID, Name, Age)VALUES (1, 'Alice', 22),(2, 'Bob ', 20);select * from Students; # Printing the student record
The table will look like this:
Students
StudentID (Primary Key) | Name | Age |
1 | Alice | 22 |
2 | Bob | 20 |
Try inserting two rows with the same StudentID to see what happens.
When you execute this query, the database will throw an error similar to:
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
This happens because the primary key enforces the rule that each value in the StudentID column must be unique. By trying to insert a duplicate StudentID, the database prevents the operation, ensuring data integrity.
Example 2: Adding a primary key to an existing table
If the table is already created without a primary key, we can add it later:
ALTER TABLE StudentsADD PRIMARY KEY (StudentID); -- Adds a primary key constraint to the StudentID column
While primary keys are used to maintain data integrity, there may be situations where you need to remove them. Let’s see how to do this.
Syntax for deleting a primary key
We can remove the primary key if we don’t need it anymore.
Example:
ALTER TABLE StudentsDROP PRIMARY KEY; -- Removes the primary key constraint from the table
This removes the primary key constraint from the
StudentIDcolumn.Be careful when doing this, as it can lead to duplicate or empty values in the column.
Now that we know how to create and remove a primary key, let’s explore its key characteristics.
Properties of a primary key
Unique values: No two rows can have the same value in the primary key column.
Students
StudentID | Name |
1 | Alice |
2 | Bob |
Duplicate StudentID values are not allowed. |
Not Null: A primary key column cannot have empty (null) values. For example, If
StudentIDis left blank, the database will throw an error.Unchanging: The primary key values should not change frequently to maintain consistency.
Single or Composite: A primary key can be a single column or a combination of columns (
).composite key A composite key is a primary key made up of two or more columns. For example, in a table of course enrollments, the combination of StudentID and CourseID can uniquely identify each record.
Now that we’ve covered the properties of primary keys, let’s evaluate their advantages and limitations to understand their role in database management.
Pros and Cons of a Primary Key
Pros | Cons |
This ensures data integrity and Prevents duplicate or null values. | Once set, primary keys are hard to modify. |
When you set a primary key, the database automatically creates an index on that column to speed up data retrieval. | Managing primary keys in complex systems can be challenging. |
This help in Links tables efficiently. | Indexes created for primary keys can increase storage requirements, especially for large datasets. |
To summarize the importance and functionality of primary keys, here are the key points to remember.
Key takeaways of a primary key
A primary key ensures data integrity by uniquely identifying each row in a table.
We can define a primary key during table creation or add it later.
Primary keys automatically create an index, improving search performance.
A primary key must always have unique and non-null values.
We can use a single column or a combination of columns (composite key) as a primary key.
Ready to learn SQL and become a database expert? Join our Learn SQL course and follow the path to becoming a Database Professional with SQL! Unlock your potential and gain the skills employers seek. Start your journey today!
Frequently asked questions
Haven’t found what you were looking for? Contact Us