一、数据的导入导出

1、数据导入:把系统文件的内容保存到数据库的表里

默认只有数据库管理冲数据库服务器本机登录数据库才有数据导入权限

表结构要与文件内容匹配。

把/etc/passwd文件的内容保存到数据库服务器userdb库的userlist表中

mysql> createdatabase userdb;

mysql> create table userlist(

    -> username  varchar(15) not null,

    -> password  char(1) not null,

    -> uid int(3) not null,

    -> gid int (3) not null,

    -> comment varchar(30),

    -> homedir varchar(30),

    -> shell varchar(30) not null,

    -> index(username)

    -> );

mysql>loaddata infile "/etc/passwd" into table userdb.userlist

->fieldsterminated by ":" lines terminated by "\n";

mysql>select  *  fromuserdb.userlist;   //查看表内容

mysql>alter table userlist add id int(3) primary key auto_increment first;   //在userdb库里userlist表添加id字段是主键且自动增长,放在所有字段的上方(第一)

   数据导入命令格式:

load  data infile  ‘要导入的绝对路径文件名’  into table 数据库.表名

   fields terminated by ‘字段间隔符’

   lines terminated by ‘\n’;

   字段间隔符要与文件内的匹配

   要输入导入文件的绝对路径

   导入数据的表字段类型要与文件字段匹配

   默认管理员在本机登录时有权限

\n 表示换行符

2、数据导出:将数据库里的记录保存到系统文件里

   把userlist表中的记录存放到系统文件userlist.txt文件里:

mysql>select * from userdb.userlist into outfile "/mysqlbak/new.txt" fieldsterminated by "#" lines terminated by "\n";

数据导出命令格式:  

SQL查询语句 into  outfile  ‘目录/文件名’        //指文件名导出位置

   【fields  terminated by  ‘字段间隔符’   lines  terminated  by  ‘\n’;   】  可选 默认 tab间隔 回车换行           //lines 行

 

  导出数据的内容由SQL查询语句

   文件名不用事先创建,导出时自定义

   文件名不指定路径时,默认文件将存放在“自己在什么库下执行的该命令,导出文件就存放在那个数据库目录里” 默认存放/var/lib/mysql/下的数据库目录

   指定文件名存放的路径时,要让mysql用户对目录有写的权限并且关闭 selinux

二、表记录的操作(增加记录  删除记录   修改记录  查询记录)

复制表

Create  table  新表名 SQL 查询语句;

Create  table user1    select * from  studb.user ;

复制表时,原表key字段属性不会被复制

 

复制表结构

Create  table  user1  select * from userlistwhere 1=2;

Insert  into user (username)values (null);  给username字段赋为空值

更改表名:

Alter  table  原表名 rename   [to]  新表名

1、查(select)

  select 字段名列表 from表名;    //显示指定表中的指定字段  *匹配所有字段

   select 字段名列表 from 表名 where 条件表达式

 

条件表达式:适用于查询、删除、更新操作:如下

 

   1)数值比较符号

     =    等于    可以做数值比较,也可以做字符比较,做字符比较时,字符用双引号引起      字段名=字段值    字段名=”字段值”

      >  大于

      <  小于

      >=  大于等于

      <=  小于等于

      != 不等于

      =’’ 等于空格

between…and…    在…与…之间   (只能比较数值)

 

      例:selectusername,uid from userlist where uid>10;

          select username,dir from userlistwhere uid between 30 and 40;(包括30和40)

   2)逻辑比较、多条件组合

      in    在…范围内

between…and…    在…与…之间   (只能比较数值)

select  * from user where username in(“bin”,”daemon,””root”);

      not in 不在..范围内

      is null 字段的值为“匹配空”

      is not null   字段的值为非空  “匹配非空”

distinct    不显示字段重复值   //放在字段符列表的前面

select  distinct shellfrom  user2 ;  //去掉shell字段的重复值

逻辑符号(查询条件大于等于2个条件的时候使用逻辑匹配)

and   逻辑与(例举的条件表达式要同时成立才可以,and=条件全部满足)

or    逻辑或(例举的条件表达式只要有一个条件成立就可以or=满足一个就行)

!    逻辑非  【注:不能单独使用,要和别的搭配用,比如!=】

 

     例:selectusername,dir from userlist where uid in (10,20,30);

          select name from userdb.youyang where uid notin (0,3,5,8);

          select username,dir from userlistwhere username in (“root”,”bin”,”admin”);

          select * from userlist where shell isnot NULL;

          select  distinct shell  from  userlist ;

          select username,dir from userlistwhere uid>=30 and uid<=40;

    select * from userlist whereusername="root" or uid=500 or homedir="/root";

select* from userlist where username="root" and uid=500 andhomedir="/root";

delete from mysql.user whereuser="root" and host="192.168.10.20";  //删除mysql.user表里的账户root@192.168.10.20

   3)运算符  (字段类型必须为数值类型)

      +   加法

      -   减法

      *   乘法

      /   除法

      %  取余数

     

as   取别名 (临时性作为显示字段)

    

例:select username,2014-uid as nianling from userlist;

    select name,(2000+uid)/7 as nianling fromuserdb.youyang;

selectname,(2000+uid)*7 as nianling from userdb.youyang;

selectname,100*uid as nianling from userdb.youyang;

select name,(2000+uid)%9 as nianling fromuserdb.youyang;   //(2000+uid)然后%9    就是(2000+uid)然后除以9的余数  答案=余数是3

     

 

 

  4)正则表达式 (用符号匹配一类数值的范围)

Where   字段名   regexp   “正则表达式”      

 . 单个字符   匹配任意一个字符 

*前边的一个字符出现的0次到N次  如:”^reo*” 找以re开头的,有没有o都可以

          ^ 匹配开头   

$匹配结尾   

[] 匹配范围内

[0-9] 匹配所有数字

[a-z] 匹配所有小写字母

Select* from usertab where username regexp 【wcf】

 

正则表达式前边必须加regexp才生效

          例:select *from userlist where 字段名 regexp  ‘^…$’;

       5)模糊查询like

         字段名 like 模糊查询表达式;

          %   匹配0个到N个字符

          列子:”a%”   a后边的任何字符都显示出来

                 “a%b”  a与b中间的任何字符都显示出来 

          _    匹配任意单个字符

          例子:”_a_”   显示出中间带a的 3个字符

                 “_ _a”  显示出最后带a 的3个字符

          例:select username,dir fromuserlist where username like 'a%';

       6)分组和排序

          order by  字段名[asc/desc]   排序 //默认升序

          group by  分组

          having  指定分组条件,用来做分组条件的字段一定要在查询字段内

           limit  限制查询时显示记录的条目数  

limit n,m    n表示从第几条记录显示可选项,不指定时默认从第一条开始显示,第一条记录编

号从0开始,如果从记录的第一条开始显示,n的值可以省略。m表示显示几条记录。 例子:n,m

等于2,3 就是从第2条开始往后显示3条

默认从查询结果的第一条记录开始显示  第一条记录值为0