Monday 15 October 2012

DISTINCT

Distinct statement eliminates the duplicate data in the specified column from the table.

In a table, some of the columns may contain duplicate values. Sometimes you will want to list only the different (distinct) values in a table.

The DISTINCT keyword can be used to return only distinct (different) values.
 
DISTINCT Syntax:-
 
SELECT DISTINCT <Column_Name> FROM <Table_Name>
 
DISTINCT Example:-
 
We have a table called “salestaxRate” and columns are SalesTaxRateID,  TaxType, TaxRate, Name, ModifiedDate  as shown below...


Now we want to select only the distinct values (Different Values) from the column named "TaxRate" from the table above.

We use the following SELECT statement:

SELECT DISTINCT TaxRate FROM salestaxRate

The result-set will look like this

Saturday 6 October 2012

String Functions in SQL

String Functions:-
Before we start look at the "String Functions", first we should understand what is string? and what is functions.

What is String?
In SQL, Collection of character or combination of word is called as "String". Data whichever stored using following datatype is called as string
  • Varchar
  • NVarchar
  • Text
  • Char
  • NChar
What is Functions?
Function is a set of code or procedure which will perform some specfic operation is known as functions.

What is String Functions?
The Function which is written to perform string related opearations like...
  • Findling character length
  • Searching some character from collection of texts/paragraphs
  • Replacing some character with new character
  • Spliting a word based on some delimiter
  • etc.,
Hope above explanation is helpful to understand about what is string functions
 
The following commands are the functions which are used to perform the string operations..
  • ASCII
  • NCHAR
  • SOUNDEX
  • CHAR
  • PATINDEX
  • SPACE
  • CHARINDEX
  • QUOTENAME
  • STR
  • DIFFERENCE
  • REPLACE
  • STUFF
  • LEFT
  • REPLICATE
  • SUBSTRING
  • LEN
  • REVERSE
  • UNICODE
  • LOWER
  • RIGHT
  • UPPER
  • LTRIM
  • RTRIM
Note: We will Explain about each functions in detail in further posts..

Sunday 23 September 2012

Methods to Create a Table in DB

In Previous post “SQL Table”, we’ve explained about the definition of the Table. This article will explain about “what are the ways we can create table in SQL Server”.

We can create a Table in Database in many ways, but most main method are,
  • Using SQL Scripts 
  • Using Database Tool
Using SQL Scripts: 

There are many database tools allow you to create tables without writing SQL State scripts, but given that tables are the container of all the data, all it is one of the fundamental object of the database, so it would be good if you learn thru SQL Scripts.

The CREATE TABLE statement is used to create a table in a database.

Syntax of CREATE TABLE:


Following are the basic syntax of creating a table
 

        Create table
        (
            column1 ,
            column2 ,
            column3 ,
            <...>  <...>  <...>
        )
Sample Table: EMPLOYEE
 

Execute the following Query at SQL Server management studio; it will create a Table called “Employee”
 

        Create table Employee
        (
            Emp_ID  int not null,
            First_Name varchar(50),
            Last_Name varchar(50),
            Date_of_Joining date,
            Department  varchar(50)
        )


Using Database Tool (SSMS):
 

In this method, we could create a table using the tool, SQL Server Management Studio(SSMS). 

Follow the steps below to create a table.
 

1. Open SSMS from Start Menu as shown below

2. Login/connect to the SQL Server by providing proper credentials


3. After logged in, Decide the database in which you are planning to create a table. Here am planning to create a table under “TestDB”, refer the exhibit below


4. Right click on the the Node “Tables”, select “New Table”


5. A spreadsheet will appear with 3 columns like “Column Name”, “Data Type”, “Allow Nulls”


6. Fill the sheet with columns name and its data type as shown below


7. Click Save Button from the Toolbar or Press Ctrl+S Keys
 

8. You will see a popup window with a input field, enter the table name here , press OK Button

Now you can see a new Table, Which you created, under Tables Node.


Hope this helps!.. please leave your comments here if you need any help on this..


Happy Learning.. :)

Sunday 9 September 2012

SQL Table

Tables are the basic structure where data is stored in the database. Hence table is one of the Database object.

Table consists of following Objects:-
  • Rows
  • Columns
  • Attributes
  • Relations
  • Data types
  • Constraints
  • etc.,
Table is a set of data elements (values) that is organized using a model of vertical columns and horizontal rows.

Note: A table has a specified number of columns but can have any number of rows.

What is RDBMS?

RDBMS is stands for Relational DBMS.
  • RDBMS is the extension of DBMS.
  • DBMS is built to support relational Model is known as RDBMS
  • DBMS are for smaller organizations with small amount of data, where security of the data is not of major concern and RDBMS are designed to take care of large amounts of data and also the security of this data.
  • In the Relational Data Model, Data represents in the form of table.
  • DBMS that stores the data in the form of Related Tables is known as RDBMS.
Refer the screenshot to know about various components of the basic Relational Model “Table”
 
Advantages of RDBMS:-
  • Single database can be categorized/split across different tables, which is differ from flat-file database that has one single table to store the entire database data.
  • In RDBMS, Same Database can be analyzed/viewed in different ways
  • RDBMS Data is structured in form of DB tables, records, fields, etc.,
Example for RDBMS:-
The most popular RDBMS are MS SQL Server, DB2, Oracle, MySQL and MS Access

Tuesday 28 August 2012

What is DBMS?

DBMS stands for ... Database Management System.

This system is assisting us to manage/store the Data accurately and very easiest way

DBMS is a collection of programs which helps to handle the data in following ways..
    * Adding Data
    * Deleting Unnecessery Data
    * Modifying the Existing Record

A DBMS support various functions like data security, data integrity, data sharing, data concurrence, data independence, data recovery etc. However, all database management systems that are now available in the market like Sybase, Oracle, and MS-Access do not provide the same set of functions, though all are meant for data management.

Tuesday 21 August 2012

How to Create Database?

Database can be created using following ways:-
  1. Using SQL Script
  2. Using Wizard
SQL Script:-
Create Database statement is used to create a database in SQL Server.

Syntax: 
Following SQL Syntax helps to create a database
 

CREATE DATABASE <database_Name>
 
Sample:
Execute the following Query at SQL Server management studio, it will create a Database named as “TestDB”
 
CREATE DATABASE TestDB

Wizard:-
 
Database can be created using SQL Server management studio, follow the steps given below to create a DB
 
1.    Open SQL Server management studio from Program menu (start Menu)
 
 
2.    Login/connect to the SQL Server by providing proper credentials
 
 
3. After connected to the server, Select the Database Node in the Object Explorer --> Right Click on the Database node, Select “New Database” (refer the Exhibit given below for further reference)
 
 
4.    New Database Wizard will appear as shown below,
5.    Enter the name of the database in “Database name” text field.
 

6.    Click Ok to create the database 

7.    Once it is created, it will be display under database node as shown below

 To Know More about Database, Refer the Article posted at,
http://blog-sqllearning.blogspot.in/2012/08/what-is-database.html
 

Saturday 18 August 2012

What is Database?

Database is a system in which we can store and manage the data in the organized way. It is abbreviated as DB.

It is a Repository in which we can store the collections of data. data are stored in database in the form of tables and other database objects as mentioned below:-
  • Table - Combination of rows and columns
  • Tables - Collections of Table
  • Views - Virtual Table
  • Stored procedure & function - collection of logical scripts
  • etc.,
Relational Database Management System (RDBMS) helps to access the data information from the database.

Following are the examples of the RDBMS:-
  • MsAccess
  • SQL Server
  • Oracle
  • etc.,
Refer the article given below to know about "How to Create a Database"
http://blog-sqllearning.blogspot.in/2012/08/how-to-create-database.html

What is DCL?


DCL stands for.. Data Control Language

DCL Provides the access permission over the data which is stored in the database.

DCL Commands Includes,
  • Grant - Command will permission to the user to do specific operations.
  • Revoke - command will remove the given permission from the user.
As mentioned above, DCL commands will allow or deny the following operations..
CONNECT
SELECT
INSERT
UPDATE
DELETE
EXECUTE

Monday 13 August 2012

What is TCL?


TCL Stands for...Transaction Control Language
  • TCL is a subset of SQL,
  • TCL helps to control the Transaction process in RDBMS.
TCL commands includes,
  • Begin Transaction - Marks the starting point of transaction.
  • Commit Transaction - Marks the end of a successful implicit or explicit transaction.
  • Rollback Transaction - Rolls back an explicit or implicit transaction to the beginning of the transaction

What is Transaction in SQL?

A transaction is a sequence of operations performed as a single logical unit of work, usually a group of DML statements.

A logical unit of work must follow four properties to qualify as a transaction,
  • Atomicity,
  • Consistency,
  • Isolation,
  • and Durability
These Properties are called as Transaction Property, Some people will call as ACID Property.

Atomicity: A transaction must be an atomic unit of work; either all of its data modifications are performed or none of them is performed.

Consistency: When completed, a transaction must leave all data in a consistent state. In a relational database, all rules must be applied to the transaction's modifications to maintain all data integrity. All internal data structures, such as B-tree indexes or doubly-linked lists, must be correct at the end of the transaction.

Isolation: Modifications made by concurrent transactions must be isolated from the modifications made by any other concurrent transactions. A transaction either sees data in the state it was in before another concurrent transaction modified it, or it sees the data after the second transaction has completed, but it does not see an intermediate state. This is referred to as serializability because it results in the ability to reload the starting data and replay a series of transactions to end up with the data in the same state it was in after the original transactions were performed.

Durability: After a transaction has completed, its effects are permanently in place in the system. The modifications persist even in the event of a system failure.

Sunday 12 August 2012

What is DML?


DML Stands for... Data Manipulation Language
  • DML is a language of SQL, It will be supported by RDBMS System
  • DML is helps to manipulate the data into relational database
  • DML modify the stored data (not the structure of DB object) in the database
  • DML is purely handling the data in the database in different ways like loading/removing/modifying the data in the database
Following SQL commands are part of DML
SELECT
SELECT ... INTO (It is exists in DDL & DML)
INSERT INTO
UPDATE
DELETE
TRUNCATE
Data manipulation languages are mainly classified as 2 type, procedural programming and declarative programming.

Procedural Programming : Collection of SQL Statements
Declarative Programming: The individual SQL statements are declarative

Tuesday 7 August 2012

What is DDL?


DDL Stands For...
Data Definition Language (or)
Data Description Language

Ø  DDL is a kind of programming language which helps to create Database objects like ...
Table
Columns
object Relationships
Views
Procedures
Functions
Indexes
Packages
Triggers
Types
Synonyms
Database
Linked Objects
Security
etc.,
Ø  The Script which is used to create db objects like above also called as Schema which will be stored in Data Dictionary
Ø  DDL will be supported by Database Management Systems (DBMS)
Ø  The script which helps to create/define/Modify/Remove an objects in DB will be part of DDL
Ø  Following are the general DDL Commands...
o    CREATE - To create New objects in a database
o    ALTER - Modify the structure of Existing objects database
o    DROP - Delete/Remove an objects from the database
o    TRUNCATE - Remove the data page which is attached to a table object,
o    COMMENT - Add comments to the Data dictionary
o    RENAME - rename an Existing DB object
o    SELECT ... INTO - Creating new object by using existing object schema, it will not capture identity definition and field relationship

Saturday 7 July 2012

What is SQL?



SQL is a standard language to manage the Relational Database Management system (RDBMS)

SQL stands for …
Structured Query language (Or)
Standard Query Language

History of SQL:-
SQL is called as different names like SQUARE, SEQUEL, SQL and T-SQL

SQUARE - Specifying QUeries As Relational Expressions
SEQUEL - A Structured English Query Language
SQL - Structured Query language
T-SQL – Transact SQL

Elements of SQL language:
The SQL language is subdivided into several language elements, including: Clauses, Expressions, Predicates, Queries, Statements, etc..,

Clauses:  which are constituent components of statements and queries.

Expressions: which can produce either scalar values or tables consisting of columns and rows of data.

Predicates: which specify conditions that can be evaluated to SQL three-valued logic (3VL) or Boolean (true/false/unknown) truth values and which are used to limit the effects of statements and queries, or to change program flow.

Queries: which retrieve the data based on specific criteria. This is the most important element of SQL.

Statements: which may have a persistent effect on schemata and data, or which may control transactions, program flow, connections, sessions, or diagnostics.
 
  • SQL statements also include the semicolon (";") statement terminator. Though not required on every platform, it is defined as a standard part of the SQL grammar. 
Insignificant whitespace is generally ignored in SQL statements and queries, making it easier to format SQL code for readability.
SQL Language Categories:-
SQL is classified into 4 major categories as below:-

DDL - Data Definition Language
DML - Data Manipulation Language
DCL - Data Control Language
TCL - Transaction Control Language

We will look at it in-detail later posts..