Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.78 KB | None | 0 0
  1. CREATE TABLE users (
  2. id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  3. username NVARCHAR(30) NOT NULL UNIQUE,
  4. password NVARCHAR(50) NOT NULL
  5. )
  6.  
  7. CREATE TABLE sections (
  8. id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  9. name NVARCHAR(50) NOT NULL UNIQUE,
  10. )
  11.  
  12. CREATE TABLE posts (
  13. id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  14. description NVARCHAR(255) NOT NULL,
  15. image_url NVARCHAR(255) NOT NULL,
  16. date_time DATETIME DEFAULT CURRENT_TIMESTAMP,
  17. author_id BIGINT NOT NULL,
  18. section_id BIGINT NOT NULL,
  19. CONSTRAINT FK_userId_users_posts FOREIGN KEY(author_id) REFERENCES users(id),
  20. CONSTRAINT FK_sectionId_sections FOREIGN KEY(section_id) REFERENCES sections(id)
  21. )
  22.  
  23. CREATE TABLE tags (
  24. id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  25. name VARCHAR(50) NOT NULL
  26. )
  27.  
  28. CREATE TABLE comments (
  29. id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  30. description NVARCHAR(1000) NOT NULL,
  31. date_time DATETIME DEFAULT CURRENT_TIMESTAMP,
  32. author_id BIGINT NOT NULL,
  33. post_id BIGINT NOT NULL,
  34. CONSTRAINT FK_userId_users_comments FOREIGN KEY(author_id) REFERENCES users(id),
  35. CONSTRAINT FK_postId_posts_comments FOREIGN KEY(post_id) REFERENCES posts(id)
  36. )
  37.  
  38. CREATE TABLE post_tag (
  39. post_id BIGINT NOT NULL,
  40. tag_id BIGINT NOT NULL,
  41. CONSTRAINT PK_postId_examId PRIMARY KEY(post_id, tag_id),
  42. CONSTRAINT FK_postId_posts FOREIGN KEY(post_id) REFERENCES posts(id),
  43. CONSTRAINT FK_tagId_tags FOREIGN KEY (tag_id) REFERENCES tags(id)
  44. )
  45.  
  46. CREATE TABLE rating_posts_users (
  47. post_id BIGINT NOT NULL,
  48. user_id BIGINT NOT NULL,
  49. grade INT NOT NULL,
  50. CONSTRAINT PK_raiting_posts_users PRIMARY KEY(post_id, user_id),
  51. CONSTRAINT FK_postId_userId_posts FOREIGN KEY (post_id) REFERENCES posts(id),
  52. CONSTRAINT FK_postId_userId_users FOREIGN KEY (user_id) REFERENCES users(id),
  53. CONSTRAINT CHK_gradeValue CHECK (grade = -1 OR grade = 1)
  54. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement