You are on page 1of 14

Computer Laboratory and Practical Work of DBMS

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.

An example of a database is a dictionary

Database Management System


Database management system, or DBMS, is a computer software program that
is designed as the means of managing all databases that are currently installed
on a system hard drive or network. Different types of database management
systems exist, with some of them designed for the oversight and proper control
of databases that are configured for specific purposes. Here are some examples
of the various incarnations of DBMS technology that are currently in use, and
some of the basic elements that are part of DBMS software applications.

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.
Introduction to Oracle

Introduction to Structured Query Language


(SQL)
SQL is a standard language for accessing and manipulating databases.

 SQL stands for Structured Query Language


 SQL lets you access and manipulate databases
 SQL became a standard of the American National Standards Institute
(ANSI) in 1986, and of the International Organization for
Standardization (ISO) in 1987

Structured query language is a language that provides an interface to


relational database systems. SQL was developed by IBM in the 1970s for use in
system R, and is a de facto standard, as well as an ISO and ANSI standard. SQL
is often pronounced SEQUEL.
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.

Features of Structured query language (SQL)

SQL can be used by a range of users, including those with little or no


programming experience.

 It is a nonprocedural language.
 It reduces the amount of time required for creating and maintaining systems.
 It is English like language.

What Can SQL do?


 SQL can execute queries against a database
 SQL can retrieve data from a database
 SQL can insert records in a database
 SQL can update records in a database
 SQL can delete records from a database
 SQL can create new databases
 SQL can create new tables in a database
 SQL can create stored procedures in a database
 SQL can create views in a database

Introduction to Structured Query Language


(SQL)
Structured query language is a language that provides an interface to
relational database systems. SQL was developed by IBM in the 1970s for use in
system R, and is a de facto standard, as well as an ISO and ANSI standard. SQL
is often pronounced SEQUEL.
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.

Features of Structured query language (SQL)

SQL can be used by a range of users, including those with little or no


programming experience.

 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
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.

For example: - CREATE, ALTER, DROP, TRUNCATE, COMMENT etc.

2) DML (Data Manipulation Language):- It is the area of SQL that allows


changing data within the database.

Examples:-INSERT, UPDATE, DELETE etc.

3) DCL (Data Control Language):- It is the component of SQL statement


that control access to data and to the database. Occasionally DCL statements are
grouped with DML statements.
Examples: -GRANT, REVOKE.

4) DQL (Data Query Language):- It is the component of SQL statement that


allows getting data from the database and imposing ordering upon it. It includes the
SELECT statement. This command is the heart of SQL. It allows getting the data out of
the database perform operations with it. When a SELECT is fired against a table or
tables the results is compiled into a further temporary table, which is displayed or
perhaps received by the program i.e. a front-end.

Examples: - SELECT retrieve data from the database.

5) TCL commands can only use with DML commands like INSERT, DELETE and UPDATE
only.

These operations are automatically committed in the database that’s why they cannot
be used while creating tables or dropping them.

Here are some commands that come under TCL:

COMMIT ,ROLLBACK ,SAVEPOINT

Oracle data types


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.

The different types of data types in Oracle are:-

 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.

 VARCHAR or VARCHAR2:-This data type is used to store variable


length alphanumeric data. It is a more flexible form of the CHAR data
type. The maximum this data type can hold up to 4000 characters. One
difference between this data type and char data type is oracle compares
varchar values using non padded comparison semantics i.e. the inserted
values will not be padded with spaces. VARCHAR can hold 1 to 255
characters. Varchar is usually a wiser choice than char due to its
variable length format characteristics but keep in mind that char is
much faster than varchar sometimes up to 50%.

 NUMBER: - The number data type is used to store numbers (fixed or


floating point).The precision (P) determines the length of the data
while(s), the scale, determines the number of places after the decimal.
The NUMBER data type that is used to store number data can be specified
either to store integers or decimals with the addition of a parenthetical
precision indicator. If we do not use then the default value is 0 and if we
don’t use precision then by default value stored can be of 38 digits.

 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.
 LONG:-LONG columns store variable-length character strings
containing up to 2 gigabytes, or 231-1 bytes. LONG columns have many of
the characteristics of VARCHAR2 columns. You can use LONG columns to
store long text strings. The length of LONG values may be limited by the
memory available on your computer
The use of LONG values is subject to some restrictions:

 A table can contain only one LONG column.


 You cannot create an object type with a LONG attribute.
 LONG columns cannot appear in WHERE clauses or in integrity constraints (except that they
can appear in NULL and NOTNULL constraints).
 LONG columns cannot be indexed.
 A stored function cannot return a LONG value.
 You can declare a variable or argument of a PL/SQL program unit using the LONGdatatype.
However, you cannot then call the program unit from SQL.
 Within a single SQL statement, all LONG columns, updated tables, and locked tables must be
located on the same database.

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
column of the table uniquely. Each
column has a minimum of three attributes, a name, data type and size (i.e.
column width).

Syntax: -CREATE TABLE<table name>(<column Name 1><data type>(<size>),


<columnname2><data type>(<size>));

Example:

SQL> create table student(name varchar(23),roll_no number(12),class


varchar2(12),address varchar(23));

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.
Syntax:-INSERT INTO <table name >(<columnname1>,<columnname2>)
VALUES(<expression1>,<expression 2>);

OR

INSERT INTO <tablename>VALUES(<expression1 >,<expression2> );

OR

INSERT INTO <tablename>VALUES(‘<&columnname1>’ ,’<&columnname2>’);

Example:-

SQL> insert
intostudent(name,roll_no,class,address)values(‘Prabhat’,06,’BCA’,
Hatlimore’);

1 row created.

Or

SQL> insert into studentvalues(‘kishore’,01,’BCA’,’Nagri’);

1 row created.

Or

SQL>insert into student


values(‘&name’,’&roll_no’,’&class’,’&address’);

Enter value for name: Amarjeet

Enter value for roll_no: 30

Enter value for class: BCA

Enter value for address: airwan

old 1: insert into student


values(‘&name’,’&roll_no’,’&class’,’&address’)

new 1: insert into onditi values(‘Atinder’,’04’,’BCA’,’Sawan


chack’)

1 row created.

FOR inserting more values we use ‘/’ slash after SQL> as below but after
above syntax used:

SQL> /

Enter value for name: Vinay


Enter value for roll_no: 08

Enter value for class: BCA

Enter value for address: Barnoti

old 1: insert into student


values(‘&name’,’&roll_no’,’&class’,’&address’)

new 1: insert into onditi


values(‘Vinay’,’08’,’BCA’,’Barnoti’)

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.
Syntax: - SELECT * FROM <table name>;
If we want to see all the tables that are already exist in the database .we use
SELECT * FROM TAB;
Example:-
SQL> select * from student;

NAME ROLL_NO CLASS ADDRESS


----------------------- ---------- ------------ ----------
Prabhat06BCAHatlimore
Kishore01BCANagri
Amarjeet 30BCAairwan
Vinay 08BCAbarnoti

1 row created.

When we use the command SELECT* FRM TAB;the output is displayed as:-
SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
ABC TABLE
ANKU TABLE
BONUS TABLE
DEPARTMENTS TABLE
DEPT TABLE
EMP TABLE
EMPLOYEE TABLE
EMPLOYEES TABLE
STUDENT TABLE
9 rows selected.

4) ELIMINATION OF DUPLICATE ROWS :-A table could hold duplicate rows in


such a case, only unique rows the distinct
clause can be used.
Syntax: - SELECT DISTINCT <column name 1>,<column name2> FROM <table name> ;
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
-------------------- ----------
Prabhat06

Syntax:-SELECT DISTINCT * from <tablename>;


Example:-
SQL> Select DISTINCT * from student;

NAME ROLL_NO CLASS ADDRESS


------------------ -------- ------------ ----------
Prabhat06BCAHatlimore
5) SORTING DATA IN A TABLE: - Oracle allows data from a table to be viewed in
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.
Syntax: - SELECT * FROM <tablename>ORDER BY<column name1>,<column name
2><[sort order]>;
Example:-
SQL> SELECT * FROM STUDENT ORDER BY name;

NAME ROLL_NO CLASS ADDRESS


----------------------- ---------- ------ ---------------
Amarjeet 30 BCA airwan
Atinder04 BCAsawanchakDushyant34 BCAjagatpur
Kishore01 BCANagri
Prabhat 06 BCAHatlimore
Vinay 08 BCAbarnoti

6 rows selected

SQL> SELECT*FROM STUDENT ORDER BY name desc;


NAME ROLL_NO CLASS ADDRESS
----------------------- ---------- ------------ --------------

Vinay 08 BCAbarnoti
Prabhat 06 BCAHatlimore
Kishore01 BCANagri
Dushyant34 BCAjagatpur
Atinder04 BCAsawanchakAmarjeet 30 BCA
airwan

6 rows selected.

SQL> SELECT * FROM STUDEN ORDER BY roll_nodesc;

NAME ROLL_NO CLASS ADDRESS


----------------------- ---------- ------------
---------------

Kishore01 BCANagri
Atinder04 BCAsawanchak
Prabhat 06 BCAHatlimore
Vinay 08 BCAbarnoti
Amarjeet 30 BCA airwan
Dushyant34 BCAjagatpur

6 rows selected.

6) MODIFYING THE STRUCTURE OF TABLES: - The structure of a table can be


modified by using the ALTER TABLE command. ALTER TABLE allows changing the
structure of an existing table. With ALTER TABLE it is possible to add or delete
columns, create or destroy indexes, changes the data type of existing columns, or
rename columns or the table itself.
(a) ADDING NEW COLUMNS
Syntax: -ALTER TABLE <Table name>ADD(<New column Name><data type>
(<size>),<new column name><data type>(<size>)….);
Example:- alter table student add(mobileno number(8));

(b) DROPPING A COLUMN FROM A TABLE


Syntax: - ALTER TABLE<TABLE NAME>DROP COLUMN<COLUMNNAME>;
Example:- alter table student drop column name;
(c) MODIFYING EXISTING COLUMNS
Syntax: -ALTER TABLE<Table name>MODIFY(<COLUMN NAME><NEW
DATATYPE>(<NEW SIZE>));
Example:-alter table prabhat modify(name varchar(22));

7) RENAMING TABLES: - Oracle allows renaming of tables. The rename operation


is done atomically, which means that no other thread can access any of the tables
while the rename process is running.
Syntax: - RENAME <Table name> to <New Tablename>
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 .
Syntax: -DROP TABLE <table name>;
Example:--

SQL> Drop table student;


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. Truncate table
empties a table completely. Truncate command drop and recreates the table and faster than
delete command.
Syntax:-TRUNCATE table <tablename>
Example:-

SQL> truncate table employees;


Table truncated.
9) DISPLAYING THE TABLE STRUCTURE:-To display information about the columns
defined in a table use the following syntax.
Syntax: - DESCRIBE <table name>
This command displays the columns names, the data types and the special attributes
connected to the table.
Example: -

SQL> describe employees;

Name Null? Type


---------------------------------- -------- -----------
EMP_ID NUMBER(5)
EMP_NAME VARCHAR2(20)
DEPT_ID NUMBER(10)
DEPT_NAMENAME(12)
SALARY NUMBER(21)

10) UPDATING THE CONTENTS OF A TABLE: - The update command is used to change
or modify data values in a table. The verb UPDATE in SQL is used to either all the rows from
a table or a select set of rows from a table.
 UPDATING ALL ROWS:-The update statement updates columns in the existing table’s
rows
with new values .The SET clause indicates which column data should be modifying and
the new values that they should hold. The WHERE CLAUSE specifies which rows should
be updated. Otherwise all table rows are updated.
Syntax: -UPDATE < Table name> SET <column name1>=<expression1> ,<column
name2>=<expression2>;
Eg:- Update student set course=’bca’ ;

 UPDATES RECORDS CONDITIONALLY:-


Syntax :-UPDATE <table name> SET <columnname1> = <expression1>,
<columnname2> = <expression2> WHERE <condition>;
Eg- Update student set course=’bba’ where stdid=’101’;

11)Command to find version of oracle:-

 Connect to the database server. You can find the Oracle version by issuing a
simple SQL statement.

SELECT * FROM v$version;

 Press ↵ Enter

 The Oracle version number appears next to ″Oracle Database″ in the first
line of the result.

12)Clear Screen:- How to Clear Screen in Oracle CMD:-

SQL> Host CLS OR SQL>Clear Screen;


13) SQL WHERE Clause:-

 The WHERE clause is used to filter records.


 The WHERE clause is used to extract only those records that fulfill a
specified condition.
Syntax:-
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Example:-

SQL>Select name from student where rollno=101;

SQL>Select * from student where name=‘Ravi’;

 The following operators can be used in the WHERE clause:


 OperatorDescriptionExample=Equal
 >Greater than
 <Less than
 >=Greater than or equal
 <=Less than or equal
 <>Not equal. Note: In some versions of SQL this operator may be written as !
=
 BETWEEN Between a certain range
 LIKE Search for a pattern
 IN To specify multiple possible values for a column

You might also like