Introduction To SQL
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
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:
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.
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
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
SQL Commands
SQL Commands
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
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)
Example 2
create the customer table with following columns
CUSTOMER
CUSTNO NUMBER(4)
CNAME VARCHAR2(15)
CADD1 VARCHAR210)
CADD2 VARCHAR2(10)
CZIP VARCHAR2(8)
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.
Syntax:
1. To Add new column
Ex:
Alter table student add ( tot number(3));
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.
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);
2 Select
Retrieves certain records from one or more tables.
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
3. To select all the students where tot marks greater than 400
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
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;
2. To grant select, insert privileges on student table from scott user to user1;
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
Ex:
1. Select * from dual;
4*3
-----
12