Top Banner
Advanced Database Management System Lab no. 12
15

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

Jan 03, 2016

Download

Documents

Gavin Sharp
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Advanced Database Management System Lab no. 12. Outline SQL Commands (for MySQL) –Alter Command –phpMyAdmin (DB backup)

Advanced Database Management System

Lab no. 12

Page 2: Advanced Database Management System Lab no. 12. Outline SQL Commands (for MySQL) –Alter Command –phpMyAdmin (DB backup)

Outline

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

Page 3: 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;

Page 4: Advanced Database Management System Lab no. 12. Outline SQL Commands (for MySQL) –Alter Command –phpMyAdmin (DB backup)

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);

Page 5: Advanced Database Management System Lab no. 12. Outline SQL Commands (for MySQL) –Alter Command –phpMyAdmin (DB backup)

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;

Page 6: Advanced Database Management System Lab no. 12. Outline SQL Commands (for MySQL) –Alter Command –phpMyAdmin (DB backup)

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.

Page 7: Advanced Database Management System Lab no. 12. Outline SQL Commands (for MySQL) –Alter Command –phpMyAdmin (DB backup)
Page 8: Advanced Database Management System Lab no. 12. Outline SQL Commands (for MySQL) –Alter Command –phpMyAdmin (DB backup)
Page 9: Advanced Database Management System Lab no. 12. Outline SQL Commands (for MySQL) –Alter Command –phpMyAdmin (DB backup)

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);

Page 10: Advanced Database Management System Lab no. 12. Outline SQL Commands (for MySQL) –Alter Command –phpMyAdmin (DB backup)

For further reference on ALTER

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

Page 11: Advanced Database Management System Lab no. 12. Outline SQL Commands (for MySQL) –Alter Command –phpMyAdmin (DB backup)

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’;

Page 12: Advanced Database Management System Lab no. 12. Outline SQL Commands (for MySQL) –Alter Command –phpMyAdmin (DB backup)

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>

Page 13: Advanced Database Management System Lab no. 12. Outline SQL Commands (for MySQL) –Alter Command –phpMyAdmin (DB backup)

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>_

Page 14: Advanced Database Management System Lab no. 12. Outline SQL Commands (for MySQL) –Alter Command –phpMyAdmin (DB backup)

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>

Page 15: Advanced Database Management System Lab no. 12. Outline SQL Commands (for MySQL) –Alter Command –phpMyAdmin (DB backup)

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.