Microsoft SQL Server

STORE PROCEDURE in SQL Server

A stored procedure is a named group of SQL statements that have been previously created and stored in the server database.

Stored procedures accept input parameters so that a single procedure can be used over the network by
several clients using different input data. And when the procedure is modified, all clients automatically get the new version.

Stored procedures reduce network traffic and improve performance.

Stored procedures can be used to help ensure the integrity of the database.

 CREATE PROCEDURE usp_SelectRecord  
AS
BEGIN
SELECT * FROM TABLE
END
GO

Error Handling in SQL Server

 CREATE PROCEDURE procedure_name AS  
BEGIN TRY
PRINT 'My name is Himen'
SELECT * FROM dbo.Users
PRINT 'This will not print - My name is Himen'
END TRY
BEGIN CATCH
PRINT 'And nor does this print'
END CATCH
go
Microsoft SQL Server

TABLES in SQL Server

Creating SQL Server Tables Syntax

 CREATE TABLE Persons  
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

Alter SQL Server Table syntax (ADD Column)

 ALTER TABLE table_name ADD column_name datatype  

Alter SQL Server Table syntax (DROP Column)

 ALTER TABLE table_name DROP column_name datatype  
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 *