10 Understanding Primary Keys and Foreign Keys
In relational databases, primary keys and foreign keys are used to establish relationships between tables. A primary key is a unique identifier for a record in a table, while a foreign key is a field that refers to the primary key of another table. Primary Keys A primary key is a column or set of columns in a table that uniquely identifies each row in that table. It must have a unique value for each record and cannot be null. Primary keys are used to ensure data integrity and provide a way to access records in the table quickly. They are often used as the basis for creating relationships with other tables. Let's create a table called "students" with a primary key: CREATE TABLE students ( id INT PRIMARY KEY, name VARCHAR(50), age INT, gender VARCHAR(10) ); In this example, we have created a table called "students" with an "id" column as the primary key. This column will contain a unique value for each record in the table....