SQL (Unit Iii)
SQL (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.
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.
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:
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
SYNTAX
Eg.
SQL> INSERT INTO dmc1
Values(‘1001’, ‘Ramandeep’,6455.45,2378.30);
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
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:
SNAME FEE
Teena 9800
SYNTAX:
SQL> Select column_name, column_name,… from table_name where condition;
RN SNAME
1003 Samual
1002 Sam
1001 Ram
1004 Raman
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);
Deleting tables
The drop table command is used to delete a particular table.
Syntax:
drop table table_name;
E.g.
SQL> drop table dmc5;
Syntax:
get file_name
E.g.:
SQL> get C:\ sql \dmc2.sql
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:
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;
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