Skip to content

Instantly share code, notes, and snippets.

@yang-wei
Last active September 25, 2016 13:56
Show Gist options
  • Save yang-wei/429fb82ab83043255cc1 to your computer and use it in GitHub Desktop.
Save yang-wei/429fb82ab83043255cc1 to your computer and use it in GitHub Desktop.
Thing I learned from refactoring MySQL database

Execute mysql command from CLI

mysql -u user -p database_name -e "SELECT * FROM users"

Hate to fill in password each time?

Use the .my.cnf. If you don't have create it vi ~/.my.cnf

[client]
user=your_user
password=your_password

Now we can execute our sql with shorter code.

mysql database_name -e "SELECT * FROM users"

Hooray !!!

What happen when the foreign key is being update or delete ?

I think it's important to design your database at your early stage of development because you cannot alter everything of database. As a MySQL beginner, I never pay attention to the ON UPDATE or ON DELETE clause when creating table.

Let's say we have users and items table where its relationship is one user has many items. So we will create a foreign key owner_id in items table which refer to the users table like this.

CREATE TABLE IF NOT EXISTS `items` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `owner_id` INT UNSIGNED,
  `title` VARCHAR(255) CHARACTER SET NOT NULL,
  FOREIGN KEY(owner_id) REFERENCES users(id),
  PRIMARY KEY (`id`) );

Everything is ok. Well well well you code your app, user can add item, edit it, remove it and see it. But what happen when a user created a lot of items, then when she decide to delete her account? Should we remove the items she created? Or we want to remove it together? It really depends on how your application works but the code above won't work.

So why? Because when we set the build the relationship between users and items using foreign key we must also think about what if the user id is being updated or the user is removed from database? MySQL let us set what to do via the value of ON UPDATE and ON DELETE. By default, ON UPDATE and ON DELETE is set to NO ACTION.

TYPE DESCRIPTION
NO ACTION / RESTRICT This is the default value. In our example user delete won't work
SET NULL When user is removed(or update), the column of owner_id is set to null
CASCADE When user is removed(or update), items belonged to her will be remove(or update)

Also make sure foreign key has the same column characteristic(type, size, nullable) with it's references.

Write comment for your sql

If you write your mysql in file instead of type it in the database CLI, writing comment will be helpful. In real life, apps are large and just like code, we don't always remember or know what the sql is doing. Comment can help. We can write comment in sql by various types.

-- comment
# comment
/* comment */

Tricks for mysqldump

In some time we need to backup our database and mysqldump is really handy for this.

mysqldump -u your_user -p your_database > file_name.sql

Yup ! We can now save this to some local file. This is very cool. What if we just want to save data instead of table strcuture.

mysqldump -u your_user -p your_database --no-create-info > file_name.sql

Now we have only data =). We can also choose to ignore some tables data.

mysqldump -u your_user -p your_db --no-create-info --ignore-table=your_db.users --ignore-table=your_db.users > file_name.sql

If you want to include date in your file name:

// assumed that we set our user and password in .my.cnf
mysqldump db_name > file_name_`date "+%Y%m%d_%H%M%S"`.sql

Here we have a file looks something like file_name_20151111_082826.sql

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment