Here are some basic MySQL interview questions along with their answers:
- What is MySQL?
- MySQL is an open-source relational database management system (RDBMS) that is widely used for storing and managing data.
- What is a database in MySQL?
- In MySQL, a database is a collection of related data organized and structured for efficient storage, retrieval, and management.
- What is a table in MySQL?
- In MySQL, a table is a structured collection of data stored in rows and columns. It represents a single entity or concept and consists of fields (columns) and records (rows).
- How do you create a table in MySQL?
- To create a table in MySQL, you can use the
CREATE TABLE
statement. Here’s an example:CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50), age INT, salary DECIMAL(10,2) );
- How do you insert data into a table in MySQL?
- To insert data into a table in MySQL, you can use the
INSERT INTO
statement. Here’s an example:INSERT INTO employees (id, name, age, salary) VALUES (1, 'John Doe', 30, 50000);
- How do you retrieve data from a table in MySQL?
- To retrieve data from a table in MySQL, you can use the
SELECT
statement. Here’s an example:SELECT * FROM employees;
- What is the difference between
DELETE
andTRUNCATE
statements in MySQL?
- The
DELETE
statement is used to remove specific rows from a table, based on a condition. It is slower and generates undo logs. - The
TRUNCATE
statement is used to remove all rows from a table, effectively resetting the table to its initial state. It is faster and does not generate undo logs.
- What is a primary key in MySQL?
- In MySQL, a primary key is a column (or a combination of columns) that uniquely identifies each record in a table. It ensures data integrity and allows for efficient data retrieval.
- How do you establish a connection to a MySQL database using PHP?
- To establish a connection to a MySQL database using PHP, you can use the
mysqli
orPDO
extension. Here’s an example usingmysqli
:$host = "localhost"; $username = "your_username"; $password = "your_password"; $database = "your_database"; $conn = mysqli_connect($host, $username, $password, $database); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
- What are some common data types used in MySQL?
- Some common data types in MySQL include:
- INT: for integer values
- VARCHAR: for variable-length character strings
- DECIMAL: for decimal numbers
- DATE: for dates
- TEXT: for large blocks of text
These are just a few basic MySQL interview questions to help you get started. It’s important to review and prepare for a wide range of topics to ensure you’re well-prepared for your interview.