0% found this document useful (0 votes)
12 views16 pages

Database Concepts

Uploaded by

jhony
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)
12 views16 pages

Database Concepts

Uploaded by

jhony
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/ 16

Example : Program to illustrate private and public variables

class Sample:
n1 = 12
__n2 = 14
def display(self):
print("Class variable 1 = ", self.n1)
print("Class variable 2 = ", self.__n2)
S=Sample()
S.display()
print("Value 1 = ", S.n1)
print("Value 2 = ", S.__n2)

In the above program, there are two class variables n1 and n2 are declared. The variable
n1 is a public variable and n2 is a private variable. The display( ) member method is defined to
show the values passed to these two variables.

The print statements defined within class will successfully display the values of n1 and
n2, even though the class variable n2 is private. Because, in this case, n2 is called by a method
defined inside the class. But, when we try to access the value of n2 from outside the class
Python throws an error. Because, private variable cannot be accessed from outside the class.

Output
Class variable 1 = 12
Class variable 2 = 14
Value 1 = 12

Traceback (most recent call last):


File "D:/Python/Class-Test-04.py", line 12, in <module>
print("Value 2 = ", S.__n2)
AttributeError: 'Sample' object has no attribute '__n2'

XII Std Computer Science 176

12th Computer Science_EM Chapter 10.indd 176 26-12-2022 16:45:45


10.8 Sample Programs to illustrate classes and objects

Program 1: Write a program to calculate area and circumference of a circle


class Circle:
pi=3.14
def __init__(self,radius):
self.radius=radius
def area(self):
return Circle.pi*(self.radius**2)
def circumference(self):
return 2*Circle.pi*self.radius
r=int(input("Enter Radius: "))
C=Circle(r)
print("The Area =",C.area())
print("The Circumference =", C.circumference())

Output:
Enter Radius: 5
The Area = 78.5
The Circumference = 31.400000000000002

Program 2: Write a program to accept a string and print the number of


uppercase, lowercase, vowels, consonants and spaces in the given string
class String:
def __init__(self):
self.upper=0
self.lower=0
self.vowel=0
self.consonant=0
self.space=0
self.string=""
def getstr(self):
self.string=str(input("Enter a String: "))
def count (self):
for ch in self.string:
if (ch.isupper()):
self.upper+=1
if (ch.islower()):
self.lower+=1
if (ch in ('AEIOUaeiou'):
self.vowel+=1

177 Python Classes and Objects

12th Computer Science_EM Chapter 10.indd 177 26-12-2022 16:45:45


if (ch.isspace()):
self.space+=1
self.consonant = self.upper+self.lower - self.
vowel
def display(self):
print("The given string contains...")
print("%d Uppercase letters"%self.upper)
print("%d Lowercase letters"%self.lower)
print("%d Vowels"%self.vowel)
print("%d Consonants"%self.consonant)
print("%d Spaces"%self.space)
S = String()
S.getstr()
S.count()
S.display()
Output:
Enter a String:Welcome To Learn Computer Science
The given string contains...
4 Uppercase letters
25 Lowercase letters
12 Vowels
13 Consonants
4 Spaces

Points to remember

• Python is an Object Oriented Programming language.


• Classes and Objects are the key features of Object Oriented Programming.
• In Python, a class is defined by using the keyword class.
• Variables defined inside a class is called as “Class Variable” and function are
called as “Methods”.
• The process of creating object is called as “Class Instantiation”.
• Constructor is the special function that is automatically executed when an
object of a class is created.
• In Python, there is a special function called “init” is used as Constructor.
• Destructor is also a special method gets execution automatically when an
object exits from the scope.
• In Python, __del__( ) method is used as destructor.
• A variable prefixed with double underscore is becomes private in nature.

XII Std Computer Science 178

12th Computer Science_EM Chapter 10.indd 178 26-12-2022 16:45:45


Hands on Experience

1. Write a program using class to store name and marks of students in list and print total
marks.

2. Write a program using class to accept three sides of a triangle and print its area.

3. Write a menu driven program to read, display, add and subtract two distances.

Evaluation

Part - I

Choose the best answer (1 Mark)


1. Which of the following are the key features of an Object Oriented Programming language?
(a) Constructor and Classes (b) Constructor and Object
(c) Classes and Objects (d) Constructor and Destructor
2. Functions defined inside a class:
(a) Functions (b) Module
(c) Methods (d) section
3. Class members are accessed through which operator?
(a) & (b) .
(c) # (d) %
4. Which of the following method is automatically executed when an object is created?
(a) __object__( ) (b) __del__( )
(c) __func__( ) (d) __init__( )
5. A private class variable is prefixed with
(a) __ (b) &&
(c) ## (d) **
6. Which of the following method is used as destructor?
(a) __init__( ) (b) __dest__( )
(c) __rem__( ) (d) __del__( )

179 Python Classes and Objects

12th Computer Science_EM Chapter 10.indd 179 26-12-2022 16:45:45


7. Which of the following class declaration is correct?
(a) class class_name (b) class class_name<>
(c) class class_name: (d) class class_name[ ]
8. Which of the following is the output of the following program?
class Student:
def __init__(self, name):
self.name=name
print (self.name)
S=Student(“Tamil”)
(a) Error (b) Tamil
(c) name (d) self
9. Which of the following is the private class variable?
(a) __num (b) ##num
(c) $$num (d) &&num
10. The process of creating an object is called as:
(a) Constructor (b) Destructor
(c) Initialize (d) Instantiation

Part -II

Answer the following questions (2 Marks)

1. What is class?
2. What is instantiation?
3. What is the output of the following program?
class Sample:
__num=10
def disp(self):
print(self.__num)
S=Sample()
S.disp()
print(S.__num)
4. How will you create constructor in Python?
5. What is the purpose of Destructor?

XII Std Computer Science 180

12th Computer Science_EM Chapter 10.indd 180 26-12-2022 16:45:45


Part -III

Answer the following questions (3 Marks)


1. What are class members? How do you define it?
2. Write a class with two private class variables and print the sum using a method.
3. Find the error in the following program to get the given output?
class Fruits:
def __init__(self, f1, f2):
self.f1=f1
self.f2=f2
def display(self):
print("Fruit 1 = %s, Fruit 2 = %s" %(self.f1, self.f2))
F = Fruits ('Apple', 'Mango')
del F.display
F.display()
Output
Fruit 1 = Apple, Fruit 2 = Mango
4. What is the output of the following program?
class Greeting:
def __init__(self, name):
self.__name = name
def display(self):
print("Good Morning ", self.__name)
obj=Greeting('Bindu Madhavan')
obj.display()
5. How to define constructor and destructor in Python?
Part -IV

Answer the following questions (5 Marks)


1. Explain about constructor and destructor with suitable example.

References
1. https://docs.python.org/3/tutorial/index.html
2. https://www.techbeamers.com/python-tutorial-step-by-step/#tutorial-list
3. Python programming using problem solving approach – Reema Thareja – Oxford University
press.
4. Python Crash Course – Eric Matthes – No starch press, San Francisco.

181 Python Classes and Objects

12th Computer Science_EM Chapter 10.indd 181 26-12-2022 16:45:45


CHAPTER 11
Unit IV
DATABASE CONCEPTS

11.2 Information
Learning Objectives
Information is processed data, which
At the completion of this chapter, the student allows to be utilized in a significant way.
will be able to know Example
• the concept of a database and relational SCERT
database. College Road
• different components of the database. DPI Campus
Chennai 600006
• types of database models.
As you can see
• types of relationship.
from the example above,
• the concepts of relational algebra. data appears as a set of
Introduction words and numbers. However, when the
data is processed, organized and formatted,
A database is an organized collection it gives a meaningful information about the
of data, generally stored and accessed SCERT institution contact address.
electronically from a computer system. The
11.3 Database
term "database" is also used to refer to any
Database is a repository collection of
of the DBMS, the database system or an
related data organized in a way that data can
application associated with the database.
be easily accessed, managed and updated.
Because of the close relationship between
Database can be a software or hardware
them, the term "database" is often used
based, with one sole purpose of storing data.
casually to refer to both a database and
the DBMS used to manipulate it. A school 11.4 DataBase Management
class register is a database where names are System (DBMS)
arranged alphabetically. Databases have A DBMS is a software that allows us
been around since people started recording to create, define and manipulate database,
things. Here we tend to focus on electronic allowing users to store, process and analyze
ones. data easily. DBMS provides us with an
interface or a tool, to perform various
11.1 Data operations to create a database, storing of
Data are raw facts stored in a data and for updating data, etc. DBMS also
computer. A data may contain any character, provides protection and security to the
text, word or a number. databases. It also maintains data consistency
Example : 600006, DPI Campus, SCERT, in case of multiple users.
Chennai, College Road Examples of DBMS softwares : Foxpro, dbase.
182
182

12th Computer Science_EM Chapter 11.indd 182 26-12-2022 16:46:12


11.4.1 Relational Database Management System (RDBMS)
RDBMS is more advanced version of DBMS, that allows to data in a more efficient way.
It is used to store and manupulate the data that are in the form of tables.
Examaple of RDBMS: MySQL,Oracle, MS-Access etc.,
11.4.2 Characteristics of Relational Database Management System
1. 
Ability to manipulate RDBMS provides the facility to manipulate data (store, modify
data and delete)in a data base.

2. Reduced Redundancy In the modern world hard drives are very cheap, but earlier
when hard drives were too expensive, unnecessary repetition
of data in database was a big problem But RDBMS follows
Normalisation which divides the data in such a way that
repetition is minimum.

3.Data Consistency On live data, it is being continuously updated and added,


maintaining the consistency of data can become a challenge.
But RDBMS handles it by itself.

4. Support Multiple user RDBMS allows multiple users to work on it(update, insert,
and Concurrent Access delete data) at the same time and still manages to maintain the
data consistency.

5.Query Language RDBMS provides users with a simple query language, using
which data can be easily fetched, inserted, deleted and updated
in a database.

6. Security The RDBMS also takes care of the security of data, protecting
the data from unauthorized access. In a typical RDBMS, we can
create user accounts with different access permissions, using
which we can easily secure our data by restricting user access.

7. DBMS Supports It allows us to better handle and manage data integrity in real
Transactions world applications where multi-threading is extensively used.

11.4.3 Advantages of RDBMS


• Segregation of application program
• Minimal data duplication or Data Redundancy
• Easy retrieval of data using the Query Language
• Reduced development time and maintenance

183

12th Computer Science_EM Chapter 11.indd 183 26-12-2022 16:46:12


11.4.4 Components of DBMS 3. Data: It is the resource for which DBMS
The Database Management System is designed. DBMS creation is to store and
can be divided into five major components utilize data.
as follows: 4. Procedures/Methods: They are general
1.Hardware 2.Software instructions to use a database management
system such as installation of DBMS,
3.Data 4.Procedures/Methods
manage databases to take backups, report
5.Database Access Languages generation, etc.
5. DataBase Access Languages: They are
the languages used to write commands to
access, insert, update and delete data stored
Hardware in any database.
Examples of popular DBMS: Dbase,
FoxPro
Software
11.5 Database Structure
Data Base Procedures
/ Methods Table is the entire collection of
Access
related data in one table, referred to as a File
DATA

Languages
or Table where the data is organized as row
USER and column.
Components of DBMS
Each row in a table represents a
Figure 11.1 record, which is a set of data for each
database entry.
1. Hardware: The computer, hard disk, I/O
channels for data, and any other physical Each table column represents a Field,
component involved in storage of data which groups each piece or item of data
2. Software: This main component is among the records into specific categories or
a program that controls everything. The types of data. Eg. StuNo., StuName, StuAge,
DBMS software is capable of understanding StuClass, StuSec.
the Database Access Languages and A Table is known as a RELATION
interprets into database commands for
execution. A Row is known as a TUPLE
A column is known as an ATTRIBUTE

XII Std Computer Science 184

12th Computer Science_EM Chapter 11.indd 184 26-12-2022 16:46:12


Attribute / Column Tuple / Row

StuNo StuName StuAge StuClass StuSec


1101 Kannan 14 9 A
1102 Ramakrishnan 14 9 A
1103 Vidhya 14 10 B
1104 Britto 15 9 A
1105 Padmaja 14 10 B

Table / Relation
DataBase Structure
Fig 11.2
11.6 Data Model
• A data model describes how the data can be represented and accessed from a software after
complete implementation
• It is a simple abstraction of complex real world data gathering environment.
• The main purpose of data model is to give an idea as how the final system or software will
look like after development is completed.

11.6.1 Types of Data Model


Following are the different types of a Data Model

• Hierarchical Model
• Relational Model
• Network Database Model
• Entity Relationship Model
• Object Model
1. Hierarchical Model
Hierarchical model was developed by IBM as Information Management System.

In Hierarchical model, data is represented as a simple tree like structure form. This
model represents a one-to-many relationship i.e parent-child relationship. One child can have
only one parent but one parent can have many children. This model is mainly used in IBM
Main Frame computers.

185 Database Concepts

12th Computer Science_EM Chapter 11.indd 185 26-12-2022 16:46:12


School

Course Resources

Theory Lab

Hierarchical Model Fig. 11.3

2. Relational Model
The Relational Database model was first proposed by E.F. Codd in 1970 . Nowadays, it
is the most widespread data model used for database applications around the world.
The basic structure of data in relational model is tables (relations). All the information’s
related to a particular type is stored in rows of that table. Hence tables are also known as
relations in a relational model. A relation key is an attribute which uniquely identifies a
particular tuple (row in a relation (table)).

Stu_id Name Age Subj_id Name Teacher


1 Malar 17 1 C++ Kannan
2 Suncar 16 2 Php Ramakrishnan
3 Velu 16 3 Python Vidhya

Stu_id Subj_id Marks


1 1 92
1 2 89
3 2 96
Relational Model
Fig. 11.4

3. Network Model
Network database model is an extended form of hierarchical data model. The difference
between hierarchical and Network data model is :
• In hierarchical model, a child record has only one parent node,

XII Std Computer Science 186

12th Computer Science_EM Chapter 11.indd 186 26-12-2022 16:46:12


• In a Network model, a child may have many parent nodes. It represents the data in many-
to-many relationships.
• This model is easier and faster to access the data.

School

Library Office Staff Room This child has one parent node

Student Student has 3 parent node

Network Model
Fig. 11.5

School represents the parent node


Library, Office and Staff room is a child to school
Student is a child to library, office and staff room (one to many relationship)
4. Entity Relationship Model. (ER model)
In this database model, relationship are created by dividing the object into entity and its
characteristics into attributes.
It was developed by Chen in 1976. This model is useful in developing a conceptual
design for the database. It is very simple and easy to design logical view of data. The developer
can easily understand the system by looking at ER model constructed.
Rectangle represents the entities. E.g. Doctor and Patient
Ellipse represents the attributes E.g. D-id, D-name, P-id, P-name. Attributes describes
the characteristics and each entity becomes a major part of the data stored in the database.

Diamond represents the relationship in ER diagrams

E.g. Doctor diagnosis the Patient

187 Database Concepts

12th Computer Science_EM Chapter 11.indd 187 26-12-2022 16:46:12


Doctor Diagnosis Patient

D-id D-Name P-id P-Name


ER model
Fig. 11.6

5. Object Model
Object model stores the data in the form of objects, attributes and methods, classes and
Inheritance. This model handles more complex applications, such as Geographic information
System (GIS), scientific experiments, engineering design and manufacturing. It is used in file
Management System. It represents real world objects, attributes and behaviors. It provides a
clear modular structure. It is easy to maintain and modify the existing code.

Shape
get_area()
get_perimeter()

Circle Rectangle Triangle


Radius Length Base
breath Height
Object Model
Fig. 11.7

An example of the Object model is Shape, Circle, Rectangle and Triangle are all objects
in this model.
• Circle has the attribute radius.
• Rectangle has the attributes length and breadth.
• Triangle has the attributes base and height .
• The objects Circle, Rectangle and Triangle inherit from the object Shape.

11.6.2 Types of DBMS Users


Database Administrators
Database Administrator or DBA is the one who manages the complete database
management system. DBA takes care of the security of the DBMS, managing the license keys,
managing user accounts and access etc.

XII Std Computer Science 188

12th Computer Science_EM Chapter 11.indd 188 26-12-2022 16:46:12


Application Programmers or Software Developers
This user group is involved in developing and designing the parts of DBMS.
End User
All modern applications, web or mobile, store user data. Applications are programmed
in such a way that they collect user data and store the data on DBMS systems running on their
server. End users are the one who store, retrieve, update and delete data.
Database designers: are responsible for identifying the data to be stored in the database for
choosing appropriate structures to represent and store the data.

11.7 Difference between DBMS and RDBMS

Basis of Comparison DBMS RDBMS

Expansion Database Management System Relational DataBase


Management System

Data storage Navigational model Relational model (in tables).


ie data by linked records ie data in tables as row and
column

Data redundancy Present Not Present

Normalization Not performed RDBMS uses normalization


to reduce redundancy

Data access Consumes more time Faster, compared to DBMS.

Keys and indexes Does not use. used to establish


relationship. Keys are used in
RDBMS.

Transaction Inefficient, Efficient and secure.


management Error prone and insecure.

Distributed Databases Not supported Supported by RDBMS.

Example Dbase, FoxPro. SQL server, Oracle, mysql,


MariaDB, SQLite, MS Access.

Database normalization was first proposed by Dr. Edgar F Codd as an integral part
of RDBMS in order to reduce data redundancy and improve data integrity. These rules are
known as E F Codd Rules.

189 Database Concepts

12th Computer Science_EM Chapter 11.indd 189 26-12-2022 16:46:12


11.8 Types of Relationships staff members.

Following are the types of relationships used Staff


in a database. Department

1. One-to-One Relationship Gajalakshmi

2. One-to-Many Relationship
Computer Bindhu
3. Many-to-One Relationship
4. Many-to-Many Relationship Tamil Radha
1. One-to-One Relationship
In One-to-One Relationship, one Maths Ramesh
entity is related with only one other entity.
One row in a table is linked with only one
row in another table and vice versa. Malaiarasu

For example: A student can have only


one exam number One to Many Mapping
Fig 11.9
Student Exam No
3. Many-to-One Relationship
In Many-to-One Relationship, many
entities can be related with only one in the
Tamilselvi 1001 other entity.
For example: A number of staff
Jayapandiyan 1002 members working in one Department.
Multiple rows in staff members table
Sarojini 1003 is related with only one row in Department
table.
Staff Department

One to one Relationships


Fig 11.8
Suganya
Computer
2. One-to-Many Relationship
Bala
In One-to-Many relationship, one Maths
entity is related to many other entities. Valarmathi
One row in a table A is linked to
many rows in a table B, but one row in a
table B is linked to only one row in table A. Many to one Relationship
For example: One Department has many Fig 11.10

XII Std Computer Science 190

12th Computer Science_EM Chapter 11.indd 190 26-12-2022 16:46:13


4. Many-to-Many Relationship 11.9 Relational Algebra in DBMS
A many-to-many relationship
occurs when multiple records in a table are What is Relational Algebra?
associated with multiple records in another Relational Algebra, was first created
table. by Edgar F Codd while at IBM. It was used
for modeling the data stored in relational
Example 1: Customers and Product
databases and defining queries on it.
Customers can purchase various
products and Products can be purchased by Relational Algebra is a procedural
many customers query language used to query the database
tables using SQL.
Example 2: Students and Courses
Relational algebra operations are
A student can register for many
performed recursively on a relation (table)
Courses and a Course may include many
to yield an output. The output of these
stu d e nt s
operations is a new relation, which might be
Example 3: Books and Student. formed by one or more input relations.
Many Books in a Library are issued
Relational Algebra is divided into various
to many students.
groups
Book Student
Unary Relational Operations
SELECT ( symbol : σ)
C++ Kalaivani
PROJECT ( symbol : ∏)

SQL Manjula Relational Algebra Operations from Set


Theor y
Sridevi
python • UNION (∪)
• INTERSECTION (∩)
Many to Many Relationship • DIFFERENCE (−)
Fig 11.11
• CARTESIAN PRODUCT (X)
SELECT (symbol : σ)
The relational model
was invented by Edgar General form σ ( R ) with a relation
c
Frank Codd (Father of R and a condition C on the attributes of R.
Relational DataBase) as The SELECT operation is used for
a general model of data, and selecting a subset with tuples according to a
subsequently promoted by Chris Date given condition.
and Hugh Darwen among others.
Select filters out all tuples that do not
satisfy C.
191 Database Concepts

12th Computer Science_EM Chapter 11.indd 191 26-12-2022 16:46:13

You might also like