crwmolada

db_mk_certus

Jun 5th, 2025
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 9.93 KB | None | 0 0
  1. -- =================================================================
  2. -- CREACIÓN DE LA BASE DE DATOS
  3. -- =================================================================
  4. DROP DATABASE IF EXISTS db_mk_certus;
  5. CREATE DATABASE db_mk_certus;
  6. USE db_mk_certus;
  7.  
  8. -- =================================================================
  9. -- TABLA [1]: roles
  10. -- Almacena los diferentes roles de usuario (ej. Administrador, Vendedor, Cliente).
  11. -- =================================================================
  12. CREATE TABLE roles (
  13.     role_id INT AUTO_INCREMENT PRIMARY KEY,
  14.     role_name VARCHAR(50) NOT NULL UNIQUE,
  15.     role_description TEXT
  16. );
  17.  
  18. -- =================================================================
  19. -- TABLA [2]: permissions
  20. -- Almacena permisos específicos que se pueden asignar a los roles.
  21. -- =================================================================
  22. CREATE TABLE permissions (
  23.     permission_id INT AUTO_INCREMENT PRIMARY KEY,
  24.     permission_name VARCHAR(50) NOT NULL UNIQUE,
  25.     permission_description TEXT
  26. );
  27.  
  28. -- =================================================================
  29. -- TABLA [3]: roles_permissions (Pivote)
  30. -- Tabla intermedia para una relación Muchos a Muchos entre roles y permisos.
  31. -- =================================================================
  32. CREATE TABLE roles_permissions (
  33.     role_permission_id INT AUTO_INCREMENT PRIMARY KEY,
  34.     id_role INT,
  35.     id_permission INT,
  36.    
  37.     FOREIGN KEY (id_role) REFERENCES roles(role_id) ON DELETE CASCADE,
  38.     FOREIGN KEY (id_permission) REFERENCES permissions(permission_id) ON DELETE CASCADE
  39. );
  40.  
  41. -- =================================================================
  42. -- TABLA [4]: users
  43. -- Almacena la información de autenticación de los usuarios.
  44. -- =================================================================
  45. CREATE TABLE users (
  46.     user_id INT AUTO_INCREMENT PRIMARY KEY,
  47.     -- CORRECCIÓN: Se elimina id_role para evitar redundancia. La relación se maneja en 'users_roles'.
  48.     user_email VARCHAR(100) NOT NULL UNIQUE,
  49.     user_password VARCHAR(255) NOT NULL, -- Aumentado para hashes de contraseñas seguras
  50.     user_created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  51.     user_updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  52.     user_status BOOLEAN DEFAULT TRUE
  53. );
  54.  
  55. -- =================================================================
  56. -- TABLA [5]: category_career
  57. -- Almacena las categorías de los trabajos o proyectos (ej. Diseño Gráfico, Programación).
  58. -- =================================================================
  59. CREATE TABLE category_career (
  60.     category_career_id INT AUTO_INCREMENT PRIMARY KEY,
  61.     category_career_name VARCHAR(100) NOT NULL,
  62.     category_career_description VARCHAR(255) -- Aumentado para descripciones más largas
  63. );
  64.  
  65. -- =================================================================
  66. -- TABLA [6]: people
  67. -- Almacena los datos personales de los usuarios, vinculados a una cuenta.
  68. -- =================================================================
  69. CREATE TABLE people (
  70.     person_id INT AUTO_INCREMENT PRIMARY KEY,
  71.     id_user INT NOT NULL UNIQUE, -- Un perfil por usuario
  72.     person_name VARCHAR(100) NOT NULL,
  73.     person_lastname VARCHAR(100) NOT NULL,
  74.     person_dni CHAR(8) UNIQUE,
  75.     person_mobile_phone VARCHAR(15),
  76.     person_gender CHAR(1),
  77.     person_institute_campus VARCHAR(100),
  78.     person_institutional_email VARCHAR(100) UNIQUE,
  79.     person_institutional_career VARCHAR(100),
  80.     person_current_term INT,
  81.    
  82.     FOREIGN KEY (id_user) REFERENCES users(user_id) ON DELETE CASCADE
  83. );
  84.  
  85. -- =================================================================
  86. -- TABLA [7]: works
  87. -- Almacena los trabajos o servicios ofrecidos por los vendedores.
  88. -- =================================================================
  89. CREATE TABLE works (
  90.     work_id INT AUTO_INCREMENT PRIMARY KEY,
  91.     id_seller_user INT NOT NULL,
  92.     id_work_category INT,
  93.     work_title VARCHAR(150) NOT NULL,
  94.     work_description TEXT,
  95.     work_price DECIMAL(10,2) NOT NULL,
  96.     work_image_url TEXT,
  97.     work_published_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  98.     work_updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  99.     -- CORRECCIÓN: El valor por defecto debe existir en la lista ENUM.
  100.     work_status ENUM('PUBLICADO', 'EN REVISIÓN', 'RECHAZADO') DEFAULT 'EN REVISIÓN',
  101.     work_is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
  102.  
  103.     FOREIGN KEY (id_seller_user) REFERENCES users(user_id),
  104.     FOREIGN KEY (id_work_category) REFERENCES category_career(category_career_id) ON DELETE SET NULL
  105. );
  106.  
  107. -- =================================================================
  108. -- TABLA [8]: orders
  109. -- Cabecera de las órdenes de compra.
  110. -- =================================================================
  111. CREATE TABLE orders (
  112.     order_id INT AUTO_INCREMENT PRIMARY KEY,
  113.     id_user INT NOT NULL, -- El usuario que realiza la compra
  114.     order_date DATETIME DEFAULT CURRENT_TIMESTAMP,
  115.    
  116.     FOREIGN KEY (id_user) REFERENCES users(user_id)
  117. );
  118.  
  119. -- =================================================================
  120. -- TABLA [9]: paypal_payments
  121. -- Almacena los detalles de una transacción de pago realizada con PayPal.
  122. -- =================================================================
  123. CREATE TABLE paypal_payments (
  124.     paypal_payment_id INT AUTO_INCREMENT PRIMARY KEY,
  125.     id_internal_order INT NOT NULL,
  126.     paypal_payment_payer_email VARCHAR(255),
  127.     paypal_payment_status VARCHAR(50) NOT NULL,
  128.     paypal_payment_transaction_id VARCHAR(255) UNIQUE,
  129.     paypal_payment_amount DECIMAL(10, 2) NOT NULL,
  130.     paypal_payment_currency CHAR(3) NOT NULL,
  131.     paypal_payment_transaction_type VARCHAR(50),
  132.     paypal_payment_created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  133.     paypal_payment_updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  134.  
  135.     FOREIGN KEY (id_internal_order) REFERENCES orders(order_id)
  136. );
  137.  
  138. -- =================================================================
  139. -- TABLA [10]: order_details
  140. -- Detalle de cada orden, con los trabajos comprados.
  141. -- =================================================================
  142. CREATE TABLE order_details (
  143.     order_detail_id INT AUTO_INCREMENT PRIMARY KEY,
  144.     id_order INT NOT NULL,
  145.     id_work INT NOT NULL,
  146.     order_detail_quantity INT NOT NULL DEFAULT 1,
  147.     order_detail_unit_price DECIMAL(10, 2) NOT NULL,
  148.    
  149.     FOREIGN KEY (id_order) REFERENCES orders(order_id) ON DELETE CASCADE,
  150.     FOREIGN KEY (id_work) REFERENCES works(work_id)
  151. );
  152.  
  153. -- =================================================================
  154. -- TABLA [11]: comments
  155. -- Almacena comentarios en los trabajos, con soporte para respuestas anidadas.
  156. -- =================================================================
  157. CREATE TABLE comments (
  158.     comment_id INT AUTO_INCREMENT PRIMARY KEY,
  159.     id_work INT NOT NULL,
  160.     id_user INT NOT NULL,
  161.     id_parent_comment INT NULL, -- Para respuestas a otros comentarios
  162.     comment_body TEXT NOT NULL,
  163.     comment_is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
  164.     comment_created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  165.     comment_updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  166.    
  167.     FOREIGN KEY (id_work) REFERENCES works(work_id) ON DELETE CASCADE,
  168.     FOREIGN KEY (id_user) REFERENCES users(user_id) ON DELETE CASCADE,
  169.     FOREIGN KEY (id_parent_comment) REFERENCES comments(comment_id) ON DELETE CASCADE
  170. );
  171.  
  172. -- =================================================================
  173. -- TABLA [12]: ratings
  174. -- Almacena las calificaciones y reseñas que los compradores dan a los trabajos.
  175. -- =================================================================
  176. CREATE TABLE ratings (
  177.     rating_id INT AUTO_INCREMENT PRIMARY KEY,
  178.     id_work INT NOT NULL,
  179.     id_user INT NOT NULL, -- Usuario que califica
  180.     id_order INT, -- Orden asociada a la calificación
  181.     rating_score TINYINT UNSIGNED NOT NULL CHECK (rating_score BETWEEN 1 AND 5),
  182.     rating_comment TEXT NULL,
  183.     rating_created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  184.    
  185.     FOREIGN KEY (id_work) REFERENCES works(work_id) ON DELETE CASCADE,
  186.     FOREIGN KEY (id_user) REFERENCES users(user_id) ON DELETE CASCADE,
  187.     FOREIGN KEY (id_order) REFERENCES orders(order_id) ON DELETE SET NULL
  188. );
  189.  
  190. -- =================================================================
  191. -- TABLA [13]: social_logins
  192. -- Vincula cuentas de usuario con proveedores de inicio de sesión social (Google, Facebook, etc.).
  193. -- =================================================================
  194. CREATE TABLE social_logins (
  195.     social_login_id INT AUTO_INCREMENT PRIMARY KEY,
  196.     -- CORRECCIÓN: El nombre de la columna debe ser consistente.
  197.     id_user INT NOT NULL,
  198.     social_login_provider VARCHAR(50) NOT NULL,
  199.     social_login_provider_user_id VARCHAR(255) NOT NULL,
  200.     social_login_linked_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  201.    
  202.     -- CORRECCIÓN: Se usa el nombre de columna correcto 'id_user'.
  203.     FOREIGN KEY (id_user) REFERENCES users(user_id) ON DELETE CASCADE,
  204.    
  205.     UNIQUE KEY uq_social_provider_user (social_login_provider, social_login_provider_user_id),
  206.     -- CORRECCIÓN: Se usa el nombre de columna correcto 'id_user'.
  207.     UNIQUE KEY uq_social_user_provider (id_user, social_login_provider)
  208. );
  209.  
  210. -- =================================================================
  211. -- TABLA [14]: users_roles (Pivote)
  212. -- Tabla para la relación Muchos a Muchos entre usuarios y roles.
  213. -- Esta es la forma correcta de asignar roles a usuarios.
  214. -- =================================================================
  215. CREATE TABLE users_roles (
  216.     id_user INT NOT NULL,
  217.     id_role INT NOT NULL,
  218.    
  219.     PRIMARY KEY (id_user, id_role),
  220.     FOREIGN KEY (id_user) REFERENCES users(user_id) ON DELETE CASCADE,
  221.     FOREIGN KEY (id_role) REFERENCES roles(role_id) ON DELETE CASCADE
  222. );
  223.  
  224. -- =================================================================
  225. -- FIN DEL SCRIPT
  226. -- =================================================================
Advertisement
Add Comment
Please, Sign In to add comment