Tuesday, December 21, 2010

create database tables

Creating database and assigning username/password, for handy


mysql>  create database abc;
mysql> use abc;
mysql> create table tab1 (i int, c char(3));
mysql> insert into tab1 values (1, 'A');
mysql> insert into tab1 values (2, 'B'), (3, 'C'), (4, 'D');


mysql> create user 'def'@'localhost' identified by 'def';
mysql> grant all on abc.* to 'def'@'localhost' with grant option;
mysql> flush_privileges;
mysql> flush privileges;

mysql> show grants for  'def'@'localhost';

+------------------------------------------------------------------------------------------------------------+
| Grants for def@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'def'@'localhost' IDENTIFIED BY PASSWORD '*EFA28F5E94885AA962E1AFC1912CE376EE374F65' |
| GRANT ALL PRIVILEGES ON `abc`.* TO 'def'@'localhost' WITH GRANT OPTION                                     |
+------------------------------------------------------------------------------------------------------------+


$ mysql -u def -p  abc
Enter password:


mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| abc                |
+--------------------+

mysql> use abc;
Database changed
mysql> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| tab1          |
+---------------+


mysql> create index i_idx on tab1 (i);

mysql> show indexes from tab1;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| tab1  |          1 | i_idx    |            1 | i           | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

mysql> select * from tab1;
+------+------+
| i    | c    |
+------+------+
|    1 | A    |
|    2 | B    |
|    3 | C    |
|    4 | D    |
+------+------+

mysql> create table tab2 like tab1;
mysql> desc tab2;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| i     | int(11) | YES  | MUL | NULL    |       |
| c     | char(3) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+

mysql> show indexes from tab2;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| tab2  |          1 | i_idx    |            1 | i           | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+


mysql> select * from tab2;
Empty set (0.00 sec)

mysql> create table tab3 as select * from tab1;
mysql> desc tab3;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| i     | int(11) | YES  |     | NULL    |       |
| c     | char(3) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+

mysql> show indexes from tab3;
mysql> select * from tab3;
+------+------+
| i    | c    |
+------+------+
|    1 | A    |
|    2 | B    |
|    3 | C    |
|    4 | D    |
+------+------+

mysql> insert into tab2 select * from tab1 where mod(i,2)=0;
Query OK, 2 rows affected (0.00 sec)

mysql> select * from tab2;
+------+------+
| i    | c    |
+------+------+
|    2 | B    |
|    4 | D    |
+------+------+

 drop user 'def'@'localhost' ;
 drop table tab1;
 drop database abc;



Monday, June 7, 2010

mysql: hiding command line parameters

If you run mysql --withparameters, in a shell script, all the parameters are visible
say you need count of records to be monitored

file: count.sql
select count(*) from CONTACT;

and you run,
watch -n1 "mysql -hmysqlhost -umysqluser -pmysqlpassword mydb < count.sql "

using ps axwwf|grep mysql, will display it

watch -n1 mysql -hmysqlhost -umysqluser -pmysqlpassword mydb < count.sql

the better option would be creating options file like .my.cnf for client
but there would be default .my.cnf file existing, if not you can do same.

So we will have our own

file: .my-options.cnf
[client]
user=mysqluser
password=mysqlpassword
host=mysqlhost
database=mydb

set owner read-only, and execute

mysql --defaults-extra-file=~/.my-options.cnf < count.sql

this does show the parameters when done ps, but atleast file-permission will protect it and the parameters are hidden.

if you need to put it in cron-job, specify the full path for the above files.

Tuesday, March 2, 2010

Update Data on insert/update

When we have to keep track of the row update, by having the timestamp of either created of the row or the update.


mysql> create table t (i int, d timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP);

mysql> insert into t (i) values (10);
mysql> insert into t (i) values (20);

mysql> select * from t;
+------+---------------------+
| i | d |
+------+---------------------+
| 10 | 2010-03-02 14:46:24 |
| 20 | 2010-03-02 14:46:27 |
+------+---------------------+

mysql> update t set i=30 where i=20;

mysql> select * from t;
+------+---------------------+
| i | d |
+------+---------------------+
| 10 | 2010-03-02 14:46:24 |
| 30 | 2010-03-02 14:50:52 |
+------+---------------------+

Tuesday, February 16, 2010

Search and Replace first occurence of the word


This SQL query is to search a text in varchar field and replace with the given text

Here @field is the table field, we are using now() -- current timestamp
@search is the search pattern, we are searching for number 0
@replace is what we want to replace the search pattern with we are replacing with A

select @field:=now() `field`,@search:='0' `search`, @replace:='A' `replace`,
@fieldr:=reverse(@field) `l`, @searchr:=reverse(@search) `l`, @lengthf:=length(@field) `l`, @lengths:=length(@search)-1 `l`,
concat(left(@field,instr(@field,@search)-1),@replace,right(@field,@lengthf-instr(@field,@search))) `finalfield`

We can see what is in field has replacement done in finalfield

Search and Replace last occurence of the word


This SQL query is to search a text in varchar field and replace with the given text

Here @field is the table field, we are using now() -- current timestamp
@search is the search pattern, we are searching for number 2
@replace is what we want to replace the search pattern with we are replacing with A

select @field:=now() `field`,@search:='2' `search`, @replace:='A' `replace`,
@fieldr:=reverse(@field) `l`, @searchr:=reverse(@search) `l`, @lengthf:=length(@field) `l`, @lengths:=length(@search)-1 `l`,
concat(left(@field,@lengthf-locate(@searchr,@fieldr)-@lengths),@replace,right(@field,locate(@searchr,@fieldr)-1)) `finalfield`

We can see what is in field has replacement done in finalfield

Friday, February 12, 2010

Data Types

Storage Requirements for Numeric Types in MyISAM

Data Type Storage Required
TINYINT 1 byte
SMALLINT 2 bytes
MEDIUMINT 3 bytes
INT, INTEGER 4 bytes
BIGINT 8 bytes
FLOAT(p) 4 bytes if 0 <= p <= 24, 8 bytes if 25 <= p <= 53
FLOAT 4 bytes
DOUBLE [PRECISION], REAL 8 bytes
DECIMAL(M,D), NUMERIC(M,D) Varies; see following discussion
BIT(M) approximately (M+7)/8 bytes


Type Bytes Signed(Min/Max) Unsigned(Min/Max)
TINYINT 1 -128/127 0/255
SMALLINT 2 -32768/32767 0/65535
MEDIUMINT 3 -8388608 / 8388607 0/16777215
INT 4 -2147483648 / 2147483647 0/4294967295
BIGINT 8 -9223372036854775808 / 9223372036854775807 0 /18446744073709551615