Popular Posts
Thursday, 24 November 2016
Tuesday, 22 November 2016
Monday, 21 November 2016
Sunday, 20 November 2016
SQL Views
SQL Views
- Views are virtual tables that are compiled at run time. The data associated with views are not physically stored in the view, but it is stored in the base tables of the view. A view can be made over one or more database tables.
- Generally we put those columns in view that we need to retrieve/query again and again. Once you have created the view, you can query view like as table.
- We can make index, trigger on view.
- In Sql Server we make views for security purpose since it restricts the user to view some columns/fields of the table(s).
- Views show only those columns that are present in the query which is used to make view.One more advantage of Views is, data abstraction since the end user is not aware of all the data present in database table.
Syntax for View
- Views are virtual tables that are compiled at run time. The data associated with views are not physically stored in the view, but it is stored in the base tables of the view. A view can be made over one or more database tables.
- Generally we put those columns in view that we need to retrieve/query again and again. Once you have created the view, you can query view like as table.
- We can make index, trigger on view.
- In Sql Server we make views for security purpose since it restricts the user to view some columns/fields of the table(s).
- Views show only those columns that are present in the query which is used to make view.One more advantage of Views is, data abstraction since the end user is not aware of all the data present in database table.
Syntax for View
Types of Views
In Sql Server we have two types of views.
01. System Defined Views
02. User Defined Views
Let's take a look at User Defined views.
User Defined Views
These types of view are defined by users. We have two types of user defined views.
- Simple Views (When we create a view on a single table, it is called simple view.)
-Complex Views(When we create a view on more than one table, it is called complex view.)
Example of Simple View
In simple view we can insert, update, delete data. We can only insert data in simple view if we have primary key and all not null fields in the view.
Summary
In this article I try to explain the view in sql server with example. I hope after reading this article you understood what SQL VIEW is. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.
Share with your friends! Thanks.......!
SQL Union Vs UnionAll
UNION
- The UNION command is used to select related information from two tables, much like the JOIN command.
- However, when using the UNION command all selected columns need to be of the same data type.
- With UNION, only distinct values are selected.
UNION ALL
- The UNION ALL command is equal to the UNION command, except that UNION ALL selects all values.
- The difference between Union and Union all is that Union all will not eliminate duplicate rows, instead it just pulls all rows from all tables fitting your query specifics and combines them into a table.
- A UNION statement effectively does a SELECT DISTINCT on the results set. If you know that all the records returned are unique from your union, use UNION ALL instead, it gives faster results.
1. Employee table data
2. Customer table data
UNION Example (It removes all duplicate records)
UNION ALL Example (It just concatenate records, not eliminate duplicates, so it is faster than UNION)
Summary
In this article I try to explain the differences of UNION and UNION ALL in SQL Server with a simple example. I hope after reading this article you will be able to understand cursors in Sql Server. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.
Saturday, 19 November 2016
Delete, Truncate and Drop
Delete(It deletes all data, but not table with structure)
We can delete 1 or more or All rows from a table.
It supports " Where " clause
Slow performance
We can get back rows
It can invoke triggers
Truncate
We can delete all rows from a table. It is not possible to delete 1 or 2 rows.
It won't support " Where " clause.
High performance
We can't get back rows.
It can't invoke triggers.
Drop (It deletes complete table with structure)
Note#
Truncante is performs better than delete because when you delete the records from the database, database has to perform 2 actions.
1.delete from the database
2.write the deleted records into "rollback" segments.
But incase of "Truncate" the second activity is not required.
Summary
In this article I try to explain the diffrence between DELTE , TRUNCATE and DROP in SQL Server. I hope after reading this article you will be able to understand cursors in Sql Server. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.
SQL Cursors
Cursor
- Cursor is a database object to retrieve data from a result set one row at a time, instead of the T-SQL commands that operate on all the rows in the result set at one time.
- We use cursor when we need to update records in a database table in singleton fashion means row by row.
Life Cycle of Cursor
Declare Cursor
A cursor is declared by defining the SQL statement that returns a result set.Open
A Cursor is opened and populated by executing the SQL statement defined by the cursor.Fetch
When cursor is opened, rows can be fetched from the cursor one by one or in a block to do data manipulation.Close
After data manipulation, we should close the cursor explicitly.Deallocate
Finally, we need to delete the cursor definition and released all the system resources associated with the cursor.
Syntax to Declare Cursor
Declare Cursor SQL Command is used to define the cursor with many options that impact the scalability and loading behavior of the cursor. The basic syntax is given below
Syntax to Open Cursor
A Cursor can be opened locally or globally. By default it is opened locally. The basic syntax to open cursor is given below:
Syntax to Fetch Cursor
Fetch statement provides the many options to retrieve the rows from the cursor. NEXT is the default option. The basic syntax to fetch cursor is given below:
Syntax to Close Cursor
Close statement closed the cursor explicitly. The basic syntax to close cursor is given below:
Syntax to Deallocate Cursor
Deallocate statement delete the cursor definition and free all the system resources associated with the cursor. The basic syntax to close cursor is given below:
SQL SERVER – Simple Examples of Cursors
Summary
In this article I try to explain the basic of Cursor in SQL Server with a simple example. I hope after reading this article you will be able to understand cursors in Sql Server. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.
SQL Indexes
Indexes
Indexing is way to sort and search records in the table. It will improve the speed of locating and retrieval of records from the table.It can be compared with the index which we use in the book to search a particular record.
In Sql Server there are two types of Index
1) Clustered Index
2) Non Clustered Index
Clustered Index
- Clustered index physically stored the data of the table in the order of the keys values and the data is resorted every time whenever a new value is inserted or a value is updated in the column on which it is defined.
- In a table only 1 clustered index is possible.
- In a clustered Index, the leaf node contains the actual data.
Non Clustered Index
- In case of Non clustered index it create a separate list of key values (or created a table of pointers) which points towards the location of the data in the data pages.
- In a table 249 non clustered index is possible.
- In a non clustered Index, the leaf node contains the pointer to the data rows of the table.
Guidelines for Clustered Index
With few exceptions, always create clustered indexes on columns frequently used in queires.
Use Clustered Index on columns used in following queries:
1. Queries that return a range of values by using operators such as BETWEEN, >, >=, <, and <=.
2. Queries that return large result sets.
3. Queries that use JOIN clauses using Foreign Key columns.
4. Queries that use ORDER BY or GROUP BY clauses.
For example, an index on the columns specified in the ORDER BY or GROUP BY clause may remove the need for the Database Engine to sort the data, because the rows are already sorted. This improves query performance.
Use Clustered Index on following columns in a table:
1. Columns that are unique or contain many distinct values.
For example, an Author ID uniquely identifies authors. A clustered index or PRIMARY KEY constraint on the AuthorID column would improve the performance of queries that search for author information based on the Author ID.
A clustered index could also be created on author’s FirstName, MiddleName, LastName because author records are frequently grouped and queried in this way, and the combination of these columns would still provide a high degree of difference.
2. Columns that are frequently used to return a range of values by using operators such as BETWEEN, >, >=, <, and <=.
For example, a customer ID uniquely identifies customers in the Customer table. Queries which consist of sequential search, such as WHERE CustomerID BETWEEN 900 and 999, would benefit from a clustered index on CustomerID. This is because the rows would be stored in sorted order on that key column.
3. Columns that are defined as IDENTITY because these columns are guaranteed to be unique within the table.
4. Columns that are frequently used to sort the data retrieved from a table (using ORDER BY clause).
It can be a good idea to cluster (i.e. physically sort) the table on that column to save the cost of a sort operation every time the column is queried.
Clustered Indexes are NOT good for the following columns in a table:
1. Columns that undergo frequent changes.
This causes the whole row to move which affects the performance, because the Database Engine must keep the data values of a row in physical order. This is an important consideration in large-volume transaction processing systems in which data is typically large and volatile.
2. Columns used in Composite keys.
Guidelines for Non-Clustered Index
Consider creating one or more non-clustered indexes on databases or tables with low update requirements, but large volumes of data to improve query performance.
Please note that, large numbers of indexes on a table affect the performance of INSERT, UPDATE, DELETE, and MERGE statements because all indexes must be adjusted appropriately as data in the table changes. Therefore, avoid creating indexes on this kind of tables.
Use Non-Clustered Index on columns used in following queries:
1. Queries that use JOIN or GROUP BY clauses.
Create one or more non-clustered indexes on columns involved in join and grouping operations, and a clustered index on any foreign key columns.
2. Queries that do not return large result sets.
3. Queries that contain frequently involved columns in search conditions, such as WHERE clause, which returns exact match.
Use Non-Clustered Index on following columns in a table:
1. Create non-clustered index on non-key columns that are frequently used in queries.
2. Columns with lots of distinct values, such as a combination of last name and first name, if a clustered index is used for other columns.
A table can have multiple non-clustered indexes?
A table can have multiple non-clustered indexes because they don’t affect the order in which the rows are stored on disk like clustered indexes.
Why can a table have only one clustered index?
Because a clustered index determines the order in which the rows will be stored on disk, having more than one clustered index on one table is impossible. Imagine if we have two clustered indexes on a single table – which index would determine the order in which the rows will be stored? Since the rows of a table can only be sorted to follow just one index, having more than one clustered index is not allowed.
Summary
In this article I try to explain the basic of Cursor in SQL Server with a simple example. I hope after reading this article you will be able to understand cursors in Sql Server. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.
Tuesday, 15 November 2016
JavaScript Class
Class in JavaScript
JavaScript ECMAScript 5, does not have class type. So it does not support full object oriented programming concept as other languages like Java or C#. However, you can create a function in such a way so that it will act as a class.
The following example demonstrates how a function can be used like a class in JavaScript.
As you know, we can create an object of any function using new keyword, so person1 object is created with new keyword. So now, Person will act as a class and person1 & person2 will be its objects (instances). Each object will hold their values separately because all the variables are defined with this keyword which binds them to particular object when we create an object using new keyword.
So this is how a function can be used like a class in the JavaScript.
Add Methods in a Class:
We can add a function expression as a member variable in a function in JavaScript. This function expression will act like a method of class.
In the above example, the Person function includes function expression that is assigned to a member variable getFullName. So now, getFullName() will act like a method of the Person class. It can be called using dot notation e.g. person1.getFullName().
Constructor:
In the other programming languages like Java or C#, a class can have one or more constructors. In JavaScript, a function can have one or more parameters. So, a function with one or more parameters can be used like a constructor where you can pass parameter values at the time or creating an object with new keyword.
In the above example, the Person function includes three parameters FirstName, LastName and Age. These parameters are used to set the values of a respective property.
Note : Please notice that parameter assigned to a property, if parameter value is not passed while creating an object using new then they will be undefined.
Subscribe to:
Posts (Atom)