干货篇,快来一起SQL学习吧

清安无别事
自动化测试优质创作者
2021-11-11 14:13:32

目录

 

前言

数据类型

创建数据库

查询数据库基本信息

使用数据库

创建数据表

查询表单

查询表单基本信息

查询表的字段信息

修改表名

修改字段名及数据类型

修改数据类型

增加字段

删除字段

删除数据表

增删改查

增:

改:

删:

查:

 查询指定字段:

过滤重复信息:

聚合函数

count

max

min

sum

avg

关键字查询in

not in

between and

not between and

反用null查询

and查询

or查询

字符串匹配查询

通配字符串查询

limit限制查询

order by查询

group by

数据表约束

取别名

表的关联查询


前言

        学习数据库之前,首先你得安装好数据库,Mysql、Oracle、SQL Server、Sqlite,这几种数据库都是可以的,Mysql性能较好,适应于所有平台,开源,是最流行的关系型数据库。       SQLserver数据库扩展性和可维护性、安全性好,是比较全面的数据库。还有一种比较主流的数据库是Oracle,Oracle数据库适合大型数据库,Mysql和SQLserver适合中小型数据库,一般使用Mysql就可以,比较轻量,而且性能较好。
概念

        数据库在操作时,需要使用专门的数据库操作规则和语法,这个语法就是 SQL(Structured Query Language) 结构化查询语言。

        SQL 的主要功能是和数据库建立连接,进行增删改查的操作。SQL是关系型数据库管理系统的标准语言。

        数据库服务器其实就是一个软件,比如我们安装的mysql软件(或者mariadb软件),Mysql服务器软件需要安装在服务器硬件上(就是一台计算机)才可以让外界来访问。
什么是SQL 语言:

    1. 数据定义语言 DDL(Data Definition Language) 。用于创建数据库,数据表。

    2. 数据操作语言 DML(Data Manipulation Language) 。用于从数据表中插入、修改、删除数据。

    3. 数据查询语言 DQL(Data Query Language) 。用于从数据表中查询数据。

    4. 数据控制语言 DCL(Data Control Language) 。用来设置或修改数据库用户或角色的权限。

注意点:

        使用 SQL 操作数据库时,所有的 SQL 语句都以分号结束。(切换数据库时可以不用分号)

        在 SQL 语句中,不区分大小写,编写 SQL 语句时可以根据情况用大小写的区别来增加可读性。


数据类型

     常用的数据类型有:

    1、整数类型(INT)占位4字节

    2、浮点类型有两种:单精度浮点数类型(FLOAT)占位4字节和双精度浮点数类型(DOUBLE)占位8字节

    3、字符串类型(CHAR) 占3字节和 (VARCHAR)4字节

    4、日期与时间类型(YEAR)占1字节、(DATE)占4字节、(TIME)占3字节。

基本操作

        建库,建表,添加、修改、删除表单等,以及增删改查!随意建个库,MySQL对于字母大小写没有严格的要求,必须规定你大写或小写。你随意,开心了大写,不开心了小写。😕


创建数据库

    create database 数据库名称;
    create database qingan CHARACTER SET utf8 COLLATE utf8_general_ci;

查询数据库基本信息

    show create database 数据库名称;
    show create database qingan;

使用数据库

    use 数据库名称;
    use qingan;

创建数据表

        这里将上面的例子全部用下来,活学活用,学以致用。💗,由建库到建表

    CREATE DATABASE qingan;
    use qingan;
    create table wubieshi(
                        id   varchar(18),
                        sex  char(10),
                        c_name    varchar(10)
    );

查询表单

show tables;

查询表单基本信息

    show create table 表单名称;
    show create table wubieshi;

查询表的字段信息

    desc 表单名称;
    desc wubieshi;

修改表名

    alter table 原表单名称 rename to 想要修改的名称;
    alter table wubieshi rename to qing;

修改字段名及数据类型

        这里因为上述改了表名所以后续都用qing这个表名。

    alter table 表单名称 change 原字段名 修改字段名 数据类型;
    alter table qing change id id_number varchar(40);

修改数据类型

    alter table 表名 modify 字段名 数据类型;
    alter table qing modify id varchar(10);

增加字段

    alter table 表名 add 增加字段名 数据类型;
    alter table qing add c_name varchar(30);

删除字段

    alter table 表名 drop 字段名;
    alter table t_beauty drop c_name;

删除数据表

    drop table 表名;
    drop table qing;

增删改查

        此处我们从简,别搞的那么麻烦,所以你建库建表的时候就必须要想清除了,别后面删删改改的,倒时候自己都搞错了。


增:

INSERT INTO 表名(字段名1,字段名2,...) VALUES (值 1,值 2,...);

insert into qing(id,sex,home,call_num) VALUES('小芳','女','北京',132465);

同时增加多条数据:

    insert into qing(id,sex,home,call_num) VALUES('小芳','女','北京',132465),
                                                      ('小小','女','上海',777555),
                                                      ('小沐','男','广东',1525465)

改:

        此处是将搜索id为晴雪的改成小芳,切勿搞混了。

    UPDATE 表名 SET 字段名1=值1 [WHERE 条件表达式];
    update qing set id = '小芳' where id='晴雪';

        指定一个字段数据,将所有的id字段名都更改为小北

update student set id='小北';

删:

    DELETE FROM 表名 [WHERE 条件表达式];
    delete from qing where id = '小小';

删除所有数据:

    delete from 表1;
    delete from wubieshi;

查:

    select * from 表名;
    select * from wubieshi;

 查询指定字段:

    select 字段名1,字段名2 from 表名;
    select id,name form qing;

过滤重复信息:

        过滤掉重复的name名字

    select distinct 字段名 from 表名;
    select distinct name from qing;

聚合函数


count

    查询表中有多少人

    select count(*) from 表名;
    select count(*) from qing;

max

    计算指定列最大值,这里只是举例,没有谁会去计算id的,没啥意义。

    select max(字段名) from 表名;
    sele max(id) from qing;

min

    计算指列最小值

    select min(字段名) from 表名;
    select min(id) from qing;

sum

    求和

    select sum(字段名) from 表名;
    select sum(id) from qing;

avg

    求平均值

    select avg(字段名) from 表名;
    select avg(id) from qing;

关系运算符
关系运算符    说明
=    等于
<>    不等于
<    小于
>    大于
<=    小于等于
>=    大于等于
!=    不等于

        关系运算符不要求全部记住,但是这里的必须要会,毕竟后面还是要用的。看看运用

    select * from student where 字段名>=条件;
    select * from student where age>=17;

关键字查询
in

    用于判断某个字段的值是否在指定集合中

    select * from 表名 where 字段名 in ('字段1');
    select * from qing where id in ('穆雪');

not in

    用于判断某个字段的值不在在指定集合中,不在输出全部

    select * from 表名 where 字段名 not in ('字段1');
    select * from qing where id not in ('穆雪');

between and

    select * from 表名 where 字段名 between 条件1 and 条件2;
    select * from qing where age between 15 and 18;

not between and

    select * from 表名 where 字段名 not between 条件1 and 条件2;
    select * from qing where age not between 15 and 18;

反用null查询

    查询表中不是空值的值(字段)

    select * from 表名 where 字段名 is not null;
    select * from qing where id is not null;

and查询

    select * from 表名 where 条件判断 and 条件判断;
    select * from qing where age>18 and id='沐雪';

or查询

    select * from 表名 where 条件查询 or 条件查询;
    select * from qing where age>15 or id='沐雪';

字符串匹配查询


普通字符串查询

    查询id为‘沐雪’的人

    select * from 表名 where 字段名 like 字符串;
    select * from qing where id like '沐雪';

%通配的字符串查询

    查询id为‘沐雪~’的人,如:沐雪,沐雪雪,沐雪砅等

    select * from 表名 where 字段名 like '字段1%';
    select * from qing where id like '沐雪%';

查询以某字符串结尾的记录

    查询以‘~雪’结尾的记录,如:沐雪,夏雪等

    select * from 表名 where 字段名 like '%字段1';
    select * from qing where id like '%雪';

通配字符串查询

    下划线通配符只匹配单个字符,如果要匹配多个字符,需要连续使用多个下划线通配符。单个下划线只匹配一个字符串,两个下划线匹配多个字符串。

    select * from 表名 where 字段名 like '字段1__';
    select * from qing where id like 'x__';
    select * from qing where id like 'x_';

limit限制查询

select * from qing limit i,n;

    # qing:表名
    # i:为查询结果的索引值(默认从0开始),当i=0时可省略i
    # n:为查询结果返回的数量
    # i与n之间使用英文逗号","隔开

         查询5条数据,从3开始往后查询5条数据

select * from qing limit 2,5;

order by查询

    ORDER BY默认从小到大,asc也是从小到大排序,可以忽略不写,desc从大到小排序

    select * from 表名 order by 字段名 asc;
    select * from qing order by age asc;

    select * from 表名 order by 字段名 desc;
    select * from qing order by age desc;

limit结合order by查询

select * from qing order by age asc limit 5;

group by

    多个列中取相同值,如下,在表t_beauty中取相同的字段名为id的值

    select count(*), 字段名 from 表名 group by 字段名;
    select count(*), id from qing group by id;

 结合where使用

    查询表中id>1001的数据

    select count(*), 字段名 from 表名 where 字段名>值 group by 字段名;
    select count(*), id from qing where id>1001 group by t_beauty;

组合having使用

    查询班级里薪水大于2000的数据,这里值得注意的是having后面必须接聚合函数

    select sum(字段名1),字段名2 from 表名 group by 字段名2 having sum(字段名1)>值;
    select sum(salary),class from qing group by calss having sum(salary)>2000;

数据表约束


主键约束

    create table qing(
    id int primary key,
    names varchar(20)
    );

非空约束

    create table qing(
    id int,
    names varchar(20) not null
    );

默认约束

    即给定默认值

    create table qing(
    id int,
    names varchar(20),
    gender varchar(10) default 'cha'
    );

唯一约束

    create table qing(
    id int,
    names varchar(20) unique
    );

外键约束

    CONSTRAINT fk_qing FOREIGN KEY(Id) REFERENCES qingan(id)
    CONSTRAINT 从表 FOREIGN KEY(字段名) REFERENCES 主表(字段名)

    create table qing(
    id int,
    names varchar(20) unique,
    CONSTRAINT fk_qing FOREIGN KEY(Id) REFERENCES qingan(id)
    );

    # 创建数据表创号后语法如下:
    ALTER TABLE 从表名 ADD CONSTRAINT 外键名 FOREIGN KEY (从表外键字段) REFERENCES 主表 (主键字段);
    # 删除外键约束
    alter table 从表名 drop foreign key 外键名;

取别名

    select * from 原表名 as 表名;
    select * from qing as qingan;

    select 原字段名 as 字段名 from 表名;
    select id as c_id from qingan;

表的关联查询

    适用于表与表之间有相同的数据,如class,classroom1中nname,cname字段中有相同值

    select * from 表名1 where 字段名1=(select 字段名2 from 表名2 where 字段名3='值');
    select * from class where nname=(select cname from classroom1 where cid=2);

关联删除

    DELETE t1 FROM t1,t2 WHERE t1.id=t2.id
     
    delete from 表 where 字段名=值;
    delete from qing where id=1;

多表连接查询

    SELECT * FROM 表1 CROSS JOIN 表2;
    select * from qing cross join qingan;

内连接查询

     内连接(Inner Join)又称简单连接或自然连接,是一种非常常见的连接查询。内连接使用比较运算符对两个表中的数据进行比较并列出与连接条件匹配的数据行,组合成新的记录。

         这里是查询id相同的字段:

    SELECT 表名1.字段,表名2.字段, ... FROM  表1 [INNER] JOIN  表2 ON 表2.关系字段=表1.关系字段
    SELECT qing.id,qingan.cid FROM qing INNER JOIN qingan ON qingan.cid = qing.id;

         如果是三个表,我需要查询其中的id相同的name:

SELECT qing.id,qing.bname,qingan.gname FROM qing INNER JOIN qingan ON qingan.hid = qing.hid;

外连接查询

    外连接又分为左外连接和右外连接,其用法类似,以其中一张表为主表,另外一张为从表。

    1、LEFT [OUTER] JOIN 左(外)连接:返回包括左表中的所有记录和右表中符合连接条件的记录。
    2、RIGHT [OUTER] JOIN 右(外)连接:返回包括右表中的所有记录和左表中符合连接条件的记录。

    注意事项:关联查询的时候,字段名可以不一样,当时数据类型需要一致,右外连接只需要将下面的例子left改为right即可使用,注意主从表即可

    注意:外连接查询也可以是删除!!!

    select 表1.字段1,表1.字段2,表2.字段3 from 表1 left outer join 表2 on 表1.关系字段1=表2.关系字段2;
    select class.did,class.dname,student.sname from class left outer join student on class.did=student.classid;

子查询

    子查询是指一个查询语句嵌套在另一个查询语句内部的查询;

比较运算符的子查询

    查询qingan所在班级的信息

    select * from 表1 where 字段名1>(select 字段名2 from 表2 where 字段名3=查询数据);
    select * from d_class where c_id>(select d_id from d_student where d_name='qingan');

exists

     exists关键字后面的参数可以是任意一个子查询, 它不产生任何数据只返回TRUE或FALSE。当返回值为TRUE时外层查询才会 执行

    假如qingan同学在学生表中则从班级表查询所有班级信息(看例子:)

    select * from 表1 where exists (select * from 表2 where 字段名2=查询值);
    select * from d_class where exists (select * from d_student where d_name='qingan');

any

    any关键字表示满足其中任意一个条件就返回一个结果作为外层查询条件。

    查询班级id比学生id还大的id(看例子)

    select * from 表1 where 字段1 > any (select 字段2 from 表2);
    select * from d_class where d_id > any (select c_id from student);

all

    all关键字与any有点类似,但是all关键字的子査询返回的结果需同时满足所有内层査询条件

    查询比学生所属班级的id还大的id(看例子)

    select * from 表1 where 字段1 > all (select 字段2 from 表2);
    select * from class where d_id > all (select c_id from student);

 数据库就到这了,身为测试这些差不多够用了,例子写法都给到给位朋友了,给个评论点个赞吧。很久没写数据库了,本篇也是博主自己的复习篇,望对于大家也是有用的。

...全文
29 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
java_wxid 2021-11-11
  • 打赏
  • 举报
回复

我来打卡啦

java_wxid 2021-11-11
  • 打赏
  • 举报
回复

我来打卡啦

清安无别事 2021-11-11
  • 打赏
  • 举报
回复 1

打卡

67,944

社区成员

发帖
与我相关
我的任务
社区描述
灌水发信息每周送书 灌水发干货每周送惊喜 谁最水过年送大礼 谁最硬核过年送大礼 谁最贡献过年送大礼
社区管理员
  • 1_bit
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

我们“新手村”社区就是大家的孵化器

你们学习,我来评分

每周最高分送一本书

每月第四周送一份机械键盘

咱们新手村使用一切狂暴手段让优秀的人得到奖励

 

试试用AI创作助手写篇文章吧