Using MySQL: insert into

Creating a base is a simple but responsible task. Many factors must be considered. Noticeable progress in the hardware and software of information technology does not give reason to neglect the likelihood of a technical malfunction, unauthorized access, violation of the structure of tables, adding incorrect data.

MySQL insert into

A computer is not a person, and any, even the most β€œsmart”, algorithm is still far from the ideas of natural intelligence: without the help of a programmer, it can do little. However, unlike a person, a program can consistently and correctly fulfill its mission, the main thing is to write it correctly.

MySQL concept: insert into

Externally cumbersome forms of SQL syntax are very easy to use. The practice of writing letters in upper case is still preserved, but is gradually being replaced by a more concise use of both registers. In this regard, it is important not to forget: once the mentioned field name in the same register in the same query, if used again, must be in the same spelling.

MySQL insert into values

A characteristic feature of the MySQL insert into operation, as well as many things related to Internet programming: "that which is not understood" will not be executed. An incorrectly compiled database replenishment request will simply be ignored, and this can not always be noticed immediately.

The fact of adding a record must be controlled, as well as access to the database as a whole. Each field must be filled with a value of the corresponding type. It is not always necessary to indicate the fields of the result table. It is not always necessary to pass specific values: the MySQL insert into select construct allows you to get a set of added records from another table or query.

MySQL insert into select

The request must be syntactically and logically correct. The use of any version of the MySQL query insert into values ​​design should take into account the encoding of the script in which it is located, the encoding of the database table and, in fact, the information to be added.

Classic post entry

Any MySQL table is a sequence of records, each of which has a certain number of fields. You can add entries from another table. It is enough in the request to indicate in select what and where, and in into - where. The symbol "*" says that the query refers to all the fields of each record.

Adding multiple entries

As a result of accessing this function, the contents of the $ cSrcTable table are completely written to the $ cDstTable table, from which all records will be deleted first.

Adding a single entry

The MySQL insert into values construction allows you to add records one at a time, indicating specific fields and their specific values.

Adding one / several records
This query can be divided into three queries, in each of which one list of fields (`code_back`,` owner_code`, ...) will correspond to one row of data ('~', '{$ cSChip}', '{$ SChip_s } ', ...), (' ~ ',' {$ cPetr} ',' {$ cPetr_s} ', ...) or (' ~ ',' {$ cTest} ',' {$ cTest_s} ' , ...), but it’s easier. For example, in this case, a basic set of users has been added: administrator, dispatcher, and tester.

Adding entries through a custom interface

The principle of working with MySQL is implemented through the query form. It is convenient in command line mode and in the same form is implemented in various programming languages. In PHP, in particular, a query string is used - a regular sequence of characters, the contents of which are filled in during the operation of the algorithm. Then the text of the generated query goes to the mysqli_query () function and is executed.

What form of working with the database to choose is decided in the specific case by the developer, but in all cases it is most convenient to present the task in terms of its operations of reading / writing information from / to the database in the form of its own interface. This idea can be implemented as a set of functions or as a separate object.

MySQL query insert into values

In this option, directly the operations of adding records will be hidden, and the process will consist in successively invoking your own functions. For example, scfAddUser ('Ivanov', 'Ivan') will result in a MySQL query insert into `all_users` ('last_name', 'first_name', 'status') values ​​('Ivanov', 'Ivan', 'new'). This option significantly saves the code and makes it much more readable and meaningful.

A significant difference between the native interface and the direct use of database operations in their original form is that all the operations of adding, changing, and deleting records in a separate file can be controlled and modified without modification to the code that uses them. It is safer and more efficient.

Native interface from data object

If you do not take into account simple variables, then the modern program is a collection of objects. The more qualified the problem is solved, the more efficient is the designed system of objects and their interaction with each other.

Obviously, data writing and reading operations take on a different context: an object can save itself in a database, can restore itself from a database, check its state, transfer contents to another object, etc.

This approach transfers the center of gravity from directly coding MySQL insert into queries to the interface: scfAddObject ('contens', ...), which is used by each object in its own way.

The date object will have the method myDate-> Save () and will call scfAddObject ('04 .12.2016 '), and the user object currUser-> Save () will make scfAddObject (' Ivanov - login '), ... with this every call to scfAddObject () will lead to the construction of its own version of the MySQL insert into query.

Source: https://habr.com/ru/post/K22659/


All Articles