Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
CS 2541W Database Systems & Team Projects
Laboratory Intro to mySQL
By Roxana Leontie
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.
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
Connecting to MySQLOpen MobaXterm and start local terminal
Use your GW netID to connect to the gwupyterhub.seas.gwu.edu server
Login into MySQL
Reset password
Establish a SSH connection
mysql-uGWnetID-p
Note: use your GW NetID, BUT the password CSCI2541_sp19
SETPASSWORDFOR'GWNetID'@'localhost'='NEWPASSWORD';
MySQL database An existing database is available for your use
Use your database:
showdatabases;
UseDB_name;
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
TablesCreating a table: createtabledeposit(
acctnoint,
custidint,
balancedecimal,
branch_namevarchar(20),
primarykey(acctno),
foreignkey(custid)referencescustomer(custid),
foreignkey(branch_name)referencesbranch(branch_name)
);
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!
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
Constraints
PRIMARY KEY FOREIGN KEY
Create Table Example
showtables;
View table structure: describebranch;
Modifying a table ALTERTABLEdepositADD(deposit_datedate);
ALTERTABLEdepositDROPcolumndeposit_date;
ALTERTABLEcustomerMODIFYCOLUMNnamevarchar(25);
To delete a table: DROPTABLEdeposit;
Tables
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.
INSERT Exampledescribetable_name;
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 !”
Confirm data was added: SELECT
DELETE DELETEFROMbranchWHEREbranch_name='GWU';
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
Submission
https://classroom.github.com/a/UVkuXu3B