MySQL数据库的一些总结和讲义
admin
2023-05-08 19:21:51
0

本文主要给大家介绍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的所有数据

条件匹配的表示方式: 
数值比较   >  >=   <   <=  =  !=
字符比较   =   !=

范围内匹配
(a) 字段名 in (值列表)     在...里
#select * from user where name in ("root","tom","apache");  /字段名匹配几个 查询几条
(b)字段名 between 值1 and 值2   在...之间
#select * from user where uid between 10 and 50;   /字段名匹配几个 查询几条
(c)not in
#select * from user where name not in ("root","tom","apache"); /相当与 in 的取反,匹配的都不显示

匹配空/非空
(a) is null
#select * from user where name is null;
(b) is not null
#select * fro user where name is not null;

不显示重复值
(a)distinct 字段名
#select shell from user; /查询表中的shell字段数据  ,其中有很多重复值
#select distinct shell from user  /把重复的值只显示一次,可以快速查看此字段中都有那些数据

逻辑匹配  :   有多个条件
逻辑与  and    多个条件必须都成立
逻辑或  or        多个条件有一个条件成立即可
逻辑非  !         取反
#select name from user where name="tom" and uid=500;  /查询 name字段为tom 且uid=500 ,显示name字段数据 条件都必须成立
#select name from user where name="root" or uid=100;  /查询name字段数据 其条件为 name=“root” 或者uid=100

数学运算操作   +   -   *   /   % 
字段类型必须是数值类型
#select uid,gid,uid+gid he from user; /查询 uid,gid 和uid+gid 和 的字段信息   he(uid+gid的值的字段名,可变)

模糊查询
where 字段名 like '表达式'
(a)"_" 表示一个字符
#select name from user where name like 'r_o_t';    /一个'_'表示一个字符
(b) % 0个或多个字符
#select name from user where name like 'r%';    /查询name字段r开头的所有数据
#select name from user where name like '%r%';   /查询name字段包含r的数据
#select name from user where name like '%t';    /查询name字段t结尾的所有数据
#select name from user where name liek 'r%' or name like '%t'; /以r开头或者t结尾

正则匹配
where 字段名 regexp '正则表达式'
#select name from user where name regexp '[0-9]';  /查询name字段中有数字的数据
#select name from user where name regexp '^[0-9]';  /查询name字段中 数字开头的数据
#select name from user where name regexp '^r';     /查询name字段中 r开头的数据
#select name from user where name regexp 'r.*t';  /查询name字段中,r t之间包含0个或多个字符的数据

统计函数   字段得是数值类型

(a)sum(字段名) 求和
#select sum(uid) from user;
(b)avg(字段名) 平均值
#select avg(uid) from user;
(c)max(字段名) 最大值
#select max(uid) from user;
(d)min(字段名) 最小值
#select min(uid) from user;
(e)count(字段名) 统计个数
#select sum(uid) from user;   /不统计字段值为null  

查询排序
>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<10);
#select name,uid 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";

看了以上关于MySQL数据库的一些总结和讲义,希望能给大家在实际运用中带来一定的帮助。本文由于篇幅有限,难免会有不足和需要补充的地方,如有需要更加专业的解答,可在官网联系我们的24小时售前售后,随时帮您解答问题的。

相关内容

热门资讯

凤凰独家:特朗普二儿子与夫人参... 凤凰独家报道,中美元首会谈期间,特朗普二儿子埃里克·特朗普与夫人参观人民大会堂,并在《江山如此多娇》...
联合国赞赏中美就伊朗问题保持沟... 联合国秘书长古特雷斯13日通过副发言人哈克表示,欢迎中美元首会晤,赞赏两国通过对话沟通妥善处理分歧。...
特稿|拉长合作清单 贡献建设性... 新华社北京5月13日电 题:拉长合作清单 贡献建设性力量——美国商学界人士瞩望美中经贸关系互利共赢新...
中国量子计算新突破!“九章四号... 记者从中国科学技术大学获悉,由该校潘建伟院士领衔的科研团队联合国内多家科研机构、大学,近期成功研制出...
跳河救人的外卖小哥找到了! 外... 5月12日下午5时许,漯河市郾城区孟庙镇幸福渠河堤旁,57岁的甘女士蹲在河边打水,准备回家给鱼换水,...
今年以来,越来越多美国交流团来... 4月,数十名美国犹他州青少年来豫参加2026年YES项目交流活动。图为美国青少年在郑州体验书法项目。...
“打工机器人”亮相郑州街头 机器人服务员“小盖”在郑州街边的一零售店工作。 王磊 摄机器人当服务员,在街头卖咖啡——这不是科幻电...
打响“河南服务”品牌丨盾构机有... 【开栏的话】为深入贯彻落实全省服务业大会精神,本报即日起开设“打响‘河南服务&rsquo...
一季度我国数字产业收入9.5万... 【大河财立方消息】5月14日,工信部发布的数据显示,一季度,我国数字产业实现良好开局,行业利润大幅改...
一体推进整治形式主义为基层减负... 形式主义实质是主观主义、功利主义,根源是政绩观错位、责任心缺失。当前,各地以深化“六个纠治”为抓手,...