Microsoft SQL Server

What is Normalization? What are different Normalization forms?

In relational database design, the process of organizing data to minimize redundancy is
called normalization. Normalization usually involves dividing a database into two or more tables and defining relationships between the tables. The objective is to isolate data so that additions, deletions, and modifications of a field can be made in just one table and then propagated through the rest of the database via the defined relationships.

1NF: Eliminate Repeating Groups Make a separate table for each set of related attributes, and give each table a primary key. Each field contains at most one value from its attribute domain.
2NF: Eliminate Redundant Data If an attribute depends on only part of a multi-valued key, remove it to a separate table.
3NF: Eliminate Columns Not Dependent On Key If attributes do not contribute to a description of the key, remove them to a separate table. All attributes must be directly dependent on the primary key.
BCNF: Boyce-Codd Normal Form If there are non-trivial dependencies between candidate key attributes, separate them out into distinct tables.
4NF: Isolate Independent Multiple Relationships No table may contain two or more 1:n or n:m relationships that are not directly related.
5NF: Isolate Semantically Related Multiple Relationships There may be practical constrains on information that justify separating logically related many-to-many relationships.
ONF: Optimal Normal Form A model limited to only simple (elemental) facts, as expressed in Object Role Model notation.
DKNF: Domain-Key Normal Form A model free from all modification anomalies is said to be in DKNF.

Remember, these normalization guidelines are cumulative. For a database to be in 3NF, it must first fulfill all the criteria of a 2NF and 1NF database.

Microsoft SQL Server

Tips for Optimizing QUERY in SQL Server

  • Always try to use views and stored procedures instead of doing work with heavy queries.
  • Make a habit to use constraints instead of triggers whenever it is possible.
  • When you need n number of row from database try to use top keyword or SET ROWCOUNT statement.
  • Always use table variables in place of temporary tables.
  • Avoid Union and try to use UNION ALL statement.
  • Always avoid using the DISTINCT clause, whenever possible.
  • Always try to avoid using SQL Server cursors.
  • Always try to avoid the HAVING clause.
  • Donot use select count(*) to get number of rows
  • Try to include SET NOCOUNT ON statement into your stored procedures to stop the message indicating the number of rows affected by a T-SQL statement.
  • Always use file system to store large binary objects and use the file path in database.
  • Sometimes we may have to apply more than one subqueries in our main query. Try to minimize the number of subquery block in your query.
  • Try to use column name insted of *