PROJECT Note # 01: Database Management Systems” by Hanh...

12
PROJECT Note # 01: “Database Management Systems” by Hanh Pham GOALS As data is produced and used everywhere, knowing how to work with a DBMS (Database Management Systems) and manage its data becomes an important skill which gives you advantages in today very competitive job market, whether you are an engineer or a businessman. In this project students will learn: how to work with MySQL which is a DBMS basic SQL statements to manage (create, insert, delete, and update) data MySQL We may have many different DBMS (Oracle, Microsoft SQL Server, IBM DB2, …) on the market. However, they all are based on the same principles. Therefore, if you know well how to use one DBMS then you’ll be able to switch to another DBMS without much trouble. We choose MySQL for doing projects in this course because it is free and yet powerful enough. MySQL is ranked #2 among DBMS and has a market share of 50% (http://db-engines.com/en/ranking). HOW Can I Have Access To MySQL ? Think of MySQL ( or any DBMS) as an email server that if you need to see your emails you’ll have to have an account and login using an username and a password. There are two ways: 1) Use your MySQL account at our CS Linux system (strongly recommended) OR 2) Download and install MySQL server on your own computer (can’t do project P3 which is optional) A) Use MySQL server at our CS Linux system: The MySQL server (software) runs on a CS Linux computer (host) named wyvern.cs.newpaltz.edu. Your MySQL account will be created for you after the class starts. The username for logging into the MySQL server is the same as the campus username. The password is DIFFERENT from your SUNY New Paltz password which you use to login to Blackboard or my.newpaltz.edu. The password will be emailed to you in the beginning of the semester. In order to get into your MySQL account you must login to our CS Linux system first using a SSH program for remote login. Here are different scenarios depending on which kind of computer you use: a. If you use a Windows computer: You can use putty. First, google “putty download” to get to a website that let you download it. You’ll get only 1 file named putty.exe, you may want to put it on the desktop or a different folder rather than in the download folder. Then, double click on it to run the putty program. b. If you use a Linux machine: open a terminal window, then type “ssh [email protected]c. If you use a MAC xOS (Aplle) machine: find ‘Terminal.app’ and double-click on it. Then type “ssh [email protected]

Transcript of PROJECT Note # 01: Database Management Systems” by Hanh...

Page 1: PROJECT Note # 01: Database Management Systems” by Hanh …cs.newpaltz.edu/~phamh/sdb/dbs/sdb-ProjectNote01.pdf · PROJECT Note # 01: “Database Management Systems” by Hanh Pham

PROJECT Note # 01: “Database Management Systems” by Hanh Pham

GOALS

As data is produced and used everywhere, knowing how to work with a DBMS (Database Management Systems) and manage its data becomes an important skill which gives you advantages in today very competitive job market, whether you are an engineer or a businessman. In this project students will learn:

• how to work with MySQL which is a DBMS

• basic SQL statements to manage (create, insert, delete, and update) data

MySQL We may have many different DBMS (Oracle, Microsoft SQL Server, IBM DB2, …) on the market. However, they all are based on the same principles. Therefore, if you know well how to use one DBMS then you’ll be able to switch to another DBMS without much trouble. We choose MySQL for doing projects in this course because it is free and yet powerful enough. MySQL is ranked #2 among DBMS and has a market share of 50% (http://db-engines.com/en/ranking). HOW Can I Have Access To MySQL ? Think of MySQL ( or any DBMS) as an email server that if you need to see your emails you’ll have to have an account and login using an username and a password. There are two ways:

1) Use your MySQL account at our CS Linux system (strongly recommended) OR 2) Download and install MySQL server on your own computer (can’t do project P3 which is optional)

A) Use MySQL server at our CS Linux system: The MySQL server (software) runs on a CS Linux computer (host) named wyvern.cs.newpaltz.edu. Your MySQL account will be created for you after the class starts.

• The username for logging into the MySQL server is the same as the campus username.

• The password is DIFFERENT from your SUNY New Paltz password which you use to login to Blackboard or my.newpaltz.edu. The password will be emailed to you in the beginning of the semester.

In order to get into your MySQL account you must login to our CS Linux system first using a SSH program for remote login. Here are different scenarios depending on which kind of computer you use:

a. If you use a Windows computer: You can use putty. First, google “putty download” to get to a website that let you download it. You’ll get only 1 file named putty.exe, you may want to put it on the desktop or a different folder rather than in the download folder. Then, double click on it to run the putty program.

b. If you use a Linux machine: open a terminal window, then type “ssh [email protected]

c. If you use a MAC xOS (Aplle) machine: find ‘Terminal.app’ and double-click on it. Then type “ssh [email protected]

Page 2: PROJECT Note # 01: Database Management Systems” by Hanh …cs.newpaltz.edu/~phamh/sdb/dbs/sdb-ProjectNote01.pdf · PROJECT Note # 01: “Database Management Systems” by Hanh Pham

B) Install your own MySQL server on your computer: You can download the MySQL server software (free) and install it on your own computer. http://dev.mysql.com/downloads/mysql/ After that, you can create an account for yourself and use it for the projects.

INSTRUCTIONS to use MySQL at CS linux lab: 1). Login to your CS Linux account/system:

• Locally (if you are in a Linux lab) OR

• Remotely (via Putty/SSH program from your computer): Host names:

shell01.acs.newpaltz.edu shell02.acs.newpaltz.edu shell03.acs.newpaltz.edu shell04.acs.newpaltz.edu shell05.acs.newpaltz.edu wyvern.cs.newpaltz.edu (server)

After logging in, you are in your CS Linux account. If you use hostname shell01.acs.newpaltz.edu and your username is smitha you’ll see this:

[smitha@shell01 ~]$

2). LOGIN to your MySQL account: If you are logged in to CS Linux system via wyvern.cs.newpaltz.edu, the command is: mysql -h localhost –p If you are logged in to CS Linux system but NOT via wyvern.cs.newpaltz.edu, the command is: mysql -h wyvern -p you’ll see this:

[smitha@shell01 ~]$ mysql -h wyvern -p Enter password:

then, enter your password, you’ll see:

mysql>

If you like to change your MySQL password then type this: set password = password( 'newpass' );

Page 3: PROJECT Note # 01: Database Management Systems” by Hanh …cs.newpaltz.edu/~phamh/sdb/dbs/sdb-ProjectNote01.pdf · PROJECT Note # 01: “Database Management Systems” by Hanh Pham

Your ROLE in this project and SQL

SQL is a language for “talking” with a DBMS. Each DBMS has its own version of SQL. If you think of SQL as English language then Oracle DBMS can be USA where we use American English, IBM DB2 can be UK where they use Bristish English, etc. There are different ways or different kinds of user interface via which users can interact with a DBMS:

a) via command-line SQL statements (typing SQL commands, users must know SQL) b) visual tools provided by the DBMS (select or click on SQL commands, users must know SQL) c) database applications (fill-in form, users have no knowledge of SQL) d) web-based database applications (fill-in form, users have no knowledge of SQL)

If you ever purchased something on the Internet and had to fill-in a payment form or when you looked at the schedule of classes for Fall 2014 you must have interacted with a DBMS via (d). When you write a program which connects to a database you interact with a DBMS via (c). If you ever used Microsoft Access or MySQL workbench you interacted with a DBMS via (b). In this project we will learn how to do (a). You will play the role of a user who knows SQL and can interact directly with the DBMS (MySQL server) through command-line SQL statements.

BASIC MySQL commands: A good list with explanations of main SQL commands is at: http://www.pantz.org/software/mysql/ After you login to a MySQL server through your SQL account, you’ll see a terminal window with a prompt “mysql> ” like this:

mysql>

Page 4: PROJECT Note # 01: Database Management Systems” by Hanh …cs.newpaltz.edu/~phamh/sdb/dbs/sdb-ProjectNote01.pdf · PROJECT Note # 01: “Database Management Systems” by Hanh Pham

Now, you can “tell” the DBMS what you want it to do using SQL command/statements. STEP-0: CREATE a new database If you’d like to create a new database named “FirstDB” then type:

mysql> CREATE DATABASE FirstDB;

ATTENTION:

• This SQL statement will fail if you use our MySQL server at the CS Linux because your CS MySQL account doesn’t have permission to create a new database. They should have created for you a database called “YourUseName_db”.

• However, if you have installed your own MySQL on your computer then you should be able to do this.

STEP-1: SHOW what databases you have now in a DBMS If you think of a DBMS as an email server then each database is like a folder of related emails. If you’d like to see how many databases you have then you need to type this SQL command/statement:

mysql> SHOW DATABASES;

It should show you all databases you already have:

This is called a screenshot. To take a picture/screenshot of a window in your computer push down two keyboard buttons ALT and PRT-SCR at the same time. STEP-2: USE (choose) a database to work with:

mysql> USE YourUseName_db;

Page 5: PROJECT Note # 01: Database Management Systems” by Hanh …cs.newpaltz.edu/~phamh/sdb/dbs/sdb-ProjectNote01.pdf · PROJECT Note # 01: “Database Management Systems” by Hanh Pham

STEP-3: CREATE a new table. Let’s say a small company, which sells notebooks to schools, hires you to manage their database. They ask you to maintain data about the price and dealer for each kind of notebooks (articles). First, you will need to create a table named shop which has the following contents:

TABLE shop

article dealer price

0001 A 3.45

0001 B 3.99

0002 A 10.99

0003 B 1.45

0003 C 1.69

0003 D 1.25

0004 D 19.95

You’ll need to use SQL statements to define the table shop and then insert/enter the data as following:

mysql> CREATE TABLE shop (

article INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL,

dealer CHAR(20) DEFAULT '' NOT NULL,

price DOUBLE(16,2) DEFAULT '0.00' NOT NULL,

PRIMARY KEY(article, dealer));

INSERT INTO shop VALUES

(1,'A',3.45),(1,'B',3.99),(2,'A',10.99),(3,'B',1.45),

(3,'C',1.69),(3,'D',1.25),(4,'D',19.95);

STEP-4: SEE the contents of a table using SELECT:

mysql> SELECT * FROM shop;

THEN, you’ll see this:

mysql> SELECT * FROM shop; +---------+--------+-------+

| article | dealer | price |

+---------+--------+-------+

| 0001 | A | 3.45 |

| 0001 | B | 3.99 |

Page 6: PROJECT Note # 01: Database Management Systems” by Hanh …cs.newpaltz.edu/~phamh/sdb/dbs/sdb-ProjectNote01.pdf · PROJECT Note # 01: “Database Management Systems” by Hanh Pham

| 0002 | A | 10.99 |

| 0003 | B | 1.45 |

| 0003 | C | 1.69 |

| 0003 | D | 1.25 |

| 0004 | D | 19.95 |

+---------+--------+-------+

7 rows in set (0.00 sec)

The SCREENSHOT for it would look like this:

STEP-5: INSERT (add) a new ROW/RECORD into a table: SYNTAX/RULE/FORMAT: INSERT INTO "tablename"

(first_column,...last_column)

VALUES (first_value ,...last_value);

In the example below, the column name article will match up with the value 5, and the column name dealer will match up with the value 'A'.

Example:

INSERT INTO shop

(article,dealer,price)

VALUES (5 ,'A' ,5.27);

So, if you’ll type this SQL command you’ll get this;

Page 7: PROJECT Note # 01: Database Management Systems” by Hanh …cs.newpaltz.edu/~phamh/sdb/dbs/sdb-ProjectNote01.pdf · PROJECT Note # 01: “Database Management Systems” by Hanh Pham

STEP-6: DELETE (remove) a ROW/RECORD from a table: SYNTAX/RULE/FORMAT:

DELETE FROM "tablename"

WHERE "columnname"

OPERATOR "value"

[and|or "column"

OPERATOR "value"];

[ ] = optional

Examples:

DELETE FROM shop

WHERE dealer = 'B';

STEP-7: UPDATE (change VALUES of) a whole ROW/RECORD in a table: SYNTAX/RULE/FORMAT:

UPDATE "tablename"

SET "columnname" = "newvalue"

[,"nextcolumn" = "newvalue2"...]

WHERE "columnname"

OPERATOR "value"

[and|or "column"

OPERATOR "value"];

Page 8: PROJECT Note # 01: Database Management Systems” by Hanh …cs.newpaltz.edu/~phamh/sdb/dbs/sdb-ProjectNote01.pdf · PROJECT Note # 01: “Database Management Systems” by Hanh Pham

[ ] = optional

Examples:

UPDATE shop

SET price = 8.23

WHERE dealer = 'C';

STEP-8: ADD a new COLUMN into a table SYNTAX/RULE/FORMAT:

ALTER TABLE table_name

ADD column_name datatype;

Example:

ALTER TABLE shop

ADD year INT(4);

After that the table shop will look like this:

Page 9: PROJECT Note # 01: Database Management Systems” by Hanh …cs.newpaltz.edu/~phamh/sdb/dbs/sdb-ProjectNote01.pdf · PROJECT Note # 01: “Database Management Systems” by Hanh Pham

STEP-9: UPDATE (change VALUES of) a whole COLUMN in a table: SYNTAX/RULE/FORMAT: UPDATE [LOW_PRIORITY] [IGNORE] table_name

SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...

[WHERE where_condition]

[ORDER BY ...]

[LIMIT row_count]

Example:

UPDATE shop

SET year=2000;

After that the table shop will look like this:

Page 10: PROJECT Note # 01: Database Management Systems” by Hanh …cs.newpaltz.edu/~phamh/sdb/dbs/sdb-ProjectNote01.pdf · PROJECT Note # 01: “Database Management Systems” by Hanh Pham

STEP-10: DELETE a COLUMN in a table: SYNTAX/RULE/FORMAT:

ALTER TABLE table_name

DROP COLUMN column_name;

Example:

ALTER TABLE shop

DROP COLUMN year;

After that the table shop will look like this:

Page 11: PROJECT Note # 01: Database Management Systems” by Hanh …cs.newpaltz.edu/~phamh/sdb/dbs/sdb-ProjectNote01.pdf · PROJECT Note # 01: “Database Management Systems” by Hanh Pham

Project 01’s ASSIGNMENTS STEP A: Design/Create a data table. Give it a name, for example “MyStore”. This table must have 7 rows and 3 columns whose data types are shown below:

ATTENTION: students must have their own name of table, their own names of columns and make their own data to fill into that table (all these names must be different from the names used in the instructions and examples). The FORMAT of this table is:

• The FIRST column (data type) should be unsigned integers with 3 digits (filled with 0 when needed), can’t have NULL value, default should be '000'.

• The SECOND column (data type) should be strings with maximally 10 characters. It can’t have NULL value, default should be ''.

• The THIRD column (data type) should be (floating decimal point) doubles with maximally 6 digits in total and maximally 1 digits to the right of the decimal point (after the dot). It can’t have NULL value, default should be '0.0'.

• The PRIMARY KEY should be composite: the first and second columns. This format will define your SQL statements for creating the table (step-3). You should compare this format with the table and SQL statements in the example in step-3 above to see how you should modify and reuse those SQL statements in step-3. For EXAMPLE (only for example, you should NOT have this exact name/data in your project):

Page 12: PROJECT Note # 01: Database Management Systems” by Hanh …cs.newpaltz.edu/~phamh/sdb/dbs/sdb-ProjectNote01.pdf · PROJECT Note # 01: “Database Management Systems” by Hanh Pham

TABLE MyStore

article dealer price

001 A 3.4

001 B 3.9

002 A 10.7

003 B 1.5

003 C 1.6

003 D 1.5

004 D 19.5

STEP B: Do steps 1-10 above to create and enter data for that new table in your database. Write down in your PROJECT REPORT the SQL commands and include the screenshots for every step in the same order and format used above. Your report must have a separate title page with your name and a table of contents pointing to the page # of each step. What to submit ?

Report file in Word, OpenOffice, or PDF format.

Where to submit ?

At Blackboard, under “PROJECTS” and click on “Project # 1” and then upload your file via

“Browse My Computer”.

Deadline: June 14th (hard deadline, late work will not be graded ! )