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.
Related
Error Recieved:
ERROR 1215 (HY000): Cannot add foreign key constraint
Code:
CREATE TABLE servers (
serverID varchar(20) NOT NULL,
serverPrefix varchar(1) NOT NULL,
captchaEnabled boolean NOT NULL,
giveawayEnabled boolean NOT NULL,
pollsEnabled boolean NOT NULL,
reactRolesEnabled boolean NOT NULL,
serverStatsEnabled boolean NOT NULL,
storyTimeEnabled boolean NOT NULL,
suggestionsEnabled boolean NOT NULL,
welcomeEnabled boolean NOT NULL,
captchaChannelID varchar(20),
storyTimeChannelID varchar(20),
suggestionsChannelID varchar(20),
reportChannelID varchar(20),
PRIMARY KEY (serverID)
);
CREATE TABLE welcomeMessage(
welcomeMessageID int NOT NULL AUTO_INCREMENT,
serverID varchar(20) NOT NULL,
`description` varchar(300),
instruction varchar(300),
footer varchar(100),
welcomeChannelID varchar(20),
DM boolean NOT NULL,
PRIMARY KEY (welcomeMessageID),
FOREIGN KEY (`serverID`) REFERENCES servers (`serverID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;
The error is being thrown on "CREATE TABLE welcomeMessage". I tried separating the create from the foreign key using:
ALTER TABLE welcomeMessage ADD FOREIGN KEY (`serverID`) REFERENCES servers(`serverID`);
However it just creates the table then throws the error on the "Alter" line instead.
Any help is appreciated.
The problem is the charset and the collation. For the foreign key to be well-formed, both the parent and the child columns must have the same charset and collation. The first statement relies on the default settings, while the second has explicit settings. If those are different from the defaults, a failure occurs.
Your code works if I just comment out the charset .... collate ... part of the second create table statement, as you can see in this db fiddle.
Both storage engines also need to be aligned. If your database uses MyISAM as a default engine, the code won’t work either (as the second statement explicitly uses InnoDB).
DROP TABLE IF EXISTS userRole;
DROP TABLE IF EXISTS account;
CREATE TABLE account (
lionID VARCHAR(50) NOT NULL,
firstName VARCHAR(50) NOT NULL,
hashedpass VARCHAR(255) NOT NULL,
PRIMARY KEY (lionID)
);
CREATE TABLE roles (
roleID INT UNSIGNED AUTO_INCREMENT NOT NULL,
lionID VARCHAR(50) NOT NULL,
administrator BOOLEAN NOT NULL DEFAULT False,
qRole VARCHAR(255) NOT NULL,
PRIMARY KEY (roleID),
FOREIGN KEY (lionID) REFERENCES account(lionID)
);
I am having trouble understanding why I am getting an error.. the foreign key lionID in roles is referencing the primary key lionID in account but it doesn't seem to like it. Any help is greatly appreciated.
ERROR 1064 (42000) at line 76: 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 'FOREIGN KEY lionID REFERENCES account lionID'
)' at line 8
I think the problem is in create statement:
You need to use Syntax:
CONSTRAINT `fk_account`
FOREIGN KEY (lionID) REFERENCES account(lionID)
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.
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`),
This is probably a trivial question, but I'm still a little clumsy when it comes to foreign key constraints so I wanted to make sure.
Let's say I have a table countries with the fields country_id (PK) and name, and a table cities with the fields city_id (PK), name and country_id (FK).
The foreign key cities.country_id has the constraint ON DELETE SET NULL. As I understand it, this means that if a record from countries is deleted, any records in cities that reference that deleted record's country_id will have its country_id field set to NULL.
What if, however, cities.country_id has the attribute NOT NULL? Will this prevent the foreign key constraint from working properly? It would make sense that it does, but I just want to check.
If you set ON DELETE SET NULL to your foreign key then it won't allow you to set the field as NOT NULL.
So you won't be able to create or alter the table with column as NOT NULL and ON DELETE SET NULL on CountryId
When I run the below statements:
CREATE TABLE `country` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ;
CREATE TABLE `city` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`countryId` int(10) unsigned DEFAULT NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_country` (`countryId`),
CONSTRAINT `FK_country` FOREIGN KEY (`countryId`) REFERENCES `country` (`id`) ON DELETE SET NULL ON UPDATE SET NULL
);
And I got the error in MySQL 5.5 is:
Schema Creation Failed: 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 'NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_country` (`countryId`),
CONSTRAINT `' at line 4: