frgdgdfg dfgdgdgd frgdgdfg dfgdgdgd - 6 months ago 7
SQL Question

Insert values without mentioning the column name

So i have tried inserting the values without mentioning the column name in the code..

After some searching, I have found that it is possible by passing the value as null as mentioned here.But when I did it, it threw some errors.

The code i have used

CREATE TABLE Persons
(
ID int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (ID)
);

insert into Persons values (NULL,1);


So how canI add the values without mentioning the column name?

Thanx in advance

Answer

If this is just for experimental purposes yes you can:

INSERT INTO Persons VALUES (NULL, 'Lastname', 'Firstname', 'Address test', 'city of angels');

But I strongly urge you to not make it a habit of doing this. Always add columns, as eloquently stated by David's answer.

INSERT INTO Persons (LastName, FirstName, Address, City) VALUES ('Last', 'First', 'Add', 'City');

Fiddle

Comments