MySQL
A practical MySQL command reference covering database and table operations, filtering, aggregation, joins, nested queries and basic user management.
- Written
- Published
- Updated
MySQL
<...> stands for your input, for example
if your database name is Saber, the command use <yourDatabaseName>; will be use Saber;.
Fundmental Operation
Login
mysql -u root -p
Show the version of mysql
select version();
Show all databases
show databases;
Create a database
create database <yourDatabaseName>;
Delete a database
drop database <yourDatabaseName>;
Use one database
use <yourDatabaseName>;
Show all tables in the current database
show tables;
Show all attributes of a table
describe <yourTableName>; or desc <yourTableName>;
Show the database used now
select database();
Create table
Example:
create table <tableName> (
<keyOne> int(4) not null ,
<keyTwo> VARCHAR(10),
<keyThree> VARCHAR(9),
<keyFour> INT(4) unique,
<keyFive> DATE DEFAULT NULL,
<keySix> DOUBLE(7,2),
<keySeven> DOUBLE(7,2),
primary key (<keyName>) auto_increment,
<keyEight> INT(2)
);
Delete table
drop table <tableName>;
Insert item
INSERT INTO <tableName> ( <keyOne>, <keyOTwo>, <keyThree>)
VALUES ( 7876, 'ADAMS', NULL);
Update item
update <tableName> set <key> = <sth>, <key> = <sth> where <condition>;
Delete item
delete from <tableName> where <condition>;
Query Operation
Select
select * from <tabelName>; show all items and all keys in your specified table
select <keyOne>,<keyTwo> from <tableName>; specify keys your want
where
select * from <tableName> where <condition>;
<condition> can be <keyOne> >= <number> and <keyOne> <= <number> or <keyTwo> = <sth>
NOTE: <key> is NULL or <key is not NULL
Between
select * from <tableName> where <key> between <number> and <number>;
In
select * from <tableName> where <key> in(<value1>, <value2>, <value3>);
This will select all items with key = value1 or key = value2 or key = value3.
Like
select * from <tableName> where <key> like <expression>;
This will select all items that key has the property of like experession.
Order by
select * from <tableName> order by <key> desc/asc; short for descent/ascent, default ascent
Statistic
select max(<key>) from <tableName>;
min(<key>) avg(<key>) sum(<key>) count(<key>)
Group by
select sum(<keyOne>) from <tableName> group by <keyTwo>;
merge all items with same <keyTwo> and calculate corresponding Statistic funtion of <keyOne>.
Distinct
select distinct <key>, <key> from <tableName> ;
Alias
select <key> as <aliasName> from <tableName>; or omit as
select <key> <aliasName> from <tableName>;
Note: The result of column name will be showed as alias name.
Thus this is awesome.
Limit
select * from <tableName> order by <key> desc/asc limit <number>;
Limit can be used to implement page query.
Begin from <offset> item and return <number> results.
Note: Index begins with 0.
select * from <tableName> limit <offset>, <number>;
Alter Table
ALTER TABLE <tableName>
MODIFY COLUMN <Key> VARCHAR(1000);
Command Order
SELECT -> FROM -> JOIN -> ON -> WHERE -> GROUP BY -> HAVING -> UNION -> ORDER BY ->LIMIT
Multiple Tables
Cartesian product
select * from <tableA> <AliasA>, <tableB> <AliasB>
where <AliasA>.<key> = <AliasB>.<key>;
Join on
select * from <tableA> inner join <tableB> on <tableA>.<key> = <tableB>.<key>;
select * from <tableA> left join <tableB> on <tableA>.<key> = <tableB>.<key>;
select * from <tableA> right join <tableB> on <tableA>.<key> = <tableB>.<key>;
Union
select * from <tableA> union select * from <tableB>;
Nesting
select * from <table> where( select * from <table>) ;
select * from (select * from <table>) ;
User Management
Create user
create user '<userName>'@'<host>' identified by '<password>';
create user 'Saber'@'localhost' identified by '123456';
Comments
No approved comments yet.
Your display name and plain-text comment become public only after owner approval. No account or email is required.