Answer:
Answer 1:
A table column with this constraint is called the key column for the table. This constraint helps the table to make sure that the value is not repeated and also that there are no null entries. This column does not allow null values and duplicate values. You can try inserting values to violate these conditions and see what happens. A table can have only one Primary key. Multiple columns can participate on the primary key.
Answer 2:
A primary key is a combination of fields which uniquely specify a row. This is a special kind of unique key, and it has implicit NOT NULL constraint. It means, Primary key values cannot be NULL.
A primary key in a database is a unique identifier for a record in a table. It serves two main purposes:
-
Uniqueness: A primary key ensures that each record in a table is uniquely identifiable. No two records in the table can have the same value for the primary key column(s). This uniqueness helps prevent data duplication and maintains data integrity.
-
Referential Integrity: A primary key is often used as a reference point for establishing relationships between tables in a relational database. It is used as a foreign key in related tables to create associations and maintain referential integrity. This ensures that data remains consistent across related tables.
Key characteristics of a primary key:
- Uniqueness: Each value in the primary key column(s) must be unique across all records in the table.
- Non-null: The primary key column(s) cannot contain null values. Every record must have a valid value in the primary key column(s).
- Immutable: The values in the primary key column(s) should not change after the record is inserted into the table. This helps maintain the integrity of relationships and references.
- Single or Composite: A primary key can consist of one or multiple columns, known as a composite primary key. In a composite primary key, the combination of values across the columns must be unique.
Example: Suppose you have a "Customers" table. You might designate the "CustomerID" column as the primary key. This ensures that each customer has a unique identifier, and you can use this identifier to establish relationships with other tables (e.g., orders, transactions) while maintaining data integrity.
SQL Syntax for creating a primary key:
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100)
);
In this example, the "CustomerID" column is defined as the primary key of the "Customers" table.