一、数据库相关操作

1.链接数据库

mysql -uroot -p – -u用户名 -p密码(不写密码时,不显示密码)
1
2.创建数据库

create database 数据库名 charset = utf8; --"charset = utf8"表示使用utf8编码,支持中文
1
3.查看所有数据库

show databases;
1
4.使用数据库

use 数据库名;
1
5.查看当前使用的数据库

select database();
1
6.删除数据库

drop database 数据库名;
1
7.数据库的备份与恢复

– 备份
mysqldump –uroot –p 数据库名 > python.sql; – 重定向到文件中

– 恢复
mysql -uroot –p 新数据库名 < python.sql; – 导入的时候必须要先创建数据库
1
2
3
4
5
二、数据表的操作

1.创建表
create table 数据表名字 (字段 类型 约束);

create table classes(id int unsigned primary key auto_increment,name varchar(50) not null);
1
2.查看表的结构

desc 表名;
1
3.查看所有的表

show tables;
1
4.删除表

drop table 表名;
1
5.修改表

– 添加字段
alter table 表名 add 字段 类型;

– 修改字段 类型约束
alter table 表名 modify 字段 类型及约束;

– 字段重命名
alter table 表名 change 原字段名 新字段名 类型约束;

– 删除字段
alter table 表名 drop 字段;
1
2
3
4
5
6
7
8
9
10
11
三、数据的相关操作

1.全列插入

insert into 表名(字段…) values(值…); – 可以写部分字段
1
2.多行插入

insert into 表名(字段1) values(值1),(值2),(…);
1
3.删除数据

delete from 表名 where 条件;
1
4.修改

update 表名 set 字段=值… where 条件;
1
5.查询

– 查询所有列
select * from 表名;

–按一定条件查询
select * from 表名 where 条件;

–查询指定列
select 列1,列2 from 表名;
1
2
3
4
5
6
7
8
四、查询的相关操作

1.条件查询

比较运算符
= != <> > >= < <=
– 查询学生表student中 age 不为18的学生信息
select * from student where age != 18;
select * from student where age <> 18; --<>表示不等于

– 查询学生表student中 age 大于等于18的学生姓名(字段name)
select name from student where age >= 18;
1
2
3
4
5
6
逻辑运算符
and 和
or 或者
not 不在
– 查询18岁以上女生(字段sex)的信息
select * from student where age >18 and sex = “女”;

– 查询18岁以上 或者 性别为女的学生信息
select * from student where age>18 or sex = “女”;

– 查询不在18岁以上这个范围的学生信息
select * from student where not (age > 18 and sex = “女”);
1
2
3
4
5
6
7
8
模糊查询
like
%替换任意个 _替换一个
– 查询有两个字的名字
select * from student where name like ‘__’

– 查询至少有两个字的名字
select * from student where name like’__%’
1
2
3
4
5
范围查询
in 在一个非连续范围
not in 不非连续范围
between…and… 在一个连续的范围内
not between … and… 不在连续范围内
– 年龄为18或20岁学生信息
select * from student where age in(18,20);

– 年龄不是18或20岁学生信息
select * from student where age not in(18,20);

– 年龄在18到20岁之间学生信息
select * from student where age between 18 and 20;

– 年龄不在18到20岁之间学生信息
select * from student where age not between 18 and 20;
1
2
3
4
5
6
7
8
9
10
11
空判断
is null 为空
is not null 非空
– 判断年龄为空的信息
select * from student where age is null;

– 判断年龄不为空的信息
select * from student where age is not null;

1
2
3
4
5
6

2.排序
order by 排序
asc 从小到大
desc 从大到小

– 查询年龄在12到20岁之间的女性,按照年龄从小到大到排序
select * from students where age between 12 and 20 and sex = ‘女’ order by age asc;

– 查询年龄在12到20岁之间的女性,按照年龄从大到小排序
select * from students where age between 12 and 20 and sex = ‘女’ order by age desc;
1
2
3
4
5

3.聚合函数

总数 count
最大值 max
最小值 min
求和 sum
平均值 avg
– 查询女生人数
select count(*) from student where sex = ‘女’;

– 查询最大年龄
select max(age) from student;

– 查询最小年龄
select min(age) from student;

– 求年龄总和
select sum(age) from student;

–求年龄平均值
select avg(age) from student;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

4.分组
group by 分组 having 分组条件

select 分组字段 from 表名 group by 分组字段 having 条件; – having 后常跟聚合函数
1
5.分页
limit start count;
limit 起始位置 显示的个数

– 每页显示2个,第1个页面
select * from student limit 0,2;

– 每页显示2个,第2个页面
select * from student limit 2,2;

– 每页显示2个,第3个页面
select * from student limit 4,2;

1
2
3
4
5
6
7
8
9
6.去除重复数据

select distinct 字段 from 表;
1
7.保留小数位
round(11.454, 2)
四舍五入,保留数值后两位小数

– 求平均年龄,并保留两位小数
select round( avg(age), 2) from student;

作者:weixin_44141532
来源:CSDN
原文:https://blog.csdn.net/weixin_44141532/article/details/87867843

常用的sql语句(一)相关推荐

  1. 经典SQL语句大全、50个常用的sql语句

    50个常用的sql语句 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,T ...

  2. 常用经典SQL语句大全完整版--详解+实例 (存)

    常用经典SQL语句大全完整版--详解+实例 转 傻豆儿的博客 http://blog.sina.com.cn/shadou2012  http://blog.sina.com.cn/s/blog_84 ...

  3. mysql 10分钟_10分钟入门mysql(含常用的sql语句,mysql常见问题及解决方案)

    开发中常用的sql语句 1,创建一个数据库并指定编码格式 drop database if exists test;create database test default character set ...

  4. 50个常用mysql语句_50个常用的sql语句

    50个常用的sql语句 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,T ...

  5. 常用的sql语句,sql使用大全

    常用的sql语句,sql使用大全 我工作中常用到的sql 插入 查询 更新 介绍其他的sql SQL分类 基本的sql语句 高级查询运算词 我工作中常用到的sql 下面是我工作中常用的sql,每次都是 ...

  6. mysql 输出名称_MySQL常用的SQL语句//输出所有信息showfullfieldsfrom'表名称';//改表

    MySQL常用的SQL语句 //输出所有信息 show full fields from '表名称'; //改表名 ALTER  TABLE table_name RENAME TO new_tabl ...

  7. ​MYSQL中常用的SQL语句​(增删改查)

    MYSQL中常用的SQL语句 一.增删改查 1.新增 指定字段插入: INSERT INTO <表名> (<字段1>, <字段2>, <字段3>) VA ...

  8. java开发中常用的SQL语句

    在实际开发过程中常用的sql语句, 以user_table为例. 1)创建语句如下: CREATE TABLE USER_TABLE (USER_ID VARCHAR2(8), USER_NAME V ...

  9. oracle数据库中最常用的sql语句

    对SQL语句进行调整,往往有一项前期工作,就是定位最常用的SQL 语句.Oracle数据库可以从多个方面取得SQL语句.如从数据库自身的存储过程或者函数中取得,也可以从前台的应用程序中取得.所以,数据 ...

  10. vb mysql 语句_VB与数据库编程常用的SQL语句总结

    SQL数据库介绍国庆假期 美好而短暂 现已正式结束 不忘初心,继续前行 今天我们来学习VB与数据库编程中的SQL语句. SQL简介 SQL数据库语言是一种通用的,功能强大的关系数据库语言,是操作数据库 ...

最新文章

  1. H-Net:基于无监督注意的立体深度估计
  2. Windows自带的杀进程工具
  3. [面向对象] ABAP中类重构助手Refactoring Assitant
  4. KClient——kafka消息中间件源码解读
  5. 一步步学习EF Core(2.事务与日志)
  6. CodeForces - 616D Longest k-Good Segment
  7. (DAG+固定终点的最长路和最短路)硬币问题
  8. 杭州获评全国最智慧城市
  9. ios8改变statusBar字体的显示颜色
  10. 免费的中文OCR软件
  11. css实现白光划过效果
  12. 那些我们想当然的错误[SQL]
  13. c# 正则匹配,匹配括号,中括号等
  14. “士兵突击”职场攻略心法
  15. 水仙花数python代码多种方式_水仙花数的三种解决方法(Python实现)
  16. k8s之ceph分布式存储
  17. 【ROS】实操_话题发布
  18. Java 基础语法(1)- 注释、标识符、关键字
  19. 小程序社交类目需要上传《非经营性互联网信息服务备案核准》,什么是《非经营性互联网信息服务备案核准》?
  20. 计算机学院网站规划书,计算机学院网站设计与实现 -毕业论文.docx

热门文章

  1. 随机森林重要性排序-R
  2. 战狼5天票房10亿,吴京抵押房产贷款赚大了
  3. github时好时坏连接不上的问题
  4. Word章节自动编号+题注自动编号+公式编号+交叉引用
  5. 如何在 微软Microsoft 官网 下载 office365
  6. 2018年的学习任务
  7. zabbix学习资料收集
  8. mvn clean package是个什么鬼?
  9. AndroidQ与腾讯tbs的兼容问题
  10. GD32系列总结 - systick介绍及使用