Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 45

PRACTICAL FILE

OF
RELATIONAL
DATABASE
MANAGEMENT
SYSTEM (RDBMS)

SUBMITTED TO: SUBMITTED BY:


Prof.Kamaljeet kaur Simranpal Singh Rana
Associate Professor BCA-II(B)
Department of Computer science
1
INDEX
SR. QUARRY NAME PAGE
NO. NO.
1 INTRODUCTION 4
2 INTRODUCTION TO MYSQL 7
3 INSTALLATION OF MYSQL 8
4 CREATE DATABASE STATEMENT 14
5 CREATE TABLE STATEMENT 16
6 RETRIEVAL OF ROW USING SELECT STATEMENT 18
7 CONDITIONAL RETRIEVAL OF ROWS STATEMENT 19
8 MY SQL ALTER AND DROP STATEMENT 20
9 UPDATE AND DELETE STATEMENT 22
10 JOIN SEQUENCES 25
11 COMMIT AND ROLLBACK 26
12 MYSQL IS NULL CONDITION 27
13 MYSQL AGGREGATE FUNCTION 29
14 ORDERING THE RESULT OF QUERIES 30
15 MYSQL OPERATORS 32
16 GROUPING THE RESULT OF QUERIES 34
17 LOWER QUERY 36
18 UPPER QUERY 37
19 LENGTH QUERY 38
20 CONCAT QUERY 39
21 INSTR QUERY 40
22 SUBSTR QUERY 41
23 UNION OF ALL OPERATOR 42
24 INTERSECTION OPERATOR 43

2
25 INTERSECTION ALL STATEMENT 41
26 CREATE VIEW STATEMENT 42
27 CREATE INDEX STATEMENT 42
28 UNIQUE INDEX STATEMENT 43
29 DROP INDEX 45
30 PL/SQL ARCHITECTURE 46

3
INTRODUCTION
Structured Query Language( SQL)

SQL is a special-purpose programming language


designed for managing data stored in a relational
database management system.

SQL is the language with which a coder


communicates with a database to manipulate
its data.

It is their guiding hand, voice, and fingertips


dragging across a screen, helping the coder
navigate and organise the data as they see fit.

It is how a coder converses with the


machine.

Features of Structured Query Language (SQL)


SQL is one of the most demanding skills in the current
world. Every day a huge amount of data is
4
collected and one has to deal with these
databases to create insightful information

1.Data Definition Language (DDL) – DDL


contains commands that define data
create – Creates a table
For example:
create table
tablex(attribute1 datatype…..attribute
datatype);
drop – Deletes the table along with all the
attributes
For example:
drop table tablex;
alter – Modifies the tables structures
For example:
alter table
tablex add(new row1 datatype……new rown
datatype);
rename – Changes the name of a table.
For example:
rename tablex to tabley;

2.Data Manipulation Language


(DML):
DML contains commands that manipulate data

5
insert– Used after the create command to
insert values in the table
For example:
insert into tabley values(attribute1
datatype);
:
:
:
insert into tabley values(attributen
datatype);
delete– Deletes particular rows, tuples, or
cardinality from the table
For example:
delete from tabley where condition;
update– Updates the tuples in the table
For example:
update tabley set
tuplename=’attributename’;
Triggers– Performed when particular
conditions are met on data.

Client-server execution and remote database


access – SQL commands can control how a client
application is allowed to access the database
remotely.

Security and authentication – SQL can make


sure that only specific details of the
6
database are visible to the user while the entire
database is secured by DBMS.

Embedded SQL – SQL can embed languages like


COBOL, C, Java, etc. to query at runtime

Transaction Control Language – TCL is used to


control the transactions with commands like:

commit – Saves the database when it’s


inconsistent
commit;
rollback – Rolls back to the previous
transaction
rollback;
savepoint – Goes back to the previous
transaction without going through the entire
transaction
savepoint;

7
..............................................................
Installation of SQL:
Steps to install SQL on windows 10

Step 1: Search and Download the MySQL


community server

Step 2: Open up the downloaded file and let's start the


installation procedure. Select the Server only and next

8
Step 3: Click on the Execute and its automatically
downloads and installs the required files .

Step 4: Just click next->next->next….. and then create a


password.
9
10
Step 5: and again next->execute->finish->next…. After
that enter the password created in the previous step.

Step 6: After the installation is completed just


open the MySQL workbench and add a localhost
user and done….

11
2) (I) SQL Data types
An SQL developer must decide what type of data that will be
stored inside each column when creating a table. The data type
is a guideline for SQL to understand what type of data is
expected inside of each column, and it also identifies how SQL
will interact with the stored data.

● String Data types


● Numeric Data types
12
● Date and time Data types

MySQL String Data Types

Data type Description


A FIXED length string (can contain letters,
numbers, and special characters). The size
CHAR(size)
parameter specifies the column length in
characters - can be from 0 to 255. Default is 1
A VARIABLE length string (can contain letters,
VARCHAR numbers, and special characters). The size
(size) parameter specifies the maximum string length in
characters - can be from 0 to 65535

MySQL Numeric Data Types


Data
Description
type
A bit-value type. The number of bits per value is specified
BIT(size) in size. The size parameter can hold a value from 1 to 64.
The default value for size is 1.

A medium integer. Signed range is from -2147483648


INT to 2147483647. Unsigned range is from 0 to
(size) 4294967295. The size parameter specifies
the maximum display width (which is 255)

MySQL Date and Time Data Types

13
Data type Description
A date. Format: YYYY-MM-DD. The supported
DATE
range is from '1000-01-01' to '9999-12-31'
A date and time combination. Format: YYYY-
MM-DD hh:mm:ss. The supported range is from
DATETIME'1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
(fsp) Adding DEFAULT and ON UPDATE in the
column definition to get automatic initialization
and updating to the current date and time

SQL Database
Here, we will discuss the queries and will
understand with the help of examples
Query :
Show existing databases –
Let’s consider the existing database like 22407_simranpal,
information_schema, mysql, performance_schema, rana,
sakila, sys, world . And if you want to show the exiting
database then we will use the show database query as follows:

Syntax- SHOW DATABASES;

Example:
14
.

Query:
Create a database –
Suppose we want to create a database namely a

student Syntax- CREATE DATABASE MGC college;

Example- SHOW DATABASES;

15
Query :
Using a database –
Syntax- USE mgccollege;
Example:

16
(II) Creating a Table: Query:
Create – used to create a new table in a database.
Here datatype may be varchar, integer, date, etc.
Syntax
CREATE TABLE table_name
( column1 datatype,
column2 datatype, ....);
Example:

Query:
Describe- to describe the structure of the table
record. Syntax
Describe table_name;
Example:

17
Query:
Insert- is used to insert values into an existing
table. Syntax
INSERT INTO table_name (column1, column2, column3, ...);

Example:

(III) Retrieval of Rows using Select statement


Select- is used to select data from a database. The data
returned is stored in a result table, called the result-set.
Syntax:
SELECT column1, column2, …FROM table_name;
OR
SELECT * FROM table_name;

18
Example:

(IV) Conditional Retrieval of Rows & Columns:


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

19
Example:

(V) Alter & Drop Statements:


Alter-The ALTER TABLE statement is used to
add, delete, or modify columns in an existing
table. It is also used to add and drop various
constraints on an existing table. Syntax:
ALTER TABLE table_name ADD
column_name datatype;
Example:

20
After ALTER Table:-

DROP- The DROP TABLE statement is used to


drop an existing table in a database.
Syntax: DROP TABLE table_name;

21
Example:

3). (I)Matching a Pattern from a Table :


● IN operator:
The IN operator allows you to specify multiple
values in a WHERE clause.
The IN operator is a shorthand for multiple
OR conditions. Syntax:
SELECT column_name(s) FROM table_name
WHERE column_name IN (value1, value2, ...);
Example:

22
● BETWEEN operator:
The BETWEEN operator selects values within a given
range. The values can be numbers, text, or dates. It is
inclusive: begin and end values are included.
Syntax: SELECT column_name(s)FROM
table_name WHERE column_name BETWEEN
value1 AND value2;
● LIKE operator:
The LIKE operator is used in a WHERE clause to
search for a specified pattern in a column. There
are two wildcards often used in conjunction with
the LIKE operator:
● The percent sign (%) represents zero, one, or
multiple characters
● The underscore sign (_) represents one, single
character Syntax:
SELECT column1, column2, ...FROM
table_name WHERE column LIKE pattern;
Example:

23
(II) Ordering the Result of a Query:
ORDER BY- The ORDER BY keyword is used to
sort the result-set in ascending or descending
order. The ORDER BY keyword sorts the records
in ascending order by default. To sort the records
in descending order, use the DESC keyword.
Syntax: SELECT column1, column2, ...FROM
table_name ORDER BY column1, column2, ...
ASC| DESC;
Example:

24
(III) AGGREGATE FUNCTIONS:
An aggregate function in SQL performs a calculation
on multiple values and returns a single value. SQL
provides many aggregate functions that include avg,
count, sum, min, max, etc. An aggregate function
ignores NULL values when it performs the
calculation, except for the count function.
(i) COUNT( ) FUNCTION
The COUNT( ) function returns the number of rows
that matches a specified criterion.
Syntax:
SELECT COUNT(column_name)FROM
table_name WHERE condition;
Example:

(ii)AVG( ) FUNCTION
The AVG( ) function returns the average
value of a numeric column.
25
Syntax:
SELECT AVG(column_name) FROM
table_name WHERE condition;

Example:

(iii) SUM( ) FUNCTION


The SUM( ) function returns the total sum of a
numeric column.
Syntex:
SELECT SUM(column_name) FROM
table_name WHERE condition;
Example:

26
(IV)
Grouping the Result of a Query:
GROUP BY:
It groups rows that have the same values into summary
rows. The GROUP BY statement is often used with
aggregate functions (COUNT(), MAX(), MIN(), SUM(),
AVG()) to group the result-set by one or more columns.

Syntax:
SELECT column_name(s)FROM table_name
WHERE condition GROUP BY column_name(s)
ORDER BY column_name(s);
Example:

DELETE
The DELETE statement is used to delete
existing records in a table.
Syntax:
DELETE FROM table_name WHERE condition;
27
Example :

MySQL IS NULL Condition


MySQL IS NULL condition is used to check if
there is a NULL value in the expression. It is
used with SELECT, INSERT, UPDATE and
DELETE statement.
Syntax:
expression IS NULL
Parameter

28
expression: It specifies a value to test if it is
NULL value.

Consider a table "rana" having the following data.

Execute the following query:


● SELECT *
● FROM officers
● WHERE officer_name IS NULL;

Output:

29
MySQL Aggregate Functions
MySQL's aggregate function is used to perform
calculations on multiple values and return the result
in a single value like the average of all values, the
sum of all values, and maximum & minimum value
among certain groups of values. We mostly use the
aggregate functions with SELECT statements in the
data query languages. Syntax:
mysql> SELECT COUNT(name) FROM rana;

30
Count() Function
MySQL count() function returns the total
number of values in the expression. This
function produces all rows or only some rows
of the table based on a specified condition, and
its return type is BIGINT. It returns zero if it
does not find any matching rows. It can work
with both numeric and non-numeric data types.
Example
Suppose we want to get the total number of
employees in the employee table, we need to use the
count() function as shown in the following query:
mysql> SELECT COUNT(name) FROM rana;
Output:

31
Ordering the result of
queries Syntax:-
select * from student order by Roll_No desc;

MySQL Operators.
The MySQL IN condition is used to reduce the
use of multiple OR conditions in a SELECT,
INSERT, UPDATE and DELETE statement.
In Operator
Syntax:

32
SELECT * FROM officers WHERE
officer_name = 'Ajeet'
OR
officer_name = 'Vimal' OR
officer_name = 'Deepika';

Execute the following query:


● SELECT *
● FROM officers
● WHERE officer_name IN ('Ajeet', 'Vimal',
'Deepika');

Output:

33
Lower.
The SQL LOWER () function is used to
convert all characters of a string to lower
case. Syntax: LOWER (SQL
course)
Example:

UPPER.
The SQL UPPER () function is used to
convert all characters of a string to
uppercase. Syntax: UPPER(SQLcourse)

34
example:

Length.
This function returns the length
of the input string. Syntax:
LENGTH (Column | Expression)
Example:

CONCAT.
35
This function always appends (concatenates) string2 to
the end of string1.
Syntax: CONCAT ('String1', 'String2')

Example:

INSTR.
This function returns numeric position of a
character or a string in a given string.
Syntax: INSTR (Column | Expression, 'String', [,m],
[n]) Example

36
SUBSTR.
This function returns a portion of a string from a given
start point to an end point.
Syntax: SUBSTR ('String', start-index, length of
extracted string)

Example:

Union
ALL operator A union is used for extracting rows using
the conditions specified in the query while Union All is
used for extracting all the rows from a set of two tables.
Syntax: select column name(s) from table1 UNION
ALL Select column name(s)from table2;

37
Intersection operator
The UNION ALL command combines the result set of two
or more SELECT statements (allows duplicate values).
Syntax: select column name(s) from
table1 Intersect
Select column name(s)from table2;

38
Intersection ALL statement

Syntax: select column name(s) from table1


Intersect
Select column name(s)from table2;

Create view statement


A view contains rows and columns, just like a real
table. The fields in a view are fields from one or more
real tables in the database.
Syntax: Create view view name
AS Select column1, column2
From table name
Where condition;

39
Create index statement
Indexes are used to retrieve data from the database
more quickly than otherwise. The users cannot see the
indexes, they are just used to speed up searches/queries.
Syntax: Create index index name
ON table name (column1, column2);

Unique index statement


Creates a unique index on a table. Duplicate values
are not allowed
Syntax: Unique index index name
ON table name (column1, column2)

40
Drop index statement
The DROP INDEX statement is used to delete an
index in a table.
Syntax: Drop index index
name ON table name;

PL/SQL Architecture

PL/SQL is a block structured language. The programs of


PL/SQL are logical blocks that can contain any number of
nested sub-blocks. Pl/SQL stands for "Procedural
Language extension of SQL" that is used in Oracle.
PL/SQL is integrated with Oracle database (since version
7). The functionalities of PL/SQL usually extended after
each release of Oracle database. Although PL/SQL is
closely integrated with SQL language, yet it adds some
programming constraints that are not available in SQL.

PL/SQL Functionalities

PL/SQL includes procedural language elements like


conditions and loops. It allows declaration of constants and
variables, procedures and functions, types and variable of
41
those types and triggers. It can support Array and handle
exceptions (runtime errors). After the implementation of version
8 of Oracle database have included features associated with
object orientation. You can create PL/SQL units like
procedures, functions, packages, types and triggers, etc. which
are stored in the database for reuse by applications.

With PL/SQL, you can use SQL statements to manipulate


Oracle data and flow of control statements to process the data.

The PL/SQL is known for its combination of data


manipulating power of SQL with data processing power
of procedural languages. It inherits the robustness,
security, and portability of the Oracle Database.

PL/SQL is not case sensitive so you are free to use lower case
letters or upper case letters except within string and character
literals. A line of PL/SQL text contains groups of characters
known as lexical units. It can be classified as follows:

○ Delimeters
○ Identifiers
○ Literals
○ Comments

Features of PL/SQL :

The various features of PL/SQL are given below –

● PL/SQL runs on various operating systems such as


windows, Linux etc.

● PL/SQL have an error-checking facility and displays user-


friendly messages when an error occurs in a program.
42
What do you mean by PL/SQL Architecture?

The PL/SQL runtime system is a technology and not an


independent product. This technology is actually like an
engine that exhibits PL/SQL blocks, subprograms like
functions and procedures. This engine can be installed in
an Oracle Server or in application development tools
such as Oracle Form Builder, Oracle Reports Builder etc.

PL/SQL Architecture
PL/SQL can reside in two environments –

1. The Oracle Server

43
2. The Oracle tools

These two environments are independent of each other. In


either environment, the PL/SQL engine accepts any valid
PL/SQL block as input. The PL/SQL engine executes the
procedural part of the statements and sends the SQL
statement executer in the Oracle Server. A single transfer is
required to send the block from the application to the Oracle
Server, thus improving performance, especially in a Client-
Server network. PL/SQL code can also be stored in the
Oracle server as subprograms that can be referenced by
any number of applications connected to the database.

Advantages of PL/SQL :
● PL/SQL provides better performance.
● PL/SQL has high Productivity.
● It supports Object-Oriented Programming concepts.
● It has Scalability and Manageability.
● PL/SQL supports various Web Application
Development tools.

Disadvantages of PL/SQL :

● PL/SQL requires high memory.


● Lacks of functionality debugging in stored procedures.

44

You might also like