MYSQL_Question Bank with solutions
MYSQL_Question Bank with solutions
1. If a database "Employee" exists, which MySQL command helps you to start working in
that database?
Use Employee;
2. Write MySQL command will be used to open an already existing database "LIBRARY".
Use Library;
6. Sharmila wants to make the database named ‘COMPANY’ active. Write MySQL
commands for it.
Use Company;
7. What is MySQL ?
Mysql is an open source RDBMS (Relational Database Management System)
1
MySQL Worksheet- 2
1. Write an SQL query to create the table 'Menu' with the following structure:
3. In a Student table, out of Roll Number, Name, Address which column can be set as Primary
key and why?
RollNumber can be set as Primary Key as two students cannot have a same roll
number.
4. Ms. Mirana wants to remove the entire content of a table "BACKUP" alongwith its structure
to release the storage space. What MySQL statement should she use ?
Drop Table Backup;
5. Write MySQL command to create the Table STOCK including its Constraints.
Table STOCK :
7. Saumya had previously created a table named ‘Product’ in a database using MySQL. Later
on she forgot the table structure. Suggest her suitable MySQL command through which
she can check the structure of the already created table.
Describe Product;
8. Roli wants to list the names of all the tables in her database named ‘Gadgets’. Which
command (s) she should use to get the desired result.
2
Use Gadgets;
Show tables;
9. Name the SQL commands used to :
(i) Physically delete a table from the database.
(ii) Display the structure of a table.
i) Drop table tablenae;
ii) Describe tablename;
10 Write one similarity and one difference between UNIQUE and PRIMARY KEY constraints.
.
Similarity:
Both Unique and primary key restricts duplicate values in the field.
Difference:
Unique allows null values whereas Primary doesnot allow null values to be
inserted in the field.
11 An attribute A of datatype varchar(20) has the value “Amit” . The attribute B of datatype
. char(20) has value ”Karanita” . How many characters are occupied in attribute A ? How
many characters are occupied in attribute B?
A will occupy 4 character space.
B will occupy 20 character space.
12 Mrs. Sharma is the classteacher of Class ‘XII A’ She wants to create a table ‘Student’
. to store details of her class.
i) Which of the following can be the attributes of Student table?
a) RollNo b) “Amit” c) Name d) 25
ii) Name the Primary key of the table ‘Student’. State reason for choosing it.
i) RollNo and Name can be the attributes of student table.
ii) RollNo can become the primary key of the student table as two students
cannot have a same roll number.
13 Write SQL query to create a table ‘Player’ with the following structure:
.
3
(i) What is the data type of columns OrderId and OrderDate in the table Order ?
(ii) Anita is now trying to insert the following row :
i. Name the column that might have a Primary Key constraint. Justify your answer.
ii. Name the column that might have a Unique constraint. Justify your answer.
i. PanNo might have a Primary Key constraint as two person cannot have
a same Pan Number.
ii. PhoneNo might have a unique constraint as two person will be having
different mobile numbers.
17 “ABC” Event Management Company requires data of events that are to be organized.
. Write SQL query to create a table ‘Event’ with the following structure :
4
Create table Event(
EventID Integer Primary Key,
Event Varchar(50),
DateEvent Date,
NumPerformers Integer);
18 suggest her suitable command for the following purpose:
. i. To display the list of the database already existing in MySQL.
ii. To use the database named City.
iii. To remove the pre-existing database named Clients.
iv. To remove all the records of the table named “Club” at one go along with its
structure permanently.
i. Show databases;
ii. Use City;
iii. Drop database Clients;
iv. Drop table Club;
19 While creating a table named “Employee”, Mr. Ramesh got confused as which data type he
. should chose for the column “EName” out of char and varchar. Help him in choosing the
right data type to store employee name. Give valid justification for the same.
EName field can have a varchar as a datatype as two employees will not be
having a same length of their names.
MySQL Worksheet-3
1. Kunal created a table in Mysql. Later on he found that there should have been another
column in the table. Which command should he use to add another column to the table?
Alter table tablename add fieldname datatype(size);
2. While creating a table 'Customer' Simrita forgot to set the primary key for the table. Give
the statement which she should write now to set the column 'CustiD' as the primary key of
the table?
Alter table customer add primary key(custid);
Now she wants to add a new column ‘Address’ to the above given table. Suggest suitable
MySQL command for the same.
Alter table hospital add address varchar(30);
4. Write SQL command to remove column named ‘Hobbies’ from a table named ‘Student’.
Alter table student drop hobbies;
5
5. While creating the table Student last week, Ms. Roy forgot to include the column
Game_Played. Now write a command to insert the Game_Played column with VARCHAR
data type and 30 size into the Student table?
Alter table student add game_played varchar(30);
7. Rashi wants to add another column ‘Hobbies’ with datatype and size as VARCHAR(50) in
the already existing table ‘Student’. She has written the following statement. However it
has errors. Rewrite the correct statement.
MODIFY TABLE Student Hobbies VARCHAR;
Alter table student add hobbies varchar(50);
8. Ms. Sharma has just created a table named “Employee” containing columns
Ename, Department, Salary.
After creating the table, she realized that she has forgotten to add a primary key column
in the table. Help her in writing SQL command to add a primary key column empid. Also
state the importance of Primary key in a table.
Alter table employee add primary key(empid);
9. While creating a table 'Customer' Simrita wrongly added a primary key constraint to the
field “CUSTNAME”. Now she wants to remove the primary key constraint from the
custname field. Help her in writing the correct command.
Alter table customer ass primary key(custname);
10 Mr. Akshat have added a not null constraint to the “name” field in “employees” table. But
. now he wants to remove that not null constraint. Write the command to delete the not
null constraint from name field.
Alter table employee modify name varchar(30) null;
6
MySQL Worksheet-4
1. Madhu is not able to change a value in a column to NULL. What constraint did she specify
when she created the table?
Not Null, Primary Key
Add a new row for a new item in GYM with the details: "G107", "Vibro exerciser” ,21000,
“GTCFitness"
Insert into Result values("G107", "Vibro eierciser” ,21000, “GTCFitness");
7. Rewrite the following SQL statement after correcting error(s). Underline the corrections
made.
INSERT IN STUDENT(RNO,MARKS) VALUE (5,78.5);
INSERT INTO STUDENT(RNO,MARKS) VALUES (5,78.5);
8. Rewrite the following SQL statement after correcting error(s). Underline the corrections
made.
INSERT IN EMP(EMPNO, SALES) VALUE (100, 20078.50);
INSERT INTO EMP(EMPNO, SALES) VALUES(100, 20078.50);
9. Arushi is inserting “Sharma” in the “LastName” column of the “Emp” table but an error is
being displayed. Write the correct SQL statement.
INSERT INTO Emp(‘Sharma’)VALUES(LastName) ;
INSERT INTO Emp(LastName) VALUES(‘Sharma’) ;
10 Amita has created the following table with the name ‘Order’.
.
7
One of the rows inserted is as follows :
(i) What is the data type of columns OrderId and OrderDate in the table Order ?
(ii) Anita is now trying to insert the following row :
11 In today’s digitized world with a need to store data electronically, it is very important to
. store the data in the databases. SQL is used to interact with the Database Management
System.
Classify the following commands according to their type :(DDL/DML)
i. INSERT INTO ii. ALTER TABLE
i. DML ii. DDL
8
9