4a1Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
4a. MySQL Object Management
for the Oracle DBA
Ronald BradfordSenior Consultant
MySQL IncJanuary 2008
4a2Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
Agenda
GOAL: Creating and Managing MySQL Objects SQL Object Types Creating Objects Managing Objects MySQL Data Dictionary
4a3Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
SQL Object Types
4a4Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
Object Types User Database (Schema) Tablespace / Data File Table / Column Index View Trigger Stored Procedure Stored Function User Defined Function (UDF)
No SequencesNo SnapshotsNo Synonyms
4a5Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
Users
There is no concept of user ownership within MySQL All objects are part of a database (or 'schema') Users only have object privileges assigned to them
4a6Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
Table / Column
Table Name is case sensitive (by default) Reserved Words are permitted (appropriately quoted)
http://dev.mysql.com/doc/refman/5.1/en/reservedwords.html
4a7Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
Indexes
Default Index type is BTREE Memory has HASH & BTREE Indexes MyISAM has a FULLTEXT Index
4a8Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
Views
Updatable and nonupdatable views supported view and table names must be unique DEFINER clause allows view to run as the calling user,
or another defined user View restrictions
cannot contain a subquery in the FROM clause. cannot refer to system or user variables. cannot refer to prepared statement parameters. Within a stored routine, the definition cannot refer to routine
parameters or local variables.
4a9Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
Views View restrictions (cont)
Any table or view referred to in the definition must exist. However after creation, underlying objects can be dropped or modified. View is not invalidated, but will throw error on usage.
The definition cannot refer to a TEMPORARY table You cannot create a TEMPORARY view. The tables named in the view definition must already exist. You cannot associate a trigger with a view.
View algorithms MERGE TEMPTABLE UNDEFINED
Supports WITH CHECK OPTION Do not support Materialized Views
4a10Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
Triggers
DML triggers only no system triggers [BEFORE | AFTER] [INSERT | UPDATE | DELETE] Only one of each trigger type supported FOR EACH ROW, no FOR EACH STATEMENT No INSTEAD OF No WHEN Condition NEW.col and OLD.col syntax CALL statement for SP not permitted No statements that implicitly or explicitly COMMIT |
ROLLBACK
4a11Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
Stored Procedures
PL/PSM (Persistent Stored Modules) Non scrollable, read only cursors Support for most loop types (FOR loops not yet) DEFINER clause allows stored procedure to run as the
calling user, or another defined user No packages support No anonymous blocks No concept of dependency within MySQL Stored procedures are not required for high
performance within MySQL
4a12Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
User Defined Functions (UDF) Added as Object Files Compiled and available on permanent basis
mysql> create function logger returns integer soname 'syslogudf.so';mysql> select logger('logging from ' + version());
http://dev.mysql.com/doc/refman/5.0/en/addingfunctions.html
4a13Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
MySQL Create Object Limitations
No online ALTER Table No online Add Tablespace Data File
Online Add Index (5.1) Online Add Enum Value (5.1) Online Add Column (5.1 MyISAM)
4a14Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
MySQL Auto Increment
Replacement for SEQUENCE
Limitations Only one per table No System wide concept No Get Next capability Get Value after Insert with LAST_INSERT_ID() No required in INSERT, or NULL can be used No persistence of true max value used across restart
exists in Falcon 6.0
http://dev.mysql.com/doc/refman/5.0/en/exampleautoincrement.html
4a15Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
MySQL Data Dictionary
4a16Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
MySQL Data Dictionary
Two Present Sources Privilege/System Tables 'mysql' database Information Schema
4a17Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
'mysql' database Privileges (host, db ,user, tables_priv, columns_priv,procs_priv) Help (help_category, help_keyword, help_relation, help_topic) Time zones (time_zone, time_zone_name,
time_zone_leap_second, time_zone_transition, time_zone_transition_type)
Procedure & Function (proc, func)
4a18Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
INFORMATION_SCHEMA5.0 SCHEMATA, TABLES, COLUMNS, TABLE_CONSTRAINTS,
VIEWS, STATISTICS USER_PRIVILEGES,
SCHEMA_PRIVILEGES,TABLE_PRIVILIGES, COLUMN_PRIVILEGES
CHARACTER_SETS, COLLATIONS, COLLATION_CHARACTER_SET_APPLICIBILITY
ROUTINES, PROFILING5.1 KEY_COLUMN_USAGE,PLUGINS,ENGINES,PARTITIONS,EVENT
S,FILES, REFERENTIAL_CONSTRAINTS, PROCESSLIST, GLOBAL_STATUS,
SESSION_STATUS,GLOBAL_VARIABLES,SESSION_VARIABLES 6.0 FALCON_
4a19Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
4a20Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
INFORMATION_SCHEMA ExamplesTable Disk Size for given schema
select table_name, table_rows, avg_row_length, data_length/1024/1024 as data_mb, index_length/1024/1024 as index_mb from information_schema.tables where table_schema='dbname'order by 4 desc;
4a21Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
INFORMATION_SCHEMA Examples
SELECT TABLE_SCHEMA, SUM((DATA_LENGTH + INDEX_LENGTH) / (1024 * 1024)) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES
GROUP BY TABLE_SCHEMA ORDER BY SIZE_MB DESC
SELECT ROUTINE_TYPE, ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA='dbname';
SELECT TRIGGER_NAME,EVENT_MANIPULATION,EVENT_OBJECT_TABLE, ACTION_STATEMENT FROM INFORMATION_SCHEMA.TRIGGERS WHERE TRIGGER_SCHEMA='dbname';
SELECT CONCAT('DROP TABLE ',table_name,';')INTO OUTFILE '/sql/drop_tables.sql'FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA =
'dbname';
4a22Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
INFORMATION_SCHEMA Examples
SELECT s.schema_name, CONCAT(IFNULL(ROUND((SUM(t.data_length)+ SUM(t.index_length))/1024/1024,2),0.00),'Mb') total_size, CONCAT(IFNULL(ROUND(((SUM(t.data_length)+SUM(t.index_length))SUM(t.data_free))/1024/1024,2),0.00),'Mb') data_used,
CONCAT(IFNULL(ROUND(SUM(data_free)/1024/1024,2),0.00),'Mb') data_free,
IFNULL(ROUND((((SUM(t.data_length)+SUM(t.index_length)) SUM(t.data_free))/((SUM(t.data_length)+SUM(t.index_length)))*100),2),0) pct_used, COUNT(table_name) total_tables
FROM information_schema.schemata s
LEFT JOIN information_schema.tables t ON s.schema_name = t.table_schema
WHERE s.schema_name != 'information_schema'
GROUP BY s.schema_name ORDER BY pct_used DESC\G
4a23Copyright 2008 MySQL Inc MySQL The Best Online Database for modern applications
4a. MySQL Object ManagementVersion 1.2
Questions?