The SQL INSERT command adds new rows to a table. The recommended form names the columns explicitly and supplies values in the same order.
INSERT INTO student (name,class,mark,gender)
VALUES ('Alex','Four',78,'male');
Here name, class, mark and gender are the destination columns. The values must match those columns in sequence and satisfy the table's datatype and constraint rules.
The common syntax is:
INSERT INTO table_name (column1,column2,column3)
VALUES (value1,value2,value3);
The number and order of values should correspond to the columns listed after the table name.
Text and date values are normally quoted, while numeric values are normally written without quotes:
INSERT INTO student (name,class,mark)
VALUES ('John','Three',75);
To add one student record:
INSERT INTO student (name,class,mark,gender)
VALUES ('Maria','Five',82,'female');
If the query succeeds, one new row is added to the table.
SELECT id,name,class,mark,gender
FROM student
WHERE name='Maria';
Use the SELECT tutorial and WHERE tutorial for retrieving and filtering records.
MySQL allows several rows to be added with one INSERT statement:
INSERT INTO student (name,class,mark,gender)
VALUES
('Ravi','Four',76,'male'),
('Sara','Four',88,'female'),
('Mohan','Five',69,'male');
This is clearer and usually more efficient than sending three separate INSERT statements when all rows are known together.
SQL also permits an INSERT without a column list when a value is supplied for every column in the table's expected order:
INSERT INTO t1
VALUES (1,'one');
However, explicitly naming the columns is safer and easier to maintain:
INSERT INTO t1 (id,name1)
VALUES (1,'one');
If a table later gains another column with a default value, an INSERT that explicitly names the columns it uses is less likely to require changes.
The older version of this tutorial used legacy MySQL syntax such as TYPE=MyISAM and integer display widths. A simple modern example can be written as:
CREATE TABLE t1 (
id INT NOT NULL,
name1 VARCHAR(10) NOT NULL,
PRIMARY KEY (id)
);
Now add several rows:
INSERT INTO t1 (id,name1)
VALUES
(1,'one'),
(2,'two'),
(3,'three');
When an ID column uses AUTO_INCREMENT, normally omit that column from the INSERT and let MySQL generate the value.
CREATE TABLE members (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO members (name)
VALUES ('Alex');
MySQL supplies the next generated id. See MySQL AUTO_INCREMENT for details.
If a column allows SQL NULL, insert the SQL keyword NULL without quotes:
INSERT INTO student (name,class,mark,gender)
VALUES ('Sam','Four',NULL,'male');
NULL is different from an empty string such as ''.
If a column has a defined default value, it can be omitted from the column list:
INSERT INTO members (name)
VALUES ('Nina');
or the DEFAULT keyword can be used where appropriate:
INSERT INTO members (name,status)
VALUES ('Nina',DEFAULT);
See SQL NULL values for the difference between NULL and ordinary values.
INSERT can add rows returned by a SELECT query. Explicitly list the destination and source columns so their purpose is clear:
INSERT INTO student2 (id,name,class,mark,gender)
SELECT id,name,class,mark,gender
FROM student;
The selected values must be compatible with the corresponding destination columns.
INSERT INTO student2 SELECT * when maintainability matters. Explicit columns protect the query from accidental column-order differences between the two tables.Add a WHERE condition to copy only selected rows:
INSERT INTO student2 (id,name,class,mark,gender)
SELECT id,name,class,mark,gender
FROM student
WHERE class='Four';
Only students from class Four are returned by SELECT and inserted into student2.
INSERT INTO table1 (column1)
SELECT column1
FROM table2;
For more table-copy examples, see copy data to a new table and copy data to an existing table.
This is invalid because three columns are listed but only two values are supplied:
-- Incorrect
INSERT INTO student (name,class,mark)
VALUES ('Alex','Four');
A value must be compatible with the destination column. For example, a text value should not be inserted into a numeric column when the database cannot convert it appropriately.
If a required NOT NULL column has no default, the INSERT must provide an acceptable value.
An INSERT can fail when it tries to create a value that violates a PRIMARY KEY or UNIQUE constraint.
INSERT INTO t1 (id,name1)
VALUES (1,'another');
If id=1 already exists and id is the primary key, MySQL reports a duplicate-key error.
When INSERT values come from PHP users or forms, do not construct SQL by concatenating raw input. Use a prepared statement in the application layer.
The SQL tutorial explains INSERT itself. When values come from PHP, use a PDO prepared statement with placeholders:
INSERT INTO student (name,class,mark)
VALUES (:name,:class,:mark);
The placeholders are bound to application values by PDO rather than manually placing user input into the SQL string.
PHP PDO INSERT TutorialINSERT adds one or more new rows to a database table.
Yes. Explicit column names make the query easier to maintain and ensure that each value clearly maps to its intended destination column.
Yes. MySQL supports a multi-row VALUES list, with each row enclosed in parentheses and separated by commas.
Normally omit the AUTO_INCREMENT column from the INSERT column list and let MySQL generate the next value.
Use the SQL keyword NULL without quotes, provided the destination column permits NULL.
Yes. INSERT ... SELECT inserts rows returned by a SELECT query. Explicitly listing source and destination columns is recommended.
Use a PDO prepared statement with placeholders rather than concatenating raw application input into the SQL statement.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| sagar | 08-04-2009 |
| i want to simple insert,update/edit, query in php | |
| kiran | 14-12-2009 |
| I want 2 insert record in excelsheet thru oledb jet4.0 bt condiotion is no duplicate records | |
| Regalla Naresh Reddy | 13-01-2010 |
| Hi, I have 2 tables named as PMOCreationTable and FinanceTable. PMOCreationTable has one column named as InstanceID which is unique. I have the same column in the FinanceTable. FinanceTable also have some additional columns. So i want to copy the data of InstanceID column from PMOCreationTable to FinanceTable at the same time i want insert some values in to the FinanceTable. Means at the same time i need to get the one column data from other table and insert some values in to the table along with it. Can you provide me a solution for this. Its very important for me. | |
| smo | 13-01-2010 |
| You can update 2nd table but I don't thik you can use another insert command in one go. | |
| ijah | 08-02-2010 |
| hi, i need help..i want to populate my date table according to the days based on 2006 year calendar. i will start from 1 until 365 days and 1 jan 2006 falls on Sunday. How to insert all the days starts from Sunday (1 jan 2006) to Saturday ( 7 jan 2006) and after that the day will start over again with sunday (8 jan 2006)? this should occur until the last day of 2006 which is 31/12/2006. thanks | |
| smo | 09-02-2010 |
| First you populate one column with incremental date for all 365 days. Then use the dayofweek function to update/add another field with weekdays. There can be a better way of doing this also. | |
| abhishek | 02-06-2010 |
| how to insert values in table multiple times using a single insert command | |
| Mehmood Khan | 08-06-2010 |
| hello sir .. i want to perform a opertion like this when i click show button all records are to be showd and same update ,insert in etc.... in php mysql | |
| binnie | 12-08-2010 |
| How to insert records into 2 or more tables using only one query? | |
| pakoy | 10-02-2011 |
| Hi Sir.. i want to perform operation like this i want to view GROUP BY date and VIEW all under that date | |