当前位置:网站首页>MySQL常用命令
MySQL常用命令
2022-07-28 05:28:00 【【异度空间】】
MySQL常用命令
一、数据库操作
1.创建数据库
CREATE DATABASE 数据库名;
-- 创建一个名字为python_test的数据库
CREATE DATABASE python_test;
2.删除数据库
DROP DATABASE 数据库名;
-- 删除名字为python_test的数据库
DROP DATABASE python_test;
3.查看所有数据库
SHOW DATABASES;
4.切换数据库
use 数据库名;
-- 选择名字为python_test的数据库
use python_test;
5.退出mysql
exit;
6.查看创建数据库的SQL语句
SHOW CREATE DATABASE 数据库名;
-- 显示创建数据库python_test的SQL语句
SHOW CREATE DATABASE python_test;
二、数据库表操作
1.创建表
CREATE TABLE students(id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT NOT NULL,NAME VARCHAR(20) NOT NULL,age TINYINT DEFAULT 0,gender ENUM("男","女") DEFAULT "男");
2.删除表
DROP TABLE students;
3.查看被选中数据库中的表
SHOW TABLES;
4.查看表结构
DESC 表名;
5.ALTER操作
(1)给表添加一个字段
ALTER TABLE 表名 ADD 字段名 字段类型;
-- 给students表添加一个birthday字段
ALTER TABLE students ADD birthday DATETIME NOT NULL;
(2)修改表指定字段类型
ALTER TABLE 表名 MODIFY 字段名 字段类型;
-- 修改students表中birthday字段的类型
ALTER TABLE students MODIFY birthday DATE;
(3)同时修改表的字段名和字段类型
ALTER TABLE 表名 CHANGE 原字段名 新字段名 新字段类型;
-- 修改students表中原字段为birthday的字段名称及类型
ALTER TABLE students CHANGE birthday birth DATETIME NOT NULL;
(4)删除表指定字段
ALTER TABLE 表名 DROP 字段名;
-- 删除students表中的birth字段
ALTER TABLE students DROP birth;
6.显示创建指定表的SQL语句
SHOW CREATE TABLE 表名;
-- 显示创建students表的SQL语句
SHOW CREATE TABLE students;
7.INSERT插入操作
INSERT INTO 表名 VALUES(字段1值,字段2值,...,字段n值);
-- 给students表插入一条数据(单行插入)
-- 主键列插入数据的时候可以指定为 0,null,default
INSERT INTO students VALUES(0,'Tom',18,DEFAULT);
INSERT INTO students VALUES(DEFAULT,'Yellow',28,DEFAULT);
INSERT INTO students VALUES(NULL,'LiLei',38,DEFAULT);
-- 部分列单行插入
INSERT INTO students(NAME,age) VALUES('Bob',26);
-- 全部列单行插入
INSERT INTO students VALUES(0,'Lisa',22,'女');
-- 给students表一次性插入多条数据(多行插入)
-- 部分列多行插入
INSERT INTO students(NAME,age) VALUES('Jian',23),('Anla',40);
-- 全部列多行插入
INSERT INTO students VALUES(0,'Linda',25,'女'),(0,'Daive',27,'男');
8.UPDATE更新操作
UPDATE 表名 SET 字段1=值1,字段2=值2,...,字段2=值n WHERE 字段m=值m;
-- 修改students表中NAME字段值为‘Anla’的数据
UPDATE students SET age=33,gender='女' WHERE NAME='anla';
9.DELETE删除操作
DELETE FROM students [WHERE 字段m=值m];
-- 删除表全部数据(物理删除)
DELETE FROM 表名;
-- 删除指定“字段m=值m”的数据(物理删除)
DELETE FROM 表名 WHERE 字段m=值m;
-- 物理删除students表中"id=8"的数据
DELETE FROM students WHERE id=8;
-- 设置删除字段is_del(逻辑删除)
UPDATE 表名 SET is_del=1 WHERE 字段m=值m;
-- 逻辑删除students表中NAME=‘Lisa’的数据
UPDATE students SET is_del=1 WHERE NAME='Lisa';
10.SELECT查询操作
包括:条件查询,范围查询,模糊查询,空判断查询,排序,分页查询,聚合函数,分组查询,连接查询,子查询
-- as和distinct关键字在查询中的使用
-- as 起别名 字段和表都可以用as关键字进行起别名
SELECT 字段名1 AS 别名1,字段名2 AS 别名2,...,字段名n AS 别名n FROM 表名 AS 表别名;
-- as 别名查询例子
SELECT NAME AS 姓名,age AS 年龄 FROM students;
SELECT NAME AS 姓名,age AS 年龄 FROM students AS s;
--as 关键字省略形式
SELECT NAME 姓名 FROM students s;
-- distinct 去重 去掉查询中相同字段名中的数据,只保留一条数据
SELECT DISTINCT 字段名1[,字段名2,...,字段名n] from 表名;
-- distinct去重查询例子
SELECT DISTINCT gender FROM students;
SELECT DISTINCT age,gender FROM students;
--------------------------------------------------
-- 条件查询:where
-- 比较运算符: > , >= , < , <= , !=
SELECT * FROM students WHERE id>3; -- 查询id>3的学生信息
SELECT * FROM students WHERE id<=4;
SELECT * FROM students WHERE NAME!='Bob'; -- 查询name不是Bob的信息
--逻辑运算符:and , or , not
SELECT * FROM students WHERE id>3 AND gender='女'; -- 查询id范围大于3并且性别为‘女’的信息
SELECT * FROM students WHERE id<4 OR is_del=0; -- 插叙id范围小于4或者删除标记为0的信息
SELECT * FROM students WHERE NOT (age>=18 AND age<=30);
--------------------------------------------------
-- 范围查询:between ... and , in
SELECT * FROM students WHERE id BETWEEN 3 AND 8; -- 查询id范围在3-8的信息
SELECT * FROM students WHERE NOT (id BETWEEN 3 AND 8);
SELECT * FROM students WHERE id IN (1,3,5,7); -- 查询id范围在(1,3,5,7)的信息
SELECT * FROM students WHERE id NOT IN (1,3,5,7);
--------------------------------------------------
-- 模糊查询:like [ 配合正则表达式 "%"(任意多个关键字) , "_"(任意一个关键字) ]
SELECT * FROM students WHERE NAME LIKE 'L%';
SELECT * FROM students WHERE NAME LIKE 'L___';
SELECT * FROM students WHERE NAME LIKE '%w' OR NAME LIKE 'L%';
--------------------------------------------------
-- 空判断查询:is null , is not null
SELECT * FROM students WHERE height IS NULL; -- 查询身高为空的信息
SELECT * FROM students WHERE height IS NOT NULL; -- 查询身高非空的信息
--------------------------------------------------
-- 排序:order by 升序(order by 字段名 ASC) 降序(order by 字段名 DESC)
SELECT * FROM students WHERE gender='男' AND is_del=0 ORDER BY id DESC; -- 查询性别是男且未被删除的信息,降序排列
SELECT * FROM students ORDER BY age DESC;
SELECT * FROM students ORDER BY age DESC,height DESC; -- 先按照年龄降序,年龄相同再按照身高降序进行排列
SELECT * FROM students ORDER BY age ASC;
--------------------------------------------------
-- 分页查询
select * from 表名 limit start,count; -- start是开始行索引,count是查询条数
SELECT * FROM students WHERE gender='男' LIMIT 0,3; -- 显示性别是男性的前三条数据
SELECT * FROM students WHERE gender='男' LIMIT 3; -- 简写: 显示性别是男性的前三条数据
-- 获取第n页数据
SELECT * FROM students LIMIT (n-1)*m,m;
--------------------------------------------------
-- 聚合函数(一般结合分组进行统计 不对null进行统计)
-- count(col) 表示求指定列的总行数
SELECT COUNT(id) FROM students;
SELECT COUNT(*) FROM students;
-- max(col) 表示求指定列的最大值
SELECT MAX(id) FROM students WHERE gender='女';
-- min(col) 表示求指定列的最小值
SELECT MIN(id) FROM students WHERE is_del=0;
-- sum(col) 表示求指定列的和
SELECT SUM(height) FROM students WHERE gender='男';
-- avg(col) 表示求指定列的平均值
SELECT AVG(height) FROM students WHERE gender='男';
SELECT AVG(IFNULL(height,0)) FROM students WHERE gender='男'; -- 发现空值当作0进行统计
--------------------------------------------------
-- 分组查询
--group by 字段名 (按照指定字段进行分组后只能按照指定字段进行查询)
SELECT gender FROM students GROUP BY gender;
SELECT gender,name FROM students GROUP BY gender,name;
-- group_concat(字段名) (根据指定的分组字段名进行统计指定字段名的信息集合)
-- 根据gender进行分组(实际上是分成了 男 女 两个集合),并统计name字段分别在男女集合中的分布情况,使用','进行分割
SELECT gender,GROUP_CONCAT(NAME) FROM students GROUP BY gender;
SELECT gender,AVG(age) FROM students GROUP BY gender;
SELECT gender,COUNT(*) FROM students GROUP BY gender;
-- 对分组过滤使用having
SELECT gender,COUNT(*) FROM students GROUP BY gender HAVING COUNT(*)>3;
-- with rollup 最后一行加上汇总信息 只会对集合函数这一列进行汇总
SELECT gender,COUNT(*) FROM students GROUP BY gender WITH ROLLUP;
SELECT gender,GROUP_CONCAT(age) FROM students GROUP BY gender WITH ROLLUP;
--------------------------------------------------
-- 连接查询 (实现多个表的联表查询)
-- 内连接 多张表符合条件的公共数据 交集
select 字段 from 表1 inner join 表2 on 表1.字段=表2.字段
SELECT * FROM students INNER JOIN class ON students.c_id=class.id;
SELECT s.name,c.name FROM students AS s INNER JOIN class AS c ON s.c_id=c.id;
-- 左连接 以左表为主根据条件查询右表数据,如果根据条件查询右表数据不存在使用null填充 left左边是左表,右边是右表
select 字段 from 表1 left join 表2 on 表1.字段1=表2.字段2;
SELECT * FROM students LEFT JOIN class ON students.c_id=class.id;
SELECT * FROM students AS s LEFT JOIN class AS c ON s.c_id=c.id;
-- 右链接 以右表为主根据条件查询左表数据,如果根据条件查询左表数据不存在使用NULL填充 right左边是左表,右边是右表
select 字段 from 表1 right join 表2 on 表1.字段1=表2。字段2
SELECT * FROM students AS s RIGHT JOIN class AS c ON s.c_id=c.id;
-- 自连接 左表和右表是同一张表 就是内连接一张表
CREATE TABLE areas(id VARCHAR(20) PRIMARY KEY NOT NULL,title VARCHAR(20) NOT NULL,pid VARCHAR(20));
-- 子查询 (在select语句中嵌套使用select语句)
SELECT * FROM students WHERE age>(SELECT AVG(IFNULL(age,0)) FROM students);
边栏推荐
猜你喜欢
![[explain in detail how to realize Sanzi chess step by step]](/img/17/68ef51ec2be0c86019461116ecaa82.png)
[explain in detail how to realize Sanzi chess step by step]

How to simulate the implementation of strcpy library functions

How to calculate the size of structure, segment and Consortium (common body)

archery数据库审核平台部署

What is the good brand of air conduction Bluetooth headset and the best brand recommendation of air conduction headset

RayMarching实现体积光渲染

技术分享 | 使用postman发送请求

Ten thousand words summarize and realize the commonly used sorting and performance comparison
![[dynamic planning -- the best period for buying and selling stocks Series 2]](/img/6c/887a026d3c1bcbd278bb7f3e0afd05.png)
[dynamic planning -- the best period for buying and selling stocks Series 2]

It is recommended to wear air conduction earphones, which do not need to wear in ear
随机推荐
Prometheus monitoring Nacos
Yapi vulnerability hanging horse program chongfu.sh processing
进程和线程的区别
网络通信及TCP/IP协议
手把手教你三步完成测试监控系统搭建
Mongodb quick start
代码整洁之道(二)
Leetcode brush questions diary sword finger offer II 047. Binary tree pruning
cocos2d-x 学习笔记——瓦片地图TiledMap
AQS之semaphore源码分析
软件测试的生命周期(流程)
HDU-5805-NanoApe Loves Sequence(思维题)
Problem solving for ACM freshmen in Jiangzhong on October 26
技术分享 | 使用postman发送请求
Lancher deployment practice
redis实现分布式锁思路及redission分布式锁主流程分析
测试面试题集锦(五)| 自动化测试与性能测试篇(附答案)
[explain in detail how to realize Sanzi chess step by step]
AQS之ReentrantLock源码解析
PKU-2524-Ubiquitous Religions(并查集模板)