This document provides an overview of SQL (Structured Query Language) including its basic commands and functions. It discusses how SQL is used for data administration like creating tables, indexes and views, as well as for data manipulation such as adding, modifying and deleting data. The key SQL data types and classification of SQL commands are also summarized. Examples are provided for common SQL commands like CREATE TABLE, ALTER TABLE, INSERT, SELECT and DELETE.
2. Objectives
• Explore basic commands and functions of
SQL
• How to use SQL for data administration (to
create tables, indexes, and views)
• How to use SQL for data manipulation (to
add, modify, delete, and retrieve data)
• How to use SQL to query a database to
extract useful information
3. Introduction to SQL
• SQL functions
Data definition
User define the structure.
Data retrieval
User retrieve stored data from the database.
Data manipulation
User to update database by adding new data,
removing old data and modifying previously
stored data.
4. Access Control
Restricting user’s unauthorized access.
Data sharing
Data sharing by concurrent users.
Data integrity
Protecting it from corruption due to
inconsistent updates or system failures
5. SQL Data Types
• Character Data Type
CHAR(size)
Fixed length character data of length size
bytes. This should be used for fixed length data.
2000 bytes Default and minimum size is 1 byte.
VARCHAR2(size)
Variable length character string having
maximum length size bytes. You must specify size.
4000 bytes minimum is 1
6. NVARCHAR2(size)
Variable length national character set string
having maximum length size bytes. You must specify
size. 4000 bytes minimum is 1.
NCHAR(size)
Fixed length national character set data of
length size bytes. This should be used for fixed length
data. Such as codes A100, B102... 2000 bytes Default
and minimum size is 1 byte.
7. • Numeric Data Type
NUMBER(p,s)
Number having precision p and scale s. The
precision p can range from 1 to 38. The scale s can
range from -84 to 127.
INTEGER or INT
A 64-bit signed integer value with an implied
scale of zero.
FLOAT(p)
A single or double precision floating point
number value ,precision up to a maximum of 64. 7
8. DECIMAL [(p[,s])] or DEC [(p[,s])]
DECIMAL - Precision defaults to 38,Scale
defaults to zero.
DECIMAL (p)- Scale defaults to zero.
DECIMAL (p,s)- Precision & Scale are defined by
user.
DATE
Date value should be specified in the form:
YYYY-MM-DD
TIMESTAMP
Timestamp value should be specified in the form:
YYYY-MM-DD HH:MM:SS
9. Classification of SQL Commands
1. DDL (Data definition language)
The DDL part of SQL permits database tables
to be created or deleted.
Example
a. CREATE TABLE
b. ALTER TABLE
c. DROP TABLE
d. CREATE INDEX
e. DROP INDEX
f. GRANT
g. REVOKE
10. 2. DML(Data manipulation languages)
It is used to manipulate data through queries
it also change the content of a table.
Examples
a. INSERT : To insert data into table.
b. SELECT : To display data from a table.
c. DELETE : Delete all records from a table, the space for
the record remain.
d. UPDATE : To change the content of a table.
e. LOCK : Table control concurrency.
11. 3. DCL(Data Control languages)
That control access to data and to the
database.
Examples
a. COMMIT : Save work done.
b. SAVEPONT : Indentify a point in a transaction to which
can later rollback.
c. SET TRANSACTION : Change transaction option like
what rollback segment to use.
d. GRANT/REVOKE : grant or take back permission to or
from the oracle user.
12. 4. DQL(Data query languages)
That allows getting data from the database
and imposing ordering upon it.
Examples
a. SELECT : Retrieves data from table.
13. THE CREATE TABLE COMMAND
The create table command defines each column of
table uniquely. Each column has a minimum three
attributes a name, data type and size.
Rules for creating a table
1. A name can have maximum up to 10 characters.
2. Alphabets from A-Z, a-z and digits 0-9 are allowed.
3. A name should begin with alphabet.
4. Only use special character as _(underscore).
15. Example
SQL>create table emp(empno number(4),
ename varchar(10),
job char(10),
mgr number(4),
hiredate date,
sal number(7,2),
comm number(7,2),
deptno number(2));
SQL>Table created
16. Modifying the structure of Table
1.Adding New Columns
Alter Command
Used to alter structure of DB.
Syntax :
alter table <table name>
add(<newcolumn name><data type> (<size>),
< newcolumn name><data type> (<size>));
Example:
Enter a new field eaddress in the table emp.
SQL> alter table emp
add(eaddress varchar2(30));
SQL> table altered
17. Modifying the structure of Table
2. Modifying Existing Columns
Alter Command
Only modifying column size can be increased.
Syntax :
alter table <table name>
modify(<newcolumn name><new data type>
(<new size>));
Example:
SQL> alter table emp
modify(ename varchar2(25));
18. Modifying the structure of Table
3. Dropping an Existing Columns
Alter Command
To drop an existing column.
Example:
SQL> alter table emp drop column city;
• The multiple columns can be dropped in single
command.
SQL> alter table emp drop column (city,address);
19. Modifying the structure of Table
4. Dropping Tables
Table can be deleted using drop table command.
Syntax :
SQL>drop table tablename;
Example:
SQL>drop table emp;
5.Truncate Command
Removes all records from a table including all space allocated
for the records are removed.
Syntax :
SQL>truncate table tablename;
Example:
SQL>truncate table emp;