Mysql Create Table error on second CREATE statement [duplicate] - mysql

This question already has answers here:
node-mysql multiple statements in one query
(3 answers)
Closed 5 years ago.
I'm trying to write a script to import a Mysql DB structure. I've exported the database SQL via PhpMyAdmin, and node npm model sqldump. Both produce the same error when trying to create the second table. This doesn't seem to be table-specific - I can mix the tables around (I have 20 tables in this DB) and always hit the same error on the second CREATE TABLE statement.
Can anyone point me towards something stupid I'm missing, please?
The error is:
{ Error: 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 'CREATE TABLE IF NOT EXISTS `autoresponses` (
`id` bigint(20) NOT NULL AUTO_INC' at line 14
at PromiseConnection.query (D:\dev-mysql-update\node_modules\mysql2\promise.js:75:20)
at D:\dev-mysql-update\index.js:157:7
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
message: '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 \'CREATE TABLE IF NOT EXISTS `autoresponses` (\n `id` bigint(20) NOT NULL AUTO_INC\' at line 14',
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '#42000' }
The first two tables are:
CREATE TABLE IF NOT EXISTS `autoresponders` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`uuid` char(36) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
`archived` tinyint(1) NOT NULL DEFAULT '0',
`title` varchar(255) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL,
`client_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `client_id` (`client_id`),
CONSTRAINT `autoresponders_ibfk_1` FOREIGN KEY (`client_id`) REFERENCES `clients` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `autoresponses` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`uuid` char(36) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
`archived` tinyint(1) NOT NULL DEFAULT '0',
`hours_delay` int(11) DEFAULT NULL,
`status` varchar(255) DEFAULT 'active',
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL,
`autoresponder_id` bigint(20) DEFAULT NULL,
`notification_id` bigint(20) DEFAULT NULL,
`client_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `autoresponses_autoresponder_id_notification_id_unique` (`autoresponder_id`,`notification_id`),
KEY `notification_id` (`notification_id`),
CONSTRAINT `autoresponses_ibfk_1` FOREIGN KEY (`autoresponder_id`) REFERENCES `autoresponders` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `autoresponses_ibfk_2` FOREIGN KEY (`notification_id`) REFERENCES `notifications` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Thanks in advance,
Andy

Your table name should be inside your parenthesis.

Related

ALTER TABLE table_name AUTO_INCREMENT = 1000; gives me syntax error

this is my table export:
CREATE TABLE IF NOT EXISTS `order` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`comment` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`shipping_cost` double DEFAULT NULL,
`customer_id` int(11) NOT NULL,
`delivery_type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`invoice_nr` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `order_customer_id_index` (`customer_id`),
KEY `order_invoice_nr_index` (`invoice_nr`),
KEY `order_created_at_index` (`created_at`),
KEY `order_updated_at_index` (`updated_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
when I run now:
ALTER TABLE order AUTO_INCREMENT=1000;
I get:
#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 'order AUTO_INCREMENT=1000' at line 1
the table is empty!
mysql version: 5.5.44-0ubuntu0.14.04.1
someone has an idea what could cause me this problem?
if I put for example this:
ALTER TABLE asdasdfasdfasdf AUTO_INCREMENT=1000;
I get
1146 - Table 'mydb.asdasdfasdfasdf' doesn't exist
Try:
ALTER TABLE `order` AUTO_INCREMENT=1000;
Order is a reserved word, it's trying to order...

Using MYSQL Triggers and Foreign Keys

I do not know how to handle my issue here and I am looking for the most practical solution. I have to track a user-id across multiple tables and I am looking at triggers and Foreign Keys to solve the initial inserts into the databases for each user. On registration, I would like to create a row in each database with the user id. That way its always an update statement when I reference it in the code.
Here is my Table Structure
CREATE TABLE `authentication` (
`userid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`fname` varchar(11) COLLATE utf8_unicode_ci DEFAULT NULL,
`lname` varchar(11) COLLATE utf8_unicode_ci DEFAULT NULL,
`email` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`password` varchar(11) COLLATE utf8_unicode_ci DEFAULT NULL,
`online` int(11) DEFAULT NULL,
`created_at` varchar(11) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`email`),
KEY `userid` (`userid`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `user_progress` (
`userid` int(10) NOT NULL,
`progress_event` varchar(25) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Here is an example trigger I tried to make
CREATE TRIGGER user_progress_add
AFTER INSERT ON authentication
FOR EACH ROW
BEGIN
INSERT INTO user_progress (userid, progress_status)
VALUES (NEW.userid, 'signup');
END
This trigger did not work. It gives 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 'END' at line 1".
I guess my question is, whats wrong with my trigger? is a trigger a better idea in this case than a foreign key?

table creation query showing error

I am trying to create a table it is showing following 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 ''duration' bigint (10) DEFAULT NULL,
'small_img' varchar(100) DEFAULT NULL' at line 6
Query is
CREATE TABLE videos_description(
`video_id` bigint(200) NOT NULL AUTO_INCREMENT,
`video_path` varchar(200) NOT NULL ,
`video_title` varchar(200) NOT NULL ,
`description` text DEFAULT NULL,
'duration' bigint (10) DEFAULT NULL,
'small_img' varchar(100) DEFAULT NULL,
'large_img' varchar(100) DEFAULT NULL,
`source_site` varchar(100) DEFAULT NULL,
`sent_by` int(20) DEFAULT NULL,
`received_by` int(20) DEFAULT NULL,
`message_id` bigint(200) DEFAULT NULL,
`visibility` varchar(30) DEFAULT NULL,
`adddate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (video_id),
FOREIGN KEY (`received_by`) REFERENCES `users` (`userid`) ON UPDATE CASCADE,
FOREIGN KEY (`sent_by`) REFERENCES `users` (`userid`) ON UPDATE CASCADE,
FOREIGN KEY (`message_id`) REFERENCES `messages` (`messageid`) ON DELETE CASCADE ON UPDATE CASCADE
)
My questions are
1) resolution of above mentioned error
2)What more fields should I include or remove for storing video details
You're using the wrong quote character on some of your column names. You should be using the backtick (`) everywhere.

incomprehensible error on this simple create table query

I've just exported the CREATE sql of a singular table using phpmyadmin. this is the result:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`register_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`account_type` int(11) NOT NULL,
`active` int(11) NOT NULL,
`email` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`login` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(200) COLLATE utf8_unicode_ci NOT NULL
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
But when i run this code on the very phpmyadmin i receive 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 '(`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREME' at line 14
I have tried many ways, but my skills aren't enough.
You forgot to add comma before PRIMARY KEY (id)
`name` varchar(200) COLLATE utf8_unicode_ci NOT NULL, -- <== this
PRIMARY KEY (`id`)
add comma and it will work, see this fiddle (click link)

Syntax error when running a MySQL CREATE TABLE statement [duplicate]

This question already has answers here:
1064 error in CREATE TABLE ... TYPE=MYISAM
(5 answers)
Closed 9 years ago.
CREATE TABLE users (
user_id INT(8) NOT NULL AUTO_INCREMENT,
user_name VARCHAR(30) NOT NULL,
user_pass VARCHAR(255) NOT NULL,
user_email VARCHAR(255) NOT NULL,
user_date DATETIME NOT NULL,
user_level INT(8) NOT NULL,
UNIQUE INDEX user_name_unique (user_name),
PRIMARY KEY (user_id)
) TYPE=INNODB;
When running this query on the SQL server, I am getting the following 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 'TYPE=INNODB' at line 10
Any help on why this is coming up?
Instead of
TYPE=INNODB
set
Engine=InnoDB
Use ENGINE=InnoDB;
http://dev.mysql.com/doc/refman/5.0/en/using-innodb-tables.html
The manual for CREATE TABLE doesn't include TYPE; it seems to use:
ENGINE = INNODB;
And that is the default engine, so you don't really need to specify it.
Try the following query:
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(8) NOT NULL AUTO_INCREMENT,
`user_name` varchar(30) NOT NULL,
`user_pass` varchar(255) NOT NULL,
`user_email` varchar(255) NOT NULL,
`user_date` datetime NOT NULL,
`user_level` int(8) NOT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_name_unique` (`user_name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;