SQL Server BasicsWhat is SQL Server database?What is RDBMS?What is Normalization?Why we use Denormalization?What_is_SQL?What is PL/SQL?Difference between SQL and PL/SQLDatabase TableOne to One RelationshipOne to Many RelationshipMany to Many RelationshipMany to One RelationshipString Data TypesNumber Data TypesDate Data TypesOther Data TypesCreate DatabaseDrop DatabaseCreating and Managing Users in SQL ServerCreate TableAlter TableDrop TableConstraints in SQL serverPrimary KeyForeign KeyUnique KeyCandidate KeyComposite KeyDifference between primary key and candidate keyPrimary key and foreign key relationshipSurrogate KeyCascading Referential Integrity ConstraintsSelf Referential Integrity ConstraintsInsert into statementInsert multiple recordsUpdate statementDelete statementTruncate statementDifference between Delete and TruncateAlias in SQL ServerSelect statementSelect DistinctSelect TopSelect IntoNull Functions(ISNULL(),NULLIF(),COALESCE())Sub QueryIdentity ColumnSequence objectDifference between sequence and identity columnSQL Server ClausesWHERE ClauseOrder By ClauseTop N ClauseGroup By ClauseHaving ClauseDifference between Where and HavingSQL Server OperatorsArithmetic OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsAny OperatorsAll OperatorsUnion OperatorsUnion All OperatorsDifference between Union and Union AllIntersect OperatorExcept OperatorDifference between Except and IntersectJoinsInner JoinLeft JoinRight JoinFull JoinSelf JoinCross JoinViewsWhat are views?Create views using SSMSIndexed ViewsComplex ViewsCheck Option in ViewCheck Encryption in ViewSchema Binding Option in ViewRead-only ViewsUpdatable ViewsAdvantages and disadvantages of viewsCreate multiple views on one tableCan we implement index on views?Can we Perform Insert, update, delete operation on views?Stored Procedure and FunctionsWhat are Stored Procedures?Why we use stored procedures?Passing parameters to Stored procedureUser-Defined FunctionsDifference between UDF and Stored procedurePre-Defined Functions@@Indentity and Scope_IndentityNULLIF, ISNULL and COALESCE

SQL - Candidate Key

In SQL Server, a candidate key is a set of one or more columns that can uniquely identify a row in a table. It is a candidate for being chosen as the primary key of the table. A candidate key must satisfy two properties:

  1. Uniqueness: Each value in the candidate key must be unique within the table. No two rows can have the same combination of values in the candidate key columns.
  2. Minimality: No proper subset of the candidate key should have the uniqueness property. In other words, removing any column from the candidate key would result in losing the uniqueness property.

Candidate keys provide a means to identify and distinguish rows in a table uniquely. They play a crucial role in database design when determining the primary key of a table.

Let's consider an example of table named "Students" with the following columns:

  1. StudentID (Primary Key)
  2. SocialSecurityNumber (Candidate Key)
  3. StudentEmail (Candidate Key)
  4. StudentName
  5. Birthdate
In this example, the table "Students" represents student records in a school database.
  1. StudentID: This is chosen as the Primary Key. It uniquely identifies each student in the table.
  2. SocialSecurityNumber: This is also a unique attribute that could identify each student. It's a candidate key because it can be used to uniquely identify students, but since "StudentID" is already chosen as the Primary Key, "SocialSecurityNumber" becomes an Alternate Key.
  3. StudentEmail: Similarly, "StudentEmail" is another unique attribute that could identify each student. It's also a candidate key. However, since "StudentID" is already the Primary Key, "StudentEmail" becomes another Alternate Key.

Let's insert some sample data into the "Students" table:

Table: Students
StudentID SocialSecurityNumber StudentEmail StudentName Birthdate
1 123-45-6789 john@example.com John 2000-05-15
2 987-65-4321 sarah@example.com Sarah 2001-08-20
3 555-55-5555 michael@example.com Michael 1999-02-10

In this example, "StudentID" is the Primary Key, and it uniquely identifies each student. "SocialSecurityNumber" and "StudentEmail" are both candidate keys, but they are not chosen as the Primary Key. Instead, they serve as alternate means of uniquely identifying students.

Having candidate keys allows you to perform various operations in the database, such as searching for students using different attributes or ensuring data integrity by preventing duplicate values in these unique columns.