MySQL 基础

DDL数据定义语言#

DDL 数据表操作#

查询数据库

-- 显示当前MySQL中的数据库列表
show databases;

-- 显示指定名称的数据库创建的SQL指令
show create database db_test;

创建数据库

create database test;

create database if not exists test;

-- 在创建数据库的同事指定数据库的字符集
create database test character set utf8;

修改数据库 修改数据字符集

alter database test character set utf8;

删除数据库

drop database test;

-- 如果数据库存在则删除数据库
drop database is exists test;

DDL 数据表操作#

创建数据表

use student;
create table students(
	stu_num char(8) not null unique,
	stu_name varchar(20) not null,
	age int
);

not null
unique

查询全部数据表

show tables;

查询数据表结构

desc students;

删除数据表

drop table students;

-- 存在的话就删,没有还不报错
drop table if exists students;

修改数据表

-- 修改表名
alter table <tableName> rename to <newTableName>;
 
-- 修改字符集
alter table <tableName> character set utf8

-- 添加列
alter table <tableName> add <ColumnName> varchar(200)

-- 修改字段的列表和类型
alter table <tableName> change <oldColumnName> <newCloumnName> <type>

-- 只修改字段类型
alter table <tableName> modify <columnName> <newType>

-- 删除字段
alter table <tableName> drop <columnName>

MySQL数据类型#

数值类型#

类型 内存空间大小 范围 说明
tinyint 1byte 有符号 -128~127
无符号 0~255
特小型整数(年龄)
smallint 2 有符号 -3278~32767
无符号 0~65535
小型整数
mediumint 3 有符号 -231~231 - 1
无符号 0~2^32-1
中型整数
int/integer 4 整数
bigint 8 大型整数
float 4 单精度
double 8 双精度
decimal 一般情况:第一个参数+2 decimal(10,2)

字符串类型#

类型 字符长度 说明
char 0~255 byte 定长字符串
varchar 0~65535 可变长度字符串,此类型最大长度为65535
tinyblob 0~255 存储二进制字符串
blob 0~65535 存储二进制字符串
mediumblob 0~1677215 存储二进制字符串
longblob 0~4294957295 存储二进制字符串
tinytext 0~255 文本数据
text 0~65535 文本数据
mediumtext 0~1677215 文本数据
longtext 0~4294967295 文本数据

日期类型#

类型 格式 说明
date 2022-02-22 日期,只存储年月日
time 11:11:11 时间,只存储时分秒
year 2022 年份
datetime 2022-02-22 11:11:11 日期+时间,存储年月日时分秒
timestamp 20220222 112233 日期+时间(时间戳)

字段约束#

  • 非空约束(not null)

  • 唯一约束(unique)

  • 主键约束(primary key)

    • -- 添加方法
      create table books(
      	id char(4) primary key
      );
      -- 或者
      create table books(
      	id char(4),
          primary key(id)
      );
      -- 删除数据表的主键约束
      alter table book drop primary key;
      
      -- 创建表之后添加主键约束
      alter table book modify book_id char(4) primary key;
      
  • 主键自增长

    • auto_increment
      
  • 联合主键

    • -- 联合主键的定义
      create table test(
      	id1 char(8),
          id2 char(8),
          score int,
          primary key(id1,id2)
      );
      
  • 外键约束(foreign key)

DML 数据操纵语言#

插入数据#

-- 方式一
insert into tableName () values ();

-- 方式二
insert into tableName values ();

删除数据#

-- 语法
delete from <tableName> [where conditions];

-- 例
delete from student where id=1;
delete from student where age>18;
delete from student;

修改数据#

-- 语法
update <tableName> set columnName=value [where conditions]

-- 例
update student set name='tom' where age=18;
update student set name='old' where age>50;
update student set name='everyone';

DQL 数据查询语言#

  • 单表查询
  • 多表联合查询

查询基础语法#

-- 语法
select colnumName1[,colnumName2...]p from <tableName> [where conditions]

-- *通配符
select * from <tableName>

where 子句#

delete from tableName where conditions;
update tableName set ... where conditions;
select ... from tableName where conditions;

=	>	<	!=	<>	>=	<=	

between and(区间查询)
select * from student where age between 18 and 24;

条件逻辑运算符#

在where子句中,可以将多个条件通过逻辑运算(and or not)进行拼接,通过多个条件来查询。

select * from student where gender='女' and age=18;
select * from student where gender='女' or age<20;
select * from student where age not between 18 and 20;

like子句#

  • 在like关键字后的reg表达式中
    • % 表示任意多个字符
    • _ 表示任意一个字符
-- 查询姓张的
select * from student where name like '张%';

-- 设置查询的列
select age from student where name like '李%'; 

-- 计算列
select name,2023-age from student where name like '张%';

-- as字段取别名
select name,2023-age as brity from student;

distinct 去重#

select distinct age from student;

order by 排序#

select * from tableName where conditions order by cloumnName asc|desc;
  • order by columnName 表示将查询按照指定的列排序
    • asc 按照指定的列升序(默认,可以省略)
    • desc 按照指定的列降序
select * from student where age>18 order by gender asc;
select * from student order by gender asc;
select * from student order by gender desc;

-- 单字段排序
select * from student where age>18 order by gender asc;
-- 多字段排序
select * from student order by gender,age desc;

聚合函数#

  • count 统计函数

    • -- 统计性别为男的个数
      select count(gender='男') as '男生个数' from student;
      
  • max 最大值

  • min 最小值

  • sum 求和

  • avg 平均值

日期函数#

  • now( )
  • sysdate( )

字符串函数#

  • concat( )
  • upper( )
  • lower( )
  • substring(column, start, len) start从1开始

group by分组查询#

select gender,count(id) from student group by gender;


-- group by 一般和having一起使用
select age,count(num)
from student
group by age
having count(num)>1
order by age;

语句执行属性:

  1. 先根据where条件从数据库查询记录
  2. group by对查询记录进行分组
  3. 执行having对分组后的数据进行筛选

limit 分页查询#

-- 语法
select ...
from ...
where ...
limit param1,param2
  • param1 int,表示获取查询语句的结果中的第一条数据的索引(索引从0开始)
  • param2 int,表示获取的查询记录的条数(如果剩下的数据条数<param2,则返回剩下的所有记录)

执行顺序#

  • 书写顺序:select, from, where, group by, having, select, order by, limit
  • 执行顺序:join, from, where, group by, having, select, order by, limit
posted @   sroot  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示
主题色彩