Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2018 May SQL Server Coding Standards: Choosing Correct Data Type

SQL Server Coding Standards: Choosing Correct Data Type

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Prepared by Bihag Thaker

Choosing correct data type while designing tables is an important part of application designing.

Incorrect choice of data types in tables can cause database and application to perform inefficiently.
Following are some of the guidelines to choose a proper data type for a column:

Always try to choose the correct data type depending on the type and characteristics of the values that are to be stored in a particular column.

Use Unicode data types like NCHAR, NVARCHAR and NTEXT only if other complex language characters other than simple English like characters need to be stored which can’t be stored in CHAR, VARCHAR or TEXT data types.

For Unicode, data type it requires 2 bytes to store a single character which can lead the inefficient storage of data and query execution.

Always use variable length data types like VARCHAR and NVARCHAR rather than using fixed length data types like CHAR and NCHAR as variable length data types make use of storage space efficiently.
Use fixed length data types CHAR and NCHAR only when the number of characters in data is known and determined.

Wherever possible, use VARCHAR (MAX) and NVARCHAR (MAX) data types instead of using TEXT and NTEXT data types.

Always use correct choice in numeric and integer data types. Always choose the small data type which requires less storage size over the other big data types as far as it can accommodate all the possible values in a domain and serves the purpose of the application.

For example, if a CountryID needs to be stored associated with each country name and as far as we know that the total number countries are not greater than 255, then the correct choice of data type will be TINYINT and not SMALLINT or INT.

Also, columns which are expected to store the values that fall in the range of SMALLINT; define them only as SMALLINT and not INT.

Similarly, use only BIGINT data type only when it is expected from an application to have a value which falls outside of the range if INT data type.

Use DATETIME data types wisely. If you don’t expect a date value to be much small or much large which falls in the range of SMALLDATETIME data type and accuracy of more than a minute is not required then use SMALLDATETIME and not DATETIME as it requires less storage space than DATETIME data type.

Use DATETIME data type to store date values along with the time component and when time accuracy is required in milliseconds. If time component is not relevant, you should use DATE data type which stores only the date part.

Avoid choosing data types which require explicit typecasting at the time of performing DML operations.
For example, storing date value or integer value in a string data type (VARCHAR or NVARCHAR) and then converting them back to DATETIME or INT while querying data is considered a bad practice and should be avoided.

May 26, 2018Anvesh Patel
SQL Server Coding Standards: Best way to define Table IdentifiersSQL Server Coding Standards: Best practices with Constraints
Comments: 1
  1. Femquality
    May 29, 2018 at 2:26 am

    Anvesh Patel, thank you for your blog post.Really thank you! Awesome.

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

May 26, 2018 1 Comment SQL Server Coding Standardsbasic sql commands, basic sql queries, coding best practices, SQL, sql basics, sql coding best practices, sql commands, sql database, sql formatter, sql language, SQL Programming, sql queries, sql queries for practice, sql query formatter, sql server format, sqlcode
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....