MySql problem to extract data from two 1to1 tables relationships - mysql

I'm trying to doing this exercise
Extract from the database all the records relating to the "Mathematics" lessons ('docenti.materia' column) of the teacher ('docenti' table) Andrea Bianchi with the participant ('partecipanti' table) Mario Rossi
db sql file
-- phpMyAdmin SQL Dump
-- version 5.2.0
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: Dec 17, 2022 at 04:13 PM
-- Server version: 8.0.30
-- PHP Version: 8.1.10
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `school`
--
-- --------------------------------------------------------
--
-- Table structure for table `docenti`
--
CREATE TABLE `docenti` (
`id` bigint UNSIGNED NOT NULL,
`id_persona_fisica` bigint UNSIGNED NOT NULL,
`materia` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `docenti`
--
INSERT INTO `docenti` (`id`, `id_persona_fisica`, `materia`) VALUES
(9, 2, 'Matematica'),
(10, 6, 'Storia'),
(11, 3, 'Filosofia');
-- --------------------------------------------------------
--
-- Table structure for table `lezioni`
--
CREATE TABLE `lezioni` (
`id` bigint UNSIGNED NOT NULL,
`id_docente` bigint UNSIGNED NOT NULL,
`giorno` date NOT NULL,
`ora_inizio` time NOT NULL DEFAULT '15:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `lezioni`
--
INSERT INTO `lezioni` (`id`, `id_docente`, `giorno`, `ora_inizio`) VALUES
(47, 11, '2022-04-26', '15:00:00'),
(48, 11, '2022-04-19', '15:00:00'),
(49, 11, '2022-12-27', '15:00:00'),
(50, 11, '2022-12-20', '15:00:00'),
(51, 9, '2022-03-30', '15:00:00'),
(52, 9, '2022-04-27', '15:00:00'),
(53, 9, '2022-04-20', '15:00:00'),
(54, 9, '2022-04-13', '15:00:00'),
(55, 9, '2022-12-23', '15:00:00'),
(56, 9, '2022-12-30', '15:00:00'),
(57, 9, '2022-11-25', '15:00:00'),
(58, 9, '2022-11-18', '15:00:00'),
(59, 9, '2022-12-09', '15:00:00'),
(60, 9, '2022-12-04', '15:00:00'),
(61, 10, '2022-12-26', '15:00:00'),
(62, 10, '2022-12-19', '15:00:00'),
(63, 10, '2022-12-12', '15:00:00'),
(64, 10, '2022-11-28', '15:00:00'),
(65, 10, '2022-11-21', '15:00:00'),
(66, 10, '2022-11-14', '15:00:00'),
(67, 10, '2022-04-25', '15:00:00'),
(68, 10, '2022-04-15', '15:00:00'),
(69, 10, '2022-04-11', '15:00:00');
-- --------------------------------------------------------
--
-- Table structure for table `partecipanti`
--
CREATE TABLE `partecipanti` (
`id` bigint UNSIGNED NOT NULL,
`id_persona_fisica` bigint UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `partecipanti`
--
INSERT INTO `partecipanti` (`id`, `id_persona_fisica`) VALUES
(4, 1),
(2, 4),
(5, 5),
(1, 7),
(3, 8);
-- --------------------------------------------------------
--
-- Table structure for table `persona_fisica`
--
CREATE TABLE `persona_fisica` (
`id` bigint UNSIGNED NOT NULL,
`nome` varchar(255) NOT NULL,
`cognome` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `persona_fisica`
--
INSERT INTO `persona_fisica` (`id`, `nome`, `cognome`) VALUES
(1, 'Mario', 'Rossi'),
(2, 'Andrea', 'Bianchi'),
(3, 'Ernesto', 'Pignoccheri'),
(4, 'Rosa', 'Mastro'),
(5, 'Franco', 'Tilder'),
(6, 'Sara', 'Santelli'),
(7, 'Serena', 'Baropoli'),
(8, 'Franco', 'Baglivi');
-- --------------------------------------------------------
--
-- Table structure for table `presenze`
--
CREATE TABLE `presenze` (
`id` bigint UNSIGNED NOT NULL,
`id_lezione` bigint UNSIGNED NOT NULL,
`id_partecipante` bigint UNSIGNED NOT NULL,
`ora_ingresso` time NOT NULL,
`ora_uscita` time NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `presenze`
--
INSERT INTO `presenze` (`id`, `id_lezione`, `id_partecipante`, `ora_ingresso`, `ora_uscita`) VALUES
(1, 47, 1, '08:00:00', '10:00:00'),
(2, 48, 1, '08:00:00', '10:00:00'),
(3, 49, 1, '08:00:00', '10:00:00'),
(4, 50, 1, '08:00:00', '10:00:00'),
(5, 51, 1, '08:00:00', '10:00:00'),
(6, 52, 1, '08:00:00', '10:00:00'),
(7, 53, 1, '08:00:00', '10:00:00'),
(8, 54, 1, '08:00:00', '10:00:00'),
(9, 55, 1, '08:00:00', '10:00:00'),
(10, 56, 1, '08:00:00', '10:00:00'),
(11, 57, 1, '08:00:00', '10:00:00'),
(12, 58, 1, '08:00:00', '10:00:00'),
(13, 59, 1, '08:00:00', '10:00:00'),
(14, 60, 1, '08:00:00', '10:00:00'),
(15, 61, 1, '08:00:00', '10:00:00'),
(16, 62, 1, '08:00:00', '10:00:00'),
(17, 63, 1, '08:00:00', '10:00:00'),
(18, 64, 1, '08:00:00', '10:00:00'),
(19, 65, 1, '08:00:00', '10:00:00'),
(20, 66, 1, '08:00:00', '10:00:00'),
(21, 67, 1, '08:00:00', '10:00:00'),
(22, 68, 1, '08:00:00', '10:00:00'),
(23, 69, 1, '08:00:00', '10:00:00'),
(24, 68, 2, '08:00:00', '10:00:00'),
(25, 68, 5, '08:00:00', '10:00:00'),
(26, 51, 4, '00:08:00', '00:10:00'),
(27, 52, 4, '00:08:00', '00:10:00'),
(28, 53, 4, '00:08:00', '00:10:00');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `docenti`
--
ALTER TABLE `docenti`
ADD PRIMARY KEY (`id`),
ADD KEY `FK_id_persona_fisica` (`id_persona_fisica`);
--
-- Indexes for table `lezioni`
--
ALTER TABLE `lezioni`
ADD PRIMARY KEY (`id`),
ADD KEY `FK_id_docente` (`id_docente`);
--
-- Indexes for table `partecipanti`
--
ALTER TABLE `partecipanti`
ADD PRIMARY KEY (`id`),
ADD KEY `FK_persona_fisica` (`id_persona_fisica`);
--
-- Indexes for table `persona_fisica`
--
ALTER TABLE `persona_fisica`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `presenze`
--
ALTER TABLE `presenze`
ADD PRIMARY KEY (`id`),
ADD KEY `FK_id_lezione` (`id_lezione`),
ADD KEY `FK_id_partecipante` (`id_partecipante`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `docenti`
--
ALTER TABLE `docenti`
MODIFY `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12;
--
-- AUTO_INCREMENT for table `lezioni`
--
ALTER TABLE `lezioni`
MODIFY `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=70;
--
-- AUTO_INCREMENT for table `partecipanti`
--
ALTER TABLE `partecipanti`
MODIFY `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
--
-- AUTO_INCREMENT for table `persona_fisica`
--
ALTER TABLE `persona_fisica`
MODIFY `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;
--
-- AUTO_INCREMENT for table `presenze`
--
ALTER TABLE `presenze`
MODIFY `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=29;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `docenti`
--
ALTER TABLE `docenti`
ADD CONSTRAINT `FK_id_persona_fisica` FOREIGN KEY (`id_persona_fisica`) REFERENCES `persona_fisica` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `lezioni`
--
ALTER TABLE `lezioni`
ADD CONSTRAINT `FK_id_docente` FOREIGN KEY (`id_docente`) REFERENCES `docenti` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `partecipanti`
--
ALTER TABLE `partecipanti`
ADD CONSTRAINT `FK_persona_fisica` FOREIGN KEY (`id_persona_fisica`) REFERENCES `persona_fisica` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `presenze`
--
ALTER TABLE `presenze`
ADD CONSTRAINT `FK_id_lezione` FOREIGN KEY (`id_lezione`) REFERENCES `lezioni` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `FK_id_partecipante` FOREIGN KEY (`id_partecipante`) REFERENCES `partecipanti` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
I'm trying with this query but I keep getting a blank line.
SELECT presenze.*, partecipanti.id, partecipanti.id_persona_fisica, persona_fisica.id, persona_fisica.nome, persona_fisica.cognome, docenti.id_persona_fisica, docenti.materia
FROM presenze
JOIN partecipanti ON presenze.id_partecipante = partecipanti.id
JOIN persona_fisica ON partecipanti.id_persona_fisica = persona_fisica.id
JOIN docenti ON docenti.id_persona_fisica = persona_fisica.id
WHERE persona_fisica.nome = 'Mario' AND persona_fisica.cognome = 'Rossi'
AND persona_fisica.nome = 'Andrea' AND persona_fisica.cognome = 'Bianchi'
AND docenti.materia = 'Matematica';
Where am I wrong?
************* S O L V E D *************
In the end I solved it by doing the second JOIN on the 'persona_fisica' table (alias pf2) without putting the 'ON' clause.
Thanks a lot to #blobtub for the tip!
SELECT
presenze.*,
partecipanti.id,
partecipanti.id_persona_fisica,
pf1.id,
pf1.nome,
pf1.cognome,
docenti.id_persona_fisica,
docenti.materia,
pf2.id,
pf2.nome,
pf2.cognome
FROM presenze
JOIN partecipanti
ON presenze.id_partecipante = partecipanti.id
JOIN persona_fisica pf1
ON partecipanti.id_persona_fisica = pf1.id
JOIN persona_fisica pf2
ON docenti.id_persona_fisica = pf2.id
WHERE pf1.nome = 'Mario' AND pf1.cognome = 'Rossi'
AND pf2.nome = 'Andrea' AND pf2.cognome = 'Bianchi'
AND docenti.materia = 'Matematica';

You need to use two joins onto persona_fisica, one for the partecipanti and one for the docenti
See how the persona_fisica table has two relation arrows going from the same attribute (id) on your diagram? This is what tells you you will need two joins if you want to use both relations.
Something like this (I'm afraid my Italian isn't good enough to test it, I'm sorry):
SELECT
lezioni.id_docente,
presenze.*,
partecipanti.id,
partecipanti.id_persona_fisica,
pf1.id,
pf1.nome,
pf1.cognome,
docenti.id_persona_fisica,
docenti.materia,
pf2.id,
pf2.nome,
pf2.cognome
FROM presenze
JOIN partecipanti
ON presenze.id_partecipante = partecipanti.id
JOIN persona_fisica pf1
ON partecipanti.id_persona_fisica = pf1.id
JOIN lezioni
ON presenze.id_lezioni = lezioni.id
JOIN docenti
ON docenti.id = lezioni.id_docenti
JOIN persona_fisica pf2
ON docenti.id_persona_fisica = pf2.id
WHERE pf1.nome = 'Mario' AND pf1.cognome = 'Rossi'
AND pf2.nome = 'Andrea' AND pf2.cognome = 'Bianchi'
AND docenti.materia = 'Matematica';

Related

cannot join 2 statements in mysql

My database >
CREATE TABLE `tblcompetition` (
`fldCompID` int(11) NOT NULL,
`fldDate` date NOT NULL,
`fldCompName` varchar(80) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dataark for tabell `tblcompetition`
--
INSERT INTO `tblcompetition` (`fldCompID`, `fldDate`, `fldCompName`) VALUES
(1, '2018-12-31', 'Winter Warmer'),
(2, '2019-01-31', 'Fresh New Year'),
(3, '2019-02-28', 'Month of Love'),
(4, '2018-11-30', 'Seaside Scenery');
-- --------------------------------------------------------
--
-- Tabellstruktur for tabell `tblimage`
--
CREATE TABLE `tblimage` (
`fldImageID` int(11) NOT NULL,
`fldMemberID` int(11) NOT NULL,
`fldCatID` int(11) NOT NULL,
`fldFilePath` varchar(256) NOT NULL,
`fldName` varchar(256) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dataark for tabell `tblimage`
--
INSERT INTO `tblimage` (`fldImageID`, `fldMemberID`, `fldCatID`, `fldFilePath`, `fldName`) VALUES
(6, 1, 6, 'images/Screen Shot 2019-02-14 at 14.42.07.png', 'Sunrise'),
(7, 1, 6, 'images/Screen Shot 2019-02-14 at 14.42.07.png', 'SunDown'),
(9, 11, 4, 'Imge1', 'Time Goes By'),
(18, 1, 1, 'images/Aloe.jpg', 'Aloe'),
(19, 1, 1, 'images/Aloe.jpg', 'Blur'),
(20, 1, 1, 'images/Aloe.jpg', 'Winter Time in the Country'),
(21, 1, 1, 'images/Aloe.jpg', 'Warmth'),
(22, 11, 4, 'he', 'Tomorrow');
-- --------------------------------------------------------
--
-- Tabellstruktur for tabell `tblmembentcomp`
--
CREATE TABLE `tblmembentcomp` (
`fldMembEntCompID` int(11) NOT NULL,
`fldCompID` int(11) NOT NULL,
`fldMemberID` int(11) NOT NULL,
`fldResult` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dataark for tabell `tblmembentcomp`
--
INSERT INTO `tblmembentcomp` (`fldMembEntCompID`, `fldCompID`, `fldMemberID`, `fldResult`) VALUES
(1, 2, 11, 11),
(2, 2, 1, 17),
(3, 2, 5, 19),
(4, 2, 3, 7),
(5, 2, 12, 4);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `tblcompetition`
--
ALTER TABLE `tblcompetition`
ADD PRIMARY KEY (`fldCompID`);
--
-- Indexes for table `tblimage`
--
ALTER TABLE `tblimage`
ADD PRIMARY KEY (`fldImageID`),
ADD KEY `fldMemberID` (`fldMemberID`),
ADD KEY `fldCatID` (`fldCatID`);
--
-- Indexes for table `tblmembentcomp`
--
ALTER TABLE `tblmembentcomp`
ADD PRIMARY KEY (`fldMembEntCompID`),
ADD UNIQUE KEY `fldMemberID` (`fldMemberID`),
ADD KEY `fldCompID` (`fldCompID`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `tblcompetition`
--
ALTER TABLE `tblcompetition`
MODIFY `fldCompID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
--
-- AUTO_INCREMENT for table `tblimage`
--
ALTER TABLE `tblimage`
MODIFY `fldImageID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=23;
--
-- AUTO_INCREMENT for table `tblmembentcomp`
--
ALTER TABLE `tblmembentcomp`
MODIFY `fldMembEntCompID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
--
-- Begrensninger for dumpede tabeller
--
--
-- Begrensninger for tabell `tblimage`
--
ALTER TABLE `tblimage`
ADD CONSTRAINT `tblimage_ibfk_1` FOREIGN KEY (`fldMemberID`) REFERENCES `tblmember` (`fldMemberID`),
ADD CONSTRAINT `tblimage_ibfk_2` FOREIGN KEY (`fldCatID`) REFERENCES `tblcategory` (`fldCatID`);
--
-- Begrensninger for tabell `tblmembentcomp`
--
ALTER TABLE `tblmembentcomp`
ADD CONSTRAINT `tblmembentcomp_ibfk_1` FOREIGN KEY (`fldCompID`) REFERENCES `tblcompetition` (`fldCompID`),
ADD CONSTRAINT `tblmembentcomp_ibfk_2` FOREIGN KEY (`fldMemberID`) REFERENCES `tblmember` (`fldMemberID`);
when i do >
SELECT tblmembentcomp.fldCompID, tblmembentcomp.fldMemberID, tblmembentcomp.fldResult, tblimage.fldMemberID FROM tblmembentcomp JOIN tblcompetition ON tblmembentcomp.fldCompID=tblcompetition.fldCompID ORDER BY tblmembentcomp.fldResult DESC LIMIT 3
i get 19,17,11 thats correct , but when i add > JOIN tblimage ON tblmembentcomp.fldMemberID=tblimage.fldMemberID
or full search string>
SELECT tblmembentcomp.fldCompID, tblmembentcomp.fldMemberID, tblmembentcomp.fldResult, tblimage.fldMemberID FROM tblmembentcomp JOIN tblcompetition ON tblmembentcomp.fldCompID=tblcompetition.fldCompID JOIN tblimage ON tblmembentcomp.fldMemberID=tblimage.fldMemberID ORDER BY tblmembentcomp.fldResult DESC LIMIT 3
i only get 17,17,17

How to find count in many to many relationship in MySql, Cakephp 3.6

I need to calculate the count(vendors.id) for each category.
ie: how many vendors are there for each category(not sub-category)
I am using cakephp 3.6 framework and MySQL as database
I tried with all possible way that i knew but not found any solution. Can anybody help me, is very important for my project
[UPDATE]
sql query i have used:
SELECT cat.id,cat.name ,COUNT(`vendor_id`) AS vendor_count FROM `vendor_services` JOIN `categories` ON(`vendor_services`.`category_id` = `categories`.`id`) JOIN `categories` AS cat ON(categories.category_id = cat.id) WHERE 1 GROUP BY cat.id
[UPDATE]Bellow is the sql to create corresponding tables
-- phpMyAdmin SQL Dump
-- version 4.7.0
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Dec 04, 2018 at 10:50 AM
-- Server version: 10.1.24-MariaDB
-- PHP Version: 7.1.6
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
--
-- Database: `demo123`
--
-- --------------------------------------------------------
--
-- Table structure for table `categories`
--
CREATE TABLE `categories` (
`id` int(11) NOT NULL,
`category_id` tinyint(4) NOT NULL,
`name` varchar(60) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `categories`
--
INSERT INTO `categories` (`id`, `category_id`, `name`) VALUES
(1, 0, 'Books'),
(2, 0, 'Electronics'),
(3, 0, 'Garden'),
(4, 1, 'Novel'),
(5, 1, 'Science'),
(6, 1, 'Story'),
(7, 2, 'Mobile'),
(8, 2, 'Tablet'),
(9, 2, 'Headphone'),
(10, 3, 'Pumps'),
(11, 3, 'Pipes');
-- --------------------------------------------------------
--
-- Table structure for table `vendors`
--
CREATE TABLE `vendors` (
`id` int(11) NOT NULL,
`name` varchar(60) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `vendors`
--
INSERT INTO `vendors` (`id`, `name`) VALUES
(1, 'VR Enterprizes'),
(2, 'RR Vendors'),
(3, 'JK Suppliers');
-- --------------------------------------------------------
--
-- Table structure for table `vendor_services`
--
CREATE TABLE `vendor_services` (
`id` int(11) NOT NULL,
`vendor_id` int(11) NOT NULL,
`category_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `vendor_services`
--
INSERT INTO `vendor_services` (`id`, `vendor_id`, `category_id`) VALUES
(1, 1, 4),
(2, 1, 5),
(3, 1, 6),
(4, 1, 11),
(5, 2, 7),
(6, 2, 8),
(7, 2, 9),
(8, 3, 10),
(9, 3, 11);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `categories`
--
ALTER TABLE `categories`
ADD PRIMARY KEY (`id`),
ADD KEY `category_id` (`category_id`);
--
-- Indexes for table `vendors`
--
ALTER TABLE `vendors`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `vendor_services`
--
ALTER TABLE `vendor_services`
ADD PRIMARY KEY (`id`),
ADD KEY `vendor_id` (`vendor_id`),
ADD KEY `category_id` (`category_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `categories`
--
ALTER TABLE `categories`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12;
--
-- AUTO_INCREMENT for table `vendors`
--
ALTER TABLE `vendors`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
--
-- AUTO_INCREMENT for table `vendor_services`
--
ALTER TABLE `vendor_services`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;COMMIT;
CategoriesTable
$this->hasMany(‘Subcategories’, [ 'className'=> ‘Categories’,
'foreignKey' => 'category_id',
'conditions' => ['category_id!= 0']
]);
$this->belongsTo('MainCategories', [
'className'=> ‘Categories’,
'foreignKey' => 'category_id',
]);
Below is the query in oracle, please have a look, and modify it as per mysql,
added below insertion,
INSERT INTO VENDOR_SERVICES (ID, VENDOR_ID, CATEGORY_ID)
VALUES(11, 3, 3);
SELECT FRM.CATEGORY_ID, FRM.VENDOR_ID, FRM.VENDER_NAME,COUNT(VENDOR_ID) counts FROM (
SELECT distinct CT.CATEGORY_ID, VS.VENDOR_ID, VS.CATEGORY_ID VS_CATEGORY_ID,vn.ID, vn.NAME VENDER_NAME FROM CATEGORIES CT
INNER JOIN VENDOR_SERVICES VS ON VS.CATEGORY_ID=CT.CATEGORY_ID
INNER JOIN VENDORS VN ON VS.VENDOR_ID=VN.ID) frm
group by CATEGORY_ID, VENDOR_ID, VENDER_NAME

import .sql file into mysql from mac command line. tried mysql -u root -p db_name > path/to/dbfile.sql

I have tried doing it from the command line
mysql -u root -p db_name > ~/Documents/db_name.sql
I have tried doing it from mysqlimport
mysqlimport -u root -p db_name ~/Documents/db_name.sql
I have tried both while being in the correct directory using just the file name.
I have tried entering mysql using
mysql -u root -p
use db_name;
source ~/Documents/db_name.sql;
(nothing happens - no response)
(tried with absolute path - no response)
\. ~/Documents/db_name.sql
(nothing happens)
I feel like I'm missing something. This seems like a trivial operation according to the last 30 minutes of googling and attempts.
Ultimately I had to copy and paste the entire .sql file into the mysql shell while using the correct db.
I feel like a caveman. Please help.
Edit: SQL file contents
-- phpMyAdmin SQL Dump
-- version 4.4.15.5
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1:8889
-- Generation Time: May 09, 2017 at 09:27 PM
-- Server version: 5.6.34-log
-- PHP Version: 7.0.13
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `movie-buff`
--
-- --------------------------------------------------------
--
-- Table structure for table `directors`
--
CREATE TABLE IF NOT EXISTS `directors` (
`director_id` int(11) NOT NULL,
`first` varchar(60) DEFAULT NULL,
`last` varchar(60) DEFAULT NULL,
`country` varchar(100) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `directors`
--
INSERT INTO `directors` (`director_id`, `first`, `last`, `country`) VALUES
(1, 'Jean-Pierre', 'Jeunet', 'France'),
(2, 'Jean', 'Renoir', 'France'),
(3, 'Akira', 'Kurosawa', 'Japan'),
(4, 'Jane', 'Campion', 'New Zealand'),
(5, 'Sally', 'Potter', 'UK'),
(6, 'Kasi', 'Lemmons', 'USA'),
(7, 'Ava', 'DuVernay', 'USA'),
(8, 'Todd', 'Haynes', 'USA'),
(9, 'Marleen', 'Gorris', 'Netherlands');
-- --------------------------------------------------------
--
-- Table structure for table `movies`
--
CREATE TABLE IF NOT EXISTS `movies` (
`movie_id` int(11) NOT NULL,
`title` varchar(130) DEFAULT NULL,
`year` int(11) DEFAULT NULL,
`director_id` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `movies`
--
INSERT INTO `movies` (`movie_id`, `title`, `year`, `director_id`) VALUES
(1, 'The City of Lost Children', 1995, 1),
(2, 'Amelie', 2001, 1),
(3, 'The Rules of the Game', 1939, 2),
(4, 'La Grande Illusion', 1937, 2),
(5, 'The Lower Depths', 1936, 2),
(6, 'Alien: Resurrection', 1997, 1),
(7, 'Ran', 1985, 3),
(8, 'Seven Samurai', 1954, 3),
(9, 'Throne of Blood', 1957, 3),
(10, 'An Angel at My Table', 1990, 4),
(11, 'The Piano', 1993, 4),
(12, 'Orlando', 1992, 5),
(13, 'The Tango Lesson', 1997, 5),
(14, 'Talk to Me', 2007, 6),
(15, 'Eve''s Bayou', 1997, 6),
(16, 'Selma', 2014, 7),
(18, 'Far From Heaven', 2002, 8),
(19, 'I''m Not There', 2007, 8),
(20, 'Carol', 2015, 8),
(21, 'Antonia''s Line', 1995, 9),
(22, 'Mrs. Dalloway', 1997, 9);
-- --------------------------------------------------------
--
-- Table structure for table `viewers`
--
CREATE TABLE IF NOT EXISTS `viewers` (
`viewer_id` int(11) NOT NULL,
`first` varchar(60) DEFAULT NULL,
`last` varchar(60) DEFAULT NULL,
`email` varchar(80) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `viewers`
--
INSERT INTO `viewers` (`viewer_id`, `first`, `last`, `email`) VALUES
(1, 'Tim', 'Labonne', 'tim_labonne#live.com'),
(2, 'Alicen', 'Brightley', 'abrightley#yahoo.com'),
(3, 'Renard', 'Sartor', 'rsartor#washu.edu'),
(4, 'Luigi', 'Greco', 'elgreco#live.com'),
(5, 'Jackie', 'Linwood', 'jLinwood#yahoo.com'),
(6, 'Caroline', 'Smith', 'youknowwho#live.com');
-- --------------------------------------------------------
--
-- Table structure for table `viewings`
--
CREATE TABLE IF NOT EXISTS `viewings` (
`viewing_id` int(11) NOT NULL,
`viewer_id` int(11) NOT NULL,
`movie_id` int(11) NOT NULL,
`date_viewed` date DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `viewings`
--
INSERT INTO `viewings` (`viewing_id`, `viewer_id`, `movie_id`, `date_viewed`) VALUES
(1, 1, 4, '2008-10-07'),
(2, 1, 2, '2009-12-18'),
(3, 1, 1, '2010-02-27'),
(4, 1, 21, '2010-03-14'),
(5, 2, 21, '2015-04-15'),
(6, 2, 22, '2015-10-04'),
(7, 2, 7, '2015-11-30'),
(8, 2, 9, '2016-01-05'),
(9, 2, 12, '2016-04-14'),
(10, 2, 16, '2017-01-23'),
(11, 3, 8, '2016-02-14'),
(12, 3, 18, '2016-03-20'),
(13, 3, 22, '2016-04-07'),
(14, 4, 20, '2017-01-03'),
(15, 4, 18, '2017-01-14'),
(16, 4, 15, '2017-02-08'),
(17, 4, 10, '2007-09-23'),
(18, 4, 2, '2017-03-05'),
(19, 4, 4, '2017-04-13'),
(20, 4, 12, '2017-04-30'),
(21, 4, 14, '2017-05-02'),
(22, 4, 21, '2017-05-08'),
(23, 5, 2, '2013-08-25'),
(24, 5, 3, '2013-12-16'),
(25, 5, 7, '2014-03-18'),
(26, 6, 11, '2013-11-30'),
(27, 6, 2, '2013-12-18'),
(28, 6, 14, '2014-04-29'),
(29, 6, 5, '2016-12-03'),
(30, 6, 13, '2017-01-09'),
(31, 6, 18, '2017-02-13'),
(32, 6, 21, '2017-03-14'),
(33, 6, 15, '2017-04-15');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `directors`
--
ALTER TABLE `directors`
ADD PRIMARY KEY (`director_id`);
--
-- Indexes for table `movies`
--
ALTER TABLE `movies`
ADD PRIMARY KEY (`movie_id`),
ADD KEY `director_id` (`director_id`);
--
-- Indexes for table `viewers`
--
ALTER TABLE `viewers`
ADD PRIMARY KEY (`viewer_id`);
--
-- Indexes for table `viewings`
--
ALTER TABLE `viewings`
ADD PRIMARY KEY (`viewing_id`),
ADD KEY `viewer_id` (`viewer_id`),
ADD KEY `movie_id` (`movie_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `directors`
--
ALTER TABLE `directors`
MODIFY `director_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=10;
--
-- AUTO_INCREMENT for table `movies`
--
ALTER TABLE `movies`
MODIFY `movie_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=23;
--
-- AUTO_INCREMENT for table `viewers`
--
ALTER TABLE `viewers`
MODIFY `viewer_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=7;
--
-- AUTO_INCREMENT for table `viewings`
--
ALTER TABLE `viewings`
MODIFY `viewing_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=34;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `movies`
--
ALTER TABLE `movies`
ADD CONSTRAINT `movies_ibfk_1` FOREIGN KEY (`director_id`) REFERENCES `directors` (`director_id`);
--
-- Constraints for table `viewings`
--
ALTER TABLE `viewings`
ADD CONSTRAINT `viewings_ibfk_1` FOREIGN KEY (`viewer_id`) REFERENCES `viewers` (`viewer_id`),
ADD CONSTRAINT `viewings_ibfk_2` FOREIGN KEY (`movie_id`) REFERENCES `movies` (`movie_id`);
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
You should use the mysql command in order to import a mysqldump sql file:
mysql -u root -p db_name < ~/Documents/db_name.sql
The mysqlimport utility is used to insert data from textfiles into the database, it is a wrapper around the LOAD DATA INFILE sql statement. From mysqlimport documentation:
The mysqlimport client provides a command-line interface to the LOAD
DATA INFILE SQL statement. Most options to mysqlimport correspond
directly to clauses of LOAD DATA INFILE syntax. See Section 13.2.6,
“LOAD DATA INFILE Syntax”.
I can't reproduce the problem:
File: ~/Documents/db_name.sql
CREATE DATABASE IF NOT EXISTS `movie-buff`;
USE `movie-buff`;
--
-- Table structure for table `directors`
--
CREATE TABLE IF NOT EXISTS `directors` (
`director_id` int(11) NOT NULL,
`first` varchar(60) DEFAULT NULL,
`last` varchar(60) DEFAULT NULL,
`country` varchar(100) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `directors`
--
INSERT INTO `directors` (`director_id`, `first`, `last`, `country`)
VALUES
(1, 'Jean-Pierre', 'Jeunet', 'France'),
(2, 'Jean', 'Renoir', 'France'),
(3, 'Akira', 'Kurosawa', 'Japan'),
(4, 'Jane', 'Campion', 'New Zealand'),
(5, 'Sally', 'Potter', 'UK'),
(6, 'Kasi', 'Lemmons', 'USA'),
(7, 'Ava', 'DuVernay', 'USA'),
(8, 'Todd', 'Haynes', 'USA'),
(9, 'Marleen', 'Gorris', 'Netherlands');
MySQL:
mysql> source ~/Documents/db_name.sql
Database changed
Query OK, 0 rows affected (0.01 sec)
Query OK, 9 rows affected (0.00 sec)
Records: 9 Duplicates: 0 Warnings: 0
It also works with:
mysql> \. ~/Documents/db_name.sql
$ mysql -u user -p movie-buff < ~/Documents/db_name.sql
UPDATE
In the case of mysql> source ~/Documents/db_name.sql, mysql> \. ~/Documents/db_name.sql and $ mysql -u user -p < ~/Documents/db_name.sql (without specifying the database) the database may not be created, but must be created in the script (see script above). In the case of $ mysql -u user -p movie-buff < ~/Documents/db_name.sql the database must be created before.

MySQL three way join?

I need to amend the following SQL so that it retrieves TTFF data for items that have category "beer" set in a table called planogram
select p.product_name, count(t.product_id), avg(t.TTFF), std(t.TTFF)
from product p left join
TTFFdata t
on p.product_id = t.product_id
where planogram_id = 1 group by p.product_name
However the products don't have category data, so I need to somehow get if from the categories in the planogram table. Something like this, but with the appropriate joining to make it work:
select p.product_name, count(t.product_id), avg(t.TTFF), std(t.TTFF)
from product p left join
TTFFdata t
on p.product_id = t.product_id
where planogram.category = "beer" group by p.product_name
Two things to consider:
I'm not very experiencd with SQL.
I think the database I've been given is badly structured (no category ids on the products?!), but I probably have to work with it as it is.
Any help much appreciated.
The full sql I'm working with is below:
-- phpMyAdmin SQL Dump
-- version 4.6.5.2
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: Mar 08, 2017 at 07:50 PM
-- Server version: 10.1.21-MariaDB
-- PHP Version: 5.6.30
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `chart`
--
-- --------------------------------------------------------
--
-- Table structure for table `planogram`
--
CREATE TABLE `planogram` (
`planogram_id` int(11) NOT NULL,
`planogram_name` varchar(200) NOT NULL,
`project_name` varchar(200) NOT NULL,
`category` varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `planogram`
--
INSERT INTO `planogram` (`planogram_id`, `planogram_name`, `project_name`, `category`) VALUES
(1, 'planogramA', '', 'beer'),
(2, 'planogramB', '', 'coffee'),
(3, 'planogramC', '', 'coffee'),
(4, 'planogramD', '', 'fruit'),
(5, 'planogramE', '', 'beer');
-- --------------------------------------------------------
--
-- Table structure for table `product`
--
CREATE TABLE `product` (
`product_id` int(11) NOT NULL,
`product_name` varchar(200) NOT NULL,
`detail` varchar(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `product`
--
INSERT INTO `product` (`product_id`, `product_name`, `detail`) VALUES
(1, 'beer1', ''),
(2, 'beer2', ''),
(3, 'coffee1', ''),
(4, 'coffee2', ''),
(5, 'coffee3', ''),
(6, 'coffee4', ''),
(7, 'coffee5', ''),
(8, 'coffee6', ''),
(9, 'beer3', ''),
(10, 'beer4', ''),
(13, 'fruit1', ''),
(14, 'fruit2', '');
-- --------------------------------------------------------
--
-- Table structure for table `TTFFdata`
--
CREATE TABLE `TTFFdata` (
`record_id` int(11) NOT NULL,
`participant_id` varchar(45) NOT NULL,
`TTFF` decimal(10,2) NOT NULL,
`product_id` int(11) NOT NULL,
`planogram_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `TTFFdata`
--
INSERT INTO `TTFFdata` (`record_id`, `participant_id`, `TTFF`, `product_id`, `planogram_id`) VALUES
(1, 'a1', '6.21', 1, 1),
(2, 'a2', '5.70', 1, 1),
(3, 'a3', '6.00', 1, 1),
(4, 'b1', '3.40', 2, 1),
(5, 'b2', '4.30', 2, 1),
(6, 'b3', '6.00', 2, 1),
(7, 'c1', '7.00', 3, 2),
(8, 'c2', '8.00', 3, 2),
(9, 'c3', '5.00', 3, 2),
(10, 'd1', '8.90', 4, 2),
(11, 'd2', '3.00', 4, 2),
(12, 'd3', '4.50', 4, 2),
(15, 'e1', '8.00', 5, 2),
(16, 'e2', '9.00', 5, 2),
(17, 'f1', '5.50', 6, 3),
(18, 'f2', '5.00', 6, 3),
(19, 'g1', '5.20', 7, 3),
(20, 'g2', '3.60', 7, 3),
(21, 'h1', '5.00', 8, 3),
(22, 'h2', '5.10', 8, 3),
(23, 'i1', '6.00', 13, 4),
(24, 'i2', '7.00', 13, 4),
(25, 'j1', '4.00', 14, 4),
(26, 'j2', '6.00', 14, 4),
(27, 'k1', '6.70', 9, 5),
(28, 'k2', '6.10', 9, 5),
(29, 'l1', '3.00', 10, 5),
(30, 'l2', '5.00', 10, 5);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `planogram`
--
ALTER TABLE `planogram`
ADD PRIMARY KEY (`planogram_id`);
--
-- Indexes for table `product`
--
ALTER TABLE `product`
ADD PRIMARY KEY (`product_id`);
--
-- Indexes for table `TTFFdata`
--
ALTER TABLE `TTFFdata`
ADD PRIMARY KEY (`record_id`),
ADD KEY `product_id` (`product_id`),
ADD KEY `planogram_id` (`planogram_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `planogram`
--
ALTER TABLE `planogram`
MODIFY `planogram_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
--
-- AUTO_INCREMENT for table `product`
--
ALTER TABLE `product`
MODIFY `product_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=17;
--
-- AUTO_INCREMENT for table `TTFFdata`
--
ALTER TABLE `TTFFdata`
MODIFY `record_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=31;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
You need to include all 3 tables in the join and make sure that you group by on all relevant fields to get the correct output. Based on the sample data, each product is associated with only one planogram, so my answer is based on the assumption that this is true for your entire data:
select p.product_name, count(t.product_id), avg(t.TTFF), std(t.TTFF)
from product p
left join TTFFdata t on p.product_id = t.product_id
left join planogram pl on t.planogram_id=pl.planogram_id
where planogram.category = "beer"
group by p.product_name

Importing database to ms sql server

I am trying to import my database from phpmyadmin to sql server, but getting this error when I am trying to execute the sql code to create it:
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'IF'.
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near 'nih_bw'.
Msg 911, Level 16, State 1, Line 11
Database 'nih_bw' does not exist. Make sure that the name is entered correctly.
This is the database:
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: "nih_bw"
--
CREATE DATABASE IF NOT EXISTS "nih_bw" DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE nih_bw;
-- --------------------------------------------------------
--
-- Table structure for table "emps"
--
CREATE TABLE "emps" (
"id" int NOT NULL,
"user_name" varchar(25) NOT NULL,
"first_name" varchar(15) NOT NULL,
"last_name" varchar(15) NOT NULL,
"email" varchar(25) NOT NULL,
"password" varchar(25) NOT NULL,
"user_type" char(5) NOT NULL,
PRIMARY KEY ("id")
);
--
-- Dumping data for table "emps"
--
SET IDENTITY_INSERT "emps" ON ;
INSERT INTO "emps" ("id", "user_name", "first_name", "last_name", "email", "password", "user_type") VALUES
(1, 'root', 'root', 'root', 'root#gmail.com', 'hei123', 'user');
SET IDENTITY_INSERT "emps" OFF;
-- --------------------------------------------------------
--
-- Table structure for table "measurements"
--
CREATE TABLE "measurements" (
"id" int NOT NULL,
"title" varchar(25) NOT NULL,
PRIMARY KEY ("id")
);
--
-- Dumping data for table "measurements"
--
SET IDENTITY_INSERT "measurements" ON ;
INSERT INTO "measurements" ("id", "title") VALUES
(1, 'T_Badende_per_Time'),
(2, 'T_Temperatur'),
(3, 'T_Luft_Temperatur'),
(4, 'M_Fritt_Klor'),
(5, 'M_Bundet_Klor'),
(6, 'M_Total_Klor'),
(7, 'M_PH'),
(8, 'M_Auto_Klor'),
(9, 'M_Auto_PH'),
(10, 'M_Redox'),
(11, 'O_Vannbalanse'),
(12, 'O_Alakalitet'),
(13, 'O_Hardhet'),
(14, 'O_Natrium_Bk'),
(15, 'O_Kals_Klor'),
(16, 'O_Sjokklor'),
(17, 'O_Fellingsmiddel'),
(18, 'O_Sirkulasjonsmengde'),
(19, 'O_Filtertrykk'),
(20, 'O_Spyl_Av_Filter'),
(21, 'O_Vannforbruk'),
(22, 'O_Slamsuging'),
(23, 'O_Harsil'),
(24, 'O_Ren_Utj_Tank');
SET IDENTITY_INSERT "measurements" OFF;
-- --------------------------------------------------------
--
-- Table structure for table "measure_routine"
--
CREATE TABLE "measure_routine" (
"routine_id" int NOT NULL,
"measure_id" int NOT NULL,
"pool_id" int DEFAULT NULL,
PRIMARY KEY ("routine_id","measure_id"),
KEY "pool_id" ("pool_id"),
KEY "link3" ("measure_id")
);
-- --------------------------------------------------------
--
-- Table structure for table "pools"
--
CREATE TABLE "pools" (
"id" int NOT NULL,
"name" varchar(20) NOT NULL,
PRIMARY KEY ("id")
);
--
-- Dumping data for table "pools"
--
SET IDENTITY_INSERT "pools" ON ;
INSERT INTO "pools" ("id", "name") VALUES
(1, 'Hovedbasseng'),
(2, 'Plaskebasseng'),
(99, 'Svommehall');
SET IDENTITY_INSERT "pools" OFF;
-- --------------------------------------------------------
--
-- Table structure for table "routines"
--
CREATE TABLE "routines" (
"id" int NOT NULL,
"date" datetime NOT NULL,
"time" time NOT NULL,
"value" varchar(10) NOT NULL,
"emp_id" int NOT NULL,
PRIMARY KEY ("id"),
KEY "emp_id" ("emp_id")
);
-- --------------------------------------------------------
--
-- Table structure for table "tasks"
--
CREATE TABLE "tasks" (
"id" int NOT NULL,
"title" varchar(25) NOT NULL,
PRIMARY KEY ("id")
);
--
-- Dumping data for table "tasks"
--
SET IDENTITY_INSERT "tasks" ON ;
INSERT INTO "tasks" ("id", "title") VALUES
(40, 'D_Slamsuge_Pl_Bass'),
(41, 'D_Filter_Rens'),
(42, 'D_Bypass_Pl_Bass'),
(43, 'D_Vaske_Gulv_CM'),
(44, 'D_Garderobe_0730'),
(45, 'D_Garderobe_Dag'),
(46, 'D_Male_Temp_Just'),
(47, 'D_Vaske_Pl_Bass'),
(48, 'D_Vaske_H_Bass'),
(49, 'D_Renhold_Av_Wc'),
(50, 'D_Ettersyn_Utst_Skap'),
(51, 'D_Handicap_Heis'),
(52, 'D_Ettersyn_solarier'),
(53, 'D_Vannprove_0745'),
(54, 'D_Vannprover_1100'),
(55, 'D_Vannprover_1400'),
(56, 'D_Enkle_Vedliks_Oppg'),
(57, 'D_Sjekk_Skiftplan'),
(58, 'K_Oppdatere_Seg_IkkeUtf'),
(59, 'K_Vannprover_1700'),
(60, 'K_Vannprover_2000'),
(61, 'K_Vaske_Pl_Bass'),
(62, 'K_Vaske_H_Bass'),
(63, 'K_Sla_Av_Bypass'),
(64, 'K_Ettersyn_Materiell'),
(65, 'K_Ettersyn_Solarier'),
(66, 'K_Ettersyn_Sal_4'),
(67, 'K_Runde_Gard_Kveld'),
(68, 'K_Runde_Gard_For_Steng'),
(69, 'K_Enkle_Vedlik_Oppg'),
(70, 'K_Rullestolheis_lading'),
(71, 'K_Etterfylle_Skoposer'),
(72, 'K_Skap_Lases'),
(73, 'K_Sjekk_Skiftplan'),
(74, 'K_Rydd_Tribune'),
(75, 'K_Slamsug_H_Bass'),
(76, 'K_Stenge_Prosedyre'),
(77, 'K_Beskjed_H_Vakt'),
(78, 'K_Timeteller'),
(79, 'C_Timeteller'),
(80, 'C_AllRent'),
(81, 'C_Grovrent_Ekstra'),
(82, 'C_Ren_Opplos_Tank'),
(83, 'C_Ren_Opps_Tank'),
(84, 'C_Ren_Flott_gitter'),
(85, 'C_Ren_Opplos_Filter'),
(86, 'C_Ren_Nal'),
(87, 'C_Kontr_Slang_Tank'),
(88, 'C_Kont_Ren_KostPad');
SET IDENTITY_INSERT "tasks" OFF;
-- --------------------------------------------------------
--
-- Table structure for table "task_routine"
--
CREATE TABLE "task_routine" (
"routine_id" int NOT NULL,
"task_id" int NOT NULL,
PRIMARY KEY ("routine_id","task_id"),
KEY "link6" ("task_id")
);
--
-- Constraints for dumped tables
--
--
-- Constraints for table "measure_routine"
--
ALTER TABLE "measure_routine"
ADD CONSTRAINT "link2" FOREIGN KEY ("routine_id") REFERENCES "routines" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT "link3" FOREIGN KEY ("measure_id") REFERENCES "measurements" ("id") ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table "routines"
--
ALTER TABLE "routines"
ADD CONSTRAINT "link1" FOREIGN KEY ("emp_id") REFERENCES "emps" ("id") ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table "task_routine"
--
ALTER TABLE "task_routine"
ADD CONSTRAINT "link5" FOREIGN KEY ("routine_id") REFERENCES "routines" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT "link6" FOREIGN KEY ("task_id") REFERENCES "tasks" ("id") ON DELETE CASCADE ON UPDATE CASCADE;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
I think you need to take a look at your very first statement where you are trying to create a database , Start you script with something as follows
USE master;
GO
IF DB_ID('dbname') IS NOT NULL
DROP DATABASE nih_bw
GO
CREATE DATABASE [nih_bw] ON PRIMARY
( NAME = N'nih_bw'
, FILENAME = N'C:\Path_mdf_File\nih_bw.mdf' , SIZE = 5120000KB , FILEGROWTH = 1024KB )
LOG ON
( NAME = N'nih_bw_log'
, FILENAME = N'D:\Path_For_ldf_File\nih_bw_log.ldf' , SIZE = 512000KB , FILEGROWTH = 10%)
COLLATE SQL_SwedishStd_Pref_CP1_CI_AS
GO
USE [nih_bw]
GO
/* Now from here continue with rest of your script */