In my table, I have two fields: book and reference. Neither are required to be unique on their own. However, the concatenated value of these two values must be unique.
I'm trying to create a generated column that concatenates the two, but I'm receiving the following error message when running the SQL:
Executing:
ALTER TABLE `bibleverses`.`myverses`
ADD COLUMN `fullref` VARCHAR(20) GENERATED ALWAYS AS (CONCAT(book, reference)) STORED AFTER `mp3`;
Operation failed: There was an error while applying the SQL script to the database.
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 'GENERATED ALWAYS AS (CONCAT(book, reference)) STORED AFTER `mp3`' at line 2
SQL Statement:
ALTER TABLE `bibleverses`.`myverses`
ADD COLUMN `fullref` VARCHAR(20) GENERATED ALWAYS AS (CONCAT(book, reference)) STORED AFTER `mp3`
You can achieve this thing by just applying UNIQUE key constraint to both these columns and it will become a composite key so that you can store the unique values in a pair of these two columns. You can try following SQL statement :
ALTER TABLE bibleverses.myverses ADD UNIQUE(book, reference);
First execute an alter table to add your new column, then you run an update to fill out the fild, like:
ALTER TABLE <table_name> ADD COLUMN <column name, type, definition, etc>;
UPDATE TABLE <table_name> SET <field> = <value>;
To create a virtual generated column (which updates if the constituents changes)
ALTER TABLE producerADD COLUMNFullName varchar(32) as (CONCAT(FirstName,' ',Surname));
Related
I can't figure out why I can't change a column's data type from TEXT to INT. Here is the script I'm using.
ALTER TABLE covidworlddata.coviddeaths
ALTER COLUMN total_deaths INT;
Error 1366: Incorrect integer value: '' for column 'total_deaths' at row 1 SQL Statement: ALTER TABLE covidworlddata.coviddeaths CHANGE COLUMN total_deaths total_deaths INT(255) NULL DEFAULT NULL
Error Code: 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 'INT' at line 2
Is it because the first row of the csv is the column names (e.g. total_deaths) as seen below?
Source data csv
ALTER COLUMN does not allow to change the datatype. It only allows you to set/drop DEFAULT values and change the visibility of the column. You must use either CHANGE COLUMN or MODIFY COLUMN. See ALTER TABLE Statement.
This problem causes #1064.
Your column which you want to alter have string datatype now and contains empty string as a value. When you modify the column definition the server must convert the datatype for existing values. Empty string cannot be converted to numeric datatype correctly. You must update your table prevously and set the column values to the value which can be converted correctly (for example, this can be '0' or NULL).
This problem causes #1366.
You have a syntax error, so check the syntax. As a minimum you need this
ALTER TABLE covidworlddata.coviddeaths
CHANGE COLUMN total_deaths total_deaths INT;
That's old column name, new column name (which is the same here) and then the rest of the column definition.
Reference: https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
I'm currently learning SQL using the popsql ide. I'm trying to run the command to create a table but I keep gettting
ER_PARSE_ERROR: 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 '--auto increment automatically adds 1 to the primary key-- name VARCHAR(20) ' at line 2
What am I doing wrong? Below is the sql code
--writing table in all caps differentiates it from other phrases
--the keyword phrase NOT NULL indicates that the column has to have a value in it at every row
--the UNIQUE keyphrase indicates that the column values have to be unique
CREATE TABLE student (
student_id INT AUTO_INCREMENT,--auto increment automatically adds 1 to the primary key
name VARCHAR(20) NOT NULL,
major VARCHAR (20) UNIQUE,
age INT DEFAULT 'undecided', --the DEFAULT keyword will populate a particular row with phrase in quotes if there is no info entered
PRIMARY KEY(student_id)
);
--constraints are rules that we set in our db to control what happens to info.
--creating a rule such that a particular keyword should populate a row when no data is in it is a constraint
DESCRIBE student;
--the keyword 'DROP' deletes any item that we specify--
DROP TABLE student;
--THIS LINE ADDS THE GPA COLUMN TO OUR TABLE--
ALTER TABLE student ADD gpa DECIMAL(3,2);
--this command displays the info that's currently in the table
SELECT * FROM student;
--INSERTING DATA INTO THE TABLE
--the order in which the tables were created should be the order in whcich they should be inserted into the table
--INSERT INTO student VALUES (2,'Rose', 'Chemistry',3.23);
--you can specify what values to add to the database if you don't have a particular key
INSERT INTO student(name, major) VALUES('John', 'Chemistry');
The error message is correct: you should consult the manual when you are in doubt.
From the MySQL documentation on comments:
From a -- sequence to the end of the line. In MySQL, the -- (double-dash) comment style requires the second dash to be followed by at least one whitespace or control character (such as a space, tab, newline, and so on). This syntax differs slightly from standard SQL comment syntax, as discussed in Section 1.7.2.4, “'--' as the Start of a Comment”.
You did not do that, so your syntax is invalid.
I have a database (some) with a table (exp) of over 800 records, I want to change the name of a column from "Nr._CRT" to "ID", keeping the type smallint.
Error is :
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 '._CRT to ID' at line 1
So far I have tried the following
ALTER TABLE exp change Nr._CRT ID ;
ALTER TABLE exp change 'Nr._CRT' 'ID' ;
ALTER TABLE exp RENAME COLUMN Nr._CRT TO ID;
ALTER TABLE exp RENAME COLUMN 'Nr._CRT' TO 'ID';
ALTER TABLE exp CHANGE Nr._CRT ID ;
ALTER TABLE exp CHANGE 'Nr._CRT' 'ID' ;
ALTER TABLE exp RENAME COLUMN Nr._CRT TO ID;
ALTER TABLE exp RENAME COLUMN 'Nr._CRT' TO 'ID';
Any help please ...
Some of the statements in your list are valid MySQL syntax. The problem is how to properly quote the original column name, that contains special characters: you need backticks rather than single quotes (which are meant for litteral strings, not for identifiers).
For example:
ALTER TABLE exp RENAME COLUMN `Nr._CRT` TO id;
I am currently trying to insert brackets into the column name of my table. However, that results in an error when I run my script.
The format of my table in my script previously reads:
cursor.execute("CREATE TABLE IF NOT EXISTS table (date date, voltage decimal (2,2))")
I then made changes to this part of the script to add brackets to my table column name. It now reads:
cursor.execute("CREATE TABLE IF NOT EXISTS table (date date, voltage(V) decimal (2,2))")
After adding the brackets i.e. (V), the script fails to run.
The error I get is:
SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(V) decimal (2,2))' at line 1
How can I add brackets to the column name without obtaining an error?
If you want to use special characters in the name of a database, table, or column, put the name in backticks.
CREATE TABLE IF NOT EXISTS table (
date date,
`voltage(V)` decimal (2,2)
)
You'll also need to use the backticks in all queries that refer to the column, so it will probably annoy all your other programmers.
See When to use single quotes, double quotes, and backticks in MySQL for more information about quoting in MySQL.
After table setup, I suddenly remember to update it by adding one column and assign all the same value to that column. So I wrote the queries on Workbench like this
ALTER TABLE sthebc3_cle ADD COLUMN Species char(30) AFTER Genome_ACC;
SET SQL_SAFE_UPDATES=0;
UPDATE cle.sthebc3_cle
SET Species='StHe';
But it reported error like
error 1054: unknown column "Species" in "field list
I checked table after ALTER, The new column "Species" was indeed added to the column and values are NULL.
How could be the error reported?
You need to make sure that the current session is the database you want. There is always a selected database in MySQL.
If you want to be 100% sure which database you are using you Always put nameofthedatabase.tablename,
For the table
ALTER TABLE cle.sthebc3_cle ADD COLUMN Species char(30) AFTER Genome_ACC;
SET SQL_SAFE_UPDATES=0;
UPDATE cle.sthebc3_cle
SET Species='StHe';
For you view, try this
USE cle;
CREATE VIEW all_cle AS
(SELECT Species, Genome_ACC, CLE_start, CLE_end, CLE_domain
FROM nameofdatabase.cowpea_cle)
UNION
(SELECT Species, Genome_ACC, CLE_start, CLE_end, CLE_domain
FROM nameofdatabase.sthebc3_cle);