Primary/Foreign Key naming and DDL syntax

I am using SQL 2012 and want my CREATE TABLE DDL statements to look something like this:

create table mytable (mypkcol int not null primary key, myfkcol int not null foreign key references myothertable(myothertable_pkcol))

or, for tables with multi-column primary keys, I like to use the syntax:

create table mytable (mypkcol1 int not null, mypkcol2 int not null, myothercol varchar(20) null, primary key (mypkcol1,mypkcol2))

By using this syntax to define table constraints, SQL will auto-create constraint names and I don’t have to manage a naming convention or bother with making sure all my constraint names make sense.

I haven’t been able to figure out a way to do this. I did figure out how to generate the PK inside the create table, but it still wants to use the primary key name to name the PK constraint.

Any tips? Thanks!


TDM always generates PK names, that’s how the generator is designed. You can add your suggestion to Idea Pond and we regularly review suggestions there and consider them for implementation.

Should you want this feature immediately, you can try to edit generator script via Expert Mode | Customization, backup the original script just in case first.



thanks for the quick response. Maybe I should change my ways and investigate TDM’s ability to auto-name the constraints for me in a way that makes some sense.

I haven’t yet investigated the customization option - I will have a look at it, thanks.

Yes, try to make use of the built-in functionality.

As for the customization, what db platforms and versions does this case include for you?