MySQL基础(二)

本文详细介绍了MySQL数据库中的表结构操作,包括查看、创建、修改和删除表,以及数据的查询、添加、修改和删除。重点讲解了逻辑删除的概念,通过添加`is_delete`字段实现数据的软删除。此外,还涵盖了SQL语句中的as关键字用于字段和表的别名,以及distinct关键字用于去除重复数据。最后,讨论了where条件查询的各种运算符和模糊查询的应用,帮助读者掌握数据库的基本操作技巧。
摘要由CSDN通过智能技术生成

MySQL数据库基础知识(二)

表结构操作的SQL语句
  1. 查看当前数据库中的所有表

    show tables;
    
  2. 创建表

    create table students(
     id int unsigned primary key auto_increment not null,
     name varchar(20) not null,
     age tinyint unsigned default 0,
     height decimal(5,2),
     gender enum('男','女','人妖','保密')
    );
    

    说明:

    create table 表名(
    字段名称 数据类型 可选的约束条件
    ...
    
    );
    

    ps : auto_increment 表示自增

    查看表结构

    dese 表名;
    
  3. 修改表-添加字段

    alter table 表名 add 列名 类型 约束;
    例:
    alter table students add birthday datetime;
    

    说明:

    • modify: 只能修改字段类型或者约束,不能修改字段名
  4. 修改表-修改字段类型

    alter table 表名 modify 列名 类型 约束;
    例:
    alter table students modify birthday date not null;
    
  5. 修改表-修改字段名和字段类型
    alter table 表名 change 原名 新名 类型及约束;
    例:
    alter table students change birthday birth datetime not null;
    
    说明:
    
        change: 既能对字段重命名又能修改字段类型还能修改约束
    
  6. 修改表-删除字段

    alter table 表名 drop 列名;
    例:
    alter table students drop birthday;
    
  7. 查看创表SQL语句

    show create table 表名;
    例:
    show create table students;
    
  8. 查看创库SQL语句

    show create database 数据库名;
    例:
    show create database mytest;
    
  9. 删除表

    drop table 表名;
    例:
    drop table students;
    
表数据操作的SQL语句
  1. 查询数据

    -- 1. 查询所有列
    select * from 表名;
    例:
    select * from students;
    -- 2. 查询指定列
    select 列1,列2,... from 表名;
    例:
    select id,name from students;
    
  2. 添加数据

    -- 1. 全列插入:值的顺序与表结构字段的顺序完全一一对应
    insert into 表名 values (...)
    例:
    insert into students values(0, 'xx', default, default, '男');
    -- 2. 部分列插入:值的顺序与给出的列顺序对应
    insert into 表名 (列1,...) values(值1,...)
    例:
    insert into students(name, age) values('王二小', 15);
    -- 3. 全列多行插入
    insert into 表名 values(...),(...)...;
    例:
    insert into students values(0, '张飞', 55, 1.75, '男'),(0, '关羽', 58, 1.85, '男');
    -- 4. 部分列多行插入
    insert into 表名(列1,...) values(值1,...),(值1,...)...;
    例:
    insert into students(name, height) values('刘备', 1.75),('曹操', 1.6);
    
  3. 修改数据

    update 表名 set1=1,2=2... where 条件
    例:
    update students set age = 18, gender = '女' where id = 6;
    
  4. 删除数据

    delete from 表名 where 条件
    例:
    delete from students where id=5;
    

    问题:

    上面的操作称之为物理删除,一旦删除就不容易恢复,我们可以使用逻辑删除的方式来解决这个问题。

    -- 添加删除表示字段,0表示未删除 1表示删除
    alter table students add isdelete bit default 0;
    -- 逻辑删除数据
    update students set isdelete = 1 where id = 8;
    

    说明:

    • 逻辑删除,本质就是修改操作

as和distinct关键字

1. as关键字

在使用SQL语句显示结果的时候,往往在屏幕显示的字段名并不具备良好的可读性,此时可以使用 as 给字段起一个别名。

  1. 使用 as 给字段起别名

    select id as 序号, name as 名字, gender as 性别 from students;
    
  2. 可以通过 as 给表起别名

    -- 如果是单表查询 可以省略表名
    select id, name, gender from students;
    
    -- 表名.字段名
    select students.id,students.name,students.gender from students;
    
    -- 可以通过 as 给表起别名 
    select s.id,s.name,s.gender from students as s;
    

    说明:

    • 在这里给表起别名看起来并没有什么意义,然而并不是这样的,我们在后期学习 自连接 的时候,必须要对表起别名。
2. distinct关键字

distinct可以去除重复数据行。

select distinct1,... from 表名;

例: 查询班级中学生的性别
select name, gender from students;

-- 看到了很多重复数据 想要对其中重复数据行进行去重操作可以使用 distinct
select distinct name, gender from students;
3. 小结
  • as 关键字可以给表中字段 或者 表名起别名
  • distinct 关键字可以去除重复数据行。

where条件查询

学习目标

  • 能够写出模糊查询的SQL语句

1. where条件查询的介绍

使用where条件查询可以对表中的数据进行筛选,条件成立的记录会出现在结果集中。

where语句支持的运算符:

  1. 比较运算符
  2. 逻辑运算符
  3. 模糊查询
  4. 范围查询
  5. 空判断

where条件查询语法格式如下:

select * from 表名 where 条件;
例:
select * from students where id = 1;
2. 比较运算符查询
  1. 等于: =
  2. 大于: >
  3. 大于等于: >=
  4. 小于: <
  5. 小于等于: <=
  6. 不等于: != 或 <>

例1:查询编号大于3的学生:

select * from students where id > 3;

例2:查询编号不大于4的学生:

select * from students where id <= 4;

例3:查询姓名不是“黄蓉”的学生:

select * from students where name != '黄蓉';

例4:查询没被删除的学生:

select * from students where is_delete=0;
3. 逻辑运算符查询
  1. and
  2. or
  3. not

例1:查询编号大于3的女同学:

select * from students where id > 3 and gender=0;

例2:查询编号小于4或没被删除的学生:

select * from students where id < 4 or is_delete=0;

例3:查询年龄不在10岁到15岁之间的学生:

select * from students where not (age >= 10 and age <= 15);

说明:

  • 多个条件判断想要作为一个整体,可以结合‘()’。
4. 模糊查询
  1. like是模糊查询关键字
  2. %表示任意多个任意字符
  3. _表示一个任意字符

例1:查询姓黄的学生:

select * from students where name like '黄%';

例2:查询姓黄并且“名”是一个字的学生:

select * from students where name like '黄_';

例3:查询姓黄或叫靖的学生:

select * from students where name like '黄%' or name like '%靖';
5. 范围查询
  1. between … and … 表示在一个连续的范围内查询
  2. in 表示在一个非连续的范围内查询

例1:查询编号为3至8的学生:

select * from students where id between 3 and 8;

例2:查询编号不是3至8的男生:

select * from students where (not id between 3 and 8) and gender='男';

例3:查询编号不是3、5、7的人:

select * from students where id not in (3,5,7);

6. 空判断查询
  1. 判断为空使用: is null
  2. 判断非空使用: is not null

例1:查询没有填写身高的学生:

select * from students where height is null;

注意:

  1. 不能使用 where height = null 判断为空
  2. 不能使用 where height != null 判断非空
  3. null 不等于 ‘’ 空字符串
7. 小结
  • 常见的比较运算符有 >,<,>=,<=,!=
  • 逻辑运算符and表示多个条件同时成立则为真,or表示多个条件有一个成立则为真,not表示对条件取反
  • like和%结合使用表示任意多个任意字符,like和_结合使用表示一个任意字符
  • between-and限制连续性范围 in限制非连续性范围
  • 判断为空使用: is null
  • 判断非空使用: is not null

排序

排序查询语法:
select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...]

语法说明:

  1. 先按照列1排序、如果列1的值相同,则按照列2排序,以此类推
  2. asc 升序,默认按照升序排序
  3. desc 降序

eg:

select * from students where gender=1 and is_delete=0 order by id desc;

分页查询

目的:为了方便显示那种一页显示不完的查询结果

分页查询语法
select * from 表明 limit start,count

语法说明:

  1. limit是分页查寻关键字
  2. start表示开始行索引,默认是0
  3. count表示查询条数

eg:

select * from students limit 0,3;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值