A Composite Key, in SQL, is a combination of two or more columns that uniquely identifies each row in a table. Unlike a single-column primary key, a composite key leverages the synergy of multiple columns to create a distinctive identifier for a record.
Consider this scenario: you're managing employee data, and you want to find all records for an employee named Rahul. With a single column, like the employee name, you might encounter multiple entries with the same name. By combining columns, such as employee name and employee number, you create a composite key that streamlines your search, making it more efficient.
CREATE TABLE Employee_Detail (
EmployeeID INT NOT NULL,
EmployeeName VARCHAR(50) NOT NULL,
Department VARCHAR(50),
CONSTRAINT pk_composite_key PRIMARY KEY (EmployeeID, EmployeeName)
);
This SQL code creates a table named Employee_Detail with the following columns:
This column represents the unique identifier for each employee. It is defined as an integer (INT) and is marked as "NOT NULL," meaning that every record must have a value for the EmployeeID.
This column stores the name of the employee. It is defined as a variable character string (VARCHAR) with a maximum length of 50 characters. Similar to EmployeeID, it is marked as "NOT NULL."
This column represents the department in which the employee works. It is defined as a variable character string (VARCHAR) with a maximum length of 50 characters. Unlike the previous columns, it is not marked as "NOT NULL," allowing records to have a null value for the department.
This line establishes a composite primary key for the table. The primary key is named pk_composite_key and is formed by combining the EmployeeID and EmployeeName columns. The primary key ensures that each combination of EmployeeID and EmployeeName is unique in the table, preventing duplicate entries.
In SQL, you can modify composite keys using the ALTER command. To add columns, use ALTER-ADD, and to delete columns, use ALTER-DROP.
ALTER TABLE TableName
ADD CONSTRAINT NewConstraint PRIMARY KEY (Column1, Column2, ColumnN);
This part specifies the name of the table that you want to alter. Replace TableName with the actual name of the table you are modifying.
This part adds a new constraint to the table. A constraint is a rule that is enforced to maintain the integrity of the data in the table. In this case, a new constraint is being added, and it is named NewConstraint. You can replace NewConstraint with a more descriptive name that reflects the purpose of the constraint.
This part defines the type of constraint being added. It specifies that a primary key constraint is being added to the table. The primary key uniquely identifies each record in the table. The columns Column1, Column2, and ColumnN are the columns that together form the composite primary key. This means that the combination of values in these columns must be unique for each record in the table.
In summary, this SQL code alters an existing table by adding a new composite primary key constraint named NewConstraint. The primary key is formed by combining the specified columns (Column1, Column2, and ColumnN), ensuring unique identification for each record in the table.
ALTER TABLE TableName
DROP CONSTRAINT ExistingConstraint;
This SQL code is an "ALTER TABLE" statement, which is used to modify an existing table. Here's a breakdown of each part of the code:
This part specifies the name of the table that you want to alter. Replace TableName with the actual name of the table you are modifying.
This part instructs the database to drop or remove a specific constraint from the table. In this case, the constraint being dropped is named ExistingConstraint. You should replace ExistingConstraint with the actual name of the constraint you want to remove.
In summary, this SQL code alters an existing table by dropping a constraint named ExistingConstraint. The DROP CONSTRAINT statement removes the specified constraint from the table, allowing you to modify the table's structure or add a different constraint if needed.