0% found this document useful (0 votes)
23 views16 pages

SQL (Unit Iii)

Uploaded by

Diya Ahuja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views16 pages

SQL (Unit Iii)

Uploaded by

Diya Ahuja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

UNIT- III

SQL: SQL is the language used to manipulate relational databases. It is tied very closely with relational model
which was proposed by Dr. E.F Codd in 1970. The language was developed by IBM to manipulate the data
stored within Codd’s model was originally called Structured English Query Language (SEQUEL).

SQL is a simple and powerful language used to create, manipulate and retrieve data and structures in the
database. It is a non procedural language. Today SQL is accepted as the Universal standard database access
language. Most applications developed today use a relational database.

SQL Datatypes

Tables in SQL are used for storing data which consists of both rows and columns. The columns are used for
storing and attribute the data. Each column in a table (relational) was a common name and a datatype. The
datatype restricts the values that can be entered into the column. The datatype available in SQL are:

1) Character datatype
2) Number datatype
3) Date/Time object
4) LOB (Large Object) datatype.

Character Datatype

 CHAR(N): This datatype is used for storing fixed length character strings. A character can be any letter,
Number or a punctuation mark. The maximum size of CHAR datatype is 2000 characters. If you don’t
specify a length the default size is 1 character
 VARCHAR(N): This datatype is used to store variable length character strings. It is rarely used.
 VARCHAR2(N): This is used to store variable length character strings when declaring a column of
datatype VARCHAR2, you must specify its size. It can have a maximum size of 4000 characters.
 NCHAR(N): This is used to store fixed length National character set. This character set enables
developers and administrators to extend the standard database character set. In order to store data in
language and character sets other than English. The maximum column size allowed is 2000 bytes.
 NVARCHAR2(N): This is used to store variable length string in the database’s National character set.
The maximum column size allowed is 4000 bytes.
 LONG(N): This is used to store a large amount of variable length character strings. LONG columns can
be up to 2 GB in size.
 RAW AND LONGRAW DATATYPE: These are used to store binary data such as sound, graphics,
documents etc. The maximum length of Raw And Longraw columns is 2000 bytes and 2 GB
respectively.
Number Datatypes

NUMBER: This is used to store zero, positive and negative fixed and floating point numbers. The Number
datatype have a precision (P) and a scale (S). The precision is the total no. of digits in the Number and can
range from 1 to 38 digits. The scale can be either positive which indicates the No. of digits to the right of the
decimal point or negative which will round the number up by the no. of places to the left of the decimal point. If
you do not define a scale, it is assumed to be a zero.

Syntax: Number (Precision, Scale);

Date/ Time Datatype

DATE: This datatype is used to store date and time information. It is stored in a specified internal oracle format
that includes not only the month, day and year but also the hours, minutes and seconds. The oracle Date
datatype uses 7 bytes for data storage and one byte is used for the length of data.

TIMESTAMP: This is an extension of the Date datatype. It allows time to be stored as date with fractional
seconds.

INTERVAL YEAR TO MONTH: It allows time to be stored as an interval of years and months.

INTERVAL DAY TO SECOND: It allows time to be stored as an interval of days to hours, minutes and
seconds.

LOB’s (Large Objects)

1) BLOB (BINARY LARGE OBJECTS) : This datatype is used to store binary data such as images,
audio files, videos etc. it can store up to 4 GB of binary data.
2) CLOB(CHARACTER LARGE OBJECT): This datatype is used for storing large amount of
character data. It can store up to 4 GB of data.
3) BFILE(BINARY FILES): This datatype when declared for a columns containing pointers to large
binary files stored on the file system of the database server.
 What is a table?

A table is a database object which is used to store data in a relational database. Each table consists of rows and
columns. A column in a database table represents the table’s attributes and a row represents a single set of
column values in a database table. Each column of the table has a column name and a datatype associated with
it. The following guidelines should be followed when designing a table:

1) Use descriptive names for tables, columns and indexes.


2) Table should be normalized before creation, at least up to third normal form.
3) A table must have at least one column.
4) Each column of the table should have a proper data type.
5) Columns that define NULLS are defined last to conserve storage space.
6) Make the proper documentation of the meaning of each table and its columns.
7) It is recommended that all the integrity constraints should be determined before creating a table.
8) Do not use cryptic codes or numbers in your table names.
9) Use the same name to describe the same attributes across the tables.

 Creating a table
In order to store and manage data, it is necessary to create tables. In oracle/SQL, tables are created using
CREATE TABLE command.
SYNTAX
Create table tablename
(Column_name datatype(size),
Column_name datatype(size),…..);
SQL> Create table dmc1
(rn varchar2(5),
Sname varchar2(20),
Fee number(6,2),
Fine number(6,2)
);
Table created

 Describe command

If you wish to see the table definition (data dictionary) of a given table, You may use the DESCRIBE verb or
command.

SYNTAX

SQL> DESCRIBE <TABLE NAME>;


SQL> DESC TABLENAME;
SQL> describe dmc1;
 Insertion of data into table
Once a table is created the most natural thing to do is load this table with data to be manipulated later. This is
done using the INSERT INTO command.

SYNTAX

SQL> INSERT INTO <table name>


(column, column,…)
Values (expression,expression….);

Eg.
SQL> INSERT INTO dmc1
Values(‘1001’, ‘Ramandeep’,6455.45,2378.30);

SQL> INSERT INTO dmc1


(rn, sname, fee, fine)
Values(‘1002’, ‘Inderdeep’,6900.54,3278.30);

Viewing data in the table (select statement)


Once data has been inserted into a table, the next most logical operation would be to view what has been
entered. The SELECT SQL verb is used to achieve this.

SYNTAX
SQL>SELECT <OPTIONS> from <TABLE NAME>;

Oracle allows the users to use the meta character asterisk (*) which is expanded by oracle to mean all columns
in the table. The oracle server compiles the sentence, executes it and retrieves data for all columns or rows from
the table.
SQL> select * from dmc1;
SQL> select rn from dmc1;
SQL> select rn, sname from dmc1;
 Filterng table data
While viewing data from a table it is rare that all the data from the table will be required each time. Hence, SQL
must give us a method of filtering out data that is not required. The ways of filtering table data will be:
1) SELECTED COLUMNS AND ALL ROWS
SYNTAX:
SQL> Select column_name from table_name;
Or
SQL> Select column_name, column_name….from table_name;
SQL> Select Fee from dmc1;

FEE
6300
7300.45
9800
4500
SQL> Select rn, Fee from dmc1;
RN FEE
1001 6300
1002 7300.45
1003 5331.56
1004 9800

2) Selected rows and all columns

The oracle provides the option of using a where clause in SQL sentences to apply a filter on the rows,
the select statement will repeat when a where clause is added to the SQL sentence, the oracle server
compares each record from the table with the condition specified in the where clause. Oracle displays
only those records that satisfy the specified condition.

SYNTAX:

SQL> select * from dmc1 where fine<500;


RN SNAME FEE FINE
1001 Raman 6300 300
1002 Daman 7300.45 300.45
1004 Teena 9800 200.56

SQL> select sname,fee from dmc1 where sname= ‘Teena’

SNAME FEE
Teena 9800

3) Selected columns and selected rows


To view a specific dataset from the table and also a selected no. of columns we may use the following
syntax:

SYNTAX:
SQL> Select column_name, column_name,… from table_name where condition;

SQL>Select rn, sname from dmc1 where fee>600;

RN SNAME FEE FINE


1001 Raman 6300 300
1002 Daman 7300.45 300.45
1004 Teena 9800 200.56
 Elimination of duplicates from the select statements
:- distinct clause
A table could hold duplicate rows. In such a case, to see only unique rows the syntax is:
Syntax:
Select Distinct column_name , column_name from table;
This syntax scans through the values of columns specified and displays unique values from amongst them
E.g.:
select * from dmc1;
RN SNAME FEE FINE
1001 Ram 5000 20
1001 Ram 5000 20
1002 Sam 8000 200
1003 Samual 7000 250
1003 Samual 7000 250
1004 Raman 9000 150
SQL> select distinct * from dmc1;
RN SNAME FEE FINE
1001 Ram 5000 20
1004 Raman 9000 150
1002 Sam 8000 200
1003 Samual 7000 250
SQL> select distinct rn, sname from dmc1;

RN SNAME
1003 Samual
1002 Sam
1001 Ram
1004 Raman

 Sorting data in table


 (order by clause)
Oracle allows data from a table to be viewed in a sorted. The rows retrieved from the table will be sorted in
either ascending or descending order depending on the condition specified in the select sentence.
Syntax:
select * from dmc1 order by sname;
RN SNAME FEE FINE
1001 Ram 5000 20
1001 Ram 5000 20
1004 Raman 9000 150
1002 Sam 8000 200
1003 Samual 7000 250
1003 Samual 7000 250

select * from dmc1 order by sname desc;


RN SNAME FEE FINE
1003 Samual 7000 250
1003 Samual 7000 250
1002 Sam 8000 200
1004 Raman 9000 150
1001 Ram 5000 20
1001 Ram 5000 20

 Creating a table from a table


We can create a table from an existing table by using the following statements
Syntax: create table table_name (column_name, column_name) as select column_name, column_name from
existing table_name;
SQL> Create Table dmc2 (scode, amount)
AS SELECT rn, fee from dmc1;

SQL> insert into dmc2


Values (‘1005’, 5000);
Select * from dmc2;
SCODE AMOUNT
1001 5000
1001 5000
1002 8000
1003 7000
1003 7000
1004 9000
1005 5000

 Inserting data into a table from another table


In addition to inserting 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.
Syntax:
insert into table_name
Select (columns) from existing_table_name;
E.g. SQL> Create table dmc3
(cc varchar2(5), name varchar2(20));

SQL> insert into dmc3


Select rn, sname from dmc1;

SQL> Create table dmc4


(code varchar2(5), name varchar2(20), amt number(6,2));

SQL> insert into dmc4


Select rn, sname , fee from dmc1 where fee>=7000;

 Insertion of selected datasets from a table into a new table


You can insert specific records from a specified table into a new table by using the following syntax:
Syntax:
insert into new_table_name
Select (columns) from existing table_name Where (condition);
SQL> create table dmc5
(cd varchar2(5), pname varchar2(20), amount Number (6,2));
Table created
SQL> insert into dmc5
Select rn, sname , fee from dmc1
where sname= ‘Ram’;
2 rows created.
SQL> select * from dmc5;
CD PNAME AMOUNT
1001 Ram 5000
1001 Ram 5000

1) Delete operations
The verb delete from in SQL is used to remove rows from tables.
a) Removal of all rows
Syntax:
Delete from table_name;
SQL> DELETE FROM dmc5;
2 rows deleted
b) Removal of specified Rows
Syntax:
DELETE FROM table_name where=(condition);

SQL> delete from dmc2 where scode= ‘1001’;


2 rows deleted
SQL> insert into dmc2
Values (‘1001’,5000);
SQL> select * from dmc2;
SCODE AMOUNT
1001 5000
1002 8000
1003 7000
1003 7000
1004 9000

 Updating the contents of a table


The update command is used to change or modify data values in a table.
 Updating of all rows
Syntax:
update table_name
SET column_name=expression,
Column_name=expression….;
SQL> update dmc1
set fee=fee+100;

SQL> select * from dmc1;


 Updating of specified rows
Syntax:
update table_name
set column_name=expression….
where (condition);
e.g.

SQL> update dmc1;


set fine=220 where fee=5100;

SQL> select * from dmc1;

 Modifying the structure of a table


The alter table command is used to make changes in table structure
1. Adding new columns:
Syntax:
alter table table_name
add(new column_name datatype(size));
e.g.
SQL> alter table dmc1
add (city varchar2(10));
table altered.
SQL> desc dmc1;
Name Null Type
RN VARCHAR2 (5)
SNAME VARCHAR2 (20)
FEE NUMBER (6, 2)
FINE NUMBER (6, 2)
CITY VARCHAR2 (10)
SQL> update dmc1
set city= ‘moga’ where fine=220;
SQL> update dmc1
set city= ‘chandigarh’ where sname= ‘raman’;

 Updating existing columns


The modify verb is used along with ALTER TABLE command to update a specified column.
Syntax:
alter table table_name
modify (column_name new_datatype (new size));
E.g.
SQL> alter table dmc1
modify (city varchar2 (20));
 Renaming tables
The rename command is used to change the name of an existing table.
Syntax:
rename old_tablename to new_tablename;
E.g.:
SQL> Rename dmc1 to bca2;
Table is renamed.

 Deleting tables
The drop table command is used to delete a particular table.
Syntax:
drop table table_name;
E.g.
SQL> drop table dmc5;

 displaying user objects


It is possible to display user objects like tables, views etc on the screen by using Select command.
Syntax:
Select <expression> from tab;
e.g.
SQL> select * from tab;

 Working with an ascii editor


SQL is a single sentence language. The sentence once typed at the SQL prompt cannot be retrieved and
corrected. The technique used to overcome this constant retyping of sentences is to create an ASCII file. Place
the SQL sentence as the contents of the ASCII file. Any ASCII editor can be used. One convenient editor to use
will be windows notepad which is a part of MS windows operating system. To open notepad generally the ED
command is used. Each SQL sentence is to be terminated with a / in the first column of the next line.
Syntax:
ed table_name
E.g.:
SQL> ed dmc7
To run this .SQL file after saving the file and exiting from the text editor type the following command.

Syntax:
get file_name

E.g.:
SQL> get C:\ sql \dmc2.sql

Computations on table data

 Arithmetic operators:
Oracle allows arithmetic operators to be used while viewing records from a table or while performing
data manipulation.

+ Addition
- Subtraction
* Multiplication
/ Division
** Exponent
() Enclosed
operation
Syntax:

Select column_name, column_name (operator) expression from table;


SQL> select rn, sname , fee*.10 from bca2;

 Renaming columns used with expression list


We can rename the default output column names with an alias , when required , by giving new name in the
select command.
E.g.:
SQL> select rn, sname , fee*.10 Discount from bca2;
 Logical operators
A. The AND operator: The oracle engine will process all rows on a table and display the result only when
all of the conditions specified using the AND operator are satisfied.
Syntax:
Select fields from table_name
Where condition AND condition;
SQL> select * from bca2
Where sname= ‘Samual’ AND fee=7100;
2 rows selected.

B. The OR operator: The oracle engine will process all rows in a table and display the result only when
any of the conditions specified using the OR operator is satisfied.
Syntax:
Select fields from table_name
Where condition OR condition;
E.g.:
SQL> select * from bca2
Where sname= ‘Samual’ OR fee<8000;
SQL> select * from bca2
Where sname= ‘Samual’ OR fee<7000;

The NOT operator


The oracle engine will process all rows in a table and display the result only when none of the
conditions specified using the NOT operator are satisfied.
Syntax:
Select fields from table_name
Where NOT (condition);
E.g.:
SQL> select * from bca2
Where NOT(fee<5000);
SQL> select * from bca2
Where NOT(fee>6000);

 The BETWEEN operator


 (Range Searching):
In order to select data that is within the range of values, the BETWEEN operator is used. The
BETWEEN operator allows the selection of rows that contain values within a specified lower and upper
limit. The range coded after the word BETWEEN is inclusive.
Syntax:
Select fields from table_name
Where field BETWEEN condition AND condition;
E.g.:
SQL> select rn, sname from bca2 where fine BETWEEN 150 AND 200;

SQL> Insert into bca2


Values (‘1006’, ‘Deepka’,7590,178, ‘Jalandhar’);
1 row created.

E.g.:
SQL> select rn, sname from bca2 where fine BETWEEN 150 AND 200;

SQL> select rn, sname, fine from bca2 where fine= 150 AND fine=200;
No rows selected.
SQL> select rn, sname, fine from bca2 where fine>= 150 AND fine<=200;
RN SNAME FINE

1002 Sam 200


1004 Raman 150
1005 Samual 200
1006 Deepka 178

You might also like