<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MySQL Question of the Day &#187; learning</title>
	<atom:link href="http://mysql-qotd.casperia.net/archives/tag/learning/feed" rel="self" type="application/rss+xml" />
	<link>http://mysql-qotd.casperia.net</link>
	<description>mysql 5.0/5.1 questions for learning purposes</description>
	<lastBuildDate>Fri, 06 Aug 2010 16:42:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Question 85: What is the result of the query?</title>
		<link>http://mysql-qotd.casperia.net/archives/562</link>
		<comments>http://mysql-qotd.casperia.net/archives/562#comments</comments>
		<pubDate>Fri, 06 Aug 2010 16:41:45 +0000</pubDate>
		<dc:creator>plogi</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=562</guid>
		<description><![CDATA[

select n,n and 3,n &#038; 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;


&#160;
Hints:
The subquery v1 returns [...]]]></description>
			<content:encoded><![CDATA[<p><small>
<pre>
select n,n and 3,n &#038; 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;
</pre>
<p></small><br />
&nbsp;<br />
Hints:<br />
The subquery v1 returns column n with the rows 0,1,2,3,4,5,6,7,null.<br />
&#8220;&#038;&#8221; is the binary and-operation. &#8220;MOD&#8221; is the modulo (remainder) calculation.<br />
Mysql treats 0 as false and anything else as true.<br />
The constants TRUE and FALSE have the values 1 and 0.</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/562/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Question 84: Which of the following are equivalent?</title>
		<link>http://mysql-qotd.casperia.net/archives/541</link>
		<comments>http://mysql-qotd.casperia.net/archives/541#comments</comments>
		<pubDate>Tue, 13 Jul 2010 17:20:39 +0000</pubDate>
		<dc:creator>plogi</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=541</guid>
		<description><![CDATA[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 >= [...]]]></description>
			<content:encoded><![CDATA[<p>1) where a in (1,2,3)<br />
2) where a = 1 and a = 2 and a = 3<br />
3) where b > 10 and b < 20<br />
4) where a = 1 or a = 2 or a = 3<br />
5) where b between 10 and 20<br />
6) where b >= 10 and b < = 20<br />
7) where b >= 10 and b < 20<br />
&nbsp;<br />
Which of the following are equivalent?<br />
(Find all correct answers)<br />
&nbsp;<br />
a) 3 and 5<br />
b) 5 and 6<br />
c) 1 and 4<br />
d) 5 and 7<br />
e) 1 and 2<br />
&nbsp;<br />
[Just for fun]</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/541/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Question 83: How to get the correct price?</title>
		<link>http://mysql-qotd.casperia.net/archives/537</link>
		<comments>http://mysql-qotd.casperia.net/archives/537#comments</comments>
		<pubDate>Mon, 12 Jul 2010 18:03:48 +0000</pubDate>
		<dc:creator>plogi</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=537</guid>
		<description><![CDATA[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&#8230;


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
   [...]]]></description>
			<content:encoded><![CDATA[<p>Extending on the scenario of Q80:<br />
We want to read the correct historical price and sum up per product/day.<br />
The price table is below&#8230;</p>
<p><small>
<pre>
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;
</pre>
<p></small><br />
&nbsp;<br />
[Just for fun]</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/537/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Question 82: More solutions for Q80?</title>
		<link>http://mysql-qotd.casperia.net/archives/535</link>
		<comments>http://mysql-qotd.casperia.net/archives/535#comments</comments>
		<pubDate>Sun, 11 Jul 2010 11:43:14 +0000</pubDate>
		<dc:creator>plogi</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=535</guid>
		<description><![CDATA[Hello again!
Still about question 80&#8230; 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)
&#160;
[Just for fun]
]]></description>
			<content:encoded><![CDATA[<p>Hello again!<br />
Still about question 80&#8230; 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)<br />
&nbsp;<br />
[Just for fun]</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/535/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Question 81: More solutions for Q80?</title>
		<link>http://mysql-qotd.casperia.net/archives/533</link>
		<comments>http://mysql-qotd.casperia.net/archives/533#comments</comments>
		<pubDate>Sat, 10 Jul 2010 17:52:32 +0000</pubDate>
		<dc:creator>plogi</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=533</guid>
		<description><![CDATA[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&#8217;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]  [...]]]></description>
			<content:encoded><![CDATA[<p>Hi!<br />
It is really important to find as many different solutions as possible for a problem. The reason is, that sometimes a certain solution won&#8217;t work depending on the situation. So you need to have alternatives.<br />
For question 80 I offered two possibilities, one with user-variables and one with a subquery. Are there more?</p>
<p>[Just for fun]  </p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/533/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Question 80: How do you read between the rows?</title>
		<link>http://mysql-qotd.casperia.net/archives/528</link>
		<comments>http://mysql-qotd.casperia.net/archives/528#comments</comments>
		<pubDate>Fri, 09 Jul 2010 18:41:48 +0000</pubDate>
		<dc:creator>plogi</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=528</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>Imagine you get the logs of a vending-machine.<br />
You want to know the average time between vending-events.<br />
The log table looks like this:</p>
<p><small>
<pre>
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;
</pre>
<p></small></p>
<p>How do you calculate the average time between the vending-events?<br />
&nbsp;<br />
[Just for fun]</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/528/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Question 79: Which alternatives exist?</title>
		<link>http://mysql-qotd.casperia.net/archives/526</link>
		<comments>http://mysql-qotd.casperia.net/archives/526#comments</comments>
		<pubDate>Thu, 08 Jul 2010 17:29:05 +0000</pubDate>
		<dc:creator>plogi</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=526</guid>
		<description><![CDATA[You want to list all rows from the mysql.help_keyword table,
in which the name contains &#8216;TREE&#8217; or &#8216;TYPE&#8217;.
The obvious choice is:


SELECT * FROM mysql.help_keyword WHERE name like '%TREE%' or name like '%TYPE%';
+-----------------+-------+
&#124; help_keyword_id &#124; name  &#124;
+-----------------+-------+
&#124;              87 &#124; RTREE &#124;
&#124;   [...]]]></description>
			<content:encoded><![CDATA[<p>You want to list all rows from the mysql.help_keyword table,<br />
in which the name contains &#8216;TREE&#8217; or &#8216;TYPE&#8217;.<br />
The obvious choice is:<br />
<small>
<pre>
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)
</pre>
<p></small></p>
<p>What other alternatives can you think of? </p>
<p>[Just for fun]</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/526/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Question 78: How do you repair this table?</title>
		<link>http://mysql-qotd.casperia.net/archives/524</link>
		<comments>http://mysql-qotd.casperia.net/archives/524#comments</comments>
		<pubDate>Wed, 07 Jul 2010 19:07:36 +0000</pubDate>
		<dc:creator>plogi</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=524</guid>
		<description><![CDATA[Again, we&#8217;ll create and then break a table. Follow the instructions below:
&#160;


drop table if exists t1;
create table t1 (tid bigint not null auto_increment,
                 ttype int,
               [...]]]></description>
			<content:encoded><![CDATA[<p>Again, we&#8217;ll create and then break a table. Follow the instructions below:<br />
&nbsp;<br />
<small>
<pre>
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;
</pre>
<p></small></p>
<p>While keeping the mysql session open, go to the dbdir directory and delete t1.MYI .<br />
In the mysql session, issue a “check table t1;”<br />
you should see something like this:<br />
<small>
<pre>
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)
</pre>
<p></small><br />
&nbsp;<br />
Note, that a &#8220;REPAIR TABLE t1;&#8221; will not repair the table this time (try it!).<br />
Pick from the options below everything that needs to be done in order to repair this table.</p>
<pre>
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;
</pre>
<p>&nbsp;<br />
[MyISAM table maintenance - Investigate and repair broken tables]</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/524/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Question 77: How do you copy the table?</title>
		<link>http://mysql-qotd.casperia.net/archives/520</link>
		<comments>http://mysql-qotd.casperia.net/archives/520#comments</comments>
		<pubDate>Sat, 03 Jul 2010 14:10:31 +0000</pubDate>
		<dc:creator>plogi</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=520</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>You want to copy a MyIsam table the fasted way possible from database qotd1 to qotd2 inside the same instance.<br />
Check the scenario below:</p>
<p><small>
<pre>
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;
</pre>
<p></small></p>
<p>You decide, that using operating system means will be the fastest way.<br />
Which of the procedures below will make a consistent copy?<br />
(Find all correct answers)<br />
&nbsp;</p>
<pre>
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;
</pre>
<p>&nbsp;<br />
[MyISAM table maintenance - Backup]</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/520/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Question 76: How do you repair the table?</title>
		<link>http://mysql-qotd.casperia.net/archives/517</link>
		<comments>http://mysql-qotd.casperia.net/archives/517#comments</comments>
		<pubDate>Thu, 01 Jul 2010 18:13:30 +0000</pubDate>
		<dc:creator>plogi</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=517</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>Now we create and break a table:<br />
<small>
<pre>
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;
</pre>
<p></small><br />
&nbsp;<br />
While keeping the mysql session open, go to the dbdir directory and delete t1.MYI .<br />
Then copy break_it.MYI to t1.MYI .<br />
In the mysql session, issue a &#8220;check table t1;&#8221;</p>
<p>you should see something like this:<br />
<small>
<pre>
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)
</pre>
<p></small><br />
How do you repair it again?</p>
<pre>
a) fix table t1;
b) alter table t1 engine myisam;
c) repair table t1;
d) optimize table t1;
</pre>
<p>&nbsp;<br />
[MyISAM table maintenance - Investigate and repair broken tables]</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/517/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

