Jump to content

Problem with Inserting data in tables


anser316

Recommended Posts

Hi, I have a problem with inserting data into my 'total_stock' table. I want to calculate 'total_stock' by adding all 'stock'. I have been trying to insert data in 'stocks' table, within inserting data in 'total_stock' as i cant add into stock table first as i need the foreign which i havent created.

 

 

create table total_stock(
t_id                 smallint(2) unsigned not null,
total_stock    float not null,
constraint total_pk primary key (t_id));

create table stocks
s_id      smallint(2) unsigned not null,
stock    int not null,
t_id      smallint(2) unsigned not null,
constraint stocks_pk primary key (s_id)
constraint stocks_tid_fk foreign key (t_id)
REFERENCES total_stock(t_id));

Link to comment
Share on other sites

insert into table_stock (t_id) values (1);

insert into stocks (s_id,stock,t_id) values (1,10,1);
insert into stocks (s_id,stock,t_id) values (2,20,1);

UPDATE table_stock SET total_stock=SUM(stock)
WHERE t_id=1

 

I dont know how to sum the stock and store it properly

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.