Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. USE gc200380935;
  2.  
  3. DROP TABLE IF EXISTS donations;
  4. CREATE TABLE donations (
  5.  
  6. first_name VARCHAR(20) NOT NULL,
  7. last_name VARCHAR(20) NOT NULL,
  8. recipient VARCHAR(255) NOT NULL );
  9.  
  10. INSERT INTO donations (first_name, last_name, recipient)
  11. VALUES ('Eric', 'Smith', 'Barrie, Liberal Party');
  12.  
  13. INSERT INTO donations (first_name, last_name, recipient)
  14. VALUES ('Alicia', 'Jones', 'Simcoe North, Conservative Party');
  15.  
  16. INSERT INTO donations (first_name, last_name, recipient)
  17. VALUES ('Sue', 'Wilson', 'London South, NDP');
  18.  
  19. /*View the donations table*/
  20. select * FROM donations;
  21.  
  22. /*Is this in 1NF (first normal form)?
  23. No, it needs a unqiue identifier (key) for each row
  24. Let add a column called donorID*/
  25. ALTER TABLE donations
  26. ADD COLUMN donorID INT NOT NULL auto_increment primary KEY;
  27. /*Let's make the donorID the first column*/
  28. ALTER TABLE donations
  29. MODIFY COLUMN donorID INT FIRST;
  30.  
  31. /*Let's move the recipient column to be the 2nd column*/
  32. ALTER TABLE donations
  33. MODIFY COLUMN recipient VARCHAR(255) AFTER donorID;
  34.  
  35.  
  36.  
  37. select * FROM donations;
  38. /*The riding and political parties are currently in the same column, which is not atomic. As such, we need to
  39. add 2 columns*/
  40.  
  41.  
  42.  
  43. alter table donations
  44. add column riding VARCHAR(25) AFTER recipient;
  45.  
  46. alter table donations
  47. add column party VARCHAR(25) AFTER riding;
  48.  
  49.  
  50. select * FROM donations;
  51.  
  52. /*We need to populate these new fields, let's write some queries to do that*/
  53. select recipient , INSTR(recipient, ',') from donations;
  54.  
  55. /*OR try substring index*/
  56.  
  57. /*Now let's update the riding column with this information*/
  58.  
  59. /*Create a query to give us the political party*/
  60.  
  61. /*We've tested our query, let's update the table*/
  62.  
  63. /*We now need to DROP the recipient column*/
  64.  
  65. /*We are now in first normal form! Also known as 1NF*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement