美文网首页程序员
MySQL使用总结

MySQL使用总结

作者: 孟军伟 | 来源:发表于2018-07-22 11:41 被阅读0次

数据库

- 创建数据库

    - create database 数据库名称 charset=utf8;

- 使用数据库

    - use 数据库名

- 查看创建数据库语句

    - show create database 数据库名

- 查看所有的数据库

    - show databases;

- 删除数据库

    - drop database 数据库名;

- 查看当前使用的数据库

    - select database()

数据表

- 创建数据表

    - create table 表名(

    - id int primary key auto_increment not null,

    - name varchar(40) default '',

    - price decimal(5,2),

    - cate_id int unsigned,

    - is_show bit default 1,

    - foreign key(cate_id) references goods_cates(id),

    - ) select 语句

- 查看表结构

    - desc 表名;

- 显示数据库中所有的表

    - show tables;

- 删除数据表

    - drop tables 表名

- 查询数据表

    - select distinct *

    - from 表名

    - where ....

    - group by 字段名... (having ...|group_concat()|聚合函数|with rollup)

    - order by 字段名 ...

    - limit start,count

    - 完整的select语句执行顺序

        - from 表名

        - where ....

        - group by ...

        - select distinct *

        - having ...

        - order by 字段名 ...

        - limit start,count

    -    连接查询

        - select * from 表1  (inner|left|right) join 表2 on 表1.字段 = 表2.字段 (inner|left|right) join 表2 on 表1.字段 = 表2.字段;  相当于一张虚表

    - 子查询

        - select 嵌套select    子查询 为可以返回一行一列,一列,一行,表 ;可以结合 in ,  is  ,between  下限  and 上限等使用

    - 视图

        - 创建视图

            - create view v_视图名 as select语句

        - 查看视图

            - select * from 视图名

        - 删除视图

            - drop view 视图名

        - 查看视图是否创建成功

            - show tables;

- 表数据修改

    - 插入数据

        -  insert into 表名(字段,...) values(),()

        - insert  into 表名 (字段,...)  select * from 表名 group

    - 修改数据

        - update 表名 set 字段1 = 值1, 字段2=值2...  where 条件

    - 删除记录

        - delete from 表名 where 条件

        - update 表名 set isdelete = 1 where 条件

字段

- 添加字段

    - alter table 表名 add 字段 类型 约束条件;

    - alter table 表名 add foreign key (字段) references 外键表名 (关联字段);

- 修改字段

    - alter table 表名 change 旧字段名称 新字段名称 类型 约束条件,change 旧字段名称 新字段名称 类型 约束条件 ;

    - alter table 表名 modify 类型 约束条件;

- 删除字段

    - alter table 表名 drop 字段;

    - alter table 表名 drop foreign key 外键名称

聚合函数

- count 统计记录数量 - 计数(不统计字段中的null项)

-  max  求出集合中的最大值

- min  求出集合中的最小值(如果其中有空值,最小值不为空,从非空中查找最小值)

- sum 对集合中的某个字段数据求和

-  avg 对集合中的某个字段数据求平均值

- round(小数, 保留的位数,在进行取舍的时候遵循四舍五入的规则)

事务

- 具有原子性、一致性、隔离性、持久性

- set autocommit = 0在黑窗口中关闭自动提交 rollback

索引

- 创建索引

    - create index 索引名 on 表名(字段名(长度))

- 展示索引

    - show index from 表名

- 删除索引

    - drop index 索引名 on 表名

数据库账户管理

- 查看所有用户

    - 进入mysql系统数据库

    - select host,user,authentication_string from user;

- 创建账户

    - grant 权限列表 on 数据库 to '用户名'@'访问主机' identified by '密码';

- 修改权限

    - grant 权限名称 on 数据库 to 账户@主机 with grant option;

- 修改密码

    - update user set authentication_string=password('新密码') where user='用户名';

- 删除账户

    - drop user '用户名'@'主机';

    - delete from user where user='用户名';

- 刷新权限

    - flush privileges

相关文章

网友评论

    本文标题:MySQL使用总结

    本文链接:https://www.haomeiwen.com/subject/deelmftx.html