Visual C#

Microsoft Visual C#: The Uniqueness of Records

One of the primary concerns of records is their uniqueness. In a professional database, you usually want to make sure that each record on a table can be…

1 min read214 words2 code samples

Introduction#

It is essential that records within a database remain distinct. Professional table design typically requires a reliable method for identifying each row uniquely. Microsoft SQL Server provides several mechanisms for this, such as indexes, primary keys, and identity columns. One common strategy is to enforce a rule that ensures values in a specific column are unique.

Visually Creating a Uniqueness Rule#

To define a column so that every value in it must be unique, open the Table window, right-click the column you want, and choose Indexes/Keys... In the Indexes/Keys dialog box, select Add. Then, on the right side, set Is Unique to Yes:

After doing this, click Close.

Programmatically Creating a Uniqueness Rule#

Transact-SQL includes the UNIQUE keyword to help you define columns where every value must be different. You can apply this constraint by typing UNIQUE after the column's data type. Consider the following example:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ExoDB1
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }

        private void btnDatabase_Click(object sender, EventArgs e)
        {
            using (SqlConnection connection = 
                new SqlConnection("Data Source=(local);" +
                                  "Database='Exercise';" +
                                  "Integrated Security=yes;"))
            {
                SqlCommand command =
                    new SqlCommand("CREATE TABLE Students" +
                                   "(" +
                                   "    StudentNumber int UNIQUE," +
                                   "    FirstName nvarchar(50)," +
                                   "    LastName nvarchar(50) NOT NULL" +
                                   ");",
                           connection);
                connection.Open();
                command.ExecuteNonQuery();

                MessageBox.Show("A new table named Students has been created.");
            }
        }
    }
}

When a column has been marked as unique, during data entry, the user must provide a unique value for each new record created. If an existing value is assigned to the column, this would produce an error:

C#
CREATE TABLE Students
(
    StudentNumber int UNIQUE,
    FirstName nvarchar(50),
    LastName nvarchar(50) NOT NULL
);
GO

INSERT INTO Students
VALUES(24880, N'John', N'Scheels'),
      (92846, N'Rénée', N'Almonds'),
      (47196, N'Peter', N'Sansen'),
      (92846, N'Daly', N'Camara'),
      (36904, N'Peter', N'Sansen');
GO

By the time the fourth record is entered, since it uses a student number that exists already, the database engine would produce an error:

Home Copyright © 2010-2011 C# Key