|
Managing a MySQL Database
Configuration
How do you configure phpMyAdmin?
Create a directory "phpMyAdmin/config", copy config.inc.php into it, then run "phpMyAdmin/setup/" from the browser
How do you make configuration changes permanent?
When using "setup", save the changes, and then move config.inc.php from the phpMyAdmin/config/ folder back to the phpMyAdmin/ folder. Warning for Mac users: PHP does not seem to like Mac end of lines character ("\r"). So ensure you choose the option that allows to use the *nix end of line character ("\n") in your text editor before saving a script you have modified.
Why would you want to configure phpMyAdmin?
For example, you might want to switch to the "mysqli" extension, which gives you added features when using phpMyAdmin. You might also consider changing the "collation" settings to "utf-8"
How do you configure MySQL?
Use phpMyAdmin "status" to get advice on current settings. Click on the links for the documentation associated with settings you are considering changing. Modify the "my.ini" file (use the XAMPP panel). Restart MySQL.
Why would you want to change the MySQL configuration?
For the most part the settings are installed to be system specific and usually do not need to be changed. You may notice sluggish performance, however, and find that based on the usage on your server you need to make minor changes to improve performance.
Creating a database
What exactly is a database?
A database is a collection of tables, indexes and links (relations) stored on the server in a manner dictated by the storage engine
What is meant by "collation"?
"collation" refers to how text data is physically stored. Originally ASCII was used, however ASCII due to its single byte nature, is unable to cope with international character sets.
What is the best "collation" to use?
You should use the language version which matches what your website users will be using. utf8_* is recommended for a best match with PHP, however there are caveats. See: http://stackoverflow.com/questions/367711/what-is-the-best-collation-to-use-for-mysql-with-php. See also: http://code.openark.org/blog/mysql/mysqls-character-sets-and-collations-demystified
What is meant by privileges?
Database privileges involve creating users and assigning rights to one or more databases and/or tables
How do you create a database and privileges using phpMyAdmin?
Select "Database" and then "Create", then select "Privileges" and then "Add User". You can also add a user and create a database of the same name at the same time.
Establishing Relationships and Constraints
Why would you want to establish a relationship in MySQL?
Constraints are a built-in means of maintaining database integrity. In order to enforce a constraint, you would need to establish a relationship.
What is an "orphaned" row and what is the danger?
An "orphaned row" is where the parent table row has been deleted, but the child table rows are still in existence
What should you do before setting relationships?
1. The database storage engine must be set to "InnoDB" 2. Create either a Primary or Unique index on the column(s) in the parent table. 3. Both sets of keys must have the same data types and length.
What are the options for a relationship?
On update or on delete: cascade, set null, no action, and restrict
How do you go about setting up a relationship using phpMyAdmin?
Select the database (use breadcrumbs at top), then "Designer", then click on the icon for "Create Relation", select the parent table, and then the "foreign key" in the child table. You can also select the child table, "Structure", "Relation View", and create a link back to the parent
How do you change or delete a relationship?
Select the database, then "Designer". Locate relationship link on the child table and click. You can delete and then re-add the relationship. Alternatively, select the child table, "Structure", and then click on "Relation View", which lets you edit directly.
LAB: Defining Tables and Relationships for a Website
What is the purpose of the lab?
In this lab you will be defining a series of tables and relationships which will be used in later labs
Create a database "sweetscomplete"
- From the browser go to http://localhost/phpmyadmin
- Select "users" - "Add User"
- Create a user "sweetscomplete", use "localhost" and enter a password
- Select "Create database with same name ... "
- Click on "Add User"
- Select the database and set the collation to utf8_general_ci
- Make sure the "Engine" is set to "Innodb"
Create a table "products" ...
Name | Type | Size | Null | And ... |
---|
product_id | int | 8 | N | Primary Key, Auto Increment | sku | char | 16 | Y | | title | varchar | 128 | N | Index | description | varchar | 4096 | Y | | price | decimal | 6,2 | N | | special | int | 1 | N | default: as defined: 0 | link | varchar | 128 | Y | |
Create a table "purchases" ...
Name | Type | Size | Null | And ... |
---|
purchase_id | int | 8 | N | Primary Key, Auto Increment | transaction | char | 8 | N | | product_id | int | 8 | N | Index | user_id | int | 8 | Y | | date | timestamp | | N | | quantity | int | 6 | N | | sale_price | decimal | 8,2 | N | |
Import data ....
- Select the "products" table - "Import" - "Choose File"
- From the WorkingFiles folder for this chapter select mysql_project_products.csv
- Make sure "Format" indicates "CSV" and click "Go"
- Select the "purchases" table - "Import" - "Choose File"
- From the WorkingFiles folder for this chapter select mysql_project_purchases.csv
- Make sure "Format" indicates "CSV" and click "Go"
Set up a relation ...
- Select "Purchases" - "Structure" and click on "Relation View"
- Next to "product_id" click "Foreign Key Constraint"
- Select sweetscomplete.products.product_id
- ON DELETE: CASCADE | ON UPDATE: CASCADE
- Click "Save"
- Test by inserting a couple of rows and then trying to delete
Verify results ...
Browse the "products" and "purchases" tables. If you do not see a list of products, review the preceding procedures
|
|