Why won't my query let me assign primary key? - mysql

Just a note, I'm pretty new to SQL.
I'm using MySQL and using the app SequelPro.
I receive this error when I try to make a table:
"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 '(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8' at line 4"
This is the code I'm using to make my table:
CREATE TABLE klout_scores_3 (
id INT(11) NOT NULL AUTO_INCREMENT,
score INT(11)
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

score INT(11)
You want a comma , at end of that line.

Your query is being interpreted like this:
CREATE TABLE klout_scores_3 (
id INT(11) NOT NULL AUTO_INCREMENT,
score INT(11) PRIMARY KEY
(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The (id) part is really confusing to MySQL and it's raising an error right there. Although adding a , after the INT(11) part will fix it, a better solution is to move the declaration:
CREATE TABLE klout_scores_3 (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
score INT(11)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
It's a little concerning that you have a table name like klout_scores_3, as that suggests you have N of these tables. Relational database design and database normalization
rules strongly frowns on this, you should have a singular table with some kind of ..._id column to relate the scores to whatever record that 3 identifies.

Related

Why is the MySQL notation "UNIQUE field (field)" causing an error in MySQL 5.7.21?

I ran into a new problem today, when I was dealing with a mysql query that works on 10.1.19-MariaDB localhost, but not on MySQL 5.7.21-0ubuntu0.16.04.1-log:
CREATE TABLE testing (
pageid INT UNSIGNED NOT NULL AUTO_INCREMENT,
position SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (pageid),
UNIQUE position (position)
) ENGINE=InnoDB CHARSET=utf8
In MySQL 5.6 it works without any hickups (fiddle), however, in MySQL 5.7.21 (fiddle) it throws:
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 'position (position)) ENGINE=InnoDB CHARSET=utf8' at line 1
I figured out a solution by replacing UNIQUE position (position) with UNIQUE (position).
But I am wondering, what the underlying problem is, why it works with the other db system, and I am not sure if my solution is correct.
MySQL doesn't allow you to create an index with the same name of the column
The following code works for MySQL (fiddle)
CREATE TABLE testing (
pageid INT UNSIGNED NOT NULL AUTO_INCREMENT,
position SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (pageid),
UNIQUE idx_position (position)
) ENGINE=InnoDB CHARSET=utf8
That's why it is so important to wrap field and table names in backticks in MySQL (and their derivatives):
Try this (same query and index names, only added backticks):
CREATE TABLE `testing` (
`pageid` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`position` SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (`pageid`),
UNIQUE `position` (`position`)
) ENGINE=InnoDB CHARSET=utf8;

SQL query Failed

please give me recommendation my query does not working
SQL query:
CREATE TABLE `amenities` (
`amenities_id` int(11) NOT NULL auto_increment,
`pic` varchar(100) NOT NULL,
`des` text NOT NULL,
PRIMARY KEY (`amenities_id`)
) TYPE=MariaDB AUTO_INCREMENT=13
MySQL said: Documentation
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'TYPE=MariaDB AUTO_INCREMENT=13' at line 6
There is no type table option, you possibly want to define the table engine
and there is no mariadb engine try
CREATE TABLE amenities ( amenities_id int(11) NOT NULL auto_increment,
pic varchar(100) NOT NULL, des text NOT NULL, PRIMARY KEY (amenities_id) )
AUTO_INCREMENT=13,
engine=innodb
Or leave out the engine option if you want to default the table to the database engine/
Hope this works.
CREATE TABLE amenities (
amenities_id int(11) NOT NULL auto_increment,
pic varchar(100) NOT NULL,
des text NOT NULL,
PRIMARY KEY (amenities_id)
) AUTO_INCREMENT=13
The TYPE keyword was replaced by ENGINE long ago.
The ENGINEs are InnoDB, MyISAM, MEMORY, ARIA and possibly others. Not MySQL, nor MariaDB.
The error message ... near 'TYPE ... points exactly at or after the offending syntax: TYPE in this case. (Not AUTO_INCREMENT, which was later)
AUTO_INCREMENT=13 is produced by SHOW CREATE TABLE for possible reloading. However, it is rarely useful otherwise. It is also mostly harmless.
DROP TABLE IF EXISTS `amenities`;
CREATE TABLE `amenities` (
`amenities_id` int(11) NOT NULL AUTO_INCREMENT,
`pic` varchar(100) NOT NULL,
`des` text,
PRIMARY KEY (`amenities_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Create new Table amenities where amenities_id is a PRIMARY KEY that will be auto increment. another table field pic is a varchar data type and des is a text data type that is used for rich text.

MySQL Query Improvement/Optimisation - Insert Row Without Conflicts

I am having problems with table locking in my little app and want to know how I can improve my query.
Below is the relevant table information.
| assigned_presents | CREATE TABLE `assigned_presents` (
`id` bigint(10) unsigned NOT NULL AUTO_INCREMENT,
`recipient_id` bigint(10) unsigned NOT NULL,
`present_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `recipient_id_index` (`recipient_id`),
KEY `present_id` (`present_id`),
CONSTRAINT `fk_message` FOREIGN KEY (`recipient_id`) REFERENCES `person` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=12914 DEFAULT CHARSET=latin1 |
| present | CREATE TABLE `present` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`description` varchar(100) DEFAULT NULL,
`type_id` int(10) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `present_type_id` (`present`,`type_id`),
KEY `index_type_id` (`type_id`)
) ENGINE=InnoDB AUTO_INCREMENT=83196627 DEFAULT CHARSET=latin1 |
| person | CREATE TABLE `person` (
`id` bigint(10) unsigned NOT NULL AUTO_INCREMENT,
KEY `added_index` (`added`),
) ENGINE=InnoDB AUTO_INCREMENT=249486846 DEFAULT CHARSET=utf8 |
And this is my current query. It does the job but I'm wondering if it could be improved.
INSERT INTO assigned_presents (present_id, recipient_id)
SELECT present.id, ${person_id} FROM present WHERE present.type_id IN (""" + ${present_type_options} + """)
AND NOT EXISTS (SELECT present_id FROM assigned_presents WHERE assigned_present.present_id=present.id)
ORDER BY present.id LIMIT 1
It is basically assigning a present of a particular type (${present_type_options}) to a person (${person_id}) but making sure that present hasn't already been assigned. I hope it makes sense.
I basically want to insert a present_id and person_id into assigned_presents as long as the present_id doesn't already exist in the table.
The query makes sense to me but I am not an experienced MySQL user. I do see that there may be row locking issues when called under heavy load (I haven't figured out how to stress test it) due to the query relying on two select queries before the insert.
The error I get is:
Deadlock found when trying to get lock; try restarting transaction
Also, I can't retry the query. It HAS to work the first time it is called. The reason for this is that this is a first in first served type API where responses are immediate.
Hope someone can help.

MySQL stop inserting when duplicate key condition is met

I have a table constructed by the followinng:
CREATE TABLE IF NOT EXISTS test_table (
ID int(11) NOT NULL AUTO_INCREMENT,
ProfileID int(11) NOT NULL,
ForeignID int(11) NOT NULL,
PRIMARY KEY (ProfileID,ForeignID) )
ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I want to do something a little peculiar though, say there are 4 records in the database:
RecA, RecB, RecC, RecD
I would like to run the following query and have the insert behavior stop when a duplicate key was encountered:
INSERT IGNORE INTO test_table (ProfileID, ForeignID) VALUES(RecE, RecF, RecA, RecB, RecG);
So the query would only insert RecE and RecF, is there a way to do this in MySQL, perhaps using ON DUPLICATE KEY? Ideally the execution would just be terminated once a duplicate has been found, I am not too familiar with SQL syntax though.
Where RecG was explicitly not inserted.

SQL Error - null auto increment

I'm doing a mySQL tutorial to learn how to write sql statements. I keep getting this:
#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 ''add_delete_record' ( 'id' int(11) NOT NULL AUTO_INCREMENT, 'content' text' at line 1
This is the sql I am using:
CREATE TABLE IF NOT EXISTS 'add_delete_record' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'content' text NOT NULL,
PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
This is the exact code the tutorial gave, so I am not sure if the tutorial is just older than my version of mysql(v5.5) or if I have something tiny wrong that I am missing.
You should be using backticks(`) instead of single quotes (').
CREATE TABLE IF NOT EXISTS `add_delete_record` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
sql fiddle
Just remove all single quotes from everywhere the query will run fine.