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
);
Related
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;
Getting this error and not being savvy with creating tables and columns some of the other similar problems and answers haven't helped.
This is the error:
Error
SQL query:
INSERT INTO orders_session_info
(txn_signature , orders_id , payment , payment_title , payment_amount , payment_currency , payment_currency_val , sendto , billto , language , language_id , currency , currency_value , firstname , lastname , content_type , affiliate_id , affiliate_date , affiliate_browser, affiliate_ipaddress , affiliate_clickthroughs_id )
VALUES
(
'd4208b15558a761c34dc7c79b87275b4', '20167', 'paypal', 'PayPal', '273.56', 'USD', '1.00000000', '140', '140', 'english', '1', 'USD', '1.00000000', 'Pauls', 'Dandzbergs', 'physical', '0', '0000-00-00 00:00:00', '', '', '0'
);
MySQL said: Documentation
#1062 - Duplicate entry '0' for key 'PRIMARY'
And the code:
-- Table structure for table `orders_session_info`
--
DROP TABLE IF EXISTS orders_session_info;
CREATE TABLE orders_session_info (
txn_signature int(11) NOT NULL AUTO_INCREMENT,
orders_id int(11) NOT NULL,
payment varchar(32) NOT NULL,
payment_title varchar(32) NOT NULL,
payment_amount varchar(32) NOT NULL,
payment_currency varchar(32) NOT NULL,
payment_currency_val varchar(32) NOT NULL,
sendto varchar(32) NOT NULL,
billto varchar(32) NOT NULL,
language varchar(32) NOT NULL,
language_id varchar(32) NOT NULL,
currency varchar(32) NOT NULL,
currency_value varchar(32) NOT NULL,
firstname varchar(32) NOT NULL,
lastname varchar(128) NOT NULL,
content_type varchar(128) NOT NULL,
affiliate_id varchar(128) NOT NULL,
affiliate_date varchar(128) NOT NULL,
affiliate_browser varchar(128) NOT NULL,
affiliate_ipaddress varchar(128) NOT NULL,
affiliate_clickthroughs_id varchar(128) NOT NULL,
PRIMARY KEY (txn_signature),
KEY txn_signature(orders_id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
any specific help would be much appreciated...
--
-- Dumping data for table orders_session_info
Your primary key field txn_signature is an auto increment field. This means the database will create a unique value for it automatically when you insert a new row.
Therefore you should not include this field in your INSERT statement - the database will take care of it for you. Right now it thinks you're trying to override the automatic functionality and insert a value of "d4208b15558a761c34dc7c79b87275b4" manually. Obviously this would result in duplicate IDs once you have more than one row, so it's not allowed. Also, your field is an int and you're trying to insert a string into it, which will never work either.
MySQL Distrib 5.5.31
I have dropped old DB because of incorrect charset. So I try to create new DB and load correct dump. Also I had an old version mysql before.
While creating a BD and loading a dump I am getting the following errors:
ERROR 1067 (42000) at line 225234: Invalid default value for 'ts_created'
ERROR 1146 (42S02) at line 225243: Table 'ardostore.toc_piwik_site' doesn't exist
ERROR 1067 (42000) at line 225252: Invalid default value for 'date_registered'
ERROR 1146 (42S02) at line 225263: Table 'ardostore.toc_piwik_user' doesn't exist
225234:
create table toc_piwik_site (
idsite int(10) unsigned not null auto_increment,
name varchar(90) not null,
main_url varchar(255) not null,
ts_created timestamp default 'CURRENT_TIMESTAMP' not null,
feedburnerName varchar(100),
PRIMARY KEY (idsite)
);
225243:
insert into toc_piwik_site (idsite, name, main_url, ts_created, feedburnerName) values ('1', 'Store Name', 'http://ardostore.com', '2012-10-18 19:58:49', NULL);
drop table if exists toc_piwik_site_url;
create table toc_piwik_site_url (
idsite int(10) unsigned not null,
url varchar(255) not null,
PRIMARY KEY (idsite, url)
);
225252:
create table toc_piwik_user (
login varchar(100) not null,
password char(32) not null,
alias varchar(45) not null,
email varchar(100) not null,
token_auth char(32) not null,
date_registered timestamp default 'CURRENT_TIMESTAMP' not null,
PRIMARY KEY (login),
UNIQUE uniq_keytoken (token_auth)
);
225263
insert into toc_piwik_user (login, password, alias, email, token_auth, date_registered) values ('toc_piwik_view', '5f4dcc3b5aa765d61d8327deb882cf99', 'toc_piwik_view', 'sonarius#gmail.com', 'a674bf3fe5d9c0651ac32b28fcbe74f8', '2012-10-18 19:58:49');
drop table if exists toc_piwik_user_dashboard;
create table toc_piwik_user_dashboard (
login varchar(100) not null,
iddashboard int(11) not null,
layout text not null,
PRIMARY KEY (login, iddashboard)
);
UPD
ERROR 1170 (42000) at line 225099: BLOB/TEXT column 'query' used in key specification without a ke y length
create table toc_piwik_log_profiling (
query text not null,
count int(10) unsigned,
sum_time_ms float,
UNIQUE query (query)
);
What's causing the issue? And how to fix?
default value 'CURRENT_TIMESTAMP' is a string. remove quotes and it will work:
create table toc_piwik_site (
idsite int(10) unsigned not null auto_increment,
name varchar(90) not null,
main_url varchar(255) not null,
ts_created timestamp default CURRENT_TIMESTAMP not null,
feedburnerName varchar(100),
PRIMARY KEY (idsite)
);
Remove the ' around `CURRENT_TIMESTAMP. Those make it a string.
Write it for example like this:
create table toc_piwik_site (
idsite int(10) unsigned not null auto_increment,
name varchar(90) not null,
main_url varchar(255) not null,
ts_created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
feedburnerName varchar(100),
PRIMARY KEY (idsite)
);
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;
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;