mysql手册01_常用SQL语句大全


查询所有数据库:
show databases;选中某个数据库:
use blublog;创建数据库:
create database mysqltest;查询数据库中的所有数据表:
show tables;创建表示例:
create table pet (name varchar(20), owner varchar(20), species varchar(20), sex char(1), birth date, death date);查看数据表的具体结构:
describe pet;+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name    | varchar(20) | YES  |     | NULL    |       |
| owner   | varchar(20) | YES  |     | NULL    |       |
| species | varchar(20) | YES  |     | NULL    |       |
| sex     | char(1)     | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| death   | date        | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+添加数据示例:
insert into pet values('Puffball','Diane','hamster','f','1999-03-30',null);
insert into pet values('格洛米','许嵩','柯基犬','f','2006-01-01',null);删除数据示例:
delete from pet where name = 'burk';修改数据示例:
update pet set name = 'Buffy-1' where name = 'Buffy';
基础查询语句:
select * from t_blog;
select id,title from t_blog;条件查询:
select * from t_blog where id = 40;
select * from student where degree between 60 and 80;
select * from student where degree > 60 and degree < 80;
select * from student where classes='1001' or sex='女';
select * from student where degree in(60, 80, 100);模糊查询:
select * from t_blog where content like '%数据库%';
select * from t_blog where content not like '%广告%';
注 '3%'表示以3开头,'%3'表示以3结尾。排序查询:
select * from student order by degree asc;
select * from student order by degree desc;
select * from student order by classes asc,degree desc;计数和统计:
select count(*) from student where sex='男';
select max(degree) from student;
select min(degree) from student;
select avg(degree) from student;通过嵌套和分页查询分数最高的student数据:
select * from student where degree=(select max(degree) from student);
select * from student order by degree desc limit 0,1;按班级分组查询平均分:
select avg(degree) from student group by classes;查询学生个数大于10人的班级平均分
select classes,avg(degree) from student group by classes having count(classes)>10;两表联查
select sname,cname,degree from student,classes where student.classes=classes.id;字段去重查询:
select distinct species from pet;使用year函数查询出生年份为2006年的记录:
select * from pet where year(birth)=2006;使用year函数和now函数查询出生年份为今年的记录:
select * from pet where year(birth)=year(now());使用year函数和now函数查询年龄:
select name,year(now())-year(birth) as '年龄' from pet;//any---查询1班中分数大于2班中至少一个人(2班最低分)的数据
select * from student where classes='1班' and degree>any(select degree from student where classes='2班');//all---查询1班中分数大于2班所有人(2班最高分)的数据
select * from student where classes='1班' and degree>all(select degree from student where classes='2班');//求并集,注:union前后的select语句必须拥有相同数量的列,在union之前的select语句里设置别名:
select tname as name,tsex as sex,tbirthday as birthday from teacher
union
select sname,ssex,sbirthday from student;
退出数据库服务器:
exit;
  • mysql常用数据类型:
数值类型:
tinyint, smallint, mediumint, int或integer, bigint, float, double, decimal
日期和时间类型:
date    2020-08-10
time    12:22:32
year    2020
datetime    2020-08-10 12:22:32
timestamp   时间戳
字符串类型(blob表示二进制形式):
char, varchar, tinyblob, tinytext, blob, text, mediumblob, mediumtext, longblob, longtext
  • mysql建表约束
-- 主键约束:字段值不重复且不为空,能够唯一确定表中的一条数据
-- 自增约束:如名,一般和主键约束配合使用
-- 唯一约束:约束字段值不可重复
-- 非空约束:约束字段值不可为空
-- 默认约束:约束字段的默认值
-- 外键约束:从表的外键值要参照主表。被从表引用的主表记录不可删除。
//设置id主键约束,id不能为空也不能重复
create table user(id int primary key, name varchar(20));
//设置联合主键id和name,id和name不能为空,不能同时重复
create table user2(id int, name varchar(20), password varchar(20), primary key(id,name));
//设置id自增约束,配合主键约束使用
create table user3(id int primary key auto_increment, name varchar(20));
//给已经创建好的表的id字段添加主键约束
alter table user4 add primary key(id);
//给已经创建好的表的id字段删除主键约束
alter table user4 drop primary key(id);
//使用modify字段添加或删除约束
alter table user4 modify id int primary key;
//唯一约束
create table user5(id, name varchar(20) unique);
create table user5(id, name varchar(20), unique(id,name));
alter table user5 add unique(name);
alter table user5 modify name varchar(20) unique;
//删除唯一约束
alter table user5 drop index name;
//非空约束
create table user6(id int, name varchar(20) not null);
//默认约束
create table user7(id int, name varchar(20), age int default 18);
//外键约束
create table classes(id int primary key, name varchar(20));
create table students(id int primary key, name varchar(20), class_id int, foreign key(class_id) references classes(id));
  • 数据库的三大设计范式
第一范式:确保每列保持原子性
第二范式:满足第一范式的前提下,要求确保表中的每列都和主键相关
第三范式:满足第二范式的前提下,确保每列都和主键列直接相关,而不是间接相关
不满足第一范式:
create table students2(id int primary key, name varchar(20), address varchar(30));
insert into students2 values(1,'张三','中国江苏南京');
insert into students2 values(2,'李四','中国安徽合肥');
insert into students2 values(3,'王五','中国浙江杭州');
满足第一范式:
create table students3(id int primary key, name varchar(20), country varchar(30), province varchar(30), city varchar(30));
insert into students3 values(1,'张三','中国','江苏','南京');
insert into students3 values(2,'李四','中国','安徽','合肥');
insert into students3 values(3,'王五','中国','浙江','杭州');不满足第二范式:
create table myorder(product_id int,customer_id int,product_name varchar(20),customer_name varchar(20),primary key(product_id, customer_id));
满足第二范式:
create table myorder(id int primary key, product_id int, customer_id int);
create table product(id int primary key, name varchar(20));
create table customer(id int primary key, name varchar(20));不满足第三范式:
create table myorder(id int primary key, product_id int, customer_id int, customer_phone varchar(15));
满足第三范式:
create table myorder(id int primary key, product_id int, customer_id int);
create table customer(id int primary key, name varchar(20), customer_phone varchar(15));
  • 复制表数据做条件查询
student表:
+----+------+---------+--------+
| id | name | classes | degree |
+----+------+---------+--------+
|  1 | zs   | 1班     |   87.5 |
|  2 | lisi | 2班     |   65.5 |
|  3 | ww   | 3班     |   91.5 |
|  4 | zs1  | 1班     |   46.5 |
|  5 | zs2  | 1班     |     54 |
|  6 | zs3  | 1班     |    100 |
|  7 | ww2  | 3班     |   99.5 |
|  8 | ww3  | 3班     |     20 |
|  9 | lisi | 2班     |   85.5 |
| 10 | ssss | 1班     |     65 |
| 11 | ssad | 1班     |     66 |
+----+------+---------+--------+查询低于班级平均分的student数据
select * from student a where degree<(select avg(degree) from student b where a.classes = b.classes);
结果:
+----+------+---------+--------+
| id | name | classes | degree |
+----+------+---------+--------+
|  2 | lisi | 2班     |   65.5 |
|  4 | zs1  | 1班     |   46.5 |
|  5 | zs2  | 1班     |     54 |
|  8 | ww3  | 3班     |     20 |
| 10 | ssss | 1班     |     65 |
| 11 | ssad | 1班     |     66 |
+----+------+---------+--------+
  • SQL的四种连接查询
内连接: inner join 或 join
外连接-左连接:left join 或 left outer join
外连接-右连接:right join 或 right outer join
外连接-完全外连接:full join 或 full outer join
create table person(id int, name varchar(20), cardId int);
create table card(id int, name varchar(20)); insert into card values(1,'饭卡'),(2,'公交卡'),(3,'银行卡'),(4,'购物卡'),(5,'门禁卡');
insert into person values(1,'zhangsan',1),(2,'lisi',3),(3,'wangwu',6);内连接查询(按对应条件查询出两个表的值):
select * from person inner join card on person.cardId=card.id;
+------+----------+--------+------+-----------+
| id   | name     | cardId | id   | name      |
+------+----------+--------+------+-----------+
|    1 | zhangsan |      1 |    1 | 饭卡      |
|    2 | lisi     |      3 |    3 | 银行卡    |
+------+----------+--------+------+-----------+左连接查询(左边表person数据全部取出,如果条件不对应就将右表card的数据设为NULL值):
select * from person left join card on person.cardId=card.id;
+------+----------+--------+------+-----------+
| id   | name     | cardId | id   | name      |
+------+----------+--------+------+-----------+
|    1 | zhangsan |      1 |    1 | 饭卡      |
|    2 | lisi     |      3 |    3 | 银行卡    |
|    3 | wangwu   |      6 | NULL | NULL      |
+------+----------+--------+------+-----------+右连接查询(右边表card数据全部取出,如果条件不对应就将左表person的数据设为NULL值):
select * from person right join card on person.cardId=card.id;
+------+----------+--------+------+-----------+
| id   | name     | cardId | id   | name      |
+------+----------+--------+------+-----------+
|    1 | zhangsan |      1 |    1 | 饭卡      |
|    2 | lisi     |      3 |    3 | 银行卡    |
| NULL | NULL     |   NULL |    2 | 公交卡    |
| NULL | NULL     |   NULL |    4 | 购物卡    |
| NULL | NULL     |   NULL |    5 | 门禁卡    |
+------+----------+--------+------+-----------+完全外连接查询
select * from person full join card on person.cardId=card.id;
结果:
ERROR 1054 (42S22): Unknown column 'person.cardId' in 'on clause'原因:mysql不支持完全外连接查询语句,而是通过左连接union右连接来完成查询的:
select * from person left join card on person.cardId=card.id
union
select * from person right join card on person.cardId=card.id;
+------+----------+--------+------+-----------+
| id   | name     | cardId | id   | name      |
+------+----------+--------+------+-----------+
|    1 | zhangsan |      1 |    1 | 饭卡      |
|    2 | lisi     |      3 |    3 | 银行卡    |
|    3 | wangwu   |      6 | NULL | NULL      |
| NULL | NULL     |   NULL |    2 | 公交卡    |
| NULL | NULL     |   NULL |    4 | 购物卡    |
| NULL | NULL     |   NULL |    5 | 门禁卡    |
+------+----------+--------+------+-----------+

mysql手册01_常用SQL语句大全相关推荐

  1. [MySQL学习]常用SQL语句大全总结

    转载地址:http://www.cnblogs.com/0351jiazhuang/p/4530366.html SQL是(Structured Query Language)结构化查询语言的简称,下 ...

  2. ORACLE常用SQL语句大全

    ORACLE常用SQL语句大全 DDL:数据库定义语言(CREATE.ALTER.DROP.TRUNCATE.COMMENT.RENAME),用来创建数据库中的表.索引.视图.存储过程.触发器等对象的 ...

  3. 55:Mysql用户管理|常用sql语句|mysql数据库备份恢复

    2019独角兽企业重金招聘Python工程师标准>>> 1.Mysql用户管理: 场景,为了安全,新建的站点,创建新的用户,或者给已有用户授权,对某个库或者某个表有权限: 语法: g ...

  4. 一些常用SQL语句大全

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server - 创建 ...

  5. MySQL初级篇——常用SQL语句(大总结)

    文章目录: 1.数据库的相关操作 1.1 连接数据库 1.2 创建数据库 1.3 查看数据库有哪些 1.4 查看指定的数据库 1.5 删除指定的数据库 1.6 使用数据库(很重要) 1.7 用户的创建 ...

  6. 全网最牛最全面的最常用SQL语句大全

    [文章末尾..................] DDL(Data Definition Language)数据定义语言 一.操作库 -- 创建库 create database db1; -- 创建 ...

  7. 达梦数据库常用sql语句大全

    常用sql 一.对象操作相关sql 1.查看表占用空间: 2.拼接sql,批量删除模式下的表: 3.查找一个表属于哪个用户: 4.查询某一模式下的所有表名 5.查询数据库中所有用户及状态 6.查询当前 ...

  8. mysql用语_mysql基本sql语句大全(基础用语篇)

    1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 备份 ...

  9. jpa 自定义sql if_常用SQL语句大全总结

    SQL是(Structured Query Language)结构化查询语言的简称,下面赵一鸣随笔博客从基础知识.判断对象和应用技巧等方面,介绍了SQL的应用方法. 基础 创建数据库 创建之前判断该数 ...

  10. 常用SQL语句大全总结

    SQL是(Structured Query Language)结构化查询语言的简称,下面Mr.韦淋随笔博客从基础知识.判断对象和应用技巧等方面,介绍了SQL的应用方法. 基础 创建数据库 创建之前判断 ...

最新文章

  1. ajax hapi上传文件,javascript – hapi.js Cors Pre-flight不返回Access-Control-Allow-Origin标头...
  2. Disable auto select after clicking magnifier
  3. 熊市利好,Bit-Z推出币圈最高返佣50%
  4. Mysql中查找并删除重复数据的方法
  5. 直接插入排序,折半插入排序,希尔排序,简单选择排序,冒泡排序,快速排序模板以及比较次数与移动次数的分析,折半搜索算法模板
  6. JAVA 代码交互率低的原因分析,深入剖析Java编程中的中文问题及建议最优解决方法...
  7. Java案例:清洗网址垃圾字符
  8. java带界面的代码_求一个java swing带界面的万年历代码
  9. html5模板md风格,使用CSS3 制作一个material-design 风格登录界面实例
  10. 计算机系统常见故障分析与排除,电脑常见网络故障分析与排除方法
  11. 领跑衫获奖感言 课程总结
  12. 保研杂记(上)心灵鸡汤篇
  13. pyd文件逆向(二)
  14. 项目一:认识Linux操作系统
  15. Filecoin系列 - 源码分析 - CPU SHA扩展
  16. ensp使用web登录防火墙
  17. Processing学习 — Processing结合Kinect2实现人影互动
  18. VIM 编辑器使用教程
  19. chef怎么读(chef怎么读英语)
  20. 通俗易懂的磁盘分区教程,图文并茂简单明了!

热门文章

  1. 百度知道1000指数的关键词留链接排名到第一的实战案例
  2. 美团王兴怒卸百度 App
  3. 《善用佳软:高效能人士的软件应用之道》一2.6 小工具之计算器
  4. 数据库索引是什么?为什么要使用索引?
  5. BeanUtils与PropertyUtils的copyProperties方法的差别
  6. 36-基于51单片机的LED彩灯控制器设计
  7. 转:Visio 2010 产品秘钥 亲测可用的
  8. 创客匠人知识付费SaaS系统功能介绍
  9. 算法学习笔记 全源最短路径Johnson算法(用于稀疏图和有负边的图)
  10. 关于解决miui10国际版刷入之后无法认证的问题