Advertisement
trungore10

Database homework

Mar 30th, 2021 (edited)
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. 1,
  2. SELECT title, first_name, last_name, start_date, end_date, CONCAT(title, '. ', first_name, ' ', last_name, ' (', start_date, ' - ', IF(end_date is NULL, "no end date", end_date), ')') AS information
  3. FROM employee;
  4.  
  5. 2,
  6. ALTER TABLE `account` DROP FOREIGN KEY `account_customer_fk`;
  7. ALTER TABLE `account` ADD CONSTRAINT `account_customer_fk` FOREIGN KEY (`cust_id`) REFERENCES `customer`(`cust_id`) ON DELETE RESTRICT ON UPDATE CASCADE;
  8.  
  9. ALTER TABLE `business` DROP FOREIGN KEY `business_employee_fk`;
  10. ALTER TABLE `business` ADD CONSTRAINT `business_employee_fk` FOREIGN KEY (`cust_id`) REFERENCES `customer`(`cust_id`) ON DELETE RESTRICT ON UPDATE CASCADE;
  11.  
  12. ALTER TABLE `individual` DROP FOREIGN KEY `individual_customer_fk`;
  13. ALTER TABLE `individual` ADD CONSTRAINT `individual_customer_fk` FOREIGN KEY (`cust_id`) REFERENCES `customer`(`cust_id`) ON DELETE RESTRICT ON UPDATE CASCADE;
  14.  
  15. ALTER TABLE `officer` DROP FOREIGN KEY `officer_customer_fk`;
  16. ALTER TABLE `officer` ADD CONSTRAINT `officer_customer_fk` FOREIGN KEY (`cust_id`) REFERENCES `customer`(`cust_id`) ON DELETE RESTRICT ON UPDATE CASCADE;
  17.  
  18. UPDATE customer
  19. SET cust_id = CONCAT('100', cust_id);
  20.  
  21. UPDATE customer
  22. SET cust_id = SUBSTR(cust_id, 4);
  23.  
  24. 3,
  25. INSERT INTO `customer` (`cust_id`, `address`, `city`, `cust_type_cd`, `fed_id`, `postal_code`, `state`)
  26. VALUES (NULL, 'Tan Binh', 'Ho Chi Minh', 'h', 'ha', '01942', 'mm'),
  27. (NULL, 'somewhere', 'Ha Noi', 'a', 'xx', '12344', 'ma'),
  28. (NULL, 'asgard', 'My Tho', 'b', 'na', '34444', 'ma');
  29.  
  30. SELECT postal_code, COUNT(postal_code) AS num_customers
  31. FROM `customer`
  32. GROUP BY postal_code
  33. HAVING postal_code is not NULL;
  34.  
  35. 4,
  36. SELECT txn_id, amount, CAST(txn_date AS DATE) AS date
  37. FROM `acc_transaction`
  38. WHERE 1;
  39.  
  40. 5,
  41. SELECT *, (YEAR(CURRENT_DATE()) - YEAR(birth_date)) AS age
  42. FROM `individual`
  43. HAVING age > 50
  44. ORDER BY age;
  45.  
  46. 6,
  47. SELECT CONCAT(LEFT(first_name, 1), RIGHT(first_name, 1), '. ', last_name) AS name, title
  48. FROM `employee`
  49. WHERE title LIKE '%teller%'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement