Class 12 | MySQL 7

Inserting Data into table

Command: Insert into
This command helps us to add rows(tuples) to the relation/table.

Method 1
Syntax:
insert into tablename
values( value_of_col1,value_of_col2,value_of_col3);

On using this syntax:

• Values of all the cols has to be given
• Sequence and data types of the cols have to be taken care of.
• Values of char, varchar, date, and long types have to be given in single quotes.
• If we do not have a value of a particular column then it should be specified as “null” instead of giving some wrong/invalid value.
• Values of calculated columns should also be given as “null”.

Example:1
Table:Student(roll,name,age)

Insert into student values(101,’hello’,10);
Insert into student values(102,’abc’,null);
Insert into student values(103,null,12);
Insert into student values(null,’abc’,null);

Example:2
Table:Std(roll,name,m1,m2,m3,total,per)
Insert into std values(101,’abc’,98,87,99,null,null);

Example:3
Table:Bank(accno,name,bal)
Insert into bank values(1001,’abc’,5800);

Example:4
Table:Product(pno,name,rate,qty,cost)
Insert into product values(101,’lux’,17,10,null);

Method 2
syntax:
insert into tablename (col1,col2,col3)
values( value_of_col1,value_of_col2,value_of_col3);

On using this syntax:

• The cols whose values is to be given are specified and their respective values are given.
• Values of selected cols can be given.
• Sequence can be changed.
• The columns which are not specified will automatically get initialized by null value.
• Null means unknown value. It does not mean spaces for varchar fields. It does not mean zero for numeric fields.
• When we have calculated columns we can use this method.

Example:1
Table:Std(roll,name,age)
Insert into std (roll,age) values(101,10);
Insert into std (name) values(‘abc’);
Insert into std (name,roll) values(‘amit’,101);

Example:2
Table:Student(roll,name,m1,m2,m3,total,per)
Insert into student(roll,name,m1,m2,m3) values(104,’aa’,98,87,76);

To Save the data in the table

Command: Commit
This command helps us to make the changes permanent

Syntax:
commit;

To Undo the operations

Command: Rollback

This command helps us to undo the changes since last commit

Syntax:
Rollback;