QUERIES in MySQL

MySQL QUERIES

In MySQL, we can execute many queries including the Select, Insert, Update, Delete, Drop, Alter table, and Create a table or database.

MySQL Create Database Query:

Uses:
To create a MySQL database.

Syntax:

CREATE DATABASE database_name;

Example:

CREATE DATABASE stores;

MySQL Select/Use Query:

Uses:
To retrieve records from the database.

Syntax:

USE database_name;

Example:

USE stores;

MySQL Create Query:

Uses:
To create a table, procedure, view, and a function.

Syntax: To create a table.

CREATE TABLE table_name
(   
  column_1 data_type [ NULL | NOT NULL ],  
  column_2 data_type [ NULL | NOT NULL ],  
  ...  
  column_n data_type [ NULL | NOT NULL ]  
);

Example: Creating a table.

CREATE TABLE items
( id INT NOT NULL,    
  name VARCHAR(50) NOT NULL,    
  number INT(100));

MySQL Alter Query:

Uses:
To add, update, eliminate, or drop columns of a table.

Syntax: To add columns in a table.

ALTER TABLE table_name   
ADD column data_type  [ NULL | NOT NULL ] ;

Example: Adding a column in a table.

ALTER TABLE items   
ADD price INT(100);

MySQL Insert Query:

Uses:
To insert new records in an existing table.

Syntax:

INSERT into table_name VALUES(value1, value2, .. valuen);

Example:

INSERT into items VALUES(1, 'Electronics', 10);

MySQL Update Query:

Uses:
To modify the existing records of a table.

Syntax:

UPDATE table_name 
SET column_1 = value1, column_2 = value2 
WHERE conditions;

Example:

UPDATE items 
SET name = ‘Sports’, number = 20
WHERE id = 1;

MySQL Delete Query:

Uses:
To delete records of a table from the MySQL database.

Syntax:

DELETE from table_name 
WHERE conditions;

Example:

DELETE from items 
WHERE id = 1;

MySQL Select/Use Query:

Uses:
To retrieve records of a table from a database.

Syntax: To select records from a table.

SELECT * from table_name;

Example:

SELECT * from items;

MySQL Truncate Table Query:

Uses:
To delete the records of a table without removing its structure.

Syntax:

TRUNCATE TABLE table_name;

Example:

TRUNCATE TABLE items;

MySQL Drop Query:

Uses:
To drop a database, a table, or a view in MySQL.

Syntax: To drop a table.

DROP TABLE table_name;

Example:

DROP TABLE items;