mysql important questions
mysql important questions
Answer
Question 2
What is a constraint ? Name some constraints that you can apply to enhance database integrity.
Answer
1. Unique constraint.
3. Default constraint.
4. Check constraint.
Question 3
What is the role of UNIQUE constraint ? How is PRIMARY KEY constraint different from UNIQUE
constraint ?
Answer
UNIQUE constraint ensures that no two rows have the same value in the specified column(s).
There are differences between UNIQUE and PRIMARY KEY constraints. 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 4
Answer
A primary key is a unique identifier for each record in a table, and it must be unique and not null. A
PRIMARY KEY constraint declares a column or one group of columns as the primary key of the table.
This constraint must be applied to columns declared as NOT NULL.
Question 5
Answer
The NOT NULL constraint is used in SQL to ensure that a column cannot contain NULL (i.e., empty)
values.
A DEFAULT constraint is used in SQL to specify a default value for a column in a table. When the user
does not enter a value for the column (having default value), automatically the defined default value
is inserted in the field.
Question 6
When a column's value is skipped in an INSERT command, which value is inserted in the database ?
Answer
The columns that are not listed in the INSERT command will have their default value, if it is defined
for them, otherwise, NULL value. If any other column (that does not have a default value and is
defined NOT NULL) is skipped or omitted, an error message is generated and the row is not added.
Question 7
Can a column defined with NOT NULL constraint, be skipped in an INSERT command ?
Answer
If a column defined with a NOT NULL constraint is skipped in an INSERT command, it will result in an
error, and the row will not be added to the table.
Question 8
Answer
To view the structure of a table in SQL, we use DESC[RIBE] command of MySQL * Plus. The syntax of
this command is as follows : DESC[RIBE] <tablename> ;.
For example, the command to view the structure of table Dept is DESCRIBE Dept ; or DESC Dept ;.
Question 9
Table Empl has same structure as that of table EMPL. Write a query statement to insert data from
table NewEmpl into EMPL where salary and comm is more than Rs. 4000.
Answer
SELECT *
FROM NewEmpl
Question 10
UPDATE EMPL ;
Answer
The error in the statement UPDATE EMPL; is that it is incomplete. The UPDATE command specifies
the rows to be changed using the WHERE clause and the new data using the SET keyword. Therefore,
the statement is incomplete as it lacks both the SET and WHERE clauses.
Question 11
Answer
The statement DELETE ALL FROM TABLE EMPL; is in error due to the misuse of the keyword ALL and
the unnecessary inclusion of TABLE before the table name. In SQL, the syntax of DELETE statement is
:
[ WHERE <PREDICATE> ] ;
According to this syntax, the correct command to remove all the contents of EMPL table is :
Question 12
Answer
Examples of DDL commands are CREATE, ALTER, DROP, GRANT, Examples of DML commands are
ANALYZE etc. INSERT, UPDATE, DELETE, SELECT etc.
Question 1
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.
Question 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.
Question 3
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.
Question 4
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.
Question 5
1. Table
2. Values
3. Relation
4. Field
Answer
Values
According to this syntax, Values keyword is used to specify the values that will be inserted into the
specified columns of the table.
Question 6
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.
Question 7
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.
Question 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.
Question 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.
Question 10
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 ;.
Question 11
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.
Question 12
Which command is used for removing a table and all its data from the database :
4. All of these
Answer
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.
Question 13
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.
Question 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.
Question 1
Question 2
To list the names of existing database, you can use SHOW DATABASES command.
Question 3
Question 4
Question 5
Question 6
To increase the size of a column in an existing table, use command ALTER TABLE.
Question 7
To remove table data as well table structure, use command DROP TABLE.
Question 8
To define a column as a primary key, primary key constraint is used in CREATE TABLE.
True/False Questions
Question 1
Answer
True
Reason — In SQL, 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.
Question 2
False
Reason — Constraints can be defined with the CREATE TABLE command in SQL, but they can also be
added or altered later using the ALTER TABLE command. The ALTER TABLE command allows to modify
an existing table by adding, modifying, or dropping columns, as well as adding an integrity
constraints.
Question 3
Answer
False
Reason — 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 4
Answer
False
Reason — DELETE from <table> command is used to remove all the contents from the table, leaving
it empty. On the other hand, the DROP TABLE command is used to delete the table from the
database along with all its data and structure.
Question 5
Answer
True
Reason — Conditional updates in table data are possible through the UPDATE command in SQL. The
UPDATE command specifies the rows to be changed using the WHERE clause and the new data using
the SET keyword. This enables to perform updates selectively based on specific conditions defined in
the WHERE clause.
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)
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)
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)
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
Reason. The UPDATE command as well ALTER TABLE command make changes in the table.
Answer
(e)
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.
Question 1
What are different divisions of SQL and commands ? Give examples of commands in each division.
Answer
1. Data Definition Language (DDL) commands — CREATE, ALTER, DROP, TRUNCATE etc.
Question 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.
Question 3
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.
Question 4
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.
Question 5
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.
Question 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.
Question 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.
Question 8(i)
Answer
This command is used to delete a specific table This command is used to delete an entire database
from the database along with all its data, indexes, including all its tables, views, stored procedures,
triggers, and constraints. triggers, and other objects.
The syntax is : DROP TABLE table_name;. The syntax is : DROP DATABASE database_name;.
Question 8(ii)
Answer
DROP TABLE DROP clause of ALTER TABLE
This command is used to delete a specific table from This command is used to remove a specific
the database along with all its data, indexes, triggers, component of a table, such as columns,
and constraints. constraints, or indexes.
Question 1
Insert all those records of table Accounts into table Pending where amt_outstanding is more than
10000.
Answer
Question 2
Answer
Table employee
UPDATE employee
Output
To view all the details (all columns and rows) of the "employee" table the below query is executed :
SELECT * FROM employee ;
+----+------------+-----------+----------+--------+
+----+------------+-----------+----------+--------+
+----+------------+-----------+----------+--------+
Question 3
Give commission of Rs.500 to all employees who joined in year 1982 (table Empl).
Answer
Table Empl
UPDATE Empl
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.
Question 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
Question 5
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
Question 6
Answer
SHOW TABLES ;
Question 7
Write Query statements for following transaction : (Consider tables of question 12)
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
1.
UPDATE product
2.
SELECT *
FROM Orders
3.
UPDATE Product
SELECT ProdNo#
FROM Orders
);
Question 8
Modify table Empl, add another column called Grade of VARCHAR type, size 1 into it.
Answer
Question 9
Answer
UPDATE Empl
UPDATE Empl
UPDATE Empl
UPDATE Empl
Question 10
Add a constraint (NN-Grade) in table Empl that declares column Grade not null.
Answer
Question 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)
Question 12
Answer
Question 13
Answer
Question 14
Create the table Department table based on the following table instance chart.
Length 8 25
Answer
ID NUMBER(8),
Name VARCHAR(25)
);
Question 15
Populate the table Department with data from table dept. Including only required columns.
Answer
FROM dept ;
Question 16
Create the table Employee based on the following table instance chart.
ID NUMBER 8
First_Name VARCHAR 25
Last_Name VARCHAR 25
Dept_ID NUMBER 8
Answer
ID NUMBER(8),
First_Name VARCHAR(25),
Last_Name VARCHAR(25),
Dept_ID NUMBER(8)
);
Question 17
Answer
Question 18
Cust_ID NUMBER 7
Cust_Name VARCHAR 30
Cust_Address1 VARCHAR 20
Cust_Address2 VARCHAR 30
Pincode NUMBER 6
Cust_Phone VARCHAR 10
Answer
Cust_ID NUMBER(7),
Cust_Name VARCHAR(30),
Cust_Address1 VARCHAR(20),
Cust_Address2 VARCHAR(30),
Pincode NUMBER(6),
Cust_Phone VARCHAR(10)
);
Question 19
Add one column Email of data type VARCHAR and size 30 to the table Customer.
Answer
Question 20
Answer
Question 21
VALUES
(11, 'Amit', '1st Main Street', 'Mumbai', 12345, '5551234121', '[email protected]', 'High'),
(24, 'Vidya', '4th Main Street', 'Bangalore', 54321, '5234325678', '[email protected]', 'Medium'),
(39, 'Amruta', '78th Main Street', 'Goa', 98765, '5976539012', '[email protected]', 'Low');
Question 22
Answer
Question 23
Length 2 20
Answer
);
Question 24
Fk Table Department
Fk Column Dept_ID
Length 6 20 30 10 9, 2 2
Answer
EmpAddress VARCHAR(30),
EmpPhone VARCHAR(10),
DeptID VARCHAR(2),
);
Question 25
Answer
DESCRIBE <TABLENAME> ;