database - Mysql assigning default causes error -


i trying run below query create table in mysql , getting error.

create table newtable (         version_id int(11) auto_increment not null,         test_id int(11)   default version_id,         primary key(version_id)   );  error 1064 (42000): have error in sql syntax; check manual corresponds mysql server version right syntax use near 'version_id not null, primary key(version_id), unique key(test_id) )' @ line 1 

i getting above error.

i think problem setting test_id default version_id, because works otherwise.

thank you
bala

-- update 

here wanted do,

when create new row, want use version_id key. when update, want use value of existing record key. note test_id not primary key.

i don't think can use "variables" defaults. have constants. if want effect, in stored procedure / trigger "before insert" such if trial_id not supplied gets assigned same value version_id...

here's a tutorial might toward end.

i think trigger like:

mysql> create trigger mytrigger     -> before insert on newtable     -> each row     -> begin     ->      if new.trial_id null     ->         set new.trial_id = new.version_id;     ->      end if;     -> end$$ 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -