21
CS 2541W Database Systems & Team Projects Laboratory Intro to mySQL By Roxana Leontie

CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

CS 2541W Database Systems & Team Projects

Laboratory Intro to mySQL

By Roxana Leontie

Page 2: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

Owen’s PHP form works well. Too

well...

The new report form is great, but now I’m getting to many emails. I need a way to store them and easily find the ones related to Fang.

Page 3: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

MySQL excels at storing data into TABLES

The web server processed web page request, runs PHP scripts, and returns HTML content.

Data

Th e dat a b as e s e r ve r reads and writes data from/to the database. The database itse lf is

often stored as files on a hard drive, but it doesn’t necessarily have to be.

Client browser

MySQL databaseServer computer

Page 4: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

Connecting to MySQLOpen MobaXterm and start local terminal

Page 5: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

Use your GW netID to connect to the gwupyterhub.seas.gwu.edu server

Login into MySQL

Reset password

[email protected]

Establish a SSH connection

mysql-uGWnetID-p

Note: use your GW NetID, BUT the password CSCI2541_sp19

SETPASSWORDFOR'GWNetID'@'localhost'='NEWPASSWORD';

Page 6: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

MySQL database An existing database is available for your use

Use your database:

showdatabases;

UseDB_name;

Page 7: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

Bank Database SchemaBranch_Name Assets Branch_City

Branch

CustID LoanNo Amount Branch_Name

Loan

CustID AccNo Balance Branch_Name

Deposit

CustID Name Street City Zip

Customer

Page 8: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

TablesCreating a table: createtabledeposit(

acctnoint,

custidint,

balancedecimal,

branch_namevarchar(20),

primarykey(acctno),

foreignkey(custid)referencescustomer(custid),

foreignkey(branch_name)referencesbranch(branch_name)

);

Page 9: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

TablesCreating a table: createtabledeposit(

acctnoint,

custidint,

balancedecimal,

branch_namevarchar(20),

primarykey(acctno),

foreignkey(custid)referencescustomer(custid),

foreignkey(branch_name)referencesbranch(branch_name)

);

Note! Tables customer and branchshould to be

created before!

Page 10: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

Basic data types

char(n) Fixed length character string of length n (max 255)

varchar(n) Variable length character string (max 255)

date holds a date field (28-Jan-2013)decimal(n,d) real numbers occupying up to n spaces

with d digits after the decimal pointint(n) integer with up to n digits

Page 11: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

Constraints

PRIMARY KEY FOREIGN KEY

Page 12: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

Create Table Example

showtables;

Page 13: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

View table structure: describebranch;

Modifying a table ALTERTABLEdepositADD(deposit_datedate);

ALTERTABLEdepositDROPcolumndeposit_date;

ALTERTABLEcustomerMODIFYCOLUMNnamevarchar(25);

To delete a table: DROPTABLEdeposit;

Tables

Page 14: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

Populate tables: INSERTThe name of t he table . . . i n O wen’s c a s e , i t w i l l b e aliens_abduction.

The SQL keywords

INSERT INTO begin

the statement

The next part is a list of your table column names, separated by comas. More column names

follow, with no comma after the last one.

Another SQL keyword, this one

signaling that the values for the columns follow.

This next part is a list of the values to be inserted, separated by commas.

Th e s i n g l e q u o t e s a re correct. Use them whenever you’r inserting text, even if it’s a single character!

More quo ted va lue s follow, with no comma after the last one.

IMPORTANT: these need to be in the same order as the column names.

Page 15: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

INSERT Exampledescribetable_name;

Page 16: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

Confirm data was added: SELECT

Follow SELECT with a list of the columns you want data for.

A SELECT always take place with respect to a specific table, not a database in general.

The FROM part of a SELECT statement is how SELECT knows what table we’ll be selecting data from.

The asterisk, or “star”, tells the SELECT statement to get the data for all the columns in the table.

No list of columns is necessary because * means “get them all !”

Page 17: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

Confirm data was added: SELECT

Page 18: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

DELETE DELETEFROMbranchWHEREbranch_name='GWU';

Page 19: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

In-class exercise Create all the tables described in the Bank Database Schema

For each table creation record the create statements and show the description

Run the script provided to populate the tables For the next part record the query and output

Write select statements to show the data in each table Show all the loans that were made at branch GWU Delete all the loans from branch Downtown

Page 20: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

Submission

https://classroom.github.com/a/UVkuXu3B

Page 21: CS 2541W Database Systems & Team Projects Laboratorybhagiweb/cs2541/labs/lab3.pdfIntro to mySQL By Roxana Leontie Owen’s PHP form works well. Too well... The new report form is great,

References

www.w3schools.com

Head first PHP & MySQL book.