SQL PRIMARY KEY
The PRIMARY KEY constraint in SQL ensures that each record in a table is uniquely identified. It is a combination of the UNIQUE and NOT NULL constraints, meaning the column(s) defined as a primary key must contain unique values and cannot have NULL entries.
Why Use the PRIMARY KEY Constraint?
The PRIMARY KEY is essential in relational databases to:
- Ensure Unique Identification: Each row in a table can be uniquely identified.
- Prevent Missing Data: Primary key columns cannot contain
NULLvalues. - Support Relationships: Acts as a foundation for connecting tables through foreign keys.
- Optimize Data Retrieval: Facilitates efficient indexing for faster query performance.
Characteristics of PRIMARY KEY
- A table can have only one primary key.
- The primary key can be a single column or a combination of columns (composite key).
- Columns in a primary key are always
NOT NULLby definition.
How to Use the PRIMARY KEY Constraint
The PRIMARY KEY constraint can be defined during table creation or added later using the ALTER TABLE statement.
Adding PRIMARY KEY During Table Creation
Defining a Single-Column Primary Key
1 2 3 4 5CREATE TABLE Students ( StudentID INT PRIMARY KEY, Name VARCHAR(100), EnrollmentDate DATE );
- Ensures the
StudentIDcolumn uniquely identifies each record in theStudentstable.
Defining a Composite Primary Key
1 2 3 4 5 6CREATE TABLE Enrollments ( CourseID INT, StudentID INT, EnrollmentDate DATE, PRIMARY KEY (CourseID, StudentID) );
- Combines
CourseIDandStudentIDto ensure that each course-student pair is unique.
Add PRIMARY KEY to an Existing Table
You can use the ALTER TABLE statement to add a primary key to a table after it has been created.
Add a Single-Column Primary Key
1 2ALTER TABLE Students ADD PRIMARY KEY (StudentID);
Add a Composite Primary Key
1 2ALTER TABLE Enrollments ADD PRIMARY KEY (CourseID, StudentID);
Remove a PRIMARY KEY Constraint
To remove a primary key from a table, use the appropriate command based on your database system.
Drop a Primary Key
1 2ALTER TABLE Students DROP PRIMARY KEY;
- This removes the primary key from the
Studentstable.
Primary Key vs. Unique Constraint
- Purpose:
- The
PRIMARY KEYuniquely identifies each record in a table and ensures that noNULLvalues are present. - The
UNIQUEconstraint ensures that all values in a column (or combination of columns) are distinct but allows a singleNULLvalue (depending on the database).
- The
- Quantity:
- A table can have only one primary key.
- A table can have multiple unique constraints applied to different columns or column combinations.
- NULL Values:
PRIMARY KEYcolumns cannot containNULLvalues.UNIQUEcolumns can contain oneNULLvalue (depending on the database system).
- Relationship Support:
- A
PRIMARY KEYis commonly used to establish relationships between tables via foreign keys. - A
UNIQUEconstraint is not typically used for defining relationships but ensures uniqueness in specific fields.
- A
- Implicit Indexing:
- Both
PRIMARY KEYandUNIQUEconstraints automatically create an index on the constrained column(s) for faster lookups.
- Both
- Use Case:
- Use
PRIMARY KEYfor the main unique identifier of a table, such asOrderIDorEmployeeID. - Use
UNIQUEfor secondary unique fields, like email addresses or usernames.
- Use
Best Practices for Using PRIMARY KEY
- Use Meaningful Columns: Choose fields that uniquely identify records, such as
EmployeeID,OrderID, orISBN. - Avoid Large Composite Keys: Use composite keys only when necessary, and keep them minimal to simplify relationships.
- Plan for Relationships: Define primary keys with foreign key relationships in mind to ensure smooth integration between tables.
- Avoid Repeating Values: Ensure no duplicate or missing values in columns defined as primary keys.
Key Points to Remember
- The
PRIMARY KEYconstraint ensures that columns contain unique and non-null values. - It can be defined on a single column or multiple columns (composite key).
- A table can have only one primary key, but it can span multiple columns.
- Adding or removing primary keys can be done using the
ALTER TABLEstatement.
Frequently Asked Questions
A primary key is a unique identifier for each record in a database table, ensuring that each row is distinct. It cannot have NULL values. A foreign key is a column or a set of columns in one table that links to the primary key in another table, ensuring referential integrity between the two tables.
A primary key is a column or set of columns that uniquely identifies each row in a table, and it cannot contain NULL values. A unique key also ensures that all values in a column are distinct but allows for NULL values, and you can have multiple unique keys in a table, unlike the primary key which can only appear once.
A primary key is a field or combination of fields in a database table that uniquely identifies each record. It must contain unique values, and no NULL values are allowed in primary key columns. It is used to enforce entity integrity.
No, a primary key cannot be NULL. It must always contain a unique value to ensure that each record in the table is uniquely identifiable.
Still have questions?Contact our support team