0

How to insert a default value in all columns after inserting a value in a single column,

Example: I created a product table and the columns are id (autoincrement), product_name, and item.

CREATE TABLE product ( id int AUTOINCREMENT, product_name varchar(255), item int ); 

How can I insert only the product_name and have the item automatically populated with the value 30?

insert into product values ( 'burger' ) 

and have the result be

id: product_name: item: 7 burger 30 
6
  • 1
    Dup of stackoverflow.com/questions/11801911/…. Please use the search feature before asking questions. This is easily found on Stack Overflow, and the rest of the internet. Commented Sep 18, 2013 at 5:14
  • 2
    @JonathonReinhart It's not a duplicate of that at all. He isn't asking to insert multiple rows. Commented Sep 18, 2013 at 8:04
  • @JonathonReinhart I agree with Fluffeh; I don't see how that's a duplicate of this.' Commented Sep 18, 2013 at 15:23
  • Okay, but it shows how to insert into a single column. Wasn't that the point? Commented Sep 18, 2013 at 20:30
  • @Jonathon Reinhart, my point here is after you created a table, if i insert in only single row, the other rows should have a default value. Commented Sep 19, 2013 at 1:46

4 Answers 4

2

Set Default for item:

CREATE TABLE product ( id int AUTOINCREMENT, product_name varchar(255), item int DEFAULT 30); 
Sign up to request clarification or add additional context in comments.

2 Comments

so using the DEFAULT function will make the value of item as 30 each time i insert a new product name?
Exactly if you don't set a value for this column the default value will be inserted
0
CREATE TABLE product ( id int AUTOINCREMENT, product_name varchar(255), item int ); 

in mssql server use the following query:

ALTER TABLE product ADD item int CONSTRAINT DF_TestTable_NewCol DEFAULT 30 not null GO 

1 Comment

The tag however specifically asks for MySQL :(
0

If your table has default or increment values, you can use either of the following syntax to insert the data and have it automatically fill the rest:

CREATE TABLE product ( id int AUTOINCREMENT, product_name varchar(255), item int ); 

You have an automatic value for ID, but no defaults set for either of the other two:

insert into product (product_name, item) values ('Burger', 30); 

OR

insert into product values(null, 'Burger', 30) 

Now, if you set your table to have default columns like @cha suggested, you can enter in even less info:

CREATE TABLE product ( id int AUTOINCREMENT, product_name varchar(255), item int default 30 ); 

Allows you to:

insert into product (product_name) values ('Burger'); 

OR

insert into product values(null, 'Burger', null) 

Comments

0

Via phpMyAdmin, go to Structure for your table, click on Change for the item column. You can set the default value there by picking "As defined:" and entering the value.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.