Thursday, April 30, 2009

SQL

SQL has two streams: DML-Data manipulation language and DDL-Data definition language.

A database may have many tables. These tables may be interlinked with each other using some KEY field, which must be unique, and should not be left blank.This linking has given birth to RDBMS. Other characteristics of RDBMS are Normalization, Concurrent execution of transactions etc.
Data Manipulation Language
The basic structure of SQL contains three clauses: SELECT, FROM and WHERE
Examples:
SELECT fieldname1, fieldname2, fieldname3 FROM tablename;
SELECT * FROM tablename;
For all columns * is used, there should not be any space in tablename and fieldname. They are typed as one word.
SELECT employeeidno FROM employeestatisticstable WHERE salary >=5000;
SELECT employeeidno FROM employeestatisticstable WHERE position='manager';
Text are given in single quotes ' '.
SELECT employeeidno FROM employeestatisticstable WHERE salary >=5000 AND position='staff';
SELECT employeeidno FROM employeestatisticstable WHERE position='manager' OR (salary>50000 AND benefit>10000);
OR is performed before AND. Use paraenthesis if you want otherwise.
IN and BETWEEN are used for range. NOT IN and NOT BETWEEN are also used for opposite result of IN and BETWEEN.
SELECT employeeidno FROM employeestatisticstable WHERE position IN('manager','staff');
SELECT employeeidno FROM employeestatisticstable WHERE position NOT IN('manager','staff');
SELECT employeeidno FROM employeestatisticstable WHERE salary BETWEEN 30000 AND 50000;
SELECT employeeidno FROM employeestatisticstable WHERE salary NOT BETWEEN 30000 AND 50000;
Using LIKE and underscore(_)
SELECT ssn FROM employee WHERE lastname LIKE 'L%';
This gives all names starting with L. If you want names ending with L, use'%L'. If you want R anywhere in the name use '%R%'.
_ matches any character. '___' will match any string with exactly 3 chars. '___' will match any string with at least 3 chars.
SELECT ssn FROM employee WHERE lastname LIKE '___%';
Ordering the display in ascending or descending order.
SELECT fieldname1, fieldname2, fieldname3 FROM tablename ORDER BY fieldname1;
SELECT fieldname1, fieldname2, fieldname3 FROM tablename ORDER BY fieldname1 DESC;

Joins:
A database may have many tables. These tables may be interlinked with each other using some KEY field, which must be unique, and should not be left blank.This linking has given birth to RDBMS. Primary Key is a column or set of columns that uniquely identifies the rest of the data in any given row. A Foriegn Key is a colum in a table where that column is primary key for another table, which means that any data in a foreign key column must have a corresponding data in the other table where that column is primary key. In DBMS speak-this correspondence is known as referential integrity.
For example, there are three tables. AntiqueOwners, Orders and Antiques.
AntiqueOwners
OwnerID OwnerLastName OwnerFirstName

Orders
OwnerID ItemDesired

Antiques
SellerID BuyerID Item

SELECT antiqueowners.ownerlastname, antiqueowners.ownerfirstname FROM antiqueowners, antiques WHERE antiques.buyerid=antiqueowners.ownerid AND antiques.item='chair';

Aggregate Functions- gives sum average min etc for the entire column for matching condition.
SELECT SUM(salary), AVG(salary) FROM employeestatiscticstable;
SELECT MIN(benefits)FROM employeestatiscticstable WHERE position='manager';
SELECT COUNT(*)FROM employeestatiscticstable WHERE position='staff';

Data Definition Language:

CREATE TABLE tablename (column definition list);
CREATE TABLE airport (airport char(4) not null, aname varchar(20), checkin varchar(50));
Copying table:
CREATE TABLE ticket2(ticketno, ticketdate, pid) AS SELECT ticketno, ticketdate, pid FROM ticket;
CREATE TABLE ticket3 AS SELECT * FROM ticket;
DROP TABLE table2;
INSERT INTO is used to insert new rows into a table.
INSERT INTO tablename VALUES(value1, value2,...);
You need to provide value for all columns. If you do not want to then see below.
INSERT INTO tablename (cloumn1, column2,...)VALUES(value1, value2,...);
UPDATE tablename SET columnname=newvalue WHERE columnname=somevalue;
UPDATE person SET lastname='Ratna' WHERE firstname='Anupama';
UPDATE person SET address='ancd', city='Mumbai' WHERE lastname='sharma';
Delete is used to delete the rows from the table.
DELETE FROM tablename WHERE columnname=somevalue;
To delete all the rows in a table
DELETE * FROM tablename;

No comments: