Vous êtes sur la page 1sur 13

Introduction to SQL

SQL is the abbreviation for Structured Query Language. It is pronounced as "sequel". SQL was first developed
by IBM in the mid of 1970s. It is an ANSI/ISO.

SQL is considered a fourth generation language. It is English-like and intuitive. The most important point to
be noted here is that SQL is case insensitive, which means SELECT and select have same meaning
in SQL statements. SQL is robust enough to be used by, Users with non-technical backgrounds, Professional
Developers, Database Administrators. SQL is a non-procedural language that emphasizes what to get, but not
how to get it.

SQL is the international standard language for the relational database management systems. It has become a
Standard Universal Language used by most of the RDBMS such as Oracle, Microsoft SQL Server, DB2, Sybase.
Few of the SQL Commands used in SQL are DDL such as Create, Alter, Drop DML Commands such as Select, Insert,
Update, Delete etc.
 Store Data
 Modified Data
 Retrieve Data
 Delete Data
 Create tables and other database objects

A Brief History of SQL


1970 – Dr. Edgar F. "Ted" Codd of IBM is known as the father of relational
databases. He described a relational model for databases.
1974 – Structured Query Language appeared.
1978 – IBM worked to develop Codd's ideas and released a product named
System/R.
1986 – IBM developed the first prototype of relational database and standardized
by ANSI. The first relational database was released by Relational Software which
later came to be known as Oracle.
SQL * Plus: It is the utility develop by Oracle Corporation for running SQL

Logical and Physical Storage Structures


Logical Storage Structures:
A tablespace stores the tables, views, indexes and other schema objects. It is the primary logical storage
structure of an Oracle Database.
Each tablespace can contain one or more segments, which are made up of extents, which consist of database
blocks.
Each database object has its own segment storage. When an object runs out of space, an extent is issued to
the object. Each extent is made up of database blocks, which are the smallest logical storage unit.

Physical Storage Structures:


A tablespace is stored in one or more datafiles. A datafile is a binary file whose name usually ends in .dbf.
Datafiles consist of operating system blocks, which are not the same as database blocks. A database block
may consist of many operating system blocks.
A database is the set of datafiles, as well as files containing configuration and management information,
necessary to run an RDBMS.
An Oracle database include a system tablespace, as well as others. The system tablespace contains the Data
Dictionary.
Data Types:
In SQL, there are several different data types. These data types define the domain of values that each column
can contain.
The primary data types are
1. Number
2. char
3. Varchar2
4. Date
5. Timestamp

1. Number(precision, scale)
Precision refers to the total number of digits, and scale is the number of digits to the right of the decimal point.
If precision and scale are not speicfied, the max values are assumed, by default 38 digits. If a value exceeds
the precision, Oracle returns an error. If the value exceeds the scale, it will be rounded.
Example:
Defination Data Stored Data
price number 12345.678 12345.678
qty number(3) 1234 Error
rate number(5,2) 123.45 123.45
rate number(5,2) 123.45678 123.46
rate number(7,3) 123432.54 123432.540
rate number(5,2) 1234.56 1234.56

2. CHAR
It is a fixed character datatype to store alpha numeric values. Use CHAR column for values that are always the
same length. such as state abbreviations, area codes, phone numbers, etc.

Example:

Defination Data Stored Data Memory Used

sname char(10) Rajeh Rajeh Total 10 bytes of memory used


mobileno char(12) 1234567890 1234567890 Total 12 bytes of memory used
hno char(15) 6-100/2a 6-100/2a Total 15 bytes of memory used
zipcoe char(12) 508001 508001 Total 12 bytes of memory used
flag char(1) Y Y Total 1 bytes of memory used

3. VARCHAR2
It is a variable character datatype to store alpha numeric values. For example name of employee, name of
students, name of sates, hno etc. A varchar2 column can contain no more than 4000 bytes.

Defination Data Stored Data Memory Used


ename varchar(20) Rajeh Rajeh Total 5 bytes of memory used
sname varchar(10) Anil Kumar Anil Kumar Total 11 bytes of memory used
mobileno char(12) 1234567890 1234567890 Total 10 bytes of memory used
hno char(15) 6-100/2a 6-100/2a Total 8 bytes of memory used
zipcoe char(12) 508001 508001 Total 7 bytes of memory used

DATE
representing date and time values. It has the ability to store the month, day, year, century,
hours, minutes, and seconds.

Example
Defination Data Stored Data

dob date 12-Jan-90 12-Jan-90


hiredate date 10-Feb-17 10-Feb-17

TIMESTAMP
Oracle has expanded on the DATE datatype and has given us the TIMESTAMP datatype which
stores all the information that the DATE datatype stores, but also includes fractional seconds. If
you want to convert a DATE datatype to a TIMESTAMP datatype format, just use the CAST
function as below.

Example:
Defination Data Stored Data

dob timestamp 12-Jan-90 12-JAN-90 05.00.00.000000 AM


hiredate timestamp 01-Mar-17 01-MAR-17 09.00.00.000000 AM

SELECT CAST(hiredate AS TIMESTAMP) "Date" FROM emp

SQL Commands

SQL Commands

DDL DML TCL DCL


Create Select Commit Grant
Alter Insert Rollback Revoke
Truncate Update
Drop Delete
Rename
Describe

DDL (Data Definition Language) Commands


DDL commands that define a database, including creating, altering and droping tables establishing
constraints.

DML (Data Manipulation Language) Commands


DML commands that maintain and query a database.

DCL (Data Control Language) Commands


DCL commands grant and revoke are used to give or removed access rights to the database and the
structures within it.

TCL (Transaction Control Language) Commands


TCL statements are used to manage the changes made by DML statements.
Changes to data are executed using commit, rollback and savepoint.
TCL changes can be grouped together into logical transactions.

Naming Restrictions Table and column names:


1. Must start with a letter
2. Can contain up to 30 alphanumeric characters
3. Cannot contain spaces or special characters such as “!,” but “$,” “#,” and “_” are permitted.
4. Table names must be unique within one user account in the Oracle database.
5. Column names must be unique within a table.
6. Some words have a special meaning in the Oracle database and in the SQL programming language.
7. These are called “reserved” words.
8. It is best to avoid using these as names for your tables and columns.
9. Some common examples of Oracle reserved words are:
TABLE, NUMBER, SEQUENCE, ORDER, VALUES

Table: EMP Table: DEPT

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO DEPTNO DNAME LOC
7369 SMITH CLERK 7902 17-Dec-80 800 20 10 ACCOUNTING NEW YORK
7499 ALLEN SALESMAN 7698 20-Feb-81 1600 300 30 20 RESEARCH DALLAS
7521 WARD SALESMAN 7698 22-Feb-81 1250 500 30 30 SALES CHICAGO
7566 JONES MANAGER 7839 2-Apr-81 2975 20 40 OPERATIONS BOSTON
7654 MARTIN SALESMAN 7698 28-Sep-81 1250 1400 30
7698 BLAKE MANAGER 7839 1-May-81 2850 30
7782 CLARK MANAGER 7839 9-Jun-81 2450 10
7788 SCOTT ANALYST 7566 9-Dec-82 3000 20 Table: SALGRADE
7839 KING PRESIDENT 17-Nov-81 5000 10 GRADE LOSAL HISAL
7844 TURNER SALESMAN 7698 8-Sep-81 1500 0 30 1 700 1200
7876 ADAMS CLERK 7788 12-Jan-83 1100 20 2 1201 1400
7900 JAMES CLERK 7698 3-Dec-81 950 30 3 1401 2000
7902 FORD ANALYST 7566 3-Dec-81 3000 20 4 2001 3000
7934 MILLER CLERK 7782 23-Jan-82 1300 10 5 3001 9999

DDL (Data Definition Language) Commands


DDL commands that define a database, including creating, altering and droping tables establishing
constraints.

1. Create
2. Alter
3. Truncate
4. Drop
5. Describe
6. Rename

1. Create
Create table <table_name> (col1 datatype, col2 datatype, ....., coln datatype);
Example 1
create the student table with following columns
STUDENT
SNO CHAR(10)
SNAME VARCHAR2(15)
M NUMBER(3)
P NUMBER(3)
C NUMBER(3)

create table student ( sno char(10),


sname varchar2(15),
m number(3),
p number(3),
c number(3)
);

Example 2
create the customer table with following columns
CUSTOMER
CUSTNO NUMBER(4)
CNAME VARCHAR2(15)
CADD1 VARCHAR210)
CADD2 VARCHAR2(10)
CZIP VARCHAR2(8)

SQL>create table customer (custno number(4), cname varchar2(15),


cadd1 varchar2(10), cadd2 varchar2(10),
czip varchar2(8) );
Example 3
create the employee table with following columns

EMPLOYEE
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)

SQL> create table employee


( empno number(4), ename varchar2(10), job varchar2(9), mgr number(4),
hiredate date,sal number(7,2), comm number(7,2), deptno number(2)
);

Table created.

ALTER COMMAND

Modifies definition (Structure) an existing database object, such as a table. The alter command is
used to perform the following functions.

1. Add, Modify, Drop columns


2. Add, Drop Constraints

Syntax:
1. To Add new column

alter table <table_name> add ( column datatype(size));

Ex:
Alter table student add ( tot number(3));

2. To modify the structure datatype of existing column

Syntax:
alter table <table_name> modify ( column datatype(size) );

Ex:
alter table student modify ( rno char(8) );

3. Drop a column
Syntax:
alter table <table_name> drop <column_name>;

3. Truncate
The truncate command used to remove all rows from the table permanently. But structure
remains.

Syntax:
Truncate table <table_name>;

Ex:
Truncate table student;

4. Drop
Drop command removes the table structure from the database including all the rows.

Syntax:
Drop table <table_name>;

Ex:
Drop table student;

5. Describe
The describe command is used to list all the columns in the specified table.

Syntax:
Describe <table_name>

Ex:
Describe student

6. Rename
To change the name of the table

Syntax:
Rename table <old_name> to <new_name>;

Ex:
Rename table student to mystudent;

DML Commands

1. Insert
The Insert command is used to enter and store the values in the database object in the defined order and type.

The Insert command is implemented in 3 methods

1. Simple Insert 2. Partial Insert 3. Interactive Insert

1. Simple Insert
Insert into <table_name> (col1, col2, col3, ... , coln) values ( value1, value2, value3, ... , valuen)l

Ex:
Insert into student (rno,sname,m,p,c) values (1,'Rakesh',89,98,84);

2. Partial Insert
Syntax:
Insert into <table_name> (col1, col2, col3, ... , coln) values ( value1, value2, value3, ... , valuen)l

Ex:
Insert into student(rno,sname) values (2,'Vijay');

3. Interactive Insert
Syntax:
Insert into <table_name> (col1, col2, col3, ... , coln) values (&col1, &col2, &col3, ... , &coln);

Insert into student (rno,sname,m,p,c) values (&rno,'&sname',&m,&p,&c);

2 Select
Retrieves certain records from one or more tables.

SQL data retrievals are done using the select statement


Data retrievals are also called queries.

Syntax:
SELECT *
FROM <table_name>
WHERE <condition>
ORDER BY column_name;

select * from t;
select * from t where [conditions];
select f1, f2, ...from t;
select f1, f2, ...from t where [conditions];
Conditions
Mathematical and Logical operators
=, <>, >, <, >=, <=, and, or, not
ex: ... where price > 10;
ex: ... where price > 10 and code <> 1234;

Wildcard
like
Compares to similar character string
% represents any group of characters
_ represents one character

ex: ... where name like 'S%';


... where name like '_llen';
Order by
default is ascending.
... order by f1;
add DESC for descending
... order by f1 desc
Ex:
1. To select all rows, all columns from the student table

Select * from student;

2. To select rno,sname, tot columns from student table

Select rno,sname,tot from student;

3. To select all the students where tot marks greater than 400

Select * from student


where tot > 400;

4. To select a student where rno=2

Select * from student


where rno=2;

5. To select all the students ascending order

Select * from student


order by sname asc;

6. To select all the students descending order

Select * from student


order by sname desc;

3. Update
Update command used to update rows.

Update command is used to edit or update the data based on conditions for the selected record or column.
The update is used for multiple purpose to change the value of record, to enter the values for the columns
which are left empty during the insertion.

Syntax:
update <table_name>
set column = value
where <condition>;

Ex:
1. update student
set tot = m+p+c;

2. update student
set m=90
where rno=2;

4. Delete
The delete command used to delete records from the table based on user choice either single record or multiple
records based on the condition.

Syntax:
delete from <table_name>
where <condition>;

Ex:
1. Delete a record from student where rno is 4

delete from student


where rno = 4;

2. To delete all rows from student table

delete from student;

TCL Commands

1. Commit
The commit statement ends the unit of work in which it is executed and saves the works.

Syntax:
commit;
or
commit work;

2. Rollback
The process of undoing a change to a database then the rollback statement is used.

Syntax;
rollback;

DCL Commands
1. Grant
The grant command is used to provide access or privileges on the database objects to the users.

Syntax:
Grant <privilege_name>
on <object_name>
to <user_name>
[ with grant option ];

Ex:
1. To grant all privileges (select, insert, update, delete) on student table from scott user to user1;

Scott> grant all on student to user1;

2. To grant select, insert privileges on student table from scott user to user1;

scott> grant select, insert on student to user1;

3. To list out all the rows of student table from user1;

user1> select * from scott.student;

4. Insert a record in to student table from user1

user1> insert into scott.student values (7,'Syed',77,88,90);

2. Revoke Command
Revoke command is used to remove the given privileges from the specified user of the database.

Syntax:
Revoke [ Grant option for ] <privilege>
on <object_name>
from <user_name>;

Ex:
1. Revoke select, insert privileges from user1

Scott> revoke select,insert on student from user1;

2. Revoke all privileges on student table from user1

Scott> revoke all on student from user1;

Create a new table from an existing table


create table newtable as select * from oldtable;
create table newtable as select column1, column2, ... from oldtable;

Copy contents of one table to another table


insert into to_table select * from from_table;
insert into to_table (column1, column2, ...) select column1, column2, ... from from_table;
Dual table in Oracle
This is a single row and single column dummy table provided by Oracle. This is used to perform mathematical
calculations without using table.

Ex:
1. Select * from dual;

2. Select 4*3 from dual;

4*3
-----
12

Vous aimerez peut-être aussi