Databases and SQLite: book back answers
22 solved questions from the end of this chapter, Class 12 Computer Science, Tamil Nadu State Board. Each answer is checked against the textbook, and you can open the page that proves it.
← Notes and quiz for Databases and SQLiteChoose the best answer (1 Mark)
Which of the following is an organized collection of data?
- (A) Database
- (B) DBMS
- (C) Information
- (D) Records
Try it, then show the answerAnswer
(A) Database
The chapter defines a database as an organized collection of data.
From the book: “A database is an organized collection of data.”
SQLite falls under which database system?
- (A) Flat file database system
- (B) Relational Database system
- (C) Hierarchical database system
- (D) Object oriented Database system
Try it, then show the answerAnswer
(B) Relational Database system
SQLite is described as a simple relational database system.
From the book: “SQLite is a simple relational database system”
Which of the following is a control structure used to traverse and fetch the records of the database?
- (A) Pointer
- (B) Key
- (C) Cursor
- (D) Insertion point
Try it, then show the answerAnswer
(C) Cursor
The cursor traverses and fetches database records.
From the book: “Cursor in step 3: is a control structure used to traverse and fetch the records of the database.”
Any changes made in the values of the record should be saved by the command
- (A) Save
- (B) Save As
- (C) Commit
- (D) Oblige
Try it, then show the answerAnswer
(C) Commit
The chapter says record changes should be saved using the Commit command.
From the book: “Any changes made in the values of the record should be saved by the commend "Commit"”
Which of the following executes the SQL command to perform some action?
- (A) execute()
- (B) key()
- (C) cursor()
- (D) run()
Try it, then show the answerAnswer
(A) execute()
The execute method runs the SQL command to perform an action.
From the book: ““execute” method executes the SQL command to perform some action.”
Which of the following function retrieves the average of a selected column of rows in a table?
- (A) Add()
- (B) SUM()
- (C) AVG()
- (D) AVERAGE()
Try it, then show the answerAnswer
(C) AVG()
The chapter identifies AVG() as the function that retrieves an average.
From the book: “AVG() function retrieves the average of a selected column of rows in a table.”
The function that returns the largest value of the selected column is
- (A) MAX()
- (B) LARGE()
- (C) HIGH()
- (D) MAXIMUM()
Try it, then show the answerAnswer
(A) MAX()
MAX() returns the largest value of a selected column.
From the book: “The MAX() function returns the largest value of the selected column.”
Which of the following is called the master table?
- (A) sqlite_master
- (B) sql_master
- (C) main_master
- (D) master_main
Try it, then show the answerAnswer
(A) sqlite_master
The chapter names sqlite_master as the master table holding key information about database tables.
From the book: “The master table holds the key information about your database tables and it is called sqlite_master”
The most commonly used statement in SQL is
- (A) cursor
- (B) select
- (C) execute
- (D) commit
Try it, then show the answerAnswer
(B) select
The chapter says SELECT is the most commonly used statement in SQL.
From the book: ““Select” is the most commonly used statement in SQL”
Which of the following keyword avoid the duplicate?
- (A) Distinct
- (B) Remove
- (C) Where
- (D) GroupBy
Try it, then show the answerAnswer
(A) Distinct
DISTINCT fetches only unique values, avoiding duplicate values.
From the book: “When we use distinct keyword only the unique values are fetched.”
Part - II: Answer the following questions (2 Marks)
Mention the users who uses the Database.
Try it, then show the answerAnswer
Database users can be human users, other programs, or applications.
The chapter explicitly includes people and software as database users.
From the book: “Users don't have to be human users. They can be other programs and applications as well.”
Which method is used to connect a database? Give an example.
Try it, then show the answerAnswer
The connect() method is used. Example: connection = sqlite3.connect("Academy.db")
The chapter instructs creating a connection with connect() and passing the database file name.
From the book: “create a connection using connect () method and pass the name of the database File”
What is the advantage of declaring a column as “INTEGER PRIMARY KEY”
Try it, then show the answerAnswer
It automatically assigns an integer one greater than the highest value already used when NULL is supplied; if the table is empty, it assigns 1.
This is the automatic increment behaviour described for an INTEGER PRIMARY KEY column.
From the book: “If a column of a table is declared to be an INTEGER PRIMARY KEY, then whenever a NULL will be used as an input for this column, the NULL will be automatically converted into an integer”
Which method is used to fetch all rows from the database table?
Try it, then show the answerAnswer
fetchall()
The fetchall() method retrieves every row from the table.
From the book: “The fetchall() method is used to fetch all rows from the database table”
Part - III: Answer the following questions (3 Marks)
What is SQLite?What is it advantage?
Try it, then show the answerAnswer
SQLite is a simple relational database system that saves data in regular data files within the computer’s internal memory. It is embedded in applications rather than requiring a separate database server, and it is fast, rigorously tested, and flexible.
These are the characteristics and advantages listed in the chapter.
From the book: “It is designed to be embedded in applications, instead of using a separate database server program such as MySQLor Oracle. SQLite is fast, rigorously tested, and flexible”
Mention the difference between fetchone() and fetchmany()
Try it, then show the answerAnswer
fetchone() returns the next row of a query result set, or None if no row is left. fetchmany(n) returns the next specified number of rows (n) from the result set.
The methods differ in how many rows they return at a time.
From the book: “The fetchone () method returns the next row of a query result set or None in case there is no row left. cursor.fetchmany() method that returns the next number of rows (n)”
What is the use of Where Clause.Give a python statement Using the where clause.
Try it, then show the answerAnswer
The WHERE clause extracts only records that fulfil a specified condition. Example: cursor.execute("SELECT * FROM student WHERE gender='M'")
WHERE filters records by a condition; the chapter demonstrates it in a SELECT statement.
From the book: “The WHERE clause is used to extract only those records that fulfill a specified condition.”
Read the following details.Based on that write a python script to display records in desending order of Eno database name :- organization.db Table name :- Employee Columns in the table :- Eno, EmpName, Esal, Dept
Try it, then show the answerAnswer
import sqlite3 connection = sqlite3.connect("organization.db") cursor = connection.cursor() cursor.execute("SELECT * FROM Employee ORDER BY Eno DESC") result = cursor.fetchall() print(*result, sep="\n")
The chapter demonstrates using ORDER BY to sort query results and fetchall() to retrieve the records. DESC specifies descending order.
From the book: “The ORDER BY Clause can be used along with the SELECT statement to sort the data of specific fields in an ordered way.”
Part - IV: Answer the following questions (5 Marks)
Write in brief about SQLite and the steps used to use it.
Try it, then show the answerAnswer
SQLite is a simple relational database system that stores data in regular files on the computer. Python has a native library for SQLite, so a separate database server is not needed.
- Create a connection: Use the connect() method and pass the database file name. If the file exists, it opens that database; otherwise, Python creates a new one.
- Create a cursor: Create a cursor object from the connection. The cursor is used to traverse and fetch database records.
- Write SQL commands: Write the required SQL statement and execute it using the cursor object. SQL commands in Python can be written as triple-quoted strings.
- Execute the command: Pass the SQL command to the cursor’s execute() method to perform the required action, such as creating a table or inserting records.
- Save changes: Commit changes made to records before closing the database connection so that they are saved.
Example: The chapter connects to the database with `connection = sqlite3.connect("Academy.db")` and creates a cursor with `cursor = connection.cursor()`.
Thus, Python uses a connection and cursor to execute SQL commands in SQLite and save database changes.
From the book: “The cursor is used to traverse the records from the result set. You can define a SQL command with a triple quoted string in Python.”
Write the Python script to display all the records of the following table using fetchmany() Icode ItemName Rate 1003 Scanner 10500 1004 Speaker 3000 1005 Printer 8000 1008 Monitor 15000 1010 Mouse 700
Try it, then show the answerAnswer
The `fetchmany(n)` method returns the next number of rows from a query result set. To display all records, repeatedly fetch rows until no rows remain.
- Connect to the database: Import `sqlite3` and connect to the database containing the table.
- Create a cursor: Create a cursor object to execute the SQL command and fetch records.
- Select the records: Execute a `SELECT` query to retrieve all records from the table.
- Fetch records in batches: Call `fetchmany()` repeatedly; it returns the next number of rows of the result set. Continue until it returns an empty list.
- Display each record: Print the rows in each batch, then fetch the next batch until all rows have been displayed.
Example: import sqlite3 connection = sqlite3.connect("Academy.db") cursor = connection.cursor() cursor.execute("SELECT * FROM Item") while True: result = cursor.fetchmany(2) if not result: break for record in result: print(record) connection.close()
This script displays every record by repeatedly fetching and printing batches of rows.
From the book: “Displaying specified number of records is done by using fetchmany(n). This method returns the number of rows of the result set.”
hat is the use of HAVING clause. Give an example python script
Try it, then show the answerAnswer
The HAVING clause filters grouped data based on group functions. Unlike WHERE, it can be used with group functions such as COUNT().
- Purpose: HAVING is used to filter data based on group functions.
- Use with GROUP BY: It is used with grouped results to select only the groups that meet a condition.
- Group functions: Group functions such as COUNT can be used in the HAVING condition.
- Difference from WHERE: WHERE filters records, while HAVING filters data using group functions.
Example: import sqlite3 connection = sqlite3.connect("Academy.db") cursor = connection.cursor() cursor.execute("SELECT GENDER,COUNT(GENDER) FROM Student GROUP BY GENDER HAVING COUNT(GENDER)>3") result = cursor.fetchall() co = [i[0] for i in cursor.description] print(co) print(result)
Thus, HAVING is used to filter grouped results by applying conditions to group functions.
From the book: “Having clause is used to filter data based on the group functions.”
Write a Python script to create a table called ITEM with following specification. Add one record to the table. Name of the database :- ABC Name of the table :- Item Column name and specification :- Icode :integer and act as primary key Item Name :- Character with length 25 Rate :- Integer Record to be added :- 1008, Monitor,15000
Try it, then show the answerAnswer
The Python sqlite3 module can create a database table and insert records into it. The cursor executes SQL commands, and commit saves the changes.
- Import the module and connect: Connect to the database file ABC.db and create a cursor for executing SQL commands.
- Insert the record: Use an INSERT statement to add the given item code, name and rate to ITEM.
- Execute the commands: Execute the table creation and insertion SQL statements using the cursor.
- Save and close: Commit the changes before closing the connection.
The ITEM table is created in ABC.db and the specified record is saved.
From the book: “Any changes made in the values of the record should be saved by the commend "Commit" before closing the "Table connection".”