Presentation is loading. Please wait.

Presentation is loading. Please wait.

New Foreign Keys in 6.1 Konstantin Osipov, Staff Engineer, MYSQL

Similar presentations


Presentation on theme: "New Foreign Keys in 6.1 Konstantin Osipov, Staff Engineer, MYSQL"— Presentation transcript:

1 New Foreign Keys in 6.1 Konstantin Osipov, Staff Engineer, MYSQL
Peter Gulutzan, SQL Architect, MYSQL

2 Introduction and agenda
WL#148 “Foreign keys” (created in 2001) Project objectives: support all storage engines better standard compliance (SQL 2003) emphasis on consistency In this talk Peter and Kostja will: discuss why new foreign keys are needed and how they are better demonstrate live

3 Foreign key features – Standard SQL:2003 (Core)
From WL#148 specification:

4 Terminology T1 is a “child” or “referencing”
InnoDB/MySQL, or stuff in 5.1 is “old style” or “native InnoDB” WL#148, or stuff in my:mysql-6.1-fk: “new style” or “all-engines” T1 is a “child” or “referencing” T2 is a “parent” or “referenced”

5 Demonstration begins... To start MySQL server in new mode:
mysqld \ --foreign-key-all-engines=1

6 No child left behind With –foreign-key-all-engines, ordinarily, MySQL prevents a “child” row from having a value that's not in the “parent” table. The following statements are affected: (assuming CREATE TABLE parent) CREATE TABLE child ... – fails if no parent DROP/ALTER TABLE parent ... – fails if child INSERT INTO child ... – fails if no parent value UPDATE child ... – fails if no parent value UPDATE parent ... – fails if a dangling child value DELETE FROM parent – fails if a dangling child value

7 Foreign key features – referential action
From WL#148 specification:

8 New stuff: ENGINE=Anything
Foreign keys will work with MyISAM, Falcon, Maria and so on... Foreign keys will continue to work with InnoDB, either the old style (using code in the storage engine), or the new style (using code in the server). This can cause complications when you mix engines.

9 New stuff: column referencing
This syntax used to do nothing: CREATE TABLE ... column_name TYPE ... REFERENCES parent_table_name (parent_column_name) Now it works properly.

10 New stuff: statement checks
Consider this foreign key relationship: Now let's update the parent table, UPDATE t1 SET a=CASE WHEN a=1 THEN 3 WHEN a=2 THEN 1;

11 New stuff: recursion T1 can refer to T2 which refers to T3 which refers to T1. Or, more simply, T1 can refer to T1 (“self- reference”).

12 Engine capabilities Problematic example: UPDATE two rows.
First row update succeeds. Second row update fails due to foreign key violation. This works well with InnoDB/Falcon but poorly with MyISAM – one needs to rollback the statement in case of failure. Therefore we disallow foreign key features that could lead to trouble for engines that have no statement rollback.

13 Old style and new style: different behavior
New style is different because: It's more standard. It has to operate on more engines. It can do statement checks. Locking behavior is different. So: Some DDL works differently. Some DML works differently. You have to be aware before upgrading.

14 Old style and new style: FOREIGN KEY clause
Old style: FOREIGN KEY name. New style: CONSTRAINT name. The old style clause is still supported but translated to CONSTRAINT name.

15 Old style and new style: default actions
Old style: assume RESTRICT is the default. New style: assume NO ACTION is the default. It's a change in defaults, you might not notice if you're restoring from a dump. Old style: RESTRICT and NO ACTION are the same. New style: RESTRICT and NO ACTION differ as per SQL standard. The difference is end-of-statement checks.

16 Old style and new style: non-unique parent
Old style: columns in parent table don't have to be unique. New style: parent columns must be unique. So you have to say UNIQUE or PRIMARY KEY when you define the parent table.

17 Old style and new style: REFERENCES privilege
Old style: REFERENCES privilege is not checked. New style: well, um, privilege still not checked. However, WL#4099 "REFERENCES privilege" is part of the task.

18 Old style and new style: column definition
Old style: parent/child column types and lengths can differ. New style: parent/child column types and lengths can not differ.

19 Old style and new style: cascades activate triggers
Old style: ON [UPDATE|DELETE] [CASCADE|SET NULL| SET DEFAULT] doesn't activate triggers. New style: triggers are activated.

20 Old style and new style: change-distinct behavior
Old style: changing 'a' to 'A' can cause cascading. New style: changing 'a' to 'A' can not cause cascading.

21 Take your time, the old style remains supported.
Upgrading Learn all the differences Take a backup Start the server with –foreign-key-all- engines Today: dump and restore. In future: run mysql_upgrade Take your time, the old style remains supported.

22 Optional foreign key features
From WL#148 specification:

23 Links WL#148 "Foreign keys" task description (MySQL Forge)
WL#4099 "REFERENCES privilege" (MySQL Forge) Foreign key bugs (bugs.mysql.com) MySQL + Foreign keys source available at Launchpad These slides, available at Peter Gulutzan's blog

24 The end – thank you! Surely there are questions?
“Konstantin and Peter” show will occur again tomorrow, we'll be demonstrating many 6.0 and 6.1 features. Where: MySQL Camp, Bayshore room When: Thursday 11:55 am – 12:35 pm Title: Test drive MySQL 6.1


Download ppt "New Foreign Keys in 6.1 Konstantin Osipov, Staff Engineer, MYSQL"

Similar presentations


Ads by Google