insert查询不适用于具有外键的行

时间:2014-05-06 08:54:27

标签: mysql sql database

我正在尝试学习外键约束到目前为止我已经能够在mysql中创建外键

这是我对三个表的创建表查询:

create table customer(
CustId int(100) not null AUTO_INCREMENT primary key,
FirstName varchar(300) default null,
LastName varchar(300) default null,
Gender varchar(200) default null,
Category varchar(200) default null,
DateOfBirth varchar(200) default null,
Age int(3)default null
);

create table address(
Address_Id int(100) not null AUTO_INCREMENT primary key,
Address varchar(1000) default null,
Country varchar(500) default null,
State varchar (500) default null,
city varchar(500)default null,
PinCode int(10)default null,
CustId int(100) not null,
foreign key(CustId) references customer(CustId)
);

create table contact(
Contact_Id int(100) not null AUTO_INCREMENT primary key,
EmailId varchar(500)default null,
ContactNo varchar(20) default null,
MobileNo varchar(20) default null,
CustId int(100) not null,
Address_Id int(100) not null,
foreign key(CustId) references customer(CustId),
foreign key(Address_Id) references address(Address_Id)
);

K现在我知道了,直到这里:

START TRANSACTION;
SET @lid := null;

insert into customer (FirstName,LastName,Gender,Category,DateOfBirth,Age)values('Ashok','sharma','Male','Affiliate','1988-04-17','26');
SELECT LAST_INSERT_ID() INTO @lid; 
insert into address(CustId, Address,Country,State,city,Pincode)values (@lid, 'No.1645','India','Karnataka','Bangalore','560060');
COMMIT;

使用事务和提交是解决方案,但如何为联系人表格执行此操作,我有太多的外键。我需要获得两个自动递增的值。

请指导我。

1 个答案:

答案 0 :(得分:1)

您可以在多个语句中执行此操作:

START TRANSACTION;
SET @lid := null;

insert into customer (FirstName,LastName,Gender,Category,DateOfBirth,Age)values('Ashok','sharma','Male','Affiliate','1988-04-17','26');
SELECT LAST_INSERT_ID() INTO @lid; 
insert into address(CustId, Address,Country,State,city,Pincode)values (@lid, 'No.1645','India','Karnataka','Bangalore','560060');
COMMIT;
  

对于MySQL 5.1.12及更高版本,没有参数,LAST_INSERT_ID()返回一个64位值,表示由于最近执行的INSERT语句而成功为AUTO_INCREMENT列插入的第一个自动生成的值。

这是会话绑定的,所以不要担心,另一个会话会弄乱你的last_insert_id或其他东西。阅读更多相关信息here

你最好把它全部放在像我这样的交易中。这可以确保所有语句都成功或不成功,而不仅仅是其中的一部分。你必须使用InnoDB。 MyISAM不支持此功能。或者你忍受风险:)但是因为你使用外键我假设你使用InnoDB,只是想提及它的完整性。

我使用的变量当然可以用PHP变量替换。或者你这样做:

START TRANSACTION;

insert into customer (FirstName,LastName,Gender,Category,DateOfBirth,Age)values('Ashok','sharma','Male','Affiliate','1988-04-17','26');
SELECT LAST_INSERT_ID() INTO @lid; 
insert into address(CustId, Address,Country,State,city,Pincode)
SELECT @lid, 'No.1645','India','Karnataka','Bangalore','560060';
COMMIT;

修改

START TRANSACTION;
SET @lid_c := null;
SET @lid_a := null;

insert into customer (FirstName,LastName,Gender,Category,DateOfBirth,Age)values('Ashok','sharma','Male','Affiliate','1988-04-17','26');
SELECT LAST_INSERT_ID() INTO @lid_c; 
insert into address(CustId, Address,Country,State,city,Pincode)values (@lid, 'No.1645','India','Karnataka','Bangalore','560060');

SELECT LAST_INSERT_ID() INTO @lid_a; 
INSERT INTO contact (CustId, Address_Id, another_column) VALUES
(@lid_c, @lid_a, 'another_value');

COMMIT;