ComputersProgramming

Using MySQL: insert into

Creating a database is a simple but important task. There are many factors to consider. Noticeable progress in the hardware and software of information technologies does not give grounds for neglecting the probability of technical malfunction, unauthorized access, violation of the structure of tables, and the addition of incorrect data.

The computer is not human, and any, even the most "smart" algorithm, is still far from the ideas of natural intelligence: without the help of a programmer, he has not much ability. However, unlike a person, the program can perform its mission stably and correctly, the main thing is to write it correctly.

The concept of MySQL: insert into

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

A characteristic feature of the operation MySQL insert into, as well as much, that is connected with Internet programming: "will not be fulfilled what is not understood." Incorrectly compiled query for replenishment of the database will be ignored, and not always it can be seen right away.

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

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

Classic addition of a record

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

As a result of the call to this function, the contents of the $ cSrcTable table will be completely written into the table $ cDstTable, from which all records will be removed beforehand.

Adding one record

The MySQL insert into values structure allows you to add records one by one, specifying the specific fields and their corresponding specific values.

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

Adding entries via your own interface

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

What form of work with a database to choose is to be decided in a particular case by the developer, but in all cases it is most convenient to present the task in part of its read / write operations from / to the database in the form of an own inter- face. This idea can be implemented as a set of functions or an individual object.

In this case, the operations of adding records will be hidden directly, and the process will consist in the sequential call of their 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.

The essential 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 made in a separate file can be controlled and changed without modifying the code that uses them. It is more safe and effective.

Own interface from the data object

If you do not take into account simple variables, then the modern program is a collection of objects. The more skillfully the task is solved, the more effectively the projected system of objects and their interaction with each other.

Obviously, the operations of writing and reading data take a different context: the object can save itself in the database, it can recover itself from the database, check its state, transfer the content to another object, etc.

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

The date object will have the method myDate-> Save () and execute the call scfAddObject ('04 .12.2016 '), and the user's currUser-> Save () object will make scfAddObject (' Ivanov-login '), ... with each scfAddObject () Will result in the design of its MySQL query insert into.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.atomiyme.com. Theme powered by WordPress.