Creating a database for a class in PHP and MYSQL. Running into an error when creating the database, phpMyAdmin gives me a useless error message. (Software that gives a better error message would be awesome BTW)
phpMyAdmin 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 customers ( customer_id INT UNSIGNED NOT NULL auto increment, fir' at line 1
USE isys288_gottfrk
CREATE TABLE customers ( customer_id INT UNSIGNED NOT NULL
auto increment, first_name carchar(20) NOT NULL, last_name VARCHAR(40) NOT NULL,
PRIMARY KEY (customer_id), INDEX full_name (last_name, first_name) ) engine =
innodb;
There were several errors in your code.
USE isys288_gottfrk;
CREATE TABLE customers
(
customer_id INT UNSIGNED NOT NULL auto_increment,
first_name varchar(20) NOT NULL,
last_name VARCHAR(40) NOT NULL,
PRIMARY KEY (customer_id),
INDEX full_name (last_name, first_name)
) engine = innodb;
I suggest you use a DB tool like MySQL Workbench to generate your tables. Then you can see the problems right away.
Juergen d edited your question for formatting, this might have helped you as well: Your initial statement was all on one line, so the error was flagged in that one line. If you edited it on multi lines (like in your question, or better in his answer) you might have gotten a better message, e.g. one that flags that the line containing carchar(20) contains a problem (it's even better the way he has it in his answer, but it correctly uses varchar(20), not flagging a problem)
Yes, it would be nice if error messages would immediately flag the problem, but sometimes it's already better when the input is formatted in a more human-readable way. I've tested the same with a multiline statement, mysql stated
ERROR 1064 (42000): 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 'carchar(20)
Try this:
CREATE TABLE customers (
customer_id INT UNSIGNED NOT NULL AUTO_INCREMENT
, first_name VARCHAR (20) NOT NULL
, last_name VARCHAR (40) NOT NULL
, PRIMARY KEY (customer_id)
, INDEX full_name (last_name, first_name)
) ENGINE = INNODB ;
You can use some GUI clients like SQLyog for this.
Related
I am trying to create a table, but it gives me this error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax touse near ' INT NOT NULL AUTO_INCREMENT
Here is my code:
CREATE TABLE bowling_scores (
PlayerID, INT NOT NULL AUTO_INCREMENT
, LName VARCHAR(100) NOT NULL
, FName VARCHAR(100) NOT NULL
, Game1 INT NULL
, Game2 INT NULL
, Game3 INT NULL
, Game4 INT NULL
, PRIMARY KEY (PlayerID)
);
Here's a tip about syntax error messages: the message tells you where the error is.
check the manual that corresponds to your MySQL server version for the right syntax to use near ' INT NOT NULL AUTO_INCREMENT
This is telling you that MySQL became confused because it wasn't expecting to see that text. This usually means you made a mistake at that point, or immediately before it.
This can help you narrow down your search in the SQL statement to the point where it went wrong.
The error message also tells you what to do: check the manual. You should help yourself become better at SQL by reading documentation and examples.
I've been using MySQL for nearly 20 years, but I still refer to the reference docs every day. In fact, it's because I am experienced with MySQL that I know this is the right thing to do.
All SQL statements have a reference page in the manual. Here's the page for CREATE TABLE: https://dev.mysql.com/doc/refman/5.7/en/create-table.html
The MySQL manual is large, so it's easy to think "I don't know where the right page is." Gradually you can learn how the manual is organized. Pay attention to the hierarchy of links on the left column. When in doubt, just use Google for phrases like "mysql create table syntax".
You should be able to answer simple syntax errors for yourself instead of posting to Stack Overflow. You'll get your answer more quickly!
This is an obvious error:
PlayerID, INT NOT NULL AUTO_INCREMENT
You have an extraneous comma. It should be:
PlayerID INT NOT NULL AUTO_INCREMENT
Stop putting commas on the next line of your SQL statements as well!
This should work. You had a comma after PlayerID
CREATE TABLE bowling_scores (
PlayerID INT NOT NULL AUTO_INCREMENT,
LName VARCHAR(100) NOT NULL,
FName VARCHAR(100) NOT NULL,
Game1 INT NULL,
Game2 INT NULL,
Game3 INT NULL,
Game4 INT NULL,
PRIMARY KEY (PlayerID)
);
You should be writing something like:
creat table Bowling_score(Playerid int, Firstname varchar(100), Lastname varchar(100), Game1 int, Game2 int, Game3 int);
I can't tell what is wrong with this query.
create table roles
(
id int unsigned not null auto_increment
,name varchar(32) not null
,phone varchar(256) not null
,primary key (id)
)
engine=innodb
default charset=utf8;
When I try to run it, I get:
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 'unsigned not null auto_increment ,role_id int unsigned ' at
line 3
Using MySQL 5.7
Thanks for the advice, tadman
Turns out I had a tab between "int unsigned" instead of a space. Replaced the tab with a space and it worked just fine.
That was driving me crazy for almost 2 hours.
I am getting a SQL query syntax error while creating a table. Here is the SQL query:
CREATE TABLE ACCOUNT (
ACCNO NUMBER(5) NOT NULL,
NAME VARCHAR(20) NOT NULL,
BA L NUMBER(8,2) NOT NULL,
CREATION-DT DATE NOT NULL,
PRIMARY KEY ( ACCNO )
);
Here is the error:
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 'NUMBER(5) NOT NULL.
What is wrong with my syntax?
Using SQLfiddle you can see this is not valid.
You have multiple problems with your syntax; invalid data types, invalid column names, etc. You can encapsulate the invalid names in backticks but then you will have to remember to encapsulate them later.
Instead of Number you probably meant numeric but i would suggest trying Integer instead.
Or just try this:
CREATE TABLE ACCOUNT (
ACCNO INTEGER(5) NOT NULL,
NAME VARCHAR(20) NOT NULL,
BAL INTEGER(8) NOT NULL,
CREATIONDT DATE NOT NULL,
PRIMARY KEY ( ACCNO )
);
NUMBER is not a valid datatype in MySQL, you should choose the one from the list (maybe you've meant NUMERIC).
You should also escape some identifiers (in your case, column names BA L, which includes a space within it, and CREATION-DT, which includes a dash) with backticks ` in MySQL:
CREATE TABLE `ACCOUNT` (
`ACCNO` NUMERIC(5) NOT NULL,
`NAME` VARCHAR(20) NOT NULL,
`BA L` NUMERIC(8,2) NOT NULL,
`CREATION-DT` DATE NOT NULL,
PRIMARY KEY ( `ACCNO` )
);
SQLFiddle
I try to create a table but I keep getting an SQL Error but I can't figure out why.
This is the error:
check the manual that corresponds to your MySQL server version for the
right syntax to use near '+491634170770 (Id INT NOT NULL PRIMARY KEY
AUTO_INCREMENT,Type VARCHAR(20),Conte' at line 1 ERROR 1064 (42000):
You have an error in your SQL syntax;
This is my statement:
CREATE TABLE +491234175789 (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);
I already tried to find a solution here, but my syntax seems to be correctly. I think its because of the name of the table. But using backticks like this ´+491234175789´ didn't work.
This is the backtick `:
CREATE TABLE `+491234175789` (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);
However, don't create a table name that requires backticks. It is just bad form and makes queries harder to read and write -- you are creating problems for the future. Call it something like:
CREATE TABLE t_491234175789 (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);
I have a 2-table schema which can be specified as follows:
CREATE TABLE MEMBERS
( membershipnumber int NOT NULL PRIMARY KEY,
firstname varchar(20) NOT NULL,
lastname varchar(20) NOT NULL,
email varchar(30) NOT NULL,
status varchar(15) NOT NULL DEFAULT 'unapproved',
);
CREATE TABLE TELEPHONENUMBERS
( telephone varchar(15) NOT NULL PRIMARY KEY,
membershipnumber INT NOT NULL REFERENCES MEMBERS(membershipnumber),
isprimary enum('0','1') NOT NULL DEFAULT '1'
);
I am trying to specify an integrity constraint to indicate that any individual member may have only one primary telephone number (i.e. telephonenumbers.isprimary='1') and any number of secondary telephone numbers.
Here is my current attempt:
ALTER TABLE MEMBERS
ADD CONSTRAINT oneprimary_ck CHECK (SELECT COUNT(isprimary)=1 FROM TELEPHONENUMBERS WHERE TELEPHONENUMBERS.membershipnumber = membershipnumber) IN '1';
However this yields the following syntax error:
ERROR 1064 (42000): 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 'SELECT COUNT(isprimary)=0 FROM TELEPHONENUMBERS WHERE TELEPHONENUMBERS.membershipnumber=1 IN '1') IN ''
I'm assuming my approach is totally wrong, but am unsure how to proceed. Would it be better to do this using a trigger or is there a small alteration ot my approach that would work?
I am using server version: 5.5.30.
With many thanks,
Froskoy.
Does it work any better if you swap ...
ALTER TABLE MEMBERS ADD CONSTRAINT oneprimary_ck CHECK
(SELECT COUNT(isprimary)=1 FROM TELEPHONENUMBERS
WHERE TELEPHONENUMBERS.membershipnumber = membershipnumber) IN '1';
... with ...
ALTER TABLE MEMBERS ADD CONSTRAINT oneprimary_ck CHECK
(SELECT COUNT(isprimary) FROM TELEPHONENUMBERS
WHERE isprimary=1 AND TELEPHONENUMBERS.membershipnumber = membershipnumber) > 1;
... ?
Two things are wrong in your approach:
A check constraint cannot contain a subquery
Check constraints are not enforced by MySQL (not in 5.5 anyway)
What you can do, is create a trigger that raises an error. Here's an example:
https://stackoverflow.com/a/7189396/417194