数据库基础操作

数据库基本操作

创建表的语法

create table 表名( 字段名 类型 字段约束, 字段名 类型 字段约束, 字段名 类型 字段约束, )engine=innodb default
charset=utf8mb4;

create table if not exists users(     // if not exists 如果表不存在,则创建, 如果存在就不执行这条命令
id int not null primary key auto_increment,
name varchar(4) not null,
age tinyint,
sex enum('男','女')
)engine=innodb default charset=utf8mb4;

删除表:

drop table 表名;

表结构:

desc 表名;

数据操作 增删改查

insert into 表名(字段1,字段2,字段3) values

(a值1,a值2,a值3),

(b值1,b值2,b值3);

insert into stu values
-> (null,'zhaoliu',25,'w','lamp94'),
-> (null,'uu01',26,'m','lamp94'),
-> (null,'uu02',28,'w','lamp92'),
-> (null,'qq02',24,'m','lamp92'),
-> (null,'uu03',32,'m','lamp138'),
-> (null,'qq03',23,'w','lamp94'),
-> (null,'aa',19,'m','lamp138');

delete from 表名 [where 条件]

delete from stu where id=100;
--删除stu表中id值为100的数据

delete from stu where id>=20 and id<=30;
-- 删除stu表中id值为20到30的数据

delete from stu where id between 20 and 30;
-- 删除stu表中id值为20到30的数据(等价于上面写法)

update 表名 set 字段1=值1,字段2=值2,字段n=值n… where 条件

update stu set sex='m',classid='lamp92' where id=12 or id=14 --等价于下面
update stu set sex='m',classid='lamp92' where id in(12,14);
-- 将id值为12和14的数据值sex改为m,classid改为lamp92
基础查询
# 查询表中所有列 所有数据
select * from users;--不建议使用此方法,减少或禁止全部搜索的操作
# 指定字段列表进行查询
select id,name,phone from users;
Where 条件查询
-- 查询users表中 age > 22的数据
select * from users where age > 22;
-- 查询 users 表中 name=某个条件值 的数据
select * from users where name = '王五';
-- 查询 users 表中 年龄在22到25之间的数据
select * from users where age >= 22 and age <= 25;
select * from users where age between 22 and 25;
-- 查询 users 表中 年龄不在22到25之间的数据
select * from users where age < 22 or age > 25;
select * from users where age not between 22 and 25;
-- 查询 users 表中 年龄在22到25之间的女生信息
select * from users where age >= 22 and age <= 25 and sex = '女';
Like 子句
-- like 语句 like某个确定的值 和。where name = '王五' 是一样
select * from users where name like '王五';

-- 使用 % 模糊搜索。%代表任意个任意字符
-- 查询name字段中包含五的
select * from users where name like '%五%';
-- 查询name字段中最后一个字符 为 五的
select * from users where name like '%五';
-- 查询name字段中第一个字符 为 王 的
select * from users where name like '王%';

-- 使用 _ 单个的下划线。表示一个任意字符,使用和%类似

-- 查询表中 name 字段为两个字符的数据
select * from users where name like '__';
-- 查询 name 字段最后为五,的两个字符的数据
select * from users where name like '_五';
Mysql中的统计函数(聚合函数)
# 计算 users 表中 最大年龄,最小年龄,年龄和及平均年龄
select max(age),min(age),sum(age),avg(age) from users;

-- 上面数据中的列都是在查询时使用的函数名,不方便阅读和后期的调用,可以通过别名方式 美化
select max(age) as max_age,
min(age) min_age,sum(age) as sum_age,
avg(age) as avg_age
from users;

-- 统计 users 表中的数据量
select count(*) from users;
-- 注意,如果指定的列上出现了NULL值,那么为NULL的这个数据不会被统计
Group BY 分组
-- 统计 users 表中 男女生人数,
-- 很明显按照上面的需要,可以写出两个语句进行分别统计
select count(*) from users where sex = '女';
select count(*) from users where sex = '男';

Order by 排序
-- 按照年龄对结果进行排序,从大到小
select * from users order by age desc;
-- 从小到大排序 asc 默认就是。可以不写
select * from users order by age;
-- 也可以按照多个字段进行排序
select * from users order by age,id; # 先按照age进行排序,age相同情况下,按照id进行排序
select * from users order by age,id desc;
Limit 数据分页

limit n 提取n条数据,
limit m,n 跳过m跳数据,提取n条数据

-- 查询users表中的数据,只要3条
select * from users limit 3;
-- 跳过前4条数据,再取3条数据
select * from users limit 4,3;

以上就是我经过学习和请教后总结的内容。如有错误或者漏项,纯属水平有限[捂脸]。
本人只是java学习中的小学生,有错误可以提出并解释,不然我怕我看不懂[捂脸]。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值