Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why creating Tables in run-time (code behind) is bad?

Tags:

c#

sql

People suggest creating database table dynamically (or, in run-time) should be avoided, with the saying that it is bad practice and will be hard to maintain.

I don't see the reason why, and I don't see difference between creating table and any another SQL query/statement such as SELECT or INSERT. I wrote apps that create, delete and modify database and tables in run time, and so far I do not see any performance issues.

Can anyone explane the cons of creating database and tables in run-time?

like image 398
KMC Avatar asked May 14 '11 04:05

KMC


2 Answers

Tables are much more complex entities than rows and managing table creation is much more complex than an insert which has to abide by an existing model, the table. True, a table create statement is a standard SQL operation but depending on creating them dynamically smacks of a bad design decisions.

Now, if you just create one or two and that's it, or an entire database dynamically, or from a script once, that might be ok. But if you depend on having to create more and more tables to handle your data you will also need to join more and more and query more and more. One very serious issue I encountered with an app that made use of dynamic table creation is that a single SQL Server query can only involve 255 tables. It's a built-in constraint. (And that's SQL Server, not CE.) It only took a few weeks in production for this limit to be reached resulting in a nonfunctioning application.

And if you get into editing the tables, e.g. adding/dropping columns, then your maintenance headache gets even worse. There's also the matter of binding your db data to your app's logic. Another issue is upgrading production databases. This would really be a challenge if a db had been growing with objects dynamically and you suddenly needed to update the model.

When you need to store data in such a dynamic manner the standard practice is to make use of EAV models. You have fixed tables and your data is added dynamically as rows so your schema does not have to change. There are drawbacks of course but it's generally thought of as better practice.

like image 172
Paul Sasik Avatar answered Oct 13 '22 18:10

Paul Sasik


KMC ,

Remember the following points

  1. What if you want to add or remove a column , you many need to change in the code and compile it agian
  2. what if the database location changes
  3. Developers who are not very good at database can make changes , if you create the schema at the backend , DBA's can take care of it.
  4. If you get any performance issues , it may get tough to debug.
like image 2
kobe Avatar answered Oct 13 '22 16:10

kobe