Error #1054 in MySQL Trigger - mysql

This is my trigger.
DELIMITER //
CREATE TRIGGER verificare_masa
BEFORE INSERT ON Rezervare
FOR EACH ROW
BEGIN
IF (NEW.Data_Rezervarii=Data_Rezervarii) AND (NEW.NumarMasa=NumarMasa) THEN
SET NEW.NumarMasa= NULL;
END IF;
END //
DELIMITER ;
I want to make trigger on 1 table (rezervare).
When I execute the trigger, it has been created. But, when I insert data into table rezervare, It become
Error Code: 1054. Unknown column 'Data_Rezervarii' in 'field list'
I want to check if a reservation is already in the data base for that date
and mass required is already reserved for that date

From your attempt, it appears that you want to still insert a new row with the same Data_Rezervarii, but with NumarMasa NULL. If so, your trigger should be something like
CREATE TRIGGER verificare_masa
BEFORE INSERT ON Rezervare FOR EACH ROW
SET NEW.NumarMasa = IF(EXISTS(
SELECT 1 FROM Rezervare
WHERE Data_Rezervarii=NEW.Data_Rezervarii
AND NumarMasa = NEW.NumarMasa
),NULL,NEW.NumarMasa);
Then it would work like this:
MariaDB [test]> select * from Rezervare;
+-----------------+-----------+
| Data_Rezervarii | NumarMasa |
+-----------------+-----------+
| 2016-12-12 | 1 |
| 2016-12-12 | 2 |
| 2016-12-13 | 3 |
+-----------------+-----------+
3 rows in set (0.00 sec)
MariaDB [test]> INSERT INTO Rezervare VALUES ('2016-12-12',1),('2016-12-12',4);
Query OK, 2 rows affected (0.20 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [test]> select * from Rezervare;
+-----------------+-----------+
| Data_Rezervarii | NumarMasa |
+-----------------+-----------+
| 2016-12-12 | 1 |
| 2016-12-12 | 2 |
| 2016-12-13 | 3 |
| 2016-12-12 | NULL |
| 2016-12-12 | 4 |
+-----------------+-----------+
5 rows in set (0.00 sec)
But if you actually want to skip the new record completely if one already exists in the table, it should be done by adding a unique index on these two columns and using INSERT IGNORE.

Related

MARIADB - Can SQL convert NULL to an empty string if no default value is provided?

I have a table that has nullable columns:
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
I insert a row with name set to NULL;
INSERT INTO some_table (id, name) VALUES (1, NULL);
Query OK, 1 row affected (0.02 sec)
SELECT * FROM some_table;
+------+------+
| id | name |
+------+------+
| 1 | NULL |
+------+------+
1 row in set (0.01 sec)
If I alter the table's name column to be not-nullable it apparently converts NULL to an empty string:
ALTER TABLE some_table CHANGE COLUMN name name VARCHAR(255) NOT NULL;
Query OK, 1 row affected, 1 warning (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 1
SELECT * FROM some_table;
+------+------+
| id | name |
+------+------+
| 1 | |
+------+------+
1 row in set (0.02 sec)
At this point I would expect an exception to be raised telling me that I have NULL in my dataset and I can not set the column name to NOT NULL.
Is this a configurable option in SQL/MariaDB?
Why is NULL being converted to an empty string?
There is a warning being invoked when altering the table:
SHOW WARNINGS;
+---------+------+-------------------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------------------+
| Warning | 1265 | Data truncated for column 'name' at row 1 |
+---------+------+-------------------------------------------+
1 row in set (0.01 sec)
Version:
SELECT version();
+----------------+
| version() |
+----------------+
| 5.5.62-MariaDB |
+----------------+
1 row in set (0.02 sec)
Apparently, from the documentation for ALTER TABLE, enabling strict mode would prevent your alter statement from succeeding:
This conversion may result in alteration of data. For example, if you shorten a string column, values may be truncated. To prevent the operation from succeeding if conversions to the new data type would result in loss of data, enable strict SQL mode before using ALTER TABLE.
One way to enable strict mode from within MySQL:
SET GLOBAL sql_mode='STRICT_TRANS_TABLES';
See here for other options.
Using 10.3.15-MariaDB-1 on Debian Buster, I cannot reproduce the problem:
MariaDB [foo]> CREATE TABLE some_table(id int(11), name varchar(255));
Query OK, 0 rows affected (0.009 sec)
MariaDB [foo]> INSERT INTO some_table (id, name) VALUES (1, NULL);
Query OK, 1 row affected (0.003 sec)
MariaDB [foo]> SELECT * FROM some_table;
+------+------+
| id | name |
+------+------+
| 1 | NULL |
+------+------+
1 row in set (0.000 sec)
MariaDB [foo]> ALTER TABLE some_table CHANGE COLUMN name name VARCHAR(255) NOT NULL;
ERROR 1265 (01000): Data truncated for column 'name' at row 1
MariaDB [foo]> SELECT * FROM some_table;
+------+------+
| id | name |
+------+------+
| 1 | NULL |
+------+------+
1 row in set (0.000 sec)
MariaDB [foo]> SELECT version();
+-------------------+
| version() |
+-------------------+
| 10.3.15-MariaDB-1 |
+-------------------+
1 row in set (0.000 sec)
If possible, I suggest you update your MariaDB version. It seems very old to me.

what syntax to use to update a SET column in mysql?

I created a column called oilcompany that has SET data (Hunt, Pioneer, Chevron, BP)
I can enter any one of those into the oilcompany column and change from one to another one but I can not figure out how to change from one oilcompany to multiple oilcompany (eg. Hunt and BP)... any suggestion?
In the MySQL documentation there are not examples for UPDATE statements, but I normally use two ways to update these kind of columns:
Using text values
Using numeric values
Creating the test environment
mysql> CREATE TABLE tmp_table(
-> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> oilcompany SET('Hunt', 'Pioneer', 'Chevron', 'BP')
-> );
Query OK, 0 rows affected (0.54 sec)
mysql> INSERT INTO tmp_table(oilcompany) VALUES ('Hunt'), ('Pioneer');
Query OK, 2 rows affected (0.11 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM tmp_table;
+----+------------+
| id | oilcompany |
+----+------------+
| 1 | Hunt |
| 2 | Pioneer |
+----+------------+
2 rows in set (0.00 sec)
Alternative#1: Using Text Values
As a SET is a collection of ENUM elements, and any ENUM element can be treated as a string, then we can do things like:
mysql> UPDATE tmp_table
-> SET oilcompany = 'Hunt,BP'
-> WHERE id = 1;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM tmp_table;
+----+------------+
| id | oilcompany |
+----+------------+
| 1 | Hunt,BP |
| 2 | Pioneer |
+----+------------+
2 rows in set (0.00 sec)
Alternative#2: Using Numeric Values
Any SET element is stored internally as a 64bit number containing the combination of the bits that represent each SET element.
In our table: 'Hunt'=1, 'Pioneer'=2, 'Chevron'=4, 'BP'=8.
Also, mysql allows to use these numbers instead of text values. If we need to see the numeric value in the select, we need to use the SET column inside a numeric expression (E.g. adding zero).
Let's see the current values:
mysql> SELECT id, oilcompany+0, oilcompany FROM tmp_table;
+----+--------------+------------+
| id | oilcompany+0 | oilcompany |
+----+--------------+------------+
| 1 | 9 | Hunt,BP |
| 2 | 2 | Pioneer |
+----+--------------+------------+
2 rows in set (0.00 sec)
Here 9 = 'Hunt' (1) + 'BP' (8) and 2 = 'Pioneer' (2).
Now, let's change the Pioneer to 'Hunt' (1) + 'Chevron' (4):
mysql> UPDATE tmp_table
-> SET oilcompany = 5
-> WHERE id = 2;
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT id, oilcompany+0, oilcompany FROM tmp_table;
+----+--------------+--------------+
| id | oilcompany+0 | oilcompany |
+----+--------------+--------------+
| 1 | 9 | Hunt,BP |
| 2 | 5 | Hunt,Chevron |
+----+--------------+--------------+
2 rows in set (0.00 sec)

is it possible to stop auto increment in mysql for particular record and store duplicate number

i want to insert a duplicated value into table which have auto_increamented colum.
can i insert like this
auto_incremet
+---------------+---------
| invoiceNumber | totalAmt |
+---------------+----------+
| 1 | 200 |
| 0 158
| 2 | 1200 |
| 0 122 |
| 3 65 |
| 4 | 240 |
| 5 | 330 |
| 6 | 80 |
+---------------+---------
i do not want to increment the value for particular record .
is it possible to do so?
it is possible with additional table so that i can send particular records to other table and stop it increment in first table.
but cant it be done with same table ?
Yes, but it can't be 0. It could be 1, though.
MariaDB [wow]> create table test ( id int(11) not null auto_increment, name varchar(15), key id (id));
Query OK, 0 rows affected (0.02 sec)
MariaDB [wow]> insert into test (id, name) values (1, 'mark');
Query OK, 1 row affected (0.00 sec)
MariaDB [wow]> insert into test (id, name) values (0, 'allan');
Query OK, 1 row affected (0.01 sec)
MariaDB [wow]> insert into test (id, name) values (1, 'patrick');
Query OK, 1 row affected (0.00 sec)
MariaDB [wow]> insert into test (name) values ('chris');
Query OK, 1 row affected (0.00 sec)
MariaDB [wow]> insert into test (name) values ('oliver');
Query OK, 1 row affected (0.00 sec)
MariaDB [wow]> insert into test (id, name) values (1, 'damien');
Query OK, 1 row affected (0.00 sec)
Observe:
MariaDB [wow]> select * from test;
+----+---------+
| id | name |
+----+---------+
| 1 | mark |
| 2 | allan |
| 1 | patrick |
| 3 | chris |
| 4 | oliver |
| 1 | damien |
+----+---------+
Obviously this is not a very good idea. Create a second column and call it invoice_id. Increment it using a sequence table.
But yes, to answer the question, although auto_increment must be on a key, it does not need to be on a UNIQUE key such as the primary key.

Stored Procedure returns different result than Query

I am trying to use a Mysql(MariDB) Stored-Procedure of a simple query with one parameter. However, the results of the invoked Procedure differs from the initial query, and I don't understand why.
Here is My initial Query:
MariaDB [(none)]> SELECT * FROM ob1.eco_serie WHERE TS_ID = 3;
+-----+-------+---------------------+---------------------+------+
| id | TS_ID | Date_period | Date_publi | Val |
+-----+-------+---------------------+---------------------+------+
| 4 | 3 | 1996-10-31 00:00:00 | 1996-11-01 00:00:00 | 50.5 |
| 5 | 3 | 1996-11-30 00:00:00 | 1996-12-02 00:00:00 | 53 |
| 6 | 3 | 1996-12-31 00:00:00 | 1997-01-02 00:00:00 | 55.2 |
... ... ........... ............. ...
Then I make a Stored-Procedure of this Query:
MariaDB [(none)]> CREATE PROCEDURE ob1.GET_eco_serie (IN par_serie_ID INT)
-> BEGIN
-> SELECT * FROM ob1.eco_serie WHERE TS_ID = par_serie_ID;
-> END//
Query OK, 0 rows affected (0.06 sec)
But, when I call this Procedure with the same parameters as before, I get an empty result:
MariaDB [(none)]> CALL ob1.GET_eco_serie(#3);
Empty set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Any idea what is going on here? Thanks.
call a procedure like this:
CALL ob1.GET_eco_serie(3);
Just remove # sign

Enforce order on composite primary key

Say I have the following table
item_a_id, item_b_id, value
Where item_a_id and item_b_id are a composite primary key. In my example a,b and b,a are equivalent. Therefore I want to ensure that item_a_id < item_b_id. Obviously the application logic will enforce this but is there a way to ensure the database does too?
In a reasonably current version of MySql you can use triggers to emulate a check constraint that produces the desired behavior.
Well, in your case, you could use trigger to check values before insert/update and swap it to ensure item_a_id will always less than item_b_id.
Assuming the table name is item_links, you could try this:
DELIMITER |
CREATE TRIGGER ensure_a_b_before_insert BEFORE INSERT ON item_links
FOR EACH ROW
BEGIN
IF NEW.item_a_id > NEW.item_b_id THEN
SET #tmp = NEW.item_b_id;
SET NEW.item_b_id = NEW.item_a_id;
SET NEW.item_a_id = #tmp;
END IF;
END;
|
CREATE TRIGGER ensure_a_b_before_update BEFORE UPDATE ON item_links
FOR EACH ROW
BEGIN
IF NEW.item_a_id > NEW.item_b_id THEN
SET #tmp = NEW.item_b_id;
SET NEW.item_b_id = NEW.item_a_id;
SET NEW.item_a_id = #tmp;
END IF;
END;
|
DELIMITER ;
Here's what I got when I test inserting:
mysql> INSERT INTO `item_links` (`item_a_id`, `item_b_id`, `value`)
-> VALUES ('1', '2', 'a')
-> , ('3', '2', 'b')
-> , ('4', '1', 'c');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM `item_links`;
+-----------+-----------+-------+
| item_a_id | item_b_id | value |
+-----------+-----------+-------+
| 1 | 2 | a |
| 2 | 3 | b |
| 1 | 4 | c |
+-----------+-----------+-------+
3 rows in set (0.00 sec)
Update works, too:
mysql> UPDATE `item_links`
-> SET `item_a_id` = 100, `item_b_id` = 20
-> WHERE `item_a_id` = 1 AND `item_b_id` = 2;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM `item_links`;
+-----------+-----------+-------+
| item_a_id | item_b_id | value |
+-----------+-----------+-------+
| 20 | 100 | a |
| 2 | 3 | b |
| 1 | 4 | c |
+-----------+-----------+-------+
3 rows in set (0.00 sec)