当前位置:网站首页>MySQL basic DML and DDL learning
MySQL basic DML and DDL learning
2022-06-09 09:14:00 【Maximize+】
DML Add data

# Additions and deletions
USE atguigudb;
CREATE TABLE IF NOT EXISTS emp1(
id INT,
e_name VARCHAR(15),
hire_date DATE,
salary DOUBLE(10,2)
);
DESC emp1;
SELECT*FROM emp1;
# Add data
# The way 1: Add data one by one
#① Be sure to add... In the order of the declared fields
INSERT INTO emp1
VALUES(1,'Tom','2000-12-21',3400);
SELECT*FROM emp1;
#② Indicate the field to add Recommended More flexible
INSERT INTO emp1(id,hire_date,salary,e_name)
VALUES(2,'1999-09-09',4000,'Jerry');
# If the fields are incomplete, the default is NULL
INSERT INTO emp1(id,salary,e_name)
VALUES(3,4500,'shk');
#③
INSERT INTO emp1(id,e_name,salary)
VALUES(4,'Jim',5000),(5,' Zhang Junjie ',5500);
SELECT*FROM emp1;
# The way 2: Insert the query results into the table
SELECT*FROM emp1;
INSERT INTO emp1(id,e_name,salary,hire_date)
# Query statement
SELECT employee_id,last_name,salary,hire_date# The fields to be queried must correspond to the fields added to the table one by one
FROM employees
WHERE department_id IN(70,60);
SELECT*FROM emp1;
DESC emp1;
DESC employees;
# explain empl The length of the field to add data in the table cannot be less than employees The length of the query field in the table .
# If you add emp1 The field length of the data to be added in the table is less than employees Query fields in the table
# There is a risk of adding failure
DML Update delete operation

# Update data ( Modifying data )
#UPDATE...SET...WHERE...
# Batch modification of data can be realized ,
UPDATE emp1
SET hire_date = CURDATE()
WHERE id = 5;
SELECT*FROM emp1;
# Modify multiple fields of a piece of data at the same time
UPDATE emp1
SET hire_date = CURDATE(),salary = 6000
WHERE id = 4;
SELECT*FROM emp1;
# Include the characters... In the names in the table a Salary increase 20%
UPDATE emp1
SET salary = salary *1.2
WHERE e_name LIKE '%a%';
SELECT*FROM emp1;
# There may be unsuccessful cases when modifying data ( It may be due to constraints )
#update employees
#set department_id = 10000
#where employee_id = 102;
# Delete data delete ... from...where...
DELETE FROM emp1
WHERE id = 1;
SELECT*FROM emp1;
# When deleting data, it may also fail because of constraints
#delete from department
#here department_id = 50;
# Summary :DML Operation by default , Data will be submitted automatically after execution
# If you want to not automatically submit data after execution , You need to use SET autocmmit = FLASE;
#MySQL8 New features : Calculated column
CREATE TABLE test1(
a INT,
b INT,
c INT GENERATED ALWAYS AS (a + b) VIRTUAL #C The field is the calculated column
);
INSERT INTO test1(a,b)
VALUES(10,20);
SELECT*FROM test1;
UPDATE test1
SET a = 100;
DML And DDL Comprehensive case
# Super integrated instance
# Create database test01_library
CREATE DATABASE IF NOT EXISTS test01_library CHARACTER SET 'utf8';
USE test01_library;
# Create table books The table structure is as follows
CREATE TABLE books(
id INT,
NAME VARCHAR(50),
AUTHORS VARCHAR(100),
price FLOAT,
pubdate YEAR,
note VARCHAR(100),
num INT
);
DESC books;
SELECT*FROM books;
# towards books Insert records in the table
#1) Do not specify field name , Insert the first record
INSERT INTO books
VALUES(1,'Tal of AAA','Dickes',23,'1995','novel',11);
SELECT*FROM books;
#2) Specify all field names , Insert the second record
INSERT INTO books(id,NAME,AUTHORS,price,pubdate,note,num)
VALUES(2,'EmmaT','Jane lura',35,'1993','joke',22);
SELECT*FROM books;
#3) Insert multiple records at the same time ( All the remaining records )
INSERT INTO books(id,NAME,AUTHORS,price,pubdate,note,num)
VALUES
(3,'Story of Jane','Jane Tim',40,'2001','novel',0),
(4,'Lover Day','George Byron',20,'2005','novel',30),
(5,'Old land','Honore Blade',30,'2010','law',0),
(6,'The Battle','Upton Sara',30,'1999','medicine',40),
(7,'Rose Hood','Richard haggard',28,'2008','cartoon',28);
# Fixed query table statement
SELECT*FROM books;
# Type the novel (novel) The prices of all the books have increased 5
UPDATE books
SET price = price + 5
WHERE note = 'novel';
# Title The Book EmmaT Change the price of your book to 40, And change the description to drama.
UPDATE books
SET price = 40,note = 'drama'
WHERE NAME = 'EmmaT';
# Delete repository as 0 The record of
DELETE FROM books
WHERE num = 0;
# Included in the title of the statistical book a A Book of letters
SELECT NAME
FROM books
WHERE NAME LIKE '%a%';
# Included in the title of the statistical book a The number of alphabetic books and the total stock
SELECT COUNT(*),SUM(num)
FROM books
WHERE NAME LIKE '%a%';
# find "novel" Type of book , In descending order of price
SELECT NAME,note
FROM books
WHERE note = 'novel'
ORDER BY price DESC;
# Search for book information , Sort by inventory quantity in descending order , If the inventory is the same, follow note Ascending sort
SELECT *FROM books
ORDER BY num DESC,note ASC;
# according to note Number of classified statistical books
SELECT note,COUNT(*) " Number "
FROM books
GROUP BY note;
# according to note Inventory quantity of classified statistics , Show that the inventory exceeds 30 Ben's
SELECT note,SUM(num) " Inventory "
FROM books
GROUP BY note
HAVING SUM(num)>30;
# Search all books , Each page displays five , Show second page
SELECT*FROM books
LIMIT 5,5;
# according to note Inventory quantity of classified statistics , Displays the most in stock
# The way 1
SELECT note,MAX(sum_num)
FROM(
SELECT note,SUM(num) sum_num
FROM books
GROUP BY note
) b;
# The way 2
SELECT note,SUM(num) sum_num
FROM books
GROUP BY note
ORDER BY sum_num DESC
LIMIT 0,1;
# Query the title of the book to 10 Number of characters , Not including the space inside
SELECT NAME
FROM books
WHERE CHAR_LENGTH(REPLACE(NAME,' ','')) >=10;
# Check the title and type of the book , among note The value is novel Show novel ,law Show legal ,medicine Show medicine
#cartoon Show cartoon ,joke Show jokes
SELECT NAME " Title ",note,CASE note WHEN 'novel' THEN ' A novel '
WHEN 'law' THEN ' law '
WHEN 'medicine' THEN ' medicine '
WHEN 'cartoon' THEN ' cartoon '
WHEN 'joke' THEN ' joke '
ELSE ' other '
END " type "
FROM books
# Check the title and inventory , among num Value exceeds 30 Ben's , Show unsalable , Greater than 0 And below 10 Of ,
# Show best sellers , by 0 The display of is out of stock
SELECT NAME " Title ",num AS " stock ",CASE WHEN num>30 THEN ' Unsalable '
WHEN num>0 AND num<10 THEN ' Sell well '
WHEN num=0 THEN ' No goods '
ELSE ' normal '
END " Display state "
FROM books;
# Count each Note Total inventory and total amount
SELECT IFNULL(note,' Total inventory ') AS note,SUM(num)
FROM books
GROUP BY note WITH ROLLUP;
# Count each note The number of , And total
SELECT IFNULL(note,' Total inventory ') AS note,COUNT(*)
FROM books
GROUP BY note WITH ROLLUP;
# Count the top three books in stock
SELECT*FROM books
ORDER BY num DESC
LIMIT 0,3;
# Find the first book published
SELECT *FROM books
ORDER BY pubdate ASC
LIMIT 0,1;
# find novel The most expensive book in
SELECT * FROM books
WHERE note = 'novel'
ORDER BY price DESC
LIMIT 0,1
# Find the book with the most words in the title , No spaces
SELECT *
FROM books
ORDER BY CHAR_LENGTH(REPLACE(NAME,' ','')) DESC
LIMIT 0,1;
边栏推荐
- In depth analysis: what is the prospect of doing software testing at the age of 25?
- [untitled]
- 安科瑞企业微电网能效管理平台推动电网向能源互联网升级
- [redis learning 13] redis builds master-slave cluster, sentinel cluster and partition cluster
- [matlab] [digital simulation] [1] linprog solving linear programming, standard type
- JS realizes three-level linkage
- 微信小程序--嘟嘟会议--会议发布和我的会议查看
- Use of cookies, sessions and tokens in web development of system development series
- [antenna] [2] explanation of some nouns and simple concepts, still
- 单元测试中遇到的怪事
猜你喜欢

Implementation of WTM based on NETCORE framework

附十七章 网络程序解读限定文章

Shared network port settings for Dell Idrac express version

MySQL basic multi table query

3D编程模式:依赖隔离模式

RMAN backup concept_ About RMAN incremental backup

Application of acrel bus intelligent lighting control system in hospital

The fire door monitoring system carries out 24-hour real-time automatic inspection on the status of the fire door

防火门监控系统对防火门状态进行24小时实时自动巡检

MySQL基础 多表查询
随机推荐
Wechat applet to obtain basic user information
Crop the target area in the image according to the projection coordinates (with complete code)
安科瑞企业微电网能效管理平台推动电网向能源互联网升级
2022-2028 global technology monitoring countermeasures industry research and trend analysis report
Opencv get image pixel value data type
探索MySQL not in到底走索引吗?
了解图数据库neo4j(二)
ESP32学习笔记【WiFi网络篇】-01AP&STA
Configuring the environment for RMAN backups_ Configure advanced backup options
Configuring the environment for RMAN backups_ Configure the deletion policy for archived redo logs
[antenna] [2] explanation of some nouns and simple concepts, still
JS realizes three-level linkage
RMAN backup concept_ catalogue
Android常见原理性面试题(初步整理)~
MySQL基础 查询语句
C pointer review
MySQL基础 子查询
Configuring the environment for RMAN backups
English grammar_ adverb of manner
NetCore框架WTM的分表分库实现