Professional Documents
Culture Documents
CLASS-XII 2020-21
DISTRIBUTION OF MARKS:
UNIT UNIT NAME MARKS
II Computer Networks 10
TOTAL 70
What is Data?
What is a Database?
Ease of updating data: With the database, we can flexibly update the
data according to our convenience. Moreover, multiple people can also
edit data at same time.
Types of DBMS
Let's see how the DBMS family got evolved with the time. Following
diagram shows the evolution of DBMS categories.
There are 4 major types of DBMS. Let's look into them in detail.
Hierarchical DBMS
In a Hierarchical database, model data is organized in a tree-like
structure. Data is Stored Hierarchically (top down or bottom up) format.
Data is represented using a parent-child relationship. In Hierarchical
DBMS parent may have many children, but children have only one parent.
Network Model
The network database model allows each child to have multiple parents.
It helps you to address the need to model more complex relationships
like as the orders/parts many-to-many relationship. In this model,
entities are organized in a graph which can be accessed through several
paths.
Relational model
Relational DBMS is the most widely used DBMS model because it is one
of the easiest. This model is based on normalizing data in the rows and
columns of the tables. Relational model stored in fixed structures and
manipulated using SQL.
Object-Oriented Model
In Object-oriented Model data stored in the form of objects. The
structure which is called classes which display data within it. It defines a
database as a collection of objects which stores both data members
values and operations.
The table name and column names are helpful to interpret the meaning of
values in each row. The data are represented as a set of relations. In the
relational model, data are stored as tables. However, the physical storage
of the data is independent of the way the data are logically organized.
Relational Model Concepts
Tables – In the Relational model the, relations are saved in the table
format. It is stored along with its entities. A table has two properties
rows and columns. Rows represent records and columns represent
attributes.
Column: The column represents the set of values for a specific attribute.
Relation key - Every row has one, two or multiple attributes, which is
called relation key.
Domain :It is a collection of values from which the value is derived for a
column.
What are Keys?
Example:
Example:
In the following example, StudID is a Primary Key.
Example:
In this table, StudID, Roll No, Email are qualified to become a primary
key. But since StudID is the primary key, Roll No, Email becomes the
alternative key.
Example: In the given table Stud ID, Roll No, and email are candidate
keys which help us to uniquely identify the student record in the table.
Example:
DeptCode DeptName
001 Science
002 English
005 Computer
Teacher ID Fname Lname
B002 David Warner
B017 Sara Joseph
B009 Mike Brunton
In this table, adding the foreign key in Deptcode to the Teacher name,
we can create a relationship between the two tables.
Features of SQL:
1. Retrieve data from database
2. Insert data in database
3. Update data in database
4. Create new database
5. Create new tables in database
6. Create views in database
DDL commands are used for creating databases and tables. It contains
necessary statements for creating, manipulating, altering and deleting
tables.
1. CREATE (create database and table)
2. ALTER (alter table)
3. DROP (delete table)
Data Manipulation Language:
Data Types(MySQL):
We will use only bold datatypes
DATE YYYY-MM-DD
SQL commands:
Creating a database-
Deleting a database
To insert new rows into an existing table use the INSERT command:
mysql>INSERT INTO student values(‘dwivedi’,’freya’,’Udaipur’,’4’);
INSERT command can also be used to take or derive values from one
table and place them in another by using it with a query. To do this,
simply replace the VALUES clause with an appropriate query as follows:
With the SELECT command we can retrieve (or see) previously inserted
rows:
Example:
‘A%’ represents any string starting /with ‘A’ character.
‘_ _A’ represents any 3 character string ending with ‘A’.
‘_B%’ represents any string having second character ‘B’
‘_ _ _’ represents any 3 letter string.
A pattern is case sensitive and can be used with LIKE operator.
Altering Table
Syntax
ALTER TABLE command to add a New Column in an existing table is as
follows.
ALTER TABLE table_name ADD column_name datatype;
ALTER TABLE employee
ADD (tel_number integer);
ALTER TABLE command to DROP COLUMN in an existing table is as
follows.
ALTER TABLE table_name DROP COLUMN column_name;
ALTER TABLE employee
DROP grade;
Example:
SELECT COUNT(class) FROM student GROUP BY city;
COUNT(class)
2
2
City count(*)
Jaipur 2
Kota 2
Ajmer 1
The GROUP BY Clause is used to group rows with the same values.
The GROUP BY Clause is used together with the SQL SELECT statement.
The SELECT statement used in the GROUP BY clause can only be used to
contain column names, aggregate functions, constants and expressions.
The HAVING clause is used to restrict the results returned by the
GROUP BY clause.
Aggregate Functions
Name Purpose
SUM() Returns the sum of the given column.
MIN() Returns the minimum value in the given column.
MAX() Returns the maximum value in the given column.
AVG() Returns the Average value of the given column.
COUNT() Returns the total number of values/ records as per given
column.
Note :- Where clause works in respect of the whole table but Having
works on Group only. If Where and Having both are used then Where
will be executed first.
Joins: equi-join and natural join
A join is a query that combines rows from two or more tables. In a JOIN
query more than one table are listed in the FROM clause. MySQL
provides various type of Joining :
The join, in which columns are compared for equality is called Equi-Join.
A non-equi join specifies condition with non-equality operator. In equi-
join we put (*) in the select list therefore the common column will appear
twice in the output.
To understand the output, let's take 2 table one for employee (contains
employee detail with deptno) and another for department contains
deptno and other department details.
The JOIN in which only one of the identical columns exists is called
Natural Join. It is similar to Equi-join except that duplicate columns are
eliminated in Natural join that would otherwise appear in Equi-Join.
The reason of this error is – the deptno exists in both the table, so in
this case if we are selecting or using only deptno then it becomes
ambiguous from which table this deptno will be selected
To resolve this error, just qualify the common column by table name
as TableName.column name
Till now we have performed joining using traditional SQL method which is
common to most of the RDBMS software now we will learn MySQL style
of joining using JOIN clause. MySQL support various options with JOIN
import mysql.connector Or
import mysql.connector as ms
connect(host=<server_name>,user=<user_name>,passwd=<password>[,data
base=<database>])
Example:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="roo
t“, database=“school”)
print(mydb)
Example:
Cursor object :
The MySQLCursor class instantiates objects that can execute operations
such as SQL statements. Cursor objects interact with the MySQL
server using a MySQLConnection object.
TO CREATE CURSOR
Cursor_name = connectionObject.cursor()
For e.g.
mycursor = mycon.cursor()
TO EXECUTE QUERY
Example:
Output shows cursor is created and query is fired and stored, but no
data is coming.
To fetch data we have to use functions like fetchall(), fetchone(),
fetchmany() etc.
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="")
mycursor=mydb.cursor()
mycursor.execute("create database if not exists school")
mycursor.execute("show databases")
for x in mycursor:
print(x)
Line 5 executes the sql query show databases and store result in
mycursor as collection, whose values are being fetched in x variable one
by one.
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="",database=
"student")
mycursor=mydb.cursor()
mycursor.execute("create table student(rollno int(3) primary key,name
varchar(20),age int(2))")
Example: change table structure (add, edit, remove column of a
table)
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="",database=
"student")
mycursor=mydb.cursor()
mycursor.execute("alter table emp add (bonus int(3))")
mycursor.execute("desc emp")
for x in mycursor:
print(x)
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="",database=
"student")
mycursor=mydb.cursor()
while 1==1:
ch=int(input("enter -1 to exit any other no to insert record into student table"))
if ch==-1:
break
eno=int(input("Enter eno"))
ename=input("Enter ename")
edept=input("Enter dept")
sal=int(input("Enter salary"))
mydb.commit()
Example:(search a record)
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="",database=
"student")
mycursor=mydb.cursor()
nm=input("enter name")
mycursor.execute("select * from emp where ename='"+nm+"'")
for x in mycursor:
print (x)
Example: (delete record of a table)
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="",database=
"student")
mycursor=mydb.cursor()
mycursor.execute("delete from emp where eno=100")
mydb.commit()
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="",da
tabase="student")
mycursor=mydb.cursor()
mycursor.execute("update emp set sal=1000 where eno=101")
mydb.commit()
Example:
Example:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="",database=
"student")
mycursor=mydb.cursor()
mycursor.execute("select * from emp")
myrecords=mycursor.fetchall()
for x in myrecords:
print (x)
Example:
Example:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="",database=
"student")
mycursor=mydb.cursor()
mycursor.execute("select * from emp")
row=mycursor.fetchone()
while row is not None:
print(row)
row = mycursor.fetchone()
Example:
Example:
Example: (rowcount()):
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="",database=
"student")
mycursor=mydb.cursor()
mycursor = mydb.cursor(buffered=True)
mycursor.execute("select * from emp")
noofrows=mycursor.rowcount
print("No of rows in student table are",noofrows)
This type cursor fetches rows and buffers them after getting output
from MySQL database. We can use such cursor as iterator. There is no
point in using buffered cursor for single fetching of rows. If we don’t use
buffered cursor then we will get -1 as output from