table creation query showing error - mysql

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.

Related

What is my syntax error in this mySQL create table statement?

I am typing the following:
CREATE TABLE events (
`id` mediumint unsigned not null auto_increment,
`user` varchar(30) not null,
`time` datetime not null,
`duration` decimal(5,2) default 1.0,
`title` tinytext not null,
`location` text default null,
`tag` ENUM(‘red’,’orange’,’yellow’,’green’,’blue’,’violet’,’brown’,’black’) default null,
PRIMARY KEY (`id`),
FOREIGN KEY (`user`) references users (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
and I am getting the following response:
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 '‘red’,’orange’,’yellow’,’green’,’blue’,’violet’,’brown' at line 8
This may be a case of tired eyes, but I am stumped. What is wrong with my enum statement?
Change:
`tag` ENUM(‘red’,’orange’,’yellow’,’green’,’blue’,’violet’,’brown’,’black’)
To:
`tag` ENUM('red', 'orange','yellow','green','blue','violet','brown','black')
Enclose data inside ENUM by Single Quote
CREATE TABLE `events` (
`id` mediumint unsigned not null auto_increment,
`user` varchar(30) not null,
`time` datetime not null,
`duration` decimal(5,2) default 1.0,
`title` tinytext not null,
`location` text default null,
`tag` ENUM('red','orange','yellow','green','blue','violet','brown','black') default null,
PRIMARY KEY (`id`),
FOREIGN KEY (`user`) references users (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Note: event is a MySQL RESERVED KEYWORD. When you use MySQL RESERVED KEYWORDS as identifier enclosing by backtick is recommended.
Problem is with the quotes(‘red’) you used for ENUM values try this ;)
CREATE TABLE events (
`id` mediumint unsigned not null auto_increment,
`user` varchar(30) not null,
`time` datetime not null,
`duration` decimal(5,2) default 1.0,
`title` tinytext not null,
`location` text default null,
`tag` ENUM('red','orange','yellow','green','blue','violet','brown','black') default null,
PRIMARY KEY (`id`),
FOREIGN KEY (`user`) references users (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And before executing this query make sure that users table exists and have column username;
Use single quote in enum instead of backquote. This worked for me:
CREATE TABLE events ( `id` mediumint unsigned not null auto_increment, `user` varchar(30) not null, `time` datetime not null, `duration` decimal(5,2) default 1.0, `title` tinytext not null, `location` text default null, `tag` ENUM('red','orange','yellow','green','blue','violet','brown','black') default null, PRIMARY KEY (`id`), FOREIGN KEY (`user`) references users (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Change
`tag` ENUM(‘red’,’orange’,’yellow’,’green’,’blue’,’violet’,’brown’,’black’)
To
`tag` ENUM('red','orange','yellow','green','blue','violet','brown','black')
Use '' in case of ‘’ .

Adding new column to table as foreign key

I am having difficult time trying to add a column to my sql table that will be a foreign key to another table. I'm getting the following 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 ''description_id' INT,
ADD FOREIGN KEY (description_id)
REFERENCES description(de' at line 2
From my query:
ALTER TABLE images
ADD COLUMN 'description_id' INT,
ADD FOREIGN KEY (description_id)
REFERENCES description(description_id)
Here is my table, where I am trying to add the column to:
CREATE TABLE `images` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`pattern` varchar(225) DEFAULT NULL,
`color` varchar(225) DEFAULT NULL,
`imageUrl` varchar(225) DEFAULT NULL,
`imageSource` varchar(225) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=latin1;
Here is the table that is being connected:
CREATE TABLE `description` (
`description_id` int(11) NOT NULL,
`color` varchar(255) DEFAULT NULL,
`pattern` varchar(255) DEFAULT NULL,
`body` varchar(255) DEFAULT NULL,
PRIMARY KEY (`description_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
You can't ALTER TABLE ADD, ADD. Try:
ALTER TABLE images
ADD COLUMN 'description_id' INT
REFERENCES description(description_id)

Mysql gives me error while running a query

I couldn't find the problem with this query. Would you tell me what is wrong?
CREATE TABLE 'wish' (
'id' int(10) unsigned NOT NULL AUTO_INCREMENT,
'title' varchar(256) NOT NULL,
'issue_number' varchar(10) DEFAULT NULL,
'type_id' int(10) unsigned DEFAULT NULL,
'publication_date' date DEFAULT NULL,
'store_link' varchar(255) DEFAULT NULL,
'notes' text DEFAULT NULL,
'got_it' int(10) unsigned DEFAULT NULL,
PRIMARY KEY ('id'),
KEY 'type_id' ('type_id'),
KEY 'got_it' ('got_it'),
CONSTRAINT 'wish_ibfk_1' FOREIGN KEY ('type_id') REFERENCES 'type' ('id'),
CONSTRAINT 'wish_ibfk_2' FOREIGN KEY ('got_it') REFERENCES 'user' ('id')
) ENGINE=InnoDB;
Mysql gives 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 ''wish' ( 'id' int(10) unsigned NOT NULL AUTO_INCREMENT, 'title' varchar(256)' at line 1
EDIT:
When tried the query replacing back ticks with single quotes, error occurred:
error: #1005 - Can't create table 'comic-booksdb.wish' (errno: 150)
Thank you
Use backticks(`) instead of single quote:
CREATE TABLE `wish` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(256) NOT NULL,
`issue_number` varchar(10) DEFAULT NULL,
`type_id` int(10) unsigned DEFAULT NULL,
`publication_date` date DEFAULT NULL,
`store_link` varchar(255) DEFAULT NULL,
`notes` text DEFAULT NULL,
`got_it` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `type_id` (`type_id`),
KEY `got_it` (`got_it`),
CONSTRAINT `wish_ibfk_1` FOREIGN KEY (`type_id`) REFERENCES `type` (`id`),
CONSTRAINT `wish_ibfk_2` FOREIGN KEY (`got_it`) REFERENCES `user` (`id`)
) ENGINE=InnoDB;
Side Note:
Back ticks are to be used for table and column identifiers, but are only necessary when the identifier is a MySQL reserved keyword, or when the identifier contains whitespace characters or characters beyond a limited set it is often recommended to avoid using reserved keywords as column or table identifiers when possible, avoiding the quoting issue.
Back ticks are necessary for situations like the following:
SELECT id, `my name`, `another field` , `field,with,comma`
EDIT
To avoid 'errno: 150' when dealing with integer data types, verify that the primary and foreign key columns of interest have the same integer types (size and sign, as indicated above). e.g. if primary key is 'unsigned int' and foreign key is simply 'int', then 'errno: 150' is likely.

It it possible to add a foreign key constraint to a composite key in mysql?

So I have 2 tables.
subject_schedule:
CREATE TABLE IF NOT EXISTS `subject_schedule` (
`subject` varchar(10) NOT NULL,
`schedule_id` int(11) NOT NULL,
`id` int(11) NOT NULL,
PRIMARY KEY (`subject`,`schedule_id`),
KEY `schedule_id` (`schedule_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
and appointment:
CREATE TABLE IF NOT EXISTS `appointment` (
`work_plan` varchar(1000) DEFAULT NULL,
`date` date DEFAULT NULL,
`homework_given` varchar(1000) DEFAULT NULL,
`tutor_comments` varchar(1000) DEFAULT NULL,
`admin_comments` varchar(1000) DEFAULT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`schedule_id` int(11) NOT NULL,
`attended` tinyint(1) NOT NULL DEFAULT '1',
`arrival_time` time DEFAULT NULL,
`departure_time` time DEFAULT NULL,
`homework_completed` tinyint(1) NOT NULL DEFAULT '0',
`subject` varchar(10) NOT NULL,
PRIMARY KEY (`id`),
KEY `schedule_id` (`schedule_id`),
KEY `subject` (`subject`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10004 ;
I want to create a foreign key which references the composite key in appointment. I have tried:
ALTER TABLE 'appointment'
ADD CONSTRAINT 'appointment_fk' FOREIGN KEY (`schedule_id`, `subject`)
REFERENCES 'subject_schedule' ('schedule_id', 'subject');
but it returns an error in PhpMyAdmin:
#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 ''appointment' ADD CONSTRAINT 'appointment_fk' FOREIGN KEY
(schedule_id, `su' at line 1
What am I doing wrong?
Is it better to just have an id as a primary key and reference that instead of using a composite key?
the columnNames shouldn't be wrap with single quotes because it will be converted to a string (not a column anymore)
ALTER TABLE appointment
ADD CONSTRAINT appointment_fk FOREIGN KEY (`schedule_id`, `subject`)
REFERENCES subject_schedule (schedule_id, subject);
SQLFiddle Demo

Mysql Create Table error on second CREATE statement [duplicate]

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.