Last active
June 7, 2019 05:29
-
-
Save jocoonopa/b9c86e86ce10efce2103cd7c232ac860 to your computer and use it in GitHub Desktop.
資策會 dba 需求
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP TABLE IF EXISTS `dba_used_contacts`; | |
CREATE TABLE `dba_used_contacts` ( | |
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, | |
/* Contact */ | |
`contacts_membership_no` VARCHAR(191) NOT NULL, | |
`contacts_first_name` VARCHAR(191) NOT NULL, | |
/* Contact.emailContacts <EmailContact> */ | |
`email_contacts_address` VARCHAR(191) NOT NULL, | |
/* Contact.profile.phoneNumbers <PhoneNumber>(First) */ | |
`phone_numbers_number` VARCHAR(191) NOT NULL, | |
`created_at` timestamp DEFAULT CURRENT_TIMESTAMP, | |
`updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | |
PRIMARY KEY (`id`), | |
UNIQUE KEY `dba_used_contacts_contacts_membership_no_unique` (`contacts_membership_no`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP TRIGGER IF EXISTS dba_used_contacts_created; | |
DELIMITER $$ | |
CREATE TRIGGER dba_used_contacts_created AFTER INSERT | |
ON dba_used_contacts | |
FOR EACH ROW | |
BEGIN | |
CALL dba_used_contacts_created_callback( | |
NEW.id, | |
NEW.contacts_membership_no, | |
NEW.contacts_first_name, | |
NEW.email_contacts_address, | |
NEW.phone_numbers_number | |
); | |
END $$ | |
DELIMITER ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP PROCEDURE IF EXISTS dba_used_contacts_created_callback; | |
DELIMITER $$ | |
CREATE PROCEDURE dba_used_contacts_created_callback( | |
IN dba_user_contacts_id INT(10), | |
IN contacts_membership_no VARCHAR(191), | |
IN contacts_first_name VARCHAR(191), | |
IN email_contacts_address VARCHAR(191), | |
IN phone_numbers_number VARCHAR(191) | |
) | |
BEGIN | |
IF (SELECT count(*) FROM contacts WHERE membership_no = contacts_membership_no) > 0 THEN | |
BEGIN | |
DECLARE v_profile_id INT(10); | |
DECLARE v_contact_id INT(10); | |
SELECT id, profile_id INTO v_contact_id, v_profile_id FROM contacts WHERE membership_no = contacts_membership_no; | |
UPDATE phone_numbers SET `number` = phone_numbers_number WHERE profile_id = v_profile_id; | |
UPDATE email_contacts SET `address` = email_contacts_address WHERE contact_id = v_contact_id; | |
UPDATE contacts SET `first_name` = contacts_first_name WHERE id = v_contact_id; | |
END; | |
ELSE | |
BEGIN | |
-- 新增 Profile | |
INSERT INTO profiles() VALUES (); | |
/** | |
* Profile ID | |
* @type INT | |
*/ | |
SET @profiles_id := LAST_INSERT_ID(); | |
-- 新增 Contact,並且勾住 Profile | |
INSERT INTO contacts(`membership_no`, `first_name`, `profile_id`) VALUES (contacts_membership_no, contacts_first_name, @profiles_id); | |
/** | |
* Contact ID | |
* @type INT | |
*/ | |
SET @contacts_id := LAST_INSERT_ID(); | |
-- 新增 PhoneNumber | |
INSERT INTO phone_numbers(`profile_id`, `number`, `type`, `description`) VALUES (@profiles_id, phone_numbers_number, 1, 'created from trigger'); | |
-- 新增 EmailContact | |
INSERT INTO email_contacts(`contact_id`, `address`, `description`) VALUES (@contacts_id, email_contacts_address, 'created from trigger'); | |
END; | |
END IF; | |
END $$ | |
DELIMITER ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP TRIGGER IF EXISTS dba_used_contacts_updated; | |
DELIMITER $$ | |
CREATE TRIGGER dba_used_contacts_updated BEFORE UPDATE | |
ON dba_used_contacts | |
FOR EACH ROW | |
BEGIN | |
IF NEW.phone_numbers_number <> OLD.phone_numbers_number THEN | |
UPDATE phone_numbers SET `number` = NEW.phone_numbers_number WHERE `number` = OLD.phone_numbers_number; | |
END IF; | |
IF NEW.email_contacts_address <> OLD.email_contacts_address THEN | |
UPDATE email_contacts SET `address` = NEW.email_contacts_address WHERE `address` = OLD.email_contacts_address; | |
END IF; | |
IF NEW.contacts_first_name <> OLD.contacts_first_name THEN | |
UPDATE contacts SET `first_name` = NEW.contacts_first_name WHERE `membership_no` = OLD.contacts_membership_no; | |
END IF; | |
END $$ | |
DELIMITER ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSERT INTO dba_used_contacts (contacts_membership_no,contacts_first_name,email_contacts_address,phone_numbers_number) VALUES('AAAAAAAAAA', '洪大頭', '[email protected]', '0900000000'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment