MySQL数据库(三)数据导入导出 ,管理表记录,匹配条件,MySQL管理工具

一、数据导入导出
1.检索目录:数据导入导出时文件存放目录
mysql> show variables;   ==>>查看当前系统的所有变量值
mysql> show variables like "%file%";  ==>>查看当前系统的所有变量值包含file的
mysql> show variables like "secure_file_priv";   ==>>查看数据导入导出时文件存放目录
+------------------+-----------------------+
| Variable_name    | Value                 |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+
修改数据导入导出时文件存放目录
[root@mysql50 ~]# mkdir /myload
[root@mysql50 ~]# chown mysql /myload
[root@mysql50 ~]# vim /etc/my.cnf
secure_file_priv=/myload/    ==>>添加
[root@mysql50 ~]# systemctl restart mysqld
mysql> show variables like "secure_file_priv";
+------------------+----------+
| Variable_name    | Value    |
+------------------+----------+
| secure_file_priv | /myload/ |
+------------------+----------+

2.数据导入:把系统文件内容存储到数据库的表里
    命令格式:
    mysql> load data infile "文件的绝对路径" into table 库名.表名 
    -> fields terminated by "分隔符"
    -> lines terminated by "\n";
mysql> system cp /etc/passwd /myload/       ==>>添加system 执行系统命令
mysql> create database db3;
mysql> create table db3.user(
    -> name char(50),
    -> password char(1),
    -> uid int,
    -> gid int,
    -> comment varchar(150),
    -> homedir char(80),
    -> shell char(100)
    -> );
mysql> load data infile "/myload/passwd" into table db3.user 
    -> fields terminated by ":"
    -> lines terminated by "\n";
mysql> alter table db3.user add id int primary key auto_increment first;
mysql> mysql> select * from db3.user;

3.数据导出:把表记录存储到系统文件里
    命令格式:
    select命令 into outfile “目录/文件名”
    fields terminated by "分隔符"           ==>>选用
    lines terminated by "\n";          ==>>选用
mysql> select name,password from db3.user into outfile "/myload/user1.txt";

二、管理表记录
1.插入表记录
    命令格式:
    插入1条记录给所有字段赋值
    insert into 库名.表名 values();
    插入多条记录给所有字段赋值
    insert into 库名.表名 values(),(),();
    插入1条记录给指定字段赋值
    insert into 库名.表名(字段名) values();
    插入多条记录给指定字段赋值
    insert into 库名.表名(字段名) values(),(),();
2.查询表记录
    命令格式:
    select 字段名列表 from 库名.表名 [ where 条件];
3.更新表记录,修改指定列的值
    命令格式:
    update 库名.表名 set 字段名=值,字段名=值,....[ where 条件];
4.删除表记录
    命令格式:
    delete from 库名.表名 [ where 条件];
    
三、匹配条件
1.基本匹配条件(适用于select,update,delete)
数值比较
mysql> select * from db3.user where id <= 10;
mysql> select * from db3.user where uid != gid;
字符比较
mysql> select name,shell from db3.user where shell != "/bin/bash";
匹配空 is null
mysql> select id,name from user where name is null;
匹配非空 is not null
mysql> select id,name from user where name is not null;
逻辑匹配:多个判断条件时使用
    逻辑与  and或&&
    逻辑或  or 或||
    逻辑非  not或!
mysql> select * from user where name="root" or uid=1;
范围匹配
    between 数字1 and 数字2
    in (值列表)
    not in (值列表)
mysql> select * from user where uid between 10 and 30;
mysql> select * from user where uid in(3,4,5,6,8,9,1);

2.高级匹配条件(适用于select,update,delete)
模糊查询 like
    通配符 _表示一个字符 %表示0~n个字符
mysql> select name from db3.user where name like '____';
mysql> select name from db3.user where name like '__%__';
mysql> select name from db3.user where name like '%a%';
正则匹配 regexp
mysql> select name from db3.user where name regexp '^a|t$';
mysql> select name from db3.user where name regexp '^r.*t$';
四则运算
mysql> select name,uid from db3.user where uid % 2 = 0;
mysql> update db3.user set uid=uid+1 where id <= 3;
mysql> select name,uid,gid,(uid+gid)/2 pjz from db3.user where name="mysql";
+-------+------+------+---------+
| name  | uid  | gid  | pjz     |
+-------+------+------+---------+
| mysql |   27 |   27 | 27.0000 |
+-------+------+------+---------+
mysql> select name,age,2019 - age s_year from db3.user where name="root";
+------+------+--------+
| name | age  | s_year |
+------+------+--------+
| root |   19 |   2000 |
+------+------+--------+

3.操作查询结果(适用于select)
    聚集(内置)函数:
    avg(字段名)      统计字段平均值
    sum(字段名)      统计字段之和
    min(字段名)      统计字段最小值
    max(字段名)      统计字段最大值
    count(字段名)    统计字段值个数
mysql> select count(*) from user;
mysql> select count(name) from user;
mysql> select max(gid) from user where gid >= 300;
mysql> select avg(age) from user;
mysql> select sum(age) from user;

    查询结果排序:
    sql查询 order by 字段名 [asc|desc];
        asc  升序排序
        desc 降序排序
mysql> select name,uid from user
    -> where uid 
    -> between 10 and 100
    -> order by uid desc;

    查询结果分组:
    sql查询 group by 字段名;
mysql> select shell from user where uid <= 10 group by shell;

    查询结果过滤:
    sql查询 having 条件表达式;
mysql> select name from user where shell != "/bin/bash" having name="mysql";

    限制查询结果显示行数
    sql查询 limit 数字;         ==>>显示查询结果钱多少条记录
    sql查询 limit 数字1,数字2; ==>>显示指定范围内的查询记录
        数字1 起始行(0表示第一行)
        数字2 总行数
mysql> select name,uid,gid from user limit 3,3;
+------+------+------+
| name | uid  | gid  |
+------+------+------+
| adm  |    3 |    4 |
| lp   |    4 |    7 |
| sync |    5 |    0 |
+------+------+------+
mysql> select name,uid,gid from user limit 3,4;
+----------+------+------+
| name     | uid  | gid  |
+----------+------+------+
| adm      |    3 |    4 |
| lp       |    4 |    7 |
| sync     |    5 |    0 |
| shutdown |    6 |    0 |
+----------+------+------+

四、MySQL管理工具
phpMyAdmin
[root@mysql50 ~]# yum -y install httpd php php-mysql
[root@mysql50 ~]# systemctl enable --now httpd             ==>>启动服务并加入开机自启
[root@mysql50 ~]# tar -zxf phpMyAdmin-2.11.11-all-languages.tar.gz 
[root@mysql50 ~]# mv phpMyAdmin-2.11.11-all-languages /var/www/html/phpmyadmin
[root@mysql50 ~]# cd /var/www/html/phpmyadmin              
[root@mysql50 phpmyadmin]# cp config.sample.inc.php config.inc.php 
[root@mysql50 phpmyadmin]# vim config.inc.php 
 17 $cfg['blowfish_secret'] = 'ssh123';
 31 $cfg['Servers'][$i]['host'] = 'localhost';
[root@mysql50 ~]# netstat -lntup|grep :80
tcp6       0      0 :::80                   :::*                    LISTEN      54821/httpd    
[root@mysql50 ~]# systemctl stop firewalld
浏览器访问http://192.168.4.50/phpmyadmin

你可能感兴趣的:(MySQL,linux,服务器)