Advertisement
aidanozo

Untitled

Jun 3rd, 2024
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.91 KB | None | 0 0
  1. CREATE DATABASE record_company;
  2. USE record_company;
  3. CREATE TABLE bands (
  4.     id INT NOT NULL AUTO_INCREMENT ,
  5.     name VARCHAR(255) NOT NULL ,
  6.     PRIMARY KEY (id)
  7. );
  8. CREATE TABLE albums (
  9.     id INT NOT NULL AUTO_INCREMENT ,
  10.     name VARCHAR(255) NOT NULL ,
  11.     release_year INT ,
  12.     band_id INT NOT NULL ,
  13.     PRIMARY KEY(id) ,
  14.     FOREIGN KEY(band_id) REFERENCES bands(id)
  15. );
  16.  
  17. INSERT INTO bands (name)
  18. VALUES ('Iron Maden');
  19. INSERT INTO bands (name)
  20. VALUES ('Deuce'), ('Avenged Sevenfold'), ('Ankor');
  21.  
  22. SELECT * FROM bands;
  23. SELECT * FROM bands LIMIT 2; --Afiseaza doar primele 2 linii
  24. SELECT name FROM bands; --Doar coloana "name", fara "id"
  25. SELECT id AS 'ID', name AS 'Band Name' FROM bands; --Se modifica numele coloanelor in tabel
  26. SELECT * FROM bands ORDER BY name; --Nu vor mai fi ordonate dupa id, ci dupa nume
  27. SELECT * FROM bands ORDER BY name DESC;
  28.  
  29. INSERT INTO albums (name, release_year, band_id)
  30. VALUES  ('The Number of the Beasts', 1985, 1),
  31.         ('Power Slave', 1984, 1),
  32.         ('Nightmare', 2018, 2),
  33.         ('Nightmare', 2010, 3),
  34.         ('Test Album', NULL, 3);
  35. SELECT * FROM albums;
  36. SELECT DISTINCT name FROM albums; --Doar numele unice
  37.  
  38. UPDATE albums
  39. SET release_year = 1982 WHERE id = 1;
  40.  
  41. SELECT * FROM albums
  42. WHERE release_year < 200;
  43.  
  44. SELECT * FROM albums
  45. WHERE name LIKE '%er%' --Trebuie ca numele sa aiba er inauntru
  46. OR band_id = 2;
  47.  
  48. SELECT * FROM albums
  49. WHERE release_year = 1984 AND band_id = 1;
  50.  
  51. SELECT * FROM albums
  52. WHERE release_year BETWEEN 2000 AND 2018;
  53.  
  54. SELECT * FROM albums
  55. WHERE release_year IS NULL;
  56.  
  57. DELETE FROM albums WHERE id = 5;
  58.  
  59. SELECT * FROM albums;
  60.  
  61. SELECT * FROM bands
  62. JOIN albums ON bands.id = albums.id
  63.  
  64. SELECT AVG(release_year) FROM albums;
  65.  
  66. SELECT band_id, COUNT(band_id) FROM albums
  67. GROUP BY band_id;
  68.  
  69. SELECT b.name AS band_name, COUNT (a.id) AS num_albums
  70. FROM bands AS b
  71. LEFT JOIN albums AS a ON b.id = a.band_id
  72. WHERE b.name = 'Deuce'
  73. GROUP BY b.id
  74. HAVING num_albums = 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement