MySQL Basic Operating Guide

Log data
mysql -uroot -proot

Exit the database
Exit
quit
Ctrl + d

View database version
select version ();

Display time
select now ();

View current database using
select databases ();

View all databases
show databases;

Create a database
create database python charset = utf8;

Use database
use python;

Delete database
drop database python;

View all the tables in the current database
show tables;

创建表
create table students(
id int unsigned primary key auto_increment,
name varchar(20) not null,
age tinyint(1),
high decimal(3,2),
dender enum('M','W'),
cls_id int unsigned
);

See Data Table Structure
desc classes;

View table structure to create a
show create table students \ G;

Add field name
ALTER Table Students. The Add Birthday datetime;
ALTER Table Students. The Add is_delete 0 'bit default; // 0' bit value

Rename field name
alter table students change birthday birth datetime;

Modify the field type
alter table students modify birth date not null ;

Delete field
alter table students drop birth;

Drop table
drop table students;

Data Query
SELECT from classes;
SELECT
from Students. WHERE name = 'sima3';

select id, name from students;
to field aliasing
select id as 'No', name as 'name' from Students.;
SELECT Age, name from Students.;

Inserting data
INSERT INTO classes values (. 1, 'python20', 70);
INSERT INTO classes values (null, 'python19', 60);

Single insertion
INSERT INTO Students. Values (null, 'sima2', 18 is, 1.74, 'M',. 1);
INSERT INTO Students. (ID, name) values (null, 'sima3');

Inserting a plurality of
insert into students values (null, ' oyang', 18, 1.74, 'M', 1), (null, 'oyang1', 18, 1.74, 'M', 1);

修改数据
update students set age=38;
update students set age=88 where name='sima3';
update students set high=1.2,gender='M' where name='sima3';

Delete data
delete from students where id = 2;

Data backup
the mysqldump -uroot--proot Python> python.sql
MySQL -uroot--proot Python <python.sql

Alias to the table
SELECT s.id, Students. S.name from AS S;
SELECT students.id, Students. Students.name from AS S; (error)

To re-
select distinct gender from students;

比较运算符
select from students where age>18;
select
from students where age=<18;
select * from students where age!=18;

逻辑运算
select from students where age>18 and age<20;
select
from students where age<18 or age>20;
select * from students where not age>18;

Fuzzy query
the SELECT from the WHERE name like Students 'wang%'; (the beginning of)
the SELECT
from the WHERE name like Students 'wang%%'; (contain)
the SELECT from the WHERE name like Students '__'; (two words of the name)
the SELECT
from students where name like '__%' ; ( larger than the word name)

范围查询
select from students where id in (1,3);
select
from students where id between 1 and 3; (包含1 3)
select * from students where id not between 1 and 3; (包含1 3)

空值判断
select from students where height is null;
select
from students where height is not null;

排序 where 之后
select from students order by height;
select
from students order by height desc;
select * from students order by age desc,height desc;

Aggregation function
select count (*) as count from students; ( record the total number of records)
SELECT max (Age) from Students.; (Maximum age)
SELECT min (Age) from Students.; (Youngest)
SELECT SUM (Age) from Students.; (sum)
SELECT AVG (Age) from Students.; (average)
SELECT round (AVG (Age), 2) from Students.; (rounded, round not aggregate functions)

group分组
select gender from students group by gender; (查询年龄类别)
select gender,count() from students group by gender;
select gender,max(age) from students where gender='M' or gender='W' group by gender;
select gender,group_concat(name) from students group by gender; (group_concat 对应的名字连接)
select gender,avg(age) from students group by gender;
select gender,avg(age) as avg from students group by gender having avg>19;(分组后的年龄大于19岁)
select gender,avg(age),group_concat(name) from students group by gender;
select gender,count(
) as c,group_concat(name) from students group by gender having c>2;
select gender,count(*) from students group by gender with rollup;(总计)

limit restrictions recorded (written in the last sql)
SELECT from Students. 0,2 limit; (0 start, after 2 records)
SELECT
from Students. limit 2;

Standard SQL:
SELECT field from table [where] [group by] [ order by] [limit]

Tab
SELECT from Students. 0,2 limit;
SELECT
(the number of tabs per query, n is the first few pages, m); from students limit ( n-1) * m, m

连接 (on 关联表的条件 where 查询条件)
select from students inner join classes;(笛卡尔积)
select
from students inner join classes where students.cls_id=classes.id;
select students.name,classes.name from students inner join classes where students.cls_id=classes.id;
select s.name,c.name from students as s inner join classes as c where s.cls_id=c.id;
select s.name,c.name from students as s,classes as c where s.cls_id=c.id;
select s.,c.name from students as s inner join classes as c on s.cls_id=c.id;
select
from students inner join classes where students.cls_id=classes.id order by classes.id;
select i.code,i.price,f.note_info from infos i , focus f where i.id=f.id;

External connection: no data is found in the primary table from the table, from the table which corresponds to the NULL
left outer connector (table on the left main table, the table on the right of the table)
SELECT * from the Join classes left Students. Students.cls_id = ON WHERE classes.id classes.name is null;

Right outer join (right table-based table, table from the table on the left)
SELECT * from the Join right Students. Students.cls_id ON = classes.id classes;

自连接
select * from areas as city inner join areas province on city.pid=province.aid where province.title='广西省';

Subquery
1, subquery returns only one data, called scalar subquery
SELECT from Students. WHERE height> (SELECT AVG (height) from Students.);
2, the result is one, i.e., a plurality of rows, referred to as column subquery
SELECT
from students where id in (select id from classes);

去重
select cate_name from goods group by cate_name;
select distinct cate_name from goods;

select * from goods where price > (select avg(price) from goods) order by price desc;

select * from goods
inner join
(select cate_name,max(price) as max_price from goods group by cate_name ) as max_price_goods
on goods.cate_name = max_price_goods.cate_name and goods.price = max_price_goods.max_price;

分组插入
insert into goods_cates(name) (select cate_name from goods group by cate_name);
update goods inner join goods_cates on goods.cate_name=goods_cates.name set goods.cate_name=goods_cates.id;

alter table goods change cate_name cate_id int unsigned not null;

Foreign key constraint: the primary key of a table in another table occurs, the primary key is another foreign key of a table; can not exist without the foreign key data
ALTER Table Goods the Add Foreign Key (cate_id) References goods_cates (ID);
ALTER Table Goods the Add Foreign Key (brand_id) References goods_brands (ID);
Create Table goods_test (
ID int unsigned Primary Key AUTO_INCREMENT,
name VARCHAR (150) Not null,
cate_id int unsigned Not null,
brand_id int unsigned Not null,
Foreign Key (cate_id) References goods_cates (ID ),
foreign key (brand_id) References goods_brands (ID)
);
ALTER Table foreign key goods_test drop foreign key name (show create table goods_test);

View: only query statement execution result field types and constraints, the complex Sql a virtual package table
select goods.name gname, goods_cates.name gcname, goods_brands.name gbname from goods inner join goods_cates on goods.cate_id = goods_cates.id inner join goods_brands on goods.brand_id = goods_brands.id;
create
the create View v_goods_info AS
the SELECT goods.name gname, goods_cates.name GCName, goods_brands.name gbname Goods from
Inner goods.cate_id the Join goods_cates ON = goods_cates.id
Inner ON the Join goods_brands Goods. brand_id = goods_brands.id;
see
Show the Tables;
the SELECT * from v_goods_info;
delete
drop view v_goods_info;

Index: (BTREE)
Premise 1: For a regular need to update and insert a table, where there is no need for a statement index rarely used alone
to view the show index from goods;
create create index idx_1 on goods (name ( 150)) ;
remove the index drop index idx_1 on goods;

set profiling = 1 (time monitoring)
Show Profiles; time view

User Management:
View all users: select host, user from user;
create a user: create user 'wufj1' @ ' %' identified by 'wufj1';
authorized:. Grant select on jing_dong * to 'wufj1' @ '%'; ( multiple databases, execute multiple statements)
privileges: select alert drop insert update delete select => all privileges

指定ip授权:grant all privileges on . to 'root'@'10.3.32.61' identified by 'root' WITH GRANT OPTION;;

所以权限:grant all on wufj.* to 'wufj'@'%' identified by 'wufj' WITH GRANT OPTION;;

Refresh permissions: flush privileges;
view the user permissions: show grants for 'wufj1' @ '%';

Modify permission: grant select, update on jing_dong * to 'wufj1' @ '%' with grant option;.

Change password: alter user 'wufj1' @ '%' identified by '123';

删除用户:
drop user 'wufj1'@'%';
delete from user where host='%' and user='wufj1'

Copying the data between different database tables
INSERT INTO sw_setname SELECT * FROM wangsunew.sw_setname;

Guess you like

Origin blog.51cto.com/13025012/2482112