Learning Mysql


-> artist, album WHERE artist.artist_id=album.artist_id



Download 4.24 Mb.
View original pdf
Page260/366
Date04.08.2023
Size4.24 Mb.
#61806
1   ...   256   257   258   259   260   261   262   263   ...   366
Learning MySQL
-> artist, album WHERE artist.artist_id=album.artist_id
-> INTO OUTFILE '/tmp/artists_and_albums.csv' FIELDS TERMINATED BY ',';
Query OK, 13 rows affected (0.02 sec)
Here, we’ve saved the results into the file artists_and_albums.csv in the /tmp directory;
the MySQL server must be able to write to the directory that you specify. On a Windows system, specify a path such as C:\artists_and_albums.csv instead. If you omit the
FIELDS
284 | Chapter 8:
Doing More with MySQL

TERMINATED BY
clause, the server will use tabs as the default separator between the data values.
You can view the contents of the file artists_and_albums.csv in a text editor, or import it into a spreadsheet program:
New Order,Retro - John McCready FAN
New Order,Substance (Disc New Order,Retro - Miranda Sawyer POP
New Order,Retro - New Order / Bobby Gillespie LIVE
New Order,Power\, Corruption & Lies
New Order,Substance 1987 (Disc New Order,Brotherhood
Nick Cave & The Bad Seeds,Let Love In
Miles Davis,Live Around The World
Miles Davis,In A Silent Way
The Rolling Stones,Exile On Main Street
The Stone Roses,Second Coming
Kylie Minogue,Light Years
Notice how the comma in
Power,
Corruption
&
Lies has been automatically escaped with a backslash to distinguish it from the separator. Spreadsheet programs understand this and remove the backslash when importing the file.
Creating Tables with Queries
You can create a table or easily create a copy of a table using a query. This is useful when you want to build anew database using existing data—for example, you might want to copy across a list of countries—or when you want to reorganize data for some reason. Data reorganization is common for producing reports, merging data from two or more tables, and redesigning on the fly. This short section shows you how it’s done.
From MySQL 4.1 onward, you can easily duplicate the structure of a table using a variant of the CREATE TABLE
syntax:
mysql> CREATE TABLE artist LIKE artist;
Query OK, 0 rows affected (0.24 sec)
mysql> DESCRIBE artist_2;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| artist_id | smallint(5) | | PRI | 0 | |
| artist_name | char) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
2 rows inset sec)
mysql> SELECT * FROM artist_2;
Empty set (0.30 sec)

Download 4.24 Mb.

Share with your friends:
1   ...   256   257   258   259   260   261   262   263   ...   366




The database is protected by copyright ©ininet.org 2024
send message

    Main page