Cách xem relationship trong phpmyadmin

Bật chế độ xem quan hệ trong phpMyAdmin / MAMP

Nếu bạn đang sử dụng MAMP cho các dự án điều khiển cơ sở dữ liệu của mình, bạn có thể sẽ sử dụng phpMyAdmin để quản trị cơ sở dữ liệu MySQL của mình nếu bạn quyết định đi theo lộ trình đó. Nếu bạn đang tạo một cơ sở dữ liệu, bạn có thể tự hỏi làm thế nào để tạo mối quan hệ và tìm khóa cho các bảng của mình.

Trước tiên, bạn cần kiểm tra xem bạn có quyền truy cập vào dạng xem Quan hệ hay không. Để thực hiện việc này, hãy mở phpMyAdmin và chọn một cơ sở dữ liệu. Bạn cần đảm bảo rằng công cụ lưu trữ của bảng được thiết lập để sử dụng InnoDB. Bấm vào một bảng trong cơ sở dữ liệu của bạn và chọn tab Hoạt động. Đảm bảo rằng công cụ lưu trữ được đặt để sử dụng InnoDB và lưu các thay đổi của bạn.

Bây giờ, quay lại chế độ xem bảng của bạn và nhấp vào tab Cấu trúc. Tùy thuộc vào phiên bản phpMyAdmin của bạn, bạn sẽ thấy một liên kết có tiêu đề Chế độ xem quan hệ bên dưới cấu trúc bảng. Nếu bạn có thể thấy nó, bạn tốt để đi. Nếu không thể, bạn cần làm theo các bước bên dưới để đặt phpMyAdmin để bật chế độ xem Quan hệ.

  1. Tìm thấy /Applications/MAMP/bin/phpMyAdmin/scripts/create_tables.sql
  2. Tôi để tệp này mặc định nhưng bạn có thể thay đổi tên bảng thành bất kỳ tên nào bạn muốn. Tôi đã để lại phpMyAdmin của mình
  3. Mở phpMyAdmin và chuyển đến tab Nhập .
  4. Nhấp vào nút duyệt và tìm tệp create_tables.sql, sau đó nhấp vào Bắt đầu .
  5. Các bảng cần thiết cho dạng xem Quan hệ sẽ được thêm vào cơ sở dữ liệu bạn đã chỉ định.
  6. Mở /Applications/MAMP/bin/phpMyAdmin/config.inc.php
  7. Tìm khối mã cấu hình (các) Máy chủ và thay thế / bỏ ghi chú mã sau và điền vào các giá trị. Nếu bạn để mọi thứ mặc định trong tệp create_tables.sql thì bạn chỉ cần cắt và dán các dòng bên dưới.

    $cfg['Servers'][$i]['pmadb'] = 'phpmyadmin';
    $cfg['Servers'][$i]['bookmarktable'] = 'pma_bookmark';
    $cfg['Servers'][$i]['relation'] = 'pma_relation';
    $cfg['Servers'][$i]['table_info'] = 'pma_table_info';
    $cfg['Servers'][$i]['table_coords'] = 'pma_table_coords';
    $cfg['Servers'][$i]['pdf_pages'] = 'pma_pdf_pages';
    $cfg['Servers'][$i]['column_info'] = 'pma_column_info';
    $cfg['Servers'][$i]['history'] = 'pma_history';
    
  8. Lưu tệp và khởi động lại MAMP và làm mới bảng điều khiển phpMyAdmin của bạn.

  9. Đi tới cơ sở dữ liệu của bạn và xem một trong các bảng của bạn ở chế độ Cấu trúc. Bây giờ bạn sẽ thấy liên kết Chế độ xem quan hệ.

Nguồn: http://newvibes.com/blog/enabling-relation-view-in-phpmyadmin-mamp/

8 hữu ích 1 bình luận chia sẻ

phpMyAdmin

phpMyAdmin allows relationships (similar to foreign keys) using MySQL-native (InnoDB) methods when available and falling back on special phpMyAdmin-only features when needed. There are two ways of editing these relations, with the relation view and the drag-and-drop designer – both of which are explained on this page.

Technical info¶

Currently the only MySQL table type that natively supports relationships is InnoDB. When using an InnoDB table, phpMyAdmin will create real InnoDB relations which will be enforced by MySQL no matter which application accesses the database. In the case of any other table type, phpMyAdmin enforces the relations internally and those relations are not applied to any other application.

Relation view¶

In order to get it working, you first have to properly create the [[pmadb|pmadb]]. Once that is setup, select a table’s “Structure” page. Below the table definition, a link called “Relation view” is shown. If you click that link, a page will be shown that offers you to create a link to another table for any (most) fields. Only PRIMARY KEYS are shown there, so if the field you are referring to is not shown, you most likely are doing something wrong. The drop-down at the bottom is the field which will be used as the name for a record.

Relation view example¶

Cách xem relationship trong phpmyadmin
Cách xem relationship trong phpmyadmin

Let’s say you have categories and links and one category can contain several links. Your table structure would be something like this:

  • category.category_id (must be unique)
  • category.name
  • link.link_id
  • link.category_id
  • link.uri.

Open the relation view (below the table structure) page for the link table and for category_id field, you select category.category_id as master record.

If you now browse the link table, the category_id field will be a clickable hyperlink to the proper category record. But all you see is just the category_id, not the name of the category.

Cách xem relationship trong phpmyadmin

To fix this, open the relation view of the category table and in the drop down at the bottom, select “name”. If you now browse the link table again and hover the mouse over the category_id hyperlink, the value from the related category will be shown as tooltip.

Cách xem relationship trong phpmyadmin

Designer¶

The Designer feature is a graphical way of creating, editing, and displaying phpMyAdmin relations. These relations are compatible with those created in phpMyAdmin’s relation view.

To use this feature, you need a properly configured phpMyAdmin configuration storage and must have the $cfg['Servers'][$i]['table_coords'] configured.

To use the designer, select a database’s structure page, then look for the Designer tab.

To export the view into PDF, you have to create PDF pages first. The Designer creates the layout, how the tables shall be displayed. To finally export the view, you have to create this with a PDF page and select your layout, which you have created with the designer.