从连接数据库到导出文件

1.连接数据库:

   mysql -uroot -p         #执行完后需要输入数据库密码

2.显示现存的库

  show  databases;

3.创建新的库

   create database 库名;

4.进入新的库或进入以前的库

  use 库名;

5.显示本库中已经存在的表:

  show tables;

6.创建新的表

 create table 表名(
            字段名 数据类型,
            字段名 数据类型,
            字段名 数据类型
            )character set utf8; 

7.显示表结构

   desc 表名;

8.插入数据

   插入(insert)
   insert into 表名 values(值1),(值2);
   insert into 表名(字段1,字段2) values(值1),(值2)

9.查询数据(select)
   select * from 表名 where 条件;
   select 字段1,字段2 from 表名 where 条件

   distinct: 不显示字段的重复值
   select distinct 字段1,字段2 from 表名;

   sql高级查询:

        3.select...聚合函数 from 表名
        1.where...   
        2.group by...    分组
        4.having...        分组后再次进行筛选
        5.order by...     排序  默认值为升序
        6.limit m,n;       进行分页  m为页,n为每页的记录条数

         数字代表在查询语句存放的位置

         聚合函数的分类:
              avg(字段名):求该字段的平均值
              sum(字段名):求和
              max(字段名):最大值
              min(字段名):最小值
              count(字段名):统计该字段记录的个数

10.删除表中数据

      1.删除表记录(delete)
          1.delete from 表名 where 条件;
          2.注意:一定要加where,不加where条件,就会全部删除表记录
 11.更新表记录(update)
        1.update 表名 set 字段1=值1,字段2=值2,字段3=值3 where 条件;
        2.注意:
            一定要加where条件,不加条件全部修改

     表字段 和 表记录的操作    
                表字段(alter table 表名)                 表记录
           增          add                               inset into 表名....
           删          drop                             delete from 表名....  
           改          modify                         update 表名 set....
           查          desc 表名;                   select * from 表名....

12.创建主键和自增索引

        1.创建表时
                1.id int primary key auto_increment,
                alter table 表名 auto_increment=10000,
                示例:
                 create table t1

                 ( id int(3) zerofill primary key auto_increment)character set utf8; 
                2.
                    id int auto_increment,
                    name varchar(20),
                    primary key(id,name) #复合主键
            2.已有表
                alter table 表名 add id int primary key;    #没有表字段时
                alter table 表名 modify id int auto_increment;        #已有字段时

             创建普通索引(index)

              create index 字段名 on 表名(字段名);

               示例:create index name on t1(name);
 

13.删除主键
            1.先删除自增长属性(modify)
                alter table 表名 modify id int;
            2.删除主键
                alter table 表名 drop primary key

13.导出

        select ... from 表名
        into outfile '存放路径/文件名'       
        fileds terminated by '分隔符'
        lines terminated by '\n';

14.导入文件

        load data infile '绝对路径/文件名'                  #导入文件
        into table 表名                                  #导入表
        fields terminated by '分隔符'        #字段和单元格的关系
        lines terminated by '\n';

 15.将文件拷贝到数据库搜索路径
       show variables like '%secure%'
       sudo cp 源文件 /var/lib/mysql-files/

            
 

猜你喜欢

转载自blog.csdn.net/zhangshuaijun123/article/details/82287101