zhouying23 发表于 2018-10-6 12:28:47

初识Mysql(三)


目录
  1 数据导入
  
2 数据导出
  3 管理表记录
1 数据导入
  1.1 查看默认使用的目录及目录是否存在:
  mysql> show variables like "secure_file_priv";
  数据导入导出使用的目录:/var/lib/mysql-files/
  1.2 修改目录
  修改配置文件中导入数据存放的路径
  vim /etc/my.cnf#打开配置文件
  
  secure_file_priv=”/myload”
  mkdir /myload ;chown mysql /myload #创建目录及修改所有者(mysql写)
  systemctl restart mysqld#重启服务
  注意selinux需要关闭
  mysql> show variables like "secure_file_priv"; #此时路径已改变
  1.3 导入格式
  导入之前创建新的数据库,新的表;
  且导入的数据要符合表字段的类型;
  load data infile “目录名/文件名” into table 表名
  fields terminated by “分隔符”
  lines terminated by “\n”;
2 数据导出
  2.1 导出格式
  只导出表内的数据,字段并没有被导出;导出的路径必须在定义的路径下;
  select 字段名(*) from 表名 into outfile “/目录/文件” ;
  fields terminated by “分隔符”lines terminated by “\n”;   #可加可不加
3 管理表记录
  3.1 增 插入表记录
  一次插入1条记录给所有字段赋值
  insert into 表名 values(值列表);
  一次插入多条记录给所有字段赋值
  insert into 表名 values(值列表),(值列表),(值列表);
  一次插入1条记录给指定字段赋值
  insert into 表名(字段名)values(值列表);
  一次插入多条记录给指定字段赋值
  insert into 表名(字段名)values(值列表),(值列表),(值列表);
  3.2 查询表记录
  3.2.1单表查询
  select 字段名 from 表名;
  select 字段名 from 表名 where 条件;
  3.2.2where嵌套查询
  把内层查询结果作为外查询的条件
  同表查询:
  select 字段名 from 表1 where 条件 (select 字段名 from 表1);
  select 字段名 from 表1 where 条件(select 字段名 from 表1 where条件);
  不同表查询:
  select 字段名 from 表1 where 条件 (select 字段名 from 表2);
  select 字段名 from 表1 where 条件 (select 字段名 from 表2 where条件);
  3.2.3多表查询:
  格式1:select 字段 from 表1,表2; (笛卡尔集,多匹配少,出现重复)
  格式2:select 字段 from 表1,表2where 条件;
  连接查询:
  内连接查询:select 字段 from 表1,表2;
  外连接查询:
  左连接查询(以左表记录为主)
  select 字段 from 表1 left join 表2 on 条件;
  右连接查询(以右表记录为主)
  select 字段 from 表1 right join 表2 on 条件;
  案例:新建两张表,分别取/etc/passwd的前3和前5行
  select * from t3,t4;
  select * from t3,t4 where t3.name=t4.name;
  select * from t3 leftjoin t4 on t3.uid=t4.uid ;
  select * from t3 right join t4 on t3.uid=t4.uid ;
  select t3.* from t3 leftjoin t4 on t3.uid=t4.uid ;
  select t3.shell from t3 leftjoin t4 on t3.uid=t4.uid ;
  3.3 匹配条件的表示方式
  3.3.1 数值比较
  = ; 或!= ; > ; < ; >= ;select user,2018-s_year as age from userinfowhere user=&quot;root&quot;;
  mysql> select uid,gid,uid+gid as sum from userinfowhere user=&quot;mysql&quot;;
  mysql> select uid,gid,uid+gid as sum,(uid+gid)/2 as avgfrom userinfo where user=&quot;mysql&quot;;
  3.3.4 模糊查询 like
  where 字段名 like “通配字串”;
  _ : 匹配单个字符
  % : 匹配0个或多个字符
  3.3.5使用正则表达式
  where 字段名 regexp ‘正则表达式’;
  3.4 常用函数
  avg() : 集合的平均值
  sum() : 对集合的各参数求值
  min() : 集合中的最小值
  max() : 集合的最大值
  count() : 记录的个数 #空值不计算在内
  3.5 查询结果排序/分组
  order by 字段名;
  select查询 order by 字段名asc/desc;
  group by 字段名
  select查询 group by 字段名
  和select distinct查询一样(查找不重复的)
  limitn,m#限制显示记录条目数
  n 表示起始行 第一行的编号是0   m 表示总行数
  mysql> select *from userinfo limit 1,1;#显示第二行(1行以后的1行)
  3.6 更新记录
  update 表名 set 字段=值,字段=值;    #批量修改
  update 表名 set 字段=值,字段=值where 条件;#按条件修改
  3.7 删除记录
  delete from 表名;#删除所有
  delete from 表名 where 条件: #删除符合条件的
  truncate table 表名; #快速删除所有数据
  3.8 表的复制及改名
  将源表xxx复制为新表yyy
  create table yyy select * from xxx;
  将指定的查询结果复制为新表zzz
  create table zzz select查询;
  注意:复制过来的新表的key值是无法继承的;
  将源表的结构复制到新表vvv
  create table vvv select * from xxx where false;(false条件不成立)
  将源表改名为mmm
  alter table xxx rename to mmm;

页: [1]
查看完整版本: 初识Mysql(三)