MySQL Interview Questions
MySQL Interview Questions
1) What is MySQL?
Formerly MySQL was initially owned by a for-profit firm MySQL AB, then
Sun Microsystems bought it, and then Oracle bought Sun Microsystems,
so Oracle currently owns MySQL.
The Lamp is a platform used for web development. The Lamp uses Linux,
Apache, MySQL, and PHP as an operating system, web server, database
& object-oriented scripting language. And hence abbreviated as LAMP.
2) In which language MySQL has been written?
MySQL is written in C and C++, and its SQL parser is written in yacc.
o Flexible structure
o High performance
o Drivers
o Graphical Tools
o JSON Support
o Geo-Spatial Support
4) What is the difference between MySQL and SQL?
A PHP script is required to store and retrieve the values inside the
database.
o Tables are a way to represent the division of data in a database while the
database is a collection of tables and data.
o Tables are used to group the data in relation to each other and create a
dataset. This dataset will be used in the database. The data stored in the
table in any form is a part of the database, but the reverse is not true.
o A database is a collection of organized data and features used to access
them, whereas the table is a collection of rows and columns used to store
the data.
First of all, the MYSQL server is free to use for developers and small
enterprises.
MySQL has very stable versions available, as MySQL has been in the
market for a long time. All bugs arising in the previous builds have been
continuously removed, and a very stable version is provided after every
update.
The MySQL database server is very fast, reliable, and easy to use. You
can easily use and modify the software. MySQL software can be
downloaded free of cost from the internet.
There are many tables that remain present by default. But, MyISAM is
the default database engine used in MySQL. There are five types of tables
that are present:
o MyISAM
o Heap
o Merge
o INNO DB
o ISAM
Installing MySQL on our system allows us to safely create, drop, and test
web applications without affecting our live website's data. There are
many ways to use MySQL on our system, but the best way is to install it
manually. The manual installation allows us to learn more about the
system and provides more control over the database. To see the
installation steps of MySQL in Windows goes to the below link:
https://www.javatpoint.com/how-to-install-mysql
o It provides more control to how and when MySQL server starts and
closes.
We can check the MySQL version on Linux using the below command:
1. mysql -v
In this output, we can see the additional version information about the
installed MySQL software like innodb_version, protocol_version,
version_ssl_library, etc.
10) How to add columns in MySQL?
A column is a series of cells in a table that stores one value for each row
in a table. We can add columns in an existing table using the ALTER
TABLE statement as follows:
column];
We can delete a table in MySQL using the Drop Table statement. This
statement removes the complete data of a table, including structure and
definition from the database permanently. Therefore, it is required to be
careful while deleting a table. After using the statement, we cannot
recover the table in MySQL. The statement is as follows:
The foreign key is used to link one or more tables together. It matches
the primary key field of another table to link the two tables. It allows us
to create a parent-child relationship with the tables. We can add a foreign
key to a table in two ways:
1. [CONSTRAINT constraint_name]
MySQL allows us to connect with the database server in mainly two ways:
1. mysql
Finally, type the password for the selected user account root and press
Enter:
After successful connection, we can use the below command to use the:
1. USE database_name;
We can change the MySQL root password using the below statement in
the new notepad file and save it with an appropriate name:
1. mysqld --init-file=C:\\mysql-notepadfile.txt
Finally, we can log into the MySQL server as root using this new
password. After launches the MySQL server, it is to delete the C:\myswl-
init.txt file to ensure the password change.
A new popup screen appears where we need to fill all the details. After
entering the details, click on the Apply button and then the Finish button
to complete the database creation.
Launch the MySQL Workbench and go to the Navigation tab and click on
the Schema menu where all the previously created databases are shown.
Select any database and double click on it. It will show the sub-menus
where we need to select the Tables option.
If we want to change more than one table name, use the below syntax:
Now, use the below command to import the data into the newly created
database:
o MySQL Workbench
n [FIRST|AFTER existing_column];
Suppose the column's current name is S_ID, but we want to change this
with a more appropriate title as Stud_ID. We will use the below
statement to change its name:
....;
We can insert data in a MySQL table using the INSERT STATEMENT. This
statement allows us to insert single or multiple rows into a table. The
following is the basic syntax to insert a record into a table:
If we want to insert more than one rows into a table, use the below
syntax:
2. VALUES
5. ...
We can delete a row from the MySQL table using the DELETE STATEMENT
within the database. The following is the generic syntax of DELETE
statement in MySQL to remove one or more rows from a table:
It is noted that if we have not specified the WHERE clause with the
syntax, this statement will remove all the records from the given table.
We can connect two or more tables in MySQL using the JOIN clause.
MySQL allows various types of JOIN clauses. These clauses connect
multiple tables and return only those records that match the same value
and property in all tables. The following are the four easy ways to join
two or more tables in MySQL:
o Inner Join
o Left Join
o Right Join
o Cross Join
Sometimes we need to fetch data from three or more tables. There are
two types available to do these types of joins. Suppose we have three
tables named Student, Marks, and Details.
Let's say Student has (stud_id, name) columns, Marks has (school_id,
stud_id, scores) columns, and Details has (school_id, address, email)
columns.
This approach is similar to the way we join two tables. The following
query returns result from three tables:
It is another approach to join more than two tables. In the above tables,
we have to create a parent-child relationship. First, create column X as a
primary key in one table and as a foreign key in another table. Therefore,
stud_id is the primary key in the Student table and will be a foreign key
in the Marks table. Next, school_id is the primary key in the Marks table
and will be a foreign key in the Details table. The following query returns
result from three tables:
1. UPDATE table_name
3. [WHERE Clause]
datatype]) ]
2. BEGIN
4. END;
This statement can return one or more value through parameters or may
not return any result. The following example explains it more clearly:
1. DELIMITER $$
3. BEGIN
5. END$$
A view is a database object whose values are based on the base table. It
is a virtual table created by a query by joining one or more tables. It is
operated similarly to the base table but does not contain any data of its
own. If any changes occur in the underlying table, the same changes
reflected in the View also.
2. SELECT columns
3. FROM tables
4. [WHERE conditions];
2. [before | after]
5. BEGIN
6. --variable declarations
7. --trigger code
8. END;
After the release of MySQL version 8, we can use the below command to
clear the command line screen:
MySQL allows us to import the CSV (comma separated values) file into a
database or table. A CSV is a plain text file that contains the list of data
and can be saved in a tabular format. MySQL provides the LOAD DATA
INFILE statement to import a CSV file. This statement is used to read a
text file and import it into a database table very quickly. The full syntax
to import a CSV file is given below:
ame.csv'
6. IGNORE 1 ROWS;
cifier));
4. FROM information_schema.tables
6. GROUP BY table_schema;
If we want to check the size of the table in a specific database, use the
following statement:
1. SELECT table_name AS 'Table Name',
3. FROM information_schema.TABLES
Working with the MySQL server, it is a common task to view or list the
available databases. We can view all the databases on the MySQL server
host using the following command:
MySQL uses the LIMIT keyword, which can be used to limit the result set.
It will allow us to get the first few rows, last few rows, or range of rows.
It can also be used to find the second, third, or nth highest salary. It
ensures that you have use order by clause to sort the result set first and
then print the output that provides accurate results. The following query
is used to get the second highest salary in MySQL:
1. SELECT salary
There are some other ways to find the second highest salary in MySQL,
which are given below:
This statement uses subquery and IN clause to get the second highest
salary:
1. SELECT MAX(salary)
2. FROM employees
This query uses subquery and < operator to return the second highest
salary:
o It is not possible to use Where command with TRUNCATE QLbut you can
use it with DELETE command.
o The DELETE command is used to delete data from a table. It only deletes
the rows of data from the table while truncate is a very dangerous
command and should be used carefully because it deletes every row
permanently from a table.
There are only six Triggers allowed to use in the MySQL database.
1. Before Insert
2. After Insert
3. Before Update
4. After Update
5. Before Delete
6. After Delete
Tables that are present in memory is known as HEAP tables. When you
create a heap table in MySQL, you should need to specify the TYPE as
HEAP. These tables are commonly known as memory tables. They are
used for high-speed storage on a temporary basis. They do not allow
BLOB or TEXT fields.
1. TINYBLOB
2. BLOB
3. MEDIUMBLOB
4. LONGBLOB
The differences among all these are the maximum length of values they
can hold.
1. TINYTEXT
2. TEXT
3. MEDIUMTEXT
4. LONGTEXT
48) What is a trigger in MySQL?
49) What is the difference between the heap table and the
temporary table?
Heap tables:
Heap tables are found in memory that is used for high-speed storage
temporarily. They do not allow BLOB or TEXT fields.
Temporary tables:
The temporary tables are used to keep the transient data. Sometimes it
is beneficial in cases to hold temporary data. The temporary table is
deleted after the current client session terminates.
Main differences:
The heap tables are shared among clients, while temporary tables are not
shared.
Heap tables are just another storage engine, while for temporary tables,
you need a special privilege (create temporary table)
50) What is the difference between FLOAT and DOUBLE?
2. MySQL uses only just under 1 MB of RAM on your laptop, while Oracle 9i
installation uses 128 MB.
3. MySQL is great for database enabled websites while Oracle is made for
enterprises.
4. MySQL is portable.
3. The maximum no. of character CHAR data types can hold is 255
characters, while VARCHAR can hold up to 4000 characters.
Mysql_connect:
2. Every time you need to open and close the database connection,
depending on the request.
Mysql_pconnect:
3. It is more useful if your site has more traffic because there is no need to
open and close connection frequently and whenever the page is loaded.
55) What does "i_am_a_dummy flag" do in MySQL?
1. SELECT CURRENT_DATE();
Actually, both Unix timestamp and MySQL timestamp are stored as 32-bit
integers, but MySQL timestamp is represented in the readable format of
YYYY-MM-DD HH:MM:SS format.
NOW() command is used to show current year, month, date with hours,
minutes, and seconds while CURRENT_DATE() shows the current year
with month and date only.
SELECT NOW();
SQLyog program is the most popular GUI tool for admin. It is the most
popular MySQL manager and admin tool. It combines the features of
MySQL administrator, phpMyadmin, and others. MySQL front ends and
MySQL GUI tools.
The =, <>, <=, <, >=, >, <<, >>, < = >, AND, OR or LIKE operator
are the comparison operators in MySQL. These operators are generally
used with SELECT statement.
ENUMs are used to limit the possible values that go in the table:
For example:
For example:
Example:
2. while($row = mysql_fetch_row($result))
3. {
4. Some statement;
5. }
79) What is the difference between mysql_connect and
mysql_pconnect?
MySQL data directory is a place where MySQL stores its data. Each
subdirectory under this data dictionary represents a MySQL database. By
default, the information managed my MySQL = server mysqld is stored in
the data directory.
o [abc] matches a or b or z
o | separates strings
o "." Can be used to match any single character. "|" can be used to match
either of the two strings
o REGEXP can be used to match the input characters with the database.
Example:
1. Select employee_name
2. From employee
4. Order by employee_name
84) What is the usage of the "i-am-a-dummy" flag in
MySQL?
The SELECT command is used to view the content of the table in MySQL.
1. mysql;
2. mysql mysql.out;
Federated tables are tables that point to the tables located on other
databases on some other server.
To identify each row of a table, we will use a primary key. For a table,
there exists only one primary key.
o PHP Driver
o JDBC Driver
o ODBC Driver
o C WRAPPER
o PYTHON Driver
o PERL Driver
o RUBY Driver
o CAP11PHP Driver
o Ado.net5.mxz
Majorly SQL commands can be divided into three categories, i.e., DDL,
DML & DCL. Data Definition Language (DDL) deals with all the database
schemas, and it defines how the data should reside in the database.
Commands like CreateTABLE and ALTER TABLE are part of DDL.
Data Control Languages (DCL) are related to the Grant and permissions.
In short, the authorization to access any part of the database is defined
by these.