+ All Categories
Home > Technology > Php meetup-nop2015

Php meetup-nop2015

Date post: 22-Jan-2018
Category:
Upload: daud-mukadar
View: 361 times
Download: 0 times
Share this document with a friend
20
PHP Malang UG Meet Up, Nopember 2015 by Daud Mukadar @dmukadar Tips & Trik MySQL
Transcript
Page 1: Php meetup-nop2015

PHP Malang UG Meet Up, Nopember 2015

by Daud Mukadar@dmukadar

Tips & Trik MySQL

Page 2: Php meetup-nop2015

$ whoami

• SLUG-er

• KLAS-er

• MPUG-er

• Accidental linux user• Once an employee

• Freelance consultant

Daud Mukadar• Been using PHP when GLOBAL VARIABLE 

was the thing• Love to solve problems• Like to write queries• Like to learn new things• Currently bootstraping: Tunas Mandiri IT 

Solution

Page 3: Php meetup-nop2015

• Other Trick– Understanding join

– Partitioning– Tree structure

• Prepare Data

• Simple tips– forgot password– Que on FK– Serial as PK

– On Select– HEAP!

On Menu Tonight

Page 4: Php meetup-nop2015

• Mockaroo.com

• GenerateData.com

• DummyData.me

• DatabaseTestData.com

Prepare Data

Page 5: Php meetup-nop2015

• Stop MySQL$ sudo service mysqld stop #or sudo /etc/init.d/mysqld stop

• Re-run it without ACL functionality$ sudo mysqld_safe ­­skip­grant­tables

• Hack a new password$ mysql ­u root

mysql> use mysql;

mysql> update user set password = PASSWORD('obladi') where user = 'root'

• Flush itmysql> flush privileges;

• Restart$ sudo kill `pidof mysqld_safe`

$ sudo service mysql start

ST: What's the password dude?

Page 6: Php meetup-nop2015

• Should you use it?• joining int with int

959 rows in set (0.10 sec)

• Joining int with tiny int119 rows in set (0.03 sec)

• Joining int with unsigned int959 rows in set (0.05 sec)

• Joining int with varchar959 rows in set (0.06 sec)

• Joining varchar with varcharEmpty set (1.11 sec)

ST: on Foreign Key

Page 7: Php meetup-nop2015

• Using serialmysql> CREATE TABLE tbl_users (

id SERIAL,

username VARCHAR(40) UNIQUE,

passwd VARCHAR(32) NOT NULL,

last_login TIMESTAMP NOT NULL,

last_ip_address INT(11) UNSIGNED,

created TIMESTAMP,

created_by INT(11),

updated TIMESTAMP,

updated_by INT(11)

) engine=InnoDB;

• Resultmysql> desc tbl_users;

• +­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­+­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+

• | Field           | Type                | Null | Key | Default             | Extra                       |

• +­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­+­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+

• | id              | bigint(20) unsigned | NO   | PRI | NULL                | auto_increment              |

• | username        | varchar(40)         | YES  | UNI | NULL                |                             |

• | passwd          | varchar(32)         | NO   |     | NULL                |                             |

• | last_login      | timestamp           | NO   |     | CURRENT_TIMESTAMP   | on update CURRENT_TIMESTAMP |

• | last_ip_address | int(11) unsigned    | YES  |     | NULL                |                             |

• | created         | timestamp           | NO   |     | 0000­00­00 00:00:00 |                             |

• | created_by      | int(11)             | YES  |     | NULL                |                             |

• | updated         | timestamp           | NO   |     | 0000­00­00 00:00:00 |                             |

• | updated_by      | int(11)             | YES  |     | NULL                |                             |

• +­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­+­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+

• 9 rows in set (0.03 sec)

ST: Primary Key

Page 8: Php meetup-nop2015

• Asterisk or Notmysql> select * from tbl_nilai where id_mhs = 958;                    

+­­­­­­+­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­+­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+

| id   | id_mhs | mata_kuliah                   | semester | nilai | updated    | guid                                 |

+­­­­­­+­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­+­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+

|  464 |    958 | Ilmu ekonomi                  |        3 | 94.78 | 2015­10­01 | 7f5aeb02­ca5d­41d4­a513­51fe11c5bea6 |

|  529 |    958 | Fraud Examination             |        8 | 57.75 | 2015­10­26 | 4fc65837­bd75­424b­9f68­b04903960f1e |

| 1113 |    958 | Matematika Ekonomi            |        3 | 70.39 | 2015­10­04 | 00be5fdc­c28b­4c0a­9bf0­c3d39b2984f9 |

| 1672 |    958 | Ekonomi 2                     |        4 | 66.49 | 2015­10­03 | 5bb0f205­d746­4e60­b3c3­0cb58075d656 |

| 2169 |    958 | Manajemen Sumber Daya Manusia |        1 | 34.91 | 2015­10­08 | c0d11e1c­6204­4a46­9ef6­baecddda2ab1 |

| 2172 |    958 | Manajemen Distribusi Ritel    |        6 | 45.33 | 2015­10­18 | e7c1a632­d9e0­4655­bb4a­329a348d75c5 |

| 2193 |    958 | Studi Kelayakan Bisnis        |       13 | 59.61 | 2015­10­30 | 116ff35d­6cd0­48cb­b307­32731b3c67a6 |

| 2270 |    958 | Studi Kelayakan Bisnis        |       13 | 62.62 | 2015­10­03 | edacb7d0­25db­4dfd­8124­4bd8fd5ffb5c |

| 2668 |    958 | Evaluasi Kinerja              |       12 | 82.41 | 2015­10­06 | a465c348­966e­42de­b14a­36e635301e2a |

| 2715 |    958 | Perilaku Organisasi           |       15 | 67.66 | 2015­10­19 | f0568107­0486­408e­bef3­7cacf9663b0c |

+­­­­­­+­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­+­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+

10 rows in set (0.02 sec)

mysql> select id, id_mhs, mata_kuliah, semester, nilai, updated, guid from tbl_nilai where id_mhs = 74; 

+­­­­­­+­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­+­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+

| id   | id_mhs | mata_kuliah                | semester | nilai | updated    | guid                                 |

+­­­­­­+­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­+­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+

|  879 |     74 | Manajemen Kompensasi       |       10 | 97.98 | 2015­10­13 | b8f9903f­989d­464b­bca1­7d8f689147b2 |

|  890 |     74 | Manajemen Produk & Harga   |        9 | 49.50 | 2015­10­18 | 9b036b96­888b­4e62­a158­d33ac2f3f4ba |

| 1247 |     74 | Audit 1                    |        9 | 81.01 | 2015­10­05 | c1e89f6c­28e2­4b8a­8fc2­dbeca5e1ba0d |

| 1695 |     74 | Manajemen Produk dan Harga |        8 | 39.17 | 2015­10­06 | 5e14640f­1ac6­4add­b9ca­ad1f17293dab |

| 1720 |     74 | Akuntansi Manajemen        |       16 | 93.00 | 2015­10­23 | 198f3e12­ae64­48fa­967a­8ffcc9b01338 |

| 1884 |     74 | Bahasa Inggris             |        3 |  6.96 | 2015­10­24 | 486e42a5­85f1­4a0b­96b1­6997ce705293 |

| 2475 |     74 | Pengantar Manajemen        |        7 | 28.31 | 2015­10­29 | a007b6f1­e912­4d94­963c­05094adc4c5a |

| 2611 |     74 | Audit 1                    |        3 | 55.46 | 2015­10­15 | a3613465­5967­45d7­a6a3­748915a391f6 |

| 2921 |     74 | Ekonomi 2                  |        8 | 10.14 | 2015­10­09 | d4826e58­0174­4922­9682­16ea46065923 |

| 2957 |     74 | Bahasa Inggris             |        9 | 26.33 | 2015­10­26 | 84bc611d­10dc­41aa­8297­67ff29e7ae0e |

+­­­­­­+­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­+­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+

10 rows in set (0.02 sec)

ST: On Select

Page 9: Php meetup-nop2015

• Limiting to 1mysql> select * from tbl_nilai where id_mhs = 435;                                                     

+­­­­+­­­­­­­­+­­­­­­­­­­­­­+­­­­­­­­­­+­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+

| id | id_mhs | mata_kuliah | semester | nilai | updated    | guid                                 |

+­­­­+­­­­­­­­+­­­­­­­­­­­­­+­­­­­­­­­­+­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+

| 30 |    435 | Ekonomi 2   |       11 | 58.40 | 2015­10­12 | 4ef8a9db­5dde­431e­99cd­493bacaeb342 |

+­­­­+­­­­­­­­+­­­­­­­­­­­­­+­­­­­­­­­­+­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+

1 row in set (0.02 sec)

mysql> select * from tbl_nilai where id_mhs = 555 limit 1;

+­­­­­+­­­­­­­­+­­­­­­­­­­­­­­­+­­­­­­­­­­+­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+

| id  | id_mhs | mata_kuliah   | semester | nilai | updated    | guid                                 |

+­­­­­+­­­­­­­­+­­­­­­­­­­­­­­­+­­­­­­­­­­+­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+

| 674 |    555 | Ekonomi Makro |       11 | 61.29 | 2015­10­22 | df79768c­b385­475f­b25c­885a489c62fc |

+­­­­­+­­­­­­­­+­­­­­­­­­­­­­­­+­­­­­­­­­­+­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+

1 row in set (0.01 sec)

ST: On Select

Page 10: Php meetup-nop2015

• MySQL DB Engine• Storing temporary non essential value with heapmysql> CREATE TABLE login_tracker(

user_id INT(11) NOT NULL,

login_time TIMESTAMP,

last_activity TIMESTAMP

) engine=memory;

mysql> INSERT INTO login_tracker(5, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);

• Heap Limitation:– Index usage: not optimal, limited conditional operator, can not be

ordered– No support auto increment– Not for text or blob

ST: HEAP TO THE RESCUE

Page 11: Php meetup-nop2015

UNDERSTANDING JOIN

• AsumsiTABLE_A       TABLE_B

id name       id  name

­­ ­­­­       ­­  ­­­­

1  Pirate     1   Rutabaga

2  Monkey     2   Pirate

3  Ninja      3   Darth Vader

4  Spaghetti  4   Ninja

• Inner Join on A.name = B.nameid name       id  name

­­ ­­­­       ­­  ­­­­

1  Pirate     2   Pirate

3  Ninja      4   Ninja

Page 12: Php meetup-nop2015

UNDERSTANDING JOIN

• AsumsiTABLE_A       TABLE_B

id name       id  name

­­ ­­­­       ­­  ­­­­

1  Pirate     1   Rutabaga

2  Monkey     2   Pirate

3  Ninja      3   Darth Vader

4  Spaghetti  4   Ninja

• Full Outer Join on A.name = B.nameid    name       id    name

­­    ­­­­       ­­    ­­­­

1     Pirate     2     Pirate

2     Monkey     null  null

3     Ninja      4     Ninja

4     Spaghetti  null  null

null  null       1     Rutabaga

null  null       3     Darth Vader

Page 13: Php meetup-nop2015

UNDERSTANDING JOIN

• AsumsiTABLE_A       TABLE_B

id name       id  name

­­ ­­­­       ­­  ­­­­

1  Pirate     1   Rutabaga

2  Monkey     2   Pirate

3  Ninja      3   Darth Vader

4  Spaghetti  4   Ninja

• Left Outer Join on A.name=B.nameid  name       id    name

­­  ­­­­       ­­    ­­­­

1   Pirate     2     Pirate

2   Monkey     null  null

3   Ninja      4     Ninja

4   Spaghetti  null  null

Page 14: Php meetup-nop2015

UNDERSTANDING JOIN

• AsumsiTABLE_A       TABLE_B

id name       id  name

­­ ­­­­       ­­  ­­­­

1  Pirate     1   Rutabaga

2  Monkey     2   Pirate

3  Ninja      3   Darth Vader

4  Spaghetti  4   Ninja

• How ?id  name       id     name

­­  ­­­­       ­­     ­­­­

2   Monkey     null   null

4   Spaghetti  null   null

Page 15: Php meetup-nop2015

UNDERSTANDING JOIN

• AsumsiTABLE_A       TABLE_B

id name       id  name

­­ ­­­­       ­­  ­­­­

1  Pirate     1   Rutabaga

2  Monkey     2   Pirate

3  Ninja      3   Darth Vader

4  Spaghetti  4   Ninja

• How ?id  name       id     name

­­  ­­­­       ­­     ­­­­

2   Monkey     null   null

4   Spaghetti  null   null

Page 16: Php meetup-nop2015

Partitioning

• Check Pluginmysql> show plugins;

+­­­­­­­­­­­­+­­­­­­­­­­+­­­­­­­­­­­­­­­­+­­­­­­­­­+­­­­­­­­­+

| Name       | Status   | Type           | Library | License |

+­­­­­­­­­­­­+­­­­­­­­­­+­­­­­­­­­­­­­­­­+­­­­­­­­­+­­­­­­­­­+

| binlog     | ACTIVE   | STORAGE ENGINE | NULL    | GPL     |

| partition  | ACTIVE   | STORAGE ENGINE | NULL    | GPL     |

| ARCHIVE    | ACTIVE   | STORAGE ENGINE | NULL    | GPL     |

| BLACKHOLE  | ACTIVE   | STORAGE ENGINE | NULL    | GPL     |

| CSV        | ACTIVE   | STORAGE ENGINE | NULL    | GPL     |

| FEDERATED  | DISABLED | STORAGE ENGINE | NULL    | GPL     |

| MEMORY     | ACTIVE   | STORAGE ENGINE | NULL    | GPL     |

| InnoDB     | ACTIVE   | STORAGE ENGINE | NULL    | GPL     |

| MyISAM     | ACTIVE   | STORAGE ENGINE | NULL    | GPL     |

| MRG_MYISAM | ACTIVE   | STORAGE ENGINE | NULL    | GPL     |

+­­­­­­­­­­­­+­­­­­­­­­­+­­­­­­­­­­­­­­­­+­­­­­­­­­+­­­­­­­­­+

10 rows in set (0.02 sec)

• Buat PartisiCREATE TABLE tbl_patient (

    ssn VARCHAR(35),

    first_name VARCHAR(25) NOT NULL,

    last_name VARCHAR(25) NOT NULL,

    title VARCHAR(16) NOT NULL,

    dob DATE,

    registered DATE NOT NULL

)

PARTITION BY RANGE( YEAR(registered) ) (

    PARTITION p0 VALUES LESS THAN (1985),

    PARTITION p1 VALUES LESS THAN (1995),

    PARTITION p2 VALUES LESS THAN (2005),

    PARTITION p3 VALUES LESS THAN (2010),

    PARTITION p4 VALUES LESS THAN MAXVALUE

);

Page 17: Php meetup-nop2015

Tree Structure

• Adjency Listmysql> CREATE TABLE category(

        category_id INT AUTO_INCREMENT PRIMARY KEY,

        name VARCHAR(20) NOT NULL,

        parent INT DEFAULT NULL

);

Mysql> SELECT * FROM category ORDER BY category_id;

+­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­+

| category_id | name                 | parent |

+­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­+

|           1 | ELECTRONICS          |   NULL |

|           2 | TELEVISIONS          |      1 |

|           3 | TUBE                 |      2 |

|           4 | LCD                  |      2 |

|           5 | PLASMA               |      2 |

|           6 | PORTABLE ELECTRONICS |      1 |

|           7 | MP3 PLAYERS          |      6 |

|           8 | FLASH                |      7 |

|           9 | CD PLAYERS           |      6 |

|          10 | 2 WAY RADIOS         |      6 |

+­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­+

10 rows in set (0.00 sec)

Page 18: Php meetup-nop2015

Tree Structure

• Nested Listmysql> CREATE TABLE nested_category (

        category_id INT AUTO_INCREMENT PRIMARY KEY,

        name VARCHAR(20) NOT NULL,

        lft INT NOT NULL,

        rgt INT NOT NULL

);

Mysql> SELECT * FROM category ORDER BY category_id;

+­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­+­­­­­+­­­­­+

| category_id | name                 | lft | rgt |

+­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­+­­­­­+­­­­­+

|           1 | ELECTRONICS          |   1 |  20 |

|           2 | TELEVISIONS          |   2 |   9 |

|           3 | TUBE                 |   3 |   4 |

|           4 | LCD                  |   5 |   6 |

|           5 | PLASMA               |   7 |   8 |

|           6 | PORTABLE ELECTRONICS |  10 |  19 |

|           7 | MP3 PLAYERS          |  11 |  14 |

|           8 | FLASH                |  12 |  13 |

|           9 | CD PLAYERS           |  15 |  16 |

|          10 | 2 WAY RADIOS         |  17 |  18 |

+­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­+­­­­­+­­­­­+

Page 19: Php meetup-nop2015

Tree Structure

• Nested Listmysql> CREATE TABLE nested_category (

        category_id INT AUTO_INCREMENT PRIMARY KEY,

        name VARCHAR(20) NOT NULL,

        lft INT NOT NULL,

        rgt INT NOT NULL

);

Mysql> SELECT * FROM category ORDER BY category_id;

+­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­+­­­­­+­­­­­+

| category_id | name                 | lft | rgt |

+­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­+­­­­­+­­­­­+

|           1 | ELECTRONICS          |   1 |  20 |

|           2 | TELEVISIONS          |   2 |   9 |

|           3 | TUBE                 |   3 |   4 |

|           4 | LCD                  |   5 |   6 |

|           5 | PLASMA               |   7 |   8 |

|           6 | PORTABLE ELECTRONICS |  10 |  19 |

|           7 | MP3 PLAYERS          |  11 |  14 |

|           8 | FLASH                |  12 |  13 |

|           9 | CD PLAYERS           |  15 |  16 |

|          10 | 2 WAY RADIOS         |  17 |  18 |

+­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­+­­­­­+­­­­­+

Page 20: Php meetup-nop2015

Tree Structure

• Nested Listmysql> CREATE TABLE nested_category (

        category_id INT AUTO_INCREMENT PRIMARY KEY,

        name VARCHAR(20) NOT NULL,

        lft INT NOT NULL,

        rgt INT NOT NULL

);

Mysql> SELECT * FROM category ORDER BY category_id;

+­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­+­­­­­+­­­­­+

| category_id | name                 | lft | rgt |

+­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­+­­­­­+­­­­­+

|           1 | ELECTRONICS          |   1 |  20 |

|           2 | TELEVISIONS          |   2 |   9 |

|           3 | TUBE                 |   3 |   4 |

|           4 | LCD                  |   5 |   6 |

|           5 | PLASMA               |   7 |   8 |

|           6 | PORTABLE ELECTRONICS |  10 |  19 |

|           7 | MP3 PLAYERS          |  11 |  14 |

|           8 | FLASH                |  12 |  13 |

|           9 | CD PLAYERS           |  15 |  16 |

|          10 | 2 WAY RADIOS         |  17 |  18 |

+­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­+­­­­­+­­­­­+


Recommended