当前位置:网站首页>数据库操作 Day 6
数据库操作 Day 6
2022-07-29 05:09:00 【小洪正在努力】
文章目录
数据库:
数据在内存:
- 优点:读写速度快。
- 缺点:程序结束数据丢失。
数据保存到文件:
- 优点:数据可以永久保存。
- 缺点:1.频繁的IO操作,效率低。
2.数据的管理非常不方便,需要把所有的数据整体都读取出来才能操作。
数据库:
- 数据永久保存。
- 管理方便。
Mysql
一:数据库操作:
创建数据库
CREATE DATABASE school DEFAULT CHARACTER SET UTF8;
- SHOW DATABASES; 展示所有数据库。

删除数据库
DROP DATABASE school;
二:数据库表的操作:
创建表
-- 建表
CREATE TABLE student(
id INT,
`name` CHAR(10),
age INT,
gender CHAR(1)
)
--查找所有表
SHOW TABLES;

查看表结构
-- 查看表结构
DESC student;

删除表
--删表
DROP TABLE student;
更改表结构
1.添加字段
-- 添加字段
ALTER TABLE student ADD COLUMN address CHAR(255);

2.修改字段
-- 修改字段
AlTER TABLE student CHANGE address addr char(20);

3.删除字段
-- 删除字段
ALTER TABLE student DROP COLUMN addr;

修改表名
-- 修改表名
ALTER TABLE student RENAME TO teacher;

添加主键和设置自增:
CREATE TABLE student(
id INT PRIMARY KEY AUTO_INCREMENT,nameCHAR(20),
age INT,
gender char(1)
)
三:表的基本操作
1.增
- 1
-- 插入列(一次插一条)
INSERT INTO student(`name`,age,gender) VALUES("张三",1,"男");

- 2
--插入列(一次插多条)
INSERT INTO student(`name`,age,gender) VALUES("李四",2,"女"),("王五",3,"男");

2.改
--修
UPDATE student SET gender="男" WHERE `name`="李四";

3.删
--删
DELETE FROM student WHERE `name`="李四";

4.查
SELECT * FROM student;
查询表的所有内容,不推荐,内容少时可以操作,建议需要哪个列就找哪个列。
- 1
-- 查询指定列
SELECT `name`,age,gender FROM student WHERE id=1;

- 2
-- 查询时候合并列,字段可以当成java里面的变量来运算
SELECT id,`name`,(php+java) AS '总成绩' FROM student;

– 查询时候去掉重复的记录
SELECT DISTINCT address FROM student;查询时候添加常量列,通过as可以起别名
SELECT id,name,age AS ‘年龄’ FROM student;
拓展:
1.聚合查询

2.查询排序

3.分组查询
- 1
--分组查询
-- select后面的查询都是基于group by之后的
SELECT gender,COUNT(id) AS "个数" FROM student GROUP BY gender;

- 2
-- group by之后的条件查询使用having
SELECT gender,COUNT(id) AS "个数" FROM student GROUP BY gender HAVING COUNT(id)>2;

边栏推荐
- 容器安全开源检测工具--问脉 VeinMind(镜像后门、恶意样本、敏感信息、弱口令等)
- C语言 N皇后问题
- 水一篇图的拓扑排序
- 平行云CEO 李岩:CloudXR ,开启通往元宇宙的通道
- More than 200 ISVs have settled in! The first anniversary of Alibaba cloud computing nest
- Live broadcast preview | how to save 30% labor cost and shorten 80% trademark processing cycle?
- 京东云联合Forrester咨询发布混合云报告 云原生成为驱动产业发展新引擎
- AD常用快捷键
- 365天挑战LeetCode1000题——Day 037 元素和小于等于阈值的正方形的最大边长 + 满足条件的子序列数目
- Helm chart for Kubernetes
猜你喜欢

365天挑战LeetCode1000题——Day 041 二分查找完结纪念 + 第 N 个神奇数字 + 在线选举

C language handwritten qq-ai version

预约中,2022京东云产业融合新品发布会线上开启

365天挑战LeetCode1000题——Day 039 完全二叉树插入器 + 寻找峰值 II + 快照数组

JD cloud and Forrester consulting released a hybrid cloud report that cloud Nativity has become a new engine driving industrial development

阿里云架构师细说游戏行业九大趋势

OCCT学习001-----简介

重定向和文件

The latest tank battle 2022 - Notes on the whole development -2

Qt版的贪食蛇游戏项目
随机推荐
ANSI C类型限定符
Occt learning 003 - MFC single document project
Li Yan, CEO of parallel cloud: cloudxr, opens the channel to the metauniverse
How rimworld uploads creative workshops through steamcmd
365 day challenge leetcode 1000 questions - day 039 full binary tree inserter + find peak II + snapshot array
vim编辑器使用
With frequent data leakage and deletion events, how should enterprises build a security defense line?
Best practices for elastic computing in the game industry
Custom QML control: imagebutton
365 day challenge leetcode 1000 questions - day 041 two point search completion anniversary + nth magic number + online election
Come on! See how Clickhouse, which has risen 16 places a year, can be implemented in jd.com
C语言求字符串的长度
MFC集成qt验证及问题处理
C语言用指向指针的指针对n个整数排序
CMU15-213 Malloc Lab实验记录
为啥谷歌的内部工具不适合你?
Container security open source detection tool - veinmind (mirror backdoor, malicious samples, sensitive information, weak password, etc.)
CryEngine3 调试Shader方法
Xiaolu Inn - Trailer
【C语言系列】—文件操作详解(上)