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.
Read Also:
- Understanding Artificial Intelligence: A Human-Centric Overview
- Addressing AI Risks: Achieving the AI Risk Management Professional Certification
- Mastering Scaled Scrum: Earning the Scaled Scrum Professional Certification
- Strengthening Agile Leadership: Achieving the Scrum Master Professional Certificate
- Advancing My Expertise in AI: Earning the CAIEC Certification
- Achieving the CAIPC Certification: Advancing My AI Expertise
Subscribe to the GnoelixiAI Hub newsletter on LinkedIn and stay up to date with the latest AI news and trends.
Subscribe to my YouTube channel.
Reference: aartemiou.com (https://www.aartemiou.com)
© Artemakis Artemiou
Rate this article:
Artemakis Artemiou is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.