Mysql study notes (3) Add, delete, modify and check table data.
This part is the simplest and the most troublesome. It’s simple because it actually only includes four parts: additions, deletions, and insertions. Generally speaking, adding data, deleting data, modifying data, and querying data are not troublesome. They are commonly used in our daily life. Who doesn't know this? When I was learning MySQL at a training institution, I knew that a bottleneck in a programmer's growth journey was the database. How to write highly maintainable SQL statements and how to maintain high maintainability while maintaining high execution efficiency is a difficult problem. I'm currently working on a rather tricky project. I often left join 5 to 6 tables and scan tens of thousands of tables. The query speed is surprisingly slow. Tens of thousands is just test data. When the real project comes online, the amount of data may reach millions. Therefore, inefficient mysql statements may cause the program to crash directly.
Therefore, the addition, deletion, modification and insertion of data is the most difficult and crucial, so you must learn it well. But today we only write the simplest additions, deletions and insertions. The following blogs will continue to organize related knowledge in depth.
Add data:
insert into table name (field 1, field 2, field 3, field n) values (value 1, value 2, value 3, value 4);
This is relatively simple. This is a general statement. You can insert one piece of data or insert multiple pieces of data. This is included in the test sql of "Mysql Study Notes (2) Addition, Deletion, Modification and Inquiry of Table Structure". You can refer to it, I won’t repeat the code here.
When inserting data into the auto-increment auto_increment field, it is recommended to insert a NULL value. At this time, the field will insert the next number into the auto-increment field. In fact, I usually just don’t write.
When inserting data into a default value constrained field, the field value can use the default keyword, which means that the default value of the field is inserted.
When inserting new records, you need to pay attention to the foreign key constraint relationships between tables. In principle, insert data into the parent table first, and then insert data into the child table.
Delete data:
Deleting data is a relatively dangerous operation. It may be used during the development and testing phase, but when the actual project is launched, there will be no delete permission.
Syntax: delete from table name where condition;
delete from classes where class_no = 53; (You can continue to use the test sql in "Mysql Study Notes (2) Addition, Deletion, Modification and Check of Table Structure");
I would like to emphasize that deleting data and modifying data without any conditions are both scoundrels.
Modify data:
Modifying data is also a very dangerous operation. When the project goes online, only certain fields of some tables are allowed to be changed.
Syntax: update table name set field name = field value where condition.
Update classes set class_name='roverliang' where class_no=52;
I would like to emphasize that deleting data and modifying data without any conditions are both scoundrels.
Query data:
Basically, more than 90% of the database operations in the project are query operations. Therefore, whether the query statement is well written will directly reflect a programmer's programming ability.
I am against those people who shake their heads in denial when they see a long SQL statement. They learned from some information that writing long SQL statements will reduce execution efficiency, and they simply avoid long MySQL statements. MySQL, which could have been completed in one go, was abruptly divided into several scattered fragments.
For those of us who are engaged in technology, it is wrong to rely on feelings. Everything must be based on facts. The speed of SQL execution is not determined by feelings. What really determines the execution speed of sql is mysql itself, so if you encounter any doubts, just put it into mysql and run it. Which one is faster and which one is slower is a matter of distinction.
If the execution time of the two is almost the same, of course you should not hesitate to choose SQL in one go. It’s so easy to maintain! It can greatly reduce the amount of code.
Some friends may retort that such a long sql is annoying to look at, so how can it be easy to maintain? This is actually related to personal habits. For long SQL statements, I usually write this way. I don’t know if it is correct. Please give me some advice.
Copy code
#longsql;
select field 1, field 2, field 3, field n
from Table 1 as t1, Table 2 as t2, Table 3 as t3
on t1.Field 1 = t4.Field 1
left join Table 5 as t5 on t1.Field2 = t5.Field2
where t1.Field1=1 and t2.Field2=2 and t3.Field3 > 3
group by t1.Field1
order by t1 1,5
Write the code as Bai Juyi's poetry, strive to be concise, but take into account the readability, so that it can be understood by a three-year-old child and a seventy-year-old woman.
Write the code as a graphic design work, with neat layout between equal signs and variables. The code snippets are well-proportioned and uniform.
Don’t be stingy with spaces and line breaks. Think of code as a flow of data and give it room to flow.
The above is the addition, deletion, modification and query of table data in Mysql study notes (3). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

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 an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings
