Exit Chapter 11 ยท ๐Ÿ“… Table Creation and Data Manipulation Commands โ€” Quiz

Playing as a guest? to earn XP and track your progress.

Question 1 of 50 0 correct so far

๐Ÿ“… Table Creation and Data Manipulation Commands Quiz

Q1.

Which SQL command is used to create a new database in MySQL?

Explanation

CREATE DATABASE is the correct DDL command to create a new database in MySQL.

Q2.

Which command is used to select and activate a database for use in MySQL?

Explanation

USE database_name; selects and activates a database for the current session.

Q3.

What will happen if you run CREATE DATABASE school; when a database named 'school' already exists?

Explanation

MySQL throws an error if you try to CREATE DATABASE with a name that already exists. Use CREATE DATABASE IF NOT EXISTS to avoid this error.

Q4.

Which clause is added to CREATE DATABASE to avoid an error if the database already exists?

Explanation

CREATE DATABASE IF NOT EXISTS database_name; creates the database only if it does not already exist, preventing an error.

Q5.

Which command permanently deletes an entire database along with all its tables and data?

Explanation

DROP DATABASE permanently removes the entire database, all its tables, and all data inside it. This action cannot be undone.

Q6.

Which SQL command shows the name of the currently active database?

Explanation

SELECT DATABASE(); returns the name of the currently selected database in the active session.

Q7.

A student writes CREATE TABLE Student (RollNo INT); but gets a 'No database selected' error. What is the most likely reason?

Explanation

MySQL requires you to first select a database using USE database_name; before creating tables. Without it, MySQL does not know where to create the table.

Q8.

Which data type should be used for a column that stores percentage marks such as 91.50?

Explanation

DECIMAL(5,2) stores exact decimal numbers with 5 total digits and 2 after the decimal point, making it ideal for precise values like 91.50.

Q9.

What is the key difference between CHAR(10) and VARCHAR(10)?

Explanation

CHAR(n) always uses exactly n characters of storage. VARCHAR(n) uses only as much storage as the actual data requires, up to n characters.

Q10.

Which constraint ensures that a column cannot store a NULL value?

Explanation

NOT NULL constraint ensures that a column must always have a value; an INSERT or UPDATE that leaves this column empty will fail.

Q11.

Which constraint ensures that all values in a column are different from each other?

Explanation

UNIQUE constraint prevents duplicate values in a column. Unlike PRIMARY KEY, a UNIQUE column can contain NULL values.

Q12.

A PRIMARY KEY constraint is equivalent to which combination of constraints?

Explanation

A PRIMARY KEY automatically enforces both NOT NULL (the column must have a value) and UNIQUE (no two rows can share the same value).

Q13.

How many PRIMARY KEY constraints can a single table have in MySQL?

Explanation

A table can have only ONE Primary Key. However, that Primary Key can span multiple columns (composite primary key).

Q14.

Which of the following is TRUE about the UNIQUE constraint?

Explanation

UNIQUE allows NULL values โ€” multiple rows can all have NULL in a UNIQUE column because NULL is not considered equal to any value, including another NULL.

Q15.

Which constraint is used to link a column in one table to the PRIMARY KEY of another table?

Explanation

FOREIGN KEY creates a referential link between a column in the child table and the PRIMARY KEY column in the parent table, enforcing Referential Integrity.

Q16.

A table Orders has a FOREIGN KEY referencing Customers(CustId). What happens if you insert a row in Orders with a CustId that does not exist in Customers?

Explanation

Referential Integrity enforced by FOREIGN KEY means the value must exist in the parent table's PRIMARY KEY column. If it does not exist, the INSERT fails with a constraint violation error.

Q17.

Which constraint automatically assigns a value to a column when no value is explicitly provided during INSERT?

Explanation

The DEFAULT constraint specifies a pre-set value that MySQL uses automatically when an INSERT statement does not provide a value for that column.

Q18.

A column is defined as City VARCHAR(20) DEFAULT 'Delhi'. A row is inserted without specifying City. What value does City get?

Explanation

When no value is provided for City during INSERT, the DEFAULT value 'Delhi' is automatically used.

Q19.

Which constraint ensures that the Marks column only accepts values between 0 and 100?

Explanation

CHECK (Marks >= 0 AND Marks <= 100) defines a condition that every value in the Marks column must satisfy before being stored.

Q20.

Which of the following correctly creates a table with RollNo as the PRIMARY KEY?

Explanation

PRIMARY KEY is correctly defined inline after the column definition: RollNo INT PRIMARY KEY. This is the standard syntax.

Q21.

What is the purpose of DML commands in SQL?

Explanation

DML (Data Manipulation Language) commands โ€” INSERT, UPDATE, DELETE โ€” are used to manipulate the actual data stored within database tables.

Q22.

Which SQL command is used to add new rows to an existing table?

Explanation

INSERT INTO table_name (columns) VALUES (values); is the correct DML command for adding new rows to a table.

Q23.

Identify the error in this INSERT statement: INSERT INTO Student VALUES (1, Priya, 91.5);

Explanation

String values in SQL must always be enclosed in single quotes: 'Priya'. Without quotes, MySQL treats it as a column name, causing an error.

Q24.

A table Employee has 5 columns. When using INSERT without specifying column names, how many values must be provided?

Explanation

When column names are not specified in INSERT, values must be provided for ALL columns in the exact order they appear in the table definition.

Q25.

Which INSERT statement correctly inserts a NULL value for the Salary column?

Explanation

NULL (without quotes) is the correct keyword to explicitly insert a missing/unknown value. An empty string '' and 0 are actual values, not NULL.

Q26.

Which SQL command is used to modify existing data in a table?

Explanation

UPDATE is the DML command used to change existing values in one or more rows of a table.

Q27.

What is the correct syntax for the UPDATE command?

Explanation

The correct syntax is: UPDATE table_name SET column_name = value [WHERE condition]; The SET keyword assigns new values and WHERE restricts which rows are affected.

Q28.

What happens when an UPDATE statement is executed without a WHERE clause?

Explanation

Without a WHERE clause, UPDATE applies the change to ALL rows in the table. This is a dangerous mistake โ€” always verify your WHERE condition before running UPDATE.

Q29.

Which query correctly increases the salary of all employees in the 'IT' department by 5000?

Explanation

To increase the existing value, you must reference the column itself: SET Salary = Salary + 5000. Writing SET Salary = 5000 would replace the value, not add to it.

Q30.

Which SQL command is used to remove specific rows from a table?

Explanation

DELETE FROM table_name [WHERE condition]; is the DML command used to remove rows. TRUNCATE removes all rows but is a DDL command, not DML.

Q31.

What is the result of DELETE FROM Employee; (without a WHERE clause)?

Explanation

DELETE FROM without WHERE removes all rows but the table structure (columns, constraints) remains intact. This is different from DROP TABLE, which removes the entire table.

Q32.

Which statement about TRUNCATE is correct?

Explanation

TRUNCATE is a DDL command that removes all rows instantly without logging each deletion. It keeps the table structure intact but is auto-committed and cannot be rolled back.

Q33.

A student wants to delete only employees who work in the 'Sales' department. Which query is correct?

Explanation

The correct syntax is DELETE FROM table_name WHERE condition; The FROM keyword is required, and no column list is specified in DELETE.

Q34.

Which is a key difference between DELETE and TRUNCATE?

Explanation

DELETE supports a WHERE clause to selectively remove rows. TRUNCATE removes ALL rows at once with no WHERE support. DELETE is DML (can be rolled back); TRUNCATE is DDL (cannot be rolled back).

Q35.

Which command is used to change the structure of an existing table in MySQL?

Explanation

ALTER TABLE is the DDL command used to modify the structure of an existing table โ€” adding/removing columns, changing data types, and managing constraints.

Q36.

Which ALTER TABLE clause is used to add a new column to an existing table?

Explanation

ALTER TABLE table_name ADD column_name datatype; is the correct syntax to add a new column. The ADD clause is used for this purpose.

Q37.

A table Student exists with column Marks INT. Which query changes its data type to DECIMAL(5,2)?

Explanation

MODIFY is used to change a column's data type or size while keeping the same column name. Syntax: ALTER TABLE table_name MODIFY column_name new_datatype;

Q38.

What is the difference between ALTER TABLE MODIFY and ALTER TABLE CHANGE?

Explanation

MODIFY changes a column's data type/size/constraints but keeps the same column name. CHANGE can do everything MODIFY does AND rename the column simultaneously.

Q39.

Which query correctly renames the column DOJ to DateOfJoining in the Employee table (keeping DATE type)?

Explanation

CHANGE is used to rename a column. Syntax: ALTER TABLE table_name CHANGE old_column_name new_column_name datatype; The data type must always be specified.

Q40.

Which clause is used with ALTER TABLE to remove a column from a table?

Explanation

ALTER TABLE table_name DROP COLUMN column_name; permanently removes the column and all its data from the table.

Q41.

What happens to the data in a column when it is removed using ALTER TABLE DROP COLUMN?

Explanation

ALTER TABLE DROP COLUMN is a DDL auto-committed operation. The column and all data stored in it are permanently deleted with no way to recover them.

Q42.

A table was created without a PRIMARY KEY. Which query adds EmpId as the PRIMARY KEY afterwards?

Explanation

ALTER TABLE table_name ADD PRIMARY KEY (column_name); is the correct syntax to add a Primary Key to an existing table.

Q43.

Which SQL command permanently deletes an entire table including its structure, data, and constraints?

Explanation

DROP TABLE removes the entire table โ€” its structure, all data, all constraints, and all indexes โ€” permanently. Unlike DELETE or TRUNCATE, the table itself no longer exists after DROP TABLE.

Q44.

After TRUNCATE TABLE Student; is executed, which of the following is TRUE?

Explanation

TRUNCATE removes all rows but the table structure remains. The empty Student table still exists and can accept new INSERT statements. It cannot be rolled back.

Q45.

How does DROP TABLE Employee; differ from DELETE FROM Employee;?

Explanation

DELETE FROM removes rows but the table structure stays intact. DROP TABLE removes everything โ€” structure, data, constraints, and indexes. The table no longer exists after DROP TABLE.

Q46.

Which of the following commands belongs to the DDL (Data Definition Language) category?

Explanation

ALTER TABLE is a DDL command because it modifies database structure. INSERT, UPDATE, and DELETE are all DML commands because they manipulate data.

Q47.

A table is defined with Salary DECIMAL(10,2) DEFAULT 0.00. The following is executed: INSERT INTO Employee (EmpId, Name) VALUES (5, 'Ravi');. What will be the value of Salary?

Explanation

Since Salary has DEFAULT 0.00, omitting it in INSERT triggers the default. The stored value will be 0.00. No error occurs because Salary is not defined as NOT NULL.

Q48.

A FOREIGN KEY in the Employee table references Department(DeptId). Which operation would violate referential integrity?

Explanation

Deleting a parent row whose PRIMARY KEY is referenced by an existing FOREIGN KEY in the child table violates referential integrity, causing a constraint error unless ON DELETE CASCADE is defined.

Q49.

Which of the following SQL statements has a syntax error?

Explanation

ALTER TABLE CHANGE requires the new column name AND the data type to be specified. CHANGE Marks Score; is missing the data type. Correct form: CHANGE Marks Score DECIMAL(5,2);

Q50.

Which of the following statements about the DROP DATABASE command is TRUE?

Explanation

DROP DATABASE permanently removes the entire database โ€” including every table, every row of data, and every constraint โ€” in a single auto-committed operation. It cannot be rolled back and does not require tables to be dropped first.