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

MYSQL-约束,条件判断,组函数

主键约束

1.创建表时候定义约束

create table person(
        id int ,
        name varchar(100),
        income decimal(18,2),
        primary key (id,name) 
    );

2.创建表之后,使用alter添加主键

alter table person3 add primary key(id);

主键自增

1.创建表的时候定义

create table person4(
    id int auto_increment ,
    name varchar(200),
     primary key(id)
);

2.创建表之后,使用alter定义自增

alter table person5 modify id int auto_increment;
//设置自增起始值
alter table person6 auto_increment=10000; 

外键约束

1.创建表时添加外键约束

create table teacher(
    id int ,
    name varchar(20),
    primary key (id)
);
create table student (
    id int ,
    name varchar(20),
    teacher_id int ,
    primary key (id),
    foreign key (teacher_id) references teacher(id)
);

2.创建完表之后,添加外键约束

alter table student1 add foreign key (teacher_id) references teacher1 (id);

唯一约束

1.创建表时,添加unique约束

create table temp (
    id int ,
    `name` varchar(20),
    unique(id)
);
或
   create table temp (
    id int  unique ,
    `name` varchar(20)
);

2.创建表之后,添加unique约束

alter table temp1 add unique (id);

非空约束,默认约束

1. 创建表时,添加约束

create table temp2(
    id int not null,
    `name` varchar(30) default  'abc',
    sex varchar(10) not null default '男'
);

2. 创建表时,添加约束

alter table temp3 modify id int not null ;
alter table temp3 modify name  varchar(30)   default  'abc';
alter table temp3 modify sex varchar(10) not null  default '男';

条件判断

1.and

select * from student where name='张三' and score > 90;

2.or

select * from student where name='张三' or score > 90;

关系表达式

  • > , >= , <  , <= ,<>,=
  • > : 大于
  • < : 小于
  • >= : 大于等于
  • <= : 小于等于
  • = : 相等
  • <> : 不等于

3.between and

select 列限定 from 表限定 where 列名 between 值1 and 值2;

4.in

select 列限定 from 表限定 where 列名 in(值1,值2....);

5.模糊查询 like

select 列限定 from 表限定 where 列名 like  '值' ;
  1. 其中 % 匹配任意个数的任意字符
  2.  _ 匹配单个任意字符

6.order by 排序

select 列限定 from 表限定 order by 列名 asc/desc;
Asc : 升序
Desc : 降序

7.limit 

select 列限定 from 表限定 limit 条数;
select 列限定 from 表限定 limit 开始值(不包含) ,条数;

组函数

  1. count(*) : 总条数
  2. max(字段名) : 最大值
  3. min(字段名) : 最小值
  4. avg(字段名) : 平均值
  5. sum(字段名) : 总和

1.group by

select teacher_id, count(*) as stu_count  from student group by teacher_id;

2.Having

select teacher_id, avg(score) as avg from student group by teacher_id having avg > 60;

子查询

1.Union与 union all 

  1. 合并查询,合并查询的结果
  2. Union 会去除重复项
  3. Union all 不会去除重复项
select * from student where teacher_id=1 
        union
select * from student where score > 60;

select * from student where teacher_id=1 
        union all
select * from student where score > 60;

它俩的作用是把两张表或者更多表合并成一张表

2.常用函数

  1. select version() ;显示当前MySQL软件的版本
  2. select database();显示当前所处数据库是哪个
  3. select  char_length('中国');返回字符个数。
  4. select  length('中国');返回字符所占字节数,MySQL中,一个UTF8编码的汉字占3个字节
  5. select  concat(  'a',  'b',  'c',  'd');返回  'abcd'。字符串拼接函数
  6. select  concat_ws(  '=',  'a',  'b',  'c');返回  'a=b=c'。字符串拼接函数,第一个是拼接间隔符
  7. select   upper('abcd');返回ABCD。将参数中所有小写字母转换为大写
  8. select  lower('ABCD');返回abcd。将参数中所有大写字母转换为小写
  9. select  substring(  '系统信息类',  1,  3  );返回  系统信。第2个参数代表从1开始的第几个字符,第3个参数代表截取字符个数

相关文章:

  • Docker环境下使用docker-compose一键式搭建RocketMQ(4.5.0版本)集群及其管理工具(外网版)
  • Python爬虫之Js逆向案例(11)-百度翻译
  • NX二次开发-外部开发模式exe(不打开NX进行后台操作)以及封装exe传参调用
  • 分享8个前端可以制作360度WebVr全景视图框架
  • 物联网毕设 -- 智能厨房检测系统
  • I2C基础
  • 社区老年人服务系统设计与实现(安卓APP+SSH后台+MYSQL)
  • 《Unity Magica Cloth从入门到详解》之(4)MeshCloth网布
  • uniapp小程序长按识别关注公众号
  • 计算机毕业设计springboot+vue基本微信小程序的乐旋乒乓球课程管理系统 uniapp 小程序
  • dir_day11
  • 漫画 | 打死我也不学编译了!
  • 14天机器学习DAY1-5|线性回归原理小结
  • 飞机电子式模拟空速表的设计与制作
  • 机器学习笔记 - YOLOv7 论文简述与推理
  • ----------
  • [LeetCode] Wiggle Sort
  • Angular4 模板式表单用法以及验证
  • canvas 五子棋游戏
  • const let
  • golang中接口赋值与方法集
  • JavaScript新鲜事·第5期
  • LeetCode29.两数相除 JavaScript
  • Linux链接文件
  • macOS 中 shell 创建文件夹及文件并 VS Code 打开
  • Otto开发初探——微服务依赖管理新利器
  • PHP的类修饰符与访问修饰符
  • Python 使用 Tornado 框架实现 WebHook 自动部署 Git 项目
  • Sequelize 中文文档 v4 - Getting started - 入门
  • spring学习第二天
  • Three.js 再探 - 写一个跳一跳极简版游戏
  • 从PHP迁移至Golang - 基础篇
  • 使用iElevator.js模拟segmentfault的文章标题导航
  • 你学不懂C语言,是因为不懂编写C程序的7个步骤 ...
  • ​​​​​​​STM32通过SPI硬件读写W25Q64
  • ​io --- 处理流的核心工具​
  • ######## golang各章节终篇索引 ########
  • #[Composer学习笔记]Part1:安装composer并通过composer创建一个项目
  • #laravel部署安装报错loadFactoriesFrom是undefined method #
  • #微信小程序:微信小程序常见的配置传值
  • #我与Java虚拟机的故事#连载09:面试大厂逃不过的JVM
  • (2021|NIPS,扩散,无条件分数估计,条件分数估计)无分类器引导扩散
  • (3)llvm ir转换过程
  • (二)延时任务篇——通过redis的key监听,实现延迟任务实战
  • (四) 虚拟摄像头vivi体验
  • (译)2019年前端性能优化清单 — 下篇
  • (转)一些感悟
  • **PHP二维数组遍历时同时赋值
  • ./configure、make、make install 命令
  • .Net mvc总结
  • .net 验证控件和javaScript的冲突问题
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地中转一个自定义的弱事件(可让任意 CLR 事件成为弱事件)
  • .Net8 Blazor 尝鲜
  • .NET开源、简单、实用的数据库文档生成工具
  • @Value获取值和@ConfigurationProperties获取值用法及比较(springboot)