WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

MySQL · Basic · question 10 of 100

How do you create a database and table in MySQL?

📕 Buy this interview preparation book: 100 MySQL questions & answers — PDF + EPUB for $5

To create a database and table in MySQL, you need to have access to a MySQL server and be able to log in to that server with appropriate privileges to create new databases and tables.

First, log in to the MySQL server using the command line client or a graphical user interface such as MySQL Workbench. Here is an example of logging in to a MySQL server using the command line:

mysql -u username -p

Replace ‘username‘ with the username that you use to log in to the MySQL server. You will be prompted for a password.

Once you are logged in, you can create a database using the ‘CREATE DATABASE‘ statement. Here is an example of creating a database called ‘example_db‘:

CREATE DATABASE example_db;

Next, you can create a table within that database using the ‘CREATE TABLE‘ statement. Here is an example of creating a table called ‘users‘ within the ‘example_db‘ database:

USE example_db;

CREATE TABLE users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(50),
  email VARCHAR(50),
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

In this example, the ‘users‘ table has four columns: ‘id‘, ‘name‘, ‘email‘, and ‘created_at‘. The ‘id‘ column is the primary key of the table and is an auto-incrementing integer. The ‘name‘ and ‘email‘ columns are both of type ‘VARCHAR(50)‘, which can hold up to 50 characters of text. The ‘created_at‘ column is of type ‘TIMESTAMP‘ and includes a default value of the current timestamp.

Once the table is created, you can start inserting data into it using the ‘INSERT INTO‘ statement. Here is an example of inserting a row into the ‘users‘ table:

INSERT INTO users (name, email) VALUES ('John Doe', 'johndoe@example.com');

This statement inserts a row with a ‘name‘ of "John Doe" and an ‘email‘ of "johndoe@example.com" into the ‘users‘ table.

That’s it! You have now created a database and table in MySQL and inserted data into that table.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic MySQL interview — then scores it.
📞 Practice MySQL — free 15 min
📕 Buy this interview preparation book: 100 MySQL questions & answers — PDF + EPUB for $5

All 100 MySQL questions · All topics