in
An entity ;             an  olect icent:fed and
                                   tfat        an
        Stuned      fn          chtatane.
                                                          emphyee.3
                  A Stucent, Ca, Bcolk
                         cnty
                                                 tyeá      f entttes
                 Gclechan   < Sla
                                                 Colkge     ma
       Ex:: Al shudenta in                a
             Stdent enty set
3)
     Relatodhi: -
                                         an      aeGaton
                            an mae             entteh.
           blw
      tx     A studnt Cpyd in a Caote.
                                iA a      htontP blo stud ard aust
                                                            entrtie
            A et                                            Samf
                                       seta.
      ty. A 'err|A" clatothps                        blo studnlt
         and (oone -hncm a
In the context of a relational database, Keys are one of the basic
requirements of a relational database model. keys are fundamental
components that ensure data integrity, uniqueness, and efficient access.
It is widely used to identify the tuples(rows) uniquely in the table.
  We also use keys to set up relations amongst various columns and tables
of a relational database. Let's explore the various types of keys used in a
relational model, which are essential for organizing and querying data
effectively.
Why do we require Keys in a DBMS?
Keys are crucial in aDatabase Management System (DBMS) for several
reasonS:
      Uniqueness: Keys ensure that each record in a table is unique and
      can be identified distinctly.
       Data lntegrity: Keys prevent dataduplication and maintain the
       consistency of the data.
       Efficient Data Retrieval: By defining relationships between tables,
       keys enable faster querying and better data organization. Without
       keys, it would be extremely difficult to manage large datasets, and
      queries would become inefficient and prone to errors.
Different Types of Database Keys
1. Super Key
The set of one or more attributes (columns) that can uniquely identify a
tuple (record) is known as Super Key. t may include extra attributes
that aren't essential for uniqueness but stilluniquely identify the row. For
Example, STUD_NO, (STUDNO, STUD_NAME), etc.
      Asuper key is a group of single or multiple keys that uniquely
      identifies rows in atable. It supports NULL values in
                                                            rows.
      A super key can contain extra attributes that aren't
                                                           necessary for
      uniqueness. For example, if the "STUDNO" column can uniquely
      identify a student, adding "SNAME" to it will still form a valid
      super key, though it's unnecessary.
Example: Consider the STUDENT table
STUD_NO SNAME ADDRESS PHONE
 1         Shyam      Delhi     123456789
 2         Rakesh     Kolkata   223365796
 3         Suraj      Delhi     175468965
 Asuper key could be acombination of STUD_NO and PHONE, as this
combination uniquely identifies a student.
                           Super Key
                        Candidate Key
                         Primary Key
                                                                    Relatio
n between Primary Key, Candidate Key, and Super Key
2. Candidate Key
The minimal set of attributes
                              that canuniquely identify atuple is known
as a candidate key. For Example, STUD NO in STUDENT
                                                        relation.
      A candidate key is a minimal super key, meaning it can
                                                              uniqueiy
       identify a record but contains no extra attributes.
    " lt is a super key with no repeated data is called a
                                                          candidate key.
      The minimalset of attributes that can uniquely identify
                                                              a record.
    " Acandidate key must contain unique values,
                                                  ensuring that no two
       rows have the same value in the candidate key's columns.
      Every table must have at least a single candidate key.
     A table can have multiple candidate keys but only one primary
                                                                   key.
Example: For the STUDENT table below, STUD_NO can be a candidate
key, as it uniquely identifies each record.
STUD_NO SNAME ADDRESSPHONE
1           Shyam     Delhi      12345%789
2           Rakesh    Kolkata    223365796
3          Suraj      Delhi      175468965
Table STUDENTCOURSE
STUD_NO TEACHER_NO COURSE_NO
STUD _NO TEACHERNO COURSE_NO
1          O01              CO01
2                           COOS
A composite candidate key example: (STUD_NO, COURSE_NO} can be a
candidate key for a STUDENT_COURSE table.
3. Primary Key
There can be more than one candidate key in elation out of which one
can be chosen as the primary key. For Example, STUD_NO, as well as
STUD_PHONE, are candidate keys for relation STUDENT but STUD_NO
can be chosen as the primary key (only one out of many candidate keys).
     Aprimary key is a unque key, meaning it can uniquely identify
     each record (tuple) in a table.
     It must have unique values and cannot contain any duplicate values.
     A primary key cannot be NULL, as it needs to provide avalid,
     unique identifier for every record.
       Aprimary key does not have to consist of a single column. In some
     cases, a composite primary key (made of multiple columns) can be
     used to uniquely identify records in a table.
     Databases typically store rows ordered in memory according to
     primary key for fast access of records using primary key.
Example:
STUDENT table -> StudentSTUD NO, SNAME, ADDRESS, PHOWE) »
 STUDNOis a primary key
Table STUDENT
STUD_NO SNAME ADDRESSPHONE
 1
            Shyam      Delhi     123456789
 2          Rakesh     Kolkata   223365746
 3          Suraj      Delhi     175468965
4. Altemate Key
An alternate key is any candidate key in atable that is not chosen as the
primary key. In other words, all the keys that are not selected as the
primary key are considered alternate keys.
     An alternate key is also referred to as a secondary key because it
     Can   uniquely identify records in a table, just like the primary key.
     An alternate key can consist of one or more columns (fields) that
     can uniquely identify a record, but it is not the primary key
     Eq:- SNAME, and ADDRESS is Alternate keys
Example: In the STUDENT table, both STUD NO and PHONE are
candidate keys. If STUD NO ischosen as the primary key,
then PHONE Would be considered an alternate key.
                                  Candidate Key
              StudlDRoll No. First Name Last Name              Email
                          21        Tom            Cox      abc@gfg.org
                2         22       John           Butler    xyz@gfg.org
                3         23       Alice      Peterson      mno@gfg.org
           Primary Key
                                     Alternate Key
Primary Key, Candidate Key, and Alternate Key
s. Foreign Key
A foreiqn key is an attribute in one table that refers to the
                                                              primary key
in another table. The table that contains the foreign key is called the
referencing table, and the table that is referenced is called the referenced
table.
         A foreign key in one table points to the primary key in another
         table, establishing a relationship between them.
   " It helps connect two or more tables, enabling you to create
         relationships between them. This is essential for maintaining data
         integrity and preventing data redundancy.
         They act as a cross-reference between the tables.
         For example, DNO is a primary key in the DEPT table and a non
         key in EMP
Example: Consider the STUDENTCOURSE table
STUD_NO        TEACHER NO COURSE NO
1          O0S                    COO1
           OS6                    COOs
Here, STUD_NO in the STUDENT COURSE table is a foreign key that
references the STUD_NOprimary key in the sTUDENT table.
Explanation:
      Unlike the Primary Key of any given relation, Foreign Key can be
      NULL as wellas may contain duplicate tuples i.e. it need not follow
      uniqueness constraint. For Example, STUD NO in the
     STUDENT_COURSE relation is not unique.
      It has been repeated for the first and third tuples. However, the
     STUD_NO in STUDENT relation is a primary key and it needs to be
      always unique, and it cannot be null.
      Example: In the sTUDENT COURSE table, (STUD NO, COURSE NO} can form
      a composite key to uniquely identify each record.
                                  Candidate Key                    Foreign Key
        Table 1                                                                        Table 2
                                                                             Dld       Designation
                EmplDEmp Name EmpLicence EmpPassport         DId
                                       ELI01        PA123     2                  2        BPO
                 001      Tom
                                      EL102         PA125     3                  3      Account
                005       John
                                      EL103         PAI29     5                             IT
                O08       Alice
            Primary Key
                                          Alternate Key     Primary Key Candidate Key Alternate Key
                                                                            Superkey
                                           Unique Key