MySql Event is not working - mysql

I have got this mysql data structure
| create_t | delete_t |
| 1479643496 | 1479643616 |
| 1479643496 | 1479643616 |
| 1479643637 | 1479643757 |
create_t and delete_t are both BigInt
And i have created an event like this
CREATE EVENT assaassas
ON SCHEDULE EVERY 50 SECOND
DO
DELETE FROM Test WHERE delete_time<='$date->getTimestamp()'"
But it is fruitless.

Related

MySQL 5.6 information_schema.REFERENTIAL_CONSTRAINTS inconsistency

I'm building the Docker image with MySQL 5.6 inside (for tests/CI purposes only) and creates the schema and all the tables and then commit the docker container as an image. I intentionally remap the DATADIR to a specific location to keep it inside the container, and not on the volume.
Base image is mysql/mysql-server:5.6
I noticed this super strange behavior:
Every time I start the container and make the following query for the first time:
select * from information_schema.REFERENTIAL_CONSTRAINTS;
I see something like:
+-----+------------------------+-----+
| ... | UNIQUE_CONSTRAINT_NAME | ... |
+-----+------------------------+-----+
| | NULL | |
| | PRIMARY | |
| | PRIMARY | |
| | PRIMARY | |
| | NULL | |
| | PRIMARY | |
| | NULL | |
But if I call it again straight away I see
+-----+------------------------+-----+
| ... | UNIQUE_CONSTRAINT_NAME | ... |
+-----+------------------------+-----+
| | PRIMARY | |
| | PRIMARY | |
| | PRIMARY | |
| | PRIMARY | |
| | PRIMARY | |
| | PRIMARY | |
| | PRIMARY | |
So after first call some of the constraints with initially NULL values became "initialised" somehow.
The code of creating these foreign keys is similar so have no idea why some of them have NULLs and some don't.
...FOREIGN KEY (campaign) REFERENCES coupon_campaign(_id) ON DELETE CASCADE,...
Why it might harm? I use JOOQ and want to do a jooq-codegen on the created container. And because JOOQ uses this system tables to describe the data structure it produces inconsistent results.
Could somebody please hint why this might happen?

Mysql table does not accept any update request

I am using Server version: 5.6.27-log MySQL Community Server (GPL) and I have a problem with a table.
I tried to update some fields with a GUI software, but when I came back to the command line, the lines I tried to update where not updated.
I tried to see if the table was locked using SHOW OPEN TABLES as stated in various other questions. But my table does not appear to be locked:
+--------------------+-------------------------------------------------+--------+-------------+
| Database | Table | In_use | Name_locked |
+--------------------+-------------------------------------------------+--------+-------------+
| arcdev | SCHEDULED_COMMAND | 0 | 0 |
And as soon as I try to make an update like:
UPDATE SCHEDULED_COMMAND SET field = 1;
The server just keeps loading and nothing happen. I tried on other tables and everything worked just fine.
I also tried some DELETE requests and even a DROP TABLE and nothing work so far...
What am I missing?
Thank you for your precious help!
EDIT: Here is the result of the SHOW PROCESSLIST command while a request is hanging:
+--------+----------+---------------------------------------+-----------+---------+------+----------+-------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+--------+----------+---------------------------------------+-----------+---------+------+----------+-------------------------------+
| 282588 | rdsadmin | localhost:17966 | mysql | Sleep | 2 | | NULL |
| 534575 | arc | XXXXXX:49376 | arcdev | Sleep | 17 | | NULL |
| 534579 | arc | XXXXXX:49443 | arcdev | Query | 0 | init | SHOW PROCESSLIST |
| 534659 | arc | XXXXXX:49836 | arcdev | Query | 14 | updating | DELETE FROM SCHEDULED_COMMAND |
+--------+----------+---------------------------------------+-----------+---------+------+----------+-------------------------------+

disabling entries (for a particular set of days) in a database

I'm working on an application using JSP and MYSQL.
The application includes feature for user to input 'Date' and 'Hours', which are stored in a table 'epinfo' in MYSQL.
+-------+-------------+------+-----+------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+------------+-------+
| uid | varchar(10) | NO | PRI | | |
| dt | date | NO | PRI | 0000-00-00 | |
| hrs | int(2) | YES | | NULL | |
+-------+-------------+------+-----+------------+-------+
Another feature is applying for leave, stored in a different table 'leave'
+--------+-------------+------+-----+------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+------------+-------+
| uid | varchar(10) | NO | PRI | | |
| stdt | date | NO | PRI | 0000-00-00 | |
| enddt | date | NO | PRI | 0000-00-00 | |
+--------+-------------+------+-----+------------+-------+
I need to disable and delete any entries for this time period, in the table epinfo.
Eg. If a leave application entry is 2014-06-14 to 2014-06-16, I need to make sure the user cannot enter any data for 14,15,16th of June in the table epinfo.
I know a BEFORE INSERT Trigger can help here, which goes something like this:
create trigger cons_check
before insert on epinfo
for each row
begin
if exists(select * from leave where stdt<=entered_date and enddt>=entered_date)
then
Disable entry in database
else
insert into table.
end if
end
Can someone help me create this trigger? Or help with a different solution?
Also, the entered date is the value I have in my insert query. How can I use this value in the trigger?

Open a record, update it and insert as new record in MySQL

I have a db with 10 fields, of which only a few change very often. I would like to open the current record, update it, and enter it back into the db with a new auto id while retaining the previous record.
If I INSERT a new record, I have to re-enter all the info even tho it may not have changed.
If I UPDATE a record, it overwrites the previous values.
Any suggestion would be greatly appreciated
How about a suitably modified version of
insert into table select * from table where id='current_id'
Sample query:Say i want to read first record ,lift student, subject but change marks and insert a new record
insert into score(student,subject,marks)
select student,subject,'30' from score where id=1;
Sample Data
+---------+---------+-------+----+
| student | subject | marks | id |
+---------+---------+-------+----+
| A1 | phy | 20 | 1 |
| A1 | bio | 87 | 2 |
| A2 | che | 24 | 3 |
| A3 | che | 50 | 4 |
Table structure:
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| student | varchar(20) | YES | | NULL | |
| subject | varchar(20) | YES | | NULL | |
| marks | int(1) | YES | | NULL | |
| id | int(10) | NO | PRI | NULL | auto_increment |
+---------+-------------+------+-----+---------+----------------+
You can do it via two different queries: SELECT and INSERT, or you can use INSERT INTO SELECT construction. Check documentation for complete reference http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

Dropping table makes MySQL hang

When I try to drop a table, MySQL hangs. I don't have any other open sessions. How to resolve this? I have waited for 10 hours and the process has not terminated.
I'm trying easier answer for newbies as i am:
1) run :
SHOW PROCESSLIST
if you get something like:
+----+-----------------+-----------------+--------+------------+-----------+---------------------------------+---------------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------------+--------+------------+-----------+---------------------------------+---------------------------------------------------+
| 4 | event_scheduler | localhost | NULL | Daemon | 580410103 | Waiting on empty queue | NULL |
| 13 | root | localhost:50627 | airbnb | Sleep | 10344 | | NULL |
| 17 | root | localhost:50877 | NULL | Query | 2356 | Waiting for table metadata lock | SHOW FULL COLUMNS FROM `airbnb`.`characteristics` |
| 18 | root | localhost:50878 | airbnb | Query | 2366 | Waiting for table metadata lock | DROP TABLE `airbnb`.`characteristics` |
| 21 | root | localhost:51281 | airbnb | Query | 2305 | Waiting for table metadata lock | SHOW FULL COLUMNS FROM `airbnb`.`bed_type` |
| 22 | root | localhost:51282 | airbnb | Query | 2301 | Waiting for table metadata lock | SHOW INDEXES FROM `airbnb`.`characteristics` |
| 23 | root | localhost:51290 | airbnb | Query | 2270 | Waiting for table metadata lock | SHOW FULL COLUMNS FROM `airbnb`.`property_type` |
| 24 | root | localhost:51296 | airbnb | Query | 2240 | Waiting for table metadata lock | SHOW INDEXES FROM `airbnb`.`property_type` |
| 26 | root | localhost:51303 | NULL | Query | 2212 | Waiting for table metadata lock | SHOW FULL COLUMNS FROM `airbnb`.`characteristics` |
| 27 | root | localhost:51304 | NULL | Query | 2218 | Waiting for table metadata lock | SHOW FULL COLUMNS FROM `airbnb`.`bed_type` |
| 29 | root | localhost:51306 | NULL | Query | 2176 | Waiting for table metadata lock | SHOW INDEXES FROM `airbnb`.`characteristics` |
| 30 | root | localhost:51308 | NULL | Query | 2122 | Waiting for table metadata lock | DROP TABLE `airbnb`.`characteristics` |
| 34 | root | localhost:51312 | NULL | Query | 2063 | Waiting for table metadata lock | SHOW FULL COLUMNS FROM `airbnb`.`characteristics` |
| 35 | root | localhost:51313 | NULL | Query | 2066 | Waiting for table metadata lock | SHOW FULL COLUMNS FROM `airbnb`.`bed_type` |
| 39 | root | localhost:51338 | NULL | Query | 2004 | Waiting for table metadata lock | SHOW FULL COLUMNS FROM `airbnb`.`characteristics` |
| 40 | root | localhost:51339 | NULL | Query | 2008 | Waiting for table metadata lock | SHOW FULL COLUMNS FROM `airbnb`.`bed_type` |
| 45 | root | localhost | airbnb | Field List | 997 | Waiting for table metadata lock | |
| 46 | root | localhost | airbnb | Field List | 798 | Waiting for table metadata lock | |
| 53 | root | localhost | airbnb | Query | 0 | starting | SHOW PROCESSLIST |
+----+-----------------+-----------------+--------+------------+-----------+---------------------------------+---------------------------------------------------+
with State : waiting for table metadata lock (as mentioned in official answer)
2) KILL 13 (13 coresponding to the Id).
If it's indeed a deadlock, all the following processes will continue normally.
Waiting for table metadata lock
drop table tableA name
SELECT l1.lat, l1.lon, l2.zipcode FROM tableA l1, tableBl2 where l1.lat = l2.latitude and l1.lon = l2.longitude limit 10
If this is your table, see this link
you have an implicit deadlock. Kill the other transactions to release the drop, or kill the drop to release the other transactions.
You can use KILL thread_id, in sql_plus.
I'm adding further information since I came up with another interesting experience.
Metadata Dead locks may equally happen between a ddl operation on a given table (drop, alter...) and a select query on that table.
Yes, select.
So, if you loop over a cursor in mysql (or php, for example with pdo::fetch), and you run a ddl statement on the same table(s), you will get a deadlock.
One solution to this atypical scenario is to release the implicit locks with a commit statement systematically after any select statement is completely fetched.
Restarting MySQL might not be the prettiest solution but it worked for me:
sudo /etc/init.d/mysql restart
mysqladmin drop YOURDATABASE