MySQL Cannot add foreign key constraint Error Code: 1215 - mysql
I am making a simple MySQL database but i keep getting an error:Cannot add foreign key constraint any help appreciated!
Also a Side question is there any difference between using symbol ` and ' while creating your database?
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET #OLD_SQL_NOTES=##SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`a3823833_MiniPost` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `a3823833_MiniPost`;
/*Table structure for table `user` */
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`userid` bigint(10) NOT NULL,
`username` varchar(20) NOT NULL,
`password` varchar(20) NOT NULL,
`email` varchar(50) NOT NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*Table structure for table `profile` */
DROP TABLE IF EXISTS `profile`;
CREATE TABLE `profile` (
`userid` int(11),
`lastName` varchar(50),
`firstName` varchar(50),
`dob` varchar(10),
PRIMARY KEY (`userid`),
CONSTRAINT `profile_fk` FOREIGN KEY (`userid`) REFERENCES `user` (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
the bigint(10) and int(11) are not the same. make em same, and pk on parent table (as u are)
Related
Problem with importing mysql workbench data
I have a table and data. When importing, automatically importing data into the user table throws a foreign key error. How do I load data into a table? I understand that the problem is in the foreign key and the difference in id and name, but how to import the data? need to write some kind of request? This is the csv of the loaded data Administrator,j.doe#amonic.com,123,John,Doe,Abu dhabi,1/13/1983,1 User,k.omar#amonic.com,4258,Karim,Omar,Abu dhabi,3/19/1980,1 User,h.saeed#amonic.com,2020,Hannan,Saeed,Cairo,12/20/1989,1 User,a.hobart#amonic.com,6996,Andrew,Hobart,Riyadh,1/30/1990,1 User,k.anderson#amonic.com,4570,Katrin,Anderson,Doha,11/10/1992,1 User,h.wyrick#amonic.com,1199,Hava,Wyrick,Abu dhabi,8/8/1988,1 User,marie.horn#amonic.com,55555,Marie,Horn,Bahrain,4/6/1981,1 User,m.osteen#amonic.com,9800,Milagros,Osteen,Abu dhabi,2/3/1991,0 and this is the mysql code -- Dumping data for table `countries` -- LOCK TABLES `countries` WRITE; /*!40000 ALTER TABLE `countries` DISABLE KEYS */; INSERT INTO `countries` VALUES (1,'Afghanistan'),(2,'Albania'),(3,'Algeria'),(4,'Andorra'),(5,'Angola'),(6,'Antigua & Deps'),(7,'Argentina'),(8,'Armenia'),(9,'Australia'),(10,'Austria'),(11,'Azerbaijan'),(12,'Bahamas'),(13,'Bahrain'),(14,'Bangladesh'),(15,'Barbados'),(16,'Belarus'),(17,'Belgium'),(18,'Belize'),(19,'Benin'),(20,'Bhutan'),(21,'Bolivia'),(22,'Bosnia Herzegovina'),(23,'Botswana'),(24,'Brazil'),(25,'Brunei'),(26,'Bulgaria'),(27,'Burkina'),(28,'Burundi'),(29,'Cambodia'),(30,'Cameroon'),(31,'Canada'),(32,'Cape Verde'),(33,'Central African Rep'),(34,'Chad'),(35,'Chile'),(36,'China'),(37,'Colombia'),(38,'Comoros'),(39,'Congo'),(40,'Congo {Democratic Rep}'),(41,'Costa Rica'),(42,'Croatia'),(43,'Cuba'),(44,'Cyprus'),(45,'Czech Republic'),(46,'Denmark'),(47,'Djibouti'),(48,'Dominica'),(49,'Dominican Republic'),(50,'East Timor'),(51,'Ecuador'),(52,'Egypt'),(53,'El Salvador'),(54,'Equatorial Guinea'),(55,'Eritrea'),(56,'Estonia'),(57,'Ethiopia'),(58,'Fiji'),(59,'Finland'),(60,'France'),(61,'Gabon'),(62,'Gambia'),(63,'Georgia'),(64,'Germany'),(65,'Ghana'),(66,'Greece'),(67,'Grenada'),(68,'Guatemala'),(69,'Guinea'),(70,'Guinea-Bissau'),(71,'Guyana'),(72,'Haiti'),(73,'Honduras'),(74,'Hungary'),(75,'Iceland'),(76,'India'),(77,'Indonesia'),(78,'Iran'),(79,'Iraq'),(80,'Ireland {Republic}'),(81,'Israel'),(82,'Italy'),(83,'Ivory Coast'),(84,'Jamaica'),(85,'Japan'),(86,'Jordan'),(87,'Kazakhstan'),(88,'Kenya'),(89,'Kiribati'),(90,'Korea North'),(91,'Korea South'),(92,'Kosovo'),(93,'Kuwait'),(94,'Kyrgyzstan'),(95,'Laos'),(96,'Latvia'),(97,'Lebanon'),(98,'Lesotho'),(99,'Liberia'),(100,'Libya'),(101,'Liechtenstein'),(102,'Lithuania'),(103,'Luxembourg'),(104,'Macedonia'),(105,'Madagascar'),(106,'Malawi'),(107,'Malaysia'),(108,'Maldives'),(109,'Mali'),(110,'Malta'),(111,'Marshall Islands'),(112,'Mauritania'),(113,'Mauritius'),(114,'Mexico'),(115,'Micronesia'),(116,'Moldova'),(117,'Monaco'),(118,'Mongolia'),(119,'Montenegro'),(120,'Morocco'),(121,'Mozambique'),(122,'Myanmar, {Burma}'),(123,'Namibia'),(124,'Nauru'),(125,'Nepal'),(126,'Netherlands'),(127,'New Zealand'),(128,'Nicaragua'),(129,'Niger'),(130,'Nigeria'),(131,'Norway'),(132,'Oman'),(133,'Pakistan'),(134,'Palau'),(135,'Panama'),(136,'Papua New Guinea'),(137,'Paraguay'),(138,'Peru'),(139,'Philippines'),(140,'Poland'),(141,'Portugal'),(142,'Qatar'),(143,'Romania'),(144,'Russian Federation'),(145,'Rwanda'),(146,'St Kitts & Nevis'),(147,'St Lucia'),(148,'Saint Vincent & the Grenadines'),(149,'Samoa'),(150,'San Marino'),(151,'Sao Tome & Principe'),(152,'Saudi Arabia'),(153,'Senegal'),(154,'Serbia'),(155,'Seychelles'),(156,'Sierra Leone'),(157,'Singapore'),(158,'Slovakia'),(159,'Slovenia'),(160,'Solomon Islands'),(161,'Somalia'),(162,'South Africa'),(163,'South Sudan'),(164,'Spain'),(165,'Sri Lanka'),(166,'Sudan'),(167,'Suriname'),(168,'Swaziland'),(169,'Sweden'),(170,'Switzerland'),(171,'Syria'),(172,'Taiwan'),(173,'Tajikistan'),(174,'Tanzania'),(175,'Thailand'),(176,'Togo'),(177,'Tonga'),(178,'Trinidad & Tobago'),(179,'Tunisia'),(180,'Turkey'),(181,'Turkmenistan'),(182,'Tuvalu'),(183,'Uganda'),(184,'Ukraine'),(185,'United Arab Emirates'),(186,'United Kingdom'),(187,'United States'),(188,'Uruguay'),(189,'Uzbekistan'),(190,'Vanuatu'),(191,'Vatican City'),(192,'Venezuela'),(193,'Vietnam'),(194,'Yemen'),(195,'Zambia'),(196,'Zimbabwe'); /*!40000 ALTER TABLE `countries` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `offices` -- DROP TABLE IF EXISTS `offices`; /*!40101 SET #saved_cs_client = ##character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `offices` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `CountryID` int(11) NOT NULL, `Title` varchar(50) COLLATE utf8_bin NOT NULL, `Phone` varchar(50) COLLATE utf8_bin NOT NULL, `Contact` varchar(250) COLLATE utf8_bin NOT NULL, PRIMARY KEY (`ID`), KEY `FK_Office_Country` (`CountryID`), CONSTRAINT `FK_Office_Country` FOREIGN KEY (`CountryID`) REFERENCES `countries` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; /*!40101 SET character_set_client = #saved_cs_client */; -- -- Dumping data for table `offices` -- LOCK TABLES `offices` WRITE; /*!40000 ALTER TABLE `offices` DISABLE KEYS */; INSERT INTO `offices` VALUES (1,185,'Abu dhabi','638-757-8582\r\n','MIchael Malki'),(3,52,'Cairo','252-224-8525','David Johns'),(4,13,'Bahrain','542-227-5825','Katie Ballmer'),(5,142,'Doha','758-278-9597','Ariel Levy'),(6,152,'Riyadh','285-285-1474','Andrew Hobart'); /*!40000 ALTER TABLE `offices` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `roles` -- DROP TABLE IF EXISTS `roles`; /*!40101 SET #saved_cs_client = ##character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `roles` ( `ID` int(11) NOT NULL, `Title` varchar(50) COLLATE utf8_bin NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; /*!40101 SET character_set_client = #saved_cs_client */; -- -- Dumping data for table `roles` -- LOCK TABLES `roles` WRITE; /*!40000 ALTER TABLE `roles` DISABLE KEYS */; INSERT INTO `roles` VALUES (1,'Administrator'),(2,'User'); /*!40000 ALTER TABLE `roles` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `users` -- DROP TABLE IF EXISTS `users`; /*!40101 SET #saved_cs_client = ##character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `users` ( `ID` int(11) NOT NULL, `RoleID` int(11) NOT NULL, `Email` varchar(150) COLLATE utf8_bin NOT NULL, `Password` varchar(50) COLLATE utf8_bin NOT NULL, `FirstName` varchar(50) COLLATE utf8_bin DEFAULT NULL, `LastName` varchar(50) COLLATE utf8_bin NOT NULL, `OfficeID` int(11) DEFAULT NULL, `Birthdate` date DEFAULT NULL, `Active` tinyint(1) DEFAULT NULL, PRIMARY KEY (`ID`), KEY `FK_Users_Offices` (`OfficeID`), KEY `FK_Users_Roles` (`RoleID`), CONSTRAINT `FK_Users_Offices` FOREIGN KEY (`OfficeID`) REFERENCES `offices` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `FK_Users_Roles` FOREIGN KEY (`RoleID`) REFERENCES `roles` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; /*!40101 SET character_set_client = #saved_cs_client */; -- -- Dumping data for table `users` --
MySQL Procedure with parameter syntax
I have a schema with lists of characters from star wars, the movies they appear in, the planets they visit, etc. Here is the schema: CREATE DATABASE IF NOT EXISTS `starwarsFINAL` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `starwarsFINAL`; DROP TABLE IF EXISTS `characters`; /*!40101 SET #saved_cs_client = ##character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `characters` ( `character_name` varchar(45) NOT NULL, `race` varchar(45) DEFAULT NULL, `homeworld` varchar(45) DEFAULT 'Unknown', `affiliation` varchar(45) DEFAULT NULL, PRIMARY KEY (`character_name`), KEY `planet_fk` (`homeworld`), CONSTRAINT `planet_fk` FOREIGN KEY (`homeworld`) REFERENCES `planets` (`planet_name`) ON DELETE SET NULL ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = #saved_cs_client */; DROP TABLE IF EXISTS `movies`; /*!40101 SET #saved_cs_client = ##character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `movies` ( `movie_id` int(11) NOT NULL, `title` varchar(128) DEFAULT NULL, `scenes_in_db` int(11) DEFAULT NULL, `scenes_in_movies` int(11) DEFAULT NULL, PRIMARY KEY (`movie_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = #saved_cs_client */; DROP TABLE IF EXISTS `planets`; /*!40101 SET #saved_cs_client = ##character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `planets` ( `planet_name` varchar(45) NOT NULL, `planet_type` varchar(30) DEFAULT NULL, `affiliation` varchar(30) DEFAULT NULL, PRIMARY KEY (`planet_name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = #saved_cs_client */; DROP TABLE IF EXISTS `timetable`; /*!40101 SET #saved_cs_client = ##character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `timetable` ( `character_name` varchar(45) DEFAULT NULL, `planet_name` varchar(45) DEFAULT 'Unknown', `movie_id` int(11) DEFAULT NULL, `arrival` int(11) DEFAULT NULL, `departure` int(11) DEFAULT NULL, `time_id` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`time_id`), UNIQUE KEY `timetable_un` (`character_name`,`planet_name`,`movie_id`,`arrival`), KEY `timetable_fkplanet` (`planet_name`), KEY `timetable_fkmovie` (`movie_id`), CONSTRAINT `timetable_fkcharacter` FOREIGN KEY (`character_name`) REFERENCES `characters` (`character_name`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `timetable_fkmovie` FOREIGN KEY (`movie_id`) REFERENCES `movies` (`movie_id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `timetable_fkplanet` FOREIGN KEY (`planet_name`) REFERENCES `planets` (`planet_name`) ON DELETE SET NULL ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=57 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = #saved_cs_client */; Here's my problem: Write a procedure track_planet(planet) that accepts a planet name and returns a result set that contain the planet name, the movie name, and the number of characters that appear on that planet during that movie. Here's what I have so far, but I'm lost/stuck on how to make it work. (I'm new to MySQL) DROP PROCEDURE IF EXISTS track_planet; DELIMITER $$ CREATE PROCEDURE track_planet(IN planet VARCHAR(45)) BEGIN SELECT planet_name FROM planets, title FROM movies, COUNT(DISTINCT character_name) FROM characters WHERE planet_name = planet; END DELIMITER;
Hello_ friend. First thanks for posting the schema of your database it really helps. From it I was able to made this picture: And I've changed your code to this: DELIMITER $$ DROP PROCEDURE IF EXISTS track_planet $$ CREATE PROCEDURE track_planet(IN planet VARCHAR(45)) BEGIN SELECT tt.planet_name as planetName, m.title as movieName, COUNT(tt.character_name) as characters FROM timetable tt LEFT JOIN movies m ON tt.movie_id = m.movie_id WHERE tt.planet_name = planet GROUP BY planetName, movieName; END $$ DELIMITER ; The code for the function declaration is pretty much the same except few modifications. The major change is the SELECT statement: selecting the necessary columns; seems that you have most of the information inside timetable so only join movies to get movie name; use GROUP BY first on planet name and then on movie name so it will count characters on a specific planet by specific movie and that's it ... Hope this was helpful. Let me know if it works for you. Cheers!
Cannot add foreign key constraint
First, here's my main reference table mysql dump -- phpMyAdmin SQL Dump -- version 4.1.14 -- http://www.phpmyadmin.net -- -- Host: 127.0.0.1 -- Generation Time: Nov 17, 2015 at 03:15 AM -- Server version: 5.6.17 -- PHP Version: 5.5.12 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: `j_inventory` -- -- -------------------------------------------------------- -- -- Table structure for table `users` -- CREATE TABLE IF NOT EXISTS `users` ( `user_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `username` varchar(200) COLLATE utf8_unicode_ci NOT NULL, `password` varchar(500) COLLATE utf8_unicode_ci NOT NULL, `real_password` varchar(250) COLLATE utf8_unicode_ci NOT NULL, `role` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `full_name` varchar(250) COLLATE utf8_unicode_ci NOT NULL, `remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', `status` varchar(100) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=44 ; /*!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 the second table where the hopefully the foreign key resides -- phpMyAdmin SQL Dump -- version 4.1.14 -- http://www.phpmyadmin.net -- -- Host: 127.0.0.1 -- Generation Time: Nov 17, 2015 at 03:14 AM -- Server version: 5.6.17 -- PHP Version: 5.5.12 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: `j_inventory` -- -- -------------------------------------------------------- -- -- Table structure for table `user_details` -- CREATE TABLE IF NOT EXISTS `user_details` ( `id` int(11) NOT NULL AUTO_INCREMENT, `phone` varchar(200) NOT NULL, `age` int(100) NOT NULL, `gender` varchar(50) NOT NULL, `address` varchar(250) NOT NULL, `course` varchar(250) NOT NULL, `college` varchar(200) NOT NULL, `year` int(11) NOT NULL, `user_id` int(100) NOT NULL, `updated_at` timestamp NOT NULL, `created_at` timestamp NOT NULL, PRIMARY KEY (`id`), KEY `user_id` (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; /*!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 */; the foreign key (not set up due to mysql error "Cannot add foreign key constraint ") in the users_details is the 'user_id' and the reference table is the 'users' table where the reference key for the foreign key is the 'user_id' on the users table and I tried this ALTER TABLE user_details ADD FOREIGN KEY fk1(user_id) REFERENCES users(user_id) ON DELETE CASCADE ON UPDATE NO ACTION; but sadly and unfortunately it throws me an error Error SQL query: ALTER TABLE user_details ADD CONSTRAINT fk1 FOREIGN KEY (user_id) REFERENCES j_inventory.users(user_id) ON DELETE CASCADE ON UPDATE NO ACTION; MySQL said: Documentation 1215 - Cannot add foreign key constraint Documentation any help, clues, ideas, suggestions, recommendations please?
Your user id is unsigned on the users table. Remove 'unsigned' from this field in your users table create statement and see if it works. I think what you're seeing is an error caused by a mismatch between the user_id field types on your users and user_details tables
MySQL #1100 - Table 'USER' was not locked with LOCK TABLES
I am having a very strange problem. I have developed a database and it works perfectly on my local server however then I tried to put it online on my server I keep getting MySQL error: #1100 - Table 'USER' was not locked with LOCK TABLES (This is then I try to import my .sql file via phpMyAdmin) Once again this same script gives no errors on my local server (WAMP server) I have no Idea why it does not work then I try to put it online. Also it creates the tables and structure the problem is only with the insert statements. SQL script: /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET #OLD_SQL_NOTES=##SQL_NOTES, SQL_NOTES=0 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/`a3823833_MiniPos` /*!40100 DEFAULT CHARACTER SET latin1 */; USE `a3823833_MiniPos`; /*Table structure for table `user` */ DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `userid` int(4) NOT NULL auto_increment, `username` varchar(20) NOT NULL, `password` varchar(20) NOT NULL, `email` varchar(50) NOT NULL, PRIMARY KEY (`userid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*Table structure for table `profile` */ DROP TABLE IF EXISTS `profile`; CREATE TABLE `profile` ( `userid` int(4) NOT NULL, `lastName` varchar(50), `firstName` varchar(50), `dob` varchar(10), PRIMARY KEY (`userid`), CONSTRAINT `profile_fk` FOREIGN KEY (`userid`) REFERENCES `user` (`userid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*Table structure for table 'topic'*/ DROP TABLE IF EXISTS `topic`; CREATE TABLE `topic` ( `topicid` int(4) NOT NULL auto_increment, `userid` int(4) NOT NULL, `text` varchar(2000), `time` datetime, PRIMARY KEY (`topicid`), CONSTRAINT `userid_fk` FOREIGN KEY (`userid`) REFERENCES `user` (`userid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /* Table structure for table 'post'*/ DROP TABLE IF EXISTS `post`; CREATE TABLE `post` ( `postid` int(4) NOT NULL auto_increment, `topicid` int(4), `userid` int(4) NOT NULL, `text` varchar(2000), `time` datetime, PRIMARY KEY (`postid`), CONSTRAINT `post_userid_fk` FOREIGN KEY (`userid`) REFERENCES `user` (`userid`), CONSTRAINT `post_topicid_fk` FOREIGN KEY (`topicid`) REFERENCES `topic` (`topicid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; LOCK TABLES `user` WRITE; INSERT INTO USER VALUES (1,'Martynas','MartynPass','Martyn#email.com'),(2,'Syed','SyedPass','Syed#email.com'),(3,'Stephanie','StephPass','Steph#email.com'); UNLOCK TABLES; LOCK TABLES `topic` WRITE; INSERT INTO topic VALUES (1, 1,'Topic Number 1','2015-06-11 20:15:00'),(2, 2,'Topic Number 2','2015-06-10 19:15:00'),(3, 3,'Topic Number 3','2015-06-09 18:15:00'); UNLOCK TABLES; LOCK TABLES `post` WRITE; INSERT INTO post VALUES (1, 1, 1, 'Very Interesting Topic Number 1','2015-06-11 20:15:00'),(2, 1, 2, 'Agree!','2015-06-11 21:15:00'); UNLOCK TABLES;
Mysql Database not creating table [messy code]
I tried to add this: -- phpMyAdmin SQL Dump -- version 3.4.5 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: Nov 09, 2013 at 04:43 PM -- Server version: 5.5.16 -- PHP Version: 5.3.8 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: `rezzion` -- -- -------------------------------------------------------- -- -- Table structure for table `punishments` -- CREATE TABLE IF NOT EXISTS `punishments` ( `username` varchar(20) NOT NULL AUTO_INCREMENT, `punisher` varchar(20) NOT NULL, `server` varchar(15) NOT NULL, `type` int(255) NOT NULL, `date` varchar(1024) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; /*!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 */; to my database "rezzion" & for some reason i'm getting this error: Anyone know why it's doing this? I have my database set to utf8-bin & it's name is rezzion... I'm confused why the code above isn't working?
Your error starts, at least, here: `username` varchar(20) NOT NULL AUTO_INCREMENT, This is not allowed in MySQL. And, besides, you're declaring primary key with another column: PRIMARY KEY (`id`)
Auto Increment can only be used on a INT change it to a integer and your problem is solved
AUTO_INCREMENT my only be used with number types. You are applying it to a VARCHAR column. Also, you specify an "id" column as your primary key which does not exist. You probably want it to look like this: CREATE TABLE IF NOT EXISTS `punishments` ( `id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `username` varchar(20) NOT NULL, `punisher` varchar(20) NOT NULL, `server` varchar(15) NOT NULL, `type` int(255) NOT NULL, `date` varchar(1024) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;