STRUCTURED QUERY LANGUAGE
SQL is a non procedural database language used for storing
and retrieving data from the database.
SQL was invented by IBM in early 1970s.
SQL supports the following categories of commands to communicate
with the database
Languages
DDL (Data Definition Language)
Commands
CREATE, ALTER, DROP,
RENAME, TRUNCATE
DML (Data Manipulation Language)
INSERT, DELETE, UPDATE
DCL (Data Control Language)
GRANT, REVOKE
TCL (Transaction Control Language) COMMIT, ROLLBACK,
M. Anbarasi, AP,SCSE,VIT
SAVEPOINT
Oracle Data types
z
Char (length) Fixed length character
z E.g char(10)
Varchar2(length) Variable length character
z E.g varchar2(10)
Number Integer of any
z e.g number(3) only 3 digits
z E.g number(4,1) float of max 1 decimal place
Data
z E.g 01-jan-06
M. Anbarasi, AP,SCSE,VIT
DDL commands
are used for table definition. They are
used to create, remove and alter the structure of
database objects.
CREATE
z ALTER
z TRUNCATE
z DROP
z RENAME
z
M. Anbarasi, AP,SCSE,VIT
DDL (DATA DEFINITION LANGUAGE)
z
CREATE Used to create the table
Syntax
CREATE TABLE < tablename> (
<column name1 > < datatype>,
<column name 2> < datatype>,
<column name 3> < datatype>..
<column name 1000>
<datatype>
);
M. Anbarasi, AP,SCSE,VIT
E.g.
CREATE TABLE emp(
emp_id NUMBER(6),
ename VARCHAR2(20),
ph_no VARCHAR2(20),
job_id
VARCHAR2(10),
salary
NUMBER(8,2)) ;
M. Anbarasi, AP,SCSE,VIT
ALTER Altering the table
z
Add - Adding new columns
z
ALTER TABLE <tablename> add ( <column name > <
datatype>);
E.g alter table emp add( dob date);
Modify - Modify the data type or increase / decrease the column
width
z
ALTER TABLE <tablename> modify (<column name > <
newdatatype>);
E.g alter table emp modify(job_id varchar2(20));
M. Anbarasi, AP,SCSE,VIT
ALTER con..
z To Drop
a column
z ALTER TABLE <tablename> drop
column < column
name>;
z E.g alter table emp drop column job_id
z To Rename a column
z ALTER TABLE <tablename> rename column <old
column name> to <new column name>
z E.g alter table emp rename column dob to dateofbirth;
M. Anbarasi, AP,SCSE,VIT
RENAME & DROP
z Rename change the table name
z Rename <old tablename> to <new tablename>;
z E.g rename emp to employee;
z Drop drop the table definition
z Drop Table <table name>;
z E.g drop table employee;
Truncate Removing the rows not definition
z Truncate table <table name>;
z E.g Truncate table employee;
M. Anbarasi, AP,SCSE,VIT
Data Manipulation Language
z
DML commands are used to insert, update,
retrieval and delete information in the database.
z INSERT
z UPDATE
z DELETE
M. Anbarasi, AP,SCSE,VIT
Insert Command
z Inserting values
INSERT INTO <tablename> VALUES( val1,val2 );
E.g insert into emp values(10,anu,0416-2265767,
sales,4000);
Inserting interactively
z
INSERT INTO <tablename> VALUES( &<column name1> ,
& <column name2> );
E.g insert into emp values(&emp_id,&ename,
&ph_no,&job_id,&salary);
M. Anbarasi, AP,SCSE,VIT
Inserting null values
z
INSERT INTO <tablename> VALUES( val1, , ,val4);
z E.g insert into emp values(10,anu, ,NULL,4000);
INSERT INTO <tablename> (column name1,column name2)
values (val1,val2);
z E.g insert into emp(emp_id, ename, salary) values
(10,banu,5000);
M. Anbarasi, AP,SCSE,VIT
z UPDATE
z a. Simple update
z UPDATE
< tablename> SET <col> = < new value>;
z E.g update emp set salary=salary*10;
z b.Using where clause
z UPDATE
< tablename> SET <col1> = < new value> , <col2> =
< new value> WHERE <conditions>;
z E.g update emp set salary = salary*10 where emp_id =10;
M. Anbarasi, AP,SCSE,VIT
DELETE
z
Deleting all rows
z
z
DELETE FROM <tablename>;
E.g delete from emp;
Deleting specific rows
z
z
DELETE FROM <tablename> where <condition>;
E.g delete from emp where emp_id=10;
M. Anbarasi, AP,SCSE,VIT
SELECT
z Selecting all the rows from a table
z
z
Selecting specific rows
z
z
SELECT * FROM < tablename>;
E.g select * from emp;
SELECT * FROM < tablename> where
<condition>;
E.g select * from emp where salary>5000;
Selecting specific column
z
z
SELECT <col1>, <col2> FROM < tablename>;
E.g select emp_id, ename from emp;
M. Anbarasi, AP,SCSE,VIT
Alias name
z
z
z
Selecting distinct values for a column
z
z
SELECT <col1> <alias name 1> , <col2> < alias name 2>
FROM <tablename>;
E.g select emp_id employee no from emp;
E.g select emp_id as employee_id from emp;
SELECT DISTINCT <col2> FROM < tab1>;
E.g Select distinct emp_id from emp;
Selecting columns satisfying a condition
z
z
SELECT <col1>, <col2> FROM < table name> WHERE
<conditions>;
E.g select emp_id, ename from emp where salary>5000;
M. Anbarasi, AP,SCSE,VIT