Thursday, October 21, 2010

SQL SYNTAX
Select Statement
SELECT "column_name" FROM "table_name"
Distinct
SELECT DISTINCT "column_name"
FROM "table_name"
Where
SELECT "column_name"
FROM "table_name"
WHERE "condition"
And/Or
SELECT "column_name"
FROM "table_name"
WHERE "simple condition"
{[AND/OR] "simple condition"}+
In
SELECT "column_name"
FROM "table_name"
WHERE "column_name" IN('value 1','value 2',...)
Between
SELECT "column_name"
FROM "table_name"
WHERE "column_name" BETWEEN 'value1' AND 'value2'

Like
SELECT "column_name"
FROM "table_name"
WHERE "column_name" LIKE {PATTERN}
Order By
SELECT "column_name"
FROM "table_name"
[WHERE "condition"]
ORDER BY "column_name" [ASC,DESC]
Count
SELECT COUNT("column_name")
FROM "table_name"
Group By
SELECT "column_name1", SUM("column_name2")
FROM "table_name"
GROUP BY "column_name1"
Having
SELECT "column_name1", SUM("column_name2")
FROM "table_name"
GROUP BY "column_name1"
HAVING (arithmetic function condition)
Create Table Statement
CREATE TABLE "table_name"
("column 1" "data_type_for_column_1",
"column 2" "data_type_for_column_2",
...)
Drop Table Statement
DROP TABLE "table_name"
Truncate Table Statement
TRUNCATE TABLE "table_name"
Insert Into Statement
INSERT INTO "table_name" ("column1","column2",...)
VALUES ("value1","value2",...)
Update Statement
UPDATE "table_name"
SET "column_1"=[new value]
WHERE {condition}
Delete From Statement
DELETE FROM "table-name"
WHERE {condition}

Monday, October 18, 2010

SQL Operators

1) SQL Logical Operators

There are three Logical Operators namely, AND, OR, and NOT. These operators compare two conditions at a time to determine whether a row can be selected for the output. When retrieving data using a SELECT statement, you can use logical operators in the WHERE clause, which allows you to combine more than one condition.

Logical Operators Description
OR For the row to be selected at least one of the conditions must be true.
AND For a row to be selected all the specified conditions must be true.
NOT For a row to be selected the specified condition must be false.


2) Comparison Operators:

Comparison operators are used to compare the column data with specific values in a condition.
Comparison Operators are also used along with the SELECT statement to filter data based on specific conditions.
The below table describes each comparison operator.
Comparison Operators Description
= equal to
<>, != is not equal to
< less than
> greater than
>= greater than or equal to
<= less than or equal to


3) Comparison Keywords 

There are other comparison keywords available in sql which are used to enhance the search capabilities of a sql query. They are "IN", "BETWEEN...AND", "IS NULL", "LIKE".

Comparision Operators Description
LIKE column value is similar to specified character(s).
IN column value is equal to any one of a specified set of values.
BETWEEN...AND column value is between two values, including the end values specified in the range.
IS NULL column value does not exist.

SQL LIKE Operator

The LIKE operator is used to list all rows in a table whose column values match a specified pattern. It is useful when you want to search rows to match a specific pattern, or when you do not know the entire value. For this purpose we use a wildcard character '%'.
For example: To select all the students whose name begins with 'S'
SELECT first_name, last_name
FROM student_details
WHERE first_name LIKE 'S%';
The output would be similar to:
first_name last_name
------------- -------------
Stephen Fleming
Shekar Gowda
The above select statement searches for all the rows where the first letter of the column first_name is 'S' and rest of the letters in the name can be any character.
There is another wildcard character you can use with LIKE operator. It is the underscore character, ' _ ' . In a search string, the underscore signifies a single character.
For example: to display all the names with 'a' second character,
SELECT first_name, last_name
FROM student_details
WHERE first_name LIKE '_a%';
The output would be similar to:
first_name last_name
------------- -------------
Rahul Sharma
NOTE:Each underscore act as a placeholder for only one character. So you can use more than one underscore. Eg: ' __i% '-this has two underscores towards the left, 'S__j%' - this has two underscores between character 'S' and 'i'.

SQL BETWEEN ... AND Operator

The operator BETWEEN and AND, are used to compare data for a range of values.
For Example: to find the names of the students between age 10 to 15 years, the query would be like,
SELECT first_name, last_name, age
FROM student_details
WHERE age BETWEEN 10 AND 15;
The output would be similar to:
first_name last_name age
------------- ------------- ------
Rahul Sharma 10
Anajali Bhagwat 12
Shekar Gowda 15

SQL IN Operator:

The IN operator is used when you want to compare a column with more than one value. It is similar to an OR condition.
For example: If you want to find the names of students who are studying either Maths or Science, the query would be like,
SELECT first_name, last_name, subject
FROM student_details
WHERE subject IN ('Maths', 'Science');
The output would be similar to:
first_name last_name subject
------------- ------------- ----------
Anajali Bhagwat Maths
Shekar Gowda Maths
Rahul Sharma Science
Stephen Fleming Science
You can include more subjects in the list like ('maths','science','history')
NOTE:The data used to compare is case sensitive.

SQL IS NULL Operator

A column value is NULL if it does not exist. The IS NULL operator is used to display all the rows for columns that do not have a value.
For Example: If you want to find the names of students who do not participate in any games, the query would be as given below
SELECT first_name, last_name
FROM student_details
WHERE games IS NULL
There would be no output as we have every student participate in a game in the table student_details, else the names of the students who do not participate in any games would be displayed.

Introduction of SQL

SQL stands for “Structured Query Language” 

It is a query language used for accessing and modifying information in the database. IBM first developed SQL in 1970s. Also it is an ANSI/ISO standard. It has become a Standard Universal Language used by most of the relational database management systems (RDBMS). 

Some of the RDBMS systems are: Oracle, Microsoft SQL server, Sybase etc. 

Most of these have provided their own implementation thus enhancing it's feature and making it a powerful tool. Few of the sql commands used in sql programming are 
SELECT Statement, 
UPDATE Statement, 
INSERT INTO Statement, 
DELETE Statement, 
WHERE Clause,
ORDER BY Clause, 
GROUP BY Clause, 
ORDER Clause, Joins, Views, GROUP Functions, Indexes etc.

In a simple manner, SQL is a non-procedural, English-like language that processes data in groups of records rather than one record at a time. 

Few functions of SQL are:
  1. store data
  2. modify data
  3. retrieve data
  4. modify data
  5. delete data
  6. create tables and other database objects
  7. delete data

Wednesday, October 13, 2010

Differences between SQL and MySQL

SQL stands for Structured Query Language.
It's a standard language for accessing and manipulating databases.
MySQL is a database management system, like SQL Server 2005, Oracle, Informix, Postgres etc.
MySQL is a RDMS (Relational Database Management System).
All types of RDMB use SQL.
SQL is used to manipulate database or to create a database. It's actually a common language.
MySQL is an actual computer application. You must need to download or collect it and install it.
MySQL is one of the most popular open source database management system.
MySQL has an SQL interpreter.
MySQL can be used as the back engine database for several kinds of applications and it's one of the best choice for many web programmers for web-base application.

---------------------------------------------------------------------------------------------------------------------------
SQL is a language used with databases, mySQL is a database application that uses SQL.

SQL is a common  database computer language designed for the retrieval and management of data in relational database management systems (RDBMS) -- basically a standard interactive and programming language for querying and modifying data and managing databases.  Very standard for uses ranging from the simplest Microsoft Access applications, up to complex multi-server distributed Oracle applications.

MySQL is a multithreaded, multi-user SQL database management system (DBMS) providing multi-user access to a number of databases.  MySQL is commonly the back engine database for a great many applications, and often the database of choice for web-based applications.

Comparing the two is a little like comparing the English language to Tom Clancey's last book, one uses the other -- but from there the differences are many.

Friday, October 08, 2010

Difference between Trigger and a Stored Procedure

  1.  when you create a trigger you have to identify event and action of your trigger but when you create stored procedure you don't identify event and action.
  2. trigger is run automatically if the event is occured but stored procedure don't run automatically but you have to run it manually 
  3. within a trigger you can call specific stored procedure but within a stored procedure you cann;t call a trigger
-----------------------------------

  • Actually triger in action which is performed automatically before or after a event occur and stored procedure is a procedure which is executed when the it is called. Stored procedure is module.
-----------------------------
  1. Triggers are implicitly called by DB itself while Stored procedure has to be manually called by user. 
  2. Stored procedure can pass the parameters which is not a case with Triggers. 
  3. While creating a Trigger triggering event n action has to be specified which isn’t a case with Stored procedure. 
  4. A Trigger can call the specific Stored procedure in it but the reverse is not true.