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 */
Related
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';
I want to build simple database system about bike (or bicycle) rental store.
Here are the tables, and columns in parantheses:
Model (MNr, Modelname, dayprice)
Bike (MNr, CopyNr, Frame, Color)
Customer (CNr, Name, Surename, MobileNr)
Rental (CNr, MNr, CopyNr, DateOut, DateIn)
(MNr stands for model number, CNr stands for customer number).
Models are bike models, Bike table shows data on each particular bike.
There can be several bikes of each model, to distinguish between them
"CopyNr" column is used, where each bike in a given model is numbered 1, 2, 3, etc.
Rental: A rental relationship always applies to only one bike and one customer.
So I put the following primary and foreign keys:
Model (MNr (PK), Modelname, dayprice)
Bike (MNr (FK), CopyNr (PK), Frame, Color)
Customer (CNr (PK), Firstame, Surname, MobileNr)
Rent (CNr (FK), MNr (FK), CopyNr (FK), DateOut, DateIn)
The "CopyNr" at "Rental" is referring to the "CopyNr" of "Bikes". So I used "CopyNr" as
primary key column.
But that column can contain duplicate values, like there can be copy nr 2 of model A and copy nr 2 of
model B.
And primary key columns don't allow duplicate values.
I wrote the following in the SQL file, and when but it on PHPMyAdmin.
Here's the code from SQL code:
[It had Norwegian names, I changed it to English, thus color names are Norwegian]
-- phpMyAdmin SQL Dump
-- version 4.8.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1:3306
-- Generation Time: Feb 04, 2020 at 07:15 PM
-- Server version: 10.2.26-MariaDB-log
-- PHP Version: 7.1.30
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+01: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 */;
--
--
--
-- --------------------------------------------------------
--
-- Table structure for table `Customer`
--
CREATE TABLE `Customer` (
`CNr` int(2) NOT NULL,
`Firstname` varchar(20),
`Surname` varchar(20),
`MobileNr` varchar(8),
PRIMARY KEY (CNr)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Customer` (`CNr`, `Firstname`, `Surname`, `MobileNr`) VALUES
(1, 'Olav', 'Petterses', '88888888'),
(2, 'Petter', 'Olavsson', '44444444');
-- --------------------------------------------------------
--
-- Table structure for table `Model`
--
CREATE TABLE `Model` (
`MNr` int(5) NOT NULL,
`Modelname` varchar(20),
`Dayprice` double DEFAULT NULL,
PRIMARY KEY (MNr)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Model` (`MNr`, `Modelname`, `Dayprice`) VALUES
(2, 'FirstPrice', 10),
(15, 'DBS', 50),
(16, 'DBS', 60);
-- --------------------------------------------------------
--
-- Table structure for table `Bike`
--
CREATE TABLE `Bike` (
`MNr` int(5),
`CopyNr` int(2) NOT NULL,
`Frame` int(3),
`Color` varchar(10),
PRIMARY KEY (`CopyNr`, `MNr`),
CONSTRAINT `Bike_ibfk_1` FOREIGN KEY (`MNr`) REFERENCES `Model` (`MNr`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Bike` (`MNr`, `CopyNr`, `Frame`, `Color`) VALUES
(2, 1, 55, 'rød'),
(15, 1, 65, 'rød'),
(16, 1, 55, 'grønn'),
(2, 2, 55, 'rød'),
(15, 2, 65, 'grønn');
-- --------------------------------------------------------
--
-- Table structure for table `Rent`
--
CREATE TABLE `Rent` (
`CNr` int(2),
`MNr` int(5),
`CopyNr` int(2),
`DatoUt` date,
`DateInn` date,
CHECK (`DatoUt` < `DateInn`),
CONSTRAINT `Rent_ibfk_1` FOREIGN KEY (`MNr`) REFERENCES `Model` (`MNr`),
CONSTRAINT `Rent_ibfk_2` FOREIGN KEY (`CopyNr`) REFERENCES `Bike` (`CopyNr`),
CONSTRAINT `Rent_ibfk_3` FOREIGN KEY (`CNr`) REFERENCES `Customer` (`CNr`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Rent` (`CNr`, `MNr`, `CopyNr`, `DatoUt`, `DateInn`) VALUES
(1, 15, 1, '2020-01-01', '2020-01-30'),
(2, 15, 2, '2020-02-15', '2020-02-29');
/*!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 */;
When I put it on SQL on PHPAdmin and click "GO", I got error because of CHECK ( DatoIn > DateUt) code.
And I remove that part, then I get "Rent" table empty of values.
What's wrong am I doing?
Why is the "Rent" table empty of values when I put this on SQL on PHPMyAdmin?
Should I restructure the table relations?
And why "CHECK" doesn't work?
The error message I get where I use CHECK:
I believe you're seeing the syntax warning due to a bug in the parser used by phpMyAdmin which improperly marks this syntax as incorrect. I think that bug specifically is already reported as https://github.com/phpmyadmin/sql-parser/issues/167.
As nick indicates, you can still use the query despite the syntax warning.
I think that the date out should've be less than the date in
CHECK (DatoUt < DateInn),
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
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
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.