Created table with generated columns throw an error - mysql

I am trying to build my table in MySQL following this answer. Hower i keep getting errors in MySQL even launching the same query used as example in the answer
this query is from documentation and works correctly
CREATE TABLE triangle (
sidea DOUBLE,
sideb DOUBLE,
sidec DOUBLE AS (SQRT(sidea * sidea + sideb * sideb)) stored
);
this query comes from the answer i've linked and it gives me an error
CREATE TABLE IF NOT EXISTS MyTable (
id int NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
description varchar(50) NOT NULL,
slug text NOT NULL AS (SHA2(CONCAT(name, description), 256) STORED,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8;
this is the query i am trying to execute based on the answer which is giving me also an error
CREATE TABLE IF NOT EXISTS myTable(
id BIGINT NOT NULL AUTO_INCREMENT,
first VARCHAR(104) NOT NULL DEFAULT '',
second DATETIME NULL,
third DATETIME NULL,
fourth VARCHAR(255) NULL,
table_key varchar(64) NOT NULL AS (SHA2(concat_ws(',',first, second, third, fourth), 256)) stored unique,
PRIMARY KEY (id),
INDEX (first));
can you help me figuring out why it's not working?
Note that SHA2(concat_ws(',',first, second, third, fourth), 256) this works in a normal select statement
EDIT
this is the error i am getting
Query execution failed
Reason:
Errore SQL [1064] [42000]: (conn:28) 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 'AS (SHA2(CONCAT(name, description), 256) STORED,
PRIMARY KEY (id)
) DEFAULT ' at line 5
Query is : CREATE TABLE IF NOT EXISTS MyTable (
id int NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
description varchar(50) NOT NULL,
slug text NOT NULL AS (SHA2(CONCAT(name, description), 256) STORED,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8

You have to add the NOT NULL after the AS part (computed column definition) of the column like this:
CREATE TABLE IF NOT EXISTS myTable(
id BIGINT NOT NULL AUTO_INCREMENT,
first VARCHAR(104) NOT NULL DEFAULT '',
second DATETIME NULL,
third DATETIME NULL,
fourth VARCHAR(255) NULL,
table_key varchar(64) AS (SHA2(concat_ws(',',first, second, third, fourth), 256)) stored unique NOT NULL,
PRIMARY KEY (id),
INDEX (first)
);
Also have a look at the official document of CREATE TABLE. There you can find the description of the datatype syntax. The definition of the computed column is part of the datatype.

Related

Multiple Errors in Uploading Database Columns MySQL

I am new to MySQL and am uploading a database for the first time. I am not uploading the data, just the columns for now. The issue is that I get an error for every line of code provided:
#1064 - You have an error in your SQL syntax;
I can understand if I was getting the message for just one or two lines of the code, but I am beginning to think I misformatted all the database file since I'm getting that error message for every line.
I previously changed the primary/foreign keys
CONSTRAINT `PurchasePK` PRIMARY KEY (`P_ORDERNO`)
CONSTRAINT `PurchaseFK` FOREIGN KEY (`SUPPLY_CODE`)
to: P_ORDERNO int(5) NOT NULL auto_increment PRIMARY KEY,
And still get errors.
I would appreciate if someone took a look at the contents of the .sql file below and let me know if there is something I am missing that is giving me consistent errors.
-- Table structure for table `Purchase`
CREATE TABLE IF NOT EXISTS `Purchase` (
`P_ORDERNO` int(5) NOT NULL auto_increment PRIMARY KEY,
`SUPPLY_CODE` int(5) NOT NULL auto_increment FOREIGN KEY,
`P_ORDER_DATE` timestamp NOT NULL,
`P_ORDER_AMT` int(5) NOT NULL,
`SUPPLY_DESC` varchar(50) NULL,
`SUPPLY QTY` int(5) NOT NULL,
);
ENGINE = InnoDB;
-- Table structure for table `Vendor`
CREATE TABLE IF NOT EXISTS `Vendor` (
`VENDORNO` int(5) NOT NULL auto_increment PRIMARY KEY,
`VENDOR_NAME` varchar(50) NULL,
`VENDOR_STREET` varchar(50) NULL,
`VENDOR_CITY` varchar(50) NULL,
`VENDOR_STATE` varchar(2) NULL,
`VENDOR_ZIP` varchar(3) NULL,
`VENDOR_AREA_CODE` varchar(5) NULL,
`VENDOR_PHONE` varchar(10) NULL,
);
ENGINE = InnoDB;
-- Table structure for table `Supply`
CREATE TABLE IF NOT EXISTS `Supply` (
`SUPPLY_CODE` int(5) NOT NULL auto_increment PRIMARY KEY,
`SUPPLY_DESC` varchar(50) NULL,
`SUPPLY QTY` int(5) NOT NULL,
);
ENGINE = InnoDB;
You have trailing commas
`SUPPLY QTY` int(5) NOT NULL,
^---here
);
in each of the table definitions. That makes the DB server expect another field definition, but instead it runs into );

phpmyadmin,mysql - Token Mismatch but i successfully created a table

I am doing a table with a foreign key, here it is.
create table PM_Team_Members
(
PM_Team_Members_ID int NOT NULL auto_increment PRIMARY KEY,
PM_Team_Members_firstName varchar(50) not null,
PM_Team_Members_middleName varchar(50) not null,
PM_Team_Members_lastName varchar(50) not null,
PM_Team_Members_address varchar(255) not null,
PM_Team_Members_contact numeric not null,
PM_Spec_id int,
constraint fk_PM_id foreign key (PM_Spec_id) references PM_Specialization(PM_Spec_id)
)
and this is the reference table
create table PM_Specialization
(
PM_Spec_ID int auto_increment PRIMARY KEY,
PM_Spec_Specialization varchar(50) not null,
PM_Spec_Description varchar(255) not null
)
When I click GO for the query. It said that MySQL returned an empty result set (i.e. zero rows). (Query took 0.2162 sec) which I assume that it go correct. But then a pop-up show that Error: Token Mismatch
How is that? Did I do something wrong or is it some kind of a bug?
This part references PM_Specialization(PM_Spec_id) )
You have PM_Spec_ID not PM_Spec_id in your PM_Specialization table.

I'm trying to create a database but i keep getting the same error, and according to me and my fellow students the code is fine

This is just an example, from the first 2 tables but i get the same error:
MySQL said: Documentation
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 ')' at line 10
CREATE TABLE Tallas
(
IDTalla int NOT NULL AUTO_INCREMENT,
descripcion varchar(45) NOT NULL,
PRIMARY KEY (IDTalla),
)
CREATE TABLE Modelo
(
IDModelo int NOT NULL AUTO_INCREMENT,
IDTalla int NOT NULL,
modeloNombre varchar(45) NOT NULL,
precio varchar(45) NOT NULL,
informacion varchar(45) NOT NULL,
PRIMARY KEY (IDModelo),
FOREIGN KEY (IDTalla) REFERENCES Modelo(IDTalla),
)
Remove the coma at PRIMARY KEY (IDTalla), to make it PRIMARY KEY (IDTalla)
and the same with REFERENCES Modelo(IDTalla),
Remove the comma -> (IDTalla),) (IDTalla))
Looks like you put an extra comma in your create table statement just after primary key.
CREATE TABLE Tallas (
IDTalla int NOT NULL AUTO_INCREMENT,
descripcion varchar(45) NOT NULL,
PRIMARY KEY (IDTalla),
)
This should be:
CREATE TABLE Tallas (
IDTalla int NOT NULL AUTO_INCREMENT,
descripcion varchar(45) NOT NULL,
PRIMARY KEY (IDTalla)
)
Same with the second statement.

learning sql creating table gives error

I am learning to use phpmyadmin and there is a database tshirtshop which has a table department now I want to create a category table but the sql in phpmyadmin is giving me error following is SQL query
CREATE TABLE 'category' (
'category_id' INT NOT NULL AUTO_INCREMENT,
'department_id' INT NOT NULL,
'name' VARCHAR(100) NOT NULL,
'description' VARCHAR(1000),
PRIMARY KEY ('category_id'),
KEY 'idx_category_department_id' ('department_id')
) ENGINE=MyISAM;
and following is the 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
''category' ( 'category_id' INT NOT NULL AUTO_INCREMENT, 'department_id' INT NOT ' at line 1
what should I do to eliminate the error?
Correct query is
CREATE TABLE `category` (
`category_id` INT NOT NULL AUTO_INCREMENT,
department_id INT NOT NULL,
name VARCHAR(100) NOT NULL,
description VARCHAR(1000),
PRIMARY KEY (`category_id`),
KEY `idx_category_department_id` (`department_id`)
) ENGINE=MyISAM;
you cannot use single quote to enclose field name or table name, either use tilt ` or don't use anything. in my query I try to use both just to show you.
Try change the escape char from ' to ` . MySQL thinks that youre object names are strings.
CREATE TABLE `category` (
`category_id` INT NOT NULL AUTO_INCREMENT,
`department_id` INT NOT NULL,
`name` VARCHAR(100) NOT NULL,
`description` VARCHAR(1000),
PRIMARY KEY (`category_id`),
KEY `idx_category_department_id` (`department_id`)
) ENGINE=MyISAM;

Error in SQL statement

I cannot find this error in the sql, could anyone help?
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 'ID MEDIUMINT(9) NOT NULL AUTO_INCREMENT, datetimeTIMESTAMP, title TEXT NOT' at line 1
CREATE TABLE tableName1 (
ID MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
timestamp TIMESTAMP NOT NULL,
title TEXT NOT NULL,
titleEdited TEXT,
description TEXT NOT NULL,
descriptionEdited TEXT,
URL VARCHAR(255) NOT NULL,
URLEdited VARCHAR(255),
imgPath VARCHAR(255) NOT NULL,
PRIMARY KEY (ID),
UNIQUE KEY (URL, imgPath)
)
CREATE TABLE tableName2 (
ID MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
linkID MEDIUMINT(9) NOT NULL,
timestamp TIMESTAMP NOT NULL,
comment TEXT NOT NULL,
userID BIGINT(20) NOT NULL,
PRIMARY KEY(ID),
FOREIGN KEY(linkID) REFERENCES tableName1(ID),
FOREIGN KEY(userID) REFERENCES users(ID)
)
I think the only thing that is wrong here is that mySQL needs a semicolon after each execution. You should just have to end each create table with a ; and this should work
Here is a SQL Fiddle to prove that. (Minus the FK to users since that table is not in your example)
Are you sure you are creating this tables and getting error?
I created them succesfuly.
And with looking error, it seems you are trying to create another field named datetime which type is timestamp? datetime is reserverd word. You need to use backticks.
'ID MEDIUMINT(9) NOT NULL AUTO_INCREMENT, datetimeTIMESTAMP, title TEXT NOT' at line 1