To insert a new record in a table we use Insert Query.
Syntax:
To insert all fields value in table we use following insert query
1 2 | INSERT INTO Table_name VALUES (value1, value2,....... value n); |
To insert all or selected fields value in table we use following insert query
1 2 | INSERT INTO Table_name(field1,field2,...field n) VALUES (value1, value2,....... value n); |
We have created a table student as below
1 2 3 | CREATE TABLE student(id int(10) NOT NULL AUTO_INCREMENT, name varchar(50),address varchar(50), class varchar(30),age int(2), PRIMARY KEY(ID)); |
To insert record in above table we use insert query like below
1 2 | insert into student values(1,'Ram Kumar','New Delhi','5th',11); insert into student values(2,'Mohan Kumar','New Delhi','5th',11); |
to insert specific column value we have to provide column name in insert query
1 2 | insert into student(name,address,class) values('Sohan Kumar','Bhopal','5th'); insert into student(name,class,age) values('Rohan Kumar','5th',11); |
here we have not provided any value for id column because id is auto increment column so it will automatically increment id column value one by one.
To see inserted values in table we use
1 | select * from student; |
1 2 3 4 5 6 7 8 9 10 | mysql> select * from student; +----+-------------+-----------+-------+------+ | id | name | address | class | age | +----+-------------+-----------+-------+------+ | 1 | Ram Kumar | New Delhi | 5th | 10 | | 2 | Mohan Kumar | New Delhi | 5th | 10 | | 3 | Sohan Kumar | Bhopal | 5th | NULL | | 4 | Rohan Kumar | NULL | 5th | 11 | +----+-------------+-----------+-------+------+ 4 rows in set (0.00 sec) |
We have not provided value for few fields that is showing null.