MYSQL Interview Questions
There is given MYSQL interview questions and answers that have been asked in many companies. Let's see the list of top MYSQL interview questions.
What is a TRIGGER in MySQL?
A trigger is a table-associated database object in MySQL. It is activated when a specified action takes place. A trigger can be invoked after or before the event takes place. It can be used on INSERT, DELETE, and UPDATE. It uses the respective syntax to define the triggers. For example, BEFORE INSERT, AFTER DELETE, etc..
Can a PRIMARY KEY be DROPPED in MySQL? If yes, how?
Yes, it is possible to drop the primary key from a table. The command to use is again, the ALTER TABLE followed by DROP. It goes like this:
ALTER TABLE table_name DROP PRIMARY KEY;.
What are the different ways to JOIN TABLES in MySQL?
Join is used to link one or more tables together, with the common column’s values in both tables. Primarily there are four types of joins: 1. Inner Join – Inner join uses a join predicate, which is a condition used to make the join. Here is the syntax: SELECT something FROM tablename INNER JOIN another table ON condition; 2. Left Join – Left join also requires a join condition. The left join chooses information beginning from the left table. For each entry in the left table, the left compares each entry in the right table. Here is the syntax: SELECT something FROM tablename LEFT JOIN another table ON condition; 3. Right Join – Opposite to left join and, with one difference in the query, that is the name of join. Here care should be taken about the order of tables. Here is the syntax: SELECT something FROM tablename LEFT JOIN another table ON condition; 4. Cross Join – Cross join has no join condition. It makes a cartesian of rows of both the tables. Here is the syntax: SELECT something FROM tablename CROSS JOIN another table; Note: While dealing with just one table, self-join is also possible. .
How does DATABASE IMPORT/EXPORT work in MySQL?
It can be done in two ways. One is to use phpMyAdmin, and the second is to use the command line access of MySQL. The latter can be done by using the command named mysqldump. It goes something like this:
· mysqldump -u username -p databasename > dbsample.sqlTo import a database into MySQL, only a sign change is required, with a command of MySQL. The command goes something like this:
· mysql -u username -p databasename < dbsample.sql.
What is a FOREIGN KEY? Write a QUERY to implement the same in MySQL.?
A foreign key is used to connect two tables. A FOREIGN KEY is a field (or assortment of it) in one table that alludes to the PRIMARY KEY in another table. The FOREIGN KEY requirement is utilised to forestall activities that would crush joins between tables. To assign a foreign key, it is important to mention it while creating the table. It can be assigned by invoking the FOREIGN KEY query. Something like this: FOREIGN KEY (Any_ID) REFERENCES Table_to_reference(Any_ID).
Write a QUERY for a column addition in MySQL.
For this, an ALTER TABLE query is required. Once invoked, simply mention the column and its definition. Something like this: ALTER TABLE cars ADD COLUMN engine VARCHAR(80) AFTER colour;.
What is the difference between the LIKE and REGEXP operators?
LIKE and REGEXP operators are used to express with ^ and %.
.SELECT * FROM employee WHERE emp_name REGEXP “^b”;SELECT * FROM employee WHERE emp_name LIKE “%b”;
What are the drivers in MySQL?
Following are the drivers available in MySQL:
- PHP Driver
- JDBC Driver
- ODBC Driver
- C WRAPPER
- PYTHON Driver
- PERL Driver
- RUBY Driver
- CAP11PHP Driver
- Ado.net5.mxj
What storage engines are used in MySQL?
Storage engines are called table types and data is stored in files using various techniques. Technique involves:
- Storage mechanism
- Locking levels
- Indexing
- Capabilities and functions.
How can we connect to a MySQL database from a PHP script?
To be able to connect to a MySQL database, we must use mysqli_connect() function as follows:
.$database=mysqli_connect("HOST","USER_NAME","PASSWORD"); mysqli_select_db($database,"DATABASE_NAME");