Can anybody help me identify the error in this statement? - mysql

The table looks like this
mysql> DESCRIBE tenants;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| domain | varchar(255) | NO | UNI | NULL | |
| database | varchar(255) | NO | UNI | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
mysql> INSERT INTO tenants(name,domain,database) VALUES ('varun','varun.localhost','sms_varun');
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 'database) VALUES
('varun','varun.localhost','sms_varun')' at line 1
I am using mysql Ver 8.0.30 for Linux on x86_64 (MySQL Community Server - GPL)

DATABASE is a reserved MySQL keyword (see here), so you will have to escape it in your insert statement (and forever) to make it work:
INSERT INTO tenants (name, domain, `database`)
VALUES ('varun', 'varun.localhost', 'sms_varun');
You should avoid using reserved MySQL keywords for your column and table names.

Related

how to take input using concat_ws keyword in insert statement in mysql?

I have a table called 'exam_paper_details', described below:
create table exam_paper_details(exam_id varchar(5) primary key,exam_date date,exam_subject varchar(10),exam_paper_creation_prof varchar(60),exam_paper_creation_prof_division varchar(20),exam_paper_creation_prof_role varchar(10),exam_paper_creation_prof_id varchar(7));
+-----------------------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------------------+-------------+------+-----+---------+-------+
| exam_id | varchar(5) | NO | PRI | NULL | |
| exam_date | date | YES | | NULL | |
| exam_subject | varchar(10) | YES | | NULL | |
| exam_paper_creation_prof | varchar(60) | YES | | NULL | |
| exam_paper_creation_prof_division | varchar(20) | YES | | NULL | |
| exam_paper_creation_prof_role | varchar(10) | YES | | NULL | |
| exam_paper_creation_prof_id | varchar(7) | YES | | NULL | |
+-----------------------------------+-------------+------+-----+---------+-------+
7 rows in set (0.01 sec)
Now I want to insert rows using concat_ws keyword.
I have tried this quesry:
insert into exam_paper_details (exam_id,exam_date,exam_subject,concat_ws(',',exam_paper_creation_prof,exam_paper_creation_prof_division,exam_paper_creation_prof_role,exam_paper_creation_prof_id)) values ('001','2022-02-27','abc','Mr. XYZ,Science,Assi. Prof,A123');
Gives me following 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 '(',',exam_paper_creation_prof,exam_paper_creation_prof_division,exam_paper_creat' at line 1
Again I have tried this below query:
insert into exam_paper_details select '001' as exam_id,'2022-02-27' as exam_date,'abc' as exam_subject,concat_ws(',',exam_paper_creation_prof,exam_paper_creation_prof_division,exam_paper_creation_prof_role,exam_paper_creation_prof_id)='Mr. XYZ,Science,Assi. Prof,A123' from exam_paper_details;
Gives me following error:
ERROR 1136 (21S01): Column count doesn't match value count at row 1
Again I have tried following one:
insert into exam_paper_details (exam_id,exam_date,exam_subject,exam_paper_creation_prof,exam_paper_creation_prof_division,exam_paper_creation_prof_role,exam_paper_creation_prof_id) select * from (select '001' as exam_id,'2022-02-27' as exam_date,'abc' as exam_subject,concat_ws(',',exam_paper_creation_prof,exam_paper_creation_prof_division,exam_paper_creation_prof_role,exam_paper_creation_prof_id)=('Mr. XYZ,Science,Assi. Prof,A123')) as tmp;
This gives me following error:
ERROR 1054 (42S22): Unknown column 'exam_paper_creation_prof' in 'field list'
Please help me.

summing up bigint values in a column

I have a table in mysql (v 5.6.23) that is described as follows:
mysql> describe as_dcm_testing;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| LCID | varchar(32) | NO | PRI | NULL | |
| LASTACTIVITY | varchar(32) | YES | | NULL | |
| USAGE | bigint(20) | YES | | NULL | |
| SERVICELEVEL | varchar(16) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
I want to sum up all of the values in the USAGE column so I tried using the sum function. The issue is I seem to be getting an error . Is this error because usage is BIGINT instead of INT? How do I sum up the values in a column with bigint values?
mysql> select SUM(USAGE) as usage from as_dcm_testing;
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 'USAGE) as usage from as_dcm_testing' at line 1
mysql>
Thanks in advance
A
USAGE is a reserved word in MySQL. You have to enclose it in backticks:
select SUM(`USAGE`) as `usage` from as_dcm_testing;

Using Alter and ignore in mysql to remove duplicates

I am using MYSQL and have a table 'bid' which has duplicates entries into it
My table schema is
ITEM_CODE | int(11) | YES | | NULL | |
| Max_BidP | int(11) | YES | | NULL | |
| Seller_Name | varchar(45) | YES | | NULL | |
| Buyer_Name | varchar(45) | YES | | NULL | |
| ITEM_NAME | varchar(45) | YES | | NULL | |
| Qty | int(11) | YES | | 1 | |
+-------------+-------------+------+-----+---------+-------+
One of the entries in the table
16 | 30 | sahraw | sahraw | J.K Rowling | 1 |
16 | 30 | sahraw | sahraw | J.K Rowling | 1 |
I am trying to remove the dulicates and the query I am specifying is
ALTER IGNORE TABLE bid ADD UNIQUE INDEX (ITEM_CODE , Max_BidP ,Seller_Name , Buyer_Name , ITEM_NAME , Qty);
But its giving me an 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 'IGNORE TABLE bid ADD UNIQUE INDEX (ITEM_CODE , Max_BidP ,Seller_Nam' at line 1
Any suggestions where I am going wrong.
Thanks
Please check the MySQL version you are using.
As of MySQL 5.7.4, the IGNORE clause for ALTER TABLE is removed and its use produces an error.
http://dev.mysql.com/doc/refman/5.7/en/alter-table.html
If you're using MySql 5.7.4 or later, IGNORE is no longer available. See MySQL “ALTER IGNORE TABLE” Error In Syntax

ERROR 1064 (42000) SQL Insert into

I have looked at a lot of other questions asked on this topic and still can not figure out what the error of my code is in SQL. I created a table called easy_drinks and when I try to insert one record it is giving me an error. Can someone please explain to me the error? Also, what is the best way to debug this kind of error in the future if I can't find out the issue?
mysql> DESC easy_drinks;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| drink_name | varchar(40) | NO | | NULL | |
| main | varchar(20) | NO | | NULL | |
| amount1 | decimal(4,2) | NO | | NULL | |
| second | varchar(20) | NO | | NULL | |
| amount2 | decimal(4,2) | NO | | NULL | |
| directions | blob | NO | | NULL | |
+------------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> INSERT INTO easy_drinks (drink_name, main, amount1, second, amount2, directions) VALUES (‘Blackthorn’, ‘tonic water’, 1.5, ‘pineapple juice’, 1, ‘stir with ice, strain into cocktail glass with lemon twist’);
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 'water’, 1.5, ‘pineapple juice’, 1, ‘stir with ice, strain into c' at line 1
I am running MySQL version 5.6.22
Replace your quotes from ` to '

Why my trigger not working in MySQL?

mysql> desc test;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| a | int(10) unsigned | NO | | NULL | |
| b | int(10) unsigned | NO | | NULL | |
| c | int(10) unsigned | NO | | NULL | |
| str | varchar(9) | YES | | NULL | |
+-------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> CREATE TRIGGER Capitalize BEFORE INSERT ON test
-> SET NEW.str = UPPER(NEW.str)
-> ;
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 'SET NEW.str = UPPER(NEW.str)' at line 2
mysql>
I'm following this answer:
How to define a column that can automate capitalizing?
CREATE TRIGGER Capitalize BEFORE INSERT ON test
FOR EACH ROW
SET NEW.str = UPPER(NEW.str);
Heed the advice in the error message: "check the manual that corresponds to your MySQL server version for the right syntax to use."