In an attempt to find out which column(s) in a table on SQL Server can help distinguish each row uniquely, one can either use the INFORMATION_SCHEMA views or the sys catalog views. These special tables assist in determining the primary key columns. Such knowledge is critical in appreciating the structure of the data within the table and the best ways to access specific record types.
If you want to implement follow this:
SELECT
kcu.COLUMN_NAME
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
WHERE
tc.TABLE_NAME = 'your_table_name'
AND tc.CONSTRAINT_TYPE = 'PRIMARY KEY';
SELECT
c.name AS Column_Name
FROM
sys.key_constraints kc
JOIN
sys.index_columns ic ON kc.object_id = ic.object_id AND kc.type = 'PK'
JOIN
sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
WHERE
kc.name = 'PK_your_table_name'; -- Replace with your actual primary key name or omit for all PKs