mysql 数据库操作

1.  mysql -uroot -pqwe123    (进入mysql)  -u = user      -p = password

2.  q退出

3.  select user();  查看当前用户

创建用户的三步:

  1.  create user "用户名"@"%" identified by "密码"  ;  (创建用户," ; "不能少)

  2.  grant all on *.* to "用户名"@"%";

  3.  flush privileges;

库操作:

  1.  select database(); 查看当前库

  2.  show databases;  查看所有库

  3.  create database  名称 ;   创建一个库,若已存在,则报错

  4.  create database if not exists 名称;      同上,但存在不报错

  5.  drop database [if exists] 名称 ; 删除库

  6.  use 名称;  使用库,进入库
表操作:

  1.  show tables;  查看所有表

  2.  create talbe name1(

        id  int,

        name   varchar(20)

      );            创建表,要把字段也定义

  3.  drop table 表名;      删除表

  4.  desc 表名;    查看该表

  5.  show create table 表名 ;  同上

  6.  增加字段

     alter table 表名 add sex char(20) first;

     alter table 表名 add sex char(20) after name;

  7.  删除字段

     alter table 表名 drop sex;  

  8.  修改字段:

     alter table 表名 modify sex int;  (修改数据类型)

     alter table 表名 change id sex char(4);    (修改字段名)

  

  9.  修改表名:

     alter table 表名 rename 新表名;

  数据操作:

    1. 增加数据:

       insert into 表名( id ,name) values(1,"haha"), (3,"bebe");    插入数据,多条

       insert into 表名( id ,name) value(1,"haha");  插入单条

       insert into 表名  set id = 2,name = "qiugei"  插入,不常用,用上面

    2.  查询数据:

       select * from 表名;    查看该表的所有数据

       select * from T1 where id = 1;    根据条件查询数据,where 筛选条件

    3.  修改数据:

       update T1(表名) set name = "qiaqia"  where id = 1;  修改数据

    4.  删除数据:

       delete from T1 where name = "dada";    删除数据

    5.  数据约束:

        create table test(

            id int primary key auto_increment,     主键,自增长约束

            name varchar(20) not null,          非空约束

            number int  unique key,          唯一约束

            sex char(4) default "man",          默认约束

             f_id int  not null,            外键约束,下面是外键定义

             [constraint f_name] foreign key(f_id) references test2(id)  该表的外键与test2表的id字段关联

             )

        联合主键:

        create table test3(

          id int not null,

          c_id  int,

          primary key(id,c_id))

   查询操作:

      1.  select * from student where name like "王%";    查询name以王字开头的数据

      2.  select * from student where name like "王_";    查询name以王字开头后面只有一个字符的

      3.  排序:

         select * from student where name like "王%"  order by class_id; 以class_id字段升序

         select * from student where name like "王%"  order by class_id desc; 以class_id字段降序

      4.  限制输出:

         select * from test1 limit 2 ;    只查询出前2行数据

         select * from test1 limit 2,4;    从第三行开始,往下找4条数据

       5.  聚合函数:

          MAX  求最大

          MIN   求最小

          SUM    求和

          AVG    求平均

          ROUND    四舍五入

          COUNT    统计次数

          GROUP BY    分组

            select class,group_concat(name) from student group by class;  以班级分组,并罗列出每组里面分别的name

            select count(*) from test1 group by class_id;      把class_id相同的数据分为一组

            select count(class_id) from test1 group by class_id having count(class_id) = 2;    把class_id个数为2的数据查出来

        6.子查询:

           select *from test1 where class_id > (select avg(class_id) from test1);    查询class_id大于class_id 平均值的数据

           select * from test1 where class_id in (select class_id from test2 where name = "计算机" )    用于搜索一个本表没有的字段

      7.  表连接 :

        内连接:

          select * from test1 inner join test2     无条件内连接

          select * from test1 inner join test2  on test1.c_id = test2.c_id   有条件内连接

        外连接(必须有条件):

        左外:select * from test1 left join test2 on test1.c_id = test2.c_id;

        右外:   select * from test1 right join test2 on test1.c_id = test2.c_id;

        多表连接 :
          select * from test1

          left join test2 on test1.c_id = test2.c_id

          left join test3 on test2.s_id = test3.s_id;

          多行写,可读性较高

        快捷储存一张表:

          create table tb1(
           select * from test1 right join test2 on test1.c_id = test2.c_id

            );

          调用:

          select * from tb1;

    事务操作(必须确认才能加入数据):

      1.  start transaction;   开启事务

      2.  insert .....      插入数据

      3.  rollback;      回滚操作

      4..  commit;      确认

    表关系:

        

原文地址:https://www.cnblogs.com/cxhzy/p/9895770.html