Uploaded by ayanotaka4141

DATABASE MANAGEMENT SYSTEM CLASS 12th PROJECT COMPUTER SCIENCE

advertisement
INTRODUCTION
Database Management Systems (DBMS) are software systems used to store,
retrieve, and run queries on data. A DBMS serves as an interface between an
end-user and a database, allowing users to create, read, update, and delete data
in the database.
DBMS manage the data, the database engine, and the database schema,
allowing for data to be manipulated or extracted by users and other programs.
This helps provide data security, data integrity, concurrency, and uniform data
administration procedures.
DBMS optimizes the organization of data by following a database schema
design technique called normalization, which splits a large table into smaller
tables when any of its attributes have redundancy in values. DBMS offer many
benefits over traditional file systems, including flexibility and a more complex
backup system.
1
TYPES OF DBMS
Database management systems can be classified based on a variety of criteria
such as the data model, the database distribution, or user numbers. The most
widely used types of DBMS software are relational, distributed, hierarchical,
object-oriented, and network.
#Distributed database management system
A distributed DBMS is a set of logically interrelated databases distributed over a
network that is managed by a centralized database application. This type of
DBMS synchronizes data periodically and ensures that any change to data is
universally updated in the database.
#Hierarchical database management system
Hierarchical databases organize model data in a tree-like structure. Data storage
is either a top-down or bottom-up format and is represented using a parentchild relationship.
#Network database management system
The network database model addresses the need for more complex
relationships by allowing each child to have multiple parents. Entities are
organized in a graph that can be accessed through several paths.
#Relational database management system
Relational database management systems (RDBMS) are the most popular data
model because of its user-friendly interface. It is based on normalizing data in
the rows and columns of the tables. This is a viable option when you need a
data storage system that is scalable, flexible, and able to manage lots of
information.
#Object-oriented database management system
Object-oriented models store data in objects instead of rows and columns. It is
based on object-oriented programming (OOP) that allows objects to have
members such as fields, properties, and methods.
2
NEED FOR A DBMS
1. Data modelling: A DBMS provides tools for creating and modifying data
models, which define the structure and relationships of the data in a
database.
2. Data storage and retrieval: A DBMS is responsible for storing and
retrieving data from the database, and can provide various methods for
searching and querying the data.
3. Concurrency control: A DBMS provides mechanisms for controlling
concurrent access to the database, to ensure that multiple users can access
the data without conflicting with each other.
4. Data integrity and security: A DBMS provides tools for enforcing data
integrity and security constraints, such as constraints on the values of data
and access controls that restrict who can access the data.
5. Backup and recovery: A DBMS provides mechanisms for backing up and
recovering the data in the event of a system failure.
6. DBMS can be classified into two types: Relational Database Management
System (RDBMS) and Non-Relational Database Management System
(NoSQL or Non-SQL)
7. RDBMS: Data is organized in the form of tables and each table has a set of
rows and columns. The data is related to each other through primary and
foreign keys.
8. NoSQL: Data is organized in the form of key-value pairs, document, graph,
or column-based. These are designed to handle large-scale, highperformance scenarios.
3
THE RELATIONAL
MODEL TERMINOLOGY
#Relation
In general, a relation is a table i.e., data arranged in rows and columns. A
relation has
the following properties :
1. In any given column of a table, all items are of the same kind whereas items in
different columns may not be of the same kind.
2. For a row, each column must have an atomic (indivisible) value and also for a
row, a column cannot have more than one value.
3. All rows of a relation are distinct. That is, a relation does not contain two rows
which are identical in every column. That is, each row of the relation can be
uniquely identified by its contents.
4. There is no order maintained for rows inside a relation. That is, we cannot
retrieve anything by saying that from row number 5, column name is to be
accessed.
5. The columns of a relation are assigned distinct names and the ordering of
these columns is immaterial.
#Domain
A crucial feature of relational data model is that associations between rows are
represented solely by data values in columns drawn from a common domain. A
domain is a pool of values from which the actual values appearing in a given
column are drawn.
A domain is said to be atomic if elements of the domain are considered to be
indivisible units.
#Attributes
The columns of tables (relations) are generally referred to as attributes.
4
#Tuple
The rows of tables (relations) are generally referred to as Tuple (usually
pronounced to rhyme with “couples”.
#Degree
The number of attributes in a relation determines the degree of a relation. A
relation
having 3 attributes is said to be a relation of degree 3. Similarly, a relation
having n attributes is said to be a relation of degree n. Relations of degree one
are
said to be unary, relations of degree two are binary, relations of degree three
are ternary, and relations of degree n are n-ary
#Cardinality
The number of tuples (rows) in a relation is called the cardinality of the relation.
5
KEYS IN RELATIONAL
DATABASE
1. Candidate Key: The minimal set of attributes that can uniquely identify a
tuple is known as a candidate key. For Example, STUD_NO in STUDENT
relation.
 It is a minimal super key.
 It is a super key with no repeated data is called a candidate key.
 The minimal set of attributes that can uniquely identify a record.
 It must contain unique values.
 It cannot contain NULL values.
 Every table must have at least a single candidate key.
 A table can have multiple candidate keys but only one primary key.
 The value of the Candidate Key is unique and may be null for a tuple.
 There can be more than one candidate key in a relationship.
Example:
STUD_NO is the candidate key for relation STUDENT.
>Table STUDENT
STUD_NO

SNAME
ADDRESS
PHONE
1
Shyam
Delhi
123456789
2
Rakesh
Kolkata
223365796
3
Suraj
Delhi
175468965
The candidate key can be simple (having only one attribute) or composite
as well.
Example:
{STUD_NO, COURSE_NO} is a candidate key for relation STUDENT_COURSE.
6
>Table STUDENT_COURSE
STUD_NO TEACHER_NO COURSE_NO
1
001
C001
2
056
C005
2. Primary Key: There can be more than one candidate key in relation out of
which one can be chosen as the primary key. For Example, STUD_NO, as well as
STUD_PHONE, are candidate keys for relation STUDENT but STUD_NO can be
chosen as the primary key (only one out of many candidate keys).
 It is a unique key.
 It can identify only one tuple (a record) at a time.
 It has no duplicate values, it has unique values.
 It cannot be NULL.
 Primary keys are not necessarily to be a single column; more than one
column can also be a primary key for a table.
Example:
STUDENT table -> Student(STUD_NO, SNAME,
ADDRESS, PHONE) , STUD_NO is a primary key
>Table STUDENT
STUD_NO
SNAME
ADDRESS
PHONE
1
Shyam
Delhi
123456789
2
Rakesh
Kolkata
223365796
3
Suraj
Delhi
175468965
3. Super Key: The set of attributes that can uniquely identify a tuple is known
as Super Key. For Example, STUD_NO, (STUD_NO, STUD_NAME), etc. A super
key is a group of single or multiple keys that identifies rows in a table. It
supports NULL values.
 Adding zero or more attributes to the candidate key generates the super
key.
 A candidate key is a super key but vice versa is not true.
7

Super Key values may also be NULL.
Example:
Consider the table shown above.
STUD_NO+PHONE is a super key.
Relation between Primary Key, Candidate Key, and Super Key
4. Alternate Key: The candidate key other than the primary key is called an
alternate key.
 All the keys which are not primary keys are called alternate keys.
 It is a secondary key.
 It contains two or more fields to identify two or more records.
 These values are repeated.
 Eg:- SNAME, and ADDRESS is Alternate keys
Example:
Consider the table shown above.
STUD_NO, as well as PHONE both,
are candidate keys for relation STUDENT but
PHONE will be an alternate key
(only one out of many candidate keys).
Primary Key, Candidate Key, and Alternate Key
8
5. Foreign Key: If an attribute can only take the values which are present as
values of some other attribute, it will be a foreign key to the attribute to which
it refers. The relation which is being referenced is called referenced relation
and the corresponding attribute is called referenced attribute the relation
which refers to the referenced relation is called referencing relation and the
corresponding attribute is called referencing attribute. The referenced attribute
of the referenced relation should be the primary key to it.
 It is a key it acts as a primary key in one table and it acts as
secondary key in another table.
 It combines two or more relations (tables) at a time.
 They act as a cross-reference between the tables.
 For example, DNO is a primary key in the DEPT table and a non-key in EMP
Example:
Refer Table STUDENT shown above.
STUD_NO in STUDENT_COURSE is a
foreign key to STUD_NO in STUDENT relation.
Table STUDENT_COURSE
STUD_NO TEACHER_NO COURSE_NO
1
005
C001
2
056
C005
Unlike the Primary Key of any given relation, Foreign Key can be NULL as well as may
contain duplicate tuples i.e. it need not follow uniqueness constraint. For Example,
STUD_NO in the STUDENT_COURSE relation is not unique. It has been repeated for the first
and third tuples. However, the STUD_NO in STUDENT relation is a primary key and it needs
to be always unique, and it cannot be null.
Relation between Primary Key and Foreign Key
9
6. Composite Key: Sometimes, a table might not have a single
column/attribute that uniquely identifies all the records of a table. To uniquely
identify rows of a table, a combination of two or more columns/attributes can
be used. It still can give duplicate values in rare cases. So, we need to find the
optimal set of attributes that can uniquely identify rows in a table.
 It acts as a primary key if there is no primary key in a table
 Two or more attributes are used together to make a composite key.
 Different combinations of attributes may give different accuracy in terms of
identifying the rows uniquely.
Example:
FULLNAME + DOB can be combined
together to access the details of a student.
Different Types of Keys
10
SQL
SQL, Structured Query sometimes also Language, was developed in 1970s in an
IBM Laboratory. SQL, referred to as SEQUEL is a 4th generation non-procedural
language. The non-procedural languages just need to be specified "WHAT of
the problem and not a detailed procedure about HOW it is to be done'. Since
SQL is non-procedural and that is what makes it
simple, it makes an RDBMS possible.
The Structured Query Language (SQL) is a language that enables you to create
and operate on relational databases, which are sets of related information
stored in tables. The SOL (Structured Query Language) has proved to be a
standard language as it allows users to learn one set of commands and use it to
create, retrieve, alter, and transfer information regardless of whether they are
working on a PC, a workstation, a mini, or a mainframe.
SQL enables the following:
(i) Creating/modifying a database's structure
(ii) Changing security settings for system
(iii) Permitting users for working on databases or tables
(iv) Querying database
(v) Inserting/Modifying/Deleting the database contents.
11
SQL ELEMENTS
The MySQL implementation of SQL has certain elements that play an important
role in defining/querying a database.
(i) Literals (ii) Data types (iii) Nulls (iv) Comments
1. Literals
Literals, in general, refer to a fixed data value. This fixed data value may be of
character type or numeric literal. all character literals are enclosed in single
quotation marks or double quotation marks. Characters that are not enclosed in
quotation marks refer to the schema object names.
Numbers that are not enclosed in quotation marks are numeric literals. Numeric
literals can either be integer literals i.e., without any decimal or be real literals
i.e., with a decimal point.
2.Data types
MySQL uses many differernt data types, divided into three categories :
1. Numeric 2. Date and time 3.String types
1.Numeric Data Types
Data type
Description
INT(size)
A medium integer. Signed range is from -2147483648 to
2147483647. Unsigned range is from 0 to 4294967295.
The size parameter specifies the maximum display width (which
is 255)
TINYINT(size)
A very small integer. Signed range is from -128 to 127. Unsigned
range is from 0 to 255. The size parameter specifies the
maximum display width (which is 255)
SMALLINT(size)
A small integer. Signed range is from -32768 to 32767. Unsigned
range is from 0 to 65535. The size parameter specifies the
12
maximum display width (which is 255)
MEDIUMINT(size)
A medium integer. Signed range is from -8388608 to 8388607.
Unsigned range is from 0 to 16777215. The size parameter
specifies the maximum display width (which is 255)
BOOLEAN
Zero is considered as false, nonzero values are considered as
true.
BIGINT(size)
A large integer. Signed range is from -9223372036854775808 to
9223372036854775807. Unsigned range is from 0 to
18446744073709551615. The size parameter specifies the
maximum display width (which is 255)
FLOAT(size, d)
A floating point number. The total number of digits is specified
in size. The number of digits after the decimal point is specified
in the d parameter. This syntax is deprecated in MySQL 8.0.17,
and it will be removed in future MySQL versions
DOUBLE(size, d)
A normal-size floating point number. The total number of digits
is specified in size. The number of digits after the decimal point
is specified in the d parameter
DECIMAL(size, d)
An exact fixed-point number. The total number of digits is
specified in size. The number of digits after the decimal point is
specified in the d parameter. The maximum number for size is
65. The maximum number for d is 30. The default value for size is
10. The default value for d is 0.
2. Date and Time Data Types
Data type
Description
DATE
A date. Format: YYYY-MM-DD. The supported range is from
'1000-01-01' to '9999-12-31'
DATETIME(fsp)
A date and time combination. Format: YYYY-MM-DD
hh:mm:ss. The supported range is from '1000-01-01
13
00:00:00' to '9999-12-31 23:59:59'. Adding DEFAULT and ON
UPDATE in the column definition to get automatic
initialization and updating to the current date and time
TIMESTAMP(fsp)
A timestamp. TIMESTAMP values are stored as the number
of seconds since the Unix epoch ('1970-01-01 00:00:00'
UTC). Format: YYYY-MM-DD hh:mm:ss. The supported range
is from '1970-01-01 00:00:01' UTC to '2038-01-09 03:14:07'
UTC. Automatic initialization and updating to the current
date and time can be specified using DEFAULT
CURRENT_TIMESTAMP and ON UPDATE
CURRENT_TIMESTAMP in the column definition
TIME(fsp)
A time. Format: hh:mm:ss. The supported range is from '838:59:59' to '838:59:59'
YEAR
A year in four-digit format. Values allowed in four-digit
format: 1901 to 2155, and 0000.
MySQL 8.0 does not support year in two-digit format.
3. String Data Types
Data type
Description
CHAR(size)
A FIXED length string (can contain letters, numbers, and
special characters). The size parameter specifies the column
length in characters - can be from 0 to 255. Default is 1
VARCHAR(size)
A VARIABLE length string (can contain letters, numbers,
and special characters). The size parameter specifies the
maximum column length in characters - can be from 0 to
65535
TINYBLOB or
For BLOBs (Binary Large OBjects). Max length: 255 bytes
TINYTEXT
Holds a string with a maximum length of 255 characters
14
TEXT(size) or
Holds a string with a maximum length of 65,535 bytes
BLOB(size)
For BLOBs (Binary Large OBjects). Holds up to 65,535 bytes
of data
MEDIUMTEXT or
Holds a string with a maximum length of 16,777,215
characters
MEDIUMBLOB
For BLOBs (Binary Large OBjects). Holds up to 16,777,215
bytes of data
LONGTEXT or
LONGBLOB
Holds a string with a maximum length of 4,294,967,295
characters
For BLOBs (Binary Large OBjects). Holds up to
4,294,967,295 bytes of data
ENUM(val1, val2, val3, ...)
A string object that can have only one value, chosen from a
list of possible values. You can list up to 65535 values in an
ENUM list. If a value is inserted that is not in the list, a
blank value will be inserted. The values are sorted in the
order you enter them
3. Null Values
1f a column in a row has no value, then column is said to be null. or to contain a
null. Nulls can appear in columns of any data type provided they are not
restricted by NOT NULL or PRIMARY KEY integrity constraints.
4.Comments
A comment is a text that is not executed ; it is only for documentation purposes.
A comment generally describes the statement's purpose within an application.
A comment can appear between any keywords, parameters or punctuation
marks in a statement
15
SQL STATEMENTS
SQL, technically speaking, is a data sublanguage. That is, it is a language used to
interact with database. In other words, all SQL statements are instructions to the
database only. And that is where it differs from general-purpose programming
languages like 'C" or 'C++' or Python' etc.
SQL provides many different types of commands used for different purposes.
SQL commands can be divided into following categories :
(i) Data Definition Language (DDL) Commands
(ii) Data Manipulation Language (DML) Commands
(iii) Transaction Control Language (TCL) Commands
1. DDL Commands
The Data Definition Language (DDL) commands, as the name suggests, allow
you to perform tasks related to data definition. That is, through these
commands, we can perform tasks like
(i) Create, alter, and drop schema objects
(ii) Grant and revoke privileges and roles.
(iii) Maintenance commands.
2. DML Commands
The Data Manipulation Language (DML) commands, as the name suggests, are
used to manipulate data. That is, DML commands query and manipulate data in
existing schema objects. For example, to insert a tuple in a table, a DML
statement (INSERT INTO) is used ; to modify a tuple in a table, another DML
statement (UPDATE) is used: to delete a tuple in a table, the DML statement
DELETE is used. Other examples of DML commands are: SELECT, LOCK TABLE
etc.
16
3. TCL Commands
A transaction is one complete unit of work e.g., preparing report-card for a
student is a transaction, but it involves many steps like -open student's table,
read data from it, open marks' table, read data from it, calculate percentage and
grade, write the computed result to result file and finally close these three tables
(student table, marks table and result table).
A transaction is successfully completed (known as COMMIT) if and only if all its
constituent steps are successfully completed. To manage and control the
transactions, the transaction control Commands are used. These commands
manage changes made by DML commands.
17
MYSQL
MySQL is a freely available open source Relational Database Management
System (RDBMS) that uses Structured Query Language (SQL). It is downloadable from site www.mysql.org In a MySQL database, information is stored in
Tables. A single MySQL database can Contain many tables at once and store
thousands of individual records. MySQL provides you with a rich set of features
that support a secure environment for storing, maintaining, and accessing data.
MySQL is a fast, reliable, scalable alternative to many of the commercial RDBMS
available today.
MySQL was created and is supported by MySQL AB, a company based in
Sweden (www.mysql.com). This company is now a subsidiary of Sun
Microsystems, which holds the copyright to most of the codebase. The chief
inventor of MySQL was Michael Widenius. MySQL has been named after his
daughter My. The logo of MySQL, the dolphin, is named as " Sakila".
#Features of MySQL:
1. Speed: If the server hardware is optimal, MySQL runs very fast. It supports
clustered servers for demanding applications.
2. Ease of use: MySQL is a high-performance, relatively simple database system.
From the beginning, MySQL has typically been configured, monitored, and
managed from the Command line. However, several MySQL graphical interfaces
are also available.
3. Cost: MysQL is available free of cost. MySQL is a "Open Source" database.
MySQL is part of LAMP (Linux, Apache, MySQL, PHP / Perl / Python)
environment, a fast growing open source enterprise software stack. More and
more companies are using LAMP as an alternative to expensive proprietary
software stacks because of its lower Cost, reliability, and documentation.
4. Query Language Support: MySQL understands standards based SQL
(Structured Query Language).
18
5. Portability: MySQL provides portablility as it has been tested with a broad
range of different compilers and can work on many different platforns. It is fully
multi-threaded using kernel threads. It can easily use mnultiple CPUs if they are
available.
6.Data Types: MySOL provides many data types to support different types of
data. It also suPports fixed-length and variable-length records.
7. Security: MySQL offers a privilege and password system that is very flexible
and secure, and that allows host-based verification. Passwords are secure
because all
password traffic is encrypted when you connect to a server.
8. Scalability and Limits: MySQL can handle large databases. Some real life
MySQL databases
contain 50 million records, some have up to 60,000 tables and about
5,000,000,000 rows.
9. Connectivity: Clients can connect to MySQL Server using several protocols:
10. Localization: The server can provide error messages to clients in many
languages.
11. Clients and Tools: MySQL provides several client and utility programs.
These include both command-line programs such as mysqldump and
mysqladmin, and graphical programs such as MySQL Administrator and MySQL
Query Browser. MySQL Server has built-in support for SQL statements to check,
optimize, and repair tables.
19
SAMPLE DATABASE
HOTEL MANAGEMENT
SYSTEM USING MYSQL
>>Creating Tables
#Customers
mysql>CREATE TABLE customer
(cno
FIXED(4)
PRIMARY KEY,
title
CHAR(7),
firstname
CHAR(20),
name
CHAR(40)
zip
CHAR(5),
address
CHAR(40)
NOT NULL,
NOT NULL);
# Rooms
mysql> CREATE TABLE room
(rno
FIXED(4),
type
CHAR(6),
price
FIXED(6,2));
PRIMARY KEY (rno,type),
#Reservations
mysql>CREATE TABLE reservation
(rno
FIXED(4)
cno
FIXED(4),
hno
FIXED(4),
type
CHAR(6),
arrival
DATE
departure
DATE
PRIMARY KEY,
NOT NULL,
NOT NULL);
20
>>Filling Tables
#Customers
mysql>INSERT INTO customer VALUES (3000,'Mrs','Jenny','Porter','10580','1340 N. Ash Street,
#3');
mysql>INSERT INTO customer VALUES (3100,'Mr','Peter','Brown','48226','1001 34th
St., APT.3');
mysql>INSERT INTO customer VALUES (3200,'Company',NULL,'Datasoft','90018','486
Maple St.');
mysql>INSERT INTO customer VALUES (3300,'Mrs','Rose','Brian','75243','500
Yellowstone Drive, #2');
mysql>INSERT INTO customer VALUES (3400,'Mrs','Mary','Griffith','20005','3401 Elder
Lane');
mysql>INSERT INTO customer VALUES (3500,'Mr','Martin','Randolph','60615','340 MAIN
STREET, #7');
mysql>INSERT INTO customer VALUES (3600,'Mrs','Sally','Smith','75243','250 Curtis
Street');
mysql>INSERT INTO customer VALUES (3700,'Mr','Mike','Jackson','45211','133 BROADWAY
APT. 1');
mysql>INSERT INTO customer VALUES (3800,'Mrs','Rita','Doe','97213','2000 Humboldt
St., #6');
mysql>INSERT INTO customer VALUES (3900,'Mr','George','Howe','75243','111 B
Parkway, #23');
mysql>INSERT INTO customer VALUES (4000,'Mr','Frank','Miller','95054','27 5th St.,
76');
mysql>INSERT INTO customer VALUES (4100,'Mrs','Susan','Baker','90018','200 MAIN
STREET, #94');
mysql>INSERT INTO customer VALUES (4200,'Mr','Joseph','Peters','92714','700 S. Ash
St., APT.12');
mysql>INSERT INTO customer VALUES (4300,'Company',NULL,'TOOLware','20019','410
Mariposa St., #10');
mysql>INSERT INTO customer VALUES (4400,'Mr','Antony','Jenkins','20903','55 A
Parkway, #15')
21
# Rooms
mysql>INSERT INTO room VALUES (1,'single',3500);
mysql>INSERT INTO room VALUES (2,'double',4000);
mysql>INSERT INTO room VALUES (3,'single', 3500);
mysql>INSERT INTO room VALUES (4,'double', 4000);
mysql>INSERT INTO room VALUES (5,'single', 3500);
mysql>INSERT INTO room VALUES (6,'double', 4000);
mysql>INSERT INTO room VALUES (7,'single', 3500);
mysql>INSERT INTO room VALUES (8,'double', 4000);
mysql>INSERT INTO room VALUES (9,'single', 3500);
mysql>INSERT INTO room VALUES (10,'double', 4000);
mysql>INSERT INTO room VALUES (11,'suite', 3500);
mysql>INSERT INTO room VALUES (12,'single', 3500);
mysql>INSERT INTO room VALUES (13,'double',4000);
mysql>INSERT INTO room VALUES (14,'single',3500);
mysql>INSERT INTO room VALUES (15,'double',4000);
mysql>INSERT INTO room VALUES (16,'suite', 4500);
mysql>INSERT INTO room VALUES (17,'single', 3500);
mysql>INSERT INTO room VALUES (18,'double’, 4000);
mysql>INSERT INTO room VALUES (19,'suite', 4500);
mysql>INSERT INTO room VALUES (20,'single', 3500);
#Reservations
mysql>INSERT INTO reservation VALUES (3,3000.80,'single','2023-1-13','2023-1-17');
mysql>INSERT INTO reservation VALUES (16,3200.50,'suite','2023-2-13','2023-2-15');
mysql>INSERT INTO reservation VALUES (20,3900.110,'single','2023-02-21','2023-0225');
mysql>INSERT INTO reservation VALUES (18,3600.70,'double','2023-03-1','2023-03-4');
mysql>INSERT INTO reservation VALUES (15,3100.120,'double','2023-3-23','2023-328');
mysql>INSERT INTO reservation VALUES (20,4300.140,'double','2023-4-1','2023-4-5');
22
>>Output
19
23
BIBILIOGRAPHY
>>Websites
https://mycbseguide.com
https://byjus.com
https://www.appdynamics.com
https://www.geeksforgeeks.org
https://www.google.com
>>Books
Computer Science NCERT Class XII
Computer Science with Python Class 12 by Sumita Arora
24
Download