I have two database old.sql
& new.sql
.
I have user table named user
in new.sql
. and users
in old.sql
.
table structure for user
from new.sql
--
-- Table structure for table `user`
--
DROP TABLE IF EXISTS `user`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT,
`email` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`password` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`firstName` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`lastName` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`phoneNumber` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`role` enum('USER','ADMIN') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'USER',
`createdOn` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY (`id`),
UNIQUE KEY `User_email_key` (`email`),
UNIQUE KEY `User_phoneNumber_key` (`phoneNumber`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
and users
from old.sql
DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT,
`roleId` int NOT NULL,
`firstName` varchar(255) DEFAULT NULL,
`lastName` varchar(255) DEFAULT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) DEFAULT NULL,
`status` tinyint(1) DEFAULT '1',
`resetPasswordExpires` datetime DEFAULT NULL,
`resetPasswordToken` varchar(255) DEFAULT NULL,
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
KEY `roleId` (`roleId`),
CONSTRAINT `Users_ibfk_1` FOREIGN KEY (`roleId`) REFERENCES `roles` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=884 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
now I want to migrate user from old database to new database. this is only one table, we have many tables to migrate data.
how can I achieve that ?
also we have done normalization in new database like
old.sql
one table separated in 2 or 3 new tables in new database.
These DBs are on the same MySQL instance.
CodePudding user response:
You can use a simple script like:
INSERT INTO
user (id, email, password, firstName, lastName, phoneNumber, role, createdOn)
SELECT
id, email, password, firstName, lastName, null, IF(roleId = 1, 'ADMIN', 'USER'), created
FROM users;
Just set correct roleId for condition and remove id if you need a new one.
CodePudding user response:
Tiny example of the data transferring realization with some remarks: DEMO fiddle