In MySQL, the following query can be used to retrieve a table's primary key:
SHOW KEYS FROM tablename WHERE Key_name = 'PRIMARY'
For SQL Server, there are multiple approaches to accomplish this task:
Execute the following query:
SELECT COLUMN_NAME FROM sys.primary_keys WHERE TABLE_NAME = 'TableName'
Replace 'TableName' with the actual table name for which you want to retrieve the primary key.
This query can be used to retrieve primary key information for both MySQL and SQL Server:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE OBJECTPROPERTY(OBJECT_ID(CONSTRAINT_SCHEMA + '.' + QUOTENAME(CONSTRAINT_NAME)), 'IsPrimaryKey') = 1 AND TABLE_NAME = 'TableName'
Again, replace 'TableName' with the desired table name.
The above is the detailed content of How to Retrieve Table Primary Keys in SQL Server and MySQL?. For more information, please follow other related articles on the PHP Chinese website!