gmdzxx 发表于 2018-10-6 14:00:33

MySQL数据库(三)

数据导入与导出  (1)数据导入
  mysql>load data infile "目录/文件名" into table 表名    /默认目录为 /var/lib/mysql-files,可修改配置文件改变默认目录,如不修改配置文件,默认且一定要在这个目录下
  >field terminated by "字段间隔符"            /导入的文本数据的字段间隔符号
  >lines terminated by "\n"               /导入的文本数据的行结束符号
  mysql> show variables like "secure_file_priv"; 查看默认目录
  /etc/my.cnf
  secure_file_priv="/myfile"
  把/etc/passwd 数据导入到表user中
  a.创建表user(表中字段要与导入的数据中字段相同)
  create table user(
  >name char(15),
  >passwd char(8),
  >uid int,
  >gid int,
  >comment varchar(50),
  >homedir varchar(30),
  >shell varchar(30),
  >index(name),
  >unique index(uid)
  >);
  b.把/etc/passwd 数据放入默认目录/var/lib/mysql-files
  cp /etc/passwd /var/lib/mysql-files/
  c.数据导入
  load data infile "/var/lib/mysql-files/passwd" into table user \
  >fields terminated by ":" lines terminated by "\n";/此表中字段间隔符为":",行结束符号为"\n".
  d.验证
  select * from user;
  (2)数据导出   把表记录通过sql查询存储到系统文件中
  >sql查询   into outfile "目录/文件名" fields terminated by "字段间隔符" lines terminated by "行结束符号"
  /目录默认/var/lib/mysql-files 如不修改配置文件,默认且一定要在这个目录下
  #把uid小于100的数据导出到user1.txt文件中
  >select * from user where uid < 100 into outfile "/var/lib/mysql-files/user.txt" \
  >fields terminated by "*"\   /字段间隔符为 *
  >lines terminated by "#"       /行间隔符为 #
  #################################################################################
  管理表记录
  增
  insert into 库名.表名 values(值列表),(值列表);            /给所有字段赋值
  insert into 库名.表名(字段名列表) values(值列表),(值列表);/只给个别字段赋值
  查
  select * from 库名.表名;      /查询表所有字段数据
  select 字段名列表 from 库名.表名   /查询个别字段数据
  #select name ,uid from user;
  select * from 库名.表名 where 条件;
  #select * from user where name="root";/查询name字段为root的所有数据
  条件匹配的表示方式:
  数值比较   >>=   <   sql查询 order by 字段名 /默认升序   desc 降序
  #select uid from user order by uid;/查询uid字段数据并升序排序
  #select uid from user order by uid desc;/查询uid字段数据并降序排序
  #select * from user order by uid;/查询表中所有数据并以uid升序排序
  查询分组
  >sql查询 group by 字段名    /把相同的数据放在group组里,相当于不显示重复值
  #select shell from user group by shell;
  限制查询显示行数
  (a)limit 数字n; 显示前n行
  #select * from user limit 3;
  #select * from user where uid between 10 and 50 limit 3;
  (b)limit 数字m,数字n;显示m行后的n行
  #select * from user limit 1,2; 显示第1行后的两行内容 相当于显示2-3行内容
  #select * from user limit 1,1; 显示第二行内容
  嵌套查询 把内层的查询结果作为外层的查询条件
  select * from user where 条件 (select * from 表名 where 条件);
  #select * from user where name in (select name from user where uid (select avg(uid) from user);
  复制表
  作用:快速建表 、 备份表
  create table 表名 sql查询
  #create table user1 select name,uid,homedir from user limit 3; /把sql查询的结果作为新表的结构及数据,
  #create table user2 select name,uid,shell from user limit 4;
  多表查询
  select 字段名列表 from 表名列表;笛卡尔集
  #select * from user1,user2;   /显示为 3*4 12 行,user1表的每一行对应user2表的每一行
  #select * from user1,user2 where user1.name=user2.name and user1.uid=user2.uid; /显示 user1 与user2 name,uid字段相等的行
  连接查询
  (a)左连接查询以左边表(A)为主查询某个条件下 表A,表B的数据
  select * from 表A left join 表B on 条件;
  #select * from user1 left join user2 on user1.uid=user2.uid;
  (b)右连接查询 以左边表(B)为主查询某个条件下 表A,表B的数据
  #select * from user1 right join user2 on user1.uid=user2.uid;
  改
  修改某字段内的记录
  (a)update 表名 set 字段名="";
  #update user1 set uid=3;
  (b)update 表面 set 字段名="" where 条件;
  #update user2 set uid=2 where name="root";
  删
  删除记录
  (a)delete from 表名;/删除表中记录
  #delete from user1;
  (b)delete from 表名 where 条件; /删除某个条件下的一行记录
  #delete from user1 where name="root";

页: [1]
查看完整版本: MySQL数据库(三)