Extract data from another table in MYSQL database, making changes to existing columns
P粉143640496
P粉143640496 2023-09-10 08:45:15
0
1
515

I have a MYSQL database (in PHPMyAdmin) with two tablesusersandposts. Both tables have ausernamecolumn. I want to modify theusernamecolumn in thepoststable so that it extracts the username data from theuserstable, that is, thepoststable The data is automatically updated from theuserstable and is referenced during any updates to theuserstable.

I initially thought I could use foreign keys to achieve this functionality, but if I understand correctly, foreign keys are only associated with the primary key in the parent table, right?

I get an error saying the following syntax is incorrect, although it doesn't give any hints/solutions:

ALTER TABLE posts MODIFY COLUMN username VARCHAR(55) NOT NULL REFERENCES users(username) ON UPDATE CASCADE

How to modify an existing column so that it references/uses data from a column in a different table in the database?

Theusernamecolumns in the two tables have the same type, size and attributes, that is,VARCHAR(55) NOT NULL, and the innoDB storage engine is used.

P粉143640496
P粉143640496

reply all (1)
P粉501683874

Foreign keys are a data integrity check, nothing more. It ensures that the fields in the child table contain values that appear in the referenced fields in the parent table. That's it*.

You cannot use foreign keys to merge data from one table into another table. However, theJOINstatement does exactly what you need, and used with the foreign key ensures that each post has a valid username to identify the correct user data.

Take this example:

User data

CREATE TABLE `userdata` ( `id` int NOT NULL AUTO_INCREMENT, `username` varchar(45) NOT NULL, `displayName` varchar(45) NOT NULL, `email` varchar(45) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id_UNIQUE` (`id`), UNIQUE KEY `username_UNIQUE` (`username`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;

Post

CREATE TABLE `posts` ( `id` int NOT NULL AUTO_INCREMENT, `username` varchar(45) NOT NULL, `message` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id_UNIQUE` (`id`), KEY `fk_posts_userdata_idx` (`username`), CONSTRAINT `fk_posts_userdata` FOREIGN KEY (`username`) REFERENCES `userdata` (`username`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;

We canJOINtheuserdatatable with thepoststable using the following query:

select `posts`.`id` AS `postId`, `posts`.`username` AS `username`, `posts`.`message` AS `message`, `userdata`.`id` AS `userId`, `userdata`.`displayName` AS `displayName`, `userdata`.`email` AS `email` from (`posts` join `userdata` on(`userdata`.`username` = `posts`.`username`));

You can further create aVIEWbased on this query to return data:

CREATE VIEW `posts_users` AS select `posts`.`id` AS `postId`, `posts`.`username` AS `username`, `posts`.`message` AS `message`, `userdata`.`id` AS `userId`, `userdata`.`displayName` AS `displayName`, `userdata`.`email` AS `email` from (`posts` join `userdata` on(`userdata`.`username` = `posts`.`username`));

Use theSELECTstatement to query the view:

select * from posts_users order by postId

Demo:https://www.db-fiddle.com/f/tbBXvthBtwH7CKu1yjzPjQ/0

*Foreign keys also allow updates and deletes from the parent table to be cascaded to the child tables, but this is beyond the scope of this article.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!