数据库练习一

今天又开始了数据库的学习之路。加油吧。

首先创建、使用数据库,然后创建students和classes两张表:

-- students表
create table students(
id int unsigned primary key auto_increment not null,
name varchar(20) default '',
age tinyint unsigned default 0,
height decimal(5,2),
gender enum('男','⼥','中性','保密') default '保密',
cls_id int unsigned default 0,
is_delete bit default 0
);

create table classes (
id int unsigned auto_increment primary key not null,
name varchar(30) not null
);

然后将准备的数据写入:

-- 向students表中插⼊数据
insert into students values
(0,'⼩明',18,180.00,2,1,0),
(0,'⼩⽉⽉',18,180.00,2,2,1),
(0,'彭于晏',29,185.00,1,1,0),
(0,'刘德华',59,175.00,1,2,1),
(0,'⻩蓉',38,160.00,2,1,0),
(0,'凤姐',28,150.00,4,2,1),
(0,'王祖贤',18,172.00,2,1,1),
(0,'周杰伦',36,NULL,1,1,0),
(0,'程坤',27,181.00,1,2,0),
(0,'刘亦菲',25,166.00,2,2,0),
(0,'⾦星',33,162.00,3,3,1),
(0,'静⾹',12,180.00,2,4,0),
(0,'郭靖',12,170.00,1,4,0),
(0,'周杰',34,176.00,2,5,0);

insert into classes values 
(0, "python_01期"), 
(0, "python_02期");

创建好后 select 一下,出现了下面的情况:
在这里插入图片描述
有几个人的名字显示为 ”❓“ ,这是咋回事?抱着学习的态度在网上寻找出现问题的原因。虽然问题没有解决,但是还是学到了一些东西,当作知识储备了。mysql数据库表出现两种中文乱码问题的三种解决方案,大家在遇到类似问题可以参考下。

  • 查询所有字段
select * from 表名;
例:
select * from students;
  • 查询指定字段
select1,2,... from 表名;
例:
select name from students;
  • 使用 as 给字段起别名
select id as 标号, name as 名字, gender as 性别 from students;
  • 可以通过 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;
  • 消除重复行
select后⾯列前使⽤distinct可以消除重复的⾏

select distinct1,... from 表名;
例:
select distinct gender from students;

条件
使⽤where⼦句对表中的数据筛选,结果为true的⾏会出现在结果集中

  • 语法如下:
select * from 表名 where 条件;
例:
select * from students where id=1;

where后面支持多种运算符,进行运算处理。

  • 比较运算符
  • 逻辑运算符
  • 模糊运算
  • 范围查询
  • 空判断

比较运算符

  • 等于:=
  • 大于:>
  • 大于等于:>=
  • 小于:<
  • 小于等于:<=
  • 不等于:!= 或 <>

例一:查询标号不大于4的学生

select * from students where id <= 4;

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

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

逻辑运算符

  • and
  • or
  • not

例三:查询编号大于3的男同学

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

例四:查询编号⼩于4或没被删除的学⽣

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

模糊查询

  • like
  • %表示任意多个任意字符
  • _表示一个任意字符

例五:查询姓黄的学生

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

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

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

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

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

范围查询

  • in表示在⼀个⾮连续的范围内

例八:查询编号是1或3或8的学生

select * from students where id in(1,3,8);
  • between … and …表示在⼀个连续的范围内

例九:查询编号为3⾄8的学⽣

select * from students where id between 3 and 8;

例十:查询学⽣是3⾄8的男⽣

select * from students where id between 3 and 8 and gender=1;

空判断

  • 注意:null 与 ‘’是不同的
  • 判空 : is null
  • 判非空: is not null

例11:查询没有填写身⾼的学⽣

select * from students where  height is null;

例12:查询填写了身高的学生

select * from students where height is not null;

例13:查询填写了身⾼的男⽣

select * from students where height is not null and gender=1;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡小冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值