1. Interactive SQL
Dr. P. ISAKKI ALIAS DEVI, M.C.A.,M.Phil.,Ph.D.
Associate Professor,
Department of Computer Applications,
Ayya Nadar Janaki Ammal College.
Sivakasi.
2. SQL
SQL is standard language for making queries in relational database packages such as SQL server,
Ingress, Sybase, Oracleetc.
The ORACLE system uses non-procedural Structured Query Language to communicate with its
databasekernel.
In 1986, the American National Standard Institute (ANSI) made SQL the standard for all DBMS. It’s a
powerful query language and all the applications development tools that ORACLE provides are
SQLbased.
SQL is a non-procedural language since only the task that has to be achieved is specified, not how
to go about doing the job i.e. it provides automatic navigation to the data. The records are processed
set at a time rather than just a record at atime.
SQL does not support any programming language constructs like if..else or while etc, but can be
embedded in other programming languages like C, COBOL, PL/ orADA
There are two types of SQL: 1) Interactive SQL 2) EmbeddedSQL
Interactive SQL is used to operate directly on a database to produce output for ourpurpose.
Embedded SQL consists of SQL commands put inside programs that are mostly written in some
other high level language. This method can make the program more powerful andefficient.
Features of SQL
It’s an interactive query language that allows the users to use SQL statements to retrieve data and
display it o thescreen.
It’s a database programming language that allows programmer to embed SQL statements in 3GL
programs to access data in adatabase.
It’s a database administration language that defines the structure of the database and controls the
user access todata.
It’s a client/server language that allows application programs on PCs connected via LAN to
communicate with the database servers that store shareddata.
It’s a database gateway language and often used in gateway that allows one brand of DBMS to
communicate with another brand.
SQL*PLUS
SQL*PLUS is powerful ORACLE support that can take your instructions for oracle. This flexible tool
allows both developers and end-user to issue SQL commands directly against the
3. database.
Database developers use SQL*PLUS to create, fill and monitor an application’s database.
End- users of the database use it to perform ad hoc queries against thedatabase.
Basically SQL*PLUS is a command line interpreter. Commands issued in SQL*PLUS are
broken into two distinct groups: the ANSI standards SQL commands to create, monitor and
manipulate an application’s database; and the SQL*PLUS commands provided by ORACLE to
enhance the functionality of the ANSI standards SQLcommands.
SQL*PLUS commands control the SQL*PLUS environment, format the output of SQL
commands, and control database transaction processing.
SQL*PLUS provides an open port to the database. An open port gives users, depending upon
their access privileges to specific tables in a database unrestrained access to the database’s
data. When user issues a command, it directly affects the data in thedatabase.
Several levels of access security prevent SQL*PLUS and user from queuing and modifying
every table in the database. Each user runs SQL*PLUS with a specificuser
name with a specific level of database access. Furthermore, a security access is placed on
each database table, restricting user from quering and modifying other user’s table without
proper authorization.
Data Types
When you create table in SQL*PLUS, you must specify the type of data that may appear in
each column. Some of the common data types are listed below.
Data Type Command Description
Character
CHAR(size) Fixed length characterdata.
Size written in the bracket determines the length of data that can be
stored.
Default size is 1 and maximum size is255.
VARCHAR2
(size)
Variable length character string having maximum length size inbytes.
Maximum size is2000.
Size must bespecified.
Number
NUMBER(p,s) Used to store variable length numericdata.
P determines the total number of digits possible to the left of decimal
point.
S determines the total number of digits possible to the right of decimal
point.
NUMBER
(size)
Fixed point number with precision size and scale0.
Date DATE This data type is used to store data and timeinformation.
Default format isDD-MON-YY.
To enter the dates other than standard format, use the appropriate functions.
4. Long LONG Variable length character strings containing up to 2gigabytes.
Table cannot have more than one Long type of datafield.
It cannot beindexed.
It cannot be used with SQL function.
It cannot appear in WHERE, GROUP BY, ORGER BY,clauses.
Raw RAW(size) Raw binary data, size bytelong.
Maximum size is 255bytes.
CREATING A TABLE IN THE DATABASE
Sql>Create table tablename(column 1 datatype(size) [default <expr>]
[CONSTRAINT constraint name] [column_constraint],
Column 2 datatype(size)…..);
For example:
Sql>Create table client_master
( c_no varchar2(5), name varchar2(10), address varchar2(20), pincode number(6), bal_due number(10,2));
INSERTING DATA INTO THE TABLES
The INSERT command with the values clause is used to add new rows to a database table.
Sql>INSERT INTO tablename[(column1, column 2…)]
VALUES (value1, value2,..);
For example:
Sql>INSERT INTO client_master
(c_no, name, address, pincode, bal_due)
VALUES (‘C001’, ‘Ajay’, ‘A-5, Bhandu’, 384120, 500 );
DATA RETRIVAL USING SQL *PLUS
SQL*PLUS provides a query capability in the form of SELECT statement. One can view the current
information in the tables by using this statement.
The SELECT statement can be used to Display some or all the columns from a specified table.
Display some or all of the rows from a specified table.
Display calculated values from the table.
Display statistical information from the tables, like averages or sums of column values.
Combine information from two or more tables.
Displaying some or all the Columns from a Table
Sql>SELECT column1, column2,……
FROM tablename;
Sql>SELECT c_no, name
FROM client_master;
5. Sql>SELECT * FROM tablename;
Sql>SELECT * FROM client_master;
Note:
The SELECT clause followed by the FROM clause are required for any SQLquery.
Not all columns need to beselected.
Columns are displayed left or right in the orderspecified.
SELECT list items must be separated bycommas.
Rows are returned in an arbitrarymanner.
Users may query only tables they have created or tables to which they have grantedaccess.
If you want to see which tables you have in your account, a special query can be made… Sql>SELECT*
FROM tab;
Displaying Some Specified Rows from the Table
If you want conditional retrieval of rows i.e. only those rows which satisfy certain condition. You can use
WHERE clause in the SELECT statement.
sql>SELECT column list
FROM tablename
WHERE condition;
sql>SELECT c_no, name
FROM client_master
WHERE bal_due>500;
Note:
Columns specified n the WHERE clause must be part of the table specified in the formclause.
Columns used in the WHERE clause do not have to be in SELECTlist.
The WHERE clause must specify character data in the same case (upper or lower) that it is in
thedatabase.
The comparison operators that can be used in SQLstatements
< , > , <= .., >= , = ,<>
Elimination of duplicates from the select statement:
A table could hold duplicate rows. In such a case, to see only unique rows the syntax is:
Sql>SELECT DISTINCT columnname, columnname FROM tablename;
Sql>SELECT DISTINCT job FROM employee;
Sql>SELECT DISTINCT * FROM tablename;
Sql>SELECT DISTINCT * FROM client_master;
6. CRETING TABLE FROM A TABLE
Sql>CREATE TABLE tablename[(columnname, columnname)]
AS SELECT columnname, columnname
FROM tablename;
Sql>CREATE TABLE supplier_master (s_no, s_name, address, pincode, bal_due)
AS SELECT c_no, name, address, pincode, bal_due
FROM client_master;
The source table is a table identified in the select section of this SQL sentence. The target table is one
identified in the create section of this SQL sentence.
SORTING DATA IN A TABLE
SELECT * FROM TABLENAME ORDER BY <COLUMNNAME1>, <COLUMNNAME2> <{Sort order}>;
Sql> select * from supplier_master order by name;
Sql> select * from supplier_master order by name desc;
INSERTING DATA INTO A TABLE FROM ANOTHER TABLE
To insert data one row at a time into a table, it is quite possible to populate a table with data that already
exists in another table.
Sql>INSERT INTO tablename
SELECT columnname, columnname FROM tablename;
Sql>INSERT INTO supplier_master
SELECT c_no, name, address, pincode, bal_due FROM client_master;
Insertion of data set into a table from another table
Sql>INSERT INTO onetablename SELECT column1, column2,…
FROM other table
[WHERE Condition];
Sql>INSERT INTO supplier_master
SELECT c_no, name, address, pincode, bal_due
FROM client_master WHERE c_no=’C001’;
VIEWING THE STRUCTURE OF THE TABLE
Sometimes need may arise to see the column definition or the integrity constraints specified on them in a
particular table. In such conditions DESCRIBE function can be used to view table definition.
Sql>DESCRIBE tablename;
This displays list of columns and their respective column constraints for the specified table.
7. MODIFYING THE TABLE DEFINITION
To change the format of an exiting table, we can use ALTER TABLE command.
Sql>ALTER TABLE tablename [MODIFY] [ADD]
Adding New Column
Sql>ALTER TABLE tablename
ADD (new columnnamedatatype(size), new columnnamedatatype(size) );
Sql>ALTER TABLE client_master
ADD (c_faxnumber(15), city varchar2(6));
Modifying the Existing Columns:
Sql>ALTER TABLE tablename
MODIFY (columnnamenewdatatype(newsize));
Sql>ALTER TABLE client_master
MODIFY (c_faxvarchar2(10));
Note:
We can increase a CHAR column’s width or the number of the digits number of decimal places in the
NUMBER column at anytime.
We can change the column from NOT NULL TO NULL by adding the NULL clause to the end of
column specification.
To decrease a column size, the column must be NULL in all exiting rows.
To add the not NULL feature to a column, the column must have a value for every row in the table.
To change the data type of a column, all values in the column must be NULL in all rows.
To modify more than one column, use command within the parentheses to separate each column
from the next.
You may add a column at any time if NULL is specified. You may not be able to add a column
with NOT NULL specified.
You cannot change the table name and column name.
TRUNCATNG TABLES - Empties the table completely
TRUNCATE TABLE tablename;
Sql>TRUNCATE TABLE client_master;
DESTROYING TABLE / REMOVING THE TABLE FROM THE DATABASE
Sql>DROP TABLE tablename;
Sql>DROP TABLE client_master;
RENAMING TABLES
Sql>RENAME TABLE old table name TO new table name;
Sql>RENAME TABLE client_masterTO client_master1;
8. MODIFYING THE DATA IN EXISTING TABLE:
Sql>UPDATE tablename
SET column=expression or value
[WHERECondition];
Sql>UPDATE client_master
SET name= ‘Vijay, address=’Mehsana’
WHERE c_no=’c001’;
REMOVING ROWS FROM A TABLE:
The DELETE command is used to remove rows from a table. sql>DELETE FROM
tablename[WHERE condition];
sql>DELETE FROM client_master WHERE bal_due>500;
WHERE clause determines which rows will be removed. If the clause is not specified
All the rows from the table will be deleted.
A) Table Name:Client_master
Description: Used to store information about clients.
Column Name Data Type Size
Client_no Varchar2 6
Name Varchar2 20
Address1 Varchar2 30
Adddress2 Varchar2 30
Ciy Varchar2 15
Pincode Number 8
State Varchar2 15
Bal_due Number 10,2
B) Table Name: Product_master
Description: Used to store information about products.
Column Name Data Type Size
Product_no Varchar2 6
Description Varchar2 15
P_percent Number 4,2
U_measure Varchar2 10
Qty_on_hand Number 8
Reorder_lvl Number 8
9. Sell_price Number 8,2
Cost_price Number 8,2
C) Table Name:Salesman_master
Description: Used to store information about salesman working in the company.
Column Name Data Type Size
S_no Varchar2 6
S_name Varchar2 20
Address1 Varchar2 30
Address2 Varchar2 30
city Varchar2 20
Pincode Number 8
State Varchar2 20
Sal_amt Number 8,2
Tgt_to_get Number 6,2
Ytd_sales Number 6,2
remarks Varchar2 60
A) Table Name:Client_master
Client_no Name City Pincode State Bal_due
C001 Ivan Bombay 400054 Maharashtra 15000
C002 Vandana Madras 780001 Tamil Nadu 0
C003 Pramada Bombay 400057 Maharashtra 5000
C004 Basu Bombay 400056 Maharashtra 0
C005 Ravi Delhi 100001 2000
C006 Rukmani Bombay 400050 Maharashtra 0
B) Table Name: Product_master
Product_
No
Description P_
percent
U_
Measure
Qty_on_
hand
Reorder_
lvl
sell_
price
Cost_
price
P001 Floppies 5 Piece 100 20 525 500
P002 Monitor 6 Piece 10 3 12000 11280
P003 Mouse 5 Piece 20 5 1050 1000
P004 Floppies 5 Piece 100 20 525 500
P005 Keyboards 2 Piece 10 3 3150 3050
P006 Cd Drive 2.5 Piece 10 3 5250 5100
P007 1.44 Drive 4 Piece 10 3 8400 8000
C) Table Name:Salesman_master
S_no S_name City Pin State Sal_
Amt
Tgt_
to get
Ytd_
Sales
remarks
S001 Kiran Bombay 40000
2
Maharashtra 3000 100 50 Good
10. S002 Manish Bombay 40000
1
Maharashtr
a
3000 200 100 Good
S003 Ravi Bombay 40003
2
Maharashtr
a
3000 200 100 Good
S004 Ashish Bombay 40004
4
Maharashtr
a
3500 200 150 Good
Solve the following queries using the database given above.
A)Retrieving records from table.
1) Find out the names of allclients.
2) Retrieve the entire content of the client_mastertable.
3) Retrieve the list of names and the cities of all theclients
4) List the various products available from the product_mastertable.
5) List all the clients who are located inBombay.
6) Find the names of the salesman who have a salary equal to Rs.3000
B)Updating records in atable.
1) Change the city of client_no’C002’ to‘Bombay’.
2) Change the bal_due of client_no’C001’ toRs.1000
3) Change the cost price of Floppies to Rs.950.00
4) Change the city of the salesman to Mumbai.
C)Deleting records in atable:
1) Delete all salesman from the salesmane_master whose salaries are equal to Rs.3500.
2) Delete all products from product_master where the quantity on hand is equal to100.
3) Delete from client_master where the column state holds the value ‘TamilNadu’.
E)Altering the tablestructure:
• Add a column called ‘telephone’ of datatype ‘number’ and size=10 to the
client_mastertable.
• Change the size of sell_price column in product_master to10,2.
A)Deleting the table structure along withdata:
1) Destroy the table client_master along with itsdata.
A)Renaming thetable:
1) Change the name of the salesman_master table tosman_mast.