当前位置:网站首页>Introduction to SQL -- Basic additions, deletions, modifications, and exercises
Introduction to SQL -- Basic additions, deletions, modifications, and exercises
2022-07-23 06:06:00 【Diesel】
List of articles
Data addition, deletion, modification and query
1. increase :INSERT
INSERT INTO tbl_name [(column [, column ...])]
VALUES(value [, value ...]);
The inserted data must be of the same data type as the field
The length of the data must be within the specified range of the column
stay values The data position listed in must correspond to the arrangement position of the added column
Character and date data should be enclosed in single quotation marks , If the string can be converted to numeric type , You can complete implicit conversion in this field
Columns can be inserted with null values NULL, The premise is that this field allows null values
You can add multiple pieces of data in one statement :
VALUES (…), (…), (…), …
If you add data to all fields in the table , Field names can be omitted
When there is a default value , When the inserted data does not have the value of this field , Then it is added as the default value by default ; Otherwise, an error will be reported : If a field is not specified NOT NULL, This value is not given when adding data , The default is NULL Insert into this field
2. Change :UPDATE
UPDATE tbl_name
SET col_name1=expr1 [, col_name2=expr2, ...]
[WHERE where_definition]
- If there is no WHERE, Then modify all existing entries ( Use with caution !!!)
- You can modify multiple fields simultaneously in one statement
3. Delete :DELETE
DELETE FROM tbl_name
[WHERE where_definition]
- If there is no WHERE expression , Delete all data entries . This operation is different from DROP TABLE sentence
- To delete the value of a column in an entry , You should use UPDATE Empty statement (NULL perhaps ’')
4. check :SELECT
SELECT [DISTINCT] *|{col_name1, col_name2, ...}
FROM tbl_name;
DISTINCT Optional , Remove duplicate data when displaying results , Only when all fields of the query result are the same , Will remove duplicate data
You can use expressions to operate on the columns of the query
SELECT * | {col_name1 | expression, col_name1 | expression, ...} FROM tbl_nameUse AS sentence , Rename the column names of query results
SELECT col_name AS Alias FROM tbl_name
stay where Operators commonly used in clauses
Comparison operator
- > < <= >= = <> != Greater than less than
- BETWEEN … AND … The value in a certain interval : Closed interval
- IN(set) In collection ( list ) In the value of the
- [NOT] LIKE ‘pattern’ Fuzzy query :‘ king %’ String starting with Wang
- IS NULL Is it empty
Logical operators
- and And
- or or
- not Not
order by Clause sort query results
SELECT * | {col_name1 | expression, col_name1 | expression, ...}
FROM tbl_name
order by column asc|desc [, column asc|desc, ...]
column Specify sorted columns
asc Ascending ( Default ),desc Descending
order by Clause should be in SELECT End of statement
Can pass , Realize multi-level sorting
practice
curd on table
CREATE TABLE `goods` (
id INT,
goods_name VARCHAR(10),
price DOUBLE);
insert
INSERT INTO `goods` (id, goods_name, price)
VALUES(10, 'HUAWEI', 2000);
INSERT INTO `goods` (id, goods_name, price)
VALUES(11, 'Apple', 1999);
INSERT INTO `goods` (id, goods_name, price)
VALUES(12, 'VIVO', 2199), (13, 'XIAOMI', 1999);
INSERT INTO `goods`
VALUES(14, 'OPPO', 2499);
INSERT INTO `goods` (id, goods_name, price)
VALUES('8848', '8848', 23999);
INSERT INTO `goods` (id, goods_name, price)
VALUES(11, 'Samsung', NULL);
SELECT * FROM `goods`
update
UPDATE emp SET salary = 5000
UPDATE emp
SET salary = 6000
WHERE user_name = 'Danfer'
INSERT INTO emp
VALUES(2, 'Haochen', '2021-06-01', '2021-06-01 15:25:59', 'sleeping for fun', 7000, ' What a his mother , Sleep can make money ', 'h:\\haha.jpg')
UPDATE emp
SET salary = salary + 1000, job = 'sleeping and eating'
WHERE user_name = 'Danfer'
SELECT * FROM emp
delete
DELETE FROM emp
WHERE user_name = 'Danfer'
DELETE FROM emp
SELECT * FROM emp
select
CREATE TABLE score(
id INT,
`name` VARCHAR(20),
chinese INT,
math INT,
english INT);
INSERT INTO score
VALUES (1, ' king 123', 102, 135, 143),
(2, ' In the east 123', 116, 132, 142),
(3, ' Liu 123 hin ', 108, 135, 140),
(4, ' beam 123', 106, 125, 138),
(5, ' Zhou 123', 121, 113, 143),
(6, ' Liu 456', 128, 138, 137);
SELECT * FROM score;
SELECT `name`, english FROM score;
SELECT DISTINCT english FROM score;
SELECT `name`, (chinese + math + english) FROM score;
SELECT `name`, (chinese + math + english) AS total FROM score;
SELECT * FROM score WHERE `name`=' king 123'
SELECT * FROM score WHERE chinese>=105
SELECT * FROM score WHERE (chinese+math+english)>=369
SELECT * FROM score WHERE chinese>110 AND math>120
SELECT * FROM score WHERE `name` LIKE ' Liu %'
SELECT * FROM score WHERE `name` LIKE '% hin ' order by math
SELECT `name`, chinese, math, english, (chinese+math+english) as total FROM score order by total desc
边栏推荐
- 机器学习理论基础
- Lc: sword finger offer 10- I. Fibonacci series
- ROPgadget初识 ——— ret2syscall
- 浅谈LoRa,LoRaWAN,NB-IoT三类物联网技术
- Implementing IO multiplexing in UNIX using poll function to realize network socket server
- 磁盘管理操作
- pwn栈溢出基础练习题——1
- PWN —— ret2libc1
- The difference between get request and post request and packet capturing
- Establishment of zstuacm student information base (completed with linked list)
猜你喜欢

UNIX Programming - network socket

Unix programming project - the client based on raspberry pie regularly obtains the temperature and reports it to the server

Iptables firewall

机器学习理论基础

Understanding the application steps of machine learning development

Saisissez une chaîne de caractères à partir du clavier et affichez différents caractères et le nombre d'occurrences de chaque caractère. (la sortie n'est pas séquentielle) résoudre le problème en util

get请求和post请求的区别

暑期提前学大学物理C知识记录【更新】

Fiddler script personalized configuration display

Firewall Research Report
随机推荐
shell基本命令
KMP
机器学习开发应用步骤的理解
信息收集的基础要素
Shell basic commands
STM32学习—DHT11温湿度传感器采样驱动并用cjson格式上报
文件目录权限
Regular expression I
vector与迭代器部分内容学习
洛谷回文质数 Prime Palindromes
Common problems of multiple processes - how to lock the same parent thread variable (critical resource) when creating multiple threads so that the shared parent thread variable is not repeatedly modif
get请求和post请求的区别
vim编辑器的使用
信息收集调研报告
Iptables firewall
Shell programming specifications and variables
scikit-learn——机器学习应用开发的步骤
stored procedure
【数据库连接】——节选自培训
Introduction to programming 3 - seeking the maximum value