Advanced Database Management System Lab no. 12. Outline SQL Commands (for MySQL) –Alter Command...

15
Advanced Database Management System Lab no. 12

Transcript of Advanced Database Management System Lab no. 12. Outline SQL Commands (for MySQL) –Alter Command...

Advanced Database Management System

Lab no. 12

Outline

• SQL Commands (for MySQL)– Alter Command– phpMyAdmin (DB backup)

Alter table statement

• Changing a table name– alter table table_name rename

new_table_name;

• Adding Columns– alter table table_name add column

column_name column_attributes;

• Dropping Columns– alter table table_name drop column

column_name;

Alter table statement• You can add indexes using the index, unique, and primary

key commands in the same way you would use them in the create statement:

• Adding index– alter table my_table add index index_name (column_name1);

• Dropping Index– alter table my_table drop index index_name;

• Adding unique index constraint– alter table my_table add unique index_name(column_name);– Note that unique index cannot be created on a column with duplicate

values• Adding Unique constraint on a column

– alter table my_table add unique(column_name);

Alter table statement

• Adding primary key– alter table my_table add primary key(my_column);

• Dropping primary key– alter table my_table drop primary key;

Alter table statement

• Adding foreign key– alter table my_table add foreign key (my_column)

references other_table(col);

• Dropping foreign key– alter table my_table drop foriegn key;

Note:– Other option to add foreign key from PHPMyAdmin

is shown in next slide• You will have to create an index on the foreign key column

then select the option shown in next slide.

Alter cont’d

• Changing column definitions– It is possible to change a column’s name or attributes with either

the change or modify command.

• To change a column’s name you must also redefine the column’s attributes. The following will work:

– alter table my_table change old_col new_col int not null;

• To change a column’s characteristics/attributes, do the following:

– alter table my_table modify col_name varchar(50);

For further reference on ALTER

http://dev.mysql.com/doc/refman/5.0/en/alter-table.html

Show statement cont’d

• show columns– Syntax: show columns from table_name;

• show index– Syntax: show index from table_name;

• show table status– Syntax: Show table status like ‘table_name’;

The Show statement• Show Database: On mySql console write…

– mysql> show databases;• Show Tables: On MySql console write…

– mysql> Use guestbook;– mysql> show tables;

• Example:mysql> use guestbook;Database changedmysql> show tables;+---------------------+| Tables_in_guestbook |+---------------------+| guestbook || guestbook1 || my_table |+---------------------+3 rows in set (0.11 sec)mysql>

Examplemysql> show table status like 'guestbook'*************************** 1. row *************************** Name: guestbook Engine: InnoDB Version: 10 Row_format: Compact Rows: 0 Avg_row_length: 0 Data_length: 16384Max_data_length: 0 Index_length: 0 Data_free: 0 Auto_increment: NULL Create_time: 2008-04-05 01:38:12 Update_time: NULL Check_time: NULL Collation: latin1_swedish_ci Checksum: NULL Create_options: Comment: InnoDB free: 4096 kB1 row in set (0.02 sec)mysql>_

Show cont’d

• Show create table: – Syntax: show create table guestbookmysql> show create table guestbook *************************** 1. row *************************** Table: guestbookCreate Table: CREATE TABLE `guestbook` ( `name` varchar(40) default NULL, `location` varchar(40) default NULL) ENGINE=InnoDB DEFAULT CHARSET=latin11 row in set (0.00 sec)mysql>

Taking Database BackupBacking Up and Restoring Your MySQL Database - Backing Up and Restoring Your Database with PHPMyAdmin ( Page 5 of 5 )

You can backup your database using PHPMyAdmin in just a few mouse clicks. First head over to your database by clicking the database name in the list on the left of the screen. Once you get to your database details, you should get a menu header for your database which looks like so:

Click on Export. This will get you to a screen with the following options.

From here it's just a matter of clicking the right options and the 'Go' Button to get your database dump. To save the backup file on your computer, click on the Save as file option and the corresponding compression option to get the backup to download to your system.

Restoring your Database via PHPMyAdmin

Restoring your database is as easy as backing it up. If you would like to rewrite the backup over an existing database, click on the database name in the database list on the left, click all the check boxes next to the table names and select Drop in the With selected: drop down box.