当前位置:网站首页>MySQL学习笔记(1)——基础操作
MySQL学习笔记(1)——基础操作
2022-08-10 22:20:00 【Oceans_depth】
在菜鸟MySQL教程 进行的简单的数据库操作的学习和整理。
MySQL是一种流行的关系数据库管理系统(Relational Database Management System,RDBMS)
1. 简单命令
1.1 进入、离开MySQL
cmd中:
进入MySQL:
mysql -u root -p
离开:exit
1.2 数据库操作
mysql中语句以;结尾才能生效
1. 显示数据库
SHOW DATABASES;
(似乎大小写的效果是一样的)
2. 创建数据库
CREATE DATABASE study;
建议使用:
CREATE DATABASE IF NOT EXISTS study;
3. 选择数据库
use study;
查看当前选择的数据库:
SELECT DATABASE();
4. 查看数据库创建信息
SHOW CREATE DATABASE study;
SHOW CREATE DATABASE study \G
5. 删除数据库
DROP database study;
DROP database IF EXISTS study;
1.2 数据表操作
1. create创建数据表
表名称在Windows上不区分大小写,在Linux上区分大小写。
显示数据表信息:
desc book;
PS:设置id自动增加:
id INT NOT NULL AUTO_INCREMENT,
默认从1开始增加序列。指定初始值为100:
id INT NOT NULL AUTO_INCREMENT,
...
)engine=innodb auto_increment=100 charset=utf8;
2. drop删除数据表
DROP TABLE book;
3. insert插入数据
4. select读取数据
SELECT column1,column2... FROM table
[WHERE Clause]
[LIMIT N] [OFFSET M];
5. Update修改数据
UPDATE table SET field1=new, field2=new
[WHERE Clause]
6. Delete删除数据
DELETE from tabel [WHERE Clause]
不指定where时,删除所有数据。
delete from book where id=3;
7. order by排序
SELECT field1,field2,... FROM table1, table2...
ORDER BY field1 [ASC / DESC], [field2...];
- ASC 默认选项,升序;
- DESC 降序。
8. where 语句
SELECT field1,field2,... FROM table1, table2...
WHERE condition1 [and/or condition2]...
可以用关键字BINARY设定where在比较时区分大小写:
SERLECT * from book WHERE BINARY author="RUNOOB"
9. like语句
SELECT field1,field2,... FROM table1, table2...
WHERE field1 LIKE condition1 [and/or field2=value]...
LIKE 类似于等号=,多与%同用,%表示任意字符,用于模糊匹配。
10. union操作符
union连接多个select语句的结果
SELECT 语句1
UNION [ALL / DISTINCT]
SELECT 语句2;
- ALL 返回所有结果集,包含重复数据;
- DISTINCT 默认选项,删除结果集中重复的数据;
1.3 数据表进阶操作
1. group by;
SELECT column, function(column) FROM table
WHERE column operator value
GROUP BY column;
对于数据表employee:
- count 函数:
按名字进行分组,并统计每个人多少条记录: - sum函数:
WITH ROLLUP 在分组基础上进行统计(SUM,AVG,COUNT…)。
按名字进行分组,统计每人登录次数:
NULL表示所有人登录次数总和,可以用coalesce设置取代NULL的名称【select coalesce(a,b,c)】:
边栏推荐
猜你喜欢
随机推荐
商家招募电商主播要考虑哪些内容
Service - DHCP principle and configuration
BM13 determines whether a linked list is a palindrome
CFdiv2-Beautiful Mirrors-(期望)
The perfect alternative to domestic Gravatar avatars Cravatar
RecyclerView上下滑动时,不调用onBindViewHolder 导致列表的item不刷新
pytorch tear CNN
FPGA - Memory Resources of 7 Series FPGA Internal Structure -03- Built-in Error Correction Function
ArcGIS中的坐标系统和投影变换
如何成为一名正义黑客?你应该学习什么?
How does the Weiluntong touch screen display the current value of abnormal data while alarming?
配电网络扩展规划:考虑使用概率性能源生产和消费概况的决策(Matlab代码实现)
Distribution Network Expansion Planning: Consider Decisions Using Probabilistic Energy Production and Consumption Profiles (Matlab Code Implementation)
瑞幸咖啡第二季营收33亿:门店达7195家 更换CFO
企业云存储日常运行维护实践经验分享
LeetCode Daily 2 Questions 02: Reverse the words in a string (1200 each)
A shell script the for loop statements, while statement
自学软件测试不知道该如何学起,【软件测试技能图谱|自学测试路线图】
Leave a message with a prize | OpenBMB x Tsinghua University NLP: The update of the large model open class is complete!
2022年8月的10篇论文推荐