Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Alternate Key in SQL

Alternate Key in SQL

? Alternate Key in SQL

An Alternate Key is any candidate key not chosen as the Primary Key in a table.


? Understanding with Definitions

  • Candidate Key: A column (or combination of columns) that can uniquely identify each row.

  • Primary Key: The candidate key chosen to uniquely identify rows.

  • Alternate Key: The candidate key(s) not selected as the primary key.


? Example Table: students

student_idemailroll_noname
101a@example.com1001Alice
102b@example.com1002Bob

Candidate Keys:

  • student_id

  • email

  • roll_no

If we choose:

  • Primary Key = student_id

  • Then Alternate Keys = email, roll_no


? Declaring an Alternate Key

In SQL, you enforce an alternate key using a UNIQUE constraint.

CREATE TABLE students (  student_id INT PRIMARY KEY,  email VARCHAR(100) UNIQUE,  roll_no INT UNIQUE,  name VARCHAR(50));

Here:

  • student_id is the Primary Key

  • email and roll_no are Alternate Keys (with UNIQUE constraint)


? Why Use Alternate Keys?

  • To ensure uniqueness on other columns besides the primary key.

  • Helps prevent duplicate data (e.g., no duplicate emails or roll numbers).

  • Can be used to define foreign key relationships in other tables.


Let me know if you want to practice with queries or explore composite alternate keys too!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql