Practical File SQL Queries & DBMS
Practical File SQL Queries & DBMS
Practical File SQL Queries & DBMS
Certificate
Index
Database
A database is an organized collection of facts. In other words, we can say that it is
a collection of information arranged and presented to serve an assigned purpose.
As the tool that is employed in the broad practice of managing databases, the
DBMS is marketed in many forms. Some of the more popular examples of DBMS
solutions include Microsoft Access, FileMaker, DB2, and Oracle. All these
products provide for the creation of a series of rights or privileges that can be
associated with a specific user. This means that it is possible to designate one or
more database administrators who may control each function, as well as provide
other users with various levels of administration rights. This flexibility makes the
task of using DBMS methods to oversee a system something that can be centrally
controlled, or allocated to several different people.
SQL has been a command language for communication with the oracle 9i server
from any tool or application. Oracle SQL contains many extensions. When an
SQL statement is entered, it is stored in a part of memory called the SQL buffer
and remains there until a new SQL statement is entered.
It is a nonprocedural language.
It reduces the amount of time required for creating and maintaining systems.
It is English like language.
Components of SQL
command implicitly issues a COMMIT command to the database. Anybody using DDL
must have the CREATE object privilege and a table space area in which to create objects.
CHAR
VARCHAR (size) or VARChAR2 (size)
NUMBER
DATE
LONG.
CHAR: - This data types is used to store character strings values of fixed
length. The size in brackets determines the number of characters the cell
can hold. The maximum number of characters (i.e. the size) this data type
can hold is 255 characters. The data held is right- padded with spaces to
whatever length specified.
DATE:- The DATE data type stores date and time information. Although
date and time information can be represented in both character and number
data types, the DATE data type has special associated properties. For each
DATE value, Oracle stores the following information: century, year, month,
date, hour, minute, and second.
QUERY
A query is a concise memo submitted to an editor by a writer seeking publication. It is basically
an in query to see whether the writer’s work is of interest to a particular publication. A query
briefly details a writer’s experience and knowledge of the subject matter, and gives a summary
or synopsis of the article the writer hopes to have published. An approximate word count for
the proposed article or feature is also generally included.
1) THE CREATE TABLE COMMAND :- The CREATE TABLE command defines each
Example:
Table created.
2) THE INSERTION OF DATA INTO TABLE: - Once a table is created, the most
natural thing to do is load this with
data to be manipulated later i.e. to insert the rows in a table. The data in a table
can be inserted in three ways.
OR
OR
Example:-
SQL> insert
intostudent(name,roll_no,class,address)values('Prabhat',06,'BCA',Hat
limore');
1 row created.
Or
1 row created.
Or
1 row created.
FOR inserting more values we use ‘/’ slash after SQL> as below but after above
syntax used:
SQL> /
1 row created.
3) FOR VIEWING DATA IN THE TABLE: - Once data has been inserted into a
table, the next most logical operation
would be to view what has been inserted. The SELECT SQL verb is used to achieve
this. The SELECT command is used to retrieve rows selected from one or more
tables.
1 row created.
When we use the command SELECT* FRM TAB; the output is displayed as:-
SQL> select * from tab;
9 rows selected.
NAME ROLL_NO
-------------------- ----------
Prabhat 06
6 rows selected
6 rows selected.
6 rows selected.
Table renamed.
8) DESTROYING TABLES:-
DROP COMMAND: - By using the DROP TABLE statement with the table name we can destroy
a specific table .
Syntax: - DROP TABLE <table name>;
Example:--
Example: -
CONSTRAINTS
11) NOT NULL:- The NOT NULL column constraint ensures that a table column cannot be
left
empty. When a column is defined as not null, then that column becomes a
mandatory column. It implies that a value must be entered into the column if the record is
to be accepted for storage in the table.
Table created
13) THE UNIQUE KEY CONSTRAINT:- The unique key constraint permits multiple
entries of NULL into the column. These NULL
values are clubbed at the top of the column in the order in which they were entered into the
table. This is the essential difference between the primary key and the unique constraints
when applied to table column(s). Key point about UNIQUE constraint:
Unique key will not allow duplicate values.
Unique index is created automatically.
A table can have more than one unique key which is not possible in primary key.
Table created.
Oracle functions serve the purpose of manipulating data items and returning a result.
Functions are the programs that take zero or more arguments and return a single value.
Oracle has built a no. of functions into SQL. These functions can be called from SQL
statements.
14) COUNT (expr) function: - Returns the number of rows where expression is not null.
Syntax: - COUNT ([<distinct>[<all>] <expr>)
Example:-
COUNT(DISTINCTNAME)
-------------------
4
COUNT(SALARY)
----------
5
15) COUNT (*) function: - Returns the number of rows in the table, including
duplicates and those with nulls.
Syntax: - COUNT(*)
Example:-
COUNT(*)
----------
5
salary
----------
5
16) THE SUM FUNCTION: - Returns the sum of the values of ‘n’.
Syntax: - SUM ([<distinct>][<all>] <expr>)
Example:-
SUM(SALARY)
-----------
295000
MAX(SALARY)
-----------
75000
Example:-
SQL> select min (salary) from employees;
MIN(SALARY)
-----------
55000
19) THE AVG FUNCTION: - Returns an average value of ‘n’, ignoring null values in a
column.
Syntax: - AVG ([<distinct>][<all>] <n>);
Example:-
AVG(SALARY)
-----------
59000
20) LIKE OPREATOR :- The LIKE predicate allows comparison of one string value with
another string value, which is not identical. This is achieved by using
wildcard characters. Two wildcard characters that are available are:
% allows to match any string of any length(including zero length)
_allows to match on a single character.
Example:-
Example:-
2 sonu 22 55000
3 panku 22 75000
21) IN OPERATOR:- In case a value needs to be compared to a list of values then the IN
predicate is used. The IN predicates helps reduce the need to use
multiple OR conditions.
Example:-
NOT IN OPERATOR:-
Example :-
SQL>select emp_id,name,dept_id,salary from employees where
dept_id not in(20,22);
1 sourabh 21 55000
3 anku 4 55000
5 anku 21 55000
STRING FUNCTIONS
23) UPPER function :- Returns char, with all letters forced to uppercase.
Syntax: - UPPER(char)
Example: -
UPPER(NAME)
--------------------
SOURABH
SONU
ANKU
ANKU
PANKU
LOWER(NAME)
--------------------
sourabh
sonu
anku
anku
panku
25) INITCAP function: - Returns a string with the first letter of each word in upper
case.
Syntax:- INITCAP(char)
Example:-
INITCAP(NAME)
--------------------
Sourabh
Sonu
Anku
Anku
Panku