Tag: syntax

SQL Finding primary keys and missing primary keys

Tables with Primary Keys

SELECT c.name, b.name, a.name
 FROM sys.key_constraints a
 INNER JOIN sys.tables b ON a.parent_object_id = b.OBJECT_ID
 INNER JOIN sys.schemas c ON a.schema_id = c.schema_id
 WHERE a.type = 'PK'

Tables Without Primary Keys

SELECT c.name, b.name
 FROM sys.tables b
 INNER JOIN sys.schemas c ON b.schema_id = c.schema_id
 WHERE b.type = 'U'
 AND NOT EXISTS
 (SELECT a.name
 FROM sys.key_constraints a
 WHERE a.parent_object_id = b.OBJECT_ID
 AND a.schema_id = c.schema_id
 AND a.type = 'PK' )

Authenticate User to Security Group

Imports System.Security.Principal

Public Class Form1
Public id As WindowsIdentity = WindowsIdentity.GetCurrent()
Public User As WindowsPrincipal = New WindowsPrincipal(id)

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim flg As Boolean = User.IsInRole(“Domain\Security Group”)

Select Case flg
Case True
MsgBox(“I am a member of the group”)
Case False
MsgBox(“Not a member”)
End Select

End
End Sub

End Class

SQL Basic Command Syntax

I use SQL statements on an almost daily basis, yet I have trouble remembering the syntax of even the most basic commands.  Thus I decided to post a cheat sheet here for quick and easy reference.  I hope you may find it valuable.
Selecting Records SELECT * FROM Table WHERE Param1 < 10 ORDER BY Param2
Selecting Records Into a New Table SELECT * INTO newdb.dbo.newtable FROM olddb.dbo.oldtable
Updating Records UPDATE Table SET Param1 = ‘cat’, Param3 = ‘fish’ WHERE Param2 = ‘dog’
Adding Records INSERT INTO Table (Param1, Param2) VALUES (‘xx’, ‘yy’)
Deleting Records DELETE FROM Table WHERE Param1 = ‘value’
Group By SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name = value GROUP BY column_name
List all tables & views in database SELECT TABLE_TYPE, TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_TYPE, TABLE_NAME