What is PRIMARY KEY ?
What is PRIMARY KEY ?
A PRIMARY KEY in SQL is a constraint that uniquely identifies each record in a database table. It ensures that the values in the primary key column(s) are unique and not null, thereby preventing duplicate and null entries. Here are the key characteristics and functionalities of a primary key:
Creating a Primary Key on a Single Column:
CREATE TABLE Customers (
CustomerID INT NOT NULL PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Email VARCHAR(50)
);
In this example, CustomerID
is the primary key, ensuring that each customer has a unique identifier.
Creating a Composite Primary Key:
CREATE TABLE Orders (
OrderID INT NOT NULL,
ProductID INT NOT NULL,
Quantity INT,
PRIMARY KEY (OrderID, ProductID)
);
Here, the combination of OrderID
and ProductID
serves as the primary key, ensuring that each order-product pair is unique.
Adding a Primary Key to an Existing Table:
ALTER TABLE Employees
ADD CONSTRAINT PK_Employees PRIMARY KEY (EmployeeID);
Thi...
entry
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào