Advertisement
vallec

Untitled

Mar 27th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. DROP DATABASE IF EXISTS booksClub;
  2. CREATE DATABASE booksClub;
  3. USE booksClub;
  4.  
  5. CREATE TABLE userRole(
  6. id INT AUTO_INCREMENT PRIMARY KEY,
  7. roleName ENUM('Admin', 'Librariar', 'Student', 'Teacher') NOT NULL
  8. );
  9.  
  10. CREATE TABLE publishers (
  11. id INT AUTO_INCREMENT PRIMARY KEY,
  12. name VARCHAR(255) NOT NULL,
  13. address VARCHAR(255) NOT NULL
  14. );
  15.  
  16. CREATE TABLE books (
  17. id INT AUTO_INCREMENT PRIMARY KEY,
  18. title VARCHAR(255) NOT NULL,
  19. description TEXT NOT NULL,
  20. publisherId INT NOT NULL,
  21. CONSTRAINT FOREIGN KEY (publisherId)
  22. REFERENCES publishers(id)
  23. );
  24.  
  25. CREATE TABLE genres (
  26. id INT AUTO_INCREMENT PRIMARY KEY,
  27. name VARCHAR(255) NOT NULL
  28. );
  29.  
  30. CREATE TABLE books_genres (
  31. bookId INT NOT NULL ,
  32. genreId INT NOT NULL ,
  33. CONSTRAINT FOREIGN KEY (bookId)
  34. REFERENCES books(id) ,
  35. CONSTRAINT FOREIGN KEY (genreId)
  36. REFERENCES genres(id) ,
  37. PRIMARY KEY(bookId,genreId)
  38. );
  39.  
  40. CREATE TABLE users (
  41. id INT AUTO_INCREMENT PRIMARY KEY,
  42. name VARCHAR(255) NOT NULL,
  43. egn VARCHAR(10) NOT NULL UNIQUE,
  44. pass VARCHAR(255) NOT NULL,
  45. phone VARCHAR(20) NOT NULL,
  46. email VARCHAR(255) NOT NULL UNIQUE,
  47. roleId INT NOT NULL,
  48. CONSTRAINT FOREIGN KEY (roleId)
  49. REFERENCES userRole(id)
  50. );
  51.  
  52. CREATE TABLE loanBooks (
  53. id INT AUTO_INCREMENT PRIMARY KEY,
  54. date DATE NOT NULL,
  55. userId INT NOT NULL,
  56. CONSTRAINT FOREIGN KEY (userId)
  57. REFERENCES users(id),
  58. bookId INT NOT NULL,
  59. CONSTRAINT FOREIGN KEY (bookId)
  60. REFERENCES books(id)
  61. );
  62.  
  63. CREATE TABLE authors (
  64. id INT AUTO_INCREMENT PRIMARY KEY,
  65. name VARCHAR(255) NOT NULL,
  66. info TEXT
  67. );
  68.  
  69. CREATE TABLE authors_books (
  70. bookId INT NOT NULL ,
  71. authorId INT NOT NULL ,
  72. CONSTRAINT FOREIGN KEY (bookId)
  73. REFERENCES books(id) ,
  74. CONSTRAINT FOREIGN KEY (authorId)
  75. REFERENCES authors(id) ,
  76. PRIMARY KEY(bookId,authorId)
  77. );
  78.  
  79.  
  80.  
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement