简体   繁体   English

如何获得另一个表的外键引用的主键?

[英]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有两个表useruserprofile ,并且在我的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_IDuserprofile模式。 That way, you won't accidentally add duplicate records. 这样,您就不会意外添加重复记录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将主键作为外键插入另一个表? - How do I insert the primary key to another table as foreign key? 如何将数据插入一个表并获取主键作为外键插入另一个表中? - How do I insert data into one table & get the primary key to insert in another table as a foreign key? 如何将主键放入另一个表的外键中? PHP,MySQL - How Do I Grab And Put Primary Key Into Foreign Key In Another Table? PHP, MySQL SQL PHP如何基于另一个表的主键更新外键? - SQL PHP How do I update a foreign key based on the primary key of another table? 如何从两个表中查询匹配表中的外键和另一个表中的主键 - How do I query from two tables matching the foreign key from table with the primary key from another 如何同时将外键设置为与另一个表的主键相同? - How do I set the foreign key same as primary key of another table concurrently? 如何从另一个表主键添加mysql外键? - How to Add mysql foreign key from another table primary key? Laravel:如何从数据透视表中获取其外键未引用主键的记录? - Laravel : How do I get records from pivot table whereby its foreign key does not reference a primary key? 如何使用其他表中的外键删除具有主键的行? - How do I delete row with primary key using foreign key from other table? 如何从其他表(其中主键是外键)添加动态文本到所显示的动态表? - How can I add dynamic text from a different table where primary key is a foreign key to adynamic table being shown?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM