当前位置:网站首页>Database operation day 6
Database operation day 6
2022-07-29 05:33:00 【Xiao Hong is working hard】
List of articles
database :
The data is in memory :
- advantage : Fast reading and writing .
- shortcoming : Program end data loss .
Save the data to a file :
- advantage : Data can be stored permanently .
- shortcoming :1. Frequent IO operation , Low efficiency .
2. Data management is very inconvenient , You need to read all the data as a whole before you can operate .
database :
- Data is permanently stored .
- Easy to manage .
Mysql
One : Database operation :
Create database
CREATE DATABASE school DEFAULT CHARACTER SET UTF8;
- SHOW DATABASES; Show all the databases .
Delete database
DROP DATABASE school;
Two : Operation of database table :
Create table
-- Build table
CREATE TABLE student(
id INT,
`name` CHAR(10),
age INT,
gender CHAR(1)
)
-- Find all tables
SHOW TABLES;
View table structure
-- View table structure
DESC student;
Delete table
-- Delete table
DROP TABLE student;
Change table structure
1. Add fields
-- Add fields
ALTER TABLE student ADD COLUMN address CHAR(255);
2. Modify fields
-- Modify fields
AlTER TABLE student CHANGE address addr char(20);
3. Delete field
-- Delete field
ALTER TABLE student DROP COLUMN addr;
Modify the name of the table
-- Modify the name of the table
ALTER TABLE student RENAME TO teacher;
Add primary key and set self increment :
CREATE TABLE student(
id INT PRIMARY KEY AUTO_INCREMENT,name
CHAR(20),
age INT,
gender char(1)
)
3、 ... and : Basic operation of the watch
1. increase
- 1
-- Insert column ( Insert one at a time )
INSERT INTO student(`name`,age,gender) VALUES(" Zhang San ",1," male ");
- 2
-- Insert column ( Insert more than one at a time )
INSERT INTO student(`name`,age,gender) VALUES(" Li Si ",2," Woman "),(" Wang Wu ",3," male ");
2. Change
-- repair
UPDATE student SET gender=" male " WHERE `name`=" Li Si ";
3. Delete
-- Delete
DELETE FROM student WHERE `name`=" Li Si ";
4. check
SELECT * FROM student;
All the contents of the query table , Not recommended , When the content is small, you can operate , It is suggested that you can find whichever column you need .
- 1
-- Query specified column
SELECT `name`,age,gender FROM student WHERE id=1;
- 2
-- Merge columns when querying , Fields can be treated as java Inside the variable to operate
SELECT id,`name`,(php+java) AS ' Total score ' FROM student;
– Remove duplicate records during query
SELECT DISTINCT address FROM student;Add constant columns when querying , adopt as It's a nickname
SELECT id,name,age AS ‘ Age ’ FROM student;
expand :
1. Aggregate query
2. Query sequence
3. Group query
- 1
-- Group query
-- select The following queries are based on group by After that
SELECT gender,COUNT(id) AS " Number " FROM student GROUP BY gender;
- 2
-- group by The following condition query uses having
SELECT gender,COUNT(id) AS " Number " FROM student GROUP BY gender HAVING COUNT(id)>2;
边栏推荐
猜你喜欢
随机推荐
C language first level pointer
QML control: combobox
[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start
D3d Shader Instruction
vim编辑器使用
ClickHouse学习(十一)clickhouseAPI操作
串口通讯部分详解
CSDN的md编辑器如何输入上下标?公式和非公式的输入方式不一样
365天挑战LeetCode1000题——Day 036 二叉树剪枝 + 子数组和排序后的区间和 + 删除最短的子数组使剩余数组有序
QML type: mousearea
三次握手四次挥手针对面试总结
Common shortcut keys for Ad
Occt learning 001 - Introduction
Redirection and files
QT series - Installation
AD常用快捷键
QML type: state state
CMU15-213 Shell Lab实验记录
redis的基本使用
Xiaolu Inn - Trailer