Skip to content Skip to sidebar Skip to footer

Compulsory Primary Key For Sql Server

Is there any configuration setting in SQL Server 2008 for put Primary key must when create a New table ? I know Oracle does not allow to create a Table without Primary key.But SQL

Solution 1:

You could use DDL Triggers for this, and you'd have to parse Sql to check if PK is being created. I don't think there's another option.

Example:

create trigger trgTable
on database
for create_table, alter_table
as
set nocount on

declare @data xml, @obj varchar(255), @type varchar(255), @cmd varchar(max)
set @data = EVENTDATA()

select 
    @obj    = @data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(256)'), 
    @type   = @data.value('(/EVENT_INSTANCE/ObjectType)[1]', 'varchar(25)'), 
    @cmd    = @data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'varchar(max)')

-- now do your logic here

Post a Comment for "Compulsory Primary Key For Sql Server"