SQL First Page and Index
SQL First Page and Index
INDEX
Sr.No. Program name Pg No. Remarks
1. SQL introduction 1
3. SQL Process 2
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
Commit
Rollback
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)
VARCHAR :-
This data type is used to store variable length alphanumeric data. The
maximum this data type can hold is 4000 characters. .
LONG :-
This data type is used to store variable length character strings
containing up to 2GB.
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 :- 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:-
Syntax:-
Example:-
SQL> create table student(roll_no number (5),name
varchar2(10),class varchar2(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:-
Example:-
1 ram cs mohan 50
Syntax:-
Example:-
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:-
Example:-
1 ram cs mohan 50
1 ram cs mohan 50
DROP TABLE:-is used To remove the definition of oracle table, the drop
table statement.
Syntax:-
Example:-
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:-
Example:-
1 row deleted.
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:-
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:-
VALUES(value1,value2,value3);
Example:-
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:-
Example:-
Example:-
1 ram cs mohan 50
1 ram cs mohan 50
WHERE CLAUSE:-The where clause specifies the criteria for selection of rows
to be returned.
Syntax:-
Example:-
SQL> update student set address='HODAL' where ROLL_NO=1;
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:-
Example:-
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:-
Example:-
SQL> commit;
Commit complete.
Syntax:-
Where
Example:-
SQL> select * from result;
1 row deleted.
SQL> rollback ;
Rollback complete.
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:-
Example:-
109 rani ba
110 bhanu bba
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
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:-
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:-
Example:-
CREATE VIEW mohit AS SELECT empno, ename, sal, comm FROM emp;