deyanmalinov

01-mysql

Sep 19th, 2020 (edited)
151
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE TABLE `employees` (
  2.   `id` INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
  3.   `first_name` VARCHAR(50) NOT NULL,
  4.   `last_name` VARCHAR(50) NOT NULL
  5. );
  6.  
  7. CREATE TABLE `categories` (
  8.   `id` INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
  9.   `name` VARCHAR(50) NOT NULL
  10. );
  11.  
  12. CREATE TABLE `products` (
  13.   `id` INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
  14.   `name` VARCHAR(50) NOT NULL,
  15.   `category_id` INT NOT NULL
  16. );
  17. INSERT INTO `employees` (`id`, `first_name`, `last_name`) VALUES
  18. ('1', 'Test1', 'Test1'),
  19. ('2', 'Test2', 'Test2'),
  20. ('3', 'Test3', 'Test3');
  21.  
  22. ALTER TABLE `employees`
  23. ADD COLUMN `middle_name` VARCHAR(45);
  24.  
  25. ALTER TABLE `products`
  26. ADD CONSTRAINT `category_id`
  27.   FOREIGN KEY (`category_id`)
  28.   REFERENCES `categories` (`id`)
  29.   ON DELETE NO ACTION
  30.   ON UPDATE cascade;
  31.  
  32. ALTER TABLE `employees`
  33. CHANGE COLUMN `middle_name` `middle_name` VARCHAR(100) NOT NULL ;
RAW Paste Data