Index in SQL with examples

Data Storage:

  1. The data in SQL Server is stored in the form of data pages.  
  2. The size of each data page is 8kb.
  3. The data page contains the page header which is 96 bytes.
  4. The header will have previous pointer (PP), object id, next pointer (NP).
  5. The previous pointer pointing to the previous page, this value is NULL for first page.
  6. The next pointer pointing to the next page, it’s value is NULL for the last page.

Data Access:

In SQL Server Data will be accessed in two ways.

Table Scan : It starts from beginning of the table and scan each and every page for required data, it extracts the rows to meet the query criteria.

Using Index: When index is created for a table, it travels through index tree structure to determine the required rows and extracts the rows which will meets the query criteria.

User Defined Functions in SQL Server

The functions created by the user are called user defined functions.

 Types of user defined functions:

 1. Scalar Valued Functions
2. Table Valued Functions

1. Scalar Valued Functions:

The functions which return scalar (single value) to the calling environment are called scalar valued functions.

create function (function_name)
(@para1 datatype(size), ……@para n datatype (size))
retrun datatype
function body
return ( variable)
For executing the function
Declare @ var (datatype)
Set @var= owner.funciton_name ( para1, para2..)
Example: Write a function to find the sum of the two numbers
Create function add_sum ( @a int, @b int)
Return int
Declare @c int,
Set @c = @a+@b;
Executing the function
Declare @z int
Set @z= dbo.add_sum(3,5)

Aggregate and Group Functions in SQL and Oracle with examples

  • Aggregate functions will acts group of values at a time and returns a single value. It is also called Group functions.
  • Aggregate functions will ignore NULL values while calculating.
  • Here is the sample emp table using for solving examples.

emp sample table for solving examples
Emp Sample Table
1. Sum:
Sum is used to find the sum of values in a column.

Example:Write a query for find the total salary of emp table.
select sum(sal) from emp
2. Avg: Avg is used to find average values for given values.

Example:Write a query for find the average salary of emp table.

select avg(sal) from emp

Database (DB)Testing - Strategy, Concepts, Checklist with sample test cases

In software testing there will be so many types of testings we will be carried out to deliver the product as per the client needs or as per the requirements. In which database testing or db testing is the one which requires thorough knowledge on sql commands, stored procedures, functions and triggers. There will be so many database software available in the market like SQL Server, Oracle, MySQL, PostgreSQL, Hadoop (Hadoop distributed file system) etc., What ever may be the database software we are using there will be slight difference in SQL query syntax, however most of the things will be same in all software. Database testing or DB testing is a technique of validating the database in which data is getting inserted/updated/deleted in various tables on various databases. Database testing is will differ based on the type of the data you are going to verify. I mean based on the OLTP or OLAP (Online Analytical Processing) the method of database testing will differ. For OLTP (Online Transaction Processing) we are verifying the stored procedures and validating the data is inserted/deleted/modified properly or not. And for OLAP lot of scripts or DW jobs will comes into picture. Having extensive knowledge on database testing I can say database testing should be carried out manually, why because I didn't find any automation testing tool which fulfills the db tsting requirements. Whenever the project started and ready for testing initially we will get the packages, stored procedures, functions etc for QA. In the sense the Database testing concept is divided into two phases.

  1. Backend Database Testing
  2. Frontend Database Testing

Functions in SQL - Scalar - Mathematical, String, Date and Time functions

A function is a predefined programming segment specially used for specific and well defined task.

Functions in SQL Server:

     1. Scalar functions
     2. Aggregate or group functions

1. Scalar functions:

These scalar functions will take single value as input and returns a single value.
  • Mathematical Functions
  • String functions
  • Date time functions
  • System functions
  • Security functions
  • Confirmation functions
  • Cursor functions
  • System statistic functions
  • Text and image functions
We will concentrate on first three functions now.

Transactions in SQL

Transaction is a logical unit of work in a database.

Types of transactions:

1. Auto commit transactions
2. Explicit transactions
3. Implicit transactions.

1. Auto Commit Transactions:

Every individual DML statement is an auto commit transaction.
insert into dept values (50,'export','hyd')

Constraints in Database

  • Constraint in a database is a mechanism used by Oracle (or) SQL Server to control the unwanted or invalid data flow into a table. (or)
  • Enforcing the rules to maintain the data integrity.
  • This mechanism automatically activates when user performance a DML operation.
Types of Constraints:

  1. NOT NULL: It will not allows the NULL values, but allow the duplicate values.
  2. UNIQUE: It will now allows the duplicate values, but allows NULL values.
  3. Primary Key:
    1. It is a combination of unique, not null and clustered index.
    2. It will not allow NULL values and duplicated values.
     4. Default: It is used to insert default values into the column when user skips the column from insert statement.
     5. Check: It is used to impose a condition rule.
     6. Foreign Key:
  1. It is used to establish parent/child or Master/Detailed relationships between the tables.
  2. It always linked to primary key column or unique key column of master table.
  3. It will not allow NULL values.