【MySQL】(4)操作数据表中的记录_MySQL
1. 插入记录INSERT
方法一:
INSERT [INTO] tbl_name [(clo_name,...)] {VALUES | VALUE} ({expr | DEFAULT},...),(...),...;
例如:
CREATE TABLE users(id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, username VARCHAR(20) NOT NULL, password VARCHAR(32) NOT NULL, age TINYINT UNSIGNED NOT NULL DEFAULT 10, sex BOOLEAN); # 插入记录,不指定列的数目时,必须所有的字段都要赋值 INSERT users VALUES(NULL, 'Tom', '123', 25, 1); INSERT users VALUES(NULL, 'Tom2', '123', 28, 1); INSERT users VALUES(DEFAULT, 'Tom3', '111', 28, 1); # 使用数学表达式也可以添加值 INSERT users VALUES(DEFAULT, 'Tom4', '111', 3*7+2/3, 1); # 给年龄DEFAULT,会采用默认值10 INSERT users VALUES(DEFAULT, 'Tom5', '111', DEFAULT, 1); # 一次添加多条记录 INSERT users VALUES(DEFAULT, 'Tom6', '111', DEFAULT, 1), (NULL, 'Rose', md5('213'), DEFAULT, 0);
INSERT [INTO] tbl_name SET col_name={exp | DEFAULT},...;
这个方法与第一种方式的区别在于,此方法可以使用子查询(SubQuery),此方法一次性只能插入一条记录。
例如:
INSERT users SET username='Ben', password='456';
INSERT [INTO] tbl_name [(col_name, ...)] SELECT ...;
使用此方法可以将查询结果插入到指定数据表。
2. 单表更新记录UPDATE
UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET col_name1={expr1 | DEFAULT} [, col_name2={expr2 | DEFAULT}] ... [WHERE where_condition];
例如:
# 所有记录的年龄都加5 UPDATE users SET age=age+5; # 更新多列 UPDATE users SET age=age-id, sex=0; # 更新id为偶数的记录 UPDATE users SET age=age+10 WHERE id%2=0;
3. 单表删除记录DELETE
DELETE FROM tbl_name [WHERE where_condition];
例如:
DELETE FROM users WHERE id=6;
即使在删除后,id号不连续,那么新增的数据还是会在最大的id号加一。
4. 查询表达时解析
SELECT select_expr [, select_expr ...] [FROM table_references [WHERE whrere_condition] [GROUP BY {col_name | position} [ASC | DESC],...][HAVING where_condition][ORDER BY {col_name | expr | position} [ASC | DESC], ...][LIMIT {[offset,] row_count | row_count OFFSET offset}]];
每一个表达式表示想要的一列,必须至少有一个。多个烈之间以英文逗号分隔。星号(*)表示所有列。tbl_name.*可以表示命名表的所有列。查询表达式可以使用[AS] alias_name为其赋予别名。别名可用于GROUP BY,ORDER BY或HAVING字句。
例如:
# 查看MySQL版本 SELECT VERSION(); # 查看当前时间 SELECT NOW(); # 只查看前两列 SELECT id, username FROM users; SELECT username, id FROM users; SELECT users.id, users.username FROM users; SELECT id AS userid, username AS uname FROM users; # AS关键字可以省略,但是尽量写上,避免不必要的错误 SELECT id username FROM users;
(1). WHERE
条件表达式
对记录进行过滤,如果没有指定WHERE字句,则显示所有记录。在WHERE表达式中,可以使用MySQL支持的函数或运算符。
(2). GROUP BY
查询结果分组
例如:
SELECT sex FROM users GROUP BY sex; # 1表示按照SELECT语句中第一个出现的字段排序 SELECT sex FROM users GROUP BY 1;
分组条件
例如:
# 当HAVING语句有age的条件时,前面的SELECT中必须出现这个age字段 SELECT sex, age FROM users GROUP BY 1 HAVING age>35; # 或者是一个聚合函数 SELECT sex, age FROM users GROUP BY 1 HAVING count(id)>=2;
对查询结果进行排序
例如:
# 按照id降序排列 SELECT * FROM users ORDER BY id DESC; # 同时以两个字段排序age默认升序,id降序 SELECT * FROM users ORDER BY age, id DESC;
限制查询结果返回的数量
例如:
# 从第1条开始返回2条记录 SELECT * FROM users LIMIT 2; # 从第1条开始,偏移2条后,查询2条记录 SELECT * FROM users LIMIT 2 OFFSET 2; # 从第4条开始(从0开始计数),返回2条记录 SELECT * FROM users LIMIT 3, 2; SELECT * FROM users ORDER BY id DESC LIMIT 2, 2; CREATE TABLE test(id TINYINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, username VARCHAR(20)); # 将users年龄大于30的数据插入test表 INSERT test(username) SELECT username FROM users WHERE age>=30;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is a common relational database that is a core component of many websites and applications. As the amount of data becomes larger and larger, how to optimize the performance of MySQL becomes particularly important. One of the key areas is the compression of data tables. In this article we will introduce the data table compression technology in MySQL. Compressed tables and non-compressed tables There are two types of data tables in MySQL: compressed tables and non-compressed tables. Uncompressed tables are MySQL's default table type, which use fixed-length row format to store data. This means data

MySQL modifies the data table: 1. First check all tables in the database, the code is: "SHOW TABLES;"; 2. Modify the table name, the code is: "ALTER TABLE old table name RENAME [TO] new table name;". 3. Check whether the table name is modified successfully. The code is: "SHOW TABLES;"

MySQL is a very popular open source relational database management system that supports complete DDL (data definition language) operations. DDL is a language used to define and manage various data objects in the database, including data tables, views, indexes, etc. It is very important for database administrators and developers to be proficient in DDL operation technology of data tables in MySQL. This article will introduce in detail the technology and methods of DDL operation of data tables in MySQL, and provide practical operation examples. 1. Create a data table. Creating a data table is in DDL.

MySQL is an open source relational database management system. Its basic functions are excellent in database design, data storage and management. In MySQL, the data table is the most basic unit of data storage. In practical applications, data table reloading is a very common operating technique, which can help us improve the operating efficiency of the database and improve the stability of the system. This article will introduce this operation technique in detail from the concepts, principles and practical applications of data table overloading in MySQL. 1. What is data table overloading? Data table overloading is

Introduction to the method of using MySQL's AVG function to calculate the average value of numeric columns in a data table: MySQL is an open source relational database management system with a wealth of built-in functions to process and calculate data. Among them, the AVG function is a function used to calculate the average of a numeric column. This article will introduce how to use the AVG function to calculate the average value of numeric columns in a MySQL data table, and provide relevant code examples. 1. Create a sample data table First, we need to create a sample data table for demonstration. Suppose we have a file called

How to realize the underlying optimization of MySQL: horizontal and vertical partitioning strategies of data tables, which require specific code examples. Introduction: In large-scale application scenarios, MySQL databases often face the pressure of storing and querying massive data. In order to solve this problem, MySQL provides data table partitioning strategies, including horizontal partitioning (HorizontalPartitioning) and vertical partitioning (VerticalPartitioning). This article will introduce how to implement MySQL underlying optimization, focusing on

How to use thinkorm to implement related queries between data tables Introduction: During database development, we often encounter situations where we need to perform related queries between multiple data tables. Using thinkorm, an excellent database ORM framework, you can easily implement associated queries of data tables and improve development efficiency. This article will introduce how to use thinkorm to implement related queries between data tables, and provide code examples to help readers better understand. 1. Basic concepts Before performing related queries, you first need to understand th

How to use the MAX function in MySQL to find the maximum value in the data table Introduction: In MySQL, we often need to perform various queries and analysis on the data table, including finding the maximum value in the data table. The maximum value in a data table can be easily found using the MAX function and is very useful when further processing the data. This article will introduce how to use the MAX function to find the largest value in the data table, and give corresponding code examples. 1. Introduction to the MAX function The MAX function is an aggregate function in MySQL. Use
