select n,n and 3,n & 3, n mod 4, n and false,n and true from (select 0 n union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select null) v1;
Hints:
The subquery v1 returns column n with the rows 0,1,2,3,4,5,6,7,null.
“&” is the binary and-operation. “MOD” is the modulo (remainder) calculation.
Mysql treats 0 as false and anything else as true.
The constants TRUE and FALSE have the values 1 and 0.
drop table if exists vending_price ; create table vending_price (product_id int,valid_from date,valid_to date,price decimal (8,2), primary key (product_id,valid_from,valid_to)) engine = innodb; insert into vending_price select v1.product_id,v2.d,adddate(v2.d,interval 3 day),rand() * 15 from (select distinct product_id from vending_log) v1, (select '2010-01-01' as d union select '2010-01-05' union select '2010-01-09' union select '2010-01-13' union select '2010-01-17' ) v2;
[Just for fun]
[Just for fun]
]]>
DROP TABLE IF EXISTS vending_log; CREATE TABLE `vending_log` ( `sold_ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `product_id` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`sold_ts`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- this fills test-data set @a:='2010-01-01 12:00:00'; insert into vending_log select @a:=adddate(@a,interval 1 + rand()*2500 second), floor(rand()*100) from information_schema.tables v1, information_schema.tables v2 limit 1000;
How do you calculate the average time between the vending-events?
[Just for fun]
SELECT * FROM mysql.help_keyword WHERE name like '%TREE%' or name like '%TYPE%'; +-----------------+-------+ | help_keyword_id | name | +-----------------+-------+ | 87 | RTREE | | 107 | TYPE | | 441 | BTREE | | 445 | TYPES | +-----------------+-------+ 4 rows in set (0.01 sec)
What other alternatives can you think of?
[Just for fun]
]]>drop table if exists t1; create table t1 (tid bigint not null auto_increment, ttype int, tsum double, tdate date, primary key (tid),index(ttype,tdate)) engine=myisam; insert into t1 select null,rand()*99999999,floor(rand()*3), adddate('2010-01-01',interval rand()*360 day) from information_schema.columns, information_schema.tables limit 40000 ; flush tables; select concat(@@global.datadir,database()) as dbdir;
While keeping the mysql session open, go to the dbdir directory and delete t1.MYI .
In the mysql session, issue a “check table t1;â€
you should see something like this:
MariaDB [tt]> check table t1; +-------+-------+----------+----------------------------------+ | Table | Op | Msg_type | Msg_text | +-------+-------+----------+----------------------------------+ | tt.t1 | check | Error | Can't find file: 't1' (errno: 2) | | tt.t1 | check | status | Operation failed | +-------+-------+----------+----------------------------------+ 2 rows in set (0.00 sec)
Note, that a “REPAIR TABLE t1;” will not repair the table this time (try it!).
Pick from the options below everything that needs to be done in order to repair this table.
a) mysqladmin -p -u root shutdown b) repair table t1 index_rebuild; c) repair table t1 use_frm; d) repair table t1 extended; e) repair table t1 quick;
[MyISAM table maintenance - Investigate and repair broken tables]
create database qotdt1; create database qotdt2; use qotdt1; set @a:=0; create table qt1 (primary key (tid),index(ttype,tdate)) engine=myisam select @a:=@a+1 as tid,rand()*99999999 as tsum, floor(rand()*3) as ttype, adddate('2010-01-01',interval rand()*360 day) as tdate from information_schema.columns, information_schema.tables limit 100000 ; select @@global.datadir;
You decide, that using operating system means will be the fastest way.
Which of the procedures below will make a consistent copy?
(Find all correct answers)
a) FLUSH TABLES WITH READ LOCK; Copy [datadir]/qotdt1/qt1.* to [datadir]/qotdt2/ UNLOCK TABLES; FLUSH TABLES; b) Shutdown the instance. Copy [datadir]/qotdt1/qt1.* to [datadir]/qotdt2/ Startup the instance. c) LOCK TABLE qotdt1.qt1 READ; FLUSH TABLES; Copy [datadir]/qotdt1/qt1.* to [datadir]/qotdt2/ UNLOCK TABLES; FLUSH TABLES;
[MyISAM table maintenance - Backup]
drop table if exists t1; set @a:=0; create table t1 (primary key (tid),index(ttype,tdate)) engine=myisam select @a:=@a+1 as tid,rand()*99999999 as tsum, floor(rand()*3) as ttype, adddate('2010-01-01',interval rand()*360 day) as tdate from information_schema.columns, information_schema.tables limit 40000 ; create table if not exists break_it like t1; flush tables; select concat(@@global.datadir,database()) as dbdir;
While keeping the mysql session open, go to the dbdir directory and delete t1.MYI .
Then copy break_it.MYI to t1.MYI .
In the mysql session, issue a “check table t1;”
you should see something like this:
MariaDB [tt]> check table t1; +-------+-------+----------+-------------------------------------------------+ | Table | Op | Msg_type | Msg_text | +-------+-------+----------+-------------------------------------------------+ | tt.t1 | check | warning | Table is marked as crashed | | tt.t1 | check | warning | Size of datafile is: 1693848 Should be: 0 | | tt.t1 | check | error | Record-count is not ok; is 45360 Should be: 0 | | tt.t1 | check | warning | Found 45360 key parts. Should be: 0 | | tt.t1 | check | error | Corrupt | +-------+-------+----------+-------------------------------------------------+ 5 rows in set (0.12 sec)
How do you repair it again?
a) fix table t1; b) alter table t1 engine myisam; c) repair table t1; d) optimize table t1;
[MyISAM table maintenance - Investigate and repair broken tables]