Practical file of
Submitted to: Submitted by:
Ms. Geetika Arora Name: Neha Jain
(Asst. Prof. in CS. Dept) Class: BCA 3rd sem.
Roll no: ………………
INDEX
Sr.No. Program name Pg No. Remarks
1. SQL introduction 1
2. Brief History of SQL 1
3. SQL Process 2
4. SQL Languages 2-3
5. SQL Data types 3-4
6. SQL Commands 5-13
7. Transaction Statements 14-15
8. SQL Operators 16-18
SQL (Structured Query Language)
SQL is Structured Query Language, which is a computer language for storing,
manipulating and retrieving data stored in a relational database.
SQL is the standard language for Relational Database System. All the Relational
Database Management Systems (RDMS) like MySQL, MS Access, Oracle,
Sybase, Informix, Postgres and SQL Server use SQL as their standard database
language.
Advantages of SQL
SQL is widely popular because it offers the following advantages −
Allows users to access data in the relational database management systems.
Allows users to describe the data.
Allows users to define the data in a database and manipulate that data.
Allows embedding within other languages using SQL modules, libraries &
pre-compilers.
Allows users to create and drop databases and tables.
Allows users to create view, stored procedure, functions in a database.
Allows users to set permissions on tables, procedures and views.
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 Process
When you are executing an SQL command for any RDBMS, the system
determines the best way to carry out your request and SQL engine figures out how
to interpret the task.
There are various components included in this process.
These components are −
Query Dispatcher
Optimization Engines
Classic Query Engine
SQL Query Engine, etc.
A classic query engine handles all the non-SQL queries, but a SQL query engine
won't handle logical files.
Following is a simple diagram showing the SQL Architecture −
SQL LANGUAGES
DDL – DATA DEFINATION LANGUAGE:-
The SQL sentences that are used to create these objects are called DDL’s or
Data Definition Language. DDL is part of sql which helps a user in defining
the data structures into the database. Following are the various DDL
commands are
Alter table & Create table & drop table
Create view & drop view
DML – DATA MANIPULATION LANGUAGE:-
The SQL sentences used to manipulate data within these objects are called
DML’s or Data Manipulation Language. It is language that enables users to
access or manipulate data as organized by appropriate data model. By data
manipulation we have
Retrieval of information stored in database.
Insertion of new information into database.
Deletion of information from database.
Modification of data stored in database.
DCL – DATA CONTROL LANGUAGE:-
The SQL sentences, which are used to control the behavior of these objects,
are called DCL’s or Data Control Language. It is language used to control
data and access to the database. Following are some DCL commands are
Commit
Rollback
DATA TYPES OF SQL
CHAR:-
This data type is used to store character strings values of fixed length.
The size in brackets determines the number of characters the cell can
hold.
Syntax:- CHAR(SIZE)
Example :-CHAR (20)
VARCHAR :-
This data type is used to store variable length alphanumeric data. The
maximum this data type can hold is 4000 characters. .
Example:- VARCHAR (20) OR VARCHAR2 (20)
NUMBER :-
The NUMBER data type is used to store numbers. The precision, (P),
determines the maximum length of the data, whereas the scale, (S),
determines the number of places to the right of the decimal..
Syntax:- NUMBER (P, S)
Example:- NUMBER (10, 2)
LONG :-
This data type is used to store variable length character strings
containing up to 2GB.
Syntax:- LONG (SIZE)
Example:- LONG (20)
DATE :-
This data type is used to represent data and time. The standard format
id DD-MM-YY as in 13-JUL-85.
LONG RAW : -
LONG RAW data types are used to store binary data, such as
Digitized picture or image. LONG RAW data type can contain up to 2GB.
Syntax :-LONGRAW (SIZE)
RAW :-
It is used to hold strings of byte oriented data. Data type can have a
maximum length of 255 bytes.
Syntax :- RAW(SIZE)
SQL COMMANDS
CREATE TABLE:-
A table is basic unit of storage. It is composed of rows and columns. To
create a table we will name the table and the columns of the table. We
follow the rules to name tables and columns:-
It must begin with a letter and can be up to 30 characters long.
It must not be duplicate and not any reserved word.
Syntax:-
CREATE TABLE tablename (column_name1 datatype (size),
column_name2 datatype (size) …);
Example:-
SQL> create table student(roll_no number (5),name
varchar2(10),class varchar2(5)
, marks number (5));
SQl>
SELECT:-
The select command of sql lets you make queries on the database. A query
is a command that is given certain specified information from the database
tables. It can be used to retrieve a subset of rows or columns from one or
more tables.
Syntax:-
SELECT * FROM <tablename>;
Example:-
SQL> select * from student;
ROLL_NO NAME CLASS FATHERS_NA MARKS
---------- ---------- ---------- ---------- ----------
1 ram cs mohan 50
2 roshni maths mohanr 60
3 roshi phy mohansr 67
9 poonam chem shyam 23
6 shilpi chem harish 23
6 priti chem harish 23
CREATE TABLE FROM ANOTHER TABLE:-
We can create a table by applying as sub query clause from another clause.
It will create the table as well as insert the rows returned from sub query.
Syntax:-
CREATE TABLE tablename(<column1>,<column2>,……)
AS SELECT <column1>,<column2> FROM <tablename>;
Example:-
SQL> create table
student1(ROLL_NO,NAME,CLASS,FATHERS_NA,MARKS,ADDRESS)as
select Roll_no,name,class,fathers_name,marks,address from student;
ROLL_NO NAME CLASS FATHERS_NA MARKS ADDRESS
---------- ---------- ---------- ---------- ---------- --------- ----------
1 ram cs mohan 50 HODAL
2 roshni maths mohanr 60 palwal
3 roshi phy mohansr 67 palwal
ALTER TABLE:-
After creating a table one may have need to change the table either by add
new columns or by modify existing columns. One can do so by using alter
table command.
Syntax:-
ALTER TABLE tablename ADD(col1 datatype,col2 datatype);
Example:-
SQL> select * from student;
ROLL_NO NAME CLASS FATHERS_NA MARKS
---------- ---------- ---------- ---------- ---------- ---------
1 ram cs mohan 50
2 roshni maths mohanr 60
3 roshi phy mohansr 67
SQL> alter table student add (address varchar2(10));
Table altered.
SQL> select * from student;
ROLL_NO NAME CLASS FATHERS_NA MARKS ADDRESS
---------- ---------- ---------- ---------- ---------- --------- ----------
1 ram cs mohan 50
2 roshni maths mohanr 60
3 roshi phy mohansr 67
DROP TABLE:-is used To remove the definition of oracle table, the drop
table statement.
Syntax:-
DROP TABLE tablename
Example:-
DROP TABLE student;
RENAME:-One can change the name of a table by rename command.
Syntax:-
RENAME oldname TO newname;
Example:-
RENAME student TO result;
DELETE:-One can delete data fron table by using delete from statement. The
delete statement removes rows from table but it doesn’t release storage
space.
Syntax:-
DELETE FROM tablename WHERE <condition>;
Example:-
SQL> select * from result;
ROLL_NO NAME HINDI ENGLISH MATH TOTAL
---------- ---------- ---------- ---------- ---------- ----------
101 seema 90 70 75 235
102 meena 60 60 70 190
103 reena 50 80 70 200
SQL> delete from result where roll_no=101;
1 row deleted.
SQL> select * from result;
ROLL_NO NAME HINDI ENGLISH MATH TOTAL
---------- ---------- ---------- ---------- ---------- ----------
102 meena 60 60 70 190
103 reena 50 80 70 200
DESCRIBE:-To find information about columns like column name, their data
types and other attributes of a table we can use DESCRIBE command.
Syntax:-
DESCRIBE tablename;
Example:-
SQL> desc student;
Name Null? Type
----------------------------------------- -------- ----------------------------
ROLL_NO NUMBER(10)
NAME VARCHAR2(10)
CLASS VARCHAR2(10)
FATHERS_NAME VARCHAR2(10)
MARKS NUMBER(10)
ADDRESS VARCHAR2(10)
INSERT:-To add new rows in an existing oracle table the insert command is
used.
Syntax:-
INSERT INTO tablename(col1,col2,col3,..)
VALUES(value1,value2,value3);
Example:-
SQL> insert into result1(102,'toni',70,89,70);
SQL> insert into result1 values(102,'toni',70,89,70);
SQL> insert into result1 values(103,'moni',80,59,90);
SQL> insert into result1 values(104,'soni',90,59,90);
SQL> select * from result1;
ROLL_NO NAME HINDI ENGLISH MATH
---------- ---------- ---------- ---------- ----------
102 toni 70 89 70
103 moni 80 59 90
104 soni 90 59 90
UPDATE:-The update command enables user to change the values of
existing rows.
Syntax:-
UPDATE tablename SET col1=value1,col2=value2;
Example:-
SQL> update result1 set total=hindi+english+math;
ROLL_NO NAME HINDI ENGLISH MATH TOTAL
---------- ---------- ---------- ---------- ---------- ----------
102 toni 70 89 70 229
103 moni 80 59 90 229
104 soni 90 59 90 239
DISTINCT CLAUSE:-The distinct keyword duplicates all rows from results of a
select statement.
Syntax:- SELECT DISTINCT FROM tablename;
Example:-
ROLL_NO NAME CLASS FATHERS_NA MARKS
---------- ---------- ---------- ---------- ----------
9 priti chem shyam 23
6 priti chem monu 23
6 priti chem harish 23
3 roshi phy mohansr 67
2 roshni maths mohanr 60
1 ram cs mohan 50
SQL> select distinct * from student;
ROLL_NO NAME CLASS FATHERS_NA MARKS
---------- ---------- ---------- ----------------- ----------
3 roshi phy mohansr 67
9 priti chem monu 23
2 roshni maths mohanr 60
6 priti chem harish 23
1 ram cs mohan 50
WHERE CLAUSE:-The where clause specifies the criteria for selection of rows
to be returned.
Syntax:-
SELECT <col1>,<col2> FROM tablename WHERE <cond>;
Example:-
SQL> update student set address='HODAL' where ROLL_NO=1;
SQL> update student set address='palwal' where ROLL_NO=2;
ROLL_NO NAME CLASS FATHERS_NA MARKS ADDRESS
---------- ---------- ---------- ----------- ---------- ------------------
1 ram cs mohan 50 HODAL
2 roshni maths mohanr 60 palwal
3 roshi phy mohansr 67
9 priti chem harish 23
6 priti chem harish 23
6 priti chem harish 23
ORDER BY CLAUSE:-You can sort the results of query ina specific order using
order by clause. It allows sorting of query results by one or more columns. It
can be done either in ascending or descending.
Syntax:-
SELECT * FROM tablename ORDER BY col1,col2,col3;
Example:-
SQL>select * from student order by ROLL_NO desc;
ROLL_NO NAME CLASS FATHERS_NA MARKS
---------- ---------- ---------- ---------- ----------
9 priti chem shyam 23
6 priti chem monu 23
6 priti chem harish 23
3 roshi phy mohansr 67
2 roshni maths mohanr 60
1 ram cs mohan 50
TRANSACTION STATEMENTS
COMMIT:- A COMMIT ends the current transaction and makes permanent any
changes made during the transaction. All transactional locks acquired on tables
are released.
Syntax:-
COMMIT [work] [comment text];
Where
Work is optional and comment text is used to specify comment
Example:-
SQL> commit;
Commit complete.
ROLLBACK:- A ROLLBACK does exactly the opposite of COMMIT. It ends the
transaction but undoes any changes made during the transaction.
Syntax:-
ROLLBACK [WORK] [TO [SAVEPOINT] save point]
Where
WORK is optional and is provided for ANSI compatibility. SAVEPOINT is
optional and is used to rollback a partial transaction, as far as the specified
save point.SAVEPOINT is a save point created during the current transaction.
Example:-
SQL> select * from result;
ROLL_NO NAME HINDI ENGLISH MATH TOTAL
---------- ---------- ---------- ---------- ---------- ----------
101 seema 90 70 75 235
102 meena 60 60 70 190
103 reena 50 80 70 200
SQL> delete from result where roll_no=101;
1 row deleted.
SQL> select * from result;
ROLL_NO NAME HINDI ENGLISH MATH TOTAL
--------- ---------- ---------- ---------- ---------- ----------
102 meena 60 60 70 190
103 reena 50 80 70 200
SQL> rollback ;
Rollback complete.
SQL> select * from result;
ROLL_NO NAME HINDI ENGLISH MATH TOTAL
---------- ---------- ---------- --------- ---------- ----------
101 seema 90 70 75 235
102 meena 60 60 70 190
103 reena 50 80 70 200
SET OPERATORS
Set keywords are used to combine information of similar type from one or more
than one table. Set operations and the operators are based on set theory. It
consumes two or more queries into one result. The types of set operators are:-
UNION:-The union clause merges the outputs of multiple queries into a
single set of rows and columns. It combines rows returned by two select
statements by eliminating duplicate rows.
Syntax:-
SELECT <statement> UNION SELECT <statements>;
Example:-
SQL> select * from t1;
ROLL_NO NAME CLASS
---------- -------- -----
101 seema bca
109 rani ba
110 bhanu bba
105 rahul bca
SQL> select * from result;
ROLL_NO NAME HINDI ENGLISH MATH
---------- ---------- ---------- ---------- ----------
101 seema 90 70 75
102 meena 60 60 70
103 reena 50 80 70
104 monu 78 67 79
105 rahul 58 67 89
106 azad 78 56 69
SQL> select name from result union select name from t1;
NAME
----------
Azad
Bhanu
Meena
Monu
Rahul
Rani
Reena
Seema
INTERSECT:-The intersect operator combines two select statements and
return only those rows that are returned by both queries.
Syntax:-
SELECT <statement> INTERSECT SELECT <statements>;
Example:-
SQL> select name from result intersect select name from t1;
NAME
----------
Rahul
Seema
MINUS:-It combines the result of two queries and returns only those values
that are selected by first query but not in second query.
Syntax:-
SELECT <statement> MINUS SELECT <statements>;
Example:-
SQL> select name from result minus select name from t1;
NAME
----------
Azad
Meena
Monu
Reena
ENHANCING PERFORMANCE
Views:-
A view is very commonly used database object that is derived at runtime. A view
contains data of its own. Its contents are derived from another table. It is virtual
table & does not have any data of its own.
Syntax:-
CREATE [OR REPLACE] VIEW view name AS sub query
[WITH CHECK OPTION] [WITH READ ONLY];
Example:-
CREATE VIEW mohit AS SELECT empno, ename, sal, comm FROM emp;