Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
29 views21 pages

Ch-12 Table Creation and Data Manipulation Commands

The document is a question bank for Computer Science students at St. Britto's Academy, focusing on SQL commands related to table creation and data manipulation. It includes multiple choice questions, fill-in-the-blank exercises, assertions with reasons, and application-based questions, covering topics such as DDL, DML, constraints, and the differences between various SQL commands. The content is structured to aid students in understanding and applying SQL concepts effectively.

Uploaded by

vbecky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views21 pages

Ch-12 Table Creation and Data Manipulation Commands

The document is a question bank for Computer Science students at St. Britto's Academy, focusing on SQL commands related to table creation and data manipulation. It includes multiple choice questions, fill-in-the-blank exercises, assertions with reasons, and application-based questions, covering topics such as DDL, DML, constraints, and the differences between various SQL commands. The content is structured to aid students in understanding and applying SQL concepts effectively.

Uploaded by

vbecky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 21

ST BRITTO’S ACADEMY – 2024-25

STD XII - COMPUTER SCIENCE – QUESTION BANK


CHAPTER 12 – TABLE CREATION AND DATA MANIPULATION COMMANDS

I.Multiple Choice Questions


1.Consider the following SQL statement. What type of statement is this ?

CREATE TABLE employee (name VARCHAR, id INTEGER)


1. DML
2. DDL
3. DCL
4. Integrity constraint
Answer

DDL

Reason — The SQL statement CREATE TABLE employee (name VARCHAR, id INTEGER) is
a Data Definition Language (DDL) statement. DDL statements are used to define, modify,
and delete database objects such as tables, views, indexes, etc. In this case, the statement
is creating a new table named employee with columns, name of type VARCHAR and id of
type INTEGER.

2.The data types CHAR(n) and VARCHAR(n) are used to create ..............., and ............... length
types of string/text fields in a database.
1. Fixed, equal
2. Equal, variable
3. Fixed, variable
4. Variable, equal
Answer

Fixed, variable
Reason — CHAR datatype specifies a fixed length string. Defining a length is not required,
but the default is 1. While VARCHAR datatype specifies a variable length string. Defining a
length is required.

3. A table Table1 has two text fields defined as below :

Name1 varchar(20),
Name2 char(20),

If Name1 stores value as 'Ana' and Name2 stores value as 'Anuj', then Name1 will
consume .............. characters' space and Name2 will consume ............... characters' space.

1. 3, 20
2. 20, 4
3. 20, 20
4. 3, 4
Answer

3, 20

Reason — For the field Name1 with VARCHAR(20) datatype, storing the value 'Ana' will
consume 3 character's space because VARCHAR(n) column can have a maximum size of n
bytes and it stores values exactly as specified without adding blanks for shorter lengths.
Exceeding n bytes results in an error message. Whereas for the
field Name2 with CHAR(20) datatype, storing the value 'Anuj' will consume 20 characters'
space because CHAR(n) ensures that all values stored in that column are of length n bytes,
padding shorter values with blanks while maintaining a fixed size of n bytes.

4.Consider the following SQL statement. What type of statement is this ?

INSERT INTO instructor VALUES (10211, 'Shreya' , 'Biology', 66000 ) ;


1. Procedure
2. DML
3. DCL
4. DDL
Answer

DML

Reason — The above SQL statement is Data Manipulation Language (DML) statement.
DML statements are used to access and manipulate data in tables. The DML commands
include SELECT, LOCK TABLE, UPDATE, INSERT INTO, DELETE. In this case, the INSERT
INTO statement is used to insert a new row of data into the instructor table.

5.In the given query which keyword has to be inserted ?

INSERT INTO employee ............... (1002, Kausar, 2000) ;


1. Table
2. Values
3. Relation
4. Field
Answer

Values

Reason — The syntax of INSERT INTO command is :


INSERT INTO <tablename> [<column List>]
Values (<value>, <value> ...............) ;
According to this syntax, Values keyword is used to specify the values that will be inserted
into the specified columns of the table.

6.Which of the following is/are the DDL statements ?

1. Create
2. Drop
3. Alter
4. All of these
Answer

All of these

Reason — DDL (Data Definition Language) commands are used to create and define
tables and other database objects in SQL (Structured Query Language). DDL commands
such as CREATE, ALTER, and DROP, are used to create, define, change and delete objects
like tables, indexes, views, and constraints.
7. In SQL, which command(s) is(are) used to change a table's structure/characteristics ?

1. ALTER TABLE
2. MODIFY TABLE
3. CHANGE TABLE
4. All of these
Answer

ALTER TABLE

Reason — The ALTER TABLE command in SQL is used to change the definitions of
existing tables. It allows for various operations such as adding a new column, redefining a
column, and adding an integrity constraint. Therefore, it changes the structure of the table.
8.Which of the following commands will delete the table from MYSQL database ?

1. DELETE TABLE
2. DROP TABLE
3. REMOVE TABLE
4. ALTER TABLE
Answer

DROP TABLE

Reason — The DROP TABLE command in SQL will delete the table from the MYSQL
database. Once this command is executed, the table and all its associated data are removed
from the database. After dropping the table, the table name is no longer recognized within
the database system, and no further commands can be executed on that object.

9................ defines rules regarding the values allowed in columns and is the standard
mechanism for enforcing database integrity.

1. Column
2. Constraint
3. Index
4. Trigger
Answer

Constraint
Reason — Constraint defines rules regarding the values allowed in columns and is the
standard mechanism for enforcing database integrity. Once an integrity constraint is
enabled, all data in the table must confirm to the rule that it specifies.

10................ command is used to remove primary key from a table in SQL.

1. update
2. remove
3. alter
4. drop
Answer

alter

Reason — To remove a primary key constraint from a table the ALTER command is used.
The DROP clause of ALTER TABLE command is used with syntax ALTER TABLE
<TABLENAME> DROP PRIMARY KEY ;.

11.Which command defines its columns, integrity constraint in create table ?

1. Create table command


2. Drop table command
3. Alter table command
4. All of these
Answer

Create table command

Reason — The CREATE TABLE command is used to define a new table in SQL, and it
allows to define the columns of the table along with any integrity constraints such as
primary keys, foreign keys, unique constraints, etc.

12.Which command is used for removing a table and all its data from the database ?

1. Create table command


2. Drop table command
3. Alter table command
4. All of these
Answer
Drop table command

Reason — The DROP TABLE command is used to delete a table and all its data from the
database. Once this command is given, the table name is no longer recognized and no
more commands can be given on the object.

13.Which of the following is not a DDL command ?

1. UPDATE
2. TRUNCATE
3. ALTER
4. None of these
Answer

UPDATE

Reason — Data Definition Language (DDL) statements are used to define, modify, and
delete database objects such as tables, views, indexes, etc. The DDL commands are
CREATE, ALTER, TRUNCATE, DROP etc. But the UPDATE command is Data Manipulation
Language (DML) command, used to modify existing data in a table.

14.Which of the following is not a legal constraint for a CREATE TABLE command ?

1. Primary key
2. Foreign key
3. Unique
4. Distinct
Answer

Distinct

Reason — The legal constraints for a CREATE TABLE command include the Primary key
constraint, Foreign key constraint, Unique constraint, Check constraint, Default constraint.
However, the Distinct is not a valid option for a CREATE TABLE command.
II.Fill in the Blanks

1.A database can be opened with USE <database> command.

2.To list the names of existing database, you can use SHOW DATABASES command.

3.A constraint is a condition or check applicable on a field or a set of fields.

4.The REFERENCES constraint creates a foreign key.

5.Issue COMMIT command to make changes to a table permanent.

6.To increase the size of a column in an existing table, use command ALTER TABLE.

7.To remove table data as well table structure, use command DROP TABLE.

8.To define a column as a primary key, primary key constraint is used in CREATE TABLE.
III.Assertions and Reasons

Question 1

Assertion. The PRIMARY KEY and UNIQUE constraints are the same.

Reason. The columns with PRIMARY KEY or UNIQUE constraints have unique values for
each row.

Answer

(d)

Assertion is false but Reason is true.

Explanation
UNIQUE and PRIMARY KEY constraints are not the same and there are differences
between them. Though both ensure unique values for each row in a column, but UNIQUE
allows NULL values whereas PRIMARY KEY does not. Also, there can exist multiple
columns with UNIQUE constraints in a table, but there can exist only one column or one
combination with PRIMARY KEY constraint.

Question 2

Assertion. The treatment of NULL values is different with PRIMARY KEY and UNIQUE
constraints.

Reason. The column(s) with PRIMARY KEY do not allow the NULL value even in a single
row but UNIQUE constraint allows NULL for one of the rows.

Answer

(a)

Both Assertion and Reason are true and Reason is the correct explanation of Assertion.

Explanation
The treatment of NULL values is different with PRIMARY KEY and UNIQUE constraints.
The UNIQUE constraint allows NULL values for one of the rows, while the PRIMARY KEY
does not allow the NULL value in any row.
Question 3

Assertion. There is no restriction on the number of columns that can have PRIMARY KEY
constraint or UNIQUE constraints.

Reason. There can be multiple columns with UNIQUE constraint but PRIMARY KEY
constraint can be defined only once for one or more columns.

Answer

(d)

Assertion is false but Reason is true.

Explanation
There can exist multiple columns with UNIQUE constraints in a table, but there can exist
only one column or one combination with PRIMARY KEY constraint.

Question 4

Assertion. There are different commands for creating and changing table design.

Reason. The CREATE TABLE command creates the tables while ALTER TABLE command
changes the design of an existing table.

Answer

(a)

Both Assertion and Reason are true and Reason is the correct explanation of Assertion.

Explanation
The CREATE TABLE command is used to create tables in a database, specifying the table's
structure, including column names, data types, and constraints. Conversely, the ALTER
TABLE command is used to modify the structure of an existing table, such as adding,
removing, or modifying columns, constraints, or indexes.
Question 5

Assertion. Both DELETE and DROP TABLE carry out the same thing — deletion in tables.

Reason. The DELETE command deletes the rows and DROP TABLE deletes the whole
table.

Answer

(d)

Assertion is false but Reason is true.

Explanation
The DELETE command removes rows from a table while leaving the table structure intact.
It does not delete the entire table, instead, it removes specific rows within it. On the other
hand, the DROP TABLE command in SQL deletes a table from the database, including its
structure and all its data. So, while both commands involve deletion, they operate on
different levels: the DELETE command removes rows, while the DROP TABLE command
removes the entire table.

Question 6

Assertion. Both UPDATE and ALTER TABLE commands are similar.

Reason. The UPDATE command as well ALTER TABLE command make changes in the
table.

Answer

(e)

Both Assertion and Reason are false.

Explanation
The UPDATE command specifies the rows to be changed using the WHERE clause and the
new data using the SET keyword. On the other hand, the ALTER TABLE command is used
to change the definitions of existing tables. It can add columns, integrity constraints, and
redefine columns. While both commands involve making changes, they operate on
different aspects of the table - UPDATE command modifies data, while ALTER TABLE
command modifies the table structure.
Type A: Short Answer Questions/Conceptual Questions
1.What are different divisions of SQL and commands ? Give examples of commands in each
division.

Answer

SQL commands can be divided into following categories :

1. Data Definition Language (DDL) commands — CREATE, ALTER, DROP, TRUNCATE


etc.
2. Data Manipulation Language (DML) commands — INSERT, UPDATE, DELETE etc.
3. Transaction Control Language (TCL) commands — COMMIT, ROLLBACK,
SAVEPOINT etc.
4. Session Control Commands
5. System Control Commands
2.What is foreign key ? How do you define a foreign key in your table ?

Answer

A non-key attribute, whose values are derived from the primary key of some other table, is
known as foreign key in its current table. Defining a foreign key in a table involves
specifying the relationship between the tables and setting up rules for data integrity.
When two tables are related by a common column or set of columns, the related column(s)
in the parent table (or primary table) should be either declared as a primary key or unique
key. Meanwhile, the related column(s) in the child table (or related table) should have a
foreign key constraint referencing the primary or unique key in the parent table.

3.How is FOREIGN KEY commands different from PRIMARY KEY command ?

Answer

The PRIMARY KEY is a set of one or more attributes that can uniquely identify tuples
within the relation. A primary key column cannot contain NULL values, and it must have
unique values for each row. Only one primary key constraint can exist per table.
Conversely, the FOREIGN KEY command establishes a relationship between two tables by
linking a column or set of columns in one table to the primary key or a unique key in
another table. It enforces referential integrity, ensuring that values in the foreign key
column(s) of the referencing table match values in the referenced table's primary key or
unique key column(s). A foreign key can allow NULL values, indicating that the
relationship is optional. Multiple foreign key constraints can exist in a table, each
referencing a different parent table.
4.How is FOREIGN KEY commands related to the PRIMARY KEY ?

Answer

FOREIGN KEY commands establish relationships between tables by linking columns in one
table to the PRIMARY KEY or a unique key in another table. This linkage ensures
referential integrity, meaning that values in the FOREIGN KEY column(s) of the
referencing table must match values in the PRIMARY KEY or unique key column(s) of the
referenced table. Therefore, FOREIGN KEY commands are directly related to PRIMARY
KEY commands as they rely on the unique identification provided by PRIMARY KEY
constraints in other tables.

5.How do you enforce business rules on a database ?

Answer

Database constraints enforce business rules on a database. These include PRIMARY KEY
for unique identifiers, FOREIGN KEY for maintaining relationships between tables,
UNIQUE for ensuring uniqueness, CHECK constraint limit values that can be inserted into
a column of a table, and default constraints are utilized to specify a default value for a
column when no value is explicitly provided during an insert operation.

6.What are table constraints ? What are column constraints? How are these two different ?

Answer

Table constraints are rules or conditions applied to an entire table in a database. They are
defined when creating or altering a table's schema.

Column constraints are rules or conditions applied to individual columns within a


database table. They are specified at the column level when creating or altering a table's
schema.

The difference between the two is that column constraints apply only to individual
columns, whereas table constraints apply to groups of one or more columns.

7.What is default value ? How do you define it ? What is the default value of column for
which no default value is define ?

Answer

A default value is a predefined value assigned to a column in a database table. It can be


specified using the DEFAULT clause when defining the table's schema. If no default value
is defined for a column, and a new row is inserted into the table without providing a value
for that column, the column's default value will be NULL, unless the column is defined
with a NOT NULL constraint. In such cases, an error will occur if a value is not provided.
8.Differentiate between DROP TABLE, DROP DATABASE.

Answer

DROP TABLE DROP DATABASE

This command is used to


delete a specific table from the This command is used to delete an entire database
database along with all its including all its tables, views, stored procedures,
data, indexes, triggers, and triggers, and other objects.
constraints.

The syntax is : DROP TABLE


The syntax is : DROP DATABASE database_name;.
table_name;.

9.Differentiate between DROP TABLE, DROP clause of ALTER TABLE.

Answer

DROP TABLE DROP clause of ALTER TABLE

This command is used to delete a


This command is used to remove a specific
specific table from the database along
component of a table, such as columns,
with all its data, indexes, triggers, and
constraints, or indexes.
constraints.

The syntax is : DROP TABLE The syntax is : ALTER TABLE table_name DROP
table_name; COLUMN column_name;
Type B: Application Based Questions
1.Insert all those records of table Accounts into table Pending where amt_outstanding is
more than 10000.

Answer

INSERT INTO Pending SELECT * FROM Accounts WHERE amt_outstanding > 10000;

2.Increase salary of employee records by 10% (table employee).

Answer

Table employee

ID First_Name Last_Name User_ID Salary

1 Dim Joseph Jdim 5000

2 Jaganath Mishra jnmishra 4000

3 Siddharth Mishra smishra 8000

4 Shankar Giri sgiri 7000

5 Gautam Buddha bgautam 2000

UPDATE employee SET Salary = (Salary * 0.1) + Salary ;


3.Give commission of Rs.500 to all employees who joined in year 1982 (table Empl).

Answer

Table Empl

DEPT
EMPNO ENAME JOB MGR HIREDATE SAL COMM
NO

1990-12-
8369 SMITH CLERK 8902 800 NULL 20
18

1991-02-
8499 ANYA SALESMAN 8698 1600 300 30
20

1991-02-
8521 SETH SALESMAN 8698 1250 500 30
22

1991-04-
8566 MAHADEVAN MANAGER 8839 2985 NULL 20
02

1991-09-
8654 MOMIN SALESMAN 8698 1250 1400 30
28

1991-05-
8698 BINA MANAGER 8839 2850 NULL 30
01

1991-11-
8839 AMIR PRESIDENT NULL 5000 NULL 10
18

1991-09-
8844 KULDEEP SALESMAN 8698 1500 0 30
08

1991-06-
8882 SHIAVNSH MANAGER 8839 2450 NULL 10
09

8886 ANOOP CLERK 8888 1993-01- 1100 NULL 20


DEPT
EMPNO ENAME JOB MGR HIREDATE SAL COMM
NO

12

1992-12-
8888 SCOTT ANALYST 8566 3000 NULL 20
09

1991-12-
8900 JATIN CLERK 8698 950 NULL 30
03

1991-12-
8902 FAKIR ANALYST 8566 3000 NULL 20
03

1992-01-
8934 MITA CLERK 8882 1300 NULL 10
23

UPDATE Empl SET COMM = 500 WHERE YEAR(HIREDATE) = 1982;

Explanation

Since there are no employees who joined in the year 1982 according to the data provided
in the "Empl" table, executing the given SQL query will result in no changes to the "COMM"
column.

4. Allocate the department situated in BOSTON to employee with employee number 7500
(tables EMPL, Dept)

Answer

UPDATE EMPL SET DEPTNO = ( SELECT DEPTNO FROM Dept WHERE LOC = 'BOSTON')
WHERE EMPNO = 7500;
5.Given the following tables :

Orders (OrdNo, Ord_date, ProdNo#, Qty)


Product (ProdNo, Descp, Price)
Payment (OrdNo, Pment)
Write a query to delete all those records from table Orders whose complete payment has
been made.

Answer

DELETE FROM Orders WHERE OrdNo IN (SELECT Payment.OrdNo FROM Payment


WHERE Payment.Pment = 'COMPLETE');

6.Enlist the names of all tables created by you.

Answer

SHOW TABLES ;

7.Write Query statements for following transaction : (Consider tables of question 12)

1. Increase price of all products by 10%.


2. List the details of all orders whose payment is pending as per increased price.
3. Decrease prices by 10% for all those products for which orders were placed 10
months before.
Answer

The following tables are considered :

Orders (OrdNo, Ord_date, ProdNo#, Qty)


Product (ProdNo, Descp, Price)
Payment (OrdNo, Pment)
1.

UPDATE product
SET price = (price * 0.1) + price ;

2.

SELECT *
FROM Orders
JOIN Payment ON Orders.OrdNo = Payment.OrdNo
WHERE Payment.Pment = 'Pending';
3.

UPDATE Product
SET Price = Price - (Price * 0.1)
WHERE ProdNo IN (
SELECT ProdNo#
FROM Orders
WHERE YEAR(Ord_date) = YEAR(CURDATE()) - 1
AND MONTH(Ord_date) = MONTH(CURDATE()) - 10
);

8.Modify table Empl, add another column called Grade of VARCHAR type, size 1 into it.

Answer

ALTER TABLE Empl ADD (Grade VARCHAR(1)) ;

9.In the added column Grade, assign grades as follows :

if sal is in range 700 — 1500, Grade is 1 ;


if sal is in range 1500 — 2200, Grade is 2 ;
if sal is in range 2200 — 3000, Grade is 3 ;
if sal is in range 3000 — Grade is 4 ;

Answer

UPDATE Empl SET Grade = '1' WHERE Sal >= 700 AND Sal <= 1500;

UPDATE Empl SET Grade = '2' WHERE Sal > 1500 AND Sal <= 2200;

UPDATE Empl SET Grade = '3' WHERE Sal > 2200 AND Sal <= 3000;

UPDATE Empl SET Grade = '4' WHERE Sal > 3000;

10. Add a constraint (NN-Grade) in table Empl that declares column Grade not null.

Answer

ALTER TABLE Empl ADD CONSTRAINT NN_Grade (Grade NOT NULL) ;


11.Insert a record of your choice in table Empl. Make sure not to enter Grade.

Answer

INSERT INTO Empl (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES (12345, 'DEEPAK', 'CLERK', 8902, '1990-12-18', 8000, 200, 10);

12.Modify the definition of column Grade. Increase its size to 2.

Answer

ALTER TABLE Empl MODIFY (Grade VARCHAR(2)) ;

13. Drop the table Empl.

Answer

DROP TABLE IF EXISTS Empl;

14Create the table Department table based on the following table instance chart.

Column Name ID Name

Data Type NUMBER VARCHAR

Length 8 25

Answer

CREATE TABLE Department (ID NUMBER(8),Name VARCHAR(25));


15.Create the table Employee based on the following table instance chart.

Column
Data Type Length
Name

ID NUMBER 8

First_Name VARCHAR 25

Last_Name VARCHAR 25

Dept_ID NUMBER 8

Answer

CREATE TABLE Employee (ID NUMBER(8),First_Name VARCHAR(25),


Last_Name VARCHAR(25),Dept_ID NUMBER(8));

16. Drop table Employee and Department.

Answer

DROP TABLE IF EXISTS Employee ;


DROP TABLE IF EXISTS Department ;

17. View structures of all tables created by you.

Answer

DESCRIBE <TABLENAME> ;
18.Create table Customer as per following Table Instance Chart.

Column Name Data Type Length

Cust_ID NUMBER 7

Cust_Name VARCHAR 30

Cust_Address1 VARCHAR 20

Cust_Address2 VARCHAR 30

Pincode NUMBER 6

Cust_Phone VARCHAR 10

Answer

CREATE TABLE Customer (Cust_ID NUMBER(7),Cust_Name VARCHAR(30),


Cust_Address1 VARCHAR(20),Cust_Address2 VARCHAR(30),Pincode NUMBER(6),
Cust_Phone VARCHAR(10));

19.Add one column Email of data type VARCHAR and size 30 to the table Customer.

Answer

ALTER TABLE Customer ADD column Email VARCHAR(30) ;

20. Add one more column CustomerIncomeGroup of datatype VARCHAR(10).

Answer

ALTER TABLE Customer ADD column CustomerIncomeGroup VARCHAR(10);

You might also like