SQL INSERT Command in MySQL

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.

Recommended: include the column list in INSERT statements. This makes the query easier to read and less dependent on the physical column order of the table.

SQL INSERT Syntax Top ↑

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);

Insert One Row Top ↑

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.

Check the inserted row Top ↑

SELECT id,name,class,mark,gender
FROM student
WHERE name='Maria';

Use the SELECT tutorial and WHERE tutorial for retrieving and filtering records.

Insert Multiple Rows with One Statement Top ↑

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.

Why Specify Column Names? Top ↑

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.

Modern sample table Top ↑

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');

INSERT with an AUTO_INCREMENT Column Top ↑

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.

Insert NULL and DEFAULT Values Top ↑

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.

Copy Rows with INSERT ... SELECT Top ↑

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.

Avoid INSERT INTO student2 SELECT * when maintainability matters. Explicit columns protect the query from accidental column-order differences between the two tables.

Copy Only Matching Rows Top ↑

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.

Copy one column between tables Top ↑

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.

Common SQL INSERT Errors Top ↑

Column and value counts do not match Top ↑

This is invalid because three columns are listed but only two values are supplied:

-- Incorrect
INSERT INTO student (name,class,mark)
VALUES ('Alex','Four');

Wrong datatype Top ↑

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.

NOT NULL column has no value Top ↑

If a required NOT NULL column has no default, the INSERT must provide an acceptable value.

Duplicate key value Top ↑

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.

Unescaped application input Top ↑

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.

INSERT Records from PHP PDO Top ↑

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 Tutorial SQL WHERE SQL UPDATE INSERT using SET

AUTO_INCREMENT Copy to New Table Copy to Existing Table

Frequently Asked Questions Top ↑

Q1: What does SQL INSERT do?

INSERT adds one or more new rows to a database table.

Q2: Should I specify column names in an INSERT statement?

Yes. Explicit column names make the query easier to maintain and ensure that each value clearly maps to its intended destination column.

Q3: Can one INSERT statement add several rows?

Yes. MySQL supports a multi-row VALUES list, with each row enclosed in parentheses and separated by commas.

Q4: How do I insert a value into an AUTO_INCREMENT column?

Normally omit the AUTO_INCREMENT column from the INSERT column list and let MySQL generate the next value.

Q5: How do I insert SQL NULL?

Use the SQL keyword NULL without quotes, provided the destination column permits NULL.

Q6: Can INSERT copy records from another table?

Yes. INSERT ... SELECT inserts rows returned by a SELECT query. Explicitly listing source and destination columns is recommended.

Q7: How should PHP form values be inserted safely?

Use a PDO prepared statement with placeholders rather than concatenating raw application input into the SQL statement.



SQL WHERE SQL UPDATE


Subscribe to our YouTube Channel here



plus2net.com
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




SQL Video Tutorials










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer