Primary keys in SQL are unique identifiers for rows in a database table. Each table can have only one primary key, which serves to enforce entity integrity by ensuring that each record is unique and can be reliably referenced. A primary key can be composed of a single column or a combination of multiple columns, but the key feature is that it must contain unique values, and it cannot contain NULLs. This means that every entry within the column(s) designated as the primary key can be distinguished from every other entry.
For example, consider a table called "Employees" where each employee has a unique EmployeeID. In this case, the EmployeeID can serve as the primary key. It ensures that no two employees can have the same ID. If an attempt is made to insert a record with an existing EmployeeID, the database will prevent this due to the uniqueness requirement of the primary key. This guarantees that operations like updates, deletions, or lookups are always performed on the correct employee without ambiguity.
Another example is a "Courses" table that lists various courses in a university. If it includes a CourseCode as its primary key, this code must be unique for each course offered. However, if a table needs to represent many-to-many relationships, such as students enrolled in courses, a composite primary key can be employed using both StudentID and CourseCode. This combination effectively identifies each record in the junction table uniquely, ensuring that no duplicate enrollment can occur. In essence, primary keys are fundamental to maintaining the integrity and organization of data within SQL databases.