I have a MySQL database:
CREATE TABLE `users votes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`forumtopicid` int(11) DEFAULT NULL,
`replyid` int(11) DEFAULT NULL
)
I'm trying to create two unique constraints:
CREATE UNIQUE INDEX index_first
ON `users votes`(userid, forumtopicid, replyid)
WHERE forumtopicid IS NOT NULL;
CREATE UNIQUE INDEX index_second
ON `users votes`(userid, replyid)
WHERE forumtopicid IS NULL;
This is throwing an error that I'm unsure how to fix. Is it not possible to add a WHERE clause for MySQL constraints?
#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 'WHERE forumtopicid IS NOT NULL' at line 3
That's correct - you cannot use WHERE in CREATE INDEX statements (in mysql).
Related
I have this code:
CREATE TABLE Company ( Comp_Code INT(5) NOT NULL AUTO_INCREMENT=1000, Comp_Name VARCHAR(15) NOT NULL, PRIMARY KEY (Comp_Code) );
but when i run it in MYSQL from WAMPServer it gives an error:
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 '=1000,
Comp_Name VARCHAR(15) NOT NULL,
PRIMARY KEY (Comp_Code)
)' at line 2
Why is this happening? I can't seem to find any solution to this particular issuer.
AUTO_INCREMENT is column option.
Initial AUTO_INCREMENT=1000 value is table option.
CREATE TABLE Company (
Comp_Code INT(5) NOT NULL AUTO_INCREMENT, -- specify that the column is AI
Comp_Name VARCHAR(15) NOT NULL,
PRIMARY KEY (Comp_Code)
) AUTO_INCREMENT=1000; -- specify initial AI value
I am new to SQL databases. I am using MySQL for my current project
Here is my query
CREATE TABLE FRIENDS (
F_ID VARCHAR(50) NOT NULL PRIMARY KEY,
USER1_ID VARCHAR(50) NOT NULL FOREIGN KEY REFERENCES users(U_ID),
USER2_ID VARCHAR(50) NOT NULL FOREIGN KEY REFERENCES users(U_ID),
TIME_OF_FRIENDSHIP TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
When I run this query I get this error.
>[Error] Script lines: 1-22 -------------------------
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 'CREATE TABLE FRIENDS (
F_ID VARCHAR(50) NOT NULL PRIMARY KEY,
USER1_ID V' at line 17
Warnings: --->
W (1): 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 'CREATE TABLE FRIENDS (
F_ID VARCHAR(50) NOT NULL PRIMARY KEY,
USER1_ID V' at line 17
<---
[Executed: 18/11/2020 8:14:46 AM] [Execution: 0ms]
Can someone help me out with this problem.
I guess you are using MS SQL Server syntax and you try to run it on MySQL. Here is MySQL syntax for creating table with FK.
CREATE TABLE FRIENDS (
F_ID VARCHAR(50) NOT NULL PRIMARY KEY,
USER1_ID VARCHAR(50) NOT NULL,
USER2_ID VARCHAR(50) NOT NULL,
TIME_OF_FRIENDSHIP TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (USER2_ID) REFERENCES USERS(U_ID),
FOREIGN KEY (USER1_ID) REFERENCES USERS(U_ID)
);
Reference https://www.w3schools.com/sql/sql_foreignkey.asp
I am trying to import a schema into MySQL (MariaDB 10.1.36) and I am getting the above error. I also tried direct forward engineering in MySQL Workbench but the results are the same.
ERROR 1064 (42000): 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 ')
ENGINE = InnoDB
Sample code that fails is:
CREATE TABLE IF NOT EXISTS `example`.`adhere` (
`adhere_id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`description` VARCHAR(100) NOT NULL,
`id_uuid` VARCHAR(36) NULL,
PRIMARY KEY (`adhere_id`),
INDEX `ix_tmp_autoinc` (`adhere_id` ASC) VISIBLE)
ENGINE = InnoDB
AUTO_INCREMENT = 19
DEFAULT CHARACTER SET = latin1;
I tried changing the backticks into single quotes in vain, I later removed them to same result. The expectation is to have the table created so do the rest structured like so.
remove the VISIBLE word
DEMO
CREATE TABLE IF NOT EXISTS `example`.`adhere`
(
`adhere_id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`description` VARCHAR(100) NOT NULL,
`id_uuid` VARCHAR(36) NULL,
PRIMARY KEY (`adhere_id`),
INDEX `ix_tmp_autoinc` (`adhere_id` ASC)
)
Check that your version of mariadb supports invisible/visible indexes here https://mariadb.com/kb/en/library/invisible-columns/ and if not turn off the option in mysql workbench here https://dev.mysql.com/doc/workbench/en/wb-table-editor-indexes-tab.html
I am using MySQL version 5.7.18 and MySQL Workbench is targeting version 5.6.20. However, when I try and create a table using the table generator it produces the following sql statement:
CREATE TABLE `sys`.`annotations` (
`id` INT GENERATED ALWAYS AS () VIRTUAL,
`annotation` LONGTEXT NOT NULL,
PRIMARY KEY (`id`));
But this produces the following error message:
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 ') STORED',
`annotation` LONGTEXT NOT NULL,
PRIMARY KEY (`id`))' at line 2
SQL Statement:
CREATE TABLE `sys`.`annotations` (
`id` INT GENERATED ALWAYS AS () STORED,
`annotation` LONGTEXT NOT NULL,
PRIMARY KEY (`id`))
Why would this be happening and how would I go about fixing this?
I have no idea what an empty expression means for a computed column. Nor do I understand a VIRTUAL column being a primary key. I would use:
CREATE TABLE sys.`annotations` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`annotation` LONGTEXT NOT NULL
);
I'm trying to put together a MySQL database for a forum, And when I try to make a section table I keep encountering a problem
#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 7
Here's the code:
CREATE TABLE sections (
sect_id INT(8) NOT NULL AUTO_INCREMENT,
sect_name VARCHAR(255) NOT NULL,
sect_desc VARCHAR(255) NOT NULL,
UNIQUE INDEX sect_name_unique (sect_name),
PRIMARY KEY (sect_id)
) TYPE=INNODB;
Use the following query
CREATE TABLE IF NOT EXISTS sections (
sect_id INT(8) NOT NULL AUTO_INCREMENT,
sect_name VARCHAR(255) NOT NULL,
sect_desc VARCHAR(255) NOT NULL,
UNIQUE INDEX sect_name_unique (sect_name),
PRIMARY KEY (sect_id)
) ENGINE=InnoDB
To mention the type of engine use ENGINE keyword.