Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql:模拟插入_Mysql_Insert - Fatal编程技术网

Mysql:模拟插入

Mysql:模拟插入,mysql,insert,Mysql,Insert,我在MySQL数据库中使用外键和主键时遇到问题 现在我有两张表p1,p2 create table p1( id int(10) not null auto_increment, type_id int(10) default 1, name varchar(20), primary key(id, type_id) ); create table p2( id int(10) not null, type_id int(10) n

我在MySQL数据库中使用外键和主键时遇到问题

现在我有两张表p1,p2

create table p1(
    id int(10) not null auto_increment, 
    type_id int(10) default 1, 
    name varchar(20), 
    primary key(id, type_id)
    );
create table p2(
    id int(10) not null, 
    type_id int(10) not null, 
    name varchar(20), 
    primary key(id, type_id)
    );
alter table p1 add foreign key(id, type_id) references p2(id, type_id) on delete cascade;
当我将值插入p2时,我希望p1用值id更新,键入插入p1中的\u id

insert into p2(name) values('p2');
set @temp = 0;
select last_insert_id() into @temp;
insert into p1(id, type_id, name) values(@temp, name);

我怎么能这样做

使用MyISAM表格-noway。插入将始终按顺序进行。使用InnoDb-使用线程执行查询

或者您的意思是您需要在事务中这样做?

您可以在p2和p3上设置


此外,您的insert查询已中断。您指定了三列,但只指定了两个值。

我尝试使用事务,但失败。你能查一下哪里错了吗?@jasonbar,因为我不知道如何将这三个值插入表p2中。