Frage

ich zufällig zwei Spalten haben die gleichen Namen wie zwei Worte SQL reserviert haben, Schlüssel und Wert. Wenn die SELECT-Anweisung kann ich eine Tabelle Alias ??erstellen und es auf diese Weise lösen.

Nun zu INSERT Daten Ich versuche, und es scheint, als ob Sie nicht Tabellenalias in der INSERT-Anweisung erstellen können.

INSERT INTO attributeStrings ats
(ats.ItemID,ats.Key,ats.Value)
VALUES (3,'Categories','TechGUI')

Ich bekomme Fehler bei 'ats (ats.ItemID,ats.Key,ats.Value) VALUES (3,'Categories','TechGUI')' diesen Alias ??angibt, kann nicht erstellt werden.

Gibt es Möglichkeiten, dies zu lösen, ohne die Spalte Schlüssel und Wert zu umbenennen?

War es hilfreich?

Lösung

Mit Back-tick reservierte Worte zu entkommen.

  INSERT INTO `attributeStrings` (`ItemID`, `Key`,`Value`) VALUES (3,'Categories','TechGUI')

Sieht aus wie Insert nicht unterstützt Alias. sehen hier


Edit: ok, die MySQL ref sagt kein Alias ??in Einsatz


Es funktioniert

mysql> INSERT INTO `attributeStrings` (`ItemID`, `Key`,`Value`) VALUES (3,'Categories','TechGUI');
Query OK, 1 row affected (0.03 sec)

mysql> select * from attributeStrings;
+--------+------------+---------+
| ItemId | Key        | Value   |
+--------+------------+---------+
|      3 | Categories | TechGUI |
+--------+------------+---------+
1 row in set (0.00 sec)

Andere Tipps

Sie einfach die Mafioso sein auf der Grundlage Ihrer Frage

INSERT INTO attributeStrings
VALUES (3,'Categories','TechGUI');
/* if the table have more than 3 columns, fill-up every column then */

Andere Probleme

Sinnlose camelcase zu haben, da Windows-Dateinamen mit Fall nicht empfindlich noch unterstützen.

So können Sie praktisch haben gleiche Tabelle mit unterschiedlichem Fall unter Linux / Mac, aber nicht auf Windows

mysql> create table abc (id int(10));
Query OK, 0 rows affected (0.00 sec)

mysql> create table abC (id int(10));
Query OK, 0 rows affected (0.01 sec)

Key ist ein

reserviertes Wort

In Mysql so muss es zitiert werden

Sie können alle Reservierte Wörter überprüfen hier

it seems like you can't create table alias in the INSERT statement

Some SQL products (e.g. SQL Server) do allow this but I'm not sure that's a good thing.

According to SQL Standards, an alias should have the effect of the table being materialized i.e. an INSERT (or any other flavour of update) to an aliased table should leave the underlying base table(s) unaffected.

In SQL Server, such an INSERT does affect the underlying base table(s) but I think the actual (non-compliant) effect is what most SQL coders would expect to happen in that situation. Put another way, a valid INSERT statement that resulted in no rows being added I think would initially be suspected as a bug, even though that's the way Standard SQL is supposed to work.

So I think disallowing the situation to arise in the first place, while again being technically non-compliant with Standards, is desireable, in my opinion.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top