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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.