TheHingineer

  • DBMS


  • DBMS Part-1

  • DBMS Part-2

  • DBMS Part-3

  • DBMS Part-4

  • DBMS Part-5

  •  Keys in DBMS

     Keys ka use kyun hota hai?

    DBMS (Database Management System) mein keys ka use data ko uniquely identify karne aur tables ke beech relation banane ke liye hota hai. Keys se:

    • Hum records ko unique tarike se pehchaan sakte hain

    • Tables ke beech relationship banate hain

    • Duplicate data se bachav hota hai

    • Data consistency maintain hoti hai


     Types of Keys in DBMS (Simple Examples ke saath)


    1. Primary Key

    • Ye table ka main key hoti hai jo har row ko uniquely identify karti hai.

    • Ye kabhi NULL nahi hoti.

    • Ye unique honi chahiye.

     Example:

    Student Table

    StudentID Name Age
    101 Riya 20
    102 Aman 21
     

     Yahan par StudentID primary key hai, kyunki ye har student ko unique tarike se pehchaan raha hai.

    2. Candidate Key

    • Wo sabhi columns jo kisi record ko uniquely identify kar sakte hain.

    • Inme se ek ko primary key banaya jata hai.

     Example:

    Employee Table

    EmpID Email Phone
    1 raj@gmail.com 9999988888
    2 priya@gmail.com 9999977777
     

     EmpID, Email, aur Phone sabhi unique hain → sabhi candidate keys hain.

    3. Alternate Key

    • Candidate keys me se jo primary key nahi banti, wo alternate key hoti hai.

     Example:

    Upar wale example me agar EmpID ko primary key banaya jaye, to Email aur Phone alternate keys ban jati hain.

    4. Foreign Key

    • Foreign key ek aisi key hoti hai jo ek table ke primary key ko dusre table me refer karti hai.

    • Iska use tables ke beech connection banane ke liye hota hai.

     Example:

    Department Table

    DeptID DeptName
    10 HR
    20 IT
     

    Employee Table

    EmpID Name DeptID
    1 Raj 10
    2 Priya 20
     

     Yahan Employee Table mein DeptID foreign key hai jo Department Table ke DeptID ko refer kar rahi hai.

    5. Super Key

    • Koi bhi column ya columns ka group jo record ko uniquely identify kare → Super Key

    • Sabhi candidate keys super keys hoti hain, lekin sabhi super keys candidate nahi hoti.

     Example:

    EmpID Name Email
    1 Raj raj@gmail.com
     

    Super Keys:

    • {EmpID}

    • {Email}

    • {EmpID, Name} → Super key hai par minimal nahi, isliye candidate nahi.

    6. Composite Key

    • Jab 2 ya 2 se zyada columns milke primary key bante hain, to usse composite key kehte hain.

     Example:

    Enrollment Table

    StudentID CourseID Grade
    101 DB101 A
    101 OS102 B
    102 DB101 A+
     

     Yahan StudentID + CourseID milkar ek unique record identify karte hain → Composite Key


     Summary Table

    Key Type Use / Kaam
    Primary Key Table ke har row ko uniquely identify karta hai
    Candidate Key Wo sabhi keys jo unique hain (Primary ban sakti hain)
    Alternate Key Candidate keys jo primary nahi bani
    Foreign Key Ek table ke column ko doosre table ke primary key se jodta hai
    Super Key Koi bhi column/group jo uniquely identify kare
    Composite Key Multiple columns milke primary key bante hain
    Scroll to Top