数据库MySQL

数据库基础篇总结

2018-10-15  本文已影响101人  aec367caa311

连接数据库的方式

  1. 使用命令进入本机数据库(本机需要安装有数据库)
    命令:mysql -h localhost -uroot -proot
  2. 客户端连接数据库(需要安装数据库的客户端)
    填入ip/域名
    填入用户名
    填入密码
    填入端口


    数据库客户端
  3. 写代码连接数据库
    Java代码可以使用JDBC连接数据库用sql脚本使用数据库数据

条件运算符和常用关键字

  • in:同一个字段有多个筛选条件,每个条件都需要用英文的逗号分隔
  • in:对多个字段的多个筛选条件,每个条件都需要用英文的逗号分隔
  • in:不能对null进行筛选
  • asc:升序
  • desc:降序
  • 只能mysql使用
  • 查询结果是以0为其实序号
  • limit n,m:n代表起始位置,m代表展示m条数据
  • "count":统计
  • "sum":求和
  • "avg":平均
  • "max":最大
  • "min":最小

增、删、改、查格式

1. 增
insert into table (no1,no2) value (001,002);
insert into table (no1,no2) values (001,002);
拓展内容
insert into table1 select * from table where no = 1;

注意:两张表的数据结构必须完全相同,where后面跟的条件最好为表的主键或是唯一的值,不然可能出现备份了很多与其字段名相同的数据

insert into table1  (no,name) select id,ip from table where no = 1;

注意:不限制两表结构是否一致,where后面跟的条件最好为表的主键或是唯一的值,不然可能出现备份了很多与其字段名相同的数据

2. 删
delect from table where name = 'wang';
拓展内容
delect from table;

注意:只删除表中的数据,不清楚内存,如果表中字段有自增的值清除表后自增还会存在,添加数据是会在清除表前的数据基础增加

truncate table table;

注意:表中的数据全部清除,如果表中字段有自增的值清除后添加数据会从最开始增加

drop table group;
drop database table;
3. 改
update table set name = '王',id = 001 where no = 002;
拓展内容
alter table group;
4. 查
select name,id from table where no = 001;
拓展内容
select name from table1 where id = (select Id from table2 where no = 001);

多表关联

内连接
select * from table a inner join table b on a.no = b.no;

select * from table a join table b on a.no = b.no;
左连接
select * from table a left join table b on a.no = b.no;
右连接
select * from table a right join table b on a.no = b.no;
全连接
select * from table a full join table b on a.no = b. no;

表备份

备份到临时表

格式:

备份成SQL脚本
  1. 右键点击需要导出的表
  2. 点击备份/导出-->导出表数据作为


    image.png
  3. 选择SQL-->结构和数据-->设置储存地址


    image.png
上一篇 下一篇

猜你喜欢

热点阅读