Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4991

Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4993

Warning: Cannot modify header information - headers already sent by (output started at /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php:4991) in /www/htdocs/v030397/mysql-qotd/wp-includes/feed-rss2.php on line 8
MySQL Question of the Day » question http://mysql-qotd.casperia.net mysql 5.0/5.1 questions for learning purposes Fri, 06 Aug 2010 16:42:05 +0000 http://wordpress.org/?v=abc en hourly 1 Question 85: What is the result of the query? http://mysql-qotd.casperia.net/archives/562 http://mysql-qotd.casperia.net/archives/562#comments Fri, 06 Aug 2010 16:41:45 +0000 plogi http://mysql-qotd.casperia.net/?p=562
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.

]]>
http://mysql-qotd.casperia.net/archives/562/feed 1
Question 84: Which of the following are equivalent? http://mysql-qotd.casperia.net/archives/541 http://mysql-qotd.casperia.net/archives/541#comments Tue, 13 Jul 2010 17:20:39 +0000 plogi http://mysql-qotd.casperia.net/?p=541 10 and b < 20 4) where a = 1 or a = 2 or a = 3 5) where b between 10 and 20 6) where b >= 10 and b < = 20 7) where b >= [...]]]> 1) where a in (1,2,3)
2) where a = 1 and a = 2 and a = 3
3) where b > 10 and b < 20
4) where a = 1 or a = 2 or a = 3
5) where b between 10 and 20
6) where b >= 10 and b < = 20
7) where b >= 10 and b < 20
 
Which of the following are equivalent?
(Find all correct answers)
 
a) 3 and 5
b) 5 and 6
c) 1 and 4
d) 5 and 7
e) 1 and 2
 
[Just for fun]

]]>
http://mysql-qotd.casperia.net/archives/541/feed 1
Question 83: How to get the correct price? http://mysql-qotd.casperia.net/archives/537 http://mysql-qotd.casperia.net/archives/537#comments Mon, 12 Jul 2010 18:03:48 +0000 plogi http://mysql-qotd.casperia.net/?p=537 Extending on the scenario of Q80:
We want to read the correct historical price and sum up per product/day.
The price table is below…

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]

]]>
http://mysql-qotd.casperia.net/archives/537/feed 1
Question 82: More solutions for Q80? http://mysql-qotd.casperia.net/archives/535 http://mysql-qotd.casperia.net/archives/535#comments Sun, 11 Jul 2010 11:43:14 +0000 plogi http://mysql-qotd.casperia.net/?p=535 Hello again!
Still about question 80… We have now 3 solutions to read values from the previous row: Subquery, user variables and with a helper-table + join. There is another feature (at least) which could be used, maybe a more procedural way? (hint, hint)
 
[Just for fun]

]]>
http://mysql-qotd.casperia.net/archives/535/feed 1
Question 81: More solutions for Q80? http://mysql-qotd.casperia.net/archives/533 http://mysql-qotd.casperia.net/archives/533#comments Sat, 10 Jul 2010 17:52:32 +0000 plogi http://mysql-qotd.casperia.net/?p=533 Hi!
It is really important to find as many different solutions as possible for a problem. The reason is, that sometimes a certain solution won’t work depending on the situation. So you need to have alternatives.
For question 80 I offered two possibilities, one with user-variables and one with a subquery. Are there more?

[Just for fun]

]]>
http://mysql-qotd.casperia.net/archives/533/feed 1
Question 80: How do you read between the rows? http://mysql-qotd.casperia.net/archives/528 http://mysql-qotd.casperia.net/archives/528#comments Fri, 09 Jul 2010 18:41:48 +0000 plogi http://mysql-qotd.casperia.net/?p=528 Imagine you get the logs of a vending-machine.
You want to know the average time between vending-events.
The log table looks like this:

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]

]]>
http://mysql-qotd.casperia.net/archives/528/feed 3
Question 79: Which alternatives exist? http://mysql-qotd.casperia.net/archives/526 http://mysql-qotd.casperia.net/archives/526#comments Thu, 08 Jul 2010 17:29:05 +0000 plogi http://mysql-qotd.casperia.net/?p=526 You want to list all rows from the mysql.help_keyword table,
in which the name contains ‘TREE’ or ‘TYPE’.
The obvious choice is:
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]

]]>
http://mysql-qotd.casperia.net/archives/526/feed 1
Question 78: How do you repair this table? http://mysql-qotd.casperia.net/archives/524 http://mysql-qotd.casperia.net/archives/524#comments Wed, 07 Jul 2010 19:07:36 +0000 plogi http://mysql-qotd.casperia.net/?p=524 Again, we’ll create and then break a table. Follow the instructions below:
 
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]

]]>
http://mysql-qotd.casperia.net/archives/524/feed 1
Question 77: How do you copy the table? http://mysql-qotd.casperia.net/archives/520 http://mysql-qotd.casperia.net/archives/520#comments Sat, 03 Jul 2010 14:10:31 +0000 plogi http://mysql-qotd.casperia.net/?p=520 You want to copy a MyIsam table the fasted way possible from database qotd1 to qotd2 inside the same instance.
Check the scenario below:

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]

]]>
http://mysql-qotd.casperia.net/archives/520/feed 1
Question 76: How do you repair the table? http://mysql-qotd.casperia.net/archives/517 http://mysql-qotd.casperia.net/archives/517#comments Thu, 01 Jul 2010 18:13:30 +0000 plogi http://mysql-qotd.casperia.net/?p=517 Now we create and break a table:
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]

]]>
http://mysql-qotd.casperia.net/archives/517/feed 1