Difference between revisions of "Exploring the database"

From Dreamwidth Notes
Jump to: navigation, search
(first version)
 
Line 1: Line 1:
Sign into your development environment with SSH.
+
This guide will help guide you in poking around your Dreamwidth installation's database. This could help you during coding! 
  
 
== Signing into MySQL ==
 
== Signing into MySQL ==
 +
 +
Sign into your development environment with SSH.
  
 
You can find the log in credentials you need in the <code>$LJHOME/etc/config-private.pl</code>:
 
You can find the log in credentials you need in the <code>$LJHOME/etc/config-private.pl</code>:
Line 56: Line 58:
 
* Default -- the default value for the column, if none is specified
 
* Default -- the default value for the column, if none is specified
 
* Extra -- if <code>auto_increment</code> is here, that means that every new row to that table automatically gets a higher value than the last row added.
 
* Extra -- if <code>auto_increment</code> is here, that means that every new row to that table automatically gets a higher value than the last row added.
 +
 +
== Viewing data ==
 +
 +
The most basic way of viewing is this command, but only run it if you have a few users on your system:
 +
 +
mysql> SELECT * FROM user;
 +
 +
You can show only the columns you're interested in like this:
 +
 +
mysql> SELECT <strong>userid,user,name</strong> FROM user;
 +
 +
And even limit the number of rows you get back:
 +
 +
mysql> SELECT userid,user,name FROM user <strong>LIMIT 5</strong>;
 +
 +
You can even search for a row matching specific requirements:
 +
 +
mysql> SELECT userid,user,name FROM user <strong>WHERE user='foxfirefey'</strong>;
 +
 +
== Changing data ==
 +
 +
See [http://dev.mysql.com/doc/refman/5.0/en/update.html UPDATE].
 +
 +
mysql> SELECT status FROM user WHERE userid=1;
 +
mysql> UPDATE user SET status='A' WHERE userid=1;
 +
mysql> SELECT status FROM user WHERE userid=1;
 +
 +
mysql> SELECT userid,user,email,status FROM user;
 +
mysql> UPDATE user SET status='A' WHERE status='N';
 +
mysql> SELECT userid,user,email,status FROM user;
 +
 +
== Adding data ==
 +
 +
See [http://dev.mysql.com/doc/refman/5.0/en/insert.html INSERT].
 +
 +
== Important information locations ==
 +
 +
=== Account information ===
 +
 +
* <code>user</code>
 +
 +
=== Entry information ===
 +
 +
* <code>log2</code>
 +
 +
=== Comments ===
 +
 +
* <code>talk2</code>
 +
 +
=== Invites ===
 +
 +
* <code>acctcode</code>
  
 
[[Category: Development]]
 
[[Category: Development]]

Revision as of 23:23, 6 January 2010

This guide will help guide you in poking around your Dreamwidth installation's database. This could help you during coding!

Signing into MySQL

Sign into your development environment with SSH.

You can find the log in credentials you need in the $LJHOME/etc/config-private.pl:

'user' => 'dh_foxfirefey',
'pass' => 'XXXXXXXXXXX',    # CHANGETHIS
'dbname' => 'dreamhack_foxfirefey',

Replace the items in brackets with the values assigned above:

mysql -u <user> -p <pass> <dbname>

For example:

mysql -u dh_foxfirefey -p dreamhack_foxfirefey

And enter the password when prompted. You will then see information like this before it gives you a prompt:

Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12284107
<strong>Server version: 5.0.51a-3ubuntu5.4 (Ubuntu)</strong>

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

The emphasized portion lets you know what version of MySQL you are using, in case you have to look things up in the MySQL documentation.

Basic Database Navigation

Show all tables

Data in the database is stored in a collection of tables, each of which is like a spreadsheet with rows containing a certain amount of columns. To present a list of all the tables in the database, use this command:

mysql> SHOW TABLES;

Show all columns in a table

This command will show you a list of all of the columns in a table, which is all of the pieces of data a row has.

mysql> SHOW COLUMNS FROM <tablename>;

For example:

mysql> SHOW COLUMNS FROM user;

The list will have a table of information about each column (or field):

  • Field -- the name of the column
  • Type -- the type of data the column stores. You can read more about it at MySQL's documentation.
  • Null -- whether or not NULL (the absence of a value) is allowed for this column
  • Key -- A table's key determines how you can look up data in the database. PRI here means that is the tables primary key. UNI means that bit of data will be unique for that row--that is, if one row has "moo", another row cannot have the value "moo" in that table.
  • Default -- the default value for the column, if none is specified
  • Extra -- if auto_increment is here, that means that every new row to that table automatically gets a higher value than the last row added.

Viewing data

The most basic way of viewing is this command, but only run it if you have a few users on your system:

mysql> SELECT * FROM user;

You can show only the columns you're interested in like this:

mysql> SELECT userid,user,name FROM user;

And even limit the number of rows you get back:

mysql> SELECT userid,user,name FROM user LIMIT 5;

You can even search for a row matching specific requirements:

mysql> SELECT userid,user,name FROM user WHERE user='foxfirefey';

Changing data

See UPDATE.

mysql> SELECT status FROM user WHERE userid=1;
mysql> UPDATE user SET status='A' WHERE userid=1;
mysql> SELECT status FROM user WHERE userid=1;
mysql> SELECT userid,user,email,status FROM user;
mysql> UPDATE user SET status='A' WHERE status='N';
mysql> SELECT userid,user,email,status FROM user;

Adding data

See INSERT.

Important information locations

Account information

  • user

Entry information

  • log2

Comments

  • talk2

Invites

  • acctcode