Keys
3
A key in SQL is a column or a combination
of columns used to uniquely identify each
row in a database table.
Keys help to keep data organized, ensure
data accuracy, and create relationships
between different tables.
Types of Keys
1
2
Primary Key
5
Foreign Key
3
4
Composite Key
Candidate Key
Alternate Key
Super Key
5
6
Primary Key
• A primary key is a column that has a unique and not null value.
6
• It uniquely identifies each record in a table.
• Each table can only have one primary key.
• It allows you to quickly find or reference specific records in a
table.
Syntax
Create Table Customers(
CustomerID int primary Key,
CustomerNames varchar(40),
Phonenumber varchar(40),
Email varchar(45)
)
Scenario
While Creating Table:
CREATE TABLE table_name (
column1 data_type, PRIMARY
KEY
column2 data_type;
)
Alter Table Customers
ADD Primary Key(CustomerID);
Scenario
4
Adding a primary key to an
existing table:
ALTER TABLE table_name
ADD PRIMARY KEY
(column_name);
Syntax
Alter Table Customers(
Drop Primary key;
)
Scenario
Drop Primery Key from Table:
Alter TABLE table_name (
DROP PRIMARY KEY;
)
4
Example scenario
Customers table:
4
Products table: Orders table:
PRIMARY KEY PRIMARY KEY PRIMARY KEY
Foreign Key
6
• A column or a set of columns in one table that refers to the primary key in another table.
• It establishes a relationship between two tables.
• A table can have multiple foreign keys, each linking to different tables.
• Foreign key columns can contain NULL ,meaning that not all records need to relate to
the referenced table.
• We can change the name of a foreign key column in SQL.
• We cannot add new values in a foreign key column that do not exist in the referenced
primary key column.
Syntax
CREATE TABLE suppliers (
supplier_id INT PRIMARY KEY,
ProductID int,
supplier_name VARCHAR(100),
supplier_contact VARCHAR(100),
foreign key (ProductID) references
Products(ProductID) );
Scenario
Alter Table suppliers
ADD Foreign Key(ProductID)
references Products(ProductID);
Scenario
4
Adding a primary key to an existing table:
ALTER TABLE table_name
ADD FOREIGN KEY (foreign_key_column) REFERENCES
referenced_table(referenced_column);
While Creating Table:
CREATE TABLE table_name (
column1 data_type,
column2 data_type,
FOREIGN KEY(foreign_key_column)
REFERENCES referenced_table(referenced_column)
);
Syntax
Alter Table suppliers(
Drop Foreign key
fk_ProductID;
)
Scenario
Drop Foreign Key from Table:
Alter TABLE table_name (
DROP Foreign KEY foreign_key_name;
)
4
Example scenario
4
Products table: Suppliers table:
Foreign KEY
PRIMARY KEY
Example scenario
Customers table:
4
Products table: Orders table:
PRIMARY KEY PRIMARY KEY FOREIGN
KEYS
• It is also a primary key that is consists of two or more columns used together
to uniquely identify a record in a table.
• Composite keys are used when a single column is not sufficient to ensure the
uniqueness of records.
• UNIQUE + NONUNIQUE = COMPOSITE KEY.
• The combination of values in these columns must be unique across the table.
• Composite key columns can not be NULL.
Composite Key
6
Example scenario
4
Products table: Suppliers table:
COMPOSITE KEY
UNIQUE+NONUNIQUE
ProductID+Price
COMPOSITE KEY
NONUNIQUE+UNIQUE
Supplier_name+supplier_contact
Example scenario
Customers table:
4
Orders table:
COMPOSITE KEY
NONUNIQUE+UNIQUE
CustomerName+Phonenumber
COMPOSITE KEY
UNIQUE+NONUNIQUE
OrderID+OrderStatus
• It is not the primary key.
• It includes unique columns.
• An alternate key is a column or a set of columns in a database table that
can be used to uniquely identify records.
• A table can have multiple alternate keys. While there is only one primary
key, any number of alternate keys can exist.
• It can be NULL.
Alternate Key
6
Example scenario
4
Customers table: Suppliers table:
Alternate KEY
Alternate KEY
• It also include primary key column.
• It includes all the unique columns.
• A candidate key can consist of a single column or multiple columns
(composite candidate key).
• Candidate keys cannot contain NULL values. Every row must have a valid,
unique value for the candidate key.
Candidate Key
6
Example scenario
4
Customers table: Suppliers table:
CANDIDATE KEY
CANDIDATE KEY
• It is a set of key that contains all the keys in it.
• Every super key must have the ability to uniquely identify each row in a
table. This means that no two rows can have the same values in all the
columns that make up the super key.
• A super key can consist of a single column or multiple columns. For
example, a single column like customer_id or a combination of columns
like (first_name, last_name, date_of_birth) can form a super key.
Super Key
6
Example scenario
4
Customers table:
CustomerID
CustomerID+ CustomerNames
CustomerID+CustomerNames+Email
Email
CustomerID+Email

Keys in SQL.........................pptx

  • 1.
    Keys 3 A key inSQL is a column or a combination of columns used to uniquely identify each row in a database table. Keys help to keep data organized, ensure data accuracy, and create relationships between different tables.
  • 2.
    Types of Keys 1 2 PrimaryKey 5 Foreign Key 3 4 Composite Key Candidate Key Alternate Key Super Key 5 6
  • 3.
    Primary Key • Aprimary key is a column that has a unique and not null value. 6 • It uniquely identifies each record in a table. • Each table can only have one primary key. • It allows you to quickly find or reference specific records in a table.
  • 4.
    Syntax Create Table Customers( CustomerIDint primary Key, CustomerNames varchar(40), Phonenumber varchar(40), Email varchar(45) ) Scenario While Creating Table: CREATE TABLE table_name ( column1 data_type, PRIMARY KEY column2 data_type; ) Alter Table Customers ADD Primary Key(CustomerID); Scenario 4 Adding a primary key to an existing table: ALTER TABLE table_name ADD PRIMARY KEY (column_name);
  • 5.
    Syntax Alter Table Customers( DropPrimary key; ) Scenario Drop Primery Key from Table: Alter TABLE table_name ( DROP PRIMARY KEY; ) 4
  • 6.
    Example scenario Customers table: 4 Productstable: Orders table: PRIMARY KEY PRIMARY KEY PRIMARY KEY
  • 7.
    Foreign Key 6 • Acolumn or a set of columns in one table that refers to the primary key in another table. • It establishes a relationship between two tables. • A table can have multiple foreign keys, each linking to different tables. • Foreign key columns can contain NULL ,meaning that not all records need to relate to the referenced table. • We can change the name of a foreign key column in SQL. • We cannot add new values in a foreign key column that do not exist in the referenced primary key column.
  • 8.
    Syntax CREATE TABLE suppliers( supplier_id INT PRIMARY KEY, ProductID int, supplier_name VARCHAR(100), supplier_contact VARCHAR(100), foreign key (ProductID) references Products(ProductID) ); Scenario Alter Table suppliers ADD Foreign Key(ProductID) references Products(ProductID); Scenario 4 Adding a primary key to an existing table: ALTER TABLE table_name ADD FOREIGN KEY (foreign_key_column) REFERENCES referenced_table(referenced_column); While Creating Table: CREATE TABLE table_name ( column1 data_type, column2 data_type, FOREIGN KEY(foreign_key_column) REFERENCES referenced_table(referenced_column) );
  • 9.
    Syntax Alter Table suppliers( DropForeign key fk_ProductID; ) Scenario Drop Foreign Key from Table: Alter TABLE table_name ( DROP Foreign KEY foreign_key_name; ) 4
  • 10.
    Example scenario 4 Products table:Suppliers table: Foreign KEY PRIMARY KEY
  • 11.
    Example scenario Customers table: 4 Productstable: Orders table: PRIMARY KEY PRIMARY KEY FOREIGN KEYS
  • 12.
    • It isalso a primary key that is consists of two or more columns used together to uniquely identify a record in a table. • Composite keys are used when a single column is not sufficient to ensure the uniqueness of records. • UNIQUE + NONUNIQUE = COMPOSITE KEY. • The combination of values in these columns must be unique across the table. • Composite key columns can not be NULL. Composite Key 6
  • 13.
    Example scenario 4 Products table:Suppliers table: COMPOSITE KEY UNIQUE+NONUNIQUE ProductID+Price COMPOSITE KEY NONUNIQUE+UNIQUE Supplier_name+supplier_contact
  • 14.
    Example scenario Customers table: 4 Orderstable: COMPOSITE KEY NONUNIQUE+UNIQUE CustomerName+Phonenumber COMPOSITE KEY UNIQUE+NONUNIQUE OrderID+OrderStatus
  • 15.
    • It isnot the primary key. • It includes unique columns. • An alternate key is a column or a set of columns in a database table that can be used to uniquely identify records. • A table can have multiple alternate keys. While there is only one primary key, any number of alternate keys can exist. • It can be NULL. Alternate Key 6
  • 16.
    Example scenario 4 Customers table:Suppliers table: Alternate KEY Alternate KEY
  • 17.
    • It alsoinclude primary key column. • It includes all the unique columns. • A candidate key can consist of a single column or multiple columns (composite candidate key). • Candidate keys cannot contain NULL values. Every row must have a valid, unique value for the candidate key. Candidate Key 6
  • 18.
    Example scenario 4 Customers table:Suppliers table: CANDIDATE KEY CANDIDATE KEY
  • 19.
    • It isa set of key that contains all the keys in it. • Every super key must have the ability to uniquely identify each row in a table. This means that no two rows can have the same values in all the columns that make up the super key. • A super key can consist of a single column or multiple columns. For example, a single column like customer_id or a combination of columns like (first_name, last_name, date_of_birth) can form a super key. Super Key 6
  • 20.
    Example scenario 4 Customers table: CustomerID CustomerID+CustomerNames CustomerID+CustomerNames+Email Email CustomerID+Email