表1:id,name,age....
表2:id ,user_id,.....
表1的id和表2的id是自动生成的,表1的id与表2的id是关联的。
现在我想用一个语句同时向表1,表2插入内容,表2的user_id是表1的id。
我想这样做是因为怕两个表的数据插入不同步,所以想同步,这样表1不能插入的时候表2也不能插入,表2插入出错,表1也不能插入。。不知道你们明不明白。
表2:id ,user_id,.....
表1的id和表2的id是自动生成的,表1的id与表2的id是关联的。
现在我想用一个语句同时向表1,表2插入内容,表2的user_id是表1的id。
我想这样做是因为怕两个表的数据插入不同步,所以想同步,这样表1不能插入的时候表2也不能插入,表2插入出错,表1也不能插入。。不知道你们明不明白。
亲,我建了外键,求sql语句。。
declare exit handler for sqlwarning rollback;
START TRANSACTION;
insert into table1(a,b,c,d) values(var1,var2,var3,var4);
insert into table2(e,f,g) values(var5,var6,string1);
COMMIT;
表1 id(主键,自增),editer_id,title,context
表2 id(主键,自增),apply_id(外键,关联表1的主键,非自增),apply_type,applyer_idINSERT INTO t_editarticle (editer_id,article_id,title,context) VALUES(1,1,'ww','ee') DECLARE @ID INT SELECT @ID = SCOPE_IDENTITY() INSERT INTO t_check (apply_id,apply_type,applyer_id) VALUES(@ID,1,1)网上这样教,可是我试了一下,报错的:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE @ID INT SELECT @ID = SCOPE_IDENTITY() INSERT INTO t_check (apply_id,appl' at line 1
INSERT INTO t_check (apply_id,apply_type,applyer_id) VALUES(LAST_INSERT_ID(),1,1)