There may be times when you realize that you’ve created a table that is too wide, with too many columns. Perhaps some columns would be handled better in a separate table. Or
perhaps you started adding new columns to an existing table and found it became unruly over time. In either case, you could create a smaller table and then move data from the larger table into the new, smaller one. To do this, you can create a new table with the same settings for the columns you want to move, then copy the data from the first table to the new table, and then delete the columns you no longer need from the first table. If you wanted to make this transition by the method just described, the individual column settings will need to be same in the new table to prevent problems or loss of data.
An easier method for creating a table based on another table is to use the CREATE TABLE
with the LIKE clause. Let’s try that to create a copy of the birds table. Enter the following in mysql on your server:
CREATE TABLE birds_new LIKE birds;
This creates an identical table like the birds table, but with the name birds_new. If you enter the SHOW TABLES statement in mysql, you will see that you now have a birds table and a new table, birds_new.
NOTE
You can use an underscore (i.e., _) in a table name, but you may want to avoid using hyphens. MySQL interprets a hyphen as a minus sign and tries to do a calculation between the two words given, which causes an error. If you want to use a hyphen, you must always reference the table name within quotes.
Execute the following three SQL statements to see what you now have:
DESCRIBE birds;
DESCRIBE birds_new;
SELECT * FROM birds_new;
Empty set (0.00 sec)
The first two SQL statements will show you the structure of both tables. They will confirm that they are identical except for their names. To save space, I didn’t include the results of those two SQL statements here.
The third SQL statement should show you all of the rows of data in the birds_new table.
Because we copied only the structure of the birds table when we created the new table, there is no data — as indicated by the message returned. We could copy the data over when we’re finished altering the table if that’s what we want to do.
This method can also be used when making major modifications to a table. In such a situation, it’s good to work from a copy of the table. You would then use the ALTER TABLE
statement to change the new table (e.g., birds_new). When you’re finished making the changes, you would then copy all of the data from the old table to the new table, delete the original table, and then rename the new table.
In such a situation, you may have one minor problem. I said earlier that the tables are identical except for the table names, but that’s not exactly true. There may be one other difference. If the table has a column that uses AUTO_INCREMENT for the default value, the counter will be set to 0 for the new table. You must determine the current value of
AUTO_INCREMENT for the birds table to be assured that the rows in the new table have the correct identification numbers. Enter the following SQL statement in mysql:
SHOW CREATE TABLE birds \G
In the results, which are not shown, the last line will reveal the current value of the
AUTO_INCREMENT variable. For instance, the last line may look as follows:
...
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 COLLATE=latin1_bin
In this excerpt of the results, you can see that the variable, AUTO_INCREMENT is currently 6.
Set AUTO_INCREMENT to the same value in the birds_new table by entering the following SQL statement in mysql:
ALTER TABLE birds_new AUTO_INCREMENT = 6;
When you’re ready to copy the data from one table to the other, you can use the INSERT…
SELECT syntax. This is covered in Other Possibilities.
Instead of copying the data after you’re finished modifying the new table, you can copy the data while creating the new table. This might be useful when you want to move only certain columns with their data to a new table, without any alterations to the columns. To do this, you would still use the CREATE TABLE statement, but with a slightly different syntax.
Let’s suppose that we have decided that we want to create a new table for details about each bird (e.g., migratory patterns, habitats, etc.). Looking at the birds table, though, we decide that the description column and its data belong in this new table. So we’ll create a new table and copy that column’s settings and data, as well as the bird_id into the new table. We can do that by entering the following from mysql to get the table started:
CREATE TABLE birds_details SELECT bird_id, description FROM birds;
This creates the birds_details table with two columns, based on the same columns in the
birds table. It also copies the data from the two columns in the birds table into the
birds_details table. There is one minor, but necessary, difference in one of the columns in the new table. The difference has to do with AUTO_INCREMENT again, but not in the same way as earlier examples. Enter the DESCRIBE statement to see the difference:
DESCRIBE birds_details;
+---+---+---+---+---+---+
| Field | Type | Null | Key | Default | Extra | +---+---+---+---+---+---+
| bird_id | int(11) | NO | | 0 | |
| description | text | YES | | NULL | | +---+---+---+---+---+---+
The difference here is that the bird_id does not use AUTO_INCREMENT. This is good
because we have to manually set the value of the bird_id for each row that we enter. We won’t have details for each bird, though, and we won’t necessarily be entering them in the same order as we will in the birds table. We could change the bird_id column in this table to an AUTO_INCREMENT column, but that would cause problems — trying to keep it in line with the birds table would be maddening. We could, however, make an index for the
bird_id column in the birds_details table by using the ALTER TABLE statement and setting the column to a UNIQUE key. That would allow only one entry per bird, which may be a good idea. This is covered in Indexes.
The CREATE TABLE…SELECT statement created the birds_details table with only two
columns. We said, though, that we want more columns for keeping other information on birds. We’ll add those additional columns later with the ALTER TABLE statement, in the exercises at the end of the chapter. For now, let’s remove the column description from the birds table by entering this from mysql:
ALTER TABLE birds
DROP COLUMN description;
This will delete the column and the data in that column. So be careful using it. This clause will be covered in more depth in Chapter 6.