MySQL simple application

Open:
find the installation path to 'bin' folder under 'mysql.exe',
the data "path \ bin \ mysql -uroot -p" in the DOS environment,
the role add "-uroot -p" is because the entry MySQL account password is required for landing, the default account password is "root",
so when you are finished entering the path, under press enter, you will be asked to enter a password

First, the commonly used commands:

show命令:
    show databases;     显示mysql中所有数据库的名称。 
    show tables或show tables from database_name; 显示当前数据库中所有表的名称。 
    show columns from table_name from database_name; 
        或show columns from database_name.table_name; -- 显示表中列名称。 
    show grants for user_name;      显示一个用户的权限,显示结果类似于grant 命令。 
    show index from table_name;     显示表的索引。 

use命令:
    use database_name;

显示当前mysql版本和当前日期:
    select version(),current_date;

Second, the basic operation

Database operations:

Create a database: CREATE DATABASE db_name
If you do not create: create database if not exists db_name
Delete the database (silent): DROP DATABASE db_name
Delete the database (prompted): mysqladmin drop db_name
If there is deleted: drop database if exists db_name
display database information : show create database db_name
change the database character encoding: show create database charset = xxx (character encoding, such as utf8)
select database: use db_name

Operating table:

Creating tables and fields:

create table tb_name(
    字段1 数据类型,
    字段2 数据类型,
    .........,
    primary key(字段)     #设置主键
);
eg:
CREATE TABLE IF NOT EXISTS 'ips'(
    id INT UNSIGNED AUTO_INCREAMENT,
    ip VARCHAR(50) NOT NULL,
    PRIMARY KEY(id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

type of data:

    int 整型
    tinyint 整型(0-256) 
    decimal 浮点型(总位数,小数位数) 例如 decimal(3,1)
    char(X) 定长字符型 例如 char(10)
    varchar(X) 可变长度字符型 例如varchar(10)
    text 大段文本
    binary 二进制(存储照片等)

Main: data tables created under Latin characters, a Chinese equivalent to two characters, it is recommended utf-8

Field properties:

    null:空
    not null:不为空
    default 'XXXX':默认值
    auto_increment:自动增长
    primary key:主键(主键的特点,不为空,不重复,一个表只能有一个主键,但是一个主键可以由多个列组成)

The Create Table: show create table tb_name
delete a table: DROP TABLE tb_name
delete multiple tables: drop table tb_name1, tb_name2, ...
to display the data table structure: describe (or desc) tb_name

Basic operation data:

Operation of data is the most complex part, summed up as "CRUD" ,, one of the more important operations:

Data Insert: insert into tb_name (Field 1, Field 2, ...) values (value 1, value 2, ...)
Note that the data type is inserted, to add the string ''
field can be different, but the value must be correspond

All data query: select * from tb_name
specific query data field: select field 1, .. from tb_name
all specific conditions data query: select * from tb_name where 1 = the value of field 1 and field 2 = ... and value 2
WHERE field 1> field value 1 or 2 <2 value
comparison operators:>> = <<= = (assignment and comparison are '=') <> (not equal)
logical operators: and or not

Sort:
SELECT * from tb_name Order by field x asc; (default ascending order)
SELECT * from tb_name Order by field x desc; (in descending order)
SELECT * from tb_name WHERE field x = value x order by field x asc; (by a conditions sorting)

Take the first n data: select * from tb_name limit n;
from the beginning of m n-th strip (the starting position as 0): select * from tb_name limit n, m;
query before n data under a certain sort: select * from tb_name order by field x asc limit n (desc);

Data de-duplication:
SELECT DISTINCT from tb_name * WHERE condition;

Delete data:
delete from tb_name WHERE field value x = x;
all data delete from tb_name table is deleted

Data Review:
UPDATA tb_name SET Field Value 1 = 1, the value of field 2 = 2 WHERE condition;
UPDATA tb_name SET Field Value 1 = 1 (change the value for a field, it does not require additional conditions)

Aggregation function:

SUM (); summing select sum (fields x) from XX (table);
AVG (); averaging select avg (fields x) from XX (table);
max (); selecting the maximum value select max (field x) from XX (table);
min (); for the minimum select min (fields x) from XX (table);
COUNT (); the number of records select count (fields x) from XX (table); / select max (*) from XX (table);
using a function name in the function select a field condition () from XX (table) WHERE field value x = x;

Guess you like

Origin www.cnblogs.com/jian-h/p/11872672.html