21MySQL - CS108 Stanford Winter 2012 Handout#21 Young MySQL...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS108, Stanford Handout #21 Winter 2012 Young MySQL This handout briefly describes how to work with Stanford’s MySQL database server directly. Handout #21 will describe how to access the database from Java. Representation of Information Most modern databases are relational databases. In a relational database, information is stored in tables. For example, we can represent information about major metropolitan areas using the following table: +---------------+---------------+------------+ | metropolis | continent | population | +---------------+---------------+------------+ | Mumbai | Asia | 20400000 | | New York | North America | 21295000 | | San Francisco | North America | 5780000 | | London | Europe | 8580000 | | Rome | Europe | 2715000 | | Melbourne | Australia | 3900000 | | San Jose | North America | 7354555 | | Rostov-on-Don | Europe | 1052000 | +---------------+---------------+------------+ Working with MySQL We will be working with Stanford’s MySQL database. At this point you should have received an e-mail containing the following pieces of information: Your MySQL Username Your MySQL Initial Password The name of the Stanford MySQL Database Server Your MySQL Database Name Keep this information handy, you’ll need it as you work with MySQL. Logging In To use MySQL login to a Stanford UNIX computer and enter the following at the command prompt:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 mysql -h mysql-user.stanford.edu -u userID p where you replace userID with your MySQL user ID. The system will prompt you for a password. Note that the User ID and Password for MySQL are not the same as your regular Stanford ID and password (and you should keep it that way for security reasons ). Once you’ve logged in, notify MySQL which database you want to use. With Stanford’s MySQL setup, each user has been assigned a specific database that they can use. This is the database name you received via e-mail along with your username and password. We tell MySQL which database to use like this: mysql> USE database_name ; where database_name is replaced by the database name you have been assigned. Creating a Table We will now create a table and add it to our database. To define a table, we need to give the table a name, and determine what columns will be in the table. Here is an example: 1 mysql> CREATE TABLE metropolises ( -> metropolis CHAR(64), -> continent CHAR(64), -> population BIGINT -> ); We’ve created a table with three columns, a metropolis column composed of 64-character long strings, a continent column with 64-character long strings, and a population column which is a 64-bit integer. Our next step is to populate our table. We do this using INSERT commands: mysql> INSERT INTO metropolises VALUES( -> "Mumbai","Asia",20400000); mysql> INSERT INTO metropolises VALUES( -> "New York","North America",21295000); mysql> INSERT INTO metropolises VALUES( -> "San Francisco","North America",5780000); 1 The “mysql>” is the standard MySQL command prompt. The “->” is the MySQL prompt used to show line
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/12/2012 for the course CS 108 taught by Professor Jimenez during the Winter '08 term at Stanford.

Page1 / 13

21MySQL - CS108 Stanford Winter 2012 Handout#21 Young MySQL...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online