Mysql: Tabelle erzeugen mit standard werten aus anderer Tabelle?
Insanity
- php
Moinsen Freaks...
Kann mir jemand erzählen ob es möglich ist, eine neue Tabelle zu erstellen, wo schon werte drinstehen, die aus anderen tabellen zusammengesetzt wurden. z.B.
Tabelle1:
Feld1:5
Neue Tabelle:
Feld1:Wert von Tabelle1.Wert1
ohne das ich das irgendwie einfügen muss???
Thx
Insane
Hi Insanity,
Kann mir jemand erzählen ob es möglich ist, eine neue Tabelle zu erstellen, wo schon werte drinstehen, die aus anderen tabellen zusammengesetzt wurden. z.B.
schau mal hier:
http://www.mysql.com/doc/C/R/CREATE_TABLE.html
Ich lese dir mal ein wenig aus dem Handbuch vor:
"If you specify a SELECT after the CREATE statement, MySQL will create new fields for all elements in the SELECT. For example:
mysql> CREATE TABLE test (a int not null auto_increment,
-> primary key (a), key(b))
-> TYPE=MyISAM SELECT b,c from test2;
This will create a MyISAM table with three columns, a, b, and c. Notice that the columns from the SELECT statement are appended to the right side of the table, not overlapped onto it. Take the following example:
mysql> select * from foo;
+---+
| n |
+---+
| 1 |
+---+
mysql> create table bar (m int) select n from foo;
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from bar;
+------+---+
| m | n |
+------+---+
| NULL | 1 |
+------+---+
1 row in set (0.00 sec)
For each row in table foo, a row is inserted in bar with the values from foo and default values for the new columns. CREATE TABLE ... SELECT will not automatically create any indexes for you. This is done intentionally to make the command as flexible as possible. If you want to have indexes in the created table, you should specify these before the SELECT statement:
mysql> create table bar (unique (n)) select n from foo;
If any errors occur while copying the data to the table, it will automatically be deleted. To ensure that the update log/binary log can be used to re-create the original tables, MySQL will not allow concurrent inserts during CREATE TABLE ... SELECT. "
Noch Fragen?
Gruß
Peppo