SQL Create Table Error - mysql

Thanks for all the help everyone provides.
I am trying to run my DDL statements and I am getting a weird error. If I copy and paste the ddl it works, but when I try to run it from a *.sql file, it gives an error. Here it is:
mysql> source createtable.sql
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 'CREAT
E TABLE stars
(
id int NOT NULL AUTO_INCREMENT,
first_name varchar(50) NOT ' at line 12
I don't know if this makes a difference or not, but it is the second table def in my ddl script.
Thanks!
EDIT
Here is the starting of the DDL. It gives me an error on the second table.
CREATE TABLE movies
(
id int NOT NULL AUTO_INCREMENT,
title varchar(100) NOT NULL default '',
year int NOT NULL,
director varchar(100) NOT NULL default '',
banner_url varchar(200) default '',
trailer_url varchar(200) default '',
PRIMARY KEY (id)
)
CREATE TABLE stars
(
id int NOT NULL AUTO_INCREMENT,
first_name varchar(50) NOT NULL default '',
last_name varchar(50) NOT NULL default '',
dob date,
photo_url varchar(200) default '',
PRIMARY KEY (id)
)

You'll need to separate your statements with semicolons:
CREATE TABLE movies
(
id int NOT NULL AUTO_INCREMENT,
title varchar(100) NOT NULL default '',
year int NOT NULL,
director varchar(100) NOT NULL default '',
banner_url varchar(200) default '',
trailer_url varchar(200) default '',
PRIMARY KEY (id)
);
CREATE TABLE stars
(
id int NOT NULL AUTO_INCREMENT,
first_name varchar(50) NOT NULL default '',
last_name varchar(50) NOT NULL default '',
dob date,
photo_url varchar(200) default '',
PRIMARY KEY (id)
);

Related

Problems with sql MariaDB (1064)

I can't solve this error showing on my database:
check the manual that corresponds to your MariaDB server version for
the right syntax to use near 'CREATE TABLE tf_links' at line 12
My SQL file code is:
CREATE TABLE tf_cookies (
cid int(10) NOT NULL auto_increment,
uid int(10) NOT NULL default '0',
host VARCHAR(255) default NULL,
data VARCHAR(255) default NULL,
PRIMARY KEY (cid)
) ENGINE=MyISAM;
--
-- tf_links
--
CREATE TABLE tf_links (
lid int(10) NOT NULL auto_increment,
url VARCHAR(255) NOT NULL default '',
sitename VARCHAR(255) NOT NULL default 'Old Link',
sort_order TINYINT(3) UNSIGNED default '0',
PRIMARY KEY (lid)
) ENGINE=MyISAM;

Why always this error occured when I create table in MySQL database?

CREATE TABLE zgd_users_table (
user_id INT NOT NULL AUTO_INCREMENT,
user_name VARCHAR(50) NOT NULL,
user_mobile VARCHAR(20) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(100),
nickname VARCHAR(50) NOT NULL DEFAULT "Default Name",
level TINYINT NOT NULL DEFAULT 3,
locked TINYINT(1) NOT NULL DEFAULT false,
create_time TIMESTAMP NOT NULL,
comment VARCHAR(255),
PRIMARY KEY(user_id);
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 '' at line 13
You forgot to close your command with )! Change your query to the following:
CREATE TABLE zgd_users_table (
user_id INT NOT NULL AUTO_INCREMENT,
user_name VARCHAR(50) NOT NULL,
user_mobile VARCHAR(20) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(100),
nickname VARCHAR(50) NOT NULL DEFAULT 'Default Name',
level TINYINT NOT NULL DEFAULT 3,
locked TINYINT(1) NOT NULL DEFAULT false,
create_time TIMESTAMP NOT NULL,
comment VARCHAR(255),
PRIMARY KEY(user_id)
);
Additional comment about the " on DEFAULT:
Single quotes are used to indicate the beginning and end of a string in SQL. Double quotes generally aren't used in SQL, but that can vary from database to database.
Source (see comments too): https://stackoverflow.com/a/1992331/3840840
You're missing a closing bracket at the end.
Missing ) closing bracket. Working query is:
CREATE TABLE zgd_users_table (
user_id INT NOT NULL AUTO_INCREMENT,
user_name VARCHAR(50) NOT NULL,
user_mobile VARCHAR(20) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(100),
nickname VARCHAR(50) NOT NULL DEFAULT "Default Name",
level TINYINT NOT NULL DEFAULT 3,
locked TINYINT(1) NOT NULL DEFAULT false,
create_time TIMESTAMP NOT NULL,
comment VARCHAR(255),
PRIMARY KEY(user_id));
Try this Query you miss ) in query:
CREATE TABLE zgd_users_table (
user_id INT NOT NULL AUTO_INCREMENT,
user_name VARCHAR(50) NOT NULL,
user_mobile VARCHAR(20) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(100),
nickname VARCHAR(50) NOT NULL DEFAULT "Default Name",
level TINYINT NOT NULL DEFAULT 3,
locked TINYINT(1) NOT NULL DEFAULT false,
create_time TIMESTAMP NOT NULL,
comment VARCHAR(255),
PRIMARY KEY(user_id));
syntax error
you missing the closing bracket at end
do it
CREATE TABLE zgd_users_table (
user_id INT NOT NULL AUTO_INCREMENT,
user_name VARCHAR(50) NOT NULL,
user_mobile VARCHAR(20) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(100),
nickname VARCHAR(50) NOT NULL DEFAULT "Default Name",
level TINYINT NOT NULL DEFAULT 3,
locked TINYINT(1) NOT NULL DEFAULT false,
create_time TIMESTAMP NOT NULL,
comment VARCHAR(255),
PRIMARY KEY(user_id));

#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 ')'

This is the code. However I kept getting 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 ')' at line 7
Weirdly line 7 is the CREATE TABLE academicnews( line. Which does not contain ')' .
CREATE TABLE academicnews(
anewsID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
anewsContent TEXT NOT NULL,
imagePath VARCHAR(200) NOT NULL,
timeNews DATE NOT NULL,
); #Line 7
Get rid of the last comma. It is unnecessary and invalid.
CREATE TABLE academicnews(
anewsID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
anewsContent TEXT NOT NULL,
imagePath VARCHAR(200) NOT NULL,
timeNews DATE NOT NULL, <-- HERE
);
It should be
CREATE TABLE academicnews(
anewsID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
anewsContent TEXT NOT NULL,
imagePath VARCHAR(200) NOT NULL,
timeNews DATE NOT NULL
);
You are getting this error bcoz of an addition comma.
CREATE TABLE academicnews(
anewsID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
anewsContent TEXT NOT NULL,
imagePath VARCHAR(200) NOT NULL,
timeNews DATE NOT NULL, <--- This is the error
);
CREATE TABLE IF NOT EXISTS `testinfo` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`sl_no` int(10) NOT NULL,
`p1` int(3) DEFAULT NULL,
`p2` int(3) DEFAULT NULL,
`p3` int(3)DEFAULT select [p1]+[p2],
`mid` int(8) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `mid` (`mid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

what's wrong with the mysql command?

when i run the following command in phpmyadmin.
CREATE TABLE subscribers (
'subscriber_id' int(11) NOT NULL auto_increment,
'customers_id' int(11) default NULL,
'email_address' varchar(96) NOT NULL default '',
'email_format' varchar(4) NOT NULL default 'TEXT',
PRIMARY KEY ('subscriber_id')
) TYPE=MyISAM;
it shows me #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 ''subscriber_id' int(11) NOT NULL auto_increment, 'customers_id' int(11) defaul' at line 2
INSERT INTO query_builder (query_id, query_category, query_name, query_description, query_string, query_keys_list) VALUES (6, 'email,newsletters', 'All Newsletter Subscribers', 'Returns name and email address of all Customer Account subscribers and all Newsletter-Only subscribers.', 'select c.customers_firstname, c.customers_lastname, s.email_address as customers_email_address from TABLE_SUBSCRIBERS as s left join TABLE_CUSTOMERS as c on c.customers_id = s.customers_id ', '')
it shows :
1062 - Duplicate entry '6' for key 1
i fell the command is right. i don't know how to correct it? thank you.
You don't need the single quote around the column name. Remove that.
This should be
CREATE TABLE subscribers (
subscriber_id int(11) NOT NULL auto_increment,
customers_id int(11) default NULL,
email_address varchar(96) NOT NULL default '',
email_format varchar(4) NOT NULL default 'TEXT',
PRIMARY KEY (subscriber_id)
) TYPE=MyISAM;
Although, You can enclose the column name in backticks if that is conflicting with the mysql reserved words.
You should not use strings as column names. Also it is ENGINE not TYPE.
It should look like:
CREATE TABLE subscribers (
subscriber_id int(11) NOT NULL auto_increment,
customers_id int(11) default NULL,
email_address varchar(96) NOT NULL default '',
email_format varchar(4) NOT NULL default 'TEXT',
PRIMARY KEY (subscriber_id)
) ENGINE=MyISAM;
Just removing the quotes around the column name, and changing the last bit to Engine=MyISAM worked for me:
CREATE TABLE subscribers (
subscriber_id int(11) NOT NULL auto_increment,
customers_id int(11) default NULL,
email_address varchar(96) NOT NULL default '',
email_format varchar(4) NOT NULL default 'TEXT',
PRIMARY KEY (subscriber_id)
) Engine=MyISAM;

mySql errno: 150 Create table statement inside [duplicate]

This question already has answers here:
MySQL: Can't create table (errno: 150)
(35 answers)
Closed 1 year ago.
DROP TABLE IF EXISTS members;
CREATE TABLE members (
Member_ID char(10) NOT NULL default '',
Provider_ID char(10) NOT NULL default '',
First_Name varchar(30) NOT NULL default '',
Middle_Initial char(1) NOT NULL default '',
Last_Name varchar(40) NOT NULL default '',
Address varchar(100) NOT NULL default '',
City varchar(100) NOT NULL default '',
State char(2) NOT NULL default '',
Zip varchar(5) NOT NULL default '',
PRIMARY KEY (Member_ID),
FOREIGN KEY (PROVIDER_ID) REFERENCES PROVIDER(PROVIDER_ID)
) ;
DROP TABLE IF EXISTS provider;
CREATE TABLE provider (
Provider_ID char(10) NOT NULL default '',
Provider_Name varchar(50) NOT NULL default '',
Address varchar(100) NOT NULL default '',
City varchar(100) NOT NULL default '',
State char(2) NOT NULL default '',
Zip char(5) NOT NULL default '',
PRIMARY KEY (Provider_ID)
) ;
DROP TABLE IF EXISTS procedures;
CREATE TABLE procedures (
Procedure_ID char(10) NOT NULL default '',
Doctor_ID char(10) NOT NULL default '',
Member_ID char(10) NOT NULL default '',
Procedure_Type_ID char(10) NOT NULL default '',
Procedure_Name varchar(50) NOT NULL default '',
Cost int(10) NOT NULL default '0',
Date_Executed date NOT NULL default '2000-01-01',
PRIMARY KEY(Procedure_ID),
FOREIGN KEY(DOCTOR_ID) REFERENCES DOCTOR(DOCTOR_ID),
FOREIGN KEY(MEMBER_ID) REFERENCES MEMBER(MEMBER_ID),
FOREIGN KEY(PROCEDURE_TYPE_ID) REFERENCES PROCEDURE_TYPE(PROCEDURE_TYPE_ID)
) ;
DROP TABLE IF EXISTS procedure_type;
CREATE TABLE procedure_type (
Procedure_Type_ID char(10) NOT NULL default '',
Procedre_Type_Name varchar(50) NOT NULL default '',
Procedure_Description varchar(255) NOT NULL default '',
Procedure_ID char(10) NOT NULL default '',
PRIMARY KEY(Procedure_Type_ID),
FOREIGN KEY(PROCEDURE_ID) REFERENCES PROCEDURES(PROCEDURE_ID)
) ;
DROP TABLE IF EXISTS doctor_type;
CREATE TABLE doctor_type (
Type_ID char(10) NOT NULL default '',
Type_Name varchar(50) NOT NULL default '',
Type_Description varchar(255) NOT NULL default '',
PRIMARY KEY(Type_ID)
) ;
DROP TABLE IF EXISTS doctor;
CREATE TABLE doctor (
Doctor_ID char(10) NOT NULL default '',
Type_ID char(10) NOT NULL default '',
Doctor_Name varchar(50) NOT NULL default '',
Address varchar(100) NOT NULL default '',
City varchar(100) NOT NULL default '',
State char(2) NOT NULL default '',
Zip char(5) NOT NULL default '',
PRIMARY KEY(Doctor_ID),
FOREIGN KEY(TYPE_ID) REFERENCES DOCTOR_TYPE(TYPE_ID)
) ;
ERROR 1005 (HY000): Can't create table 'thunderhawk.members' (errno: 150)
Any ideas?
I know it is a foreign key problem. I seem to be following the foreign key syntax...
This question is a dupe of:
MySQL: Can't create table (errno: 150)
The error message tells you exactly what the problem is: table thunderhawk.members cannot be created because of error number (errno) 150, a foreign key constraint would be violated:
FOREIGN KEY (PROVIDER_ID) REFERENCES PROVIDER(PROVIDER_ID)
You cannot create the members table before creating the provider table, because the members table has a foreign key dependency on the provider table.
Usually, If the dbms is not able to create a table that means the following things could have gone wrong:
a) The dbms process doesnt has write permission to write in the data directory, or the disk quota for the folder has exceeded.
b) file system is full and there is no space to write any data to the file.
Check your mysqld.log for more details.
first thing you need to sure that check that both table need to have same storage type like InnoDB.
see here a reference : http://verysimple.com/2006/10/22/mysql-error-number-1005-cant-create-table-mydbsql-328_45frm-errno-150/
Try this code:
DROP TABLE IF EXISTS members;
CREATE TABLE members (
Member_ID char(10) NOT NULL default 0,
Provider_ID char(10) NOT NULL default 0,
First_Name varchar(30) NOT NULL default 0,
Middle_Initial char(1) NOT NULL default 0,
Last_Name varchar(40) NOT NULL default 0,
Address varchar(100) NOT NULL default 0,
City varchar(100) NOT NULL default 0,
State char(2) NOT NULL default 0,
Zip varchar(5) NOT NULL default 0,
PRIMARY KEY (Member_ID),
FOREIGN KEY (PROVIDER_ID) REFERENCES PROVIDER(PROVIDER_ID)
) ;