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;(3). HAVING
分组条件
例如:
# 当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;(4). ORDER BY
对查询结果进行排序
例如:
# 按照id降序排列 SELECT * FROM users ORDER BY id DESC; # 同时以两个字段排序age默认升序,id降序 SELECT * FROM users ORDER BY age, id DESC;(5). LIMIT
限制查询结果返回的数量
例如:
# 从第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;

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.

MySQL is suitable for beginners because: 1) easy to install and configure, 2) rich learning resources, 3) intuitive SQL syntax, 4) powerful tool support. Nevertheless, beginners need to overcome challenges such as database design, query optimization, security management, and data backup.

Yes,SQLisaprogramminglanguagespecializedfordatamanagement.1)It'sdeclarative,focusingonwhattoachieveratherthanhow.2)SQLisessentialforquerying,inserting,updating,anddeletingdatainrelationaldatabases.3)Whileuser-friendly,itrequiresoptimizationtoavoidper

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment