Insert Euro and Dollar symbol to a column in mysql - mysql

I have 2 rows which looks like this.
This is the create statement of the table.
CREATE TABLE `currency` (
`ID` int(10) unsigned NOT NULL,
`Name` varchar(100) NOT NULL,
`Abbreviation` varchar(10) NOT NULL,
`symbol` varchar(5) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE = InnoDB DEFAULT CHARSET = latin1;
And all I want to do is insert symbols for them, but its not working.
I tried doing it this way.
insert into currency (id, symbol ) values (1,'€') ;
insert into currency (id, symbol ) values (2,'$') ;

Use UPDATE with a WHERE condition.
UPDATE currency
SET symbol = '€'
WHERE ID = 1;
UPDATE currency
SET symbol = '$'
WHERE ID = 2;

You need to insert all the columns:
insert into currency(id, name, description, symbol )
select 1, 'EUR', 'Euro', '€' union all
select 2, 'USD', 'United States Dollar', '$';
You have columns that are declared NOT NULL with no default values. These need to be assigned a value.

Related

Trigger to update all records with same data

I have a column named 'Ratio' in my table. Which I want to use to store the value of the ratio of the items with respect to the total value at a specific date.
The problem I'm facing is that the trigger only changes the ratio of one row (NEW.row) and leaves the values of the rest alone. But when one value changes I want them all to change since it is a ratio.
This is what I have so far;
CREATE TABLE `item_table` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`Date` DATE DEFAULT NULL,
`Name` VARCHAR(20) DEFAULT NULL,
`Value` DECIMAL(7 , 6 ) DEFAULT NULL,
`Ratio` DECIMAL(7 , 6 ) DEFAULT NULL,
PRIMARY KEY (`id`)
);
CREATE
TRIGGER `ins_ratio`
BEFORE INSERT ON `item_table` FOR EACH ROW
SET NEW . `Ratio` = NEW.`Value` / (SELECT
SUM(`Value`)
FROM
`item_table`
WHERE
Date = NEW.Date);
How can I get this done?
CREATE TRIGGER `ins_ratio`
BEFORE INSERT ON `item_table`
FOR EACH ROW
SET NEW.`Ratio` = NEW.`Value` / (SELECT COALESCE(SUM(`Value`), 0) + NEW.`Value`
FROM `item_table`
WHERE `Date` = NEW.`Date`);
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=e387a87a05422afec40ecad070e78627
Pay attention - your Ratio is cumulative, not recalculational.

MYSQL trigger insertion and updation errors : MySQL error 1241: Operand should contain 1 column(s)

I have 4 tables which look like....
CREATE TABLE `Faculty` (
`FID` varchar(10) NOT NULL,
`Name` varchar(30) NOT NULL,
`DOB` date NOT NULL,
`Sem` varchar(1) NOT NULL,
`Section` varchar(1) NOT NULL,
`Subject Code` varchar(6) NOT NULL,
`Dep` varchar(3) NOT NULL,
`Hours Taken` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `Student` (
`USN` varchar(10) NOT NULL,
`DOB` date NOT NULL,
`Dep` varchar(3) NOT NULL,
`SEM` int(1) NOT NULL,
`Class` varchar(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `Student Attendance` (
`USN` varchar(10) NOT NULL,
`Subject Code` varchar(6) NOT NULL,
`Attendance` int(11) DEFAULT '0',
`Absent Days` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `Subjects` (
`Subject` varchar(40) NOT NULL,
`Subject Code` varchar(6) NOT NULL,
`Dep` varchar(3) NOT NULL,
`Sem` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Now I needed to make sure that the number of hours that a student (Student Attendance.Attendance) has attended aren't greater than the number of hours the faculty member has taken (Faculty.Hours Taken) for that particular subject (Subject Code). So I wrote a trigger to check to check on insertion and updation if the above condition is satisfied.
If the number of hours(Student Attendance.Attendance) are greater then I have set the Student Attendance.Attendance to some String. I'm hoping that this will work as an assertion and give me an error.
CREATE TRIGGER `HoursCheckonInsert` BEFORE INSERT ON `Student
Attendance`
FOR EACH ROW SET NEW.Attendance= IF(
( Select k.USN, s.`Subject Code`,f.`Hours Taken`,s.Attendance
From Faculty f, `Student Attendance` s, Student k
where s.`Subject Code` = f.`Subject Code` AND k.SEM = f.Sem AND k.Class=f.Section AND s.USN=k.USN AND
NEW.Attendance < f.`Hours Taken` OR NEW.Attendance = f.`Hours Taken`
),
NEW.Attendance,
'abcdef'
)
CREATE TRIGGER `HoursCheckonUpdate` BEFORE UPDATE ON `Student Attendance
FOR EACH ROW SET NEW.Attendance= IF(
(Select k.USN, s.`Subject Code`,f.`Hours Taken`,s.Attendance
From Faculty f, `Student Attendance` s, Student k
where NEW.`Subject Code`=f.`Subject Code` AND NEW.USN=k.USN AND
NEW.USN=s.USN AND k.SEM=f.Sem AND k.Class=f.Section AND k.DEP=f.DEP
AND (NEW.Attendance < f.`Hours Taken` OR NEW.Attendance = f.`Hours Taken`)
),
NEW.Attendance,
'abcdef'
)
When I try to insert a value into the Student Attendance, I get this error:
INSERT INTO `Student Attendance` (`USN`, `Subject Code`, `Attendance`, `Absent Days`) VALUES ('1KS15BT001', '15BT44', '0', '0');
Error:
#1241 - Operand should contain 1 column(s)
I'm new to triggers in MySQL.
MySQL's IF() function expects a scalar expression as its first argument. A scalar expression means it must be one column, and at most one row.
The subquery you use has four columns, and some unknown number of rows.
From your comments, it sounds like you're just interested in whether there are zero or more than zero rows matching the condition. You're right, it doesn't matter what columns you select—if you're using an EXISTS predicate to test the subquery instead of returning the result of the subquery in a scalar expression.
You should use the EXISTS predicate for what you want to do. See https://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html
CREATE TRIGGER `HoursCheckonInsert`
BEFORE INSERT ON `Student Attendance`
FOR EACH ROW
SET NEW.Attendance = IF(
EXISTS(
SELECT *
FROM Faculty AS f
JOIN `Student Attendance` AS s ON s.`Subject Code` = f.`Subject Code`
JOIN Student AS k ON k.SEM = f.Sem AND k.Class=f.Section AND k.uSN = s.USN
WHERE NEW.Attendance <= f.`Hours Taken`),
NEW.Attendance,
'abcdef');
Then it really doesn't matter what columns you put in the select-list. I show using SELECT * above. It doesn't matter, because EXISTS is only checking for the presence of any matching rows, and it won't have a result set at all—only the boolean for whether there are zero or more than zero rows.
You should also use JOIN syntax instead of the obsolete comma-style syntax for joins. I show that in the example above.

mysql query to join five tables

want to join tables
i have five tables like this
Table-1 named as software
CREATE TABLE IF NOT EXISTS `software` (
`software_name` varchar(50) NOT NULL,
`software_version` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `software` (`software_name`, `software_version`) VALUES
('freemap', '1.0'),
('freegps', '1.2');
Table-2 named as cms
CREATE TABLE IF NOT EXISTS `cms` (
`cms_name` varchar(50) NOT NULL,
`cms_product` varchar(50) NOT NULL,
`cms_version` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `cms` (`cms_name`, `cms_product`, `cms_version`) VALUES
('org:freemap:1.0', 'freemap', '1.0'),
('org:freegps:1.0', 'freegps', '1.2');
Table-3 named as cms_to_sve
CREATE TABLE IF NOT EXISTS `cms_to_sve` (
`cms_id` varchar(50) NOT NULL,
`sw_vul_id` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `cms_to_sve` (`cms_id`, `sw_vul_id`) VALUES
('org:freemap:1.0', '423'),
('org:freemap:1.0', '424'),
('org:freemap:1.0', '425'),
('org:freemap:1.0', '426'),
('org:freegps:1.2', '940'),
('org:freegps:1.2', '941');
Table-4 named as software_details
CREATE TABLE IF NOT EXISTS `software_details` (
`sw_id` varchar(50) NOT NULL,
`sve_id` varchar(50) NOT NULL,
`score` varchar(50) NOT NULL,
`ratio` varchar(50) NOT NULL,
`swe_id` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `software_details` (`sw_id`, `sve_id`, `score`, `ratio`, `swe_id`) VALUES
('423', '2001-1991', '5', 'high', '320'),
('424', '2004-1996', '7.5', 'medium', '460'),
('425', '2008-9001', '8', 'low', '122'),
('426', '2012-0002', '4', 'high', '128'),
('940', '2003-1993', '6', 'medium', '424'),
('941', '2006-1994', '3', 'high', '112');
Table-5 named as swe
CREATE TABLE IF NOT EXISTS `swe` (
`swe_name` varchar(50) NOT NULL,
`swe_id` varchar(50) NOT NULL,
`swe_des` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `swe` (`swe_name`, `swe_id`, `swe_des`) VALUES
('ref software', '320', 'hello'),
('ref complicated', '480', 'hi welcome'),
('ref contact', '122', 'how are you'),
('ref admire', '123', 'who is that'),
('ref super', '424', 'well join us'),
('ref nice', '112', 'cheers');
i want to join these five tables
i have few hints
need to compare table 1 and table 2 (that is table
software and table cms)
compare software_name with cms_product and
software_version with cms_version
these should relate with cms_name
with second table column cms_name has to join with the third
table common column cms_id
where in third table cms_id must be equal to sw_vul_id
then join fourth table
now join fourth table from third table using sw_vul_id is equal
to sw_id and get the remaining column values
join fifth table and fourth table by swe-id and get the other
column values
finally i want to have an output like this
i need query for this .
Just like PeteCon said in his comment , left join can be used, hope this will help.
select tbl1.software_name, tbl1.software_version, tbl4.sve_id, tbl4.score, tbl4.ratio, tbl5.swe_id, tbl5.swe_name, tbl5.swe_des
from software tbl1
left join cms tbl2 on tbl1.software_name = tbl2.cms_product and tbl1.software_version = tbl2.cms_version
left join cms_to_sve tbl3 on tbl2.cms_name = tbl3.cms_id
left join software_details tbl4 on tbl3.sw_vul_id = tbl4.sw_id
left join swe tbl5 on tbl4.swe_id = tbl5.swe_id
http://sqlfiddle.com/#!9/3735e7/1
I think some of your sample data is not sufficient to get the result that you want, but in your real database the query should give you something like you want.

Invalid use of group function - MySQL using COALESCE

I want a database table with links that are used to generate the navigation on a website. For instance, the text 'Home' will link to 'http://example.com/home' and the text 'Twitter' will link to the Twitter URL, etc. I also wanted to be able to change the order in which the links are presented, hence the order column. I also want to be able to edit the links, that's why I'm using auto_incremented id's.
Now I want order to be unique, so my plan was to get the max of order and just add one. This is the query I'm using, but it will return: Invalid use of group function
INSERT INTO
`links`
(`id`, `order`, `text`, `html_text`, `link`, `html_link`)
VALUES
(NULL, COALESCE((MAX(`order`) + 1), 1), 'text', 'text (html)', 'url', 'url (html)');
My table is like this:
CREATE TABLE IF NOT EXISTS `links` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order` int(11) NOT NULL,
`text` varchar(31) NOT NULL,
`html_text` varchar(63) NOT NULL,
`link` varchar(127) NOT NULL,
`html_link` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `order` (`order`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
How do I get a valid query doing just what I want?
Thanks in advance!
If you want to do it in one shot, you'll have to do an INSERT ... SELECT combination to get the value from the database and insert based on it;
INSERT INTO
`links`
(`id`, `order`, `text`, `html_text`, `link`, `html_link`)
SELECT
NULL, COALESCE((MAX(`order`) + 1), 1), 'text', 'text (html)', 'url', 'url (html)'
FROM `links`;
An SQLfiddle to test with.

Generate auto incremented id for BPM application

Within a BPM web application, I have a field for an invoice # on a particular page but I need for it to be auto generated every time a user attaches an invoice and views that page. That number must be unique and preferably auto-incremented. A value for the invoice # field can be displayed by querying from a table from an external MYSQL database. So every time a user lands on that particular page, a SELECT query statement can be fired.
On MYSQL end, how would I set this up? So basically, I would like to setup a query for that invoice # field where it will for run a query for example,
SELECT invoice_num FROM invoice_generator
and every time this query runs, it would return the next incremented number.
You can use mysql trigger concept here....
I have added one example here...
It will be very usefull for u (see this link also :http://www.freemindsystems.com/mysql-triggers-a-practical-example/)
CREATE TABLE `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL DEFAULT '',
`price` int(20) NOT NULL DEFAULT '0',
`other` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `products_name_idx` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `freetags` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`tag` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `freetagged_objects` (
`tag_id` int(20) NOT NULL DEFAULT '0',
`object_id` int(20) NOT NULL DEFAULT '0',
`tagged_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`module` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`tag_id`, `object_id`),
KEY `freetagged_objects_tag_id_object_id_idx` (`tag_id`, `object_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
INSERT_PRODUCTS_TAGS
DELIMITER ||
DROP TRIGGER IF EXISTS insert_products_tags;
||
DELIMITER ##
CREATE TRIGGER insert_products_tags AFTER INSERT ON products
FOR EACH ROW
BEGIN
DECLARE current_id integer;
DECLARE tag_id integer;
DECLARE next integer;
DECLARE tag_field varchar(255);
DECLARE next_sep integer;
DECLARE current_tag varchar(255);
DECLARE right_tag varchar(255);
-- We use the field other as comma-separated tag_field
SET tag_field = NEW.other;
-- Check for empty tags
IF (CHAR_LENGTH(tag_field) <> 0) THEN
-- Loop until no more ocurrencies
set next = 1;
WHILE next = 1 DO
-- Find possition of the next ","
SELECT INSTR(tag_field, ',') INTO next_sep;
IF (next_sep > 0) THEN
SELECT SUBSTR(tag_field, 1, next_sep - 1) INTO current_tag;
SELECT SUBSTR(tag_field, next_sep + 1, CHAR_LENGTH(tag_field)) INTO right_tag;
set tag_field = right_tag;
ELSE
set next = 0;
set current_tag = tag_field;
END IF;
-- Drop spaces between comas
SELECT TRIM(current_tag) INTO current_tag;
-- Insert the tag if not already present
IF (NOT EXISTS (SELECT tag FROM freetags WHERE tag = current_tag)) THEN
-- Insert the tag
INSERT INTO freetags (tag) values (current_tag);
SELECT LAST_INSERT_ID() INTO tag_id;
ELSE
-- Or get the id
SELECT id FROM freetags WHERE tag = current_tag INTO tag_id;
END IF;
-- Link the object tagged with the tag
INSERT INTO freetagged_objects
(tag_id, object_id, module)
values
(tag_id, NEW.id, 'products');
END WHILE;
END IF;
END;
##
Now If you execute an insert on products table:
INSERT INTO PRODUCTS
(name, price, other)
values
("product1", 2, "tag1, tag2,tag3 , tag 4");