Advertisement
dlozic

MySQL Baza CRM

Jan 6th, 2022 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 6.01 KB | None | 0 0
  1. DROP PROCEDURE IF EXISTS `drop_all_tables`;
  2.  
  3. DELIMITER $$
  4. CREATE PROCEDURE `drop_all_tables`()
  5. BEGIN
  6.     DECLARE _done INT DEFAULT FALSE;
  7.     DECLARE _tableName VARCHAR(255);
  8.     DECLARE _cursor CURSOR FOR
  9.         SELECT table_name
  10.         FROM information_schema.TABLES
  11.         WHERE table_schema = SCHEMA();
  12.     DECLARE CONTINUE HANDLER FOR NOT FOUND SET _done = TRUE;
  13.     SET FOREIGN_KEY_CHECKS = 0;
  14.     OPEN _cursor;
  15.     REPEAT FETCH _cursor INTO _tableName;
  16.     IF NOT _done THEN
  17.         SET @stmt_sql = CONCAT('DROP TABLE ', _tableName);
  18.         PREPARE stmt1 FROM @stmt_sql;
  19.         EXECUTE stmt1;
  20.         DEALLOCATE PREPARE stmt1;
  21.     END IF;
  22.     UNTIL _done END REPEAT;
  23.     CLOSE _cursor;
  24.     SET FOREIGN_KEY_CHECKS = 1;
  25. END$$
  26. DELIMITER ;
  27. call drop_all_tables();
  28. DROP PROCEDURE IF EXISTS `drop_all_tables`;
  29.  
  30. CREATE TABLE gradovi (
  31.     postbr INT PRIMARY KEY,
  32.     naziv_grada VARCHAR(50) UNIQUE NOT NULL,
  33.     vrijeme_unosa TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  34. );
  35.  
  36. INSERT INTO gradovi (postbr, naziv_grada, vrijeme_unosa) VALUES
  37.     (10000, 'Zagreb', '2015-06-25 12:55:01'),
  38.     (51000, 'Rijeka', '2015-06-26 07:12:51'),
  39.     (35000, 'Sl. Brod', CURRENT_TIMESTAMP),
  40.     (21000, 'Split', '2017-01-18 11:11:11'),
  41.     (52100, 'Pula', NULL),
  42.     (31000, 'Osijek', CURRENT_TIMESTAMP),
  43.     (23000, 'Zadar', '2020-04-03'),
  44.     (42000, 'Varaždin', '2020-08-01'),
  45.     (22000, 'Šibenik', '2020-09-04 15:30:21'),
  46.     (20000, 'Dubrovnik', '2020-01-09');
  47.  
  48.  
  49. CREATE TABLE uloge (
  50.     id INT PRIMARY KEY AUTO_INCREMENT,
  51.     naziv_uloge VARCHAR(50) UNIQUE NOT NULL,
  52.     nadredeni_id INT,
  53.     FOREIGN KEY (nadredeni_id)
  54.         REFERENCES uloge(id)
  55.         ON DELETE SET NULL
  56. );
  57.  
  58. INSERT INTO uloge (naziv_uloge, nadredeni_id) VALUES
  59.     ('CEO', NULL),
  60.     ('CTO', 1),
  61.     ('COO', 1),
  62.     ('CFO', 1),
  63.     ('Senior programer/ka', 2),
  64.     ('Programer/ka', 5),
  65.     ('Knjigovodstvo', 4),
  66.     ('Pomoćnik/ca knjigovodstva', 7),
  67.     ('Vanjski suradnik', 3),
  68.     ('Voditelj nadzora', 3),
  69.     ('Pomoćnik/ca nadzora', 10);
  70.  
  71. CREATE TABLE statusi_zaposlenika (
  72.     id INT PRIMARY KEY AUTO_INCREMENT,
  73.     status VARCHAR(50) UNIQUE NOT NULL,
  74.     broj_zaposlenika INT DEFAULT 0
  75. );
  76.  
  77. INSERT INTO statusi_zaposlenika (status) VALUES
  78.     ('Aktivno zaposlen/a'),
  79.     ('Vanjski suradnik/ca'),
  80.     ('Suspendiran/a'),
  81.     ('Izvanredni otkaz'),
  82.     ('Bolovanje');
  83.  
  84. CREATE TABLE zaposlenici (
  85.     id INT PRIMARY KEY AUTO_INCREMENT,
  86.     titula VARCHAR(50),
  87.     ime VARCHAR(50) NOT NULL,
  88.     prezime VARCHAR(50) NOT NULL,
  89.     email VARCHAR(255) UNIQUE,
  90.     uloga_id INT,
  91.     status_id INT,
  92.     postbr_prebivaliste INT,
  93.     postbr_boraviste INT,
  94.     datum_rodenja DATE,
  95.  
  96.     FOREIGN KEY (uloga_id)
  97.         REFERENCES uloge(id)
  98.         ON DELETE SET NULL,
  99.  
  100.     FOREIGN KEY (status_id)
  101.         REFERENCES statusi_zaposlenika(id)
  102.         ON DELETE SET NULL,
  103.  
  104.     FOREIGN KEY (postbr_prebivaliste)
  105.         REFERENCES gradovi(postbr)
  106.         ON DELETE SET NULL,
  107.  
  108.     FOREIGN KEY (postbr_boraviste)
  109.         REFERENCES gradovi(postbr)
  110.         ON DELETE SET NULL
  111. );
  112.  
  113. CREATE TABLE statusi_kontakata (
  114.     id INT PRIMARY KEY AUTO_INCREMENT,
  115.     status VARCHAR(50) UNIQUE NOT NULL,
  116.     broj_kontakata INT DEFAULT 0
  117. );
  118.  
  119. INSERT INTO statusi_kontakata (status) VALUES
  120.     ('Aktivan'),
  121.     ('Neaktivan'),
  122.     ('Čeka na odobrenje'),
  123.     ('Čeka brisanje');
  124.  
  125. CREATE TABLE tvrtke (
  126.     id INT PRIMARY KEY AUTO_INCREMENT,
  127.     naziv VARCHAR(255) NOT NULL,
  128.     oib_tvrtke VARCHAR(11) UNIQUE,
  129.     adresa VARCHAR(255),
  130.     www VARCHAR(255),
  131.     tip VARCHAR(10) DEFAULT 'd.o.o.' NOT NULL,
  132.     postbr_sjediste INT,
  133.     FOREIGN KEY (postbr_sjediste)
  134.         REFERENCES gradovi(postbr)
  135.         ON DELETE SET NULL
  136. );
  137.  
  138. CREATE TABLE kontakti (
  139.     id INT PRIMARY KEY AUTO_INCREMENT,
  140.     ime VARCHAR(255) NOT NULL,
  141.     prezime VARCHAR(255) NOT NULL,
  142.     titula VARCHAR(5),
  143.     email VARCHAR(255) UNIQUE,
  144.     status_id INT,
  145.     tvrtka_id INT,
  146.     adresa VARCHAR(255),
  147.     mobitel VARCHAR(30),
  148.     postbr_grad INT,
  149.     privatni TINYINT DEFAULT 0,
  150.     izradio_id INT,
  151.  
  152.     FOREIGN KEY (status_id)
  153.         REFERENCES statusi_kontakata(id)
  154.         ON DELETE SET NULL,
  155.  
  156.     FOREIGN KEY (tvrtka_id)
  157.         REFERENCES tvrtke(id)
  158.         ON DELETE SET NULL,
  159.  
  160.     FOREIGN KEY (postbr_grad)
  161.         REFERENCES gradovi(postbr)
  162.         ON DELETE SET NULL,
  163.  
  164.     FOREIGN KEY (izradio_id)
  165.         REFERENCES zaposlenici(id)
  166.         ON DELETE SET NULL
  167. );
  168.  
  169. CREATE TABLE prioriteti_zadataka (
  170.     id INT PRIMARY KEY AUTO_INCREMENT,
  171.     prioritet VARCHAR(50) UNIQUE NOT NULL,
  172.     broj_zadataka INT DEFAULT 0
  173. );
  174.  
  175.  
  176. INSERT INTO prioriteti_zadataka (prioritet) VALUES
  177.     ('Niski prioritet'),
  178.     ('Srednji prioritet'),
  179.     ('Visoki prioritet'),
  180.     ('Hitno!');
  181.  
  182. CREATE TABLE zadaci (
  183.     id INT PRIMARY KEY AUTO_INCREMENT,
  184.     naslov VARCHAR(100) NOT NULL,
  185.     izvrsitelj_id INT,
  186.     zamjenik_id INT,
  187.     izradio_id INT,
  188.     datum_pocetka DATE,
  189.     datum_zavrsetka DATE,
  190.     sati_predvideno INT DEFAULT 0,
  191.     sati_odradeno INT DEFAULT 0,
  192.     sati_razlika INT DEFAULT 0,
  193.     tekst_zadatka varchar(2048),
  194.     kontakt_id INT,
  195.     prioritet_id INT,
  196.  
  197.     FOREIGN KEY (izvrsitelj_id)
  198.         REFERENCES zaposlenici(id)
  199.         ON DELETE SET NULL,
  200.  
  201.     FOREIGN KEY (zamjenik_id)
  202.         REFERENCES zaposlenici(id)
  203.         ON DELETE SET NULL,
  204.  
  205.     FOREIGN KEY (zamjenik_id)
  206.         REFERENCES zaposlenici(id)
  207.         ON DELETE SET NULL,
  208.  
  209.     FOREIGN KEY (kontakt_id)
  210.         REFERENCES kontakti(id)
  211.         ON DELETE SET NULL,
  212.  
  213.     FOREIGN KEY (prioritet_id)
  214.         REFERENCES prioriteti_zadataka(id)
  215.         ON DELETE SET NULL
  216. );
  217.  
  218. CREATE TABLE statusi_racuna (
  219.     id INT PRIMARY KEY AUTO_INCREMENT,
  220.     status VARCHAR(50) UNIQUE NOT NULL,
  221.     broj_racuna INT DEFAULT 0
  222. );
  223.  
  224. INSERT INTO statusi_racuna (status) VALUES
  225.     ('Neplaćen'),
  226.     ('Plaćen'),
  227.     ('Storniran');
  228.  
  229. CREATE TABLE racuni (
  230.     id INT PRIMARY KEY AUTO_INCREMENT,
  231.     broj_racuna VARCHAR(5) UNIQUE NOT NULL,
  232.     datum_racuna DATE,
  233.     datum_dospijeca DATE,
  234.     placeno TINYINT DEFAULT 0,
  235.     ukupno_bez_pdv DECIMAL(15, 2) DEFAULT 0,
  236.     pdv_vrijednost DECIMAL(15, 2) DEFAULT 0,
  237.     ukupno  DECIMAL(15, 2) DEFAULT 0,
  238.     tvrtka_id INT,
  239.     status_id INT,
  240.  
  241.     FOREIGN KEY (tvrtka_id)
  242.         REFERENCES tvrtke(id)
  243.         ON DELETE SET NULL,
  244.  
  245.     FOREIGN KEY (status_id)
  246.         REFERENCES statusi_racuna(id)
  247.         ON DELETE SET NULL
  248. );
  249.  
  250. COMMIT;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement