Advertisement
vencinachev

172DB2

Dec 3rd, 2020
1,120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.33 KB | None | 0 0
  1. SHOW DATABASES;
  2.  
  3. CREATE DATABASE autopark;
  4.  
  5. USE autopark;
  6.  
  7. CREATE TABLE persons (
  8. id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
  9. firstname VARCHAR(200) NOT NULL,
  10. lastname VARCHAR(200) NOT NULL,
  11. age INT,
  12. city VARCHAR(100)
  13. );
  14.  
  15. INSERT INTO persons
  16. VALUES (1, 'Gosho', 'Borisov', 32, 'Kaspichan');
  17.  
  18. INSERT INTO persons (id, firstname, lastname, age, city)
  19. VALUES (2, 'Stamat', 'Todorov', 22, 'Lesidren');
  20.  
  21. INSERT INTO persons (firstname, lastname, city)
  22. VALUES ('Slavi', 'Trifonov', 'Pleven');
  23.  
  24. INSERT INTO persons (firstname, lastname, city)
  25. VALUES ('Ani', 'Vasileva', 'Pleven');
  26.  
  27.  
  28. SELECT * FROM persons WHERE city = 'Pleven';
  29.  
  30. SELECT * FROM persons;
  31.  
  32. DELETE FROM persons WHERE id = 5;
  33.  
  34. UPDATE persons SET city = 'Pleven' WHERE id = 2;
  35.  
  36.  
  37.  
  38. CREATE TABLE cars(
  39. id INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
  40. model VARCHAR(100),
  41. maxspeed INT,
  42. personId INT,
  43. FOREIGN KEY (personId) REFERENCES persons(id)
  44. );
  45.  
  46.  
  47. INSERT INTO cars
  48. VALUES (1, 'Opel', 200, 1);
  49.  
  50. INSERT INTO cars
  51. VALUES (2, 'Renault', 180, 1);
  52.  
  53. INSERT INTO cars (model, maxspeed, personId)
  54. VALUES ('Moskvich', 165, 2);
  55.  
  56. INSERT INTO cars (model, maxspeed, personId)
  57. VALUES ('Lada', 195, 2);
  58.  
  59. SELECT * FROM cars;
  60.  
  61. SELECT cars.id, cars.model, CONCAT(persons.firstname, ' ', persons.lastname) AS carowner
  62. FROM persons, cars
  63. WHERE cars.personId = persons.id;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement