Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
79 views

Cycle Test V - Answer Key Marks: 50

This summarizes a document containing an answer key for a computer science exam on relational databases and SQL. It includes: 1. Multiple choice questions on SQL commands like DDL, DML and data manipulation. 2. Short answer questions defining concepts like advantages of CSV files, reading from CSV files, advantages of DBMS, attributes and tuples in a database. 3. Detailed questions explaining writing to CSV files, data types in SQL with examples, using ALTER TABLE to modify tables, SQL constraints. 4. Key differences between SQL elements like keywords, clauses and statements.

Uploaded by

Pranavhari T.N.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Cycle Test V - Answer Key Marks: 50

This summarizes a document containing an answer key for a computer science exam on relational databases and SQL. It includes: 1. Multiple choice questions on SQL commands like DDL, DML and data manipulation. 2. Short answer questions defining concepts like advantages of CSV files, reading from CSV files, advantages of DBMS, attributes and tuples in a database. 3. Detailed questions explaining writing to CSV files, data types in SQL with examples, using ALTER TABLE to modify tables, SQL constraints. 4. Key differences between SQL elements like keywords, clauses and statements.

Uploaded by

Pranavhari T.N.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

MAHATMA MONTESSORI SCHOOL SET A

(Affiliated to Central Board of Secondary Education)

CYCLE TEST V - ANSWER KEY Marks: 50


STD:XII COMPUTER SCIENCE (Code: 083) Time: 2 Hrs

I. Choose the best answers: (5 X 1 = 5)


1. The ____ allows us to perform tasks related to changing the structure of a table. a. DDL
2. A table “Transport” in a database has degree 3 and cardinality 8. What is the number of rows and
columns in it? c. rows = 8, columns = 3
3. _____ command helps us to view the structure of the table. c. Describe
4. CSV stands for _____. c. Comma Separated Values
5. An SQL command is made up of _______ number of clauses. d. Two or more.

II. Answer the following briefly: (10 X 2 = 20)


6. Write the advantages of CSV file.
Advantages of CSV files are as follows:
. CSV is easy to generate and import onto a spreadsheet or database
• CSV is faster to handle and smaller in size.
• CSV is human readable and applications easy to edit manually.
• CSV is capable of storing large amount of data
• CSV is processed by almost all existing

7. Write short notes on reading from a CSV file.


Reading from a CSV file is done using the reader object.
Reader object is an iterable object that gives access to each line of the CSV file as a list of fields.
next() function is used to read the next line of the CSV file.
Before reading from a CSV file, the file should exist. It can be either created directly in
MSExcel or through a python program we can write into a file.

8. What are the advantages of DBMS?


1. Elimination of Data Redundancy:
. 2. Data Consistency:
3. Sharing of Data:
4. Reduced Programming Effort:
5. Database Enforces Standards
6. Improved Data Integrity:
7. Privacy and Security:
8. Economical:
9. Improved Backup and Recovery System:
10. Meeting Enterprise Requirements than Individual Requirements:
1
9. What do you mean by attributes and tuples in terms of relational database? Give example.
Attribute: In a relational table, an attribute is a set of values of a particular type. The term
attribute is also used to represent a column. A set of attributes defines property of entity.

Tuple: Each row in a table is known as tuple. It is also called a row/record. A single entry
in a table is called a record or row.
Example:-

.
10. Write the difference between Drop and Delete commands.
Drop – This is a DDL command. It is used to drop a table or database.
Delete – This is a DML command. It is used to delete the records/rows in a table.
Using this command we can either delete all the rows or specific rows.
DROP command is used to drop a table along with all the records stored in it whereas
DELETE command is used to delete all the records or some of the records from a table
without deleting the table.

11. What is the difference between primary and foreign key?


Primary Key Primary key is a set of one or more fields/columns of a table that
uniquely identifies a record in a database table. It cannot accept null, duplicate
values. Only one Candidate Key can be the Primary Key.
.
Foreign Key: A foreign key is a non-key attribute whose value is derived from the primary
key of another table; in other words, a primary key in some other table having
relationship with the current or original table

12. Write one similarity and one difference between CHAR and VARCHAR data types.
SIMILARITY: Both CHAR and VARCHAR data types are used to store strings.
DIFFERENCE: CHAR is of fixed length but VARCHAR is of variable length

For example, name char(10); with blank data.


name="anu";
name field occupies 3 bytes, with the For example, name varchar(10);
first three bytes with values and the rest name="anu";
2
then name occupies only 3+2=5 bytes, other two bytes for variable length
the first three bytes for value and the information.
13. Write MySQL command for the following.
(i) to display the list of existing databases. SHOW DATABASES;
(ii) to open an already existing database “CONTACTS” USE CONTACTS;

14. Write a MySql command to create the Table STOCK including its Constraints.

Table STOCK

CREATE table STOCK(Id int(4) Primary Key,


Name Varchr(20),
Company Varchar(20),
Price int(4) Not Null);

15. What are the classification of SQL commands? Name the commands under each classification.

III. Answer the following in detail: (5X 3 = 15)


16. Write short notes on writing to a CSV file.
Csv.writer() function is used to write to a csv file. This function returns a writer object that
Converts user’s data into a delimited string.
Writerobj = csv.writer(f, delimiter = ‘any character’)

3
These stringx cn be written into csv files using the following 2 functions.
1. writerow() – To write one row at a time from the list using loop and iteration
2. writerows() – To write all the rows in one go without using loop/iteration
17. What is a data type? Name any 5 data types available in MySQL. Write the difference between
Numeric and Decimal data types with examples.

In SQL each column of the table is assigned a data type which conveys the kind of value that
will be stored in the column.
Data types available in MySQL – integer, float, numeric, decimal, char, varchar, date, time

18. How is alter table command used to modify the structure of the table? Write all the possibilities
in alter table command.
ALTER TABLE command is used to modify the definition (structure) of a table by modifying
the definition of its columns. The ALTER TABLE command is used to perform the following
operations:

4
type of any column or to modify its size.

(a) Adding a column to an existing table


Syntax for adding a new column:
ALTER TABLE <table_name> ADD(<column_name><datatype> [size]);
For example, to add a new column Mobile_no of type integer in the table student:
ALTER TABLE student ADD (Mobile_no integer);
(b) Adding a column with default value
Syntax for adding a column with a default value:
ALTER TABLE <table_name>
ADD ([column_name1]<datatype1>default data);
For example, ALTER TABLE student ADD(City char(6) DEFAULT “DELHI”);
(c) Modifying an existing column definition
The MODIFY clause can be used with ALTER TABLE command to change the datatype,
size, constraint related to any column of the table.
Syntax for modifying existing column datatype:
ALTER TABLE <table_name>
MODIFY([column_name1] <datatype1>);
For example,
ALTER TABLE student MODIFY (Name varchar(25));
The above command will modify the datatype size for the Name field from 20 to 25
characters.
(d) Renaming a column
Syntax for renaming an existing column:
ALTER TABLE <table_name>
CHANGE [COLUMN] <old-column-name> <new-column-name> column_definition;
For example,
ALTER TABLE student
CHANGE City State varchar(10);
The above command shall rename the City column to State.

19. Define the various SQL constraints.


Constraints are the rules enforced on data or columns on a table. These are used to restrict the
values that can be inserted in a table. This ensures data accuracy and reliability in the database.

Following are the most commonly used constraints available in SQL:

(a) NOT NULL Constraint: Ensures that a column cannot have NULL value.
(b) DEFAULT Constraint: Provides a default value for a column when no value is specified.
(c) UNIQUE Constraint: Ensures that all values in a column are unique.
There should not be any redundant value in a column which is being restricted.
5
(d) PRIMARY Key: Uniquely identifies each row/record in a database table.
(e) FOREIGN Key: Uniquely identifies a row/record in any other database table.

20. What is a keyword, clause and statement/command in terms of SQL. Explain with example.
A keyword refers to an individual SQL element that has a special meaning in SQL.
For example, SELECT and FROM are keywords.
A clause is a distinct logical part of an SQL statement. Clausesbegin with a keyword for which
they are named and consist of arguments as well.
For example, SELECT empno, ename, FROM employee WHERE salary>45000, are clauses in SQL.
Here, arguments complete or modify the meaning of a clause, which is salary in the given example.
A statement or command is a combination of two or more clauses. Statements are basically
the instructions given to SQL database for executing any task.
For example, SELECT * FROM employee is an SQL statement.
An important point to remember here is that all the statements in SQL terminate with a semi-colon (;).
Also, SQL is not case sensitive; therefore, we can type commands in either upper case or lower case.

IV. Case study questions: (2 X 5 = 10)

21. Ashvik, a student of class 12th, is learning CSV File Module in Python. During examination, he
has been assigned an incomplete python code (shown below) to create a CSV File 'Stud.csv' (content
shown below). Help him in completing the code which creates the desired CSV File.

CSV File

import _______________ #Statement-1


with open(__________________)as csv1: #Statement-2
abc=csv.__________________ #Statement-3
abc.writerow(["NAME","LOCATION","CITY"])
record=[ ]
while True:
Name=input("Enter Name\n")

6
Location=input("Enter Loc\n")
City=input("Enter City\n")
data=[ Name, Location, City]
record.append(_____) #Statement-4
ch=input("insert more record(y/n):")
if ch=='n':
break
abc.______________(record) #Statement-5

a. Identify the suitable code for blank space in line marked as Statement-1. csv
b. Identify the missing code for blank space in line marked as Statement-2? 'Stud.csv', ‘w’
c. Write the function name (with argument) that should be used in the blank space of line marked as
Statement-3. csv.writer (csv1)
d. Identify the suitable code for blank space in the line marked as Statement-4. data
e. Write the function name that should be used in the blank space of line marked as Statement-6 to
create the desired CSV File? writerows

22. Consider the following tables Games and answer the following questions

a. Which column will be suitable to choose as a Primary Key? Justify your answer.
Gcode is suitable for primary key as it is a Games table and GCode is unique for each game
b. Write a command to create the above table structure.
CREATE TABLE GAMES(GCode int, GameName varchar(20),
Type varchar(10), Number int,
PrizeMoney int, ScheduleDate date);
c. Write a command to add a column Date_Of_Registration with the data type date.
ALTER TABLE GAMES ADD Date_Of_Registration date;
d. Write a command to insert the data (104, ‘Shuttle cock’, ‘Indoor’, 2, 10000, ‘2004-01-21’)
Insert into GAMES values (104, ‘Shuttle cock’, ‘Indoor’, 2, 10000, ‘2004-01-21’)
e. Write a command to increase the price money by 1000 for all the Indoor games.
7
UPDATE GAMES SET PrizeMoney = PrizeMoney + 1000 WHERE Type = ‘Indoor’;

********
MAHATMA MONTESSORI SCHOOL SET B
(Affiliated to Central Board of Secondary Education)
CYCLE TEST V-ANSWER KEY Marks: 50
STD:XII COMPUTER SCIENCE (Code: 083) Time: 2 Hrs
I. Choose the best answers: (5 X 1 = 5)
1. The ____ allows us to perform tasks related to changing the values of the record in a table.
b. DML
2. Mr. James created a table CLIENT with 2 rows and 4 columns. He added 2 more rows to it and deleted one
column. What is the Cardinality and Degree of the resultant Table CLIENT? d. Degree 3, Cardinality 4
3. _____ command helps us to view the structure of the table. d. Either b or c
4. CSV stands for _____. c. Comma Separated Values
5. An SQL Clause is made up of _______ . c. Keywords and Arguments

II. Answer the following briefly: (10 X 2 = 20)


6. Write the advantages of CSV file.
Advantages of CSV files are as follows:
. CSV is easy to generate and import onto a spreadsheet or database
• CSV is faster to handle and smaller in size.
• CSV is human readable and applications easy to edit manually.
• CSV is capable of storing large amount of data
• CSV is processed by almost all existing

7. Write short notes on writing to a CSV file.


Csv.writer() function is used to write to a csv file. This function returns a writer object that
Converts user’s data into a delimited string.
Writerobj = csv.writer(f, delimiter = ‘any character’)
These stringx cn be written into csv files using the following 2 functions.
3. writerow() – To write one row at a time from the list using loop and iteration
4. writerows() – To write all the rows in one go without using loop/iteration

8. What are the advantages of DBMS?


1. Elimination of Data Redundancy:
. 2. Data Consistency:
3. Sharing of Data:
4. Reduced Programming Effort:
5. Database Enforces Standards
6. Improved Data Integrity:
7. Privacy and Security:
8
8. Economical:
9. Improved Backup and Recovery System:
10. Meeting Enterprise Requirements than Individual Requirements:
9. What do you mean by attributes and tuples in terms of relational database? Give example.
Attribute: In a relational table, an attribute is a set of values of a particular type. The term
attribute is also used to represent a column. A set of attributes defines property of entity.

Tuple: Each row in a table is known as tuple. It is also called a row/record. A single entry
in a table is called a record or row.
Example:-

.
10. Write the difference between Alter and Update commands.
ALTER – It is a DDL command. It is used to modify the definition/structure of a table.
Using Alter command we can add, remove, change the data type/size, rename
a column.
UPDATE – It is a DML command. It is used to modify the values in the records in a
table.

11. What is the difference between primary and candidate key?


Primary Key Primary key is a set of one or more fields/columns of a table that uniquely
identifies a record in a database table. It cannot accept null, duplicate values.
Only one Candidate Key can be the Primary Key.

Candidate Key: A candidate key refers to all the attributes in a relation that are unique
or are capable of becoming a primary key. There can be more than one
candidate key

12. Write one similarity and one difference between Decimal and Numeric data types.
SIMILARITY – Both the data types are used to define numbers with decimal places
DIFFERENCE -

9
13. Write MySQL command for the following.
(i) to list down the exiting tables in an opened database. SHOW TABLES;
(ii) to create a database named “CONTACTS” CREATE DATABASE CONTACTS;

14. Write SQL query to create a table ‘Song’ with the following structure
Table Song

ANS:
CREATE TABLE Song(Songid int Primry Key, Title Varchar(50), Duration int, ReleaseDate Date);

15. What are the classification of SQL commands? Name the commands under each classification.

10
III. Answer the following in detail: (5X 3 = 15)

16. Write short notes on reading from a CSV file.


Reading from a CSV file is done using the reader object.
Reader object is an iterable object that gives access to each line of the CSV file as a list of fields.
next() function is used to read the next line of the CSV file.
Before reading from a CSV file, the file should exist. It can be either created directly in
MSExcel or through a python program we can write into a file.

17. What is a data type? Name any 5 data types available in MySQL. Write the difference between
Numeric and Decimal data types with examples.

In SQL, each column of the table is assigned a data type which conveys the kind of value that
will be stored in the column.
Data types available in MySQL – integer, float, numeric, decimal, char, varchar, date, time

11
18. What are the different ways to insert records in table? Explain with syntax and examples.

Inserting Data into a Table

The INSERT INTO command is used to insert a new record/row/tuple in a table.


It is possible to write the INSERT INTO statement in the following different forms:
(a) Inserting data (for all the columns) into a table: In the first method, it does not specify
the column names where the data will be inserted, only their values.
Syntax for SQL INSERT is:
INSERT INTO table_name VALUES (value1, value2, value3...);
For example, INSERT INTO student VALUES (1,“Raj Kumar”, ‘M’, 93, ‘2000-11-17’);

(b) Inserting data directly into a table: The second form specifies both the column names
and the values to be inserted.
Syntax: INSERT INTO table_name (column1,column2,columnN,...)
 VALUES (value1,value2,valueN,...);
Here, column1, column2, ...columnN—the names of the columns in the table for which you
want to insert data.
For example, INSERT INTO student(RollNo, Name, Gender, Marks, DOB)
VALUES(2,‘Deep Singh’, ‘M’, 98, ‘1996-08-22’);

12
(c) Inserting data into specific columns of a table:
Syntax for SQL INSERT is:
INSERT INTO <table_name>[(column1, column2, ... columnN)]
values [(value1, value2,.... valueN)];
For example, to insert a record into the student table for the columns Rollno, Name and
Marks only, the SQL insert query is:
INSERT INTO student (Rollno, Name, Marks) values (4,”Radhika Gupta”,78);

(d) Inserting NULL values into a table


Null is not equivalent to 0, i.e., NULL ≠ 0. It acts as a placeholder for unknown or inapplicable
values.

For example, INSERT INTO student(Rollno, Name, Gender, Marks, DOB)


VALUES(12,‘Swati Mehra’, ‘F’, NULL, NULL);

19. Can you add more than one column in an existing table? If yes, explain.
Yes we can add more than one column in an existing table as follows.
Alter table tablename add column1 datatype1, column2 datatype2…..;
Ex., Alter table STUDENT ADD grade char(2), DOB date;

20. What is a keyword, clause and statement/command in terms of SQL. Explain with example.
A keyword refers to an individual SQL element that has a special meaning in SQL.
For example, SELECT and FROM are keywords.
A clause is a distinct logical part of an SQL statement. Clausesbegin with a keyword for which
they are named and consist of arguments as well.
For example, SELECT empno, ename, FROM employee WHERE salary>45000, are clauses in SQL.
Here, arguments complete or modify the meaning of a clause, which is salary in the given example.
A statement or command is a combination of two or more clauses. Statements are basically
the instructions given to SQL database for executing any task.
For example, SELECT * FROM employee is an SQL statement.
An important point to remember here is that all the statements in SQL terminate with a semi-colon (;).
Also, SQL is not case sensitive; therefore, we can type commands in either upper case or lower case.

IV. Case study questions: (2 X 5 = 10)


21. Latika is making software on “Items & their prices” in which various records are to be
stored/retrieved in STORE.CSV data file. It consists some records (Item &Price). She has
written the following code in python. As a programmer, you have to help her to successfully
execute the program.
import___________ #Statement-1

def AddItem(Item,Price): #Statement-2

13
f=open(“STORE.CSV”,_________) #Statement-3

fw=csv.writer(f)

fw.writerow(_____________) #Statement-4

f.close()

def ShowRecord():

with open(“STORE.CSV”,”r”) as NI:

NewItem=csv.___________(NI) #Statement-5

for rec in NewItem:

print(rec[0],“#”,rec[1])

AddItem(“Sugar”,38.0)

AddItem(“Rice”,48.50)

ShowRecord() #Statement-6

a. Which module should be imported in Statement-1. csv

b. Which file mode to be passed to add new record inStatement-3. ‘a’

c. Fill in the blank in Statement-4 to write the item and its corresponding price into the file?

[Item, Price]

d. Which function to be used in Statement-5 to read the data from a csvfile. reader(NI)

e. The output after executing Statement-6 will be ________ Sugar#38.0


Rice#48.50
22. Consider the following tables Employee and answer the following questions.

TABLE: EMPLOYEE

14
a. Which column will be suitable to choose as a Primary Key? Justify your answer.
Ecode is suitable for primary key as it is a EMPLOYEE table and ECode is unique for each employee
b. Write a command to create the above table structure.
CREATE TABLE EMPLOYEE(ECODE INT, NAME VARCHAR(30),
DESIG VARCHAR(20), SGRADE VARCHAR(10),
DOJ DATE, DOB DATE);
c. Write a command to add a column Basic_Salaty with the data type integer.
ALTER TABLE EMPLOYEE ADD BASIC_SALARY INT;
d. Write a command to insert the data
(104, ‘Baradhwaj’, ‘Clerk’, ‘S07’, ‘2004-01-21’, ‘1985-05-31’)
INSERT INTO EMPLOYEE VALUES(104, ‘Baradhwaj’, ‘Clerk’, ‘S07’, ‘2004-01-21’,
‘1985-05-31’);
e. The employee Mr. Ravi Chander is promoted as an Executive. How will you write a
command to change his Designation in the above table.
UPDATE EMPLOYEE
SET DESIG = ‘EXECUTIVE’
WHERE NAME = ‘RAVI CHANDER’;

********

15

You might also like