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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
CREATE DATABASE database_name;
CREATE DATABASE database_name;
CREATE DATABASE database_name;

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
CREATE DATABASE stores;
CREATE DATABASE stores;
CREATE DATABASE stores;

MySQL Select/Use Query:

Uses:
To retrieve records from the database.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
USE database_name;
USE database_name;
USE database_name;

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
USE stores;
USE stores;
USE stores;

MySQL Create Query:

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

Syntax: To create a table.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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 ]
);
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 ] );
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
CREATE TABLE items
( id INT NOT NULL,
name VARCHAR(50) NOT NULL,
number INT(100));
CREATE TABLE items ( id INT NOT NULL, name VARCHAR(50) NOT NULL, number INT(100));
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ALTER TABLE table_name
ADD column data_type [ NULL | NOT NULL ] ;
ALTER TABLE table_name ADD column data_type [ NULL | NOT NULL ] ;
ALTER TABLE table_name   
ADD column data_type  [ NULL | NOT NULL ] ;

Example: Adding a column in a table.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ALTER TABLE items
ADD price INT(100);
ALTER TABLE items ADD price INT(100);
ALTER TABLE items   
ADD price INT(100);

MySQL Insert Query:

Uses:
To insert new records in an existing table.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
INSERT into table_name VALUES(value1, value2, .. valuen);
INSERT into table_name VALUES(value1, value2, .. valuen);
INSERT into table_name VALUES(value1, value2, .. valuen);

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
INSERT into items VALUES(1, 'Electronics', 10);
INSERT into items VALUES(1, 'Electronics', 10);
INSERT into items VALUES(1, 'Electronics', 10);

MySQL Update Query:

Uses:
To modify the existing records of a table.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
UPDATE table_name
SET column_1 = value1, column_2 = value2
WHERE conditions;
UPDATE table_name SET column_1 = value1, column_2 = value2 WHERE conditions;
UPDATE table_name 
SET column_1 = value1, column_2 = value2 
WHERE conditions;

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
UPDATE items
SET name = ‘Sports’, number = 20
WHERE id = 1;
UPDATE items SET name = ‘Sports’, number = 20 WHERE id = 1;
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
DELETE from table_name
WHERE conditions;
DELETE from table_name WHERE conditions;
DELETE from table_name 
WHERE conditions;

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
DELETE from items
WHERE id = 1;
DELETE from items WHERE id = 1;
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT * from table_name;
SELECT * from table_name;
SELECT * from table_name;

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT * from items;
SELECT * from items;
SELECT * from items;

MySQL Truncate Table Query:

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

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
TRUNCATE TABLE table_name;
TRUNCATE TABLE table_name;
TRUNCATE TABLE table_name;

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
TRUNCATE TABLE items;
TRUNCATE TABLE items;
TRUNCATE TABLE items;

MySQL Drop Query:

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

Syntax: To drop a table.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
DROP TABLE table_name;
DROP TABLE table_name;
DROP TABLE table_name;

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
DROP TABLE items;
DROP TABLE items;
DROP TABLE items;