自己对MySQ基础语句的理解分享(1)

发布时间:2026/7/17 5:05:49
自己对MySQ基础语句的理解分享(1) -- 这里写注释内容# 这里写注释内容show databases; #显示所有数据库名称create database test01; #创建数据库 test01create database if not exists test02; #如果 test02 这个数据库不存在则创建一个 test02 的数据库create database test03 character set utf8; #创建数据库 test03 并且设置字符集为 utf8create database test03 character set utf8mb4; #创建数据库 test03 并且设置字符集为 utf8mb4drop database test03; #删除数据库 test03use test; #使用数据库 test 用于切换到指定数据库中操作show tables; #显示所有表的名称SHOW CREATE DATABASE test; #显示数据库 test 的信息show create table info; #显示表 info 的信息CREATE table info(id int,name varchar(10),age int,gender varchar(1),height double,insert_time date) default charsetutf8mb4; #创建表 info 并设置 id 字段(列)为整数类型name 字段(列)为字符串类型age 字段(列)为整数类型gender 字段(列)为字符串类型height 字段(列)为浮点数类型insert_time 字段(列)为日期字符集设置为 utf8mb4drop table info; #删除表 infodesc info; # 查看 info 表的结构insert into info(name) values(王平); #给 info 表中 name 字段添加一个数据insert into info(id,name,age,gender,height,insert_time) values(1,张三,12,男,1.66,2025-5-21); #给 info 表中 id,name,age,gender,height,insert_time 字段分别添加一个数据insert into info(id,name,age,gender,height,insert_time) values(1,张三,12,男,1.66,2025-5-21),(2,李四,25,男,1.71,2025-5-21),(3,王五,39,男,1.87,2025-5-21); #给 info 表中 id,name,age,gender,height,insert_time 字段分别添加3个数据CREATE table info(id int primary key,name varchar(10),age int,gender varchar(1),height double,insert_time date)engine InnoDB default charsetutf8mb4; #创建表 info 并设置引擎为 InnoDB ...select * from info; #查询 info 表中所有内容delete from info; #删除 info 表中所有内容ALTER DATABASE test CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; #更改数据库 test 的字符集为 utf8mb4 并设置排序规则为不区分大小写alter table info convert to character set utf8mb4 collate utf8mb4_bin; #更改表 info 的字符集为 utf8mb4 并设置排序规则为区分大小写select name from info; #查询表 info 中 name 字段(列)的内容update info set age 39 where id 3; #只将 info 表中 id 字段(列)中等于 3 那一行的 age 字段(列)内容改为 39update info set age 39,gender 女 where id 3;#只将 info 表中 id 字段(列)中等于 3 那一行的 age 字段(列)内容改为 39gender 字段(列)的内容改为女delete from info where name 张三; #删除 info 表中 name 张三那一行的内容;select * from info where id 1; #查询 info 表中 id 1 的那一行内容select name,gender from info where id 1; #查询 info 表中 id 1 的那一行中 name 字段(列)和 gender 字段(列)的内容select name,gender from info; #查询 info 表中 name 字段(列)和 gender 字段(列)的内容select distinct gender from info; #查询 info 表中 gender 字段(列)的内容并且不显示重复的内容select name,age 1 from info; #查询 info 表中 name 字段(列)和 age 字段(列)的内容,并且将 age 字段(列)不为空的内容全部加一select name,IFNULL(age,0) 1 from info; #查询 info 表中 name 字段(列)和 age 字段(列)的内容,并且将 age 字段(列)为空的内容全部变成零然后全部加一age 字段(列)不为空的内容则直接加一select name,IFNULL(age,0) 19 from info where id 4; #实际上age并没有加19只是显示出来时加了19age要想真正加19需用updateselect name as 名字 from info; #查询 info 表中 name 字段(列)的内容,并且给 name 字段取一个名为 名字 的别名select * from info where age between 10 and 30; #查询 info 表中所有内容但是查询时要满足 age 字段(列)中的数据是大于等于10到小于等于30的select * from info where age 10 and age 30; #查询 info 表中所有内容但是查询时要满足 age 字段(列)中的数据是大于等于10到小于等于30的select * from info where age 12 or name 马六 or age 25; #查询 info 表中 age 12 或者 name 马六 或者 age 25 的那一行数据select * from info where age in(12,25,39);#查询 info 表中 age 12 或者 name 马六 或者 age 25 的那一行数据select * from info where age is NULL; #查询 info 表中所有内容但是会筛选 age 字段(列)为空的内容select * from info where age is not NULL; #查询 info 表中所有内容但是会筛选 age 字段(列)不为空的内容select * from product where name like %手%; #查询 product 表中所有内容但是查询时会满足 name 字段中的内容是有 手 的select * from product where name like 小米%; #查询 product 表中所有内容但是查询时会满足 name 字段中的内容是 小米 开头的select * from product where name like %手机; #查询 product 表中所有内容但是查询时会满足 name 字段中的内容是 小米 结尾的select * from students where name like ___; #查询 students 表中所有内容但是查询时会满足 name 字段中的内容是三个字符的select * from product where name like %手% or stock like %2%; #查询 product 表中所有内容但是查询时会满足 name 字段中的内容是有 手 的或者 stock 字段中的内容是有 2 的select count(*) from students; #查询 students 表中有多少行数据select count(name) from students; #查询 name 字段有几行数据select name,max(age) from students; #查询 students 表中 name 字段中的第一个数据和 age 字段中最大的数据(这样写name与age对应的数据会不准需用子查询才准)select name,min(age) from students; #查询 students 表中 name 字段中的第一个数据和 age 字段中最小的数据select name,sum(age) from students; #查询 students 表中 name 字段中的第一个数据和 age 字段中所有数据加在一起的值select name,avg(age) from students; #查询 students 表中 name 字段中的第一个数据和 age 字段中平均值select * from students order by age asc; #查询 students 表中所有内容并且 age 字段的内容按照从小到大顺序显示select * from students order by age; #查询 students 表中所有内容并且 age 字段的内容按照从小到大顺序显示select * from students order by age desc; #查询 students 表中所有内容并且 age 字段的内容按照从大到小顺序显示select * from product group by brand; #查询 product 表中所有内容并且 brand 字段的内容会分组重复的内容不显示会分到同一组select brand from product group by brand; #查询 product 表中 brand 字段的内容并且 brand 字段的内容会分组select brand,sum(price) from product group by brand; #查询 product 表中 brand 字段和查询每个组求和 price 字段过后的内容 并且 brand 字段的内容会分组select brand,sum(price) from product where price 3000 group by brand; #查询 product 表中 brand 字段和查询每个组求和 price 字段过后的内容 其中 price 3000 的数据才会分组select brand,sum(price) from product where price 8000 group by brand having sum(price) 9000; #查询 product 表中 brand 字段和查询每个组求和 price 字段过后的内容 其中 price 8000 的数据才会分组并且只显示 price 求和过后大于等于 9000 的数据select brand,sum(price) from product where price 3000 group by brand having sum(price) 9000 order by sum(price) desc; #查询 product 表中 brand 字段和查询每个组求和 price 字段过后的内容 其中 price 3000 的数据才会分组并且只显示 price 求和过后大于等于 9000 的数据同时 price 求和过后大于等于 9000 的数据按照从大到小排序select brand,sum(price) from product where price 3000 group by brand having sum(price) 9000 order by sum(price) asc; #查询 product 表中 brand 字段和查询每个组求和 price 字段过后的内容 其中 price 3000 的数据才会分组并且只显示 price 求和过后大于等于 9000 的数据同时 price 求和过后大于等于 9000 的数据按照从小到大排序select * from product limit 0,3; #查询 product 表中 1~3行数据其中 limit 0,3 这里面的 0 表示从第1行开始显示3 表示显示3行内容select * from product limit 3,1; #查询 product 表中第4行数据其中 limit 3,1 这里面的 3 表示从第4行开始显示1 表示显示1行内容create table students(id int primary key,name varchar(10),age int,gender varchar(1))default charset utf8mb4; #其中 id int primary key 这个表示给 id 字段设置为主键此时 id 字段的内容就是唯一的了不能重复create table students(id int primary key auto_increment,name varchar(10),age int,gender varchar(1))default charset utf8mb4; #其中 id int primary key auto_increment 这个表示给 id 字段设置为主键自增此时 id 字段的内容会在添加新内容时从1开始自动增加(id 字段的内容可以不用手动添加了)create table students(id int primary key auto_increment,name varchar(10) not NULL)default charset utf8mb4; #其中 name varchar(10) not NULL 这个表示当 students 表添加新内容时 name 字段必须要添加内容select version(); #查询系统版本show engines; #查询系统引擎select name,age from students where age (select max(age) from students); #子查询先执行 () 这里面的内容这里是用于查询年龄最大的学生是谁select name from students union all select name from product; #其中 union all 用于查询两个表的内容会显示重复的内容(两个表的字段数量需一致否则会报错)select * from students union all select * from product; #这里两个表中的字段数量不一致导致报错select name from students union all select name,price from product; #这里两个表中的字段数量不一致导致报错select name from students union select brand from product; #其中 union 用于查询两个表的内容不会显示重复的内容(两个表的字段数量需一致否则会报错)#外键约束create table sub(id int,product varchar(20),info_id int,primary key(info_id),constraint fk_empl foreign key (info_id) references info(id) #这里是给 sub 表创建外键约束)engine InnoDB default charset utf8mb4; #表的引擎必须是 InnoDB 才能创建外键约束ALTER TABLE sub ADD CONSTRAINT fk_empl_info_id FOREIGN KEY (info_id) REFERENCES info(id); #给 sub 表添加外键约束(前提 sub 表没有外键约束)alter table sub engine InnoDB; # 将 sub 表的引擎改为 InnoDBalter table sub drop foreign key fk_empl; #删除外键约束