数据库sql的基本操作

管理数据库结构

创建删除数据库

create database 数据库名称 ;
drop database 数据库名称;

查看数据库

show databases;

选择数据库

use 数据库名称;

操作数据表sql

创建数据表
create table 表名(
字段名 类型;
)

create table student (
id int not null;
name varchar(10) not null;
age int not null;
score int not null;
);

删除数据表

drop table 表名;

查看数据表的结构

desc 表名;

修改数据表

  1. 新增一个字段 alter table 表名 add 字段名 类型
  2. 修改一个字段 alter table 表名 change 旧字段名 新字段名 新字段类型
  3. 删除一个字段 alter table 表名 drop 字段名

sql函数

数学函数

abs(字段名) 求绝对值
select abs(num) from student;

floor()返回小于参数的最大整数
select floor(score) from student;

ceil()返回大于参数的最大整数;
select ceil(score) from student;

字符串函数

insert (str,index,length,changeStr)
str中index位置开始,长度为length的字符替换为changeStr,index1开始
select insert(name,2,2,'mysqlStr') from user;

upper(),ucase()
将字母值变为大写
select upper(name) from user;
select ucase(name) from user;

lower(),lcase()
将字母值变为小写
select lower(name) from user;
select lcase(name) from user;

left(str,len)
返回str字符串的前len个字符
select left(name,3) from user;

right(str,len)
返回str字符串的后len个字符
select right(name,3) from user;

substring(str,index,len)
截取str,从index位置开始,长度为len
select substring(name,2,2) from user;

reverse()
反序输出
select reverse(name) from user;

日期函数

curdate() current_date() 获取当前日期
select current_date();
select curdate();

curtime() current_time() 获取当前时间
select curtime();
select current_time();

now() 获取当前的日期和时间
select now();

datediff(day1,day2) 计算两时间相隔的天数
select datediff('2022-5-1','2022-3-31');

adddate(day,num) 计算day日期num之后的天数
select adddate('2022-2-20',173);
 
subdate(day,num)计算day日期num之前的天数
select subdate('2022-2-20',173);

聚合函数

count() 根据某个字段统计记录数
select count(*) from user;

sum() 计算某个字段值的总和
select sum(num) from user;

avg() 求某个字段值的平均值
select avg(num) from user;

max() 求某个字段值的最大值
select max(num) from user;

min() 求某个字段值的最小值
select min(num) from user;

select name,count(id) from user group by name order by count(id) desc;

select name,count(id) from user group by name having count(id) > 2 order by count(id) desc;

where 和 having 的区别

where 和 having 都是追加条件的,区别在于 SQL 是否进行了分组查询,如果进行了分组查询则使用 having,如果没有进行分组查询则使用 where。

SQL 运算符

算术运算符

1、执行运算符:加减乘除

select num+100 from user;

2、比较运算符:大于、等于、小于、不等于

select num > 100 from user;

3、逻辑运算符:&&、||、!

select !(num < 100 || num > 50) from user;

特殊运算符

1、is null 判断字段值是否为空

select name is null from user;

2、between and 判断字段值是否在某个区间之内

select num between 50 and 100 from user;

3、in 判断字段值是否在某个确定值的集合内

select name from user where id in (1,2,3);
select name from user where id = 1 || id = 2 || id = 3;

4、like 模糊查询

包含’电脑’

select * from user where name like '%电脑%';

以’电脑’开头

select * from user where name like '电脑%';

以’电脑’结尾

select * from user where name like '%电脑';

name 长度为 2

select * from user where name like '__';

name 长度为 3,并且以’电’开头

select * from user where name like '电__';

name 长度为 3,中间为’电’

select * from user where name like '_电_';

name 长度为 3,以’电’结尾

select * from user where name like '__电';

数据 CRUD

CRUD

create 创建

read 读取

update 修改

delete 删除

添加数据

insert into 数据表(字段列表) values(字段值)
insert into user VALUES('数据库',300,33);

查询数据

select 字段列表 from 数据表名;
select id,name from user;
select * from user;

修改数据

update 数据表名 set 字段名=字段值,字段名=字段值,... where 条件

删除数据

delete from 数据表名 where 条件
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值