• Mysql 基础


    ##判断数据库是否存在,存在就删除

    drop database if exists testdb;
    
    • 1

    创建数据库表的操作

    create database testdb;
    
    • 1

    ##使用数据库

    use testdb;
    
    • 1

    ##判断创建的表是否存在,存在就删除

    drop table if exists tb_testInfo;
    
    • 1

    ##创建数据表 create table 表名(字段名 类型 长度 <约束默认值,注释>)

    create table tb_testInfo (
     test_id integer(10) comment '测试主键' primary key auto_increment,
     test_name varchar(38) not null,
     test_pwd varchar(20) default '88888888' comment '测试密码'
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5

    mysql 表约束

    – 非空 NOT NULL 指定列在插入数据的时候必须有值 create table tb_testInfo (test_name varchar(38) not null)
    – 非负 UNSIGNED 插入的数字不能是负数
    – 主键 PRIMARY 列的值必须唯一 create table tb_testInfo (test_id integer(10) primary key)
    – 自增 AUTO_INCREMENT 只应用于整型的主键列 create table tb_testInfo (test_id integer(10) primary key auto_increment)
    – 默认 DEFAULT 指定默认值
    – 注释 COMMENT 说明字段 create table tb_testInfo (test_id integer(10) comment ‘测试主键’ primary key auto_increment)

    mysql 常用数据类型

    – 极小整形 TINYINT 非负最大值255 1个字节
    – 小整形 SAMLLINT 非负最大值65535 2个字节
    – 整形 INT 非负最大值4294967295 4个字节
    – 单精度 FLOAT 4个字节
    – 单精度 DECIMAL M+2字节 可以指定小树位数 DECIMAL(4,3)
    – 定长字符串 CHAR 最大保存255个字节 如果值没有到指定长度使用空格补充
    – 变长字符串 VARCHAR 最大保存255个字节 用多大占多大
    – 文本 TEXT 最大保存65535个字节
    – 日期 DATA
    – 日期 DATATIME

    ##判断创建的表是否存在,存在就删除

    drop table if exists tb_user;
    
    • 1

    创建用户表

    create table tb_user(
    	user_id int auto_increment primary key comment '用户编号',
    	user_name varchar(30) not null,
    	user_bir date,
    	user_gender char(3),
    	uesr_state tinyint(1) not null,
    	user_height decimal(4,1) not null,
    	user_dec text
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    表的操作

    表字段新增 语法:alter table tb_user add 字段名称 字段类型 长度 <约束默认值,注释>

    alter table tb_user add user_phone varchar(11) not null comment '用户电话';
    
    • 1

    表字段类型修改 语法

    alter table tb_user modify user_phone int(8) not null comment '用户电话';
    
    • 1

    表字段名称修改

    alter table tb_user change user_phone tel_phone varchar(11) not null comment '座机';
    
    • 1

    查询表字段详情

    desc tb_user;
    
    • 1

    表字段删除

    alter table tb_user drop tel_phone;
    
    • 1

    表名修改

    alter table tb_user rename to t_user;
    
    • 1

    表引擎修改

    alter table t_user engine=myisam;
    
    • 1

    删除表(暴力删除)

    drop table t_user;
    drop table if exists t_user;
    
    
    create table td_user3(
    	user_id int primary key,
    	`name` varchar(10)
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    查询当前数据库中所有的表

    show tables;
    
    • 1

    查询创建表时的SQL语句

    show create table td_user2;
    
    • 1

    表中的数据操作

    新增数据 insert into 表名称(字段名1,字段名2… 字段名n)values(值1,值2… 值n),(值1,值2… 值n)

    insert into td_user (name,user_sex,user_phone) values ('name',29,18);
    
    • 1

    简写前提是插入的数据对应所有的字段(需要对应主键)

    insert into td_user values (1,'name',29,18);
    
    • 1

    插入指定列的值(必须插入非空字段)

    insert into td_user (user_phone) values (1256);
    
    • 1

    修改表中的数据 update 表名 set 字段1=修改的值1,字段2=修改的值2… 字段n=修改的值n where 修改的条件

    update td_user set user_phone=200 where user_id = 3;
    
    • 1

    修改条件

    update td_user set user_phone=15 where user_phone < 20 or user_phone > 100;
    
    • 1

    删除表中的数据 delete from 表名 where 删除的条件

    delete from td_user where user_id = 3;
    
    • 1

    查询

    基础查询

    select * from td_user;
    
    • 1

    where 查询带条件查询(比较运算 < > <=, != <>, =)

    select * from td_user where name='age';
    select * from td_user where user_phone < 29;
    select * from td_user where user_phone != 29;
    
    • 1
    • 2
    • 3

    逻辑运算符(and or)

    select * from td_user where user_phone <= 15 and user_id = 2;
    select * from td_user where user_phone <= 15 or user_phone = 29;
    
    • 1
    • 2

    模糊匹配 (like,_占位符、%通配符)

    select * from td_user where `name` like 'name%';
    select * from td_user where `name` like 'na_%';
    
    • 1
    • 2

    查询包含字段的

    select * from td_user where `name` like '%name%';
    
    • 1

    IN 查询 多个查询

    select * from td_user where user_phone in (15,75,12);
    
    • 1

    group by 分组查询

    count(field) 统计数量

    select count(user_id) 用户数量 from td_user;
    
    • 1

    sum(field) 符合条件相加

    select sum(user_phone) 总数 from td_user where `name` like '%name%';
    
    • 1

    avg(field) 符合条件平均值

    select avg(user_phone) 平均 from td_user where `name` like '%name%';
    
    • 1

    统计所以信息中多个条件的平均值

    select avg(user_sex) 平均值 from td_user group by `name`;
    
    • 1

    显示多行

    select name as 名称, avg(user_sex) 平均值 from td_user group by `name`;
    
    • 1

    oeder by 排序查询

    select * from td_user order by user_id asc; ## 升序
    select * from td_user order by user_id desc; ## 降序
    select * from td_user where user_id > 1 order by user_id asc;
    
    • 1
    • 2
    • 3

    limit 查询结果截取

    查询最高值并截取第一个 limit 1 只能出现在最末尾

    select max(score) from td_score where cou_id = 1;
    select * from td_score where cou_id = 1 order by score desc limit 1;
    
    • 1
    • 2

    查询前几名 limit 3

    select * from td_score where cou_id = 2 order by score desc limit 3;
    
    • 1

    使用limit分页显示截取 代表第0个位置查询4条 0代表从那条数据开始往下查询 4 每页显示的数量

    select * from td_score limit 0,2; ## 第一页
    select * from td_score limit 2,2; ## 第二页
    select * from td_score limit 6,2; ## 第三页
    
    • 1
    • 2
    • 3

    合并查询

    # 前后查询结果互不干扰
    select * from td_score where id=2 union select * from td_score where pass=42;
    
    • 1
    • 2

    关联查询

    create table t_dept (
     dept_id  int primary key,
     dept_name varchar(30) not null
    )
    
    create table t_emt (
     emp_id int primary key,
     emp_name varchar(20) not null,
     emp_salary decimal(5,1) not null,
     dept_id int not null
    )
    
    insert into t_dept values(10,'研发部'),(20,'市场部'),(30,'销售部');
    
    insert into t_emt values(1,'zhangsna1',5550,10);
    insert into t_emt values(2,'zhangsna2',5550,10);
    insert into t_emt values(3,'zhangsna3',5550,10);
    insert into t_emt values(4,'wanwu1',3000,20);
    insert into t_emt values(5,'wanwu2',3000,20);
    insert into t_emt values(6,'lisi',3590,30);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    左关联 中关联 右关联 统称外光联

    左关联(查询的数据是根据主表中的数据限制的,哪怕是没有主表中对应的数据也会关联空的数据)

    select * from t_dept d left join t_emt  e on d.dept_id = e.dept_id;
    
    • 1

    中关联(查询存在关联的数据,不存在的关联信息自动去掉)

    select * from t_dept d join t_emt e on d.dept_id = e.dept_id;
    
    • 1

    右关联

    select * from t_dept d right join t_emt e on d.dept_id = e.dept_id;
    
    • 1

    内关联

    select * from t_dept d,t_emt e where d.dept_id = e.dept_id;
    
    • 1

    横链接 union(可以去掉重复数据)union all (不会排除重复的数据) 把两个查询的结果来显示

    select * from t_emt where dept_id in (10,20)
    union
    select * from t_emt where dept_id in (30);
    
    • 1
    • 2
    • 3

    mysql 函数

    ABS取绝对值

    select abs(-0.8),abs(0.8),abs(0);
    
    • 1

    ceiling 返回大于单前数的最小整数 cil 向上取整

    select ceiling(1.1),ceil(1.1);
    
    • 1
  • 相关阅读:
    qt实现coturn穿透客户端,coturn服务器搭建
    Spring Boot 到底是单线程还是多线程
    【图像处理】【应用程序设计】加载,编辑和保存图像数据、图像分割、色度键控研究(Matlab代码实现)
    【论文精读】Chain-of-Thought Prompting Elicits Reasoning in Large Language Models
    推荐系统笔记(八):推荐系统中的长尾效应
    Generative AI 新世界 | Falcon 40B 开源大模型的部署方式分析
    深度强化学习——DQN算法原理
    算法提升——LeetCode第385场周赛总结
    Rust 学习笔记(持续更新中…)
    批量录入表格中回车事件处理
  • 原文地址:https://blog.csdn.net/weixin_44640323/article/details/127793249