Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Code Smells

Tags:

sql

Could you please list some of the bad practices in SQL, that novice people do?

I have found the use of "WHILE loop" in scenarios which could be resolved using set operations.

Another example is inserting data only if it does not exist. This can be achieved using LEFT OUTER JOIN. Some people go for "IF"

Any other thoughts?

Edit: What I am looking for is specific scenarios (as mentioned in the question) that could be achieved using SQL without using procedural constructs

Thanks

Lijo

like image 638
Lijo Avatar asked Apr 17 '10 06:04

Lijo


People also ask

What is smell in coding?

In computer programming, a code smell is any characteristic in the source code of a program that possibly indicates a deeper problem. Determining what is and is not a code smell is subjective, and varies by language, developer, and development methodology.

How do you fix code smells?

Take a God object, separate its data and functions according to what problems they exist to solve, then turn those groupings into objects. If you have a God object, it may be better off as a composition of many smaller objects.

What is C# smell?

Code smell is a word which was popularized by Kent Beck on words wiki. It is any characteristic in programing source code that indicates a deeper problem in our code. Code smell is not a bug but it is a sign of bad construction of your program.


1 Answers

Here are some I have seen:

  • Using cursors instead of equivalent (and faster) set operations (joins etc).
  • Dynamic SQL for everything.
  • Code that is open to SQL Injection attacks.
  • Full outer joins even when they are not needed.
  • Huge stored procedures (hundreds/thousands of lines).
  • No comments.
like image 93
Oded Avatar answered Sep 28 '22 19:09

Oded