SQL Server: Loading data and creating primary keys.


I am creating database tables and loading data into them. Then I add Primary Key / Identity column to the table (with default clustered index). Is it right design? Or it is better to create Identity column and then load data?

Thanks in advance.
Who is Participating?
well...it depends.  :)
It depends on your goal.  Normally the constriants are defined first, but if this table is just bulk loaded into once, then loading the data and then defining the constraints/indexes is likely just fine.
Data load can always be done without primary key. If the table doesn't have constraints imposed then the data load is faster. Since load process doesn't have to check constraints for each insert.

you can very well add identity column later on. i can guess this is one time process. Since if we load the data again this will have primary key already present.

Anthony PerkinsCommented:
>>Is it right design? Or it is better to create Identity column and then load data?<<
The best approach may be both. :)

In other words create your table with the Primary Key and create a staging table without the constraints. You can then import the data into your staging table and then validate it, prior to copying it to your final Production table.
If you create the indexes first and then load the data, it will take longer to load the large amount of data.  However, if you create the indexes after you load, it will take some time to load the idexes.
It doesn't matter which way you do it.  The best way to find out is to try it out on your test system, this will tell which way is the fastest.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.