How to I correct this? - mysql

I am trying to get this mysql query to work, but it will not.
CREATE TABLE IF NOT EXISTS `gospel_library`.`catalog_book_files` (
`id` INT NOT NULL AUTO_INCREMENT,
`book_id` INT NOT NULL,
`book_order` INT NOT NULL DEFAULT 0,
`added` DATETIME NOT NULL,
`modified` DATETIME NOT NULL,
`version` INT NOT NULL DEFAULT 1,
`downloads` INT NOT NULL DEFAULT 0,
`file` VARCHAR(100) NOT NULL,
`uri` VARCHAR(500) NOT NULL,
PRIMARY KEY (`id`),
INDEX `catalog_book_files_ndx1` (`book_id` ASC, `book_order` ASC),
INDEX `catalog_book_files.fk1_idx` (),
CONSTRAINT `catalog_book_files.fk1`
FOREIGN KEY ()
REFERENCES `gospel_library`.`catalog_book` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
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 '), CONSTRAINT catalog_book_files.fk1 FOREIGN KEY () REFERENCES
`gosp' at line 13
I am trying to figure out what solution I would use to correct this. Any help would be appreciated.

The problem is you are creating an index with no columns specified. Either Remove
INDEX `catalog_book_files.fk1_idx` (),
or change it to specify one or more columns
INDEX `catalog_book_files.fk1_idx` (`somecolumn`),

Related

Cannot creat database tabele: syntax error

I'm trying to create mysql db table as below, but it got a syntax error at line 4.
CREATE TABLE findable_drivers (
id BIGINT NOT NULL AUTO_INCREMENT,
driver_id BIGINT NOT NULL,
current_role VARCHAR(100) NOT NULL, #here
lat DOUBLE NOT NULL,
lng DOUBLE NOT NULL,
findable BOOLEAN DEFAULT 0 NOT NULL,
CONSTRAINT findable_drivers_PK PRIMARY KEY (id),
CONSTRAINT findable_drivers_UN UNIQUE KEY (driver_id),
CONSTRAINT findable_drivers_driver_FK FOREIGN KEY (driver_id) REFERENCES driver(id)
)
ENGINE=InnoDB
DEFAULT CHARSET=latin1
COLLATE=latin1_swedish_ci ;
the error description:
#1064 - 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 'current_role VARCHAR(100) NOT NULL,
lat DOUBLE NOT NULL,
lng DOUBLE NOT N...' at line 4
CURRENT_ROLE() is actually a MySQL/MariaDB system function which returns active roles for the current session. So in your CREATE TABLE statement, the database thinks you are trying to call this function. You should call the current_role column something else. If you must stick with this name, then will have to forever refer to it using backticks:
CREATE TABLE findable_drivers (
id BIGINT NOT NULL AUTO_INCREMENT,
driver_id BIGINT NOT NULL,
`current_role` VARCHAR(100) NOT NULL, -- must use backticks here
lat DOUBLE NOT NULL,
lng DOUBLE NOT NULL,
findable BOOLEAN DEFAULT 0 NOT NULL,
CONSTRAINT findable_drivers_PK PRIMARY KEY (id),
CONSTRAINT findable_drivers_UN UNIQUE KEY (driver_id),
CONSTRAINT findable_drivers_driver_FK FOREIGN KEY (driver_id) REFERENCES driver(id)
) ENGINE=InnoDB
DEFAULT CHARSET=latin1
COLLATE=latin1_swedish_ci;
Tim is correct
It would be good if you use different column name than the defined keywords.

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.

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.

Error 1064 <42000> on mysql when i'm trying to create table

CREATE TABLE `photos` (
`title` varchar(255) not null,
`id` int(11) not null,
`ph_path` varchar(255) not null,
`description` varchar(255) not null,
`privilange` varchar(20) not null,
`owner` varchar(60) not null,
`provoles` int(11),
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=greek;
I'm getting error 1064 <4200> and I'm not sure what is wrong.
You have a trailing comma in the primary key:
PRIMARY KEY (`id`), <--- remove that
The full error would read something like:
check the manual that corresponds to your MySQL server version for the right syntax to use near ') ENGINE=InnoDB
In MySQL, the position pointed to by the error message (near ')ENGINE) will show you the character immediately after where the error occurred. Look to the previous thing in your statement and you'll find your syntax error.
You will have to remove the comma after PRIMARY KEY (`id`).

MySQL: Error Message Can't create table (errno: 150)

I have two tables, 'po' and 'receive'
CREATE TABLE `po` (
`PO_ID` bigint(20) NOT NULL,
`SERVICE_TYPE` bit(1) DEFAULT NULL,
`ENTRY_DATE` date NOT NULL,
`RECEIPT_DATE` date DEFAULT NULL,
`TURNOVER` date DEFAULT NULL,
`MOBILIZATION` date DEFAULT NULL,
`SITE_NAME` varchar(255) NOT NULL,
`SITE_CODE` varchar(45) DEFAULT NULL,
`SITE_TIN` varchar(45) DEFAULT NULL,
`SITE_ADDRESS` varchar(255) NOT NULL,
`COST` decimal(11,2) NOT NULL,
`XML` text,
PRIMARY KEY (`PO_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
CREATE TABLE `receive` (
`RECEIPT_ID` varchar(100) NOT NULL,
`RECEIPT_DATE` date NOT NULL,
`PO_NUMBER` bigint(20) NOT NULL,
`SUPPLIER_ID` int(11) NOT NULL,
PRIMARY KEY (`RECEIPT_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
I'm trying to connect two tables by defining a foreign key 'fk_po' on the table 'receive'
ALTER TABLE `fourthwave`.`receive`
ADD CONSTRAINT `fk_po`
FOREIGN KEY (`PO_NUMBER` )
REFERENCES `fourthwave`.`po` (`PO_ID` )
ON DELETE SET NULL
ON UPDATE SET NULL
, ADD INDEX `fk_po` (`PO_NUMBER` ASC)
However, the alter query above throws an error :
Error Code: 1005. Can't create table 'fourthwave.#sql-aec_11' (errno:150)
Am i getting this error because the field's names, 'PO_ID' and 'PO_NUMBER' on both tables are different?
Execute SHOW ENGINE INNODB STATUS statement after ALTER TABLE, and you will see the error message - 'You have defined a SET NULL condition though some of the
columns are defined as NOT NULL'.
ALTER TABLE `receive`
ADD CONSTRAINT `fk_po`
FOREIGN KEY (`PO_NUMBER` )
REFERENCES `po` (`PO_ID` )
ON DELETE SET NULL
ON UPDATE SET NULL
, ADD INDEX `fk_po` (`PO_NUMBER` ASC);
SHOW ENGINE INNODB STATUS;
You need an index on PO_NUMBER in the receive table. The field you are referencing in a foreign key always should be indexed.