• Mysql通过控制台CURD


    1、mysql登录:

    mysql -u root -p 或者mysql -uroot -p******

    password:

    2、展示当前所有的db:

    show databases;

    3、创建DB:

    create database mydb1;

    4、对表进行操作,要先选中DB:

    use mydb1;

    5、展示所有的表:

    show tables;

    6、创建表:

    create table tb_student(
    number char(11),
    name varchar(50),
    age int,
    gender varchar(10)
    );

    7、显示表结构
    desc tb_student;

    8、表添加一列
    alter table tb_student
    add(
    education varchar(50)
    )

    9、表删除一列
    alter table tb_student
    drop education;

    10、表修改一列
    alter table tb_student
    modify education varchar(100);

    11、对表进行重命名
    alter table tb_student
    rename to tb_stu;

    12、注意事项:

    ***数据库中的所有字符串类型,必须使用单引!不能使用双引!
    ****日期类型也要使用单引!!

    13、插入数据,完全插入
    insert into tb_student(number,name,age,gender)values('131006101','zhangSan',13,'male');

    14、插入部分数据,其他数据默认为NULL
    insert into tb_student(number,name)values('131006103','liSi');

    15、直接表名后跟values,默认添加全部数据(不建议使用,可读性太低)
    insert into tb_student values('131006102','wangWu',18,'female');

    16、更新数据--设置所有行数据的age都等于18.
    update tb_student set age = 18;

    17、按要求更新数据(下面这两句的效果是一样的)
    update tb_student set age = 45 where number = '131006101' or number = '1310006102';
    update tb_student set age = 45 where number in('131006101','131006102');

    18、查询中的>=、<=和between and的作用一样(下面这两句的效果一样)
    select * from tb_student where age>=20 and age<=40;
    select * from tb_student where age between 20 and 40;

    19、按条件查询时,如果需要查询某字段为null时,需要下面这么写
    select * from tb_student where age is null;
    select * from tb_student where age is not null;

    20、删除表中信息
    delete from tb_student;(表中信息全部删除)
    delete from tb_student where name = 'zhangSan';

    21、/*一般:一个项目创建一个用户!一个项目对应的数据库只有一个!这个用户只能对这个数据库有权限,其他数据库你就操作不了了!!*/

    22、创建用户如

    create user ccZhao@IP地址 IDENTIFIED by 'password';
    create user ccZhao@localhost IDENTIFIED by '123';
    //如果想在任意IP地址上都能登录,按照下面的方式
    create user userName@'%' IDENTIFIED by 'password';

    23、登录的时候这么写:
    mysql -uccZhao -p'password' -h'IP';(访问本机时,我们习惯上省略localhost,即如序号 1  所示)

    24、给用户授权
    grant create,insert,update,delete,drop on mydb1.* to ccZhao@localhost;(.*   代表该DB中的所有表,当然也可以写   .table,特指某张表的某些权限)
    //要是给用户所有权限的话,可以这么写
    grant all on mydb1.* to ccZhao@localhost;


    25、撤销权限
    revoke delete on mydb1.* from ccZhao@localhost;

    //注意:给用户授权、撤销权限、查看权限的时候必须是超级用户,才有这个权限哟!!!

    26、查看用户权限
    show grants for ccZhao@localhost;

    27、删除用户
    drop user ccZhao@localhost;

  • 相关阅读:
    TreeSet集合的add()方法的源码解析
    Ubuntu下定时任务和自启动任务的部署
    基于Django的独立运行脚本开发
    python做量化交易干货分享
    使用Glide以及OkHttp集成
    Redis实现简单消息队列
    Linux下高并发socket最大连接数
    Ubuntu14.04部署pyspider的过程
    pymongo "ServerSelectionTimeoutError: No servers found yet" 错误的解决
    gitolite服务器部署中的一些坑
  • 原文地址:https://www.cnblogs.com/ZT-SummerRain/p/6723712.html
Copyright © 2020-2023  润新知