zoukankan      html  css  js  c++  java
  • mysql常用操作

    一. 用户管理

    a. 用户管理

    1
    2
    3
    4
    5
    6
    7
    8
    #创建用户
        create user '用户名'@'IP地址' identified by '密码';
    #删除用户
        drop user '用户名'@'IP地址';
    #修改用户
        rename user '用户名'@'IP地址'; to '新用户名'@'IP地址';;
    #修改密码
        set password for '用户名'@'IP地址' = Password('新密码')

    b. 授权管理

    1
    2
    3
    show grants for '用户'@'IP地址'                 # -- 查看权限
    grant  权限 on 数据库.表 to   '用户'@'IP地址'     # -- 授权
    revoke 权限 on 数据库.表 from '用户'@'IP地址'     # -- 取消权限
    按 Ctrl+C 复制代码
    按 Ctrl+C 复制代码

    二. 库操作

    a. 创建数据库

    1
    2
    3
    4
    5
    6
    # utf-8
    CREATE DATABASE 数据库名称 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
    create database olddog CHARACTER SET utf8  COLLATE utf8_general_ci;
     
    # gbk
    CREATE DATABASE 数据库名称 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

    b. 数据库常用操作

    1
    2
    3
    4
    5
    show create database oldboyG;      #查看创建库的信息
     
    show databases;
    show databases like "%old%";
    select database();           #查看进入的数据库

    c. 常用命令

    select version();       #查看版本
    select user();          #查看当前的用户
    select now();           #查看当前时间
    help create database    #查看创建数据库帮助
    show character set;     #查看字符集
      
    create database oldboy CHARACTER SET utf8 COLLATE utf8_general_ci;   #创建数据库
    grant all on oldboy.* to tom@localhost identified by '123456';       #授权用户
    show grants for tom@localhost;                                       #查看用户的权限
    select user,host from mysql.user;                                    #查看有哪些用户
    use test;
    mysql> create table test(
        -> id int(4),
        -> name varchar(16)
        -> )ENGINE=innodb default charset=utf8;
    show create table testG;                                       #查看创建的表
    desc test;                                                      #查看表结构
    insert into test values(1,'oldboy');                            #插入数据
    update 表名 set 字段=“” where 字段......;                       #修改字段数据
    delete from 表名 where 条件;                                   #删除字段数据
      
    2、查询(DQL)
        select user,host,password from mysql.user;        #正常查询
        select user,host,password from mysql.user order by user asc;   #升序查询
        select user,host,password from mysql.user order by user desc;  #倒序查询
      
    3、数据操作语言(DML)INSERT UPDATE DELETE
        delete from mysql.user where user="tom";
      
    4、事物处理语言(DPL)BEGIN TRANSACYION,COMMIT,ROLLBACK
    5、数据控制语言(DCL)GRANT REVOKE
    6、数据定义原因(DDL)CREATE DROP ALTER

      

    三. 表操作 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    #创建表
       
    create table 表名(
        列名  类型  是否可以为空,
        列名  类型  是否可以为空
    )ENGINE=InnoDB DEFAULT CHARSET=utf8
       
    #删除表
       
    drop table 表名
       
    #清空表
       
    delete from 表名
    truncate table 表名   #推荐使用
       
       
    #修改表
       
      #添加列:alter table 表名 add 列名 类型
      #删除列:alter table 表名 drop column 列名
      #修改列:
            alter table 表名 modify column 列名 类型;  -- 类型
            alter table 表名 change 原列名 新列名 类型; -- 列名,类型
         
      #添加主键:
            alter table 表名 add primary key(列名);
      #删除主键:
            alter table 表名 drop primary key;
            alter table 表名  modify  列名 int, drop primary key;
        #自增主键修改  alter table db1 AUTO_INCREMENT=10;
         
      #添加外键:alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
      #删除外键:alter table 表名 drop foreign key 外键名称
         
      #修改默认值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
      #删除默认值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
       
       
    #修改表名
    rename table 表名old to 表名new;
    alter table 表名old rename to 表名new; 

    四. 表内容操作

    a. 增

    1
    2
    3
    insert into 表 (列名,列名...) values (值,值,值...)
    insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)
    insert into 表 (列名,列名...) select (列名,列名...) from 

    b. 删

    1
    2
    delete from 
    delete from 表 where id1 and name='alex'

    c. 改

    1
    update 表 set name = 'alex' where id>1

    d. 查

    1
    2
    3
    select * from 
    select * from 表 where id 1
    select nid,name,gender as gg from 表 where id 1  

     

    五. SQL语句数据 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    1、增
     
    insert into 表 (列名,列名...) values (值,值,值...);
    insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...);
    insert into 表 (列名,列名...) select (列名,列名...) from 表;
    insert into tb11(name,age) values('alex',12);
    insert into tb11(name,age) values('alex',12),('root',18);
    insert into tb12(name,age) select name,age from tb11;
     
     
    2、删
     
    delete from 表;
    delete from 表 where id1 and name='alex';
    delete from tb12 where id >=2 or name='alex';
     
     
    3、改
     
    update 表 set name='alex' where id>1;
    update tb12 set name='alex' where id>12 and name='xx'
    update tb12 set name='alex',age=19 where id>12 and name='xx'
     
     
    4、查
     
    select * from tb12;        
    select id,name from tb12;
    select id,name as cname from tb12 where id 10 or name ='xxx';
    复制代码
     1 a、条件
     2     select * from 表 where id > 1 and name != 'alex' and num = 12;
     3 
     4     select * from 表 where id between 5 and 16;
     5 
     6     select * from 表 where id in (11,22,33)
     7     select * from 表 where id not in (11,22,33)
     8     select * from 表 where id in (select nid from 表)
     9 
    10 b、通配符
    11     select * from 表 where name like 'ale%'  - ale开头的所有(多个字符串)
    12     select * from 表 where name like 'ale_'  - ale开头的所有(一个字符)
    13 
    14 c、限制
    15     select * from 表 limit 5;            - 前5行
    16     select * from 表 limit 4,5;          - 从第4行开始的5行
    17     select * from 表 limit 5 offset 4    - 从第4行开始的5行
    18 
    19 d、排序
    20     select * from 表 order by 列 asc              - 根据 “列” 从小到大排列
    21     select * from 表 order by 列 desc             - 根据 “列” 从大到小排列
    22     select * from 表 order by 列1 desc,列2 asc    - 根据 “列1” 从大到小排列,如果相同则按列2从小到大排序
    23 
    24 e、分组
    25     select num from 表 group by num
    26     select num,nid from 表 group by num,nid
    27     select num,nid from 表  where nid > 10 group by num,nid order nid desc
    28     select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid
    29 
    30     select num from 表 group by num having max(id) > 10
    31 
    32     特别的:group by 必须在where之后,order by之前
    33 
    34 f、连表
    35     select * from userinfo5,department5
    36                     
    37     select * from userinfo5,department5 where userinfo5.part_id = department5.id
    38     
    39 
    40     select * from userinfo5 left join department5 on userinfo5.part_id = department5.id
    41     # userinfo5左边全部显示
    42     
    43     
    44     # select * from userinfo5 right join department5 on userinfo5.part_id = department5.id
    45     # department5右边全部显示
    46     
    47     
    48     select * from userinfo5 innder join department5 on userinfo5.part_id = department5.id
    49     将出现null时一行隐藏
    50     
    51     
    52     
    53     select * from department5 
    54     left join userinfo5 on userinfo5.part_id = department5.id
    55     left join userinfo6 on userinfo5.part_id = department5.id
    复制代码
  • 相关阅读:
    MogoDb的使用及配置
    HttpClient中转上传文件
    springboot +Thymeleaf+UEditor整合记录
    Linux 下安装mysql
    Java开发微信公众号(五)---微信开发中如何获取access_token以及缓存access_token
    Java开发微信公众号(四)---微信服务器post消息体的接收及消息的处理
    Java开发微信公众号(三)---微信服务器请求消息,响应消息,事件消息以及工具处理类的封装
    Java开发微信公众号(二)---开启开发者模式,接入微信公众平台开发
    Java开发微信公众号(一)---初识微信公众号以及环境搭建
    mybatis sql转义符号
  • 原文地址:https://www.cnblogs.com/ppf3678/p/10171447.html
Copyright © 2011-2022 走看看