0% found this document useful (0 votes)
6 views

SQL Intro2

SQL is a non-procedural language used to store and retrieve data from a database. It was invented by IBM in the 1970s and supports commands to define, manipulate, and control database objects and data. These commands are divided into four main categories: DDL for defining database schema; DML for manipulating data; DCL for controlling access; and TCL for grouping transactions.

Uploaded by

mikku ninavi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SQL Intro2

SQL is a non-procedural language used to store and retrieve data from a database. It was invented by IBM in the 1970s and supports commands to define, manipulate, and control database objects and data. These commands are divided into four main categories: DDL for defining database schema; DML for manipulating data; DCL for controlling access; and TCL for grouping transactions.

Uploaded by

mikku ninavi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

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 1970’s.
SQL supports the following categories of commands to communicate
with the database
Languages Commands
DDL (Data Definition Language) 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)
z Varchar2(length) – Variable length character
z E.g varchar2(10)
z Number – Integer of any
z e.g number(3) – only 3 digits
z E.g number(4,1) – float of max 1 decimal place
z Data
z E.g 01-jan-06

M. Anbarasi, AP,SCSE,VIT
z DDL commands
are used for table definition. They are
used to create, remove and alter the structure of
database objects.

z CREATE
z ALTER
z TRUNCATE
z DROP
z RENAME

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>);
z E.g alter table emp add( dob date);

z Modify - Modify the data type or increase / decrease the column


width
z ALTER TABLE <tablename> modify (<column name > <
newdatatype>);

z 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;

z 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

z INSERT INTO <tablename> VALUES( val1,val2 …);

z E.g insert into emp values(10,’anu’,’0416-2265767’,


’sales’,4000);

z Inserting interactively
z INSERT INTO <tablename> VALUES( &<column name1> ,
& <column name2> …);

z E.g insert into emp values(&emp_id,’&ename’,


’&ph_no’,’&job_id’,&salary);

M. Anbarasi, AP,SCSE,VIT
z Inserting null values

z INSERT INTO <tablename> VALUES( val1,’ ‘,’ ‘,val4);

z E.g insert into emp values(10,’anu’,’ ’,NULL,4000);

z 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 DELETE FROM <tablename>;
z E.g delete from emp;

z Deleting specific rows


z DELETE FROM <tablename> where <condition>;
z E.g delete from emp where emp_id=10;

M. Anbarasi, AP,SCSE,VIT
SELECT
z Selecting all the rows from a table
z SELECT * FROM < tablename>;
z E.g select * from emp;
z Selecting specific rows
z SELECT * FROM < tablename> where
<condition>;
z E.g select * from emp where salary>5000;
z Selecting specific column
z SELECT <col1>, <col2> FROM < tablename>;
z E.g select emp_id, ename from emp;

M. Anbarasi, AP,SCSE,VIT
z Alias name
z SELECT <col1> <alias name 1> , <col2> < alias name 2>
FROM <tablename>;
z E.g select emp_id “employee no” from emp;
z E.g select emp_id as employee_id from emp;
z Selecting distinct values for a column
z SELECT DISTINCT <col2> FROM < tab1>;
z E.g Select distinct emp_id from emp;
z Selecting columns satisfying a condition
z SELECT <col1>, <col2> FROM < table name> WHERE
<conditions>;
z E.g select emp_id, ename from emp where salary>5000;
M. Anbarasi, AP,SCSE,VIT

You might also like