F.P
F.P

Reputation: 17831

Skip column when inserting into MySQL

How can I skip a column when inserting a new row into a table without knowing neither the names nor the amount of columns in my table?

Using INSERT (col1, col2) VALUES (1, 2) is not an option, because I can not know the amount of columns during runtime. It's all calculated from user input.

Thus, I need to skip the first column (id, PRIMARY KEY auto_increment) when inserting.

Upvotes: 2

Views: 14618

Answers (3)

stephen
stephen

Reputation: 1

You can just input in a one liner like this:

The first column is parentID so I'm skipping that and going straight to student:

INSERT into Lib_Book(guardian, studentFN, studentLN, year) VALUES ('Father','Joe', 'Gitter','2019');

Upvotes: 0

Nesim Razon
Nesim Razon

Reputation: 9794

You can insert without giving column names, but you had to give some values for all columns.

INSERT INTO comments 
VALUES (null, 2, 3, 4,null,6) 

CREATE TABLE IF NOT EXISTS `comments` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `imageid` int(10) unsigned NOT NULL DEFAULT '0',
  `uid` bigint(20) unsigned NOT NULL DEFAULT '0',
  `content` text CHARACTER SET utf8,
  `adate` datetime DEFAULT NULL,
  `ip` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`),
   KEY `ids` (`imageid`,`adate`) USING BTREE
) ENGINE=InnoDB  DEFAULT CHARSET=latin1  ;

Upvotes: 4

barsju
barsju

Reputation: 4446

Try inserting 0 as the first value. If the column is auto-incremented it should work.

From MySQL reference: "No value was specified for the AUTO_INCREMENT column, so MySQL assigned sequence numbers automatically. You can also explicitly assign NULL or 0 to the column to generate sequence numbers. " http://dev.mysql.com/doc/refman/5.5/en/example-auto-increment.html

Upvotes: 3

Related Questions