zoukankan      html  css  js  c++  java
  • mysql数据库导入导出 查询 修改表记录

    mysql数据导入导出:

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

    导入数据的基本格式:
    mysql> load data infile "文件名" into table 表名 fields terminated by '分隔符' lines terminated by ' ';

    实例:把系统用户信息保存到hydra01库下的userinfo表里
    mysql> create table userinfo(name char(20),password char(1),uid int(2),gid int(2),comment varchar(50),homedir varchar(60),shell varchar(25),index(name));
    mysql> load data infile "/etc/passwd" into table hydra01.userinfo fields terminated by ":" lines terminated by ' ';(导入数据)
    mysql> alter table userinfo add id int(2) auto_increment primary key first;(添加编号)
    mysql> desc userinfo;
    +----------+-------------+------+-----+---------+----------------+
    | Field | Type | Null | Key | Default | Extra |
    +----------+-------------+------+-----+---------+----------------+
    | id | int(2) | NO | PRI | NULL | auto_increment |
    | name | char(20) | YES | MUL | NULL | |
    | password | char(1) | YES | | NULL | |
    | uid | int(2) | YES | | NULL | |
    | gid | int(2) | YES | | NULL | |
    | comment | varchar(50) | YES | | NULL | |
    | homedir | varchar(60) | YES | | NULL | |
    | shell | varchar(25) | YES | | NULL | |
    +----------+-------------+------+-----+---------+----------------+

    导出:
    把数据库中表里的记录存储到系统文件里

    导出数据的基本格式:
    格式一:mysql> select * from 库.表 into outfile "文件名";
    格式二:mysql> select * from 库.表 into outfile "文件名" fields terminated by "符号";

    实例:把mysql库下user表里的所有记录保存到系统文件xx.txt里
    mysql> select * from mysql.user into outfile "xx.txt";
    [root@mysql ~]# find / -name "xx.txt"
    /var/lib/mysql/xx.txt(导出的内容默认存放在mysql下)

    也可以导出到指定目录下
    [root@mysql ~]# mkdir mysqldata
    [root@mysql ~]# chown mysql /root/mysqldata(更改属主)
    mysql> select * from mysql.user into outfile "/root/mysqldata/xx2.txt";


    ——————————————————————————————————————————————————————————————————

    管理表记录

    向表中插入新记录
    一次向表中插入一条新记录,给新记录的每个字段都赋值
    格式:mysql> insert into 库名.表名 values(字段值列表);
    实例:给hydra01库的userinfo表插入一条新信息
    mysql> insert into hydra01.userinfo values(26,"hydra","x",2003,20003,"hail hydra","/home/hydra","/bin/bash");


    一次向表中插入多条新记录,给新记录的每个字段都赋值
    格式:mysql> insert into 库名.表名 values(字段值列表),values(字段值列表),(字段值列表);
    实例:给hydra01库的userinfo表插入多条新信息
    mysql> insert into hydra01.userinfo values(29,"FBI","x",1937,1937,"hail hydra","/home/hydra","/bin/bash"),(30,"CIA","x",1937,1937,"hail hydra","/home/hydra","/bin/bash");


    一次向表中插入多条新记录,给新记录的指定字段赋值
    格式:mysql> insert into 库名.表名(字段名列表) values(字段值列表);
    实例:给hydra01库的userinfo表插入新信息,并只给指定字段赋值
    mysql> insert into hydra01.userinfo(name,password,uid,gid,comment,homedir,shell)values("Anonymousx","x",1937,1937,"teacher","/home/Anonymousx","/sbin/nogin");

    查看效果
    mysql> select * from userinfo;
    +----+------------+----------+------+-------+------------------------------+---------------------+----------------+
    | 26 | hydra | x | 2003 | 20003 | hail hydra | /home/hydra | /bin/bash |
    | 27 | FBI | x | 2003 | 2003 | hail hydra | /home/hydra | /bin/bash |
    | 28 | CIA | x | 2003 | 2003 | hail hydra | /home/hydra | /bin/bash |
    | 29 | FBI | x | 1937 | 1937 | hail hydra | /home/hydra | /bin/bash |
    | 30 | CIA | x | 1937 | 1937 | hail hydra | /home/hydra | /bin/bash |
    | 31 | Anonymous | NULL | NULL | NULL | NULL | NULL | NULL |
    | 32 | Anonymousx | x | 1937 | 1937 | teacher | /home/Anonymousx | /sbin/nogin |
    +----+------------+----------+------+-------+------------------------------+---------------------+----------------+


    —————————————————————————————————————————————————————————————————————————————————————

    查询表
    格式:
    select 字段名列表 from 库名.表名 where 条件;

    条件的表示方式:
    数值比较:
    条件格式:字段名 符号 数值
    = != < > <= >=
    实例:mysql> select * from userinfo where id <=10;

    字符比较:
    条件格式:字段名 符号 "值"
    = != "
    实例:mysql> select * from userinfo where shell!="sbin/nologin";

    范围匹配:
    条件格式:字段名 符号 匹配
    between..adn.. /在..之间
    in (值列表) /在里面
    not in(值列表) /不在里面
    实例:mysql> select * from userinfo where uid between 10 and 20;

    逻辑匹配:
    条件格式:字段名 符号 匹配 (多个查询条件时使用)
    and:多个条件同时匹配
    or:多个条件,匹配某一条件就可以
    !:取反
    实例:mysql> select * from userinfo where name="mysql" or uid=3000 ;

    匹配空:
    条件格式:字段名 符号 匹配
    is null
    实例:mysql> select * from userinfo where name is null;

    匹配非空:
    条件格式:字段名 符号 匹配
    is not null
    实例:mysql> select * from userinfo where name is not null;

    模糊查询:
    条件格式:字段名 like '表达式'
    %:匹配0个或多个字符
    _:匹配任意一个字符
    实例:mysql> select * from userinfo where name like '____';

    正则匹配:
    条件格式:字段名 regexp '正则表达式'
    ^:开头
    $:结尾
    .:任意字符
    *:任意字符
    []:区间
    实例:mysql> select * from userinfo where uid regexp '[0-100]';

    数学计算:
    条件格式:字段名 计算 as 起名字 from 计算的表;
    + - * / %(取于)
    实例:mysql> select uid,gid,(uid+gid)/2 as zhi from userinfo;

    聚集函数:
    条件格式:select xxx(字段名) from 表;
    max(字段名)求最大值
    min(字段名)求最小值
    avg(字段名)求评价值
    sum(字段名)求和
    count(字段名)统计值个数
    实例:mysql> select max(uid) from userinfo;

    给查询结果排序:
    格式:order by 字段名 排序方式;
    排序方式:
    asc:升序(默认排序方式)
    desc:降序
    实例:mysql> select name,uid from userinfo where uid >=10 and uid <=50 order by uid desc;

    给查询结果分组:
    格式:group by 字段名
    实例: mysql> select shell from userinfo where uid <=10 group by shell;

    限制显示查询结果显示的记录数inmit:
    格式:limit 行数;
    实例:mysql> select name,uid from userinfo where uid <=8 limit 2;


    综合测试:
    1.输出userinfo表中uid号最大的用户信息
    测试:mysql> select * from userinfo order by uid desc limit 1;

    2.输出表中uid号是两位数的最大的用户名和uid号
    测试:mysql> select name,uid from userinfo where uid regexp '^..$' order by uid desc limit 1;


    查看表中符合条件的记录,【所有】字段的值
    格式:mysql> select * from 库名.表名 wher 条件;
    实例:
    mysql> select * from userinfo where id <=10;(数值比较)
    mysql> select * from userinfo where shell!="sbin/nologin";(字符比较)
    mysql> select * from userinfo where uid between 10 and 20;(范围匹配)
    mysql> select * from userinfo where uid in (5,15,25);(范围匹配)
    mysql> select * from userinfo where name in ("apache","mysql");(范围匹配)
    mysql> select * from userinfo where name not in ("apache","mysql");(范围匹配)
    mysql> select * from userinfo where name regexp '[0-9]';(正则)

    查看表中符合条件记录,【指定】字段的值
    格式:mysql> select 指定字段 from 库名.表名 wher 条件;
    实例:
    mysql> select id from userinfo where id <=10;(数值比较)
    mysql> select name from userinfo where shell="sbin/nologin";(字符比较)
    mysql> select name,id from userinfo where uid between 10 and 20;(范围匹配)
    mysql> select name from userinfo where name="mysql" and uid=3000 ;(逻辑匹配)
    mysql> select name from userinfo where name="mysql" or uid=3000 ;(逻辑匹配)
    mysql> select name from userinfo where name is null;(匹配空)
    mysql> select name from userinfo where name is not null;(匹配非空)
    mysql> select name from userinfo where name like '____';(模糊查询)
    mysql> select max(uid) from userinfo;(聚集函数)
    mysql> select uid,gid,(uid+gid)/2 as zhi from userinfo;(数学计算)

    —————————————————————————————————————————————————————————————————————————————————————

    嵌套查询:把内层查询结果作为外层查询的查询条件。
    格式:where 条件(子查询);
    实例:把userinfo表中uid字段的值小于次字段平均值的用户名和uid号显示出来
    测试:mysql> select name,uid from userinfo where uid < (select avg(uid) from userinfo);
    实例:嵌套查询可以跨库查询
    测试:mysql> select name from userinfo where name in (select user from mysql.user where user="root" and host="localhost");

    复制表:(原表的索引不会被复制)
    格式:create table 新名字 select * from 被复制的表
    测试:mysql> create table userinfox select * from userinfo;
    实例:复制userinfo表的前十条
    测试;mysql> create table userinfox1 select * from userinfo limit 10;

    复制表结构:(复制表结构不会复制数据)
    格式:格式:create table 新名字 select * from 被复制的表 where 1 = 2;
    测试:mysql> create table userinfox2 select * from userinfo where 1 = 2;

    多表查询:
    格式一:select 字段名 from 表名,表名;
    格式二:select 字段名 from 表名,表名 where 条件;
    测试:mysql> select * from userinfox,hydra;
    测试:mysql> select userinfo.name,userinfox.name from userinfo,userinfox where userinfo.uid = userinfox.uid;

    连接查询:
    左连接查询:left join ...on(以左边的表为主显示查询结果)
    格式:select * from 左表 left join 右表 on 条件;
    测试:mysql> select * from userinfo left join userinfox on userinfo.name=userinfox.name;

    右链接查询:right join ...on(以右边的表为主显示查询结果)
    格式:select * from 左表 right join 右表 on 条件;
    测试:mysql> select * from userinfo right join userinfox on userinfo.name=userinfox.name;

    修改表记录
    批量修改:
    格式:update 库名.表名 set 字段名=值,字段名=值;
    测试:mysql> update userinfox set uid=0,gid=0;
    修改符合条件记录字段的值:
    格式:update 库名.表名 set 字段名=值,字段名=值 where 条件;
    测试:mysql> update userinfox set homedir="/opt" where name="/bin/bash";


    删除表记录
    删除所有记录:
    格式:delete from 表名;
    测试:delete from hydra01;
    删除符合条件的记录:
    格式:delete from 表名 where 条件;
    测试:mysql> delete from userinfo where id is not null;


    ————————————————————————————————————————————————————————————————————————

  • 相关阅读:
    js高级教程阅读笔记 第一章-js的简介
    angular.element方法汇总
    AngularJS第六课(路由)
    AngularJS第五课(模块,动画,依赖注入)
    javascript基础整理(面试必备)
    Google工具page-speed使用教程(网站性能检测)
    常见前端面试题及答案
    css之布局那些事
    jquery之全屏滚动插件fullPage.js
    Git远程操作详解
  • 原文地址:https://www.cnblogs.com/Hydraxx/p/7460186.html
Copyright © 2011-2022 走看看