0% found this document useful (0 votes)
11 views22 pages

Lab Dbms

A Relational Database Management System (RDBMS) is software that manages databases using a relational model, organizing data into tables for efficient storage and retrieval. It supports complex queries and transactions while ensuring data security and integrity, but can be complex and resource-intensive for large datasets. SQL (Structured Query Language) is used to interact with RDBMS, categorized into various types including DDL, DML, DQL, DCL, and TCL, each serving specific functions in database management.

Uploaded by

Prerana laha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views22 pages

Lab Dbms

A Relational Database Management System (RDBMS) is software that manages databases using a relational model, organizing data into tables for efficient storage and retrieval. It supports complex queries and transactions while ensuring data security and integrity, but can be complex and resource-intensive for large datasets. SQL (Structured Query Language) is used to interact with RDBMS, categorized into various types including DDL, DML, DQL, DCL, and TCL, each serving specific functions in database management.

Uploaded by

Prerana laha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

RDBMS

( RELATIONAL DATABASE MANAGEMENT SYSTEM)


A Relational Database Management System (RDBMS) is a software system that
manages databases based on the relational model. It organizes data into tables
consisting of rows and columns, facilitating efficient data storage, retrieval, and
manipulation.

KEY POINTS :
• It is a collection of related tables .
Table – It is a data file that stores organised data and also contains similar
records.
• Here, each row if identified as entity , then the table will be considered as
entity set.
• Horizontal lines of the table form the rows , which is also known as tuple or
records.
• Vertical lines of the table form the columns, which is also known as attribute
or fields.
• The facts describing any entity is known as data.
• The tables are stored in the database in form of a structure known as
schemas.

ILLUSTRATION :
( columns /attribute /fields)

STUDENT
Roll no name dept age
1. priya cse 21
2. riya cse 20
3. mukesh cse 22

( rows /tuple /records)

Here, Roll no is the key attribute “ for unique identification .“


EXAMPLES OF RDBMS :

1. ORACLE
2. DB2
3. Microsoft SQL Server
4. MySQL
5. SQLite
6. IBM Lotus
7. SQL Anywhere
8. Visual Fac Pro
9. Vertica

10.Microsoft Access

11. Open Base

12.IBM Informix

13. Ingres

14. Apacha Derby

• Advantages OF RDBMS:
o Efficient data management and retrieval.
o Support for complex queries and transactions.

o Data security and integrity.

o Scalability to handle large datasets.

• Disadvantages OF RDBMS:
o Can become complex with very large or highly interconnected
datasets.

o May require substantial resources for setup and maintenance.

o Less suitable for unstructured data compared to some NoSQL


databases.
DBMS ARCHITECTURE ( ORACLE)
Also known as :

• ( CLIENT - SERVER ARCHITECTURE )


• ( 2 - TIER ARCHITECTURE )

• It runs the DBMS as a process on the server and run a client database
application on each client.

• The client application sends a request for data over the network to the
server .

• When the server receives client request , the DBMS retrieves data from
the database, performs the required processing on the data , and sends
only the requested data back to the client over the network.

3- TIER ARCHITECTURE :
Three-tier architecture is a well-established software design pattern that divides an
application into three distinct layers, each responsible for specific aspects of the
application's functionality.

1. Presentation Tier:

• Function: This is the user interface layer where users interact with the
application. It displays information to the user and collects input.

• Technologies: Commonly implemented using web technologies like HTML,


CSS, and JavaScript for web applications.

2. Application Tier (Logic Tier):

• Function: Also known as the business logic layer, it processes user inputs,
makes logical decisions, and performs computations. It acts as an
intermediary between the presentation and data tiers.

• Technologies: Typically developed using programming languages such as


Java, Python, PHP, or .NET.

3. Data Tier:

• Function: This layer is responsible for storing and managing data. It handles
database operations like querying and updating data.
• Technologies: Utilizes database management systems (DBMS) such as
MySQL, PostgreSQL, Oracle, or SQL Server.

Benefits of Three-Tier Architecture:

• Modularity: Each tier can be developed and maintained independently,


enhancing code manageability.
• Scalability: Allows for scaling individual layers based on demand without
affecting the entire application.
• Maintainability: Simplifies troubleshooting and updates, as changes in one
tier have minimal impact on others.
• Security: Enhances security by isolating the data layer, reducing direct
access to the database.

SQL * PLUS :
SQL*Plus is an interactive and batch query tool provided with every Oracle Database
installation. It offers a command-line interface that allows users to execute SQL and
PL/SQL commands, manage database objects, and perform administrative tasks.

Key Features:
Command Execution: Users can input and run SQL statements, PL/SQL blocks,
and SQL*Plus-specific commands to interact with the Oracle Database.
• Scripting Support: SQL*Plus supports the creation and execution of scripts,
enabling automation of repetitive tasks and batch processing.

• Formatting and Reporting: It provides commands to format query results,


perform calculations, and generate reports, which can be outputted to various
formats, including text and HTML.

• Database Administration: Administrators can use SQL*Plus to start up and


shut down databases, manage user privileges, and perform other essential
administrative functions.

• Environment Customization: The tool allows users to configure the


environment settings, such as setting variables and customizing the display,
to enhance the user experience.

## For getting connected to ORACLE and receiving SQL


prompt we need to give a command in the terminal , as
follows-
Sqlplus username/ password@ipaddress/XEPDB1
- Sqlplus cse22141/[email protected]/XEPDB1

SQL * PLUS FILE COMMAND :

In SQL*Plus, several commands facilitate file operations, enabling users to


save, retrieve, and execute SQL scripts efficiently. Here's an overview of these
commands:

1. SAVE

• Purpose: Saves the current contents of the SQL buffer to a file.


• Syntax: SAVE [FILE_NAME] [CREATE|REPLACE|APPEND]
• Example: SAVE myscript.sql REPLACE
• Description: This command saves the SQL statements currently in the
buffer to a file named myscript.sql. The REPLACE option ensures that if
a file with the same name exists, it will be overwritten.
2. GET

• Purpose: Retrieves the contents of a file into the SQL buffer.


• Syntax: GET [FILE_NAME]
• Example: GET myscript.sql
• Description: This command loads the contents of myscript.sql into the
SQL buffer, allowing for review or execution.

3. START

• Purpose: Executes a script file containing SQL commands.


• Syntax: START [FILE_NAME]
• Example: START myscript.sql
• Description: This command runs the SQL commands stored in
myscript.sql. It's particularly useful for executing batch scripts.

4. @ (AT Sign)
• Purpose: Executes a script file containing SQL commands.
• Syntax: @ [FILE_NAME]
• Example: @myscript.sql
• Description: Functions similarly to the START command, executing the
SQL commands in myscript.sql.
5. @@ (Double AT Sign)

• Purpose: Executes a nested script relative to the directory of the current


script.
• Syntax: @@ [FILE_NAME]
• Example: @@subscript.sql
• Description: When running a script that calls another script located in
the same directory, @@ ensures the called script is found relative to the
current script's location.

6. SPOOL

• Purpose: Directs the output of SQL*Plus to a file.


• Syntax: SPOOL [FILE_NAME]
• Example: SPOOL output.txt
• Description: Begins writing the output of SQL commands to output.txt.
To stop spooling, use SPOOL OFF.
These commands enhance the efficiency of script management
and execution in SQL*Plus, streamlining database interactions.
SQL ( STRUCTURED QUERY LANGUAGE ):
• It was first developed by IBM in 1970 with name SEQUEL.
• In 1986, the ANSI and ISO published SQL standard ;SQL-86
• ANSI published extended standard SQL-89 in 1989.
• SQL is a ideal database language to –
1. Create database and table structures.
2. Perform basic data management operations.
3. Perform complex queries to transform data into useful information.

TYPES OF SQL :

1. DDL (DATA DEFINATION LANGUAGE )


2. DML( DATA MANIPULATION LANGUAGE )
3. DQL( DATA QUERY LANGUAGE)
4. TCL( DATA TRANSACTION LANGUAGE)
5. DCL ( DATA CONTROL LANGUAGE)

In SQL (Structured Query Language), commands are categorized based on their


functionalities to manage and manipulate relational databases. The primary
categories include:

1. Data Definition Language (DDL):


• Purpose: Defines and modifies the structure of database objects such as
tables, indexes, and schemas.
• Common Commands:
o CREATE: Establishes a new table or database.
o ALTER: Modifies an existing database object.
o DROP: Deletes a table or database.
o TRUNCATE: Removes all records from a table, resetting its storage.
o RENAME : This is used to change the name of existing database
objects, such as tables or columns. This operation is essential for
maintaining clarity and consistency in database schemas, especially
during development or restructuring phases.

2. Data Manipulation Language (DML):


• Purpose: Handles the manipulation of data within existing tables.
• Common Commands:
o INSERT: Adds new records to a table.
o UPDATE: Modifies existing records.
o DELETE: Removes records from a table.
3.Data Query Language (DQL):

• Purpose: Retrieves data from the database based on specific criteria.


• Primary Command:
o SELECT: Fetches data from one or more tables.

4. Data Control Language (DCL):


• Purpose: Manages access permissions and controls user privileges.
• Common Commands:
o GRANT: Provides specific privileges to users.
o REVOKE: Removes previously granted privileges.

5. Transaction Control Language (TCL):


• Purpose: Manages transactions within the database, ensuring data integrity.
• Common Commands:
o COMMIT: Saves all changes made during the current transaction.
o ROLLBACK: Reverts changes made during the current transaction.
o SAVEPOINT: Sets a point within a transaction to which one can roll
back.
DIFFERENCE BETWEEN ABOVE 5 TYPES OF SQL LANGUAGE :

PURPOSE of Tab and Dual in SQL LANGUAGE :

In SQL, particularly within Oracle databases, the DUAL table is a special, single-row,
single-column table automatically created along with the data dictionary. It is owned
by the user SYS but is accessible to all users. The table has one column, DUMMY,
defined as VARCHAR2(1), and contains one row with a value of 'X'.

Purpose of the DUAL Table:

• Evaluating Expressions: The DUAL table is commonly used to evaluate


expressions that do not require data from a specific table. For example, to
retrieve the current system date and time, you can execute:
sql
SELECT SYSDATE FROM DUAL;

This query returns the current date and time.

• Performing Calculations: It allows for the execution of arithmetic operations


or functions without referencing actual tables. For instance:

sql

SELECT 2 * 3 AS result FROM DUAL;


This query returns the result of the multiplication, which is 6.

• Retrieving Pseudo column Values: DUAL is used to select values from


pseudo columns like USER or SYSDATE:

sql

SELECT USER FROM DUAL;

This returns the name of the current user.

The DUAL table is a convenient tool in Oracle SQL for scenarios where a table
reference is syntactically required, but no actual table data is necessary.

## TO CHECK INDIVIDUAL USER –

Command in terminal : SQL> select user from dual;


Output : USER

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

CSE22141

Regarding the term "Tab" in SQL, it does not refer to a standard concept or
command. In some contexts, "TAB" might be used as an alias or shorthand for a
table name, but it is not a reserved keyword or a predefined table in SQL.

## TO CHECK WHETHER YOU HAVE CREATED YOUR OWN TABLE –

Command in terminal :
SQL> select * from tab; Output: no rows selected
DATA TYPES :
In SQL, data types define the kind of data that can be stored in a table's columns,
ensuring data integrity and optimizing storage. They can be broadly categorized as
follows:

1. Numeric Data Types:


• Exact Numerics:

o INT or INTEGER: Stores whole numbers.

o SMALLINT: Stores smaller range of whole numbers.

o BIGINT: Stores larger range of whole numbers.

o DECIMAL(p, s) or NUMERIC(p, s): Stores fixed-point numbers with


precision p and scale s.

• Approximate Numerics:

o FLOAT: Stores floating-point numbers.

o REAL: Stores single-precision floating-point numbers.

o DOUBLE PRECISION: Stores double-precision floating-point numbers.

2. Character String Data Types:


• CHAR(n): Fixed-length character string of length n.

• VARCHAR(n): Variable-length character string with a maximum length of n.

• TEXT: Variable-length character string for large text data.

3. Unicode Character String Data Types:

• NCHAR(n): Fixed-length Unicode character string of length n.

• NVARCHAR(n): Variable-length Unicode character string with a maximum


length of n.

• NTEXT: Variable-length Unicode character string for large text data.

4. Binary Data Types:

• BINARY(n): Fixed-length binary data of length n.


• VARBINARY(n): Variable-length binary data with a maximum length of n.

• IMAGE: Variable-length binary data for large binary objects.

5. Date and Time Data Types:


• DATE: Stores date values (e.g., YYYY-MM-DD).
• TIME: Stores time values (e.g., HH:MM:SS).

• DATETIME: Stores both date and time values.

• TIMESTAMP: Stores date and time values, often with time zone information.

6. Boolean Data Type:


• BOOLEAN: Stores TRUE or FALSE values.

7. Other Data Types:

• ENUM: Stores a list of predefined text values.

• SET: Stores a set of predefined text values.

• JSON: Stores JSON-formatted data.

• XML: Stores XML-formatted data.

In SQL, data types define the nature of data that can be stored in a table's columns,
ensuring data integrity and optimizing storage. Here's a brief overview of some
commonly used data types:

1. CHAR:

• Description: Stores fixed-length character strings.


• Usage: Ideal for storing data where the length is consistent, such as country
codes or status flags.

• Example: CHAR(10) reserves 10 bytes for the string, padding with spaces if
the input is shorter.

2. VARCHAR/VARCHAR2:
• Description: Stores variable-length character strings.

• Usage: Suitable for data with varying lengths, like names or addresses.
• Example: VARCHAR2(50) can store up to 50 characters, using only the
necessary space for each entry.

• Note: In Oracle databases, VARCHAR2 is commonly used instead of


VARCHAR. While currently synonymous, VARCHAR is reserved for future
use and may differ in behavior.

3. NUMBER:
• Description: Stores numeric data with optional precision and scale.
• Usage: Used for integers, decimals, and floating-point numbers.

• Example: NUMBER(10, 2) can store numbers up to 10 digits, with 2 digits


after the decimal point.

4. DATE:

• Description: Stores date and time information.

• Usage: Captures dates and times, often including year, month, day, hour,
minute, and second.

• Example: A DATE field can store values like '2025-02-12 10:49:42'.

5. INTEGER:
• Description: Stores whole numbers.

• Usage: Suitable for counting items or representing identifiers.

• Example: INTEGER can store values like 42 or -7.

6. NUMERIC:

• Description: Stores exact numeric values with defined precision and scale.

• Usage: Used when exact precision is crucial, such as in financial calculations.

• Example: NUMERIC(8, 3) can store numbers up to 8 digits, with 3 digits after


the decimal point.

7. LOB (Large Object):

• Description: Designed to store large amounts of data, such as text, images,


or multimedia.

• Types:
o CLOB: Character Large Object for large text data.

o BLOB: Binary Large Object for binary data like images or videos.
• Usage: Ideal for storing extensive data that exceeds typical column size
limitations.

• Example: A CLOB can store large text documents, while a BLOB can store a
video file.
WRITE ADVANTAGE OF VARCHAR2 OVER CHAR :
In Oracle SQL, choosing between the VARCHAR2 and CHAR data types depends
on the nature of the data you intend to store. While both are used for storing
character strings, VARCHAR2 offers several advantages over CHAR:

1. Efficient Storage:

• Dynamic Length: VARCHAR2 stores strings with variable lengths, using only
as much space as needed for each entry, plus a small amount of overhead. In
contrast, CHAR is a fixed-length data type that pads shorter strings with
spaces to match the defined length, potentially leading to wasted storage
space.

2. Performance Considerations:

• Reduced Padding Overhead: Since VARCHAR2 does not pad strings to a


fixed length, it can lead to more efficient storage and retrieval operations,
especially when dealing with a large number of variable-length strings.

3. Flexibility in Data Storage:

• Variable-Length Data: VARCHAR2 is ideal for columns where the length of


data varies significantly, such as names or addresses. This flexibility ensures
that storage is optimized for each entry.

4. Simplified Data Retrieval:

• Elimination of Trailing Spaces: With CHAR, retrieving data can be


cumbersome due to the need to handle trailing spaces added for padding.
VARCHAR2 eliminates this issue, simplifying data retrieval and comparison
operations.

5. Compatibility with External Systems:

• Consistent Data Representation: When interfacing with external systems or


applications that expect variable-length data, VARCHAR2 ensures that the
stored data aligns with these expectations, reducing potential compatibility
issues.
Question 1:
To create a table having student details roll no , name , dept, marks with table
name as STUDENT and insert all the details as follows :

STUDENT

Roll no name Department marks


1. avik Cse 23
2. priya Cse 45
3. riya Ee 54
4. ritika It 32
5. rahul Ece 98

## commands to be written in the terminal step by step , we go


as follows :
• First use the DDL command for table creation

SQL> CREATE table student


(
Roll number(2) primary key,
Name varchar2(15),
Dept varchar2(5) not null,
Marks number(4,2)
);

Output : TABLE CREATED

• Next in order to view the table created :

SQL> SELECT * FROM tab;


Output :

TNAME
TABTYPE CLUSTERID
STUDENT
TABLE

SQL> DESCRIBE STUDENT ;

Or

SQL> DESC STUDENT;

Output ;

Name null? Type

Roll not null number(2)

Name varchar2(15)

Dept not null varchar2(5)

Marks number(4,2)

• NOW in order make changes in the structure level of the table ;

We again use DDL command “ ALTER”

SQL> SELECT * from student;

Output : no rows selected

## FOR INSERTING DATA IN A ROW :


We use DML command “ INSERT”
SQL> INSERT into student

Values (1, ‘avik’,’cse’,23),


Values(2,’priya’,’cse’,45),

Values(3,’riya’,’ee’,54),

Values(4,’ritika’,’it’,32),
Values(5,’rahul’,’ece’,98);

Output:

5 rows selected .

SQL> SELECT * FROM STUDENT

Output :

Roll name dept marks

1 avik cse 23

2 priya cse 45

3 riya ee 54

4 ritika it 32

5 rahul ece 98

** IMPORTANT NOTE **
If we enter the command in this way in the terminal –

SQL>insert into student

values ( 2,’riya’,’ee’,54);

output ;

error at line 1:
ORA-00001 : UNIQUE CONSTRAINTS
## change the order of entry in the data level :

SQL> INSERT INTO STUDENT(NAME , ROLL, MARKS,DEPT)

VALUES(‘avik’,1, 23,’cse’);

Output: 1 row selected

**IF we give the entry of one student with no department ; then it will
give error **
Eg :

SQL> INSERT FROM STUDENT( NAME , MARKS , ROLL)

VALUES(‘riki’,13,6);

Ouput ; error

Can’t insert null into

## METHODS OF INSERTING NULL :

CASE 1 –

SQL> INSERT FROM STUDENT (ROLL , DEPT, MARKS)

VALUES( 7, ‘CSE’,33);

Output : 1 row created .

CASE 2-

SQL> INSERT INTO STUDENT


VALUES(8,NULL,’EE’,91);
Question 2 :
Add column of type number(10) and address of type varchar2(40) in the
previous table .

Here again we use DDL command “ ALTER.”

SQL> alter table student add(mobile number(10),address(40));

Output: table altered.

SQL> SET LINESIZE 500


/

Output ;

Roll no name dept marks Mobile address


1. avik cse 23
2. priya cse 45
3. riya ee 54
4. ritika it 32
5. rahul ece 98

** using ALTER command we can’t add a not null attribute **

## ADDING A NEW COLUMN TO THE TABLE :

SQL>ALTER TABLE STUDENT ADD(ADDRESS NUMBER(10));

## FOR ALTERING THE TABLE :

SQL> ALTER TABLE STUDENT MODIFY ADDRESS NUMBER(10);

Output: table altered.


SQL> SET LINESIZE 50

DESC STUDENT;

Output :

Name null? Type

Roll not null number(2)


Name varchar2(15)

Dept not null varchar2(5)

Marks number(4,2)

Mobile number(10)

Address number(10)

## DROPPING A COLUMN N :

SQL> ALTER TABLE STUDENT DROP COLUMN ADDRESS;

Output :

Name null? Type

Roll not null number(2)


Name varchar2(15)

Dept not null varchar2(5)

Marks number(4,2)

Mobile number(10)

** IMPORTANT NOTE**

FOR SAVING ALL THE ENTIRE INFORMATION DATA , we have two methods –
1.use COMMIT COMMAND
2.RUN ANY DDL COMMAND at last

** TCL mainly control all DML commands**

## INSERT MOBILE NUMBER :

We use here DML command “UPDATE”.

Few steps we need to go through for better observation here

SQL> ROLLBACK;

Output; rollback complete

( data permanently saved hence not deleted any thing)

SQL> UPDATE STUDENT SET MOBILE = 9978234501;

Output: ( all roll no.s have the same mobile no assigned )

Output ;

Roll no name dept marks Mobile address


1. avik cse 23 9978234501
2. priya cse 45 9978234501
3. riya ee 54 9978234501
4. ritika it 32 9978234501
5. rahul ece 98 9978234501

**WE CAN USE ROLLBACK AFTER DML COMMAND TO GET BACK BUT
WITHOUT USING ANY DDL COMMAND OR COMMIT COMMAND**

SQL> UPDATE STUDENT SET MOBILE=2134567891

WHERE ROLL=1;
Output ;

Roll no name dept marks Mobile address


1. avik cse 23 2134567891
2. priya cse 45 9978234501
3. riya ee 54 9978234501
4. ritika it 32 9978234501
5. rahul ece 98 9978234501

SQL> COMMIT;

SQL> ROLLBACK;

Output : [ no changes in the data ]

You might also like