ALTER Command Building Database Relationships. page 21/4/2014 Presentation ALTER Command The ALTER...
-
Upload
jacob-pruitt -
Category
Documents
-
view
226 -
download
0
Transcript of ALTER Command Building Database Relationships. page 21/4/2014 Presentation ALTER Command The ALTER...
ALTER Command
Building Database Relationships
page 204/10/23 Presentation
ALTER Command
The ALTER command allows you to change almost everything in your table without having to reinsert your data.
But be careful, if you change a column of one data type to a different one, you risk losing your data.
page 304/10/23 Presentation
ALTER Command
Dataville Alterations: OUR SERVICES FOR EXISTING TABLES:
CHANGE both the name and data type of an existing column *
MODIFY the data type or position of an existing column * ADD a column to your table—you pick the data type DROP a column from your table * * Possible loss of data may occur, no guarantees offered.
page 404/10/23 Presentation
All about relationships
Import projekts table Let’s use DESCRIBE to see how this table is
constructed: DESCRIBE projekts; This shows us if a columns is the primary key and
what type of data is being stored in each column.
page 504/10/23 Presentation
ALTER TABLE Our first step will be to use ALTER TABLE and
give our table a meaningful name.
ALTER TABLE projekts
RENAME TO project_list;
page 604/10/23 Presentation
Retooling our columns
We can RENAME our existing columns. By renaming these columns that contain valid data, we won’t need to insert the data into new columns.
We need to add three columns called est_cost, con_phone, and start_date.
page 704/10/23 Presentation
ALTER and CHANGE
Change the column number to have a new name, proj_id, and set it to AUTO_INCREMENT. Then we’ll make it a primary key.
page 804/10/23 Presentation
Change two columns with one SQL statement We’ll alter the names of the columns called
descriptionofproj and contractoronjob, and at the same time we’re also going to change their data types.
All we have to do is include both CHANGE COLUMN lines in one ALTER TABLE statement and put a comma between them.
If you change the data type to something new, you may lose data.
page 904/10/23 Presentation
MODIFY keyword The MODIFY keyword. It changes only the data
type of a column and leaves the name alone. For example, suppose you needed a longer
column to hold the proj_desc. You want it to be VARCHAR(120).
Here’s what you need to do.
page 1004/10/23 Presentation
DROP that column
It’s good programming practice to have only the columns you need in your table. If you aren’t using a column, drop it.
With ALTER, you can easily add it back again, if you need it in the future.
Once you’ve dropped a column, everything that was stored in it is removed too!
page 1104/10/23 Presentation
Moving the columns
ALTER TABLE mytable MODIFY COLUMN mycolumn INT AFTER someothercolumn;
ALTER TABLE mytable MODIFY COLUMN mycolumn INT FIRST;
page 1204/10/23 Presentation
Questions???