Create table throws an error - mysql

Hi I am trying to create a table using SQL in Mysql but I keep getting an error.Here is my code:
USE e-commerce
CREATE TABLE `categories` (
`id` SMALLINT NOT NULL AUTO_INCREMENT,
`category` VARCHAR( 30) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `category` (`category`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
When I try to run this in phpmyadmin SQL console I get this error:
#1064 - 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 `categories` ( `id` SMALLINT NOT NULL AUTO_INCREMENT, `category` ' at line 2
What am I doing wrong?

Add semicolon after use
USE e-commerce;
CREATE TABLE `categories` (
`id` SMALLINT NOT NULL AUTO_INCREMENT,
`category` VARCHAR( 30) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `category` (`category`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Missing semi-colon
USE e-commerce;
CREATE TABLE `categories` (
`id` SMALLINT NOT NULL AUTO_INCREMENT,
`category` VARCHAR( 30) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `category` (`category`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

In your code you have 2 queries. You should always end query with a semicolon.
So try the following
USE e-commerce;
CREATE TABLE `categories` (
`id` SMALLINT NOT NULL AUTO_INCREMENT,
`category` VARCHAR( 30) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `category` (`category`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Related

mariaDB foreign key unformed - varchar not a signed/unsigned issue

MAC Sierra 10.12.2
mariaDB Vers 10.1.19
Construct statements are cut/paste from Querious Table Syntax window.
First table:
CREATE TABLE `USAStates` (
`state_id` int(11) unsigned zerofill NOT NULL AUTO_INCREMENT,
`state_abbrev` varchar(6) NOT NULL DEFAULT '',
`electoral_votes` smallint(2) DEFAULT '0',
`fed_reg` smallint(2) DEFAULT '0',
`econ_anal_reg` varchar(128) DEFAULT NULL,
`FRB_district` varchar(128) DEFAULT NULL,
PRIMARY KEY (`state_id`,`state_abbrev`)
) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=utf8;
Second Table:
CREATE TABLE `FRBCommentary` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`state_abbrev` varchar(6) NOT NULL DEFAULT '',
`FRB_district_rem` longtext,
PRIMARY KEY (`id`,`state_abbrev`)
) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=utf8;
Results of ALTER statement from command window; same results using Querious tools:
MariaDB [ESHOP]>
ALTER TABLE FRBCommentary
ADD CONSTRAINT FOREIGN KEY (state_abbrev)
REFERENCES USAStates (state_abbrev);
ERROR 1005 (HY000): Can't create table eshop.#sql-74b_41 (errno:
150 "Foreign key constraint is incorrectly formed") MariaDB [ESHOP]>
Used to be very competent at MS Access...years later trying to learn the real thing.
You need an index on the referenced column.
ALTER TABLE USAStates ADD INDEX (state_abbrev);

MySQL query not working, probably missing a character somewhere?

I probably just missed a character somewhere but I can't seem to figure out where.
CREATE TABLE IF NOT EXISTS `paginas` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`naam` varchar NOT NULL,
`inhoud` varchar,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
The error
1064 - 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 'NOT NULL, inhoud varchar, PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CH' at line 3
You need to specify the length for varchar fields
CREATE TABLE IF NOT EXISTS `paginas` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`naam` varchar(255) NOT NULL,
`inhoud` varchar(255),
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
See fiddle demo

Contraint sql error: #1005 - Can't create table

Error
I have a working site in one server. I decided to move it to another company. I exported the database with phpmyadmin and uploaded in the new server. Everytime I try to import the database I receive this error:
SQL query:
--
-- Constraints for table `seller_cart`
--
ALTER TABLE `seller_cart`
ADD CONSTRAINT `seller_cart_ibfk_1`
FOREIGN KEY ( `subscription` )
REFERENCES `subscription` ( `id` ) ,
ADD CONSTRAINT `seller_cart_ibfk_2`
FOREIGN KEY ( `user` )
REFERENCES `users` ( `user_id` )
ON DELETE CASCADE
ON UPDATE CASCADE ,
ADD CONSTRAINT `seller_cart_ibfk_3`
FOREIGN KEY ( `featured_item` )
REFERENCES `featured_item` ( `item` )
ON DELETE CASCADE
ON UPDATE CASCADE ;
MySQL said: Documentation
#1005 - Can't create table 'project123.#sql-12050_1d' (errno: 150) (Details...)
HERE'S THE STRUCTURE OF seller cart:
--
-- Table structure for table `seller_cart`
--
CREATE TABLE IF NOT EXISTS `seller_cart` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user` bigint(20) NOT NULL,
`subscription` bigint(20) DEFAULT NULL,
`description` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`item_listings` text COLLATE utf8_unicode_ci,
`featured_item` bigint(20) DEFAULT NULL,
`quantity` int(2) NOT NULL,
`price` float NOT NULL,
PRIMARY KEY (`id`),
KEY `user` (`user`),
KEY `subscription` (`subscription`),
KEY `featured_item` (`featured_item`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
What could be the problem? I just exported and imported the samedatabase. Even tried on a localhost and the same problem.
This error is thrown when you are trying to refer a column which is not indexed.
Check if
subscription ( id )
users ( user_id )
featured_item ( item )
have valid indexes defined. If not, define them and run your ALTER ... statement.
Refer To: Rules for Foreign Key Constraints:
InnoDB and FOREIGN KEY Constraints

Update MySQL: Duplicate unique key #1062

I have a table like this:
CREATE TABLE IF NOT EXISTS `activity` (
`ID` int(8) NOT NULL AUTO_INCREMENT,
`action` tinyint(1) NOT NULL,
`userID` int(8) NOT NULL,
`categoryID` int(8) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `UNIQUE` (`userID`,`categoryID`),
KEY `categoryID` (`categoryID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2 ;
With the next information:
INSERT INTO `activity` (`ID`, `action`, `userID`, `categoryID`) VALUES
(1, 2, 11, 312);
I'm trying to execute this query:
UPDATE `activity` SET `action` = '3' WHERE `userID` = '11' AND `categoryID` = '312' ;
And response me with that:
Duplicate entry '11-312' for key 'UNIQUE'
I don't know why. I ain't changing unique keys or inserting another new record. What is the problem?
Thanks.
I don't know why, when I export whole database, instead of show before SQL, show this:
CREATE TABLE IF NOT EXISTS `activity` (
`ID` int(8) NOT NULL AUTO_INCREMENT,
`action` tinyint(1) NOT NULL,
`userID` int(8) NOT NULL,
`categoryID` int(8) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `UNIQUE` (`action`,`userID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2 ;
Now I rewrite setup table sql and it works.

How to optimize this mysql query - explain output included

This is the query (a search query basically, based on tags):-
select
SUM(DISTINCT(ttagrels.id_tag in (2105,2120,2151,2026,2046) )) as key_1_total_matches, td.*, u.*
from Tutors_Tag_Relations AS ttagrels
Join Tutor_Details AS td ON td.id_tutor = ttagrels.id_tutor
JOIN Users as u on u.id_user = td.id_user
where (ttagrels.id_tag in (2105,2120,2151,2026,2046)) group by td.id_tutor HAVING key_1_total_matches = 1
And following is the database dump needed to execute this query:-
CREATE TABLE IF NOT EXISTS `Users` (
`id_user` int(10) unsigned NOT NULL auto_increment,
`id_group` int(11) NOT NULL default '0',
PRIMARY KEY (`id_user`),
KEY `Users_FKIndex1` (`id_group`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=730 ;
INSERT INTO `Users` (`id_user`, `id_group`) VALUES
(303, 1);
CREATE TABLE IF NOT EXISTS `Tutor_Details` (
`id_tutor` int(10) unsigned NOT NULL auto_increment,
`id_user` int(10) NOT NULL default '0',
PRIMARY KEY (`id_tutor`),
KEY `Users_FKIndex1` (`id_user`),
KEY `id_user` (`id_user`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=58 ;
INSERT INTO `Tutor_Details` (`id_tutor`, `id_user`) VALUES
(26, 303);
CREATE TABLE IF NOT EXISTS `Tags` (
`id_tag` int(10) unsigned NOT NULL auto_increment,
`tag` varchar(255) default NULL,
PRIMARY KEY (`id_tag`),
UNIQUE KEY `tag` (`tag`),
KEY `id_tag` (`id_tag`),
KEY `tag_2` (`tag`),
KEY `tag_3` (`tag`),
KEY `tag_4` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2957 ;
INSERT INTO `Tags` (`id_tag`, `tag`) VALUES
(2026, 'Brendan.\nIn'),
(2046, 'Brendan.'),
(2105, 'Brendan'),
(2120, 'Brendan''s'),
(2151, 'Brendan)');
CREATE TABLE IF NOT EXISTS `Tutors_Tag_Relations` (
`id_tag` int(10) unsigned NOT NULL default '0',
`id_tutor` int(10) unsigned default NULL,
`tutor_field` varchar(255) default NULL,
`cdate` timestamp NOT NULL default CURRENT_TIMESTAMP,
`udate` timestamp NULL default NULL,
KEY `Tutors_Tag_Relations` (`id_tag`),
KEY `id_tutor` (`id_tutor`),
KEY `id_tag` (`id_tag`),
KEY `id_tutor_2` (`id_tutor`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `Tutors_Tag_Relations` (`id_tag`, `id_tutor`, `tutor_field`, `cdate`, `udate`) VALUES
(2105, 26, 'firstname', '2010-06-17 17:08:45', NULL);
ALTER TABLE `Tutors_Tag_Relations`
ADD CONSTRAINT `Tutors_Tag_Relations_ibfk_2` FOREIGN KEY (`id_tutor`) REFERENCES `Tutor_Details` (`id_tutor`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `Tutors_Tag_Relations_ibfk_1` FOREIGN KEY (`id_tag`) REFERENCES `Tags` (`id_tag`) ON DELETE NO ACTION ON UPDATE NO ACTION;
What the query does?
This query actually searches tutors which contain "Brendan"(as their name or biography or something). The id_tags 2105,2120,2151,2026,2046 are nothing but the tags which are LIKE "%Brendan%".
My question is :-
1.In the explain of this query, the reference column shows NULL for ttagrels, but there are possible keys (Tutors_Tag_Relations,id_tutor,id_tag,id_tutor_2). So, why is no key being taken. How to make the query take references. Is it possible at all?
2. The other two tables td and u are using references. Any indexing needed in those? I think not.
Check the explain query output here
http://www.test.examvillage.com/explain.png
Don't analyze performance of database with single record in table. Create at least 100 records.