Presentation on theme: "ALTER Command Building Database Relationships. page 21/4/2014 Presentation ALTER Command The ALTER command allows you to change almost everything in your."— Presentation transcript:
page 21/4/2014 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 31/4/2014 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 tableyou pick the data type DROP a column from your table * * Possible loss of data may occur, no guarantees offered.
page 41/4/2014 Presentation All about relationships Import projekts table Lets 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 51/4/2014 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 61/4/2014 Presentation Retooling our columns We can RENAME our existing columns. By renaming these columns that contain valid data, we wont need to insert the data into new columns. We need to add three columns called est_cost, con_phone, and start_date.
page 71/4/2014 Presentation ALTER and CHANGE Change the column number to have a new name, proj_id, and set it to AUTO_INCREMENT. Then well make it a primary key.
page 81/4/2014 Presentation Change two columns with one SQL statement Well alter the names of the columns called descriptionofproj and contractoronjob, and at the same time were 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 91/4/2014 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). Heres what you need to do.
page 101/4/2014 Presentation DROP that column Its good programming practice to have only the columns you need in your table. If you arent using a column, drop it. With ALTER, you can easily add it back again, if you need it in the future. Once youve dropped a column, everything that was stored in it is removed too!
page 111/4/2014 Presentation Moving the columns ALTER TABLE mytable MODIFY COLUMN mycolumn INT AFTER someothercolumn; ALTER TABLE mytable MODIFY COLUMN mycolumn INT FIRST;