s-1 dbqa
s-1 dbqa
Cartesian Product
The cartesian product is a binary operation and is denoted by (x). The degree of new relation is the
sum of the degrees of two relations on which Cartesian product is operated. The number of
tuples, of the new relation is equal to the product of the number of tuples, of the two relations
on which Cartesian product is performed.
e.g. if A = {1, 2, 3} and B = {a, b, c}, find A x B.
A x B = ((1, a), (1, b), (1, c), (2, a), (2, b), (2, c), (3, a), (3, b), (3, c))
In SQL the CROSS JOIN or CARTESIAN JOIN is used to produce the cartesian product of two
tables.
The Cartesian product is a basic type of join that matches each row from one table to every row
from another table.
e.g. Consider the following EMPLOYEES and DEPARTMENTS tables:
To get the cartesian product, the following sql statement can be used:
1 MARK QUESTIONS
144
Ans. d. mycursor.rowcount
2 MARKS QUESTIONS
1. Write the code to create the connection in which database’s name is Python,
name of host, user and password can taken by user. Also, print that connection?
Ans.import mysql.connector
mycon = mysql.connector.connect( host = “localhost”, user = “root”, passwd = “tiger”,
database = “Python”)
print(mycon)
3. Explain the following results retrieval methods with examples-
fetchone () rowcount ()
Ans. fetchone() :- The fetchone() method will return only one row from the result set in the
form of tuple containing a record.
rowcount() :- cursor.rowcount() that always return how many records have been retrieved
so for using any of the fetch..() methods.
3. Write a small python program to insert a record in the table books with
attributes (title ,isbn).
Ans. import mysql.connector as Sqlator
conn =sqlator.connect(host=”localhost”,user=”root”,passwd=””,database=”test”)
cursor=con.cursor()
query=”INSERT into books(title,isbn) values(‘{}’{})”.format(‘Neelesh’,’5143’)
cursor.execute(query)
con.close()
4. Differentiate between fetchone() and fetchall() methods.
Ans. fetch(): It returns the next row from the result set as tuple. If there are no more
rows to retrieve, None is returned.
fetchall():It fetches all the rows of a query result. It returns all the rows as a list of tuples.
An empty list is returned if there is no record to fetch.
5. Consider the following Python code for updating the records:
def Update(eno):
#Assume basic setup import, connection(con) and cursor(mycursor) is created query="update emp set
salary=90000 where empno=” + str(eno) mycursor.execute(query)
Codeisrunningbuttherecordinactualdatabaseisnotupdating,whatcouldbethepossible reason?
a) save() function ismissing
b) con.save() function ismissing
c) con.commit() function ismissing
d) commit() function ismissing
145
se=”MYPROJECT”)
2. The given program is used to connect with MySQL abd show the name of the all
the record from the table “stmaster” from the database “oraclenk”. You are
required to complete the statements so that the code can be executed properly.
import _____.connector__pymysql
dbcon=pymysql._____________(host=”localhost”, user=”root”,
________=”sia@1928”)
if dbcon.isconnected()==False
print(“Error in establishing connection:”)
cur=dbcon.______________()
query=”select * from stmaster”
cur.execute(_________)
resultset=cur.fetchmany(3)
for row in resultset:
print(row)
dbcon.______()
Following python code is written to access the records of table : EMP, What will be the output of following
code?
# Assume All basic setup related to connection and cursor creation is already done
query="select * fromemp"
mycursor.execute(query)
results = mycursor.fetchone()
results = mycursor.fetchone()
results = mycursor.fetchone()
d = int(results[3])
print(d*3)
Ans. 165000
5. Find the output:
146
Ans. Total records fetched so far are 1
Total records fetched so far are2
Total records fetched so far are4
147