Unit V - DBMS
Unit V - DBMS
Unit V - DBMS
Introduction
What Is a Database?
• Database
– A collection of related data stored in a manner that
enables information to be retrieved as needed
• Database Management System (DBMS)
– Used to create, maintain, and access databases
– Example:Microsoft Access, OpenOffice Base, Corel
Paradox, Oracle Database, etc.
2
What Is a Database?
3
What Is a Database?
4
What is a Database?
– Primary Key
• Field that uniquely identifies the records in a table
• Field in a table that is used to relate that table to other
tables
5
What Is a Database?
– Advantages
• Low level of redundancy
– Faster response time
– Lower storage
requirements
– Easier to secure
– Increased data
accuracy
– Disadvantages
• Increased vulnerability
(backup is essential) 7
Data Hierarchy
– Fields/columns
• Hold single pieces of data
– Records/rows
• Groups of related fields
– Tables
• Collection of related records
– Database
• Contains a group of related tables
8
Database Models
9
The Relational Database Model
(RDBMS)
10
The Relational Database Model
(RDBMS)
11
What is SQL?
12
• SQLite is a relational database management system based on SQL.
• Also, it reads and writes directly to a disk file that can be easily copied.
• It is platform-independent.
REAL Real values are real numbers with decimal values that use 8-byte
floats.
TEXT TEXT is used to store character data. The maximum length of TEXT
is unlimited. SQLite supports various character encodings.
BLOB BLOB stands for a binary large object that can store any kind of
data. The maximum size of BLOB is, theoretically, unlimited.
Connecting to an SQLite
Database
The first step to working with your database is to create a connection with it.
You are now ready to execute queries against the database and manipulate the data.
But after we have done that, it is very important to do two things:
2. Close the connection to the database to prevent the SQLite database from getting
locked. When an SQLite database is locked, it will not be accessible by other users
and will give an error.
# save changes
conn.commit()
print('Changes saved.’)
But if we are adding values for all the columns in the table, we can just
simplify things and get rid of the column names in the SQL statement:
INSERT INTO table_name
VALUES (value1, value2, value3 …);
If you instead wanted to fetch values for all the attributes in the table, use
the * character instead of the column names:
SELECT * FROM table_name;
To fetch multiple rows, you can execute a SELECT statement and iterate
over it directly using only a single call on the Cursor object:
Fetching Records from a SQL table
fetchall()
UPDATE SQL command.
The UPDATE command is used to modify existing records in a table. However,
always make you sure you provide which records need to be updated in the
WHERE clause otherwise all the records will be updated!
UPDATE table_name
SET column1 = value1, column2 = value2 …
WHERE condition;
Delete statement
To delete the records from the database, we use the DELETE statement. However,
make sure to use the WHERE clause otherwise all the records will be deleted from
the table!
DELETE FROM table_name
WHERE condition;
Drop Table statement
Finally, if we had to drop an entire table from the database and not just its records,
we use the DROP TABLE statement. All the records in the table along with the
table structure will be lost after this!
Drop table table_name;