insert - MySQL - Creating records in table 2 on creation in table 1 -


say have 2 tables, ballplayers , stats in database.

when add ballplayers record (add new team) there way create new entry player in stats table well?

    ballplayers                           stats  id, playername, number          id, totalpts, totalrebounds 

(they referenced using id column in each table.)

so this... add ballplayers

    ballplayers  id, playername, number   1    nick        22 

how can mysql create new entry in stats triggered on entry creation in ballplayers given id , having defaults of totalpts , totalrebounds set 0?

    stats  id, totalpts, totalrebounds   1     0           0 

is bad db design? possible without having write 2 queries? (insert ballplayers; & insert stats;)any appreciated.

if chose go route of trigger, automatically add row second table, using following code:

create trigger trig_ballplayers_after_insert after insert on ballplayers each row insert ignore stats values(new.id, 0, 0); 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -