gpt4 book ai didi

mysql - 插入查询不适用于具有外键的行

转载 作者:行者123 更新时间:2023-11-29 12:59:58 25 4
gpt4 key购买 nike

我正在尝试学习外键约束,到目前为止我已经能够在 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)
);

现在我明白了:

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;

使用事务和提交是解决方案,但如何处理外键过多的联系人表。我需要获得两个自动递增的值。

请在这方面指导我。

最佳答案

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

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;

For MySQL 5.1.12 and later, with no argument, LAST_INSERT_ID() returns a 64-bit value representing the first automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement.

这是 session 绑定(bind)的,所以不用担心,另一个 session 会弄乱你的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;

关于mysql - 插入查询不适用于具有外键的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23490128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com