我尝试在同一个查询的多个表中搜索<code>insert信息,但发现这是不可能的?所以我想通过简单地使用多个查询来 insert 它,即;
I tried searching a way to insert information in multiple tables in the same query, but found out it's impossible?
So I want to insert it by simply using multiple queries i.e;
INSERT INTO users (username, password) VALUES('test', 'test')
INSERT INTO profiles (userid, bio, homepage) VALUES('[id of the user here?]','Hello world!', 'http://www.stackoverflow.com')
但是如何将 users 的自动增量 id 提供给 profile<的手动"userid/code> 表?
But how can I give the auto-increment id from the users to the "manual" userid for the profile table?
不,您不能在一个 MySQL 命令中插入多个表.但是,您可以使用事务.
No, you can't insert into multiple tables in one MySQL command. You can however use transactions.
BEGIN;
INSERT INTO users (username, password)
VALUES('test', 'test');
INSERT INTO profiles (userid, bio, homepage)
VALUES(LAST_INSERT_ID(),'Hello world!', 'http://www.stackoverflow.com');
COMMIT;
看看LAST_INSERT_ID() 重用自增值.
Have a look at LAST_INSERT_ID() to reuse autoincrement values.
你说经过这么长时间的尝试,它仍然不起作用.我不能简单地将刚刚生成的 ID 放在 $var 中并将该 $var 放在所有 MySQL 命令中吗?"
让我详细说明:这里有 3 种可能的方法:
Let me elaborate: there are 3 possible ways here:
在上面的代码中.这个这一切都在 MySQL 中完成,并且LAST_INSERT_ID() 在第二个语句将自动成为自动增量列的值插入第一个声明.
In the code you see above. This
does it all in MySQL, and the
LAST_INSERT_ID() in the second
statement will automatically be the
value of the autoincrement-column
that was inserted in the first
statement.
不幸的是,当第二条语句本身在具有自动递增列的表中插入行时,LAST_INSERT_ID() 将更新为表 2 的,而不是表 1.如果您仍然之后需要表 1 的内容,我们将不得不将其存储在一个变量中.这将我们引向方式 2 和方式 3:
Unfortunately, when the second statement itself inserts rows in a table with an auto-increment column, the LAST_INSERT_ID() will be updated to that of table 2, and not table 1. If you still need that of table 1 afterwards, we will have to store it in a variable. This leads us to ways 2 and 3:
将把 LAST_INSERT_ID() 存入一个 MySQL 变量:
Will stock the LAST_INSERT_ID() in
a MySQL variable:
INSERT ...
SELECT LAST_INSERT_ID() INTO @mysql_variable_here;
INSERT INTO table2 (@mysql_variable_here, ...);
INSERT INTO table3 (@mysql_variable_here, ...);
将把 LAST_INSERT_ID() 存入一个php 变量(或任何语言可以连接到您的数据库选择):
Will stock the LAST_INSERT_ID() in a
php variable (or any language that
can connect to a database, of your
choice):
插入...LAST_INSERT_ID(),方法是在 MySQL 中执行该文字语句,或者使用例如 php 的 mysql_insert_id() 为您执行此操作INSERT [在此处使用您的 php 变量]INSERT ...LAST_INSERT_ID(), either by executing that literal statement in MySQL, or using for example php's mysql_insert_id() which does that for youINSERT [use your php variable here]无论您选择哪种解决方法,您都必须决定应该发生什么执行在查询之间中断(例如,您的数据库服务器崩溃).如果您可以忍受有些已经完成,有些还没有",请不要继续阅读.
Whatever way of solving this you choose, you must decide what should happen should the execution be interrupted between queries (for example, your database-server crashes). If you can live with "some have finished, others not", don't read on.
但是,如果您决定要么完成所有查询,要么不完成 - 我不希望某些表中的行但其他表中没有匹配的行,我总是希望我的数据库表保持一致",您需要包装所有交易中的语句.这就是我在这里使用 BEGIN 和 COMMIT 的原因.
If however, you decide "either all queries finish, or none finish - I do not want rows in some tables but no matching rows in others, I always want my database tables to be consistent", you need to wrap all statements in a transaction. That's why I used the BEGIN and COMMIT here.
这篇关于MySQL插入多个表?(数据库规范化?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何有效地使用窗口函数根据 N 个先前值来决定How to use windowing functions efficiently to decide next N number of rows based on N number of previous values(如何有效地使用窗口函数根据
在“GROUP BY"中重用选择表达式的结果;条款reuse the result of a select expression in the quot;GROUP BYquot; clause?(在“GROUP BY中重用选择表达式的结果;条款?)
Pyspark DataFrameWriter jdbc 函数的 ignore 选项是忽略整Does ignore option of Pyspark DataFrameWriter jdbc function ignore entire transaction or just offending rows?(Pyspark DataFrameWriter jdbc 函数的 ig
使用 INSERT INTO table ON DUPLICATE KEY 时出错,使用 Error while using INSERT INTO table ON DUPLICATE KEY, using a for loop array(使用 INSERT INTO table ON DUPLICATE KEY 时出错,使用 for 循环数组
pyspark mysql jdbc load 调用 o23.load 时发生错误 没有合pyspark mysql jdbc load An error occurred while calling o23.load No suitable driver(pyspark mysql jdbc load 调用 o23.load 时发生错误 没有合适的
如何将 Apache Spark 与 MySQL 集成以将数据库表作为How to integrate Apache Spark with MySQL for reading database tables as a spark dataframe?(如何将 Apache Spark 与 MySQL 集成以将数据库表作为