Mysql gives me error while running a query - mysql

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.

Related

cant create a MySql table

I have created this table
CREATE TABLE `Overview` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ops` varchar(11) NOT NULL,
`date` date NOT NULL,
`title` text,
`beneficiary_Institution` varchar(100) DEFAULT NULL,
`description` longtext,
`public_Expenditure_Budget` decimal(14,2) DEFAULT NULL,
`payments` decimal(14,2) DEFAULT NULL,
`completition_percent` int(11) DEFAULT NULL,
`start` varchar(11) DEFAULT NULL,
`finish` varchar(11) DEFAULT NULL,
`sub_projects` int(11) DEFAULT NULL,
`public_aid` decimal(14,2) DEFAULT NULL,
`operational_programme_number` int(11) DEFAULT NULL,
`operational_programme` varchar(100) DEFAULT NULL,
`title_ops` text,
`map_coordinates` varchar(15000) DEFAULT NULL,
PRIMARY KEY (`id`,`ops`,`date`)
)ENGINE=InnoDB;
and i try to create a table that has a foreign key with that
CREATE TABLE `Proposal_Body` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`ops` VARCHAR(11) NULL,
`title` VARCHAR(100) NULL,
`representative` VARCHAR(45) NULL,
`address` VARCHAR(45) NULL,
`email` VARCHAR(45) NULL,
PRIMARY KEY (`id`),
INDEX `fk_Proposal_Body_1_idx` (`ops` ASC),
CONSTRAINT `fk_Proposal_Body_1`
FOREIGN KEY (`ops`)
REFERENCES `espanew`.`Overview` (`ops`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)ENGINE=InnoDB;
This and any other attempt i tried gives me this error
ERROR 1005 (HY000): Can't create table
'Espa_Projects_Eng.Proposal_Body' (errno: 150)
I know that this is a foreign key problem, but i cant see the reason. This code is a comming from the mysql client, where i did the creation using the ui, didnt type the commands by hand, so it should work.
Thanks a lot
Reading from the documentation:
If you re-create a table that was dropped, it must have a definition
that conforms to the foreign key constraints referencing it. It must
have the correct column names and types, and it must have indexes on
the referenced keys, as stated earlier. If these are not satisfied,
MySQL returns Error 1005 and refers to Error 150 in the error message,
which means that a foreign key constraint was not correctly formed.
Similarly, if an ALTER TABLE fails due to Error 150, this means that a
foreign key definition would be incorrectly formed for the altered
table.
As you can see your first table has this primary key:
PRIMARY KEY (`id`,`ops`,`date`)
but in the second table the foreign key is:
FOREIGN KEY (`ops`)
You have to correct this to make it work.
Your Primary key is a composite key
PRIMARY KEY (id,ops,date)
And your foreign key is consist of only single column i.e.
FOREIGN KEY (ops)
Foreign keys have to match the primary/unique key they reference column for column. To remove this error either you need to add id and date to the Proposal_Body table or your first table Overview primary key is wrong and should just be (id).

How do I solve the MySQL error where I Cannot add foreign key constraint because of ERROR 1215?

This is my first table.
CREATE TABLE `raw_orders` (
`row_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`order_id` VARCHAR(45) COLLATE utf8mb4_unicode_ci NOT NULL,
`order_revenue` FLOAT NOT NULL,
PRIMARY KEY(`row_id`),
KEY(`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ciÍž
This is my second table
CREATE TABLE `formatted_orders` (
`order_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`client_order_id` VARCHAR(50) COLLATE utf8mb4_general_ci NOT NULL,
`order_revenue` FLOAT NOT NULL,
PRIMARY KEY(`order_id`),
KEY(`client_order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ciÍž
I am trying to add foreign key in formatted_orders linking it to raw_orders by using this
ALTER TABLE formatted_orders
ADD FOREIGN KEY (client_order_id) REFERENCES raw_orders(order_id);
But I get this error
ERROR (HY000): Cannot add foreign key constraint
You can simply add a foreign key in the table formatted_orders like this:
CREATE TABLE `formatted_orders` (
`order_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`client_order_id` VARCHAR(50) COLLATE utf8mb4_general_ci NOT NULL,
`order_revenue` FLOAT NOT NULL,
PRIMARY KEY(`order_id`),
FOREIGN KEY (`client_order`) REFERENCES raw_orders(`order_id`)
)
The reason you cannot add the constraint is because you specify different collations for the columns in the two tables. Also, the columns should be the same size, although MySQL will let you create the constraint even if they're not.
Change to the same collation (COLLATE utf8mb4_unicode_ci for instance) for both columns and it will work.
See this SQL Fiddle for an example.
The MySQL documentation states that:
Corresponding columns in the foreign key and the referenced key must
have similar data types. The size and sign of integer types must be
the same. The length of string types need not be the same. For
nonbinary (character) string columns, the character set and collation
must be the same.

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.

MySQL Adding Foreign Key

I have two tables:
Table 1: staff_incharge with following columns - First_name and staff_id (This is the Primary Key)
Table 2: student-info with following columns - First_name Last_name ID(Primary Key)
I have another column staff_id which I'm trying to make as a Foreign key, but it shows some error.
This is the query I'm using in MySQL Query Browser:
alter table `student_info`
add constraint foreign key (staff_id)
references staff_incharge(staff_id);
The error is:
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 'student_info add constraint foreign key (staff_id) references
staff_incharge' at line 1.
Can someone tell me what I'm doing wrong?
student_info table was created first and then staff_incharge was created next.
Output of the following query:
SHOW CREATE TABLE student_info;
CREATE TABLE `student_info` (
`First_name` varchar(15) NOT NULL DEFAULT '',
`Last_name` varchar(45) NOT NULL,
`ID` int(10) unsigned NOT NULL DEFAULT '0',
`staff_id` int(11) NOT NULL DEFAULT '0',
`City` varchar(15) NOT NULL DEFAULT '',
`Marks1` int(10) unsigned NOT NULL DEFAULT '0',
`Marks2` int(10) unsigned NOT NULL DEFAULT '0',
`Marks3` int(10) unsigned NOT NULL DEFAULT '0',
`Total_marks` int(10) unsigned NOT NULL DEFAULT '0',
`Branch` varchar(10) NOT NULL,
PRIMARY KEY (`ID`,`First_name`)
SHOW CREATE TABLE staff_incharge;
'staff_incharge', 'CREATE TABLE `staff_incharge` (
`First_name` varchar(20) NOT NULL,
`Staff_id` int(11) NOT NULL,
PRIMARY KEY (`Staff_id`)
)
Minimum code - without explicitly naming the foreign key constraint (MySQL will choose a semi-random name):
ALTER TABLE student_info
ADD FOREIGN KEY (staff_id)
REFERENCES staff_incharge (staff_id) ;
Naming the foreign key constraint:
ALTER TABLE student_info
ADD CONSTRAINT staff_incharge__student_info__FK -- name of your choice
FOREIGN KEY (staff_id)
REFERENCES staff_incharge (staff_id) ;
I got it! The problem was that I had given different default values for ID and staff_id.
The main thing is to ensure foreign key and referenced column should have same data types, length, attributes, default values, collation.
These links helped:
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
http://forums.mysql.com/read.php?22,19755,499517

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