Download - MySQL Schema Maintenance

Transcript
Page 1: MySQL Schema Maintenance

MySQL schema maintenance

Giuseppe MaxiaMySQL Community Team Lead

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.

Thursday, 03 December 2009

Page 4: MySQL Schema Maintenance

tables

Thursday, 03 December 2009

Page 5: MySQL Schema Maintenance

tables

table nametable nametable name

column column column

Thursday, 03 December 2009

Page 6: MySQL Schema Maintenance

tables

table nametable nametable name

column column column

CREATE TABLE employees ( ID INT NOT NULL, name VARCHAR(20), salary INT, PRIMARY KEY (ID))

Thursday, 03 December 2009

Page 7: MySQL Schema Maintenance

relationship

employeesemployees

ID INT

name varchar (10)

salary INT

dept_ID INT

departmentsdepartments

ID INT

name varchar (10)

Thursday, 03 December 2009

Page 8: MySQL Schema Maintenance

relationship

employeesemployees

ID INT

name varchar (10)

salary INT

dept_ID INT

departmentsdepartments

ID INT

name varchar (10)

1

Thursday, 03 December 2009

Page 9: MySQL Schema Maintenance

relationship

employeesemployees

ID INT

name varchar (10)

salary INT

dept_ID INT

departmentsdepartments

ID INT

name varchar (10)

1many

Thursday, 03 December 2009

Page 10: MySQL Schema Maintenance

relationship

employeesemployees

ID INT

name varchar (10)

salary INT

dept_ID INT

salessales

cust_ID INT

emp_ID INT

s_date DATE

amount DECIMAL

customercustomer

ID INT

name varchar (10)

Thursday, 03 December 2009

Page 11: MySQL Schema Maintenance

relationship

employeesemployees

ID INT

name varchar (10)

salary INT

dept_ID INT

salessales

cust_ID INT

emp_ID INT

s_date DATE

amount DECIMAL

customercustomer

ID INT

name varchar (10)

11

Thursday, 03 December 2009

Page 12: MySQL Schema Maintenance

relationship

employeesemployees

ID INT

name varchar (10)

salary INT

dept_ID INT

salessales

cust_ID INT

emp_ID INT

s_date DATE

amount DECIMAL

customercustomer

ID INT

name varchar (10)

manymany

11

Thursday, 03 December 2009

Page 13: MySQL Schema Maintenance

relationship

employeesemployees

ID INT

name varchar (10)

salary INT

dept_ID INT

salessales

cust_ID INT

emp_ID INT

s_date DATE

amount DECIMAL

customercustomer

ID INT

name varchar (10)

manymany

11

many to many

Thursday, 03 December 2009

Page 14: MySQL Schema Maintenance

Main actors

• Production

• Development

• Stage

• Test

Thursday, 03 December 2009

Page 15: MySQL Schema Maintenance

Production database

• You must have one

Thursday, 03 December 2009

Page 16: MySQL Schema Maintenance

Development database

• Where you create new thingsThursday, 03 December 2009

Page 17: MySQL Schema Maintenance

Test database

• Where you experiment crazy ideas

Thursday, 03 December 2009

Page 18: MySQL Schema Maintenance

Stage database

• Where you test before deploying to production

Thursday, 03 December 2009

Page 19: MySQL Schema Maintenance

Schema maintenance actions

• Reverse engineering

• Forward engineering

• Synchronization

Thursday, 03 December 2009

Page 20: MySQL Schema Maintenance

Reverse Engineering

Thursday, 03 December 2009

Page 21: MySQL Schema Maintenance

Reverse Engineering

DDLdept_empdept_empdept_emp

departmentdepartmentdepartment

dept_managerdept_managerdept_manager

employeesemployeesemployees

titlestitlestitles

salariessalariessalaries

Thursday, 03 December 2009

Page 22: MySQL Schema Maintenance

Reverse Engineering

DDLdept_empdept_empdept_emp

departmentdepartmentdepartment

dept_managerdept_managerdept_manager

employeesemployeesemployees

titlestitlestitles

salariessalariessalaries

Thursday, 03 December 2009

Page 23: MySQL Schema Maintenance

Reverse Engineering

DDLdept_empdept_empdept_emp

departmentdepartmentdepartment

dept_managerdept_managerdept_manager

employeesemployeesemployees

titlestitlestitles

salariessalariessalaries

Thursday, 03 December 2009

Page 24: MySQL Schema Maintenance

Forward Engineering

Thursday, 03 December 2009

Page 25: MySQL Schema Maintenance

Forward Engineering

Thursday, 03 December 2009

Page 26: MySQL Schema Maintenance

Forward Engineering

Thursday, 03 December 2009

Page 27: MySQL Schema Maintenance

Forward Engineering

DDLdept_empdept_empdept_emp

departmentdepartmentdepartment

dept_managerdept_managerdept_manager

employeesemployeesemployees

titlestitlestitles

salariessalariessalaries

Thursday, 03 December 2009

Page 28: MySQL Schema Maintenance

Forward Engineering

DDLdept_empdept_empdept_emp

departmentdepartmentdepartment

dept_managerdept_managerdept_manager

employeesemployeesemployees

titlestitlestitles

salariessalariessalaries

WARNING!

Forward engineerin

g is destr

uctive.

Thursday, 03 December 2009

Page 29: MySQL Schema Maintenance

Synchronization

Thursday, 03 December 2009

Page 30: MySQL Schema Maintenance

Synchronization

Thursday, 03 December 2009

Page 31: MySQL Schema Maintenance

Synchronization

Thursday, 03 December 2009

Page 32: MySQL Schema Maintenance

Synchronization

Thursday, 03 December 2009

Page 33: MySQL Schema Maintenance

Synchronization

Thursday, 03 December 2009

Page 34: MySQL Schema Maintenance

Synchronization

Thursday, 03 December 2009

Page 35: MySQL Schema Maintenance

Synchronization

Thursday, 03 December 2009

Page 36: MySQL Schema Maintenance

Synchronization

Thursday, 03 December 2009

Page 37: MySQL Schema Maintenance

Synchronization

Thursday, 03 December 2009

Page 38: MySQL Schema Maintenance

Synchronization

Thursday, 03 December 2009

Page 39: MySQL Schema Maintenance

Synchronization

Thursday, 03 December 2009

Page 40: MySQL Schema Maintenance

Synchronization

Thursday, 03 December 2009

Page 41: MySQL Schema Maintenance

Synchronization

Thursday, 03 December 2009

Page 42: MySQL Schema Maintenance

How it works

Production

StageTest

Development

OK?

no

yes

OK?yes

no

Thursday, 03 December 2009

Page 43: MySQL Schema Maintenance

How else it works

Production

StageTest

Development

• Partition adjustment

• Dynamic schemas

• Hot fixes

Thursday, 03 December 2009

Page 44: MySQL Schema Maintenance

What happens in between

Test

Development

OK?

no

Thursday, 03 December 2009

Page 45: MySQL Schema Maintenance

What happens in between

Test

Development

OK?

no What now?

Thursday, 03 December 2009

Page 46: MySQL Schema Maintenance

What happens in between

Test

Development

OK?

no What now? Wrong schema here

Thursday, 03 December 2009

Page 47: MySQL Schema Maintenance

What happens in between

Test

Development

OK?

no What now? Wrong schema here

REWIND?

Thursday, 03 December 2009

Page 48: MySQL Schema Maintenance

Schema maintenance: how to

1. Poor man's wayThursday, 03 December 2009

Page 49: MySQL Schema Maintenance

How poor is the poor man?• only command line access• No Perl, PHP, or Python• Can't install anything

• Command line access• Perl or Python available• Can install modules

• Command line access• GUI• Can install everything

Thursday, 03 December 2009

Page 50: MySQL Schema Maintenance

How poor is the poor man?• only command line access• No Perl, PHP, or Python• Can't install anything

• Command line access• Perl or Python available• Can install modules

• Command line access• GUI• Can install everything

EXTREMELY

POOR

Thursday, 03 December 2009

Page 51: MySQL Schema Maintenance

How poor is the poor man?• only command line access• No Perl, PHP, or Python• Can't install anything

• Command line access• Perl or Python available• Can install modules

• Command line access• GUI• Can install everything

POOR

EXTREMELY

POOR

Thursday, 03 December 2009

Page 52: MySQL Schema Maintenance

How poor is the poor man?• only command line access• No Perl, PHP, or Python• Can't install anything

• Command line access• Perl or Python available• Can install modules

• Command line access• GUI• Can install everything

POOR

EXTREMELY

POOR

RICH

Thursday, 03 December 2009

Page 53: MySQL Schema Maintenance

HOST 1

so, the extremely poor man's way ...

HOST 2

ProductionDevelopment

Thursday, 03 December 2009

Page 54: MySQL Schema Maintenance

HOST 1

so, the extremely poor man's way ...

get the schema

TEXT FILE

HOST 2

ProductionDevelopment

Thursday, 03 December 2009

Page 55: MySQL Schema Maintenance

HOST 1

so, the extremely poor man's way ...

get the schema

TEXT FILE

HOST 2

get the schema

TEXT FILE

ProductionDevelopment

Thursday, 03 December 2009

Page 56: MySQL Schema Maintenance

HOST 1

so, the extremely poor man's way ...

get the schema

TEXT FILE

HOST 2

get the schema

TEXT FILE

diff

ProductionDevelopment

Thursday, 03 December 2009

Page 57: MySQL Schema Maintenance

HOST 1

so, the extremely poor man's way ...

get the schema

TEXT FILE

HOST 2

get the schema

TEXT FILE

diff

ProductionDevelopment

then … uh … figure out how to continue

Thursday, 03 December 2009

Page 58: MySQL Schema Maintenance

HOST 1

the poor man's way ...HOST 2

ProductionDevelopment

Perl Script

get the schema

TEXT FILE

get the schema

TEXT FILE

diff

Thursday, 03 December 2009

Page 59: MySQL Schema Maintenance

HOST 1

the poor man's way ...HOST 2

ProductionDevelopment

Perl Script

get the schema

TEXT FILE

get the schema

TEXT FILE

diff

TELLS YOU WHAT TO DO

Thursday, 03 December 2009

Page 60: MySQL Schema Maintenance

HOST 1

not so poor man's way ...HOST 2

ProductionDevelopment

Python Script

DOES A LOT OF THINGS

http://schemasync.org

TELLS YOU WHAT TO DO

Thursday, 03 December 2009

Page 61: MySQL Schema Maintenance

HOST 1

not so poor man's way ...HOST 2

ProductionDevelopment

Python Script

DOES A LOT OF THINGS

http://schemasync.org

TELLS YOU WHAT TO DO

WARNING: doesn't deal with partitionsThursday, 03 December 2009

Page 62: MySQL Schema Maintenance

The rich man

http://aquafold.com

Thursday, 03 December 2009

Page 63: MySQL Schema Maintenance

The rich man

http://aquafold.com

WARNING: doesn't deal with partitionsThursday, 03 December 2009

Page 64: MySQL Schema Maintenance

MySQL Workbench

http://wb.mysql.com

Thursday, 03 December 2009

Page 65: MySQL Schema Maintenance

MySQL Workbench

http://wb.mysql.com

Thursday, 03 December 2009

Page 66: MySQL Schema Maintenance

MySQL Workbench

http://wb.mysql.com

Thursday, 03 December 2009

Page 67: MySQL Schema Maintenance

Schema and model

Thursday, 03 December 2009

Page 68: MySQL Schema Maintenance

Schema and model

DDLdept_empdept_empdept_emp

departmentdepartmentdepartment

dept_managerdept_managerdept_manager

employeesemployeesemployees

titlestitlestitles

salariessalariessalaries

Thursday, 03 December 2009

Page 69: MySQL Schema Maintenance

Schema and model

DDLdept_empdept_empdept_emp

departmentdepartmentdepartment

dept_managerdept_managerdept_manager

employeesemployeesemployees

titlestitlestitles

salariessalariessalaries

Thursday, 03 December 2009

Page 70: MySQL Schema Maintenance

Physical schema

implemented in a specific DBMS

Thursday, 03 December 2009

Page 71: MySQL Schema Maintenance

Physical schema

DDLdept_empdept_empdept_emp

departmentdepartmentdepartment

dept_managerdept_managerdept_manager

employeesemployeesemployees

titlestitlestitles

salariessalariessalaries

implemented in a specific DBMS

Thursday, 03 December 2009

Page 72: MySQL Schema Maintenance

Conceptual model

DBMS agnostic

Thursday, 03 December 2009

Page 73: MySQL Schema Maintenance

Conceptual model

DBMS agnostic

Thursday, 03 December 2009

Page 74: MySQL Schema Maintenance

MySQL Workbench basics

Thursday, 03 December 2009

Page 75: MySQL Schema Maintenance

Creating tables (1)

Thursday, 03 December 2009

Page 76: MySQL Schema Maintenance

Creating tables (2)

Thursday, 03 December 2009

Page 77: MySQL Schema Maintenance

Creating tables (3)

Thursday, 03 December 2009

Page 78: MySQL Schema Maintenance

Creating tables (3)

Thursday, 03 December 2009

Page 79: MySQL Schema Maintenance

Creating tables (4)

Thursday, 03 December 2009

Page 80: MySQL Schema Maintenance

Creating tables (4)

Thursday, 03 December 2009

Page 81: MySQL Schema Maintenance

Creating tables (5)

Thursday, 03 December 2009

Page 82: MySQL Schema Maintenance

Creating tables (5)

Thursday, 03 December 2009

Page 83: MySQL Schema Maintenance

database connections(1)

Thursday, 03 December 2009

Page 84: MySQL Schema Maintenance

database connections(2)

Thursday, 03 December 2009

Page 85: MySQL Schema Maintenance

Reverse engineering in the old world

Thursday, 03 December 2009

Page 86: MySQL Schema Maintenance

Reverse engineering in the old world

show create table departments\G

Create Table: CREATE TABLE `departments`

(

`dept_id` int(10) unsigned NOT NULL,

`department` char(30) NOT NULL,

PRIMARY KEY (`dept_id`),

KEY `department` (`department`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1

1 row in set (0.00 sec)

Thursday, 03 December 2009

Page 87: MySQL Schema Maintenance

Reverse engineering in the old world

show create table departments\G

Create Table: CREATE TABLE `departments`

(

`dept_id` int(10) unsigned NOT NULL,

`department` char(30) NOT NULL,

PRIMARY KEY (`dept_id`),

KEY `department` (`department`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1

1 row in set (0.00 sec)

Thursday, 03 December 2009

Page 88: MySQL Schema Maintenance

Reverse engineering (1)

Thursday, 03 December 2009

Page 89: MySQL Schema Maintenance

Reverse engineering (2)

Thursday, 03 December 2009

Page 90: MySQL Schema Maintenance

Reverse engineering (3)

Thursday, 03 December 2009

Page 91: MySQL Schema Maintenance

Reverse engineering (4)

Thursday, 03 December 2009

Page 92: MySQL Schema Maintenance

Reverse engineering (5)

Thursday, 03 December 2009

Page 93: MySQL Schema Maintenance

Reverse engineering (6)

Thursday, 03 December 2009

Page 94: MySQL Schema Maintenance

Reverse engineering (7)

Thursday, 03 December 2009

Page 95: MySQL Schema Maintenance

Reverse engineering (8)

Thursday, 03 December 2009

Page 96: MySQL Schema Maintenance

Reverse engineering (9)

Thursday, 03 December 2009

Page 97: MySQL Schema Maintenance

Reverse engineering (10)

Thursday, 03 December 2009

Page 98: MySQL Schema Maintenance

forward engineering in the old world

mysqldump --host=development -B \

--no-data crosstab > crosstab.sql

mysql --host=stage < crosstab.sql

Thursday, 03 December 2009

Page 99: MySQL Schema Maintenance

Forward engineering (1)

Thursday, 03 December 2009

Page 100: MySQL Schema Maintenance

Forward engineering (2)

Thursday, 03 December 2009

Page 101: MySQL Schema Maintenance

Forward engineering (3)

Thursday, 03 December 2009

Page 102: MySQL Schema Maintenance

Forward engineering (4)

Thursday, 03 December 2009

Page 103: MySQL Schema Maintenance

synchronization in the old world

Thursday, 03 December 2009

Page 104: MySQL Schema Maintenance

Synchronization (1)

Thursday, 03 December 2009

Page 105: MySQL Schema Maintenance

Synchronization (1)

Thursday, 03 December 2009

Page 106: MySQL Schema Maintenance

Synchronization (2)

Thursday, 03 December 2009

Page 107: MySQL Schema Maintenance

Synchronization (3)

Thursday, 03 December 2009

Page 108: MySQL Schema Maintenance

Synchronization (4)

Thursday, 03 December 2009

Page 109: MySQL Schema Maintenance

Synchronization (5)

Thursday, 03 December 2009

Page 110: MySQL Schema Maintenance

Synchronization (6)

Thursday, 03 December 2009

Page 111: MySQL Schema Maintenance

More goodies

• MySQL Workbench 5.2 (Beta) - live demo

Thursday, 03 December 2009

Page 112: MySQL Schema Maintenance

Thanks

Let's talk!

Thursday, 03 December 2009