Sabtu, 06 Agustus 2016

[Step 03] modify a table with the command 'alter table'

In previous posts we have learned how to create a table, now we will learn how to modify a table
in previous posts we have created a database with the name becoding that contains a table with namefriend that which has the following structure:
ScreenShot: Structure [Click Here] 

if you do not have a database of over please read his tutorial part 2
To modify the table structure of the command used is 'alter table table_name' followed by the command,the command in question is as follows: 
1.Change / Modify (used to change the field). 
2.Add column (used to add field).
 3.Drop column (used to delete the field). 

1.Change / Modify 
Now we will start by modifying a field with two kinds of commands that 'change' and 'modify'.then what is the difference between these two commands, the difference is the change used to change a field from the name to the data type, whereas modify only be used to change the data type of a field. 
how writing is: 
Change: change old_field_name new_field_name new_data_type; 
Modify: modify field_name new_data_type;

for example we want to change the size of the field name that was originally 12 to 30, then the command used is'Alter table friend modify name char (30) not null;', used modify because we only want to change field size.
ScreenShot: Modify [Click Here]
Tthen what if we want to change the field 'name' to 'friend_name' command is used is'Alter table friend change name friend_name char (30) not null;', used change as we change the name of this field.
ScreenShot: Change [Click Here] 

2.Add column

We've learned how to modify the field now we will learn how to add a new field to a certain table.with the command 'add column'. 
writing is 'add column field_name data_type;' 
for example, we will add a new column with the names 'grade' data type char size 1 into table 'friend', then the writing is'Alter table friend add column grade char (1) not null;'
ScreenShot: Add Column [Click Here] 

3.Drop column 

Next we will learn to remove a field from a table with a drop column command,writing is 'drop column field_name;'. 
for example, we will remove  Grade field from the table friend the , then the writing is'Alter table friend drop column grade;'.
ScreenShot: Drop Column [Click Here] 

That some commands to modify the next field we will continue with the command alter the other, i hope this post can be useful for you all. Our BeCoding.tk thank you for visiting and reading to the end.

Tidak ada komentar:

Posting Komentar