Getting Started with Mysql - Adding, Deleting, Modifying and Querying Table Data

  

This part is the easiest and the most troublesome. It's simple because it actually only includes the addition and deletion of four parts. Generally speaking, it is not troublesome to add data, delete data, modify data, and query data. We use it every day. Who doesn't know this? When I learned mysql in a training institution, I knew that a bottleneck in the development of programmers was the database. How to write high-maintenance SQL statements, and how to maintain high-maintenance and high-efficiency execution at the same time, this is a difficult problem. I am working on a relatively difficult project recently. I often left join 5~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 is launched, the amount of data may reach millions. Therefore, the inefficient mysql statement may crash the program directly. Brothers IT Education (www.lampbrother.net)
  Therefore, the addition, deletion, modification and insertion of the data part is the most difficult and crucial, and must be learned well. But we only write the simplest additions and deletions today, and the following blogs will continue to organize relevant 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, and Modification of Table Structure". For reference, the code will not be repeated here.
  Note:
  When inserting into char, varchar, text and date fields, the field value should be enclosed in single quotes.
  When inserting data into the auto-increment auto_increment field, it is recommended to insert a NULL value, and the field will insert the next number into the auto-increment field. In fact, I usually don't write directly.
  When inserting data into the default value constraint field, the default keyword can be used for the field value, indicating that the default value of the field is inserted.
  When inserting a new record, you need to pay attention to the foreign key constraint relationship between the tables. In principle, insert data into the parent table first, and then insert data into the child table.
  Deleting data:
  Deleting data is a relatively dangerous operation. Usually, it may be used in the development and testing stage, but when the real 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 and modification of table structure");
  Emphasize one sentence, delete data and modify data , no conditions are stinky hooligans.
  Modifying data:
  Modifying data is also a very dangerous operation. When the project is online, only certain fields of certain 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;
  Emphasize that deleting data and modifying data without conditions are stinky hooligans.
  Query data:
  Basically, more than 90% of the operations on the database in the project are query operations. Therefore, whether the query statement is well written will directly reflect the programming ability of a programmer.
  Oppose those people and shake their heads in denial when they see a long SQL statement. They learned from some information that long sql statements will reduce the execution efficiency, and they are almost avoided for long mysql statements. The mysql that could have been completed in one go was forcibly divided into several scattered fragments.
  For those of us in technology, it just doesn't feel right. Everything must be based on facts. The speed of SQL execution is not determined by feeling. It is mysql itself that really decides the execution speed of sql, so if you encounter doubts, just put it in mysql and run it. Which one is fast and which one is slow, we will see each other.
  If the execution time of the two is almost the same, of course, you should choose the one-stop sql without hesitation. Good maintenance! It can greatly reduce the amount of code.
  Some friends may retort that such a long SQL statement is annoying to look at, so how can it be maintained? This is actually related to personal habits. For long SQL statements, I usually write this way, I don’t know if it’s right, please We give pointers.
  Copy code
  #long sql;
  select field 1, field 2, field 3, field n
  from table one as t1, table two as t2, table three as t3
  left join table four as t4 on t1. field 1 = t4. field 1
  left join table five as t5 on t1. field 2 = t5. field 2
  where t1. field 1=1 and t2. field 2=2 and t3. field 3 > 3
  group by t1. field 1
  order by t1
  limit 1, 5;
  Copy the code
  In a word, the principle of everything is to be logically clear, the layout is beautiful, and the alignment must be aligned.
  Write the code as Bai Juyi's poems, try to be concise, but take into account the readability, so that three-year-old children and seventy-year-old old women can understand.
  Write your code as if it were a graphic design, with neat layout between equals signs and between variables. The code snippets are patched and neatly arranged.
  Don't be stingy with spaces and newlines. Think of your code as a flow of data, and give your code room to flow.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326426318&siteId=291194637