Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A script to test existence of primary keys

Trying to figure out a SQL script to test the existence of primary keys in cerain tables. If the table has no primary key, then the script should output the table name.

Tables to test:
TableA
TableB
TableC

After running the script (and lets say TableA and TableC have PKs, but not TableB), then the output would be below:

NoKeys
TableB
like image 292
cdub Avatar asked Nov 06 '12 19:11

cdub


1 Answers

My version:

SELECT 1  
FROM SYS.KEY_CONSTRAINTS
WHERE [TYPE] = 'PK' AND [PARENT_OBJECT_ID] = OBJECT_ID('USER.USER');

As a reference check Microsoft docs here.

like image 178
Arsen Khachaturyan Avatar answered Sep 23 '22 23:09

Arsen Khachaturyan