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.
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:
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:
Now take an example of alter command
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
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
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;
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
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 single column |
Here we are deleting a single column.
Alter with drop command |
Here we deleting multiple columns with a single alter statement.
See Also
Database
Tables
Update Command
Count Function
See Also
Database
Tables
Update Command
Count Function
No comments:
Post a Comment