[英]How do I get the primary key being referenced by a foreign key of another table?
I have two tables user
and userprofile
in MySQL
and I have a php script on my web server that handles the insertion of data into these tables. 我在
MySQL
有两个表user
和userprofile
,并且在我的Web服务器上有一个php脚本,用于处理将数据插入这些表中。
Here are the SQL commands used to define the tables. 这是用于定义表的SQL命令。
CREATE TABLE user (
USER_ID INT NOT NULL AUTO_INCREMENT,
USERNAME VARCHAR(20),
USER_PASSWORD VARCHAR(255),
PRIMARY KEY (USER_ID)
);
CREATE TABLE userprofile (
USER_ID INT NOT NULL,
FULL_NAME VARCHAR(255),
AGE INT,
FOREIGN KEY (USER_ID) REFERENCES user (USER_ID)
);
I am able to successfully write to the database table using PHP
for the user
table with the following SQL
command "INSERT INTO user (USERNAME, USER_PASSWORD) VALUES ('$username', '$hashedPassword')"
which is executed inside a PHP
function that I have defined. 我可以使用
PHP
函数内部执行的以下SQL
命令"INSERT INTO user (USERNAME, USER_PASSWORD) VALUES ('$username', '$hashedPassword')"
将PHP
user
表成功写入数据库表。我定义的
The problem I am experiencing now is let's say that I need to add information such as the FULL_NAME
, and AGE
of the user. 我现在遇到的问题是,我需要添加诸如
FULL_NAME
和用户的AGE
之类的信息。 Since I am allowing the user to include this information later on (they are optional because of user privacy). 由于我允许用户稍后再包含此信息(由于用户隐私,它们是可选的)。 How exactly would I populate the database table
userprofile
without having the USER_ID
? 在没有
USER_ID
情况下,如何准确填充数据库表userprofile
?
I have looked over the documentation for creating database tables and I believe that my definitions are correct. 我查看了用于创建数据库表的文档,并且相信我的定义是正确的。 My initial thought was to implement a function to get the
USER_ID
of the current user from the database, and then use that to insert their data into the userprofile
table. 我最初的想法是实现一个从数据库中获取当前用户的
USER_ID
的功能,然后使用该功能将其数据插入到userprofile
表中。 The expected behavior I wanted was after adding the user with the SQL
command above, the userprofile
would also have a row created since it is linked by a foreign key USER_ID
. 预期的行为,我想要的是增加与用户之后
SQL
上面的命令,在userprofile
也将有一行创建,因为它是由一个外键链接USER_ID
。 I am not really sure how to guarantee that these two tables are synchronized with each other. 我不太确定如何保证这两个表彼此同步。
To store the name and age of your user you must have their USER_ID
. 要存储用户的姓名和年龄,您必须具有其
USER_ID
。 This is because you are using USER_ID
as the unique identifier for your users and it is a foreign key in the userprofile
table. 这是因为您使用
USER_ID
作为用户的唯一标识符,并且它是userprofile
表中的外键。
Typically when a user "logs in" - whatever that means for your website - you will determine their USER_ID
and use it as a session variable (eg. in $_SESSION
) so that you make use of it on subsequent pages (eg. the page where they edit their profile). 通常,当用户“登录”(对您的网站意味着什么)时,您将确定其
USER_ID
并将其用作会话变量(例如,在$_SESSION
),以便在后续页面(例如,页面)中使用它。他们在哪里编辑个人资料。
Regarding synchronisation: based on your definitions you do not need to add a record to userprofile
at the same time as user
. 关于同步:根据您的定义,您无需与
user
同时将记录添加到userprofile
中。 You can, but it would be effectively empty, as you do not know their name or age yet. 您可以,但是实际上是空的,因为您尚不知道他们的姓名或年龄。
That said, you also need to check whether their userprofile
already exists. 这就是说,你还需要检查他们的是否
userprofile
已经存在。 That is, are you updating their profile or adding to it. 也就是说,您是要更新他们的个人资料还是将其添加到其中。 So it would be simpler to create their user profile record when you create their user record.
因此,在创建他们的用户记录时,创建他们的用户配置文件记录会更加简单。 That way, you know you are always updating.
这样,您就知道自己一直在更新。
You should add a UNIQUE
constraint to USER_ID
in the userprofile
schema. 您应该添加
UNIQUE
约束USER_ID
在userprofile
模式。 That way, you won't accidentally add duplicate records. 这样,您就不会意外添加重复记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.