Attribution:Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
Otherwise noted: Claude Anderson was given permission to modify the slides for CSSE 290 at Rose-Hulman by author Jessica Miller.
The authors' original slides, based on Web Programming Step by Step, can be seen at http://webstepbook.com.
Some of the examples in some days' slides are from David Fisher at Rose-Hulman, who was kind enough to allow me to use them.
My intention is to mark these examples with [DSF].
SHOW DATABASES; SHOW TABLES; DESCRIBE table;
SHOW TABLES; +-----------+ | students | | courses | | grades | | teachers | +-----------+ 4 rows in set
INSERT
statementINSERT INTO table VALUES (value, value, ..., value);
INSERT INTO students VALUES (789, "Nelson", "muntz@fox.com", "haha!");
INSERT
INSERT INTO table (columnName, columnName, ..., columnName) VALUES (value, value, ..., value);
INSERT INTO students (name, email) VALUES ("Lewis", "lewis@fox.com");
REPLACE
statementREPLACE INTO table (columnName, columnName, ..., columnName) VALUES (value, value, ..., value);
REPLACE INTO students VALUES (789, "Martin", "prince@fox.com");
UPDATE
statementUPDATE table SET column = value, ..., column = value WHERE column = value;
UPDATE students SET email = "lisasimpson@gmail.com" WHERE id = 888;
DELETE
statementDELETE FROM table WHERE condition;
DELETE FROM students WHERE id = 888;
CREATE DATABASE name; DROP DATABASE name;
CREATE DATABASE warcraft;
CREATE TABLE name ( columnName type constraints, ... columnName type constraints ); DROP TABLE name;
CREATE TABLE students ( id INTEGER, name VARCHAR(20), email VARCHAR(32), password VARCHAR(16) );
BOOLEAN
: either TRUE
or FALSE
INTEGER
DOUBLE
VARCHAR(length)
: a stringENUM(value, ..., value)
: a fixed set of valuesDATE
, TIME
, DATETIME
BLOB
: binary dataCREATE TABLE students ( id INTEGER UNSIGNED NOT NULL PRIMARY KEY, name VARCHAR(20) NOT NULL, email VARCHAR(32), password VARCHAR(16) NOT NULL DEFAULT "12345" );
NOT NULL
: not allowed to insert a null/empty value in any row for that columnPRIMARY KEY
/ UNIQUE
: no two rows can have the same valueDEFAULT value
: if no value is provided, use the given defaultAUTO_INCREMENT
: default value is the last row's value plus 1 (useful for IDs)UNSIGNED
: don't allow negative numbers (INTEGER
only)ALTER TABLE name RENAME TO newName;
ALTER TABLE students RENAME TO children;
ALTER TABLE name ADD COLUMN columnName type constraints; ALTER TABLE name DROP COLUMN columnName; ALTER TABLE name CHANGE COLUMN oldColumnName newColumnName type constraints;
name | course | teacher | grade | |
---|---|---|---|---|
Bart | bart@fox.com | Computer Science 142 | Krabappel | B- |
Bart | bart@fox.com | Computer Science 143 | Hoover | C |
Milhouse | milhouse@fox.com | Computer Science 142 | Krabappel | B+ |
Lisa | lisa@fox.com | Computer Science 143 | Hoover | A+ |
Lisa | lisa@fox.com | Computer Science 190M | Stepp | A+ |
Ralph | ralph@fox.com | Informatics 100 | Krabappel | D+ |
|
|
|
|
grades
with student_id
of 888 are Lisa's grades)Suppose we want to write a web store like Amazon.com. The store sells products that can be purchased by customers online. The customer can add items to their shopping cart and then order them. The customer can also check the order's status, whether it has shipped, etc.
webster.cs.washington.edu
.
ssh yourUWnetID@webster.cs.washington.edu
Once connected via SSH, type the following command at the prompt:
mysql -u username -p
Type in your MySQL password (should have been emailed to you). The screen will not show anything as you type the password.
Now at the mysql>
prompt, you can type SQL commands. End each with a semicolon ;
.