Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the preferred way of coding the database?

 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which way do you prefer to code the Database?

1)


2)




Thanks
Jack
 
chris webster
Bartender
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't do "SELECT * FROM customer".

It's quite rare that you really need every column from every record in a given table for a DB application, and fetching unnecessary data just adds extra work and uses up bandwidth. Select specific columns, and only for the records you want i.e. use a WHERE clause wherever possible.

Another problem with "SELECT * ..." is that the table structure may change. Columns may be added/removed from the table, or the column names/data types might change, but there's nothing in your SQL statement to tell the next programmer whether they need to change your SQL to take account of these changes. Make your intentions for your SQL query explicit - select specific columns by name - so that maintaining the program is easier when (not if) the DB changes.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Along with the good advice from Chris, the style of option 2 is definitely the way to go. You don't want calling code to have to know about SQL strings or result sets. It simply needs to provide the ID of the customer to retrieve and the method does the rest.
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it, thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic