Thursday, 1 August 2019

Mysql Table Queries | Alter Command

My Sql Table Queries


Table Queries can be 
  • Create Table 
  • Insert into
  • Alter Table
  • Drop Table
  • Show Tables
  • Update Table
  • Delete
  • Describe and many more.
As we discussed about create table in our last post,if you want to see click on Tables

Insert Into command is used to insert or enter a new row of data.

Syntax:

insert into table_name values ();
or
insert into table_name(column_name) values ();

for example:

insert values table


Alter table

It is used to change or modify the structure of table.It works on column wise.You can change the column name, modify the datatype of column,drop the particular column,add a column using alter command. 

Alter Command 
  • Add :- To add a new column
  • Drop :- To delete the column
  • Modify :- To change the datatype
  • Change :- To change the column name   


Syntax:

alter table table_name
add column_name datatype;

alter table table_name
change old_column_name  new_column_name  datatype;

alter table table_name
drop column_name;

alter table table_name
modify column_name newDatatype;


Before taking example we will discuss about Describe Command .

Describe Command is used to view the structure of the table.It displays the fields,key,datatype etc..

For example:
Describe Command Display Structure


Now take an example of alter command


How to add new column


Here, we are adding a new column called 'course' to the table 'student'.After adding a new column , it takes 'Null' values by default.
Null means unknown value.It is not equal to the zero.

Change with Alter command


Change column name with alter

Here, we are changing the old column name to new column name .


We have discussed about Database and Tables , we have also learn about alter command,insert command,create command.

Now we will learn alter in detail with practical examples:

Modify Datatype with Alter Command

we can change or modify the  datatype of  column , size of datatype using modify keyword with alter.

we have a table called student
To view the data use select statement

This table has four columns , each column has its datatype.we can check or view the datatype of each column using describe command.Describe command is used to view table structure.we can use

syntax:

describe table_name;
or
desc table_name;

To view table structure using desc cmd

Here, 'studentid' column is of integer type . It takes only integer values (2,101,67,890) not accept character values ('S001') .If we want to enter or insert 'S001' these types of values ,we will have to change the datatype . For this operation we use modify keyword with alter command.

lets see
To change the datatype using alter
Alter with modify

Here, we are modifying the datatype of 'studentid 'column from integer to character.


Drop Column 

we can drop or delete the particular column using drop keyword with alter command.We can delete multiple columns also with a single alter statement permanently.

Drop Column using Alter Command
Drop single column



Here we are deleting a single column.

alter command in mysql
Alter with drop command

Here we deleting multiple columns with a single alter statement.


See Also

Database
Tables
Update Command
Count Function

No comments:

Post a Comment

Recent Post

Python Project | Banking System | Project on Banking System using python

  PYTHON PROJECT Banking System import csv import pandas as pd import matplotlib.pyplot as plt import sys import datetime found=False def ne...