Hi friends! This article, discusses about the benefits of primary keys in database tables.
A primary key is a special column (or set of combined columns) in a relational database table, that is used to uniquely identify each record. A database primary key cannot contain NULL values and each table can have only one primary key. A primary key can either be a single column, or a combined set of columns.
Benefits of Primary Key in a Database
The benefits of using primary keys in relational databases are many. The list below, summarizes the main benefits:
- Database operations like searching for records are faster because an index is used, based on the primary key.
- Using the primary key, you can easily identify and find unique rows in the database table.
- They allow you to update/delete only specific records by uniquely identifying them.
- The table’s data is sorted based on the primary key.
- They help you avoid inserting duplicate records in a table.
- The allow creating referential integrity constraints (i.e. use of foreign keys).
Examples of Primary Keys in SQL Server Database
Below, you can find some simple examples of creating tables with primary keys in SQL Server databases.
SQL Server Table with Primary Key – No Auto-Increment
CREATE TABLE tblTest1 ( id INT PRIMARY KEY, code VARCHAR(50) ); GO
In the above example, the T-SQL code creates a SQL Server database table, along with setting the column “ID” as primary key. However, you will still need to include the value for the “ID” column in your INSERT statements, since auto-increment was not specified.
The second example, specifies auto-increment for the primary key.
SQL Server Table with Primary Key – With Auto-increment
CREATE TABLE tblTest2 ( id INT PRIMARY KEY IDENTITY(1,1), code VARCHAR(50) ); GO
As you can see, in this second example, the T-SQL code creates a SQL Server database table, along with setting the column “ID” as primary key and also, it uses the IDENTITY property, in order for the primary key value, to be automatically set after each INSERT statement.
See more examples of the CREATE TABLE statement in SQL Server, in this MS Docs article.
Recommended course: “Essential SQL Server Development Tips for SQL Developers”
Make sure that you check our online course on Udemy, titled “Essential SQL Server Development Tips for SQL Developers” (special limited-time discount included in link).
Sharpen your SQL Server database programming skills via a large set of tips on T-SQL and database development techniques. The course, among other, features over than 30 live demonstrations!
Recommended Online Courses:
- SQL Server 2022: What’s New – New and Enhanced Features
- Introduction to Azure Database for MySQL
- Working with Python on Windows and SQL Server Databases
- Boost SQL Server Database Performance with In-Memory OLTP
- Introduction to Azure SQL Database for Beginners
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- A Guide on How to Start and Monetize a Successful Blog
- Data Management for Beginners – Main Principles
Read Also:
- What can you do using SQL Server?
- How to Get Started with SQL Server – First Steps
- How to Create Databases in SQL Server
- How to Rebuild All Indexes Online for a SQL Server Database
- What are SQL Server Stored Procedures?
- Welcome to {essentialDevTips.com}!
- How to Write a “Hello World” App in Visual C++
- How to Write a “Hello World” App in C#
- Main Data Structures in Python
- SyntaxError: invalid syntax when using IF in Python – How to Resolve it
- What is SQL Server Management Studio?
- Difference Between SQL and SQL Server
- Using Dynamic Memory Allocation in Java
- Securing your SQL Server Instances
- What are T-SQL Snippets in SSMS?
- Software Review: SQLNetHub’s Snippets Generator
Reference: {essentialDevTips.com} (https://www.essentialdevtips.com/)
© essentialDevTips.com
Rate this article:
Artemakis Artemiou is a Senior SQL Server Architect, Author, a 9 Times Microsoft Data Platform MVP (2009-2018). He has over 15 years of experience in the IT industry in various roles. Artemakis is the founder of SQLNetHub and {essentialDevTips.com}. Artemakis is the creator of the well-known software tools Snippets Generator and DBA Security Advisor. Also, he is the author of many eBooks on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA). Moreover, Artemakis teaches on Udemy, you can check his courses here.