Relationship and keys Flashcards
2
Keys
Key create the relationships between entities
Relationships definition
How 2 or more entities are linked to another.
Primary key definition
Primary key in a database is like a unique ID for each row of data.
Foreign key definition
Foreign keys link data in one table to the data in another table.
Alternate key
Alternate key associated with one or more columns whose values uniquely identify every row in the table, but which is not the primary key.
Secondary key
A secondary key is an additional piece of information used to organize and retrieve data.
Foreign key example
You have 2 tables in a database: one for students and another for classes. In the table for classes, you might have a column that stores the ID of the teacher for each class. That ID is a foreign key because it refers to the ID of a teacher in the table for students
Secondary key example
To make this search faster, you can create a secondary key based on the author’s name. This secondary key acts like a shortcut, allowing you to directly access all books by a particular author without having to scan through every book in the database.
Alternate key example
Let’s consider a database for a company’s employees. Each employee is assigned a unique employee ID, which serves as the primary key in the database. Now, let’s say that each employee also has a unique email address. Although the email address is not the primary key, it can still uniquely identify each employee because no two employees share the same email address.
In this case, the email address serves as an alternate key. It’s an alternative way to uniquely identify employees in the database, even though it’s not the primary means of identification. So, if you wanted to look up an employee’s information using their email address, you could do so efficiently because the email address is an alternate key that uniquely identifies them.
Primary key example
Each student has a unique student ID number assigned to them when they enroll. In this table, the student ID number would serve as the primary key.