Below are the few point on Primary key.
- Primary key is one of the most commonly in use constraint in MS SQL.
- Only 1 Primary key can be defined in a table. 430 more words
Here is a small script to get information about the Primary Key column(s) and Foreign Key column(s) in SQL Server.
;WITH GETINFO AS ( SELECT DBNAME = CONVERT(SYSNAME,DB_NAME()), OWNERNAME = CONVERT(SYSNAME,SCHEMA_NAME(OBJ1.SCHEMA_ID)), = CONVERT(SYSNAME,OBJ1.NAME), PKCOLUMN_NAME = CONVERT(SYSNAME,COL1.NAME), = CONVERT(SYSNAME,OBJ2.NAME), FKCOLUMN_NAME = CONVERT(SYSNAME,COL2.NAME), ORDINALPOSITION = ISNULL(CONVERT(SMALLINT,FKCOL.CONSTRAINT_COLUMN_ID),0), = CONVERT(SYSNAME,OBJECT_NAME(FK.OBJECT_ID)), = CONVERT(SYSNAME,I.NAME) FROM SYS.ALL_OBJECTS OBJ1, SYS.ALL_OBJECTS OBJ2, SYS.ALL_COLUMNS COL1, SYS.ALL_COLUMNS COL2, SYS.FOREIGN_KEYS FK INNER JOIN SYS.FOREIGN_KEY_COLUMNS FKCOL ON (FKCOL.CONSTRAINT_OBJECT_ID = FK.OBJECT_ID) INNER JOIN SYS.INDEXES I ON (FK.REFERENCED_OBJECT_ID = I.OBJECT_ID AND FK.KEY_INDEX_ID = I.INDEX_ID) WHERE OBJ1.OBJECT_ID = FK.REFERENCED_OBJECT_ID AND OBJ2.OBJECT_ID = FK.PARENT_OBJECT_ID AND COL1.OBJECT_ID = FK.REFERENCED_OBJECT_ID AND COL2.OBJECT_ID = FK.PARENT_OBJECT_ID AND COL1.COLUMN_ID = FKCOL.REFERENCED_COLUMN_ID AND COL2.COLUMN_ID = FKCOL.PARENT_COLUMN_ID ),SHOWINFO AS ( SELECT DISTINCT DBNAME,OWNERNAME,,, PRIMARYKEYCOLUMNNAMES= REPLACE( (SELECT A.PKCOLUMN_NAME AS FROM GETINFO A WHERE A.94 more words