02.数据库的增删改查

时间:2019-10-29
本文章向大家介绍02.数据库的增删改查,主要包括02.数据库的增删改查使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.用户管理

创建用户
create user 用户名@IP地址 identified by 密码;
删除用户
drop user 用户名@IP地址;
修改用户
rename user 用户名@IP地址 to 新用户名@IP地址;
修改密码
set password for 用户名@IP地址 = Password(新密码)

2.数据库命名规则:

  • 可以由字母、数字、下划线、@、#、$组成
  • 区分大小写
  • 唯一性
  • 不能使用关键字如 create select
  • 不能单独使用数字
  • 最长128位

    3.数据类型

    字符串
字段 长度 名称
bit 0 1 字节
char 0-255字节 定长字符串
varchar 0-65535字节 变长字符串
tinytext 0-255字节 短文本字符串
text 0-65536字节 长文本数据
数据类型
类型 名称
tinyint 小整数值
int 大整数值
float 单精度浮点数
decimal(M,D) 小数值 M:全长 D:小数点后位数
日期和时间
字段 名称 格式
date 日期 YYY-MM-DD
time 时间 HH-MM-SS
year 年份 YYYY
datetime 日期+时间 YYY-MM-DD HH-MM-SS

4.数据库操作

  • 创建数据库  create database 数据库名称 charset=”utf8”;
  • 使用数据库  use 数据库名称; (Database changed 表示已经选择了数据库)
  • 查看数据库  show databases;
  • 删除数据库  drop database 数据库名称;
  • 退出数据库  exit;或quit;
  • 查看数据库文件位置  show global variables like "%datadir";
  • 查看数据库安装位置  show variables like "%char%";
  • 查看版本  select version();
  • 查看系统时间  select now();

5.数据表操作

  • 创建表  create table 表名(字段名1,字段名2,字段名3......);
  • 查看表  show tables;
  • 修改表名  alter table 旧表名 rename 新表名;
  • 添加字段  alter table 表名 add 字段名 字段类型 [是否允许非空];
  • 修改字段类型  alter table 表名 modify 字段名称 字段类型 是否允许非空;
             alter table 表名 change 旧字段名 新字段名 字段类型
  • 修改字段名称  alter table 表名 change 旧字段名 新字段名 字段类型 是否允许非空;
  • 删除字段  alter table 字段名 drop 字段名;
  • 表中插入数据 
    • insert into 表名 (字段1,字段2,...字段N) values ( 值1, 值2, ... 值N );
    • insert into student values(0,"李四",13); (0默认递增)
    • insert into student values(0,"王五",14),(0,"赵六",18),(0,"钱七",20);
    • insert into student values(10,"小李",20);
      • 如果数据是字符型,必须使用单引号或者双引号,如:"value"。
      • 可以指定id值进行插入,
      • 下次插入的id会在上次结果进行增加
      • 注意:id要以自增长方式插入
  • 复制表 create table b1 select * from db2.a1; (结构和数据都复制)
        create table b2 select * from db2.a1 where 1>5;(只复制结构不复制数据)
  • 删除表 drop table 表名;
  • 查看表的结构 desc 表名;
  • 取别名 select name as "姓名",age as "年龄" from student as s;
  • 使用别名 select student.name as "姓名",student.age as "年龄" from student;
  • 表数据修改/更新 update 表名 set 字段=值 where 条件;
  • 表数据删除 delete from 表名 where 条件;
  • 添加主键 alter table 表名 add primary key(列名);
  • 删除主键 alter table 表名 drop primary key;
  • 添加外键 alter table 从表 add constraint 外键名称 foreign key 从表(外键字段) references 主表(主键字段);
  • 删除外键 alter table 表名 drop foreign key 外键名称
  • 修改默认值 alter table 表名 alter 字段名 set default 1000;
  • 删除默认值 alter table 表名 alter 字段名 drop default;
创建表student 实例:
create table student(
    id int unsigned primary key auto_increment,
    name varchar(30) not null,
    age tinyint unsigned not null,
    height decimal(5,2),
    sex enum("男","女","保密")default "保密",
    cls_id int default 1,
    is_delete bit default 0);

约束

  • primary key 标识该字段为该表的主键,可以唯一的标识记录
  • foreign key 标识该字段为该表的外键
  • not null  标识该字段不能为空
  • unique key 标识该字段的值是唯一的
  • auto_increment  定义列为自增的属性,整数类型一般用于主键,数值会自动加1
  • default   为该字段设置默认值

消除重复行 select distinct 字段名 from 表名;

6.条件查询

  • where:数据库中常用的是where关键字,用于在初始表中筛选查询。它是一个约束声明,用于约束数据,在返回结果集之前起作用。
  • group by 对select查询出来的结果集按照某个字段或者表达式进行分组,获得一组组的集合,然后从每组中取出一个指定字段或者表达式的值。
  • having 用于对where和group by查询出来的分组经行过滤,查出满足条件的分组结果。它是一个过滤声明,是在查询返回结果集以后对查询结果进行的过滤操作。

  执行顺序
select –>where –> group by–> having–>order by

7.运算符

where后面支持多种运算符

  • 比较运算符 > < >= <= != 或 <>
  • 逻辑运算符 and or not
  • 模糊运算符 like _ 表示一个字符 % 表示一个或多个字符
  • 范围运算符 in ( ) not in ( )
  • 空判断 is null   is not null

查询所有字段 select * from student;
查询单个字段 select name from student;
查询两个字段 select name,age from student;

比较运算符
# 1.查询年龄大于30的学生
select * from student where age>30;
# 2.查询年龄大于等于38岁的学生
select * from student where age>=38;
# 3.查询年龄不是18岁的学生
select * from student where age!=18;
# 4.查询年龄等于18岁的学生
select * from student where age=18;

模糊查询 like   % 和 _ 占位
~~~

1.查询姓黄的人

select * from student where name like "黄%";

2.查询姓周的人,名字是一个字

select * from student where name like "周_";
~~~

逻辑运算符

and 用于多个条件连接
~~~

1.查询编号大于17的女生

select * from student where id>17 and sex="女";

2.查询年龄大于20被删除的人

select * from student where age>20 and is_delete=0;

3.查询年龄大于25没有被删除的男生

select * from student where age>25 and is_delete=1 and sex="男";
~~~
or 或者 or两边有一个条件满足就可以查询出来
~~~

18以上或者身高超过180(包含)以上

select * from student where age>18 or height >= 180.00;
~~~
not 非 
年龄不是18岁的学生
~~~
select * from student where age != 18;
select * from student where age <> 18;
~~~

范围查询

in 在非连续的范围之内

select * from student where age in (18,34,49,45);

not in 不在非连续的范围之内
~~~

查询年龄不是34、18岁的学生信息

select * from student where age not in (18,34);
~~~
between...and... 表示在一个连续的范围内,两边都包含
~~~

查询年龄在18到34之间的信息

select * from student where age between 18 and 34;
~~~
not between...and... 表示不在一个连续的范围内
~~~

查询年龄不在18到34之间的信息

select * from student where age not between 18 and 34;
~~~

非空判断

is null 空
~~~

查询没有填写身高的学生

select * from student where height is null;
~~~
is not null 非空
~~~

查询填写了身高的学生

select * from student where height is not null;
~~~

8.排序操作 order by

asc 升序
~~~

查询未删除的学生信息,按照年龄升序

select * from student where is_delete=1 order by age asc;
~~~
desc 降序
~~~

查询已删除的学生信息,按照学号进行降序

select * from student where is_delete=0 order by id desc;
~~~

9.聚合函数

为了快速得到统计数据,一般我们会常用到5个聚合函数:
max( ) min( ) avg( ) sum( ) count( )
~~~

max( ) 查询女生中最大的id

select max(id) from student where sex="女";

min( ) 查询男生中年纪最小的学生

select min(age) from student where sex="男";

avg( )查询女生的平均年龄

select avg(age) from student where sex="女";

round去除小数点部分

select round(sum(age)/count(*)) from student where sex="女";
select round(avg(age)) from student where sex="女";

sum( ) 查询男生的总年龄

select sum(age) from student where sex="男";

count() 表示计算总行数
select count(
) from student;

#### 10.分组查询 group by

对性别进行分组

select sex from student group by sex;

查询男女生中年龄最大的

select sex,max(age) from student group by sex;
~~~
group_concat( ):起到连接作用,可以作为一个字段输出来使用,得出该字段的集合。
~~~
select sex, group_concat(name) from student group by sex;
~~~
group by + having
having 表达式:在分组查询结果后,用having指定条件输出结果
~~~
select sex,count(*) from student group by sex having sex="男";
~~~
having 和 where 一样,但having 只能用在group by
where 是对原始数据条件筛选,having 对于分组之后的数据做进一步筛选
~~~

除了男生以外的分组人数

select sex,count(*) from student group by sex having sex!="男";

查询平均年龄超过30岁的性别

select sex,group_concat(name) from student group by sex having avg(age)>30;
~~~

11.分页查询

limit start,count

  • start 表示从哪里开始查询,默认是0开始,可以不写
  • count 每页显示条数
# 获取第一页,每页显示4条内容
select * from student limit 0,4;
# 获取第二页
select * from student limit 4,4;
# 每页显示5个,显示第三页信息,按照年龄从小到大排序
select * from student order by age asc limit 10,5;

12.连接查询

当数据来源于多张表时,需要将多张表连接成一个大的数据集,在选择合适的列返回

  • student:cls_id
  • class : id
    连接查询就是将两个表按照某种条件合并到一起
内连接查询

格式 : table1 inner join table2 on 设置连接条件
~~~
select * from student inner join class on student.cls_id = class.id;
~~~
按照要求显示姓名和学生对应的班级名字
~~~
select student.name,class.name from student inner join class on student.cls_id = class.id;

select s.name,c.name from student as s inner join class as c on s.cls_id = c.id;
~~~

外连接查询

左外连接 : left join
~~~

左外连接查询,查询每位学生对应的班级信息,不满足以null进行填充

select * from student inner join class on student.cls_id = class.id;
~~~
右外连接 : right join (不经常使用)
~~~

右外连接查询,查询的结果为两个表匹配到的数据,右表特有的数据,对于左表中不存在的数据使用null进行填充

select * from student right join class on student.cls_id = class.id;
~~~
内连接其他写法
~~~
select * from student cross join class on student.cls_id = class.id;
~~~
外连接其他写法
~~~
select * from student left outer join class on student.cls_id = class.id;
~~~

自关联

表自己关联自己  表1 inner join 表1

内连接练习

# 从外部导入数据
source C:/Users/ASUS/Desktop/areas.sql;

create table area(aid int primary key,atitle varchar(40),pid int);

# 查询出所有省份
select * from area where pid is null;

# 查询出 广东省  有哪些  市
select p.atitle,c.atitle from area as p inner join area as c on c.pid = p.aid where p.atitle = "广东省";

# 查询广州市 下有哪些区县
select c.atitle,s.atitle from area as c inner join area as s on s.pid =c.aid where c.atitle = "广州市";
13.子查询
  • 子查询 : 在一个select 语句中嵌入另外一个select语句,被嵌入的select语句称为子查询
  • 主查询 : 主要查询的对象,第一个select查询语句

主查询和子查询关系

  1. 子查询嵌入在主查询语句中
  2. 子查询辅助主查询的,要么充当条件,要么充当数据源
  3. 子查询是可以独立存在的语句,是一条完整的select语句。

注意 : 先执行子查询语句,在执行主查询语句

  • 标量子查询: 子查询返回的结果是一个数
  • 列级子查询: 返回的结果是一列(一列多行)
  • 行级子查询: 返回结果是一行 (一行多列)
标量子查询

需要在小括号中优先执行,是一个可独立执行的sql语句
~~~

查询出高于平均身高的信息

select * from student where height>(select avg(height) from student);
~~~

列子查询
# 查找 18,20,40岁学生信息
select * from student where age in (select age from student where age in (18,20,40));
行子查询
# 查询 班级年龄最大,身高最高的学生
select * from student where(age,height) = (select max(age),max(height) from student);

原文地址:https://www.cnblogs.com/mpc1996/p/11759431.html