DBMS
DBMS
PRACTICAL FILE
OF
DATABASE MANAGEMENT SYSTEMS
SUBJECT CODE – BTCS-505-20
227024 SIGNATURE
• Introduction to ORACLE and oraclelive.com compiler
• Features of ORACE and Roadmap of various Versions
• SQL Fundamentals
a. DDL
b. DML
c. DCL
d. TCL Introduction to various Data Types.
RDBMS Database
A relational database management system (RDBMS) stores data in a tabular form where
a column represents a property and each row in a table represents a record. RDBMS
allows Create, Read, Update, and Delete (CRUD) operations. Structured Query
Language (SQL) is the language that is used to query, update, and delete data in
SQL language queries are also known as SQL commands or SQL statements.
1. Oracle
2. MySQL
3. SQL Server
4. PostgreSQL
5. IBM DB2
6. Microsoft Access
7. SQLite
8. MariaDB
9. Informix
227024 SIGNATURE
Oracle
Oracle Database is a relational database management system (RDBMS) developed by
Oracle Corporation. It is a software system that allows users to store, organize, and retrieve
The Oracle Database is designed to run on various operating systems, including Windows,
Unix, and Linux. Businesses and organizations of all sizes use it for various applications,
data warehousing.
Oracle Live SQL is a web-based SQL editor with an Oracle database built-in. It’s
maintained by Oracle and allows you to write and run SQL statements easily without
As of October 2022, Oracle Live SQL runs Oracle database version 19c Enterprise Edition
–
19.14.0.0.0.
How to Access?
227024 SIGNATURE
You’ll see a page that looks like this:
The main feature of this page is to allow you to search or browse for tutorials. We’ll cover
this later in the guide, but Oracle Live SQL includes a range of tutorials to help you learn
specific features of Oracle, where you can read instructions and run the SQL right in the
browser.
We can also just access an SQL editor by clicking Start Coding Now.
Once you’ve signed in, this is the main Oracle Live SQL screen you’ll see.
227024 SIGNATURE
There is a range of things on the screen:
Sidebar: On the left, you’ll see a sidebar that includes a few menu items such as Home,
My
Session, and Schema. We’ll cover these options later in this guide. If you don’t see this
sidebar, you can click on the hamburger menu to show and hide it.
SQL Worksheet: the top part of the main area of the screen is the SQL Worksheet. You
Output: the output of your SQL statements is shown at the bottom of the screen. This can
be resized up and down to make more room for the SQL or more room for the output.
Buttons: on the top right of the screen you can see a few buttons:
preference.
227024 SIGNATURE
• Save: save your SQL worksheet
Footer: on the bottom of the screen, you can see some information about the tool: what
version of Oracle Live SQL is published, what Oracle database version it uses, and some
• High availability: The database has built-in mechanisms for data protection and
such as encryption, access controls, and auditing, ensuring that data is kept safe
caching, indexing, and query optimization technologies, as well as support for in-
• Manageability: Oracle Database includes a range of tools and features for managing
the database, such as graphical user interfaces, command-line interfaces, and APIs,
which simplify tasks such as database monitoring, backup and recovery, and
performance tuning.
227024 SIGNATURE
• Compatibility: The database supports a wide range of programming languages and
interfaces, including SQL, PL/SQL, Java, and .NET, allowing developers to build
custom applications that can access and manipulate the data in the database.
History of Oracle
Oracle Corporation was founded by Lawrence Ellison (Larry Ellison), Bob Miner, Ed
Oates, and Bruce Scott in August 1977. They have a lot of experience in building database
programs for several companies and builds their first project (a special database program)
for the CIA (Central Intelligence Agency). Oracle was named after "Project Oracle," a
project for one of their clients named Central Intelligence Agency, and the company that
created Oracle was called Systems Development Labs (SDL). Systems Development Labs
was renamed Relational Software Inc. (RSI) in 1978 to expand their market for the new
database. They had again changed the name of the company from RSI to Oracle Systems
Corporation in 1982.
The first commercially available RDBMS named Oracle V2 (Version 2) was built using
PDP-
commercial RDBMS in 1977, it wasn't available for purchase until 1979, when Oracle
In 1983, Oracle database portable version named "Oracle version 3" was released. This
version was written in the C programming language. It was the first relational database that
can run in mainframes, minicomputers, PCs, or any hardware with a C compiler. It also
227024 SIGNATURE
The other subsequent versions are:
This new feature has the capability to connect the client's software to a database
o In 1989, Oracle 6 added support for PL/SQL language. It also comes with new
features such as OLTP high-speed systems, hot backup capability, and row-level
locking.
o In 1992, Oracle 7 was released. This version comes in the market as a result of four
years of hard work and two years of customer testing. It added some exciting
performance. o In 1997, Oracle 8 was released. This version comes with the support
of ORDBMS that was designed to work with Oracle's network computer (NC). It
o In 1998, Oracle 8i was released. Here 'I' stands for Internet. It was the first database
version that added support for Web technologies such as Java and HTTP.
o In 2001, Oracle 9i was released with 400 new features such as XML, RAC (Real
Application Clusters), etc. These features reduce database size and provide high
o In 2003, Oracle 10g was released with grid computing technology means grid. It
o In 2006, Oracle 11g was released. This version comes with new features such as
227024 SIGNATURE
Column Partitioning, Case sensitive passwords, Online Patching, Parallel Backups
o In Feb 2018, Oracle 18 C was released. This version was the world's first
autonomous database.
Introduction to SQL
Structure Query Language(SQL) is a database query language used for storing and
managing data in Relational DBMS. SQL was the first commercial language introduced for
E.F Codd's Relational model of database. Today almost all RDBMS(MySql, Oracle,
Infomix, Sybase, MS Access) use SQL as the standard database query language. SQL is
SQL Command
This includes changes to the structure of the table like creation of table, altering table,
All DDL commands are auto-committed. That means it saves all the changes permanently
in the database.
Command Description
227024 SIGNATURE
truncate delete data from table
DML commands are used for manipulating the data stored in the table and not the table
itself.
DML commands are not auto-committed. It means changes are not permanent to database,
Command Description
These commands are to keep a check on other commands and their affect on the database.
These commands can annul changes made by other commands by rolling the data back to
its original state. It can also make any temporary change permanent.
Command Description
227024 SIGNATURE
rollback to undo change
Data control language are the commands to grant and take back authority from any
database user.
Command Description
In Oracle, every value has a data type that defines a set of characteristics for the value.
These characteristics cause Oracle to treat the values of one data type differently from the
values of another. For example, you can add values of the NUMBER data type, but not
When creating a new table, you specify a data type for each of its columns. Similarly, when
you create a new procedure, you specify a data type for each of its arguments.
The data type defines the allowed values that each column or argument can store. For
example, a DATE column cannot store a value of February 30, because this is not a valid
date.
Oracle has a number of built-in data types illustrated in the following table:
227024 SIGNATURE
1 VARCHAR2(size [BYTE | CHAR])
1 NVARCHAR2(size)
2 NUMBER[(precision [, scale]])
8 LONG
12 DATE
21 BINARY_FLOAT
22 BINARY_DOUBLE
23 RAW(size)
24 LONG RAW
69 ROWID
96 NCHAR[(size)]
112 CLOB
112 NCLOB
113 BLOB
114 BFILE
227024 SIGNATURE
231 TIMESTAMP [(fractional_seconds)] WITH LOCAL TIMEZONE
Each data type has a code managed internally by Oracle. To find the data type code of a
VARCHAR.
The NCHAR and NVARCHAR2 data types are for storing Unicode character strings.
The fixed-length character data types are CHAR, NCHAR and the variable-length character
VARCHAR is the synonym of VARCHAR2. However, you should not use VARCHAR
because Oracle may change its semantics in the future. For character data
The NUMBER data type has precision p and scale s. The precision ranges from 1 to 38
If you don’t specify the precision, the column can store values including fixed-point and
Datetime data types are DATE, TIMESTAMP, TIMESTAMP WITH TIME ZONE, and
TIMESTAMP WITH LOCAL TIME ZONE. The values of a datetime data type are
datetimes.
The interval data types are INTERVAL YEAR TO MONTH and INTERVAL DAY TO
227024 SIGNATURE
SECOND. The values of the interval data type are intervals.
The RAW and LONG RAW data types are for storing binary data or byte strings e.g., the
The RAW data type can store up to 2000 bytes while the LONG RAW data type can store
up to 2GB.
227024 SIGNATURE
Sno Varchar2 5 Primary Key
Sname Varchar2 20 Unique
City Varchar2 20 City must be New Delhi,
Patiala, Amritsar and Qadian
Status Number 3 Must be greater than 10
CODE:
CREATE TABLE S (
Sno VARCHAR2(5) PRIMARY KEY,
Sname VARCHAR2(20) UNIQUE,
City VARCHAR2(20) CHECK (City IN ('New Delhi', 'Patiala', 'Amritsar', 'Qadian')),
Status NUMBER(3) CHECK (Status > 10)
);
Explanation of the Code:
1. Sno VARCHAR2(5) PRIMARY KEY: Defines the Sno column as a VARCHAR2 type with
a maximum width of 5 characters and sets it as the primary key, which ensures that
the values in this column are unique and not null.
2. Sname VARCHAR2(20) UNIQUE: Defines the Sname column as a VARCHAR2 type
with a maximum width of 20 characters and adds a unique constraint to ensure that
all values in this column are distinct.
3. City VARCHAR2(20) CHECK (City IN ('New Delhi', 'Patiala', 'Amritsar', 'Qadian')):
Defines the City column as a VARCHAR2 type with a maximum width of 20 characters
and enforces a check constraint to ensure that the values in this column can only be
one of the specified cities: 'New Delhi', 'Patiala', 'Amritsar', or 'Qadian'.
4. Status NUMBER(3) CHECK (Status > 10): Defines the Status column as a NUMBER
type with a maximum of 3 digits and enforces a check constraint to ensure that the
values in this column are greater than 10.
OUTPUT:
227024 SIGNATURE
b). Relation Name: P
CODE:
CREATE TABLE P (
Pno VARCHAR2(5) PRIMARY KEY,
Pname VARCHAR2(20) UNIQUE,
Color VARCHAR2(20) NOT NULL,
City VARCHAR2(20) CHECK (City IN ('Jalandhar', 'Patiala', 'Amritsar', 'Qadian'))
);
1. Pno VARCHAR2(5) PRIMARY KEY: Defines the Pno column as a VARCHAR2 type
with a maximum width of 5 characters and sets it as the primary key. This ensures that each
value in this column is unique and not null.
2. Pname VARCHAR2(20) UNIQUE: Defines the Pname column as a VARCHAR2 type
with a maximum width of 20 characters and adds a unique constraint to ensure that all values
in this column are distinct.
3. Color VARCHAR2(20) NOT NULL: Defines the Color column as a VARCHAR2 type
with a maximum width of 20 characters and adds a NOT NULL constraint, which ensures
that this column cannot have null values.
4. City VARCHAR2(20) CHECK (City IN ('Jalandhar', 'Patiala', 'Amritsar', 'Qadian')):
Defines the City column as a VARCHAR2 type with a maximum width of 20 characters and
enforces a check constraint to ensure that the values in this column can only be one of the
specified cities: 'Jalandhar', 'Patiala', 'Amritsar', or 'Qadian'.
OUTPUT:
227024 SIGNATURE
C). Relation Name: SP
CODE:
CREATE TABLE SP (
Sno VARCHAR2(5),
Pno VARCHAR2(5),
Qty NUMBER(4),
PRIMARY KEY (Sno, Pno),
FOREIGN KEY (Sno) REFERENCES Supplier(Sno),
FOREIGN KEY (Pno) REFERENCES Part(Pno),
CHECK (Qty > 100)
);
1. Sno VARCHAR2(5): Defines the Sno column as a VARCHAR2 type with a maximum width
of 5 characters. This column will reference the Sno column from the Supplier table.
2. Pno VARCHAR2(5): Defines the Pno column as a VARCHAR2 type with a maximum
width of 5 characters. This column will reference the Pno column from the Part table.
227024 SIGNATURE
3. Qty NUMBER(4): Defines the Qty column as a NUMBER type with up to 4 digits.
4. PRIMARY KEY (Sno, Pno): Defines a composite primary key on the combination of Sno
and Pno, ensuring that each pair of Sno and Pno is unique within the SP table.
5. FOREIGN KEY (Sno) REFERENCES Supplier(Sno): Establishes a foreign key constraint
on the Sno column, ensuring that every value in Sno exists in the Sno column of the Supplier
table.
6. FOREIGN KEY (Pno) REFERENCES Part(Pno): Establishes a foreign key constraint on
the Pno column, ensuring that every value in Pno exists in the Pno column of the Part table.
7. CHECK (Qty > 100): Enforces a check constraint on the Qty column to ensure that all values
are greater than 100.
OUTPUT:
II). Identify the primary key of SP table and apply Primary constraint on that.
PRIMARY KEY (Sno, Pno): This line defines a composite primary key on the combination
of Sno and Pno. This ensures that the combination of these two columns is unique across all
rows in the SP table.
In this table:
Sno and Pno together form the primary key, ensuring that each combination of these values is
unique in the SP table.
The primary key constraint enforces uniqueness and helps in efficient data retrieval.
Statement:
ALTER TABLE SP
ADD CONSTRAINT sp_pk
PRIMARY KEY (Sno, Pno);
227024 SIGNATURE
SNO NAME CITY STATUS
S1 SUNEE QADIAN 20
T
S2 ANKIT AMRITSA 10
R
S3 AMIT PATIALA 30
CODE:
Explanation:
INSERT INTO S (Sno, Sname, City, Status): Specifies the table and columns into which
data will be inserted.
VALUES ('S1', 'SUNEET', 'QADIAN', 20): Specifies the values to be inserted into the
columns Sno, Sname, City, and Status, respectively.
227024 SIGNATURE
P1 NUT RED QADIAN
P2 BOLT GREEN AMRITSAR
P3 SCREW BLUE JALANDHAR
P4 SCREW RED QADIAN
CODE:
Explanation:
INSERT INTO P (Pno, Pname, Color, City): Specifies the table P and the columns
into which data will be inserted.
VALUES ('P1', 'NUT', 'RED', 'QADIAN'): Specifies the values for the columns Pno,
Pname, Color, and City, respectively.
227024 SIGNATURE
S1 P2 300
S1 P3 500
S2 P1 250
CODE:
Explanation:
INSERT INTO SP (Sno, Pno, Qty): Specifies the table SP and the columns into which data
will be inserted.
VALUES ('S1', 'P1', 250): Specifies the values for the columns Sno, Pno, and Qty.
b). Check the working of all the constraints by inserting the invalid data in the
corresponding columns.
1). TABLE S
i). Primary Key Violation:
227024 SIGNATURE
INSERT INTO S (Sno, Sname, City, Status)
VALUES ('S1', 'SUNEET', 'QADIAN', 20);
INSERT INTO S (Sno, Sname, City, Status)
VALUES ('S1', 'ANOTHER_NAME', 'QADIAN', 30); ----//Should fail due to duplicate Sno
2). TABLE P
3). TABLE SP
227024 SIGNATURE
VALUES ('S1', 'P1', 250);
INSERT INTO SP (Sno, Pno, Qty)
VALUES ('S1', 'P1', 300); --// Should fail due to duplicate combination of Sno and Pno
FOR TABLE S
Primary Key Violation:
Unique Constraint Violation:
Check Constraint Violation (City):
Check Constraint Violation (Status):
FOR TABLE P
Primary Key Violation:
Unique Constraint Violation:
Not Null Constraint Violation:
Check Constraint Violation (City):
FOR TABLE SP
Primary Key Violation:
Foreign Key Constraint Violation (Sno):
Foreign Key Constraint Violation (Pno):
Check Constraint Violation (Qty):
Column-Level Constraints
CREATE TABLE Employee (
227024 SIGNATURE
EmpID INT PRIMARY KEY -- Primary Key Constraint at column level
Name VARCHAR(100) NOT NULL, -- Not Null Constraint at column level
Email VARCHAR(100) UNIQUE, -- Unique Constraint at column level
Salary DECIMAL(10, 2) CHECK (Salary > 0) -- Check Constraint at column level
);
* Primary Key Constraint: Ensures EmployeeID is unique and not null.
* Not Null Constraint: Ensures Name cannot be null.
* Unique Constraint: Ensures Email is unique across the table.
* Check Constraint: Ensures Salary is greater than 0.
Table-Level Constraints
CREATE TABLE Order (
OrderID INT,
CustomerID INT,
ProductID INT,
Quantity INT,
PRIMARY KEY (OrderID), -- Primary Key Constraint at table level
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID), -- Foreign Key
Constraint at table level
FOREIGN KEY (ProductID) REFERENCES Product(ProductID), -- Foreign Key
Constraint at table level
CHECK (Quantity > 0) -- Check Constraint at table level
);
227024 SIGNATURE