user4903
user4903

Reputation:

What is the best way to insert into and update a single row table in MySQL?

I have a MySQL table that will only have one row. What should my statement be for the first time I insert to this row, and for subsequent updates? I tried an insert where the primary key equals 1, but this doesn't account for the first time around when no row exists yet.

Upvotes: 3

Views: 1277

Answers (2)

Greg Hewgill
Greg Hewgill

Reputation: 993035

If your table will only ever have one row, you might consider preloading initial data into the row in your database creation script. Then your code will only ever need to issue an UPDATE statement. Also, you will not need a primary key column because there is only ever one row. You can then issue UPDATE statements without needing a WHERE clause, too.

Upvotes: 9

SoapBox
SoapBox

Reputation: 20609

INSERT INTO table(col1,col2,col3) VALUES(val1,val2,val3) ON DUPLICATE KEY UPDATE col1 = val1, col2 = val2, col3 = val3;

Upvotes: 8

Related Questions