r/SQLOptimization • • Aug 08 '24

Automating Primary Key generation

Defining a primary key has always been a manual task and however we are rapidly moving towards automation, this task has been overlooked. I work in a company where ETL is my forte. So I've pitched to write a stored procedure that identifies the columns that optimally define a unique row in the table. So far I've put forward these points which will have some weightage while deciding such columns: • Cardinality • Column Data Type • Column Name What else would you add? Any suggestions on how to proceed with this?

2 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/stuart_lit Aug 08 '24

What do you mean here by auto increments?

So I'd explain the workflow: We get some raw data and then we define data types on those columns while putting it into a new table and assign a primary key. Till now we have automated the process data type allocation. Now we are trying to work on automating the primary key. Considering NULLs is a good idea, will keep that in consideration.

2

u/Apoffys Aug 08 '24

What do you mean here by auto increments?

Essentially, a new column for the primary key (often just named "id" or similar) with a generated, unique value. Often this is an integer that is "auto incremented", as each time you insert a row this value is incremented by 1 and assigned as the ID for the new column. An alternate solution is to use a randomly generated string (i.e. GUID) as the ID for each column.

Most database management systems have some sort of built-in feature to handle this for you and it's the "standard" way of handling the issue. There are drawbacks, but it saves a lot of headache and potential problems. Is there a reason you're not using this?