Related
I downloaded the script that I want to install on my VPS, and as the creator who created this script said in the instructions, I import only the file that is called sql.sgl into the root folder where the database is created, but after that I can not run the script, and as I see in MySql no tables was created, so I wonder if there is a possibility to create everything from sql.sql file via commands in mysql, If someone can tell me just what commands in Linux I use to manually create all the tables and the rest from the file bellow:
anyway i just checked the error.log file and the error is on this script line 9 , please help , is this code ok ?
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_saha = "111.111.111.135";
$database_saha = "search";
$username_saha = "root";
$password_saha = "me111";
$saha = mysqli_connect($hostname_saha, $username_saha, $password_saha, $database_saha) or trigger_error(mysqli_error($saha),E_USER_ERROR);
mysqli_set_charset( $saha, 'utf8');
?>
-- version 4.6.0
-- http://www.phpmyadmin.net
--
-- Host: localhost:3306
-- Generation Time: Dec 04, 2016 at 12:02 PM
-- Server version: 5.5.52-cll-lve
-- PHP Version: 5.6.14
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: `hello`
--
-- --------------------------------------------------------
--
-- Table structure for table `admin`
--
CREATE TABLE `admin` (
`id` int(11) NOT NULL,
`un` varchar(30) NOT NULL,
`pw` varchar(30) NOT NULL,
`demo` enum('Y','N') NOT NULL DEFAULT 'Y',
`max_pages` int(11) DEFAULT '20',
`max_content` int(11) DEFAULT '500'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `admin`
--
INSERT INTO `admin` (`id`, `un`, `pw`, `demo`, `max_pages`, `max_content`) VALUES
(1, 'administrator', 'admin', 'N', 20, 500);
-- --------------------------------------------------------
--
-- Table structure for table `crawl`
--
CREATE TABLE `crawl` (
`id` int(11) NOT NULL,
`base_url` varchar(1000) NOT NULL,
`actual_url` varchar(2000) NOT NULL,
`title` varchar(2500) NOT NULL,
`description` varbinary(200) DEFAULT NULL,
`keywords` varbinary(200) DEFAULT NULL,
`content` varchar(500) DEFAULT NULL,
`current_url` varchar(2000) NOT NULL,
`last_update` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`deleted` enum('Y','N') DEFAULT 'N',
`block_update` enum('Y','N') DEFAULT 'N',
`visits` int(11) DEFAULT '0',
`manual` enum('Y','N') DEFAULT 'N'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `settings`
--
CREATE TABLE `settings` (
`id` int(11) NOT NULL,
`base_url` varchar(1000) NOT NULL,
`actual_url` varchar(2000) NOT NULL,
`demo` enum('Y','N') DEFAULT 'N',
`date` timestamp NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `admin`
--
ALTER TABLE `admin`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `un` (`un`),
ADD UNIQUE KEY `un_2` (`un`);
--
-- Indexes for table `crawl`
--
ALTER TABLE `crawl`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `settings`
--
ALTER TABLE `settings`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `admin`
--
ALTER TABLE `admin`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=18;
--
-- AUTO_INCREMENT for table `crawl`
--
ALTER TABLE `crawl`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=747;
--
-- AUTO_INCREMENT for table `settings`
--
ALTER TABLE `settings`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=26;
/*!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 */;```
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
MySQL gives me this error:
Error Code: #1822 - Failed to add the foreign key constraint. Missing index for constraint '' in the referenced table 'produtos'
This is my database:
-- phpMyAdmin SQL Dump
-- version 4.7.7
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: 10-Jun-2018 às 22:31
-- Versão do servidor: 5.6.39
-- PHP Version: 5.6.30
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
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 */;
--
--
--
-- --------------------------------------------------------
--
-- Estrutura da tabela `fornecedores`
--
CREATE TABLE `fornecedores` (
`Id_Forn` int(9) NOT NULL,
`Nome` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`Empresa` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`Descrição` mediumtext COLLATE utf8_unicode_ci NOT NULL,
`Contacto` int(9) NOT NULL,
`Referencia` int(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Estrutura da tabela `funcionários`
--
CREATE TABLE `funcionários` (
`ID_Func` int(9) NOT NULL,
`First_name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`Last_name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`Email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`User` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`PassWord` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`Contacto` int(9) NOT NULL,
`NIF` int(9) NOT NULL,
`Morada` mediumtext COLLATE utf8_unicode_ci NOT NULL,
`Vencimento` int(9) NOT NULL,
`Cargo` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=COMPACT;
--
-- Extraindo dados da tabela `funcionários`
--
-- --------------------------------------------------------
--
-- Estrutura da tabela `newsletter`
--
CREATE TABLE `newsletter` (
`Email` varchar(100) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Estrutura da tabela `produtos`
--
CREATE TABLE `produtos` (
`Referencia` int(20) NOT NULL,
`Tipo` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`Nome` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
`Descricao` varchar(10000) COLLATE utf8_unicode_ci NOT NULL,
`Imagens` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`Stock` int(3) NOT NULL,
`Numero_Serie` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`Preco` int(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Extraindo dados da tabela `produtos`
--
-- --------------------------------------------------------
--
-- Estrutura da tabela `utilizadores`
--
CREATE TABLE `utilizadores` (
`ID_Uti` int(9) NOT NULL,
`First_name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`Las_name` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
`Email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`Contacto` int(9) NOT NULL,
`PassWord` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`Newsletter` int(1) DEFAULT NULL,
`Morada` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`Codigo_Postal` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL,
`Conselho` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`NIF` int(9) DEFAULT NULL,
`Ativo` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Extraindo dados da tabela `utilizadores`
--
-- --------------------------------------------------------
--
-- Estrutura da tabela `vendas`
--
CREATE TABLE `vendas` (
`ID_Vendas` int(9) NOT NULL,
`ID_Uti` int(9) NOT NULL,
`Referencia` int(20) NOT NULL,
`Data` date NOT NULL,
`Estado` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `fornecedores`
--
ALTER TABLE `fornecedores`
ADD PRIMARY KEY (`Id_Forn`),
ADD UNIQUE KEY `Contacto` (`Contacto`),
ADD UNIQUE KEY `Referencia` (`Referencia`),
ADD FOREIGN KEY (`Referencia`) REFERENCES `produtos` (`Referencia`) ;
--
-- Indexes for table `funcionários`
--
ALTER TABLE `funcionários`
ADD PRIMARY KEY (`ID_Func`),
ADD UNIQUE KEY `Email` (`Email`),
ADD UNIQUE KEY `Contacto` (`Contacto`);
--
-- Indexes for table `newsletter`
--
ALTER TABLE `newsletter`
ADD PRIMARY KEY (`Email`);
--
-- Indexes for table `produtos`
--
ALTER TABLE `produtos`
ADD PRIMARY KEY (`Referencia`),
ADD UNIQUE KEY `Nome` (`Nome`);
--
-- Indexes for table `utilizadores`
--
ALTER TABLE `utilizadores`
ADD PRIMARY KEY (`ID_Uti`),
ADD UNIQUE KEY `Email` (`Email`);
--
-- Indexes for table `vendas`
--
ALTER TABLE `vendas`
ADD PRIMARY KEY (`ID_Vendas`),
ADD UNIQUE KEY `ID_Uti` (`ID_Uti`),
ADD UNIQUE KEY `Referencia` (`Referencia`),
ADD FOREIGN KEY (`ID_Uti`) REFERENCES `utilizadores` (`ID_Uti`),
ADD FOREIGN KEY (`Referencia`) REFERENCES `produtos` (`Referencia`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `fornecedores`
--
ALTER TABLE `fornecedores`
MODIFY `Id_Forn` int(9) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `funcionários`
--
ALTER TABLE `funcionários`
MODIFY `ID_Func` int(9) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=15582;
--
-- AUTO_INCREMENT for table `produtos`
--
ALTER TABLE `produtos`
MODIFY `Referencia` int(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=96455;
--
-- AUTO_INCREMENT for table `vendas`
--
ALTER TABLE `vendas`
MODIFY `ID_Vendas` int(9) NOT NULL AUTO_INCREMENT;
COMMIT;
I can't understand where the error is.
Foreign keys need to point to a primary key or unique constraint. However, the script you are using tries to create a foreign key when the referenced column is not (yet) a primary key.
See the error below?
ALTER TABLE `fornecedores`
ADD PRIMARY KEY (`Id_Forn`),
ADD UNIQUE KEY `Contacto` (`Contacto`),
ADD UNIQUE KEY `Referencia` (`Referencia`),
ADD FOREIGN KEY (`Referencia`) REFERENCES `produtos` (`Referencia`) ;
At this point the column Referencia is not yet a primary key on produtos. It's made a primary key a few lines later in your script.
Solution? Just rearrange your script to create all primary keys first, and foreign keys second. Easy.
I have a big website, at the moment i have about 10k unique visitors daily, I have 2000 categories, 12k Tags, 36k custom taxonomies.
It's not a design flaw, as its a website to watch online movies, I used, categories for Seasons and stuff like that, the custom taxonomies are Actors, regizors etc.
The problem is that my hosting keeps crashing with 100% processor, the resources cannot be an issue, as i have my own server with 4xXeon, 64 GB ram.
I have isolated the problem, and it's due to very slow queries for example, what are my options? :
# Time: 160830 17:01:20
# User#Host: razvypp[razvypp] # localhost []
# Query_time: 9.159090 Lock_time: 0.001680 Rows_sent: 30 Rows_examined: 420117
SET timestamp=1472565680;
SELECT wphn_posts.ID
FROM wphn_posts
INNER JOIN wphn_term_relationships
ON (wphn_posts.ID = wphn_term_relationships.object_id)
WHERE 1=1
AND ( wphn_term_relationships.term_taxonomy_id IN (3630,
4955,4956,4957,4958,4959,4960,4961,4962,4963,4964,4965,
4966,4967,4968,4969,4970,4971,4972,4973,4974,4975,4976,
4977,4978,4979,4980,4981,4982,4983,4984,4985,4986,4987,
4988,4989,4990,4991,4992,4993,4994,4995,4996,4997,4998,
4999,5000,5001,5002,5003,5004,5005,5006,5007,5008,5009,
5010,5011,5012, ... (hundreds of numbers) ...,49740,49779)
Here is a dump of the tables
-- phpMyAdmin SQL Dump
-- version 4.4.15
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: 30 Aug 2016 la 15:17
-- Versiune server: 5.5.51-log
-- PHP Version: 5.5.37
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: `trolio`
--
-- --------------------------------------------------------
--
-- Structura de tabel pentru tabelul `wphn_posts`
--
CREATE TABLE IF NOT EXISTS `wphn_posts` (
`ID` bigint(20) unsigned NOT NULL,
`movie_type` int(2) NOT NULL,
`post_author` bigint(20) unsigned NOT NULL DEFAULT '0',
`post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_date_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_content` longtext NOT NULL,
`post_title` text NOT NULL,
`post_excerpt` text NOT NULL,
`post_status` varchar(20) NOT NULL DEFAULT 'publish',
`comment_status` varchar(20) NOT NULL DEFAULT 'open',
`ping_status` varchar(20) NOT NULL DEFAULT 'open',
`post_password` varchar(20) NOT NULL DEFAULT '',
`post_name` varchar(200) NOT NULL DEFAULT '',
`to_ping` text NOT NULL,
`pinged` text NOT NULL,
`post_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_modified_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_content_filtered` longtext NOT NULL,
`post_parent` int(20) unsigned NOT NULL DEFAULT '0',
`guid` varchar(255) NOT NULL DEFAULT '',
`menu_order` int(11) NOT NULL DEFAULT '0',
`post_type` varchar(20) NOT NULL DEFAULT 'post',
`post_mime_type` varchar(100) NOT NULL DEFAULT '',
`comment_count` bigint(20) NOT NULL DEFAULT '0',
`views` int(99) NOT NULL,
`views_this_week` int(5) NOT NULL,
`imdb` double NOT NULL,
`imdb_votes` float NOT NULL,
`imdb_id` varchar(20) NOT NULL,
`fb_shares_root` int(5) NOT NULL,
`movie_name` varchar(140) NOT NULL,
`season` int(2) NOT NULL,
`episode` int(5) NOT NULL,
`serial_id` int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structura de tabel pentru tabelul `wphn_term_relationships`
--
CREATE TABLE IF NOT EXISTS `wphn_term_relationships` (
`object_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`term_taxonomy_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`term_order` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structura de tabel pentru tabelul `wphn_term_taxonomy`
--
CREATE TABLE IF NOT EXISTS `wphn_term_taxonomy` (
`term_taxonomy_id` bigint(20) unsigned NOT NULL,
`term_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`taxonomy` varchar(32) NOT NULL DEFAULT '',
`description` longtext NOT NULL,
`parent` bigint(20) unsigned NOT NULL DEFAULT '0',
`count` bigint(20) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `wphn_posts`
--
ALTER TABLE `wphn_posts`
ADD PRIMARY KEY (`ID`),
ADD KEY `post_name` (`post_name`(191)),
ADD KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`),
ADD KEY `post_parent` (`post_parent`),
ADD KEY `post_author` (`post_author`),
ADD KEY `serial_id` (`serial_id`),
ADD KEY `episode` (`episode`),
ADD KEY `season` (`season`),
ADD KEY `movie_name` (`movie_name`);
--
-- Indexes for table `wphn_term_relationships`
--
ALTER TABLE `wphn_term_relationships`
ADD PRIMARY KEY (`object_id`,`term_taxonomy_id`),
ADD KEY `term_taxonomy_id` (`term_taxonomy_id`),
ADD KEY `term_order` (`term_order`);
--
-- Indexes for table `wphn_term_taxonomy`
--
ALTER TABLE `wphn_term_taxonomy`
ADD PRIMARY KEY (`term_taxonomy_id`),
ADD UNIQUE KEY `term_id_taxonomy` (`term_id`,`taxonomy`),
ADD KEY `taxonomy` (`taxonomy`),
ADD KEY `parent` (`parent`),
ADD KEY `count` (`count`),
ADD KEY `term_id` (`term_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `wphn_posts`
--
ALTER TABLE `wphn_posts`
MODIFY `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `wphn_term_taxonomy`
--
ALTER TABLE `wphn_term_taxonomy`
MODIFY `term_taxonomy_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT;
/*!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 */;
"Rows examined" = 420K -- That smacks of a missing index.
Probably missing:
INDEX(term_taxonomy_id, object_id)
Please provide SHOW CREATE TABLE for the two tables.
What is the value of innodb_buffer_pool_size?
I understand this is a common and annoying problem so before I ask for an explanation I just want to say sorry.
As the title says I've been getting a 1064 syntax error from my MySQL db I imported in to php myadmin running wamp and building through netbeans.
I've checked out how other posts on the matter and have done reading about proper syntax but cannot see what my problem is.
An explanation would be greatly appreciated as I really want to learn this and improve my skills.
(if you have any questions about my code please ask)
Here's my full code:
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 utf8 */;
--
-- Database: `mydb`
--
CREATE DATABASE IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `mydb`;
-- --------------------------------------------------------
--
-- Table structure for table `follow`
--
DROP TABLE IF EXISTS `follow`;
CREATE TABLE IF NOT EXISTS `follow` (
`follow_id` int(11) NOT NULL,
`follow_current_user` varchar(255) NOT NULL,
`follow_profile_user` varchar(255) NOT NULL,
`follow_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=17 ;
--
-- Dumping data for table `follow`
--
INSERT INTO `follow` (`follow_id`, `follow_current_user`, `follow_profile_user`, `follow_timestamp`) VALUES
(13, 'dude', 'the', '2015-10-10 03:09:58'),
-- --------------------------------------------------------
--
-- Table structure for table `user`
--
DROP TABLE IF EXISTS `user`;
CREATE TABLE IF NOT EXISTS `user` (
`user_id` int(11) NOT NULL,
`user_email` varchar(255) NOT NULL,
`user_password` varchar(255) NOT NULL,
`user_firstname` text NOT NULL,
`user_lastname` text NOT NULL,
`user_avatar` varchar(255) NOT NULL,
`user_shortbio` text,
`user_username` varchar(255) NOT NULL,
`user_longbio` text,
`user_facebook` varchar(255) DEFAULT NULL,
`user_twitter` varchar(255) DEFAULT NULL,
`user_linkedin` varchar(255) DEFAULT NULL,
`user_stumbleupon` varchar(255) DEFAULT NULL,
`user_pinterest` varchar(255) DEFAULT NULL,
`user_googleplus` varchar(255) DEFAULT NULL,
`user_website` varchar(255) DEFAULT NULL,
`user_dob` date DEFAULT NULL,
`user_profession` text,
`user_gender` varchar(255) DEFAULT NULL,
`user_maritialstatus` varchar(255) DEFAULT NULL,
`user_address` text,
`user_backgroundpicture` varchar(255) NOT NULL,
`user_joindate` date NOT NULL,
`user_country` varchar(255) DEFAULT NULL,
`user_skype` varchar(255) DEFAULT NULL,
`user_github` varchar(255) DEFAULT NULL,
`user_youtube` varchar(255) DEFAULT NULL,
`user_vimeo` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=18 ;
--
-- Dumping data for table `user`
--
INSERT INTO `user` (`user_id`, `user_email`, `user_password`, `user_firstname`, `user_lastname`, `user_avatar`, `user_shortbio`, `user_username`, `user_longbio`, `user_facebook`, `user_twitter`, `user_linkedin`, `user_stumbleupon`, `user_pinterest`, `user_googleplus`, `user_website`, `user_dob`, `user_profession`, `user_gender`, `user_maritialstatus`, `user_address`, `user_backgroundpicture`, `user_joindate`, `user_country`, `user_skype`, `user_github`, `user_youtube`, `user_vimeo`) VALUES
(1, 'me#gmail.com', '123password123', 'Forname', 'Surname', '1470024_601216136604826_1065650331_n-884862517.jpg', 'Lorem ipsum.', 'username', 'Lorem ipsum', NULL, NULL, NULL, NULL, NULL, NULL, 'mysite.com', '1987-11-11', 'Student', 'Male', NULL, 'kablah', 'untitled-90969185.jpg', '2015- 11-11', 'Country', NULL, NULL, NULL, NULL),
--
-- --------------------------------------------------------
--
-- Table structure for table `view`
--
DROP TABLE IF EXISTS `view`;
CREATE TABLE IF NOT EXISTS `view` (
`view_id` int(11) NOT NULL,
`view_profile_user` varchar(255) NOT NULL,
`view_current_user` varchar(255) NOT NULL,
`view_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=123 ;
--
-- Dumping data for table `view`
--
INSERT INTO `view` (`view_id`, `view_profile_user`, `view_current_user`, `view_timestamp`) VALUES
(65, 'blub', 'rugsworth', '2015-10-18 19:30:56'),
--
-- Indexes for dumped tables
--
--
-- Indexes for table `follow`
--
ALTER TABLE `follow`
ADD PRIMARY KEY (`follow_id`);
--
-- Indexes for table `user`
--
ALTER TABLE `user`
ADD PRIMARY KEY (`user_id`), ADD UNIQUE KEY `user_email` (`user_email`), ADD UNIQUE KEY `user_username` (`user_username`);
--
-- Indexes for table `view`
--
ALTER TABLE `view`
ADD PRIMARY KEY (`view_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `follow`
--
ALTER TABLE `follow`
MODIFY `follow_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=17;
--
-- AUTO_INCREMENT for table `user`
--
ALTER TABLE `user`
MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=18;
--
-- AUTO_INCREMENT for table `view`
--
ALTER TABLE `view`
MODIFY `view_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=123;
/*!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 */;
And just the part of my code I think is causing the problem:
-- Dumping data for table `user`
--
INSERT INTO `user` (`user_id`, `user_email`, `user_password`, `user_firstname`, `user_lastname`, `user_avatar`, `user_shortbio`, `user_username`, `user_longbio`, `user_facebook`, `user_twitter`, `user_linkedin`, `user_stumbleupon`, `user_pinterest`, `user_googleplus`, `user_website`, `user_dob`, `user_profession`, `user_gender`, `user_maritialstatus`, `user_address`, `user_backgroundpicture`, `user_joindate`, `user_country`, `user_skype`, `user_github`, `user_youtube`, `user_vimeo`) VALUES
(1, 'me#gmail.com', '123password123', 'Forname', 'Surname', '1470024_601216136604826_1065650331_n-884862517.jpg', 'Lorem ipsum.', 'username', 'Lorem ipsum', NULL, NULL, NULL, NULL, NULL, NULL, 'mysite.com', '1987-11-11', 'Student', 'Male', NULL, 'kablah', 'untitled-90969185.jpg', '2015- 11-11', 'Country', NULL, NULL, NULL, NULL),
--
-- --------------------------------------------------------
--
-- Table structure for table `view`
--
DROP TABLE IF EXISTS `view`;
CREATE TABLE IF NOT EXISTS `view` (
`view_id` int(11) NOT NULL,
`view_profile_user` varchar(255) NOT NULL,
`view_current_user` varchar(255) NOT NULL,
`view_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=123 ;
--
-- Dumping data for table `view`
--
INSERT INTO `view` (`view_id`, `view_profile_user`, `view_current_user`, `view_timestamp`) VALUES
(65, 'blub', 'rugsworth', '2015-10-18 19:30:56'),
You have a comma at the end of your INSERT INTO 'view' statement instead of a semicolon.
Should be:
INSERT INTO `view` (`view_id`, `view_profile_user`, `view_current_user`, `view_timestamp`)
VALUES (65, 'blub', 'rugsworth', '2015-10-18 19:30:56');
Does that fix it up?
I am having some trouble with phpMyAdmin and MySQL. All of the tables load just fine except for the order table. No matter if I do it all at once, or one table at a time, I get a #1215 - Cannot add foreign key constraint.
This happens for the Orders table only and the Customer_Number attribute. What in the world am I missing here. Thanks in advance.
-- phpMyAdmin SQL Dump
-- version 4.2.11
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Apr 19, 2015 at 01:22 AM
-- Server version: 5.6.21
-- PHP Version: 5.6.3
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
SET foreign_key_checks=0;
/*!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: `popcorn`
-- --------------------------------------------------------
-- Table structure for table `customer`
CREATE TABLE IF NOT EXISTS `customer` (
`Scout_Number` int(10) NOT NULL,
`Customer_Number` int(10) NOT NULL,
`Fname` varchar(15) NOT NULL,
`Lname` varchar(15) NOT NULL,
`House_Number` int(7) NOT NULL,
`Street` varchar(15) NOT NULL,
`City` varchar(15) NOT NULL,
`State` char(2) NOT NULL,
`Zip` int(5) NOT NULL,
`Phone` int(10) NOT NULL,
`Email` varchar(30) NOT NULL,
PRIMARY KEY (Scout_Number, Customer_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
-- Table structure for table `den`
CREATE TABLE IF NOT EXISTS `den` (
`Den_Number` int(3) NOT NULL,
`User_Name` varchar(8) NOT NULL,
`Fname` varchar(15) NOT NULL,
`Lname` varchar(15) NOT NULL,
`Phone` int(10) NOT NULL,
`Email` varchar(30) NOT NULL,
`Den_City` varchar(15) NOT NULL,
`Sales_Goal` int(10) NOT NULL,
`Den_Sales_Total` decimal(10,2) NOT NULL,
`Den_State` char(2) NOT NULL,
PRIMARY KEY (Den_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- Dumping data for table `den`
-- Table structure for table `order`
CREATE TABLE IF NOT EXISTS `order` (
`Order_Number` int(10) NOT NULL,
`Customer_Number` int(10) NOT NULL,
`Donation` decimal(5,2) NOT NULL,
`Order_Total` decimal(5,2) NOT NULL,
`Payment_Status` char(1) NOT NULL,
`Payment_Type` varchar(10) NOT NULL,
`Date` date NOT NULL,
`Delivery_Status` char(1) NOT NULL,
PRIMARY KEY (Order_Number),
FOREIGN KEY (Customer_Number) REFERENCES customer(Customer_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
-- Table structure for table `order_product`
CREATE TABLE IF NOT EXISTS `order_product` (
`Order_Number` int(10) NOT NULL,
`Product_Number` int(10) NOT NULL,
PRIMARY KEY (Order_Number, Product_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
-- Table structure for table `product`
CREATE TABLE IF NOT EXISTS `product` (
`Product_Number` int(10) NOT NULL,
`Product_Name` varchar(15) NOT NULL,
`Description` text NOT NULL,
`Image` blob NOT NULL,
`Price` decimal(5,2) NOT NULL,
PRIMARY KEY (Product_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
-- Table structure for table `scout`
CREATE TABLE IF NOT EXISTS `scout` (
`Scout_Number` int(10) NOT NULL,
`User_Name` char(8) NOT NULL,
`Fname` varchar(15) NOT NULL,
`Lname` varchar(15) NOT NULL,
`Sales_Goal` decimal(10,2) NOT NULL,
`Prize_Progress` int(10) NOT NULL,
`Den_Number` int(3) NOT NULL,
PRIMARY KEY (Scout_Number),
FOREIGN KEY (Den_Number) REFERENCES den(Den_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!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 */;
Columns referenced in a foreign key have to be indexed. You don't have an index on Customer_Number in the customer table. Either change the order of the columns in the composite primary key to(Customer_Number, Scout_Number)or add an additional key just on theCustomer_Number` column.
Note, however, that having a foreign key pointing to a non-unique column is a MySQL extension to SQL, and likely to be a bad idea. See Can a foreign key reference a non-unique index?. I wonder why the primary key of the customer table isn't just Customer_Number.