Mysql Interview Questions

How many TRIGGERS are allowed on MySql table?

MySql table allows following 6 triggers:

-BEFORE INSERT
-AFTER INSERT
-BEFORE UPDATE
-AFTER UPDATE
-BEFORE DELETE and
-AFTER DELETE

Differentiate between FLOAT and DOUBLE.

FLOAT stores floating point numbers with accuracy up to eight places and has four bytes while DOUBLE stores floating point numbers with accuracy upto 18 places and has eight bytes.

Tell us something about Heap tables.

– HEAP tables are found in memory.
– They are used for high speed storage on temporary basis.

Some of their characteristics are:
– They do not allow BLOB or TEXT fields.
– Only comparison operators like =, <,>, = >,=< , can be used with them.
– AUTO_INCREMENT is not supported by HEAP tables
– Indexes should be NOT NULL

How do you control the max size of a HEAP table?

– Maximum size of Heap table can be controlled using MySQL config variable called max_heap_table_size.

What are the advantages of MySQL in comparison to Oracle?

– MySQL is open source software available at zero cost.
– It is portable
– GUI with command prompt.
– Administration is supported by MySQL Query Browser

What does myisamchk do?

– It compresses the MyISAM tables, which reduces their disk or memory usage.

How can we convert between Unix & MySQL timestamps?

– MySQL timestamp can be converted into Unix timestamp using the command UNIX_TIMESTAMP.
– Unix timestamp can be converted into MySQL timestamp using the command FROM_UNIXTIME.

What is BLOB?

– BLOB stands for binary large object.
– It that can hold a variable amount of data.

There are four types of BLOB based on the maximum length of values they can hold:

– TINYBLOB
– BLOB
– MEDIUMBLOB
– LONGBLOB

What is TEXT?

TEXT is case-insensitive BLOB. The four types of TEXT are:

– TINYTEXT
– TEXT
– MEDIUMTEXT
– LONGTEXT

What is the difference between BLOB and TEXT?

– In BLOB sorting and comparison is performed in case-sensitive for BLOB values
– In TEXT types sorting and comparison is performed case-insensitive.

How is a MyISAM table stored?

MyISAM table is stored on disk in three formats.
– ‘.frm’ file – storing the table definition
– ‘.MYD’ (MYData) – data file
– ‘.MYI’ (MYIndex) – index file

Explain advantages of MyISAM over InnoDB?

– MyISAM follows a much more conservative approach to disk space management – storing each MyISAM table in a separate file, which can be further compresses, if required.
– InnoDB stores the tables in tablespace. Further optimization is difficult with them.

How would concatenate strings in MySQL?

With the use of – CONCAT (string1, string2, string3)

How would you get the current date in Mysql?

By using SELECT CURRENT_DATE();

How would you enter Characters as HEX Numbers?

– To enter characters as HEX numbers, you can enter HEX numbers with single quotes and a prefix of (X)
– Alternatively, just prefix HEX numbers with (Ox).

How are MySQL timestamps seen to a user?

– MySQL time stamps are seen by a user in a readable format : YYYY-MM-DD HH:MM:SS.

What are the steps required to view your MYSQL database?

There are certain steps that are required before you can view MySQL database and they are as follows:
1. Login as database user: for example:
[root@ tmp]# mysql -u mysqluser -p sales
2. List all your MySQL databases: use the show command to view the list of all available MySQL databases. For example to view sales database use the command shown below:
mysql> show databases;
| Database |
| salesdata |

Write a command to view the MySQL database table structure

To view the database table structure describe command is used that provides the list of all the data fields used in the database table. Example: a table named sales will have the four fields like: name, description, num, and date.
mysql> describe test;
| Field | Type | Null | Key | Default | Extra|
| num | int(11)| |PRI | NULL | auto_increment |
| date | date| | MUL | 0000-00-00 |
| name | varchar(50) | MUL |
| description | varchar(75) | YES | | NULL

Write a command to view the content of the table

To view all the data that is contained in a table named sales use the select command. For example: to see the data of first row in a table using the command as:
Mysql> select * from sales limit 1;
If the database is created recently then it will give a blank listing, but after the data is being entered it will show the full listing in a tabular form.

What is the procedure to configure the application of MYSQL?

The procedure to configure the application of MySQL is:
1. First create a database
2. Test the database by informing about the database name, IP address of the database client server, username and password of the application.
3. Edit the special application specific configuration file using a GUI or command line.
4. Configure the language with which MySQL will interact.

What are the applications required to support MYSQL?

The applications that are required to support MySQL are as follows:
1. PHP-mysql MySQL database is used specifically to support PHP
2. perl-DBI: provides a generic Perl interface for interacting with relational databases
3. perl-DBD-MySQL MySQL database specific support for Perl
4. A web server is required to configure the database and its configuration
5. Programming language is required which supports MySQL.

Write a query to stop MYSQL in unix

The query to stop MySQL is quite useful when an error is occurred or when data has to be saved from any mishap. It is also used for retrieving the root password because it is either easily forgotten or misplaced. To stop the service the following command is required:
1) Stop MySQL
[root@ tmp]# service mysqld stop
Stopping MySQL: [ OK ]
[root@ tmp]#

Write a query to MYSQL in safe mode and to change the root password

To start MySQL in safe mode, mysqld_safe command is used which allow it to run in the safe mode and it also doesn’t allow the reading of tables with the database passwords:
[root@ ]# mysqld_safe –skip-grant-tables –skip-networking &[1] 13007
[root@ ]# Starting mysqld daemon with databases from /var/lib/mysql
[root@ ]#
After running the MySQL in safe mode the password protection gets removed and to use the password protection mechanism a command is used as follows:
Mysql -u root command
[root@bigboy tmp] # mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.16
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
Mysql> [ a message is being shown which allow user to take the control of the root]

How to take MYSQL database backup?

To take the database backup uses the following syntax:
mysqldump –add-drop-table -u [username] -p[password] [database] > [backup_file]
This command will take the database backup by knowing the username and password for the database connection and dropping any table which is being deleted or not in use. It is always a good practice to take the backup of mysql as it contains all the database information that a user can access. While using the command keep a note that there should not be any space between –p switch and password, if there is then you will get a syntax error.

Write a command with which MySQL table can be repaired

The command syntax with which mysql table can be repaired is as follows:
REPAIR TABLE tablename;
REPAIR TABLE tablename QUICK;
REPAIR TABLE tablename EXTENDED;
The command will just do as it says repair a specified table, but if QUICK or EXTENDED are used then the meaning of it changes. In case of QUICK it will repair only the index tree, whereas in case of EXTENDED it will create index row by row and repair it.

What are the different tables present in MySQL?

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:
1. MyISAM
2. Heap
3. Merge
4. INNO DB
5. ISAM

What does the file with the extension: frm, myd, and myi contain?

MySQL default table type is MyISAM, where there are three kind of files that are stored inside MyISAM. The file names begin with the table name and have the extensions such as frm, myd and myi. The explanation of each file is given below:
.frm file consists of the table definition that are stored in the database
.myd is an extension that is used by a data file.
.myi is an extension that is used by index file.

What is the difference between MYSQL and SQL?

SQL is known as standard query language, as the name implies it is the language which is used to interact with the database like MySQL. Whereas, MySQL is a database that store various types of data and keep it safe. A PHP script is required to store and retrieve the values inside the database.

How database are managed?

Database is a collection of data and it is managed by a database server, which is a special program that is also known as MySQL database server. Application that you create usually communicates with the database server in the language which it can understand; mostly SQL language is used for communication. Database server in return interacts with the web server on same server or computer. Database server and web server result in the data which is being shown on the web.

What is required to create MYSQL database?

To create MySQL databse the first component which has to be present is a database server on which the queries of database will run and software tool through which you can access the applications. It also requires PHP scripts for communicating with the database using SQL commands.

What do you understand by MYSQL terminal?

MySQL terminal is used as a command line interface in many operating system. It provides a way to access the database and other resources using the SQL commands that are interpreted by the MySQL database server.

Why phpMyAdmin is used for MYSQL?

PhpMyAdmin is a very popular and easy to use GUI tool that can allow SQL commands to be run to create database, create tables, insert data and retrieve it. It provides a web based interface to the user for the ease of use. phpMyAdmin allows user to manage everything from one place and no other installation is required in the computer after this.

Write a query to create a database and a table?

MySQL comes up with some default database that can be used as a base to create a new one. The command that is used to create a new database is as follows:
CREATE DATABASE SQL command
The command has to be written in MySQL terminal. This command will create a new database and then you can create new tables and include data in it.

How can you make a database as your current database?

After making a database the first thing which has to be done is to create a table inside the database to test the new database that is being created. The command which is used to do that is:
USE aliendatabase;
This command allows us to make a database which is not a current database as my current. I have to just use the USE variable and the name of the database and it will become active for use.

What is the difference between a database and a table?

There is a major difference between a database and a table. The differences are as follows:
Tables are a way to represent the division of data in a database. Whereas, database is a collection of tables and data
Tables group the data in relation with each other and create a dataset; this dataset will be used in the database. The data which are stored in the table in any form is a part of the database, but opposite is not true.

Is the syntax correct? Explain the meaning of the syntax given below

$dbc = mysqli_connect(‘data.aliensabductedme.com’, ‘owen’, ‘aliensrool’, ‘aliendatabase’);
Yes the syntax is correct and this query is getting used to connect the database with a PHP script. PHP uses three functions to communicate with MySQL database: The three functions are: mysqli_connect(), mysqli_query(), and mysqli_close().
mysqli_connect() and mysqli_query() are used to connect to mysql database and issue a query to the database. mysqli_query() is also used to retrieve the string or any other data from the database. Mysqli_close() is used to close a connection with the database.

How to connect a PHP script with the MySQL database?

PHP script can be connected with MySQL database by using the function called as mysqli_connect() function. You need to provide the information related to your location, username, and password to the server to get permission to interact with the MySQL database server. When database name will be asked provide the name and it will connect you to the database.

What is the difference between truncate and delete?

Delete command is used to delete data from a table for example Remove emails where we write a script to delete the customer’s data. It deletes the rows of data from a table. The syntax of it as follows:
DELETE FROM table_name
Whereas, truncate is very dangerous command and should be used carefully as it deletes every row from a table. The syntax of it as follows:
TRUNCATE TABLE “table_name”

How important is to list the column names when doing an INSERT?

It is not important to list the column names when doing using an INSERT command as you can provide the column information and values in the table in the same order in which they appear in the table structure. It is safer and convenient way to specify the column names as it will keep the count of the column you are visiting.

Where’s database data actually stored? Is there a way to see the files which are stored?

Database data is usually get stored in the computer hard-disk and you can manage the data by the database program like MySQL and phpAdmin. The files can be seen but database files remain in binary format so it can be opened and read but, you have to put extra effort to understand it. SQL is given for the purpose of interacting with the database and read the database and retrieve the information out of it.

Why to use CHAR instead of VARCHAR in the database?

CHAR is much more accurate and efficient to use. CHAR doesn’t have to keep a count of the variable length. It is more efficient when you have to use it for a text column which is of an exact length. Char is used for the data which are fixed, but VARCHAR is used for data like password, which are variable.

What are ENUMs used for in MySQL?

ENUM is used to limit the possible values and store it together. It is a function that can be created to store the similar values together. It is used in creation of table. The syntax of it is as follows:
CREATE TABLE months (month ENUM “January”, “February”, “March”,…);
INSERT months VALUES (“April”);

What is the purpose of -> in the MySQL terminal?

-> prompt in the command of MySQL indicates that a single statement is being entered across multiple lines. From this prompt MySQL interprets that you haven’t finished entering the statements. It has no impact of enter which you might press to go to the next line. MySQL will execute the statement only when you will insert the semicolon in the end which it recognizes.

How to find the unique values if the value in the column is repeated?

If the values in the column of a table are repeating and a unique value has to be found then the following command can be used in the query:
SELECT DISTINCT user_firstname FROM users;
There is another command which can be used to find the command to see the distinct values as:
SELECT COUNT (DISTINCT user_firstname) FROM users;

When to use ORDER BY in DELETE statement?

The ORDER BY clause in DELETE statement is used when a deletion has to take place by in a specified order. The syntax is like this
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
[WHERE where_condition]
[ORDER BY …]
[LIMIT row_count]
ORDER BY clause is specified, the rows are deleted in the order that is specified.

What is the difference between Unix timestamps and MySQL timestamps?

The unix timestamp is stored as 32 bit integer whereas, MySQL timestamps are stored in 32 bit integers but represented differently then UNIX timestamps like YYYY-MM-DD HH:MM:SS format. Unix timestamp is given as month-day-year-HH:MM:SS..