Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 0.78 KB | None | 0 0
  1. CREATE TABLE models(
  2. model_id INT NOT NULL UNIQUE,
  3. name VARCHAR(50),
  4. manufacturer_id INT NOT NULL
  5. );
  6.  
  7. CREATE TABLE manufacturers(
  8. manufacturer_id INT NOT NULL UNIQUE,
  9. name VARCHAR(50),
  10. established_on VARCHAR(50)
  11. );
  12.  
  13. INSERT INTO manufacturers VALUES
  14. (1, 'BMW', '01/03/1916'),
  15. (2, 'Tesla', '01/01/2003'),
  16. (3, 'Lada', '01/05/1966');
  17.  
  18. INSERT INTO models VALUES
  19. (101, 'X1', 1),
  20. (102, 'i6', 1),
  21. (103, 'Model S', 2),
  22. (104, 'Model X', 2),
  23. (105, 'Model 3', 2),
  24. (106, 'Nova', 3);
  25.  
  26. ALTER TABLE models
  27. ADD CONSTRAINT pk_model_id PRIMARY KEY(model_id);
  28.  
  29. ALTER TABLE manufacturers
  30. ADD CONSTRAINT pk_manufacturer_id PRIMARY KEY(manufacturer_id);
  31.  
  32. ALTER TABLE models
  33. ADD CONSTRAINT fk_monufacturer_model_id FOREIGN KEY(manufacturer_id) REFERENCES manufacturers(manufacturer_id);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement