Data Programming: Primary Keys
A relational database is a system in which information flows from one database object to another. For example, if you create an application used to…
Introduction#
In a relational database, data is passed between different objects. For example, a car rental system might use one table for vehicle stock and another for rental transactions. By selecting an existing car record in the transaction table, you eliminate the need to re-type vehicle details for each rental, reducing the risk of inconsistent data like misspelled car models. Relational database rules encourage creating links between objects so that information is shared effectively. These links primarily connect tables, and managing the flow from table A to table B requires making table A's data accessible through several methods:
- You must be able to uniquely identify each record from a table (A) without any confusion. For example, if you create a list of cars on a table, you should make sure that there is a unique (no duplicate) tag number for each car because each car should have one and must have one tag number. This ensures that there are no duplicate records on the table.
- A table (A) that holds information should make that information available to other tables (such as B)
- Two tables must not serve the same purpose. Once you have unique information on each table, one table can make its data available to other tables that need it so that the same information should not be entered in more than one table
These problems are solved by specifying a particular column as the "key" of the table. Such a column is referred to as the primary key. In a relational database, which is the case for most of the databases you will be creating, each table should have at least one primary key. As an example, a primary key on an car table of a car rental company can be set on a Tag Number field because each car should have a unique tag number. A table can also use more than one column to represent the primary key if you judge it necessary. Once you have decided that a table will have a primary key, you must decide what type of data that field will hold. If you are building a table that can use a known and obvious field as unique, an example would be the shelf number of a library, you can set its data type as char or varchar and make it a primary key. In many other cases, for example if you cannot decide on a particular field that would hold unique information, an example would be customers Contact Name, you should create your own unique field and make it the Primary Key. Such a field should have an int data type.
Visually Creating a Primary Key#
To create a primary key in the Microsoft SQL Server Management Studio or Microsoft Visual Studio, in the table, create a column and specify its data type:
- Then, on the toolbar, click the Set Primary Key button
- You can also right-click a column and click Set Primary Key
Here is an example:
Creating a Primary Key With SQL#
To create a primary column using SQL, the primary thing to do is, on the right side of the column definition, type PRIMARY KEY. Here is an example:
CREATE TABLE Persons
(
PersonID int identity(1,1) PRIMARY KEY NOT NULL,
FirstName varchar(20),
LastName varchar(20) NOT NULL
);The Primary Key Constraint#
In the SQL, you can give a specific name to a primary key. To do this, you can first create the column. Then, somewhere before the closing parenthesis of the table, specify the primary key column using the following formula:
CONSTRAINT PrimaryKeyName PRIMARY KEY(ColumnName)The CONSTRAINT keyword and the PRIMARY KEY clause (which is not case-sensitive) are essential in this syntax. Replace the PrimaryKeyName placeholder with your chosen name for the key. Inside the PRIMARY KEY parentheses, list the column that will serve as the unique identifier. Consider this example:
CREATE TABLE Persons
(
PersonID int identity(1,1) NOT NULL,
FirstName varchar(20),
LastName varchar(20) NOT NULL,
CONSTRAINT PrimKeyPeople PRIMARY KEY(PersonID)
);By convention or tradition, the name of the primary starts with PK_ followed by the name of the table. Here is an example:
CREATE TABLE Persons
(
PersonID int identity(1,1) NOT NULL,
FirstName varchar(20),
LastName varchar(20) NOT NULL,
CONSTRAINT PK_Persons PRIMARY KEY(PersonID)
);Adding a Primary Key#
After creating a table or when inheriting a table created by someone else, you may find out that it lacks a primary key. You can add it, of course following some rules. You have two options. Imagine you have the following table:
CREATE TABLE Employees
(
FirstName nvarchar(20),
LastName nvarchar(20),
DepartmentCode nchar(6)
);You can add the PRIMARY KEY expresion after defining the new column. Here is an example:
ALTER TABLE Employees
ADD EmployeeNumber int not null PRIMARY KEY;As an alternative, you can add a column, and then use the CONSTRAINT formula to define the primary key. Here is an example:
ALTER TABLE Employees
ADD EmployeeNumber int not null
CONSTRAINT PK_Employees PRIMARY KEY(EmployeeNumber);Home Copyright © 2010-2011 C# Key

