what's wrong with the mysql command? - mysql

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;

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;

error in mysql command

I'm following a book to learn cakephp. I have this table to create in mysql but I keep having an error
CREATE TABLE IF NOT EXISTS 'categories'(
'id' int(10) NOT NULL AUTO_INCREMENT,
'parent_id' int(11) NOT NULL DEFAULT '0',
'name' varchar(50) character NOT NULL,
'description' varchar(200) character NOT NULL,
'image' varchar(255) character NOT NULL,
PRIMARY KEY ('id'),
KEY 'cat_parent_id' ('parent_id'),
KEY 'cat_name' ('name')
);
INSERT INTO 'categories' ('id', 'parent_id', 'name', 'description', 'image') VALUES
(17, 0, 'Jazz', 'Everything from 1890s', ''),
(12, 0, 'Classical', 'From Medieval to Contemporary', ''),
(13, 17, 'Dizzy Gillepsie', 'The Trumpeter Master', ''),
(14, 12, 'Mozart', 'The Old Favourite', '');
The error at line 1 ???:
#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 ''categories'(
'id' int(10) NOT NULL AUTO_INCREMENT,
'parent_id' int(11) NOT NU' at line 1
Single-quotes are for string literals, not identifiers (column or table names). Remove them. Also, I am not sure what character is supposed to do, unless you were intending to specify a character set (CHARACTER SET <charset_name>):
CREATE TABLE IF NOT EXISTS categories(
id int(10) NOT NULL AUTO_INCREMENT,
parent_id int(11) NOT NULL DEFAULT '0',
name varchar(50) NOT NULL,
description varchar(200) NOT NULL,
image varchar(255) NOT NULL,
PRIMARY KEY (id),
KEY cat_parent_id (parent_id),
KEY cat_name (name)
);
If you need to quote an identifier in MySQL, use the backtick `. This can be helpful in easily spotting which are table/column names, and which are keywords. Some developers also insist on using them everywhere as a style rule:
CREATE TABLE IF NOT EXISTS `categories`(
`id` int(10) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) NOT NULL DEFAULT '0',
`name` varchar(50) NOT NULL,
`description` varchar(200) NOT NULL,
`image` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `cat_parent_id` (`parent_id`),
KEY `cat_name` (`name`)
);
Quoting is also a must when using a reserved word as a column name (NOT RECOMMENDED):
CREATE TABLE IF NOT EXISTS foobar(
id int(10) NOT NULL AUTO_INCREMENT,
`table` int(11) NOT NULL,
`select` int(11) NOT NULL
);

SQL Create Table Error

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)
);

MySQL Results pivot table - of a sort

I have a table as defined below:
CREATE TABLE `z_data` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`subscriberid` INT(11) NOT NULL DEFAULT '0',
`Email Address` VARCHAR(255) NOT NULL DEFAULT '',
`Title` VARCHAR(255) NOT NULL DEFAULT '',
`First Name` VARCHAR(255) NOT NULL DEFAULT '',
`Last Name` VARCHAR(255) NOT NULL DEFAULT '',
`Postal Code` VARCHAR(255) NOT NULL DEFAULT '',
`banned` INT(11) NOT NULL DEFAULT '0',
`bounced` INT(1) NOT NULL DEFAULT '0',
`unsub` INT(1) NOT NULL DEFAULT '0',
`duplicate` INT(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `idx_subscriberid` (`subscriberid`),
KEY `idx_banned` (`banned`),
KEY `idx_bounced` (`bounced`),
KEY `idx_unsub` (`unsub`),
KEY `idx_duplicate` (`duplicate`),
KEY `idx_email` (`Email Address`),
FULLTEXT KEY `idx_emailaddress` (`Email Address`)
) ENGINE=MYISAM AUTO_INCREMENT=20 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC
This table is populated from a CSV file and a query updates the subscriberid column with a number if the emailaddress is allready present in the main database with a query like:
UPDATE `z_data` AS z
LEFT JOIN main_subscribers AS b ON (z.`Email Address`=b.emailaddress && b.listid=1 && b.unsubscribed!=0)
SET z.subscriberid = b.subscriberid
WHERE b.subscriberid IS NOT NULL;";
Now, my question. I need to select the subscriberids of all of the records in z_data in rows of ten fields, this can be comma seperated lists.
my result could be similar to:
1293572,1293573,1293574,1293575,1099590,1174275,1293576,1293577,1293578,1293579,
673070,813617,1293580,1293581,1293582,1131221,1293583,1182045,419085,1293584,
1050278,1293585,1064945,638483,737691,1293586,1293587,799800,1110596,1293588,
1293589,1293590,1293591,421394,1293592,1293593,1293594,1293595,1293596,851491,
1293597,1293598,1293599,628250,1293600,1293601,1293602,535366,1293603,256590,
1293604,1293605,736956,1293606,1209511,673075,1293607,1293608,1293609,754357,
The reason for this is so that I can store these values in a TEXT field for later use in a IN clause and yet have a reasonably human readable script.
I have managed to get the first row thus:
SELECT GROUP_CONCAT(a.subscriberid) AS 'IDs' FROM (
SELECT subscriberid FROM
`z_data`
WHERE subscriberid!=0
LIMIT 1,10
) AS a
but do not know how to 'walk' though the rest of the table when the number of rows with subscriberids is unknown.
Any suggestions would be gratefully received.

phpMyadmin - Error #1064

When trying to create this table in my db, I get an 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 'unsigned NOT NULL default '0',
PRIMARY KEY (reminder_id), KEY
reminder_id (rem' at line 5
CREATE TABLE reminder_events (
reminder_id bigint(20) unsigned NOT NULL auto_increment,
reminder_name varchar(255) NOT NULL default '',
reminder_desc text,
reminder_date varchar(8) unsigned NOT NULL default '0',
PRIMARY KEY (reminder_id),
KEY reminder_id (reminder_id)
) TYPE=MyISAM;
Can anyone see what I'm doing wrong?
The problem is that you're trying to define a text field (varchar) as being unsigned, which is something that can only apply to a numerical field.
i.e.: "reminder_date varchar(8) unsigned NOT NULL default '0'," makes no sense.
However, as the field is called "reminder_date", I'm guessing you're attempting to store a date, in which case you really want to use MySQLs DATE field type.
e.g.: "reminder_date DATE NOT NULL,"
Additionally, if you're going to want to search on any of these fields, you should also add some indexes to speed up the searches. So, if you wanted to be able to search on the name and date, you could use:
CREATE TABLE reminder_events (
reminder_id bigint(20) unsigned NOT NULL auto_increment,
reminder_name varchar(255) NOT NULL DEFAULT '',
reminder_desc text,
reminder_date DATE NOT NULL,
PRIMARY KEY (reminder_id),
KEY reminder_id (reminder_id),
KEY reminder_name (reminder_name),
KEY reminder_date (reminder_date)
) TYPE=MyISAM;
Just remove unsigned.
CREATE TABLE reminder_events (
reminder_id bigint(20) unsigned NOT NULL auto_increment,
reminder_name varchar(255) NOT NULL default '',
reminder_desc text,
reminder_date varchar(8) NOT NULL default '0',
PRIMARY KEY (reminder_id),
KEY reminder_id (reminder_id)
) TYPE=MyISAM;
Write ENGINE instead of TYPE
CREATE TABLE reminder_events (
reminder_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
reminder_name VARCHAR(255) NOT NULL DEFAULT '',
reminder_desc TEXT,
reminder_date VARCHAR(8) NOT NULL DEFAULT '0',
PRIMARY KEY (reminder_id),
KEY reminder_id (reminder_id)
) ENGINE=MYISAM;