0% found this document useful (0 votes)
17 views

Dbms

Uploaded by

agam12993
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)
17 views

Dbms

Uploaded by

agam12993
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/ 26

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

1.Entity Integrity Constraint:


•Definition: Ensures that each table has a primary key, and that the primary key column cannot have NULL val
ues.
•Example: StudentID as a primary key in a Students table.
CREATE TABLE Students (
StudentID INT PRIMARY KEY, Name VARCHAR(100), Age INT );
2. Referential Integrity Constraint:
•Definition: Ensures that a foreign key value must match a primary key value in another table, or be NULL.
•Example: CourseID in Enrollments table referencing CourseID in Courses table.
CREATE TABLE Enrollments (
EnrollmentID INT PRIMARY KEY, StudentID INT, CourseID INT, FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);
3. Unique Constraint:
•Definition: Ensures that all values in a column or a set of columns are unique.
•Example: Email column in Users table must have unique values.
CREATE TABLE Users ( UserID INT PRIMARY KEY, Email VARCHAR(100) UNIQUE );
4. Domain Constraints
•Definition: Ensures that all values in a column must be from a predefined domain.
•Example: Age column can only have integer values between 1 and 100.

JensDBMS
Martensson 10
Relational Model Constraints

CREATE TABLE Students (


StudentID INT, Name VARCHAR(100), Age INT CHECK (Age BETWEEN 1 AND 100));
Not Null Constraint:
• Definition: Ensures that a column cannot have NULL values.
• Example: Name column in Students table must always have a value.
CREATE TABLE Students (
StudentID INT PRIMARY KEY, Name VARCHAR(100) NOT NULL, Age INT );

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)

Data Manipulation Language (DML)


DML commands deal with the manipulation of data within the database. Here are some common DML
commands:
•SELECT: Retrieves data from one or more tables.
sql
SELECT * FROM Students;
•INSERT: Adds new records to a table.
sql
INSERT INTO Students (StudentID, Name, Age) VALUES (1, 'Alice', 20);
•UPDATE: Modifies existing records in a table.
sql
UPDATE Students SET Age = 21 WHERE StudentID = 1;
•DELETE: Removes records from a table.
sql
DELETE FROM Students WHERE StudentID = 1;

•DDL: Defines and manages the structure of database objects.


•DML: Manipulates and manages the data within the database.

Jens DBMS
Martensson 13
SQL(Structured Query Language)

Data Manipulation Language (DML)


DML commands deal with the manipulation of data within the database. Here are some common DML
commands:
•SELECT: Retrieves data from one or more tables.
sql
SELECT * FROM Students;
•INSERT: Adds new records to a table.
sql
INSERT INTO Students (StudentID, Name, Age) VALUES (1, 'Alice', 20);
•UPDATE: Modifies existing records in a table.
sql
UPDATE Students SET Age = 21 WHERE StudentID = 1;
•DELETE: Removes records from a table.
sql
DELETE FROM Students WHERE StudentID = 1;

•DDL: Defines and manages the structure of database objects.


•DML: Manipulates and manages the data within the database.

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 );

FLOAT: Floating-point numbers.


CREATE TABLE ExampleTable ( ColumnName FLOAT );

DECIMAL: Stores fixed


point numbers with a specific precision and scale. Precision is the total number of digits, and scale is the number
of digits to the right of the decimal point.
CREATE TABLE ExampleTable ( ColumnName DECIMAL(10, 2) );

2. Character Data Types


CHAR: Stores fixed-length character strings. The length is specified in parentheses.
CREATE TABLE ExampleTable ( ColumnName CHAR(10) );

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)

mysql> USE SCHOOLDATABASE101;


Database changed
CREATE TABLE: Creates a new table.
CREATE TABLE Students ( StudentID INT PRIMARY KEY, Name VARCHAR(100), Age INT
,GENDER CHAR(1) NOT NULL CITY VARCHAR(20)
);

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

mysql> CREATE TABLE SCHOOL1 (


-> ID INT PRIMARY KEY,
-> NAME VARCHAR(10),
-> GENDER VARCHAR(4),
-> CITY VARCHAR(10),
-> SALARY INT NOT NULL DEFAULT 10000);
Query OK, 0 rows affected (0.05 sec)

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;

Field Type Null Key Default Extra


Student_ID int(4) NO PRI NULL
varchar(3
name YES NULL
0)
city char(20) YES NULL
mobileno INT(10) YES NULL
ADM_NO int(6) YES NULL

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;

DROP TABLE STATEMENT


The DROP TABLE statement in SQL is used to delete a table and all its data from a database. This action is
permanent and cannot be undone, so it should be used with caution.
DROP TABLE table_name;

Jens DBMS
Martensson 24
;

CREATE TABLE Students ( StudentID INT PRIMARY KEY, Name VARCHAR(30), Age INT, Class
VARCHAR(10));
DROP TABLE Students;

ALTER TABLE STATEMENT


The ALTER TABLE statement in SQL is used to modify the structure of an existing table. This can include adding,
deleting, or modifying columns, as well as adding or dropping constraints.
Syntax:
ALTER TABLE table_name ADD column_name datatype;
or
ALTER TABLE table_name DROP COLUMN column_name;
or
ALTER TABLE table_name MODIFY COLUMN column_name datatype;
CREATE TABLE Students ( StudentID INT PRIMARY KEY, Name VARCHAR(30), Age INT));
To add a new column Class:
ALTER TABLE Students ADD Class VARCHAR(10);
Example 2: Dropping a Column
To remove the Age column from the Students table:
ALTER TABLE Students DROP COLUMN Age;

Jens DBMS
Martensson 25
;

Example 3: Modifying a Column


To change the data type of the Name column to VARCHAR(50):
ALTER TABLE Students MODIFY COLUMN Name VARCHAR(50);
Example 4: Rename attributes
ALTER TABLE table_name CHANGE Old_Name New_Name;

Jens DBMS
Martensson 26

You might also like