Mysql Syntax Error - sql

Any idea why this is popping up :( ?
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE `clocks` ( `id` int(11) NOT NULL AUTO_INCREMENT, ' at line 6
** Here is the query **
CREATE TABLE `clients` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`clientname` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=127 DEFAULT CHARSET=latin1;
CREATE TABLE `clocks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`projid` int(11) DEFAULT NULL,
`staffid` int(11) DEFAULT NULL,
`clientid` int(11) DEFAULT NULL,
`desc` longtext,
`hours` varchar(255) DEFAULT NULL,
`date` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=26 DEFAULT CHARSET=latin1;
CREATE TABLE `config` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`key` varchar(255) DEFAULT NULL,
`value` longtext,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
CREATE TABLE `projects` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`clientid` int(11) DEFAULT NULL,
`projectname` varchar(255) DEFAULT NULL,
`projectdesc` longtext,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=36 DEFAULT CHARSET=latin1;
CREATE TABLE `staff` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`active` int(11) DEFAULT NULL,
`type` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=latin1;
CREATE TABLE `temp_clocks` (
`id` int(11) DEFAULT NULL,
`projid` int(11) DEFAULT NULL,
`staffid` int(11) DEFAULT NULL,
`clientid` int(11) DEFAULT NULL,
`desc` longtext,
`timestamp` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

You didn't separate multiple queries with a semicolon.
Also, mysql_query doesn't take multiple queries. Use the new mysqli extension and mysqli::multi_query for that.

You've specified a size of 11 for an int type - but this only comes in 1,2,,4 and 8 byte flavours and IIRC the size is implicit in the type (tinyint, smallint, mediumint, int and bigint).
C.

Related

MySQL: All parts of primary key must be NOT NULL; if you need NULL in a key, use UNIQUE instead fivem

can someone fix the whole sql for me because i don't really understand sql
this is for my fivem server
https://www.mediafire.com/file/4hqnihgpxu7gfsd/mdt.sql/file
CREATE TABLE IF NOT EXISTS `mdt_data` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cid` VARCHAR(20) NOT NULL,
`information` MEDIUMTEXT DEFAULT NULL,
`tags` TEXT NOT NULL,
`gallery` TEXT NOT NULL,
`jobtype` VARCHAR(25) DEFAULT 'police',
`pfp` TEXT DEFAULT NULL,
`fingerprint` VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (`cid`),
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_bulletin` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` TEXT NOT NULL,
`desc` TEXT NOT NULL,
`author` varchar(50) NOT NULL,
`time` varchar(20) NOT NULL,
`jobtype` VARCHAR(25) DEFAULT 'police',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_reports` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`author` varchar(50) DEFAULT NULL,
`title` varchar(255) DEFAULT NULL,
`type` varchar(50) DEFAULT NULL,
`details` text DEFAULT NULL,
`tags` text DEFAULT NULL,
`officersinvolved` text DEFAULT NULL,
`civsinvolved` text DEFAULT NULL,
`gallery` text DEFAULT NULL,
`time` varchar(20) DEFAULT NULL,
`jobtype` varchar(25) DEFAULT 'police',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_bolos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`author` varchar(50) DEFAULT NULL,
`title` varchar(50) DEFAULT NULL,
`plate` varchar(50) DEFAULT NULL,
`owner` varchar(50) DEFAULT NULL,
`individual` varchar(50) DEFAULT NULL,
`detail` text DEFAULT NULL,
`tags` text DEFAULT NULL,
`gallery` text DEFAULT NULL,
`officersinvolved` text DEFAULT NULL,
`time` varchar(20) DEFAULT NULL,
`jobtype` varchar(25) NOT NULL DEFAULT 'police',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_convictions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cid` varchar(50) DEFAULT NULL,
`linkedincident` int(11) NOT NULL DEFAULT 0,
`warrant` varchar(50) DEFAULT NULL,
`guilty` varchar(50) DEFAULT NULL,
`processed` varchar(50) DEFAULT NULL,
`associated` varchar(50) DEFAULT '0',
`charges` text DEFAULT NULL,
`fine` int(11) DEFAULT 0,
`sentence` int(11) DEFAULT 0,
`recfine` int(11) DEFAULT 0,
`recsentence` int(11) DEFAULT 0,
`time` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_incidents` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`author` varchar(50) NOT NULL DEFAULT '',
`title` varchar(50) NOT NULL DEFAULT '0',
`details` text NOT NULL,
`tags` text NOT NULL,
`officersinvolved` text NOT NULL,
`civsinvolved` text NOT NULL,
`evidence` text NOT NULL,
`time` varchar(20) DEFAULT NULL,
`jobtype` varchar(25) NOT NULL DEFAULT 'police',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_logs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`text` text NOT NULL,
`time` varchar(20) DEFAULT NULL,
`jobtype` varchar(25) DEFAULT 'police',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_vehicleinfo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`plate` varchar(50) DEFAULT NULL,
`information` text NOT NULL ,
`stolen` tinyint(1) NOT NULL DEFAULT 0,
`code5` tinyint(1) NOT NULL DEFAULT 0,
`image` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_impound` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`vehicleid` int(11) NOT NULL,
`linkedreport` int(11) NOT NULL,
`fee` int(11) DEFAULT NULL,
`time` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
made changes in mdt_data table and mdt_vehicleinfo table

Having trouble adding a foreign key in HeidiSQL (Error 1215)

I've tried just about everything but I'm getting error 1215 when trying to create a foreign key in a child table I have. Here are my tables:
CREATE TABLE `Con` (
`ConID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(250) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`Website` varchar(500) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`FirstYear` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`ConID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `ConEvent` (
`EventID` int(11) NOT NULL AUTO_INCREMENT,
`ConID` int(11) NOT NULL,
`DateStart` date DEFAULT NULL,
`DateEnd` date DEFAULT NULL,
`Year` tinyint(4) DEFAULT NULL,
`Venue` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
`Address` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
`City` tinytext COLLATE utf8_unicode_ci,
`StateProvince` tinytext COLLATE utf8_unicode_ci,
`Country` tinytext COLLATE utf8_unicode_ci,
PRIMARY KEY (`EventID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Here is my syntax:
ALTER TABLE ConEvent
ADD FOREIGN KEY (ConID) REFERENCES Con(ConID);
I can't SHOW ENGINE INNODB STATUS; because I'm not a super user (error 1227). I tried to make myself one but was unable to.

How to resolve 'Foreign key constraint is incorrectly formed' issue

I am trying to create a database table called wp_tokens with a foreign key relationship to another table called wp_users. However, every time I attempt to run the create table SQL, I get the error "Foreign key constraint is incorrectly formed". I have tried multiple re-edits of the same code, but I just cannot figure out what is going on.
This is the code for wp_users
CREATE TABLE `wp_users` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_login` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_pass` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_nicename` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_url` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_activation_key` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_status` int(11) NOT NULL DEFAULT '0',
`display_name` varchar(250) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`ID`),
KEY `user_login_key` (`user_login`),
KEY `user_nicename` (`user_nicename`),
KEY `user_email` (`user_email`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci
This is the SQL for wp_tokens
CREATE TABLE `wp_tokens` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) NOT NULL,
`token` varchar(255) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES wp_users (`ID`)
)
Any help would be greatly appreciated!
The unsigned is important -- the types need to be identical. Try this:
CREATE TABLE `wp_tokens` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) unsigned NOT NULL,
`token` varchar(255) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES wp_users (`ID`)
)
Here is a rextester.

Mapping joined query result into Eloquent model

I have following database schema:
CREATE TABLE `languages` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`code` char(2) COLLATE utf8_unicode_ci NOT NULL,
`latin_name` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`local_name` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `languages_code_unique` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `films` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `films_downloads` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`film_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `films_downloads_film_id_foreign` (`film_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `films_downloads_langs` (
`download_id` int(10) unsigned NOT NULL,
`language_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`download_id`,`language_id`),
KEY `films_downloads_langs_language_id_foreign` (`language_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I would get all Languages by film for films list and use it next like
foreach ($films as $film) $film->languages;
SQL query and following code in Laravel allow me get what I want, but
in flat array.
$q = DB::table('films)
->select('languages.latin_name', 'films.id as film_id')
->join('films_downloads', 'films.id', '=', 'films_downloads.app_id')
->join('films_downloads_langs', 'films_downloads.id', '=', 'films_downloads_langs.download_id')
->join('languages', 'languages.id', '=', 'films_downloads_langs.language_id')
->whereIn('films_downloads.film_id', $films_ids)
->groupBy('languages.id' ,'films_downloads.app_id')
->get();
I would nested arrays / collections / models (prefer), with grouping repeating languages in mysql for multiple films at one query. How I can fill Film models collection by languages collections?

Cannot add or update a child row: a foreign key constraint fails (Clubs and Users)

User Table
CREATE TABLE IF NOT EXISTS `users` (
`userId` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`firstname` varchar(25) NOT NULL,
`lastname` varchar(25) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(32) NOT NULL,
`addressLine1` varchar(80) NOT NULL,
`addressLine2` varchar(80) NOT NULL,
`town` varchar(30) NOT NULL,
`county` varchar(30) NOT NULL,
PRIMARY KEY (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=55 ;
Club Table
CREATE TABLE IF NOT EXISTS `clubs` (
`clubId` int(11) NOT NULL AUTO_INCREMENT,
`clubName` varchar(100) NOT NULL,
`startTime` varchar(5) NOT NULL,
`finishTime` varchar(5) NOT NULL,
`date` date NOT NULL,
PRIMARY KEY (`clubId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
memberid Table
CREATE TABLE IF NOT EXISTS `membersid` (
`memberId` int(11) NOT NULL AUTO_INCREMENT,
`clubId` int(11) NOT NULL,
`userId` int(11) NOT NULL,
PRIMARY KEY (`memberId`,`clubId`,`userId`),
UNIQUE KEY `memberId` (`memberId`),
KEY `clubId` (`clubId`),
KEY `userId` (`userId`),
KEY `clubId_2` (`clubId`),
KEY `clubId_3` (`clubId`),
KEY `userId_2` (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
DAO
String query = "INSERT INTO membersid( userId, clubId ) VALUES ( ?,? )";
Keep getting error Cannot add or update a child row: a foreign key constraint fails
If someone could be that would be great:)
Your keys in the membersid table (please change that name to members) are messed up. Try
CREATE TABLE IF NOT EXISTS members
(
`memberId` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`clubId` int(11) NOT NULL,
`userId` int(11) NOT NULL,
UNIQUE KEY (`clubId`,`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;