unexpected error 1054 in sql - 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);

Related

in SQL is there a code to fill in a new column when you add a new column to a table?

I find it difficult to fill a new column that I just added to an already existing table in SQL
I tried the Insert command, to fill in the table afresh, but couldn't see it true because the table has 1445 rows
You could specify a default value when you add the column:
ALTER TALE mytable ADD COLUMN new_column VARCHAR(10) DEFAULT 'my_value'
Alternatively, once you added the column, you could use an update statement:
UPDATE mytable
SET new_column = 'my_value'
Just like the answer above you can use ALTER TABLE to add a new column
ALTER TABLE dummy
ADD temp_column VARCHAR(50);
Then you can use the update and add a where statement to be more specific
UPDATE dummy
-> SET temp_column = 'DEFAULT'
-> where id > 10;
Alternatively you could try using Five, you can import your database in it as an SQL dump, it actually allows you to do way more with new columns, like copy data from an existing field in the table or fill in the values from a query
here is an image of Five prompting you to fill in values for a new column
there are other things as well like you can write queries directly in Five, reuse the queries and the tables can be generated with simple point and click.
Disclaimer: I work for Five

How to make sure that the column value is always added in Uppercase?

I have an existing column host and want to make sure that the column value host is always added in Uppercase.
I am using Alter Table command as below:
ALTER TABLE `mydb`.`myTable`
CHANGE COLUMN `host` `host`
VARCHAR(255) GENERATED ALWAYS AS (UPPER()) STORED;
This is causing an error and I am not able to alter the column.
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1582: Incorrect parameter count in the call to native function 'UPPER'
SQL Statement:
ALTER TABLE mydb.myTable
CHANGE COLUMN host host VARCHAR(255) GENERATED ALWAYS AS (UPPER()) VIRTUAL
I want to make sure that moving forward the value inserted in host is always in uppercase.
I want to achieve this by updating the value in uppercase at runtime.
I don't want to add any constraint as it will cause an error when the host value is entered in lowercase.
It is not entirely clear what you are trying to do.
If you want to set a constraint on the table that allows only upper case letters in host, use a check constraint:
alter table mytable
add constraint chk_host_upper
check (host rlike '^[A-Z]*$')
;
This prevents lower case character to be written to the column; an attempt to do so results in a runtime error.
If, on the other hand, you want to create a new column, that returns the upper case value of host, regardless of the actual column code:
alter table mytable
add column host_upper varchar(50) -- same as the original column
as (upper(host))
;
With this set up at hand, you can query the new column host_upper when you want the upper case values.
Finally: if you want to convert input value to upper case on the fly on inserts, then you need a trigger:
delimiter //
create trigger mytrigger
before insert on mytable
for each row
begin
set new.host = upper(new.host);
end
//
delimiter ;
You can just assign the host column to have a case insensitive collation:
ALTER TABLE mydb.myTable MODIFY host VARCHAR(255)
CHARACTER SET latin1
COLLATE latin1_swedish_ci;
Now the case of the host you use when inserting does not matter, and you may compare against this column using any case. If you still need to view the host as uppercase, then just use UPPER():
SELECT UPPER(host) AS host_upper
FROM myTable;

mysql error 1062 during alter table modify column

I have a table that looks like this:
CREATE TABLE t1 (
id BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
col1 VARCHAR(256),
UNIQUE INDEX t1_col1_index (col1)
)
I'm trying to modify the col1 type using the following query:
ALTER TABLE t1 MODIFY COLUMN col1 varchar(191) COLLATE utf8mb4_unicode_ci;
However, I run into this duplication error:
error: ("1062", "QMYSQL3: Unable to execute statement", "Duplicate entry '+123456789' for key 't1_col1_index'")
I initially thought it could be because two or more rows might 'contain' similar value for col1 and on changing varchar length the data gets truncated but then I found out that data truncation wouldn't even allow the query to go through. Any pointers on what could be causing this?
EDIT (Resolved): Truncation does happen when ##sql_mode is not set with STRICT_TRANS_TABLES. This was causing the error.
You are reducing the length of a varchar column that is controlled by a UNIQUE constraint.
This is risky business. Oversize data will be silently trimed (unless you have the ##sql_mode set to STRICT_TRANS_TABLES in which case an error will be raised). This probably generates duplicates, which cause the error to be raised by your UNIQUE constraint.
You can check the max length of the values in your column with :
SELECT MAX(CHAR_LENGTH(col1)) FROM t1:
I am not sure if this is work.
Try to check the table t1.
select count(1) from t1 where col1 = 123456789
Now if count is greater than one then try to remove the other one and leave only one record.
Then try to run your statement again.
Reminder:
Do back up first before removing.

Truncated incorrect integer value MySQL

I have a table ABC that has many columns, two of which are:
ID - VARCHAR(10) and ROLE VARCHAR(10).
Now I have been trying to update the column ROLE using the ID and this is the query:
UPDATE TABLE ABC
SET ROLE='READ_ONLY'
WHERE ID='AB234PQR'
Now for some unknown reason, i have been getting the error - truncated incorrect integer value. I have no idea where I am going wrong.I have been banging my head over this for a while now.
I have visited other questions with the similar title.All use convert or some other function in where clause, But I have not used any such thing, still it gives me the same error.
I checked the table description and it seems fine. Where can I be going wrong? Any help is appreciated.
Can you please try this:
UPDATE ABC
SET ROLE='READ_ONLY'
WHERE ID='AB234PQR'
The correct syntax to update table entries is:
UPDATE `table_name`
SET `column_name` = 'value'
WHERE `column_name2` = 'value2';
It is as well recommended to use backticks around table names and column names, just like the way you can see in my snippet above.
Therefore using
UPDATE `ABC`
SET `ROLE` = 'READ_ONLY'
WHERE `ID` = 'AB234PQR'
should do the trick.

MySQL - Concatenate Existing Fields in New Generated Field

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));