Tip of the Day : How to Determine if a Table has a Primary Key

Welcome to SQL Server Helper !!!

This site is intended for those who are beginning to use SQL Server as part of their day-to-day activities.  You will find in this site a collection of useful functions, triggers, stored procedures and tips and tricks related to SQL Server.

Should you have any comments or questions regarding this site or if you want to ask SQL Server-related questions, e-mail us here.

We hope we are able to help and we are glad to help!!!

SQL Server Tip of the Day - February 28, 2017

How to Determine if a Table has a Primary Key

To determine if a table has a primary key, run the following statement:

SELECT OBJECTPROPERTY(OBJECT_ID(N'[dbo].[User]'), 
'TableHasPrimaryKey')

A value of 1 means that the specified table has a Primary Key constraint while a value of 0 means the table doesn’t have a Primary Key constraint.  A value of NULL means either the table doesn’t exist or the object name passed is not a table.

To get a list of tables that doesn’t have a Primary Key constraint, run the following statement:

SELECT [Name]
FROM [sys].[objects]
WHERE [Type_Desc] = 'USER_TABLE' AND
      OBJECTPROPERTY([Object_ID], 'TableHasPrimaryKey') = 0
ORDER BY [Name]

SQL Server 2012

SQL Server 2008

User-Defined Functions

Date Functions

A collection of useful user-defined functions that deal with dates.

String Functions

A collection of useful user-defined functions that deal with strings (varchar/char/nvarchar/nchar).

Tree Functions

A collection of useful user-defined functions that deal with tree or hierarchical design structures.

Table-Valued Functions

A collection of useful table-valued user-defined functions that can be used to join with other tables.

SQL Server Built-in Functions

A reference to all built-in functions available within SQL Server grouped into categories.

Tips and Tricks

A collection of useful SQL Server-related tips and tricks:

SQL Server Error Messages

A list of SQL Server error messages and for certain error messages, discusses ways on how to solve the error or work around them:

Frequently Asked Questions