Dbms SQL File
Dbms SQL File
Certificate
This is certified that the practical file belongs to Prabhat Kumar, Class roll no. 06
and Examination Roll no…………of session 2009-10 and has completed all the
practical in the college computer lab under our guidance and his behavior in the
college is good.
3
DBMS & SQL queries
Database
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.
5
DBMS & SQL queries
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.
Components of SQL
1) DDL (Data Definition Language):-
It is a set of SQL commands used to create, modify and delete database
structures but not data. They are normally used by the DBA not by user to a
limited extent, a database designer or application developer. These
statements are immediate i.e. they are not susceptible to ROLLBACK
commands. It should also be noted that if several DML statements for
example UPDATES are executed then issuing any DDL command would
COMMIT all the updates as every DDL 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.
Data types come in several forms and sizes, allowing the programmer to create
tables suited to the scope of the project. The decisions made in choosing proper
data types greatly influence the performance of a database. The information in the
database is maintained in the form of tables and each table consists of rows and
columns, which store data and therefore this data must have some data type i.e. the
type of data, which is stored in the table.
CHAR
NUMBER
DATE
LONG.
CHAR: -
VARCHAR or VARCHAR2:-
NUMBER: -
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.
9
DBMS & SQL queries
LONG:-
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.
The CREATE TABLE command defines each column of the table uniquely.
Each column has a minimum of three attributes, a name, data type and size (i.e.
column width).
Example:
SQL> create table student(name varchar(23),roll_no number(12),class
varchar2(12),address varchar(23));
Table created.
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.
11
DBMS & SQL queries
OR
OR
Example:-
SQL> insert
intostudent(name,roll_no,class,address)values('Prabhat',06,'BCA',Hatlimore');
1 row created.
Or
SQL> insert into student values('kishore',01,'BCA','Nagri');
1 row created.
Or
SQL> insert into student values('&name','&roll_no','&class','&address');
1 row created.
1 row created.
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.
Example:-
SQL> select * from student;
13
DBMS & SQL queries
1 row created.
ABC TABLE
ANKU TABLE
BONUS TABLE
DEPARTMENTS TABLE
14
DBMS & SQL queries
DEPT TABLE
EMP TABLE
EMPLOYEE TABLE
EMPLOYEES TABLE
STUDENT TABLE
9 rows selected.
A table could hold duplicate rows in such a case, only unique rows the
distinct clause can be used.
This syntax will give the unique values of column 1 and column 2.
Example:-
SQL> select distinct name,roll_no from student;
NAME ROLL_NO
-------------------- ----------
Prabhat 06
Oracle allows data from a table to be viewed sorted order. The rows retrieve
from the table will be sorted either in ascending or descending order
depending on the condition specified in the select sentence.
Example:-
SQL> SELECT * FROM STUDENT ORDER BY name;
6 rows selected
6 rows selected.
17
DBMS & SQL queries
6 rows selected.
7) RENAMING TABLES: -
Example:-
SQL> rename student to candidates;
Table renamed.
8) DESTROYING TABLES:-
DROP COMMAND: -
By using the DROP TABLE statement with the table name we can destroy
a specific table .
Table dropped.
TRUNCATE COMMAND:-
The truncate command is much faster in comparison to delete statement
but similar to the drop command as to destroy a specific table.
Table truncated.
20
DBMS & SQL queries
Example: -
SQL> describe employees;
EMP_ID NUMBER(5)
EMP_NAME VARCHAR2(20)
DEPT_ID NUMBER(10)
DEPT_NAME NAME(12)
SALARY NUMBER(21)
CONSTRAINTS
11)NOT NULL:- The NOT NULL column constraint ensures that a table
column cannot be 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.
or
Example:-
23
DBMS & SQL queries
Table created
DEPT_NAME VARCHAR2(25)
DEPT_LOC CHAR(5)
E_NO NUMBER(11).
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:
A table can have more than one unique key which is not possible in
primary key.
Example:-
SQL> create table student1(roll_no number(12)primary key,dob
date,name varchar2(20),class varchar2(2),e_mail varchar2(20) constraint
un_st unique);
Table created.
DOB DATE
NAME VARCHAR2(20)
CLASS VARCHAR2(20)
E_MAIL VARCHAR2(20)
ORACLE FUNCTIONS
1 sourabh 21 55000
2 sonu 22 55000
3 anku 4 55000
5 anku 21 55000
3 panku 22 75000
COUNT(DISTINCTNAME)
-------------------
COUNT(SALARY)
----------
27
DBMS & SQL queries
Syntax: - COUNT(*)
Example:-
SQL> select count(*) from employees;
COUNT(*)
----------
salary
----------
SUM(SALARY)
-----------
295000
MAX(SALARY)
-----------
75000
Example:-
29
DBMS & SQL queries
MIN(SALARY)
-----------
55000
AVG(SALARY)
-----------
59000
Example:-
SQL> select emp_id,name,dept_id,salary from employees where name
like 'a%';
3 anku 4 55000
5 anku 21 55000
Example:-
SQL>select emp_id,name,dept_id,salary from employees where name
not like 'a%';
1 sourabh 21 55000
2 sonu 22 55000
3 panku 22 75000
3 anku 4 55000
5 anku 21 55000
Example:-
2 sonu 22 55000
3 panku 22 75000
NOT IN OPERATOR:-
Example :-
SQL>select emp_id,name,dept_id,salary from employees where dept_id
not in(20,22);
32
DBMS & SQL queries
1 sourabh 21 55000
3 anku 4 55000
5 anku 21 55000
2 sonu 22 55000
3 panku 22 75000
1 sourabh 21 55000
5 anku 21 55000
STRING FUNCTIONS
Syntax: - UPPER(char)
Example: -
UPPER(NAME)
--------------------
SOURABH
SONU
ANKU
ANKU
PANKU
34
DBMS & SQL queries
Syntax: - LOWER(char)
Example:-
SQL> select lower(name) from employees;
LOWER(NAME)
--------------------
sourabh
sonu
anku
anku
panku
Syntax:- INITCAP(char)
Example:-
INITCAP(NAME)
--------------------
Sourabh
Sonu
Anku
Anku
Panku