如何在vb中打印mysql查询值_零散的MySql基础记不住,看这一篇就够啦

服务启动和停止

  1. 停止数据库服务
net stop mysql
  1. 开启数据库服务
net start mysql

数据库相关操作

连接数据库

mysql -uroot -p  密码

修改密码

alter user 'root'@'localhost' identified by 'root';

创建数据库

create database 数据库名;

显示所有数据库

show databases;

使用数据库

use 数据库名;

删除数据库

drop database 数据库名;

数据库表相关操作

创建数据库表

create table 表名 (   列名1 数据类型1,      列命2 数据类型2,      ...      列名n 数据类型n,      primary key 列名(主键) );

查看数据库中的所有表

show tables;

查看数据库表结构

desc 表名;

删除表

drop table 表名;

修改数据库表名

alter table 表名 rename to 新表名;

添加列

alter table 表名 add 列名 数据类型;

删除列

alter table 表名 drop 列名;

表中数据相关操作

添加数据

insert into 表名(列名1,列名2,...列名n) values(值1,值2,...值n);

添加列要和值相对应

insert into 表名 values(值1,值2,...值n);

值要包含表中所有的列

删除数据

delete from 表名 where 条件;

删除满足条件的数据。

delete from 表名;

默认删除表里的所有数据

修改数据

update 表名 set 列名1 = 值1, 列名2 = 值2,... where 条件

修改满足条件的数据

update 表名 set 列名1 = 值1, 列名2 = 值2,...;

如果是不加条件,则修改所有的数据。

查询数据

select 列命 from 表名,

查询某列的数据

select * from 表名

查询所有列的数据

select distinct 列命 from 表名,

查询去重后的数据

条件语句

12c2125a0d85b53974521fffa44c78f5.png
select * from 表名 where 条件1 and 条件2,查询同时满足条件1和条件2的数据。select * from 表名 where 条件1 or 条件2,查询满足条件1或条件2的数据。select * from 表名 where not 条件1,查询不满足条件1的数据。
dc4004e60870d127fd06020c735ff397.png
select * from 表名 where 列名 is null;,查询某列为空的数据。select * from 表名 where 列名 is not null;,查询某列非空的数据。select * from 表名 where 列名 between 值1 and 值2;,查询某列在值1和值2之间的数据。select * from 表名 where 列名 like 'hello%';,查询所有以hello开头的数据,like结合%使用,其中%代表0到任意个字符。select * from 表名 where 列名 like 'hello_';,查询所有以hello开头并且后面只跟一个字符的数据,like结合_使用,_代表1个字符。select * from 表名 where 列名in (值1,值2,...);,查询某列在某区域内的数据。

排序和分页

整理了一份Java核心知识点。覆盖了JVM、锁、并发、Java反射、Spring原理、微服务、Zookeeper、数据库、数据结构等大量知识点。基本上涵盖了Java架构所有的技术知识点的资料,还覆盖了大厂面试题,同时也有一些springboot等项目源码分享给大家

e466a83416bf725ca3454b249fbada44.png
5a8f9c0f71f309627866fc27c49f5f37.png

由于资料图片太多就不一一的展示出来了

如果需要获取到这个文档的话帮忙转发一下然后再关注我私信回复“架构资料”得到获取方式吧!

select* from 表名 order by 列名;,通过该列进行升序排序。select* from 表名 order by 列名 desc;,通过该列进行降序排序。select* from 表名 limit offset,pagesize;,查询索引从offset(第一个数据索引是0)开始,每页显示pagesize个元素。select* from user limit 0,10;,查询出的用户表数据,从第1个用户开始显示,每页显示10个。select* from user limit 10,10;,查询出的用户表数据,从第10个用户开始显示,每页显示10个。

分组查询

select 字段名 from 表名 group by 字段名select 字段名 count(字段名) from 表名 group by 字段名

举个例子

这里以下方的数据为例子

c8231f480c86b3b473592682c109a391.png
select user_type from user group by user_type;
630161746d1a68e1ea757d7d4f03cec5.png
select user_type,count(user_type) from user group by user_type;
3e7ee9971e5485eb30a3ae7ad0402eb0.png

联表查询

联表查询关键字为join,如果需要判断条件的话是添加join on + 条件 以下方的例子为例子

12f6ced2fa1c1c39b9cfcd1884b7b7fd.png

表结构

661e233bbdff481eef5495c9243768d4.png

内连接

内连接为一种最常用的联表查询,即,inner join,当我们查询了学生姓名和成绩的时,需要用到student 学生表和result成绩表,而inner join 查出的结果就是,学生表中有该学生而且成绩表中对应的有该学生的成绩,满足这一条件成绩就会被查询出来。

e2e0414d618370d28dc4d4af8dc22ec7.png

栗子如下

124ca6e53ab6011d1fc42e2eafa08661.png

外连接

外连接分为左连接和右连接,

左连接

所谓的左连接,也就是在内连接的基础上,把左表中的所有信息给打印。

fc5c08601cf531924dbc9e4406b6abda.png

右连接

右连接和左连接差不多,在内连接的基础上把右边的表的信息打印。

3249dfaacb1c395b778ecafe6c512108.png

以查询学生姓名和成绩为例子,右连接会把没有姓名的成绩打印出来,下面进行演示。

a6f5392f6256c666422747d256a48b62.png

MySql 事物

事物简介

事物

事物是一个最小的不可再分的工作单元,通常一个事物对应一个完整的业务。事物处理可以用来维护数据库的完整性,保证成批的SQL语句要吗全部执行,要么都不执行。

事物操作

开启事物

start transaction;

提交事物,数据写回磁盘

commit

回滚事物

rollback

查看事物是否开启自动提交

show variables like 'autocommit';
4ddae0e7604df3ecf928a9a3b7c5ea59.png

关闭事物自动提交

set autocommit=off;
a6a07354d6b520ae0097896047f229ab.png

通过银行转账的例子演示事物

数据如下

385340266b698d466f90fd72456b061a.png

同时失败或者同时成功

update bank set money=700 where id=1;update bank set money=600 where id=2;

所以需要先开启事物,再提交事物

start transaction;update bank set money=700 where id=1;update bank set money=600 where id=2;commit;

事物特征

原子性 一致性 隔离性 持久性

事物的安全隐患

48c53f40238257f34207bf8cc7432f0b.png

查看事物隔离级别

select @@transaction_isolation;
8d38df7fb43a3efce8ab84cdb4035d27.png

设置隔离级别为读未提交

set session transaction isolation level read uncommitted;

设置隔离界别为读已提交

set session transaction isolation level read committed;

设置隔离级别为可重复读

set session transaction isolation level repeatable read;

设置隔离界别为可串行化

set session transaction isolation level serializable;

MySql 索引

索引分为主键索引,唯一索引,普通索引,组合索引,全文索引。

  1. 查看表中数据数量
select count(*) from 表名;
9371792c307f1e5df5093cb5594e743b.png
  1. 查看表中索引
show index from 表名;
741215a60a46e4839277e73c4eb939f6.png
  1. 删除索引
drop index 索引名 on 表名;
  1. 删除主键索引,也就是删除了该字段
alter table 表名 drop 主键字段名;

主键索引

表结构

create table test(   id int(11),   name varchar(25),   primary key (id));

创建表的时候添加索引

alter table test add constraint id primary key(id);

唯一索引

表结构

create table test(id int(11),name varchar(25),unique index_unique_test_name (name));

创建表之后创建唯一索引

create unique index index_unique_test_name on test(name);

修改表结构为唯一索引

alter table test add unique index index_unique_test_name (name);

普通索引

表结构

create table test(id int(11),name varchar(25),index index_test_name (name));

创建表之后创建普通索引

create index index_test_name on test(name);

修改表结构为普通索引

alter table test add index index_test_name (name);

组合索引

表结构

create table test(id int(11),name varchar(25),index index_test_id_name (id,name));

创建表之后创建组合索引

create index index_test_id_name on test(id,name);

修改表结构为普通索引

alter table test add index index_test_id_name (id,name);

全文索引

表结构

create table test(id int(11),name varchar(25),content text,fulltext (text));

创建表之后创建组合索引

create fulltext index index_content on test(content);

修改表结构为普通索引

alter table test add fulltext index index_content (content);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值