Google

For More SQL Programs Click Here..
SQL: SQL (Structured Query Language) is a computer language aimed to store, manipulate, and retrieve data stored in relational databases. The first incarnation of SQL appeared in 1974, when a group in IBM developed the first prototype of a relational database. The first commercial relational database was released by Relational Software (later becoming Oracle).

Standards for SQL exist. However, the SQL that can be used on each one of the major RDBMS today is in different flavors. This is due to two reasons: 1) the SQL standard is fairly complex, and it is not practical to implement the entire standard, and 2) each database vendor needs a way to differentiate its product from others. In this tutorial, such differences are noted where appropriate.

This SQL programming help site lists commonly-used SQL statements, and is divided into the following sections:

* SQL Commands: Basic SQL statements for storing, retrieving, and manipulating data in a relational database.
* Table Manipulation: How SQL statements are used to manage tables inside the database.
* Advanced SQL: Advanced SQL commands.
* SQL Syntax: A single page that lists the syntax for all the SQL commands in this tutorial.

For each command, the SQL syntax will first be presented and explained, followed by an example. By the end of this tutorial, you should have a good general understanding of the SQL syntax, and be able to write SQL queries using the correct syntax. My experience is that understanding the basics of SQL is much easier than mastering all the intricacies of this database language, and I hope you will reach the same conclusion as well.

If you are interested in how to retrieve data using SQL, we recommend that you start with the SQL Commands section. If you are interested in understanding how SQL can be used to manipulate database tables, we recommend that you start with the Table Manipulation section. If you are looking for help on a specific SQL command, you can use the Site Map to find the command you are looking for.

Since we have started dealing with numbers, the next natural question to ask is if it is possible to do math on those numbers, such as summing them up or taking their average. The answer is yes! SQL has several arithematic functions, and they are:

- AVG
- COUNT
- MAX
- MIN
- SUM

The syntax for using functions is,

SELECT "function type"("column_name")
FROM "table_name"

For example, if we want to get the sum of all sales from the following table,

Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999

we would type in

SELECT SUM(Sales) FROM Store_Information

Result:
SUM(Sales)
$2750

$2750 represents the sum of all Sales entries: $1500 + $250 + $300 + $700.

In addition to using functions, it is also possible to use SQL to perform simple tasks such as addition (+) and subtraction (-). For character-type data, there are also several string functions available, such as concatenation, trim, and substring functions. Different RDBMS vendors have different string functions implementations, and it is best to consult the references for your RDBMS to see how these functions are used.

Views can be considered as virtual tables. Generally speaking, a table has a set of definition, and it physically stores the data. A view also has a set of definitions, which is build on top of table(s) or other view(s), and it does not physically store the data.

The syntax for creating a view is as follows:

CREATE VIEW "VIEW_NAME" AS "SQL Statement"

"SQL Statement" can be any of the SQL statements we have discussed in this tutorial.

Let's use a simple example to illustrate. Say we have the following table:

TABLE Customer
(First_Name char(50),
Last_Name char(50),
Address char(50),
City char(50),
Country char(25),
Birth_Date date)

and we want to create a view called V_Customer that contains only the First_Name, Last_Name, and Country columns from this table, we would type in,

CREATE VIEW V_Customer
AS SELECT First_Name, Last_Name, Country
FROM Customer

Now we have a view called V_Customer with the following structure:

View V_Customer
(First_Name char(50),
Last_Name char(50),
Country char(25))

We can also use a view to apply joins to two tables. In this case, users only see one view rather than two tables, and the SQL statement users need to issue becomes much simpler. Let's say we have the following two tables:

Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999

Table Geography
region_name store_name
East Boston
East New York
West Los Angeles
West San Diego

and we want to build a view that has sales by region information. We would issue the following SQL statement:

CREATE VIEW V_REGION_SALES
AS SELECT A1.region_name REGION, SUM(A2.Sales) SALES
FROM Geography A1, Store_Information A2
WHERE A1.store_name = A2.store_name
GROUP BY A1.region_name

This gives us a view, V_REGION_SALES, that has been defined to store sales by region records. If we want to find out the content of this view, we type in,

SELECT * FROM V_REGION_SALES

Result:
REGION SALES
East $700
West $2050

SQL Index:



Index in sql is created on existing tables to retrieve the rows quickly.



When there are thousands of records in a table, retrieving information will take a long time. Therefore indexes are created on columns which are accessed frequently, so that the information can be retrieved quickly. Indexes can be created on a single column or a group of columns. When a index is created, it first sorts the data and then it assigns a ROWID for each row.



Syntax to create Index:

CREATE INDEX index_name
ON table_name (column_name1,column_name2...);

Syntax to create SQL unique Index:


CREATE UNIQUE INDEX index_name
ON table_name (column_name1,column_name2...);



- index_name is the name of the INDEX.
- table_name is the name of the table to which the indexed column belongs.
- column_name1, column_name2... is the list of columns which make up the INDEX.





In Oracle there are two types of SQL index namely, implicit and explicit.



Implicit Indexes: They are created when a column is explicity defined with PRIMARY KEY, UNIQUE
KEY Constraint.


Explicit Indexes: They are created using the "create index.. " syntax.

NOTE:
1) Even though sql indexes are created to access the rows in the table quickly, they slow down DML operations like INSERT, UPDATE, DELETE on the table, because the indexes and tables both are updated along when a DML operation is performed. So use indexes only on columns which are used to search the table frequently.
2) Is is not required to create indexes on table which have less data.
3) In oracle database you can define up to sixteen (16) columns in an INDEX.

Free Web Hosting