MySQL – Queries
The following is a list of commonly used MySQL queries for creating databases, using databases, creating tables, inserting records, updating records, deleting records, selecting records, truncating tables, and dropping tables.
The MySQL create database command is used to create a database.
create database employees;
The MySQL database is used to select the database.
The MySQL create query creates a table, view, procedure, and function.
CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(30) NOT NULL, last_name VARCHAR(30) NOT NULL, email VARCHAR(50) UNIQUE NOT NULL, salary DOUBLE NOT NULL );
A MySQL alter query adds, modifies, deletes, or drops columns from a table. Here is a query to add a column to the employees table:
ALTER TABLE employees ADD age varchar(50);
When inserting records into a table, MySQL insert query is used. For example:
INSERT INTO employees( first_name, last_name,email,salary) VALUES ('Ravi', 'V', 'firstname.lastname@example.org',100000);
A MySQL update query is used to update records in a table. For example:
update customers set first_name=Sai, email@example.com where id=1;
The update query in MySQL is used to delete records from a table in the database. For example:
delete from customers where id=101;
A select query is used to retrieve records from a database. For example:
SELECT * from employees;
Truncate Table Query
In MySQL, an update query is used to truncate or remove records from a table. Structure is not removed by this process. For example:
truncate table employees;
The drop query in MySQL is used to delete a table, view, or database. In the event that you drop a table, its structure and data will be removed. For example:
drop table employees;