alter command building database relationships. page 21/4/2014 presentation alter command the alter...

12
ALTER Command Building Database Relationships

Upload: jacob-pruitt

Post on 26-Mar-2015

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ALTER Command Building Database Relationships. page 21/4/2014 Presentation ALTER Command The ALTER command allows you to change almost everything in your

ALTER Command

Building Database Relationships

Page 2: ALTER Command Building Database Relationships. page 21/4/2014 Presentation ALTER Command The ALTER command allows you to change almost everything in your

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 3: ALTER Command Building Database Relationships. page 21/4/2014 Presentation ALTER Command The ALTER command allows you to change almost everything in your

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 4: ALTER Command Building Database Relationships. page 21/4/2014 Presentation ALTER Command The ALTER command allows you to change almost everything in your

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 5: ALTER Command Building Database Relationships. page 21/4/2014 Presentation ALTER Command The ALTER command allows you to change almost everything in your

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 6: ALTER Command Building Database Relationships. page 21/4/2014 Presentation ALTER Command The ALTER command allows you to change almost everything in your

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 7: ALTER Command Building Database Relationships. page 21/4/2014 Presentation ALTER Command The ALTER command allows you to change almost everything in your

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 8: ALTER Command Building Database Relationships. page 21/4/2014 Presentation ALTER Command The ALTER command allows you to change almost everything in your

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 9: ALTER Command Building Database Relationships. page 21/4/2014 Presentation ALTER Command The ALTER command allows you to change almost everything in your

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 10: ALTER Command Building Database Relationships. page 21/4/2014 Presentation ALTER Command The ALTER command allows you to change almost everything in your

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 11: ALTER Command Building Database Relationships. page 21/4/2014 Presentation ALTER Command The ALTER command allows you to change almost everything in your

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 12: ALTER Command Building Database Relationships. page 21/4/2014 Presentation ALTER Command The ALTER command allows you to change almost everything in your

page 1204/10/23 Presentation

Questions???