Dbms
Dbms
A database-management system (DBMS) is a collection of interrelated data and a set of programs to access
those data.
The collection of data referred to as the database which contains information relevant to an enterprise.
The primary goal of a DBMS is to provide a way to store and retrieve database information that is both
convenient and efficient.
Database Management System (DBMS): A software package/ system to facilitate the creation and maintenance
of a computerized database.
It defines (data types, structures, constraints), construct (storing data on some storage medium controlled by
DBMS) and manipulate (querying, update, report generation) databases for various applications.
Jens Martensson
DBMS 2
DBMS
A Database Management System (DBMS) is a software package designed to store and manage databases:
Manages very large amounts of data.
2. Supports efficient access to very large amounts of data.
3. Supports concurrent access to very large amounts of data.
Example: bank and its ATM machines.
4. Supports secure, atomic access to very large amounts
of data.
DBMS
Jens Martensson 3
Characteristics of DBMS
Data Independence: DBMS separates data from the application programs, making it easier to modify data structu
res without changing the application logic.
Efficient Data Access: Provides mechanisms for efficient data retrieval, including indexing, query optimization,
and transaction management.
Data Integrity and Security: Ensures data accuracy and consistency through integrity constraints and provides
security mechanisms to prevent unauthorized access.
Data Consistency: Maintains data consistency through ACID (Atomicity, Consistency, Isolation, Durability) propert
ies in transactions.
Data Abstraction: Offers different levels of data abstraction (physical, logical, and view levels) to simplify data
management and user interaction.
Multi-User Support: Allows multiple users to access and manipulate data concurrently without conflicts.
Backup and Recovery: Provides backup and recovery mechanisms to prevent data loss due to system failures or
disasters.
Database Languages: Supports various languages for data definition, manipulation, and control (e.g., SQL).
Data Redundancy Control: Minimizes data redundancy by storing data in a normalized form, ensuring efficient
use of storage space.
.
DBMS
Jens Martensson 4
Advantages and Disadvantages of DBMS
Advantages of DBMS:
Data Consistency and Integrity: Ensures that data remains consistent, accurate, and reliable through
constraints and transaction management.
Data Security: Implements various security measures to prevent unauthorized access and data breaches.
Data Sharing: Allows multiple users to access and work on data simultaneously, promoting collaboration and
efficiency.
Data Independence: Separates data structure from application logic, making it easier to modify data without
affecting applications.
Backup and Recovery: Provides mechanisms to back up data regularly and restore it in case of a failure.
Reduced Data Redundancy: Minimizes redundancy by storing data in a normalized form, saving storage space
and enhancing efficiency.
Enhanced Data Access: Offers powerful query languages like SQL, enabling complex data retrieval and
manipulation.
DBMS
Jens Martensson 5
Advantages and Disadvantages of DBMS
Disadvantages of DBMS
Complexity: Setting up and maintaining a DBMS requires specialized knowledge and skills.
Cost: The initial setup, maintenance, and licensing fees for commercial DBMS can be expensive.
Performance: May slow down under heavy loads or complex queries, requiring optimization.
Size: DBMS software can be large, requiring significant storage space and system resources.
Training: Users and administrators need proper training to effectively use and manage the DBMS.
Vendor Dependence: Switching DBMS vendors or systems can be challenging and costly due to compatibility
issues.
DBMS
Jens Martensson 6
Relational Data Model of DBMS
The Relational Data Model is a foundational concept in DBMS (Database Management System) that organizes
data into structured formats called tables (or relations). Each table consists of rows (tuples) and columns (attribut
es).
1. Entity:
An entity is a real
world object or concept that can have data stored about it. Each entity has attributes, which are
details that describe it.
EX. Student: An entity representing a student in a school database.
Attributes: Student ID, Name, Age, Class, Address
2. Relations: Collections of related data entries, essentially representing an entity (e.g., Students, Courses).
3. Columns (Attributes): Columns represent the attributes or properties of the entity (e.g., student name,
student ID, course name course ID).
4. Rows (Tuples): Each row in a table represents a single record or instance of the entity.
5. Constraints:
Constraints in a DBMS ensure that the data stored in a database adheres to certain rules and conditions.
They help maintain data integrity, consistency, and accuracy.
DBMS
Jens Martensson 7
Keys in DBMS
Keys are fundamental to the design and structure of a relational database in DBMS, ensuring that data is
properly indexed and relationships are maintained. Here’s a breakdown of the main types of keys:
Primary Key
•Definition: Uniquely identifies each row in a table.
•Characteristics: Cannot contain NULL values; must be unique.
•Example: StudentID in a Students table.
Foreign Key
•Definition: Establishes a relationship between two tables by referencing the primary key of another table.
•Characteristics: Can contain NULL values; enforces referential integrity.
Example: CourseID in an Enrollments table referencing CourseID in the Courses table.
Candidate Key
•Definition: A column, or a set of columns, that can uniquely identify any database record without referring to any
other data.
•Characteristics: Can be multiple in a table; one is chosen as the primary key.
•Example: StudentID and Email in a Students table could both be candidate keys.
DBMS
Jens Martensson 8
Keys in DBMS
Composite Key
•Definition: A primary key composed of two or more columns.
•Characteristics: Used when a single column is not enough to uniquely identify a row.
•Example: OrderID and ProductID together in an OrderDetails table.
•Alternate Key
•Definition: Candidate keys that are not chosen as the primary key.
•Characteristics: Unique but not used as the main identifier for the table.
•Example: If StudentID is the primary key, Email can be an alternate key
JensDBMS
Martensson 9
Relational Model Constraints
JensDBMS
Martensson 10
Relational Model Constraints
Jens DBMS
Martensson 11
SQL(Structured Query Language)
SQL (Structured Query Language) is a standardized programming language used to manage and manipulate rel
ational databases. It allows users to perform various operations such as querying, updating, and managing data
within the database. SQL is essential for interacting with databases to retrieve and store information efficiently.
Data Definition Language (DDL)
DDL commands deal with the structure of the database, defining and managing schema objects such as tables
and indexes.
CREATE: Creates a new table, database, index, or view.
sql
CREATE TABLE Students ( StudentID INT PRIMARY KEY, Name VARCHAR(100), Age INT );
•ALTER: Modifies an existing database object, such as a table.
sql
ALTER TABLE Students ADD Email VARCHAR(100);
•DROP: Deletes a table, database, or other database object.
sql
DROP TABLE Students;
•TRUNCATE: Removes all records from a table, but keeps the structure for future use.
sql
TRUNCATE TABLE Students;
Jens DBMS
Martensson 12
SQL(Structured Query Language)
Jens DBMS
Martensson 13
SQL(Structured Query Language)
Jens DBMS
Martensson 14
Data Type in SQL
SQL data types specify the kind of values that can be stored in a column. Here are some commonly used SQL d
ata types:
1. Numeric Data Types
INT: Integer values.
CREATE TABLE ExampleTable ( ColumnName INT );
Jens DBMS
Martensson 15
Data Type in SQL
VARCHAR: Stores variable-length character strings. The maximum length is specified in parentheses.
CREATE TABLE ExampleTable ( ColumnName VARCHAR(100) );
3. Date and Time Data Types
DATE: Stores date values in the format YYYY-MM-DD.
CREATE TABLE ExampleTable ( ColumnName DATE );
TIME: Stores time values in the format HH:MM:SS.
CREATE TABLE ExampleTable ( ColumnName TIME );
DATETIME: Stores both date and time values in the format YYYY-MM-DD HH:MM:SS.
CREATE TABLE ExampleTable ( ColumnName DATETIME );
4. Boolean Data Type
BOOLEAN: Stores TRUE or FALSE values. Not all SQL databases support a BOOLEAN type directly, so they
use integers (0 for FALSE, 1 for TRUE).
CREATE TABLE ExampleTable ( ColumnName BOOLEAN );
Jens DBMS
Martensson 16
Data Type in SQL
5. Binary Data Types
BINARY: Stores fixed-length binary data. The length is specified in parentheses.
CREATE TABLE ExampleTable ( ColumnName BINARY(10) );
VARBINARY: Stores variable-length binary data. The maximum length is specified in parentheses.
CREATE TABLE ExampleTable ( ColumnName VARBINARY(100) );
Jens DBMS
Martensson 17
SQL Statements
SQL statements are commands used to perform various operations on the data and structure of a database.
A database statement is a command in SQL used to perform operations on a database. These statements
can be categorized into different types based on their functionalities:
Creates a new database
CREATE DATABAES DATABASE_NAME;
CREATE DATABASE SCHOOLDATABASE;
SHOW DATABAES;
USE DATABASE_NAME;
mysql> CREATE DATABASE SCHOOLDATABASE101;
Query OK, 1 row affected (0.01 sec)
Jens DBMS
Martensson 18
DEFAULT constraint
The DEFAULT constraint in SQL is used to set a default value for a column when no value is specified dur
ing an INSERT operation.
CREATE TABLE Students ( StudentID INT PRIMARY KEY, Name VARCHAR(100), Age INT DEFAULT
18 );
The Age column has a DEFAULT value of 18. If a new student is added without specifying an age, the database
will automatically use 18.
Jens DBMS
Martensson 19
DEFAULT constraint
Jens DBMS
Martensson 20
Check constraint
The CHECK constraint in SQL is used to limit the value range that can be placed in a column. If you define a
CHECK constraint on a single column, it allows only certain values for this column.
If you define it on a table, it can limit the values in certain columns based on other columns.
mysql> CREATE TABLE Students (
-> StudentID INT PRIMARY KEY,
-> Name VARCHAR(100),
-> Age INT CHECK (Age >= 18 AND Age <= 25 );
Query OK, 0 rows affected (0.03 sec)
mysql> desc students;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| StudentID | int | NO | PRI | NULL | |
| Name | varchar(100) | YES | | NULL | |
| Age | int | YES | | NULL | |
•+-----------+--------------+------+-----+---------+-------+
The CHECK constraint ensures that the value of Age must be between 18 and 25.
3 rows in set (0.01 sec)
Jens DBMS
Martensson 21
;
DESCRIBE TABLE
In SQL, the DESCRIBE statement (or DESC for short) is used to show the structure of a table. It provides details
about the columns, including their names, data types, and constraints .
DESCRIBE table_name;
Or
DESC table_name;
CREATE TABLE students ( student_ID INT(4) PRIMARY KEY, name VARCHAR(30), city CHAR(20), mobileno
INT(10), ADM_NO INT(6) );
DESCRIBE students;
Jens DBMS
Martensson 22
C
I;
SHOW TABLE
The SHOW TABLES statement is used to list all the tables in a given database, not within a specific table. So, if
you want to list all the tables in a database that includes a Student table.
Tables_in_school
USE school; SHOW TABLES; Student
The result would be a list of all tables in the school database
Courses
INSERT STATEMENT Enrollments
Teachers
The INSERT statement in SQL is used to add new records to a table.
The basic syntax and some examples to demonstrate how it works .
Syntax:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
Example 1: Inserting a Record into the Students Table
CREATE TABLE Students( StudentID INT PRIMARY KEY, Name VARCHAR(30), Age INT, Class VARCHAR(10));
INSERT INTO Students (StudentID, Name, Age, Class) VALUES (1, 'Alice', 14, '8A');
Example 2: Inserting Multiple Records
INSERT INTO Students (StudentID, Name, Age, Class) VALUES (2, 'Bob', 15, '9B'), (3, 'Charlie', 14, '8A');
Jens DBMS
Martensson 23
;
DELETE TABLE
The DELETE statement in SQL is used to remove records from a table. This can be based on a condition, or it
can remove all rows if no condition is specified.
Syntax:
DELETE FROM table_name WHERE condition;
Ex.
CREATE TABLE Students ( StudentID INT PRIMARY KEY, Name VARCHAR(30), Age INT, Class VARCHAR(10) );
DELETE FROM Students WHERE StudentID = 1;
Deleting Multiple Records
DELETE FROM Students WHERE Class = '8A';
Deleting All Records
DELETE FROM Students;
Deleting Records with a Condition
DELETE FROM Students WHERE Age > 15;
Jens DBMS
Martensson 24
;
CREATE TABLE Students ( StudentID INT PRIMARY KEY, Name VARCHAR(30), Age INT, Class
VARCHAR(10));
DROP TABLE Students;
Jens DBMS
Martensson 25
;
Jens DBMS
Martensson 26