Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql (5.1) insert syntax > col_name=value?

Is there a way in mysql to insert a new row in a way that more directly associates a value with its column (rather than table(col_name) values(value))? When inserting a lot of values at one time, listing them in-line gets rather confusing and leads to errors/mistakes.

I'm looking for something more like UPDATE's syntax of SET col_name='value'.

I see in the mysql doc for INSERT there is the following:

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    SET col_name={expr | DEFAULT}, ...
    [ ON DUPLICATE KEY UPDATE
      col_name=expr
        [, col_name=expr] ... ]

but that is just for duplicates :/

like image 204
Jakob Jingleheimer Avatar asked Sep 15 '11 17:09

Jakob Jingleheimer


People also ask

What is the syntax for inserting data in MySQL?

In syntax, First, you must specify the name of the table. After that, in parenthesis, you must specify the column name of the table, and columns must be separated by a comma. The values that you want to insert must be inside the parenthesis, and it must be followed by the VALUES clause.

How do I insert data into a specific row in MySQL?

When inserting a single row into the MySQL table, the syntax is as follows: INSERT INTO table_name(column_1,column_2,column_3) VALUES (value_1,value_2,value_3); In the INSERT INTO query, you should specify the following information: table_name : A MySQL table to which you want to add a new row.

How can we insert data into a value?

INSERT INTO Syntax 1. Specify both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3, ...)


1 Answers

In the MySQL docs, [text] means "text is optional". So this is perfectly valid:

INSERT INTO table
SET col1='value1', col2='value2'...
like image 163
CanSpice Avatar answered Sep 21 '22 12:09

CanSpice