当前位置: 首页 > news >正文

SQL数据库语句练习

1、mysql常用的数据类型是_整数(int)__、_小数(decimal)__、_字符串(varchar)__、_日期时间(datetime)___。

2、mysql的约束有__主键(primary key)_、_非空(not null)__、_唯一(unique)__、_默认值(default)___、_外键(foreign key)___。

3、数据表操作:

(1)创建学生表,字段要求如下:姓名(长度为10),年龄,身高(保留小数点2位)。

create table students(

id int unsigned,

name varchar(10),

age int unsigned,

height decimal(5,2)

)

(2)删除学生表。

drop table students

4、数据操作-增删改

(1)向students表中插入一个学生,设置所有字段信息:id(0),姓名(亚瑟),年龄(22),身高(175,5)。

insert into students values(0,’亚瑟’,22,175.5)

(2)向students表中插入一个学生,只设置姓名(老夫子)。

insert into students(name) values(‘老夫子’)

(3)在students表中修改id为5的学生数据,姓名改为狄仁杰,年龄为20。

update students set name=’狄仁杰’,age=20 where id=5

(4)在students表中删除id为6的学生数据。

delete from students where id=6

5、数据操作-条件查询

创建数据表如下:

drop table if exists students;

create table students(

studentNo varchar(10) primary key,

name varchar(10),

sex varchar(1),

hometown varchar(20),

age tinyint(4),

class varchar(10),

card varchar(20)

)

准备数据例如:

insert into students values

(‘001’,’王昭君’,’女’,’北京’,’20’,’1班’,’340322199001247654’)

(1)查询小乔的年龄。(比较运算符)

select age from students where name=’小乔’

(2)查询 20岁以下的学生。(比较运算符)

select * from students where age<20

(3)查询家乡不在北京的学生。(比较运算符)

select * from students where hometown!=’北京’

(4)查询学号是’007’的学生的身份证号。(比较运算符)

select card from students where studentNo=’007’

(5)查询’1班’以外的学生的信息。(比较运算符)

select * from students where class!=’1班’

(6)查询年龄大于20的学生的姓名和性别。(比较运算符)

select name and age from students where age>20

(7)查询河南或河北的学生。(逻辑运算符)

select * from students where hometown=’河南’ or hometown=’河北’

(8)查询’1班’的’上海’的学生。(逻辑运算符)

select * from students where calss=’1班’ and  hometown=‘上海’

(9)查询非20岁的学生。(逻辑运算符)

select * from students where age!=20

(10)查询姓名为两个字的学生。(模糊查询)

select * from students where name like ‘__’

(11)查询姓百且年龄大于20的学生。(模糊查询)

select * from students where name like ‘百%’ and age>20

(12)查询学号以1结尾的学生。(模糊查询)

select * from students where studentNo like ‘__1’

(13)查询年龄在18或19或22的学生。(范围查询)

select * from students where age in(18,19,22)

(14)查询年龄在20到25以外的学生。(范围查询)

select * from students where age not between 20 and 25

(15)查询没有填写身份证的学生。(空判断)

select * from students where card is null

(16)查询填写了身份证的学生。(空判断)

select * from students where card is not null

(17)查询所有学生信息,按班级从小到大排序,班级相同时,再按学号从小到大排序。(排序)

select * from students order by class ,studentNo

http://www.lryc.cn/news/415694.html

相关文章:

  • 【Python】常用的pdf提取库介绍对比
  • sbatch提交并行作业 运行python程序 指定输入参数从1到100
  • OD C卷 - 中庸行者
  • 最新CSS3横向菜单的实现
  • (2024,LlamaGen,Llama,自回归下一token预测,模型扩展)自回归模型优于扩散:Llama 用于可扩展图像生成
  • 重新安装操作系统的软件都有哪些?
  • 深圳水务展|2025深圳国际水务科技博览会
  • OpenAI not returning a result?
  • [Windows]_[初级]_[GetVersionEx获取系统版本错误的原因]
  • 2024,Java开发在中国市场还有发展前景吗?
  • gcc: string.c_str gcc-8.5的一个问题
  • 一道笔试题 - 无重复字符的最长子串
  • C#反射的NullReferenceException
  • 100道C/C++面试题
  • Python(模块)
  • 【八股文】Java基础篇
  • python rsa如何安装
  • P10289 [GESP样题 八级] 小杨的旅游
  • 网络编程 ----------- 4、组播与广播
  • 最短路径算法:Bellman-Ford算法
  • 爬虫:xpath模块及昵图网实例
  • 高级java每日一道面试题-2024年8月03日-web篇-forward和redirect有什么区别?
  • 如何让你的网站拥有更好的体验
  • opencascade AIS_TypeFilter AIS_XRTrackedDevice源码学习
  • 使用Spring AOP监控指定方法执行时间
  • 最新CSS3纵向菜单的实现
  • GooLeNet模型搭建
  • 使用ThreadLocal来存取单线程内的数据
  • elasticsearch教程
  • Arrays、Lambda表达式、Collection集合