This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
en:dev:all:examples:sql [03.06.2015 15:55] – external edit 127.0.0.1 | en:dev:all:examples:sql [20.02.2019 07:46] – [SELECT] Manuela v.d.Decken | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | FIXME **This page is not fully translated, yet. Please help completing the translation.**\\ //(remove this paragraph once the translation is finished)// | + | ====== |
- | ====== So funktioniert das mit SQL... ====== | + | All the examples |
- | Die ganzen Beispiele | + | ===== Building |
- | ===== SQL-Statements richtig aufgebaut | + | This is |
- | Das ist schon <del>fast</ | + | * Statements |
- | Also am besten erst ein mal die grundlegensten Regeln: | + | * Statements |
- | * Statements | + | * all SQL keywords must be written |
- | * Statements | + | * all field and table names must be enclosed |
- | * alle SQL Schlüsselwörter müssen | + | * Statements |
- | * alle Feld- und Tabellennamen müssen | + | |
- | * Statements | + | SQL statements should be built up visually such that they can be read and understood quickly and easily. The line by line breakdown by action keywords is very useful here. If a line is too long (Coding Standards) then spread with indentation on multiple lines. In the context of selecting fields it should be noted that the server can provide complete records |
- | SQL-Statements sollten auch optisch so aufgebaut werden, dass sie problemlos und schnell gelesen und erfasst werden können. Die zeilenweise Aufteilung nach Action-Schlüsselwörtern ist an der Stelle sehr sinnvoll. Ist eine Zeile zu lang (Codingstandards) dann mit Einrückung auf mehrere Zeilen verteilen. Bei der Feldauswahl im SELECT-Bereich ist zu beachten, dass der Server komplette Datensätze | + | |
- | Um den Aufbau von Statements zu demonstrieren, folgen einige Beispiele der wichtigsten Abfragetypen. | + | |
---- | ---- | ||
==== SELECT ==== | ==== SELECT ==== | ||
- | Alle vier Beispiele geben jeweils ein Result-Objekt mit allen Datensätzen der zum aktuellen Zeitpunkt sichtbaren Sections einer Seite zurück. | + | Each of all the four examples give back a result-object with all records of the visible sections of a page at the current time. |
- | <code php Beispiel-1.php> | + | <code php Example-1.php> |
- | $oResult = $oDb-> | + | $oResult = $oDb-> |
</ | </ | ||
- | <code php Beispiel-2.php> | + | <code php Example-2.php> |
$sql = ' | $sql = ' | ||
- | $sql .= 'WHERE `page_id`=' | + | $sql .= 'WHERE `page_id`=' |
$sql .= 'ORDER BY `block`, `position`'; | $sql .= 'ORDER BY `block`, `position`'; | ||
$oResult = $oDb-> | $oResult = $oDb-> | ||
</ | </ | ||
- | <code php Beispiel-3.php> | + | <code php Example-3.php> |
$sql = ' | $sql = ' | ||
| | ||
. 'FROM `' | . 'FROM `' | ||
. 'WHERE `page_id`=' | . 'WHERE `page_id`=' | ||
- | | + | |
- | | + | |
. 'ORDER BY `block`, `position`'; | . 'ORDER BY `block`, `position`'; | ||
$oResult = $oDb-> | $oResult = $oDb-> | ||
</ | </ | ||
- | <code php Beispiel-4.php> | + | <code php Example-4.php> |
$sql = ' | $sql = ' | ||
. 'WHERE `page_id`=' | . 'WHERE `page_id`=' | ||
- | | + | |
- | | + | |
. 'ORDER BY `block`, `position`'; | . 'ORDER BY `block`, `position`'; | ||
$oResult = $oDb-> | $oResult = $oDb-> | ||
- | </ | + | </ |
- | Welche Beispiele lassen sich leichter lesen, verstehen und bei Bedarf auch leichter ändern? **1**& | + | Question: Which of the examples are easier to read, understand and modify if required? **1**& |
---- | ---- | ||
==== DELETE ==== | ==== DELETE ==== | ||
- | Bei dieser Abfrage kann man eigentlich nichts falsch machen... | + | With this query, you can not go wrong... except for the case that you provide the wrong deletion criteria. ;-)\\ |
- | Aber auch hier gilt: Erst das Statement | + | But again: First build the statement and save it in a variable and then passe it to the query method. |
<code php DELETE-1.php> | <code php DELETE-1.php> | ||
$sql = ' | $sql = ' | ||
. 'WHERE `user_id`=' | . 'WHERE `user_id`=' | ||
$oDb-> | $oDb-> | ||
- | </ | + | </ |
---- | ---- | ||
==== INSERT / UPDATE ==== | ==== INSERT / UPDATE ==== | ||
- | In diesem Bereich wird es langsam interessant. Es gibt mehrere verschiedene Arten die Statements für INSERTs | + | In this area it will get interesting. There are several different ways to build the statements for INSERTs |
- | Bei **allen** Arten von INSERTs | + | With **all** kinds of INSERTs, however the SQL-strict rule applies, that values must be assigned to **all** record fields. Excluded are only the fields that are already defined |
- | Immer wenn Daten in die Datenbank geschrieben werden sollen, sind bestimmte Sicherheitsregeln zu beachten. | + | |
- | * Es muss sichergestellt sein, dass nur der richtige Datentyp übergeben wird. | + | |
- | * Es muss sichergestellt sein, dass jeder übergebene Wert zuvor überprüft wurde und gültig ist. | + | |
- | * Es dürfen nicht Werte aus Superglobalen Arrays, speziell aus // | + | |
- | * Stringvariable müssen vor der Übergabe '// | + | |
- | * Enthält eine Tabelle ein // | + | |
- | :!: Im Umfeld von WebsiteBaker ist für INSERT und UPDATE Statements ausschließlich die ' | + | |
- | Erst ein Beispiel wie ein Statement | + | * It must be ensured that only the correct data type is passed. |
+ | * It must be ensured that any value passed was previously examined and is valid. | ||
+ | * It is not allowed to pass values of superglobal arrays, especially from // | ||
+ | * String variables have to be '// | ||
+ | * If a table contains an // | ||
+ | |||
+ | :!: In the context of WebsiteBaker | ||
+ | |||
+ | Only one example of how such a statement | ||
<code php insert-01.php> | <code php insert-01.php> | ||
- | // das ist übrigens ein Original-Statement aus WB-2.8.3-SP1 'add user' | + | // by the way this is an original statement from WB-2.8.3-SP1 'add user' |
$sql = " | $sql = " | ||
</ | </ | ||
- | Das selbe Statement jetzt nach den zuvor beschriebenen Regeln: | + | The same statement now following the rules described above: |
<code php insert-02.php> | <code php insert-02.php> | ||
$sql = ' | $sql = ' | ||
Line 87: | Line 84: | ||
| | ||
</ | </ | ||
- | Ein UPDATE | + | An UPDATE |
<code php SET-1.php> | <code php SET-1.php> | ||
$sql = ' | $sql = ' | ||
Line 93: | Line 90: | ||
| | ||
. 'WHERE `user_id`=' | . 'WHERE `user_id`=' | ||
- | </ | + | </ |
---- | ---- | ||
- | |||
==== REPLACE ==== | ==== REPLACE ==== | ||
- | //(mySQL spezifische Erweiterung zu ANSI-SQL)// | + | //(MySQL specific extension to ANSI SQL)// |
- | Wie überall gibt es fast keine Regel ohne Ausnahme.\\ | + | Like everywhere else, there is almost no rule without exception.\\ |
- | **REPLACE** | + | **REPLACE** |
- | Wird versucht, einen Datensatz einzufügen, der einen Index-Konflikt mit einem bestehende Datensatz auslöst, wird der Prozess nicht abgebrochen, | + | If you try to insert a record which would cause an index conflict with an existing data set, the process will not be canceled, but the existing, old record is deleted and the new values are added instead.\\ |
- | **REPLACE** | + | **REPLACE** |
- | Der WebsiteBaker-Core benutzt dieses | + | The WebsiteBaker |
<code sql> | <code sql> | ||
-- Structure of table ' | -- Structure of table ' | ||
Line 114: | Line 110: | ||
){TABLE_ENGINE}; | ){TABLE_ENGINE}; | ||
</ | </ | ||
- | Im Statement muss zwingend ein Wert für das Feld mit dem PRIMARY_KEY | + | In a statement it is mandatory to pass a value for the field with the PRIMARY_KEY |
- | Auch der Aufbau des Statements weicht vom normalen | + | |
<code php snippet.php> | <code php snippet.php> | ||
$sql = ' | $sql = ' | ||
Line 125: | Line 120: | ||
}</ | }</ | ||
- |