What is SQL Indexes? Type of SQL Indexes.


What is SQL Indexes? Type of SQL Indexes.

Indexes are lookup tables that the database search engine can use to speed up data retrieval. Put, an index is a pointer to data in a table. An index in a database is very similar to an index in a book
An index helps speed up SELECT command and WHERE clauses, but it slows down data input, with UPDATE and INSERT statements.
Indexes can be created or dropped with no effect on the data
Creating an index need the CREATE INDEX statement, which allows you to name the index, to specify the table and which column or columns to index, and to indicate whether the index is in ascending or descending order

The CREATE INDEX Command:
  CREATE INDEX index_name ON table_name;

Single-Column Indexes:
CREATE INDEX index_name on table_name (column_name);

Composite Indexes:
A composite index is an index on two or more columns of a table
CREATE INDEX index_name on table_name (col1, col2);

Whether to create a single-column index or a composite index, take into consideration the column(s) that you may use very often in a query's WHERE clause.

Implicit Indexes:
Implicit indexes are indexes that are automatically created by the database server when an object is created. Indexes are automatically created for primary key constraints and unique constraints

The DROP INDEX Command:
An index can be dropped using SQL DROP command. Care should be taken when dropping an index because performance may be slowed or improved
DROP INDEX index_name

We should avoid indexes in.

Though indexes are intended to enhance a database's performance, there are times when they should be avoided. The following guidelines indicate when the use of an index should be reconsidered

  • It should not be used on small tables.
  • Tables that have frequent, large batch update or insert operations.
  • It should not be used on columns that contain a high number of NULL values.
  • Columns that are frequently manipulated should not be indexed. 


SQL Useful Functions

SQL Useful Functions

SQL has many built-in functions for performing processing on string or numeric data. Following is the list of all useful SQL built-in functions

  • SQL COUNT Function - The SQL COUNT aggregate function is used to count the number of rows in a database table.
  • SQL MAX Function - The SQL MAX aggregate function allows us to select the maximum value for a certain column.
  • SQL MIN Function - The SQL MIN aggregate function allows us to select the minimum value for a certain column.
  • SQL AVG Function - The SQL AVG aggregate function selects the average value for certain table column.
  • SQL SUM Function - The SQL SUM aggregate function allows selecting the total for a numeric column.
  • SQL SQRT Functions - This is used to generate a square root of a given number.
  • SQL RAND Function - This is used to generate a random number using SQL command.
  • SQL CONCAT Function - This is used to concatenate any string inside any SQL command.
  • SQL Numeric Functions - Complete list of SQL functions required to manipulate numbers in SQL.
  • SQL String Functions - Complete list of SQL functions required to manipulate strings in SQL.



Rules to create SQL Sub Queries


Rules to create SQL Sub Queries

A Subquery or Inner query or Nested query is a query within another SQL query and embedded within the WHERE clause.

A subquery is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved.

Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the operators like =, >=, <=, IN, BETWEEN etc.

There are a few rules that subqueries must follow:
  • Subqueries must be enclosed within parentheses.
  • A subquery can have only one column in the SELECT clause, unless multiple columns are in the main query for the subquery to compare its selected columns.
  • The BETWEEN operator cannot be used with a subquery; however, the BETWEEN operator can be used within the subquery.
  • Subqueries that return more than one row can only be used with multiple value operators, such as the IN operator.
  • The SELECT list cannot include any references to values that evaluate to a BLOB, ARRAY, CLOB, or NCLOB.
  • A subquery cannot be immediately enclosed in a set function.
  • An ORDER BY cannot be used in a subquery, although the main query can use an ORDER BY. The GROUP BY can be used to perform the same function as the ORDER BY in a subquery. 

To Know More On Subquery Please check 




SQL HAVING CLAUSE


The HAVING clause enables you to specify conditions that filter which group results appear in the final results. The WHERE clause places conditions on the selected columns, whereas the HAVING clause places conditions on groups created by the GROUP BY clause.

Syntax: 

SELECT ...
FROM ...
WHERE ...
GROUP BY ...
HAVING ...
ORDER BY ...

The HAVING clause must follow the GROUP BY clause in a query and must also precede the ORDER BY clause if used. The following is the syntax of the SELECT statement, including the HAVING clause:

SELECT column1, column2
FROM table1, table2
WHERE [ conditions ]
GROUP BY column1, column2
HAVING [ conditions ]

ORDER BY column1, column2

Configuring session state on SQL server



There are 2 steps for setting up the ASPState database and the formation setting in your web.config.

you need to use ASP_RegSQl.exe from a command prompt. The script file and the aspregsql EXE are found at
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727.
Formation Steps:
1     Open a command prompt and locate the following path:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 based on your OS version and .NET version

      Use the following statement:

a.       Using default ASPState database and SQL security
aspnet_regsql -S serverName -U UserName -P Password -ssadd -sstype p

b.       Using default ASPState database and windows security
aspnet_regsql -S serverName -E -ssadd -sstype p

c.       Using custom database and SQL security
aspnet_regsql -d TableName -S serverName -U UserName -P Password -ssadd -sstype c

         1) t - Stores session data in the SQL Server tempdb database. This is the default. If you store session data in the tempdb database, the session data is lost if SQL Server is restarted.

         2)   p - Stores session data in the ASPState database instead of in the tempdb database.

         3)  c - Stores session data in a custom database. If you specify the c option, you must also include the name of the custom database using the -d option.

     In your configuration file [web.config]:

       A) Using default SQL security:

<sessionstate mode="SQLServer" timeout="60" allowcustomsqldatabase="true"
sqlconnectionstring="Data Source=Server;User ID=sa;Password=sa;"
cookieless="false">

      B) Using default windows security:
<sessionstate mode="SQLServer" timeout="60" allowcustomsqldatabase="true"
sqlconnectionstring=" Data Source=Server;Integrated-Security=SSPI;”
cookieless="false">

      C) Custom database name:
<sessionstate mode="SQLServer" timeout="60" allowcustomsqldatabase="true"
sqlconnectionstring=" Data Source=Server; Initial Catalog = tblname; User ID=sa;Password=sa;”
cookieless="false">

NOTE: Things to lookout for when using SQL to store the session info in a web form situation:
    a)  The machine key among the servers' needs to be the same as AspState Session info is encrypted using the machine key.

    b) The application path to your websites on all machines needs to be constant as well.

Why we can't execute a stored procedure from a User Defined function(UDF)

Functions cannot "touch" any database but read them only. Stored procedures can do anything and everything with databases. You ...